diff options
author | Russ Dill | 2002-10-14 21:41:28 +0000 |
---|---|---|
committer | Russ Dill | 2002-10-14 21:41:28 +0000 |
commit | 61fb48930f45aa536584b2047f9e703186e8f69f (patch) | |
tree | e3b93e0a694be81939f8c4762553c43ffdb9b10b /networking/udhcp/arpping.c | |
parent | 9060a7315980bb05f42eab76b88746d43e138188 (diff) | |
download | busybox-61fb48930f45aa536584b2047f9e703186e8f69f.zip busybox-61fb48930f45aa536584b2047f9e703186e8f69f.tar.gz |
added full udhcp integration
Diffstat (limited to 'networking/udhcp/arpping.c')
-rw-r--r-- | networking/udhcp/arpping.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c new file mode 100644 index 0000000..d71cd10 --- /dev/null +++ b/networking/udhcp/arpping.c @@ -0,0 +1,105 @@ +/* + * arpping.c + * + * Mostly stolen from: dhcpcd - DHCP client daemon + * by Yoichi Hariguchi <yoichi@fore.com> + */ + +#include <sys/types.h> +#include <sys/time.h> +#include <time.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/if_ether.h> +#include <net/if_arp.h> +#include <netinet/in.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "dhcpd.h" +#include "debug.h" +#include "arpping.h" + +/* args: yiaddr - what IP to ping + * ip - our ip + * mac - our arp address + * interface - interface to use + * retn: 1 addr free + * 0 addr used + * -1 error + */ + +/* FIXME: match response against chaddr */ +int arpping(u_int32_t yiaddr, u_int32_t ip, unsigned char *mac, char *interface) +{ + + int timeout = 2; + int optval = 1; + int s; /* socket */ + int rv = 1; /* return value */ + struct sockaddr addr; /* for interface name */ + struct arpMsg arp; + fd_set fdset; + struct timeval tm; + time_t prevTime; + + + if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) { + LOG(LOG_ERR, "Could not open raw socket"); + return -1; + } + + if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) { + LOG(LOG_ERR, "Could not setsocketopt on raw socket"); + close(s); + return -1; + } + + /* send arp request */ + memset(&arp, 0, sizeof(arp)); + memcpy(arp.ethhdr.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */ + memcpy(arp.ethhdr.h_source, mac, 6); /* MAC SA */ + arp.ethhdr.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */ + arp.htype = htons(ARPHRD_ETHER); /* hardware type */ + arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */ + arp.hlen = 6; /* hardware address length */ + arp.plen = 4; /* protocol address length */ + arp.operation = htons(ARPOP_REQUEST); /* ARP op code */ + *((u_int *) arp.sInaddr) = ip; /* source IP address */ + memcpy(arp.sHaddr, mac, 6); /* source hardware address */ + *((u_int *) arp.tInaddr) = yiaddr; /* target IP address */ + + memset(&addr, 0, sizeof(addr)); + strcpy(addr.sa_data, interface); + if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) + rv = 0; + + /* wait arp reply, and check it */ + tm.tv_usec = 0; + time(&prevTime); + while (timeout > 0) { + FD_ZERO(&fdset); + FD_SET(s, &fdset); + tm.tv_sec = timeout; + if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) { + DEBUG(LOG_ERR, "Error on ARPING request: %s", strerror(errno)); + if (errno != EINTR) rv = 0; + } else if (FD_ISSET(s, &fdset)) { + if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0; + if (arp.operation == htons(ARPOP_REPLY) && + bcmp(arp.tHaddr, mac, 6) == 0 && + *((u_int *) arp.sInaddr) == yiaddr) { + DEBUG(LOG_INFO, "Valid arp reply receved for this address"); + rv = 0; + break; + } + } + timeout -= time(NULL) - prevTime; + time(&prevTime); + } + close(s); + DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V"); + return rv; +} |