xref: /titanic_51/usr/src/lib/libbc/libc/net/ether_addr.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1990 by Sun Microsystems, Inc.
26  *
27  * All routines necessary to deal with the file /etc/ethers.  The file
28  * contains mappings from 48 bit ethernet addresses to their corresponding
29  * hosts name.  The addresses have an ascii representation of the form
30  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
31  * bytes are always in network order.
32  */
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <net/if_arp.h>
40 #include <netinet/if_ether.h>
41 
42 static char *domain; /* NIS domain name */
43 static int usingyellow;
44 static char *ethers = "/etc/ethers";
45 
46 /*
47  * Parses a line from /etc/ethers into its components.  The line has the form
48  * 8:0:20:1:17:c8	krypton
49  * where the first part is a 48 bit ethernet addrerss and the second is
50  * the corresponding hosts name.
51  * Returns zero if successful, non-zero otherwise.
52  */
53 ether_line(s, e, hostname)
54 	char *s;		/* the string to be parsed */
55 	struct ether_addr *e;	/* ethernet address struct to be filled in */
56 	char *hostname;		/* hosts name to be set */
57 {
58 	register int i;
59 	unsigned int t[6];
60 
61 	i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
62 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
63 	if (i != 7) {
64 		return (7 - i);
65 	}
66 	for (i = 0; i < 6; i++)
67 		e->ether_addr_octet[i] = t[i];
68 	return (0);
69 }
70 
71 /*
72  * Converts a 48 bit ethernet number to its string representation.
73  */
74 #define EI(i)	(unsigned int)(e->ether_addr_octet[(i)])
75 char *
76 ether_ntoa(e)
77 	struct ether_addr *e;
78 {
79 	static char *s;
80 
81 	if (s == 0) {
82 		s = (char *)malloc(18);
83 		if (s == 0)
84 			return (0);
85 	}
86 	s[0] = 0;
87 	sprintf(s, "%x:%x:%x:%x:%x:%x",
88 	    EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
89 	return (s);
90 }
91 
92 /*
93  * Converts a ethernet address representation back into its 48 bits.
94  */
95 struct ether_addr *
96 ether_aton(s)
97 	char *s;
98 {
99 	static struct ether_addr *ep;
100 	register int i;
101 	unsigned int t[6];
102 
103 	if (ep == 0) {
104 		ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr));
105 		if (ep == 0)
106 			return (0);
107 	}
108 	i = sscanf(s, " %x:%x:%x:%x:%x:%x",
109 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
110 	if (i != 6)
111 	    return ((struct ether_addr *)NULL);
112 	for (i = 0; i < 6; i++)
113 		ep->ether_addr_octet[i] = t[i];
114 	return (ep);
115 }
116