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