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 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 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. 33 */ 34 35 #include <stdio.h> 36 #include <ctype.h> 37 #include <stdlib.h> 38 #include <sys/types.h> 39 #include <net/if_dl.h> 40 41 /* 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>) 45 */ 46 /* ARGSUSED */ 47 char * 48 _link_ntoa(const unsigned char *macaddr, char *str, int size, int mactype) 49 { 50 char *buf; 51 int i, n; 52 53 if (((buf = str) == NULL) && 54 ((buf = malloc(3 * size)) == NULL)) 55 return (NULL); 56 n = sprintf(buf, "%x", *macaddr++); 57 for (i = 0; i < (size - 1); i++) 58 n += sprintf(buf+n, ":%x", *macaddr++); 59 return (buf); 60 } 61 62 /* 63 * Converts a string possibly representing a link address into its 64 * bit format, returning length of the address in bytes. 65 */ 66 uchar_t * 67 _link_aton(const char *ascaddr, int *maclen) 68 { 69 unsigned char cval, num = 0; 70 int idx = 0, numcolons = 0, digits = 0; 71 uchar_t *netaddr; 72 const char *cptr; 73 char lastc = ':'; 74 75 while (isspace(*ascaddr)) 76 ascaddr++; 77 78 /* 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. 84 */ 85 cptr = ascaddr; 86 while ((cval = *cptr++) != '\0') { 87 if (cval == ':') { 88 if (lastc == ':') 89 break; 90 numcolons++; 91 digits = 0; 92 } else if (!isxdigit(cval)) { 93 break; 94 } else { 95 digits++; 96 } 97 98 if (digits > 2) 99 break; 100 101 lastc = cval; 102 } 103 if ((lastc == ':') || (cval != '\0' && !isspace(cval)) || 104 (digits > 2)) { 105 *maclen = -1; 106 return (NULL); 107 } 108 109 if ((netaddr = malloc(numcolons + 1)) == NULL) { 110 *maclen = 0; 111 return (NULL); 112 } 113 114 for (;;) { 115 cval = *ascaddr++; 116 if (isdigit(cval)) { 117 num = (num << 4) | (cval - '0'); 118 } else if (isxdigit(cval)) { 119 num = (num << 4) | 120 (cval - (isupper(cval) ? 'A' : 'a') + 10); 121 } else if (cval == ':') { 122 netaddr[idx++] = num; 123 num = 0; 124 } else { 125 /* 126 * We must have hit a whitespace. Stop 127 * parsing now. 128 */ 129 netaddr[idx++] = num; 130 break; 131 } 132 } 133 *maclen = idx; 134 return (netaddr); 135 } 136