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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Function implementations to convert between link layer addresses and
29  * ascii representations of the form "x:x:x:...:x:x:x" where x is a hex
30  * number between 0x00 and 0xff; the bytes are always in network order.
31  */
32 
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <net/if_dl.h>
38 
39 /*
40  * Converts a "size" bytes long mac address to its string representation.
41  * Currently, the "mactype" is unused, but in the future, the string
42  * can be modulated by "mactype" (IFT_* value from <net/if_types.h>)
43  */
44 /* ARGSUSED */
45 char *
_link_ntoa(const unsigned char * macaddr,char * str,int size,int mactype)46 _link_ntoa(const unsigned char *macaddr, char *str, int size, int mactype)
47 {
48 	char *buf;
49 	int i, n;
50 
51 	if (((buf = str) == NULL) &&
52 	    ((buf = malloc(3 * size)) == NULL))
53 		return (NULL);
54 	n = sprintf(buf, "%x", *macaddr++);
55 	for (i = 0; i < (size - 1); i++)
56 		n += sprintf(buf+n, ":%x", *macaddr++);
57 	return (buf);
58 }
59 
60 /*
61  * Converts a string possibly representing a link address into its
62  * bit format, returning length of the address in bytes.
63  */
64 uchar_t *
_link_aton(const char * ascaddr,int * maclen)65 _link_aton(const char *ascaddr, int *maclen)
66 {
67 	unsigned char cval, num = 0;
68 	int idx = 0, numcolons = 0, digits = 0;
69 	uchar_t *netaddr;
70 	const char *cptr;
71 	char lastc = ':';
72 
73 	while (isspace(*ascaddr))
74 		ascaddr++;
75 
76 	/*
77 	 * Find how many :'s in the string. Also sanity check
78 	 * the string for valid hex chars, absence of white
79 	 * spaces, not starting or ending with :, absence of
80 	 * consecutive :'s, excessive digits per element
81 	 * and non-null string.
82 	 */
83 	cptr = ascaddr;
84 	while ((cval = *cptr++) != '\0') {
85 		if (cval == ':') {
86 			if (lastc == ':')
87 				break;
88 			numcolons++;
89 			digits = 0;
90 		} else if (!isxdigit(cval)) {
91 			break;
92 		} else {
93 			digits++;
94 		}
95 
96 		if (digits > 2)
97 			break;
98 
99 		lastc = cval;
100 	}
101 	if ((lastc == ':') || (cval != '\0' && !isspace(cval)) ||
102 	    (digits > 2)) {
103 		*maclen = -1;
104 		return (NULL);
105 	}
106 
107 	if ((netaddr = malloc(numcolons + 1)) == NULL) {
108 		*maclen = 0;
109 		return (NULL);
110 	}
111 
112 	for (;;) {
113 		cval = *ascaddr++;
114 		if (isdigit(cval)) {
115 			num = (num << 4) | (cval - '0');
116 		} else if (isxdigit(cval)) {
117 			num = (num << 4) |
118 			    (cval - (isupper(cval) ? 'A' : 'a') + 10);
119 		} else if (cval == ':') {
120 			netaddr[idx++] = num;
121 			num = 0;
122 		} else {
123 			/*
124 			 * We must have hit a whitespace. Stop
125 			 * parsing now.
126 			 */
127 			netaddr[idx++] = num;
128 			break;
129 		}
130 	}
131 	*maclen = idx;
132 	return (netaddr);
133 }
134