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);
171 if(!mtu || mtu == 65535) {
175 debug("Using MTU %lu\n", mtu);
177 utcp_set_mtu(u, mtu ? mtu - 28 : 1300);
180 int main(int argc, char *argv[]) {
184 if(argc < 2 || argc > 3) {
188 bool server = argc == 2;
189 bool connected = false;
190 uint32_t flags = UTCP_TCP;
191 size_t read_size = 102400;
193 if(getenv("DROPIN")) {
194 dropin = atof(getenv("DROPIN"));
197 if(getenv("DROPOUT")) {
198 dropout = atof(getenv("DROPOUT"));
201 if(getenv("DROPFROM")) {
202 dropfrom = atoi(getenv("DROPFROM"));
205 if(getenv("DROPTO")) {
206 dropto = atoi(getenv("DROPTO"));
209 if(getenv("REORDER")) {
210 reorder = atof(getenv("REORDER"));
213 if(getenv("REORDER_DIST")) {
214 reorder_dist = atoi(getenv("REORDER_DIST"));
217 if(getenv("FLAGS")) {
218 flags = atoi(getenv("FLAGS"));
221 if(getenv("READ_SIZE")) {
222 read_size = atoi(getenv("READ_SIZE"));
226 mtu = atoi(getenv("MTU"));
229 if(getenv("BUFSIZE")) {
230 bufsize = atoi(getenv("BUFSIZE"));
233 char *reference_filename = getenv("REFERENCE");
235 if(reference_filename) {
236 reference = fopen(reference_filename, "r");
239 if(dropto < dropfrom) {
245 struct addrinfo hint = {
246 .ai_flags = server ? AI_PASSIVE : 0,
247 .ai_socktype = SOCK_DGRAM,
250 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
253 debug("Could not lookup address: %s\n", strerror(errno));
257 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
260 debug("Could not create socket: %s\n", strerror(errno));
264 static const int one = 1;
265 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
268 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
269 debug("Could not bind: %s\n", strerror(errno));
273 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
274 debug("Could not connect: %s\n", strerror(errno));
283 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
286 debug("Could not initialize UTCP\n");
290 utcp_set_user_timeout(u, 10);
294 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
297 utcp_set_sndbuf(c, bufsize);
298 utcp_set_rcvbuf(c, bufsize);
302 struct pollfd fds[2] = {
303 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
304 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
309 struct timespec timeout = utcp_timeout(u);
311 while(!connected || utcp_is_active(u)) {
312 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
314 if(max > sizeof(buf)) {
318 if(max > read_size) {
322 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1;
324 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
326 if((dir & DIR_READ) && max) {
327 poll(fds, 2, timeout_ms);
329 poll(fds + 1, 1, timeout_ms);
334 ssize_t len = read(0, buf, max);
335 debug("stdin %zd\n", len);
342 utcp_shutdown(c, SHUT_WR);
353 ssize_t sent = utcp_send(c, buf, len);
356 debug("Short send: %zd != %zd\n", sent, len);
363 struct sockaddr_storage ss;
364 socklen_t sl = sizeof(ss);
365 int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
366 debug("netin %zu\n", len);
369 debug("Error receiving UDP packet: %s\n", strerror(errno));
374 if(!connect(s, (struct sockaddr *)&ss, sl)) {
382 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
385 if(utcp_recv(u, buf, len) == -1) {
386 debug("Error receiving UTCP packet: %s\n", strerror(errno));
389 debug("Dropped incoming packet\n");
393 timeout = utcp_timeout(u);
402 debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);