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