ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FastPing/FastPing.xs
Revision: 1.12
Committed: Wed Feb 2 19:26:45 2011 UTC (13 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-2_0
Changes since 1.11: +49 -32 lines
Log Message:
docs

File Contents

# User Rev Content
1 root 1.8 #if defined(__linux) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
2 root 1.5 # define ENABLE_IPV6 1 // if you get compilation problems try to disable IPv6
3 root 1.2 #else
4 root 1.5 # define ENABLE_IPV6 0
5 root 1.2 #endif
6 root 1.1
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 root 1.8 #if ENABLE_IPV6 && !defined (__CYGWIN__)
35 root 1.1 # 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 root 1.9 #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 root 1.1
46     #define HDR_SIZE_IP4 20
47     #define HDR_SIZE_IP6 48
48    
49 root 1.11 static int thr_res[2]; // worker thread finished status
50     static int icmp4_fd = -1;
51     static int icmp6_fd = -1;
52 root 1.10
53 root 1.9 /*****************************************************************************/
54    
55 root 1.1 typedef double tstamp;
56    
57 root 1.5 static tstamp
58     NOW (void)
59 root 1.1 {
60     struct timeval tv;
61 root 1.9
62 root 1.1 gettimeofday (&tv, 0);
63 root 1.9
64     return tv.tv_sec + tv.tv_usec * 1e-6;
65 root 1.1 }
66    
67 root 1.9 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 root 1.8 typedef struct
92     {
93 root 1.1 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 root 1.11 /*****************************************************************************/
109 root 1.1
110 root 1.11 typedef uint8_t addr_tt[16];
111 root 1.9
112 root 1.10 typedef struct
113     {
114     tstamp next;
115 root 1.12 tstamp interval;
116 root 1.10 int addrlen;
117 root 1.11
118     addr_tt lo, hi; /* only if !addrcnt */
119    
120     int addrcnt;
121     /* addrcnt addresses follow */
122 root 1.10 } RANGE;
123    
124     typedef struct
125     {
126     RANGE **ranges;
127     int rangecnt, rangemax;
128 root 1.1
129 root 1.12 tstamp next;
130 root 1.10 tstamp interval;
131 root 1.12
132 root 1.10 tstamp maxrtt;
133 root 1.12
134 root 1.10 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 root 1.1
157 root 1.8 typedef struct
158     {
159 root 1.1 uint8_t type, code;
160     uint16_t cksum;
161 root 1.11
162 root 1.1 uint16_t id, seq;
163 root 1.11
164 root 1.10 uint16_t pinger;
165     uint16_t magic;
166 root 1.11
167 root 1.9 uint32_t stamp_hi;
168     uint32_t stamp_lo;
169 root 1.1 } PKT;
170    
171 root 1.9 static int
172 root 1.10 pkt_is_valid_for (PKT *pkt, PINGER *pinger)
173 root 1.9 {
174 root 1.10 return pkt->id == pinger->magic1
175     && pkt->seq == pinger->magic2
176     && pkt->magic == pinger->magic3;
177 root 1.9 }
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 root 1.10 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 root 1.9 /*****************************************************************************/
219    
220 root 1.10 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 root 1.1 {
230 root 1.11 tstamp wait = DRAIN_INTERVAL / 2.;
231 root 1.10
232     while (sendto (fd, buf, len, flags, sa, salen) < 0 && errno == ENOBUFS)
233 root 1.11 ssleep (wait *= 2.);
234 root 1.10 }
235    
236 root 1.11 // ping current address, return true and increment if more to ping
237     static int
238     range_send_ping (RANGE *self, PKT *pkt)
239 root 1.10 {
240 root 1.11 // 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 root 1.10 pkt->cksum = 0;
249    
250 root 1.11 if (self->addrlen == 4)
251 root 1.10 {
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 root 1.11 memcpy (&sa.sin_addr, addr, sizeof (sa.sin_addr));
261 root 1.10
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 root 1.1
271 root 1.10 sa.sin6_family = AF_INET6;
272     sa.sin6_port = 0;
273     sa.sin6_flowinfo = 0;
274     sa.sin6_scope_id = 0;
275 root 1.1
276 root 1.11 memcpy (&sa.sin6_addr, addr, sizeof (sa.sin6_addr));
277 root 1.10
278 root 1.11 xsendto (icmp6_fd, pkt, sizeof (*pkt), 0, &sa, sizeof (sa));
279 root 1.10 #endif
280     }
281 root 1.11
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 root 1.10 }
304    
305 root 1.11 /*****************************************************************************/
306    
307 root 1.10 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 root 1.1
334 root 1.10 self->ranges [k] = elem;
335 root 1.1 }
336    
337     static void
338 root 1.10 upheap (PINGER *self, int k)
339 root 1.1 {
340 root 1.10 RANGE *elem = self->ranges [k];
341 root 1.1
342 root 1.10 while (k)
343 root 1.1 {
344 root 1.10 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 root 1.1
351 root 1.10 k = j;
352 root 1.1 }
353 root 1.10
354     self->ranges [k] = elem;
355 root 1.1 }
356    
357     static void *
358 root 1.10 ping_proc (void *self_)
359 root 1.1 {
360 root 1.10 PINGER *self = (PINGER *)self_;
361 root 1.1 PKT pkt;
362    
363     memset (&pkt, 0, sizeof (pkt));
364    
365 root 1.12 tstamp now = NOW ();
366 root 1.10
367 root 1.12 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 root 1.1
376 root 1.10 while (self->rangecnt)
377 root 1.1 {
378 root 1.10 RANGE *range = self->ranges [0];
379 root 1.1
380 root 1.10 // ranges [0] is always the next range to ping
381     tstamp wait = range->next - now;
382 root 1.6
383 root 1.10 // compare with the global frequency limit
384     {
385 root 1.12 tstamp diff = self->next - now;
386 root 1.10
387     if (wait < diff)
388 root 1.12 wait = diff; // global rate limit overrides
389     else
390     self->next = range->next; // fast forward
391 root 1.10 }
392    
393     if (wait > 0.)
394     ssleep (wait);
395    
396     now = NOW ();
397    
398     ts_to_pkt (&pkt, now);
399    
400 root 1.11 if (!range_send_ping (range, &pkt))
401 root 1.1 {
402 root 1.10 self->ranges [0] = self->ranges [--self->rangecnt];
403     range_free (range);
404     }
405     else
406 root 1.12 range->next = self->next + range->interval;
407 root 1.10
408 root 1.11 downheap (self);
409 root 1.1
410 root 1.12 self->next += self->interval;
411     now = NOW ();
412 root 1.10 }
413    
414     ssleep (self->maxrtt);
415    
416     {
417     uint16_t id = self->id;
418 root 1.1
419 root 1.10 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 root 1.1
438 root 1.10 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 root 1.1
467 root 1.10 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 root 1.1 {
478 root 1.10 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 root 1.12 self->next = 0.;
490 root 1.10 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 root 1.11 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 root 1.10 /*****************************************************************************/
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 root 1.1
543 root 1.10 {
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 root 1.1
552 root 1.10 av_push (self->recvq, newRV_noinc ((SV *)pkt));
553     }
554     }
555 root 1.1
556 root 1.10 static void
557     recv_flush (void)
558     {
559     if (firstrecv < 0)
560     return;
561    
562     ENTER;
563     SAVETMPS;
564 root 1.1
565 root 1.11 do
566 root 1.10 {
567     dSP;
568     PINGER *self = pingers [firstrecv];
569     firstrecv = self->nextrecv;
570 root 1.1
571 root 1.10 self->nextrecv = -1;
572 root 1.1
573 root 1.10 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 root 1.1 }
579 root 1.11 while (firstrecv >= 0);
580 root 1.1
581 root 1.10 FREETMPS;
582     LEAVE;
583 root 1.1 }
584    
585 root 1.10 /*****************************************************************************/
586    
587     #if 0
588 root 1.1 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 root 1.6 if (av_len (res_av) < 0)
596     return;
597    
598 root 1.1 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 root 1.10 #endif
615 root 1.1
616     static void
617     boot ()
618     {
619 root 1.10 if (pipe (thr_res) < 0)
620     croak ("AnyEvent::FastPing: unable to create receive pipe");
621 root 1.1
622 root 1.10 sv_setiv (get_sv ("AnyEvent::FastPing::THR_RES_FD", 1), thr_res [0]);
623 root 1.1
624     icmp4_fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
625 root 1.2 fcntl (icmp4_fd, F_SETFL, O_NONBLOCK);
626 root 1.1 #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 root 1.5 #if ENABLE_IPV6
635 root 1.1 icmp6_fd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
636 root 1.2 fcntl (icmp6_fd, F_SETFL, O_NONBLOCK);
637 root 1.1 # 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 root 1.12 #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 root 1.10 MODULE = AnyEvent::FastPing PACKAGE = AnyEvent::FastPing PREFIX = pinger_
656 root 1.1
657     BOOT:
658     {
659     HV *stash = gv_stashpv ("AnyEvent::FastPing", 1);
660    
661 root 1.9 if (sizeof (PKT) & 3)
662     croak ("size of PKT structure is not a multiple of 4");
663    
664 root 1.1 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 root 1.10 void
676 root 1.11 _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 root 1.10 _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 root 1.12
761 root 1.10 SvPOK_only (pv);
762 root 1.11 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), gv_stashpv (SvPVutf8_nolen (klass), 1))));
763 root 1.10 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 root 1.1 CODE:
777 root 1.10 RETVAL = self->id;
778     OUTPUT:
779     RETVAL
780    
781     void pinger_start (PINGER *self)
782    
783     void pinger_stop (PINGER *self)
784 root 1.6
785 root 1.11 void
786     _stop_id (UV id)
787 root 1.10 CODE:
788     if (id < pingercnt && pingers [id])
789     pinger_stop (pingers [id]);
790 root 1.1
791 root 1.11 void
792     interval (PINGER *self, NV interval)
793 root 1.10 CODE:
794 root 1.12 NOT_RUNNING;
795 root 1.10 self->interval = interval > MIN_INTERVAL ? interval : MIN_INTERVAL;
796 root 1.1
797 root 1.11 void
798     max_rtt (PINGER *self, NV maxrtt)
799     CODE:
800 root 1.12 NOT_RUNNING;
801 root 1.11 self->maxrtt = maxrtt;
802    
803     void
804 root 1.12 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 root 1.10 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 root 1.12 NOT_RUNNING;
818 root 1.1
819 root 1.10 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 root 1.1
822 root 1.10 if (lo_len == 4 && icmp4_fd < 0) croak ("IPv4 support unavailable");
823     if (lo_len == 16 && icmp6_fd < 0) croak ("IPv6 support unavailable");
824 root 1.1
825 root 1.11 if (memcmp (lo, hi, lo_len) > 0)
826     croak ("AnyEvent::FastPing::add_range called with lo > hi");
827    
828 root 1.10 range = calloc (1, sizeof (RANGE));
829 root 1.1
830 root 1.10 range->next = 0;
831     range->interval = interval > MIN_INTERVAL ? interval : MIN_INTERVAL;
832     range->addrlen = lo_len;
833 root 1.1
834 root 1.10 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 root 1.1
837 root 1.11 pinger_add_range (self, range);
838 root 1.1 }
839    
840 root 1.10 void
841 root 1.12 add_hosts (PINGER *self, SV *addrs, NV interval = 0, UV interleave = 1)
842 root 1.1 CODE:
843 root 1.11 {
844     AV *av;
845 root 1.12 int i, j, k;
846 root 1.11 int cnt;
847     int addrlen;
848     RANGE *range;
849 root 1.12 NOT_RUNNING;
850 root 1.1
851 root 1.11 if (!SvROK (addrs) || SvTYPE (SvRV (addrs)) != SVt_PVAV)
852 root 1.12 croak ("AnyEvent::FastPing::add_hosts expects an arrayref with binary IPv4 or IPv6 addresses");
853 root 1.9
854 root 1.11 av = (AV *)SvRV (addrs);
855     cnt = av_len (av) + 1;
856 root 1.1
857 root 1.11 if (!cnt)
858     XSRETURN_EMPTY;
859 root 1.1
860 root 1.11 addrlen = SvCUR (*av_fetch (av, 0, 1));
861 root 1.1
862 root 1.11 if (addrlen != 4 && addrlen != 16)
863     croak ("AnyEvent::FastPing::add_hosts addresses must be specified as binary IPv4 or IPv6 addresses");
864 root 1.1
865 root 1.11 for (i = cnt; --i; )
866     {
867     SV *sv = *av_fetch (av, i, 1);
868 root 1.1
869 root 1.11 if (!sv_utf8_downgrade (sv, 1) || addrlen != SvCUR (sv))
870     croak ("AnyEvent::FastPing::add_hosts addresses must all have the same size");
871     }
872 root 1.1
873 root 1.11 range = calloc (1, sizeof (RANGE) + cnt * addrlen);
874 root 1.10
875 root 1.11 range->next = 0;
876     range->interval = interval > MIN_INTERVAL ? interval : MIN_INTERVAL;
877     range->addrlen = addrlen;
878     range->addrcnt = cnt;
879 root 1.1
880 root 1.12 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 root 1.1
890 root 1.11 pinger_add_range (self, range);
891 root 1.1 }
892