]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Call abort() only when debugging is enabled.
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index db775a9e8a5b37cc3e15c2ff06bf8cc4236c4260..d49c4ddcce2cfbf5822efb899b49b4cd186419c4 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -366,7 +366,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
                c->snd.nxt += seglen;
                left -= seglen;
 
-               if(c->state != ESTABLISHED && !left && seglen) {
+               if(c->state != ESTABLISHED && seglen && c->snd.nxt == c->snd.last) {
                        switch(c->state) {
                        case FIN_WAIT_1:
                        case CLOSING:
@@ -440,6 +440,77 @@ static void swap_ports(struct hdr *hdr) {
        hdr->dst = tmp;
 }
 
+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[];
+       } *pkt;
+
+       pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
+       if(!pkt)
+               return;
+
+       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:
+               case FIN_WAIT_1:
+                       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(c->state == FIN_WAIT_1)
+                               len--;
+                       if(len > utcp->mtu)
+                               len = utcp->mtu;
+                       else {
+                               if(c->state == FIN_WAIT_1)
+                                       pkt->hdr.ctl |= FIN;
+                       }
+                       buffer_copy(&c->sndbuf, pkt->data, 0, len);
+                       print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + len);
+                       utcp->send(utcp, pkt, sizeof pkt->hdr + len);
+                       break;
+
+               default:
+                       // TODO: implement
+#ifdef UTCP_DEBUG
+                       abort();
+#endif
+                       break;
+       }
+
+       free(pkt);
+}
+
+
 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
        if(!utcp) {
                errno = EFAULT;
@@ -555,7 +626,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
        case TIME_WAIT:
                break;
        default:
+#ifdef UTCP_DEBUG
                abort();
+#endif
+               break;
        }
 
        // 1b. Drop packets with a sequence number not in our receive window.
@@ -655,7 +729,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        set_state(c, CLOSED);
                        return 0;
                default:
+#ifdef UTCP_DEBUG
                        abort();
+#endif
+                       break;
                }
        }
 
@@ -711,10 +788,13 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
        } else {
                if(!len) {
                        c->dupack++;
-                       if(c->dupack >= 3) {
+                       if(c->dupack == 3) {
                                debug("Triplicate ACK\n");
                                //TODO: Resend one packet and go to fast recovery mode. See RFC 6582.
-                               //abort();
+                               //We do a very simple variant here; reset the nxt pointer to the last acknowledged packet from the peer.
+                               //This will cause us to start retransmitting, but at the same speed as the incoming ACKs arrive,
+                               //thus preventing a drop in speed.
+                               c->snd.nxt = c->snd.una;
                        }
                }
        }
@@ -751,7 +831,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        // Ehm, no. We should never receive a second SYN.
                        goto reset;
                default:
+#ifdef UTCP_DEBUG
                        abort();
+#endif
+                       return 0;
                }
 
                // SYN counts as one sequence number
@@ -781,7 +864,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                case SYN_SENT:
                case SYN_RECEIVED:
                        // This should never happen.
+#ifdef UTCP_DEBUG
                        abort();
+#endif
+                       return 0;
                case ESTABLISHED:
                case FIN_WAIT_1:
                case FIN_WAIT_2:
@@ -793,7 +879,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        // Ehm no, We should never receive more data after a FIN.
                        goto reset;
                default:
+#ifdef UTCP_DEBUG
                        abort();
+#endif
+                       return 0;
                }
 
                ssize_t rxd;
@@ -822,7 +911,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                case SYN_SENT:
                case SYN_RECEIVED:
                        // This should never happen.
+#ifdef UTCP_DEBUG
                        abort();
+#endif
+                       break;
                case ESTABLISHED:
                        set_state(c, CLOSE_WAIT);
                        break;
@@ -841,7 +933,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        // Ehm, no. We should never receive a second FIN.
                        goto reset;
                default:
+#ifdef UTCP_DEBUG
                        abort();
+#endif
+                       break;
                }
 
                // FIN counts as one sequence number
@@ -883,7 +978,7 @@ reset:
 }
 
 int utcp_shutdown(struct utcp_connection *c, int dir) {
-       debug("%p shutdown %d\n", c ? c->utcp : NULL, dir);
+       debug("%p shutdown %d at %u\n", c ? c->utcp : NULL, dir, c->snd.last);
        if(!c) {
                errno = EFAULT;
                return -1;
@@ -896,6 +991,7 @@ int utcp_shutdown(struct utcp_connection *c, int dir) {
        }
 
        // TODO: handle dir
+       // TODO: check that repeated calls with the same parameters should have no effect
 
        switch(c->state) {
        case CLOSED:
@@ -989,73 +1085,6 @@ int utcp_abort(struct utcp_connection *c) {
        return 0;
 }
 
-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[];
-       } *pkt;
-
-       pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
-       if(!pkt)
-               return;
-
-       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:
-               case FIN_WAIT_1:
-                       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(c->state == FIN_WAIT_1)
-                               len--;
-                       if(len > utcp->mtu)
-                               len = utcp->mtu;
-                       else {
-                               if(c->state == FIN_WAIT_1)
-                                       pkt->hdr.ctl |= FIN;
-                       }
-                       buffer_copy(&c->sndbuf, pkt->data, 0, len);
-                       print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + len);
-                       utcp->send(utcp, pkt, sizeof pkt->hdr + len);
-                       break;
-
-               default:
-                       // TODO: implement
-                       abort();
-       }
-
-       free(pkt);
-}
-
 /* Handle timeouts.
  * One call to this function will loop through all connections,
  * checking if something needs to be resent or not.
@@ -1149,60 +1178,71 @@ void utcp_exit(struct utcp *utcp) {
 }
 
 uint16_t utcp_get_mtu(struct utcp *utcp) {
-       return utcp->mtu;
+       return utcp ? utcp->mtu : 0;
 }
 
 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
        // TODO: handle overhead of the header
-       utcp->mtu = mtu;
+       if(utcp)
+               utcp->mtu = mtu;
 }
 
 int utcp_get_user_timeout(struct utcp *u) {
-       return u->timeout;
+       return u ? u->timeout : 0;
 }
 
 void utcp_set_user_timeout(struct utcp *u, int timeout) {
-       u->timeout = timeout;
+       if(u)
+               u->timeout = timeout;
 }
 
 size_t utcp_get_sndbuf(struct utcp_connection *c) {
-       return c->sndbuf.maxsize;
+       return c ? c->sndbuf.maxsize : 0;
 }
 
 size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
-       return buffer_free(&c->sndbuf);
+       if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT))
+               return buffer_free(&c->sndbuf);
+       else
+               return 0;
 }
 
 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
+       if(!c)
+               return;
        c->sndbuf.maxsize = size;
        if(c->sndbuf.maxsize != size)
                c->sndbuf.maxsize = -1;
 }
 
 bool utcp_get_nodelay(struct utcp_connection *c) {
-       return c->nodelay;
+       return c ? c->nodelay : false;
 }
 
 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
-       c->nodelay = nodelay;
+       if(c)
+               c->nodelay = nodelay;
 }
 
 bool utcp_get_keepalive(struct utcp_connection *c) {
-       return c->keepalive;
+       return c ? c->keepalive : false;
 }
 
 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
-       c->keepalive = keepalive;
+       if(c)
+               c->keepalive = keepalive;
 }
 
 size_t utcp_get_outq(struct utcp_connection *c) {
-       return seqdiff(c->snd.nxt, c->snd.una);
+       return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
 }
 
 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
-       c->recv = recv;
+       if(c)
+               c->recv = recv;
 }
 
 void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
-       c->poll = poll;
+       if(c)
+               c->poll = poll;
 }