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