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 __RCSID("$MirOS: src/lib/libc/net/linkaddr.c,v 1.6 2010/01/07 22:34:53 tg Exp $");
37
38 extern const uint8_t mbsd_digits_hex[17];
39
40 /* States*/
41 #define NAMING 0
42 #define GOTONE 1
43 #define GOTTWO 2
44 #define RESET 3
45 /* Inputs */
46 #define DIGIT (4*0)
47 #define END (4*1)
48 #define DELIM (4*2)
49 #define LETTER (4*3)
50
51 void
link_addr(const char * addr,struct sockaddr_dl * sdl)52 link_addr(const char *addr, struct sockaddr_dl *sdl)
53 {
54 char *cp = sdl->sdl_data;
55 char *cplim = sdl->sdl_len + (char *)sdl;
56 int byte = 0, state = NAMING;
57 size_t new = 0;
58
59 memset((char *)&sdl->sdl_family, 0, sdl->sdl_len - 1);
60 sdl->sdl_family = AF_LINK;
61 do {
62 state &= ~LETTER;
63 if ((*addr >= '0') && (*addr <= '9')) {
64 new = *addr - '0';
65 } else if ((*addr >= 'a') && (*addr <= 'f')) {
66 new = *addr - 'a' + 10;
67 } else if ((*addr >= 'A') && (*addr <= 'F')) {
68 new = *addr - 'A' + 10;
69 } else if (*addr == 0) {
70 state |= END;
71 } else if (state == NAMING &&
72 (((*addr >= 'A') && (*addr <= 'Z')) ||
73 ((*addr >= 'a') && (*addr <= 'z'))))
74 state |= LETTER;
75 else
76 state |= DELIM;
77 addr++;
78 switch (state /* | INPUT */) {
79 case NAMING | DIGIT:
80 case NAMING | LETTER:
81 *cp++ = addr[-1];
82 continue;
83 case NAMING | DELIM:
84 state = RESET;
85 sdl->sdl_nlen = cp - sdl->sdl_data;
86 continue;
87 case GOTTWO | DIGIT:
88 *cp++ = byte;
89 /* FALLTHROUGH */
90 case RESET | DIGIT:
91 state = GOTONE;
92 byte = new;
93 continue;
94 case GOTONE | DIGIT:
95 state = GOTTWO;
96 byte = new + (byte << 4);
97 continue;
98 default: /* | DELIM */
99 state = RESET;
100 *cp++ = byte;
101 byte = 0;
102 continue;
103 case GOTONE | END:
104 case GOTTWO | END:
105 *cp++ = byte;
106 /* FALLTHROUGH */
107 case RESET | END:
108 break;
109 }
110 break;
111 } while (cp < cplim);
112 sdl->sdl_alen = cp - LLADDR(sdl);
113 new = cp - (char *)sdl;
114 if (new > sizeof(*sdl))
115 sdl->sdl_len = new;
116 return;
117 }
118
119 char *
link_ntoa(const struct sockaddr_dl * sdl)120 link_ntoa(const struct sockaddr_dl *sdl)
121 {
122 static char obuf[64];
123 char *out = obuf;
124 int i;
125 u_char *in = (u_char *)LLADDR(sdl);
126 u_char *inlim = in + sdl->sdl_alen;
127 int firsttime = 1;
128
129 if (sdl->sdl_nlen) {
130 memmove(obuf, sdl->sdl_data, sdl->sdl_nlen);
131 out += sdl->sdl_nlen;
132 if (sdl->sdl_alen)
133 *out++ = ':';
134 }
135 while (in < inlim) {
136 if (firsttime)
137 firsttime = 0;
138 else
139 *out++ = '.';
140 i = *in++;
141 if (i > 0xf) {
142 out[1] = mbsd_digits_hex[i & 0xf];
143 i >>= 4;
144 out[0] = mbsd_digits_hex[i];
145 out += 2;
146 } else
147 *out++ = mbsd_digits_hex[i];
148 }
149 *out = 0;
150 return (obuf);
151 }
152