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