Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <curl/curl.h>
  4.  
  5. #define NUMT 32
  6.  
  7. CURLSH *shdl[NUMT];
  8.  
  9. static pthread_mutex_t dnslock;
  10.  
  11. static void lock_cb(CURL *handle, curl_lock_data data,
  12.         curl_lock_access access, void *userptr)
  13. {
  14.     (void)access; /* unused */
  15.     (void)userptr; /* unused */
  16.     (void)handle; /* unused */
  17.     (void)data; /* unused */
  18.     pthread_mutex_lock(&dnslock);
  19. }
  20.  
  21. static void unlock_cb(CURL *handle, curl_lock_data data,
  22.         void *userptr)
  23. {
  24.     (void)userptr; /* unused */
  25.     (void)handle;  /* unused */
  26.     (void)data;    /* unused */
  27.     pthread_mutex_unlock(&dnslock);
  28. }
  29.  
  30. size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
  31. {
  32.     return size * nmemb;
  33. }
  34.  
  35. static void *pull_one_url(void *arg)
  36. {
  37.     CURL *curl;
  38.     int i = (int)arg;
  39.     char buf[512];
  40.     char buf2[512];
  41.     char *ip;
  42.     struct curl_slist *slist = NULL;
  43.  
  44.     curl = curl_easy_init();
  45.     while (1) {
  46.         if (i++ >= NUMT)
  47.             i = 0;
  48.         sprintf(buf, "http://foo:8080/127.0.0.%i", i);
  49.         sprintf(buf2, "foo:8080:127.0.0.%i", i);
  50.         slist = curl_slist_append(slist, buf2);
  51.         curl_easy_reset(curl);
  52.         curl_easy_setopt(curl, CURLOPT_SHARE, shdl[i]);
  53.         curl_easy_setopt(curl, CURLOPT_URL, buf);
  54.         curl_easy_setopt(curl, CURLOPT_RESOLVE, slist);
  55.         curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, 0L);
  56.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  57.  
  58.         curl_easy_perform(curl);
  59.  
  60.         curl_easy_setopt(curl, CURLOPT_SHARE, NULL);
  61.         curl_slist_free_all(slist);
  62.         slist = NULL;
  63.         curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip);
  64.         printf("connected to %s for 127.0.0.%i\n", ip, i);
  65.     }
  66.     curl_easy_cleanup(curl);
  67.     return NULL;
  68. }
  69.  
  70. int main(int argc, char **argv)
  71. {
  72.     pthread_t tid[NUMT];
  73.     int i;
  74.     int error;
  75.  
  76.     curl_global_init(CURL_GLOBAL_ALL);
  77.     pthread_mutex_init(&dnslock, NULL);
  78.  
  79.     for(i = 0; i< NUMT; i++) {
  80.         shdl[i] = curl_share_init();
  81.         curl_share_setopt(shdl[i], CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  82.         curl_share_setopt(shdl[i], CURLSHOPT_LOCKFUNC, lock_cb);
  83.         curl_share_setopt(shdl[i], CURLSHOPT_UNLOCKFUNC, unlock_cb);
  84.     }
  85.     for(i = 0; i< NUMT; i++) {
  86.         pthread_create(&tid[i],
  87.                 NULL, /* default attributes please */
  88.                 pull_one_url,
  89.                 (void *)(i+1));
  90.     }
  91.  
  92.     /* now wait for all threads to terminate */
  93.     for(i = 0; i< NUMT; i++) {
  94.         error = pthread_join(tid[i], NULL);
  95.         fprintf(stderr, "Thread %d terminated\n", i);
  96.     }
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement