xref: /netbsd/lib/libc/net/linkaddr.c (revision c4a72b64)
1 /*	$NetBSD: linkaddr.c,v 1.12 2002/11/11 18:07:52 thorpej 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.12 2002/11/11 18:07:52 thorpej 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 
49 #include <assert.h>
50 #include <string.h>
51 
52 /* States*/
53 #define NAMING	0
54 #define GOTONE	1
55 #define GOTTWO	2
56 #define RESET	3
57 /* Inputs */
58 #define	DIGIT	(4*0)
59 #define	END	(4*1)
60 #define DELIM	(4*2)
61 #define LETTER	(4*3)
62 
63 void
64 link_addr(addr, sdl)
65 	register const char *addr;
66 	register struct sockaddr_dl *sdl;
67 {
68 	register char *cp = sdl->sdl_data;
69 	char *cplim = sdl->sdl_len + (char *)(void *)sdl;
70 	register int byte = 0, state = NAMING;
71 	register int newaddr = 0;	/* pacify gcc */
72 
73 	_DIAGASSERT(addr != NULL);
74 	_DIAGASSERT(sdl != NULL);
75 
76 	(void)memset(&sdl->sdl_family, 0, (size_t)sdl->sdl_len - 1);
77 	sdl->sdl_family = AF_LINK;
78 	do {
79 		state &= ~LETTER;
80 		if ((*addr >= '0') && (*addr <= '9')) {
81 			newaddr = *addr - '0';
82 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
83 			newaddr = *addr - 'a' + 10;
84 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
85 			newaddr = *addr - 'A' + 10;
86 		} else if (*addr == 0) {
87 			state |= END;
88 		} else if (state == NAMING &&
89 			   (((*addr >= 'A') && (*addr <= 'Z')) ||
90 			   ((*addr >= 'a') && (*addr <= 'z'))))
91 			state |= LETTER;
92 		else
93 			state |= DELIM;
94 		addr++;
95 		switch (state /* | INPUT */) {
96 		case NAMING | DIGIT:
97 		case NAMING | LETTER:
98 			*cp++ = addr[-1];
99 			continue;
100 		case NAMING | DELIM:
101 			state = RESET;
102 			sdl->sdl_nlen = cp - sdl->sdl_data;
103 			continue;
104 		case GOTTWO | DIGIT:
105 			*cp++ = byte;
106 			/* FALLTHROUGH */
107 		case RESET | DIGIT:
108 			state = GOTONE;
109 			byte = newaddr;
110 			continue;
111 		case GOTONE | DIGIT:
112 			state = GOTTWO;
113 			byte = newaddr + (byte << 4);
114 			continue;
115 		default: /* | DELIM */
116 			state = RESET;
117 			*cp++ = byte;
118 			byte = 0;
119 			continue;
120 		case GOTONE | END:
121 		case GOTTWO | END:
122 			*cp++ = byte;
123 			/* FALLTHROUGH */
124 		case RESET | END:
125 			break;
126 		}
127 		break;
128 	} while (cp < cplim);
129 	sdl->sdl_alen = cp - LLADDR(sdl);
130 	newaddr = cp - (char *)(void *)sdl;
131 	if ((size_t) newaddr > sizeof(*sdl))
132 		sdl->sdl_len = newaddr;
133 	return;
134 }
135 
136 static const char hexlist[16] = "0123456789abcdef";
137 
138 char *
139 link_ntoa(sdl)
140 	register const struct sockaddr_dl *sdl;
141 {
142 	static char obuf[64];
143 	register char *out = obuf;
144 	register size_t i;
145 	register u_char *in = (u_char *)LLADDR(sdl);
146 	u_char *inlim = in + sdl->sdl_alen;
147 	int firsttime = 1;
148 
149 	_DIAGASSERT(sdl != NULL);
150 
151 	if (sdl->sdl_nlen) {
152 		(void)memcpy(obuf, sdl->sdl_data, (size_t)sdl->sdl_nlen);
153 		out += sdl->sdl_nlen;
154 		if (sdl->sdl_alen)
155 			*out++ = ':';
156 	}
157 	while (in < inlim) {
158 		if (firsttime)
159 			firsttime = 0;
160 		else
161 			*out++ = '.';
162 		i = *in++;
163 		if (i > 0xf) {
164 			out[1] = hexlist[i & 0xf];
165 			i >>= 4;
166 			out[0] = hexlist[i];
167 			out += 2;
168 		} else
169 			*out++ = hexlist[i];
170 	}
171 	*out = 0;
172 	return (obuf);
173 }
174