linkaddr.c revision 1.7 1 /* $NetBSD: linkaddr.c,v 1.7 1998/07/26 13:03:43 mycroft 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 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)linkaddr.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: linkaddr.c,v 1.7 1998/07/26 13:03:43 mycroft Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <net/if_dl.h>
48 #include <string.h>
49
50 /* States*/
51 #define NAMING 0
52 #define GOTONE 1
53 #define GOTTWO 2
54 #define RESET 3
55 /* Inputs */
56 #define DIGIT (4*0)
57 #define END (4*1)
58 #define DELIM (4*2)
59 #define LETTER (4*3)
60
61 void
62 link_addr(addr, sdl)
63 register const char *addr;
64 register struct sockaddr_dl *sdl;
65 {
66 register char *cp = sdl->sdl_data;
67 char *cplim = sdl->sdl_len + (char *)sdl;
68 register int byte = 0, state = NAMING;
69 register int newaddr = 0; /* pacify gcc */
70
71 bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1);
72 sdl->sdl_family = AF_LINK;
73 do {
74 state &= ~LETTER;
75 if ((*addr >= '0') && (*addr <= '9')) {
76 newaddr = *addr - '0';
77 } else if ((*addr >= 'a') && (*addr <= 'f')) {
78 newaddr = *addr - 'a' + 10;
79 } else if ((*addr >= 'A') && (*addr <= 'F')) {
80 newaddr = *addr - 'A' + 10;
81 } else if (*addr == 0) {
82 state |= END;
83 } else if (state == NAMING &&
84 (((*addr >= 'A') && (*addr <= 'Z')) ||
85 ((*addr >= 'a') && (*addr <= 'z'))))
86 state |= LETTER;
87 else
88 state |= DELIM;
89 addr++;
90 switch (state /* | INPUT */) {
91 case NAMING | DIGIT:
92 case NAMING | LETTER:
93 *cp++ = addr[-1];
94 continue;
95 case NAMING | DELIM:
96 state = RESET;
97 sdl->sdl_nlen = cp - sdl->sdl_data;
98 continue;
99 case GOTTWO | DIGIT:
100 *cp++ = byte;
101 /* FALLTHROUGH */
102 case RESET | DIGIT:
103 state = GOTONE;
104 byte = newaddr;
105 continue;
106 case GOTONE | DIGIT:
107 state = GOTTWO;
108 byte = newaddr + (byte << 4);
109 continue;
110 default: /* | DELIM */
111 state = RESET;
112 *cp++ = byte;
113 byte = 0;
114 continue;
115 case GOTONE | END:
116 case GOTTWO | END:
117 *cp++ = byte;
118 /* FALLTHROUGH */
119 case RESET | END:
120 break;
121 }
122 break;
123 } while (cp < cplim);
124 sdl->sdl_alen = cp - LLADDR(sdl);
125 newaddr = cp - (char *)sdl;
126 if (newaddr > sizeof(*sdl))
127 sdl->sdl_len = newaddr;
128 return;
129 }
130
131 static const char hexlist[16] = "0123456789abcdef";
132
133 char *
134 link_ntoa(sdl)
135 register const struct sockaddr_dl *sdl;
136 {
137 static char obuf[64];
138 register char *out = obuf;
139 register int i;
140 register u_char *in = (u_char *)LLADDR(sdl);
141 u_char *inlim = in + sdl->sdl_alen;
142 int firsttime = 1;
143
144 if (sdl->sdl_nlen) {
145 bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen);
146 out += sdl->sdl_nlen;
147 if (sdl->sdl_alen)
148 *out++ = ':';
149 }
150 while (in < inlim) {
151 if (firsttime)
152 firsttime = 0;
153 else
154 *out++ = '.';
155 i = *in++;
156 if (i > 0xf) {
157 out[1] = hexlist[i & 0xf];
158 i >>= 4;
159 out[0] = hexlist[i];
160 out += 2;
161 } else
162 *out++ = hexlist[i];
163 }
164 *out = 0;
165 return (obuf);
166 }
167