blob: 6383f9a88e9771ba22431c3b01e0242f8dbc067f [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9e598412003-01-09 10:06:01 +00002/*
3 * arping.c - Ping hosts by ARP requests/replies
4 *
Rob Landley8ea52052006-03-30 21:50:57 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath9e598412003-01-09 10:06:01 +00006 *
7 * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
9 */
10
Glenn L McGrath9e598412003-01-09 10:06:01 +000011#include <arpa/inet.h>
12#include <net/if.h>
13#include <netinet/ether.h>
14#include <netpacket/packet.h>
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Glenn L McGrath9e598412003-01-09 10:06:01 +000017
Denis Vlasenko459be352007-06-17 19:09:05 +000018/* We don't expect to see 1000+ seconds delay, unsigned is enough */
19#define MONOTONIC_US() ((unsigned)monotonic_us())
20
Denis Vlasenko459be352007-06-17 19:09:05 +000021enum {
22 DAD = 1,
23 UNSOLICITED = 2,
24 ADVERT = 4,
25 QUIET = 8,
26 QUIT_ON_REPLY = 16,
27 BCAST_ONLY = 32,
28 UNICASTING = 64
Rob Landley8ea52052006-03-30 21:50:57 +000029};
Rob Landley8ea52052006-03-30 21:50:57 +000030
Denis Vlasenkofff9b692007-11-23 09:15:26 +000031struct globals {
32 struct in_addr src;
33 struct in_addr dst;
34 struct sockaddr_ll me;
35 struct sockaddr_ll he;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000036 int sock_fd;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000037
38 int count; // = -1;
39 unsigned last;
40 unsigned timeout_us;
41 unsigned start;
42
43 unsigned sent;
44 unsigned brd_sent;
45 unsigned received;
46 unsigned brd_recv;
47 unsigned req_recv;
48};
49#define G (*(struct globals*)&bb_common_bufsiz1)
50#define src (G.src )
51#define dst (G.dst )
52#define me (G.me )
53#define he (G.he )
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000054#define sock_fd (G.sock_fd )
Denis Vlasenkofff9b692007-11-23 09:15:26 +000055#define count (G.count )
56#define last (G.last )
57#define timeout_us (G.timeout_us)
58#define start (G.start )
59#define sent (G.sent )
60#define brd_sent (G.brd_sent )
61#define received (G.received )
62#define brd_recv (G.brd_recv )
63#define req_recv (G.req_recv )
64#define INIT_G() \
65 do { \
66 count = -1; \
67 } while (0)
Glenn L McGrath9e598412003-01-09 10:06:01 +000068
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000069static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000070 struct in_addr *dst_addr, struct sockaddr_ll *ME,
71 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000072{
73 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +000074 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +000075 struct arphdr *ah = (struct arphdr *) buf;
76 unsigned char *p = (unsigned char *) (ah + 1);
77
78 ah->ar_hrd = htons(ME->sll_hatype);
79 ah->ar_hrd = htons(ARPHRD_ETHER);
80 ah->ar_pro = htons(ETH_P_IP);
81 ah->ar_hln = ME->sll_halen;
82 ah->ar_pln = 4;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000083 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000084
85 memcpy(p, &ME->sll_addr, ah->ar_hln);
86 p += ME->sll_halen;
87
88 memcpy(p, src_addr, 4);
89 p += 4;
90
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000091 if (option_mask32 & ADVERT)
Glenn L McGrath9e598412003-01-09 10:06:01 +000092 memcpy(p, &ME->sll_addr, ah->ar_hln);
93 else
94 memcpy(p, &HE->sll_addr, ah->ar_hln);
95 p += ah->ar_hln;
96
97 memcpy(p, dst_addr, 4);
98 p += 4;
99
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000100 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000101 if (err == p - buf) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000102 last = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000103 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000104 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000105 brd_sent++;
106 }
107 return err;
108}
109
Denis Vlasenko459be352007-06-17 19:09:05 +0000110static void finish(void) ATTRIBUTE_NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000111static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000112{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000113 if (!(option_mask32 & QUIET)) {
114 printf("Sent %u probe(s) (%u broadcast(s))\n"
115 "Received %u repl%s"
116 " (%u request(s), %u broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000117 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000118 received, (received == 1) ? "ies" : "y",
119 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000120 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000121 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000122 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000123 if (option_mask32 & UNSOLICITED)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000124 exit(0);
125 exit(!received);
126}
127
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000128static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000129{
Denis Vlasenko459be352007-06-17 19:09:05 +0000130 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000131
Denis Vlasenko459be352007-06-17 19:09:05 +0000132 now = MONOTONIC_US();
133 if (start == 0)
134 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000135
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000136 if (count == 0 || (timeout_us && (now - start) > timeout_us))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000137 finish();
138
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000139 /* count < 0 means "infinite count" */
140 if (count > 0)
141 count--;
Denis Vlasenko459be352007-06-17 19:09:05 +0000142
143 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000144 send_pack(&src, &dst, &me, &he);
145 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000146 finish();
147 }
148 alarm(1);
149}
150
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000151static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000152{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000153 struct arphdr *ah = (struct arphdr *) buf;
154 unsigned char *p = (unsigned char *) (ah + 1);
155 struct in_addr src_ip, dst_ip;
156
Glenn L McGrath9e598412003-01-09 10:06:01 +0000157 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000158 if (FROM->sll_pkttype != PACKET_HOST
159 && FROM->sll_pkttype != PACKET_BROADCAST
160 && FROM->sll_pkttype != PACKET_MULTICAST)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000161 return 0;
162
163 /* Only these types are recognised */
164 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
165 return 0;
166
167 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000168 if (ah->ar_hrd != htons(FROM->sll_hatype)
169 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000170 return 0;
171
172 /* Protocol must be IP. */
173 if (ah->ar_pro != htons(ETH_P_IP))
174 return 0;
175 if (ah->ar_pln != 4)
176 return 0;
177 if (ah->ar_hln != me.sll_halen)
178 return 0;
179 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
180 return 0;
181 memcpy(&src_ip, p + ah->ar_hln, 4);
182 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000183 if (!(option_mask32 & DAD)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000184 if (dst.s_addr != src_ip.s_addr)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000185 return 0;
186 if (src.s_addr != dst_ip.s_addr)
187 return 0;
188 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
189 return 0;
190 } else {
191 /* DAD packet was:
192 src_ip = 0 (or some src)
193 src_hw = ME
194 dst_ip = tested address
195 dst_hw = <unspec>
196
197 We fail, if receive request/reply with:
198 src_ip = tested_address
199 src_hw != ME
200 if src_ip in request was not zero, check
201 also that it matches to dst_ip, otherwise
202 dst_ip/dst_hw do not matter.
203 */
204 if (src_ip.s_addr != dst.s_addr)
205 return 0;
206 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
207 return 0;
208 if (src.s_addr && src.s_addr != dst_ip.s_addr)
209 return 0;
210 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000211 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000212 int s_printed = 0;
213
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000214 printf("%scast re%s from %s [%s]",
215 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
216 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000217 inet_ntoa(src_ip),
218 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000219 if (dst_ip.s_addr != src.s_addr) {
220 printf("for %s ", inet_ntoa(dst_ip));
221 s_printed = 1;
222 }
223 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
224 if (!s_printed)
225 printf("for ");
226 printf("[%s]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000227 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000228 }
Rob Landley8ea52052006-03-30 21:50:57 +0000229
Denis Vlasenko459be352007-06-17 19:09:05 +0000230 if (last) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000231 unsigned diff = MONOTONIC_US() - last;
232 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000233 } else {
234 printf(" UNSOLICITED?\n");
235 }
236 fflush(stdout);
237 }
238 received++;
239 if (FROM->sll_pkttype != PACKET_HOST)
240 brd_recv++;
241 if (ah->ar_op == htons(ARPOP_REQUEST))
242 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000243 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000244 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000245 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000246 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000247 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000248 }
249 return 1;
250}
251
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000252int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000253int arping_main(int argc, char **argv)
254{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000255 const char *device = "eth0";
Glenn L McGrath9e598412003-01-09 10:06:01 +0000256 char *source = NULL;
257 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000258 unsigned char *packet;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000259
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000260 INIT_G();
261
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000262 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000263
Rob Landleyafb94ec2006-07-16 08:06:34 +0000264 // Drop suid root privileges
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000265 // Need to remove SUID_NEVER from applets.h for this to work
266 //xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000267
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000268 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000269 unsigned opt;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000270 char *str_count, *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000271
272 /* Dad also sets quit_on_reply.
273 * Advert also sets unsolicited.
274 */
Denis Vlasenko459be352007-06-17 19:09:05 +0000275 opt_complementary = "=1:Df:AU";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000276 opt = getopt32(argv, "DUAqfbc:w:I:s:",
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000277 &str_count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000278 if (opt & 0x40) /* -c: count */
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000279 count = xatoi_u(str_count);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000280 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000281 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000282 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000283 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000284 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000285
Denis Vlasenko459be352007-06-17 19:09:05 +0000286 target = argv[optind];
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000287
Denis Vlasenko40920822006-10-03 20:28:06 +0000288 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000289
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000290 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000291 struct ifreq ifr;
292
293 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000294 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
295 /* We use ifr.ifr_name in error msg so that problem
296 * with truncated name will be visible */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000297 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, "interface %s not found", ifr.ifr_name);
298 me.sll_ifindex = ifr.ifr_ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000299
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000300 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000301
Glenn L McGrath9e598412003-01-09 10:06:01 +0000302 if (!(ifr.ifr_flags & IFF_UP)) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000303 bb_error_msg_and_die("interface %s is down", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000304 }
305 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000306 bb_error_msg("interface %s is not ARPable", device);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000307 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000308 }
309 }
310
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000311 /* if (!inet_aton(target, &dst)) - not needed */ {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000312 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000313 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000314 memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4);
315 if (ENABLE_FEATURE_CLEAN_UP)
316 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000317 }
318
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000319 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000320 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000321 }
322
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000323 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000324 src = dst;
325
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000326 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000327 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000328 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000329
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000330 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
331 bb_perror_msg("cannot bind to device %s", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000332 memset(&saddr, 0, sizeof(saddr));
333 saddr.sin_family = AF_INET;
334 if (src.s_addr) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000335 /* Check that this is indeed our IP */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000336 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000337 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000338 } else { /* !(option_mask32 & DAD) case */
339 /* Find IP address on this iface */
Eric Andersen0cb6f352006-01-30 22:30:41 +0000340 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000341
342 saddr.sin_port = htons(1025);
343 saddr.sin_addr = dst;
344
Denis Vlasenko703e2022007-01-22 14:12:08 +0000345 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000346 bb_perror_msg("setsockopt(SO_DONTROUTE)");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000347 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
348 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000349 bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000350 }
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000351 if (saddr.sin_family != AF_INET)
352 bb_error_msg_and_die("no IP address configured");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000353 src = saddr.sin_addr;
354 }
355 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000356 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000357
358 me.sll_family = AF_PACKET;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000359 //me.sll_ifindex = ifindex; - done before
Glenn L McGrath9e598412003-01-09 10:06:01 +0000360 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000361 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000362
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000363 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000364 socklen_t alen = sizeof(me);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000365
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000366 if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) {
367 bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000368 }
369 }
370 if (me.sll_halen == 0) {
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000371 bb_error_msg("interface %s is not ARPable (no ll address)", device);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000372 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000373 }
374 he = me;
375 memset(he.sll_addr, -1, he.sll_halen);
376
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000377 if (!(option_mask32 & QUIET)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000378 /* inet_ntoa uses static storage, can't use in same printf */
379 printf("ARPING to %s", inet_ntoa(dst));
380 printf(" from %s via %s\n", inet_ntoa(src), device);
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000381 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000382
Glenn L McGrath9e598412003-01-09 10:06:01 +0000383 {
384 struct sigaction sa;
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +0000385
Glenn L McGrath9e598412003-01-09 10:06:01 +0000386 memset(&sa, 0, sizeof(sa));
387 sa.sa_flags = SA_RESTART;
388
389 sa.sa_handler = (void (*)(int)) finish;
390 sigaction(SIGINT, &sa, NULL);
391
392 sa.sa_handler = (void (*)(int)) catcher;
393 sigaction(SIGALRM, &sa, NULL);
394 }
395
396 catcher();
397
Denis Vlasenko459be352007-06-17 19:09:05 +0000398 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000399 while (1) {
400 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000401 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000402 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000403 int cc;
404
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000405 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000406 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000407 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000408 continue;
409 }
410 sigemptyset(&sset);
411 sigaddset(&sset, SIGALRM);
412 sigaddset(&sset, SIGINT);
413 sigprocmask(SIG_BLOCK, &sset, &osset);
414 recv_pack(packet, cc, &from);
415 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000416 }
417}