blob: 7bc440174513e0b4644056f61daa866c9c430513 [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 Welte40d56f92014-08-18 19:03:40 +020023#include <stdint.h>
24#include <string.h>
25#include <stdlib.h>
Harald Welte78b08682014-08-21 02:37:57 +020026#include <unistd.h>
Harald Welte40d56f92014-08-18 19:03:40 +020027
28
29int 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 Weltefe3e42b2014-08-18 19:19:45 +020049
Holger Hans Peter Freytherefe01002014-08-21 14:14:38 +020050#if defined(__FreeBSD__)
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
58int 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 Weltefe3e42b2014-08-18 19:19:45 +020090#include <sys/ioctl.h>
91#include <net/if.h>
92#include <netinet/ip.h>
93
94int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
95{
96 int fd, rc;
97 struct ifreq ifr;
98
99 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
100 if (fd < 0)
101 return fd;
102
103 memset(&ifr, 0, sizeof(ifr));
104 memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
105 rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
106 close(fd);
107
108 if (rc < 0)
109 return rc;
110
111 memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6);
112
113 return 0;
114}
Holger Hans Peter Freytherefe01002014-08-21 14:14:38 +0200115#endif