From a050f74bce84922f013d2e6363357373196ae8e6 Mon Sep 17 00:00:00 2001 From: "Joseph C. Lehner" Date: Tue, 9 Aug 2016 11:41:50 +0200 Subject: [PATCH] Create static ARP entry for device --- ethsock.c | 20 ++++++++++++-------- nmrp.c | 18 ++++++++++++++++++ nmrpd.h | 2 ++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/ethsock.c b/ethsock.c index 232d84f..61e9759 100644 --- 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 7995d42..fbb4cea 100644 --- 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 841b4f3..ef25a69 100644 --- 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 -- 2.25.1