functions

Link to this paste: http://bugs.dragonflybsd.org/pastes/428

Added by tuxillo over 1 year ago.
Syntax: Plain Text

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
#include <stdio.h>
#include <net/ethernet.h>
#include <sys/types.h>

struct ether_addr *
kether_aton(const char *macstr, struct ether_addr *addr)
{
        unsigned int o0, o1, o2, o3, o4, o5;
        int n;

        if (macstr == NULL || addr == NULL)
                return NULL;

        n = sscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2,
            &o3, &o4, &o5);
        if (n != 6)
                return NULL;

        addr->octet[0] = o0;
        addr->octet[1] = o1;
        addr->octet[2] = o2;
        addr->octet[3] = o3;
        addr->octet[4] = o4;
        addr->octet[5] = o5;

        return addr;
}

char *
kether_ntoa(const struct ether_addr *addr, char *buf)
{
        int len = 3 * ETHER_ADDR_LEN;
        int n;

        n = snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr->octet[0],
            addr->octet[1], addr->octet[2], addr->octet[3], addr->octet[4], addr->octet[5]);

        if (n < 17)
                return NULL;

        return buf;
}

int
main(void)
{
        struct ether_addr hwaddr;
        char buf[18];
        const char mac[] = "aa:bb:cc:dd:ee:ff";

        kether_aton(mac, &hwaddr);


        fprintf(stdout, "%s\n", kether_ntoa(&hwaddr, buf));
        return 0;
}

Download