summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaditya Dhruv <[email protected]>2025-08-05 22:32:32 -0500
committerAaditya Dhruv <[email protected]>2025-08-05 22:32:32 -0500
commit39738987f9c53cfac6cf2e158545223e85274940 (patch)
tree0d2909f629fd8df25c5db3f0fd985d9bb588a851
parentee8fbab4cf15f138a0d7f87f26c778ba1c9052ca (diff)
Add support for automatically detecting ifindexHEADmaster
Scan all the interfaces, match the one with the same sender hardware address and then use the scanned ifindex
-rw-r--r--src/network.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/network.c b/src/network.c
index 122676d..bb2d395 100644
--- a/src/network.c
+++ b/src/network.c
@@ -1,5 +1,6 @@
#include "junk/network.h"
#include <arpa/inet.h>
+#include <linux/if_link.h>
#include <net/if.h>
#include <errno.h>
#include <linux/if_arp.h>
@@ -14,6 +15,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
+#include <ifaddrs.h>
#define TAG "network"
@@ -156,7 +158,26 @@ int eth_arp_send(int sockfd, arp_packet *packet) {
memset(&addrinfo, 0, sizeof addrinfo);
addrinfo.sll_family = AF_PACKET;
addrinfo.sll_protocol = htonl(ETH_P_ARP);
- addrinfo.sll_ifindex = if_nametoindex("enp6s0");
+ struct ifaddrs *ifaddr;
+ if (getifaddrs(&ifaddr) == -1) {
+ perror("getifaddrs");
+ }
+
+ for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr == NULL) {
+ continue;
+ }
+ int family = ifa->ifa_addr->sa_family;
+ if (family == AF_PACKET) {
+ struct sockaddr_ll* hwaddr = (struct sockaddr_ll*)ifa->ifa_addr;
+ if (memcmp(hwaddr->sll_addr, packet->sender_hardware_address, 6) == 0) {
+ fprintf(stderr, "Found intf ifindex %d\n", hwaddr->sll_ifindex);
+ addrinfo.sll_ifindex = hwaddr->sll_ifindex;
+ }
+ }
+ }
+
+
addrinfo.sll_hatype = htonl(ARPHRD_ETHER);
addrinfo.sll_pkttype = PACKET_BROADCAST;
addrinfo.sll_halen = 6;