1 |
#if defined(__linux) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__CYGWIN__) |
2 |
# define ENABLE_IPV6 1 // if you get compilation problems try to disable IPv6 |
3 |
#else |
4 |
# define ENABLE_IPV6 0 |
5 |
#endif |
6 |
|
7 |
#include "EXTERN.h" |
8 |
#include "perl.h" |
9 |
#include "XSUB.h" |
10 |
|
11 |
#include <pthread.h> |
12 |
|
13 |
#include <stdio.h> |
14 |
#include <stdlib.h> |
15 |
#include <string.h> |
16 |
|
17 |
#include <time.h> |
18 |
#include <poll.h> |
19 |
#include <unistd.h> |
20 |
#include <inttypes.h> |
21 |
#include <fcntl.h> |
22 |
#include <errno.h> |
23 |
|
24 |
#include <sys/types.h> |
25 |
#include <sys/time.h> |
26 |
#include <sys/socket.h> |
27 |
|
28 |
#include <netinet/in.h> |
29 |
#include <arpa/inet.h> |
30 |
|
31 |
#ifdef __linux |
32 |
# include <linux/icmp.h> |
33 |
#endif |
34 |
#if ENABLE_IPV6 && !defined (__CYGWIN__) |
35 |
# include <netinet/icmp6.h> |
36 |
#endif |
37 |
|
38 |
#define ICMP4_ECHO 8 |
39 |
#define ICMP4_ECHO_REPLY 0 |
40 |
#define ICMP6_ECHO 128 |
41 |
#define ICMP6_ECHO_REPLY 129 |
42 |
|
43 |
#define DRAIN_INTERVAL 1e-6 // how long to wait when sendto returns ENOBUFS, in seconds |
44 |
#define MIN_INTERVAL 1e-6 // minimum packet send interval, in seconds |
45 |
|
46 |
#define HDR_SIZE_IP4 20 |
47 |
#define HDR_SIZE_IP6 48 |
48 |
|
49 |
static int thr_res[2]; // worker thread finished status |
50 |
static int icmp4_fd = -1; |
51 |
static int icmp6_fd = -1; |
52 |
|
53 |
/*****************************************************************************/ |
54 |
|
55 |
typedef double tstamp; |
56 |
|
57 |
static tstamp |
58 |
NOW (void) |
59 |
{ |
60 |
struct timeval tv; |
61 |
|
62 |
gettimeofday (&tv, 0); |
63 |
|
64 |
return tv.tv_sec + tv.tv_usec * 1e-6; |
65 |
} |
66 |
|
67 |
static void |
68 |
ssleep (tstamp wait) |
69 |
{ |
70 |
#if defined (__SVR4) && defined (__sun) |
71 |
struct timeval tv; |
72 |
|
73 |
tv.tv_sec = wait; |
74 |
tv.tv_usec = (wait - tv.tv_sec) * 1e6; |
75 |
|
76 |
select (0, 0, 0, 0, &tv); |
77 |
#elif defined(_WIN32) |
78 |
Sleep ((unsigned long)(delay * 1e3)); |
79 |
#else |
80 |
struct timespec ts; |
81 |
|
82 |
ts.tv_sec = wait; |
83 |
ts.tv_nsec = (wait - ts.tv_sec) * 1e9; |
84 |
|
85 |
nanosleep (&ts, 0); |
86 |
#endif |
87 |
} |
88 |
|
89 |
/*****************************************************************************/ |
90 |
|
91 |
typedef struct |
92 |
{ |
93 |
uint8_t version_ihl; |
94 |
uint8_t tos; |
95 |
uint16_t tot_len; |
96 |
|
97 |
uint16_t id; |
98 |
uint16_t flags; |
99 |
|
100 |
uint8_t ttl; |
101 |
uint8_t protocol; |
102 |
uint16_t cksum; |
103 |
|
104 |
uint32_t src; |
105 |
uint32_t dst; |
106 |
} IP4HDR; |
107 |
|
108 |
/*****************************************************************************/ |
109 |
|
110 |
typedef uint8_t addr_tt[16]; |
111 |
|
112 |
typedef struct |
113 |
{ |
114 |
tstamp next; |
115 |
tstamp interval; |
116 |
int addrlen; |
117 |
|
118 |
addr_tt lo, hi; /* only if !addrcnt */ |
119 |
|
120 |
int addrcnt; |
121 |
/* addrcnt addresses follow */ |
122 |
} RANGE; |
123 |
|
124 |
typedef struct |
125 |
{ |
126 |
RANGE **ranges; |
127 |
int rangecnt, rangemax; |
128 |
|
129 |
tstamp next; |
130 |
tstamp interval; |
131 |
|
132 |
tstamp maxrtt; |
133 |
|
134 |
uint16_t magic1; |
135 |
uint16_t magic2; |
136 |
uint16_t magic3; |
137 |
|
138 |
int id; |
139 |
|
140 |
AV *recvq; /* receive queue */ |
141 |
int nextrecv; |
142 |
SV *recvcb; |
143 |
|
144 |
pthread_t thrid; |
145 |
int running; |
146 |
} PINGER; |
147 |
|
148 |
static PINGER **pingers; |
149 |
static int *pingerfree; /* freelist next */ |
150 |
static int pingercnt; |
151 |
static int pingermax; |
152 |
static int firstfree = -1; |
153 |
static int firstrecv = -1; |
154 |
|
155 |
/*****************************************************************************/ |
156 |
|
157 |
typedef struct |
158 |
{ |
159 |
uint8_t type, code; |
160 |
uint16_t cksum; |
161 |
|
162 |
uint16_t id, seq; |
163 |
|
164 |
uint16_t pinger; |
165 |
uint16_t magic; |
166 |
|
167 |
uint32_t stamp_hi; |
168 |
uint32_t stamp_lo; |
169 |
} PKT; |
170 |
|
171 |
static int |
172 |
pkt_is_valid_for (PKT *pkt, PINGER *pinger) |
173 |
{ |
174 |
return pkt->id == pinger->magic1 |
175 |
&& pkt->seq == pinger->magic2 |
176 |
&& pkt->magic == pinger->magic3; |
177 |
} |
178 |
|
179 |
static void |
180 |
ts_to_pkt (PKT *pkt, tstamp ts) |
181 |
{ |
182 |
/* move 12 bits of seconds into the 32 bit fractional part */ |
183 |
/* leaving 20 bits subsecond resolution and 44 bits of integers */ |
184 |
/* (of which 32 are typically usable) */ |
185 |
ts *= 1. / 4096.; |
186 |
|
187 |
pkt->stamp_hi = ts; |
188 |
pkt->stamp_lo = (ts - pkt->stamp_hi) * 4294967296.; |
189 |
} |
190 |
|
191 |
static tstamp |
192 |
pkt_to_ts (PKT *pkt) |
193 |
{ |
194 |
return pkt->stamp_hi * 4096. |
195 |
+ pkt->stamp_lo * (4096. / 4294967296.); |
196 |
} |
197 |
|
198 |
static void |
199 |
pkt_cksum (PKT *pkt) |
200 |
{ |
201 |
uint_fast32_t sum = -pkt->cksum; |
202 |
uint32_t *wp = (uint32_t *)pkt; |
203 |
int len = sizeof (*pkt) / 4; |
204 |
|
205 |
do |
206 |
{ |
207 |
uint_fast32_t w = *(volatile uint32_t *)wp++; |
208 |
sum += (w & 0xffff) + (w >> 16); |
209 |
} |
210 |
while (len--); |
211 |
|
212 |
sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */ |
213 |
sum += sum >> 16; /* add carry */ |
214 |
|
215 |
pkt->cksum = ~sum; |
216 |
} |
217 |
|
218 |
/*****************************************************************************/ |
219 |
|
220 |
static void |
221 |
range_free (RANGE *self) |
222 |
{ |
223 |
free (self); |
224 |
} |
225 |
|
226 |
/* like sendto, but retries on failure */ |
227 |
static void |
228 |
xsendto (int fd, void *buf, size_t len, int flags, void *sa, int salen) |
229 |
{ |
230 |
tstamp wait = DRAIN_INTERVAL / 2.; |
231 |
|
232 |
while (sendto (fd, buf, len, flags, sa, salen) < 0 && errno == ENOBUFS) |
233 |
ssleep (wait *= 2.); |
234 |
} |
235 |
|
236 |
// ping current address, return true and increment if more to ping |
237 |
static int |
238 |
range_send_ping (RANGE *self, PKT *pkt) |
239 |
{ |
240 |
// send ping |
241 |
uint8_t *addr; |
242 |
|
243 |
if (self->addrcnt) |
244 |
addr = (self->addrcnt - 1) * self->addrlen + (uint8_t *)(self + 1); |
245 |
else |
246 |
addr = sizeof (addr_tt) - self->addrlen + self->lo; |
247 |
|
248 |
pkt->cksum = 0; |
249 |
|
250 |
if (self->addrlen == 4) |
251 |
{ |
252 |
struct sockaddr_in sa; |
253 |
|
254 |
pkt->type = ICMP4_ECHO; |
255 |
pkt_cksum (pkt); |
256 |
|
257 |
sa.sin_family = AF_INET; |
258 |
sa.sin_port = 0; |
259 |
|
260 |
memcpy (&sa.sin_addr, addr, sizeof (sa.sin_addr)); |
261 |
|
262 |
xsendto (icmp4_fd, pkt, sizeof (*pkt), 0, &sa, sizeof (sa)); |
263 |
} |
264 |
else |
265 |
{ |
266 |
#if ENABLE_IPV6 |
267 |
struct sockaddr_in6 sa; |
268 |
|
269 |
pkt->type = ICMP6_ECHO; |
270 |
|
271 |
sa.sin6_family = AF_INET6; |
272 |
sa.sin6_port = 0; |
273 |
sa.sin6_flowinfo = 0; |
274 |
sa.sin6_scope_id = 0; |
275 |
|
276 |
memcpy (&sa.sin6_addr, addr, sizeof (sa.sin6_addr)); |
277 |
|
278 |
xsendto (icmp6_fd, pkt, sizeof (*pkt), 0, &sa, sizeof (sa)); |
279 |
#endif |
280 |
} |
281 |
|
282 |
// see if we have any more addresses |
283 |
if (self->addrcnt) |
284 |
{ |
285 |
if (!--self->addrcnt) |
286 |
return 0; |
287 |
} |
288 |
else |
289 |
{ |
290 |
if (!memcmp (&self->lo, &self->hi, sizeof (addr_tt))) |
291 |
return 0; |
292 |
|
293 |
// increment self->lo |
294 |
{ |
295 |
int len = sizeof (addr_tt) - 1; |
296 |
|
297 |
while (!++self->lo [len]) |
298 |
--len; |
299 |
} |
300 |
} |
301 |
|
302 |
return 1; |
303 |
} |
304 |
|
305 |
/*****************************************************************************/ |
306 |
|
307 |
static void |
308 |
downheap (PINGER *self) |
309 |
{ |
310 |
RANGE *elem = self->ranges [0]; /* always exists */ |
311 |
int Nm1 = self->rangecnt - 1; |
312 |
int j; |
313 |
int k; |
314 |
|
315 |
for (k = 0; ; ) |
316 |
{ |
317 |
int j = k * 2 + 1; |
318 |
|
319 |
if (j > Nm1) |
320 |
break; |
321 |
|
322 |
if (j < Nm1 |
323 |
&& self->ranges [j]->next > self->ranges [j + 1]->next) |
324 |
++j; |
325 |
|
326 |
if (self->ranges [j]->next >= elem->next) |
327 |
break; |
328 |
|
329 |
self->ranges [k] = self->ranges [j]; |
330 |
|
331 |
k = j; |
332 |
} |
333 |
|
334 |
self->ranges [k] = elem; |
335 |
} |
336 |
|
337 |
static void |
338 |
upheap (PINGER *self, int k) |
339 |
{ |
340 |
RANGE *elem = self->ranges [k]; |
341 |
|
342 |
while (k) |
343 |
{ |
344 |
int j = (k - 1) >> 1; |
345 |
|
346 |
if (self->ranges [j]->next <= elem->next) |
347 |
break; |
348 |
|
349 |
self->ranges [k] = self->ranges [j]; |
350 |
|
351 |
k = j; |
352 |
} |
353 |
|
354 |
self->ranges [k] = elem; |
355 |
} |
356 |
|
357 |
static void * |
358 |
ping_proc (void *self_) |
359 |
{ |
360 |
PINGER *self = (PINGER *)self_; |
361 |
PKT pkt; |
362 |
|
363 |
memset (&pkt, 0, sizeof (pkt)); |
364 |
|
365 |
tstamp now = NOW (); |
366 |
|
367 |
pkt.code = 0; |
368 |
pkt.id = self->magic1; |
369 |
pkt.seq = self->magic2; |
370 |
pkt.magic = self->magic3; |
371 |
pkt.pinger = self->id; |
372 |
|
373 |
if (self->next < now) |
374 |
self->next = now; |
375 |
|
376 |
while (self->rangecnt) |
377 |
{ |
378 |
RANGE *range = self->ranges [0]; |
379 |
|
380 |
// ranges [0] is always the next range to ping |
381 |
tstamp wait = range->next - now; |
382 |
|
383 |
// compare with the global frequency limit |
384 |
{ |
385 |
tstamp diff = self->next - now; |
386 |
|
387 |
if (wait < diff) |
388 |
wait = diff; // global rate limit overrides |
389 |
else |
390 |
self->next = range->next; // fast forward |
391 |
} |
392 |
|
393 |
if (wait > 0.) |
394 |
ssleep (wait); |
395 |
|
396 |
now = NOW (); |
397 |
|
398 |
ts_to_pkt (&pkt, now); |
399 |
|
400 |
if (!range_send_ping (range, &pkt)) |
401 |
{ |
402 |
self->ranges [0] = self->ranges [--self->rangecnt]; |
403 |
range_free (range); |
404 |
} |
405 |
else |
406 |
range->next = self->next + range->interval; |
407 |
|
408 |
downheap (self); |
409 |
|
410 |
self->next += self->interval; |
411 |
now = NOW (); |
412 |
} |
413 |
|
414 |
ssleep (self->maxrtt); |
415 |
|
416 |
{ |
417 |
uint16_t id = self->id; |
418 |
|
419 |
write (thr_res [1], &id, sizeof (id)); |
420 |
} |
421 |
|
422 |
return 0; |
423 |
} |
424 |
|
425 |
/*****************************************************************************/ |
426 |
|
427 |
static void |
428 |
pinger_start (PINGER *self) |
429 |
{ |
430 |
sigset_t fullsigset, oldsigset; |
431 |
pthread_attr_t attr; |
432 |
|
433 |
if (self->running) |
434 |
return; |
435 |
|
436 |
sigfillset (&fullsigset); |
437 |
|
438 |
pthread_attr_init (&attr); |
439 |
pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 2048 ? sizeof (long) * 2048 : PTHREAD_STACK_MIN); |
440 |
|
441 |
pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
442 |
|
443 |
if (pthread_create (&self->thrid, &attr, ping_proc, (void *)self)) |
444 |
croak ("AnyEvent::FastPing: unable to create pinger thread"); |
445 |
|
446 |
pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
447 |
|
448 |
self->running = 1; |
449 |
} |
450 |
|
451 |
static void |
452 |
pinger_stop (PINGER *self) |
453 |
{ |
454 |
if (!self->running) |
455 |
return; |
456 |
|
457 |
self->running = 0; |
458 |
pthread_cancel (self->thrid); |
459 |
pthread_join (self->thrid, 0); |
460 |
} |
461 |
|
462 |
static void |
463 |
pinger_init (PINGER *self) |
464 |
{ |
465 |
memset (self, 0, sizeof (PINGER)); |
466 |
|
467 |
if (firstfree >= 0) |
468 |
{ |
469 |
self->id = firstfree; |
470 |
firstfree = pingerfree [firstfree]; |
471 |
} |
472 |
else if (pingercnt == 0xffff) |
473 |
croak ("unable to create more than 65536 AnyEvent::FastPing objects"); |
474 |
else |
475 |
{ |
476 |
if (pingercnt == pingermax) |
477 |
{ |
478 |
pingermax = pingermax * 2 + 16; |
479 |
pingers = realloc (pingers , sizeof (pingers [0]) * pingermax); |
480 |
pingerfree = realloc (pingerfree, sizeof (pingerfree [0]) * pingermax); |
481 |
} |
482 |
|
483 |
self->id = pingercnt++; |
484 |
} |
485 |
|
486 |
pingers [self->id] = self; |
487 |
|
488 |
self->recvcb = &PL_sv_undef; |
489 |
self->next = 0.; |
490 |
self->interval = MIN_INTERVAL; |
491 |
self->maxrtt = 0.5; |
492 |
self->rangemax = 16; |
493 |
self->ranges = malloc (sizeof (self->ranges [0]) * self->rangemax); |
494 |
} |
495 |
|
496 |
static void |
497 |
pinger_free (PINGER *self) |
498 |
{ |
499 |
pinger_stop (self); |
500 |
|
501 |
pingers [self->id] = 0; |
502 |
|
503 |
SvREFCNT_dec (self->recvq); |
504 |
SvREFCNT_dec (self->recvcb); |
505 |
|
506 |
pingerfree [self->id] = firstfree; |
507 |
firstfree = self->id; |
508 |
|
509 |
while (self->rangecnt) |
510 |
range_free (self->ranges [--self->rangecnt]); |
511 |
|
512 |
free (self->ranges); |
513 |
} |
514 |
|
515 |
static void |
516 |
pinger_add_range (PINGER *self, RANGE *range) |
517 |
{ |
518 |
if (self->rangecnt == self->rangemax) |
519 |
self->ranges = realloc (self->ranges, sizeof (self->ranges [0]) * (self->rangemax <<= 1)); |
520 |
|
521 |
self->ranges [self->rangecnt] = range; |
522 |
upheap (self, self->rangecnt); |
523 |
++self->rangecnt; |
524 |
} |
525 |
|
526 |
/*****************************************************************************/ |
527 |
|
528 |
static void |
529 |
recv_feed (PINGER *self, void *addr, int addrlen, tstamp rtt) |
530 |
{ |
531 |
if (!self->recvq) |
532 |
{ |
533 |
/* first seen this round */ |
534 |
if (!SvOK (self->recvcb)) |
535 |
return; |
536 |
|
537 |
self->recvq = newAV (); |
538 |
|
539 |
self->nextrecv = firstrecv; |
540 |
firstrecv = self->id; |
541 |
} |
542 |
|
543 |
{ |
544 |
AV *pkt = newAV (); |
545 |
|
546 |
av_extend (pkt, 2-1); |
547 |
|
548 |
AvARRAY (pkt)[0] = newSVpvn (addr, addrlen); |
549 |
AvARRAY (pkt)[1] = newSVnv (rtt); |
550 |
AvFILLp (pkt) = 2-1; |
551 |
|
552 |
av_push (self->recvq, newRV_noinc ((SV *)pkt)); |
553 |
} |
554 |
} |
555 |
|
556 |
static void |
557 |
recv_flush (void) |
558 |
{ |
559 |
if (firstrecv < 0) |
560 |
return; |
561 |
|
562 |
ENTER; |
563 |
SAVETMPS; |
564 |
|
565 |
do |
566 |
{ |
567 |
dSP; |
568 |
PINGER *self = pingers [firstrecv]; |
569 |
firstrecv = self->nextrecv; |
570 |
|
571 |
self->nextrecv = -1; |
572 |
|
573 |
PUSHMARK (SP); |
574 |
XPUSHs (sv_2mortal (newRV_noinc ((SV *)self->recvq))); |
575 |
self->recvq = 0; |
576 |
PUTBACK; |
577 |
call_sv (self->recvcb, G_DISCARD | G_VOID); |
578 |
} |
579 |
while (firstrecv >= 0); |
580 |
|
581 |
FREETMPS; |
582 |
LEAVE; |
583 |
} |
584 |
|
585 |
/*****************************************************************************/ |
586 |
|
587 |
#if 0 |
588 |
static void |
589 |
feed_reply (AV *res_av) |
590 |
{ |
591 |
dSP; |
592 |
SV *res = sv_2mortal (newRV_inc ((SV *)res_av)); |
593 |
int i; |
594 |
|
595 |
if (av_len (res_av) < 0) |
596 |
return; |
597 |
|
598 |
ENTER; |
599 |
SAVETMPS; |
600 |
|
601 |
for (i = av_len (cbs) + 1; i--; ) |
602 |
{ |
603 |
SV *cb = *av_fetch (cbs, i, 1); |
604 |
|
605 |
PUSHMARK (SP); |
606 |
XPUSHs (res); |
607 |
PUTBACK; |
608 |
call_sv (cb, G_DISCARD | G_VOID); |
609 |
} |
610 |
|
611 |
FREETMPS; |
612 |
LEAVE; |
613 |
} |
614 |
#endif |
615 |
|
616 |
static void |
617 |
boot () |
618 |
{ |
619 |
if (pipe (thr_res) < 0) |
620 |
croak ("AnyEvent::FastPing: unable to create receive pipe"); |
621 |
|
622 |
sv_setiv (get_sv ("AnyEvent::FastPing::THR_RES_FD", 1), thr_res [0]); |
623 |
|
624 |
icmp4_fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); |
625 |
fcntl (icmp4_fd, F_SETFL, O_NONBLOCK); |
626 |
#ifdef ICMP_FILTER |
627 |
{ |
628 |
struct icmp_filter oval; |
629 |
oval.data = 0xffffffff & ~(1 << ICMP4_ECHO_REPLY); |
630 |
setsockopt (icmp4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
631 |
} |
632 |
#endif |
633 |
|
634 |
#if ENABLE_IPV6 |
635 |
icmp6_fd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
636 |
fcntl (icmp6_fd, F_SETFL, O_NONBLOCK); |
637 |
# ifdef ICMP6_FILTER |
638 |
{ |
639 |
struct icmp6_filter oval; |
640 |
ICMP6_FILTER_SETBLOCKALL (&oval); |
641 |
ICMP6_FILTER_SETPASS (ICMP6_ECHO_REPLY, &oval); |
642 |
setsockopt (icmp6_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &oval, sizeof oval); |
643 |
} |
644 |
# endif |
645 |
#endif |
646 |
|
647 |
sv_setiv (get_sv ("AnyEvent::FastPing::ICMP4_FD", 1), icmp4_fd); |
648 |
sv_setiv (get_sv ("AnyEvent::FastPing::ICMP6_FD", 1), icmp6_fd); |
649 |
} |
650 |
|
651 |
#define NOT_RUNNING \ |
652 |
if (self->running) \ |
653 |
croak ("AnyEvent::FastPing object has been started - you have to sotp t first before calling this method, caught"); |
654 |
|
655 |
MODULE = AnyEvent::FastPing PACKAGE = AnyEvent::FastPing PREFIX = pinger_ |
656 |
|
657 |
BOOT: |
658 |
{ |
659 |
HV *stash = gv_stashpv ("AnyEvent::FastPing", 1); |
660 |
|
661 |
if (sizeof (PKT) & 3) |
662 |
croak ("size of PKT structure is not a multiple of 4"); |
663 |
|
664 |
boot (); |
665 |
|
666 |
newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0)); |
667 |
newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0)); |
668 |
|
669 |
newCONSTSUB (stash, "icmp4_pktsize", newSViv (HDR_SIZE_IP4 + sizeof (PKT))); |
670 |
newCONSTSUB (stash, "icmp6_pktsize", newSViv (HDR_SIZE_IP6 + sizeof (PKT))); |
671 |
} |
672 |
|
673 |
PROTOTYPES: DISABLE |
674 |
|
675 |
void |
676 |
_recv_icmp4 (...) |
677 |
CODE: |
678 |
{ |
679 |
char buf [512]; |
680 |
struct sockaddr_in sa; |
681 |
int maxrecv; |
682 |
|
683 |
for (maxrecv = 256+1; --maxrecv; ) |
684 |
{ |
685 |
PINGER *pinger; |
686 |
IP4HDR *iphdr = (IP4HDR *)buf; |
687 |
socklen_t sl = sizeof (sa); |
688 |
int len = recvfrom (icmp4_fd, buf, sizeof (buf), MSG_TRUNC, (struct sockaddr *)&sa, &sl); |
689 |
int hdrlen, totlen; |
690 |
PKT *pkt; |
691 |
|
692 |
if (len <= HDR_SIZE_IP4) |
693 |
break; |
694 |
|
695 |
hdrlen = (iphdr->version_ihl & 15) * 4; |
696 |
totlen = ntohs (iphdr->tot_len); |
697 |
|
698 |
if (totlen > len |
699 |
|| iphdr->protocol != IPPROTO_ICMP |
700 |
|| hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen) |
701 |
continue; |
702 |
|
703 |
pkt = (PKT *)(buf + hdrlen); |
704 |
|
705 |
if (pkt->type != ICMP4_ECHO_REPLY |
706 |
|| pkt->pinger >= pingercnt |
707 |
|| !pingers [pkt->pinger]) |
708 |
continue; |
709 |
|
710 |
pinger = pingers [pkt->pinger]; |
711 |
|
712 |
if (!pkt_is_valid_for (pkt, pinger)) |
713 |
continue; |
714 |
|
715 |
recv_feed (pinger, &sa.sin_addr, 4, NOW () - pkt_to_ts (pkt)); |
716 |
} |
717 |
|
718 |
recv_flush (); |
719 |
} |
720 |
|
721 |
void |
722 |
_recv_icmp6 (...) |
723 |
CODE: |
724 |
{ |
725 |
struct sockaddr_in6 sa; |
726 |
PKT pkt; |
727 |
int maxrecv; |
728 |
|
729 |
for (maxrecv = 256+1; --maxrecv; ) |
730 |
{ |
731 |
PINGER *pinger; |
732 |
socklen_t sl = sizeof (sa); |
733 |
int len = recvfrom (icmp6_fd, &pkt, sizeof (pkt), MSG_TRUNC, (struct sockaddr *)&sa, &sl); |
734 |
|
735 |
if (len != sizeof (PKT)) |
736 |
break; |
737 |
|
738 |
if (pkt.type != ICMP6_ECHO_REPLY |
739 |
|| pkt.pinger >= pingercnt |
740 |
|| !pingers [pkt.pinger]) |
741 |
continue; |
742 |
|
743 |
pinger = pingers [pkt.pinger]; |
744 |
|
745 |
if (!pkt_is_valid_for (&pkt, pinger)) |
746 |
continue; |
747 |
|
748 |
recv_feed (pinger, &sa.sin6_addr, 16, NOW () - pkt_to_ts (&pkt)); |
749 |
} |
750 |
|
751 |
recv_flush (); |
752 |
} |
753 |
|
754 |
void |
755 |
_new (SV *klass, UV magic1, UV magic2, UV magic3) |
756 |
PPCODE: |
757 |
{ |
758 |
SV *pv = NEWSV (0, sizeof (PINGER)); |
759 |
PINGER *self = (PINGER *)SvPVX (pv); |
760 |
|
761 |
SvPOK_only (pv); |
762 |
XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), gv_stashpv (SvPVutf8_nolen (klass), 1)))); |
763 |
pinger_init (self); |
764 |
self->magic1 = magic1; |
765 |
self->magic2 = magic2; |
766 |
self->magic3 = magic3; |
767 |
} |
768 |
|
769 |
void |
770 |
_free (PINGER *self) |
771 |
CODE: |
772 |
pinger_free (self); |
773 |
|
774 |
IV |
775 |
id (PINGER *self, ...) |
776 |
CODE: |
777 |
RETVAL = self->id; |
778 |
OUTPUT: |
779 |
RETVAL |
780 |
|
781 |
void pinger_start (PINGER *self) |
782 |
|
783 |
void pinger_stop (PINGER *self) |
784 |
|
785 |
void |
786 |
_stop_id (UV id) |
787 |
CODE: |
788 |
if (id < pingercnt && pingers [id]) |
789 |
pinger_stop (pingers [id]); |
790 |
|
791 |
void |
792 |
interval (PINGER *self, NV interval) |
793 |
CODE: |
794 |
NOT_RUNNING; |
795 |
self->interval = interval > MIN_INTERVAL ? interval : MIN_INTERVAL; |
796 |
|
797 |
void |
798 |
max_rtt (PINGER *self, NV maxrtt) |
799 |
CODE: |
800 |
NOT_RUNNING; |
801 |
self->maxrtt = maxrtt; |
802 |
|
803 |
void |
804 |
on_recv (PINGER *self, SV *cb) |
805 |
CODE: |
806 |
SvREFCNT_dec (self->recvcb); |
807 |
self->recvcb = newSVsv (cb); |
808 |
|
809 |
void |
810 |
add_range (PINGER *self, SV *lo_, SV *hi_, NV interval = 0) |
811 |
CODE: |
812 |
{ |
813 |
STRLEN lo_len, hi_len; |
814 |
char *lo = SvPVbyte (lo_, lo_len); |
815 |
char *hi = SvPVbyte (hi_, hi_len); |
816 |
RANGE *range; |
817 |
NOT_RUNNING; |
818 |
|
819 |
if (lo_len != hi_len || (lo_len != 4 && lo_len != 16)) |
820 |
croak ("AnyEvent::FastPing::add_range address range must be specified as two binary IPv4 or IPv6 addresses"); |
821 |
|
822 |
if (lo_len == 4 && icmp4_fd < 0) croak ("IPv4 support unavailable"); |
823 |
if (lo_len == 16 && icmp6_fd < 0) croak ("IPv6 support unavailable"); |
824 |
|
825 |
if (memcmp (lo, hi, lo_len) > 0) |
826 |
croak ("AnyEvent::FastPing::add_range called with lo > hi"); |
827 |
|
828 |
range = calloc (1, sizeof (RANGE)); |
829 |
|
830 |
range->next = 0; |
831 |
range->interval = interval > MIN_INTERVAL ? interval : MIN_INTERVAL; |
832 |
range->addrlen = lo_len; |
833 |
|
834 |
memcpy (sizeof (addr_tt) - lo_len + (char *)&range->lo, lo, lo_len); |
835 |
memcpy (sizeof (addr_tt) - lo_len + (char *)&range->hi, hi, lo_len); |
836 |
|
837 |
pinger_add_range (self, range); |
838 |
} |
839 |
|
840 |
void |
841 |
add_hosts (PINGER *self, SV *addrs, NV interval = 0, UV interleave = 1) |
842 |
CODE: |
843 |
{ |
844 |
AV *av; |
845 |
int i, j, k; |
846 |
int cnt; |
847 |
int addrlen; |
848 |
RANGE *range; |
849 |
NOT_RUNNING; |
850 |
|
851 |
if (!SvROK (addrs) || SvTYPE (SvRV (addrs)) != SVt_PVAV) |
852 |
croak ("AnyEvent::FastPing::add_hosts expects an arrayref with binary IPv4 or IPv6 addresses"); |
853 |
|
854 |
av = (AV *)SvRV (addrs); |
855 |
cnt = av_len (av) + 1; |
856 |
|
857 |
if (!cnt) |
858 |
XSRETURN_EMPTY; |
859 |
|
860 |
addrlen = SvCUR (*av_fetch (av, 0, 1)); |
861 |
|
862 |
if (addrlen != 4 && addrlen != 16) |
863 |
croak ("AnyEvent::FastPing::add_hosts addresses must be specified as binary IPv4 or IPv6 addresses"); |
864 |
|
865 |
for (i = cnt; --i; ) |
866 |
{ |
867 |
SV *sv = *av_fetch (av, i, 1); |
868 |
|
869 |
if (!sv_utf8_downgrade (sv, 1) || addrlen != SvCUR (sv)) |
870 |
croak ("AnyEvent::FastPing::add_hosts addresses must all have the same size"); |
871 |
} |
872 |
|
873 |
range = calloc (1, sizeof (RANGE) + cnt * addrlen); |
874 |
|
875 |
range->next = 0; |
876 |
range->interval = interval > MIN_INTERVAL ? interval : MIN_INTERVAL; |
877 |
range->addrlen = addrlen; |
878 |
range->addrcnt = cnt; |
879 |
|
880 |
if (interleave == 0) |
881 |
interleave = cnt <= 256 * 256 ? 256 : (int)sqrtf (cnt); |
882 |
|
883 |
k = cnt; |
884 |
for (j = 0; j < interleave; ++j) |
885 |
for (i = j; i < cnt; i += interleave) |
886 |
memcpy ((uint8_t *)(range + 1) + --k * addrlen, |
887 |
SvPVbyte_nolen (*av_fetch (av, i, 1)), |
888 |
addrlen); |
889 |
|
890 |
pinger_add_range (self, range); |
891 |
} |
892 |
|