blob: e12fa508b41c004c75784250f188bbbfe6dae748 [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
11#include <sys/ioctl.h>
Mike Frysinger06adf5f2006-03-22 00:25:07 +000012#include <signal.h>
Glenn L McGrath9e598412003-01-09 10:06:01 +000013
14#include <errno.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
18
19#include <arpa/inet.h>
20#include <net/if.h>
21#include <netinet/ether.h>
22#include <netpacket/packet.h>
23
24#include "busybox.h"
25
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000026static struct in_addr src;
27static struct in_addr dst;
28static struct sockaddr_ll me;
29static struct sockaddr_ll he;
30static struct timeval last;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000031
32enum cfg_e {
33 dad = 1,
34 unsolicited = 2,
35 advert = 4,
36 quiet = 8,
37 quit_on_reply = 16,
Bernhard Reutner-Fischer61536292006-04-03 09:46:29 +000038 broadcast_only = 32,
39 unicasting = 64
Rob Landley8ea52052006-03-30 21:50:57 +000040};
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000041static int cfg;
Rob Landley8ea52052006-03-30 21:50:57 +000042
43static int s;
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000044static int count = -1;
45static int timeout;
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000046static int sent;
47static int brd_sent;
48static int received;
49static int brd_recv;
50static int req_recv;
Glenn L McGrath9e598412003-01-09 10:06:01 +000051
Rob Landley8ea52052006-03-30 21:50:57 +000052
Glenn L McGrath9e598412003-01-09 10:06:01 +000053#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
54 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
55#if 0
56static void set_signal(int signo, void (*handler) (void))
57{
58 struct sigaction sa;
59
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = (void (*)(int)) handler;
62 sa.sa_flags = SA_RESTART;
63 sigaction(signo, &sa, NULL);
64}
65#endif
66
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +000067static int send_pack(int sock, struct in_addr *src_addr,
68 struct in_addr *dst_addr, struct sockaddr_ll *ME,
69 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000070{
71 int err;
72 struct timeval now;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +000073 RESERVE_CONFIG_UBUFFER(buf, 256);
Glenn L McGrath9e598412003-01-09 10:06:01 +000074 struct arphdr *ah = (struct arphdr *) buf;
75 unsigned char *p = (unsigned char *) (ah + 1);
76
77 ah->ar_hrd = htons(ME->sll_hatype);
78 ah->ar_hrd = htons(ARPHRD_ETHER);
79 ah->ar_pro = htons(ETH_P_IP);
80 ah->ar_hln = ME->sll_halen;
81 ah->ar_pln = 4;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000082 ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000083
84 memcpy(p, &ME->sll_addr, ah->ar_hln);
85 p += ME->sll_halen;
86
87 memcpy(p, src_addr, 4);
88 p += 4;
89
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000090 if (cfg&advert)
Glenn L McGrath9e598412003-01-09 10:06:01 +000091 memcpy(p, &ME->sll_addr, ah->ar_hln);
92 else
93 memcpy(p, &HE->sll_addr, ah->ar_hln);
94 p += ah->ar_hln;
95
96 memcpy(p, dst_addr, 4);
97 p += 4;
98
99 gettimeofday(&now, NULL);
100 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
101 if (err == p - buf) {
102 last = now;
103 sent++;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000104 if (!(cfg&unicasting))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000105 brd_sent++;
106 }
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000107 RELEASE_CONFIG_BUFFER(buf);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000108 return err;
109}
110
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000111static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000112{
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000113 if (!(cfg&quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000114 printf("Sent %d probes (%d broadcast(s))\n"
115 "Received %d repl%s",
116 sent, brd_sent,
117 received, (received > 1) ? "ies" : "y");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000118 if (brd_recv || req_recv) {
119 printf(" (");
120 if (req_recv)
121 printf("%d request(s)", req_recv);
122 if (brd_recv)
123 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
124 putchar(')');
125 }
126 putchar('\n');
127 fflush(stdout);
128 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000129 if (cfg&dad)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000130 exit(!!received);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000131 if (cfg&unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000132 exit(0);
133 exit(!received);
134}
135
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000136static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000137{
138 struct timeval tv;
Glenn L McGrath8b96b712003-08-28 19:54:16 +0000139 static struct timeval start;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000140
141 gettimeofday(&tv, NULL);
142
143 if (start.tv_sec == 0)
144 start = tv;
145
146 if (count-- == 0
147 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
148 finish();
149
150 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
151 send_pack(s, &src, &dst, &me, &he);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000152 if (count == 0 && cfg&unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000153 finish();
154 }
155 alarm(1);
156}
157
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000158static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000159{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000160 struct arphdr *ah = (struct arphdr *) buf;
161 unsigned char *p = (unsigned char *) (ah + 1);
162 struct in_addr src_ip, dst_ip;
163
Glenn L McGrath9e598412003-01-09 10:06:01 +0000164 /* Filter out wild packets */
165 if (FROM->sll_pkttype != PACKET_HOST &&
166 FROM->sll_pkttype != PACKET_BROADCAST &&
167 FROM->sll_pkttype != PACKET_MULTICAST)
168 return 0;
169
170 /* Only these types are recognised */
171 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
172 return 0;
173
174 /* ARPHRD check and this darned FDDI hack here :-( */
175 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
176 (FROM->sll_hatype != ARPHRD_FDDI
177 || ah->ar_hrd != htons(ARPHRD_ETHER)))
178 return 0;
179
180 /* Protocol must be IP. */
181 if (ah->ar_pro != htons(ETH_P_IP))
182 return 0;
183 if (ah->ar_pln != 4)
184 return 0;
185 if (ah->ar_hln != me.sll_halen)
186 return 0;
187 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
188 return 0;
189 memcpy(&src_ip, p + ah->ar_hln, 4);
190 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000191 if (!(cfg&dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000192 if (src_ip.s_addr != dst.s_addr)
193 return 0;
194 if (src.s_addr != dst_ip.s_addr)
195 return 0;
196 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
197 return 0;
198 } else {
199 /* DAD packet was:
200 src_ip = 0 (or some src)
201 src_hw = ME
202 dst_ip = tested address
203 dst_hw = <unspec>
204
205 We fail, if receive request/reply with:
206 src_ip = tested_address
207 src_hw != ME
208 if src_ip in request was not zero, check
209 also that it matches to dst_ip, otherwise
210 dst_ip/dst_hw do not matter.
211 */
212 if (src_ip.s_addr != dst.s_addr)
213 return 0;
214 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
215 return 0;
216 if (src.s_addr && src.s_addr != dst_ip.s_addr)
217 return 0;
218 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000219 if (!(cfg&quiet)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000220 int s_printed = 0;
Rob Landley8ea52052006-03-30 21:50:57 +0000221 struct timeval tv;
222
223 gettimeofday(&tv, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000224
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000225 printf("%s %s from %s [%s]",
226 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
227 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
228 inet_ntoa(src_ip),
229 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000230 if (dst_ip.s_addr != src.s_addr) {
231 printf("for %s ", inet_ntoa(dst_ip));
232 s_printed = 1;
233 }
234 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
235 if (!s_printed)
236 printf("for ");
237 printf("[%s]",
238 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
239 }
Rob Landley8ea52052006-03-30 21:50:57 +0000240
Glenn L McGrath9e598412003-01-09 10:06:01 +0000241 if (last.tv_sec) {
242 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
243 tv.tv_usec - last.tv_usec;
244 long msecs = (usecs + 500) / 1000;
245
246 usecs -= msecs * 1000 - 500;
247 printf(" %ld.%03ldms\n", msecs, usecs);
248 } else {
249 printf(" UNSOLICITED?\n");
250 }
251 fflush(stdout);
252 }
253 received++;
254 if (FROM->sll_pkttype != PACKET_HOST)
255 brd_recv++;
256 if (ah->ar_op == htons(ARPOP_REQUEST))
257 req_recv++;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000258 if (cfg&quit_on_reply)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000259 finish();
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000260 if (!(cfg&broadcast_only)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000261 memcpy(he.sll_addr, p, me.sll_halen);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000262 cfg |= unicasting;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000263 }
264 return 1;
265}
266
267int arping_main(int argc, char **argv)
268{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000269 char *device = "eth0";
Rob Landley8ea52052006-03-30 21:50:57 +0000270 int ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000271 char *source = NULL;
272 char *target;
273
274 s = socket(PF_PACKET, SOCK_DGRAM, 0);
Rob Landley8ea52052006-03-30 21:50:57 +0000275 ifindex = errno;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000276
Rob Landley8ea52052006-03-30 21:50:57 +0000277 setuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000278
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000279 {
280 unsigned long opt;
281 char *_count, *_timeout, *_device;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000282
283 /* Dad also sets quit_on_reply.
284 * Advert also sets unsolicited.
285 */
286 bb_opt_complementally = "Df:AU";
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000287 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
288 &_count, &_timeout, &_device);
Bernhard Reutner-Fischer61536292006-04-03 09:46:29 +0000289 cfg |= opt & 63; /* set respective flags */
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000290 if (opt & 64) /* count */
291 count = atoi(_count);
292 if (opt & 128) /* timeout */
293 timeout = atoi(_timeout);
294 if (opt & 256) { /* interface */
Rob Landleya3896512006-05-07 20:20:34 +0000295 if (strlen(_device) > IF_NAMESIZE) {
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000296 bb_error_msg_and_die("Interface name `%s' must be less than %d",
297 _device, IF_NAMESIZE);
298 }
299 device = _device;
300 }
301 if (opt & 512) /* source */
302 source = optarg;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000303 }
304 argc -= optind;
305 argv += optind;
306
307 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000308 bb_show_usage();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000309
310 target = *argv;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000311
Glenn L McGrath9e598412003-01-09 10:06:01 +0000312
313 if (s < 0) {
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000314 bb_default_error_retval = ifindex;
315 bb_perror_msg_and_die("socket");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000316 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000317 bb_default_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000318
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000319 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000320 struct ifreq ifr;
321
322 memset(&ifr, 0, sizeof(ifr));
323 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
324 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
Rob Landley8ea52052006-03-30 21:50:57 +0000325 bb_error_msg_and_die("Interface %s not found", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000326 }
327 ifindex = ifr.ifr_ifindex;
328
329 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000330 bb_error_msg_and_die("SIOCGIFFLAGS");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000331 }
332 if (!(ifr.ifr_flags & IFF_UP)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000333 bb_error_msg_and_die("Interface %s is down", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000334 }
335 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000336 bb_error_msg("Interface %s is not ARPable", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000337 exit(cfg&dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000338 }
339 }
340
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000341 if (!inet_aton(target, &dst)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000342 struct hostent *hp;
343
344 hp = gethostbyname2(target, AF_INET);
345 if (!hp) {
Rob Landley8ea52052006-03-30 21:50:57 +0000346 bb_error_msg_and_die("invalid or unknown target %s", target);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000347 }
348 memcpy(&dst, hp->h_addr, 4);
349 }
350
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000351 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000352 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000353 }
354
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000355 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000356 src = dst;
357
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000358 if (!(cfg&dad) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000359 struct sockaddr_in saddr;
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000360 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000361
362 if (probe_fd < 0) {
Rob Landley8ea52052006-03-30 21:50:57 +0000363 bb_error_msg_and_die("socket");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000364 }
365 if (device) {
366 if (setsockopt
367 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
368 strlen(device) + 1) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000369 bb_error_msg("WARNING: interface %s is ignored", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000370 }
371 memset(&saddr, 0, sizeof(saddr));
372 saddr.sin_family = AF_INET;
373 if (src.s_addr) {
374 saddr.sin_addr = src;
375 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000376 bb_error_msg_and_die("bind");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000377 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000378 } else if (!(cfg&dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000379 int on = 1;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000380 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000381
382 saddr.sin_port = htons(1025);
383 saddr.sin_addr = dst;
384
385 if (setsockopt
386 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
387 sizeof(on)) == -1)
Rob Landley8ea52052006-03-30 21:50:57 +0000388 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000389 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
390 == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000391 bb_error_msg_and_die("connect");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000392 }
393 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
394 -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000395 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000396 }
397 src = saddr.sin_addr;
398 }
399 close(probe_fd);
400 };
401
402 me.sll_family = AF_PACKET;
403 me.sll_ifindex = ifindex;
404 me.sll_protocol = htons(ETH_P_ARP);
405 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000406 bb_error_msg_and_die("bind");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000407 }
408
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000409 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000410 socklen_t alen = sizeof(me);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000411
412 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000413 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000414 }
415 }
416 if (me.sll_halen == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000417 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000418 exit(cfg&dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000419 }
420 he = me;
421 memset(he.sll_addr, -1, he.sll_halen);
422
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000423 if (!(cfg&quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000424 printf("ARPING to %s from %s via %s\n",
425 inet_ntoa(dst), inet_ntoa(src),
426 device ? device : "unknown");
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000427 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000428
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000429 if (!src.s_addr && !(cfg&dad)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000430 bb_error_msg_and_die("no src address in the non-DAD mode");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000431 }
432
433 {
434 struct sigaction sa;
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +0000435
Glenn L McGrath9e598412003-01-09 10:06:01 +0000436 memset(&sa, 0, sizeof(sa));
437 sa.sa_flags = SA_RESTART;
438
439 sa.sa_handler = (void (*)(int)) finish;
440 sigaction(SIGINT, &sa, NULL);
441
442 sa.sa_handler = (void (*)(int)) catcher;
443 sigaction(SIGALRM, &sa, NULL);
444 }
445
446 catcher();
447
448 while (1) {
449 sigset_t sset, osset;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000450 RESERVE_CONFIG_UBUFFER(packet, 4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000451 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000452 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000453 int cc;
454
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000455 if ((cc = recvfrom(s, packet, 4096, 0,
Glenn L McGrath9e598412003-01-09 10:06:01 +0000456 (struct sockaddr *) &from, &alen)) < 0) {
457 perror("recvfrom");
458 continue;
459 }
460 sigemptyset(&sset);
461 sigaddset(&sset, SIGALRM);
462 sigaddset(&sset, SIGINT);
463 sigprocmask(SIG_BLOCK, &sset, &osset);
464 recv_pack(packet, cc, &from);
465 sigprocmask(SIG_SETMASK, &osset, NULL);
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000466 RELEASE_CONFIG_BUFFER(packet);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000467 }
468}