blob: 48c14f15c1bca90576c5e30e607298c9a365f5eb [file] [log] [blame]
Rob Landley8ea52052006-03-30 21:50:57 +00001/* vi:set 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#include <sys/time.h>
14
15#include <errno.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <arpa/inet.h>
21#include <net/if.h>
22#include <netinet/ether.h>
23#include <netpacket/packet.h>
24
25#include "busybox.h"
26
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000027static struct in_addr src;
28static struct in_addr dst;
29static struct sockaddr_ll me;
30static struct sockaddr_ll he;
31static struct timeval last;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000032
33enum cfg_e {
34 dad = 1,
35 unsolicited = 2,
36 advert = 4,
37 quiet = 8,
38 quit_on_reply = 16,
Bernhard Reutner-Fischer61536292006-04-03 09:46:29 +000039 broadcast_only = 32,
40 unicasting = 64
Rob Landley8ea52052006-03-30 21:50:57 +000041};
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000042static int cfg;
Rob Landley8ea52052006-03-30 21:50:57 +000043
44static int s;
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000045static int count = -1;
46static int timeout;
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000047static int sent;
48static int brd_sent;
49static int received;
50static int brd_recv;
51static int req_recv;
Glenn L McGrath9e598412003-01-09 10:06:01 +000052
Rob Landley8ea52052006-03-30 21:50:57 +000053
Glenn L McGrath9e598412003-01-09 10:06:01 +000054#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
55 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
56#if 0
57static void set_signal(int signo, void (*handler) (void))
58{
59 struct sigaction sa;
60
61 memset(&sa, 0, sizeof(sa));
62 sa.sa_handler = (void (*)(int)) handler;
63 sa.sa_flags = SA_RESTART;
64 sigaction(signo, &sa, NULL);
65}
66#endif
67
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +000068static int send_pack(int sock, struct in_addr *src_addr,
69 struct in_addr *dst_addr, struct sockaddr_ll *ME,
70 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000071{
72 int err;
73 struct timeval now;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +000074 RESERVE_CONFIG_UBUFFER(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;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000083 ah->ar_op = cfg&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
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000091 if (cfg&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
100 gettimeofday(&now, NULL);
101 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
102 if (err == p - buf) {
103 last = now;
104 sent++;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000105 if (!(cfg&unicasting))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000106 brd_sent++;
107 }
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000108 RELEASE_CONFIG_BUFFER(buf);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000109 return err;
110}
111
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000112static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000113{
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000114 if (!(cfg&quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000115 printf("Sent %d probes (%d broadcast(s))\n"
116 "Received %d repl%s",
117 sent, brd_sent,
118 received, (received > 1) ? "ies" : "y");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000119 if (brd_recv || req_recv) {
120 printf(" (");
121 if (req_recv)
122 printf("%d request(s)", req_recv);
123 if (brd_recv)
124 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
125 putchar(')');
126 }
127 putchar('\n');
128 fflush(stdout);
129 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000130 if (cfg&dad)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000131 exit(!!received);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000132 if (cfg&unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000133 exit(0);
134 exit(!received);
135}
136
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000137static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000138{
139 struct timeval tv;
Glenn L McGrath8b96b712003-08-28 19:54:16 +0000140 static struct timeval start;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000141
142 gettimeofday(&tv, NULL);
143
144 if (start.tv_sec == 0)
145 start = tv;
146
147 if (count-- == 0
148 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
149 finish();
150
151 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
152 send_pack(s, &src, &dst, &me, &he);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000153 if (count == 0 && cfg&unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000154 finish();
155 }
156 alarm(1);
157}
158
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000159static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000160{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000161 struct arphdr *ah = (struct arphdr *) buf;
162 unsigned char *p = (unsigned char *) (ah + 1);
163 struct in_addr src_ip, dst_ip;
164
Glenn L McGrath9e598412003-01-09 10:06:01 +0000165 /* Filter out wild packets */
166 if (FROM->sll_pkttype != PACKET_HOST &&
167 FROM->sll_pkttype != PACKET_BROADCAST &&
168 FROM->sll_pkttype != PACKET_MULTICAST)
169 return 0;
170
171 /* Only these types are recognised */
172 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
173 return 0;
174
175 /* ARPHRD check and this darned FDDI hack here :-( */
176 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
177 (FROM->sll_hatype != ARPHRD_FDDI
178 || ah->ar_hrd != htons(ARPHRD_ETHER)))
179 return 0;
180
181 /* Protocol must be IP. */
182 if (ah->ar_pro != htons(ETH_P_IP))
183 return 0;
184 if (ah->ar_pln != 4)
185 return 0;
186 if (ah->ar_hln != me.sll_halen)
187 return 0;
188 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
189 return 0;
190 memcpy(&src_ip, p + ah->ar_hln, 4);
191 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000192 if (!(cfg&dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000193 if (src_ip.s_addr != dst.s_addr)
194 return 0;
195 if (src.s_addr != dst_ip.s_addr)
196 return 0;
197 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
198 return 0;
199 } else {
200 /* DAD packet was:
201 src_ip = 0 (or some src)
202 src_hw = ME
203 dst_ip = tested address
204 dst_hw = <unspec>
205
206 We fail, if receive request/reply with:
207 src_ip = tested_address
208 src_hw != ME
209 if src_ip in request was not zero, check
210 also that it matches to dst_ip, otherwise
211 dst_ip/dst_hw do not matter.
212 */
213 if (src_ip.s_addr != dst.s_addr)
214 return 0;
215 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
216 return 0;
217 if (src.s_addr && src.s_addr != dst_ip.s_addr)
218 return 0;
219 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000220 if (!(cfg&quiet)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000221 int s_printed = 0;
Rob Landley8ea52052006-03-30 21:50:57 +0000222 struct timeval tv;
223
224 gettimeofday(&tv, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000225
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000226 printf("%s %s from %s [%s]",
227 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
228 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
229 inet_ntoa(src_ip),
230 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000231 if (dst_ip.s_addr != src.s_addr) {
232 printf("for %s ", inet_ntoa(dst_ip));
233 s_printed = 1;
234 }
235 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
236 if (!s_printed)
237 printf("for ");
238 printf("[%s]",
239 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
240 }
Rob Landley8ea52052006-03-30 21:50:57 +0000241
Glenn L McGrath9e598412003-01-09 10:06:01 +0000242 if (last.tv_sec) {
243 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
244 tv.tv_usec - last.tv_usec;
245 long msecs = (usecs + 500) / 1000;
246
247 usecs -= msecs * 1000 - 500;
248 printf(" %ld.%03ldms\n", msecs, usecs);
249 } else {
250 printf(" UNSOLICITED?\n");
251 }
252 fflush(stdout);
253 }
254 received++;
255 if (FROM->sll_pkttype != PACKET_HOST)
256 brd_recv++;
257 if (ah->ar_op == htons(ARPOP_REQUEST))
258 req_recv++;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000259 if (cfg&quit_on_reply)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000260 finish();
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000261 if (!(cfg&broadcast_only)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000262 memcpy(he.sll_addr, p, me.sll_halen);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000263 cfg |= unicasting;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000264 }
265 return 1;
266}
267
268int arping_main(int argc, char **argv)
269{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000270 char *device = "eth0";
Rob Landley8ea52052006-03-30 21:50:57 +0000271 int ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000272 char *source = NULL;
273 char *target;
274
275 s = socket(PF_PACKET, SOCK_DGRAM, 0);
Rob Landley8ea52052006-03-30 21:50:57 +0000276 ifindex = errno;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000277
Rob Landley8ea52052006-03-30 21:50:57 +0000278 setuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000279
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000280 {
281 unsigned long opt;
282 char *_count, *_timeout, *_device;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000283
284 /* Dad also sets quit_on_reply.
285 * Advert also sets unsolicited.
286 */
287 bb_opt_complementally = "Df:AU";
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000288 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
289 &_count, &_timeout, &_device);
Bernhard Reutner-Fischer61536292006-04-03 09:46:29 +0000290 cfg |= opt & 63; /* set respective flags */
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000291 if (opt & 64) /* count */
292 count = atoi(_count);
293 if (opt & 128) /* timeout */
294 timeout = atoi(_timeout);
295 if (opt & 256) { /* interface */
296 if (bb_strlen(_device) > IF_NAMESIZE) {
297 bb_error_msg_and_die("Interface name `%s' must be less than %d",
298 _device, IF_NAMESIZE);
299 }
300 device = _device;
301 }
302 if (opt & 512) /* source */
303 source = optarg;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000304 }
305 argc -= optind;
306 argv += optind;
307
308 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000309 bb_show_usage();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000310
311 target = *argv;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000312
Glenn L McGrath9e598412003-01-09 10:06:01 +0000313
314 if (s < 0) {
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000315 bb_default_error_retval = ifindex;
316 bb_perror_msg_and_die("socket");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000317 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000318 bb_default_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000319
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000320 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000321 struct ifreq ifr;
322
323 memset(&ifr, 0, sizeof(ifr));
324 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
325 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
Rob Landley8ea52052006-03-30 21:50:57 +0000326 bb_error_msg_and_die("Interface %s not found", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000327 }
328 ifindex = ifr.ifr_ifindex;
329
330 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000331 bb_error_msg_and_die("SIOCGIFFLAGS");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000332 }
333 if (!(ifr.ifr_flags & IFF_UP)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000334 bb_error_msg_and_die("Interface %s is down", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000335 }
336 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000337 bb_error_msg("Interface %s is not ARPable", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000338 exit(cfg&dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000339 }
340 }
341
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000342 if (!inet_aton(target, &dst)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000343 struct hostent *hp;
344
345 hp = gethostbyname2(target, AF_INET);
346 if (!hp) {
Rob Landley8ea52052006-03-30 21:50:57 +0000347 bb_error_msg_and_die("invalid or unknown target %s", target);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000348 }
349 memcpy(&dst, hp->h_addr, 4);
350 }
351
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000352 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000353 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000354 }
355
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000356 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000357 src = dst;
358
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000359 if (!(cfg&dad) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000360 struct sockaddr_in saddr;
361 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
362
363 if (probe_fd < 0) {
Rob Landley8ea52052006-03-30 21:50:57 +0000364 bb_error_msg_and_die("socket");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000365 }
366 if (device) {
367 if (setsockopt
368 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
369 strlen(device) + 1) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000370 bb_error_msg("WARNING: interface %s is ignored", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000371 }
372 memset(&saddr, 0, sizeof(saddr));
373 saddr.sin_family = AF_INET;
374 if (src.s_addr) {
375 saddr.sin_addr = src;
376 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000377 bb_error_msg_and_die("bind");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000378 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000379 } else if (!(cfg&dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000380 int on = 1;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000381 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000382
383 saddr.sin_port = htons(1025);
384 saddr.sin_addr = dst;
385
386 if (setsockopt
387 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
388 sizeof(on)) == -1)
Rob Landley8ea52052006-03-30 21:50:57 +0000389 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000390 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
391 == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000392 bb_error_msg_and_die("connect");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000393 }
394 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
395 -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000396 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000397 }
398 src = saddr.sin_addr;
399 }
400 close(probe_fd);
401 };
402
403 me.sll_family = AF_PACKET;
404 me.sll_ifindex = ifindex;
405 me.sll_protocol = htons(ETH_P_ARP);
406 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000407 bb_error_msg_and_die("bind");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000408 }
409
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000410 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000411 socklen_t alen = sizeof(me);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000412
413 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000414 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000415 }
416 }
417 if (me.sll_halen == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000418 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000419 exit(cfg&dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000420 }
421 he = me;
422 memset(he.sll_addr, -1, he.sll_halen);
423
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000424 if (!(cfg&quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000425 printf("ARPING to %s from %s via %s\n",
426 inet_ntoa(dst), inet_ntoa(src),
427 device ? device : "unknown");
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000428 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000429
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000430 if (!src.s_addr && !(cfg&dad)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000431 bb_error_msg_and_die("no src address in the non-DAD mode");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000432 }
433
434 {
435 struct sigaction sa;
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +0000436
Glenn L McGrath9e598412003-01-09 10:06:01 +0000437 memset(&sa, 0, sizeof(sa));
438 sa.sa_flags = SA_RESTART;
439
440 sa.sa_handler = (void (*)(int)) finish;
441 sigaction(SIGINT, &sa, NULL);
442
443 sa.sa_handler = (void (*)(int)) catcher;
444 sigaction(SIGALRM, &sa, NULL);
445 }
446
447 catcher();
448
449 while (1) {
450 sigset_t sset, osset;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000451 RESERVE_CONFIG_UBUFFER(packet, 4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000452 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000453 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000454 int cc;
455
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000456 if ((cc = recvfrom(s, packet, 4096, 0,
Glenn L McGrath9e598412003-01-09 10:06:01 +0000457 (struct sockaddr *) &from, &alen)) < 0) {
458 perror("recvfrom");
459 continue;
460 }
461 sigemptyset(&sset);
462 sigaddset(&sset, SIGALRM);
463 sigaddset(&sset, SIGINT);
464 sigprocmask(SIG_BLOCK, &sset, &osset);
465 recv_pack(packet, cc, &from);
466 sigprocmask(SIG_SETMASK, &osset, NULL);
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000467 RELEASE_CONFIG_BUFFER(packet);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000468 }
469}