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.3 1996/03/16 21:25:59 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 unsigned int o0, o1, o2, o3, o4, o5; 94 95 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5); 96 97 if (i != 6) 98 return (NULL); 99 100 o.octet[0]=o0; 101 o.octet[1]=o1; 102 o.octet[2]=o2; 103 o.octet[3]=o3; 104 o.octet[4]=o4; 105 o.octet[5]=o5; 106 107 return ((struct ether_addr *)&o); 108 } 109 110 /* 111 * Convert a binary representation of an ethernet address to 112 * an ASCII string. 113 */ 114 char *ether_ntoa(n) 115 struct ether_addr *n; 116 { 117 int i; 118 static char a[18]; 119 120 i = sprintf(a,"%x:%x:%x:%x:%x:%x",n->octet[0],n->octet[1],n->octet[2], 121 n->octet[3],n->octet[4],n->octet[5]); 122 if (i < 11) 123 return (NULL); 124 return ((char *)&a); 125 } 126 127 /* 128 * Map an ethernet address to a hostname. Use either /etc/ethers or 129 * NIS/YP. 130 */ 131 132 int ether_ntohost(hostname, e) 133 char *hostname; 134 struct ether_addr *e; 135 { 136 FILE *fp; 137 char buf[BUFSIZ]; 138 struct ether_addr local_ether; 139 char local_host[MAXHOSTNAMELEN]; 140 #ifdef YP 141 char *result; 142 int resultlen; 143 char *ether_a; 144 char *yp_domain; 145 #endif 146 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) 147 return (1); 148 149 while (fgets(buf,BUFSIZ,fp)) { 150 if (buf[0] == '#') 151 continue; 152 #ifdef YP 153 if (buf[0] == '+') { 154 if (yp_get_default_domain(&yp_domain)) 155 continue; 156 ether_a = ether_ntoa(e); 157 if (yp_match(yp_domain, "ethers.byaddr", ether_a, 158 strlen(ether_a), &result, &resultlen)) { 159 continue; 160 } 161 strncpy((char *)&buf, result, resultlen); 162 free(result); 163 } 164 #endif 165 if (!ether_line(&buf, &local_ether, &local_host)) { 166 if (!bcmp((char *)&local_ether.octet[0], 167 (char *)&e->octet[0], 6)) { 168 /* We have a match */ 169 strcpy(hostname, (char *)&local_host); 170 fclose(fp); 171 return(0); 172 } 173 } 174 } 175 fclose(fp); 176 return (1); 177 } 178 179 /* 180 * Map a hostname to an ethernet address using /etc/ethers or 181 * NIS/YP. 182 */ 183 int ether_hostton(hostname, e) 184 char *hostname; 185 struct ether_addr *e; 186 { 187 FILE *fp; 188 char buf[BUFSIZ]; 189 struct ether_addr local_ether; 190 char local_host[MAXHOSTNAMELEN]; 191 #ifdef YP 192 char *result; 193 int resultlen; 194 char *yp_domain; 195 #endif 196 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) 197 return (1); 198 199 while (fgets(buf,BUFSIZ,fp)) { 200 if (buf[0] == '#') 201 continue; 202 #ifdef YP 203 if (buf[0] == '+') { 204 if (yp_get_default_domain(&yp_domain)) 205 continue; 206 if (yp_match(yp_domain, "ethers.byname", hostname, 207 strlen(hostname), &result, &resultlen)) { 208 continue; 209 } 210 strncpy((char *)&buf, result, resultlen); 211 free(result); 212 } 213 #endif 214 if (!ether_line(&buf, &local_ether, &local_host)) { 215 if (!strcmp(hostname, (char *)&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