Created
June 20, 2014 21:16
-
-
Save AlainODea/65e0d4f5db1c8052699b to your computer and use it in GitHub Desktop.
@rmustacc's ARP-based retrieval of MAC addresses. Should work on any UNIX-like.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Test getifaddrs behavior. | |
*/ | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <ifaddrs.h> | |
#include <stdio.h> | |
#include <net/if.h> | |
#include <unistd.h> | |
#include <stropts.h> | |
#include <sys/sockio.h> | |
#include <strings.h> | |
#include <net/if_arp.h> | |
int | |
main(int argc, const char *argv[]) | |
{ | |
struct ifaddrs *ifa, *oif; | |
struct arpreq arp; | |
if (getifaddrs(&ifa) != 0) { | |
perror("getifaddrs"); | |
return (1); | |
} | |
oif = ifa; | |
while (ifa != NULL) { | |
int sock, i; | |
printf("%s: [%d]\n", ifa->ifa_name, ifa->ifa_addr->sa_family); | |
if (ifa->ifa_flags & IFF_LOOPBACK) { | |
ifa = ifa->ifa_next; | |
continue; | |
} | |
sock = socket(ifa->ifa_addr->sa_family, SOCK_DGRAM, 0); | |
if (sock < 0) { | |
perror("socket"); | |
freeifaddrs(oif); | |
return (1); | |
} | |
bzero(&arp, sizeof (struct arpreq)); | |
arp.arp_pa = *ifa->ifa_addr; | |
if (ioctl(sock, SIOCGARP, (void *)&arp) != 0) { | |
perror("ioctl"); | |
return (1); | |
} | |
for (i = 0; i < 6; i++) { | |
printf("%02x%s", (unsigned char)arp.arp_ha.sa_data[i], | |
i != 5 ? ":" : ""); | |
} | |
printf("\n"); | |
ifa = ifa->ifa_next; | |
} | |
freeifaddrs(oif); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment