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