summaryrefslogtreecommitdiffhomepage
path: root/libbb/in_ether.c
blob: dadadbafef66fe46b721b57666de177de3321c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* vi: set sw=4 ts=4: */
/*
 * Utility routines.
 */

//kbuild:lib-$(CONFIG_IFCONFIG) += in_ether.o
//kbuild:lib-$(CONFIG_IFENSLAVE) += in_ether.o

#include "libbb.h"
#include <net/if_arp.h>
#include <net/ethernet.h>

/* Convert Ethernet address from "XX[:]XX[:]XX[:]XX[:]XX[:]XX" to sockaddr.
 * Return nonzero on error.
 */
int FAST_FUNC in_ether(const char *bufp, struct sockaddr *sap)
{
	char *ptr;
	int i, j;
	unsigned char val;
	unsigned char c;

	sap->sa_family = ARPHRD_ETHER;
	ptr = (char *) sap->sa_data;

	i = ETH_ALEN;
	goto first;
	do {
		/* We might get a semicolon here */
		if (*bufp == ':')
			bufp++;
 first:
		j = val = 0;
		do {
			c = *bufp;
			if (((unsigned char)(c - '0')) <= 9) {
				c -= '0';
			} else if ((unsigned char)((c|0x20) - 'a') <= 5) {
				c = (unsigned char)((c|0x20) - 'a') + 10;
			} else {
				if (j && (c == ':' || c == '\0'))
					/* One-digit byte: __:X:__ */
					break;
				return -1;
			}
			++bufp;
			val <<= 4;
			val += c;
			j ^= 1;
		} while (j);

		*ptr++ = val;

	} while (--i);

	/* Error if we aren't at end of string */
	return *bufp;
}