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.1 1995/04/02 01:31:17 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 if (yp_get_default_domain(&yp_domain)) 146 continue; 147 ether_a = ether_ntoa(e); 148 if (yp_match(yp_domain, "ethers.byaddr", ether_a, 149 strlen(ether_a), &result, &resultlen)) { 150 free(result); 151 continue; 152 } 153 strncpy((char *)&buf, result, resultlen); 154 free(result); 155 } 156 #endif 157 if (!ether_line(&buf, &local_ether, &local_host)) { 158 if (!bcmp((char *)&local_ether.octet[0], 159 (char *)&e->octet[0], 6)) { 160 /* We have a match */ 161 strcpy(hostname, (char *)&local_host); 162 fclose(fp); 163 return(0); 164 } 165 } 166 } 167 fclose(fp); 168 return (1); 169 } 170 171 /* 172 * Map a hostname to an ethernet address using /etc/ethers or 173 * NIS/YP. 174 */ 175 int ether_hostton(hostname, e) 176 char *hostname; 177 struct ether_addr *e; 178 { 179 FILE *fp; 180 char buf[BUFSIZ]; 181 struct ether_addr local_ether; 182 char local_host[MAXHOSTNAMELEN]; 183 #ifdef YP 184 char *result; 185 int resultlen; 186 char *yp_domain; 187 #endif 188 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL) 189 return (1); 190 191 while (fgets(buf,BUFSIZ,fp)) { 192 if (buf[0] == '#') 193 continue; 194 #ifdef YP 195 if (buf[0] == '+') { 196 if (yp_get_default_domain(&yp_domain)) 197 continue; 198 if (yp_match(yp_domain, "ethers.byname", hostname, 199 strlen(hostname), &result, &resultlen)) { 200 free(result); 201 continue; 202 } 203 strncpy((char *)&buf, result, resultlen); 204 free(result); 205 } 206 #endif 207 if (!ether_line(&buf, &local_ether, &local_host)) { 208 if (!strcmp(hostname, (char *)&local_host)) { 209 /* We have a match */ 210 bcopy((char *)&local_ether.octet[0], 211 (char *)&e->octet[0], 6); 212 fclose(fp); 213 return(0); 214 } 215 } 216 } 217 fclose(fp); 218 return (1); 219 } 220