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
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
35 #include <netinet/in.h>
48 static char buf[1024*1024];
49 static volatile bool accept_done = false;
50 static volatile bool read_done = false;
55 struct sockaddr_in sin;
59 ssize_t readlen = (uintptr_t)arg;
62 ss = accept(ls, (void *)&sin, &slen);
69 rv = read(ss, buf, sizeof(buf));
71 err(2, "read receiver");
75 } while (readlen != 0);
83 main(int argc, char **argv)
86 struct sockaddr_in sin;
89 int fd, cs, on, flags, error;
92 errx(1, "usage: %s <file> <start> <len> <flags>",
95 start = strtoull(argv[2], NULL, 0);
96 len = strtoull(argv[3], NULL, 0);
97 flags = strtoul(argv[4], NULL, 0);
99 fd = open(argv[1], O_RDONLY);
103 ls = socket(PF_INET, SOCK_STREAM, 0);
108 if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
109 (socklen_t)sizeof(on)) < 0)
110 err(1, "SO_REUSEADDR");
112 sin.sin_len = sizeof(sin);
113 sin.sin_family = AF_INET;
114 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
116 if (bind(ls, (void *)&sin, sizeof(sin)) < 0)
120 if (getsockname(ls, (void *)&sin, &slen) < 0)
121 err(1, "getsockname");
123 if (listen(ls, 5) < 0)
126 error = pthread_create(&pt, NULL, server, (void *)(uintptr_t)len);
128 errc(1, error, "pthread_create");
130 cs = socket(PF_INET, SOCK_STREAM, 0);
134 if (connect(cs, (void *)&sin, sizeof(sin)) < 0)
135 err(1, "connect cs");
140 if (sendfile(fd, cs, start, len, NULL, NULL, flags) < 0)