2 utcp-test.c -- Test application for UTCP
3 Copyright (C) 2014-2020 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 static struct utcp_connection *c;
30 static int dir = DIR_READ | DIR_WRITE;
35 static double reorder;
36 static long reorder_dist = 10;
38 static double dropout;
39 static long total_out;
41 static FILE *reference;
45 static char *reorder_data;
46 static size_t reorder_len;
47 static int reorder_countdown;
50 static void debug(const char *format, ...) {
55 clock_gettime(CLOCK_REALTIME, &tv);
56 len = snprintf(buf, sizeof(buf), "%ld.%06lu ", (long)tv.tv_sec, tv.tv_nsec / 1000);
59 len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
62 if(len > 0 && (size_t)len < sizeof(buf)) {
63 fwrite(buf, len, 1, stderr);
67 #define debug(...) do {} while(0)
70 static ssize_t do_recv(struct utcp_connection *rc, const void *data, size_t len) {
75 debug("Error: %s\n", strerror(errno));
79 debug("Connection closed by peer\n");
88 if(fread(buf, len, 1, reference) != 1) {
89 debug("Error reading reference\n");
93 if(memcmp(buf, data, len)) {
94 debug("Received data differs from reference\n");
99 return write(1, data, len);
102 static void do_accept(struct utcp_connection *nc, uint16_t port) {
104 utcp_accept(nc, do_recv, NULL);
108 utcp_set_sndbuf(c, bufsize);
109 utcp_set_rcvbuf(c, bufsize);
112 utcp_set_accept_cb(c->utcp, NULL, NULL);
115 static ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
116 int s = *(int *)utcp->priv;
119 if(outpktno >= dropfrom && outpktno < dropto) {
120 if(drand48() < dropout) {
121 debug("Dropped outgoing packet\n");
125 if(!reorder_data && drand48() < reorder) {
126 reorder_data = malloc(len);
129 debug("Out of memory\n");
134 memcpy(reorder_data, data, len);
135 reorder_countdown = 1 + drand48() * reorder_dist;
141 if(--reorder_countdown < 0) {
142 total_out += reorder_len;
143 send(s, reorder_data, reorder_len, MSG_DONTWAIT);
150 ssize_t result = send(s, data, len, MSG_DONTWAIT);
153 debug("Error sending UDP packet: %s\n", strerror(errno));
159 static void set_mtu(struct utcp *u, int s) {
163 socklen_t optlen = sizeof(mtu);
164 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
169 if(!mtu || mtu == 65535) {
173 debug("Using MTU %lu\n", mtu);
175 utcp_set_mtu(u, mtu ? mtu - 28 : 1300);
178 int main(int argc, char *argv[]) {
182 if(argc < 2 || argc > 3) {
186 bool server = argc == 2;
187 bool connected = false;
188 uint32_t flags = UTCP_TCP;
189 size_t read_size = 102400;
191 if(getenv("DROPIN")) {
192 dropin = atof(getenv("DROPIN"));
195 if(getenv("DROPOUT")) {
196 dropout = atof(getenv("DROPOUT"));
199 if(getenv("DROPFROM")) {
200 dropfrom = atoi(getenv("DROPFROM"));
203 if(getenv("DROPTO")) {
204 dropto = atoi(getenv("DROPTO"));
207 if(getenv("REORDER")) {
208 reorder = atof(getenv("REORDER"));
211 if(getenv("REORDER_DIST")) {
212 reorder_dist = atoi(getenv("REORDER_DIST"));
215 if(getenv("FLAGS")) {
216 flags = atoi(getenv("FLAGS"));
219 if(getenv("READ_SIZE")) {
220 read_size = atoi(getenv("READ_SIZE"));
224 mtu = atoi(getenv("MTU"));
227 if(getenv("BUFSIZE")) {
228 bufsize = atoi(getenv("BUFSIZE"));
231 char *reference_filename = getenv("REFERENCE");
233 if(reference_filename) {
234 reference = fopen(reference_filename, "r");
237 if(dropto < dropfrom) {
243 struct addrinfo hint = {
244 .ai_flags = server ? AI_PASSIVE : 0,
245 .ai_socktype = SOCK_DGRAM,
248 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
251 debug("Could not lookup address: %s\n", strerror(errno));
255 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
258 debug("Could not create socket: %s\n", strerror(errno));
262 static const int one = 1;
263 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
266 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
267 debug("Could not bind: %s\n", strerror(errno));
271 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
272 debug("Could not connect: %s\n", strerror(errno));
281 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
284 debug("Could not initialize UTCP\n");
288 utcp_set_user_timeout(u, 10);
292 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
295 utcp_set_sndbuf(c, bufsize);
296 utcp_set_rcvbuf(c, bufsize);
300 struct pollfd fds[2] = {
301 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
302 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
307 struct timespec timeout = utcp_timeout(u);
309 while(!connected || utcp_is_active(u)) {
310 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
312 if(max > sizeof(buf)) {
316 if(max > read_size) {
320 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1;
322 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
324 if((dir & DIR_READ) && max) {
325 poll(fds, 2, timeout_ms);
327 poll(fds + 1, 1, timeout_ms);
332 ssize_t len = read(0, buf, max);
333 debug("stdin %zd\n", len);
340 utcp_shutdown(c, SHUT_WR);
351 ssize_t sent = utcp_send(c, buf, len);
354 debug("Short send: %zd != %zd\n", sent, len);
361 struct sockaddr_storage ss;
362 socklen_t sl = sizeof(ss);
363 int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
364 debug("netin %zu\n", len);
367 debug("Error receiving UDP packet: %s\n", strerror(errno));
372 if(!connect(s, (struct sockaddr *)&ss, sl)) {
380 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
383 if(utcp_recv(u, buf, len) == -1) {
384 debug("Error receiving UTCP packet: %s\n", strerror(errno));
387 debug("Dropped incoming packet\n");
391 timeout = utcp_timeout(u);
400 debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);