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