blob: 5665ddb2b2522a927f1c53e8dbca3745b537a90f [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 )
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +000055static int send_pack(int sock, struct in_addr *src_addr,
56 struct in_addr *dst_addr, struct sockaddr_ll *ME,
57 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000058{
59 int err;
60 struct timeval now;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +000061 RESERVE_CONFIG_UBUFFER(buf, 256);
Glenn L McGrath9e598412003-01-09 10:06:01 +000062 struct arphdr *ah = (struct arphdr *) buf;
63 unsigned char *p = (unsigned char *) (ah + 1);
64
65 ah->ar_hrd = htons(ME->sll_hatype);
66 ah->ar_hrd = htons(ARPHRD_ETHER);
67 ah->ar_pro = htons(ETH_P_IP);
68 ah->ar_hln = ME->sll_halen;
69 ah->ar_pln = 4;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000070 ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000071
72 memcpy(p, &ME->sll_addr, ah->ar_hln);
73 p += ME->sll_halen;
74
75 memcpy(p, src_addr, 4);
76 p += 4;
77
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000078 if (cfg&advert)
Glenn L McGrath9e598412003-01-09 10:06:01 +000079 memcpy(p, &ME->sll_addr, ah->ar_hln);
80 else
81 memcpy(p, &HE->sll_addr, ah->ar_hln);
82 p += ah->ar_hln;
83
84 memcpy(p, dst_addr, 4);
85 p += 4;
86
87 gettimeofday(&now, NULL);
88 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
89 if (err == p - buf) {
90 last = now;
91 sent++;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000092 if (!(cfg&unicasting))
Glenn L McGrath9e598412003-01-09 10:06:01 +000093 brd_sent++;
94 }
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +000095 RELEASE_CONFIG_BUFFER(buf);
Glenn L McGrath9e598412003-01-09 10:06:01 +000096 return err;
97}
98
Eric Andersen14f5c8d2005-04-16 19:39:00 +000099static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000100{
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000101 if (!(cfg&quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000102 printf("Sent %d probes (%d broadcast(s))\n"
103 "Received %d repl%s",
104 sent, brd_sent,
105 received, (received > 1) ? "ies" : "y");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000106 if (brd_recv || req_recv) {
107 printf(" (");
108 if (req_recv)
109 printf("%d request(s)", req_recv);
110 if (brd_recv)
111 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
112 putchar(')');
113 }
114 putchar('\n');
115 fflush(stdout);
116 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000117 if (cfg&dad)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000118 exit(!!received);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000119 if (cfg&unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000120 exit(0);
121 exit(!received);
122}
123
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000124static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000125{
126 struct timeval tv;
Glenn L McGrath8b96b712003-08-28 19:54:16 +0000127 static struct timeval start;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000128
129 gettimeofday(&tv, NULL);
130
131 if (start.tv_sec == 0)
132 start = tv;
133
134 if (count-- == 0
135 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
136 finish();
137
138 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
139 send_pack(s, &src, &dst, &me, &he);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000140 if (count == 0 && cfg&unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000141 finish();
142 }
143 alarm(1);
144}
145
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000146static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000147{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000148 struct arphdr *ah = (struct arphdr *) buf;
149 unsigned char *p = (unsigned char *) (ah + 1);
150 struct in_addr src_ip, dst_ip;
151
Glenn L McGrath9e598412003-01-09 10:06:01 +0000152 /* Filter out wild packets */
153 if (FROM->sll_pkttype != PACKET_HOST &&
154 FROM->sll_pkttype != PACKET_BROADCAST &&
155 FROM->sll_pkttype != PACKET_MULTICAST)
156 return 0;
157
158 /* Only these types are recognised */
159 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
160 return 0;
161
162 /* ARPHRD check and this darned FDDI hack here :-( */
163 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
164 (FROM->sll_hatype != ARPHRD_FDDI
165 || ah->ar_hrd != htons(ARPHRD_ETHER)))
166 return 0;
167
168 /* Protocol must be IP. */
169 if (ah->ar_pro != htons(ETH_P_IP))
170 return 0;
171 if (ah->ar_pln != 4)
172 return 0;
173 if (ah->ar_hln != me.sll_halen)
174 return 0;
175 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
176 return 0;
177 memcpy(&src_ip, p + ah->ar_hln, 4);
178 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000179 if (!(cfg&dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000180 if (src_ip.s_addr != dst.s_addr)
181 return 0;
182 if (src.s_addr != dst_ip.s_addr)
183 return 0;
184 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
185 return 0;
186 } else {
187 /* DAD packet was:
188 src_ip = 0 (or some src)
189 src_hw = ME
190 dst_ip = tested address
191 dst_hw = <unspec>
192
193 We fail, if receive request/reply with:
194 src_ip = tested_address
195 src_hw != ME
196 if src_ip in request was not zero, check
197 also that it matches to dst_ip, otherwise
198 dst_ip/dst_hw do not matter.
199 */
200 if (src_ip.s_addr != dst.s_addr)
201 return 0;
202 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
203 return 0;
204 if (src.s_addr && src.s_addr != dst_ip.s_addr)
205 return 0;
206 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000207 if (!(cfg&quiet)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000208 int s_printed = 0;
Rob Landley8ea52052006-03-30 21:50:57 +0000209 struct timeval tv;
210
211 gettimeofday(&tv, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000212
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000213 printf("%s %s from %s [%s]",
214 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
215 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
216 inet_ntoa(src_ip),
217 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000218 if (dst_ip.s_addr != src.s_addr) {
219 printf("for %s ", inet_ntoa(dst_ip));
220 s_printed = 1;
221 }
222 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
223 if (!s_printed)
224 printf("for ");
225 printf("[%s]",
226 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
227 }
Rob Landley8ea52052006-03-30 21:50:57 +0000228
Glenn L McGrath9e598412003-01-09 10:06:01 +0000229 if (last.tv_sec) {
230 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
231 tv.tv_usec - last.tv_usec;
232 long msecs = (usecs + 500) / 1000;
233
234 usecs -= msecs * 1000 - 500;
235 printf(" %ld.%03ldms\n", msecs, usecs);
236 } else {
237 printf(" UNSOLICITED?\n");
238 }
239 fflush(stdout);
240 }
241 received++;
242 if (FROM->sll_pkttype != PACKET_HOST)
243 brd_recv++;
244 if (ah->ar_op == htons(ARPOP_REQUEST))
245 req_recv++;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000246 if (cfg&quit_on_reply)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000247 finish();
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000248 if (!(cfg&broadcast_only)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000249 memcpy(he.sll_addr, p, me.sll_halen);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000250 cfg |= unicasting;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000251 }
252 return 1;
253}
254
255int arping_main(int argc, char **argv)
256{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000257 char *device = "eth0";
Rob Landley8ea52052006-03-30 21:50:57 +0000258 int ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000259 char *source = NULL;
260 char *target;
261
262 s = socket(PF_PACKET, SOCK_DGRAM, 0);
Rob Landley8ea52052006-03-30 21:50:57 +0000263 ifindex = errno;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000264
Rob Landleyafb94ec2006-07-16 08:06:34 +0000265 // Drop suid root privileges
266 xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000267
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000268 {
269 unsigned long opt;
270 char *_count, *_timeout, *_device;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000271
272 /* Dad also sets quit_on_reply.
273 * Advert also sets unsolicited.
274 */
275 bb_opt_complementally = "Df:AU";
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000276 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
277 &_count, &_timeout, &_device);
Bernhard Reutner-Fischer61536292006-04-03 09:46:29 +0000278 cfg |= opt & 63; /* set respective flags */
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000279 if (opt & 64) /* count */
280 count = atoi(_count);
281 if (opt & 128) /* timeout */
282 timeout = atoi(_timeout);
283 if (opt & 256) { /* interface */
Rob Landleya3896512006-05-07 20:20:34 +0000284 if (strlen(_device) > IF_NAMESIZE) {
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000285 bb_error_msg_and_die("Interface name `%s' must be less than %d",
286 _device, IF_NAMESIZE);
287 }
288 device = _device;
289 }
290 if (opt & 512) /* source */
291 source = optarg;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000292 }
293 argc -= optind;
294 argv += optind;
295
296 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000297 bb_show_usage();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000298
299 target = *argv;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000300
Glenn L McGrath9e598412003-01-09 10:06:01 +0000301
302 if (s < 0) {
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000303 bb_default_error_retval = ifindex;
304 bb_perror_msg_and_die("socket");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000305 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000306 bb_default_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000307
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000308 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000309 struct ifreq ifr;
310
311 memset(&ifr, 0, sizeof(ifr));
312 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
313 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
Rob Landley8ea52052006-03-30 21:50:57 +0000314 bb_error_msg_and_die("Interface %s not found", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000315 }
316 ifindex = ifr.ifr_ifindex;
317
318 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000319 bb_error_msg_and_die("SIOCGIFFLAGS");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000320 }
321 if (!(ifr.ifr_flags & IFF_UP)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000322 bb_error_msg_and_die("Interface %s is down", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000323 }
324 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000325 bb_error_msg("Interface %s is not ARPable", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000326 exit(cfg&dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000327 }
328 }
329
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000330 if (!inet_aton(target, &dst)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000331 struct hostent *hp;
332
333 hp = gethostbyname2(target, AF_INET);
334 if (!hp) {
Rob Landley8ea52052006-03-30 21:50:57 +0000335 bb_error_msg_and_die("invalid or unknown target %s", target);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000336 }
337 memcpy(&dst, hp->h_addr, 4);
338 }
339
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000340 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000341 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000342 }
343
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000344 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000345 src = dst;
346
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000347 if (!(cfg&dad) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000348 struct sockaddr_in saddr;
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000349 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000350
351 if (probe_fd < 0) {
Rob Landley8ea52052006-03-30 21:50:57 +0000352 bb_error_msg_and_die("socket");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000353 }
354 if (device) {
355 if (setsockopt
356 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
357 strlen(device) + 1) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000358 bb_error_msg("WARNING: interface %s is ignored", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000359 }
360 memset(&saddr, 0, sizeof(saddr));
361 saddr.sin_family = AF_INET;
362 if (src.s_addr) {
363 saddr.sin_addr = src;
364 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000365 bb_error_msg_and_die("bind");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000366 }
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000367 } else if (!(cfg&dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000368 int on = 1;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000369 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000370
371 saddr.sin_port = htons(1025);
372 saddr.sin_addr = dst;
373
374 if (setsockopt
375 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
376 sizeof(on)) == -1)
Rob Landley8ea52052006-03-30 21:50:57 +0000377 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000378 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
379 == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000380 bb_error_msg_and_die("connect");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000381 }
382 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
383 -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000384 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000385 }
386 src = saddr.sin_addr;
387 }
388 close(probe_fd);
389 };
390
391 me.sll_family = AF_PACKET;
392 me.sll_ifindex = ifindex;
393 me.sll_protocol = htons(ETH_P_ARP);
394 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000395 bb_error_msg_and_die("bind");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000396 }
397
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000398 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000399 socklen_t alen = sizeof(me);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000400
401 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000402 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000403 }
404 }
405 if (me.sll_halen == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000406 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000407 exit(cfg&dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000408 }
409 he = me;
410 memset(he.sll_addr, -1, he.sll_halen);
411
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000412 if (!(cfg&quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000413 printf("ARPING to %s from %s via %s\n",
414 inet_ntoa(dst), inet_ntoa(src),
415 device ? device : "unknown");
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000416 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000417
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000418 if (!src.s_addr && !(cfg&dad)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000419 bb_error_msg_and_die("no src address in the non-DAD mode");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000420 }
421
422 {
423 struct sigaction sa;
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +0000424
Glenn L McGrath9e598412003-01-09 10:06:01 +0000425 memset(&sa, 0, sizeof(sa));
426 sa.sa_flags = SA_RESTART;
427
428 sa.sa_handler = (void (*)(int)) finish;
429 sigaction(SIGINT, &sa, NULL);
430
431 sa.sa_handler = (void (*)(int)) catcher;
432 sigaction(SIGALRM, &sa, NULL);
433 }
434
435 catcher();
436
437 while (1) {
438 sigset_t sset, osset;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000439 RESERVE_CONFIG_UBUFFER(packet, 4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000440 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000441 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000442 int cc;
443
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000444 if ((cc = recvfrom(s, packet, 4096, 0,
Glenn L McGrath9e598412003-01-09 10:06:01 +0000445 (struct sockaddr *) &from, &alen)) < 0) {
446 perror("recvfrom");
447 continue;
448 }
449 sigemptyset(&sset);
450 sigaddset(&sset, SIGALRM);
451 sigaddset(&sset, SIGINT);
452 sigprocmask(SIG_BLOCK, &sset, &osset);
453 recv_pack(packet, cc, &from);
454 sigprocmask(SIG_SETMASK, &osset, NULL);
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000455 RELEASE_CONFIG_BUFFER(packet);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000456 }
457}