Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2013-2014 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2014 by Holger Hans Peter Freyther |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
Harald Welte | 78b0868 | 2014-08-21 02:37:57 +0200 | [diff] [blame] | 26 | #include <unistd.h> |
Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 27 | |
| 28 | |
| 29 | int osmo_macaddr_parse(uint8_t *out, const char *in) |
| 30 | { |
| 31 | /* 00:00:00:00:00:00 */ |
| 32 | char tmp[18]; |
| 33 | char *tok; |
| 34 | unsigned int i = 0; |
| 35 | |
| 36 | if (strlen(in) < 17) |
| 37 | return -1; |
| 38 | |
| 39 | strncpy(tmp, in, sizeof(tmp)-1); |
| 40 | tmp[sizeof(tmp)-1] = '\0'; |
| 41 | |
| 42 | for (tok = strtok(tmp, ":"); tok && (i < 6); tok = strtok(NULL, ":")) { |
| 43 | unsigned long ul = strtoul(tok, NULL, 16); |
| 44 | out[i++] = ul & 0xff; |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 49 | |
Holger Hans Peter Freyther | 1885395 | 2015-04-11 19:29:52 +0200 | [diff] [blame] | 50 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Holger Hans Peter Freyther | efe0100 | 2014-08-21 14:14:38 +0200 | [diff] [blame] | 51 | #include <sys/socket.h> |
| 52 | #include <sys/types.h> |
| 53 | #include <ifaddrs.h> |
| 54 | #include <net/if_dl.h> |
| 55 | #include <net/if_types.h> |
| 56 | |
| 57 | |
| 58 | int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) |
| 59 | { |
| 60 | int rc = -1; |
| 61 | struct ifaddrs *ifa, *ifaddr; |
| 62 | |
| 63 | if (getifaddrs(&ifaddr) != 0) |
| 64 | return -1; |
| 65 | |
| 66 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 67 | struct sockaddr_dl *sdl; |
| 68 | |
| 69 | sdl = (struct sockaddr_dl *) ifa->ifa_addr; |
| 70 | if (!sdl) |
| 71 | continue; |
| 72 | if (sdl->sdl_family != AF_LINK) |
| 73 | continue; |
| 74 | if (sdl->sdl_type != IFT_ETHER) |
| 75 | continue; |
| 76 | if (strcmp(ifa->ifa_name, dev_name) != 0) |
| 77 | continue; |
| 78 | |
| 79 | memcpy(mac_out, LLADDR(sdl), 6); |
| 80 | rc = 0; |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | freeifaddrs(ifaddr); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | #else |
| 89 | |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 90 | #include <sys/ioctl.h> |
| 91 | #include <net/if.h> |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 92 | #include <netinet/in.h> |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 93 | #include <netinet/ip.h> |
| 94 | |
| 95 | int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) |
| 96 | { |
| 97 | int fd, rc; |
| 98 | struct ifreq ifr; |
| 99 | |
| 100 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 101 | if (fd < 0) |
| 102 | return fd; |
| 103 | |
| 104 | memset(&ifr, 0, sizeof(ifr)); |
| 105 | memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name)); |
| 106 | rc = ioctl(fd, SIOCGIFHWADDR, &ifr); |
| 107 | close(fd); |
| 108 | |
| 109 | if (rc < 0) |
| 110 | return rc; |
| 111 | |
| 112 | memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6); |
| 113 | |
| 114 | return 0; |
| 115 | } |
Holger Hans Peter Freyther | efe0100 | 2014-08-21 14:14:38 +0200 | [diff] [blame] | 116 | #endif |