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