]> git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/ether_addr.c
Libinfo-173.tar.gz
[apple/libinfo.git] / gen.subproj / ether_addr.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #if !defined(lint) && defined(SCCSIDS)
26 static char sccsid[] = "@(#)ether_addr.c 1.2 88/05/10 4.0NFSSRC; from 1.9 88/02/08 Copyr 1985 Sun Micro";
27 #endif
28
29 /*
30 * Copyright (c) 1985 by Sun Microsystems, Inc.
31 *
32 * All routines necessary to deal with the file /etc/ethers. The file
33 * contains mappings from 48 bit ethernet addresses to their corresponding
34 * hosts name. The addresses have an ascii representation of the form
35 * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the
36 * bytes are always in network order.
37 */
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47
48 static const char ethers[] = "/etc/ethers";
49
50 /*
51 * Parses a line from /etc/ethers into its components. The line has the form
52 * 8:0:20:1:17:c8 krypton
53 * where the first part is a 48 bit ethernet addrerss and the second is
54 * the corresponding hosts name.
55 * Returns zero if successful, non-zero otherwise.
56 */
57 int ether_line(s, e, hostname)
58 char *s; /* the string to be parsed */
59 struct ether_addr *e; /* ethernet address struct to be filled in */
60 char *hostname; /* hosts name to be set */
61 {
62 register int i;
63 unsigned int t[6];
64
65 i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
66 &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
67 if (i != 7) {
68 return (7 - i);
69 }
70 for (i = 0; i < 6; i++)
71 e->ether_addr_octet[i] = t[i];
72 return (0);
73 }
74
75 /*
76 * Converts a 48 bit ethernet number to its string representation.
77 */
78 #define EI(i) (unsigned int)(e->ether_addr_octet[(i)])
79 char *
80 ether_ntoa(e)
81 const struct ether_addr *e;
82 {
83 static char *s;
84
85 if (s == 0) {
86 s = (char *)malloc(18);
87 if (s == 0)
88 return (0);
89 }
90 s[0] = 0;
91 sprintf(s, "%x:%x:%x:%x:%x:%x",
92 EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
93 return (s);
94 }
95
96 /*
97 * Converts a ethernet address representation back into its 48 bits.
98 */
99 struct ether_addr *
100 ether_aton(s)
101 char *s;
102 {
103 static struct ether_addr *ep;
104 register int i;
105 unsigned int t[6];
106
107 if (ep == 0) {
108 ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr));
109 if (ep == 0)
110 return (0);
111 }
112 i = sscanf(s, " %x:%x:%x:%x:%x:%x",
113 &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
114 if (i != 6)
115 return ((struct ether_addr *)NULL);
116 for (i = 0; i < 6; i++)
117 ep->ether_addr_octet[i] = t[i];
118 return (ep);
119 }
120
121 /*
122 * Given a host's name, this routine returns its 48 bit ethernet address.
123 * Returns zero if successful, non-zero otherwise.
124 */
125 /* XXX need to override in netinfo */
126 int ether_hostton(host, e)
127 char *host; /* function input */
128 struct ether_addr *e; /* function output */
129 {
130 char currenthost[256];
131 char buf[512];
132 char *val = buf;
133 register int reason;
134 FILE *f;
135
136 if ((f = fopen(ethers, "r")) == NULL)
137 {
138 return (-1);
139 }
140
141 reason = -1;
142 while (fscanf(f, "%[^\n] ", val) == 1)
143 {
144 if ((ether_line(val, e, currenthost) == 0) &&
145 (strcmp(currenthost, host) == 0))
146 {
147 reason = 0;
148 break;
149 }
150 }
151
152 fclose(f);
153 return (reason);
154 }
155
156 /*
157 * Given a 48 bit ethernet address, this routine return its host name.
158 * Returns zero if successful, non-zero otherwise.
159 */
160 /* XXX need to override in netinfo */
161 int ether_ntohost(host, e)
162 char *host; /* function output */
163 struct ether_addr *e; /* function input */
164 {
165 struct ether_addr currente;
166 char buf[512];
167 char *val = buf;
168 register int reason;
169 FILE *f;
170
171 if ((f = fopen(ethers, "r")) == NULL)
172 {
173 return (-1);
174 }
175
176 reason = -1;
177 while (fscanf(f, "%[^\n] ", val) == 1)
178 {
179 if ((ether_line(val, &currente, host) == 0) &&
180 (bcmp(e, &currente, sizeof(currente)) == 0))
181 {
182 reason = 0;
183 break;
184 }
185 }
186
187 fclose(f);
188 return (reason);
189 }