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