-
Notifications
You must be signed in to change notification settings - Fork 0
/
ether_aton.c
executable file
·71 lines (62 loc) · 1.39 KB
/
ether_aton.c
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
59
60
61
62
63
64
65
66
67
68
69
70
/*
* ether_aton.c
*
* Created on: Feb 9, 2015
* Author: root
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
//#include <net/if_ether.h>
#include <net/ethernet.h>
#include <ctype.h>
static inline int
xdigit (char c) {
unsigned d;
d = (unsigned)(c-'0');
if (d < 10) return (int)d;
d = (unsigned)(c-'a');
if (d < 6) return (int)(10+d);
d = (unsigned)(c-'A');
if (d < 6) return (int)(10+d);
return -1;
}
/*
* Convert Ethernet address in the standard hex-digits-and-colons to binary
* representation.
* Re-entrant version (GNU extensions)
*/
struct ether_addr *
ether_aton_r (const char *asc, struct ether_addr * addr)
{
int i, val0, val1;
for (i = 0; i < ETHER_ADDR_LEN; ++i) {
val0 = xdigit(*asc);
asc++;
if (val0 < 0)
return NULL;
val1 = xdigit(*asc);
asc++;
if (val1 < 0)
return NULL;
addr->ether_addr_octet[i] = (u_int8_t)((val0 << 4) + val1);
if (i < ETHER_ADDR_LEN - 1) {
if (*asc != ':')
return NULL;
asc++;
}
}
if (*asc != '\0')
return NULL;
return addr;
}
/*
* Convert Ethernet address in the standard hex-digits-and-colons to binary
* representation.
*/
struct ether_addr *
ether_aton (const char *asc)
{
static struct ether_addr addr;
return ether_aton_r(asc, &addr);
}