diff --git a/src/dpchudp.c b/src/dpchudp.c index fd9fbd4..dd35006 100644 --- a/src/dpchudp.c +++ b/src/dpchudp.c @@ -57,6 +57,7 @@ are completely independent. #include #include #include +#include #include "klh10.h" /* Get config params */ @@ -102,6 +103,8 @@ struct in_addr ihost_ip; /* My host IP addr, net order */ void chudptohost(struct dpchudp_s *); void hosttochudp(struct dpchudp_s *); +unsigned char *chip_ipaddr(struct dpchudp_s *, int); +void check_for_typos(struct dpchudp_s *); void net_init(struct dpchudp_s *); void dumppkt(unsigned char *, int); @@ -288,18 +291,17 @@ main(int argc, char **argv) /* Make this a status (rather than debug) printout? */ if (swstatus) { int i; - char ipbuf[OSN_IPSTRSIZ]; - dbprintln("ifc \"%s\" => chaos %lo", dpchudp->dpchudp_ifnam, (long)myaddr); for (i = 0; i < dpchudp->dpchudp_chip_tlen; i++) - dbprintln(" chaos %6o => ip %s:%d", + dbprintln(" chaos %6o => %s:%d", dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_chaddr, - ip_adrsprint(ipbuf, - (unsigned char *)&dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipaddr), + dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_hostname, dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipport); } + check_for_typos(dpchudp); + /* Now start up a child process to handle input */ if (DBGFLG) dbprint("Forking R process"); @@ -542,7 +544,8 @@ chudptohost(register struct dpchudp_s *dpchudp) dbprintln("Updating CHIP entry %d for %o/%d.%d.%d.%d:%d", i, chafrom, ip[0],ip[1],ip[2],ip[3], port); dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipport = port; - memcpy(&dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipaddr, ip, IP_ADRSIZ); + sprintf(dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_hostname, + "%d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]); } /* update timestamp */ dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_lastrcvd = now; @@ -559,7 +562,8 @@ chudptohost(register struct dpchudp_s *dpchudp) dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_chaddr = chafrom; dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipport = port; dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_lastrcvd = now; - memcpy(&dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipaddr, ip, IP_ADRSIZ); + sprintf(dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_hostname, + "%d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]); dpchudp->dpchudp_chip_tlen++; } #if 0 @@ -701,10 +705,11 @@ hi_lookup(struct dpchudp_s *dpchudp, #else (chdest == dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_chaddr) #endif - && dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipaddr.s_addr != 0) { + ) { memcpy(ipport, &dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipport, sizeof(in_port_t)); - memcpy((void *)ipa, &dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_ipaddr, - sizeof(struct in_addr)); + memcpy(ipa, chip_ipaddr(dpchudp, i), sizeof(struct in_addr)); + if (ipa->s_addr == 0) + continue; if (DBGFLG) { char ipbuf[OSN_IPSTRSIZ]; dbprintln("found Chaos address %o at %s:%d", @@ -792,6 +797,40 @@ hi_iproute(struct in_addr *ipa, /* Dest IP addr to be put here */ return FALSE; } +unsigned char * +chip_ipaddr (struct dpchudp_s *dpchudp, int i) +{ + struct dpchudp_chip *chip = &dpchudp->dpchudp_chip_tbl[i]; + static unsigned char zeroes[] = { 0, 0, 0, 0 }; + struct hostent *he = gethostbyname(chip->dpchudp_chip_hostname); + + if (he == NULL) + { + return zeroes; + } + else if (he->h_addrtype != AF_INET || he->h_length != IP_ADRSIZ) + { + error("CH11 CHIP spec found non-IPv4 address"); + return zeroes; + } + + memcpy(&chip->dpchudp_chip_ipaddr, he->h_addr, IP_ADRSIZ); + return (unsigned char *)&chip->dpchudp_chip_ipaddr.s_addr; +} + +void +check_for_typos(struct dpchudp_s *dpchudp) +{ + unsigned char *ipa; + int i; + for (i = 0; i < dpchudp->dpchudp_chip_tlen; i++) + { + ipa = chip_ipaddr(dpchudp, i); + if (ipa[0]==0 && ipa[1]==0 && ipa[2]==0 && ipa[3]==0) + error("CH11 Chaos/IP mapping hostname %s invalid", + dpchudp->dpchudp_chip_tbl[i].dpchudp_chip_hostname); + } +} /* IP_WRITE - Send IP packet out over UDP */ diff --git a/src/dpchudp.h b/src/dpchudp.h index 9a73575..4166878 100644 --- a/src/dpchudp.h +++ b/src/dpchudp.h @@ -31,6 +31,9 @@ #ifndef DPCHUDP_CHIP_MAX # define DPCHUDP_CHIP_MAX 20 #endif +#ifndef DPCHUDP_CHIP_HOSTNAME_MAX +# define DPCHUDP_CHIP_HOSTNAME_MAX 100 +#endif /* If a dynamically added CHIP entry is older than this (seconds), it can get updated */ #ifndef DPCHUDP_CHIP_DYNAMIC_AGE_LIMIT @@ -43,6 +46,7 @@ struct dpchudp_chip { struct in_addr dpchudp_chip_ipaddr; /* IP address */ in_port_t dpchudp_chip_ipport; /* IP port */ time_t dpchudp_chip_lastrcvd; /* When last received, if dynamically added */ + char dpchudp_chip_hostname[DPCHUDP_CHIP_HOSTNAME_MAX+1]; }; /* DPCHUDP-specific stuff */ diff --git a/src/dvch11.c b/src/dvch11.c index fbaf175..daac6f4 100644 --- a/src/dvch11.c +++ b/src/dvch11.c @@ -54,17 +54,8 @@ static int decosfcclossage; #include "prmstr.h" /* For parameter parsing */ #include "dvuba.h" #include "dvch11.h" - #include "dpchudp.h" -#ifndef KLH10_CH11_USE_GETHOSTBYNAME -# define KLH10_CH11_USE_GETHOSTBYNAME 1 -#endif - -#if KLH10_CH11_USE_GETHOSTBYNAME -# include -#endif - #define CHUDPBUFSIZ (DPCHUDP_MAXLEN+500) /* Plenty of slop */ @@ -85,8 +76,8 @@ static int decosfcclossage; /* Chaos/IP mapping entry - see dpchudp_chip */ struct ch_chip { unsigned int ch_chip_chaddr; /* Chaos address */ - struct in_addr ch_chip_ipaddr; /* IP address */ in_port_t ch_chip_ipport; /* IP port */ + const char *ch_chip_hostname; }; struct ch11 { @@ -316,8 +307,7 @@ ch11_conf(FILE *f, char *s, struct ch11 *ch) break; else { unsigned long cha; - unsigned char ipa[IP_ADRSIZ]; - struct hostent *he; + const char *host; in_port_t ipp = CHUDP_PORT; struct ch_chip *chip; int idx; @@ -337,31 +327,18 @@ ch11_conf(FILE *f, char *s, struct ch11 *ch) } *c = '\0'; /* zap for IP parsing */ } -#if KLH10_CH11_USE_GETHOSTBYNAME - if ((he = gethostbyname(s)) == NULL) -#else - if (!parip(s, &ipa[0])) -#endif + host = strdup(s); + if (strlen(s) > DPCHUDP_CHIP_HOSTNAME_MAX) { *(--s) = '/'; /* put back slash */ if (c) *c = ':'; /* and colon */ break; /* and complain */ } -#if KLH10_CH11_USE_GETHOSTBYNAME - if ((he->h_addrtype != AF_INET) || (he->h_length != IP_ADRSIZ)) { - fprintf(stderr,"CH11 CHIP spec found non-IPv4 address"); - break; - } -#endif idx = (ch->ch_chip_tlen); /* new index */ ch->ch_chip_tbl[idx].ch_chip_chaddr = cha; ch->ch_chip_tbl[idx].ch_chip_ipport = ipp; -#if KLH10_CH11_USE_GETHOSTBYNAME - memcpy(&ch->ch_chip_tbl[idx].ch_chip_ipaddr, he->h_addr, IP_ADRSIZ); -#else - memcpy(&ch->ch_chip_tbl[idx].ch_chip_ipaddr, &ipa[0], IP_ADRSIZ); -#endif + ch->ch_chip_tbl[idx].ch_chip_hostname = host; ch->ch_chip_tlen = idx+1; /* update length */ } continue; @@ -500,8 +477,6 @@ static void ch11_cmd_chiptable(struct ch11 *ch, FILE *of) { int n, i; - unsigned char *ip; - char ipa[4*4]; struct tm *ltime; char last[128]; struct dpchudp_chip *chip; @@ -516,16 +491,14 @@ ch11_cmd_chiptable(struct ch11 *ch, FILE *of) fprintf(of,"Chaos IP Port Last received\n"); for (i = 0; i < n; i++) { chip = &dpc->dpchudp_chip_tbl[i]; - ip = (unsigned char *)&chip->dpchudp_chip_ipaddr.s_addr; - sprintf(ipa,"%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); if (chip->dpchudp_chip_lastrcvd != 0) { ltime = localtime(&chip->dpchudp_chip_lastrcvd); strftime(last, sizeof(last), "%Y-%m-%d %T", ltime); } else strcpy(last,"[static]"); - fprintf(of,"%6o %-15s %d. %s\n", + fprintf(of,"%6o %-40s %d. %s\n", chip->dpchudp_chip_chaddr, - ipa, + chip->dpchudp_chip_hostname, chip->dpchudp_chip_ipport, last); } @@ -1085,8 +1058,10 @@ chudp_init(register struct ch11 *ch, FILE *of) memset(&dpc->dpchudp_chip_tbl[junk], 0, sizeof(struct dpchudp_chip)); dpc->dpchudp_chip_tbl[junk].dpchudp_chip_chaddr = ch->ch_chip_tbl[junk].ch_chip_chaddr; dpc->dpchudp_chip_tbl[junk].dpchudp_chip_ipport = ch->ch_chip_tbl[junk].ch_chip_ipport; - memcpy(&dpc->dpchudp_chip_tbl[junk].dpchudp_chip_ipaddr, - &ch->ch_chip_tbl[junk].ch_chip_ipaddr, sizeof(struct in_addr)); + memset(&dpc->dpchudp_chip_tbl[junk].dpchudp_chip_ipaddr, + 0, sizeof(struct in_addr)); + strcpy(dpc->dpchudp_chip_tbl[junk].dpchudp_chip_hostname, + ch->ch_chip_tbl[junk].ch_chip_hostname); } dpc->dpchudp_chip_tlen = ch->ch_chip_tlen;