1 #define _DEFAULT_SOURCE 1
2 #define _POSIX_C_SOURCE 200809L
3 #define _XOPEN_SOURCE 700
10 #include <sys/ioctl.h>
12 #include <net/if_arp.h>
14 uint64_t arp_ip2mac(const char *dev
, const char *ip
) {
19 struct sockaddr_in
*sin
= (struct sockaddr_in
*) &arpreq
.arp_pa
;
22 memset(&arpreq
, 0, sizeof(struct arpreq
));
24 strcpy(arpreq
.arp_dev
, dev
);
25 //sprintf(arpreq.arp_dev, "eth-pub.%d", vlan);
27 iArpFD
= socket(AF_INET
, SOCK_DGRAM
, 0);
30 printf("Cannot open packet socket");
38 setsockopt(iArpFD
, SOL_SOCKET
, SO_RCVTIMEO
,&tv
,sizeof(struct timeval
));
40 sin
->sin_family
= AF_INET
;
41 sin
->sin_addr
.s_addr
= inet_addr(ip
);
42 sin
->sin_port
= htons(4242);
44 sendto(iArpFD
, "PING", 4, MSG_NOSIGNAL
,
45 (struct sockaddr
*) sin
, sizeof(struct sockaddr_in
));
49 if(recvfrom(iArpFD
, blah
, sizeof(blah
), 0,
50 (struct sockaddr
*) sin
, (socklen_t
* restrict
) sizeof(struct sockaddr
)) < 0)
52 //printf("UDP ping timeout\n");
55 ret
= ioctl(iArpFD
, SIOCGARP
, &arpreq
);
59 printf("RET: %d FAMILY: %d\n", ret
, arpreq
.arp_ha
.sa_family
);
64 memcpy(&mac
, &arpreq
.arp_ha
.sa_data
[0], 6);
65 mac
= be64toh(mac
<<16);
72 char *ip
= "10.10.161.3";
73 uint64_t mac
= arp_ip2mac(dev
, ip
);
75 printf("DEV %s IP %s MAC: %.12lX %s\n", dev
, ip
, mac
, mac
?"OK":"FAIL");
This page took 1.582385 seconds and 4 git commands to generate.