aboutsummaryrefslogtreecommitdiffstats
path: root/src/macaddr.c
blob: f83e0546efeff4dd41d26300e79e1b5040d8294b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * (C) 2013-2014 by Harald Welte <laforge@gnumonks.org>
 * (C) 2014 by Holger Hans Peter Freyther
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/*! \addtogroup utils
 *  @{
 */

/*! \file loggingrb.c */

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/*! \brief Parse a MAC address from human-readable notation
 *  This function parses an ethernet MAC address in the commonly-used
 *  hex/colon notation (00:00:00:00:00:00) and generates the binary
 *  representation from it.
 *  \param[out] out pointer to caller-allocated buffer of 6 bytes
 *  \param[in] in pointer to input data as string with hex/colon notation
 */
int osmo_macaddr_parse(uint8_t *out, const char *in)
{
	/* 00:00:00:00:00:00 */
	char tmp[18];
	char *tok;
	unsigned int i = 0;

	if (strlen(in) < 17)
		return -1;

	strncpy(tmp, in, sizeof(tmp)-1);
	tmp[sizeof(tmp)-1] = '\0';

	for (tok = strtok(tmp, ":"); tok && (i < 6); tok = strtok(NULL, ":")) {
		unsigned long ul = strtoul(tok, NULL, 16);
		out[i++] = ul & 0xff;
	}

	return 0;
}

#if defined(__FreeBSD__) || defined(__APPLE__)
#include <sys/socket.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#include <net/if_types.h>

/*! \brief Obtain the MAC address of a given network device
 *  \param[out] mac_out pointer to caller-allocated buffer of 6 bytes
 *  \param[in] dev_name string name of the network device
 *  \returns 0 in case of success; negative otherwise
 */
int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
{
	int rc = -1;
	struct ifaddrs *ifa, *ifaddr;

	if (getifaddrs(&ifaddr) != 0)
		return -1;

	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
		struct sockaddr_dl *sdl;

		sdl = (struct sockaddr_dl *) ifa->ifa_addr;
		if (!sdl)
			continue;
		if (sdl->sdl_family != AF_LINK)
			continue;
		if (sdl->sdl_type != IFT_ETHER)
			continue;
		if (strcmp(ifa->ifa_name, dev_name) != 0)
			continue;

		memcpy(mac_out, LLADDR(sdl), 6);
		rc = 0;
		break;
	}

	freeifaddrs(ifaddr);
	return 0;
}

#else

#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>

/*! \brief Obtain the MAC address of a given network device
 *  \param[out] mac_out pointer to caller-allocated buffer of 6 bytes
 *  \param[in] dev_name string name of the network device
 *  \returns 0 in case of success; negative otherwise
 */
int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
{
	int fd, rc;
	struct ifreq ifr;

	fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
	if (fd < 0)
		return fd;

	memset(&ifr, 0, sizeof(ifr));
	memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
	rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
	close(fd);

	if (rc < 0)
		return rc;

	memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6);

	return 0;
}
#endif

/*! @} */