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