source: rtems-libbsd/freebsd/lib/libc/net/ether_addr.c @ d48955b

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since d48955b was d48955b, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 08:02:16

Add and use <machine/rtems-bsd-user-space.h>

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