Create static ARP entry for device

This commit is contained in:
Joseph C. Lehner 2016-08-09 11:41:50 +02:00
parent e4d61b8c7f
commit a050f74bce
3 changed files with 32 additions and 8 deletions

View file

@ -453,7 +453,7 @@ int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipadd
return 0; return 0;
} }
#else #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; DWORD ret;
MIB_IPNETROW arp = { 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); memcpy(arp.bPhysAddr, hwaddr, 6);
ret = add ? CreateIpNetEntry(&arp) : DeleteIpNetEntry(&arp); if (add) {
if (ret != NO_ERROR && !nofail) { ret = CreateIpNetEntry(&arp);
win_perror2(add ? "CreateIpNetEntry" : "DeleteIpNetEntry", ret); if (ret != NO_ERROR) {
return -1; win_perror2("CreateIpNetEntry", ret);
return -1;
}
} else {
DeleteIpNetEntry(&arp);
} }
return 0; 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) int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
{ {
ethsock_arp(sock, hwaddr, ipaddr, 0, 1); ethsock_arp_del(sock, hwaddr, ipaddr);
return ethsock_arp(sock, hwaddr, ipaddr, 1, 0); return ethsock_arp(sock, hwaddr, ipaddr, 1);
} }
int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr) 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 #endif

18
nmrp.c
View file

@ -377,12 +377,19 @@ static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
} }
static struct ethsock *gsock = NULL; 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) static void sigh(int sig)
{ {
printf("\n"); printf("\n");
if (gsock) { if (gsock) {
if (garp) {
ethsock_arp_del(gsock, arpmac, &arpip);
}
ethsock_close(gsock); ethsock_close(gsock);
gsock = NULL;
} }
exit(1); exit(1);
@ -465,6 +472,7 @@ int nmrp_do(struct nmrpd_args *args)
} }
gsock = sock; gsock = sock;
garp = 0;
sigh_orig = signal(SIGINT, sigh); sigh_orig = signal(SIGINT, sigh);
if (ethsock_set_timeout(sock, args->rx_timeout)) { 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", printf("Sending configuration: ip %s, mask %s.\n",
args->ipaddr, args->ipmask); 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; break;
case NMRP_C_TFTP_UL_REQ: case NMRP_C_TFTP_UL_REQ:
if (!upload_ok) { if (!upload_ok) {
@ -693,6 +710,7 @@ int nmrp_do(struct nmrpd_args *args)
out: out:
signal(SIGINT, sigh_orig); signal(SIGINT, sigh_orig);
gsock = NULL; gsock = NULL;
ethsock_arp_del(sock, arpmac, &arpip);
ethsock_close(sock); ethsock_close(sock);
return status; return status;
} }

View file

@ -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); ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
int ethsock_set_timeout(struct ethsock *sock, unsigned msec); int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
uint8_t *ethsock_get_hwaddr(struct ethsock *sock); 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); int ethsock_list_all(void);
struct ethsock_ip_callback_args struct ethsock_ip_callback_args