简体   繁体   中英

DNS lookups using pthreads in C

I am writing a program that parses HTML, however, while it parses multiple HTML files, I need to perform DNS lookups on a collection of IPs. I was thinking to use pthreads for the lookups task.

Would you recommend doing it this way? Do I need more than one thread for this task? What are some of the potential problems I could run into? Any feedback is appreciated.

This is what I was thinking...

#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void *ip2host(void *ips[][2]){
    struct hostent *hent;
    struct in_addr addr;
    int i;
    for (i=0;i<3;i++) {
        if(!inet_aton(ips[i][0], &addr))
            return NULL;

        if((hent = gethostbyaddr((char *)&(addr.s_addr), sizeof(addr.s_addr), AF_INET))){
            ips[i][1] = malloc (strlen (hent->h_name) + 1);
            strcpy(ips[i][1], hent->h_name);
        }
    }
    return NULL;
}

int main(){
    char *ips[][2] = {
        {"199.21.99.110", NULL},
        {"66.249.73.55", NULL},
        {"74.125.225.34", NULL}
    };

    pthread_t thread1;
    if(pthread_create(&thread1, NULL, ip2host, &ips)) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    // parse html files
    int y = 0;
    while(++y < 100000);
    printf("y increment finished\n");

    if(pthread_join(thread1, NULL)) {
        fprintf(stderr, "Error joining thread\n");
        return 1;
    }
    int i;
    for(i=0; i<3; i++) {
        printf("%s\n", ips[i][1]);
    }
    return 0;
}

Just consider the DNS lookup as part of the connection process and do it before the connect() . Thats where you need it anyway, whats the point in doing it in another thread if the IP may not be ready at the moment you need it?

Remember connect() will hang your thread until the connection is stabilished too, so resolving IPs is not the only time expending stuff here.

Also, don't worry about caching DNS resolutions, the system itself will take care of that for you.

gethostbyaddr is not thread safe because it returns a pointer to a static struct. If you're using gcc, you can use gethostbyaddr_r which is a thread-safe extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM