blob: 725b0e4cbb4532f6e46b66319ecd6be9f61eed40 [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
Glenn L McGrath9e598412003-01-09 10:06:01 +000014#include <arpa/inet.h>
15#include <net/if.h>
16#include <netinet/ether.h>
17#include <netpacket/packet.h>
18
19#include "busybox.h"
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;
25static struct timeval last;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000026
27enum cfg_e {
28 dad = 1,
29 unsolicited = 2,
30 advert = 4,
31 quiet = 8,
32 quit_on_reply = 16,
Bernhard Reutner-Fischer61536292006-04-03 09:46:29 +000033 broadcast_only = 32,
34 unicasting = 64
Rob Landley8ea52052006-03-30 21:50:57 +000035};
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +000036static int cfg;
Rob Landley8ea52052006-03-30 21:50:57 +000037
38static int s;
Denis Vlasenko13858992006-10-08 12:49:22 +000039static unsigned count = UINT_MAX;
40static unsigned timeout;
Glenn L McGrath6b0658f2003-09-26 00:33:18 +000041static int sent;
42static int brd_sent;
43static int received;
44static int brd_recv;
45static int req_recv;
Glenn L McGrath9e598412003-01-09 10:06:01 +000046
47#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000048 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
49
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +000050static int send_pack(int sock, struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000051 struct in_addr *dst_addr, struct sockaddr_ll *ME,
52 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000053{
54 int err;
55 struct timeval now;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +000056 RESERVE_CONFIG_UBUFFER(buf, 256);
Glenn L McGrath9e598412003-01-09 10:06:01 +000057 struct arphdr *ah = (struct arphdr *) buf;
58 unsigned char *p = (unsigned char *) (ah + 1);
59
60 ah->ar_hrd = htons(ME->sll_hatype);
61 ah->ar_hrd = htons(ARPHRD_ETHER);
62 ah->ar_pro = htons(ETH_P_IP);
63 ah->ar_hln = ME->sll_halen;
64 ah->ar_pln = 4;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000065 ah->ar_op = cfg & advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000066
67 memcpy(p, &ME->sll_addr, ah->ar_hln);
68 p += ME->sll_halen;
69
70 memcpy(p, src_addr, 4);
71 p += 4;
72
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000073 if (cfg & advert)
Glenn L McGrath9e598412003-01-09 10:06:01 +000074 memcpy(p, &ME->sll_addr, ah->ar_hln);
75 else
76 memcpy(p, &HE->sll_addr, ah->ar_hln);
77 p += ah->ar_hln;
78
79 memcpy(p, dst_addr, 4);
80 p += 4;
81
82 gettimeofday(&now, NULL);
83 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
84 if (err == p - buf) {
85 last = now;
86 sent++;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000087 if (!(cfg & unicasting))
Glenn L McGrath9e598412003-01-09 10:06:01 +000088 brd_sent++;
89 }
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +000090 RELEASE_CONFIG_BUFFER(buf);
Glenn L McGrath9e598412003-01-09 10:06:01 +000091 return err;
92}
93
Eric Andersen14f5c8d2005-04-16 19:39:00 +000094static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +000095{
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000096 if (!(cfg & quiet)) {
97 printf("Sent %d probe(s) (%d broadcast(s))\n"
98 "Received %d repl%s"
99 " (%d request(s), %d broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000100 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000101 received, (received == 1) ? "ies" : "y",
102 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000103 }
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000104 if (cfg & dad)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000105 exit(!!received);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000106 if (cfg & unsolicited)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000107 exit(0);
108 exit(!received);
109}
110
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000111static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000112{
113 struct timeval tv;
Glenn L McGrath8b96b712003-08-28 19:54:16 +0000114 static struct timeval start;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000115
116 gettimeofday(&tv, NULL);
117
118 if (start.tv_sec == 0)
119 start = tv;
120
121 if (count-- == 0
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000122 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000123 finish();
124
125 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
126 send_pack(s, &src, &dst, &me, &he);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000127 if (count == 0 && (cfg & unsolicited))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000128 finish();
129 }
130 alarm(1);
131}
132
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000133static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000134{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000135 struct arphdr *ah = (struct arphdr *) buf;
136 unsigned char *p = (unsigned char *) (ah + 1);
137 struct in_addr src_ip, dst_ip;
138
Glenn L McGrath9e598412003-01-09 10:06:01 +0000139 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000140 if (FROM->sll_pkttype != PACKET_HOST
141 && FROM->sll_pkttype != PACKET_BROADCAST
142 && FROM->sll_pkttype != PACKET_MULTICAST)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000143 return 0;
144
145 /* Only these types are recognised */
146 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
147 return 0;
148
149 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000150 if (ah->ar_hrd != htons(FROM->sll_hatype)
151 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000152 return 0;
153
154 /* Protocol must be IP. */
155 if (ah->ar_pro != htons(ETH_P_IP))
156 return 0;
157 if (ah->ar_pln != 4)
158 return 0;
159 if (ah->ar_hln != me.sll_halen)
160 return 0;
161 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
162 return 0;
163 memcpy(&src_ip, p + ah->ar_hln, 4);
164 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000165 if (!(cfg & dad)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000166 if (src_ip.s_addr != dst.s_addr)
167 return 0;
168 if (src.s_addr != dst_ip.s_addr)
169 return 0;
170 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
171 return 0;
172 } else {
173 /* DAD packet was:
174 src_ip = 0 (or some src)
175 src_hw = ME
176 dst_ip = tested address
177 dst_hw = <unspec>
178
179 We fail, if receive request/reply with:
180 src_ip = tested_address
181 src_hw != ME
182 if src_ip in request was not zero, check
183 also that it matches to dst_ip, otherwise
184 dst_ip/dst_hw do not matter.
185 */
186 if (src_ip.s_addr != dst.s_addr)
187 return 0;
188 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
189 return 0;
190 if (src.s_addr && src.s_addr != dst_ip.s_addr)
191 return 0;
192 }
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000193 if (!(cfg & quiet)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000194 int s_printed = 0;
Rob Landley8ea52052006-03-30 21:50:57 +0000195 struct timeval tv;
196
197 gettimeofday(&tv, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000198
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000199 printf("%scast re%s from %s [%s]",
200 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
201 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000202 inet_ntoa(src_ip),
203 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000204 if (dst_ip.s_addr != src.s_addr) {
205 printf("for %s ", inet_ntoa(dst_ip));
206 s_printed = 1;
207 }
208 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
209 if (!s_printed)
210 printf("for ");
211 printf("[%s]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000212 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000213 }
Rob Landley8ea52052006-03-30 21:50:57 +0000214
Glenn L McGrath9e598412003-01-09 10:06:01 +0000215 if (last.tv_sec) {
216 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
217 tv.tv_usec - last.tv_usec;
218 long msecs = (usecs + 500) / 1000;
219
220 usecs -= msecs * 1000 - 500;
221 printf(" %ld.%03ldms\n", msecs, usecs);
222 } else {
223 printf(" UNSOLICITED?\n");
224 }
225 fflush(stdout);
226 }
227 received++;
228 if (FROM->sll_pkttype != PACKET_HOST)
229 brd_recv++;
230 if (ah->ar_op == htons(ARPOP_REQUEST))
231 req_recv++;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000232 if (cfg & quit_on_reply)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000233 finish();
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000234 if (!(cfg & broadcast_only)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000235 memcpy(he.sll_addr, p, me.sll_halen);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000236 cfg |= unicasting;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000237 }
238 return 1;
239}
240
241int arping_main(int argc, char **argv)
242{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000243 char *device = "eth0";
Rob Landley8ea52052006-03-30 21:50:57 +0000244 int ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000245 char *source = NULL;
246 char *target;
247
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000248 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000249
Rob Landleyafb94ec2006-07-16 08:06:34 +0000250 // Drop suid root privileges
251 xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000252
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000253 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000254 unsigned opt;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000255 char *_count, *_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000256
257 /* Dad also sets quit_on_reply.
258 * Advert also sets unsolicited.
259 */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000260 opt_complementary = "Df:AU";
261 opt = getopt32(argc, argv, "DUAqfbc:w:i:s:",
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000262 &_count, &_timeout, &device, &source);
263 cfg |= opt & 0x3f; /* set respective flags */
264 if (opt & 0x40) /* -c: count */
Denis Vlasenko13858992006-10-08 12:49:22 +0000265 count = xatou(_count);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000266 if (opt & 0x80) /* -w: timeout */
Denis Vlasenko13858992006-10-08 12:49:22 +0000267 timeout = xatoul_range(_timeout, 0, INT_MAX/2000);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000268 //if (opt & 0x100) /* -i: interface */
269 if (strlen(device) > IF_NAMESIZE) {
270 bb_error_msg_and_die("interface name '%s' is too long",
271 device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000272 }
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000273 //if (opt & 0x200) /* -s: source */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000274 }
275 argc -= optind;
276 argv += optind;
277
278 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 bb_show_usage();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000280
281 target = *argv;
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000282
Denis Vlasenko40920822006-10-03 20:28:06 +0000283 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000284
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000285 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000286 struct ifreq ifr;
287
288 memset(&ifr, 0, sizeof(ifr));
289 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
290 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000291 bb_error_msg_and_die("interface %s not found", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000292 }
293 ifindex = ifr.ifr_ifindex;
294
295 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000296 bb_error_msg_and_die("SIOCGIFFLAGS");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000297 }
298 if (!(ifr.ifr_flags & IFF_UP)) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000299 bb_error_msg_and_die("interface %s is down", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000300 }
301 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000302 bb_error_msg("interface %s is not ARPable", device);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000303 return (cfg & dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000304 }
305 }
306
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000307 if (!inet_aton(target, &dst)) {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000308 len_and_sockaddr *lsa;
309 lsa = host_and_af2sockaddr(target, 0, AF_INET);
310 memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4);
311 if (ENABLE_FEATURE_CLEAN_UP)
312 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000313 }
314
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000315 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000316 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000317 }
318
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000319 if (!(cfg & dad) && (cfg & unsolicited) && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000320 src = dst;
321
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000322 if (!(cfg & dad) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000323 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000324 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000325
Glenn L McGrath9e598412003-01-09 10:06:01 +0000326 if (device) {
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000327 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000328 bb_error_msg("warning: interface %s is ignored", device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000329 }
330 memset(&saddr, 0, sizeof(saddr));
331 saddr.sin_family = AF_INET;
332 if (src.s_addr) {
333 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000334 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
335 } else if (!(cfg & dad)) {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000336 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000337
338 saddr.sin_port = htons(1025);
339 saddr.sin_addr = dst;
340
Denis Vlasenko703e2022007-01-22 14:12:08 +0000341 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000342 bb_perror_msg("warning: setsockopt(SO_DONTROUTE)");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000343 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
344 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000345 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000346 }
347 src = saddr.sin_addr;
348 }
349 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000350 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000351
352 me.sll_family = AF_PACKET;
353 me.sll_ifindex = ifindex;
354 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000355 xbind(s, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000356
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000357 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000358 socklen_t alen = sizeof(me);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000359
360 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
Rob Landley8ea52052006-03-30 21:50:57 +0000361 bb_error_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000362 }
363 }
364 if (me.sll_halen == 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000365 bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000366 return (cfg & dad ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000367 }
368 he = me;
369 memset(he.sll_addr, -1, he.sll_halen);
370
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000371 if (!(cfg & quiet)) {
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000372 printf("ARPING to %s from %s via %s\n",
373 inet_ntoa(dst), inet_ntoa(src),
374 device ? device : "unknown");
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000375 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000376
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000377 if (!src.s_addr && !(cfg & dad)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000378 bb_error_msg_and_die("no src address in the non-DAD mode");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000379 }
380
381 {
382 struct sigaction sa;
Glenn L McGrathe6ae6e32003-01-19 13:31:41 +0000383
Glenn L McGrath9e598412003-01-09 10:06:01 +0000384 memset(&sa, 0, sizeof(sa));
385 sa.sa_flags = SA_RESTART;
386
387 sa.sa_handler = (void (*)(int)) finish;
388 sigaction(SIGINT, &sa, NULL);
389
390 sa.sa_handler = (void (*)(int)) catcher;
391 sigaction(SIGALRM, &sa, NULL);
392 }
393
394 catcher();
395
396 while (1) {
397 sigset_t sset, osset;
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000398 RESERVE_CONFIG_UBUFFER(packet, 4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000399 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000400 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000401 int cc;
402
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000403 cc = recvfrom(s, packet, 4096, 0, (struct sockaddr *) &from, &alen);
404 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000405 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000406 continue;
407 }
408 sigemptyset(&sset);
409 sigaddset(&sset, SIGALRM);
410 sigaddset(&sset, SIGINT);
411 sigprocmask(SIG_BLOCK, &sset, &osset);
412 recv_pack(packet, cc, &from);
413 sigprocmask(SIG_SETMASK, &osset, NULL);
Bernhard Reutner-Fischer2766eed2006-03-31 18:13:42 +0000414 RELEASE_CONFIG_BUFFER(packet);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000415 }
416}