Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
Harald Welte | 78b0868 | 2014-08-21 02:37:57 +0200 | [diff] [blame^] | 4 | #include <unistd.h> |
Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 5 | |
| 6 | |
| 7 | int 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 Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 27 | |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <net/if.h> |
| 30 | #include <netinet/ip.h> |
| 31 | |
| 32 | int 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 | } |