Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <curl/curl.h>
- #define NUMT 32
- CURLSH *shdl[NUMT];
- static pthread_mutex_t dnslock;
- static void lock_cb(CURL *handle, curl_lock_data data,
- curl_lock_access access, void *userptr)
- {
- (void)access; /* unused */
- (void)userptr; /* unused */
- (void)handle; /* unused */
- (void)data; /* unused */
- pthread_mutex_lock(&dnslock);
- }
- static void unlock_cb(CURL *handle, curl_lock_data data,
- void *userptr)
- {
- (void)userptr; /* unused */
- (void)handle; /* unused */
- (void)data; /* unused */
- pthread_mutex_unlock(&dnslock);
- }
- size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
- {
- return size * nmemb;
- }
- static void *pull_one_url(void *arg)
- {
- CURL *curl;
- int i = (int)arg;
- char buf[512];
- char buf2[512];
- char *ip;
- struct curl_slist *slist = NULL;
- curl = curl_easy_init();
- while (1) {
- if (i++ >= NUMT)
- i = 0;
- sprintf(buf, "http://foo:8080/127.0.0.%i", i);
- sprintf(buf2, "foo:8080:127.0.0.%i", i);
- slist = curl_slist_append(slist, buf2);
- curl_easy_reset(curl);
- curl_easy_setopt(curl, CURLOPT_SHARE, shdl[i]);
- curl_easy_setopt(curl, CURLOPT_URL, buf);
- curl_easy_setopt(curl, CURLOPT_RESOLVE, slist);
- curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, 0L);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_perform(curl);
- curl_easy_setopt(curl, CURLOPT_SHARE, NULL);
- curl_slist_free_all(slist);
- slist = NULL;
- curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip);
- printf("connected to %s for 127.0.0.%i\n", ip, i);
- }
- curl_easy_cleanup(curl);
- return NULL;
- }
- int main(int argc, char **argv)
- {
- pthread_t tid[NUMT];
- int i;
- int error;
- curl_global_init(CURL_GLOBAL_ALL);
- pthread_mutex_init(&dnslock, NULL);
- for(i = 0; i< NUMT; i++) {
- shdl[i] = curl_share_init();
- curl_share_setopt(shdl[i], CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
- curl_share_setopt(shdl[i], CURLSHOPT_LOCKFUNC, lock_cb);
- curl_share_setopt(shdl[i], CURLSHOPT_UNLOCKFUNC, unlock_cb);
- }
- for(i = 0; i< NUMT; i++) {
- pthread_create(&tid[i],
- NULL, /* default attributes please */
- pull_one_url,
- (void *)(i+1));
- }
- /* now wait for all threads to terminate */
- for(i = 0; i< NUMT; i++) {
- error = pthread_join(tid[i], NULL);
- fprintf(stderr, "Thread %d terminated\n", i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement