]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Update README.
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index e6f70f7b5c18f4ad83dbe427418186837ab24e8d..f5875e2f58943523bc9853c68d191267e45d2d11 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -122,27 +122,29 @@ struct utcp {
        utcp_send_t send;
 
        uint16_t mtu;
+       int timeout;
 
        struct utcp_connection **connections;
        int nconnections;
        int nallocated;
-       int gap;
 };
 
 static void set_state(struct utcp_connection *c, enum state state) {
        c->state = state;
+       if(state == ESTABLISHED)
+               timerclear(&c->conn_timeout);
        fprintf(stderr, "%p new state: %s\n", c->utcp, strstate[state]);
 }
 
-static void print_packet(void *pkt, size_t len) {
+static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, size_t len) {
        struct hdr hdr;
        if(len < sizeof hdr) {
-               fprintf(stderr, "short packet (%zu bytes)\n", len);
+               fprintf(stderr, "%p %s: short packet (%zu bytes)\n", utcp, dir, len);
                return;
        }
 
        memcpy(&hdr, pkt, sizeof hdr);
-       fprintf (stderr, "src=%u dst=%u seq=%u ack=%u wnd=%u ctl=", hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd);
+       fprintf (stderr, "%p %s: src=%u dst=%u seq=%u ack=%u wnd=%u ctl=", utcp, dir, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd);
        if(hdr.ctl & SYN)
                fprintf(stderr, "SYN");
        if(hdr.ctl & RST)
@@ -155,7 +157,7 @@ static void print_packet(void *pkt, size_t len) {
        if(len > sizeof hdr) {
                fprintf(stderr, " data=");
                for(int i = sizeof hdr; i < len; i++) {
-                       char *data = pkt;
+                       const char *data = pkt;
                        fprintf(stderr, "%c", data[i] >= 32 ? data[i] : '.');
                }
        }
@@ -163,7 +165,7 @@ static void print_packet(void *pkt, size_t len) {
        fprintf(stderr, "\n");
 }
 
-static void list_connections(struct utcp *utcp) {
+static inline void list_connections(struct utcp *utcp) {
        fprintf(stderr, "%p has %d connections:\n", utcp, utcp->nconnections);
        for(int i = 0; i < utcp->nconnections; i++)
                fprintf(stderr, "  %u -> %u state %s\n", utcp->connections[i]->src, utcp->connections[i]->dst, strstate[utcp->connections[i]->state]);
@@ -210,7 +212,7 @@ static void free_connection(struct utcp_connection *c) {
 
 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
        // Check whether this combination of src and dst is free
-       
+
        if(src) {
                if(find_connection(utcp, src, dst)) {
                        errno = EADDRINUSE;
@@ -256,6 +258,10 @@ static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t s
        c->snd.nxt = c->snd.iss + 1;
        c->rcv.wnd = utcp->mtu;
        c->utcp = utcp;
+       c->sndbufsize = 65536;
+       c->sndbuf = malloc(c->sndbufsize);
+       if(!c->sndbuf)
+               c->sndbufsize = 0;
 
        // Add it to the sorted list of connections
 
@@ -283,9 +289,11 @@ struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_
 
        set_state(c, SYN_SENT);
 
+       print_packet(utcp, "send", &hdr, sizeof hdr);
        utcp->send(utcp, &hdr, sizeof hdr);
 
-       // Set timeout?
+       gettimeofday(&c->conn_timeout, NULL);
+       c->conn_timeout.tv_sec += utcp->timeout;
 
        return c;
 }
@@ -302,7 +310,7 @@ void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
        set_state(c, ESTABLISHED);
 }
 
-int utcp_send(struct utcp_connection *c, void *data, size_t len) {
+ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
        if(c->reapable) {
                fprintf(stderr, "Error: send() called on closed connection %p\n", c);
                errno = EBADF;
@@ -330,6 +338,8 @@ int utcp_send(struct utcp_connection *c, void *data, size_t len) {
                return -1;
        }
 
+       // Add data to send buffer
+
        if(!len)
                return 0;
 
@@ -338,30 +348,44 @@ int utcp_send(struct utcp_connection *c, void *data, size_t len) {
                return -1;
        }
 
-       
+       uint32_t bufused = c->snd.nxt - c->snd.una;
+
+       if(len > c->sndbufsize - bufused)
+               len = c->sndbufsize - bufused;
+
+       memcpy(c->sndbuf + (c->snd.nxt - c->snd.una), data, len);
+
+       // Send segments
+
        struct {
                struct hdr hdr;
-               char data[len];
+               char data[c->utcp->mtu];
        } pkt;
 
        pkt.hdr.src = c->src;
        pkt.hdr.dst = c->dst;
-       pkt.hdr.seq = c->snd.nxt;
        pkt.hdr.ack = c->rcv.nxt;
        pkt.hdr.wnd = c->snd.wnd;
        pkt.hdr.ctl = ACK;
 
-       memcpy(pkt.data, data, len);
+       uint32_t left = len;
 
-       c->snd.nxt += len;
+       while(left) {
+               uint32_t seglen = left > c->utcp->mtu ? c->utcp->mtu : left;
+               pkt.hdr.seq = c->snd.nxt;
 
-       c->utcp->send(c->utcp, &pkt, sizeof pkt.hdr + len);
-       //
-       // Can we add it to the send window?
-       
-       // Do we need to kick some timers?
-       
-       return 0;
+               memcpy(pkt.data, data, seglen);
+
+               c->snd.nxt += seglen;
+               data += seglen;
+               left -= seglen;
+
+               print_packet(c->utcp, "send", &pkt, sizeof pkt.hdr + seglen);
+               c->utcp->send(c->utcp, &pkt, sizeof pkt.hdr + seglen);
+       }
+
+       fprintf(stderr, "len=%zu\n", len);
+       return len;
 }
 
 static void swap_ports(struct hdr *hdr) {
@@ -370,7 +394,11 @@ static void swap_ports(struct hdr *hdr) {
        hdr->dst = tmp;
 }
 
-int utcp_recv(struct utcp *utcp, void *data, size_t len) {
+static int32_t seqdiff(uint32_t a, uint32_t b) {
+       return a - b;
+}
+
+int utcp_recv(struct utcp *utcp, const void *data, size_t len) {
        if(!utcp) {
                errno = EFAULT;
                return -1;
@@ -384,8 +412,7 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
                return -1;
        }
 
-       fprintf(stderr, "%p got: ", utcp);
-       print_packet(data, len);
+       print_packet(utcp, "recv", data, len);
 
        struct hdr hdr;
        if(len < sizeof hdr) {
@@ -424,6 +451,7 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
                        hdr.ack = c->rcv.irs + 1;
                        hdr.seq = c->snd.iss;
                        hdr.ctl = SYN | ACK;
+                       print_packet(c->utcp, "send", &hdr, sizeof hdr);
                        utcp->send(utcp, &hdr, sizeof hdr);
                        return 0;
                } else { // CLOSED
@@ -441,10 +469,10 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
        }
 
        // It is for an existing connection.
-       
+
        if(c->state == SYN_SENT) {
                if(hdr.ctl & ACK) {
-                       if(hdr.ack <= c->snd.iss || hdr.ack > c->snd.nxt) {
+                       if(seqdiff(hdr.ack, c->snd.iss) <= 0 || seqdiff(hdr.ack, c->snd.nxt) > 0) {
                                fprintf(stderr, "Invalid ACK, %u %u %u\n", hdr.ack, c->snd.iss, c->snd.nxt);
                                goto reset;
                        }
@@ -454,7 +482,8 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
                                return 0;
                        set_state(c, CLOSED);
                        errno = ECONNREFUSED;
-                       c->recv(c, NULL, 0);
+                       if(c->recv)
+                               c->recv(c, NULL, 0);
                        return 0;
                }
                if(hdr.ctl & SYN) {
@@ -465,7 +494,7 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
 
                        if(hdr.ctl & ACK)
                                c->snd.una = hdr.ack;
-                       if(c->snd.una > c->snd.iss) {
+                       if(seqdiff(c->snd.una, c->snd.iss) > 0) {
                                set_state(c, ESTABLISHED);
                                // TODO: signal app?
                                swap_ports(&hdr);
@@ -479,6 +508,7 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
                                hdr.ack = c->rcv.nxt;
                                hdr.ctl = SYN | ACK;
                        }
+                       print_packet(c->utcp, "send", &hdr, sizeof hdr);
                        utcp->send(utcp, &hdr, sizeof hdr);
                        // TODO: queue any data?
                }
@@ -510,7 +540,7 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
        c->snd.wnd = hdr.wnd;
 
        // TODO: check whether segment really starts at rcv.nxt, otherwise trim it.
-       
+
        if(hdr.ctl & RST) {
                switch(c->state) {
                case SYN_RECEIVED:
@@ -522,7 +552,8 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
                case CLOSE_WAIT:
                        set_state(c, CLOSED);
                        errno = ECONNRESET;
-                       c->recv(c, NULL, 0);
+                       if(c->recv)
+                               c->recv(c, NULL, 0);
                        break;
                case CLOSING:
                case LAST_ACK:
@@ -549,7 +580,8 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
                case TIME_WAIT:
                        set_state(c, CLOSED);
                        errno = ECONNRESET;
-                       c->recv(c, NULL, 0);
+                       if(c->recv)
+                               c->recv(c, NULL, 0);
                        goto reset;
                        break;
                default:
@@ -563,21 +595,23 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
 
        switch(c->state) {
        case SYN_RECEIVED:
-               if(hdr.ack >= c->snd.una && hdr.ack <= c->snd.nxt)
+               if(seqdiff(hdr.ack, c->snd.una) >= 0 && seqdiff(hdr.ack, c->snd.nxt) <= 0)
                        c->utcp->accept(c, hdr.dst);
                
                if(c->state != ESTABLISHED)
                        goto reset;
+
+               c->snd.una = hdr.ack;
                break;
        case ESTABLISHED:
        case CLOSE_WAIT:
-               if(hdr.ack < c->snd.una)
+               if(seqdiff(hdr.ack, c->snd.una) < 0)
                        return 0;
-               if(hdr.ack > c->snd.nxt)
+               if(seqdiff(hdr.ack, c->snd.nxt) > 0)
                        goto ack_and_drop;
-               if(hdr.ack > c->snd.una && hdr.ack <= c->snd.nxt) {
+               if(seqdiff(hdr.ack, c->snd.una) > 0 && seqdiff(hdr.ack, c->snd.nxt) <= 0) {
                        c->snd.una = hdr.ack;
-                       if(c->snd.wl1 < hdr.seq || (c->snd.wl1 == hdr.seq && c->snd.wl2 <= hdr.ack)) {
+                       if(seqdiff(c->snd.wl1, hdr.seq) < 0 || (c->snd.wl1 == hdr.seq && seqdiff(c->snd.wl2, hdr.ack) <= 0)) {
                                c->snd.wnd = hdr.wnd;
                                c->snd.wl1 = hdr.seq;
                                c->snd.wl2 = hdr.ack;
@@ -655,7 +689,7 @@ int utcp_recv(struct utcp *utcp, void *data, size_t len) {
        }
 
        // Process the data
-       
+
        if(len && c->recv) {
                c->recv(c, data, len);
                c->rcv.nxt += len;
@@ -675,6 +709,7 @@ reset:
                hdr.seq = 0;
                hdr.ctl = RST | ACK;
        }
+       print_packet(c->utcp, "send", &hdr, sizeof hdr);
        utcp->send(utcp, &hdr, sizeof hdr);
        return 0;
 
@@ -683,10 +718,12 @@ ack_and_drop:
        hdr.seq = c->snd.nxt;
        hdr.ack = c->rcv.nxt;
        hdr.ctl = ACK;
+       print_packet(c->utcp, "send", &hdr, sizeof hdr);
        utcp->send(utcp, &hdr, sizeof hdr);
        if(c->state == CLOSE_WAIT || c->state == TIME_WAIT) {
                errno = 0;
-               c->recv(c, NULL, 0);
+               if(c->recv)
+                       c->recv(c, NULL, 0);
        }
        return 0;
 }
@@ -743,6 +780,7 @@ int utcp_shutdown(struct utcp_connection *c, int dir) {
 
        c->snd.nxt += 1;
 
+       print_packet(c->utcp, "send", &hdr, sizeof hdr);
        c->utcp->send(c->utcp, &hdr, sizeof hdr);
        return 0;
 }
@@ -799,41 +837,121 @@ int utcp_abort(struct utcp_connection *c) {
        hdr.wnd = 0;
        hdr.ctl = RST;
 
+       print_packet(c->utcp, "send", &hdr, sizeof hdr);
        c->utcp->send(c->utcp, &hdr, sizeof hdr);
        return 0;
 }
 
-void utcp_timeout(struct utcp *utcp) {
+static void retransmit(struct utcp_connection *c) {
+       if(c->state == CLOSED || c->snd.nxt == c->snd.una)
+               return;
+
+       struct utcp *utcp = c->utcp;
+
+       struct {
+               struct hdr hdr;
+               char data[c->utcp->mtu];
+       } pkt;
+
+       pkt.hdr.src = c->src;
+       pkt.hdr.dst = c->dst;
+
+       switch(c->state) {
+               case LISTEN:
+                       // TODO: this should not happen
+                       break;
+
+               case SYN_SENT:
+                       pkt.hdr.seq = c->snd.iss;
+                       pkt.hdr.ack = 0;
+                       pkt.hdr.wnd = c->rcv.wnd;
+                       pkt.hdr.ctl = SYN;
+                       print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr);
+                       utcp->send(utcp, &pkt, sizeof pkt.hdr);
+                       break;
+
+               case SYN_RECEIVED:
+                       pkt.hdr.seq = c->snd.nxt;
+                       pkt.hdr.ack = c->rcv.nxt;
+                       pkt.hdr.ctl = SYN | ACK;
+                       print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr);
+                       utcp->send(utcp, &pkt, sizeof pkt.hdr);
+                       break;
+
+               case ESTABLISHED:
+                       pkt.hdr.seq = c->snd.una;
+                       pkt.hdr.ack = c->rcv.nxt;
+                       pkt.hdr.ctl = ACK;
+                       uint32_t len = seqdiff(c->snd.nxt, c->snd.una);
+                       if(len > utcp->mtu)
+                               len = utcp->mtu;
+                       memcpy(pkt.data, c->sndbuf, len);
+                       print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr + len);
+                       utcp->send(utcp, &pkt, sizeof pkt.hdr + len);
+                       break;
+
+               default:
+                       // TODO: implement
+                       abort();
+       }
+}
+
+/* Handle timeouts.
+ * One call to this function will loop through all connections,
+ * checking if something needs to be resent or not.
+ * The return value is the time to the next timeout in milliseconds,
+ * or maybe a negative value if the timeout is infinite.
+ */
+int utcp_timeout(struct utcp *utcp) {
        struct timeval now;
        gettimeofday(&now, NULL);
+       struct timeval next = {now.tv_sec + 3600, now.tv_usec};
 
        for(int i = 0; i < utcp->nconnections; i++) {
                struct utcp_connection *c = utcp->connections[i];
                if(!c)
                        continue;
 
-               if(c->reapable) {
-                       fprintf(stderr, "Reaping %p\n", c);
-                       free_connection(c);
+               if(c->state == CLOSED) {
+                       if(c->reapable) {
+                               fprintf(stderr, "Reaping %p\n", c);
+                               free_connection(c);
+                               i--;
+                       }
                        continue;
                }
 
-               if(c->state == CLOSED)
-                       return;
-
-               if(c->conn_timeout.tv_sec && timercmp(&c->conn_timeout, &now, <)) {
-                       if(!c->reapable) {
-                               errno = ETIMEDOUT;
-                               c->recv(c, NULL, 0);
-                       }
+               if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &now, <)) {
+                       errno = ETIMEDOUT;
                        c->state = CLOSED;
-                       return;
+                       if(c->recv)
+                               c->recv(c, NULL, 0);
+                       continue;
                }
 
-               if(c->rtrx_timeout.tv_sec && timercmp(&c->rtrx_timeout, &now, <)) {
-                       // TODO: retransmit stuff;
+               if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &now, <)) {
+                       retransmit(c);
                }
+
+               if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &next, <))
+                       next = c->conn_timeout;
+
+               if(c->snd.nxt != c->snd.una) {
+                       c->rtrx_timeout = now;
+                       c->rtrx_timeout.tv_sec++;
+               } else {
+                       timerclear(&c->rtrx_timeout);
+               }
+
+               if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &next, <))
+                       next = c->rtrx_timeout;
        }
+
+       struct timeval diff;
+       timersub(&next, &now, &diff);
+       if(diff.tv_sec < 0)
+               return 0;
+       return diff.tv_sec * 1000 + diff.tv_usec / 1000;
 }
 
 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
@@ -841,12 +959,17 @@ struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_
        if(!utcp)
                return NULL;
 
+       if(!send) {
+               errno = EFAULT;
+               return NULL;
+       }
+
        utcp->accept = accept;
        utcp->pre_accept = pre_accept;
        utcp->send = send;
        utcp->priv = priv;
-       utcp->gap = -1;
        utcp->mtu = 1000;
+       utcp->timeout = 60;
 
        return utcp;
 }
@@ -858,3 +981,9 @@ void utcp_exit(struct utcp *utcp) {
                free_connection(utcp->connections[i]);
        free(utcp);
 }
+
+int utcp_set_connection_timeout(struct utcp *u, int timeout) {
+       int prev = u->timeout;
+       u->timeout = timeout;
+       return prev;
+}