Fast.xs

filter: Statementstmt 0.0% 0 / 198 Branchbran 0.0% 0 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 272 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/Write/Fast.pm ↑ Summary
1 /*
2 * $Id: Fast.xs,v 3904d9e4e406 2016/02/03 17:35:46 gomor $
3 *
4 * COPYRIGHT AND LICENSE
5 *
6 * You may distribute this module under the terms of the Artistic license.
7 * See LICENSE.Artistic file in the source distribution archive.
8 *
9 * Copyright (c) 2011-2016, Patrice <GomoR> Auffret
10 *
11 */
12  
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
16  
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netdb.h>
25 #include <netinet/in.h>
26  
27 //#define DEBUG 1
28  
29 //#define TCP_LEN 24
30 //#define TCP_OPT_LEN 4
31 #define TCP_LEN 40
32 #define TCP_OPT_LEN 20
33 #define TCP_PHDR4_LEN 12
34 #define TCP_PHDR6_LEN 36
35  
36 #define MAXERRBUF 1024
37  
38 char nwf_errbuf[MAXERRBUF];
39 int nwf_isrand = 0;
40  
41 struct tcphdr {
42 u_int16_t th_sport;
43 u_int16_t th_dport;
44 u_int32_t th_seq;
45 u_int32_t th_ack;
46 u_int8_t th_x2:4, th_off:4;
47 u_int8_t th_flags;
48 u_int16_t th_win;
49 u_int16_t th_sum;
50 u_int16_t th_urp;
51 };
52  
53 struct ptcphdr4 {
54 in_addr_t ip_src;
55 in_addr_t ip_dst;
56 u_int16_t ip_p;
57 u_int16_t tcp_len;
58 struct tcphdr tcp_hdr;
59 u_int8_t tcp_opt[TCP_OPT_LEN];
60 };
61  
62 struct ptcphdr6 {
63 struct in6_addr ip_src;
64 struct in6_addr ip_dst;
65 u_int16_t ip_p;
66 u_int16_t tcp_len;
67 struct tcphdr tcp_hdr;
68 u_int8_t tcp_opt[TCP_OPT_LEN];
69 };
70  
71 u_int16_t
72 0 _nwf_csum(u_int16_t *buf, int nwords)
73 {
74 u_int32_t sum;
75  
76 0 ▶ for (sum = 0; nwords > 0; nwords--) {
Branch: 1 branch
BranchTrueFalseTotal
for (sum = 0; nwords > 0; nwords--) { 0 0 0
77 0 sum += *buf++;
78 }
79 0 sum = (sum >> 16) + (sum & 0xffff);
80 0 sum += (sum >> 16);
81  
82 0 return ~sum;
83 }
84  
85 int
86 0 _nwf_socket(int v6, struct addrinfo *asrc, int warnings)
87 {
88 int r;
89 int fd;
90 0 int one = 1;
91 0 const int* val = &one;
92  
93 0 ▶ fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_RAW, IPPROTO_TCP);
Branch: 1 branch
BranchTrueFalseTotal
fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_RAW, IPPROTO_TCP); 0 0 0
94 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
95 0 memset(nwf_errbuf, 0, MAXERRBUF);
96 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_socket: socket: %s (%d)",
97 0 strerror(errno), errno);
98 0 return(0);
99 }
100  
101 0 r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, val, sizeof(one));
102 0 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 0 0
103 0 memset(nwf_errbuf, 0, MAXERRBUF);
104 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_socket: setsockopt: %s (%d)",
105 0 strerror(errno), errno);
106 0 return(0);
107 }
108  
109 0 ▶ if (asrc != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (asrc != NULL) { 0 0 0
110 0 r = bind(fd, (const struct sockaddr *)asrc->ai_addr, asrc->ai_addrlen);
111 0 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 0 0
112 0 memset(nwf_errbuf, 0, MAXERRBUF);
113 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_socket: bind: %s (%d)",
114 0 strerror(errno), errno);
115 0 return(0);
116 }
117 }
118  
119 0 return(fd);
120 }
121  
122 int
123 0 _nwf_inet_addr(const char *ip)
124 {
125 in_addr_t a;
126  
127 0 a = inet_addr(ip);
128 0 ▶ if (a == INADDR_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (a == INADDR_NONE) { 0 0 0
129 0 memset(nwf_errbuf, 0, MAXERRBUF);
130 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_inet_addr: %s (%d) for [%s]",
131 0 strerror(errno), errno, ip);
132 0 return(0);
133 }
134  
135 0 return(a);
136 }
137  
138 int
139 0 _nwf_sendto(int sockfd, const void *buf, size_t len, int flags,
140 const struct sockaddr *dest_addr, socklen_t addrlen,
141 char *ip_dst)
142 {
143 int r;
144  
145 0 r = sendto(sockfd, buf, len, flags, dest_addr, addrlen);
146 0 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 0 0
147 0 memset(nwf_errbuf, 0, MAXERRBUF);
148 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_sendto: %s (%d) [to %s]",
149 0 strerror(errno), errno, ip_dst);
150 0 return(0);
151 }
152  
153 0 return(1);
154 }
155  
156 void *
157 0 _nwf_malloc(size_t size)
158 {
159 void *ptr;
160  
161 0 ptr = malloc(size);
162 0 ▶ if (ptr == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == NULL) { 0 0 0
163 0 memset(nwf_errbuf, 0, MAXERRBUF);
164 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_malloc: %s (%d)", strerror(errno), errno);
165 0 return(NULL);
166 }
167  
168 0 return(ptr);
169 }
170  
171 int
172 0 _nwf_getaddrinfo(const char *node, const char *service,
173 const struct addrinfo *hints, struct addrinfo **res)
174 {
175 int r;
176  
177 0 r = getaddrinfo(node, service, hints, res);
178 0 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 0 0
179 0 memset(nwf_errbuf, 0, MAXERRBUF);
180 0 snprintf(nwf_errbuf, MAXERRBUF - 1, "_nwf_getaddrinfo: %s [%s]",
181 gai_strerror(r), node);
182 0 return(0);
183 }
184  
185 0 return(1);
186 }
187  
188 int
189 0 l4_send_tcp_syn_multi(char *ip_src, char **ip_dst, int ndst, int *ports,
190 int nports, int pps, int n, int v6, int warnings)
191 {
192 int r;
193 int fd;
194 int i;
195 int j;
196 int k;
197 int nwords;
198 struct addrinfo hints;
199 struct addrinfo *asrc;
200 struct addrinfo *adst;
201 struct sockaddr_in *ptr4;
202 struct sockaddr_in6 *ptr6;
203 u_int8_t datagram[TCP_LEN];
204 u_int8_t *pdatagram;
205 #ifdef DEBUG
206 time_t begin;
207 time_t now;
208 #endif
209 int count;
210 int scount;
211 0 int npackets = 0; // Total number of packets
212 0 int runtime = 0; // Estimated running time
213 0 int every = 0; // Sleep every number of packets
214 0 int us = 0; // Sleep time in us (minimum 10ms, per classic OS)
215 struct ptcphdr4 *ptcph4;
216 struct ptcphdr6 *ptcph6;
217 struct tcphdr *tcph;
218  
219 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
220 0 pdatagram = (u_int8_t *)_nwf_malloc(TCP_LEN + TCP_PHDR4_LEN);
221 0 ▶ if (pdatagram == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pdatagram == NULL) 0 0 0
222 0 return(0);
223 0 ptcph4 = (struct ptcphdr4 *)pdatagram;
224 0 tcph = (struct tcphdr *) (pdatagram + TCP_PHDR4_LEN);
225 }
226 else {
227 0 pdatagram = (u_int8_t *)_nwf_malloc(TCP_LEN + TCP_PHDR6_LEN);
228 0 ▶ if (pdatagram == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pdatagram == NULL) 0 0 0
229 0 return(0);
230 0 ptcph6 = (struct ptcphdr6 *)pdatagram;
231 0 tcph = (struct tcphdr *) (pdatagram + TCP_PHDR6_LEN);
232 }
233  
234 0 ▶ if (nwf_isrand == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nwf_isrand == 0) { 0 0 0
235 0 srand(time(NULL));
236 0 nwf_isrand++;
237 }
238  
239 0 memset(&hints, 0, sizeof(hints));
240 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
241 0 hints.ai_family = AF_INET;
242 }
243 else {
244 0 hints.ai_family = AF_INET6;
245 }
246 0 hints.ai_flags = AI_NUMERICHOST;
247 //hints.ai_socktype = SOCK_RAW;
248 //hints.ai_protocol = IPPROTO_RAW;
249  
250 0 r = _nwf_getaddrinfo(ip_src, NULL, &hints, &asrc);
251 0 ▶ if (r == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) { 0 0 0
252 0 freeaddrinfo(asrc);
253 0 free(pdatagram);
254 0 return(0);
255 }
256  
257 0 memset(datagram, 0, TCP_LEN);
258 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
259 0 memset(pdatagram, 0, TCP_PHDR4_LEN + TCP_LEN);
260 }
261 else {
262 0 memset(pdatagram, 0, TCP_PHDR6_LEN + TCP_LEN);
263 }
264  
265 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
266 0 ptr4 = (struct sockaddr_in *)asrc->ai_addr;
267 0 memcpy(&(ptcph4->ip_src), &(ptr4->sin_addr), 4);
268 0 ptcph4->ip_p = ntohs(6);
269 0 ptcph4->tcp_len = ntohs(TCP_LEN);
270 0 ptcph4->tcp_hdr.th_ack = 0;
271 0 ptcph4->tcp_hdr.th_x2 = 0;
272 0 ptcph4->tcp_hdr.th_off = TCP_LEN >> 2;
273 0 ptcph4->tcp_hdr.th_flags = 0x02;
274 0 ptcph4->tcp_hdr.th_win = htons(5840);
275 0 ptcph4->tcp_hdr.th_sum = 0;
276 0 ptcph4->tcp_hdr.th_urp = 0;
277 // MSS 1460
278 //memcpy(ptcph4->tcp_opt, "\x02\x04\x05\xb4", TCP_OPT_LEN);
279 0 memcpy(ptcph4->tcp_opt, "\x02\x04\x05\xb4\x08\x0a\x44\x45\x41\x44\x00\x00\x00\x00\x03\x03\x01\x04\x02\x00", TCP_OPT_LEN);
280 }
281 else {
282 0 ptr6 = (struct sockaddr_in6 *)asrc->ai_addr;
283 0 memcpy(&(ptcph6->ip_src), &(ptr6->sin6_addr), 16);
284 0 ptcph6->ip_p = ntohs(6);
285 0 ptcph6->tcp_len = ntohs(TCP_LEN);
286 0 ptcph6->tcp_hdr.th_ack = 0;
287 0 ptcph6->tcp_hdr.th_x2 = 0;
288 0 ptcph6->tcp_hdr.th_off = TCP_LEN >> 2;
289 0 ptcph6->tcp_hdr.th_flags = 0x02;
290 0 ptcph6->tcp_hdr.th_win = htons(5840);
291 0 ptcph6->tcp_hdr.th_sum = 0;
292 0 ptcph6->tcp_hdr.th_urp = 0;
293 // MSS 1460
294 //memcpy(ptcph6->tcp_opt, "\x02\x04\x05\xb4", TCP_OPT_LEN);
295 0 memcpy(ptcph6->tcp_opt, "\x02\x04\x05\xb4\x08\x0a\x44\x45\x41\x44\x00\x00\x00\x00\x03\x03\x01\x04\x02\x00", TCP_OPT_LEN);
296 }
297  
298 0 fd = _nwf_socket(v6, asrc, warnings);
299 0 ▶ if (fd == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd == 0) { 0 0 0
300 0 freeaddrinfo(asrc);
301 0 free(pdatagram);
302 0 return(0);
303 }
304  
305 #ifdef DEBUG
306 begin = time(NULL);
307 #endif
308 0 count = 0;
309 0 scount = 0;
310 0 npackets = nports * ndst * n;
311 0 runtime = npackets / pps;
312 0 us = 10000; // Minimum delay 10ms, per classic OS restiction
313 0 every = ((float)pps / (float)us) * 100.00;
314 #ifdef DEBUG
315 fprintf(stderr,
316 "Using usleep of %d every %d packets during a runtime of %d seconds\n",
317 us, every, runtime);
318 #endif
319 0 ▶ for (i=0; i<nports; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<nports; i++) { 0 0 0
320 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
321 0 ptcph4->tcp_hdr.th_sport = htons(random());
322 0 ptcph4->tcp_hdr.th_dport = htons(ports[i]);
323 0 ptcph4->tcp_hdr.th_seq = random();
324 }
325 else {
326 0 ptcph6->tcp_hdr.th_sport = htons(random());
327 0 ptcph6->tcp_hdr.th_dport = htons(ports[i]);
328 0 ptcph6->tcp_hdr.th_seq = random();
329 }
330  
331 0 ▶ for (j=0; j<ndst; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j=0; j<ndst; j++) { 0 0 0
332 //printf("Target [%s]:%d [ipv6:%d]\n", ip_dst[j], ports[i], v6);
333  
334 0 r = _nwf_getaddrinfo(ip_dst[j], NULL, &hints, &adst);
335 0 ▶ if (r == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) { 0 0 0
336 0 freeaddrinfo(asrc);
337 0 freeaddrinfo(adst);
338 0 free(pdatagram);
339 0 return(0);
340 }
341  
342 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
343 0 ptr4 = (struct sockaddr_in *)adst->ai_addr;
344 0 memcpy(&(ptcph4->ip_dst), &(ptr4->sin_addr), sizeof(in_addr_t));
345 // Compute checksums
346 0 nwords = (TCP_LEN + TCP_PHDR4_LEN) * 8 / 16;
347 0 ptcph4->tcp_hdr.th_sum = _nwf_csum((u_int16_t *)ptcph4, nwords);
348 }
349 else {
350 0 ptr6 = (struct sockaddr_in6 *)adst->ai_addr;
351 0 memcpy(&(ptcph6->ip_dst), &(ptr6->sin6_addr), 16);
352 // Compute checksums
353 0 nwords = (TCP_LEN + TCP_PHDR6_LEN) * 8 / 16;
354 0 ptcph6->tcp_hdr.th_sum = _nwf_csum((u_int16_t *)ptcph6, nwords);
355 }
356  
357 0 memcpy(datagram, tcph, TCP_LEN);
358  
359 0 ▶ for (k=0; k<n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k<n; k++) { 0 0 0
360 0 RETRY:
361 0 r = _nwf_sendto(fd, (u_int8_t *)datagram, TCP_LEN, 0,
362 0 adst->ai_addr, adst->ai_addrlen,
363 0 ip_dst[j]);
364 0 ▶ if (r == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) { 0 0 0
365 0 ▶ if (errno == ENOBUFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == ENOBUFS) { 0 0 0
366 0 ▶ if (warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings) { 0 0 0
367 0 fprintf(stderr, "WARNING: Net::Write::Fast: sendto: ENOBUFS, sleeping for 1 second\n");
368 }
369 0 sleep(1);
370 0 goto RETRY;
371 }
372 else {
373 0 ▶ if (warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings) { 0 0 0
374 0 fprintf(stderr, "WARNING: Net::Write::Fast: sendto: %s\n", nwf_errbuf);
375 }
376 }
377 //freeaddrinfo(adst); // We still need it.
378 0 continue;
379 }
380 0 count++;
381 0 scount++;
382  
383 // Sleep every X packet
384 0 ▶ if (scount > every) {
Branch: 1 branch
BranchTrueFalseTotal
if (scount > every) { 0 0 0
385 0 usleep(us);
386 0 scount = 0;
387 }
388  
389 #ifdef DEBUG
390 // Print stats and reset count
391 now = time(NULL);
392 if (now - begin >= 1) {
393 fprintf(stderr, "Sent %d pps (i/o %d pps), time to sleep %d us (total: %d)\n",
394 count, pps, us, npackets);
395 begin = time(NULL);
396 count = 0;
397 }
398 #endif
399 }
400  
401 // Reset checksum for next round
402 0 ▶ if (! v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (! v6) { 0 0 0
403 0 ptcph4->tcp_hdr.th_sum = 0;
404 }
405 else {
406 0 ptcph6->tcp_hdr.th_sum = 0;
407 }
408 0 freeaddrinfo(adst);
409 }
410 }
411 0 freeaddrinfo(asrc);
412 0 free(pdatagram);
413  
414 0 close(fd);
415  
416 0 return(1);
417 }
418  
419 char *
420 0 nwf_geterror(void)
421 {
422 0 return((char *)nwf_errbuf);
423 }
424  
425 MODULE = Net::Write::Fast PACKAGE = Net::Write::Fast
426 PROTOTYPES: DISABLE
427  
428 int
429 l4_send_tcp_syn_multi(src, dst, ports, pps, n, v6, warnings = NO_INIT)
430 char *src
431 SV *dst
432 SV *ports
433 int pps
434 int n
435 int v6
436 int warnings
437 PREINIT:
438 0 ▶ if (!SvROK(ports) || SvTYPE((SV *)SvRV(ports)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ports) || SvTYPE((SV *)SvRV(ports)) != SVt_PVAV) { 0 0 0
if (!SvROK(ports) || SvTYPE((SV *)SvRV(ports)) != SVt_PVAV) { 0 0 0
439 0 croak("Argument ports shall be an ARRAYREF");
440 }
441 0 ▶ if (!SvROK(dst) || SvTYPE((SV *)SvRV(dst)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(dst) || SvTYPE((SV *)SvRV(dst)) != SVt_PVAV) { 0 0 0
if (!SvROK(dst) || SvTYPE((SV *)SvRV(dst)) != SVt_PVAV) { 0 0 0
442 0 croak("Argument dst shall be an ARRAYREF");
443 }
444 INIT:
445 int i;
446 0 AV *p = (AV *)SvRV(ports);
447 0 AV *d = (AV *)SvRV(dst);
448 0 int plen = av_len(p) + 1;
449 0 int dlen = av_len(d) + 1;
450 int *cports;
451 0 char *targets[dlen];
452 0 Newx(cports, plen, int);
453 CODE:
454 0 ▶ for (i=0; i<plen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<plen; i++) { 0 0 0
455 0 SV **e = av_fetch(p, i, 0);
456 0 cports[i] = SvIV(*e);
457 }
458 0 ▶ for (i=0; i<dlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<dlen; i++) { 0 0 0
459 STRLEN l;
460 0 targets[i] = SvPV(*av_fetch(d, i, 0), l);
461 }
462 0 RETVAL = l4_send_tcp_syn_multi(src, targets, dlen, cports, plen, pps, n,
463 v6, warnings);
464 OUTPUT:
465 RETVAL
466  
467 char *
468 nwf_geterror()
« blib/lib/Net/Write/Fast.pm ↑ Summary