X-Git-Url: http://git.meshlink.io/?p=utcp;a=blobdiff_plain;f=test.c;h=2784649bb5ef8a8767b079cb5bf2797099cb9386;hp=cda5f176d80e74407b8d3598e4336a8292d25aa3;hb=HEAD;hpb=2c9dfa2823b30638bc2f7166c15008b722ddebbe diff --git a/test.c b/test.c index cda5f17..2a273de 100644 --- a/test.c +++ b/test.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -13,129 +14,377 @@ #include "utcp.h" -struct utcp_connection *c; -int dir = 3; -bool running = true; -double dropin; -double dropout; +#define DIR_READ 1 +#define DIR_WRITE 2 + +static struct utcp_connection *c; +static int dir = DIR_READ | DIR_WRITE; +static long inpktno; +static long outpktno; +static long dropfrom; +static long dropto; +static double reorder; +static long reorder_dist = 10; +static double dropin; +static double dropout; +static long total_out; +static long total_in; +static FILE *reference; +static long mtu; +static long bufsize; + +static char *reorder_data; +static size_t reorder_len; +static int reorder_countdown; + +#if UTCP_DEBUG +static void debug(const char *format, ...) { + struct timespec tv; + char buf[1024]; + int len; + + clock_gettime(CLOCK_REALTIME, &tv); + len = snprintf(buf, sizeof(buf), "%ld.%06lu ", (long)tv.tv_sec, tv.tv_nsec / 1000); + va_list ap; + va_start(ap, format); + len += vsnprintf(buf + len, sizeof(buf) - len, format, ap); + va_end(ap); + + if(len > 0 && (size_t)len < sizeof(buf)) { + fwrite(buf, len, 1, stderr); + } +} +#else +#define debug(...) do {} while(0) +#endif + +static ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) { + (void)c; -int do_recv(struct utcp_connection *c, const void *data, size_t len) { if(!data || !len) { if(errno) { - fprintf(stderr, "Error: %s\n", strerror(errno)); + debug("Error: %s\n", strerror(errno)); dir = 0; } else { - dir &= ~2; - fprintf(stderr, "Connection closed by peer\n"); + dir &= ~DIR_WRITE; + debug("Connection closed by peer\n"); + } + + return -1; + } + + if(reference) { + char buf[len]; + + if(fread(buf, len, 1, reference) != 1) { + debug("Error reading reference\n"); + abort(); + } + + if(memcmp(buf, data, len)) { + debug("Received data differs from reference\n"); + abort(); } - return 0; } + return write(1, data, len); } -void do_accept(struct utcp_connection *nc, uint16_t port) { +static void do_accept(struct utcp_connection *nc, uint16_t port) { + (void)port; utcp_accept(nc, do_recv, NULL); c = nc; + + if(bufsize) { + utcp_set_sndbuf(c, bufsize); + utcp_set_rcvbuf(c, bufsize); + } + + utcp_set_accept_cb(c->utcp, NULL, NULL); } -int do_send(struct utcp *utcp, const void *data, size_t len) { +static ssize_t do_send(struct utcp *utcp, const void *data, size_t len) { int s = *(int *)utcp->priv; - if(drand48() >= dropout) - return send(s, data, len, MSG_DONTWAIT); - else - return 0; + outpktno++; + + if(outpktno >= dropfrom && outpktno < dropto) { + if(drand48() < dropout) { + debug("Dropped outgoing packet\n"); + return len; + } + + if(!reorder_data && drand48() < reorder) { + reorder_data = malloc(len); + + if(!reorder_data) { + debug("Out of memory\n"); + return len; + } + + reorder_len = len; + memcpy(reorder_data, data, len); + reorder_countdown = 1 + drand48() * reorder_dist; + return len; + } + } + + if(reorder_data) { + if(--reorder_countdown < 0) { + total_out += reorder_len; + send(s, reorder_data, reorder_len, MSG_DONTWAIT); + free(reorder_data); + reorder_data = NULL; + } + } + + total_out += len; + ssize_t result = send(s, data, len, MSG_DONTWAIT); + + if(result <= 0) { + debug("Error sending UDP packet: %s\n", strerror(errno)); + } + + return result; +} + +static void set_mtu(struct utcp *u, int s) { + if(!mtu) { + socklen_t optlen = sizeof(mtu); + getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen); + } + + if(!mtu || mtu == 65535) { + mtu = 1500; + } + + debug("Using MTU %lu\n", mtu); + + utcp_set_mtu(u, mtu ? mtu - 28 : 1300); } int main(int argc, char *argv[]) { srand(time(NULL)); srand48(time(NULL)); - if(argc < 2 || argc > 3) + if(argc < 2 || argc > 3) { return 1; + } bool server = argc == 2; bool connected = false; + uint32_t flags = UTCP_TCP; + size_t read_size = 102400; + + if(getenv("DROPIN")) { + dropin = atof(getenv("DROPIN")); + } + + if(getenv("DROPOUT")) { + dropout = atof(getenv("DROPOUT")); + } + + if(getenv("DROPFROM")) { + dropfrom = atoi(getenv("DROPFROM")); + } - dropin = atof(getenv("DROPIN") ?: "0"); - dropout = atof(getenv("DROPOUT") ?: "0"); + if(getenv("DROPTO")) { + dropto = atoi(getenv("DROPTO")); + } + + if(getenv("REORDER")) { + reorder = atof(getenv("REORDER")); + } + + if(getenv("REORDER_DIST")) { + reorder_dist = atoi(getenv("REORDER_DIST")); + } + + if(getenv("FLAGS")) { + flags = atoi(getenv("FLAGS")); + } + + if(getenv("READ_SIZE")) { + read_size = atoi(getenv("READ_SIZE")); + } + + if(getenv("MTU")) { + mtu = atoi(getenv("MTU")); + } + + if(getenv("BUFSIZE")) { + bufsize = atoi(getenv("BUFSIZE")); + } + + char *reference_filename = getenv("REFERENCE"); + + if(reference_filename) { + reference = fopen(reference_filename, "r"); + } + + if(dropto < dropfrom) { + dropto = 1 << 30; + } struct addrinfo *ai; + struct addrinfo hint = { .ai_flags = server ? AI_PASSIVE : 0, .ai_socktype = SOCK_DGRAM, }; getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai); - if(!ai) + + if(!ai) { + debug("Could not lookup address: %s\n", strerror(errno)); return 1; + } int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if(s < 0) + + if(s == -1) { + debug("Could not create socket: %s\n", strerror(errno)); return 1; + } + + static const int one = 1; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); if(server) { - if(bind(s, ai->ai_addr, ai->ai_addrlen)) + if(bind(s, ai->ai_addr, ai->ai_addrlen)) { + debug("Could not bind: %s\n", strerror(errno)); return 1; + } } else { - if(connect(s, ai->ai_addr, ai->ai_addrlen)) + if(connect(s, ai->ai_addr, ai->ai_addrlen)) { + debug("Could not connect: %s\n", strerror(errno)); return 1; + } + connected = true; } + freeaddrinfo(ai); + struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s); - if(!u) + + if(!u) { + debug("Could not initialize UTCP\n"); return 1; + } - utcp_set_connection_timeout(u, 10); + utcp_set_user_timeout(u, 10); - if(!server) - c = utcp_connect(u, 1, do_recv, NULL); + if(!server) { + set_mtu(u, s); + c = utcp_connect_ex(u, 1, do_recv, NULL, flags); + + if(bufsize) { + utcp_set_sndbuf(c, bufsize); + utcp_set_rcvbuf(c, bufsize); + } + } struct pollfd fds[2] = { {.fd = 0, .events = POLLIN | POLLERR | POLLHUP}, {.fd = s, .events = POLLIN | POLLERR | POLLHUP}, }; - char buf[1024]; - int timeout = utcp_timeout(u); + char buf[102400]; + + struct timespec timeout = utcp_timeout(u); - while(dir) { - poll(fds, 2, timeout); + while(!connected || utcp_is_active(u)) { + size_t max = c ? utcp_get_sndbuf_free(c) : 0; + + if(max > sizeof(buf)) { + max = sizeof(buf); + } + + if(max > read_size) { + max = read_size; + } + + int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1; + + debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms); + + if((dir & DIR_READ) && max) { + poll(fds, 2, timeout_ms); + } else { + poll(fds + 1, 1, timeout_ms); + } if(fds[0].revents) { - int len = read(0, buf, sizeof buf); + fds[0].revents = 0; + ssize_t len = read(0, buf, max); + debug("stdin %zd\n", len); + if(len <= 0) { fds[0].fd = -1; - dir &= ~1; - if(c) + dir &= ~DIR_READ; + + if(c) { utcp_shutdown(c, SHUT_WR); - if(len < 0) + } + + if(len == -1) { break; - else + } else { continue; + } + } + + if(c) { + ssize_t sent = utcp_send(c, buf, len); + + if(sent != len) { + debug("Short send: %zd != %zd\n", sent, len); + } } - if(c) - utcp_send(c, buf, len); } if(fds[1].revents) { + fds[1].revents = 0; struct sockaddr_storage ss; - socklen_t sl; - int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl); - if(len <= 0) + socklen_t sl = sizeof(ss); + int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl); + debug("netin %zu\n", len); + + if(len <= 0) { + debug("Error receiving UDP packet: %s\n", strerror(errno)); break; - if(!connected) - if(!connect(s, (struct sockaddr *)&ss, sl)) + } + + if(!connected) { + if(!connect(s, (struct sockaddr *)&ss, sl)) { connected = true; - if(drand48() >= dropin) - utcp_recv(u, buf, len); + set_mtu(u, s); + } + } + + inpktno++; + + if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) { + total_in += len; + + if(utcp_recv(u, buf, len) == -1) { + debug("Error receiving UTCP packet: %s\n", strerror(errno)); + } + } else { + debug("Dropped incoming packet\n"); + } } timeout = utcp_timeout(u); }; utcp_close(c); + utcp_exit(u); + free(reorder_data); + + debug("Total bytes in: %ld, out: %ld\n", total_in, total_out); + return 0; }