2017-06-20 02:35:06 +00:00
|
|
|
/*! \file macaddr.c
|
|
|
|
* MAC address utility routines. */
|
2014-09-11 05:05:51 +00:00
|
|
|
/*
|
|
|
|
* (C) 2013-2014 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
* (C) 2014 by Holger Hans Peter Freyther
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
2017-11-12 16:00:26 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*
|
2014-09-11 05:05:51 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-04-25 10:11:20 +00:00
|
|
|
/*! \addtogroup utils
|
|
|
|
* @{
|
2017-06-20 02:35:06 +00:00
|
|
|
* \file macaddr.c */
|
2016-04-25 10:11:20 +00:00
|
|
|
|
2017-01-15 16:58:29 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-08-18 17:03:40 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-08-21 00:37:57 +00:00
|
|
|
#include <unistd.h>
|
2014-08-18 17:03:40 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Parse a MAC address from human-readable notation
|
2016-04-25 10:11:20 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2014-08-18 17:03:40 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-08-18 17:19:45 +00:00
|
|
|
|
2015-04-11 17:29:52 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__APPLE__)
|
2014-08-21 12:14:38 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Obtain the MAC address of a given network device
|
2016-04-25 10:11:20 +00:00
|
|
|
* \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
|
|
|
|
*/
|
2014-08-21 12:14:38 +00:00
|
|
|
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
|
|
|
|
|
2017-01-15 16:58:29 +00:00
|
|
|
#if (!EMBEDDED)
|
|
|
|
|
2014-08-18 17:19:45 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
2014-12-04 13:15:36 +00:00
|
|
|
#include <netinet/in.h>
|
2014-08-18 17:19:45 +00:00
|
|
|
#include <netinet/ip.h>
|
2018-04-17 15:58:24 +00:00
|
|
|
#include <errno.h>
|
2014-08-18 17:19:45 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Obtain the MAC address of a given network device
|
2016-04-25 10:11:20 +00:00
|
|
|
* \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
|
|
|
|
*/
|
2014-08-18 17:19:45 +00:00
|
|
|
int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
|
|
|
|
{
|
2018-04-17 15:58:24 +00:00
|
|
|
int fd, rc, dev_len;
|
2014-08-18 17:19:45 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
|
2018-04-17 15:58:24 +00:00
|
|
|
dev_len = strlen(dev_name);
|
|
|
|
if (dev_len >= sizeof(ifr.ifr_name))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-08-18 17:19:45 +00:00
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2018-04-17 15:58:24 +00:00
|
|
|
memcpy(&ifr.ifr_name, dev_name, dev_len + 1);
|
2014-08-18 17:19:45 +00:00
|
|
|
rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-15 16:58:29 +00:00
|
|
|
#endif /* !EMBEDDED */
|
|
|
|
|
2014-08-21 12:14:38 +00:00
|
|
|
#endif
|
2016-04-25 10:11:20 +00:00
|
|
|
|
|
|
|
/*! @} */
|