1 // SPDX-License-Identifier: GPL-2.0
2
3 #define _GNU_SOURCE
4
5 #include <errno.h>
6 #include <limits.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <strings.h>
15 #include <signal.h>
16 #include <unistd.h>
17 #include <time.h>
18
19 #include <sys/ioctl.h>
20 #include <sys/poll.h>
21 #include <sys/random.h>
22 #include <sys/sendfile.h>
23 #include <sys/stat.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/mman.h>
27
28 #include <arpa/inet.h>
29
30 #include <netdb.h>
31 #include <netinet/in.h>
32
33 #include <linux/tcp.h>
34 #include <linux/time_types.h>
35 #include <linux/sockios.h>
36 #include <linux/compiler.h>
37
38 extern int optind;
39
40 #ifndef IPPROTO_MPTCP
41 #define IPPROTO_MPTCP 262
42 #endif
43 #ifndef TCP_ULP
44 #define TCP_ULP 31
45 #endif
46
47 static int poll_timeout = 10 * 1000;
48 static bool listen_mode;
49 static bool quit;
50
51 enum cfg_mode {
52 CFG_MODE_POLL,
53 CFG_MODE_MMAP,
54 CFG_MODE_SENDFILE,
55 CFG_MODE_SPLICE,
56 };
57
58 enum cfg_peek {
59 CFG_NONE_PEEK,
60 CFG_WITH_PEEK,
61 CFG_AFTER_PEEK,
62 };
63
64 static enum cfg_mode cfg_mode = CFG_MODE_POLL;
65 static enum cfg_peek cfg_peek = CFG_NONE_PEEK;
66 static const char *cfg_host;
67 static const char *cfg_port = "12000";
68 static int cfg_sock_proto = IPPROTO_MPTCP;
69 static int pf = AF_INET;
70 static int cfg_sndbuf;
71 static int cfg_rcvbuf;
72 static bool cfg_join;
73 static bool cfg_remove;
74 static unsigned int cfg_time;
75 static unsigned int cfg_do_w;
76 static int cfg_wait;
77 static uint32_t cfg_mark;
78 static char *cfg_input;
79 static int cfg_repeat = 1;
80 static int cfg_truncate;
81 static int cfg_rcv_trunc;
82
83 struct cfg_cmsg_types {
84 unsigned int cmsg_enabled:1;
85 unsigned int timestampns:1;
86 unsigned int tcp_inq:1;
87 };
88
89 struct cfg_sockopt_types {
90 unsigned int transparent:1;
91 unsigned int mptfo:1;
92 };
93
94 struct tcp_inq_state {
95 unsigned int last;
96 bool expect_eof;
97 };
98
99 struct wstate {
100 char buf[8192];
101 unsigned int len;
102 unsigned int off;
103 unsigned int total_len;
104 };
105
106 static struct tcp_inq_state tcp_inq;
107
108 static struct cfg_cmsg_types cfg_cmsg_types;
109 static struct cfg_sockopt_types cfg_sockopt_types;
110
die_usage(void)111 static void die_usage(void)
112 {
113 fprintf(stderr, "Usage: mptcp_connect [-6] [-c cmsg] [-f offset] [-i file] [-I num] [-j] [-l] "
114 "[-m mode] [-M mark] [-o option] [-p port] [-P mode] [-r num] [-R num] "
115 "[-s MPTCP|TCP] [-S num] [-t num] [-T num] [-w sec] connect_address\n");
116 fprintf(stderr, "\t-6 use ipv6\n");
117 fprintf(stderr, "\t-c cmsg -- test cmsg type <cmsg>\n");
118 fprintf(stderr, "\t-f offset -- stop the I/O after receiving and sending the specified amount "
119 "of bytes. If there are unread bytes in the receive queue, that will cause a MPTCP "
120 "fastclose at close/shutdown. If offset is negative, expect the peer to close before "
121 "all the local data as been sent, thus toleration errors on write and EPIPE signals\n");
122 fprintf(stderr, "\t-i file -- read the data to send from the given file instead of stdin");
123 fprintf(stderr, "\t-I num -- repeat the transfer 'num' times. In listen mode accepts num "
124 "incoming connections, in client mode, disconnect and reconnect to the server\n");
125 fprintf(stderr, "\t-j -- add additional sleep at connection start and tear down "
126 "-- for MPJ tests\n");
127 fprintf(stderr, "\t-l -- listens mode, accepts incoming connection\n");
128 fprintf(stderr, "\t-m [poll|mmap|sendfile|splice] -- use poll(default)/mmap+write/sendfile/splice\n");
129 fprintf(stderr, "\t-M mark -- set socket packet mark\n");
130 fprintf(stderr, "\t-o option -- test sockopt <option>\n");
131 fprintf(stderr, "\t-p num -- use port num\n");
132 fprintf(stderr,
133 "\t-P [saveWithPeek|saveAfterPeek] -- save data with/after MSG_PEEK form tcp socket\n");
134 fprintf(stderr, "\t-r num -- enable slow mode, limiting each write to num bytes "
135 "-- for remove addr tests\n");
136 fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n");
137 fprintf(stderr, "\t-s [MPTCP|TCP] -- use mptcp(default) or tcp sockets\n");
138 fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n");
139 fprintf(stderr, "\t-t num -- set poll timeout to num\n");
140 fprintf(stderr, "\t-T num -- set expected runtime to num ms\n");
141 fprintf(stderr, "\t-w num -- wait num sec before closing the socket\n");
142 exit(1);
143 }
144
xerror(const char * fmt,...)145 static void __noreturn xerror(const char *fmt, ...)
146 {
147 va_list ap;
148
149 va_start(ap, fmt);
150 vfprintf(stderr, fmt, ap);
151 va_end(ap);
152 exit(1);
153 }
154
handle_signal(int nr)155 static void handle_signal(int nr)
156 {
157 quit = true;
158 }
159
getxinfo_strerr(int err)160 static const char *getxinfo_strerr(int err)
161 {
162 if (err == EAI_SYSTEM)
163 return strerror(errno);
164
165 return gai_strerror(err);
166 }
167
xgetnameinfo(const struct sockaddr * addr,socklen_t addrlen,char * host,socklen_t hostlen,char * serv,socklen_t servlen)168 static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
169 char *host, socklen_t hostlen,
170 char *serv, socklen_t servlen)
171 {
172 int flags = NI_NUMERICHOST | NI_NUMERICSERV;
173 int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen,
174 flags);
175
176 if (err) {
177 const char *errstr = getxinfo_strerr(err);
178
179 fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
180 exit(1);
181 }
182 }
183
xgetaddrinfo(const char * node,const char * service,struct addrinfo * hints,struct addrinfo ** res)184 static void xgetaddrinfo(const char *node, const char *service,
185 struct addrinfo *hints,
186 struct addrinfo **res)
187 {
188 int err;
189
190 again:
191 err = getaddrinfo(node, service, hints, res);
192 if (err) {
193 const char *errstr;
194
195 /* glibc starts to support MPTCP since v2.42.
196 * For older versions, use IPPROTO_TCP to resolve,
197 * and use TCP/MPTCP to create socket.
198 * Link: https://sourceware.org/git/?p=glibc.git;a=commit;h=a8e9022e0f82
199 */
200 if (err == EAI_SOCKTYPE) {
201 hints->ai_protocol = IPPROTO_TCP;
202 goto again;
203 }
204
205 errstr = getxinfo_strerr(err);
206
207 fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
208 node ? node : "", service ? service : "", errstr);
209 exit(1);
210 }
211 }
212
set_rcvbuf(int fd,unsigned int size)213 static void set_rcvbuf(int fd, unsigned int size)
214 {
215 int err;
216
217 err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
218 if (err) {
219 perror("set SO_RCVBUF");
220 exit(1);
221 }
222 }
223
set_sndbuf(int fd,unsigned int size)224 static void set_sndbuf(int fd, unsigned int size)
225 {
226 int err;
227
228 err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
229 if (err) {
230 perror("set SO_SNDBUF");
231 exit(1);
232 }
233 }
234
set_mark(int fd,uint32_t mark)235 static void set_mark(int fd, uint32_t mark)
236 {
237 int err;
238
239 err = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
240 if (err) {
241 perror("set SO_MARK");
242 exit(1);
243 }
244 }
245
set_transparent(int fd,int pf)246 static void set_transparent(int fd, int pf)
247 {
248 int one = 1;
249
250 switch (pf) {
251 case AF_INET:
252 if (-1 == setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)))
253 perror("IP_TRANSPARENT");
254 break;
255 case AF_INET6:
256 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)))
257 perror("IPV6_TRANSPARENT");
258 break;
259 }
260 }
261
set_mptfo(int fd)262 static void set_mptfo(int fd)
263 {
264 int qlen = 25;
265
266 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1)
267 perror("TCP_FASTOPEN");
268 }
269
do_ulp_so(int sock,const char * name)270 static int do_ulp_so(int sock, const char *name)
271 {
272 return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name));
273 }
274
275 #define X(m) xerror("%s:%u: %s: failed for proto %d at line %u", __FILE__, __LINE__, (m), proto, line)
sock_test_tcpulp(int sock,int proto,unsigned int line)276 static void sock_test_tcpulp(int sock, int proto, unsigned int line)
277 {
278 socklen_t buflen = 8;
279 char buf[8] = "";
280 int ret = getsockopt(sock, IPPROTO_TCP, TCP_ULP, buf, &buflen);
281
282 if (ret != 0)
283 X("getsockopt");
284
285 if (buflen > 0) {
286 if (strcmp(buf, "mptcp") != 0)
287 xerror("unexpected ULP '%s' for proto %d at line %u", buf, proto, line);
288 ret = do_ulp_so(sock, "tls");
289 if (ret == 0)
290 X("setsockopt");
291 } else if (proto == IPPROTO_MPTCP) {
292 ret = do_ulp_so(sock, "tls");
293 if (ret != -1)
294 X("setsockopt");
295 }
296
297 ret = do_ulp_so(sock, "mptcp");
298 if (ret != -1)
299 X("setsockopt");
300
301 #undef X
302 }
303
304 #define SOCK_TEST_TCPULP(s, p) sock_test_tcpulp((s), (p), __LINE__)
305
sock_listen_mptcp(const char * const listenaddr,const char * const port)306 static int sock_listen_mptcp(const char * const listenaddr,
307 const char * const port)
308 {
309 int sock = -1;
310 struct addrinfo hints = {
311 .ai_protocol = IPPROTO_MPTCP,
312 .ai_socktype = SOCK_STREAM,
313 .ai_flags = AI_PASSIVE | AI_NUMERICHOST
314 };
315
316 hints.ai_family = pf;
317
318 struct addrinfo *a, *addr;
319 int one = 1;
320
321 xgetaddrinfo(listenaddr, port, &hints, &addr);
322 hints.ai_family = pf;
323
324 for (a = addr; a; a = a->ai_next) {
325 sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
326 if (sock < 0)
327 continue;
328
329 SOCK_TEST_TCPULP(sock, cfg_sock_proto);
330
331 if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
332 sizeof(one)))
333 perror("setsockopt");
334
335 if (cfg_sockopt_types.transparent)
336 set_transparent(sock, pf);
337
338 if (cfg_sockopt_types.mptfo)
339 set_mptfo(sock);
340
341 if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
342 break; /* success */
343
344 perror("bind");
345 close(sock);
346 sock = -1;
347 }
348
349 freeaddrinfo(addr);
350
351 if (sock < 0) {
352 fprintf(stderr, "Could not create listen socket\n");
353 return sock;
354 }
355
356 SOCK_TEST_TCPULP(sock, cfg_sock_proto);
357
358 if (listen(sock, 20)) {
359 perror("listen");
360 close(sock);
361 return -1;
362 }
363
364 SOCK_TEST_TCPULP(sock, cfg_sock_proto);
365
366 return sock;
367 }
368
sock_connect_mptcp(const char * const remoteaddr,const char * const port,int proto,struct addrinfo ** peer,int infd,struct wstate * winfo)369 static int sock_connect_mptcp(const char * const remoteaddr,
370 const char * const port, int proto,
371 struct addrinfo **peer,
372 int infd, struct wstate *winfo)
373 {
374 struct addrinfo hints = {
375 .ai_protocol = IPPROTO_MPTCP,
376 .ai_socktype = SOCK_STREAM,
377 };
378 struct addrinfo *a, *addr;
379 int syn_copied = 0;
380 int sock = -1;
381
382 hints.ai_family = pf;
383
384 /* Keep the resolved address alive for the whole execution: it is
385 * used again when reconnecting, and will be released at exit time.
386 */
387 xgetaddrinfo(remoteaddr, port, &hints, &addr);
388 for (a = addr; a; a = a->ai_next) {
389 sock = socket(a->ai_family, a->ai_socktype, proto);
390 if (sock < 0) {
391 perror("socket");
392 continue;
393 }
394
395 SOCK_TEST_TCPULP(sock, proto);
396
397 if (cfg_mark)
398 set_mark(sock, cfg_mark);
399
400 if (cfg_sockopt_types.mptfo) {
401 if (!winfo->total_len)
402 winfo->total_len = winfo->len = read(infd, winfo->buf,
403 sizeof(winfo->buf));
404
405 syn_copied = sendto(sock, winfo->buf, winfo->len, MSG_FASTOPEN,
406 a->ai_addr, a->ai_addrlen);
407 if (syn_copied >= 0) {
408 winfo->off = syn_copied;
409 winfo->len -= syn_copied;
410 *peer = a;
411 break; /* success */
412 }
413 perror("sendto()");
414 } else {
415 if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) {
416 *peer = a;
417 break; /* success */
418 }
419 perror("connect()");
420 }
421
422 /* error */
423 close(sock);
424 sock = -1;
425 }
426
427 if (sock != -1)
428 SOCK_TEST_TCPULP(sock, proto);
429 return sock;
430 }
431
do_rnd_write(const int fd,char * buf,const size_t len)432 static size_t do_rnd_write(const int fd, char *buf, const size_t len)
433 {
434 static bool first = true;
435 unsigned int do_w;
436 ssize_t bw;
437
438 do_w = rand() & 0xffff;
439 if (do_w == 0 || do_w > len)
440 do_w = len;
441
442 if (cfg_join && first && do_w > 100)
443 do_w = 100;
444
445 if (cfg_remove && do_w > cfg_do_w)
446 do_w = cfg_do_w;
447
448 bw = write(fd, buf, do_w);
449 if (bw < 0)
450 return bw;
451
452 /* let the join handshake complete, before going on */
453 if (cfg_join && first) {
454 usleep(200000);
455 first = false;
456 }
457
458 if (cfg_remove)
459 usleep(200000);
460
461 return bw;
462 }
463
do_write(const int fd,char * buf,const size_t len)464 static size_t do_write(const int fd, char *buf, const size_t len)
465 {
466 size_t offset = 0;
467
468 while (offset < len) {
469 size_t written;
470 ssize_t bw;
471
472 bw = write(fd, buf + offset, len - offset);
473 if (bw < 0) {
474 perror("write");
475 return 0;
476 }
477
478 written = (size_t)bw;
479 offset += written;
480 }
481
482 return offset;
483 }
484
process_cmsg(struct msghdr * msgh)485 static void process_cmsg(struct msghdr *msgh)
486 {
487 struct __kernel_timespec ts;
488 bool inq_found = false;
489 bool ts_found = false;
490 unsigned int inq = 0;
491 struct cmsghdr *cmsg;
492
493 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
494 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) {
495 memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
496 ts_found = true;
497 continue;
498 }
499 if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) {
500 memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq));
501 inq_found = true;
502 continue;
503 }
504
505 }
506
507 if (cfg_cmsg_types.timestampns) {
508 if (!ts_found)
509 xerror("TIMESTAMPNS not present\n");
510 }
511
512 if (cfg_cmsg_types.tcp_inq) {
513 if (!inq_found)
514 xerror("TCP_INQ not present\n");
515
516 if (inq > 1024)
517 xerror("tcp_inq %u is larger than one kbyte\n", inq);
518 tcp_inq.last = inq;
519 }
520 }
521
do_recvmsg_cmsg(const int fd,char * buf,const size_t len)522 static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
523 {
524 char msg_buf[8192];
525 struct iovec iov = {
526 .iov_base = buf,
527 .iov_len = len,
528 };
529 struct msghdr msg = {
530 .msg_iov = &iov,
531 .msg_iovlen = 1,
532 .msg_control = msg_buf,
533 .msg_controllen = sizeof(msg_buf),
534 };
535 int flags = 0;
536 unsigned int last_hint = tcp_inq.last;
537 int ret = recvmsg(fd, &msg, flags);
538
539 if (ret <= 0) {
540 if (ret == 0 && tcp_inq.expect_eof)
541 return ret;
542
543 if (ret == 0 && cfg_cmsg_types.tcp_inq)
544 if (last_hint != 1 && last_hint != 0)
545 xerror("EOF but last tcp_inq hint was %u\n", last_hint);
546
547 return ret;
548 }
549
550 if (tcp_inq.expect_eof)
551 xerror("expected EOF, last_hint %u, now %u\n",
552 last_hint, tcp_inq.last);
553
554 if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled)
555 xerror("got %lu bytes of cmsg data, expected 0\n",
556 (unsigned long)msg.msg_controllen);
557
558 if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled)
559 xerror("%s\n", "got no cmsg data");
560
561 if (msg.msg_controllen)
562 process_cmsg(&msg);
563
564 if (cfg_cmsg_types.tcp_inq) {
565 if ((size_t)ret < len && last_hint > (unsigned int)ret) {
566 if (ret + 1 != (int)last_hint) {
567 int next = read(fd, msg_buf, sizeof(msg_buf));
568
569 xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n",
570 ret, (unsigned int)len, last_hint, tcp_inq.last, next);
571 } else {
572 tcp_inq.expect_eof = true;
573 }
574 }
575 }
576
577 return ret;
578 }
579
do_rnd_read(const int fd,char * buf,const size_t len)580 static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
581 {
582 int ret = 0;
583 char tmp[16384];
584 size_t cap = rand();
585
586 cap &= 0xffff;
587
588 if (cap == 0)
589 cap = 1;
590 else if (cap > len)
591 cap = len;
592
593 if (cfg_peek == CFG_WITH_PEEK) {
594 ret = recv(fd, buf, cap, MSG_PEEK);
595 ret = (ret < 0) ? ret : read(fd, tmp, ret);
596 } else if (cfg_peek == CFG_AFTER_PEEK) {
597 ret = recv(fd, buf, cap, MSG_PEEK);
598 ret = (ret < 0) ? ret : read(fd, buf, cap);
599 } else if (cfg_cmsg_types.cmsg_enabled) {
600 ret = do_recvmsg_cmsg(fd, buf, cap);
601 } else {
602 ret = read(fd, buf, cap);
603 }
604
605 return ret;
606 }
607
set_nonblock(int fd,bool nonblock)608 static void set_nonblock(int fd, bool nonblock)
609 {
610 int flags = fcntl(fd, F_GETFL);
611
612 if (flags == -1)
613 return;
614
615 if (nonblock)
616 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
617 else
618 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
619 }
620
shut_wr(int fd)621 static void shut_wr(int fd)
622 {
623 /* Close our write side, ev. give some time
624 * for address notification and/or checking
625 * the current status
626 */
627 if (cfg_wait)
628 usleep(cfg_wait);
629
630 shutdown(fd, SHUT_WR);
631 }
632
copyfd_io_poll(int infd,int peerfd,int outfd,bool * in_closed_after_out,struct wstate * winfo)633 static int copyfd_io_poll(int infd, int peerfd, int outfd,
634 bool *in_closed_after_out, struct wstate *winfo)
635 {
636 struct pollfd fds = {
637 .fd = peerfd,
638 .events = POLLIN | POLLOUT,
639 };
640 unsigned int total_wlen = 0, total_rlen = 0;
641
642 set_nonblock(peerfd, true);
643
644 for (;;) {
645 char rbuf[8192];
646 ssize_t len;
647
648 if (fds.events == 0 || quit)
649 break;
650
651 switch (poll(&fds, 1, poll_timeout)) {
652 case -1:
653 if (errno == EINTR)
654 continue;
655 perror("poll");
656 return 1;
657 case 0:
658 fprintf(stderr, "%s: poll timed out (events: "
659 "POLLIN %u, POLLOUT %u)\n", __func__,
660 fds.events & POLLIN, fds.events & POLLOUT);
661 return 2;
662 }
663
664 if (fds.revents & POLLIN) {
665 ssize_t rb = sizeof(rbuf);
666
667 /* limit the total amount of read data to the trunc value*/
668 if (cfg_truncate > 0) {
669 if (rb + total_rlen > cfg_truncate)
670 rb = cfg_truncate - total_rlen;
671 len = read(peerfd, rbuf, rb);
672 } else {
673 len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
674 }
675 if (len == 0) {
676 /* no more data to receive:
677 * peer has closed its write side
678 */
679 fds.events &= ~POLLIN;
680
681 if ((fds.events & POLLOUT) == 0) {
682 *in_closed_after_out = true;
683 /* and nothing more to send */
684 break;
685 }
686
687 /* Else, still have data to transmit */
688 } else if (len < 0) {
689 if (cfg_rcv_trunc)
690 return 0;
691 perror("read");
692 return 3;
693 }
694
695 total_rlen += len;
696 do_write(outfd, rbuf, len);
697 }
698
699 if (fds.revents & POLLOUT) {
700 if (winfo->len == 0) {
701 winfo->off = 0;
702 winfo->len = read(infd, winfo->buf, sizeof(winfo->buf));
703 }
704
705 if (winfo->len > 0) {
706 ssize_t bw;
707
708 /* limit the total amount of written data to the trunc value */
709 if (cfg_truncate > 0 && winfo->len + total_wlen > cfg_truncate)
710 winfo->len = cfg_truncate - total_wlen;
711
712 bw = do_rnd_write(peerfd, winfo->buf + winfo->off, winfo->len);
713 if (bw < 0) {
714 /* expected reset, continue to read */
715 if (cfg_rcv_trunc &&
716 (errno == ECONNRESET ||
717 errno == EPIPE)) {
718 fds.events &= ~POLLOUT;
719 continue;
720 }
721
722 perror("write");
723 return 111;
724 }
725
726 winfo->off += bw;
727 winfo->len -= bw;
728 total_wlen += bw;
729 } else if (winfo->len == 0) {
730 /* We have no more data to send. */
731 fds.events &= ~POLLOUT;
732
733 if ((fds.events & POLLIN) == 0)
734 /* ... and peer also closed already */
735 break;
736
737 shut_wr(peerfd);
738 } else {
739 if (errno == EINTR)
740 continue;
741 perror("read");
742 return 4;
743 }
744 }
745
746 if (fds.revents & (POLLERR | POLLNVAL)) {
747 if (cfg_rcv_trunc) {
748 fds.events &= ~(POLLERR | POLLNVAL);
749 continue;
750 }
751 fprintf(stderr, "Unexpected revents: "
752 "POLLERR/POLLNVAL(%x)\n", fds.revents);
753 return 5;
754 }
755
756 if (cfg_truncate > 0 && total_wlen >= cfg_truncate &&
757 total_rlen >= cfg_truncate)
758 break;
759 }
760
761 /* leave some time for late join/announce */
762 if (cfg_remove && !quit)
763 usleep(cfg_wait);
764
765 return 0;
766 }
767
do_recvfile(int infd,int outfd)768 static int do_recvfile(int infd, int outfd)
769 {
770 ssize_t r;
771
772 do {
773 char buf[16384];
774
775 r = do_rnd_read(infd, buf, sizeof(buf));
776 if (r > 0) {
777 if (write(outfd, buf, r) != r)
778 break;
779 } else if (r < 0) {
780 perror("read");
781 }
782 } while (r > 0);
783
784 return (int)r;
785 }
786
spool_buf(int fd,struct wstate * winfo)787 static int spool_buf(int fd, struct wstate *winfo)
788 {
789 while (winfo->len) {
790 int ret = write(fd, winfo->buf + winfo->off, winfo->len);
791
792 if (ret < 0) {
793 perror("write");
794 return 4;
795 }
796 winfo->off += ret;
797 winfo->len -= ret;
798 }
799 return 0;
800 }
801
do_mmap(int infd,int outfd,unsigned int size,struct wstate * winfo)802 static int do_mmap(int infd, int outfd, unsigned int size,
803 struct wstate *winfo)
804 {
805 char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
806 ssize_t ret = 0, off = winfo->total_len;
807 size_t rem;
808
809 if (inbuf == MAP_FAILED) {
810 perror("mmap");
811 return 1;
812 }
813
814 ret = spool_buf(outfd, winfo);
815 if (ret < 0)
816 return ret;
817
818 rem = size - winfo->total_len;
819
820 while (rem > 0) {
821 ret = write(outfd, inbuf + off, rem);
822
823 if (ret < 0) {
824 perror("write");
825 break;
826 }
827
828 off += ret;
829 rem -= ret;
830 }
831
832 munmap(inbuf, size);
833 return rem;
834 }
835
get_infd_size(int fd)836 static int get_infd_size(int fd)
837 {
838 struct stat sb;
839 ssize_t count;
840 int err;
841
842 err = fstat(fd, &sb);
843 if (err < 0) {
844 perror("fstat");
845 return -1;
846 }
847
848 if ((sb.st_mode & S_IFMT) != S_IFREG) {
849 fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
850 return -2;
851 }
852
853 count = sb.st_size;
854 if (count > INT_MAX) {
855 fprintf(stderr, "File too large: %zu\n", count);
856 return -3;
857 }
858
859 return (int)count;
860 }
861
do_sendfile(int infd,int outfd,unsigned int count,struct wstate * winfo)862 static int do_sendfile(int infd, int outfd, unsigned int count,
863 struct wstate *winfo)
864 {
865 int ret = spool_buf(outfd, winfo);
866
867 if (ret < 0)
868 return ret;
869
870 count -= winfo->total_len;
871
872 while (count > 0) {
873 ssize_t r;
874
875 r = sendfile(outfd, infd, NULL, count);
876 if (r < 0) {
877 perror("sendfile");
878 return 3;
879 }
880
881 count -= r;
882 }
883
884 return 0;
885 }
886
copyfd_io_mmap(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)887 static int copyfd_io_mmap(int infd, int peerfd, int outfd,
888 unsigned int size, bool *in_closed_after_out,
889 struct wstate *winfo)
890 {
891 int err;
892
893 if (listen_mode) {
894 err = do_recvfile(peerfd, outfd);
895 if (err)
896 return err;
897
898 err = do_mmap(infd, peerfd, size, winfo);
899 } else {
900 err = do_mmap(infd, peerfd, size, winfo);
901 if (err)
902 return err;
903
904 shut_wr(peerfd);
905
906 err = do_recvfile(peerfd, outfd);
907 *in_closed_after_out = true;
908 }
909
910 return err;
911 }
912
copyfd_io_sendfile(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)913 static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
914 unsigned int size, bool *in_closed_after_out, struct wstate *winfo)
915 {
916 int err;
917
918 if (listen_mode) {
919 err = do_recvfile(peerfd, outfd);
920 if (err)
921 return err;
922
923 err = do_sendfile(infd, peerfd, size, winfo);
924 } else {
925 err = do_sendfile(infd, peerfd, size, winfo);
926 if (err)
927 return err;
928
929 shut_wr(peerfd);
930
931 err = do_recvfile(peerfd, outfd);
932 *in_closed_after_out = true;
933 }
934
935 return err;
936 }
937
do_splice(const int infd,const int outfd,const size_t len,struct wstate * winfo)938 static int do_splice(const int infd, const int outfd, const size_t len,
939 struct wstate *winfo)
940 {
941 ssize_t in_bytes, out_bytes;
942 int pipefd[2];
943 int err;
944
945 err = pipe(pipefd);
946 if (err) {
947 perror("pipe");
948 return 2;
949 }
950
951 again:
952 in_bytes = splice(infd, NULL, pipefd[1], NULL, len - winfo->total_len,
953 SPLICE_F_MOVE | SPLICE_F_MORE);
954 if (in_bytes < 0) {
955 perror("splice in");
956 err = 3;
957 } else if (in_bytes > 0) {
958 out_bytes = splice(pipefd[0], NULL, outfd, NULL, in_bytes,
959 SPLICE_F_MOVE | SPLICE_F_MORE);
960 if (out_bytes < 0) {
961 perror("splice out");
962 err = 4;
963 } else if (in_bytes != out_bytes) {
964 fprintf(stderr, "Unexpected transfer: %zu vs %zu\n",
965 in_bytes, out_bytes);
966 err = 5;
967 } else {
968 goto again;
969 }
970 }
971
972 close(pipefd[0]);
973 close(pipefd[1]);
974
975 return err;
976 }
977
copyfd_io_splice(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)978 static int copyfd_io_splice(int infd, int peerfd, int outfd, unsigned int size,
979 bool *in_closed_after_out, struct wstate *winfo)
980 {
981 int err;
982
983 if (listen_mode) {
984 err = do_splice(peerfd, outfd, size, winfo);
985 if (err)
986 return err;
987
988 err = do_splice(infd, peerfd, size, winfo);
989 } else {
990 err = do_splice(infd, peerfd, size, winfo);
991 if (err)
992 return err;
993
994 shut_wr(peerfd);
995
996 err = do_splice(peerfd, outfd, size, winfo);
997 *in_closed_after_out = true;
998 }
999
1000 return err;
1001 }
1002
copyfd_io(int infd,int peerfd,int outfd,bool close_peerfd,struct wstate * winfo)1003 static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd, struct wstate *winfo)
1004 {
1005 bool in_closed_after_out = false;
1006 struct timespec start, end;
1007 int file_size;
1008 int ret;
1009
1010 if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0))
1011 xerror("can not fetch start time %d", errno);
1012
1013 switch (cfg_mode) {
1014 case CFG_MODE_POLL:
1015 ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out,
1016 winfo);
1017 break;
1018
1019 case CFG_MODE_MMAP:
1020 file_size = get_infd_size(infd);
1021 if (file_size < 0)
1022 return file_size;
1023 ret = copyfd_io_mmap(infd, peerfd, outfd, file_size,
1024 &in_closed_after_out, winfo);
1025 break;
1026
1027 case CFG_MODE_SENDFILE:
1028 file_size = get_infd_size(infd);
1029 if (file_size < 0)
1030 return file_size;
1031 ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size,
1032 &in_closed_after_out, winfo);
1033 break;
1034
1035 case CFG_MODE_SPLICE:
1036 file_size = get_infd_size(infd);
1037 if (file_size < 0)
1038 return file_size;
1039 ret = copyfd_io_splice(infd, peerfd, outfd, file_size,
1040 &in_closed_after_out, winfo);
1041 break;
1042
1043 default:
1044 fprintf(stderr, "Invalid mode %d\n", cfg_mode);
1045
1046 die_usage();
1047 return 1;
1048 }
1049
1050 if (ret)
1051 return ret;
1052
1053 if (close_peerfd)
1054 close(peerfd);
1055
1056 if (cfg_time) {
1057 unsigned int delta_ms;
1058
1059 if (clock_gettime(CLOCK_MONOTONIC, &end) < 0)
1060 xerror("can not fetch end time %d", errno);
1061 delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
1062 if (delta_ms > cfg_time) {
1063 xerror("transfer slower than expected! runtime %d ms, expected %d ms",
1064 delta_ms, cfg_time);
1065 }
1066
1067 /* show the runtime only if this end shutdown(wr) before receiving the EOF,
1068 * (that is, if this end got the longer runtime)
1069 */
1070 if (in_closed_after_out)
1071 fprintf(stderr, "%d", delta_ms);
1072 }
1073
1074 return 0;
1075 }
1076
check_sockaddr(int pf,struct sockaddr_storage * ss,socklen_t salen)1077 static void check_sockaddr(int pf, struct sockaddr_storage *ss,
1078 socklen_t salen)
1079 {
1080 struct sockaddr_in6 *sin6;
1081 struct sockaddr_in *sin;
1082 socklen_t wanted_size = 0;
1083
1084 switch (pf) {
1085 case AF_INET:
1086 wanted_size = sizeof(*sin);
1087 sin = (void *)ss;
1088 if (!sin->sin_port)
1089 fprintf(stderr, "accept: something wrong: ip connection from port 0");
1090 break;
1091 case AF_INET6:
1092 wanted_size = sizeof(*sin6);
1093 sin6 = (void *)ss;
1094 if (!sin6->sin6_port)
1095 fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
1096 break;
1097 default:
1098 fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
1099 return;
1100 }
1101
1102 if (salen != wanted_size)
1103 fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
1104 (int)salen, wanted_size);
1105
1106 if (ss->ss_family != pf)
1107 fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
1108 (int)ss->ss_family, pf);
1109 }
1110
check_getpeername(int fd,struct sockaddr_storage * ss,socklen_t salen)1111 static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
1112 {
1113 struct sockaddr_storage peerss;
1114 socklen_t peersalen = sizeof(peerss);
1115
1116 if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
1117 perror("getpeername");
1118 return;
1119 }
1120
1121 if (peersalen != salen) {
1122 fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
1123 return;
1124 }
1125
1126 if (memcmp(ss, &peerss, peersalen)) {
1127 char a[INET6_ADDRSTRLEN];
1128 char b[INET6_ADDRSTRLEN];
1129 char c[INET6_ADDRSTRLEN];
1130 char d[INET6_ADDRSTRLEN];
1131
1132 xgetnameinfo((struct sockaddr *)ss, salen,
1133 a, sizeof(a), b, sizeof(b));
1134
1135 xgetnameinfo((struct sockaddr *)&peerss, peersalen,
1136 c, sizeof(c), d, sizeof(d));
1137
1138 fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
1139 __func__, a, c, b, d, peersalen, salen);
1140 }
1141 }
1142
check_getpeername_connect(int fd)1143 static void check_getpeername_connect(int fd)
1144 {
1145 struct sockaddr_storage ss;
1146 socklen_t salen = sizeof(ss);
1147 char a[INET6_ADDRSTRLEN];
1148 char b[INET6_ADDRSTRLEN];
1149 const char *iface;
1150 size_t len;
1151
1152 if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
1153 perror("getpeername");
1154 return;
1155 }
1156
1157 xgetnameinfo((struct sockaddr *)&ss, salen,
1158 a, sizeof(a), b, sizeof(b));
1159
1160 iface = strchr(cfg_host, '%');
1161 if (iface)
1162 len = iface - cfg_host;
1163 else
1164 len = strlen(cfg_host) + 1;
1165
1166 if (strncmp(cfg_host, a, len) || strcmp(cfg_port, b))
1167 fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
1168 cfg_host, a, cfg_port, b);
1169 }
1170
maybe_close(int fd)1171 static void maybe_close(int fd)
1172 {
1173 unsigned int r = rand();
1174
1175 if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1))
1176 close(fd);
1177 }
1178
main_loop_s(int listensock)1179 int main_loop_s(int listensock)
1180 {
1181 struct sockaddr_storage ss;
1182 struct wstate winfo;
1183 struct pollfd polls;
1184 socklen_t salen;
1185 int remotesock;
1186 int err = 0;
1187 int fd = 0;
1188
1189 again:
1190 polls.fd = listensock;
1191 polls.events = POLLIN;
1192
1193 switch (poll(&polls, 1, poll_timeout)) {
1194 case -1:
1195 perror("poll");
1196 return 1;
1197 case 0:
1198 fprintf(stderr, "%s: timed out\n", __func__);
1199 close(listensock);
1200 return 2;
1201 }
1202
1203 salen = sizeof(ss);
1204 remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
1205 if (remotesock >= 0) {
1206 maybe_close(listensock);
1207 check_sockaddr(pf, &ss, salen);
1208 check_getpeername(remotesock, &ss, salen);
1209
1210 if (cfg_input) {
1211 fd = open(cfg_input, O_RDONLY);
1212 if (fd < 0)
1213 xerror("can't open %s: %d", cfg_input, errno);
1214 }
1215
1216 SOCK_TEST_TCPULP(remotesock, 0);
1217
1218 memset(&winfo, 0, sizeof(winfo));
1219 err = copyfd_io(fd, remotesock, 1, true, &winfo);
1220 } else {
1221 perror("accept");
1222 return 1;
1223 }
1224
1225 if (cfg_input)
1226 close(fd);
1227
1228 if (!err && --cfg_repeat > 0)
1229 goto again;
1230
1231 return err;
1232 }
1233
init_rng(void)1234 static void init_rng(void)
1235 {
1236 unsigned int foo;
1237
1238 if (getrandom(&foo, sizeof(foo), 0) == -1) {
1239 perror("getrandom");
1240 exit(1);
1241 }
1242
1243 srand(foo);
1244 }
1245
xsetsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)1246 static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
1247 {
1248 int err;
1249
1250 err = setsockopt(fd, level, optname, optval, optlen);
1251 if (err) {
1252 perror("setsockopt");
1253 exit(1);
1254 }
1255 }
1256
apply_cmsg_types(int fd,const struct cfg_cmsg_types * cmsg)1257 static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg)
1258 {
1259 static const unsigned int on = 1;
1260
1261 if (cmsg->timestampns)
1262 xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on));
1263 if (cmsg->tcp_inq)
1264 xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on));
1265 }
1266
parse_cmsg_types(const char * type)1267 static void parse_cmsg_types(const char *type)
1268 {
1269 const char *next = strchr(type, ',');
1270 unsigned int len = 0;
1271
1272 cfg_cmsg_types.cmsg_enabled = 1;
1273
1274 if (next) {
1275 parse_cmsg_types(next + 1);
1276 len = next - type;
1277 } else {
1278 len = strlen(type);
1279 }
1280
1281 if (strncmp(type, "TIMESTAMPNS", len) == 0) {
1282 cfg_cmsg_types.timestampns = 1;
1283 return;
1284 }
1285
1286 if (strncmp(type, "TCPINQ", len) == 0) {
1287 cfg_cmsg_types.tcp_inq = 1;
1288 return;
1289 }
1290
1291 fprintf(stderr, "Unrecognized cmsg option %s\n", type);
1292 exit(1);
1293 }
1294
parse_setsock_options(const char * name)1295 static void parse_setsock_options(const char *name)
1296 {
1297 const char *next = strchr(name, ',');
1298 unsigned int len = 0;
1299
1300 if (next) {
1301 parse_setsock_options(next + 1);
1302 len = next - name;
1303 } else {
1304 len = strlen(name);
1305 }
1306
1307 if (strncmp(name, "TRANSPARENT", len) == 0) {
1308 cfg_sockopt_types.transparent = 1;
1309 return;
1310 }
1311
1312 if (strncmp(name, "MPTFO", len) == 0) {
1313 cfg_sockopt_types.mptfo = 1;
1314 return;
1315 }
1316
1317 fprintf(stderr, "Unrecognized setsockopt option %s\n", name);
1318 exit(1);
1319 }
1320
xdisconnect(int fd)1321 void xdisconnect(int fd)
1322 {
1323 socklen_t addrlen = sizeof(struct sockaddr_storage);
1324 struct sockaddr_storage addr, empty;
1325 int msec_sleep = 10;
1326 void *raw_addr;
1327 int i, cmdlen;
1328 char cmd[128];
1329
1330 /* get the local address and convert it to string */
1331 if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0)
1332 xerror("getsockname");
1333
1334 if (addr.ss_family == AF_INET)
1335 raw_addr = &(((struct sockaddr_in *)&addr)->sin_addr);
1336 else if (addr.ss_family == AF_INET6)
1337 raw_addr = &(((struct sockaddr_in6 *)&addr)->sin6_addr);
1338 else
1339 xerror("bad family");
1340
1341 strcpy(cmd, "ss -Mnt | grep -q ");
1342 cmdlen = strlen(cmd);
1343 if (!inet_ntop(addr.ss_family, raw_addr, &cmd[cmdlen],
1344 sizeof(cmd) - cmdlen))
1345 xerror("inet_ntop");
1346
1347 shutdown(fd, SHUT_WR);
1348
1349 /*
1350 * wait until the pending data is completely flushed and all
1351 * the sockets reached the closed status.
1352 * disconnect will bypass/ignore/drop any pending data.
1353 */
1354 for (i = 0; ; i += msec_sleep) {
1355 /* closed socket are not listed by 'ss' */
1356 if (system(cmd) != 0)
1357 break;
1358
1359 if (i > poll_timeout)
1360 xerror("timeout while waiting for spool to complete");
1361 usleep(msec_sleep * 1000);
1362 }
1363
1364 memset(&empty, 0, sizeof(empty));
1365 empty.ss_family = AF_UNSPEC;
1366 if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0)
1367 xerror("can't disconnect: %d", errno);
1368 }
1369
main_loop(void)1370 int main_loop(void)
1371 {
1372 struct addrinfo *peer = NULL;
1373 int fd = 0, ret, fd_in = 0;
1374 struct wstate winfo;
1375
1376 if (cfg_input && cfg_sockopt_types.mptfo) {
1377 fd_in = open(cfg_input, O_RDONLY);
1378 if (fd_in < 0)
1379 xerror("can't open %s:%d", cfg_input, errno);
1380 }
1381
1382 memset(&winfo, 0, sizeof(winfo));
1383 fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer, fd_in, &winfo);
1384 if (fd < 0)
1385 return 2;
1386
1387 again:
1388 check_getpeername_connect(fd);
1389
1390 SOCK_TEST_TCPULP(fd, cfg_sock_proto);
1391
1392 if (cfg_rcvbuf)
1393 set_rcvbuf(fd, cfg_rcvbuf);
1394 if (cfg_sndbuf)
1395 set_sndbuf(fd, cfg_sndbuf);
1396 if (cfg_cmsg_types.cmsg_enabled)
1397 apply_cmsg_types(fd, &cfg_cmsg_types);
1398
1399 if (cfg_input && !cfg_sockopt_types.mptfo) {
1400 fd_in = open(cfg_input, O_RDONLY);
1401 if (fd_in < 0)
1402 xerror("can't open %s:%d", cfg_input, errno);
1403 }
1404
1405 ret = copyfd_io(fd_in, fd, 1, 0, &winfo);
1406 if (ret)
1407 goto out;
1408
1409 if (cfg_truncate > 0) {
1410 shutdown(fd, SHUT_WR);
1411 } else if (--cfg_repeat > 0) {
1412 xdisconnect(fd);
1413
1414 /* the socket could be unblocking at this point, we need the
1415 * connect to be blocking
1416 */
1417 set_nonblock(fd, false);
1418 if (connect(fd, peer->ai_addr, peer->ai_addrlen))
1419 xerror("can't reconnect: %d", errno);
1420 if (cfg_input)
1421 close(fd_in);
1422 memset(&winfo, 0, sizeof(winfo));
1423 goto again;
1424 } else {
1425 close(fd);
1426 }
1427
1428 out:
1429 if (cfg_input)
1430 close(fd_in);
1431 return ret;
1432 }
1433
parse_proto(const char * proto)1434 int parse_proto(const char *proto)
1435 {
1436 if (!strcasecmp(proto, "MPTCP"))
1437 return IPPROTO_MPTCP;
1438 if (!strcasecmp(proto, "TCP"))
1439 return IPPROTO_TCP;
1440
1441 fprintf(stderr, "Unknown protocol: %s\n.", proto);
1442 die_usage();
1443
1444 /* silence compiler warning */
1445 return 0;
1446 }
1447
parse_mode(const char * mode)1448 int parse_mode(const char *mode)
1449 {
1450 if (!strcasecmp(mode, "poll"))
1451 return CFG_MODE_POLL;
1452 if (!strcasecmp(mode, "mmap"))
1453 return CFG_MODE_MMAP;
1454 if (!strcasecmp(mode, "sendfile"))
1455 return CFG_MODE_SENDFILE;
1456 if (!strcasecmp(mode, "splice"))
1457 return CFG_MODE_SPLICE;
1458
1459 fprintf(stderr, "Unknown test mode: %s\n", mode);
1460 fprintf(stderr, "Supported modes are:\n");
1461 fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
1462 fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
1463 fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
1464 fprintf(stderr, "\t\t\"splice\" - send entire input file (splice), then read response (-l will read input first)\n");
1465
1466 die_usage();
1467
1468 /* silence compiler warning */
1469 return 0;
1470 }
1471
parse_peek(const char * mode)1472 int parse_peek(const char *mode)
1473 {
1474 if (!strcasecmp(mode, "saveWithPeek"))
1475 return CFG_WITH_PEEK;
1476 if (!strcasecmp(mode, "saveAfterPeek"))
1477 return CFG_AFTER_PEEK;
1478
1479 fprintf(stderr, "Unknown: %s\n", mode);
1480 fprintf(stderr, "Supported MSG_PEEK mode are:\n");
1481 fprintf(stderr,
1482 "\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n");
1483 fprintf(stderr,
1484 "\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n");
1485
1486 die_usage();
1487
1488 /* silence compiler warning */
1489 return 0;
1490 }
1491
parse_int(const char * size)1492 static int parse_int(const char *size)
1493 {
1494 unsigned long s;
1495
1496 errno = 0;
1497
1498 s = strtoul(size, NULL, 0);
1499
1500 if (errno) {
1501 fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1502 size, strerror(errno));
1503 die_usage();
1504 }
1505
1506 if (s > INT_MAX) {
1507 fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1508 size, strerror(ERANGE));
1509 die_usage();
1510 }
1511
1512 return (int)s;
1513 }
1514
parse_opts(int argc,char ** argv)1515 static void parse_opts(int argc, char **argv)
1516 {
1517 int c;
1518
1519 while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) {
1520 switch (c) {
1521 case 'f':
1522 cfg_truncate = atoi(optarg);
1523
1524 /* when receiving a fastclose, ignore PIPE signals and
1525 * all the I/O errors later in the code
1526 */
1527 if (cfg_truncate < 0) {
1528 cfg_rcv_trunc = true;
1529 signal(SIGPIPE, SIG_IGN);
1530 }
1531 break;
1532 case 'j':
1533 cfg_join = true;
1534 cfg_mode = CFG_MODE_POLL;
1535 break;
1536 case 'r':
1537 cfg_remove = true;
1538 cfg_mode = CFG_MODE_POLL;
1539 cfg_wait = 400000;
1540 cfg_do_w = atoi(optarg);
1541 if (cfg_do_w <= 0)
1542 cfg_do_w = 50;
1543 break;
1544 case 'i':
1545 cfg_input = optarg;
1546 break;
1547 case 'I':
1548 cfg_repeat = atoi(optarg);
1549 break;
1550 case 'l':
1551 listen_mode = true;
1552 break;
1553 case 'p':
1554 cfg_port = optarg;
1555 break;
1556 case 's':
1557 cfg_sock_proto = parse_proto(optarg);
1558 break;
1559 case 'h':
1560 die_usage();
1561 break;
1562 case '6':
1563 pf = AF_INET6;
1564 break;
1565 case 't':
1566 poll_timeout = atoi(optarg) * 1000;
1567 if (poll_timeout <= 0)
1568 poll_timeout = -1;
1569 break;
1570 case 'T':
1571 cfg_time = atoi(optarg);
1572 break;
1573 case 'm':
1574 cfg_mode = parse_mode(optarg);
1575 break;
1576 case 'S':
1577 cfg_sndbuf = parse_int(optarg);
1578 break;
1579 case 'R':
1580 cfg_rcvbuf = parse_int(optarg);
1581 break;
1582 case 'w':
1583 cfg_wait = atoi(optarg)*1000000;
1584 break;
1585 case 'M':
1586 cfg_mark = strtol(optarg, NULL, 0);
1587 break;
1588 case 'P':
1589 cfg_peek = parse_peek(optarg);
1590 break;
1591 case 'c':
1592 parse_cmsg_types(optarg);
1593 break;
1594 case 'o':
1595 parse_setsock_options(optarg);
1596 break;
1597 }
1598 }
1599
1600 if (optind + 1 != argc)
1601 die_usage();
1602 cfg_host = argv[optind];
1603
1604 if (strchr(cfg_host, ':'))
1605 pf = AF_INET6;
1606 }
1607
main(int argc,char * argv[])1608 int main(int argc, char *argv[])
1609 {
1610 init_rng();
1611
1612 signal(SIGUSR1, handle_signal);
1613 parse_opts(argc, argv);
1614
1615 if (listen_mode) {
1616 int fd = sock_listen_mptcp(cfg_host, cfg_port);
1617
1618 if (fd < 0)
1619 return 1;
1620
1621 if (cfg_rcvbuf)
1622 set_rcvbuf(fd, cfg_rcvbuf);
1623 if (cfg_sndbuf)
1624 set_sndbuf(fd, cfg_sndbuf);
1625 if (cfg_mark)
1626 set_mark(fd, cfg_mark);
1627 if (cfg_cmsg_types.cmsg_enabled)
1628 apply_cmsg_types(fd, &cfg_cmsg_types);
1629
1630 return main_loop_s(fd);
1631 }
1632
1633 return main_loop();
1634 }
1635