]> git.meshlink.io Git - utcp/blobdiff - test.c
Simulate packet reordering.
[utcp] / test.c
diff --git a/test.c b/test.c
index 0f7f33368dc1def41192c494b4a29ea918298eac..d7e1cdc5788db3dc8638e3fba93ecd09a292fb0b 100644 (file)
--- a/test.c
+++ b/test.c
@@ -24,11 +24,16 @@ long inpktno;
 long outpktno;
 long dropfrom;
 long dropto;
+double reorder;
+long reorder_dist;
 double dropin;
 double dropout;
 long total_out;
 long total_in;
 
+char *reorder_data;
+size_t reorder_len;
+int reorder_countdown;
 
 void debug(const char *format, ...) {
        struct timeval now;
@@ -63,8 +68,26 @@ void do_accept(struct utcp_connection *nc, uint16_t port) {
 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
        int s = *(int *)utcp->priv;
        outpktno++;
-       if(outpktno < dropto && outpktno >= dropfrom && drand48() < dropout)
-               return len;
+       if(outpktno >= dropfrom && outpktno < dropto) {
+               if(drand48() < dropout)
+                       return len;
+               if(!reorder_data && drand48() < reorder) {
+                       reorder_data = malloc(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);
@@ -87,6 +110,8 @@ int main(int argc, char *argv[]) {
        dropout = atof(getenv("DROPOUT") ?: "0");
        dropfrom = atoi(getenv("DROPFROM") ?: "0");
        dropto = atoi(getenv("DROPTO") ?: "0");
+       reorder = atof(getenv("REORDER") ?: "0");
+       reorder_dist = atoi(getenv("REORDER_DIST") ?: "10");
 
        struct addrinfo *ai;
        struct addrinfo hint = {
@@ -132,15 +157,17 @@ int main(int argc, char *argv[]) {
        struct timeval timeout = utcp_timeout(u);
 
        while(!connected || utcp_is_active(u)) {
-               debug("\n");
                size_t max = c ? utcp_get_sndbuf_free(c) : 0;
                if(max > sizeof buf)
                        max = sizeof buf;
 
+               int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
+
+               debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
                if((dir & DIR_READ) && max)
-                       poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
+                       poll(fds, 2, timeout_ms);
                else
-                       poll(fds + 1, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
+                       poll(fds + 1, 1, timeout_ms);
 
                if(fds[0].revents) {
                        fds[0].revents = 0;
@@ -165,7 +192,7 @@ int main(int argc, char *argv[]) {
 
                if(fds[1].revents) {
                        fds[1].revents = 0;
-                       debug("netout\n");
+                       debug("netin\n");
                        struct sockaddr_storage ss;
                        socklen_t sl = sizeof ss;
                        int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
@@ -188,6 +215,7 @@ int main(int argc, char *argv[]) {
 
        utcp_close(c);
        utcp_exit(u);
+       free(reorder_data);
 
        debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);