blob: f83e0546efeff4dd41d26300e79e1b5040d8294b [file] [log] [blame]
Harald Welte468b6432014-09-11 13:05:51 +08001/*
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 Welte2d2e2cc2016-04-25 12:11:20 +020023/*! \addtogroup utils
24 * @{
25 */
26
27/*! \file loggingrb.c */
28
Harald Welte40d56f92014-08-18 19:03:40 +020029#include <stdint.h>
30#include <string.h>
31#include <stdlib.h>
Harald Welte78b08682014-08-21 02:37:57 +020032#include <unistd.h>
Harald Welte40d56f92014-08-18 19:03:40 +020033
Harald Welte2d2e2cc2016-04-25 12:11:20 +020034/*! \brief Parse a MAC address from human-readable notation
35 * This function parses an ethernet MAC address in the commonly-used
36 * hex/colon notation (00:00:00:00:00:00) and generates the binary
37 * representation from it.
38 * \param[out] out pointer to caller-allocated buffer of 6 bytes
39 * \param[in] in pointer to input data as string with hex/colon notation
40 */
Harald Welte40d56f92014-08-18 19:03:40 +020041int osmo_macaddr_parse(uint8_t *out, const char *in)
42{
43 /* 00:00:00:00:00:00 */
44 char tmp[18];
45 char *tok;
46 unsigned int i = 0;
47
48 if (strlen(in) < 17)
49 return -1;
50
51 strncpy(tmp, in, sizeof(tmp)-1);
52 tmp[sizeof(tmp)-1] = '\0';
53
54 for (tok = strtok(tmp, ":"); tok && (i < 6); tok = strtok(NULL, ":")) {
55 unsigned long ul = strtoul(tok, NULL, 16);
56 out[i++] = ul & 0xff;
57 }
58
59 return 0;
60}
Harald Weltefe3e42b2014-08-18 19:19:45 +020061
Holger Hans Peter Freyther18853952015-04-11 19:29:52 +020062#if defined(__FreeBSD__) || defined(__APPLE__)
Holger Hans Peter Freytherefe01002014-08-21 14:14:38 +020063#include <sys/socket.h>
64#include <sys/types.h>
65#include <ifaddrs.h>
66#include <net/if_dl.h>
67#include <net/if_types.h>
68
Harald Welte2d2e2cc2016-04-25 12:11:20 +020069/*! \brief Obtain the MAC address of a given network device
70 * \param[out] mac_out pointer to caller-allocated buffer of 6 bytes
71 * \param[in] dev_name string name of the network device
72 * \returns 0 in case of success; negative otherwise
73 */
Holger Hans Peter Freytherefe01002014-08-21 14:14:38 +020074int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
75{
76 int rc = -1;
77 struct ifaddrs *ifa, *ifaddr;
78
79 if (getifaddrs(&ifaddr) != 0)
80 return -1;
81
82 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
83 struct sockaddr_dl *sdl;
84
85 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
86 if (!sdl)
87 continue;
88 if (sdl->sdl_family != AF_LINK)
89 continue;
90 if (sdl->sdl_type != IFT_ETHER)
91 continue;
92 if (strcmp(ifa->ifa_name, dev_name) != 0)
93 continue;
94
95 memcpy(mac_out, LLADDR(sdl), 6);
96 rc = 0;
97 break;
98 }
99
100 freeifaddrs(ifaddr);
101 return 0;
102}
103
104#else
105
Harald Weltefe3e42b2014-08-18 19:19:45 +0200106#include <sys/ioctl.h>
107#include <net/if.h>
Harald Weltee15ac062014-12-04 14:15:36 +0100108#include <netinet/in.h>
Harald Weltefe3e42b2014-08-18 19:19:45 +0200109#include <netinet/ip.h>
110
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200111/*! \brief Obtain the MAC address of a given network device
112 * \param[out] mac_out pointer to caller-allocated buffer of 6 bytes
113 * \param[in] dev_name string name of the network device
114 * \returns 0 in case of success; negative otherwise
115 */
Harald Weltefe3e42b2014-08-18 19:19:45 +0200116int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
117{
118 int fd, rc;
119 struct ifreq ifr;
120
121 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
122 if (fd < 0)
123 return fd;
124
125 memset(&ifr, 0, sizeof(ifr));
126 memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
127 rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
128 close(fd);
129
130 if (rc < 0)
131 return rc;
132
133 memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6);
134
135 return 0;
136}
Holger Hans Peter Freytherefe01002014-08-21 14:14:38 +0200137#endif
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200138
139/*! @} */