cURL / Mailing Lists / curl-library / Single Mail


RE: HTTP URL sent to wrong IP -- cache problem?

From: Jim Freeman <>
Date: Mon, 13 Apr 2009 17:03:59 -0700

Daniel Stenberg wrote:
> So can you provide us with a full example source code to a
> small app that seems to show this problem for you?

My sequence of steps listed in my first email didn't actually match my
code. Step 6 should have said: Do 'connect only", then do HTTP GET
using socket I/O.

Here's a very stripped down example that will always fail the 2nd time
through the loop and send the hello.txt GET to the wrong IP. IP
addresses are hard coded just to eliminate DNS caching as a contributor
to the problem. The getpeername() call is set for Windows; I believe
the arg type differs for Linux? Or you can just delete the
getpeername() block and just use a net sniffer to see the hello.txt GET
going to the wrong IP.

Tested in Windows 2003 Server in curl 7.19.2 and 7.19.4.

#include <assert.h>
#include <stdio.h>
#include <curl.h>

int main()
    CURLcode curlRes;

    CURL* curl = curl_easy_init();

    int i;
    for (i = 0; i < 2; i++)
        printf("* 1 **************\n");
        curl_easy_setopt(curl, CURLOPT_URL, "");

        curlRes = curl_easy_perform(curl);
        assert(curlRes == CURLE_OK);

        printf("* #2 **************\n");
        curl_easy_setopt(curl, CURLOPT_URL, "");
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);

        curlRes = curl_easy_perform(curl);
        assert(curlRes == CURLE_OK);

        // ask curl for IP address
        char* ip1;
        curlRes = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip1);
        assert(curlRes == CURLE_OK);
        // get raw socket handle
        int sockfd;
        curlRes = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);
        assert(curlRes == CURLE_OK);

        // ask socket for IP address
        sockaddr sockad;
        int len = sizeof(sockad);
        int rc = getpeername(sockfd, &sockad, &len);
        char ip2[20];
        sprintf(ip2, "%u.%u.%u.%u", (unsigned char)sockad.sa_data[2],
(unsigned char)sockad.sa_data[3], (unsigned char)sockad.sa_data[4],
(unsigned char)sockad.sa_data[5] );

        // Send HTTP GET request.
        const char* request = "GET /hello.txt HTTP/1.1\r\nHost:\r\n\r\n"; //
        size_t sentCount = 0;
        curlRes = curl_easy_send(curl, request, strlen(request),
        assert(sentCount == strlen(request));
        assert(curlRes == CURLE_OK);

        if (strcmp(ip1, ip2))
            printf("FAILED. getpeername() returns %s, but
CURLINFO_PRIMARY_IP returns %s\n", ip2, ip1);
    if (i == 2)

    return 0;


This electronic mail message, including any attachments or embedded documents, is intended
only for the addressee and may contain privileged and/or confidential information. Unless
expressly indicated, this electronic mail message is confidential and privileged information
of Nirvanix, Inc. Any use, dissemination, copying or distribution in any form, and any action
taken based up this email is strictly prohibited unless expressly indicated. If you have
received this message by error, please immediately delete it and any copies of it, including
any attachments hereto, and notify the sender at Nirvanix, Inc. by reply electronic mail
message or phone. Thank you.
Received on 2009-04-14