1 /* 2 * Copyright (c) 1995 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * ethernet address conversion and lookup routines 33 * 34 * Written by Bill Paul <wpaul@ctr.columbia.edu> 35 * Center for Telecommunications Research 36 * Columbia University, New York City 37 * 38 * $Id: ether_addr.c,v 1.5 1995/03/26 02:37:00 wpaul Exp $ 39 */ 40 41 42 #include <stdio.h> 43 #include <paths.h> 44 #include <sys/types.h> 45 #include <string.h> 46 #include <stdlib.h> 47 #include <sys/param.h> 48 #include <sys/socket.h> 49 #include <net/if.h> 50 #include <netinet/in.h> 51 #include <netinet/if_ether.h> 52 #ifdef YP 53 #include <rpc/rpc.h> 54 #include <rpcsvc/yp_prot.h> 55 #include <rpcsvc/ypclnt.h> 56 #endif 57 58 #ifndef _PATH_ETHERS 59 #define _PATH_ETHERS "/etc/ethers" 60 #endif 61 62 /* 63 * Parse a string of text containing an ethernet address and hostname 64 * and separate it into its component parts. 65 */ 66 int ether_line(l, e, hostname) 67 char *l; 68 struct ether_addr *e; 69 char *hostname; 70 { 71 int i, o[6]; 72 73 i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2], 74 &o[3], &o[4], &o[5], 75 hostname); 76 if (i != 7) 77 return (i); 78 79 for (i=0; i<6; i++) 80 e->octet[i] = o[i]; 81 return (0); 82 } 83 84 /* 85 * Convert an ASCII representation of an ethernet address to 86 * binary form. 87 */ 88 struct ether_addr *ether_aton(a) 89 char *a; 90 { 91 int i; 92 static struct ether_addr o; 93 94 i = sscanf(a, "%x:%x:%x:%x:%x:%x", o.octet[0], o.octet[1], o.octet[2], 95 o.octet[3], o.octet[4], o.octet[5]); 96 if (i != 6) 97 return (NULL); 98 return ((struct ether_addr *)&o); 99 } 100 101 /* 102 * Convert a binary representation of an ethernet address to 103 * an ASCII string. 104 */ 105 char *ether_ntoa(n) 106 struct ether_addr *n; 107 { 108 int i; 109 static char a[18]; 110 111 i = sprintf(a,"%x:%x:%x:%x:%x:%x",n->octet[0],n->octet[1],n->octet[2], 112 n->octet[3],n->octet[4],n->octet[5]); 113 if (i < 11) 114 return (NULL); 115 return ((char *)&a); 116 } 117 118 /* 119 * Map an ethernet address to a hostname. Use either /etc/ethers or 120 * NIS/YP. 121 */ 122 123 int ether_ntohost(hostname, e) 124 char *hostname; 125 struct ether_addr *e; 126 { 127 FILE *fp; 128 char buf[BUFSIZ]; 129 struct ether_addr local_ether; 130 char local_host[MAXHOSTNAMELEN]; 131 #ifdef YP 132 char *result; 133 int resultlen; 134 char *ether_a; 135 char *yp_domain; 136 #endif 137 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) 138 return (1); 139 140 while (fgets(buf,BUFSIZ,fp)) { 141 if (buf[0] == '#') 142 continue; 143 #ifdef YP 144 if (buf[0] == '+') { 145 fclose(fp); /* Can ignore /etc/ethers from here on. */ 146 if (yp_get_default_domain(&yp_domain)) 147 return(1); 148 ether_a = ether_ntoa(e); 149 if (yp_match(yp_domain, "ethers.byaddr", ether_a, 150 strlen(ether_a), &result, &resultlen)) { 151 free(result); 152 return(1); 153 } 154 if (!ether_line(result, &local_ether, &local_host)) { 155 strcpy(hostname, (char *)&local_host); 156 free(result); 157 return(0); 158 } else { 159 free(result); 160 return(1); 161 } 162 } 163 #endif 164 if (!ether_line(&buf, &local_ether, &local_host)) { 165 if (!bcmp((char *)&local_ether.octet[0], 166 (char *)&e->octet[0], 6)) { 167 /* We have a match */ 168 strcpy(hostname, (char *)&local_host); 169 fclose(fp); 170 return(0); 171 } 172 } 173 } 174 fclose(fp); 175 return (1); 176 } 177 178 /* 179 * Map a hostname to an ethernet address using /etc/ethers or 180 * NIS/YP. 181 */ 182 int ether_hostton(hostname, e) 183 char *hostname; 184 struct ether_addr *e; 185 { 186 FILE *fp; 187 char buf[BUFSIZ]; 188 struct ether_addr local_ether; 189 char local_host[MAXHOSTNAMELEN]; 190 #ifdef YP 191 char *result; 192 int resultlen; 193 char *yp_domain; 194 #endif 195 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) 196 return (1); 197 198 while (fgets(buf,BUFSIZ,fp)) { 199 if (buf[0] == '#') 200 continue; 201 #ifdef YP 202 if (buf[0] == '+') { 203 fclose(fp); /* Can ignore /etc/ethers from here on. */ 204 if (yp_get_default_domain(&yp_domain)) 205 return(1); 206 if (yp_match(yp_domain, "ethers.byname", hostname, 207 strlen(hostname), &result, &resultlen)) { 208 free(result); 209 return(1); 210 } 211 if (!ether_line(result, &local_ether, &local_host)) { 212 bcopy((char *)&local_ether.octet[0], 213 (char *)&e->octet[0], 6); 214 free(result); 215 return(0); 216 } else { 217 free(result); 218 return(1); 219 } 220 } 221 #endif 222 if (!ether_line(&buf, &local_ether, &local_host)) { 223 if (!strcmp(hostname, (char *)&local_host)) { 224 /* We have a match */ 225 bcopy((char *)&local_ether.octet[0], 226 (char *)&e->octet[0], 6); 227 fclose(fp); 228 return(0); 229 } 230 } 231 } 232 fclose(fp); 233 return (1); 234 } 235