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 xgetaddrinfo(remoteaddr, port, &hints, &addr);
385 for (a = addr; a; a = a->ai_next) {
386 sock = socket(a->ai_family, a->ai_socktype, proto);
387 if (sock < 0) {
388 perror("socket");
389 continue;
390 }
391
392 SOCK_TEST_TCPULP(sock, proto);
393
394 if (cfg_mark)
395 set_mark(sock, cfg_mark);
396
397 if (cfg_sockopt_types.mptfo) {
398 if (!winfo->total_len)
399 winfo->total_len = winfo->len = read(infd, winfo->buf,
400 sizeof(winfo->buf));
401
402 syn_copied = sendto(sock, winfo->buf, winfo->len, MSG_FASTOPEN,
403 a->ai_addr, a->ai_addrlen);
404 if (syn_copied >= 0) {
405 winfo->off = syn_copied;
406 winfo->len -= syn_copied;
407 *peer = a;
408 break; /* success */
409 }
410 perror("sendto()");
411 } else {
412 if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) {
413 *peer = a;
414 break; /* success */
415 }
416 perror("connect()");
417 }
418
419 /* error */
420 close(sock);
421 sock = -1;
422 }
423
424 freeaddrinfo(addr);
425 if (sock != -1)
426 SOCK_TEST_TCPULP(sock, proto);
427 return sock;
428 }
429
do_rnd_write(const int fd,char * buf,const size_t len)430 static size_t do_rnd_write(const int fd, char *buf, const size_t len)
431 {
432 static bool first = true;
433 unsigned int do_w;
434 ssize_t bw;
435
436 do_w = rand() & 0xffff;
437 if (do_w == 0 || do_w > len)
438 do_w = len;
439
440 if (cfg_join && first && do_w > 100)
441 do_w = 100;
442
443 if (cfg_remove && do_w > cfg_do_w)
444 do_w = cfg_do_w;
445
446 bw = write(fd, buf, do_w);
447 if (bw < 0)
448 return bw;
449
450 /* let the join handshake complete, before going on */
451 if (cfg_join && first) {
452 usleep(200000);
453 first = false;
454 }
455
456 if (cfg_remove)
457 usleep(200000);
458
459 return bw;
460 }
461
do_write(const int fd,char * buf,const size_t len)462 static size_t do_write(const int fd, char *buf, const size_t len)
463 {
464 size_t offset = 0;
465
466 while (offset < len) {
467 size_t written;
468 ssize_t bw;
469
470 bw = write(fd, buf + offset, len - offset);
471 if (bw < 0) {
472 perror("write");
473 return 0;
474 }
475
476 written = (size_t)bw;
477 offset += written;
478 }
479
480 return offset;
481 }
482
process_cmsg(struct msghdr * msgh)483 static void process_cmsg(struct msghdr *msgh)
484 {
485 struct __kernel_timespec ts;
486 bool inq_found = false;
487 bool ts_found = false;
488 unsigned int inq = 0;
489 struct cmsghdr *cmsg;
490
491 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
492 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) {
493 memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
494 ts_found = true;
495 continue;
496 }
497 if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) {
498 memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq));
499 inq_found = true;
500 continue;
501 }
502
503 }
504
505 if (cfg_cmsg_types.timestampns) {
506 if (!ts_found)
507 xerror("TIMESTAMPNS not present\n");
508 }
509
510 if (cfg_cmsg_types.tcp_inq) {
511 if (!inq_found)
512 xerror("TCP_INQ not present\n");
513
514 if (inq > 1024)
515 xerror("tcp_inq %u is larger than one kbyte\n", inq);
516 tcp_inq.last = inq;
517 }
518 }
519
do_recvmsg_cmsg(const int fd,char * buf,const size_t len)520 static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
521 {
522 char msg_buf[8192];
523 struct iovec iov = {
524 .iov_base = buf,
525 .iov_len = len,
526 };
527 struct msghdr msg = {
528 .msg_iov = &iov,
529 .msg_iovlen = 1,
530 .msg_control = msg_buf,
531 .msg_controllen = sizeof(msg_buf),
532 };
533 int flags = 0;
534 unsigned int last_hint = tcp_inq.last;
535 int ret = recvmsg(fd, &msg, flags);
536
537 if (ret <= 0) {
538 if (ret == 0 && tcp_inq.expect_eof)
539 return ret;
540
541 if (ret == 0 && cfg_cmsg_types.tcp_inq)
542 if (last_hint != 1 && last_hint != 0)
543 xerror("EOF but last tcp_inq hint was %u\n", last_hint);
544
545 return ret;
546 }
547
548 if (tcp_inq.expect_eof)
549 xerror("expected EOF, last_hint %u, now %u\n",
550 last_hint, tcp_inq.last);
551
552 if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled)
553 xerror("got %lu bytes of cmsg data, expected 0\n",
554 (unsigned long)msg.msg_controllen);
555
556 if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled)
557 xerror("%s\n", "got no cmsg data");
558
559 if (msg.msg_controllen)
560 process_cmsg(&msg);
561
562 if (cfg_cmsg_types.tcp_inq) {
563 if ((size_t)ret < len && last_hint > (unsigned int)ret) {
564 if (ret + 1 != (int)last_hint) {
565 int next = read(fd, msg_buf, sizeof(msg_buf));
566
567 xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n",
568 ret, (unsigned int)len, last_hint, tcp_inq.last, next);
569 } else {
570 tcp_inq.expect_eof = true;
571 }
572 }
573 }
574
575 return ret;
576 }
577
do_rnd_read(const int fd,char * buf,const size_t len)578 static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
579 {
580 int ret = 0;
581 char tmp[16384];
582 size_t cap = rand();
583
584 cap &= 0xffff;
585
586 if (cap == 0)
587 cap = 1;
588 else if (cap > len)
589 cap = len;
590
591 if (cfg_peek == CFG_WITH_PEEK) {
592 ret = recv(fd, buf, cap, MSG_PEEK);
593 ret = (ret < 0) ? ret : read(fd, tmp, ret);
594 } else if (cfg_peek == CFG_AFTER_PEEK) {
595 ret = recv(fd, buf, cap, MSG_PEEK);
596 ret = (ret < 0) ? ret : read(fd, buf, cap);
597 } else if (cfg_cmsg_types.cmsg_enabled) {
598 ret = do_recvmsg_cmsg(fd, buf, cap);
599 } else {
600 ret = read(fd, buf, cap);
601 }
602
603 return ret;
604 }
605
set_nonblock(int fd,bool nonblock)606 static void set_nonblock(int fd, bool nonblock)
607 {
608 int flags = fcntl(fd, F_GETFL);
609
610 if (flags == -1)
611 return;
612
613 if (nonblock)
614 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
615 else
616 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
617 }
618
shut_wr(int fd)619 static void shut_wr(int fd)
620 {
621 /* Close our write side, ev. give some time
622 * for address notification and/or checking
623 * the current status
624 */
625 if (cfg_wait)
626 usleep(cfg_wait);
627
628 shutdown(fd, SHUT_WR);
629 }
630
copyfd_io_poll(int infd,int peerfd,int outfd,bool * in_closed_after_out,struct wstate * winfo)631 static int copyfd_io_poll(int infd, int peerfd, int outfd,
632 bool *in_closed_after_out, struct wstate *winfo)
633 {
634 struct pollfd fds = {
635 .fd = peerfd,
636 .events = POLLIN | POLLOUT,
637 };
638 unsigned int total_wlen = 0, total_rlen = 0;
639
640 set_nonblock(peerfd, true);
641
642 for (;;) {
643 char rbuf[8192];
644 ssize_t len;
645
646 if (fds.events == 0 || quit)
647 break;
648
649 switch (poll(&fds, 1, poll_timeout)) {
650 case -1:
651 if (errno == EINTR)
652 continue;
653 perror("poll");
654 return 1;
655 case 0:
656 fprintf(stderr, "%s: poll timed out (events: "
657 "POLLIN %u, POLLOUT %u)\n", __func__,
658 fds.events & POLLIN, fds.events & POLLOUT);
659 return 2;
660 }
661
662 if (fds.revents & POLLIN) {
663 ssize_t rb = sizeof(rbuf);
664
665 /* limit the total amount of read data to the trunc value*/
666 if (cfg_truncate > 0) {
667 if (rb + total_rlen > cfg_truncate)
668 rb = cfg_truncate - total_rlen;
669 len = read(peerfd, rbuf, rb);
670 } else {
671 len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
672 }
673 if (len == 0) {
674 /* no more data to receive:
675 * peer has closed its write side
676 */
677 fds.events &= ~POLLIN;
678
679 if ((fds.events & POLLOUT) == 0) {
680 *in_closed_after_out = true;
681 /* and nothing more to send */
682 break;
683 }
684
685 /* Else, still have data to transmit */
686 } else if (len < 0) {
687 if (cfg_rcv_trunc)
688 return 0;
689 perror("read");
690 return 3;
691 }
692
693 total_rlen += len;
694 do_write(outfd, rbuf, len);
695 }
696
697 if (fds.revents & POLLOUT) {
698 if (winfo->len == 0) {
699 winfo->off = 0;
700 winfo->len = read(infd, winfo->buf, sizeof(winfo->buf));
701 }
702
703 if (winfo->len > 0) {
704 ssize_t bw;
705
706 /* limit the total amount of written data to the trunc value */
707 if (cfg_truncate > 0 && winfo->len + total_wlen > cfg_truncate)
708 winfo->len = cfg_truncate - total_wlen;
709
710 bw = do_rnd_write(peerfd, winfo->buf + winfo->off, winfo->len);
711 if (bw < 0) {
712 /* expected reset, continue to read */
713 if (cfg_rcv_trunc &&
714 (errno == ECONNRESET ||
715 errno == EPIPE)) {
716 fds.events &= ~POLLOUT;
717 continue;
718 }
719
720 perror("write");
721 return 111;
722 }
723
724 winfo->off += bw;
725 winfo->len -= bw;
726 total_wlen += bw;
727 } else if (winfo->len == 0) {
728 /* We have no more data to send. */
729 fds.events &= ~POLLOUT;
730
731 if ((fds.events & POLLIN) == 0)
732 /* ... and peer also closed already */
733 break;
734
735 shut_wr(peerfd);
736 } else {
737 if (errno == EINTR)
738 continue;
739 perror("read");
740 return 4;
741 }
742 }
743
744 if (fds.revents & (POLLERR | POLLNVAL)) {
745 if (cfg_rcv_trunc) {
746 fds.events &= ~(POLLERR | POLLNVAL);
747 continue;
748 }
749 fprintf(stderr, "Unexpected revents: "
750 "POLLERR/POLLNVAL(%x)\n", fds.revents);
751 return 5;
752 }
753
754 if (cfg_truncate > 0 && total_wlen >= cfg_truncate &&
755 total_rlen >= cfg_truncate)
756 break;
757 }
758
759 /* leave some time for late join/announce */
760 if (cfg_remove && !quit)
761 usleep(cfg_wait);
762
763 return 0;
764 }
765
do_recvfile(int infd,int outfd)766 static int do_recvfile(int infd, int outfd)
767 {
768 ssize_t r;
769
770 do {
771 char buf[16384];
772
773 r = do_rnd_read(infd, buf, sizeof(buf));
774 if (r > 0) {
775 if (write(outfd, buf, r) != r)
776 break;
777 } else if (r < 0) {
778 perror("read");
779 }
780 } while (r > 0);
781
782 return (int)r;
783 }
784
spool_buf(int fd,struct wstate * winfo)785 static int spool_buf(int fd, struct wstate *winfo)
786 {
787 while (winfo->len) {
788 int ret = write(fd, winfo->buf + winfo->off, winfo->len);
789
790 if (ret < 0) {
791 perror("write");
792 return 4;
793 }
794 winfo->off += ret;
795 winfo->len -= ret;
796 }
797 return 0;
798 }
799
do_mmap(int infd,int outfd,unsigned int size,struct wstate * winfo)800 static int do_mmap(int infd, int outfd, unsigned int size,
801 struct wstate *winfo)
802 {
803 char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
804 ssize_t ret = 0, off = winfo->total_len;
805 size_t rem;
806
807 if (inbuf == MAP_FAILED) {
808 perror("mmap");
809 return 1;
810 }
811
812 ret = spool_buf(outfd, winfo);
813 if (ret < 0)
814 return ret;
815
816 rem = size - winfo->total_len;
817
818 while (rem > 0) {
819 ret = write(outfd, inbuf + off, rem);
820
821 if (ret < 0) {
822 perror("write");
823 break;
824 }
825
826 off += ret;
827 rem -= ret;
828 }
829
830 munmap(inbuf, size);
831 return rem;
832 }
833
get_infd_size(int fd)834 static int get_infd_size(int fd)
835 {
836 struct stat sb;
837 ssize_t count;
838 int err;
839
840 err = fstat(fd, &sb);
841 if (err < 0) {
842 perror("fstat");
843 return -1;
844 }
845
846 if ((sb.st_mode & S_IFMT) != S_IFREG) {
847 fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
848 return -2;
849 }
850
851 count = sb.st_size;
852 if (count > INT_MAX) {
853 fprintf(stderr, "File too large: %zu\n", count);
854 return -3;
855 }
856
857 return (int)count;
858 }
859
do_sendfile(int infd,int outfd,unsigned int count,struct wstate * winfo)860 static int do_sendfile(int infd, int outfd, unsigned int count,
861 struct wstate *winfo)
862 {
863 int ret = spool_buf(outfd, winfo);
864
865 if (ret < 0)
866 return ret;
867
868 count -= winfo->total_len;
869
870 while (count > 0) {
871 ssize_t r;
872
873 r = sendfile(outfd, infd, NULL, count);
874 if (r < 0) {
875 perror("sendfile");
876 return 3;
877 }
878
879 count -= r;
880 }
881
882 return 0;
883 }
884
copyfd_io_mmap(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)885 static int copyfd_io_mmap(int infd, int peerfd, int outfd,
886 unsigned int size, bool *in_closed_after_out,
887 struct wstate *winfo)
888 {
889 int err;
890
891 if (listen_mode) {
892 err = do_recvfile(peerfd, outfd);
893 if (err)
894 return err;
895
896 err = do_mmap(infd, peerfd, size, winfo);
897 } else {
898 err = do_mmap(infd, peerfd, size, winfo);
899 if (err)
900 return err;
901
902 shut_wr(peerfd);
903
904 err = do_recvfile(peerfd, outfd);
905 *in_closed_after_out = true;
906 }
907
908 return err;
909 }
910
copyfd_io_sendfile(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)911 static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
912 unsigned int size, bool *in_closed_after_out, struct wstate *winfo)
913 {
914 int err;
915
916 if (listen_mode) {
917 err = do_recvfile(peerfd, outfd);
918 if (err)
919 return err;
920
921 err = do_sendfile(infd, peerfd, size, winfo);
922 } else {
923 err = do_sendfile(infd, peerfd, size, winfo);
924 if (err)
925 return err;
926
927 shut_wr(peerfd);
928
929 err = do_recvfile(peerfd, outfd);
930 *in_closed_after_out = true;
931 }
932
933 return err;
934 }
935
do_splice(const int infd,const int outfd,const size_t len,struct wstate * winfo)936 static int do_splice(const int infd, const int outfd, const size_t len,
937 struct wstate *winfo)
938 {
939 ssize_t in_bytes, out_bytes;
940 int pipefd[2];
941 int err;
942
943 err = pipe(pipefd);
944 if (err) {
945 perror("pipe");
946 return 2;
947 }
948
949 again:
950 in_bytes = splice(infd, NULL, pipefd[1], NULL, len - winfo->total_len,
951 SPLICE_F_MOVE | SPLICE_F_MORE);
952 if (in_bytes < 0) {
953 perror("splice in");
954 err = 3;
955 } else if (in_bytes > 0) {
956 out_bytes = splice(pipefd[0], NULL, outfd, NULL, in_bytes,
957 SPLICE_F_MOVE | SPLICE_F_MORE);
958 if (out_bytes < 0) {
959 perror("splice out");
960 err = 4;
961 } else if (in_bytes != out_bytes) {
962 fprintf(stderr, "Unexpected transfer: %zu vs %zu\n",
963 in_bytes, out_bytes);
964 err = 5;
965 } else {
966 goto again;
967 }
968 }
969
970 close(pipefd[0]);
971 close(pipefd[1]);
972
973 return err;
974 }
975
copyfd_io_splice(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)976 static int copyfd_io_splice(int infd, int peerfd, int outfd, unsigned int size,
977 bool *in_closed_after_out, struct wstate *winfo)
978 {
979 int err;
980
981 if (listen_mode) {
982 err = do_splice(peerfd, outfd, size, winfo);
983 if (err)
984 return err;
985
986 err = do_splice(infd, peerfd, size, winfo);
987 } else {
988 err = do_splice(infd, peerfd, size, winfo);
989 if (err)
990 return err;
991
992 shut_wr(peerfd);
993
994 err = do_splice(peerfd, outfd, size, winfo);
995 *in_closed_after_out = true;
996 }
997
998 return err;
999 }
1000
copyfd_io(int infd,int peerfd,int outfd,bool close_peerfd,struct wstate * winfo)1001 static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd, struct wstate *winfo)
1002 {
1003 bool in_closed_after_out = false;
1004 struct timespec start, end;
1005 int file_size;
1006 int ret;
1007
1008 if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0))
1009 xerror("can not fetch start time %d", errno);
1010
1011 switch (cfg_mode) {
1012 case CFG_MODE_POLL:
1013 ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out,
1014 winfo);
1015 break;
1016
1017 case CFG_MODE_MMAP:
1018 file_size = get_infd_size(infd);
1019 if (file_size < 0)
1020 return file_size;
1021 ret = copyfd_io_mmap(infd, peerfd, outfd, file_size,
1022 &in_closed_after_out, winfo);
1023 break;
1024
1025 case CFG_MODE_SENDFILE:
1026 file_size = get_infd_size(infd);
1027 if (file_size < 0)
1028 return file_size;
1029 ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size,
1030 &in_closed_after_out, winfo);
1031 break;
1032
1033 case CFG_MODE_SPLICE:
1034 file_size = get_infd_size(infd);
1035 if (file_size < 0)
1036 return file_size;
1037 ret = copyfd_io_splice(infd, peerfd, outfd, file_size,
1038 &in_closed_after_out, winfo);
1039 break;
1040
1041 default:
1042 fprintf(stderr, "Invalid mode %d\n", cfg_mode);
1043
1044 die_usage();
1045 return 1;
1046 }
1047
1048 if (ret)
1049 return ret;
1050
1051 if (close_peerfd)
1052 close(peerfd);
1053
1054 if (cfg_time) {
1055 unsigned int delta_ms;
1056
1057 if (clock_gettime(CLOCK_MONOTONIC, &end) < 0)
1058 xerror("can not fetch end time %d", errno);
1059 delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
1060 if (delta_ms > cfg_time) {
1061 xerror("transfer slower than expected! runtime %d ms, expected %d ms",
1062 delta_ms, cfg_time);
1063 }
1064
1065 /* show the runtime only if this end shutdown(wr) before receiving the EOF,
1066 * (that is, if this end got the longer runtime)
1067 */
1068 if (in_closed_after_out)
1069 fprintf(stderr, "%d", delta_ms);
1070 }
1071
1072 return 0;
1073 }
1074
check_sockaddr(int pf,struct sockaddr_storage * ss,socklen_t salen)1075 static void check_sockaddr(int pf, struct sockaddr_storage *ss,
1076 socklen_t salen)
1077 {
1078 struct sockaddr_in6 *sin6;
1079 struct sockaddr_in *sin;
1080 socklen_t wanted_size = 0;
1081
1082 switch (pf) {
1083 case AF_INET:
1084 wanted_size = sizeof(*sin);
1085 sin = (void *)ss;
1086 if (!sin->sin_port)
1087 fprintf(stderr, "accept: something wrong: ip connection from port 0");
1088 break;
1089 case AF_INET6:
1090 wanted_size = sizeof(*sin6);
1091 sin6 = (void *)ss;
1092 if (!sin6->sin6_port)
1093 fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
1094 break;
1095 default:
1096 fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
1097 return;
1098 }
1099
1100 if (salen != wanted_size)
1101 fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
1102 (int)salen, wanted_size);
1103
1104 if (ss->ss_family != pf)
1105 fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
1106 (int)ss->ss_family, pf);
1107 }
1108
check_getpeername(int fd,struct sockaddr_storage * ss,socklen_t salen)1109 static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
1110 {
1111 struct sockaddr_storage peerss;
1112 socklen_t peersalen = sizeof(peerss);
1113
1114 if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
1115 perror("getpeername");
1116 return;
1117 }
1118
1119 if (peersalen != salen) {
1120 fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
1121 return;
1122 }
1123
1124 if (memcmp(ss, &peerss, peersalen)) {
1125 char a[INET6_ADDRSTRLEN];
1126 char b[INET6_ADDRSTRLEN];
1127 char c[INET6_ADDRSTRLEN];
1128 char d[INET6_ADDRSTRLEN];
1129
1130 xgetnameinfo((struct sockaddr *)ss, salen,
1131 a, sizeof(a), b, sizeof(b));
1132
1133 xgetnameinfo((struct sockaddr *)&peerss, peersalen,
1134 c, sizeof(c), d, sizeof(d));
1135
1136 fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
1137 __func__, a, c, b, d, peersalen, salen);
1138 }
1139 }
1140
check_getpeername_connect(int fd)1141 static void check_getpeername_connect(int fd)
1142 {
1143 struct sockaddr_storage ss;
1144 socklen_t salen = sizeof(ss);
1145 char a[INET6_ADDRSTRLEN];
1146 char b[INET6_ADDRSTRLEN];
1147 const char *iface;
1148 size_t len;
1149
1150 if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
1151 perror("getpeername");
1152 return;
1153 }
1154
1155 xgetnameinfo((struct sockaddr *)&ss, salen,
1156 a, sizeof(a), b, sizeof(b));
1157
1158 iface = strchr(cfg_host, '%');
1159 if (iface)
1160 len = iface - cfg_host;
1161 else
1162 len = strlen(cfg_host) + 1;
1163
1164 if (strncmp(cfg_host, a, len) || strcmp(cfg_port, b))
1165 fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
1166 cfg_host, a, cfg_port, b);
1167 }
1168
maybe_close(int fd)1169 static void maybe_close(int fd)
1170 {
1171 unsigned int r = rand();
1172
1173 if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1))
1174 close(fd);
1175 }
1176
main_loop_s(int listensock)1177 int main_loop_s(int listensock)
1178 {
1179 struct sockaddr_storage ss;
1180 struct wstate winfo;
1181 struct pollfd polls;
1182 socklen_t salen;
1183 int remotesock;
1184 int err = 0;
1185 int fd = 0;
1186
1187 again:
1188 polls.fd = listensock;
1189 polls.events = POLLIN;
1190
1191 switch (poll(&polls, 1, poll_timeout)) {
1192 case -1:
1193 perror("poll");
1194 return 1;
1195 case 0:
1196 fprintf(stderr, "%s: timed out\n", __func__);
1197 close(listensock);
1198 return 2;
1199 }
1200
1201 salen = sizeof(ss);
1202 remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
1203 if (remotesock >= 0) {
1204 maybe_close(listensock);
1205 check_sockaddr(pf, &ss, salen);
1206 check_getpeername(remotesock, &ss, salen);
1207
1208 if (cfg_input) {
1209 fd = open(cfg_input, O_RDONLY);
1210 if (fd < 0)
1211 xerror("can't open %s: %d", cfg_input, errno);
1212 }
1213
1214 SOCK_TEST_TCPULP(remotesock, 0);
1215
1216 memset(&winfo, 0, sizeof(winfo));
1217 err = copyfd_io(fd, remotesock, 1, true, &winfo);
1218 } else {
1219 perror("accept");
1220 return 1;
1221 }
1222
1223 if (cfg_input)
1224 close(fd);
1225
1226 if (!err && --cfg_repeat > 0)
1227 goto again;
1228
1229 return err;
1230 }
1231
init_rng(void)1232 static void init_rng(void)
1233 {
1234 unsigned int foo;
1235
1236 if (getrandom(&foo, sizeof(foo), 0) == -1) {
1237 perror("getrandom");
1238 exit(1);
1239 }
1240
1241 srand(foo);
1242 }
1243
xsetsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)1244 static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
1245 {
1246 int err;
1247
1248 err = setsockopt(fd, level, optname, optval, optlen);
1249 if (err) {
1250 perror("setsockopt");
1251 exit(1);
1252 }
1253 }
1254
apply_cmsg_types(int fd,const struct cfg_cmsg_types * cmsg)1255 static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg)
1256 {
1257 static const unsigned int on = 1;
1258
1259 if (cmsg->timestampns)
1260 xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on));
1261 if (cmsg->tcp_inq)
1262 xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on));
1263 }
1264
parse_cmsg_types(const char * type)1265 static void parse_cmsg_types(const char *type)
1266 {
1267 char *next = strchr(type, ',');
1268 unsigned int len = 0;
1269
1270 cfg_cmsg_types.cmsg_enabled = 1;
1271
1272 if (next) {
1273 parse_cmsg_types(next + 1);
1274 len = next - type;
1275 } else {
1276 len = strlen(type);
1277 }
1278
1279 if (strncmp(type, "TIMESTAMPNS", len) == 0) {
1280 cfg_cmsg_types.timestampns = 1;
1281 return;
1282 }
1283
1284 if (strncmp(type, "TCPINQ", len) == 0) {
1285 cfg_cmsg_types.tcp_inq = 1;
1286 return;
1287 }
1288
1289 fprintf(stderr, "Unrecognized cmsg option %s\n", type);
1290 exit(1);
1291 }
1292
parse_setsock_options(const char * name)1293 static void parse_setsock_options(const char *name)
1294 {
1295 char *next = strchr(name, ',');
1296 unsigned int len = 0;
1297
1298 if (next) {
1299 parse_setsock_options(next + 1);
1300 len = next - name;
1301 } else {
1302 len = strlen(name);
1303 }
1304
1305 if (strncmp(name, "TRANSPARENT", len) == 0) {
1306 cfg_sockopt_types.transparent = 1;
1307 return;
1308 }
1309
1310 if (strncmp(name, "MPTFO", len) == 0) {
1311 cfg_sockopt_types.mptfo = 1;
1312 return;
1313 }
1314
1315 fprintf(stderr, "Unrecognized setsockopt option %s\n", name);
1316 exit(1);
1317 }
1318
xdisconnect(int fd)1319 void xdisconnect(int fd)
1320 {
1321 socklen_t addrlen = sizeof(struct sockaddr_storage);
1322 struct sockaddr_storage addr, empty;
1323 int msec_sleep = 10;
1324 void *raw_addr;
1325 int i, cmdlen;
1326 char cmd[128];
1327
1328 /* get the local address and convert it to string */
1329 if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0)
1330 xerror("getsockname");
1331
1332 if (addr.ss_family == AF_INET)
1333 raw_addr = &(((struct sockaddr_in *)&addr)->sin_addr);
1334 else if (addr.ss_family == AF_INET6)
1335 raw_addr = &(((struct sockaddr_in6 *)&addr)->sin6_addr);
1336 else
1337 xerror("bad family");
1338
1339 strcpy(cmd, "ss -Mnt | grep -q ");
1340 cmdlen = strlen(cmd);
1341 if (!inet_ntop(addr.ss_family, raw_addr, &cmd[cmdlen],
1342 sizeof(cmd) - cmdlen))
1343 xerror("inet_ntop");
1344
1345 shutdown(fd, SHUT_WR);
1346
1347 /*
1348 * wait until the pending data is completely flushed and all
1349 * the sockets reached the closed status.
1350 * disconnect will bypass/ignore/drop any pending data.
1351 */
1352 for (i = 0; ; i += msec_sleep) {
1353 /* closed socket are not listed by 'ss' */
1354 if (system(cmd) != 0)
1355 break;
1356
1357 if (i > poll_timeout)
1358 xerror("timeout while waiting for spool to complete");
1359 usleep(msec_sleep * 1000);
1360 }
1361
1362 memset(&empty, 0, sizeof(empty));
1363 empty.ss_family = AF_UNSPEC;
1364 if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0)
1365 xerror("can't disconnect: %d", errno);
1366 }
1367
main_loop(void)1368 int main_loop(void)
1369 {
1370 struct addrinfo *peer = NULL;
1371 int fd = 0, ret, fd_in = 0;
1372 struct wstate winfo;
1373
1374 if (cfg_input && cfg_sockopt_types.mptfo) {
1375 fd_in = open(cfg_input, O_RDONLY);
1376 if (fd_in < 0)
1377 xerror("can't open %s:%d", cfg_input, errno);
1378 }
1379
1380 memset(&winfo, 0, sizeof(winfo));
1381 fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer, fd_in, &winfo);
1382 if (fd < 0)
1383 return 2;
1384
1385 again:
1386 check_getpeername_connect(fd);
1387
1388 SOCK_TEST_TCPULP(fd, cfg_sock_proto);
1389
1390 if (cfg_rcvbuf)
1391 set_rcvbuf(fd, cfg_rcvbuf);
1392 if (cfg_sndbuf)
1393 set_sndbuf(fd, cfg_sndbuf);
1394 if (cfg_cmsg_types.cmsg_enabled)
1395 apply_cmsg_types(fd, &cfg_cmsg_types);
1396
1397 if (cfg_input && !cfg_sockopt_types.mptfo) {
1398 fd_in = open(cfg_input, O_RDONLY);
1399 if (fd_in < 0)
1400 xerror("can't open %s:%d", cfg_input, errno);
1401 }
1402
1403 ret = copyfd_io(fd_in, fd, 1, 0, &winfo);
1404 if (ret)
1405 goto out;
1406
1407 if (cfg_truncate > 0) {
1408 shutdown(fd, SHUT_WR);
1409 } else if (--cfg_repeat > 0) {
1410 xdisconnect(fd);
1411
1412 /* the socket could be unblocking at this point, we need the
1413 * connect to be blocking
1414 */
1415 set_nonblock(fd, false);
1416 if (connect(fd, peer->ai_addr, peer->ai_addrlen))
1417 xerror("can't reconnect: %d", errno);
1418 if (cfg_input)
1419 close(fd_in);
1420 memset(&winfo, 0, sizeof(winfo));
1421 goto again;
1422 } else {
1423 close(fd);
1424 }
1425
1426 out:
1427 if (cfg_input)
1428 close(fd_in);
1429 return ret;
1430 }
1431
parse_proto(const char * proto)1432 int parse_proto(const char *proto)
1433 {
1434 if (!strcasecmp(proto, "MPTCP"))
1435 return IPPROTO_MPTCP;
1436 if (!strcasecmp(proto, "TCP"))
1437 return IPPROTO_TCP;
1438
1439 fprintf(stderr, "Unknown protocol: %s\n.", proto);
1440 die_usage();
1441
1442 /* silence compiler warning */
1443 return 0;
1444 }
1445
parse_mode(const char * mode)1446 int parse_mode(const char *mode)
1447 {
1448 if (!strcasecmp(mode, "poll"))
1449 return CFG_MODE_POLL;
1450 if (!strcasecmp(mode, "mmap"))
1451 return CFG_MODE_MMAP;
1452 if (!strcasecmp(mode, "sendfile"))
1453 return CFG_MODE_SENDFILE;
1454 if (!strcasecmp(mode, "splice"))
1455 return CFG_MODE_SPLICE;
1456
1457 fprintf(stderr, "Unknown test mode: %s\n", mode);
1458 fprintf(stderr, "Supported modes are:\n");
1459 fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
1460 fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
1461 fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
1462 fprintf(stderr, "\t\t\"splice\" - send entire input file (splice), then read response (-l will read input first)\n");
1463
1464 die_usage();
1465
1466 /* silence compiler warning */
1467 return 0;
1468 }
1469
parse_peek(const char * mode)1470 int parse_peek(const char *mode)
1471 {
1472 if (!strcasecmp(mode, "saveWithPeek"))
1473 return CFG_WITH_PEEK;
1474 if (!strcasecmp(mode, "saveAfterPeek"))
1475 return CFG_AFTER_PEEK;
1476
1477 fprintf(stderr, "Unknown: %s\n", mode);
1478 fprintf(stderr, "Supported MSG_PEEK mode are:\n");
1479 fprintf(stderr,
1480 "\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n");
1481 fprintf(stderr,
1482 "\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n");
1483
1484 die_usage();
1485
1486 /* silence compiler warning */
1487 return 0;
1488 }
1489
parse_int(const char * size)1490 static int parse_int(const char *size)
1491 {
1492 unsigned long s;
1493
1494 errno = 0;
1495
1496 s = strtoul(size, NULL, 0);
1497
1498 if (errno) {
1499 fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1500 size, strerror(errno));
1501 die_usage();
1502 }
1503
1504 if (s > INT_MAX) {
1505 fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1506 size, strerror(ERANGE));
1507 die_usage();
1508 }
1509
1510 return (int)s;
1511 }
1512
parse_opts(int argc,char ** argv)1513 static void parse_opts(int argc, char **argv)
1514 {
1515 int c;
1516
1517 while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) {
1518 switch (c) {
1519 case 'f':
1520 cfg_truncate = atoi(optarg);
1521
1522 /* when receiving a fastclose, ignore PIPE signals and
1523 * all the I/O errors later in the code
1524 */
1525 if (cfg_truncate < 0) {
1526 cfg_rcv_trunc = true;
1527 signal(SIGPIPE, SIG_IGN);
1528 }
1529 break;
1530 case 'j':
1531 cfg_join = true;
1532 cfg_mode = CFG_MODE_POLL;
1533 break;
1534 case 'r':
1535 cfg_remove = true;
1536 cfg_mode = CFG_MODE_POLL;
1537 cfg_wait = 400000;
1538 cfg_do_w = atoi(optarg);
1539 if (cfg_do_w <= 0)
1540 cfg_do_w = 50;
1541 break;
1542 case 'i':
1543 cfg_input = optarg;
1544 break;
1545 case 'I':
1546 cfg_repeat = atoi(optarg);
1547 break;
1548 case 'l':
1549 listen_mode = true;
1550 break;
1551 case 'p':
1552 cfg_port = optarg;
1553 break;
1554 case 's':
1555 cfg_sock_proto = parse_proto(optarg);
1556 break;
1557 case 'h':
1558 die_usage();
1559 break;
1560 case '6':
1561 pf = AF_INET6;
1562 break;
1563 case 't':
1564 poll_timeout = atoi(optarg) * 1000;
1565 if (poll_timeout <= 0)
1566 poll_timeout = -1;
1567 break;
1568 case 'T':
1569 cfg_time = atoi(optarg);
1570 break;
1571 case 'm':
1572 cfg_mode = parse_mode(optarg);
1573 break;
1574 case 'S':
1575 cfg_sndbuf = parse_int(optarg);
1576 break;
1577 case 'R':
1578 cfg_rcvbuf = parse_int(optarg);
1579 break;
1580 case 'w':
1581 cfg_wait = atoi(optarg)*1000000;
1582 break;
1583 case 'M':
1584 cfg_mark = strtol(optarg, NULL, 0);
1585 break;
1586 case 'P':
1587 cfg_peek = parse_peek(optarg);
1588 break;
1589 case 'c':
1590 parse_cmsg_types(optarg);
1591 break;
1592 case 'o':
1593 parse_setsock_options(optarg);
1594 break;
1595 }
1596 }
1597
1598 if (optind + 1 != argc)
1599 die_usage();
1600 cfg_host = argv[optind];
1601
1602 if (strchr(cfg_host, ':'))
1603 pf = AF_INET6;
1604 }
1605
main(int argc,char * argv[])1606 int main(int argc, char *argv[])
1607 {
1608 init_rng();
1609
1610 signal(SIGUSR1, handle_signal);
1611 parse_opts(argc, argv);
1612
1613 if (listen_mode) {
1614 int fd = sock_listen_mptcp(cfg_host, cfg_port);
1615
1616 if (fd < 0)
1617 return 1;
1618
1619 if (cfg_rcvbuf)
1620 set_rcvbuf(fd, cfg_rcvbuf);
1621 if (cfg_sndbuf)
1622 set_sndbuf(fd, cfg_sndbuf);
1623 if (cfg_mark)
1624 set_mark(fd, cfg_mark);
1625 if (cfg_cmsg_types.cmsg_enabled)
1626 apply_cmsg_types(fd, &cfg_cmsg_types);
1627
1628 return main_loop_s(fd);
1629 }
1630
1631 return main_loop();
1632 }
1633