Create static ARP entry for device
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Tue, 9 Aug 2016 09:41:50 +0000 (11:41 +0200)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Tue, 9 Aug 2016 10:16:47 +0000 (12:16 +0200)
ethsock.c
nmrp.c
nmrpd.h

index 232d84fafe7a23f3185bb091e36caa63d2729abf..61e975959c8695d695807e2ecda9b386d1768327 100644 (file)
--- a/ethsock.c
+++ b/ethsock.c
@@ -453,7 +453,7 @@ int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipadd
        return 0;
 }
 #else
-static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr, int add, int nofail)
+static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr, int add)
 {
        DWORD ret;
        MIB_IPNETROW arp = {
@@ -465,10 +465,14 @@ static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ip
        
        memcpy(arp.bPhysAddr, hwaddr, 6);
        
-       ret = add ? CreateIpNetEntry(&arp) : DeleteIpNetEntry(&arp);
-       if (ret != NO_ERROR && !nofail) {
-               win_perror2(add ? "CreateIpNetEntry" : "DeleteIpNetEntry", ret);
-               return -1;
+       if (add) {
+               ret = CreateIpNetEntry(&arp);
+               if (ret != NO_ERROR) {
+                       win_perror2("CreateIpNetEntry", ret);
+                       return -1;
+               }
+       } else {
+               DeleteIpNetEntry(&arp);
        }
        
        return 0;
@@ -476,13 +480,13 @@ static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ip
 
 int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
 {
-       ethsock_arp(sock, hwaddr, ipaddr, 0, 1);
-       return ethsock_arp(sock, hwaddr, ipaddr, 1, 0);
+       ethsock_arp_del(sock, hwaddr, ipaddr);
+       return ethsock_arp(sock, hwaddr, ipaddr, 1);
 }
 
 int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
 {
-       return ethsock_arp(sock, hwaddr, ipaddr, 0, 0);
+       return ethsock_arp(sock, hwaddr, ipaddr, 0);
 }
 #endif
 
diff --git a/nmrp.c b/nmrp.c
index 7995d427f63f18704326ccf32a5b487ba5005f68..fbb4cea00f967d6b9ed91e44fe99cf3d214fd65c 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -377,12 +377,19 @@ static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
 }
 
 static struct ethsock *gsock = NULL;
+static int garp = 0;
+static struct in_addr arpip = { 0 };
+static uint8_t arpmac[6] = { 0 };
 
 static void sigh(int sig)
 {
        printf("\n");
        if (gsock) {
+               if (garp) {
+                       ethsock_arp_del(gsock, arpmac, &arpip);
+               }
                ethsock_close(gsock);
+               gsock = NULL;
        }
 
        exit(1);
@@ -465,6 +472,7 @@ int nmrp_do(struct nmrpd_args *args)
        }
 
        gsock = sock;
+       garp = 0;
        sigh_orig = signal(SIGINT, sigh);
 
        if (ethsock_set_timeout(sock, args->rx_timeout)) {
@@ -556,6 +564,15 @@ int nmrp_do(struct nmrpd_args *args)
                                printf("Sending configuration: ip %s, mask %s.\n",
                                                args->ipaddr, args->ipmask);
 
+                               memcpy(arpmac, rx.eh.ether_shost, 6);
+                               memcpy(&arpip, &ipconf.addr, sizeof(ipconf.addr));
+
+                               if (ethsock_arp_add(sock, arpmac, &arpip) != 0) {
+                                       goto out;
+                               }
+
+                               garp = 1;
+
                                break;
                        case NMRP_C_TFTP_UL_REQ:
                                if (!upload_ok) {
@@ -693,6 +710,7 @@ int nmrp_do(struct nmrpd_args *args)
 out:
        signal(SIGINT, sigh_orig);
        gsock = NULL;
+       ethsock_arp_del(sock, arpmac, &arpip);
        ethsock_close(sock);
        return status;
 }
diff --git a/nmrpd.h b/nmrpd.h
index 841b4f3758ef82d78d0b1e1a3d4e69e6c03e0559..ef25a690f48a14d1585a8ad0ad5fb91e03397560 100644 (file)
--- a/nmrpd.h
+++ b/nmrpd.h
@@ -100,6 +100,8 @@ int ethsock_send(struct ethsock *sock, void *buf, size_t len);
 ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
 int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
 uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
+int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr);
+int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr);
 int ethsock_list_all(void);
 
 struct ethsock_ip_callback_args