1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)linkaddr.c 5.1 (Berkeley) 06/01/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/types.h> 13 #include <sys/socket.h> 14 #include <net/if_dl.h> 15 16 /* States*/ 17 #define NAMING 0 18 #define GOTONE 1 19 #define GOTTWO 2 20 #define RESET 3 21 /* Inputs */ 22 #define DIGIT (4*0) 23 #define END (4*1) 24 #define DELIM (4*2) 25 #define LETTER (4*3) 26 27 link_addr(addr, sdl) 28 register char *addr; 29 register struct sockaddr_dl *sdl; 30 { 31 register char *cp = sdl->sdl_data; 32 char *cplim = sdl->sdl_len + (char *)sdl; 33 register int byte = 0, state = NAMING, new; 34 35 bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); 36 sdl->sdl_family = AF_LINK; 37 do { 38 state &= ~LETTER; 39 if ((*addr >= '0') && (*addr <= '9')) { 40 new = *addr - '0'; 41 } else if ((*addr >= 'a') && (*addr <= 'f')) { 42 new = *addr - 'a' + 10; 43 } else if ((*addr >= 'A') && (*addr <= 'F')) { 44 new = *addr - 'A' + 10; 45 } else if (*addr == 0) { 46 state |= END; 47 } else if (state == NAMING && 48 (((*addr >= 'A') && (*addr <= 'Z')) || 49 ((*addr >= 'a') && (*addr <= 'z')))) 50 state |= LETTER; 51 else 52 state |= DELIM; 53 addr++; 54 switch (state /* | INPUT */) { 55 case NAMING | DIGIT: 56 case NAMING | LETTER: 57 *cp++ = addr[-1]; continue; 58 case NAMING | DELIM: 59 state = RESET; sdl->sdl_nlen = cp - sdl->sdl_data; continue; 60 case GOTTWO | DIGIT: 61 *cp++ = byte; /*FALLTHROUGH*/ 62 case RESET | DIGIT: 63 state = GOTONE; byte = new; continue; 64 case GOTONE | DIGIT: 65 state = GOTTWO; byte = new + (byte << 4); continue; 66 default: /* | DELIM */ 67 state = RESET; *cp++ = byte; byte = 0; continue; 68 case GOTONE | END: 69 case GOTTWO | END: 70 *cp++ = byte; /* FALLTHROUGH */ 71 case RESET | END: 72 break; 73 } 74 break; 75 } while (cp < cplim); 76 sdl->sdl_alen = cp - LLADDR(sdl); 77 new = cp - (char *)sdl; 78 if (new > sizeof(*sdl)) 79 sdl->sdl_len = new; 80 return; 81 } 82 83 static char hexlist[] = "0123456789abcdef"; 84 85 char * 86 link_ntoa(sdl) 87 register struct sockaddr_dl *sdl; 88 { 89 static char obuf[64]; 90 register char *out = obuf; 91 register int i; 92 register u_char *in = (u_char *)LLADDR(sdl); 93 u_char *inlim = in + sdl->sdl_nlen; 94 int firsttime = 1; 95 96 if (sdl->sdl_nlen) { 97 bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); 98 out += sdl->sdl_nlen; 99 *out++ = ':'; 100 } 101 while (in < inlim) { 102 if (firsttime) firsttime = 0; else *out++ = '.'; 103 i = *in++; 104 if (i > 0xf) { 105 out[1] = hexlist[i & 0xf]; 106 i >>= 4; 107 out[0] = hexlist[i]; 108 out += 2; 109 } else 110 *out++ = hexlist[i]; 111 } 112 *out = 0; 113 return(obuf); 114 } 115