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