1048d19d4SFlorian Westphal // SPDX-License-Identifier: GPL-2.0 2048d19d4SFlorian Westphal 3048d19d4SFlorian Westphal #define _GNU_SOURCE 4048d19d4SFlorian Westphal 5048d19d4SFlorian Westphal #include <errno.h> 6048d19d4SFlorian Westphal #include <limits.h> 7048d19d4SFlorian Westphal #include <fcntl.h> 8048d19d4SFlorian Westphal #include <string.h> 95e6af0a7SFlorian Westphal #include <stdarg.h> 10048d19d4SFlorian Westphal #include <stdbool.h> 11048d19d4SFlorian Westphal #include <stdint.h> 12048d19d4SFlorian Westphal #include <stdio.h> 13048d19d4SFlorian Westphal #include <stdlib.h> 14048d19d4SFlorian Westphal #include <strings.h> 15df62f2ecSPaolo Abeni #include <signal.h> 16048d19d4SFlorian Westphal #include <unistd.h> 17b6ab64b0SPaolo Abeni #include <time.h> 18048d19d4SFlorian Westphal 1905be5e27SPaolo Abeni #include <sys/ioctl.h> 20048d19d4SFlorian Westphal #include <sys/poll.h> 21048d19d4SFlorian Westphal #include <sys/sendfile.h> 22048d19d4SFlorian Westphal #include <sys/stat.h> 23048d19d4SFlorian Westphal #include <sys/socket.h> 24048d19d4SFlorian Westphal #include <sys/types.h> 25048d19d4SFlorian Westphal #include <sys/mman.h> 26048d19d4SFlorian Westphal 27048d19d4SFlorian Westphal #include <netdb.h> 28048d19d4SFlorian Westphal #include <netinet/in.h> 29048d19d4SFlorian Westphal 30048d19d4SFlorian Westphal #include <linux/tcp.h> 315e6af0a7SFlorian Westphal #include <linux/time_types.h> 3205be5e27SPaolo Abeni #include <linux/sockios.h> 33048d19d4SFlorian Westphal 34048d19d4SFlorian Westphal extern int optind; 35048d19d4SFlorian Westphal 36048d19d4SFlorian Westphal #ifndef IPPROTO_MPTCP 37048d19d4SFlorian Westphal #define IPPROTO_MPTCP 262 38048d19d4SFlorian Westphal #endif 39048d19d4SFlorian Westphal #ifndef TCP_ULP 40048d19d4SFlorian Westphal #define TCP_ULP 31 41048d19d4SFlorian Westphal #endif 42048d19d4SFlorian Westphal 438a4b910dSFlorian Westphal static int poll_timeout = 10 * 1000; 44048d19d4SFlorian Westphal static bool listen_mode; 45df62f2ecSPaolo Abeni static bool quit; 46048d19d4SFlorian Westphal 47048d19d4SFlorian Westphal enum cfg_mode { 48048d19d4SFlorian Westphal CFG_MODE_POLL, 49048d19d4SFlorian Westphal CFG_MODE_MMAP, 50048d19d4SFlorian Westphal CFG_MODE_SENDFILE, 51048d19d4SFlorian Westphal }; 52048d19d4SFlorian Westphal 53df8aee6dSYonglong Li enum cfg_peek { 54df8aee6dSYonglong Li CFG_NONE_PEEK, 55df8aee6dSYonglong Li CFG_WITH_PEEK, 56df8aee6dSYonglong Li CFG_AFTER_PEEK, 57df8aee6dSYonglong Li }; 58df8aee6dSYonglong Li 59048d19d4SFlorian Westphal static enum cfg_mode cfg_mode = CFG_MODE_POLL; 60df8aee6dSYonglong Li static enum cfg_peek cfg_peek = CFG_NONE_PEEK; 61048d19d4SFlorian Westphal static const char *cfg_host; 62048d19d4SFlorian Westphal static const char *cfg_port = "12000"; 63048d19d4SFlorian Westphal static int cfg_sock_proto = IPPROTO_MPTCP; 64048d19d4SFlorian Westphal static int pf = AF_INET; 65048d19d4SFlorian Westphal static int cfg_sndbuf; 668a4b910dSFlorian Westphal static int cfg_rcvbuf; 67b08fbf24SPaolo Abeni static bool cfg_join; 6813153324SGeliang Tang static bool cfg_remove; 69b6ab64b0SPaolo Abeni static unsigned int cfg_time; 702e580a63SGeliang Tang static unsigned int cfg_do_w; 71df62f2ecSPaolo Abeni static int cfg_wait; 72dc65fe82SFlorian Westphal static uint32_t cfg_mark; 7305be5e27SPaolo Abeni static char *cfg_input; 7405be5e27SPaolo Abeni static int cfg_repeat = 1; 75*6bf41020SPaolo Abeni static int cfg_truncate; 76*6bf41020SPaolo Abeni static int cfg_rcv_trunc; 77048d19d4SFlorian Westphal 785e6af0a7SFlorian Westphal struct cfg_cmsg_types { 795e6af0a7SFlorian Westphal unsigned int cmsg_enabled:1; 805e6af0a7SFlorian Westphal unsigned int timestampns:1; 815cbd886cSFlorian Westphal unsigned int tcp_inq:1; 825e6af0a7SFlorian Westphal }; 835e6af0a7SFlorian Westphal 845fb62e9cSFlorian Westphal struct cfg_sockopt_types { 855fb62e9cSFlorian Westphal unsigned int transparent:1; 865fb62e9cSFlorian Westphal }; 875fb62e9cSFlorian Westphal 885cbd886cSFlorian Westphal struct tcp_inq_state { 895cbd886cSFlorian Westphal unsigned int last; 905cbd886cSFlorian Westphal bool expect_eof; 915cbd886cSFlorian Westphal }; 925cbd886cSFlorian Westphal 935cbd886cSFlorian Westphal static struct tcp_inq_state tcp_inq; 945cbd886cSFlorian Westphal 955e6af0a7SFlorian Westphal static struct cfg_cmsg_types cfg_cmsg_types; 965fb62e9cSFlorian Westphal static struct cfg_sockopt_types cfg_sockopt_types; 975e6af0a7SFlorian Westphal 98048d19d4SFlorian Westphal static void die_usage(void) 99048d19d4SFlorian Westphal { 100*6bf41020SPaolo Abeni fprintf(stderr, "Usage: mptcp_connect [-6] [-c cmsg] [-f offset] [-i file] [-I num] [-j] [-l] " 10105be5e27SPaolo Abeni "[-m mode] [-M mark] [-o option] [-p port] [-P mode] [-j] [-l] [-r num] " 10205be5e27SPaolo Abeni "[-s MPTCP|TCP] [-S num] [-r num] [-t num] [-T num] [-u] [-w sec] connect_address\n"); 1038a4b910dSFlorian Westphal fprintf(stderr, "\t-6 use ipv6\n"); 10405be5e27SPaolo Abeni fprintf(stderr, "\t-c cmsg -- test cmsg type <cmsg>\n"); 105*6bf41020SPaolo Abeni fprintf(stderr, "\t-f offset -- stop the I/O after receiving and sending the specified amount " 106*6bf41020SPaolo Abeni "of bytes. If there are unread bytes in the receive queue, that will cause a MPTCP " 107*6bf41020SPaolo Abeni "fastclose at close/shutdown. If offset is negative, expect the peer to close before " 108*6bf41020SPaolo Abeni "all the local data as been sent, thus toleration errors on write and EPIPE signals\n"); 10905be5e27SPaolo Abeni fprintf(stderr, "\t-i file -- read the data to send from the given file instead of stdin"); 11005be5e27SPaolo Abeni fprintf(stderr, "\t-I num -- repeat the transfer 'num' times. In listen mode accepts num " 11105be5e27SPaolo Abeni "incoming connections, in client mode, disconnect and reconnect to the server\n"); 11205be5e27SPaolo Abeni fprintf(stderr, "\t-j -- add additional sleep at connection start and tear down " 11305be5e27SPaolo Abeni "-- for MPJ tests\n"); 11405be5e27SPaolo Abeni fprintf(stderr, "\t-l -- listens mode, accepts incoming connection\n"); 115c6f4c2b0SDavide Caratti fprintf(stderr, "\t-m [poll|mmap|sendfile] -- use poll(default)/mmap+write/sendfile\n"); 116dc65fe82SFlorian Westphal fprintf(stderr, "\t-M mark -- set socket packet mark\n"); 1175fb62e9cSFlorian Westphal fprintf(stderr, "\t-o option -- test sockopt <option>\n"); 11805be5e27SPaolo Abeni fprintf(stderr, "\t-p num -- use port num\n"); 119df8aee6dSYonglong Li fprintf(stderr, 120df8aee6dSYonglong Li "\t-P [saveWithPeek|saveAfterPeek] -- save data with/after MSG_PEEK form tcp socket\n"); 12105be5e27SPaolo Abeni fprintf(stderr, "\t-t num -- set poll timeout to num\n"); 12205be5e27SPaolo Abeni fprintf(stderr, "\t-T num -- set expected runtime to num ms\n"); 12305be5e27SPaolo Abeni fprintf(stderr, "\t-r num -- enable slow mode, limiting each write to num bytes " 12405be5e27SPaolo Abeni "-- for remove addr tests\n"); 12505be5e27SPaolo Abeni fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n"); 12605be5e27SPaolo Abeni fprintf(stderr, "\t-s [MPTCP|TCP] -- use mptcp(default) or tcp sockets\n"); 12705be5e27SPaolo Abeni fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n"); 12805be5e27SPaolo Abeni fprintf(stderr, "\t-w num -- wait num sec before closing the socket\n"); 129048d19d4SFlorian Westphal exit(1); 130048d19d4SFlorian Westphal } 131048d19d4SFlorian Westphal 1325e6af0a7SFlorian Westphal static void xerror(const char *fmt, ...) 1335e6af0a7SFlorian Westphal { 1345e6af0a7SFlorian Westphal va_list ap; 1355e6af0a7SFlorian Westphal 1365e6af0a7SFlorian Westphal va_start(ap, fmt); 1375e6af0a7SFlorian Westphal vfprintf(stderr, fmt, ap); 1385e6af0a7SFlorian Westphal va_end(ap); 1395e6af0a7SFlorian Westphal exit(1); 1405e6af0a7SFlorian Westphal } 1415e6af0a7SFlorian Westphal 142df62f2ecSPaolo Abeni static void handle_signal(int nr) 143df62f2ecSPaolo Abeni { 144df62f2ecSPaolo Abeni quit = true; 145df62f2ecSPaolo Abeni } 146df62f2ecSPaolo Abeni 147048d19d4SFlorian Westphal static const char *getxinfo_strerr(int err) 148048d19d4SFlorian Westphal { 149048d19d4SFlorian Westphal if (err == EAI_SYSTEM) 150048d19d4SFlorian Westphal return strerror(errno); 151048d19d4SFlorian Westphal 152048d19d4SFlorian Westphal return gai_strerror(err); 153048d19d4SFlorian Westphal } 154048d19d4SFlorian Westphal 155048d19d4SFlorian Westphal static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen, 156048d19d4SFlorian Westphal char *host, socklen_t hostlen, 157048d19d4SFlorian Westphal char *serv, socklen_t servlen) 158048d19d4SFlorian Westphal { 159048d19d4SFlorian Westphal int flags = NI_NUMERICHOST | NI_NUMERICSERV; 160048d19d4SFlorian Westphal int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen, 161048d19d4SFlorian Westphal flags); 162048d19d4SFlorian Westphal 163048d19d4SFlorian Westphal if (err) { 164048d19d4SFlorian Westphal const char *errstr = getxinfo_strerr(err); 165048d19d4SFlorian Westphal 166048d19d4SFlorian Westphal fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr); 167048d19d4SFlorian Westphal exit(1); 168048d19d4SFlorian Westphal } 169048d19d4SFlorian Westphal } 170048d19d4SFlorian Westphal 171048d19d4SFlorian Westphal static void xgetaddrinfo(const char *node, const char *service, 172048d19d4SFlorian Westphal const struct addrinfo *hints, 173048d19d4SFlorian Westphal struct addrinfo **res) 174048d19d4SFlorian Westphal { 175048d19d4SFlorian Westphal int err = getaddrinfo(node, service, hints, res); 176048d19d4SFlorian Westphal 177048d19d4SFlorian Westphal if (err) { 178048d19d4SFlorian Westphal const char *errstr = getxinfo_strerr(err); 179048d19d4SFlorian Westphal 180048d19d4SFlorian Westphal fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n", 181048d19d4SFlorian Westphal node ? node : "", service ? service : "", errstr); 182048d19d4SFlorian Westphal exit(1); 183048d19d4SFlorian Westphal } 184048d19d4SFlorian Westphal } 185048d19d4SFlorian Westphal 1868a4b910dSFlorian Westphal static void set_rcvbuf(int fd, unsigned int size) 1878a4b910dSFlorian Westphal { 1888a4b910dSFlorian Westphal int err; 1898a4b910dSFlorian Westphal 1908a4b910dSFlorian Westphal err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); 1918a4b910dSFlorian Westphal if (err) { 1928a4b910dSFlorian Westphal perror("set SO_RCVBUF"); 1938a4b910dSFlorian Westphal exit(1); 1948a4b910dSFlorian Westphal } 1958a4b910dSFlorian Westphal } 1968a4b910dSFlorian Westphal 197048d19d4SFlorian Westphal static void set_sndbuf(int fd, unsigned int size) 198048d19d4SFlorian Westphal { 199048d19d4SFlorian Westphal int err; 200048d19d4SFlorian Westphal 201048d19d4SFlorian Westphal err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)); 202048d19d4SFlorian Westphal if (err) { 203048d19d4SFlorian Westphal perror("set SO_SNDBUF"); 204048d19d4SFlorian Westphal exit(1); 205048d19d4SFlorian Westphal } 206048d19d4SFlorian Westphal } 207048d19d4SFlorian Westphal 208dc65fe82SFlorian Westphal static void set_mark(int fd, uint32_t mark) 209dc65fe82SFlorian Westphal { 210dc65fe82SFlorian Westphal int err; 211dc65fe82SFlorian Westphal 212dc65fe82SFlorian Westphal err = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); 213dc65fe82SFlorian Westphal if (err) { 214dc65fe82SFlorian Westphal perror("set SO_MARK"); 215dc65fe82SFlorian Westphal exit(1); 216dc65fe82SFlorian Westphal } 217dc65fe82SFlorian Westphal } 218dc65fe82SFlorian Westphal 2195fb62e9cSFlorian Westphal static void set_transparent(int fd, int pf) 2205fb62e9cSFlorian Westphal { 2215fb62e9cSFlorian Westphal int one = 1; 2225fb62e9cSFlorian Westphal 2235fb62e9cSFlorian Westphal switch (pf) { 2245fb62e9cSFlorian Westphal case AF_INET: 2255fb62e9cSFlorian Westphal if (-1 == setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one))) 2265fb62e9cSFlorian Westphal perror("IP_TRANSPARENT"); 2275fb62e9cSFlorian Westphal break; 2285fb62e9cSFlorian Westphal case AF_INET6: 2295fb62e9cSFlorian Westphal if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one))) 2305fb62e9cSFlorian Westphal perror("IPV6_TRANSPARENT"); 2315fb62e9cSFlorian Westphal break; 2325fb62e9cSFlorian Westphal } 2335fb62e9cSFlorian Westphal } 2345fb62e9cSFlorian Westphal 235f730b65cSFlorian Westphal static int do_ulp_so(int sock, const char *name) 236f730b65cSFlorian Westphal { 237f730b65cSFlorian Westphal return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name)); 238f730b65cSFlorian Westphal } 239f730b65cSFlorian Westphal 240f730b65cSFlorian Westphal #define X(m) xerror("%s:%u: %s: failed for proto %d at line %u", __FILE__, __LINE__, (m), proto, line) 241f730b65cSFlorian Westphal static void sock_test_tcpulp(int sock, int proto, unsigned int line) 242f730b65cSFlorian Westphal { 243f730b65cSFlorian Westphal socklen_t buflen = 8; 244f730b65cSFlorian Westphal char buf[8] = ""; 245f730b65cSFlorian Westphal int ret = getsockopt(sock, IPPROTO_TCP, TCP_ULP, buf, &buflen); 246f730b65cSFlorian Westphal 247f730b65cSFlorian Westphal if (ret != 0) 248f730b65cSFlorian Westphal X("getsockopt"); 249f730b65cSFlorian Westphal 250f730b65cSFlorian Westphal if (buflen > 0) { 251f730b65cSFlorian Westphal if (strcmp(buf, "mptcp") != 0) 252f730b65cSFlorian Westphal xerror("unexpected ULP '%s' for proto %d at line %u", buf, proto, line); 253f730b65cSFlorian Westphal ret = do_ulp_so(sock, "tls"); 254f730b65cSFlorian Westphal if (ret == 0) 255f730b65cSFlorian Westphal X("setsockopt"); 256f730b65cSFlorian Westphal } else if (proto == IPPROTO_MPTCP) { 257f730b65cSFlorian Westphal ret = do_ulp_so(sock, "tls"); 258f730b65cSFlorian Westphal if (ret != -1) 259f730b65cSFlorian Westphal X("setsockopt"); 260f730b65cSFlorian Westphal } 261f730b65cSFlorian Westphal 262f730b65cSFlorian Westphal ret = do_ulp_so(sock, "mptcp"); 263f730b65cSFlorian Westphal if (ret != -1) 264f730b65cSFlorian Westphal X("setsockopt"); 265f730b65cSFlorian Westphal 266f730b65cSFlorian Westphal #undef X 267f730b65cSFlorian Westphal } 268f730b65cSFlorian Westphal 269f730b65cSFlorian Westphal #define SOCK_TEST_TCPULP(s, p) sock_test_tcpulp((s), (p), __LINE__) 270f730b65cSFlorian Westphal 271048d19d4SFlorian Westphal static int sock_listen_mptcp(const char * const listenaddr, 272048d19d4SFlorian Westphal const char * const port) 273048d19d4SFlorian Westphal { 274fd37c2ecSMat Martineau int sock = -1; 275048d19d4SFlorian Westphal struct addrinfo hints = { 276048d19d4SFlorian Westphal .ai_protocol = IPPROTO_TCP, 277048d19d4SFlorian Westphal .ai_socktype = SOCK_STREAM, 278048d19d4SFlorian Westphal .ai_flags = AI_PASSIVE | AI_NUMERICHOST 279048d19d4SFlorian Westphal }; 280048d19d4SFlorian Westphal 281048d19d4SFlorian Westphal hints.ai_family = pf; 282048d19d4SFlorian Westphal 283048d19d4SFlorian Westphal struct addrinfo *a, *addr; 284048d19d4SFlorian Westphal int one = 1; 285048d19d4SFlorian Westphal 286048d19d4SFlorian Westphal xgetaddrinfo(listenaddr, port, &hints, &addr); 287048d19d4SFlorian Westphal hints.ai_family = pf; 288048d19d4SFlorian Westphal 289048d19d4SFlorian Westphal for (a = addr; a; a = a->ai_next) { 290048d19d4SFlorian Westphal sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto); 291048d19d4SFlorian Westphal if (sock < 0) 292048d19d4SFlorian Westphal continue; 293048d19d4SFlorian Westphal 294f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, cfg_sock_proto); 295f730b65cSFlorian Westphal 296048d19d4SFlorian Westphal if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, 297048d19d4SFlorian Westphal sizeof(one))) 298048d19d4SFlorian Westphal perror("setsockopt"); 299048d19d4SFlorian Westphal 3005fb62e9cSFlorian Westphal if (cfg_sockopt_types.transparent) 3015fb62e9cSFlorian Westphal set_transparent(sock, pf); 3025fb62e9cSFlorian Westphal 303048d19d4SFlorian Westphal if (bind(sock, a->ai_addr, a->ai_addrlen) == 0) 304048d19d4SFlorian Westphal break; /* success */ 305048d19d4SFlorian Westphal 306048d19d4SFlorian Westphal perror("bind"); 307048d19d4SFlorian Westphal close(sock); 308048d19d4SFlorian Westphal sock = -1; 309048d19d4SFlorian Westphal } 310048d19d4SFlorian Westphal 311048d19d4SFlorian Westphal freeaddrinfo(addr); 312048d19d4SFlorian Westphal 313048d19d4SFlorian Westphal if (sock < 0) { 314048d19d4SFlorian Westphal fprintf(stderr, "Could not create listen socket\n"); 315048d19d4SFlorian Westphal return sock; 316048d19d4SFlorian Westphal } 317048d19d4SFlorian Westphal 318f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, cfg_sock_proto); 319f730b65cSFlorian Westphal 320048d19d4SFlorian Westphal if (listen(sock, 20)) { 321048d19d4SFlorian Westphal perror("listen"); 322048d19d4SFlorian Westphal close(sock); 323048d19d4SFlorian Westphal return -1; 324048d19d4SFlorian Westphal } 325048d19d4SFlorian Westphal 326f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, cfg_sock_proto); 327f730b65cSFlorian Westphal 328048d19d4SFlorian Westphal return sock; 329048d19d4SFlorian Westphal } 330048d19d4SFlorian Westphal 331048d19d4SFlorian Westphal static int sock_connect_mptcp(const char * const remoteaddr, 33205be5e27SPaolo Abeni const char * const port, int proto, 33305be5e27SPaolo Abeni struct addrinfo **peer) 334048d19d4SFlorian Westphal { 335048d19d4SFlorian Westphal struct addrinfo hints = { 336048d19d4SFlorian Westphal .ai_protocol = IPPROTO_TCP, 337048d19d4SFlorian Westphal .ai_socktype = SOCK_STREAM, 338048d19d4SFlorian Westphal }; 339048d19d4SFlorian Westphal struct addrinfo *a, *addr; 340048d19d4SFlorian Westphal int sock = -1; 341048d19d4SFlorian Westphal 342048d19d4SFlorian Westphal hints.ai_family = pf; 343048d19d4SFlorian Westphal 344048d19d4SFlorian Westphal xgetaddrinfo(remoteaddr, port, &hints, &addr); 345048d19d4SFlorian Westphal for (a = addr; a; a = a->ai_next) { 346048d19d4SFlorian Westphal sock = socket(a->ai_family, a->ai_socktype, proto); 347048d19d4SFlorian Westphal if (sock < 0) { 348048d19d4SFlorian Westphal perror("socket"); 349048d19d4SFlorian Westphal continue; 350048d19d4SFlorian Westphal } 351048d19d4SFlorian Westphal 352f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, proto); 353f730b65cSFlorian Westphal 354dc65fe82SFlorian Westphal if (cfg_mark) 355dc65fe82SFlorian Westphal set_mark(sock, cfg_mark); 356dc65fe82SFlorian Westphal 35705be5e27SPaolo Abeni if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) { 35805be5e27SPaolo Abeni *peer = a; 359048d19d4SFlorian Westphal break; /* success */ 36005be5e27SPaolo Abeni } 361048d19d4SFlorian Westphal 362048d19d4SFlorian Westphal perror("connect()"); 363048d19d4SFlorian Westphal close(sock); 364048d19d4SFlorian Westphal sock = -1; 365048d19d4SFlorian Westphal } 366048d19d4SFlorian Westphal 367048d19d4SFlorian Westphal freeaddrinfo(addr); 368f730b65cSFlorian Westphal if (sock != -1) 369f730b65cSFlorian Westphal SOCK_TEST_TCPULP(sock, proto); 370048d19d4SFlorian Westphal return sock; 371048d19d4SFlorian Westphal } 372048d19d4SFlorian Westphal 373048d19d4SFlorian Westphal static size_t do_rnd_write(const int fd, char *buf, const size_t len) 374048d19d4SFlorian Westphal { 375b08fbf24SPaolo Abeni static bool first = true; 376048d19d4SFlorian Westphal unsigned int do_w; 377048d19d4SFlorian Westphal ssize_t bw; 378048d19d4SFlorian Westphal 379048d19d4SFlorian Westphal do_w = rand() & 0xffff; 380048d19d4SFlorian Westphal if (do_w == 0 || do_w > len) 381048d19d4SFlorian Westphal do_w = len; 382048d19d4SFlorian Westphal 383b08fbf24SPaolo Abeni if (cfg_join && first && do_w > 100) 384b08fbf24SPaolo Abeni do_w = 100; 385b08fbf24SPaolo Abeni 3862e580a63SGeliang Tang if (cfg_remove && do_w > cfg_do_w) 3872e580a63SGeliang Tang do_w = cfg_do_w; 38813153324SGeliang Tang 389048d19d4SFlorian Westphal bw = write(fd, buf, do_w); 390048d19d4SFlorian Westphal if (bw < 0) 391*6bf41020SPaolo Abeni return bw; 392048d19d4SFlorian Westphal 393b08fbf24SPaolo Abeni /* let the join handshake complete, before going on */ 394b08fbf24SPaolo Abeni if (cfg_join && first) { 395b08fbf24SPaolo Abeni usleep(200000); 396b08fbf24SPaolo Abeni first = false; 397b08fbf24SPaolo Abeni } 398b08fbf24SPaolo Abeni 39913153324SGeliang Tang if (cfg_remove) 40013153324SGeliang Tang usleep(200000); 40113153324SGeliang Tang 402048d19d4SFlorian Westphal return bw; 403048d19d4SFlorian Westphal } 404048d19d4SFlorian Westphal 405048d19d4SFlorian Westphal static size_t do_write(const int fd, char *buf, const size_t len) 406048d19d4SFlorian Westphal { 407048d19d4SFlorian Westphal size_t offset = 0; 408048d19d4SFlorian Westphal 409048d19d4SFlorian Westphal while (offset < len) { 410048d19d4SFlorian Westphal size_t written; 411048d19d4SFlorian Westphal ssize_t bw; 412048d19d4SFlorian Westphal 413048d19d4SFlorian Westphal bw = write(fd, buf + offset, len - offset); 414048d19d4SFlorian Westphal if (bw < 0) { 415048d19d4SFlorian Westphal perror("write"); 416048d19d4SFlorian Westphal return 0; 417048d19d4SFlorian Westphal } 418048d19d4SFlorian Westphal 419048d19d4SFlorian Westphal written = (size_t)bw; 420048d19d4SFlorian Westphal offset += written; 421048d19d4SFlorian Westphal } 422048d19d4SFlorian Westphal 423048d19d4SFlorian Westphal return offset; 424048d19d4SFlorian Westphal } 425048d19d4SFlorian Westphal 4265e6af0a7SFlorian Westphal static void process_cmsg(struct msghdr *msgh) 4275e6af0a7SFlorian Westphal { 4285e6af0a7SFlorian Westphal struct __kernel_timespec ts; 4295cbd886cSFlorian Westphal bool inq_found = false; 4305e6af0a7SFlorian Westphal bool ts_found = false; 4315cbd886cSFlorian Westphal unsigned int inq = 0; 4325e6af0a7SFlorian Westphal struct cmsghdr *cmsg; 4335e6af0a7SFlorian Westphal 4345e6af0a7SFlorian Westphal for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) { 4355e6af0a7SFlorian Westphal if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) { 4365e6af0a7SFlorian Westphal memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts)); 4375e6af0a7SFlorian Westphal ts_found = true; 4385e6af0a7SFlorian Westphal continue; 4395e6af0a7SFlorian Westphal } 4405cbd886cSFlorian Westphal if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) { 4415cbd886cSFlorian Westphal memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq)); 4425cbd886cSFlorian Westphal inq_found = true; 4435cbd886cSFlorian Westphal continue; 4445cbd886cSFlorian Westphal } 4455cbd886cSFlorian Westphal 4465e6af0a7SFlorian Westphal } 4475e6af0a7SFlorian Westphal 4485e6af0a7SFlorian Westphal if (cfg_cmsg_types.timestampns) { 4495e6af0a7SFlorian Westphal if (!ts_found) 4505e6af0a7SFlorian Westphal xerror("TIMESTAMPNS not present\n"); 4515e6af0a7SFlorian Westphal } 4525cbd886cSFlorian Westphal 4535cbd886cSFlorian Westphal if (cfg_cmsg_types.tcp_inq) { 4545cbd886cSFlorian Westphal if (!inq_found) 4555cbd886cSFlorian Westphal xerror("TCP_INQ not present\n"); 4565cbd886cSFlorian Westphal 4575cbd886cSFlorian Westphal if (inq > 1024) 4585cbd886cSFlorian Westphal xerror("tcp_inq %u is larger than one kbyte\n", inq); 4595cbd886cSFlorian Westphal tcp_inq.last = inq; 4605cbd886cSFlorian Westphal } 4615e6af0a7SFlorian Westphal } 4625e6af0a7SFlorian Westphal 4635e6af0a7SFlorian Westphal static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len) 4645e6af0a7SFlorian Westphal { 4655e6af0a7SFlorian Westphal char msg_buf[8192]; 4665e6af0a7SFlorian Westphal struct iovec iov = { 4675e6af0a7SFlorian Westphal .iov_base = buf, 4685e6af0a7SFlorian Westphal .iov_len = len, 4695e6af0a7SFlorian Westphal }; 4705e6af0a7SFlorian Westphal struct msghdr msg = { 4715e6af0a7SFlorian Westphal .msg_iov = &iov, 4725e6af0a7SFlorian Westphal .msg_iovlen = 1, 4735e6af0a7SFlorian Westphal .msg_control = msg_buf, 4745e6af0a7SFlorian Westphal .msg_controllen = sizeof(msg_buf), 4755e6af0a7SFlorian Westphal }; 4765e6af0a7SFlorian Westphal int flags = 0; 4775cbd886cSFlorian Westphal unsigned int last_hint = tcp_inq.last; 4785e6af0a7SFlorian Westphal int ret = recvmsg(fd, &msg, flags); 4795e6af0a7SFlorian Westphal 4805cbd886cSFlorian Westphal if (ret <= 0) { 4815cbd886cSFlorian Westphal if (ret == 0 && tcp_inq.expect_eof) 4825e6af0a7SFlorian Westphal return ret; 4835e6af0a7SFlorian Westphal 4845cbd886cSFlorian Westphal if (ret == 0 && cfg_cmsg_types.tcp_inq) 4855cbd886cSFlorian Westphal if (last_hint != 1 && last_hint != 0) 4865cbd886cSFlorian Westphal xerror("EOF but last tcp_inq hint was %u\n", last_hint); 4875cbd886cSFlorian Westphal 4885cbd886cSFlorian Westphal return ret; 4895cbd886cSFlorian Westphal } 4905cbd886cSFlorian Westphal 4915cbd886cSFlorian Westphal if (tcp_inq.expect_eof) 4925cbd886cSFlorian Westphal xerror("expected EOF, last_hint %u, now %u\n", 4935cbd886cSFlorian Westphal last_hint, tcp_inq.last); 4945cbd886cSFlorian Westphal 4955e6af0a7SFlorian Westphal if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled) 4965e6af0a7SFlorian Westphal xerror("got %lu bytes of cmsg data, expected 0\n", 4975e6af0a7SFlorian Westphal (unsigned long)msg.msg_controllen); 4985e6af0a7SFlorian Westphal 4995e6af0a7SFlorian Westphal if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled) 5005e6af0a7SFlorian Westphal xerror("%s\n", "got no cmsg data"); 5015e6af0a7SFlorian Westphal 5025e6af0a7SFlorian Westphal if (msg.msg_controllen) 5035e6af0a7SFlorian Westphal process_cmsg(&msg); 5045e6af0a7SFlorian Westphal 5055cbd886cSFlorian Westphal if (cfg_cmsg_types.tcp_inq) { 5065cbd886cSFlorian Westphal if ((size_t)ret < len && last_hint > (unsigned int)ret) { 5075cbd886cSFlorian Westphal if (ret + 1 != (int)last_hint) { 5085cbd886cSFlorian Westphal int next = read(fd, msg_buf, sizeof(msg_buf)); 5095cbd886cSFlorian Westphal 5105cbd886cSFlorian Westphal xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n", 5115cbd886cSFlorian Westphal ret, (unsigned int)len, last_hint, tcp_inq.last, next); 5125cbd886cSFlorian Westphal } else { 5135cbd886cSFlorian Westphal tcp_inq.expect_eof = true; 5145cbd886cSFlorian Westphal } 5155cbd886cSFlorian Westphal } 5165cbd886cSFlorian Westphal } 5175cbd886cSFlorian Westphal 5185e6af0a7SFlorian Westphal return ret; 5195e6af0a7SFlorian Westphal } 5205e6af0a7SFlorian Westphal 521048d19d4SFlorian Westphal static ssize_t do_rnd_read(const int fd, char *buf, const size_t len) 522048d19d4SFlorian Westphal { 523df8aee6dSYonglong Li int ret = 0; 524df8aee6dSYonglong Li char tmp[16384]; 525048d19d4SFlorian Westphal size_t cap = rand(); 526048d19d4SFlorian Westphal 527048d19d4SFlorian Westphal cap &= 0xffff; 528048d19d4SFlorian Westphal 529048d19d4SFlorian Westphal if (cap == 0) 530048d19d4SFlorian Westphal cap = 1; 531048d19d4SFlorian Westphal else if (cap > len) 532048d19d4SFlorian Westphal cap = len; 533048d19d4SFlorian Westphal 534df8aee6dSYonglong Li if (cfg_peek == CFG_WITH_PEEK) { 535df8aee6dSYonglong Li ret = recv(fd, buf, cap, MSG_PEEK); 536df8aee6dSYonglong Li ret = (ret < 0) ? ret : read(fd, tmp, ret); 537df8aee6dSYonglong Li } else if (cfg_peek == CFG_AFTER_PEEK) { 538df8aee6dSYonglong Li ret = recv(fd, buf, cap, MSG_PEEK); 539df8aee6dSYonglong Li ret = (ret < 0) ? ret : read(fd, buf, cap); 5405e6af0a7SFlorian Westphal } else if (cfg_cmsg_types.cmsg_enabled) { 5415e6af0a7SFlorian Westphal ret = do_recvmsg_cmsg(fd, buf, cap); 542df8aee6dSYonglong Li } else { 543df8aee6dSYonglong Li ret = read(fd, buf, cap); 544df8aee6dSYonglong Li } 545df8aee6dSYonglong Li 546df8aee6dSYonglong Li return ret; 547048d19d4SFlorian Westphal } 548048d19d4SFlorian Westphal 54905be5e27SPaolo Abeni static void set_nonblock(int fd, bool nonblock) 550048d19d4SFlorian Westphal { 551048d19d4SFlorian Westphal int flags = fcntl(fd, F_GETFL); 552048d19d4SFlorian Westphal 553048d19d4SFlorian Westphal if (flags == -1) 554048d19d4SFlorian Westphal return; 555048d19d4SFlorian Westphal 55605be5e27SPaolo Abeni if (nonblock) 557048d19d4SFlorian Westphal fcntl(fd, F_SETFL, flags | O_NONBLOCK); 55805be5e27SPaolo Abeni else 55905be5e27SPaolo Abeni fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 560048d19d4SFlorian Westphal } 561048d19d4SFlorian Westphal 562df9e03aeSFlorian Westphal static void shut_wr(int fd) 563df9e03aeSFlorian Westphal { 564df9e03aeSFlorian Westphal /* Close our write side, ev. give some time 565df9e03aeSFlorian Westphal * for address notification and/or checking 566df9e03aeSFlorian Westphal * the current status 567df9e03aeSFlorian Westphal */ 568df9e03aeSFlorian Westphal if (cfg_wait) 569df9e03aeSFlorian Westphal usleep(cfg_wait); 570df9e03aeSFlorian Westphal 571df9e03aeSFlorian Westphal shutdown(fd, SHUT_WR); 572df9e03aeSFlorian Westphal } 573df9e03aeSFlorian Westphal 574b6ab64b0SPaolo Abeni static int copyfd_io_poll(int infd, int peerfd, int outfd, bool *in_closed_after_out) 575048d19d4SFlorian Westphal { 576048d19d4SFlorian Westphal struct pollfd fds = { 577048d19d4SFlorian Westphal .fd = peerfd, 578048d19d4SFlorian Westphal .events = POLLIN | POLLOUT, 579048d19d4SFlorian Westphal }; 580*6bf41020SPaolo Abeni unsigned int woff = 0, wlen = 0, total_wlen = 0, total_rlen = 0; 581048d19d4SFlorian Westphal char wbuf[8192]; 582048d19d4SFlorian Westphal 58305be5e27SPaolo Abeni set_nonblock(peerfd, true); 584048d19d4SFlorian Westphal 585048d19d4SFlorian Westphal for (;;) { 586048d19d4SFlorian Westphal char rbuf[8192]; 587048d19d4SFlorian Westphal ssize_t len; 588048d19d4SFlorian Westphal 589048d19d4SFlorian Westphal if (fds.events == 0) 590048d19d4SFlorian Westphal break; 591048d19d4SFlorian Westphal 592048d19d4SFlorian Westphal switch (poll(&fds, 1, poll_timeout)) { 593048d19d4SFlorian Westphal case -1: 594048d19d4SFlorian Westphal if (errno == EINTR) 595048d19d4SFlorian Westphal continue; 596048d19d4SFlorian Westphal perror("poll"); 597048d19d4SFlorian Westphal return 1; 598048d19d4SFlorian Westphal case 0: 599048d19d4SFlorian Westphal fprintf(stderr, "%s: poll timed out (events: " 600048d19d4SFlorian Westphal "POLLIN %u, POLLOUT %u)\n", __func__, 601048d19d4SFlorian Westphal fds.events & POLLIN, fds.events & POLLOUT); 602048d19d4SFlorian Westphal return 2; 603048d19d4SFlorian Westphal } 604048d19d4SFlorian Westphal 605048d19d4SFlorian Westphal if (fds.revents & POLLIN) { 606*6bf41020SPaolo Abeni ssize_t rb = sizeof(rbuf); 607*6bf41020SPaolo Abeni 608*6bf41020SPaolo Abeni /* limit the total amount of read data to the trunc value*/ 609*6bf41020SPaolo Abeni if (cfg_truncate > 0) { 610*6bf41020SPaolo Abeni if (rb + total_rlen > cfg_truncate) 611*6bf41020SPaolo Abeni rb = cfg_truncate - total_rlen; 612*6bf41020SPaolo Abeni len = read(peerfd, rbuf, rb); 613*6bf41020SPaolo Abeni } else { 614048d19d4SFlorian Westphal len = do_rnd_read(peerfd, rbuf, sizeof(rbuf)); 615*6bf41020SPaolo Abeni } 616048d19d4SFlorian Westphal if (len == 0) { 617048d19d4SFlorian Westphal /* no more data to receive: 618048d19d4SFlorian Westphal * peer has closed its write side 619048d19d4SFlorian Westphal */ 620048d19d4SFlorian Westphal fds.events &= ~POLLIN; 621048d19d4SFlorian Westphal 622b6ab64b0SPaolo Abeni if ((fds.events & POLLOUT) == 0) { 623b6ab64b0SPaolo Abeni *in_closed_after_out = true; 624048d19d4SFlorian Westphal /* and nothing more to send */ 625048d19d4SFlorian Westphal break; 626b6ab64b0SPaolo Abeni } 627048d19d4SFlorian Westphal 628048d19d4SFlorian Westphal /* Else, still have data to transmit */ 629048d19d4SFlorian Westphal } else if (len < 0) { 630*6bf41020SPaolo Abeni if (cfg_rcv_trunc) 631*6bf41020SPaolo Abeni return 0; 632048d19d4SFlorian Westphal perror("read"); 633048d19d4SFlorian Westphal return 3; 634048d19d4SFlorian Westphal } 635048d19d4SFlorian Westphal 636*6bf41020SPaolo Abeni total_rlen += len; 637048d19d4SFlorian Westphal do_write(outfd, rbuf, len); 638048d19d4SFlorian Westphal } 639048d19d4SFlorian Westphal 640048d19d4SFlorian Westphal if (fds.revents & POLLOUT) { 641048d19d4SFlorian Westphal if (wlen == 0) { 642048d19d4SFlorian Westphal woff = 0; 643048d19d4SFlorian Westphal wlen = read(infd, wbuf, sizeof(wbuf)); 644048d19d4SFlorian Westphal } 645048d19d4SFlorian Westphal 646048d19d4SFlorian Westphal if (wlen > 0) { 647048d19d4SFlorian Westphal ssize_t bw; 648048d19d4SFlorian Westphal 649*6bf41020SPaolo Abeni /* limit the total amount of written data to the trunc value */ 650*6bf41020SPaolo Abeni if (cfg_truncate > 0 && wlen + total_wlen > cfg_truncate) 651*6bf41020SPaolo Abeni wlen = cfg_truncate - total_wlen; 652*6bf41020SPaolo Abeni 653048d19d4SFlorian Westphal bw = do_rnd_write(peerfd, wbuf + woff, wlen); 654*6bf41020SPaolo Abeni if (bw < 0) { 655*6bf41020SPaolo Abeni if (cfg_rcv_trunc) 656*6bf41020SPaolo Abeni return 0; 657*6bf41020SPaolo Abeni perror("write"); 658048d19d4SFlorian Westphal return 111; 659*6bf41020SPaolo Abeni } 660048d19d4SFlorian Westphal 661048d19d4SFlorian Westphal woff += bw; 662048d19d4SFlorian Westphal wlen -= bw; 663*6bf41020SPaolo Abeni total_wlen += bw; 664048d19d4SFlorian Westphal } else if (wlen == 0) { 665048d19d4SFlorian Westphal /* We have no more data to send. */ 666048d19d4SFlorian Westphal fds.events &= ~POLLOUT; 667048d19d4SFlorian Westphal 668048d19d4SFlorian Westphal if ((fds.events & POLLIN) == 0) 669048d19d4SFlorian Westphal /* ... and peer also closed already */ 670048d19d4SFlorian Westphal break; 671048d19d4SFlorian Westphal 672df9e03aeSFlorian Westphal shut_wr(peerfd); 673048d19d4SFlorian Westphal } else { 674048d19d4SFlorian Westphal if (errno == EINTR) 675048d19d4SFlorian Westphal continue; 676048d19d4SFlorian Westphal perror("read"); 677048d19d4SFlorian Westphal return 4; 678048d19d4SFlorian Westphal } 679048d19d4SFlorian Westphal } 680048d19d4SFlorian Westphal 681048d19d4SFlorian Westphal if (fds.revents & (POLLERR | POLLNVAL)) { 682*6bf41020SPaolo Abeni if (cfg_rcv_trunc) 683*6bf41020SPaolo Abeni return 0; 684048d19d4SFlorian Westphal fprintf(stderr, "Unexpected revents: " 685048d19d4SFlorian Westphal "POLLERR/POLLNVAL(%x)\n", fds.revents); 686048d19d4SFlorian Westphal return 5; 687048d19d4SFlorian Westphal } 688*6bf41020SPaolo Abeni 689*6bf41020SPaolo Abeni if (cfg_truncate > 0 && total_wlen >= cfg_truncate && 690*6bf41020SPaolo Abeni total_rlen >= cfg_truncate) 691*6bf41020SPaolo Abeni break; 692048d19d4SFlorian Westphal } 693048d19d4SFlorian Westphal 694b08fbf24SPaolo Abeni /* leave some time for late join/announce */ 695b6ab64b0SPaolo Abeni if (cfg_remove) 696df62f2ecSPaolo Abeni usleep(cfg_wait); 697b08fbf24SPaolo Abeni 698048d19d4SFlorian Westphal return 0; 699048d19d4SFlorian Westphal } 700048d19d4SFlorian Westphal 701048d19d4SFlorian Westphal static int do_recvfile(int infd, int outfd) 702048d19d4SFlorian Westphal { 703048d19d4SFlorian Westphal ssize_t r; 704048d19d4SFlorian Westphal 705048d19d4SFlorian Westphal do { 706048d19d4SFlorian Westphal char buf[16384]; 707048d19d4SFlorian Westphal 708048d19d4SFlorian Westphal r = do_rnd_read(infd, buf, sizeof(buf)); 709048d19d4SFlorian Westphal if (r > 0) { 710048d19d4SFlorian Westphal if (write(outfd, buf, r) != r) 711048d19d4SFlorian Westphal break; 712048d19d4SFlorian Westphal } else if (r < 0) { 713048d19d4SFlorian Westphal perror("read"); 714048d19d4SFlorian Westphal } 715048d19d4SFlorian Westphal } while (r > 0); 716048d19d4SFlorian Westphal 717048d19d4SFlorian Westphal return (int)r; 718048d19d4SFlorian Westphal } 719048d19d4SFlorian Westphal 720048d19d4SFlorian Westphal static int do_mmap(int infd, int outfd, unsigned int size) 721048d19d4SFlorian Westphal { 722048d19d4SFlorian Westphal char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0); 723048d19d4SFlorian Westphal ssize_t ret = 0, off = 0; 724048d19d4SFlorian Westphal size_t rem; 725048d19d4SFlorian Westphal 726048d19d4SFlorian Westphal if (inbuf == MAP_FAILED) { 727048d19d4SFlorian Westphal perror("mmap"); 728048d19d4SFlorian Westphal return 1; 729048d19d4SFlorian Westphal } 730048d19d4SFlorian Westphal 731048d19d4SFlorian Westphal rem = size; 732048d19d4SFlorian Westphal 733048d19d4SFlorian Westphal while (rem > 0) { 734048d19d4SFlorian Westphal ret = write(outfd, inbuf + off, rem); 735048d19d4SFlorian Westphal 736048d19d4SFlorian Westphal if (ret < 0) { 737048d19d4SFlorian Westphal perror("write"); 738048d19d4SFlorian Westphal break; 739048d19d4SFlorian Westphal } 740048d19d4SFlorian Westphal 741048d19d4SFlorian Westphal off += ret; 742048d19d4SFlorian Westphal rem -= ret; 743048d19d4SFlorian Westphal } 744048d19d4SFlorian Westphal 745048d19d4SFlorian Westphal munmap(inbuf, size); 746048d19d4SFlorian Westphal return rem; 747048d19d4SFlorian Westphal } 748048d19d4SFlorian Westphal 749048d19d4SFlorian Westphal static int get_infd_size(int fd) 750048d19d4SFlorian Westphal { 751048d19d4SFlorian Westphal struct stat sb; 752048d19d4SFlorian Westphal ssize_t count; 753048d19d4SFlorian Westphal int err; 754048d19d4SFlorian Westphal 755048d19d4SFlorian Westphal err = fstat(fd, &sb); 756048d19d4SFlorian Westphal if (err < 0) { 757048d19d4SFlorian Westphal perror("fstat"); 758048d19d4SFlorian Westphal return -1; 759048d19d4SFlorian Westphal } 760048d19d4SFlorian Westphal 761048d19d4SFlorian Westphal if ((sb.st_mode & S_IFMT) != S_IFREG) { 762048d19d4SFlorian Westphal fprintf(stderr, "%s: stdin is not a regular file\n", __func__); 763048d19d4SFlorian Westphal return -2; 764048d19d4SFlorian Westphal } 765048d19d4SFlorian Westphal 766048d19d4SFlorian Westphal count = sb.st_size; 767048d19d4SFlorian Westphal if (count > INT_MAX) { 768048d19d4SFlorian Westphal fprintf(stderr, "File too large: %zu\n", count); 769048d19d4SFlorian Westphal return -3; 770048d19d4SFlorian Westphal } 771048d19d4SFlorian Westphal 772048d19d4SFlorian Westphal return (int)count; 773048d19d4SFlorian Westphal } 774048d19d4SFlorian Westphal 775048d19d4SFlorian Westphal static int do_sendfile(int infd, int outfd, unsigned int count) 776048d19d4SFlorian Westphal { 777048d19d4SFlorian Westphal while (count > 0) { 778048d19d4SFlorian Westphal ssize_t r; 779048d19d4SFlorian Westphal 780048d19d4SFlorian Westphal r = sendfile(outfd, infd, NULL, count); 781048d19d4SFlorian Westphal if (r < 0) { 782048d19d4SFlorian Westphal perror("sendfile"); 783048d19d4SFlorian Westphal return 3; 784048d19d4SFlorian Westphal } 785048d19d4SFlorian Westphal 786048d19d4SFlorian Westphal count -= r; 787048d19d4SFlorian Westphal } 788048d19d4SFlorian Westphal 789048d19d4SFlorian Westphal return 0; 790048d19d4SFlorian Westphal } 791048d19d4SFlorian Westphal 792048d19d4SFlorian Westphal static int copyfd_io_mmap(int infd, int peerfd, int outfd, 793b6ab64b0SPaolo Abeni unsigned int size, bool *in_closed_after_out) 794048d19d4SFlorian Westphal { 795048d19d4SFlorian Westphal int err; 796048d19d4SFlorian Westphal 797048d19d4SFlorian Westphal if (listen_mode) { 798048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 799048d19d4SFlorian Westphal if (err) 800048d19d4SFlorian Westphal return err; 801048d19d4SFlorian Westphal 802048d19d4SFlorian Westphal err = do_mmap(infd, peerfd, size); 803048d19d4SFlorian Westphal } else { 804048d19d4SFlorian Westphal err = do_mmap(infd, peerfd, size); 805048d19d4SFlorian Westphal if (err) 806048d19d4SFlorian Westphal return err; 807048d19d4SFlorian Westphal 808df9e03aeSFlorian Westphal shut_wr(peerfd); 809048d19d4SFlorian Westphal 810048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 811b6ab64b0SPaolo Abeni *in_closed_after_out = true; 812048d19d4SFlorian Westphal } 813048d19d4SFlorian Westphal 814048d19d4SFlorian Westphal return err; 815048d19d4SFlorian Westphal } 816048d19d4SFlorian Westphal 817048d19d4SFlorian Westphal static int copyfd_io_sendfile(int infd, int peerfd, int outfd, 818b6ab64b0SPaolo Abeni unsigned int size, bool *in_closed_after_out) 819048d19d4SFlorian Westphal { 820048d19d4SFlorian Westphal int err; 821048d19d4SFlorian Westphal 822048d19d4SFlorian Westphal if (listen_mode) { 823048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 824048d19d4SFlorian Westphal if (err) 825048d19d4SFlorian Westphal return err; 826048d19d4SFlorian Westphal 827048d19d4SFlorian Westphal err = do_sendfile(infd, peerfd, size); 828048d19d4SFlorian Westphal } else { 829048d19d4SFlorian Westphal err = do_sendfile(infd, peerfd, size); 830048d19d4SFlorian Westphal if (err) 831048d19d4SFlorian Westphal return err; 832df9e03aeSFlorian Westphal 833df9e03aeSFlorian Westphal shut_wr(peerfd); 834df9e03aeSFlorian Westphal 835048d19d4SFlorian Westphal err = do_recvfile(peerfd, outfd); 836b6ab64b0SPaolo Abeni *in_closed_after_out = true; 837048d19d4SFlorian Westphal } 838048d19d4SFlorian Westphal 839048d19d4SFlorian Westphal return err; 840048d19d4SFlorian Westphal } 841048d19d4SFlorian Westphal 84205be5e27SPaolo Abeni static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd) 843048d19d4SFlorian Westphal { 844b6ab64b0SPaolo Abeni bool in_closed_after_out = false; 845b6ab64b0SPaolo Abeni struct timespec start, end; 846048d19d4SFlorian Westphal int file_size; 847b6ab64b0SPaolo Abeni int ret; 848b6ab64b0SPaolo Abeni 849b6ab64b0SPaolo Abeni if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0)) 850b6ab64b0SPaolo Abeni xerror("can not fetch start time %d", errno); 851048d19d4SFlorian Westphal 852048d19d4SFlorian Westphal switch (cfg_mode) { 853048d19d4SFlorian Westphal case CFG_MODE_POLL: 854b6ab64b0SPaolo Abeni ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out); 855b6ab64b0SPaolo Abeni break; 856b6ab64b0SPaolo Abeni 857048d19d4SFlorian Westphal case CFG_MODE_MMAP: 858048d19d4SFlorian Westphal file_size = get_infd_size(infd); 859048d19d4SFlorian Westphal if (file_size < 0) 860048d19d4SFlorian Westphal return file_size; 861b6ab64b0SPaolo Abeni ret = copyfd_io_mmap(infd, peerfd, outfd, file_size, &in_closed_after_out); 862b6ab64b0SPaolo Abeni break; 863b6ab64b0SPaolo Abeni 864048d19d4SFlorian Westphal case CFG_MODE_SENDFILE: 865048d19d4SFlorian Westphal file_size = get_infd_size(infd); 866048d19d4SFlorian Westphal if (file_size < 0) 867048d19d4SFlorian Westphal return file_size; 868b6ab64b0SPaolo Abeni ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size, &in_closed_after_out); 869b6ab64b0SPaolo Abeni break; 870048d19d4SFlorian Westphal 871b6ab64b0SPaolo Abeni default: 872048d19d4SFlorian Westphal fprintf(stderr, "Invalid mode %d\n", cfg_mode); 873048d19d4SFlorian Westphal 874048d19d4SFlorian Westphal die_usage(); 875048d19d4SFlorian Westphal return 1; 876048d19d4SFlorian Westphal } 877048d19d4SFlorian Westphal 878b6ab64b0SPaolo Abeni if (ret) 879b6ab64b0SPaolo Abeni return ret; 880b6ab64b0SPaolo Abeni 88105be5e27SPaolo Abeni if (close_peerfd) 88205be5e27SPaolo Abeni close(peerfd); 88305be5e27SPaolo Abeni 884b6ab64b0SPaolo Abeni if (cfg_time) { 885b6ab64b0SPaolo Abeni unsigned int delta_ms; 886b6ab64b0SPaolo Abeni 887b6ab64b0SPaolo Abeni if (clock_gettime(CLOCK_MONOTONIC, &end) < 0) 888b6ab64b0SPaolo Abeni xerror("can not fetch end time %d", errno); 889b6ab64b0SPaolo Abeni delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000; 890b6ab64b0SPaolo Abeni if (delta_ms > cfg_time) { 891b6ab64b0SPaolo Abeni xerror("transfer slower than expected! runtime %d ms, expected %d ms", 892b6ab64b0SPaolo Abeni delta_ms, cfg_time); 893b6ab64b0SPaolo Abeni } 894b6ab64b0SPaolo Abeni 895b6ab64b0SPaolo Abeni /* show the runtime only if this end shutdown(wr) before receiving the EOF, 896b6ab64b0SPaolo Abeni * (that is, if this end got the longer runtime) 897b6ab64b0SPaolo Abeni */ 898b6ab64b0SPaolo Abeni if (in_closed_after_out) 899b6ab64b0SPaolo Abeni fprintf(stderr, "%d", delta_ms); 900b6ab64b0SPaolo Abeni } 901b6ab64b0SPaolo Abeni 902b6ab64b0SPaolo Abeni return 0; 903b6ab64b0SPaolo Abeni } 904b6ab64b0SPaolo Abeni 905048d19d4SFlorian Westphal static void check_sockaddr(int pf, struct sockaddr_storage *ss, 906048d19d4SFlorian Westphal socklen_t salen) 907048d19d4SFlorian Westphal { 908048d19d4SFlorian Westphal struct sockaddr_in6 *sin6; 909048d19d4SFlorian Westphal struct sockaddr_in *sin; 910048d19d4SFlorian Westphal socklen_t wanted_size = 0; 911048d19d4SFlorian Westphal 912048d19d4SFlorian Westphal switch (pf) { 913048d19d4SFlorian Westphal case AF_INET: 914048d19d4SFlorian Westphal wanted_size = sizeof(*sin); 915048d19d4SFlorian Westphal sin = (void *)ss; 916048d19d4SFlorian Westphal if (!sin->sin_port) 917048d19d4SFlorian Westphal fprintf(stderr, "accept: something wrong: ip connection from port 0"); 918048d19d4SFlorian Westphal break; 919048d19d4SFlorian Westphal case AF_INET6: 920048d19d4SFlorian Westphal wanted_size = sizeof(*sin6); 921048d19d4SFlorian Westphal sin6 = (void *)ss; 922048d19d4SFlorian Westphal if (!sin6->sin6_port) 923048d19d4SFlorian Westphal fprintf(stderr, "accept: something wrong: ipv6 connection from port 0"); 924048d19d4SFlorian Westphal break; 925048d19d4SFlorian Westphal default: 926048d19d4SFlorian Westphal fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen); 927048d19d4SFlorian Westphal return; 928048d19d4SFlorian Westphal } 929048d19d4SFlorian Westphal 930048d19d4SFlorian Westphal if (salen != wanted_size) 931048d19d4SFlorian Westphal fprintf(stderr, "accept: size mismatch, got %d expected %d\n", 932048d19d4SFlorian Westphal (int)salen, wanted_size); 933048d19d4SFlorian Westphal 934048d19d4SFlorian Westphal if (ss->ss_family != pf) 935048d19d4SFlorian Westphal fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n", 936048d19d4SFlorian Westphal (int)ss->ss_family, pf); 937048d19d4SFlorian Westphal } 938048d19d4SFlorian Westphal 939048d19d4SFlorian Westphal static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen) 940048d19d4SFlorian Westphal { 941048d19d4SFlorian Westphal struct sockaddr_storage peerss; 942048d19d4SFlorian Westphal socklen_t peersalen = sizeof(peerss); 943048d19d4SFlorian Westphal 944048d19d4SFlorian Westphal if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) { 945048d19d4SFlorian Westphal perror("getpeername"); 946048d19d4SFlorian Westphal return; 947048d19d4SFlorian Westphal } 948048d19d4SFlorian Westphal 949048d19d4SFlorian Westphal if (peersalen != salen) { 950048d19d4SFlorian Westphal fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen); 951048d19d4SFlorian Westphal return; 952048d19d4SFlorian Westphal } 953048d19d4SFlorian Westphal 954048d19d4SFlorian Westphal if (memcmp(ss, &peerss, peersalen)) { 955048d19d4SFlorian Westphal char a[INET6_ADDRSTRLEN]; 956048d19d4SFlorian Westphal char b[INET6_ADDRSTRLEN]; 957048d19d4SFlorian Westphal char c[INET6_ADDRSTRLEN]; 958048d19d4SFlorian Westphal char d[INET6_ADDRSTRLEN]; 959048d19d4SFlorian Westphal 960048d19d4SFlorian Westphal xgetnameinfo((struct sockaddr *)ss, salen, 961048d19d4SFlorian Westphal a, sizeof(a), b, sizeof(b)); 962048d19d4SFlorian Westphal 963048d19d4SFlorian Westphal xgetnameinfo((struct sockaddr *)&peerss, peersalen, 964048d19d4SFlorian Westphal c, sizeof(c), d, sizeof(d)); 965048d19d4SFlorian Westphal 966048d19d4SFlorian Westphal fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n", 967048d19d4SFlorian Westphal __func__, a, c, b, d, peersalen, salen); 968048d19d4SFlorian Westphal } 969048d19d4SFlorian Westphal } 970048d19d4SFlorian Westphal 971048d19d4SFlorian Westphal static void check_getpeername_connect(int fd) 972048d19d4SFlorian Westphal { 973048d19d4SFlorian Westphal struct sockaddr_storage ss; 974048d19d4SFlorian Westphal socklen_t salen = sizeof(ss); 975048d19d4SFlorian Westphal char a[INET6_ADDRSTRLEN]; 976048d19d4SFlorian Westphal char b[INET6_ADDRSTRLEN]; 977048d19d4SFlorian Westphal 978048d19d4SFlorian Westphal if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) { 979048d19d4SFlorian Westphal perror("getpeername"); 980048d19d4SFlorian Westphal return; 981048d19d4SFlorian Westphal } 982048d19d4SFlorian Westphal 983048d19d4SFlorian Westphal xgetnameinfo((struct sockaddr *)&ss, salen, 984048d19d4SFlorian Westphal a, sizeof(a), b, sizeof(b)); 985048d19d4SFlorian Westphal 986048d19d4SFlorian Westphal if (strcmp(cfg_host, a) || strcmp(cfg_port, b)) 987048d19d4SFlorian Westphal fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__, 988048d19d4SFlorian Westphal cfg_host, a, cfg_port, b); 989048d19d4SFlorian Westphal } 990048d19d4SFlorian Westphal 991b0519de8SFlorian Westphal static void maybe_close(int fd) 992b0519de8SFlorian Westphal { 993b0519de8SFlorian Westphal unsigned int r = rand(); 994b0519de8SFlorian Westphal 99505be5e27SPaolo Abeni if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1)) 996b0519de8SFlorian Westphal close(fd); 997b0519de8SFlorian Westphal } 998b0519de8SFlorian Westphal 999048d19d4SFlorian Westphal int main_loop_s(int listensock) 1000048d19d4SFlorian Westphal { 1001048d19d4SFlorian Westphal struct sockaddr_storage ss; 1002048d19d4SFlorian Westphal struct pollfd polls; 1003048d19d4SFlorian Westphal socklen_t salen; 1004048d19d4SFlorian Westphal int remotesock; 100505be5e27SPaolo Abeni int fd = 0; 1006048d19d4SFlorian Westphal 100705be5e27SPaolo Abeni again: 1008048d19d4SFlorian Westphal polls.fd = listensock; 1009048d19d4SFlorian Westphal polls.events = POLLIN; 1010048d19d4SFlorian Westphal 1011048d19d4SFlorian Westphal switch (poll(&polls, 1, poll_timeout)) { 1012048d19d4SFlorian Westphal case -1: 1013048d19d4SFlorian Westphal perror("poll"); 1014048d19d4SFlorian Westphal return 1; 1015048d19d4SFlorian Westphal case 0: 1016048d19d4SFlorian Westphal fprintf(stderr, "%s: timed out\n", __func__); 1017048d19d4SFlorian Westphal close(listensock); 1018048d19d4SFlorian Westphal return 2; 1019048d19d4SFlorian Westphal } 1020048d19d4SFlorian Westphal 1021048d19d4SFlorian Westphal salen = sizeof(ss); 1022048d19d4SFlorian Westphal remotesock = accept(listensock, (struct sockaddr *)&ss, &salen); 1023048d19d4SFlorian Westphal if (remotesock >= 0) { 1024b0519de8SFlorian Westphal maybe_close(listensock); 1025048d19d4SFlorian Westphal check_sockaddr(pf, &ss, salen); 1026048d19d4SFlorian Westphal check_getpeername(remotesock, &ss, salen); 1027048d19d4SFlorian Westphal 102805be5e27SPaolo Abeni if (cfg_input) { 102905be5e27SPaolo Abeni fd = open(cfg_input, O_RDONLY); 103005be5e27SPaolo Abeni if (fd < 0) 103105be5e27SPaolo Abeni xerror("can't open %s: %d", cfg_input, errno); 1032048d19d4SFlorian Westphal } 1033048d19d4SFlorian Westphal 103405be5e27SPaolo Abeni SOCK_TEST_TCPULP(remotesock, 0); 1035048d19d4SFlorian Westphal 103605be5e27SPaolo Abeni copyfd_io(fd, remotesock, 1, true); 103705be5e27SPaolo Abeni } else { 103805be5e27SPaolo Abeni perror("accept"); 1039048d19d4SFlorian Westphal return 1; 1040048d19d4SFlorian Westphal } 1041048d19d4SFlorian Westphal 104205be5e27SPaolo Abeni if (--cfg_repeat > 0) { 104305be5e27SPaolo Abeni if (cfg_input) 104405be5e27SPaolo Abeni close(fd); 104505be5e27SPaolo Abeni goto again; 104605be5e27SPaolo Abeni } 104705be5e27SPaolo Abeni 104805be5e27SPaolo Abeni return 0; 104905be5e27SPaolo Abeni } 105005be5e27SPaolo Abeni 1051048d19d4SFlorian Westphal static void init_rng(void) 1052048d19d4SFlorian Westphal { 1053048d19d4SFlorian Westphal int fd = open("/dev/urandom", O_RDONLY); 1054048d19d4SFlorian Westphal unsigned int foo; 1055048d19d4SFlorian Westphal 1056048d19d4SFlorian Westphal if (fd > 0) { 1057048d19d4SFlorian Westphal int ret = read(fd, &foo, sizeof(foo)); 1058048d19d4SFlorian Westphal 1059048d19d4SFlorian Westphal if (ret < 0) 1060048d19d4SFlorian Westphal srand(fd + foo); 1061048d19d4SFlorian Westphal close(fd); 1062048d19d4SFlorian Westphal } 1063048d19d4SFlorian Westphal 1064048d19d4SFlorian Westphal srand(foo); 1065048d19d4SFlorian Westphal } 1066048d19d4SFlorian Westphal 10675e6af0a7SFlorian Westphal static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) 10685e6af0a7SFlorian Westphal { 10695e6af0a7SFlorian Westphal int err; 10705e6af0a7SFlorian Westphal 10715e6af0a7SFlorian Westphal err = setsockopt(fd, level, optname, optval, optlen); 10725e6af0a7SFlorian Westphal if (err) { 10735e6af0a7SFlorian Westphal perror("setsockopt"); 10745e6af0a7SFlorian Westphal exit(1); 10755e6af0a7SFlorian Westphal } 10765e6af0a7SFlorian Westphal } 10775e6af0a7SFlorian Westphal 10785e6af0a7SFlorian Westphal static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg) 10795e6af0a7SFlorian Westphal { 10805e6af0a7SFlorian Westphal static const unsigned int on = 1; 10815e6af0a7SFlorian Westphal 10825e6af0a7SFlorian Westphal if (cmsg->timestampns) 10835e6af0a7SFlorian Westphal xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on)); 10845cbd886cSFlorian Westphal if (cmsg->tcp_inq) 10855cbd886cSFlorian Westphal xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on)); 10865e6af0a7SFlorian Westphal } 10875e6af0a7SFlorian Westphal 10885e6af0a7SFlorian Westphal static void parse_cmsg_types(const char *type) 10895e6af0a7SFlorian Westphal { 10905e6af0a7SFlorian Westphal char *next = strchr(type, ','); 10915e6af0a7SFlorian Westphal unsigned int len = 0; 10925e6af0a7SFlorian Westphal 10935e6af0a7SFlorian Westphal cfg_cmsg_types.cmsg_enabled = 1; 10945e6af0a7SFlorian Westphal 10955e6af0a7SFlorian Westphal if (next) { 10965e6af0a7SFlorian Westphal parse_cmsg_types(next + 1); 10975e6af0a7SFlorian Westphal len = next - type; 10985e6af0a7SFlorian Westphal } else { 10995e6af0a7SFlorian Westphal len = strlen(type); 11005e6af0a7SFlorian Westphal } 11015e6af0a7SFlorian Westphal 11025e6af0a7SFlorian Westphal if (strncmp(type, "TIMESTAMPNS", len) == 0) { 11035e6af0a7SFlorian Westphal cfg_cmsg_types.timestampns = 1; 11045e6af0a7SFlorian Westphal return; 11055e6af0a7SFlorian Westphal } 11065e6af0a7SFlorian Westphal 11075cbd886cSFlorian Westphal if (strncmp(type, "TCPINQ", len) == 0) { 11085cbd886cSFlorian Westphal cfg_cmsg_types.tcp_inq = 1; 11095cbd886cSFlorian Westphal return; 11105cbd886cSFlorian Westphal } 11115cbd886cSFlorian Westphal 11125e6af0a7SFlorian Westphal fprintf(stderr, "Unrecognized cmsg option %s\n", type); 11135e6af0a7SFlorian Westphal exit(1); 11145e6af0a7SFlorian Westphal } 11155e6af0a7SFlorian Westphal 11165fb62e9cSFlorian Westphal static void parse_setsock_options(const char *name) 11175fb62e9cSFlorian Westphal { 11185fb62e9cSFlorian Westphal char *next = strchr(name, ','); 11195fb62e9cSFlorian Westphal unsigned int len = 0; 11205fb62e9cSFlorian Westphal 11215fb62e9cSFlorian Westphal if (next) { 11225fb62e9cSFlorian Westphal parse_setsock_options(next + 1); 11235fb62e9cSFlorian Westphal len = next - name; 11245fb62e9cSFlorian Westphal } else { 11255fb62e9cSFlorian Westphal len = strlen(name); 11265fb62e9cSFlorian Westphal } 11275fb62e9cSFlorian Westphal 11285fb62e9cSFlorian Westphal if (strncmp(name, "TRANSPARENT", len) == 0) { 11295fb62e9cSFlorian Westphal cfg_sockopt_types.transparent = 1; 11305fb62e9cSFlorian Westphal return; 11315fb62e9cSFlorian Westphal } 11325fb62e9cSFlorian Westphal 11335fb62e9cSFlorian Westphal fprintf(stderr, "Unrecognized setsockopt option %s\n", name); 11345fb62e9cSFlorian Westphal exit(1); 11355fb62e9cSFlorian Westphal } 11365fb62e9cSFlorian Westphal 113705be5e27SPaolo Abeni void xdisconnect(int fd, int addrlen) 113805be5e27SPaolo Abeni { 113905be5e27SPaolo Abeni struct sockaddr_storage empty; 114005be5e27SPaolo Abeni int msec_sleep = 10; 114105be5e27SPaolo Abeni int queued = 1; 114205be5e27SPaolo Abeni int i; 114305be5e27SPaolo Abeni 114405be5e27SPaolo Abeni shutdown(fd, SHUT_WR); 114505be5e27SPaolo Abeni 114605be5e27SPaolo Abeni /* while until the pending data is completely flushed, the later 114705be5e27SPaolo Abeni * disconnect will bypass/ignore/drop any pending data. 114805be5e27SPaolo Abeni */ 114905be5e27SPaolo Abeni for (i = 0; ; i += msec_sleep) { 115005be5e27SPaolo Abeni if (ioctl(fd, SIOCOUTQ, &queued) < 0) 115105be5e27SPaolo Abeni xerror("can't query out socket queue: %d", errno); 115205be5e27SPaolo Abeni 115305be5e27SPaolo Abeni if (!queued) 115405be5e27SPaolo Abeni break; 115505be5e27SPaolo Abeni 115605be5e27SPaolo Abeni if (i > poll_timeout) 115705be5e27SPaolo Abeni xerror("timeout while waiting for spool to complete"); 115805be5e27SPaolo Abeni usleep(msec_sleep * 1000); 115905be5e27SPaolo Abeni } 116005be5e27SPaolo Abeni 116105be5e27SPaolo Abeni memset(&empty, 0, sizeof(empty)); 116205be5e27SPaolo Abeni empty.ss_family = AF_UNSPEC; 116305be5e27SPaolo Abeni if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0) 116405be5e27SPaolo Abeni xerror("can't disconnect: %d", errno); 116505be5e27SPaolo Abeni } 116605be5e27SPaolo Abeni 1167048d19d4SFlorian Westphal int main_loop(void) 1168048d19d4SFlorian Westphal { 116905be5e27SPaolo Abeni int fd, ret, fd_in = 0; 117005be5e27SPaolo Abeni struct addrinfo *peer; 1171048d19d4SFlorian Westphal 1172048d19d4SFlorian Westphal /* listener is ready. */ 117305be5e27SPaolo Abeni fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer); 1174048d19d4SFlorian Westphal if (fd < 0) 1175048d19d4SFlorian Westphal return 2; 1176048d19d4SFlorian Westphal 117705be5e27SPaolo Abeni again: 1178048d19d4SFlorian Westphal check_getpeername_connect(fd); 1179048d19d4SFlorian Westphal 1180f730b65cSFlorian Westphal SOCK_TEST_TCPULP(fd, cfg_sock_proto); 1181f730b65cSFlorian Westphal 11828a4b910dSFlorian Westphal if (cfg_rcvbuf) 11838a4b910dSFlorian Westphal set_rcvbuf(fd, cfg_rcvbuf); 1184048d19d4SFlorian Westphal if (cfg_sndbuf) 1185048d19d4SFlorian Westphal set_sndbuf(fd, cfg_sndbuf); 11865e6af0a7SFlorian Westphal if (cfg_cmsg_types.cmsg_enabled) 11875e6af0a7SFlorian Westphal apply_cmsg_types(fd, &cfg_cmsg_types); 1188048d19d4SFlorian Westphal 118905be5e27SPaolo Abeni if (cfg_input) { 119005be5e27SPaolo Abeni fd_in = open(cfg_input, O_RDONLY); 119105be5e27SPaolo Abeni if (fd < 0) 119205be5e27SPaolo Abeni xerror("can't open %s:%d", cfg_input, errno); 119305be5e27SPaolo Abeni } 119405be5e27SPaolo Abeni 119505be5e27SPaolo Abeni /* close the client socket open only if we are not going to reconnect */ 1196*6bf41020SPaolo Abeni ret = copyfd_io(fd_in, fd, 1, 0); 119705be5e27SPaolo Abeni if (ret) 119805be5e27SPaolo Abeni return ret; 119905be5e27SPaolo Abeni 1200*6bf41020SPaolo Abeni if (cfg_truncate > 0) { 1201*6bf41020SPaolo Abeni xdisconnect(fd, peer->ai_addrlen); 1202*6bf41020SPaolo Abeni } else if (--cfg_repeat > 0) { 120305be5e27SPaolo Abeni xdisconnect(fd, peer->ai_addrlen); 120405be5e27SPaolo Abeni 120505be5e27SPaolo Abeni /* the socket could be unblocking at this point, we need the 120605be5e27SPaolo Abeni * connect to be blocking 120705be5e27SPaolo Abeni */ 120805be5e27SPaolo Abeni set_nonblock(fd, false); 120905be5e27SPaolo Abeni if (connect(fd, peer->ai_addr, peer->ai_addrlen)) 121005be5e27SPaolo Abeni xerror("can't reconnect: %d", errno); 121105be5e27SPaolo Abeni if (cfg_input) 121205be5e27SPaolo Abeni close(fd_in); 121305be5e27SPaolo Abeni goto again; 1214*6bf41020SPaolo Abeni } else { 1215*6bf41020SPaolo Abeni close(fd); 121605be5e27SPaolo Abeni } 1217*6bf41020SPaolo Abeni 121805be5e27SPaolo Abeni return 0; 1219048d19d4SFlorian Westphal } 1220048d19d4SFlorian Westphal 1221048d19d4SFlorian Westphal int parse_proto(const char *proto) 1222048d19d4SFlorian Westphal { 1223048d19d4SFlorian Westphal if (!strcasecmp(proto, "MPTCP")) 1224048d19d4SFlorian Westphal return IPPROTO_MPTCP; 1225048d19d4SFlorian Westphal if (!strcasecmp(proto, "TCP")) 1226048d19d4SFlorian Westphal return IPPROTO_TCP; 1227048d19d4SFlorian Westphal 1228048d19d4SFlorian Westphal fprintf(stderr, "Unknown protocol: %s\n.", proto); 1229048d19d4SFlorian Westphal die_usage(); 1230048d19d4SFlorian Westphal 1231048d19d4SFlorian Westphal /* silence compiler warning */ 1232048d19d4SFlorian Westphal return 0; 1233048d19d4SFlorian Westphal } 1234048d19d4SFlorian Westphal 1235048d19d4SFlorian Westphal int parse_mode(const char *mode) 1236048d19d4SFlorian Westphal { 1237048d19d4SFlorian Westphal if (!strcasecmp(mode, "poll")) 1238048d19d4SFlorian Westphal return CFG_MODE_POLL; 1239048d19d4SFlorian Westphal if (!strcasecmp(mode, "mmap")) 1240048d19d4SFlorian Westphal return CFG_MODE_MMAP; 1241048d19d4SFlorian Westphal if (!strcasecmp(mode, "sendfile")) 1242048d19d4SFlorian Westphal return CFG_MODE_SENDFILE; 1243048d19d4SFlorian Westphal 1244048d19d4SFlorian Westphal fprintf(stderr, "Unknown test mode: %s\n", mode); 1245048d19d4SFlorian Westphal fprintf(stderr, "Supported modes are:\n"); 1246048d19d4SFlorian Westphal fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n"); 1247048d19d4SFlorian Westphal fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n"); 1248048d19d4SFlorian Westphal fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n"); 1249048d19d4SFlorian Westphal 1250048d19d4SFlorian Westphal die_usage(); 1251048d19d4SFlorian Westphal 1252048d19d4SFlorian Westphal /* silence compiler warning */ 1253048d19d4SFlorian Westphal return 0; 1254048d19d4SFlorian Westphal } 1255048d19d4SFlorian Westphal 1256df8aee6dSYonglong Li int parse_peek(const char *mode) 1257df8aee6dSYonglong Li { 1258df8aee6dSYonglong Li if (!strcasecmp(mode, "saveWithPeek")) 1259df8aee6dSYonglong Li return CFG_WITH_PEEK; 1260df8aee6dSYonglong Li if (!strcasecmp(mode, "saveAfterPeek")) 1261df8aee6dSYonglong Li return CFG_AFTER_PEEK; 1262df8aee6dSYonglong Li 1263df8aee6dSYonglong Li fprintf(stderr, "Unknown: %s\n", mode); 1264df8aee6dSYonglong Li fprintf(stderr, "Supported MSG_PEEK mode are:\n"); 1265df8aee6dSYonglong Li fprintf(stderr, 1266df8aee6dSYonglong Li "\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n"); 1267df8aee6dSYonglong Li fprintf(stderr, 1268df8aee6dSYonglong Li "\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n"); 1269df8aee6dSYonglong Li 1270df8aee6dSYonglong Li die_usage(); 1271df8aee6dSYonglong Li 1272df8aee6dSYonglong Li /* silence compiler warning */ 1273df8aee6dSYonglong Li return 0; 1274df8aee6dSYonglong Li } 1275df8aee6dSYonglong Li 12768a4b910dSFlorian Westphal static int parse_int(const char *size) 1277048d19d4SFlorian Westphal { 1278048d19d4SFlorian Westphal unsigned long s; 1279048d19d4SFlorian Westphal 1280048d19d4SFlorian Westphal errno = 0; 1281048d19d4SFlorian Westphal 1282048d19d4SFlorian Westphal s = strtoul(size, NULL, 0); 1283048d19d4SFlorian Westphal 1284048d19d4SFlorian Westphal if (errno) { 1285048d19d4SFlorian Westphal fprintf(stderr, "Invalid sndbuf size %s (%s)\n", 1286048d19d4SFlorian Westphal size, strerror(errno)); 1287048d19d4SFlorian Westphal die_usage(); 1288048d19d4SFlorian Westphal } 1289048d19d4SFlorian Westphal 1290048d19d4SFlorian Westphal if (s > INT_MAX) { 1291048d19d4SFlorian Westphal fprintf(stderr, "Invalid sndbuf size %s (%s)\n", 1292048d19d4SFlorian Westphal size, strerror(ERANGE)); 1293048d19d4SFlorian Westphal die_usage(); 1294048d19d4SFlorian Westphal } 1295048d19d4SFlorian Westphal 12968a4b910dSFlorian Westphal return (int)s; 1297048d19d4SFlorian Westphal } 1298048d19d4SFlorian Westphal 1299048d19d4SFlorian Westphal static void parse_opts(int argc, char **argv) 1300048d19d4SFlorian Westphal { 1301048d19d4SFlorian Westphal int c; 1302048d19d4SFlorian Westphal 1303*6bf41020SPaolo Abeni while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) { 1304048d19d4SFlorian Westphal switch (c) { 1305*6bf41020SPaolo Abeni case 'f': 1306*6bf41020SPaolo Abeni cfg_truncate = atoi(optarg); 1307*6bf41020SPaolo Abeni 1308*6bf41020SPaolo Abeni /* when receiving a fastclose, ignore PIPE signals and 1309*6bf41020SPaolo Abeni * all the I/O errors later in the code 1310*6bf41020SPaolo Abeni */ 1311*6bf41020SPaolo Abeni if (cfg_truncate < 0) { 1312*6bf41020SPaolo Abeni cfg_rcv_trunc = true; 1313*6bf41020SPaolo Abeni signal(SIGPIPE, handle_signal); 1314*6bf41020SPaolo Abeni } 1315*6bf41020SPaolo Abeni break; 1316b08fbf24SPaolo Abeni case 'j': 1317b08fbf24SPaolo Abeni cfg_join = true; 1318b08fbf24SPaolo Abeni cfg_mode = CFG_MODE_POLL; 1319b08fbf24SPaolo Abeni break; 132013153324SGeliang Tang case 'r': 132113153324SGeliang Tang cfg_remove = true; 132213153324SGeliang Tang cfg_mode = CFG_MODE_POLL; 132313153324SGeliang Tang cfg_wait = 400000; 13242e580a63SGeliang Tang cfg_do_w = atoi(optarg); 13252e580a63SGeliang Tang if (cfg_do_w <= 0) 13262e580a63SGeliang Tang cfg_do_w = 50; 132713153324SGeliang Tang break; 132805be5e27SPaolo Abeni case 'i': 132905be5e27SPaolo Abeni cfg_input = optarg; 133005be5e27SPaolo Abeni break; 133105be5e27SPaolo Abeni case 'I': 133205be5e27SPaolo Abeni cfg_repeat = atoi(optarg); 133305be5e27SPaolo Abeni break; 1334048d19d4SFlorian Westphal case 'l': 1335048d19d4SFlorian Westphal listen_mode = true; 1336048d19d4SFlorian Westphal break; 1337048d19d4SFlorian Westphal case 'p': 1338048d19d4SFlorian Westphal cfg_port = optarg; 1339048d19d4SFlorian Westphal break; 1340048d19d4SFlorian Westphal case 's': 1341048d19d4SFlorian Westphal cfg_sock_proto = parse_proto(optarg); 1342048d19d4SFlorian Westphal break; 1343048d19d4SFlorian Westphal case 'h': 1344048d19d4SFlorian Westphal die_usage(); 1345048d19d4SFlorian Westphal break; 1346048d19d4SFlorian Westphal case '6': 1347048d19d4SFlorian Westphal pf = AF_INET6; 1348048d19d4SFlorian Westphal break; 1349048d19d4SFlorian Westphal case 't': 1350048d19d4SFlorian Westphal poll_timeout = atoi(optarg) * 1000; 1351048d19d4SFlorian Westphal if (poll_timeout <= 0) 1352048d19d4SFlorian Westphal poll_timeout = -1; 1353048d19d4SFlorian Westphal break; 1354b6ab64b0SPaolo Abeni case 'T': 1355b6ab64b0SPaolo Abeni cfg_time = atoi(optarg); 1356b6ab64b0SPaolo Abeni break; 1357048d19d4SFlorian Westphal case 'm': 1358048d19d4SFlorian Westphal cfg_mode = parse_mode(optarg); 1359048d19d4SFlorian Westphal break; 13608a4b910dSFlorian Westphal case 'S': 13618a4b910dSFlorian Westphal cfg_sndbuf = parse_int(optarg); 13628a4b910dSFlorian Westphal break; 13638a4b910dSFlorian Westphal case 'R': 13648a4b910dSFlorian Westphal cfg_rcvbuf = parse_int(optarg); 1365048d19d4SFlorian Westphal break; 1366df62f2ecSPaolo Abeni case 'w': 1367df62f2ecSPaolo Abeni cfg_wait = atoi(optarg)*1000000; 1368df62f2ecSPaolo Abeni break; 1369dc65fe82SFlorian Westphal case 'M': 1370dc65fe82SFlorian Westphal cfg_mark = strtol(optarg, NULL, 0); 1371dc65fe82SFlorian Westphal break; 1372df8aee6dSYonglong Li case 'P': 1373df8aee6dSYonglong Li cfg_peek = parse_peek(optarg); 1374df8aee6dSYonglong Li break; 13755e6af0a7SFlorian Westphal case 'c': 13765e6af0a7SFlorian Westphal parse_cmsg_types(optarg); 13775e6af0a7SFlorian Westphal break; 13785fb62e9cSFlorian Westphal case 'o': 13795fb62e9cSFlorian Westphal parse_setsock_options(optarg); 13805fb62e9cSFlorian Westphal break; 1381048d19d4SFlorian Westphal } 1382048d19d4SFlorian Westphal } 1383048d19d4SFlorian Westphal 1384048d19d4SFlorian Westphal if (optind + 1 != argc) 1385048d19d4SFlorian Westphal die_usage(); 1386048d19d4SFlorian Westphal cfg_host = argv[optind]; 1387048d19d4SFlorian Westphal 1388048d19d4SFlorian Westphal if (strchr(cfg_host, ':')) 1389048d19d4SFlorian Westphal pf = AF_INET6; 1390048d19d4SFlorian Westphal } 1391048d19d4SFlorian Westphal 1392048d19d4SFlorian Westphal int main(int argc, char *argv[]) 1393048d19d4SFlorian Westphal { 1394048d19d4SFlorian Westphal init_rng(); 1395048d19d4SFlorian Westphal 1396df62f2ecSPaolo Abeni signal(SIGUSR1, handle_signal); 1397048d19d4SFlorian Westphal parse_opts(argc, argv); 1398048d19d4SFlorian Westphal 1399048d19d4SFlorian Westphal if (listen_mode) { 1400048d19d4SFlorian Westphal int fd = sock_listen_mptcp(cfg_host, cfg_port); 1401048d19d4SFlorian Westphal 1402048d19d4SFlorian Westphal if (fd < 0) 1403048d19d4SFlorian Westphal return 1; 1404048d19d4SFlorian Westphal 14058a4b910dSFlorian Westphal if (cfg_rcvbuf) 14068a4b910dSFlorian Westphal set_rcvbuf(fd, cfg_rcvbuf); 1407048d19d4SFlorian Westphal if (cfg_sndbuf) 1408048d19d4SFlorian Westphal set_sndbuf(fd, cfg_sndbuf); 1409dc65fe82SFlorian Westphal if (cfg_mark) 1410dc65fe82SFlorian Westphal set_mark(fd, cfg_mark); 14115e6af0a7SFlorian Westphal if (cfg_cmsg_types.cmsg_enabled) 14125e6af0a7SFlorian Westphal apply_cmsg_types(fd, &cfg_cmsg_types); 1413048d19d4SFlorian Westphal 1414048d19d4SFlorian Westphal return main_loop_s(fd); 1415048d19d4SFlorian Westphal } 1416048d19d4SFlorian Westphal 1417048d19d4SFlorian Westphal return main_loop(); 1418048d19d4SFlorian Westphal } 1419