ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FastPing/FastPing.xs
(Generate patch)

Comparing AnyEvent-FastPing/FastPing.xs (file contents):
Revision 1.7 by root, Sun Jan 16 17:05:32 2011 UTC vs.
Revision 1.9 by root, Sun Jan 30 02:03:35 2011 UTC

1#if defined(__linux) 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 2# define ENABLE_IPV6 1 // if you get compilation problems try to disable IPv6
3#else 3#else
4# define ENABLE_IPV6 0 4# define ENABLE_IPV6 0
5#endif 5#endif
6 6
8#include "perl.h" 8#include "perl.h"
9#include "XSUB.h" 9#include "XSUB.h"
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
13#include <math.h>
14#include <stdio.h> 13#include <stdio.h>
15#include <stdlib.h> 14#include <stdlib.h>
16#include <string.h> 15#include <string.h>
17 16
18#include <time.h> 17#include <time.h>
30#include <arpa/inet.h> 29#include <arpa/inet.h>
31 30
32#ifdef __linux 31#ifdef __linux
33# include <linux/icmp.h> 32# include <linux/icmp.h>
34#endif 33#endif
35#if ENABLE_IPV6 34#if ENABLE_IPV6 && !defined (__CYGWIN__)
36# include <netinet/icmp6.h> 35# include <netinet/icmp6.h>
37#endif 36#endif
38 37
39#define ICMP4_ECHO 8 38#define ICMP4_ECHO 8
40#define ICMP4_ECHO_REPLY 0 39#define ICMP4_ECHO_REPLY 0
41#define ICMP6_ECHO 128 40#define ICMP6_ECHO 128
42#define ICMP6_ECHO_REPLY 129 41#define ICMP6_ECHO_REPLY 129
43 42
44#define DRAIN_INTERVAL .000001 // how long to wait when sendto returns ENOBUFS, in seconds 43#define DRAIN_INTERVAL 1e-6 // how long to wait when sendto returns ENOBUFS, in seconds
45#define MIN_INTERVAL .000001 // minimum packet send interval, in seconds 44#define MIN_INTERVAL 1e-6 // minimum packet send interval, in seconds
46 45
47#define HDR_SIZE_IP4 20 46#define HDR_SIZE_IP4 20
48#define HDR_SIZE_IP6 48 47#define HDR_SIZE_IP6 48
49 48
50//TODO: xread/xwrite for atomicity? we currently rely on the fact that the pip buffersize divides exactly by pointer sizes 49//TODO: xread/xwrite for atomicity? we currently rely on the fact that the pip buffersize divides exactly by pointer sizes
51 50
52typedef uint8_t addr_t[16]; 51typedef uint8_t addr_tt[16];
52
53/*****************************************************************************/
53 54
54typedef double tstamp; 55typedef double tstamp;
55 56
56static tstamp 57static tstamp
57NOW (void) 58NOW (void)
58{ 59{
59 struct timeval tv; 60 struct timeval tv;
61
60 gettimeofday (&tv, 0); 62 gettimeofday (&tv, 0);
63
61 return tv.tv_sec + tv.tv_usec * 0.000001; 64 return tv.tv_sec + tv.tv_usec * 1e-6;
62} 65}
63 66
67static void
68ssleep (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
64typedef struct { 91typedef struct
92{
65 int family; 93 int family;
66 addr_t lo, hi; 94 addr_tt lo, hi;
67 double interval; 95 double interval;
68 tstamp next; 96 tstamp next;
69} RANGE; 97} RANGE;
70 98
71typedef struct { 99typedef struct
100{
72 SV *id; 101 SV *id;
73 double interval; 102 double interval;
74 int nranges; 103 int nranges;
75 RANGE *ranges; 104 RANGE *ranges;
76 uint32_t payload; 105 uint32_t payload;
77} REQ; 106} REQ;
78 107
79typedef struct { 108typedef struct
109{
80 uint8_t version_ihl; 110 uint8_t version_ihl;
81 uint8_t tos; 111 uint8_t tos;
82 uint16_t tot_len; 112 uint16_t tot_len;
83 113
84 uint16_t id; 114 uint16_t id;
90 120
91 uint32_t src; 121 uint32_t src;
92 uint32_t dst; 122 uint32_t dst;
93} IP4HDR; 123} IP4HDR;
94 124
95typedef struct { 125typedef struct
126{
96 uint8_t version; 127 uint8_t version;
97 uint8_t x1, x2, x3; 128 uint8_t x1, x2, x3;
98 129
99 uint16_t payload_len; 130 uint16_t payload_len;
100 uint8_t nxt_hdr; 131 uint8_t nxt_hdr;
102 133
103 uint8_t src[16]; 134 uint8_t src[16];
104 uint8_t dst[16]; 135 uint8_t dst[16];
105} IP6HDR; 136} IP6HDR;
106 137
138/*****************************************************************************/
139
107#define MAGIC 0xca4c 140#define MAGIC 0xca4c
108 141
109static uint16_t magic; 142static uint16_t magic;
110 143
111typedef struct { 144typedef struct
145{
112 uint8_t type, code; 146 uint8_t type, code;
113 uint16_t cksum; 147 uint16_t cksum;
114 uint16_t id, seq; 148 uint16_t id, seq;
115 uint32_t payload; 149 uint32_t payload;
116 tstamp stamp; // be careful when accessing this 150 uint32_t stamp_hi;
151 uint32_t stamp_lo;
117} PKT; 152} PKT;
153
154static int
155pkt_is_valid (PKT *pkt)
156{
157 return pkt->id == (uint16_t) magic
158 && pkt->seq == (uint16_t)~magic;
159}
160
161static void
162ts_to_pkt (PKT *pkt, tstamp ts)
163{
164 /* move 12 bits of seconds into the 32 bit fractional part */
165 /* leaving 20 bits subsecond resolution and 44 bits of integers */
166 /* (of which 32 are typically usable) */
167 ts *= 1. / 4096.;
168
169 pkt->stamp_hi = ts;
170 pkt->stamp_lo = (ts - pkt->stamp_hi) * 4294967296.;
171}
172
173static tstamp
174pkt_to_ts (PKT *pkt)
175{
176 return pkt->stamp_hi * 4096.
177 + pkt->stamp_lo * (4096. / 4294967296.);
178}
179
180/*****************************************************************************/
118 181
119static pthread_t pthrid; 182static pthread_t pthrid;
120static int thr_send[2]; // send to worker 183static int thr_send[2]; // send to worker
121static int thr_recv[2]; // receive from worker 184static int thr_recv[2]; // receive from worker
122 185
126 189
127static uint16_t 190static uint16_t
128icmp_cksum (void *data, unsigned int len) 191icmp_cksum (void *data, unsigned int len)
129{ 192{
130 register int sum = 0; 193 register int sum = 0;
131 uint16_t *wp; 194 uint32_t *wp;
132 195
133 assert (~len & 1);
134
135 for (wp = (uint16_t *)data; len; wp++, len -= 2) 196 for (wp = (uint32_t *)data; len; wp++, len -= 4)
136 sum += *wp; 197 sum += (*wp & 0xffff) + (*wp >> 16);
137 198
138 sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */ 199 sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */
139 sum += sum >> 16; /* add carry */ 200 sum += sum >> 16; /* add carry */
140 201
141 return ~sum; 202 return ~sum;
142} 203}
143 204
144static void 205static void
145inc_addr (addr_t *addr) 206inc_addr (addr_tt *addr)
146{ 207{
147 int len = sizeof (addr_t) - 1; 208 int len = sizeof (addr_tt) - 1;
148 209
149 do 210 do
150 { 211 {
151 if ((*addr)[len] != 0xff) 212 if ((*addr)[len] != 0xff)
152 { 213 {
211 while (req->nranges) 272 while (req->nranges)
212 { 273 {
213 RANGE *range = req->ranges; 274 RANGE *range = req->ranges;
214 int n, k; 275 int n, k;
215 276
216 if (!memcmp (&range->lo, &range->hi, sizeof (addr_t))) 277 if (!memcmp (&range->lo, &range->hi, sizeof (addr_tt)))
217 req->ranges [0] = req->ranges [--req->nranges]; 278 req->ranges [0] = req->ranges [--req->nranges];
218 else 279 else
219 { 280 {
220 // ranges [0] is always the next range to ping 281 // ranges [0] is always the next range to ping
221 tstamp wait = range->next - now; 282 tstamp wait = range->next - now;
229 else if (range) 290 else if (range)
230 next = range->next; 291 next = range->next;
231 } 292 }
232 293
233 if (wait > 0.) 294 if (wait > 0.)
234 {
235 struct timespec ts;
236
237 ts.tv_sec = wait;
238 ts.tv_nsec = (wait - ts.tv_sec) * 1000000000.;
239
240 nanosleep (&ts, 0); 295 ssleep (wait);
241 }
242 296
243 now = NOW (); 297 now = NOW ();
244 298
245 pkt.stamp = now; 299 ts_to_pkt (&pkt, now);
246 pkt.cksum = 0; 300 pkt.cksum = 0;
247 301
248 if (range->family == AF_INET) 302 if (range->family == AF_INET)
249 { 303 {
250 pkt.type = ICMP4_ECHO; 304 pkt.type = ICMP4_ECHO;
251 pkt.cksum = icmp_cksum (&pkt, sizeof (pkt)); 305 pkt.cksum = icmp_cksum (&pkt, sizeof (pkt));
252 306
253 memcpy (&sa4.sin_addr, 307 memcpy (&sa4.sin_addr,
254 sizeof (addr_t) - sizeof (sa4.sin_addr) + (char *)&range->lo, 308 sizeof (addr_tt) - sizeof (sa4.sin_addr) + (char *)&range->lo,
255 sizeof (sa4.sin_addr)); 309 sizeof (sa4.sin_addr));
256 310
257 if (sendto (icmp4_fd, &pkt, sizeof (pkt), 0, (struct sockaddr *)&sa4, sizeof (sa4)) > 0) 311 if (sendto (icmp4_fd, &pkt, sizeof (pkt), 0, (struct sockaddr *)&sa4, sizeof (sa4)) > 0)
258 errno = 0; 312 errno = 0;
259 } 313 }
261 { 315 {
262#if ENABLE_IPV6 316#if ENABLE_IPV6
263 pkt.type = ICMP6_ECHO; 317 pkt.type = ICMP6_ECHO;
264 318
265 memcpy (&sa6.sin6_addr, 319 memcpy (&sa6.sin6_addr,
266 sizeof (addr_t) - sizeof (sa6.sin6_addr) + (char *)&range->lo, 320 sizeof (addr_tt) - sizeof (sa6.sin6_addr) + (char *)&range->lo,
267 sizeof (sa6.sin6_addr)); 321 sizeof (sa6.sin6_addr));
268 322
269 if (sendto (icmp6_fd, &pkt, sizeof (pkt), 0, (struct sockaddr *)&sa6, sizeof (sa6)) > 0) 323 if (sendto (icmp6_fd, &pkt, sizeof (pkt), 0, (struct sockaddr *)&sa6, sizeof (sa6)) > 0)
270 errno = 0; 324 errno = 0;
271#endif 325#endif
272 } 326 }
273 327
274 if (errno == ENOBUFS) 328 if (errno == ENOBUFS)
275 { 329 ssleep (DRAIN_INTERVAL);
276 struct timespec ts;
277
278 ts.tv_sec = 0;
279 ts.tv_nsec = DRAIN_INTERVAL * 1000000000;
280
281 nanosleep (&ts, 0);
282 }
283 else 330 else
284 { 331 {
285 inc_addr (&range->lo); 332 inc_addr (&range->lo);
286 333
287 range->next = next; 334 range->next = next;
414 HV *stash = gv_stashpv ("AnyEvent::FastPing", 1); 461 HV *stash = gv_stashpv ("AnyEvent::FastPing", 1);
415 462
416 cbs = get_av ("AnyEvent::FastPing::CB", 1); 463 cbs = get_av ("AnyEvent::FastPing::CB", 1);
417 magic = getpid () ^ MAGIC; 464 magic = getpid () ^ MAGIC;
418 465
466 if (sizeof (PKT) & 3)
467 croak ("size of PKT structure is not a multiple of 4");
468
419 boot (); 469 boot ();
420 470
421 newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0)); 471 newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0));
422 newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0)); 472 newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0));
423 473
469 hi = *av_fetch (av, 1, 1); 519 hi = *av_fetch (av, 1, 1);
470 520
471 sv_utf8_downgrade (lo, 0); 521 sv_utf8_downgrade (lo, 0);
472 sv_utf8_downgrade (hi, 0); 522 sv_utf8_downgrade (hi, 0);
473 523
474 memset (&r->lo, 0, sizeof (addr_t)); 524 memset (&r->lo, 0, sizeof (addr_tt));
475 memset (&r->hi, 0, sizeof (addr_t)); 525 memset (&r->hi, 0, sizeof (addr_tt));
476 526
477 if (SvPOKp (lo) && SvPOKp (hi)) 527 if (SvPOKp (lo) && SvPOKp (hi))
478 { 528 {
479 if (SvCUR (lo) != SvCUR (hi)) 529 if (SvCUR (lo) != SvCUR (hi))
480 croak ("all addresses in range must be of the same size (either 4 or 16 bytes)"); 530 croak ("all addresses in range must be of the same size (either 4 or 16 bytes)");
481 531
482 if (SvCUR (lo) == 4) 532 if (SvCUR (lo) == 4)
483 { 533 {
484 r->family = AF_INET; 534 r->family = AF_INET;
485 memcpy (sizeof (addr_t) - 4 + (char *)&r->lo, SvPVX (lo), 4); 535 memcpy (sizeof (addr_tt) - 4 + (char *)&r->lo, SvPVX (lo), 4);
486 memcpy (sizeof (addr_t) - 4 + (char *)&r->hi, SvPVX (hi), 4); 536 memcpy (sizeof (addr_tt) - 4 + (char *)&r->hi, SvPVX (hi), 4);
487 } 537 }
488 else if (SvCUR (lo) == 16) 538 else if (SvCUR (lo) == 16)
489 { 539 {
490#if ENABLE_IPV6 540#if ENABLE_IPV6
491 r->family = AF_INET6; 541 r->family = AF_INET6;
492 memcpy (&r->lo, SvPVX (lo), sizeof (addr_t)); 542 memcpy (&r->lo, SvPVX (lo), sizeof (addr_tt));
493 memcpy (&r->hi, SvPVX (hi), sizeof (addr_t)); 543 memcpy (&r->hi, SvPVX (hi), sizeof (addr_tt));
494#else 544#else
495 croak ("IPv6 not supported in this configuration"); 545 croak ("IPv6 not supported in this configuration");
496#endif 546#endif
497 } 547 }
498 else 548 else
502 { 552 {
503 uint32_t addr; 553 uint32_t addr;
504 554
505 r->family = AF_INET; 555 r->family = AF_INET;
506 556
507 addr = htonl (SvUV (lo)); memcpy (sizeof (addr_t) - 4 + (char *)&r->lo, &addr, 4); 557 addr = htonl (SvUV (lo)); memcpy (sizeof (addr_tt) - 4 + (char *)&r->lo, &addr, 4);
508 addr = htonl (SvUV (hi)); memcpy (sizeof (addr_t) - 4 + (char *)&r->hi, &addr, 4); 558 addr = htonl (SvUV (hi)); memcpy (sizeof (addr_tt) - 4 + (char *)&r->hi, &addr, 4);
509 } 559 }
510 else 560 else
511 croak ("addresses in range must be strings with either 4 (IPv4) or 16 (IPv6) octets"); 561 croak ("addresses in range must be strings with either 4 (IPv4) or 16 (IPv6) octets");
512 562
513 if (r->family == AF_INET) 563 if (r->family == AF_INET)
558 struct sockaddr_in sa; 608 struct sockaddr_in sa;
559 socklen_t sl = sizeof (sa); 609 socklen_t sl = sizeof (sa);
560 AV *res_av = av_len (cbs) < 0 ? 0 : (AV *)sv_2mortal ((SV *)newAV ()); 610 AV *res_av = av_len (cbs) < 0 ? 0 : (AV *)sv_2mortal ((SV *)newAV ());
561 tstamp now = NOW (); 611 tstamp now = NOW ();
562 612
613 if (!res_av)
614 XSRETURN_UNDEF;
615
563 for (;;) 616 for (;;)
564 { 617 {
565 IP4HDR *iphdr = (IP4HDR *)buf; 618 IP4HDR *iphdr = (IP4HDR *)buf;
566 int len = recvfrom (icmp4_fd, buf, sizeof (buf), MSG_TRUNC, (struct sockaddr *)&sa, &sl); 619 int len = recvfrom (icmp4_fd, buf, sizeof (buf), MSG_TRUNC, (struct sockaddr *)&sa, &sl);
567 int hdrlen, totlen; 620 int hdrlen, totlen;
572 625
573 hdrlen = (iphdr->version_ihl & 15) * 4; 626 hdrlen = (iphdr->version_ihl & 15) * 4;
574 totlen = ntohs (iphdr->tot_len); 627 totlen = ntohs (iphdr->tot_len);
575 628
576 // packet corrupt? 629 // packet corrupt?
577 if (!res_av
578 || totlen > len 630 if (totlen > len
579 || iphdr->protocol != IPPROTO_ICMP 631 || iphdr->protocol != IPPROTO_ICMP
580 || hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen) 632 || hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen)
581 continue; 633 continue;
582 634
583 pkt = (PKT *)(buf + hdrlen); 635 pkt = (PKT *)(buf + hdrlen);
584 636
585 if (pkt->type != ICMP4_ECHO_REPLY 637 if (pkt->type != ICMP4_ECHO_REPLY || !pkt_is_valid (pkt))
586 || pkt->id != (uint16_t) magic
587 || pkt->seq != (uint16_t)~magic
588 || !isnormal (pkt->stamp))
589 continue; 638 continue;
590 639
591 { 640 {
592 AV *av = newAV (); 641 AV *av = newAV ();
593 av_push (av, newSVpvn ((char *)&sa.sin_addr, 4)); 642 av_push (av, newSVpvn ((char *)&sa.sin_addr, 4));
594 av_push (av, newSVnv (now - pkt->stamp)); 643 av_push (av, newSVnv (now - pkt_to_ts (pkt)));
595 av_push (av, newSVuv (pkt->payload)); 644 av_push (av, newSVuv (pkt->payload));
596 645
597 av_push (res_av, newRV_noinc ((SV *)av)); 646 av_push (res_av, newRV_noinc ((SV *)av));
598 } 647 }
599 } 648 }
600 649
601 if (res_av)
602 feed_reply (res_av); 650 feed_reply (res_av);
603} 651}
604 652
605void 653void
606_recv_icmp6 (...) 654_recv_icmp6 (...)
607 CODE: 655 CODE:
610 socklen_t sl = sizeof (sa); 658 socklen_t sl = sizeof (sa);
611 AV *res_av = av_len (cbs) < 0 ? 0 : (AV *)sv_2mortal ((SV *)newAV ()); 659 AV *res_av = av_len (cbs) < 0 ? 0 : (AV *)sv_2mortal ((SV *)newAV ());
612 PKT pkt; 660 PKT pkt;
613 tstamp now = NOW (); 661 tstamp now = NOW ();
614 662
663 if (!res_av)
664 XSRETURN_UNDEF;
665
615 for (;;) 666 for (;;)
616 { 667 {
617 int len = recvfrom (icmp6_fd, &pkt, sizeof (pkt), MSG_TRUNC, (struct sockaddr *)&sa, &sl); 668 int len = recvfrom (icmp6_fd, &pkt, sizeof (pkt), MSG_TRUNC, (struct sockaddr *)&sa, &sl);
618 669
619 if (len != sizeof (PKT)) 670 if (len != sizeof (PKT))
620 break; 671 break;
621 672
622 if (!res_av 673 if (pkt.type != ICMP6_ECHO_REPLY || !pkt_is_valid (&pkt))
623 || pkt.type != ICMP6_ECHO_REPLY
624 || pkt.id != (uint16_t) magic
625 || pkt.seq != (uint16_t)~magic
626 || !isnormal (pkt.stamp))
627 continue; 674 continue;
628 675
629 { 676 {
630 AV *av = newAV (); 677 AV *av = newAV ();
631 av_push (av, newSVpvn ((char *)&sa.sin6_addr, 16)); 678 av_push (av, newSVpvn ((char *)&sa.sin6_addr, 16));
632 av_push (av, newSVnv (now - pkt.stamp)); 679 av_push (av, newSVnv (now - pkt_to_ts (&pkt)));
633 av_push (av, newSVuv (pkt.payload)); 680 av_push (av, newSVuv (pkt.payload));
634 681
635 av_push (res_av, newRV_noinc ((SV *)av)); 682 av_push (res_av, newRV_noinc ((SV *)av));
636 } 683 }
637 } 684 }
638 685
639 if (res_av)
640 feed_reply (res_av); 686 feed_reply (res_av);
641} 687}
642 688

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines