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$ 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/ethernet.h> 50 #ifdef YP 51 #include <rpc/rpc.h> 52 #include <rpcsvc/yp_prot.h> 53 #include <rpcsvc/ypclnt.h> 54 #endif 55 56 #ifndef _PATH_ETHERS 57 #define _PATH_ETHERS "/etc/ethers" 58 #endif 59 60 /* 61 * Parse a string of text containing an ethernet address and hostname 62 * and separate it into its component parts. 63 */ 64 int ether_line(l, e, hostname) 65 char *l; 66 struct ether_addr *e; 67 char *hostname; 68 { 69 int i, o[6]; 70 71 i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2], 72 &o[3], &o[4], &o[5], 73 hostname); 74 if (i != 7) 75 return (i); 76 77 for (i=0; i<6; i++) 78 e->octet[i] = o[i]; 79 return (0); 80 } 81 82 /* 83 * Convert an ASCII representation of an ethernet address to 84 * binary form. 85 */ 86 struct ether_addr *ether_aton(a) 87 char *a; 88 { 89 int i; 90 static struct ether_addr o; 91 unsigned int o0, o1, o2, o3, o4, o5; 92 93 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5); 94 95 if (i != 6) 96 return (NULL); 97 98 o.octet[0]=o0; 99 o.octet[1]=o1; 100 o.octet[2]=o2; 101 o.octet[3]=o3; 102 o.octet[4]=o4; 103 o.octet[5]=o5; 104 105 return ((struct ether_addr *)&o); 106 } 107 108 /* 109 * Convert a binary representation of an ethernet address to 110 * an ASCII string. 111 */ 112 char *ether_ntoa(n) 113 struct ether_addr *n; 114 { 115 int i; 116 static char a[18]; 117 118 i = sprintf(a,"%x:%x:%x:%x:%x:%x",n->octet[0],n->octet[1],n->octet[2], 119 n->octet[3],n->octet[4],n->octet[5]); 120 if (i < 11) 121 return (NULL); 122 return ((char *)&a); 123 } 124 125 /* 126 * Map an ethernet address to a hostname. Use either /etc/ethers or 127 * NIS/YP. 128 */ 129 130 int ether_ntohost(hostname, e) 131 char *hostname; 132 struct ether_addr *e; 133 { 134 FILE *fp; 135 char buf[BUFSIZ + 2]; 136 struct ether_addr local_ether; 137 char local_host[MAXHOSTNAMELEN]; 138 #ifdef YP 139 char *result; 140 int resultlen; 141 char *ether_a; 142 char *yp_domain; 143 #endif 144 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) 145 return (1); 146 147 while (fgets(buf,BUFSIZ,fp)) { 148 if (buf[0] == '#') 149 continue; 150 #ifdef YP 151 if (buf[0] == '+') { 152 if (yp_get_default_domain(&yp_domain)) 153 continue; 154 ether_a = ether_ntoa(e); 155 if (yp_match(yp_domain, "ethers.byaddr", ether_a, 156 strlen(ether_a), &result, &resultlen)) { 157 continue; 158 } 159 strncpy(buf, result, resultlen); 160 buf[resultlen] = '\0'; 161 free(result); 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, 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 + 2]; 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 if (yp_get_default_domain(&yp_domain)) 204 continue; 205 if (yp_match(yp_domain, "ethers.byname", hostname, 206 strlen(hostname), &result, &resultlen)) { 207 continue; 208 } 209 strncpy(buf, result, resultlen); 210 buf[resultlen] = '\0'; 211 free(result); 212 } 213 #endif 214 if (!ether_line(buf, &local_ether, local_host)) { 215 if (!strcmp(hostname, local_host)) { 216 /* We have a match */ 217 bcopy((char *)&local_ether.octet[0], 218 (char *)&e->octet[0], 6); 219 fclose(fp); 220 return(0); 221 } 222 } 223 } 224 fclose(fp); 225 return (1); 226 } 227