blob: 392ee13f325d9f67a129c94013afabaf6f6ac4c2 [file] [log] [blame]
Harald Welte40d56f92014-08-18 19:03:40 +02001#include <stdint.h>
2#include <string.h>
3#include <stdlib.h>
Harald Welte78b08682014-08-21 02:37:57 +02004#include <unistd.h>
Harald Welte40d56f92014-08-18 19:03:40 +02005
6
7int osmo_macaddr_parse(uint8_t *out, const char *in)
8{
9 /* 00:00:00:00:00:00 */
10 char tmp[18];
11 char *tok;
12 unsigned int i = 0;
13
14 if (strlen(in) < 17)
15 return -1;
16
17 strncpy(tmp, in, sizeof(tmp)-1);
18 tmp[sizeof(tmp)-1] = '\0';
19
20 for (tok = strtok(tmp, ":"); tok && (i < 6); tok = strtok(NULL, ":")) {
21 unsigned long ul = strtoul(tok, NULL, 16);
22 out[i++] = ul & 0xff;
23 }
24
25 return 0;
26}
Harald Weltefe3e42b2014-08-18 19:19:45 +020027
28#include <sys/ioctl.h>
29#include <net/if.h>
30#include <netinet/ip.h>
31
32int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
33{
34 int fd, rc;
35 struct ifreq ifr;
36
37 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
38 if (fd < 0)
39 return fd;
40
41 memset(&ifr, 0, sizeof(ifr));
42 memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
43 rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
44 close(fd);
45
46 if (rc < 0)
47 return rc;
48
49 memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6);
50
51 return 0;
52}