blob: d71cd10f4f49d093af7df7d31dca16deb323329e [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/*
2 * arpping.c
3 *
4 * Mostly stolen from: dhcpcd - DHCP client daemon
5 * by Yoichi Hariguchi <yoichi@fore.com>
6 */
7
8#include <sys/types.h>
9#include <sys/time.h>
10#include <time.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <netinet/if_ether.h>
14#include <net/if_arp.h>
15#include <netinet/in.h>
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19#include <errno.h>
20
21#include "dhcpd.h"
22#include "debug.h"
23#include "arpping.h"
24
25/* args: yiaddr - what IP to ping
26 * ip - our ip
27 * mac - our arp address
28 * interface - interface to use
29 * retn: 1 addr free
30 * 0 addr used
31 * -1 error
32 */
33
34/* FIXME: match response against chaddr */
35int arpping(u_int32_t yiaddr, u_int32_t ip, unsigned char *mac, char *interface)
36{
37
38 int timeout = 2;
39 int optval = 1;
40 int s; /* socket */
41 int rv = 1; /* return value */
42 struct sockaddr addr; /* for interface name */
43 struct arpMsg arp;
44 fd_set fdset;
45 struct timeval tm;
46 time_t prevTime;
47
48
49 if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
50 LOG(LOG_ERR, "Could not open raw socket");
51 return -1;
52 }
53
54 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
55 LOG(LOG_ERR, "Could not setsocketopt on raw socket");
56 close(s);
57 return -1;
58 }
59
60 /* send arp request */
61 memset(&arp, 0, sizeof(arp));
62 memcpy(arp.ethhdr.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */
63 memcpy(arp.ethhdr.h_source, mac, 6); /* MAC SA */
64 arp.ethhdr.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
65 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
66 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
67 arp.hlen = 6; /* hardware address length */
68 arp.plen = 4; /* protocol address length */
69 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
70 *((u_int *) arp.sInaddr) = ip; /* source IP address */
71 memcpy(arp.sHaddr, mac, 6); /* source hardware address */
72 *((u_int *) arp.tInaddr) = yiaddr; /* target IP address */
73
74 memset(&addr, 0, sizeof(addr));
75 strcpy(addr.sa_data, interface);
76 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
77 rv = 0;
78
79 /* wait arp reply, and check it */
80 tm.tv_usec = 0;
81 time(&prevTime);
82 while (timeout > 0) {
83 FD_ZERO(&fdset);
84 FD_SET(s, &fdset);
85 tm.tv_sec = timeout;
86 if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
87 DEBUG(LOG_ERR, "Error on ARPING request: %s", strerror(errno));
88 if (errno != EINTR) rv = 0;
89 } else if (FD_ISSET(s, &fdset)) {
90 if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
91 if (arp.operation == htons(ARPOP_REPLY) &&
92 bcmp(arp.tHaddr, mac, 6) == 0 &&
93 *((u_int *) arp.sInaddr) == yiaddr) {
94 DEBUG(LOG_INFO, "Valid arp reply receved for this address");
95 rv = 0;
96 break;
97 }
98 }
99 timeout -= time(NULL) - prevTime;
100 time(&prevTime);
101 }
102 close(s);
103 DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
104 return rv;
105}