diff options
author | Vadim Kochan <vadim4j@gmail.com> | 2016-01-26 22:25:00 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2016-01-28 16:04:47 +0100 |
commit | b5100e276763a03c255dd1e7a4147c55c901419c (patch) | |
tree | cdd4cfd2915d4ce04ca2d6cd494b2264635dc1b4 /dev.c | |
parent | e476a36e65cd8508c6473a19e497fb04487e2214 (diff) |
dev: Add function to get device hardware address
Add device_hw_address() function to get device MAC address.
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
[tk: Add len parameter and error out on too short buffers]
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'dev.c')
-rw-r--r-- | dev.c | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -124,6 +124,31 @@ int device_address(const char *ifname, int af, struct sockaddr_storage *ss) return ret; } +int device_hw_address(const char *ifname, uint8_t *addr, size_t len) +{ + int ret, sock; + struct ifreq ifr; + + if (!addr) + return -EINVAL; + if (len < IFHWADDRLEN) + return -ENOSPC; + if (!strncmp("any", ifname, strlen("any"))) + return -EINVAL; + + sock = af_socket(AF_INET); + + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); + + ret = ioctl(sock, SIOCGIFHWADDR, &ifr); + if (!ret) + memcpy(addr, &ifr.ifr_hwaddr.sa_data[0], IFHWADDRLEN); + + close(sock); + return ret; +} + size_t device_mtu(const char *ifname) { size_t mtu = 0; |