blob: a2c4c22472f598882eefb444d93459a11346e004 [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
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000021static struct in_addr src;
22static struct in_addr dst;
23static struct sockaddr_ll me;
24static struct sockaddr_ll he;
Denis Vlasenko459be352007-06-17 19:09:05 +000025static unsigned last;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000026
Denis Vlasenko459be352007-06-17 19:09:05 +000027enum {
28 DAD = 1,
29 UNSOLICITED = 2,
30 ADVERT = 4,
31 QUIET = 8,
32 QUIT_ON_REPLY = 16,
33 BCAST_ONLY = 32,
34 UNICASTING = 64
Rob Landley8ea52052006-03-30 21:50:57 +000035};
Rob Landley8ea52052006-03-30 21:50:57 +000036
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000037static int sock;
Denis Vlasenko13858992006-10-08 12:49:22 +000038static unsigned count = UINT_MAX;
Denis Vlasenko459be352007-06-17 19:09:05 +000039static unsigned timeout_us;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000040static unsigned sent;
41static unsigned brd_sent;
42static unsigned received;
43static unsigned brd_recv;
44static unsigned req_recv;
Glenn L McGrath9e598412003-01-09 10:06:01 +000045
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000046static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000047 struct in_addr *dst_addr, struct sockaddr_ll *ME,
48 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000049{
50 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +000051 unsigned now;
52 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +000053 struct arphdr *ah = (struct arphdr *) buf;
54 unsigned char *p = (unsigned char *) (ah + 1);
55
56 ah->ar_hrd = htons(ME->sll_hatype);
57 ah->ar_hrd = htons(ARPHRD_ETHER);
58 ah->ar_pro = htons(ETH_P_IP);
59 ah->ar_hln = ME->sll_halen;
60 ah->ar_pln = 4;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000061 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000062
63 memcpy(p, &ME->sll_addr, ah->ar_hln);
64 p += ME->sll_halen;
65
66 memcpy(p, src_addr, 4);
67 p += 4;
68
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000069 if (option_mask32 & ADVERT)
Glenn L McGrath9e598412003-01-09 10:06:01 +000070 memcpy(p, &ME->sll_addr, ah->ar_hln);
71 else
72 memcpy(p, &HE->sll_addr, ah->ar_hln);
73 p += ah->ar_hln;
74
75 memcpy(p, dst_addr, 4);
76 p += 4;
77
Denis Vlasenko459be352007-06-17 19:09:05 +000078 now = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +000079 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
80 if (err == p - buf) {
81 last = now;
82 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000083 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +000084 brd_sent++;
85 }
86 return err;
87}
88
Denis Vlasenko459be352007-06-17 19:09:05 +000089static void finish(void) ATTRIBUTE_NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +000090static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +000091{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000092 if (!(option_mask32 & QUIET)) {
93 printf("Sent %u probe(s) (%u broadcast(s))\n"
94 "Received %u repl%s"
95 " (%u request(s), %u broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +000096 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000097 received, (received == 1) ? "ies" : "y",
98 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +000099 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000100 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000101 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000102 if (option_mask32 & UNSOLICITED)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000103 exit(0);
104 exit(!received);
105}
106
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000107static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000108{
Denis Vlasenko459be352007-06-17 19:09:05 +0000109 static unsigned start;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000110
Denis Vlasenko459be352007-06-17 19:09:05 +0000111 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000112
Denis Vlasenko459be352007-06-17 19:09:05 +0000113 now = MONOTONIC_US();
114 if (start == 0)
115 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000116
Denis Vlasenko459be352007-06-17 19:09:05 +0000117 if (count == 0 || (timeout_us && (now - start) > (timeout_us + 500000)))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000118 finish();
119
Denis Vlasenko459be352007-06-17 19:09:05 +0000120 count--;
121
122 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000123 send_pack(&src, &dst, &me, &he);
124 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000125 finish();
126 }
127 alarm(1);
128}
129
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000130static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000131{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000132 struct arphdr *ah = (struct arphdr *) buf;
133 unsigned char *p = (unsigned char *) (ah + 1);
134 struct in_addr src_ip, dst_ip;
135
Glenn L McGrath9e598412003-01-09 10:06:01 +0000136 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000137 if (FROM->sll_pkttype != PACKET_HOST
138 && FROM->sll_pkttype != PACKET_BROADCAST
139 && FROM->sll_pkttype != PACKET_MULTICAST)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000140 return 0;
141
142 /* Only these types are recognised */
143 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
144 return 0;
145
146 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000147 if (ah->ar_hrd != htons(FROM->sll_hatype)
148 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000149 return 0;
150
151 /* Protocol must be IP. */
152 if (ah->ar_pro != htons(ETH_P_IP))
153 return 0;
154 if (ah->ar_pln != 4)
155 return 0;
156 if (ah->ar_hln != me.sll_halen)
157 return 0;
158 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
159 return 0;
160 memcpy(&src_ip, p + ah->ar_hln, 4);
161 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000162 if (!(option_mask32 & DAD)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000163 if (src_ip.s_addr != dst.s_addr)
164 return 0;
165 if (src.s_addr != dst_ip.s_addr)
166 return 0;
167 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
168 return 0;
169 } else {
170 /* DAD packet was:
171 src_ip = 0 (or some src)
172 src_hw = ME
173 dst_ip = tested address
174 dst_hw = <unspec>
175
176 We fail, if receive request/reply with:
177 src_ip = tested_address
178 src_hw != ME
179 if src_ip in request was not zero, check
180 also that it matches to dst_ip, otherwise
181 dst_ip/dst_hw do not matter.
182 */
183 if (src_ip.s_addr != dst.s_addr)
184 return 0;
185 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
186 return 0;
187 if (src.s_addr && src.s_addr != dst_ip.s_addr)
188 return 0;
189 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000190 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000191 int s_printed = 0;
192
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000193 printf("%scast re%s from %s [%s]",
194 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
195 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000196 inet_ntoa(src_ip),
197 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000198 if (dst_ip.s_addr != src.s_addr) {
199 printf("for %s ", inet_ntoa(dst_ip));
200 s_printed = 1;
201 }
202 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
203 if (!s_printed)
204 printf("for ");
205 printf("[%s]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000206 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000207 }
Rob Landley8ea52052006-03-30 21:50:57 +0000208
Denis Vlasenko459be352007-06-17 19:09:05 +0000209 if (last) {
210 printf(" %u.%03ums\n", last / 1000, last % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000211 } else {
212 printf(" UNSOLICITED?\n");
213 }
214 fflush(stdout);
215 }
216 received++;
217 if (FROM->sll_pkttype != PACKET_HOST)
218 brd_recv++;
219 if (ah->ar_op == htons(ARPOP_REQUEST))
220 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000221 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000222 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000223 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000224 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000225 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000226 }
227 return 1;
228}
229
Denis Vlasenko06af2162007-02-03 17:28:39 +0000230int arping_main(int argc, char **argv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000231int arping_main(int argc, char **argv)
232{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000233 const char *device = "eth0";
Rob Landley8ea52052006-03-30 21:50:57 +0000234 int ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000235 char *source = NULL;
236 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000237 unsigned char *packet;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000238
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000239 sock = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000240
Rob Landleyafb94ec2006-07-16 08:06:34 +0000241 // Drop suid root privileges
242 xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000243
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000244 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000245 unsigned opt;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000246 char *str_count, *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000247
248 /* Dad also sets quit_on_reply.
249 * Advert also sets unsolicited.
250 */
Denis Vlasenko459be352007-06-17 19:09:05 +0000251 opt_complementary = "=1:Df:AU";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000252 opt = getopt32(argv, "DUAqfbc:w:I:s:",
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000253 &str_count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000254 if (opt & 0x40) /* -c: count */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000255 count = xatou(str_count);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000256 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000257 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000;
Denis Vlasenko459be352007-06-17 19:09:05 +0000258 //if (opt & 0x100) /* -I: interface */
259 if (strlen(device) >= IF_NAMESIZE) {
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000260 bb_error_msg_and_die("interface name '%s' is too long",
261 device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000262 }
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000263 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000264 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000265 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000266
Denis Vlasenko459be352007-06-17 19:09:05 +0000267 target = argv[optind];
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000268
Denis Vlasenko40920822006-10-03 20:28:06 +0000269 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000270
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000271 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000272 struct ifreq ifr;
273
274 memset(&ifr, 0, sizeof(ifr));
275 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000276 ioctl_or_perror_and_die(sock, SIOCGIFINDEX, &ifr, "interface %s not found", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000277 ifindex = ifr.ifr_ifindex;
278
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000279 xioctl(sock, SIOCGIFFLAGS, (char *) &ifr);
280
Glenn L McGrath9e598412003-01-09 10:06:01 +0000281 if (!(ifr.ifr_flags & IFF_UP)) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000282 bb_error_msg_and_die("interface %s is down", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000283 }
284 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000285 bb_error_msg("interface %s is not ARPable", device);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000286 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000287 }
288 }
289
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000290 if (!inet_aton(target, &dst)) {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000291 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000292 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000293 memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4);
294 if (ENABLE_FEATURE_CLEAN_UP)
295 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000296 }
297
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000298 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000299 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000300 }
301
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000302 if (!(option_mask32 & DAD) && (option_mask32 & UNSOLICITED) && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000303 src = dst;
304
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000305 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000306 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000307 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000308
Glenn L McGrath9e598412003-01-09 10:06:01 +0000309 if (device) {
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000310 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000311 bb_error_msg("warning: interface %s is ignored", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000312 }
313 memset(&saddr, 0, sizeof(saddr));
314 saddr.sin_family = AF_INET;
315 if (src.s_addr) {
316 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000317 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000318 } else if (!(option_mask32 & DAD)) {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000319 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000320
321 saddr.sin_port = htons(1025);
322 saddr.sin_addr = dst;
323
Denis Vlasenko703e2022007-01-22 14:12:08 +0000324 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000325 bb_perror_msg("warning: setsockopt(SO_DONTROUTE)");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000326 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
327 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000328 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000329 }
330 src = saddr.sin_addr;
331 }
332 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000333 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000334
335 me.sll_family = AF_PACKET;
336 me.sll_ifindex = ifindex;
337 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000338 xbind(sock, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000339
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000340 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000341 socklen_t alen = sizeof(me);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000342
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000343 if (getsockname(sock, (struct sockaddr *) &me, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000344 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000345 }
346 }
347 if (me.sll_halen == 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000348 bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000349 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000350 }
351 he = me;
352 memset(he.sll_addr, -1, he.sll_halen);
353
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000354 if (!(option_mask32 & QUIET)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000355 printf("ARPING to %s from %s via %s\n",
356 inet_ntoa(dst), inet_ntoa(src),
357 device ? device : "unknown");
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000358 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000359
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000360 if (!src.s_addr && !(option_mask32 & DAD)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000361 bb_error_msg_and_die("no src address in the non-DAD mode");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000362 }
363
364 {
365 struct sigaction sa;
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +0000366
Glenn L McGrath9e598412003-01-09 10:06:01 +0000367 memset(&sa, 0, sizeof(sa));
368 sa.sa_flags = SA_RESTART;
369
370 sa.sa_handler = (void (*)(int)) finish;
371 sigaction(SIGINT, &sa, NULL);
372
373 sa.sa_handler = (void (*)(int)) catcher;
374 sigaction(SIGALRM, &sa, NULL);
375 }
376
377 catcher();
378
Denis Vlasenko459be352007-06-17 19:09:05 +0000379 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000380 while (1) {
381 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000382 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000383 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000384 int cc;
385
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000386 cc = recvfrom(sock, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000387 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000388 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000389 continue;
390 }
391 sigemptyset(&sset);
392 sigaddset(&sset, SIGALRM);
393 sigaddset(&sset, SIGINT);
394 sigprocmask(SIG_BLOCK, &sset, &osset);
395 recv_pack(packet, cc, &from);
396 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000397 }
398}