/***** **** *** ** *  *   *    *     *       *     *   *  * ** *** **** *****\

    logresolve 2.0 - http://www.uunet.ca/~tomr/progs/logresolve/

    lr-ip.c - IP number utilities 2.0i1

    Tom Rathborne - tomr@uunet.ca - http://www.uunet.ca/~tomr/

\***** **** *** ** *  *   *    *     *       *     *   *  * ** *** **** *****/

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>

#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->usagecount = 1;
    record->lookuptime = (int) time(NULL);
    ip_cpy(record->ipnum, ipnum);

    if (hostdata = gethostbyaddr((const char *) ipnum, 4, AF_INET)) {
	record->noname = 0;
	record->hostname = (char *) malloc(strlen(hostdata->h_name) + 1);
	strcpy(record->hostname, hostdata->h_name);
    } else {
	record->noname = 1;
	record->hostname = (char *) malloc(IPSTRLEN);
	ip_str(record->hostname, ipnum);
    }

    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);

    if (hostdata = gethostbyaddr((const char *) record->ipnum, 4, AF_INET)) {
	record->noname = 0;
	record->hostname = (char *) malloc(strlen(hostdata->h_name) + 1);
	strcpy(record->hostname, hostdata->h_name);
    } else {
	record->noname = 1;
	record->hostname = (char *) malloc(IPSTRLEN);
	ip_str(record->hostname, record->ipnum);
    }

    return (record);
}

/*
 * ip_hash - hashes an IP number to an integer
 */

int
ip_hash(ipnum, size)
    unsigned char ipnum[4];
    int size;
{
    return ((ipnum[0] + ipnum[1] + ipnum[2] + ipnum[3]) % size);
}

/*** end of lr-ip.c ***/
