xref: /onnv-gate/usr/src/lib/libbc/libc/net/ether_addr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * Copyright (c) 1990 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * All routines necessary to deal with the file /etc/ethers.  The file
28*0Sstevel@tonic-gate  * contains mappings from 48 bit ethernet addresses to their corresponding
29*0Sstevel@tonic-gate  * hosts name.  The addresses have an ascii representation of the form
30*0Sstevel@tonic-gate  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
31*0Sstevel@tonic-gate  * bytes are always in network order.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/socket.h>
37*0Sstevel@tonic-gate #include <net/if.h>
38*0Sstevel@tonic-gate #include <netinet/in.h>
39*0Sstevel@tonic-gate #include <net/if_arp.h>
40*0Sstevel@tonic-gate #include <netinet/if_ether.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static char *domain; /* NIS domain name */
43*0Sstevel@tonic-gate static int usingyellow;
44*0Sstevel@tonic-gate static char *ethers = "/etc/ethers";
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * Parses a line from /etc/ethers into its components.  The line has the form
48*0Sstevel@tonic-gate  * 8:0:20:1:17:c8	krypton
49*0Sstevel@tonic-gate  * where the first part is a 48 bit ethernet addrerss and the second is
50*0Sstevel@tonic-gate  * the corresponding hosts name.
51*0Sstevel@tonic-gate  * Returns zero if successful, non-zero otherwise.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate ether_line(s, e, hostname)
54*0Sstevel@tonic-gate 	char *s;		/* the string to be parsed */
55*0Sstevel@tonic-gate 	struct ether_addr *e;	/* ethernet address struct to be filled in */
56*0Sstevel@tonic-gate 	char *hostname;		/* hosts name to be set */
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	register int i;
59*0Sstevel@tonic-gate 	unsigned int t[6];
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
62*0Sstevel@tonic-gate 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
63*0Sstevel@tonic-gate 	if (i != 7) {
64*0Sstevel@tonic-gate 		return (7 - i);
65*0Sstevel@tonic-gate 	}
66*0Sstevel@tonic-gate 	for (i = 0; i < 6; i++)
67*0Sstevel@tonic-gate 		e->ether_addr_octet[i] = t[i];
68*0Sstevel@tonic-gate 	return (0);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * Converts a 48 bit ethernet number to its string representation.
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate #define EI(i)	(unsigned int)(e->ether_addr_octet[(i)])
75*0Sstevel@tonic-gate char *
76*0Sstevel@tonic-gate ether_ntoa(e)
77*0Sstevel@tonic-gate 	struct ether_addr *e;
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	static char *s;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (s == 0) {
82*0Sstevel@tonic-gate 		s = (char *)malloc(18);
83*0Sstevel@tonic-gate 		if (s == 0)
84*0Sstevel@tonic-gate 			return (0);
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 	s[0] = 0;
87*0Sstevel@tonic-gate 	sprintf(s, "%x:%x:%x:%x:%x:%x",
88*0Sstevel@tonic-gate 	    EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
89*0Sstevel@tonic-gate 	return (s);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate  * Converts a ethernet address representation back into its 48 bits.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate struct ether_addr *
96*0Sstevel@tonic-gate ether_aton(s)
97*0Sstevel@tonic-gate 	char *s;
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	static struct ether_addr *ep;
100*0Sstevel@tonic-gate 	register int i;
101*0Sstevel@tonic-gate 	unsigned int t[6];
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	if (ep == 0) {
104*0Sstevel@tonic-gate 		ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr));
105*0Sstevel@tonic-gate 		if (ep == 0)
106*0Sstevel@tonic-gate 			return (0);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 	i = sscanf(s, " %x:%x:%x:%x:%x:%x",
109*0Sstevel@tonic-gate 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
110*0Sstevel@tonic-gate 	if (i != 6)
111*0Sstevel@tonic-gate 	    return ((struct ether_addr *)NULL);
112*0Sstevel@tonic-gate 	for (i = 0; i < 6; i++)
113*0Sstevel@tonic-gate 		ep->ether_addr_octet[i] = t[i];
114*0Sstevel@tonic-gate 	return (ep);
115*0Sstevel@tonic-gate }
116