1 | #define _POSIX_C_SOURCE 199309 |
1 | #if defined(__linux) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__CYGWIN__) |
2 | #define _GNU_SOURCE 1 |
|
|
3 | |
|
|
4 | #define 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 |
|
|
4 | # define ENABLE_IPV6 0 |
|
|
5 | #endif |
5 | |
6 | |
6 | #include "EXTERN.h" |
7 | #include "EXTERN.h" |
7 | #include "perl.h" |
8 | #include "perl.h" |
8 | #include "XSUB.h" |
9 | #include "XSUB.h" |
9 | |
10 | |
10 | #include <pthread.h> |
11 | #include <pthread.h> |
11 | |
12 | |
12 | #include <math.h> |
|
|
13 | #include <stdio.h> |
13 | #include <stdio.h> |
14 | #include <stdlib.h> |
14 | #include <stdlib.h> |
15 | #include <string.h> |
15 | #include <string.h> |
16 | |
16 | |
17 | #include <time.h> |
17 | #include <time.h> |
… | |
… | |
29 | #include <arpa/inet.h> |
29 | #include <arpa/inet.h> |
30 | |
30 | |
31 | #ifdef __linux |
31 | #ifdef __linux |
32 | # include <linux/icmp.h> |
32 | # include <linux/icmp.h> |
33 | #endif |
33 | #endif |
34 | #if IPV6 |
34 | #if ENABLE_IPV6 && !defined (__CYGWIN__) |
35 | # include <netinet/icmp6.h> |
35 | # include <netinet/icmp6.h> |
36 | #endif |
36 | #endif |
37 | |
37 | |
38 | #define ICMP4_ECHO 8 |
38 | #define ICMP4_ECHO 8 |
39 | #define ICMP4_ECHO_REPLY 0 |
39 | #define ICMP4_ECHO_REPLY 0 |
40 | #define ICMP6_ECHO 128 |
40 | #define ICMP6_ECHO 128 |
41 | #define ICMP6_ECHO_REPLY 129 |
41 | #define ICMP6_ECHO_REPLY 129 |
42 | |
42 | |
43 | #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 |
44 | #define MIN_INTERVAL .000001 // 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 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 |
50 | |
50 | |
51 | typedef uint8_t addr_t[16]; |
51 | typedef uint8_t addr_tt[16]; |
|
|
52 | |
|
|
53 | /*****************************************************************************/ |
52 | |
54 | |
53 | typedef double tstamp; |
55 | typedef double tstamp; |
54 | |
56 | |
55 | tstamp |
57 | static tstamp |
56 | NOW () |
58 | NOW (void) |
57 | { |
59 | { |
58 | struct timeval tv; |
60 | struct timeval tv; |
|
|
61 | |
59 | gettimeofday (&tv, 0); |
62 | gettimeofday (&tv, 0); |
|
|
63 | |
60 | return tv.tv_sec + tv.tv_usec * 0.000001; |
64 | return tv.tv_sec + tv.tv_usec * 1e-6; |
61 | } |
65 | } |
62 | |
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 | /*****************************************************************************/ |
|
|
90 | |
63 | typedef struct { |
91 | typedef struct |
|
|
92 | { |
64 | int family; |
93 | int family; |
65 | addr_t lo, hi; |
94 | addr_tt lo, hi; |
66 | double interval; |
95 | double interval; |
67 | tstamp next; |
96 | tstamp next; |
68 | } RANGE; |
97 | } RANGE; |
69 | |
98 | |
70 | typedef struct { |
99 | typedef struct |
|
|
100 | { |
71 | SV *id; |
101 | SV *id; |
72 | double interval; |
102 | double interval; |
73 | int nranges; |
103 | int nranges; |
74 | RANGE *ranges; |
104 | RANGE *ranges; |
75 | uint32_t payload; |
105 | uint32_t payload; |
76 | } REQ; |
106 | } REQ; |
77 | |
107 | |
78 | typedef struct { |
108 | typedef struct |
|
|
109 | { |
79 | uint8_t version_ihl; |
110 | uint8_t version_ihl; |
80 | uint8_t tos; |
111 | uint8_t tos; |
81 | uint16_t tot_len; |
112 | uint16_t tot_len; |
82 | |
113 | |
83 | uint16_t id; |
114 | uint16_t id; |
… | |
… | |
89 | |
120 | |
90 | uint32_t src; |
121 | uint32_t src; |
91 | uint32_t dst; |
122 | uint32_t dst; |
92 | } IP4HDR; |
123 | } IP4HDR; |
93 | |
124 | |
94 | typedef struct { |
125 | typedef struct |
|
|
126 | { |
95 | uint8_t version; |
127 | uint8_t version; |
96 | uint8_t x1, x2, x3; |
128 | uint8_t x1, x2, x3; |
97 | |
129 | |
98 | uint16_t payload_len; |
130 | uint16_t payload_len; |
99 | uint8_t nxt_hdr; |
131 | uint8_t nxt_hdr; |
… | |
… | |
101 | |
133 | |
102 | uint8_t src[16]; |
134 | uint8_t src[16]; |
103 | uint8_t dst[16]; |
135 | uint8_t dst[16]; |
104 | } IP6HDR; |
136 | } IP6HDR; |
105 | |
137 | |
|
|
138 | /*****************************************************************************/ |
|
|
139 | |
106 | #define MAGIC 0xca4c |
140 | #define MAGIC 0xca4c |
107 | |
141 | |
108 | static uint16_t magic; |
142 | static uint16_t magic; |
109 | |
143 | |
110 | typedef struct { |
144 | typedef struct |
|
|
145 | { |
111 | uint8_t type, code; |
146 | uint8_t type, code; |
112 | uint16_t cksum; |
147 | uint16_t cksum; |
113 | uint16_t id, seq; |
148 | uint16_t id, seq; |
114 | uint32_t payload; |
149 | uint32_t payload; |
115 | tstamp stamp; // be careful when accessing this |
150 | uint32_t stamp_hi; |
|
|
151 | uint32_t stamp_lo; |
116 | } 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 | /*****************************************************************************/ |
117 | |
181 | |
118 | static pthread_t pthrid; |
182 | static pthread_t pthrid; |
119 | static int thr_send[2]; // send to worker |
183 | static int thr_send[2]; // send to worker |
120 | static int thr_recv[2]; // receive from worker |
184 | static int thr_recv[2]; // receive from worker |
121 | |
185 | |
… | |
… | |
125 | |
189 | |
126 | static uint16_t |
190 | static uint16_t |
127 | icmp_cksum (void *data, unsigned int len) |
191 | icmp_cksum (void *data, unsigned int len) |
128 | { |
192 | { |
129 | register int sum = 0; |
193 | register int sum = 0; |
130 | uint16_t *wp; |
194 | uint32_t *wp; |
131 | |
195 | |
132 | assert (~len & 1); |
|
|
133 | |
|
|
134 | for (wp = (uint16_t *)data; len; wp++, len -= 2) |
196 | for (wp = (uint32_t *)data; len; wp++, len -= 4) |
135 | sum += *wp; |
197 | sum += (*wp & 0xffff) + (*wp >> 16); |
136 | |
198 | |
137 | sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */ |
199 | sum = (sum >> 16) + (sum & 0xffff); /* add high 16 to low 16 */ |
138 | sum += sum >> 16; /* add carry */ |
200 | sum += sum >> 16; /* add carry */ |
139 | |
201 | |
140 | return ~sum; |
202 | return ~sum; |
141 | } |
203 | } |
142 | |
204 | |
143 | static void |
205 | static void |
144 | inc_addr (addr_t *addr) |
206 | inc_addr (addr_tt *addr) |
145 | { |
207 | { |
146 | int len = sizeof (addr_t) - 1; |
208 | int len = sizeof (addr_tt) - 1; |
147 | |
209 | |
148 | do |
210 | do |
149 | { |
211 | { |
150 | if ((*addr)[len] != 0xff) |
212 | if ((*addr)[len] != 0xff) |
151 | { |
213 | { |
… | |
… | |
161 | static void * |
223 | static void * |
162 | ping_proc (void *unused) |
224 | ping_proc (void *unused) |
163 | { |
225 | { |
164 | PKT pkt; |
226 | PKT pkt; |
165 | struct sockaddr_in sa4; |
227 | struct sockaddr_in sa4; |
166 | #if IPV6 |
228 | #if ENABLE_IPV6 |
167 | struct sockaddr_in6 sa6; |
229 | struct sockaddr_in6 sa6; |
168 | #endif |
230 | #endif |
169 | |
231 | |
170 | memset (&pkt, 0, sizeof (pkt)); |
232 | memset (&pkt, 0, sizeof (pkt)); |
171 | |
233 | |
172 | memset (&sa4, 0, sizeof (sa4)); |
234 | memset (&sa4, 0, sizeof (sa4)); |
173 | sa4.sin_family = AF_INET; |
235 | sa4.sin_family = AF_INET; |
174 | sa4.sin_port = 0; |
236 | sa4.sin_port = 0; |
175 | #if IPV6 |
237 | #if ENABLE_IPV6 |
176 | memset (&sa6, 0, sizeof (sa6)); |
238 | memset (&sa6, 0, sizeof (sa6)); |
177 | sa6.sin6_family = AF_INET6; |
239 | sa6.sin6_family = AF_INET6; |
178 | sa6.sin6_port = 0; |
240 | sa6.sin6_port = 0; |
179 | #endif |
241 | #endif |
180 | |
242 | |
181 | for (;;) |
243 | for (;;) |
182 | { |
244 | { |
183 | REQ *req; |
245 | REQ *req; |
184 | int len = read (thr_send [0], &req, sizeof (req)); |
246 | int len = read (thr_send [0], &req, sizeof (req)); |
185 | |
247 | |
|
|
248 | tstamp now = NOW (); |
|
|
249 | tstamp next = now; |
|
|
250 | |
186 | if (!len) |
251 | if (!len) |
187 | pthread_exit (0); |
252 | pthread_exit (0); |
188 | else if (len != sizeof (req)) |
253 | else if (len != sizeof (req)) |
189 | { |
254 | { |
190 | perror ("AnyEvent::FastPing: short reead or read error"); |
255 | perror ("AnyEvent::FastPing: short read or read error"); |
191 | pthread_exit ((void *)-1); |
256 | pthread_exit ((void *)-1); |
192 | } |
257 | } |
193 | |
258 | |
194 | //TODO: bind to source address |
259 | //TODO: bind to source address |
195 | |
260 | |
196 | pkt.code = 0; |
261 | pkt.code = 0; |
197 | pkt.id = (uint16_t)magic; |
262 | pkt.id = (uint16_t)magic; |
198 | pkt.seq = (uint16_t)~magic; |
263 | pkt.seq = (uint16_t)~magic; |
199 | pkt.payload = req->payload; |
264 | pkt.payload = req->payload; |
200 | |
|
|
201 | tstamp now = NOW (); |
|
|
202 | tstamp next = now; |
|
|
203 | |
265 | |
204 | { |
266 | { |
205 | int r; |
267 | int r; |
206 | for (r = req->nranges; r--; ) |
268 | for (r = req->nranges; r--; ) |
207 | inc_addr (&req->ranges [r].hi); |
269 | inc_addr (&req->ranges [r].hi); |
208 | } |
270 | } |
209 | |
271 | |
210 | while (req->nranges) |
272 | while (req->nranges) |
211 | { |
273 | { |
212 | RANGE *range = req->ranges; |
274 | RANGE *range = req->ranges; |
|
|
275 | int n, k; |
213 | |
276 | |
214 | if (!memcmp (&range->lo, &range->hi, sizeof (addr_t))) |
277 | if (!memcmp (&range->lo, &range->hi, sizeof (addr_tt))) |
215 | req->ranges [0] = req->ranges [--req->nranges]; |
278 | req->ranges [0] = req->ranges [--req->nranges]; |
216 | else |
279 | else |
217 | { |
280 | { |
218 | // ranges [0] is always the next range to ping |
281 | // ranges [0] is always the next range to ping |
219 | tstamp wait = range->next - now; |
282 | tstamp wait = range->next - now; |
… | |
… | |
227 | else if (range) |
290 | else if (range) |
228 | next = range->next; |
291 | next = range->next; |
229 | } |
292 | } |
230 | |
293 | |
231 | if (wait > 0.) |
294 | if (wait > 0.) |
232 | { |
|
|
233 | struct timespec ts; |
|
|
234 | |
|
|
235 | ts.tv_sec = wait; |
|
|
236 | ts.tv_nsec = (wait - ts.tv_sec) * 1000000000.; |
|
|
237 | |
|
|
238 | nanosleep (&ts, 0); |
295 | ssleep (wait); |
239 | } |
|
|
240 | |
296 | |
241 | now = NOW (); |
297 | now = NOW (); |
242 | |
298 | |
243 | pkt.stamp = now; |
299 | ts_to_pkt (&pkt, now); |
244 | pkt.cksum = 0; |
300 | pkt.cksum = 0; |
245 | |
301 | |
246 | if (range->family == AF_INET) |
302 | if (range->family == AF_INET) |
247 | { |
303 | { |
248 | pkt.type = ICMP4_ECHO; |
304 | pkt.type = ICMP4_ECHO; |
249 | pkt.cksum = icmp_cksum (&pkt, sizeof (pkt)); |
305 | pkt.cksum = icmp_cksum (&pkt, sizeof (pkt)); |
250 | |
306 | |
251 | memcpy (&sa4.sin_addr, |
307 | memcpy (&sa4.sin_addr, |
252 | sizeof (addr_t) - sizeof (sa4.sin_addr) + (char *)&range->lo, |
308 | sizeof (addr_tt) - sizeof (sa4.sin_addr) + (char *)&range->lo, |
253 | sizeof (sa4.sin_addr)); |
309 | sizeof (sa4.sin_addr)); |
254 | |
310 | |
255 | 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) |
256 | errno = 0; |
312 | errno = 0; |
257 | } |
313 | } |
258 | else |
314 | else |
259 | { |
315 | { |
260 | #if IPV6 |
316 | #if ENABLE_IPV6 |
261 | pkt.type = ICMP6_ECHO; |
317 | pkt.type = ICMP6_ECHO; |
262 | |
318 | |
263 | memcpy (&sa6.sin6_addr, |
319 | memcpy (&sa6.sin6_addr, |
264 | sizeof (addr_t) - sizeof (sa6.sin6_addr) + (char *)&range->lo, |
320 | sizeof (addr_tt) - sizeof (sa6.sin6_addr) + (char *)&range->lo, |
265 | sizeof (sa6.sin6_addr)); |
321 | sizeof (sa6.sin6_addr)); |
266 | |
322 | |
267 | 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) |
268 | errno = 0; |
324 | errno = 0; |
269 | #endif |
325 | #endif |
270 | } |
326 | } |
271 | |
327 | |
272 | if (errno == ENOBUFS) |
328 | if (errno == ENOBUFS) |
273 | { |
329 | ssleep (DRAIN_INTERVAL); |
274 | struct timespec ts; |
|
|
275 | |
|
|
276 | ts.tv_sec = 0; |
|
|
277 | ts.tv_nsec = DRAIN_INTERVAL * 1000000000; |
|
|
278 | |
|
|
279 | nanosleep (&ts, 0); |
|
|
280 | } |
|
|
281 | else |
330 | else |
282 | { |
331 | { |
283 | inc_addr (&range->lo); |
332 | inc_addr (&range->lo); |
284 | |
333 | |
285 | range->next = next; |
334 | range->next = next; |
… | |
… | |
288 | |
337 | |
289 | next += req->interval; |
338 | next += req->interval; |
290 | } |
339 | } |
291 | |
340 | |
292 | // make a downheap operation |
341 | // make a downheap operation |
293 | int k = 0; |
|
|
294 | int n = 0; |
|
|
295 | for (;;) |
342 | for (n = k = 0; ; ) |
296 | { |
343 | { |
|
|
344 | int j = k * 2 + 1; |
|
|
345 | |
297 | ++n; |
346 | ++n; |
298 | int j = k * 2 + 1; |
|
|
299 | |
347 | |
300 | if (j >= req->nranges) |
348 | if (j >= req->nranges) |
301 | break; |
349 | break; |
302 | else if (j < req->nranges - 1) |
350 | else if (j < req->nranges - 1) |
303 | if (req->ranges [j].next > req->ranges [j + 1].next) |
351 | if (req->ranges [j].next > req->ranges [j + 1].next) |
304 | ++j; |
352 | ++j; |
305 | |
353 | |
306 | if (req->ranges [j].next >= req->ranges [k].next) |
354 | if (req->ranges [j].next >= req->ranges [k].next) |
307 | break; |
355 | break; |
308 | |
356 | |
|
|
357 | { |
309 | RANGE temp = req->ranges [k]; |
358 | RANGE temp = req->ranges [k]; |
310 | req->ranges [k] = req->ranges [j]; |
359 | req->ranges [k] = req->ranges [j]; |
311 | req->ranges [j] = temp; |
360 | req->ranges [j] = temp; |
|
|
361 | } |
312 | |
362 | |
313 | k = j; |
363 | k = j; |
314 | } |
364 | } |
315 | } |
365 | } |
316 | |
366 | |
… | |
… | |
321 | } |
371 | } |
322 | |
372 | |
323 | static void |
373 | static void |
324 | feed_reply (AV *res_av) |
374 | feed_reply (AV *res_av) |
325 | { |
375 | { |
326 | if (av_len (res_av) < 0) |
|
|
327 | return; |
|
|
328 | |
|
|
329 | dSP; |
376 | dSP; |
330 | SV *res = sv_2mortal (newRV_inc ((SV *)res_av)); |
377 | SV *res = sv_2mortal (newRV_inc ((SV *)res_av)); |
331 | int i; |
378 | int i; |
|
|
379 | |
|
|
380 | if (av_len (res_av) < 0) |
|
|
381 | return; |
332 | |
382 | |
333 | ENTER; |
383 | ENTER; |
334 | SAVETMPS; |
384 | SAVETMPS; |
335 | |
385 | |
336 | for (i = av_len (cbs) + 1; i--; ) |
386 | for (i = av_len (cbs) + 1; i--; ) |
… | |
… | |
358 | |
408 | |
359 | if (pipe (thr_recv) < 0) |
409 | if (pipe (thr_recv) < 0) |
360 | croak ("AnyEvent::FastPing: unable to create receive pipe"); |
410 | croak ("AnyEvent::FastPing: unable to create receive pipe"); |
361 | |
411 | |
362 | icmp4_fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); |
412 | icmp4_fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); |
|
|
413 | fcntl (icmp4_fd, F_SETFL, O_NONBLOCK); |
363 | #ifdef ICMP_FILTER |
414 | #ifdef ICMP_FILTER |
364 | { |
415 | { |
365 | struct icmp_filter oval; |
416 | struct icmp_filter oval; |
366 | oval.data = 0xffffffff & ~(1 << ICMP4_ECHO_REPLY); |
417 | oval.data = 0xffffffff & ~(1 << ICMP4_ECHO_REPLY); |
367 | setsockopt (icmp4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
418 | setsockopt (icmp4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
368 | } |
419 | } |
369 | #endif |
420 | #endif |
370 | |
421 | |
371 | #if IPV6 |
422 | #if ENABLE_IPV6 |
372 | icmp6_fd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
423 | icmp6_fd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
|
|
424 | fcntl (icmp6_fd, F_SETFL, O_NONBLOCK); |
373 | # ifdef ICMP6_FILTER |
425 | # ifdef ICMP6_FILTER |
374 | { |
426 | { |
375 | struct icmp6_filter oval; |
427 | struct icmp6_filter oval; |
376 | ICMP6_FILTER_SETBLOCKALL (&oval); |
428 | ICMP6_FILTER_SETBLOCKALL (&oval); |
377 | ICMP6_FILTER_SETPASS (ICMP6_ECHO_REPLY, &oval); |
429 | ICMP6_FILTER_SETPASS (ICMP6_ECHO_REPLY, &oval); |
… | |
… | |
409 | HV *stash = gv_stashpv ("AnyEvent::FastPing", 1); |
461 | HV *stash = gv_stashpv ("AnyEvent::FastPing", 1); |
410 | |
462 | |
411 | cbs = get_av ("AnyEvent::FastPing::CB", 1); |
463 | cbs = get_av ("AnyEvent::FastPing::CB", 1); |
412 | magic = getpid () ^ MAGIC; |
464 | magic = getpid () ^ MAGIC; |
413 | |
465 | |
|
|
466 | if (sizeof (PKT) & 3) |
|
|
467 | croak ("size of PKT structure is not a multiple of 4"); |
|
|
468 | |
414 | boot (); |
469 | boot (); |
415 | |
470 | |
416 | newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0)); |
471 | newCONSTSUB (stash, "ipv4_supported", newSViv (icmp4_fd >= 0)); |
417 | newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0)); |
472 | newCONSTSUB (stash, "ipv6_supported", newSViv (icmp6_fd >= 0)); |
418 | |
473 | |
… | |
… | |
424 | |
479 | |
425 | SV * |
480 | SV * |
426 | _req_icmp_ping (SV *ranges, NV interval, U32 payload, SV *id) |
481 | _req_icmp_ping (SV *ranges, NV interval, U32 payload, SV *id) |
427 | CODE: |
482 | CODE: |
428 | { |
483 | { |
|
|
484 | AV *rav; |
|
|
485 | int nranges, i; |
|
|
486 | REQ *req; |
|
|
487 | |
429 | if (!SvROK (ranges) || SvTYPE (SvRV (ranges)) != SVt_PVAV) |
488 | if (!SvROK (ranges) || SvTYPE (SvRV (ranges)) != SVt_PVAV) |
430 | croak ("address ranges must be given as arrayref with lo, hi pairs"); |
489 | croak ("address ranges must be given as arrayref with lo, hi pairs"); |
431 | |
490 | |
432 | AV *rav = (AV *)SvRV (ranges); |
491 | rav = (AV *)SvRV (ranges); |
433 | int nranges = av_len (rav) + 1; |
492 | nranges = av_len (rav) + 1; |
434 | |
493 | |
435 | REQ *req = malloc (sizeof (REQ)); |
494 | req = malloc (sizeof (REQ)); |
436 | int i; |
|
|
437 | |
495 | |
438 | if (interval < MIN_INTERVAL) |
496 | if (interval < MIN_INTERVAL) |
439 | interval = MIN_INTERVAL; |
497 | interval = MIN_INTERVAL; |
440 | |
498 | |
441 | req->id = newSVsv (id); |
499 | req->id = newSVsv (id); |
… | |
… | |
445 | req->ranges = (RANGE *)malloc (nranges * sizeof (RANGE)); |
503 | req->ranges = (RANGE *)malloc (nranges * sizeof (RANGE)); |
446 | |
504 | |
447 | while (nranges--) |
505 | while (nranges--) |
448 | { |
506 | { |
449 | SV *sv = *av_fetch (rav, nranges, 1); |
507 | SV *sv = *av_fetch (rav, nranges, 1); |
|
|
508 | SV *lo, *hi; |
|
|
509 | AV *av; |
|
|
510 | RANGE *r; |
450 | |
511 | |
451 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
512 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
452 | croak ("address range must be given as arrayref with lo, hi, interval arrayrefs"); |
513 | croak ("address range must be given as arrayref with lo, hi, interval arrayrefs"); |
453 | |
514 | |
454 | AV *av = (AV *)SvRV (sv); |
515 | av = (AV *)SvRV (sv); |
455 | RANGE *r = req->ranges + nranges; |
516 | r = req->ranges + nranges; |
456 | |
517 | |
457 | SV *lo = *av_fetch (av, 0, 1); |
518 | lo = *av_fetch (av, 0, 1); |
458 | SV *hi = *av_fetch (av, 1, 1); |
519 | hi = *av_fetch (av, 1, 1); |
459 | |
520 | |
460 | sv_utf8_downgrade (lo, 0); |
521 | sv_utf8_downgrade (lo, 0); |
461 | sv_utf8_downgrade (hi, 0); |
522 | sv_utf8_downgrade (hi, 0); |
462 | |
523 | |
463 | memset (&r->lo, 0, sizeof (addr_t)); |
524 | memset (&r->lo, 0, sizeof (addr_tt)); |
464 | memset (&r->hi, 0, sizeof (addr_t)); |
525 | memset (&r->hi, 0, sizeof (addr_tt)); |
465 | |
526 | |
466 | if (SvPOKp (lo) && SvPOKp (hi)) |
527 | if (SvPOKp (lo) && SvPOKp (hi)) |
467 | { |
528 | { |
468 | if (SvCUR (lo) != SvCUR (hi)) |
529 | if (SvCUR (lo) != SvCUR (hi)) |
469 | croak ("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)"); |
470 | |
531 | |
471 | if (SvCUR (lo) == 4) |
532 | if (SvCUR (lo) == 4) |
472 | { |
533 | { |
473 | r->family = AF_INET; |
534 | r->family = AF_INET; |
474 | memcpy (sizeof (addr_t) - 4 + (char *)&r->lo, SvPVX (lo), 4); |
535 | memcpy (sizeof (addr_tt) - 4 + (char *)&r->lo, SvPVX (lo), 4); |
475 | memcpy (sizeof (addr_t) - 4 + (char *)&r->hi, SvPVX (hi), 4); |
536 | memcpy (sizeof (addr_tt) - 4 + (char *)&r->hi, SvPVX (hi), 4); |
476 | } |
537 | } |
477 | else if (SvCUR (lo) == 16) |
538 | else if (SvCUR (lo) == 16) |
478 | { |
539 | { |
479 | #if IPV6 |
540 | #if ENABLE_IPV6 |
480 | r->family = AF_INET6; |
541 | r->family = AF_INET6; |
481 | memcpy (&r->lo, SvPVX (lo), sizeof (addr_t)); |
542 | memcpy (&r->lo, SvPVX (lo), sizeof (addr_tt)); |
482 | memcpy (&r->hi, SvPVX (hi), sizeof (addr_t)); |
543 | memcpy (&r->hi, SvPVX (hi), sizeof (addr_tt)); |
483 | #else |
544 | #else |
484 | croak ("IPv6 not supported in this configuration"); |
545 | croak ("IPv6 not supported in this configuration"); |
485 | #endif |
546 | #endif |
486 | } |
547 | } |
487 | else |
548 | else |
488 | croak ("addresses in range must be either 4 (IPv4) or 16 (IPV6) bytes in length"); |
549 | croak ("addresses in range must be either 4 (IPv4) or 16 (IPv6) bytes in length"); |
489 | } |
550 | } |
490 | else if (SvIOK (lo) && SvIOK (hi)) |
551 | else if (SvIOK (lo) && SvIOK (hi)) |
491 | { |
552 | { |
|
|
553 | uint32_t addr; |
|
|
554 | |
492 | r->family = AF_INET; |
555 | r->family = AF_INET; |
493 | |
556 | |
494 | uint32_t addr; |
|
|
495 | 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); |
496 | 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); |
497 | } |
559 | } |
498 | else |
560 | else |
499 | 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"); |
500 | |
562 | |
501 | if (r->family == AF_INET) |
563 | if (r->family == AF_INET) |
… | |
… | |
546 | struct sockaddr_in sa; |
608 | struct sockaddr_in sa; |
547 | socklen_t sl = sizeof (sa); |
609 | socklen_t sl = sizeof (sa); |
548 | 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 ()); |
549 | tstamp now = NOW (); |
611 | tstamp now = NOW (); |
550 | |
612 | |
|
|
613 | if (!res_av) |
|
|
614 | XSRETURN_UNDEF; |
|
|
615 | |
551 | for (;;) |
616 | for (;;) |
552 | { |
617 | { |
|
|
618 | IP4HDR *iphdr = (IP4HDR *)buf; |
553 | int len = recvfrom (icmp4_fd, buf, sizeof (buf), MSG_DONTWAIT | MSG_TRUNC, &sa, &sl); |
619 | int len = recvfrom (icmp4_fd, buf, sizeof (buf), MSG_TRUNC, (struct sockaddr *)&sa, &sl); |
|
|
620 | int hdrlen, totlen; |
|
|
621 | PKT *pkt; |
554 | |
622 | |
555 | if (len <= HDR_SIZE_IP4) |
623 | if (len <= HDR_SIZE_IP4) |
556 | break; |
624 | break; |
557 | |
625 | |
558 | IP4HDR *iphdr = (IP4HDR *)buf; |
|
|
559 | |
|
|
560 | int hdrlen = (iphdr->version_ihl & 15) * 4; |
626 | hdrlen = (iphdr->version_ihl & 15) * 4; |
561 | int totlen = ntohs (iphdr->tot_len); |
627 | totlen = ntohs (iphdr->tot_len); |
562 | |
628 | |
563 | // packet corrupt? |
629 | // packet corrupt? |
564 | if (!res_av |
|
|
565 | || totlen > len |
630 | if (totlen > len |
566 | || iphdr->protocol != IPPROTO_ICMP |
631 | || iphdr->protocol != IPPROTO_ICMP |
567 | || hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen) |
632 | || hdrlen < HDR_SIZE_IP4 || hdrlen + sizeof (PKT) != totlen) |
568 | continue; |
633 | continue; |
569 | |
634 | |
570 | PKT *pkt = (PKT *)(buf + hdrlen); |
635 | pkt = (PKT *)(buf + hdrlen); |
571 | |
636 | |
572 | if (pkt->type != ICMP4_ECHO_REPLY |
637 | if (pkt->type != ICMP4_ECHO_REPLY || !pkt_is_valid (pkt)) |
573 | || pkt->id != (uint16_t) magic |
|
|
574 | || pkt->seq != (uint16_t)~magic |
|
|
575 | || !isnormal (pkt->stamp)) |
|
|
576 | continue; |
638 | continue; |
577 | |
639 | |
|
|
640 | { |
578 | AV *av = newAV (); |
641 | AV *av = newAV (); |
579 | av_push (av, newSVpvn ((char *)&sa.sin_addr, 4)); |
642 | av_push (av, newSVpvn ((char *)&sa.sin_addr, 4)); |
580 | av_push (av, newSVnv (now - pkt->stamp)); |
643 | av_push (av, newSVnv (now - pkt_to_ts (pkt))); |
581 | av_push (av, newSVuv (pkt->payload)); |
644 | av_push (av, newSVuv (pkt->payload)); |
582 | |
645 | |
583 | av_push (res_av, newRV_noinc ((SV *)av)); |
646 | av_push (res_av, newRV_noinc ((SV *)av)); |
|
|
647 | } |
584 | } |
648 | } |
585 | |
649 | |
586 | if (res_av) |
|
|
587 | feed_reply (res_av); |
650 | feed_reply (res_av); |
588 | } |
651 | } |
589 | |
652 | |
590 | void |
653 | void |
591 | _recv_icmp6 (...) |
654 | _recv_icmp6 (...) |
592 | CODE: |
655 | CODE: |
… | |
… | |
595 | socklen_t sl = sizeof (sa); |
658 | socklen_t sl = sizeof (sa); |
596 | 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 ()); |
597 | PKT pkt; |
660 | PKT pkt; |
598 | tstamp now = NOW (); |
661 | tstamp now = NOW (); |
599 | |
662 | |
|
|
663 | if (!res_av) |
|
|
664 | XSRETURN_UNDEF; |
|
|
665 | |
600 | for (;;) |
666 | for (;;) |
601 | { |
667 | { |
602 | int len = recvfrom (icmp6_fd, &pkt, sizeof (pkt), MSG_DONTWAIT | MSG_TRUNC, &sa, &sl); |
668 | int len = recvfrom (icmp6_fd, &pkt, sizeof (pkt), MSG_TRUNC, (struct sockaddr *)&sa, &sl); |
603 | |
669 | |
604 | if (len != sizeof (PKT)) |
670 | if (len != sizeof (PKT)) |
605 | break; |
671 | break; |
606 | |
672 | |
607 | if (!res_av |
673 | if (pkt.type != ICMP6_ECHO_REPLY || !pkt_is_valid (&pkt)) |
608 | || pkt.type != ICMP6_ECHO_REPLY |
|
|
609 | || pkt.id != (uint16_t) magic |
|
|
610 | || pkt.seq != (uint16_t)~magic |
|
|
611 | || !isnormal (pkt.stamp)) |
|
|
612 | continue; |
674 | continue; |
613 | |
675 | |
|
|
676 | { |
614 | AV *av = newAV (); |
677 | AV *av = newAV (); |
615 | av_push (av, newSVpvn ((char *)&sa.sin6_addr, 16)); |
678 | av_push (av, newSVpvn ((char *)&sa.sin6_addr, 16)); |
616 | av_push (av, newSVnv (now - pkt.stamp)); |
679 | av_push (av, newSVnv (now - pkt_to_ts (&pkt))); |
617 | av_push (av, newSVuv (pkt.payload)); |
680 | av_push (av, newSVuv (pkt.payload)); |
618 | |
681 | |
619 | av_push (res_av, newRV_noinc ((SV *)av)); |
682 | av_push (res_av, newRV_noinc ((SV *)av)); |
|
|
683 | } |
620 | } |
684 | } |
621 | |
685 | |
622 | if (res_av) |
|
|
623 | feed_reply (res_av); |
686 | feed_reply (res_av); |
624 | } |
687 | } |
625 | |
688 | |