2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2020 Netflix, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/sysctl.h>
33 #include <netinet/in.h>
46 static char buf[1024*1024];
47 static volatile bool accept_done = false;
48 static volatile bool read_done = false;
53 struct sockaddr_in sin;
57 ssize_t readlen = (uintptr_t)arg;
60 ss = accept(ls, (void *)&sin, &slen);
67 rv = read(ss, buf, sizeof(buf));
69 err(2, "read receiver");
73 } while (readlen != 0);
81 main(int argc, char **argv)
84 struct sockaddr_in sin;
87 int fd, cs, on, flags, error;
90 errx(1, "usage: %s <file> <start> <len> <flags>",
93 start = strtoull(argv[2], NULL, 0);
94 len = strtoull(argv[3], NULL, 0);
95 flags = strtoul(argv[4], NULL, 0);
97 fd = open(argv[1], O_RDONLY);
101 ls = socket(PF_INET, SOCK_STREAM, 0);
106 if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
107 (socklen_t)sizeof(on)) < 0)
108 err(1, "SO_REUSEADDR");
110 sin.sin_len = sizeof(sin);
111 sin.sin_family = AF_INET;
112 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
114 if (bind(ls, (void *)&sin, sizeof(sin)) < 0)
118 if (getsockname(ls, (void *)&sin, &slen) < 0)
119 err(1, "getsockname");
121 if (listen(ls, 5) < 0)
124 error = pthread_create(&pt, NULL, server, (void *)(uintptr_t)len);
126 errc(1, error, "pthread_create");
128 cs = socket(PF_INET, SOCK_STREAM, 0);
132 if (connect(cs, (void *)&sin, sizeof(sin)) < 0)
133 err(1, "connect cs");
138 if (sendfile(fd, cs, start, len, NULL, NULL, flags) < 0)