… | |
… | |
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> |
… | |
… | |
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 | |
52 | typedef uint8_t addr_tt[16]; |
51 | typedef uint8_t addr_tt[16]; |
53 | |
52 | |
|
|
53 | /*****************************************************************************/ |
|
|
54 | |
54 | typedef double tstamp; |
55 | typedef double tstamp; |
55 | |
56 | |
56 | static tstamp |
57 | static tstamp |
57 | NOW (void) |
58 | NOW (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 | } |
|
|
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 | /*****************************************************************************/ |
63 | |
90 | |
64 | typedef struct |
91 | typedef struct |
65 | { |
92 | { |
66 | int family; |
93 | int family; |
67 | addr_tt lo, hi; |
94 | addr_tt lo, hi; |
… | |
… | |
106 | |
133 | |
107 | uint8_t src[16]; |
134 | uint8_t src[16]; |
108 | uint8_t dst[16]; |
135 | uint8_t dst[16]; |
109 | } IP6HDR; |
136 | } IP6HDR; |
110 | |
137 | |
|
|
138 | /*****************************************************************************/ |
|
|
139 | |
111 | #define MAGIC 0xca4c |
140 | #define MAGIC 0xca4c |
112 | |
141 | |
113 | static uint16_t magic; |
142 | static uint16_t magic; |
114 | |
143 | |
115 | typedef struct |
144 | typedef struct |
116 | { |
145 | { |
117 | uint8_t type, code; |
146 | uint8_t type, code; |
118 | uint16_t cksum; |
147 | uint16_t cksum; |
119 | uint16_t id, seq; |
148 | uint16_t id, seq; |
120 | uint32_t payload; |
149 | uint32_t payload; |
121 | tstamp stamp; // be careful when accessing this |
150 | uint32_t stamp_hi; |
|
|
151 | uint32_t stamp_lo; |
122 | } PKT; |
152 | } PKT; |
|
|
153 | |
|
|
154 | static int |
|
|
155 | pkt_is_valid (PKT *pkt) |
|
|
156 | { |
|
|
157 | return pkt->id == (uint16_t) magic |
|
|
158 | && pkt->seq == (uint16_t)~magic; |
|
|
159 | } |
|
|
160 | |
|
|
161 | static void |
|
|
162 | ts_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 | |
|
|
173 | static tstamp |
|
|
174 | pkt_to_ts (PKT *pkt) |
|
|
175 | { |
|
|
176 | return pkt->stamp_hi * 4096. |
|
|
177 | + pkt->stamp_lo * (4096. / 4294967296.); |
|
|
178 | } |
|
|
179 | |
|
|
180 | /*****************************************************************************/ |
123 | |
181 | |
124 | static pthread_t pthrid; |
182 | static pthread_t pthrid; |
125 | static int thr_send[2]; // send to worker |
183 | static int thr_send[2]; // send to worker |
126 | static int thr_recv[2]; // receive from worker |
184 | static int thr_recv[2]; // receive from worker |
127 | |
185 | |
… | |
… | |
131 | |
189 | |
132 | static uint16_t |
190 | static uint16_t |
133 | icmp_cksum (void *data, unsigned int len) |
191 | icmp_cksum (void *data, unsigned int len) |
134 | { |
192 | { |
135 | register int sum = 0; |
193 | register int sum = 0; |
136 | uint16_t *wp; |
194 | uint32_t *wp; |
137 | |
195 | |
138 | assert (~len & 1); |
|
|
139 | |
|
|
140 | for (wp = (uint16_t *)data; len; wp++, len -= 2) |
196 | for (wp = (uint32_t *)data; len; wp++, len -= 4) |
141 | sum += *wp; |
197 | sum += (*wp & 0xffff) + (*wp >> 16); |
142 | |
198 | |
143 | sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */ |
199 | sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */ |
144 | sum += sum >> 16; /* add carry */ |
200 | sum += sum >> 16; /* add carry */ |
145 | |
201 | |
146 | return ~sum; |
202 | return ~sum; |
… | |
… | |
234 | else if (range) |
290 | else if (range) |
235 | next = range->next; |
291 | next = range->next; |
236 | } |
292 | } |
237 | |
293 | |
238 | if (wait > 0.) |
294 | if (wait > 0.) |
239 | { |
|
|
240 | struct timespec ts; |
|
|
241 | |
|
|
242 | ts.tv_sec = wait; |
|
|
243 | ts.tv_nsec = (wait - ts.tv_sec) * 1000000000.; |
|
|
244 | |
|
|
245 | nanosleep (&ts, 0); |
295 | ssleep (wait); |
246 | } |
|
|
247 | |
296 | |
248 | now = NOW (); |
297 | now = NOW (); |
249 | |
298 | |
250 | pkt.stamp = now; |
299 | ts_to_pkt (&pkt, now); |
251 | pkt.cksum = 0; |
300 | pkt.cksum = 0; |
252 | |
301 | |
253 | if (range->family == AF_INET) |
302 | if (range->family == AF_INET) |
254 | { |
303 | { |
255 | pkt.type = ICMP4_ECHO; |
304 | pkt.type = ICMP4_ECHO; |
… | |
… | |
275 | errno = 0; |
324 | errno = 0; |
276 | #endif |
325 | #endif |
277 | } |
326 | } |
278 | |
327 | |
279 | if (errno == ENOBUFS) |
328 | if (errno == ENOBUFS) |
280 | { |
329 | ssleep (DRAIN_INTERVAL); |
281 | struct timespec ts; |
|
|
282 | |
|
|
283 | ts.tv_sec = 0; |
|
|
284 | ts.tv_nsec = DRAIN_INTERVAL * 1000000000; |
|
|
285 | |
|
|
286 | nanosleep (&ts, 0); |
|
|
287 | } |
|
|
288 | else |
330 | else |
289 | { |
331 | { |
290 | inc_addr (&range->lo); |
332 | inc_addr (&range->lo); |
291 | |
333 | |
292 | range->next = next; |
334 | range->next = next; |
… | |
… | |
418 | { |
460 | { |
419 | HV *stash = gv_stashpv ("AnyEvent::FastPing", 1); |
461 | HV *stash = gv_stashpv ("AnyEvent::FastPing", 1); |
420 | |
462 | |
421 | cbs = get_av ("AnyEvent::FastPing::CB", 1); |
463 | cbs = get_av ("AnyEvent::FastPing::CB", 1); |
422 | magic = getpid () ^ MAGIC; |
464 | magic = getpid () ^ MAGIC; |
|
|
465 | |
|
|
466 | if (sizeof (PKT) & 3) |
|
|
467 | croak ("size of PKT structure is not a multiple of 4"); |
423 | |
468 | |
424 | boot (); |
469 | boot (); |
425 | |
470 | |
426 | newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0)); |
471 | newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0)); |
427 | newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0)); |
472 | newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0)); |
… | |
… | |
563 | struct sockaddr_in sa; |
608 | struct sockaddr_in sa; |
564 | socklen_t sl = sizeof (sa); |
609 | socklen_t sl = sizeof (sa); |
565 | 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 ()); |
566 | tstamp now = NOW (); |
611 | tstamp now = NOW (); |
567 | |
612 | |
|
|
613 | if (!res_av) |
|
|
614 | XSRETURN_UNDEF; |
|
|
615 | |
568 | for (;;) |
616 | for (;;) |
569 | { |
617 | { |
570 | IP4HDR *iphdr = (IP4HDR *)buf; |
618 | IP4HDR *iphdr = (IP4HDR *)buf; |
571 | 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); |
572 | int hdrlen, totlen; |
620 | int hdrlen, totlen; |
… | |
… | |
577 | |
625 | |
578 | hdrlen = (iphdr->version_ihl & 15) * 4; |
626 | hdrlen = (iphdr->version_ihl & 15) * 4; |
579 | totlen = ntohs (iphdr->tot_len); |
627 | totlen = ntohs (iphdr->tot_len); |
580 | |
628 | |
581 | // packet corrupt? |
629 | // packet corrupt? |
582 | if (!res_av |
|
|
583 | || totlen > len |
630 | if (totlen > len |
584 | || iphdr->protocol != IPPROTO_ICMP |
631 | || iphdr->protocol != IPPROTO_ICMP |
585 | || hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen) |
632 | || hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen) |
586 | continue; |
633 | continue; |
587 | |
634 | |
588 | pkt = (PKT *)(buf + hdrlen); |
635 | pkt = (PKT *)(buf + hdrlen); |
589 | |
636 | |
590 | if (pkt->type != ICMP4_ECHO_REPLY |
637 | if (pkt->type != ICMP4_ECHO_REPLY || !pkt_is_valid (pkt)) |
591 | || pkt->id != (uint16_t) magic |
|
|
592 | || pkt->seq != (uint16_t)~magic |
|
|
593 | || !isnormal (pkt->stamp)) |
|
|
594 | continue; |
638 | continue; |
595 | |
639 | |
596 | { |
640 | { |
597 | AV *av = newAV (); |
641 | AV *av = newAV (); |
598 | av_push (av, newSVpvn ((char *)&sa.sin_addr, 4)); |
642 | av_push (av, newSVpvn ((char *)&sa.sin_addr, 4)); |
599 | av_push (av, newSVnv (now - pkt->stamp)); |
643 | av_push (av, newSVnv (now - pkt_to_ts (pkt))); |
600 | av_push (av, newSVuv (pkt->payload)); |
644 | av_push (av, newSVuv (pkt->payload)); |
601 | |
645 | |
602 | av_push (res_av, newRV_noinc ((SV *)av)); |
646 | av_push (res_av, newRV_noinc ((SV *)av)); |
603 | } |
647 | } |
604 | } |
648 | } |
605 | |
649 | |
606 | if (res_av) |
|
|
607 | feed_reply (res_av); |
650 | feed_reply (res_av); |
608 | } |
651 | } |
609 | |
652 | |
610 | void |
653 | void |
611 | _recv_icmp6 (...) |
654 | _recv_icmp6 (...) |
612 | CODE: |
655 | CODE: |
… | |
… | |
615 | socklen_t sl = sizeof (sa); |
658 | socklen_t sl = sizeof (sa); |
616 | 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 ()); |
617 | PKT pkt; |
660 | PKT pkt; |
618 | tstamp now = NOW (); |
661 | tstamp now = NOW (); |
619 | |
662 | |
|
|
663 | if (!res_av) |
|
|
664 | XSRETURN_UNDEF; |
|
|
665 | |
620 | for (;;) |
666 | for (;;) |
621 | { |
667 | { |
622 | 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); |
623 | |
669 | |
624 | if (len != sizeof (PKT)) |
670 | if (len != sizeof (PKT)) |
625 | break; |
671 | break; |
626 | |
672 | |
627 | if (!res_av |
673 | if (pkt.type != ICMP6_ECHO_REPLY || !pkt_is_valid (&pkt)) |
628 | || pkt.type != ICMP6_ECHO_REPLY |
|
|
629 | || pkt.id != (uint16_t) magic |
|
|
630 | || pkt.seq != (uint16_t)~magic |
|
|
631 | || !isnormal (pkt.stamp)) |
|
|
632 | continue; |
674 | continue; |
633 | |
675 | |
634 | { |
676 | { |
635 | AV *av = newAV (); |
677 | AV *av = newAV (); |
636 | av_push (av, newSVpvn ((char *)&sa.sin6_addr, 16)); |
678 | av_push (av, newSVpvn ((char *)&sa.sin6_addr, 16)); |
637 | av_push (av, newSVnv (now - pkt.stamp)); |
679 | av_push (av, newSVnv (now - pkt_to_ts (&pkt))); |
638 | av_push (av, newSVuv (pkt.payload)); |
680 | av_push (av, newSVuv (pkt.payload)); |
639 | |
681 | |
640 | av_push (res_av, newRV_noinc ((SV *)av)); |
682 | av_push (res_av, newRV_noinc ((SV *)av)); |
641 | } |
683 | } |
642 | } |
684 | } |
643 | |
685 | |
644 | if (res_av) |
|
|
645 | feed_reply (res_av); |
686 | feed_reply (res_av); |
646 | } |
687 | } |
647 | |
688 | |