1 /* $OpenBSD: linkaddr.c,v 1.5 2005/08/06 20:30:03 espie Exp $ */ 2 /*- 3 * Copyright (c) 1990, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <net/if_dl.h> 34 #include <string.h> 35 36 /* States*/ 37 #define NAMING 0 38 #define GOTONE 1 39 #define GOTTWO 2 40 #define RESET 3 41 /* Inputs */ 42 #define DIGIT (4*0) 43 #define END (4*1) 44 #define DELIM (4*2) 45 #define LETTER (4*3) 46 47 void 48 link_addr(const char *addr, struct sockaddr_dl *sdl) 49 { 50 char *cp = sdl->sdl_data; 51 char *cplim = sdl->sdl_len + (char *)sdl; 52 int byte = 0, state = NAMING, new; 53 54 bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); 55 sdl->sdl_family = AF_LINK; 56 do { 57 state &= ~LETTER; 58 if ((*addr >= '0') && (*addr <= '9')) { 59 new = *addr - '0'; 60 } else if ((*addr >= 'a') && (*addr <= 'f')) { 61 new = *addr - 'a' + 10; 62 } else if ((*addr >= 'A') && (*addr <= 'F')) { 63 new = *addr - 'A' + 10; 64 } else if (*addr == 0) { 65 state |= END; 66 } else if (state == NAMING && 67 (((*addr >= 'A') && (*addr <= 'Z')) || 68 ((*addr >= 'a') && (*addr <= 'z')))) 69 state |= LETTER; 70 else 71 state |= DELIM; 72 addr++; 73 switch (state /* | INPUT */) { 74 case NAMING | DIGIT: 75 case NAMING | LETTER: 76 *cp++ = addr[-1]; 77 continue; 78 case NAMING | DELIM: 79 state = RESET; 80 sdl->sdl_nlen = cp - sdl->sdl_data; 81 continue; 82 case GOTTWO | DIGIT: 83 *cp++ = byte; 84 /* FALLTHROUGH */ 85 case RESET | DIGIT: 86 state = GOTONE; 87 byte = new; 88 continue; 89 case GOTONE | DIGIT: 90 state = GOTTWO; 91 byte = new + (byte << 4); 92 continue; 93 default: /* | DELIM */ 94 state = RESET; 95 *cp++ = byte; 96 byte = 0; 97 continue; 98 case GOTONE | END: 99 case GOTTWO | END: 100 *cp++ = byte; 101 /* FALLTHROUGH */ 102 case RESET | END: 103 break; 104 } 105 break; 106 } while (cp < cplim); 107 sdl->sdl_alen = cp - LLADDR(sdl); 108 new = cp - (char *)sdl; 109 if (new > sizeof(*sdl)) 110 sdl->sdl_len = new; 111 return; 112 } 113 114 static char hexlist[] = "0123456789abcdef"; 115 116 char * 117 link_ntoa(const struct sockaddr_dl *sdl) 118 { 119 static char obuf[64]; 120 char *out = obuf; 121 int i; 122 u_char *in = (u_char *)LLADDR(sdl); 123 u_char *inlim = in + sdl->sdl_alen; 124 int firsttime = 1; 125 126 if (sdl->sdl_nlen) { 127 bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); 128 out += sdl->sdl_nlen; 129 if (sdl->sdl_alen) 130 *out++ = ':'; 131 } 132 while (in < inlim) { 133 if (firsttime) 134 firsttime = 0; 135 else 136 *out++ = '.'; 137 i = *in++; 138 if (i > 0xf) { 139 out[1] = hexlist[i & 0xf]; 140 i >>= 4; 141 out[0] = hexlist[i]; 142 out += 2; 143 } else 144 *out++ = hexlist[i]; 145 } 146 *out = 0; 147 return (obuf); 148 } 149