Use pkgsrc packages from a custom location.
[dragonfly.git] / lib / libc / net / ether_addr.c
blobac64691411736ea4bf0195d344eb2cb0b005bd2c
1 /*
2 * Copyright (c) 1995
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
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.
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.
32 * ethernet address conversion and lookup routines
34 * Written by Bill Paul <wpaul@ctr.columbia.edu>
35 * Center for Telecommunications Research
36 * Columbia University, New York City
38 * $FreeBSD: src/lib/libc/net/ether_addr.c,v 1.10.2.5 2002/04/08 08:01:50 ru Exp $
39 * $DragonFly: src/lib/libc/net/ether_addr.c,v 1.4 2005/11/13 02:04:47 swildner Exp $
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
56 #ifndef _PATH_ETHERS
57 #define _PATH_ETHERS "/etc/ethers"
58 #endif
61 * Parse a string of text containing an ethernet address and hostname
62 * and separate it into its component parts.
64 int
65 ether_line(const char *l, struct ether_addr *e, char *hostname)
67 int i, o[6];
69 i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2],
70 &o[3], &o[4], &o[5],
71 hostname);
72 if (i != 7)
73 return (i);
75 for (i=0; i<6; i++)
76 e->octet[i] = o[i];
77 return (0);
81 * Convert an ASCII representation of an ethernet address to
82 * binary form.
84 struct ether_addr *
85 ether_aton(const char *a)
87 int i;
88 static struct ether_addr o;
89 unsigned int o0, o1, o2, o3, o4, o5;
91 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
93 if (i != 6)
94 return (NULL);
96 o.octet[0]=o0;
97 o.octet[1]=o1;
98 o.octet[2]=o2;
99 o.octet[3]=o3;
100 o.octet[4]=o4;
101 o.octet[5]=o5;
103 return ((struct ether_addr *)&o);
107 * Convert a binary representation of an ethernet address to
108 * an ASCII string.
110 char *
111 ether_ntoa(const struct ether_addr *n)
113 int i;
114 static char a[18];
116 i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
117 n->octet[0], n->octet[1], n->octet[2],
118 n->octet[3], n->octet[4], n->octet[5]);
119 if (i < 17)
120 return (NULL);
121 return ((char *)&a);
125 * Map an ethernet address to a hostname. Use either /etc/ethers or
126 * NIS/YP.
129 ether_ntohost(char *hostname, const struct ether_addr *e)
131 FILE *fp;
132 char buf[BUFSIZ + 2];
133 struct ether_addr local_ether;
134 char local_host[MAXHOSTNAMELEN];
135 #ifdef YP
136 char *result;
137 int resultlen;
138 char *ether_a;
139 char *yp_domain;
140 #endif
141 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
142 return (1);
144 while (fgets(buf,BUFSIZ,fp)) {
145 if (buf[0] == '#')
146 continue;
147 #ifdef YP
148 if (buf[0] == '+') {
149 if (yp_get_default_domain(&yp_domain))
150 continue;
151 ether_a = ether_ntoa(e);
152 if (yp_match(yp_domain, "ethers.byaddr", ether_a,
153 strlen(ether_a), &result, &resultlen)) {
154 continue;
156 strncpy(buf, result, resultlen);
157 buf[resultlen] = '\0';
158 free(result);
160 #endif
161 if (!ether_line(buf, &local_ether, local_host)) {
162 if (!bcmp((char *)&local_ether.octet[0],
163 (char *)&e->octet[0], 6)) {
164 /* We have a match */
165 strcpy(hostname, local_host);
166 fclose(fp);
167 return(0);
171 fclose(fp);
172 return (1);
176 * Map a hostname to an ethernet address using /etc/ethers or
177 * NIS/YP.
180 ether_hostton(const char *hostname, struct ether_addr *e)
182 FILE *fp;
183 char buf[BUFSIZ + 2];
184 struct ether_addr local_ether;
185 char local_host[MAXHOSTNAMELEN];
186 #ifdef YP
187 char *result;
188 int resultlen;
189 char *yp_domain;
190 #endif
191 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
192 return (1);
194 while (fgets(buf,BUFSIZ,fp)) {
195 if (buf[0] == '#')
196 continue;
197 #ifdef YP
198 if (buf[0] == '+') {
199 if (yp_get_default_domain(&yp_domain))
200 continue;
201 if (yp_match(yp_domain, "ethers.byname", hostname,
202 strlen(hostname), &result, &resultlen)) {
203 continue;
205 strncpy(buf, result, resultlen);
206 buf[resultlen] = '\0';
207 free(result);
209 #endif
210 if (!ether_line(buf, &local_ether, local_host)) {
211 if (!strcmp(hostname, local_host)) {
212 /* We have a match */
213 bcopy((char *)&local_ether.octet[0],
214 (char *)&e->octet[0], 6);
215 fclose(fp);
216 return(0);
220 fclose(fp);
221 return (1);