troubleshooting Question

pthreads to resolve ip's dns name? What's wrong?

Avatar of summer_soccer
summer_soccer asked on
C
8 Comments1 Solution568 ViewsLast Modified:
Hi there,

I wrote a program, but the output file contains incorrect resolved dns names. Can anybody take a look, and point out what's wrong with my program? Thanks.

#include <stdio.h>      // for fprintf()
#include <netdb.h>      // for gethostbyname()
#include <stdlib.h>     // for exit()
#include <unistd.h>     // for fork()
#include <pthread.h>             // for mutex and threads operations

#define MAXIPS  500000
#define MAXTHREADS 150

//struct in_addr  *ipaddr;
char addrstr[256];
char *addrptr[MAXIPS];
char *hostptr[MAXIPS];
int totalips;
int len;
int i;


int totalresolvedips = 0;
pthread_mutex_t lock;


static void *resolver(void *data) {
    int index;
    struct hostent *host;
    struct in_addr *ipaddr;

    while(1) {

        pthread_mutex_lock(&lock);
        if(totalresolvedips < totalips) {
            index = totalresolvedips;
            totalresolvedips++;
            pthread_mutex_unlock(&lock);
            hostptr[index] = (char *)malloc(256);

           // oo a gethostbyaddr() to get a pointer to struct host

           ipaddr=(struct in_addr*)malloc(sizeof(struct in_addr));
           ipaddr->s_addr=inet_addr(addrptr[index]) ;

           host = gethostbyaddr(ipaddr, sizeof(in_addr_t), AF_INET);
           
           printf("Resolved %d out of %d\n", index, totalips);
   
           // output host name if host found
           if(host == NULL) {
               hostptr[index] = "host_no_name";
           }
           else {
               strcpy(hostptr[index], host->h_name);
           }
        }
        else {  // all ips have been resolved
            pthread_mutex_unlock(&lock);

            break;
        }
    }

    return NULL;

}

int main(int argc, char *argv[]) {
   
    FILE *ifp, *ofp;
    pthread_t ths[MAXTHREADS];
    void *retval;

    pthread_mutex_init(&lock, NULL);

    if(argc != 3) {    /* Test for correct number of arguments */
        fprintf(stderr, "Usage: %s <ipfile-to-be-resolved> <ipdnsfile-output>\n", argv[0]);
        exit(1);
    }

    if((ifp = fopen(argv[1], "r")) == NULL) {
        fprintf(stderr, "%s: can't open %s\n", argv[0], argv[1]);
        exit(1);
    }

    if((ofp = fopen(argv[2], "w")) == NULL) {
        fprintf(stderr, "%s: can't open %s\n", argv[0], argv[2]);
        exit(1);
    }

    totalips = 0;

    while(fgets(addrstr, 256, ifp) != NULL) {
        // printf("%s", addrstr);
       
        // remove newline
        len = strlen(addrstr);
       
        if(addrstr[len-1] == '\n')
            addrstr[len-1] = 0;

        addrptr[totalips] = (char *)malloc(256);
        strcpy(addrptr[totalips], addrstr);

        totalips++;
    }

    for(i=0; i<MAXTHREADS; i++) {
        pthread_create(&ths[i], NULL, resolver, 0);
    }

    for(i=0; i<MAXTHREADS; i++) {
        pthread_join(ths[i], &retval);
    }

    for(i=0; i<totalips; i++) {
        fprintf(ofp, "%s\t\t%s\n", addrptr[i], hostptr[i]);
    }

    fclose(ifp);
    fclose(ofp);
ASKER CERTIFIED SOLUTION
avsrivastava

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros