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