]> git.meshlink.io Git - meshlink/blob - src/utcp-test.c
Include "system.h" in the UTCP sources.
[meshlink] / src / utcp-test.c
1 /*
2     utcp-test.c -- Test application for UTCP
3     Copyright (C) 2014-2020 Guus Sliepen <guus@tinc-vpn.org>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21 #include <poll.h>
22 #include <time.h>
23
24 #include "utcp.h"
25
26 #define DIR_READ 1
27 #define DIR_WRITE 2
28
29 static struct utcp_connection *c;
30 static int dir = DIR_READ | DIR_WRITE;
31 static long inpktno;
32 static long outpktno;
33 static long dropfrom;
34 static long dropto;
35 static double reorder;
36 static long reorder_dist = 10;
37 static double dropin;
38 static double dropout;
39 static long total_out;
40 static long total_in;
41 static FILE *reference;
42 static long mtu;
43 static long bufsize;
44
45 static char *reorder_data;
46 static size_t reorder_len;
47 static int reorder_countdown;
48
49 #if UTCP_DEBUG
50 static void debug(const char *format, ...) {
51         struct timespec tv;
52         char buf[1024];
53         int len;
54
55         clock_gettime(CLOCK_REALTIME, &tv);
56         len = snprintf(buf, sizeof(buf), "%ld.%06lu ", (long)tv.tv_sec, tv.tv_nsec / 1000);
57         va_list ap;
58         va_start(ap, format);
59         len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
60         va_end(ap);
61
62         if(len > 0 && (size_t)len < sizeof(buf)) {
63                 fwrite(buf, len, 1, stderr);
64         }
65 }
66 #else
67 #define debug(...) do {} while(0)
68 #endif
69
70 static ssize_t do_recv(struct utcp_connection *rc, const void *data, size_t len) {
71         (void)rc;
72
73         if(!data || !len) {
74                 if(errno) {
75                         debug("Error: %s\n", strerror(errno));
76                         dir = 0;
77                 } else {
78                         dir &= ~DIR_WRITE;
79                         debug("Connection closed by peer\n");
80                 }
81
82                 return -1;
83         }
84
85         if(reference) {
86                 char buf[len];
87
88                 if(fread(buf, len, 1, reference) != 1) {
89                         debug("Error reading reference\n");
90                         abort();
91                 }
92
93                 if(memcmp(buf, data, len)) {
94                         debug("Received data differs from reference\n");
95                         abort();
96                 }
97         }
98
99         return write(1, data, len);
100 }
101
102 static void do_accept(struct utcp_connection *nc, uint16_t port) {
103         (void)port;
104         utcp_accept(nc, do_recv, NULL);
105         c = nc;
106
107         if(bufsize) {
108                 utcp_set_sndbuf(c, bufsize);
109                 utcp_set_rcvbuf(c, bufsize);
110         }
111
112         utcp_set_accept_cb(c->utcp, NULL, NULL);
113 }
114
115 static ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
116         int s = *(int *)utcp->priv;
117         outpktno++;
118
119         if(outpktno >= dropfrom && outpktno < dropto) {
120                 if(drand48() < dropout) {
121                         debug("Dropped outgoing packet\n");
122                         return len;
123                 }
124
125                 if(!reorder_data && drand48() < reorder) {
126                         reorder_data = malloc(len);
127
128                         if(!reorder_data) {
129                                 debug("Out of memory\n");
130                                 return len;
131                         }
132
133                         reorder_len = len;
134                         memcpy(reorder_data, data, len);
135                         reorder_countdown = 1 + drand48() * reorder_dist;
136                         return len;
137                 }
138         }
139
140         if(reorder_data) {
141                 if(--reorder_countdown < 0) {
142                         total_out += reorder_len;
143                         send(s, reorder_data, reorder_len, MSG_DONTWAIT);
144                         free(reorder_data);
145                         reorder_data = NULL;
146                 }
147         }
148
149         total_out += len;
150         ssize_t result = send(s, data, len, MSG_DONTWAIT);
151
152         if(result <= 0) {
153                 debug("Error sending UDP packet: %s\n", strerror(errno));
154         }
155
156         return result;
157 }
158
159 static void set_mtu(struct utcp *u, int s) {
160         if(!mtu) {
161                 socklen_t optlen = sizeof(mtu);
162                 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
163         }
164
165         if(!mtu || mtu == 65535) {
166                 mtu = 1500;
167         }
168
169         debug("Using MTU %lu\n", mtu);
170
171         utcp_set_mtu(u, mtu ? mtu - 28 : 1300);
172 }
173
174 int main(int argc, char *argv[]) {
175         srand(time(NULL));
176         srand48(time(NULL));
177
178         if(argc < 2 || argc > 3) {
179                 return 1;
180         }
181
182         bool server = argc == 2;
183         bool connected = false;
184         uint32_t flags = UTCP_TCP;
185         size_t read_size = 102400;
186
187         if(getenv("DROPIN")) {
188                 dropin = atof(getenv("DROPIN"));
189         }
190
191         if(getenv("DROPOUT")) {
192                 dropout = atof(getenv("DROPOUT"));
193         }
194
195         if(getenv("DROPFROM")) {
196                 dropfrom = atoi(getenv("DROPFROM"));
197         }
198
199         if(getenv("DROPTO")) {
200                 dropto = atoi(getenv("DROPTO"));
201         }
202
203         if(getenv("REORDER")) {
204                 reorder = atof(getenv("REORDER"));
205         }
206
207         if(getenv("REORDER_DIST")) {
208                 reorder_dist = atoi(getenv("REORDER_DIST"));
209         }
210
211         if(getenv("FLAGS")) {
212                 flags = atoi(getenv("FLAGS"));
213         }
214
215         if(getenv("READ_SIZE")) {
216                 read_size = atoi(getenv("READ_SIZE"));
217         }
218
219         if(getenv("MTU")) {
220                 mtu = atoi(getenv("MTU"));
221         }
222
223         if(getenv("BUFSIZE")) {
224                 bufsize = atoi(getenv("BUFSIZE"));
225         }
226
227         char *reference_filename = getenv("REFERENCE");
228
229         if(reference_filename) {
230                 reference = fopen(reference_filename, "r");
231         }
232
233         if(dropto < dropfrom) {
234                 dropto = 1 << 30;
235         }
236
237         struct addrinfo *ai;
238
239         struct addrinfo hint = {
240                 .ai_flags = server ? AI_PASSIVE : 0,
241                 .ai_socktype = SOCK_DGRAM,
242         };
243
244         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
245
246         if(!ai) {
247                 debug("Could not lookup address: %s\n", strerror(errno));
248                 return 1;
249         }
250
251         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
252
253         if(s == -1) {
254                 debug("Could not create socket: %s\n", strerror(errno));
255                 return 1;
256         }
257
258         static const int one = 1;
259         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
260
261         if(server) {
262                 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
263                         debug("Could not bind: %s\n", strerror(errno));
264                         return 1;
265                 }
266         } else {
267                 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
268                         debug("Could not connect: %s\n", strerror(errno));
269                         return 1;
270                 }
271
272                 connected = true;
273         }
274
275         freeaddrinfo(ai);
276
277         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
278
279         if(!u) {
280                 debug("Could not initialize UTCP\n");
281                 return 1;
282         }
283
284         utcp_set_user_timeout(u, 10);
285
286         if(!server) {
287                 set_mtu(u, s);
288                 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
289
290                 if(bufsize) {
291                         utcp_set_sndbuf(c, bufsize);
292                         utcp_set_rcvbuf(c, bufsize);
293                 }
294         }
295
296         struct pollfd fds[2] = {
297                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
298                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
299         };
300
301         char buf[102400];
302
303         struct timespec timeout = utcp_timeout(u);
304
305         while(!connected || utcp_is_active(u)) {
306                 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
307
308                 if(max > sizeof(buf)) {
309                         max = sizeof(buf);
310                 }
311
312                 if(max > read_size) {
313                         max = read_size;
314                 }
315
316                 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1;
317
318                 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
319
320                 if((dir & DIR_READ) && max) {
321                         poll(fds, 2, timeout_ms);
322                 } else {
323                         poll(fds + 1, 1, timeout_ms);
324                 }
325
326                 if(fds[0].revents) {
327                         fds[0].revents = 0;
328                         ssize_t len = read(0, buf, max);
329                         debug("stdin %zd\n", len);
330
331                         if(len <= 0) {
332                                 fds[0].fd = -1;
333                                 dir &= ~DIR_READ;
334
335                                 if(c) {
336                                         utcp_shutdown(c, SHUT_WR);
337                                 }
338
339                                 if(len == -1) {
340                                         break;
341                                 } else {
342                                         continue;
343                                 }
344                         }
345
346                         if(c) {
347                                 ssize_t sent = utcp_send(c, buf, len);
348
349                                 if(sent != len) {
350                                         debug("Short send: %zd != %zd\n", sent, len);
351                                 }
352                         }
353                 }
354
355                 if(fds[1].revents) {
356                         fds[1].revents = 0;
357                         struct sockaddr_storage ss;
358                         socklen_t sl = sizeof(ss);
359                         int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
360                         debug("netin %zu\n", len);
361
362                         if(len <= 0) {
363                                 debug("Error receiving UDP packet: %s\n", strerror(errno));
364                                 break;
365                         }
366
367                         if(!connected) {
368                                 if(!connect(s, (struct sockaddr *)&ss, sl)) {
369                                         connected = true;
370                                         set_mtu(u, s);
371                                 }
372                         }
373
374                         inpktno++;
375
376                         if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
377                                 total_in += len;
378
379                                 if(utcp_recv(u, buf, len) == -1) {
380                                         debug("Error receiving UTCP packet: %s\n", strerror(errno));
381                                 }
382                         } else {
383                                 debug("Dropped incoming packet\n");
384                         }
385                 }
386
387                 timeout = utcp_timeout(u);
388         };
389
390         utcp_close(c);
391
392         utcp_exit(u);
393
394         free(reorder_data);
395
396         debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);
397
398         return 0;
399 }