xref: /onnv-gate/usr/src/lib/libsocket/inet/link_addr.c (revision 3938:670947f6c3f6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3938Sjbeck  * Common Development and Distribution License (the "License").
6*3938Sjbeck  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3938Sjbeck 
220Sstevel@tonic-gate /*
23*3938Sjbeck  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Function implementations to convert between link layer addresses and
310Sstevel@tonic-gate  * ascii representations of the form "x:x:x:...:x:x:x" where x is a hex
320Sstevel@tonic-gate  * number between 0x00 and 0xff; the bytes are always in network order.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <net/if_dl.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Converts a "size" bytes long mac address to its string representation.
430Sstevel@tonic-gate  * Currently, the "mactype" is unused, but in the future, the string
440Sstevel@tonic-gate  * can be modulated by "mactype" (IFT_* value from <net/if_types.h>)
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate /* ARGSUSED */
470Sstevel@tonic-gate char *
_link_ntoa(const unsigned char * macaddr,char * str,int size,int mactype)480Sstevel@tonic-gate _link_ntoa(const unsigned char *macaddr, char *str, int size, int mactype)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	char *buf;
510Sstevel@tonic-gate 	int i, n;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if (((buf = str) == NULL) &&
540Sstevel@tonic-gate 	    ((buf = malloc(3 * size)) == NULL))
550Sstevel@tonic-gate 		return (NULL);
560Sstevel@tonic-gate 	n = sprintf(buf, "%x", *macaddr++);
570Sstevel@tonic-gate 	for (i = 0; i < (size - 1); i++)
580Sstevel@tonic-gate 		n += sprintf(buf+n, ":%x", *macaddr++);
590Sstevel@tonic-gate 	return (buf);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Converts a string possibly representing a link address into its
640Sstevel@tonic-gate  * bit format, returning length of the address in bytes.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate uchar_t *
_link_aton(const char * ascaddr,int * maclen)670Sstevel@tonic-gate _link_aton(const char *ascaddr, int *maclen)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	unsigned char cval, num = 0;
700Sstevel@tonic-gate 	int idx = 0, numcolons = 0, digits = 0;
710Sstevel@tonic-gate 	uchar_t *netaddr;
720Sstevel@tonic-gate 	const char *cptr;
730Sstevel@tonic-gate 	char lastc = ':';
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	while (isspace(*ascaddr))
760Sstevel@tonic-gate 		ascaddr++;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * Find how many :'s in the string. Also sanity check
800Sstevel@tonic-gate 	 * the string for valid hex chars, absence of white
810Sstevel@tonic-gate 	 * spaces, not starting or ending with :, absence of
820Sstevel@tonic-gate 	 * consecutive :'s, excessive digits per element
830Sstevel@tonic-gate 	 * and non-null string.
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 	cptr = ascaddr;
860Sstevel@tonic-gate 	while ((cval = *cptr++) != '\0') {
870Sstevel@tonic-gate 		if (cval == ':') {
880Sstevel@tonic-gate 			if (lastc == ':')
890Sstevel@tonic-gate 				break;
900Sstevel@tonic-gate 			numcolons++;
910Sstevel@tonic-gate 			digits = 0;
920Sstevel@tonic-gate 		} else if (!isxdigit(cval)) {
930Sstevel@tonic-gate 			break;
940Sstevel@tonic-gate 		} else {
950Sstevel@tonic-gate 			digits++;
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		if (digits > 2)
990Sstevel@tonic-gate 			break;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		lastc = cval;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	if ((lastc == ':') || (cval != '\0' && !isspace(cval)) ||
1040Sstevel@tonic-gate 	    (digits > 2)) {
1050Sstevel@tonic-gate 		*maclen = -1;
1060Sstevel@tonic-gate 		return (NULL);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
109*3938Sjbeck 	if ((netaddr = malloc(numcolons + 1)) == NULL) {
1100Sstevel@tonic-gate 		*maclen = 0;
1110Sstevel@tonic-gate 		return (NULL);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	for (;;) {
1150Sstevel@tonic-gate 		cval = *ascaddr++;
1160Sstevel@tonic-gate 		if (isdigit(cval)) {
1170Sstevel@tonic-gate 			num = (num << 4) | (cval - '0');
1180Sstevel@tonic-gate 		} else if (isxdigit(cval)) {
1190Sstevel@tonic-gate 			num = (num << 4) |
1200Sstevel@tonic-gate 			    (cval - (isupper(cval) ? 'A' : 'a') + 10);
1210Sstevel@tonic-gate 		} else if (cval == ':') {
1220Sstevel@tonic-gate 			netaddr[idx++] = num;
1230Sstevel@tonic-gate 			num = 0;
1240Sstevel@tonic-gate 		} else {
1250Sstevel@tonic-gate 			/*
1260Sstevel@tonic-gate 			 * We must have hit a whitespace. Stop
1270Sstevel@tonic-gate 			 * parsing now.
1280Sstevel@tonic-gate 			 */
1290Sstevel@tonic-gate 			netaddr[idx++] = num;
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	*maclen = idx;
1340Sstevel@tonic-gate 	return (netaddr);
1350Sstevel@tonic-gate }
136