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;
}
#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

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 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;
}

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);
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