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