source: rtems/cpukit/libnetworking/libc/ether_addr.c @ d8fce03

4.8
Last change on this file since d8fce03 was 09fdb5e8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/30/07 at 05:15:58

Eliminate SCCS, LINT. Add HAVE_CONFIG_H.

  • Property mode set to 100644
File size: 5.7 KB
Line 
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#if HAVE_CONFIG_H
43#include "config.h"
44#endif
45
46#include <stdio.h>
47#include <paths.h>
48#include <sys/types.h>
49#include <string.h>
50#include <stdlib.h>
51#include <sys/param.h>
52#include <sys/socket.h>
53#include <net/ethernet.h>
54#ifdef YP
55#include <rpc/rpc.h>
56#include <rpcsvc/yp_prot.h>
57#include <rpcsvc/ypclnt.h>
58#endif
59
60#ifndef _PATH_ETHERS
61#define _PATH_ETHERS "/etc/ethers"
62#endif
63
64/*
65 * Parse a string of text containing an ethernet address and hostname
66 * and separate it into its component parts.
67 */
68int ether_line(l, e, hostname)
69        char *l;
70        struct ether_addr *e;
71        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],
76                                              &o[3], &o[4], &o[5],
77                                              hostname);
78        if (i != 7)
79                return (i);
80
81        for (i=0; i<6; i++)
82                e->octet[i] = o[i];
83        return (0);
84}
85
86/*
87 * Convert an ASCII representation of an ethernet address to
88 * binary form.
89 */
90struct ether_addr *ether_aton(a)
91        char *a;
92{
93        int i;
94        static struct ether_addr o;
95        unsigned int o0, o1, o2, o3, o4, o5;
96
97        i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
98
99        if (i != 6)
100                return (NULL);
101
102        o.octet[0]=o0;
103        o.octet[1]=o1;
104        o.octet[2]=o2;
105        o.octet[3]=o3;
106        o.octet[4]=o4;
107        o.octet[5]=o5;
108
109        return ((struct ether_addr *)&o);
110}
111
112/*
113 * Convert a binary representation of an ethernet address to
114 * an ASCII string.
115 */
116char *ether_ntoa(n)
117        struct ether_addr *n;
118{
119        int i;
120        static char a[18];
121
122        i = sprintf(a,"%x:%x:%x:%x:%x:%x",n->octet[0],n->octet[1],n->octet[2],
123                                          n->octet[3],n->octet[4],n->octet[5]);
124        if (i < 11)
125                return (NULL);
126        return ((char *)&a);
127}
128
129/*
130 * Map an ethernet address to a hostname. Use either /etc/ethers or
131 * NIS/YP.
132 */
133
134int ether_ntohost(hostname, e)
135        char *hostname;
136        struct ether_addr *e;
137{
138        FILE *fp;
139        char buf[BUFSIZ + 2];
140        struct ether_addr local_ether;
141        char local_host[MAXHOSTNAMELEN];
142#ifdef YP
143        char *result;
144        int resultlen;
145        char *ether_a;
146        char *yp_domain;
147#endif
148        if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
149                return (1);
150
151        while (fgets(buf,BUFSIZ,fp)) {
152                if (buf[0] == '#')
153                        continue;
154#ifdef YP
155                if (buf[0] == '+') {
156                        if (yp_get_default_domain(&yp_domain))
157                                continue;
158                        ether_a = ether_ntoa(e);
159                        if (yp_match(yp_domain, "ethers.byaddr", ether_a,
160                                strlen(ether_a), &result, &resultlen)) {
161                                continue;
162                        }
163                        strncpy(buf, result, resultlen);
164                        buf[resultlen] = '\0';
165                        free(result);
166                }
167#endif
168                if (!ether_line(buf, &local_ether, local_host)) {
169                        if (!bcmp((char *)&local_ether.octet[0],
170                                (char *)&e->octet[0], 6)) {
171                        /* We have a match */
172                                strcpy(hostname, local_host);
173                                fclose(fp);
174                                return(0);
175                        }
176                }
177        }
178        fclose(fp);
179        return (1);
180}
181
182/*
183 * Map a hostname to an ethernet address using /etc/ethers or
184 * NIS/YP.
185 */
186int ether_hostton(hostname, e)
187        char *hostname;
188        struct ether_addr *e;
189{
190        FILE *fp;
191        char buf[BUFSIZ + 2];
192        struct ether_addr local_ether;
193        char local_host[MAXHOSTNAMELEN];
194#ifdef YP
195        char *result;
196        int resultlen;
197        char *yp_domain;
198#endif
199        if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
200                return (1);
201
202        while (fgets(buf,BUFSIZ,fp)) {
203                if (buf[0] == '#')
204                        continue;
205#ifdef YP
206                if (buf[0] == '+') {
207                        if (yp_get_default_domain(&yp_domain))
208                                continue;
209                        if (yp_match(yp_domain, "ethers.byname", hostname,
210                                strlen(hostname), &result, &resultlen)) {
211                                continue;
212                        }
213                        strncpy(buf, result, resultlen);
214                        buf[resultlen] = '\0';
215                        free(result);
216                }
217#endif
218                if (!ether_line(buf, &local_ether, local_host)) {
219                        if (!strcmp(hostname, local_host)) {
220                                /* We have a match */
221                                bcopy((char *)&local_ether.octet[0],
222                                        (char *)&e->octet[0], 6);
223                                fclose(fp);
224                                return(0);
225                        }
226                }
227        }
228        fclose(fp);
229        return (1);
230}
Note: See TracBrowser for help on using the repository browser.