]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/ether_addr.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
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";
30 * Copyright (c) 1985 by Sun Microsystems, Inc.
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.
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
48 static const char ethers
[] = "/etc/ethers";
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.
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 */
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
);
70 for (i
= 0; i
< 6; i
++)
71 e
->ether_addr_octet
[i
] = t
[i
];
76 * Converts a 48 bit ethernet number to its string representation.
78 #define EI(i) (unsigned int)(e->ether_addr_octet[(i)])
81 const struct ether_addr
*e
;
86 s
= (char *)malloc(18);
91 sprintf(s
, "%x:%x:%x:%x:%x:%x",
92 EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
97 * Converts a ethernet address representation back into its 48 bits.
103 static struct ether_addr
*ep
;
108 ep
= (struct ether_addr
*)calloc(1, sizeof (struct ether_addr
));
112 i
= sscanf(s
, " %x:%x:%x:%x:%x:%x",
113 &t
[0], &t
[1], &t
[2], &t
[3], &t
[4], &t
[5]);
115 return ((struct ether_addr
*)NULL
);
116 for (i
= 0; i
< 6; i
++)
117 ep
->ether_addr_octet
[i
] = t
[i
];
122 * Given a host's name, this routine returns its 48 bit ethernet address.
123 * Returns zero if successful, non-zero otherwise.
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 */
130 char currenthost
[256];
136 if ((f
= fopen(ethers
, "r")) == NULL
)
142 while (fscanf(f
, "%[^\n] ", val
) == 1)
144 if ((ether_line(val
, e
, currenthost
) == 0) &&
145 (strcmp(currenthost
, host
) == 0))
157 * Given a 48 bit ethernet address, this routine return its host name.
158 * Returns zero if successful, non-zero otherwise.
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 */
165 struct ether_addr currente
;
171 if ((f
= fopen(ethers
, "r")) == NULL
)
177 while (fscanf(f
, "%[^\n] ", val
) == 1)
179 if ((ether_line(val
, ¤te
, host
) == 0) &&
180 (bcmp(e
, ¤te
, sizeof(currente
)) == 0))