Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file macaddr.c |
| 2 | * MAC address utility routines. */ |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 3 | /* |
| 4 | * (C) 2013-2014 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2014 by Holger Hans Peter Freyther |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | * |
| 25 | */ |
| 26 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 27 | /*! \addtogroup utils |
| 28 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 29 | * \file macaddr.c */ |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 30 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 31 | #include "config.h" |
| 32 | |
Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
Harald Welte | 78b0868 | 2014-08-21 02:37:57 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 37 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 38 | /*! Parse a MAC address from human-readable notation |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 39 | * This function parses an ethernet MAC address in the commonly-used |
| 40 | * hex/colon notation (00:00:00:00:00:00) and generates the binary |
| 41 | * representation from it. |
| 42 | * \param[out] out pointer to caller-allocated buffer of 6 bytes |
| 43 | * \param[in] in pointer to input data as string with hex/colon notation |
| 44 | */ |
Harald Welte | 40d56f9 | 2014-08-18 19:03:40 +0200 | [diff] [blame] | 45 | int osmo_macaddr_parse(uint8_t *out, const char *in) |
| 46 | { |
| 47 | /* 00:00:00:00:00:00 */ |
| 48 | char tmp[18]; |
| 49 | char *tok; |
| 50 | unsigned int i = 0; |
| 51 | |
| 52 | if (strlen(in) < 17) |
| 53 | return -1; |
| 54 | |
| 55 | strncpy(tmp, in, sizeof(tmp)-1); |
| 56 | tmp[sizeof(tmp)-1] = '\0'; |
| 57 | |
| 58 | for (tok = strtok(tmp, ":"); tok && (i < 6); tok = strtok(NULL, ":")) { |
| 59 | unsigned long ul = strtoul(tok, NULL, 16); |
| 60 | out[i++] = ul & 0xff; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 65 | |
Holger Hans Peter Freyther | 1885395 | 2015-04-11 19:29:52 +0200 | [diff] [blame] | 66 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Holger Hans Peter Freyther | efe0100 | 2014-08-21 14:14:38 +0200 | [diff] [blame] | 67 | #include <sys/socket.h> |
| 68 | #include <sys/types.h> |
| 69 | #include <ifaddrs.h> |
| 70 | #include <net/if_dl.h> |
| 71 | #include <net/if_types.h> |
| 72 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 73 | /*! Obtain the MAC address of a given network device |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 74 | * \param[out] mac_out pointer to caller-allocated buffer of 6 bytes |
| 75 | * \param[in] dev_name string name of the network device |
| 76 | * \returns 0 in case of success; negative otherwise |
| 77 | */ |
Holger Hans Peter Freyther | efe0100 | 2014-08-21 14:14:38 +0200 | [diff] [blame] | 78 | int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) |
| 79 | { |
| 80 | int rc = -1; |
| 81 | struct ifaddrs *ifa, *ifaddr; |
| 82 | |
| 83 | if (getifaddrs(&ifaddr) != 0) |
| 84 | return -1; |
| 85 | |
| 86 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 87 | struct sockaddr_dl *sdl; |
| 88 | |
| 89 | sdl = (struct sockaddr_dl *) ifa->ifa_addr; |
| 90 | if (!sdl) |
| 91 | continue; |
| 92 | if (sdl->sdl_family != AF_LINK) |
| 93 | continue; |
| 94 | if (sdl->sdl_type != IFT_ETHER) |
| 95 | continue; |
| 96 | if (strcmp(ifa->ifa_name, dev_name) != 0) |
| 97 | continue; |
| 98 | |
| 99 | memcpy(mac_out, LLADDR(sdl), 6); |
| 100 | rc = 0; |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | freeifaddrs(ifaddr); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | #else |
| 109 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 110 | #if (!EMBEDDED) |
| 111 | |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 112 | #include <sys/ioctl.h> |
| 113 | #include <net/if.h> |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 114 | #include <netinet/in.h> |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 115 | #include <netinet/ip.h> |
| 116 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 117 | /*! Obtain the MAC address of a given network device |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 118 | * \param[out] mac_out pointer to caller-allocated buffer of 6 bytes |
| 119 | * \param[in] dev_name string name of the network device |
| 120 | * \returns 0 in case of success; negative otherwise |
| 121 | */ |
Harald Welte | fe3e42b | 2014-08-18 19:19:45 +0200 | [diff] [blame] | 122 | int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) |
| 123 | { |
| 124 | int fd, rc; |
| 125 | struct ifreq ifr; |
| 126 | |
| 127 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 128 | if (fd < 0) |
| 129 | return fd; |
| 130 | |
| 131 | memset(&ifr, 0, sizeof(ifr)); |
| 132 | memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name)); |
| 133 | rc = ioctl(fd, SIOCGIFHWADDR, &ifr); |
| 134 | close(fd); |
| 135 | |
| 136 | if (rc < 0) |
| 137 | return rc; |
| 138 | |
| 139 | memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6); |
| 140 | |
| 141 | return 0; |
| 142 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 143 | #endif /* !EMBEDDED */ |
| 144 | |
Holger Hans Peter Freyther | efe0100 | 2014-08-21 14:14:38 +0200 | [diff] [blame] | 145 | #endif |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 146 | |
| 147 | /*! @} */ |