Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does libcurl sometimes complain 'Couldn't resolve host name'?

Tags:

curl

libcurl

I write a multithread program used libcurl, But sometimes curl will complain it can`t resolve host name after exec curl_easy_perform, sometimes not.

size_t Http::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    MemoryStruct *mem = (MemoryStruct *)userp;

    mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
    assert(NULL != mem->memory);
    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;

    return realsize;
}

void Http::run(const URL &url, Http::FinishedCallback cbk)
{
    CURL *handle = curl_easy_init();
    if (handle) 
    {

        MemoryStruct *chunk = new MemoryStruct;
        chunk->memory = (char *)malloc(1);  /* will be grown as needed by the realloc above */
        chunk->size = 0;    /* no data at this point */


        CURLcode res; 
        curl_easy_setopt(handle, CURLOPT_URL, url.getUrl().c_str());
        curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)chunk);
        curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
        curl_easy_setopt(handle, CURLOPT_TIMEOUT, 10L);
        curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10L);
        res = curl_easy_perform(handle);

        /* check for errors */
        if (CURLE_OK != res) 
        {
            chunk->status = false;
            std::string errCode = std::to_string(res);
            chunk->memory = (char *)realloc(chunk->memory, chunk->size + errCode.size() + 1);
            memcpy(&(chunk->memory[chunk->size]), errCode.c_str(), errCode.size());
            chunk->size += errCode.size();
            chunk->memory[chunk->size] = 0;
        }
        else 
        {
            chunk->status = true;
        }

#ifdef _DEBUG
        chunk->url = url;
#endif
        cbk(chunk);
        free(chunk);
        curl_easy_cleanup(handle);
    }
}


void Http::get(URL url, FinishedCallback cbk)
{
    std::thread http(&Http::run, Http(), url, cbk);
    http.detach();
}

Http::~Http()
{
    curl_global_cleanup();
}

this is invocer.

int index = 0;
bool finish = false;
void func(Http::MemoryStruct *memo)
{
    if (!memo->status)
    {
#ifdef _DEBUG
        LOG(INFO) << "Failure:\t" << curl_easy_strerror((CURLcode)atoi(memo->memory)) << "\n";

#endif
    }
    else
    {
        //LOG(INFO) << memo->memory << '\n';
    }
    finish = memo->status;
    ++index;
}

int main(void)
{
    curl_global_init(CURL_GLOBAL_ALL);
    URL::AttribMap attribMap{ { "class", "System" }, { "token", "KY0SQ3FX996PU1ZO" }, { "type", "GetConfig" } };
    URL url("open.55.la", "/index.php", attribMap);
    Http http;
    while(index++ <=10)
    {
            try
            {
                http.get(url, func);
            }
            catch (std::_System_error &err)
            {
                LOG(INFO) << err.what();
            }
    }
    while (true)
    {
        ;
    }
    el::Loggers::flushAll();
    return EXIT_SUCCESS;
}

Does It may caused by data collapse?

like image 999
No.6 Avatar asked Feb 05 '23 15:02

No.6


1 Answers

That's the error message libcurl returns for the CURLE_COULDNT_RESOLVE_HOST error code (6).

I'll quote the Exit status section from the curl book

Couldn't resolve host. The given remote host's address was not resolved. The address of the given server could not be resolved. Either the given host name is just wrong, or the DNS server is misbehaving and doesn't know about this name when it should or perhaps even the system you run curl on is misconfigured so that it doesn't find/use the correct DNS server.

If this is returned intermittently for host names that works sometimes and sometimes not, it would indicate you have a broken system somehow, a DNS server that doesn't respond properly or perhaps some sort of DOS prevention wrongly tuned.

like image 156
Daniel Stenberg Avatar answered May 13 '23 04:05

Daniel Stenberg