xref: /minix3/lib/libc/net/linkaddr.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: linkaddr.c,v 1.16 2012/03/20 17:44:18 matt Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)linkaddr.c	8.1 (Berkeley) 6/4/93";
362fe8fb19SBen Gras #else
37*f14fb602SLionel Sambuc __RCSID("$NetBSD: linkaddr.c,v 1.16 2012/03/20 17:44:18 matt Exp $");
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #include <sys/types.h>
422fe8fb19SBen Gras #include <sys/socket.h>
432fe8fb19SBen Gras #include <net/if_dl.h>
442fe8fb19SBen Gras 
452fe8fb19SBen Gras #include <assert.h>
462fe8fb19SBen Gras #include <string.h>
472fe8fb19SBen Gras 
482fe8fb19SBen Gras /* States*/
492fe8fb19SBen Gras #define NAMING	0
502fe8fb19SBen Gras #define GOTONE	1
512fe8fb19SBen Gras #define GOTTWO	2
522fe8fb19SBen Gras #define RESET	3
532fe8fb19SBen Gras /* Inputs */
542fe8fb19SBen Gras #define	DIGIT	(4*0)
552fe8fb19SBen Gras #define	END	(4*1)
562fe8fb19SBen Gras #define DELIM	(4*2)
572fe8fb19SBen Gras #define LETTER	(4*3)
582fe8fb19SBen Gras 
592fe8fb19SBen Gras void
link_addr(const char * addr,struct sockaddr_dl * sdl)60*f14fb602SLionel Sambuc link_addr(const char *addr, struct sockaddr_dl *sdl)
612fe8fb19SBen Gras {
622fe8fb19SBen Gras 	register char *cp = sdl->sdl_data;
632fe8fb19SBen Gras 	char *cplim = sdl->sdl_len + (char *)(void *)sdl;
64*f14fb602SLionel Sambuc 	int byte = 0, state = NAMING;
65*f14fb602SLionel Sambuc 	size_t newaddr = 0;	/* pacify gcc */
662fe8fb19SBen Gras 
672fe8fb19SBen Gras 	_DIAGASSERT(addr != NULL);
682fe8fb19SBen Gras 	_DIAGASSERT(sdl != NULL);
692fe8fb19SBen Gras 
702fe8fb19SBen Gras 	(void)memset(&sdl->sdl_family, 0, (size_t)sdl->sdl_len - 1);
712fe8fb19SBen Gras 	sdl->sdl_family = AF_LINK;
722fe8fb19SBen Gras 	do {
732fe8fb19SBen Gras 		state &= ~LETTER;
742fe8fb19SBen Gras 		if ((*addr >= '0') && (*addr <= '9')) {
752fe8fb19SBen Gras 			newaddr = *addr - '0';
762fe8fb19SBen Gras 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
772fe8fb19SBen Gras 			newaddr = *addr - 'a' + 10;
782fe8fb19SBen Gras 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
792fe8fb19SBen Gras 			newaddr = *addr - 'A' + 10;
802fe8fb19SBen Gras 		} else if (*addr == 0) {
812fe8fb19SBen Gras 			state |= END;
822fe8fb19SBen Gras 		} else if (state == NAMING &&
832fe8fb19SBen Gras 			   (((*addr >= 'A') && (*addr <= 'Z')) ||
842fe8fb19SBen Gras 			   ((*addr >= 'a') && (*addr <= 'z'))))
852fe8fb19SBen Gras 			state |= LETTER;
862fe8fb19SBen Gras 		else
872fe8fb19SBen Gras 			state |= DELIM;
882fe8fb19SBen Gras 		addr++;
892fe8fb19SBen Gras 		switch (state /* | INPUT */) {
902fe8fb19SBen Gras 		case NAMING | DIGIT:
912fe8fb19SBen Gras 		case NAMING | LETTER:
922fe8fb19SBen Gras 			*cp++ = addr[-1];
932fe8fb19SBen Gras 			continue;
942fe8fb19SBen Gras 		case NAMING | DELIM:
952fe8fb19SBen Gras 			state = RESET;
96*f14fb602SLionel Sambuc 			_DIAGASSERT(__type_fit(uint8_t, cp - sdl->sdl_data));
97*f14fb602SLionel Sambuc 			sdl->sdl_nlen = (uint8_t)(cp - sdl->sdl_data);
982fe8fb19SBen Gras 			continue;
992fe8fb19SBen Gras 		case GOTTWO | DIGIT:
1002fe8fb19SBen Gras 			*cp++ = byte;
1012fe8fb19SBen Gras 			/* FALLTHROUGH */
1022fe8fb19SBen Gras 		case RESET | DIGIT:
1032fe8fb19SBen Gras 			state = GOTONE;
104*f14fb602SLionel Sambuc 			byte = (int)newaddr;
1052fe8fb19SBen Gras 			continue;
1062fe8fb19SBen Gras 		case GOTONE | DIGIT:
1072fe8fb19SBen Gras 			state = GOTTWO;
108*f14fb602SLionel Sambuc 			byte = (int)newaddr + (byte << 4);
1092fe8fb19SBen Gras 			continue;
1102fe8fb19SBen Gras 		default: /* | DELIM */
1112fe8fb19SBen Gras 			state = RESET;
1122fe8fb19SBen Gras 			*cp++ = byte;
1132fe8fb19SBen Gras 			byte = 0;
1142fe8fb19SBen Gras 			continue;
1152fe8fb19SBen Gras 		case GOTONE | END:
1162fe8fb19SBen Gras 		case GOTTWO | END:
1172fe8fb19SBen Gras 			*cp++ = byte;
1182fe8fb19SBen Gras 			/* FALLTHROUGH */
1192fe8fb19SBen Gras 		case RESET | END:
1202fe8fb19SBen Gras 			break;
1212fe8fb19SBen Gras 		}
1222fe8fb19SBen Gras 		break;
1232fe8fb19SBen Gras 	} while (cp < cplim);
124*f14fb602SLionel Sambuc 
125*f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(uint8_t, cp - LLADDR(sdl)));
126*f14fb602SLionel Sambuc 	sdl->sdl_alen = (uint8_t)(cp - LLADDR(sdl));
1272fe8fb19SBen Gras 	newaddr = cp - (char *)(void *)sdl;
128*f14fb602SLionel Sambuc 	if (newaddr > sizeof(*sdl)) {
129*f14fb602SLionel Sambuc 		_DIAGASSERT(__type_fit(uint8_t, newaddr));
130*f14fb602SLionel Sambuc 		sdl->sdl_len = (uint8_t)newaddr;
131*f14fb602SLionel Sambuc 	}
1322fe8fb19SBen Gras 	return;
1332fe8fb19SBen Gras }
1342fe8fb19SBen Gras 
1352fe8fb19SBen Gras static const char hexlist[16] = "0123456789abcdef";
1362fe8fb19SBen Gras 
1372fe8fb19SBen Gras char *
link_ntoa(const struct sockaddr_dl * sdl)138*f14fb602SLionel Sambuc link_ntoa(const struct sockaddr_dl *sdl)
1392fe8fb19SBen Gras {
1402fe8fb19SBen Gras 	static char obuf[64];
1412fe8fb19SBen Gras 	register char *out = obuf;
1422fe8fb19SBen Gras 	register size_t i;
1432fe8fb19SBen Gras 	const u_char *in = (const u_char *)CLLADDR(sdl);
1442fe8fb19SBen Gras 	const u_char *inlim = in + sdl->sdl_alen;
1452fe8fb19SBen Gras 	int firsttime = 1;
1462fe8fb19SBen Gras 
1472fe8fb19SBen Gras 	_DIAGASSERT(sdl != NULL);
1482fe8fb19SBen Gras 
1492fe8fb19SBen Gras 	if (sdl->sdl_nlen) {
1502fe8fb19SBen Gras 		(void)memcpy(obuf, sdl->sdl_data, (size_t)sdl->sdl_nlen);
1512fe8fb19SBen Gras 		out += sdl->sdl_nlen;
1522fe8fb19SBen Gras 		if (sdl->sdl_alen)
1532fe8fb19SBen Gras 			*out++ = ':';
1542fe8fb19SBen Gras 	}
1552fe8fb19SBen Gras 	while (in < inlim) {
1562fe8fb19SBen Gras 		if (firsttime)
1572fe8fb19SBen Gras 			firsttime = 0;
1582fe8fb19SBen Gras 		else
1592fe8fb19SBen Gras 			*out++ = '.';
1602fe8fb19SBen Gras 		i = *in++;
1612fe8fb19SBen Gras 		if (i > 0xf) {
1622fe8fb19SBen Gras 			out[1] = hexlist[i & 0xf];
1632fe8fb19SBen Gras 			i >>= 4;
1642fe8fb19SBen Gras 			out[0] = hexlist[i];
1652fe8fb19SBen Gras 			out += 2;
1662fe8fb19SBen Gras 		} else
1672fe8fb19SBen Gras 			*out++ = hexlist[i];
1682fe8fb19SBen Gras 	}
1692fe8fb19SBen Gras 	*out = 0;
1702fe8fb19SBen Gras 	return (obuf);
1712fe8fb19SBen Gras }
172