cURL / Mailing Lists / curl-library / Single Mail

curl-library

A problem of using CURLOPT_RESOLVE with multi interface

From: Yan Wu <wuyan.ooorg_at_gmail.com>
Date: Wed, 10 Apr 2013 14:22:54 +0800

Hi,

I got a problem when using CURLOPT_RESOLVE with multi interface, I want to
test if my DNS can resolve to more than one different IP. But the result
of the two different IP is same. Actually one IP can't be connected.

With the easy interface, the DNS chat.kele55.com can be resolved to
121.17.125.80, but can't be resolved to 121.17.125.81. I think this is the
right result.

Are there some wrong about my usage of CURLOPT_RESOLVE with multi
interface? Below is my code, I run it on Debian Linux 6, the libcurl is
version 7.28.

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

int main()
{
  char *url = "chat.kele55.com";
  char *ip1 = "121.17.125.80"; // can be connected
  char *ip2 = "121.17.125.81"; // can't be connected

  char redirect1[160] = {0};
  char redirect2[160] = {0};
  sprintf(redirect1, "%s:80:%s", url, ip1);
  sprintf(redirect2, "%s:80:%s", url, ip2);

  struct curl_slist *dns_cache_list1 = curl_slist_append(NULL, redirect1);
  struct curl_slist *dns_cache_list2 = curl_slist_append(NULL, redirect2);

  CURL *ehandle1 = curl_easy_init();
  curl_easy_setopt(ehandle1, CURLOPT_URL, url);
  curl_easy_setopt(ehandle1, CURLOPT_RESOLVE, dns_cache_list1);

  CURL* ehandle2 = curl_easy_init();
  curl_easy_setopt(ehandle2, CURLOPT_URL, url);
  curl_easy_setopt(ehandle2, CURLOPT_RESOLVE, dns_cache_list2);

  CURLM *multi_handle = curl_multi_init();
  curl_multi_add_handle(multi_
handle, ehandle1);
  curl_multi_add_handle(multi_handle, ehandle2);

  int still_running;
  curl_multi_perform(multi_handle, &still_running);

  do {
    struct timeval timeout;
    int rc;

    fd_set fdread;
    fd_set fdwrite;
    fd_set fdexcep;
    int maxfd = -1;

    long curl_timeo = -1;

    FD_ZERO(&fdread);
    FD_ZERO(&fdwrite);
    FD_ZERO(&fdexcep);

    timeout.tv_sec = 50;
    timeout.tv_usec = 0;

    curl_multi_timeout(multi_handle, &curl_timeo);
    if(curl_timeo >= 0) {
      timeout.tv_sec = curl_timeo / 1000;
      if(timeout.tv_sec > 1)
        timeout.tv_sec = 1;
      else
        timeout.tv_usec = (curl_timeo % 1000) * 1000;
    }

    curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);

    rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    switch(rc) {
    case -1:
      fprintf(stderr,"select error");
      break;
    case 0:
      fprintf(stderr, "timeout");
      break;
    default:
      curl_multi_perform(multi_handle, &still_running);
      break;
    }
  } while(still_running);

  CURLMsg *msg;
  int msgs_left;
  CURLcode result = CURLE_NOT_BUILT_IN;
  while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
    if (msg->msg == CURLMSG_DONE)
    {
      result = msg->data.result;
      printf("%d\n", result);
    }
  }

  curl_multi_cleanup(multi_handle);
  curl_easy_cleanup(ehandle1);
  curl_easy_cleanup(ehandle2);
  curl_slist_free_all(dns_cache_list1);
  curl_slist_free_all(dns_cache_list2);

  return 0;
}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-04-10