17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*d71dbb73Sjbeck * Common Development and Distribution License (the "License"). 6*d71dbb73Sjbeck * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*d71dbb73Sjbeck 227c478bd9Sstevel@tonic-gate /* 23*d71dbb73Sjbeck * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Function implementations to convert between link layer addresses and 317c478bd9Sstevel@tonic-gate * ascii representations of the form "x:x:x:...:x:x:x" where x is a hex 327c478bd9Sstevel@tonic-gate * number between 0x00 and 0xff; the bytes are always in network order. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <stdio.h> 367c478bd9Sstevel@tonic-gate #include <ctype.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <sys/types.h> 397c478bd9Sstevel@tonic-gate #include <net/if_dl.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * Converts a "size" bytes long mac address to its string representation. 437c478bd9Sstevel@tonic-gate * Currently, the "mactype" is unused, but in the future, the string 447c478bd9Sstevel@tonic-gate * can be modulated by "mactype" (IFT_* value from <net/if_types.h>) 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate /* ARGSUSED */ 477c478bd9Sstevel@tonic-gate char * 487c478bd9Sstevel@tonic-gate _link_ntoa(const unsigned char *macaddr, char *str, int size, int mactype) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate char *buf; 517c478bd9Sstevel@tonic-gate int i, n; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate if (((buf = str) == NULL) && 547c478bd9Sstevel@tonic-gate ((buf = malloc(3 * size)) == NULL)) 557c478bd9Sstevel@tonic-gate return (NULL); 567c478bd9Sstevel@tonic-gate n = sprintf(buf, "%x", *macaddr++); 577c478bd9Sstevel@tonic-gate for (i = 0; i < (size - 1); i++) 587c478bd9Sstevel@tonic-gate n += sprintf(buf+n, ":%x", *macaddr++); 597c478bd9Sstevel@tonic-gate return (buf); 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate /* 637c478bd9Sstevel@tonic-gate * Converts a string possibly representing a link address into its 647c478bd9Sstevel@tonic-gate * bit format, returning length of the address in bytes. 657c478bd9Sstevel@tonic-gate */ 667c478bd9Sstevel@tonic-gate uchar_t * 677c478bd9Sstevel@tonic-gate _link_aton(const char *ascaddr, int *maclen) 687c478bd9Sstevel@tonic-gate { 697c478bd9Sstevel@tonic-gate unsigned char cval, num = 0; 707c478bd9Sstevel@tonic-gate int idx = 0, numcolons = 0, digits = 0; 717c478bd9Sstevel@tonic-gate uchar_t *netaddr; 727c478bd9Sstevel@tonic-gate const char *cptr; 737c478bd9Sstevel@tonic-gate char lastc = ':'; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate while (isspace(*ascaddr)) 767c478bd9Sstevel@tonic-gate ascaddr++; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * Find how many :'s in the string. Also sanity check 807c478bd9Sstevel@tonic-gate * the string for valid hex chars, absence of white 817c478bd9Sstevel@tonic-gate * spaces, not starting or ending with :, absence of 827c478bd9Sstevel@tonic-gate * consecutive :'s, excessive digits per element 837c478bd9Sstevel@tonic-gate * and non-null string. 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate cptr = ascaddr; 867c478bd9Sstevel@tonic-gate while ((cval = *cptr++) != '\0') { 877c478bd9Sstevel@tonic-gate if (cval == ':') { 887c478bd9Sstevel@tonic-gate if (lastc == ':') 897c478bd9Sstevel@tonic-gate break; 907c478bd9Sstevel@tonic-gate numcolons++; 917c478bd9Sstevel@tonic-gate digits = 0; 927c478bd9Sstevel@tonic-gate } else if (!isxdigit(cval)) { 937c478bd9Sstevel@tonic-gate break; 947c478bd9Sstevel@tonic-gate } else { 957c478bd9Sstevel@tonic-gate digits++; 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (digits > 2) 997c478bd9Sstevel@tonic-gate break; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate lastc = cval; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate if ((lastc == ':') || (cval != '\0' && !isspace(cval)) || 1047c478bd9Sstevel@tonic-gate (digits > 2)) { 1057c478bd9Sstevel@tonic-gate *maclen = -1; 1067c478bd9Sstevel@tonic-gate return (NULL); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 109*d71dbb73Sjbeck if ((netaddr = malloc(numcolons + 1)) == NULL) { 1107c478bd9Sstevel@tonic-gate *maclen = 0; 1117c478bd9Sstevel@tonic-gate return (NULL); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate for (;;) { 1157c478bd9Sstevel@tonic-gate cval = *ascaddr++; 1167c478bd9Sstevel@tonic-gate if (isdigit(cval)) { 1177c478bd9Sstevel@tonic-gate num = (num << 4) | (cval - '0'); 1187c478bd9Sstevel@tonic-gate } else if (isxdigit(cval)) { 1197c478bd9Sstevel@tonic-gate num = (num << 4) | 1207c478bd9Sstevel@tonic-gate (cval - (isupper(cval) ? 'A' : 'a') + 10); 1217c478bd9Sstevel@tonic-gate } else if (cval == ':') { 1227c478bd9Sstevel@tonic-gate netaddr[idx++] = num; 1237c478bd9Sstevel@tonic-gate num = 0; 1247c478bd9Sstevel@tonic-gate } else { 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * We must have hit a whitespace. Stop 1277c478bd9Sstevel@tonic-gate * parsing now. 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate netaddr[idx++] = num; 1307c478bd9Sstevel@tonic-gate break; 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate *maclen = idx; 1347c478bd9Sstevel@tonic-gate return (netaddr); 1357c478bd9Sstevel@tonic-gate } 136