/***** **** *** ** * * * * * * * * * ** *** **** *****\ logresolve 2.0 - http://www.net/~tomr/progs/logresolve/ lr-ip.c - IP number utilities 2.0i2 Tom Rathborne - tomr@uunet.ca - http://www.net/~tomr/ DEVELOPMENT NOTES: extern int h_errno is to be interpreted thusly: NETDB_INTERNAL see errno NETDB_SUCCESS no problem HOST_NOT_FOUND Authoritative Answer Host not found TRY_AGAIN Non-Authoritive Host not found, or SERVERFAIL NO_RECOVERY Non recoverable errors, FORMERR, REFUSED, NOTIMP NO_DATA Valid name, no data record of requested type NO_ADDRESS no address, look for MX record \***** **** *** ** * * * * * * * * * ** *** **** *****/ #include #include #include #include #include #include #include #include "lr-ip.h" /* * ip_cmp - compares two IP numbers and returns TRUE if they're the same */ int ip_cmp(ipnum1, ipnum2) unsigned char ipnum1[4]; unsigned char ipnum2[4]; { return (ipnum1[0] == ipnum2[0] && ipnum1[1] == ipnum2[1] && ipnum1[2] == ipnum2[2] && ipnum1[3] == ipnum2[3]); } /* * ip_cpy - copies second IP to first. */ void ip_cpy(ipnum1, ipnum2) unsigned char ipnum1[4]; unsigned char ipnum2[4]; { ipnum1[0] = ipnum2[0]; ipnum1[1] = ipnum2[1]; ipnum1[2] = ipnum2[2]; ipnum1[3] = ipnum2[3]; } /* * ip_build - makes an IP number char array from 4 integers */ void ip_build(ipnum, a, b, c, d) unsigned char ipnum[4]; unsigned int a, b, c, d; { ipnum[0] = a; ipnum[1] = b; ipnum[2] = c; ipnum[3] = d; } /* * ip_str - converts an IP number char array to a string */ char * ip_str(string, ipnum) char *string; unsigned char ipnum[4]; { sprintf(string, "%d.%d.%d.%d", ipnum[0], ipnum[1], ipnum[2], ipnum[3]); return (string); } /* * str_ip - converts a string to an IP number char array */ int str_ip(string, ipnum) char *string; unsigned char ipnum[4]; { char str[MAXLINE], *foo, *bar; unsigned char tnum[4]; int i, ip = TRUE, baz; strncpy(str, string, MAXLINE); foo = str; bar = str; for (i = 0; (i < 4) && ip; i++) { while (*bar && *bar != '.' && *bar != ' ') bar++; *bar = 0; baz = atoi(foo); if (baz > 255) ip = FALSE; tnum[i] = baz; foo = ++bar; if (((*bar < '0') || (*bar > '9')) && i < 3) ip = FALSE; } if (ip) ip_cpy(ipnum, tnum); return (ip); } /* * ip_lookup - looks up hostname associated with IP number, returns new record */ struct nsrec * ip_lookup(ipnum) unsigned char ipnum[4]; { struct hostent *hostdata; struct nsrec *record; record = nsrec_new(); record->lookuptime = (int) time(NULL); ip_cpy(record->ipnum, ipnum); hostdata = gethostbyaddr((const char *) ipnum, 4, AF_INET); record->status = h_errno; if (hostdata) { record->hostname = (char *) malloc(strlen(hostdata->h_name) + 1); strcpy(record->hostname, hostdata->h_name); } else { record->hostname = (char *) malloc(IPSTRLEN); ip_str(record->hostname, ipnum); } record->tries++; return (record); } /* * ip_relookup - looks up an IP number, alters old record */ struct nsrec * ip_relookup(record) struct nsrec *record; { struct hostent *hostdata; free(record->hostname); record->lookuptime = (int) time(NULL); hostdata = gethostbyaddr((const char *) record->ipnum, 4, AF_INET); record->status = h_errno; if (hostdata) { record->hostname = (char *) malloc(strlen(hostdata->h_name) + 1); strcpy(record->hostname, hostdata->h_name); } else { record->hostname = (char *) malloc(IPSTRLEN); ip_str(record->hostname, record->ipnum); } record->tries++; return (record); } /* * ip_hash - hashes an IP number to an integer */ int ip_hash(ipnum, size) unsigned char ipnum[4]; int size; { /* * Used to do this: * return ((ipnum[0] + ipnum[1] + ipnum[2] + ipnum[3]) % size); * ... which is silly, since the sum is always <= 1024. *duh* * * I hope this new function is a little better for large hash tables: */ return (((ipnum[0] ^ ipnum[2]) + ((ipnum[1] ^ ipnum[3]) << 8)) % size); } /*** end of lr-ip.c ***/