From a7233f40879a47a30592d682f4570efdb43f179b Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Wed, 16 Jun 2021 16:16:22 +0200 Subject: [PATCH] ARP library --- c/arp/arp.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 c/arp/arp.c diff --git a/c/arp/arp.c b/c/arp/arp.c new file mode 100644 index 0000000..aa42fd2 --- /dev/null +++ b/c/arp/arp.c @@ -0,0 +1,77 @@ +#define _DEFAULT_SOURCE 1 +#define _POSIX_C_SOURCE 200809L +#define _XOPEN_SOURCE 700 + +#include +#include +#include +#include +#include +#include +#include +#include + +uint64_t arp_ip2mac(const char *dev, const char *ip) { + int ret; + struct arpreq arpreq; + int iArpFD; + + struct sockaddr_in *sin = (struct sockaddr_in *) &arpreq.arp_pa; + uint64_t mac = 0; + + memset(&arpreq, 0, sizeof(struct arpreq)); + + strcpy(arpreq.arp_dev, dev); + //sprintf(arpreq.arp_dev, "eth-pub.%d", vlan); + + iArpFD = socket(AF_INET, SOCK_DGRAM, 0); + if (iArpFD == -1) + { + printf("Cannot open packet socket"); + return 0; + } + + struct timeval tv = { + .tv_sec = 0, + .tv_usec = 200000 + }; + setsockopt(iArpFD, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(struct timeval)); + + sin->sin_family = AF_INET; + sin->sin_addr.s_addr = inet_addr(ip); + sin->sin_port = htons(4242); + + sendto(iArpFD, "PING", 4, MSG_NOSIGNAL, + (struct sockaddr*) sin, sizeof(struct sockaddr_in)); + + + char blah[5]; + if(recvfrom(iArpFD, blah, sizeof(blah), 0, + (struct sockaddr *) sin, (socklen_t * restrict) sizeof(struct sockaddr)) < 0) + { + //printf("UDP ping timeout\n"); + } + + ret = ioctl(iArpFD, SIOCGARP, &arpreq); + close(iArpFD); + + if(ret < 0) { + printf("RET: %d FAMILY: %d\n", ret, arpreq.arp_ha.sa_family); + perror("SIOCGARP"); + return 0; + } + + memcpy(&mac, &arpreq.arp_ha.sa_data[0], 6); + mac = be64toh(mac<<16); + + return mac; +} + +int main() { + char *dev = "eth0"; + char *ip = "10.10.161.3"; + uint64_t mac = arp_ip2mac(dev, ip); + + printf("DEV %s IP %s MAC: %.12lX %s\n", dev, ip, mac, mac?"OK":"FAIL"); +} + -- 2.30.2