From a7d260fe54e3d7b22ab803370579706759b72f8b Mon Sep 17 00:00:00 2001 From: wpaul Date: Sun, 5 Mar 1995 22:04:05 +0000 Subject: [PATCH] ether_addr.c: - Implement ether_hostton() - Implement ether_aton() - Modify ether_aton() and ether_ntoa() to match the semantics of the SunOS versions of these functions. - Neaten up ether_hostton() and ether_ntohost() a little. - Get rid of ether_print() since it isn't needed for rarpd and it isn't documented as a standard ethers(5) function. rarpd.8: - Make it clear that the 'ipaddr' that rarpd looks for in /tftpboot is actually in hexadecimal (as in /tftpboot/803B4032) since those who are not versed in the black art of system administration are not likely to know this. --- usr.sbin/rarpd/ether_addr.c | 166 +++++++++++++++++++++++++----------- usr.sbin/rarpd/rarpd.8 | 4 +- 2 files changed, 116 insertions(+), 54 deletions(-) diff --git a/usr.sbin/rarpd/ether_addr.c b/usr.sbin/rarpd/ether_addr.c index bfff4d721e2f..87d5e700a2c5 100644 --- a/usr.sbin/rarpd/ether_addr.c +++ b/usr.sbin/rarpd/ether_addr.c @@ -1,5 +1,5 @@ /* - * rarpd support routines + * ethernet address conversion and lookup routines * * Written by Bill Paul * Center for Telecommunications Research @@ -13,7 +13,11 @@ * $Id: ether_addr.c,v 1.2 1995/03/03 22:20:15 wpaul Exp $ */ + #include +#include +#include +#include #ifndef _PATH_ETHERS #define _PATH_ETHERS "/etc/ethers" @@ -26,6 +30,10 @@ struct ether_addr { unsigned char octet[6]; }; +extern int yp_get_default_domain(); +extern int yp_match(); +char *yp_domain; + /* * Parse a string of text containing an ethernet address and hostname * and separate it into its component parts. @@ -41,13 +49,47 @@ int ether_line(l, e, hostname) &o[3], &o[4], &o[5], hostname); if (i != 7) - return (-1); + return (i); for (i=0; i<6; i++) e->octet[i] = o[i]; return (0); } +/* + * Convert an ASCII representation of an ethernet address to + * binary form. + */ +struct ether_addr *ether_aton(a) + char *a; +{ + int i; + static struct ether_addr o; + + i = sscanf(a, "%x:%x:%x:%x:%x:%x", o.octet[0], o.octet[1], o.octet[2], + o.octet[3], o.octet[4], o.octet[5]); + if (i != 6) + return (NULL); + return ((struct ether_addr *)&o); +} + +/* + * Convert a binary representation of an ethernet address to + * an ASCII string. + */ +char *ether_ntoa(n) + struct ether_addr *n; +{ + int i; + static char a[18]; + + i = sprintf(a,"%x:%x:%x:%x:%x:%x",n->octet[0],n->octet[1],n->octet[2], + n->octet[3],n->octet[4],n->octet[5]); + if (i < 11) + return (NULL); + return ((char *)&a); +} + /* * Map an ethernet address to a hostname. Use either /etc/ethers or * NIS/YP. @@ -58,72 +100,92 @@ int ether_ntohost(hostname, e) struct ether_addr *e; { FILE *fp; - static char buf[BUFSIZ]; - static struct ether_addr local_ether; - static char *local_host; - static char *result; - int resultlen, i; - extern int yp_get_default_domain(); - extern int yp_match(); - static char *yp_domain; - static char ether_a[BUFSIZ]; + char buf[BUFSIZ]; + struct ether_addr local_ether; + char local_host[MAXHOSTNAMELEN]; + char *result; + int resultlen; + char *ether_a; - if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) { - perror(_PATH_ETHERS); - return (-1); - } + if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) + return (1); while (fgets(buf,BUFSIZ,fp)) { + if (buf[0] == '#') + continue; if (buf[0] == '+') { + fclose(fp); /* Can ignore /etc/ethers from here on. */ if (yp_get_default_domain(&yp_domain)) - return(-1); - sprintf(ether_a,"%x:%x:%x:%x:%x:%x", - e->octet[0], e->octet[1], - e->octet[2], e->octet[3], - e->octet[4], e->octet[5]); + return(1); + ether_a = ether_ntoa(e); if (yp_match(yp_domain, "ethers.byaddr", ether_a, - strlen(ether_a),&result, &resultlen)) - return(-1); - if (ether_line(result, &local_ether, - &local_host) == 0) { + strlen(ether_a), &result, &resultlen)) + return(1); + if (!ether_line(result, &local_ether, &local_host)) { strcpy(hostname, (char *)&local_host); return(0); } else - return(-1); + return(1); } - if (ether_line(&buf, &local_ether, &local_host) == 0) { - for (i = 0; i < 6; i++) - if (local_ether.octet[i] != e->octet[i]) - goto nomatch; + if (!ether_line(&buf, &local_ether, &local_host)) { + if (!bcmp((char *)&local_ether.octet[0], + (char *)&e->octet[0], 6)) { /* We have a match */ - strcpy(hostname, (char *)&local_host); - fclose(fp); - return(0); + strcpy(hostname, (char *)&local_host); + fclose(fp); + return(0); + } } -nomatch: } - -return (-1); +fclose(fp); +return (1); } -int ether_print(cp) - unsigned char *cp; +/* + * Map a hostname to an ethernet address using /etc/ethers or + * NIS/YP. + */ +int ether_hostton(hostname, e) + char *hostname; + struct ether_addr *e; { - printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); -} + FILE *fp; + char buf[BUFSIZ]; + struct ether_addr local_ether; + char local_host[MAXHOSTNAMELEN]; + char *result; + int resultlen; -int ether_aton(a, n) - char *a; - unsigned char *n; -{ - int i, o[6]; + if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) + return (1); - i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], - &o[3], &o[4], &o[5]); - if (i != 6) { - return (i); - } - for (i=0; i<6; i++) - n[i] = o[i]; - return (0); + while (fgets(buf,BUFSIZ,fp)) { + if (buf[0] == '#') + continue; + if (buf[0] == '+') { + fclose(fp); /* Can ignore /etc/ethers from here on. */ + if (yp_get_default_domain(&yp_domain)) + return(1); + if (yp_match(yp_domain, "ethers.byname", hostname, + strlen(hostname), &result, &resultlen)) + return(1); + if (!ether_line(result, &local_ether, &local_host)) { + bcopy((char *)&local_ether.octet[0], + (char *)&e->octet[0], 6); + return(0); + } else + return(1); + } + if (!ether_line(&buf, &local_ether, &local_host)) { + if (!strcmp(hostname, (char *)&local_host)) { + /* We have a match */ + bcopy((char *)&local_ether.octet[0], + (char *)&e->octet[0], 6); + fclose(fp); + return(0); + } + } + } +fclose(fp); +return (1); } diff --git a/usr.sbin/rarpd/rarpd.8 b/usr.sbin/rarpd/rarpd.8 index c19bc304742e..73cd0100d736 100644 --- a/usr.sbin/rarpd/rarpd.8 +++ b/usr.sbin/rarpd/rarpd.8 @@ -1,4 +1,4 @@ -.\" @(#) $Header: tcpdump.1,v 1.30 90/10/11 19:35:03 mccanne Exp $ (LBL) +.\" @(#) $Header: /a/ncvs/src/usr.sbin/rarpd/rarpd.8,v 1.1.1.1 1995/03/02 06:41:40 wpaul Exp $ (LBL) .\" .\" Copyright (c) 1988-1990 The Regents of the University of California. .\" All rights reserved. @@ -53,7 +53,7 @@ Additionally, a request is honored only if the server (i.e., the host that rarpd is running on) can "boot" the target; that is, if the directory /tftpboot/\fIipaddr\fP -exists, where \fIipaddr\fP is the target IP address. +exists, where \fIipaddr\fP is the target IP address in hexadecimal. In normal operation, .I rarpd