Consolidate #include <sys/time.h> so libbb.h does it.
[oweals/busybox.git] / networking / udhcp / arpping.c
1 /*
2  * arpping.c
3  *
4  * Mostly stolen from: dhcpcd - DHCP client daemon
5  * by Yoichi Hariguchi <yoichi@fore.com>
6  */
7
8 #include <time.h>
9 #include <sys/socket.h>
10 #include <netinet/if_ether.h>
11 #include <net/if_arp.h>
12 #include <netinet/in.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16
17 #include "dhcpd.h"
18 #include "arpping.h"
19 #include "common.h"
20
21 /* args:        yiaddr - what IP to ping
22  *              ip - our ip
23  *              mac - our arp address
24  *              interface - interface to use
25  * retn:        1 addr free
26  *              0 addr used
27  *              -1 error
28  */
29
30 /* FIXME: match response against chaddr */
31 int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
32 {
33
34         int     timeout = 2;
35         int     optval = 1;
36         int     s;                      /* socket */
37         int     rv = 1;                 /* return value */
38         struct sockaddr addr;           /* for interface name */
39         struct arpMsg   arp;
40         fd_set          fdset;
41         struct timeval  tm;
42         time_t          prevTime;
43
44
45         if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
46 #ifdef IN_BUSYBOX
47                 LOG(LOG_ERR, bb_msg_can_not_create_raw_socket);
48 #else
49                 LOG(LOG_ERR, "Could not open raw socket");
50 #endif
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.h_dest, MAC_BCAST_ADDR, 6);          /* MAC DA */
63         memcpy(arp.h_source, mac, 6);                   /* MAC SA */
64         arp.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         memcpy(arp.sInaddr, &ip, sizeof(ip));           /* source IP address */
71         memcpy(arp.sHaddr, mac, 6);                     /* source hardware address */
72         memcpy(arp.tInaddr, &yiaddr, sizeof(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         prevTime = uptime();
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: %m");
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                             memcmp(arp.tHaddr, mac, 6) == 0 &&
93                             *((uint32_t *) arp.sInaddr) == yiaddr) {
94                                 DEBUG(LOG_INFO, "Valid arp reply receved for this address");
95                                 rv = 0;
96                                 break;
97                         }
98                 }
99                 timeout -= uptime() - prevTime;
100                 prevTime = uptime();
101         }
102         close(s);
103         DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
104         return rv;
105 }