]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Fix warnings when compiling with -Wall -W.
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index 906068c0125c8c25b91bcf0b6a6585d97ee64c24..d990081a30cef77b858c2c5e300122f910a6c49f 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -1,6 +1,6 @@
 /*
     utcp.c -- Userspace TCP
-    Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org>
+    Copyright (C) 2014-2017 Guus Sliepen <guus@tinc-vpn.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -75,7 +75,7 @@ static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, si
        }
 
        memcpy(&hdr, pkt, sizeof hdr);
-       debug("%p %s: len=%lu, src=%u dst=%u seq=%u ack=%u wnd=%u ctl=", utcp, dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd);
+       debug("%p %s: len=%lu, src=%u dst=%u seq=%u ack=%u wnd=%u aux=%x ctl=", utcp, dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux);
        if(hdr.ctl & SYN)
                debug("SYN");
        if(hdr.ctl & RST)
@@ -103,8 +103,8 @@ static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, si
        debug("\n");
 }
 #else
-#define debug(...)
-#define print_packet(...)
+#define debug(...) do {} while(0)
+#define print_packet(...) do {} while(0)
 #endif
 
 static void set_state(struct utcp_connection *c, enum state state) {
@@ -131,12 +131,6 @@ static bool is_reliable(struct utcp_connection *c) {
        return c->flags & UTCP_RELIABLE;
 }
 
-static inline void list_connections(struct utcp *utcp) {
-       debug("%p has %d connections:\n", utcp, utcp->nconnections);
-       for(int i = 0; i < utcp->nconnections; i++)
-               debug("  %u -> %u state %s\n", utcp->connections[i]->src, utcp->connections[i]->dst, strstate[utcp->connections[i]->state]);
-}
-
 static int32_t seqdiff(uint32_t a, uint32_t b) {
        return a - b;
 }
@@ -342,6 +336,13 @@ static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t s
        return c;
 }
 
+static inline uint32_t absdiff(uint32_t a, uint32_t b) {
+       if(a > b)
+               return a - b;
+       else
+               return b - a;
+}
+
 // Update RTT variables. See RFC 6298.
 static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
        if(!rtt) {
@@ -356,7 +357,7 @@ static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
                utcp->rttvar = rtt / 2;
                utcp->rto = rtt + max(2 * rtt, CLOCK_GRANULARITY);
        } else {
-               utcp->rttvar = (utcp->rttvar * 3 + abs(utcp->srtt - rtt)) / 4;
+               utcp->rttvar = (utcp->rttvar * 3 + absdiff(utcp->srtt, rtt)) / 4;
                utcp->srtt = (utcp->srtt * 7 + rtt) / 8;
                utcp->rto = utcp->srtt + max(utcp->rttvar, CLOCK_GRANULARITY);
        }
@@ -418,6 +419,8 @@ struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_re
        gettimeofday(&c->conn_timeout, NULL);
        c->conn_timeout.tv_sec += utcp->timeout;
 
+       start_retransmit_timer(c);
+
        return c;
 }
 
@@ -455,7 +458,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
 
        struct {
                struct hdr hdr;
-               char data[];
+               uint8_t data[];
        } *pkt;
 
        pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
@@ -571,7 +574,7 @@ static void retransmit(struct utcp_connection *c) {
 
        struct {
                struct hdr hdr;
-               char data[];
+               uint8_t data[];
        } *pkt;
 
        pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
@@ -589,8 +592,13 @@ static void retransmit(struct utcp_connection *c) {
                        pkt->hdr.seq = c->snd.iss;
                        pkt->hdr.ack = 0;
                        pkt->hdr.ctl = SYN;
-                       print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr);
-                       utcp->send(utcp, pkt, sizeof pkt->hdr);
+                       pkt->hdr.aux = 0x0101;
+                       pkt->data[0] = 1;
+                       pkt->data[1] = 0;
+                       pkt->data[2] = 0;
+                       pkt->data[3] = c->flags & 0x7;
+                       print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + 4);
+                       utcp->send(utcp, pkt, sizeof pkt->hdr + 4);
                        break;
 
                case SYN_RECEIVED:
@@ -702,7 +710,7 @@ static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, cons
        debug("out of order packet, offset %u\n", offset);
        // Packet loss or reordering occured. Store the data in the buffer.
        ssize_t rxd = buffer_put_at(&c->rcvbuf, offset, data, len);
-       if(rxd < len)
+       if(rxd < 0 || (size_t)rxd < len)
                abort();
 
        // Make note of where we put it.
@@ -753,7 +761,7 @@ static void handle_in_order(struct utcp_connection *c, const void *data, size_t
 
        if(c->recv) {
                ssize_t rxd = c->recv(c, data, len);
-               if(rxd != len) {
+               if(rxd < 0 || (size_t)rxd != len) {
                        // TODO: handle the application not accepting all data.
                        abort();
                }
@@ -821,6 +829,49 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                return -1;
        }
 
+       // Check for auxiliary headers
+
+       const uint8_t *init = NULL;
+
+       uint16_t aux = hdr.aux;
+       while(aux) {
+               size_t auxlen = 4 * (aux >> 8) & 0xf;
+               uint8_t auxtype = aux & 0xff;
+
+               if(len < auxlen) {
+                       errno = EBADMSG;
+                       return -1;
+               }
+
+               switch(auxtype) {
+               case AUX_INIT:
+                       if(!(hdr.ctl & SYN) || auxlen != 4) {
+                               errno = EBADMSG;
+                               return -1;
+                       }
+                       init = data;
+                       break;
+               default:
+                       errno = EBADMSG;
+                       return -1;
+               }
+
+               len -= auxlen;
+               data += auxlen;
+
+               if(!(aux & 0x800))
+                       break;
+
+               if(len < 2) {
+                       errno = EBADMSG;
+                       return -1;
+               }
+
+               memcpy(&aux, data, 2);
+               len -= 2;
+               data += 2;
+       }
+
        // Try to match the packet to an existing connection
 
        struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
@@ -850,14 +901,12 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        }
 
                        // Parse auxilliary information
-                       if(hdr.aux) {
-                               if(hdr.aux != 0x0101 || len < 4 || ((uint8_t *)data)[0] != 1) {
+                       if(init) {
+                               if(init[0] < 1) {
                                        len = 1;
                                        goto reset;
                                }
-                               c->flags = ((uint8_t *)data)[3] & 0x7;
-                               data += 4;
-                               len -= 4;
+                               c->flags = init[3] & 0x7;
                        } else {
                                c->flags = UTCP_TCP;
                        }
@@ -868,13 +917,30 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        c->rcv.nxt = c->rcv.irs + 1;
                        set_state(c, SYN_RECEIVED);
 
-                       hdr.dst = c->dst;
-                       hdr.src = c->src;
-                       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);
+                       struct {
+                               struct hdr hdr;
+                               uint8_t data[4];
+                       } pkt;
+
+                       pkt.hdr.src = c->src;
+                       pkt.hdr.dst = c->dst;
+                       pkt.hdr.ack = c->rcv.irs + 1;
+                       pkt.hdr.seq = c->snd.iss;
+                       pkt.hdr.wnd = c->rcv.wnd;
+                       pkt.hdr.ctl = SYN | ACK;
+                       if(init) {
+                               pkt.hdr.aux = 0x0101;
+                               pkt.data[0] = 1;
+                               pkt.data[1] = 0;
+                               pkt.data[2] = 0;
+                               pkt.data[3] = c->flags & 0x7;
+                               print_packet(c->utcp, "send", &pkt, sizeof hdr + 4);
+                               utcp->send(utcp, &pkt, sizeof hdr + 4);
+                       } else {
+                               pkt.hdr.aux = 0;
+                               print_packet(c->utcp, "send", &pkt, sizeof hdr);
+                               utcp->send(utcp, &pkt, sizeof hdr);
+                       }
                } else {
                        // No, we don't want your packets, send a RST back
                        len = 1;
@@ -933,7 +999,7 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
 
                // cut already accepted front overlapping
                if(rcv_offset < 0) {
-                       acceptable = len > -rcv_offset;
+                       acceptable = len > (size_t)-rcv_offset;
                        if(acceptable) {
                                data -= rcv_offset;
                                len += rcv_offset;
@@ -1020,6 +1086,9 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                }
        }
 
+       if(!(hdr.ctl & ACK))
+               goto skip_ack;
+
        // 3. Advance snd.una
 
        uint32_t advanced = seqdiff(hdr.ack, c->snd.una);
@@ -1112,6 +1181,7 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        start_retransmit_timer(c);
        }
 
+skip_ack:
        // 5. Process SYN stuff
 
        if(hdr.ctl & SYN) {
@@ -1134,7 +1204,7 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                case LAST_ACK:
                case TIME_WAIT:
                        // Ehm, no. We should never receive a second SYN.
-                       goto reset;
+                       return 0;
                default:
 #ifdef UTCP_DEBUG
                        abort();
@@ -1251,6 +1321,7 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
 reset:
        swap_ports(&hdr);
        hdr.wnd = 0;
+       hdr.aux = 0;
        if(hdr.ctl & ACK) {
                hdr.seq = hdr.ack;
                hdr.ctl = RST;
@@ -1485,11 +1556,13 @@ void utcp_exit(struct utcp *utcp) {
        if(!utcp)
                return;
        for(int i = 0; i < utcp->nconnections; i++) {
-               if(!utcp->connections[i]->reapable)
-                       debug("Warning, freeing unclosed connection %p\n", utcp->connections[i]);
-               buffer_exit(&utcp->connections[i]->rcvbuf);
-               buffer_exit(&utcp->connections[i]->sndbuf);
-               free(utcp->connections[i]);
+               struct utcp_connection *c = utcp->connections[i];
+               if(!c->reapable)
+                       if(c->recv)
+                               c->recv(c, NULL, 0);
+               buffer_exit(&c->rcvbuf);
+               buffer_exit(&c->sndbuf);
+               free(c);
        }
        free(utcp->connections);
        free(utcp);
@@ -1505,6 +1578,22 @@ void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
                utcp->mtu = mtu;
 }
 
+void utcp_reset_timers(struct utcp *utcp) {
+       if(!utcp)
+               return;
+       struct timeval now, then;
+       gettimeofday(&now, NULL);
+       then = now;
+       then.tv_sec += utcp->timeout;
+       for(int i = 0; i < utcp->nconnections; i++) {
+               utcp->connections[i]->rtrx_timeout = now;
+               utcp->connections[i]->conn_timeout = then;
+               utcp->connections[i]->rtt_start.tv_sec = 0;
+       }
+       if(utcp->rto > START_RTO)
+               utcp->rto = START_RTO;
+}
+
 int utcp_get_user_timeout(struct utcp *u) {
        return u ? u->timeout : 0;
 }