]> git.meshlink.io Git - meshlink/blob - src/utcp-test.c
Check whether IP_MTU is defined.
[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 #ifdef IP_MTU
161
162         if(!mtu) {
163                 socklen_t optlen = sizeof(mtu);
164                 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
165         }
166
167 #endif
168
169         if(!mtu || mtu == 65535) {
170                 mtu = 1500;
171         }
172
173         debug("Using MTU %lu\n", mtu);
174
175         utcp_set_mtu(u, mtu ? mtu - 28 : 1300);
176 }
177
178 int main(int argc, char *argv[]) {
179         srand(time(NULL));
180         srand48(time(NULL));
181
182         if(argc < 2 || argc > 3) {
183                 return 1;
184         }
185
186         bool server = argc == 2;
187         bool connected = false;
188         uint32_t flags = UTCP_TCP;
189         size_t read_size = 102400;
190
191         if(getenv("DROPIN")) {
192                 dropin = atof(getenv("DROPIN"));
193         }
194
195         if(getenv("DROPOUT")) {
196                 dropout = atof(getenv("DROPOUT"));
197         }
198
199         if(getenv("DROPFROM")) {
200                 dropfrom = atoi(getenv("DROPFROM"));
201         }
202
203         if(getenv("DROPTO")) {
204                 dropto = atoi(getenv("DROPTO"));
205         }
206
207         if(getenv("REORDER")) {
208                 reorder = atof(getenv("REORDER"));
209         }
210
211         if(getenv("REORDER_DIST")) {
212                 reorder_dist = atoi(getenv("REORDER_DIST"));
213         }
214
215         if(getenv("FLAGS")) {
216                 flags = atoi(getenv("FLAGS"));
217         }
218
219         if(getenv("READ_SIZE")) {
220                 read_size = atoi(getenv("READ_SIZE"));
221         }
222
223         if(getenv("MTU")) {
224                 mtu = atoi(getenv("MTU"));
225         }
226
227         if(getenv("BUFSIZE")) {
228                 bufsize = atoi(getenv("BUFSIZE"));
229         }
230
231         char *reference_filename = getenv("REFERENCE");
232
233         if(reference_filename) {
234                 reference = fopen(reference_filename, "r");
235         }
236
237         if(dropto < dropfrom) {
238                 dropto = 1 << 30;
239         }
240
241         struct addrinfo *ai;
242
243         struct addrinfo hint = {
244                 .ai_flags = server ? AI_PASSIVE : 0,
245                 .ai_socktype = SOCK_DGRAM,
246         };
247
248         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
249
250         if(!ai) {
251                 debug("Could not lookup address: %s\n", strerror(errno));
252                 return 1;
253         }
254
255         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
256
257         if(s == -1) {
258                 debug("Could not create socket: %s\n", strerror(errno));
259                 return 1;
260         }
261
262         static const int one = 1;
263         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
264
265         if(server) {
266                 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
267                         debug("Could not bind: %s\n", strerror(errno));
268                         return 1;
269                 }
270         } else {
271                 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
272                         debug("Could not connect: %s\n", strerror(errno));
273                         return 1;
274                 }
275
276                 connected = true;
277         }
278
279         freeaddrinfo(ai);
280
281         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
282
283         if(!u) {
284                 debug("Could not initialize UTCP\n");
285                 return 1;
286         }
287
288         utcp_set_user_timeout(u, 10);
289
290         if(!server) {
291                 set_mtu(u, s);
292                 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
293
294                 if(bufsize) {
295                         utcp_set_sndbuf(c, bufsize);
296                         utcp_set_rcvbuf(c, bufsize);
297                 }
298         }
299
300         struct pollfd fds[2] = {
301                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
302                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
303         };
304
305         char buf[102400];
306
307         struct timespec timeout = utcp_timeout(u);
308
309         while(!connected || utcp_is_active(u)) {
310                 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
311
312                 if(max > sizeof(buf)) {
313                         max = sizeof(buf);
314                 }
315
316                 if(max > read_size) {
317                         max = read_size;
318                 }
319
320                 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1;
321
322                 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
323
324                 if((dir & DIR_READ) && max) {
325                         poll(fds, 2, timeout_ms);
326                 } else {
327                         poll(fds + 1, 1, timeout_ms);
328                 }
329
330                 if(fds[0].revents) {
331                         fds[0].revents = 0;
332                         ssize_t len = read(0, buf, max);
333                         debug("stdin %zd\n", len);
334
335                         if(len <= 0) {
336                                 fds[0].fd = -1;
337                                 dir &= ~DIR_READ;
338
339                                 if(c) {
340                                         utcp_shutdown(c, SHUT_WR);
341                                 }
342
343                                 if(len == -1) {
344                                         break;
345                                 } else {
346                                         continue;
347                                 }
348                         }
349
350                         if(c) {
351                                 ssize_t sent = utcp_send(c, buf, len);
352
353                                 if(sent != len) {
354                                         debug("Short send: %zd != %zd\n", sent, len);
355                                 }
356                         }
357                 }
358
359                 if(fds[1].revents) {
360                         fds[1].revents = 0;
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);
365
366                         if(len <= 0) {
367                                 debug("Error receiving UDP packet: %s\n", strerror(errno));
368                                 break;
369                         }
370
371                         if(!connected) {
372                                 if(!connect(s, (struct sockaddr *)&ss, sl)) {
373                                         connected = true;
374                                         set_mtu(u, s);
375                                 }
376                         }
377
378                         inpktno++;
379
380                         if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
381                                 total_in += len;
382
383                                 if(utcp_recv(u, buf, len) == -1) {
384                                         debug("Error receiving UTCP packet: %s\n", strerror(errno));
385                                 }
386                         } else {
387                                 debug("Dropped incoming packet\n");
388                         }
389                 }
390
391                 timeout = utcp_timeout(u);
392         };
393
394         utcp_close(c);
395
396         utcp_exit(u);
397
398         free(reorder_data);
399
400         debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);
401
402         return 0;
403 }