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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Function implementations to convert between link layer addresses and
31 * ascii representations of the form "x:x:x:...:x:x:x" where x is a hex
32 * number between 0x00 and 0xff; the bytes are always in network order.
38 #include <sys/types.h>
39 #include <net/if_dl.h>
42 * Converts a "size" bytes long mac address to its string representation.
43 * Currently, the "mactype" is unused, but in the future, the string
44 * can be modulated by "mactype" (IFT_* value from <net/if_types.h>)
48 _link_ntoa(const unsigned char *macaddr
, char *str
, int size
, int mactype
)
53 if (((buf
= str
) == NULL
) &&
54 ((buf
= malloc(3 * size
)) == NULL
))
56 n
= sprintf(buf
, "%x", *macaddr
++);
57 for (i
= 0; i
< (size
- 1); i
++)
58 n
+= sprintf(buf
+n
, ":%x", *macaddr
++);
63 * Converts a string possibly representing a link address into its
64 * bit format, returning length of the address in bytes.
67 _link_aton(const char *ascaddr
, int *maclen
)
69 unsigned char cval
, num
= 0;
70 int idx
= 0, numcolons
= 0, digits
= 0;
75 while (isspace(*ascaddr
))
79 * Find how many :'s in the string. Also sanity check
80 * the string for valid hex chars, absence of white
81 * spaces, not starting or ending with :, absence of
82 * consecutive :'s, excessive digits per element
83 * and non-null string.
86 while ((cval
= *cptr
++) != '\0') {
92 } else if (!isxdigit(cval
)) {
103 if ((lastc
== ':') || (cval
!= '\0' && !isspace(cval
)) ||
109 if ((netaddr
= malloc(numcolons
+ 1)) == NULL
) {
117 num
= (num
<< 4) | (cval
- '0');
118 } else if (isxdigit(cval
)) {
120 (cval
- (isupper(cval
) ? 'A' : 'a') + 10);
121 } else if (cval
== ':') {
122 netaddr
[idx
++] = num
;
126 * We must have hit a whitespace. Stop
129 netaddr
[idx
++] = num
;