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