]> git.meshlink.io Git - meshlink-tiny/blobdiff - src/utcp.c
Remove the UTCP send and receive buffers.
[meshlink-tiny] / src / utcp.c
index ca91bee26ef6d21c0f0f57dd743c27c0f9b14f8b..0909a0101e196fdecc91b991948c4e5495b10380 100644 (file)
@@ -182,329 +182,10 @@ static bool fin_wanted(struct utcp_connection *c, uint32_t seq) {
        }
 }
 
-static bool is_reliable(struct utcp_connection *c) {
-       return c->flags & UTCP_RELIABLE;
-}
-
 static int32_t seqdiff(uint32_t a, uint32_t b) {
        return a - b;
 }
 
-// Buffer functions
-static bool buffer_wraps(struct buffer *buf) {
-       return buf->size - buf->offset < buf->used;
-}
-
-static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
-       assert(!buf->external);
-
-       if(!newsize) {
-               free(buf->data);
-               buf->data = NULL;
-               buf->size = 0;
-               buf->offset = 0;
-               return true;
-       }
-
-       char *newdata = realloc(buf->data, newsize);
-
-       if(!newdata) {
-               return false;
-       }
-
-       buf->data = newdata;
-
-       if(buffer_wraps(buf)) {
-               // Shift the right part of the buffer until it hits the end of the new buffer.
-               // Old situation:
-               // [345......012]
-               // New situation:
-               // [345.........|........012]
-               uint32_t tailsize = buf->size - buf->offset;
-               uint32_t newoffset = newsize - tailsize;
-               memmove(buf->data + newoffset, buf->data + buf->offset, tailsize);
-               buf->offset = newoffset;
-       }
-
-       buf->size = newsize;
-       return true;
-}
-
-// Store data into the buffer
-static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data, size_t len) {
-       debug(NULL, "buffer_put_at %lu %lu %lu\n", (unsigned long)buf->used, (unsigned long)offset, (unsigned long)len);
-
-       // Ensure we don't store more than maxsize bytes in total
-       size_t required = offset + len;
-
-       if(required > buf->maxsize) {
-               if(offset >= buf->maxsize) {
-                       return 0;
-               }
-
-               len = buf->maxsize - offset;
-               required = buf->maxsize;
-       }
-
-       // Check if we need to resize the buffer
-       if(required > buf->size) {
-               size_t newsize = buf->size;
-
-               if(!newsize) {
-                       newsize = 4096;
-               }
-
-               do {
-                       newsize *= 2;
-               } while(newsize < required);
-
-               if(newsize > buf->maxsize) {
-                       newsize = buf->maxsize;
-               }
-
-               if(!buffer_resize(buf, newsize)) {
-                       return -1;
-               }
-       }
-
-       uint32_t realoffset = buf->offset + offset;
-
-       if(buf->size - buf->offset <= offset) {
-               // The offset wrapped
-               realoffset -= buf->size;
-       }
-
-       if(buf->size - realoffset < len) {
-               // The new chunk of data must be wrapped
-               memcpy(buf->data + realoffset, data, buf->size - realoffset);
-               memcpy(buf->data, (char *)data + buf->size - realoffset, len - (buf->size - realoffset));
-       } else {
-               memcpy(buf->data + realoffset, data, len);
-       }
-
-       if(required > buf->used) {
-               buf->used = required;
-       }
-
-       return len;
-}
-
-static ssize_t buffer_put(struct buffer *buf, const void *data, size_t len) {
-       return buffer_put_at(buf, buf->used, data, len);
-}
-
-// Copy data from the buffer without removing it.
-static ssize_t buffer_copy(struct buffer *buf, void *data, size_t offset, size_t len) {
-       // Ensure we don't copy more than is actually stored in the buffer
-       if(offset >= buf->used) {
-               return 0;
-       }
-
-       if(buf->used - offset < len) {
-               len = buf->used - offset;
-       }
-
-       uint32_t realoffset = buf->offset + offset;
-
-       if(buf->size - buf->offset <= offset) {
-               // The offset wrapped
-               realoffset -= buf->size;
-       }
-
-       if(buf->size - realoffset < len) {
-               // The data is wrapped
-               memcpy(data, buf->data + realoffset, buf->size - realoffset);
-               memcpy((char *)data + buf->size - realoffset, buf->data, len - (buf->size - realoffset));
-       } else {
-               memcpy(data, buf->data + realoffset, len);
-       }
-
-       return len;
-}
-
-// Copy data from the buffer without removing it.
-static ssize_t buffer_call(struct utcp_connection *c, struct buffer *buf, size_t offset, size_t len) {
-       if(!c->recv) {
-               return len;
-       }
-
-       // Ensure we don't copy more than is actually stored in the buffer
-       if(offset >= buf->used) {
-               return 0;
-       }
-
-       if(buf->used - offset < len) {
-               len = buf->used - offset;
-       }
-
-       uint32_t realoffset = buf->offset + offset;
-
-       if(buf->size - buf->offset <= offset) {
-               // The offset wrapped
-               realoffset -= buf->size;
-       }
-
-       if(buf->size - realoffset < len) {
-               // The data is wrapped
-               ssize_t rx1 = c->recv(c, buf->data + realoffset, buf->size - realoffset);
-
-               if(rx1 < buf->size - realoffset) {
-                       return rx1;
-               }
-
-               // The channel might have been closed by the previous callback
-               if(!c->recv) {
-                       return len;
-               }
-
-               ssize_t rx2 = c->recv(c, buf->data, len - (buf->size - realoffset));
-
-               if(rx2 < 0) {
-                       return rx2;
-               } else {
-                       return rx1 + rx2;
-               }
-       } else {
-               return c->recv(c, buf->data + realoffset, len);
-       }
-}
-
-// Discard data from the buffer.
-static ssize_t buffer_discard(struct buffer *buf, size_t len) {
-       if(buf->used < len) {
-               len = buf->used;
-       }
-
-       if(buf->size - buf->offset <= len) {
-               buf->offset -= buf->size;
-       }
-
-       if(buf->used == len) {
-               buf->offset = 0;
-       } else {
-               buf->offset += len;
-       }
-
-       buf->used -= len;
-
-       return len;
-}
-
-static void buffer_clear(struct buffer *buf) {
-       buf->used = 0;
-       buf->offset = 0;
-}
-
-static bool buffer_set_size(struct buffer *buf, uint32_t minsize, uint32_t maxsize) {
-       if(maxsize < minsize) {
-               maxsize = minsize;
-       }
-
-       buf->maxsize = maxsize;
-
-       return buf->size >= minsize || buffer_resize(buf, minsize);
-}
-
-static void buffer_transfer(struct buffer *buf, char *newdata, size_t newsize) {
-       if(buffer_wraps(buf)) {
-               // Old situation:
-               // [345......012]
-               // New situation:
-               // [012345......]
-               uint32_t tailsize = buf->size - buf->offset;
-               memcpy(newdata, buf->data + buf->offset, tailsize);
-               memcpy(newdata + tailsize, buf->data, buf->used - tailsize);
-       } else {
-               // Old situation:
-               // [....012345..]
-               // New situation:
-               // [012345......]
-               memcpy(newdata, buf->data + buf->offset, buf->used);
-       }
-
-       buf->offset = 0;
-       buf->size = newsize;
-}
-
-static void set_buffer_storage(struct buffer *buf, char *data, size_t size) {
-       if(size > UINT32_MAX) {
-               size = UINT32_MAX;
-       }
-
-       buf->maxsize = size;
-
-       if(data) {
-               if(buf->external) {
-                       // Don't allow resizing an external buffer
-                       abort();
-               }
-
-               if(size < buf->used) {
-                       // Ignore requests for an external buffer if we are already using more than it can store
-                       return;
-               }
-
-               // Transition from internal to external buffer
-               buffer_transfer(buf, data, size);
-               free(buf->data);
-               buf->data = data;
-               buf->external = true;
-       } else if(buf->external) {
-               // Transition from external to internal buf
-               size_t minsize = buf->used <= DEFAULT_SNDBUFSIZE ? DEFAULT_SNDBUFSIZE : buf->used;
-
-               if(minsize) {
-                       data = malloc(minsize);
-
-                       if(!data) {
-                               // Cannot handle this
-                               abort();
-                       }
-
-                       buffer_transfer(buf, data, minsize);
-                       buf->data = data;
-               } else {
-                       buf->data = NULL;
-                       buf->size = 0;
-               }
-
-               buf->external = false;
-       } else {
-               // Don't do anything if the buffer wraps
-               if(buffer_wraps(buf)) {
-                       return;
-               }
-
-               // Realloc internal storage
-               size_t minsize = max(DEFAULT_SNDBUFSIZE, buf->offset + buf->used);
-
-               if(minsize) {
-                       data = realloc(buf->data, minsize);
-
-                       if(data) {
-                               buf->data = data;
-                               buf->size = minsize;
-                       }
-               } else {
-                       free(buf->data);
-                       buf->data = NULL;
-                       buf->size = 0;
-               }
-       }
-}
-
-static void buffer_exit(struct buffer *buf) {
-       if(!buf->external) {
-               free(buf->data);
-       }
-
-       memset(buf, 0, sizeof(*buf));
-}
-
-static uint32_t buffer_free(const struct buffer *buf) {
-       return buf->maxsize > buf->used ? buf->maxsize - buf->used : 0;
-}
-
 // Connections are stored in a sorted list.
 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
 
@@ -549,8 +230,6 @@ static void free_connection(struct utcp_connection *c) {
        memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof(*cp));
        utcp->nconnections--;
 
-       buffer_exit(&c->rcvbuf);
-       buffer_exit(&c->sndbuf);
        free(c);
 }
 
@@ -599,17 +278,6 @@ static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t s
                return NULL;
        }
 
-       if(!buffer_set_size(&c->sndbuf, DEFAULT_SNDBUFSIZE, DEFAULT_MAXSNDBUFSIZE)) {
-               free(c);
-               return NULL;
-       }
-
-       if(!buffer_set_size(&c->rcvbuf, DEFAULT_RCVBUFSIZE, DEFAULT_MAXRCVBUFSIZE)) {
-               buffer_exit(&c->sndbuf);
-               free(c);
-               return NULL;
-       }
-
        // Fill in the details
 
        c->src = src;
@@ -702,7 +370,7 @@ struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_re
                return NULL;
        }
 
-       assert((flags & ~0x1f) == 0);
+       assert(flags == 0); // UDP only
 
        c->flags = flags;
        c->recv = recv;
@@ -717,7 +385,7 @@ struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_re
        pkt.hdr.dst = c->dst;
        pkt.hdr.seq = c->snd.iss;
        pkt.hdr.ack = 0;
-       pkt.hdr.wnd = c->rcvbuf.maxsize;
+       pkt.hdr.wnd = c->utcp->mtu;
        pkt.hdr.ctl = SYN;
        pkt.hdr.aux = 0x0101;
        pkt.init[0] = 1;
@@ -738,10 +406,6 @@ struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_re
        return c;
 }
 
-struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
-       return utcp_connect_ex(utcp, dst, recv, priv, UTCP_TCP);
-}
-
 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
        if(c->reapable || c->state != SYN_RECEIVED) {
                debug(c, "accept() called on invalid connection in state %s\n", c, strstate[c->state]);
@@ -751,32 +415,10 @@ void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
        debug(c, "accepted %p %p\n", c, recv, priv);
        c->recv = recv;
        c->priv = priv;
-       c->do_poll = true;
        set_state(c, ESTABLISHED);
 }
 
-static void ack(struct utcp_connection *c, bool sendatleastone) {
-       int32_t left = seqdiff(c->snd.last, c->snd.nxt);
-       int32_t cwndleft = is_reliable(c) ? min(c->snd.cwnd, c->snd.wnd) - seqdiff(c->snd.nxt, c->snd.una) : MAX_UNRELIABLE_SIZE;
-
-       assert(left >= 0);
-
-       if(cwndleft <= 0) {
-               left = 0;
-       } else if(cwndleft < left) {
-               left = cwndleft;
-
-               if(!sendatleastone || cwndleft > c->utcp->mss) {
-                       left -= left % c->utcp->mss;
-               }
-       }
-
-       debug(c, "cwndleft %d left %d\n", cwndleft, left);
-
-       if(!left && !sendatleastone) {
-               return;
-       }
-
+static void ack(struct utcp_connection *c, const void *data, size_t len) {
        struct {
                struct hdr hdr;
                uint8_t data[];
@@ -785,46 +427,35 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
        pkt->hdr.src = c->src;
        pkt->hdr.dst = c->dst;
        pkt->hdr.ack = c->rcv.nxt;
-       pkt->hdr.wnd = is_reliable(c) ? c->rcvbuf.maxsize : 0;
+       pkt->hdr.wnd = 0;
        pkt->hdr.ctl = ACK;
        pkt->hdr.aux = 0;
 
-       do {
-               uint32_t seglen = left > c->utcp->mss ? c->utcp->mss : left;
-               pkt->hdr.seq = c->snd.nxt;
-
-               buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
+       uint32_t seglen = len;
+       pkt->hdr.seq = c->snd.nxt;
 
-               c->snd.nxt += seglen;
-               left -= seglen;
+       c->snd.nxt += seglen;
 
-               if(!is_reliable(c)) {
-                       if(left) {
-                               pkt->hdr.ctl |= MF;
-                       } else {
-                               pkt->hdr.ctl &= ~MF;
-                       }
-               }
-
-               if(seglen && fin_wanted(c, c->snd.nxt)) {
-                       seglen--;
-                       pkt->hdr.ctl |= FIN;
-               }
+       if(fin_wanted(c, c->snd.nxt)) {
+               pkt->hdr.ctl |= FIN;
+       }
 
-               if(!c->rtt_start.tv_sec) {
-                       // Start RTT measurement
-                       clock_gettime(UTCP_CLOCK, &c->rtt_start);
-                       c->rtt_seq = pkt->hdr.seq + seglen;
-                       debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
-               }
+       if(data && len) {
+               assert(len <= c->utcp->mtu);
+               memcpy(pkt->data, data, len);
+       } else {
+               assert(!data && !len);
+       }
 
-               print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
-               c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
+       if(!c->rtt_start.tv_sec) {
+               // Start RTT measurement
+               clock_gettime(UTCP_CLOCK, &c->rtt_start);
+               c->rtt_seq = pkt->hdr.seq + seglen;
+               debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
+       }
 
-               if(left && !is_reliable(c)) {
-                       pkt->hdr.wnd += seglen;
-               }
-       } while(left);
+       print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
+       c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
 }
 
 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
@@ -868,40 +499,13 @@ ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
                return -1;
        }
 
-       // Check if we need to be able to buffer all data
-
-       if(c->flags & UTCP_NO_PARTIAL) {
-               if(len > buffer_free(&c->sndbuf)) {
-                       if(len > c->sndbuf.maxsize) {
-                               errno = EMSGSIZE;
-                               return -1;
-                       } else {
-                               errno = EWOULDBLOCK;
-                               return 0;
-                       }
-               }
-       }
-
-       // Add data to send buffer.
-
-       if(is_reliable(c)) {
-               len = buffer_put(&c->sndbuf, data, len);
-       } else if(c->state != SYN_SENT && c->state != SYN_RECEIVED) {
-               if(len > MAX_UNRELIABLE_SIZE || buffer_put(&c->sndbuf, data, len) != (ssize_t)len) {
-                       errno = EMSGSIZE;
-                       return -1;
-               }
-       } else {
-               return 0;
+       if(len > MAX_UNRELIABLE_SIZE || len > c->utcp->mtu) {
+               errno = EMSGSIZE;
+               return -1;
        }
 
        if(len <= 0) {
-               if(is_reliable(c)) {
-                       errno = EWOULDBLOCK;
-                       return 0;
-               } else {
-                       return len;
-               }
+               return len;
        }
 
        c->snd.last += len;
@@ -912,21 +516,9 @@ ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
                return len;
        }
 
-       ack(c, false);
+       ack(c, data, len);
 
-       if(!is_reliable(c)) {
-               c->snd.una = c->snd.nxt = c->snd.last;
-               buffer_discard(&c->sndbuf, c->sndbuf.used);
-       }
-
-       if(is_reliable(c) && !timespec_isset(&c->rtrx_timeout)) {
-               start_retransmit_timer(c);
-       }
-
-       if(is_reliable(c) && !timespec_isset(&c->conn_timeout)) {
-               clock_gettime(UTCP_CLOCK, &c->conn_timeout);
-               c->conn_timeout.tv_sec += c->utcp->timeout;
-       }
+       c->snd.una = c->snd.nxt = c->snd.last;
 
        return len;
 }
@@ -937,51 +529,6 @@ static void swap_ports(struct hdr *hdr) {
        hdr->dst = tmp;
 }
 
-static void fast_retransmit(struct utcp_connection *c) {
-       if(c->state == CLOSED || c->snd.last == c->snd.una) {
-               debug(c, "fast_retransmit() called but nothing to retransmit!\n");
-               return;
-       }
-
-       struct utcp *utcp = c->utcp;
-
-       struct {
-               struct hdr hdr;
-               uint8_t data[];
-       } *pkt = c->utcp->pkt;
-
-       pkt->hdr.src = c->src;
-       pkt->hdr.dst = c->dst;
-       pkt->hdr.wnd = c->rcvbuf.maxsize;
-       pkt->hdr.aux = 0;
-
-       switch(c->state) {
-       case ESTABLISHED:
-       case FIN_WAIT_1:
-       case CLOSE_WAIT:
-       case CLOSING:
-       case LAST_ACK:
-               // Send unacked data again.
-               pkt->hdr.seq = c->snd.una;
-               pkt->hdr.ack = c->rcv.nxt;
-               pkt->hdr.ctl = ACK;
-               uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
-
-               if(fin_wanted(c, c->snd.una + len)) {
-                       len--;
-                       pkt->hdr.ctl |= FIN;
-               }
-
-               buffer_copy(&c->sndbuf, pkt->data, 0, len);
-               print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
-               utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
-               break;
-
-       default:
-               break;
-       }
-}
-
 static void retransmit(struct utcp_connection *c) {
        if(c->state == CLOSED || c->snd.last == c->snd.una) {
                debug(c, "retransmit() called but nothing to retransmit!\n");
@@ -991,10 +538,6 @@ static void retransmit(struct utcp_connection *c) {
 
        struct utcp *utcp = c->utcp;
 
-       if(utcp->retransmit) {
-               utcp->retransmit(c);
-       }
-
        struct {
                struct hdr hdr;
                uint8_t data[];
@@ -1002,7 +545,7 @@ static void retransmit(struct utcp_connection *c) {
 
        pkt->hdr.src = c->src;
        pkt->hdr.dst = c->dst;
-       pkt->hdr.wnd = c->rcvbuf.maxsize;
+       pkt->hdr.wnd = c->utcp->mtu;
        pkt->hdr.aux = 0;
 
        switch(c->state) {
@@ -1030,6 +573,8 @@ static void retransmit(struct utcp_connection *c) {
                break;
 
        case ESTABLISHED:
+               break;
+
        case FIN_WAIT_1:
        case CLOSE_WAIT:
        case CLOSING:
@@ -1043,19 +588,14 @@ static void retransmit(struct utcp_connection *c) {
                if(fin_wanted(c, c->snd.una + len)) {
                        len--;
                        pkt->hdr.ctl |= FIN;
+               } else {
+                       break;
                }
 
-               // RFC 5681 slow start after timeout
-               uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
-               c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
-               c->snd.cwnd = utcp->mss;
-               debug_cwnd(c);
+               assert(len == 0);
 
-               buffer_copy(&c->sndbuf, pkt->data, 0, len);
                print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
                utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
-
-               c->snd.nxt = c->snd.una + len;
                break;
 
        case CLOSED:
@@ -1084,149 +624,6 @@ cleanup:
        return;
 }
 
-/* Update receive buffer and SACK entries after consuming data.
- *
- * Situation:
- *
- * |.....0000..1111111111.....22222......3333|
- * |---------------^
- *
- * 0..3 represent the SACK entries. The ^ indicates up to which point we want
- * to remove data from the receive buffer. The idea is to substract "len"
- * from the offset of all the SACK entries, and then remove/cut down entries
- * that are shifted to before the start of the receive buffer.
- *
- * There are three cases:
- * - the SACK entry is after ^, in that case just change the offset.
- * - the SACK entry starts before and ends after ^, so we have to
- *   change both its offset and size.
- * - the SACK entry is completely before ^, in that case delete it.
- */
-static void sack_consume(struct utcp_connection *c, size_t len) {
-       debug(c, "sack_consume %lu\n", (unsigned long)len);
-
-       if(len > c->rcvbuf.used) {
-               debug(c, "all SACK entries consumed\n");
-               c->sacks[0].len = 0;
-               return;
-       }
-
-       buffer_discard(&c->rcvbuf, len);
-
-       for(int i = 0; i < NSACKS && c->sacks[i].len;) {
-               if(len < c->sacks[i].offset) {
-                       c->sacks[i].offset -= len;
-                       i++;
-               } else if(len < c->sacks[i].offset + c->sacks[i].len) {
-                       c->sacks[i].len -= len - c->sacks[i].offset;
-                       c->sacks[i].offset = 0;
-                       i++;
-               } else {
-                       if(i < NSACKS - 1) {
-                               memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof(c->sacks)[i]);
-                               c->sacks[NSACKS - 1].len = 0;
-                       } else {
-                               c->sacks[i].len = 0;
-                               break;
-                       }
-               }
-       }
-
-       for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
-               debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
-       }
-}
-
-static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, const void *data, size_t len) {
-       debug(c, "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 <= 0) {
-               debug(c, "packet outside receive buffer, dropping\n");
-               return;
-       }
-
-       if((size_t)rxd < len) {
-               debug(c, "packet partially outside receive buffer\n");
-               len = rxd;
-       }
-
-       // Make note of where we put it.
-       for(int i = 0; i < NSACKS; i++) {
-               if(!c->sacks[i].len) { // nothing to merge, add new entry
-                       debug(c, "new SACK entry %d\n", i);
-                       c->sacks[i].offset = offset;
-                       c->sacks[i].len = rxd;
-                       break;
-               } else if(offset < c->sacks[i].offset) {
-                       if(offset + rxd < c->sacks[i].offset) { // insert before
-                               if(!c->sacks[NSACKS - 1].len) { // only if room left
-                                       debug(c, "insert SACK entry at %d\n", i);
-                                       memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof(c->sacks)[i]);
-                                       c->sacks[i].offset = offset;
-                                       c->sacks[i].len = rxd;
-                               } else {
-                                       debug(c, "SACK entries full, dropping packet\n");
-                               }
-
-                               break;
-                       } else { // merge
-                               debug(c, "merge with start of SACK entry at %d\n", i);
-                               c->sacks[i].offset = offset;
-                               break;
-                       }
-               } else if(offset <= c->sacks[i].offset + c->sacks[i].len) {
-                       if(offset + rxd > c->sacks[i].offset + c->sacks[i].len) { // merge
-                               debug(c, "merge with end of SACK entry at %d\n", i);
-                               c->sacks[i].len = offset + rxd - c->sacks[i].offset;
-                               // TODO: handle potential merge with next entry
-                       }
-
-                       break;
-               }
-       }
-
-       for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
-               debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
-       }
-}
-
-static void handle_in_order(struct utcp_connection *c, const void *data, size_t len) {
-       if(c->recv) {
-               ssize_t rxd = c->recv(c, data, len);
-
-               if(rxd != (ssize_t)len) {
-                       // TODO: handle the application not accepting all data.
-                       abort();
-               }
-       }
-
-       // Check if we can process out-of-order data now.
-       if(c->sacks[0].len && len >= c->sacks[0].offset) {
-               debug(c, "incoming packet len %lu connected with SACK at %u\n", (unsigned long)len, c->sacks[0].offset);
-
-               if(len < c->sacks[0].offset + c->sacks[0].len) {
-                       size_t offset = len;
-                       len = c->sacks[0].offset + c->sacks[0].len;
-                       size_t remainder = len - offset;
-
-                       ssize_t rxd = buffer_call(c, &c->rcvbuf, offset, remainder);
-
-                       if(rxd != (ssize_t)remainder) {
-                               // TODO: handle the application not accepting all data.
-                               abort();
-                       }
-               }
-       }
-
-       if(c->rcvbuf.used) {
-               sack_consume(c, len);
-       }
-
-       c->rcv.nxt += len;
-}
-
 static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
        // Fast path for unfragmented packets
        if(!hdr->wnd && !(hdr->ctl & MF)) {
@@ -1237,52 +634,12 @@ static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr,
                c->rcv.nxt = hdr->seq + len;
                return;
        }
-
-       // Ensure reassembled packet are not larger than 64 kiB
-       if(hdr->wnd >= MAX_UNRELIABLE_SIZE || hdr->wnd + len > MAX_UNRELIABLE_SIZE) {
-               return;
-       }
-
-       // Don't accept out of order fragments
-       if(hdr->wnd && hdr->seq != c->rcv.nxt) {
-               return;
-       }
-
-       // Reset the receive buffer for the first fragment
-       if(!hdr->wnd) {
-               buffer_clear(&c->rcvbuf);
-       }
-
-       ssize_t rxd = buffer_put_at(&c->rcvbuf, hdr->wnd, data, len);
-
-       if(rxd != (ssize_t)len) {
-               return;
-       }
-
-       // Send the packet if it's the final fragment
-       if(!(hdr->ctl & MF)) {
-               buffer_call(c, &c->rcvbuf, 0, hdr->wnd + len);
-       }
-
-       c->rcv.nxt = hdr->seq + len;
 }
 
 static void handle_incoming_data(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
-       if(!is_reliable(c)) {
-               handle_unreliable(c, hdr, data, len);
-               return;
-       }
-
-       uint32_t offset = seqdiff(hdr->seq, c->rcv.nxt);
-
-       if(offset) {
-               handle_out_of_order(c, offset, data, len);
-       } else {
-               handle_in_order(c, data, len);
-       }
+       handle_unreliable(c, hdr, data, len);
 }
 
-
 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
        const uint8_t *ptr = data;
 
@@ -1379,8 +736,6 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                ptr += 2;
        }
 
-       bool has_data = len || (hdr.ctl & (SYN | FIN));
-
        // Is it for a new connection?
 
        if(!c) {
@@ -1416,7 +771,7 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
 
                                c->flags = init[3] & 0x7;
                        } else {
-                               c->flags = UTCP_TCP;
+                               c->flags = UTCP_UDP;
                        }
 
 synack:
@@ -1435,7 +790,7 @@ synack:
                        pkt.hdr.dst = c->dst;
                        pkt.hdr.ack = c->rcv.irs + 1;
                        pkt.hdr.seq = c->snd.iss;
-                       pkt.hdr.wnd = c->rcvbuf.maxsize;
+                       pkt.hdr.wnd = c->utcp->mtu;
                        pkt.hdr.ctl = SYN | ACK;
 
                        if(init) {
@@ -1497,53 +852,14 @@ synack:
                break;
        }
 
-       // 1b. Discard data that is not in our receive window.
-
-       if(is_reliable(c)) {
-               bool acceptable;
-
-               if(c->state == SYN_SENT) {
-                       acceptable = true;
-               } else if(len == 0) {
-                       acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0;
-               } else {
-                       int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
-
-                       // cut already accepted front overlapping
-                       if(rcv_offset < 0) {
-                               acceptable = len > (size_t) - rcv_offset;
-
-                               if(acceptable) {
-                                       ptr -= rcv_offset;
-                                       len += rcv_offset;
-                                       hdr.seq -= rcv_offset;
-                               }
-                       } else {
-                               acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
-                       }
-               }
-
-               if(!acceptable) {
-                       debug(c, "packet not acceptable, %u <= %u + %lu < %u\n", c->rcv.nxt, hdr.seq, (unsigned long)len, c->rcv.nxt + c->rcvbuf.maxsize);
-
-                       // Ignore unacceptable RST packets.
-                       if(hdr.ctl & RST) {
-                               return 0;
-                       }
-
-                       // Otherwise, continue processing.
-                       len = 0;
-               }
-       } else {
 #if UTCP_DEBUG
-               int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
+       int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
 
-               if(rcv_offset) {
-                       debug(c, "packet out of order, offset %u bytes", rcv_offset);
-               }
+       if(rcv_offset) {
+               debug(c, "packet out of order, offset %u bytes", rcv_offset);
+       }
 
 #endif
-       }
 
        c->snd.wnd = hdr.wnd; // TODO: move below
 
@@ -1551,10 +867,8 @@ synack:
        // ackno should not roll back, and it should also not be bigger than what we ever could have sent
        // (= snd.una + c->sndbuf.used).
 
-       if(!is_reliable(c)) {
-               if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
-                       hdr.ack = c->snd.una;
-               }
+       if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
+               hdr.ack = c->snd.una;
        }
 
        // 2. Handle RST packets
@@ -1574,10 +888,6 @@ synack:
                                c->recv(c, NULL, 0);
                        }
 
-                       if(c->poll && !c->reapable) {
-                               c->poll(c, 0);
-                       }
-
                        return 0;
 
                case SYN_RECEIVED:
@@ -1600,17 +910,11 @@ synack:
                        // The peer has aborted our connection.
                        set_state(c, CLOSED);
                        errno = ECONNRESET;
-                       buffer_clear(&c->sndbuf);
-                       buffer_clear(&c->rcvbuf);
 
                        if(c->recv) {
                                c->recv(c, NULL, 0);
                        }
 
-                       if(c->poll && !c->reapable) {
-                               c->poll(c, 0);
-                       }
-
                        return 0;
 
                case CLOSING:
@@ -1690,14 +994,6 @@ synack:
                assert(data_acked <= bufused);
 #endif
 
-               if(data_acked) {
-                       buffer_discard(&c->sndbuf, data_acked);
-
-                       if(is_reliable(c)) {
-                               c->do_poll = true;
-                       }
-               }
-
                // Also advance snd.nxt if possible
                if(seqdiff(c->snd.nxt, hdr.ack) < 0) {
                        c->snd.nxt = hdr.ack;
@@ -1721,8 +1017,8 @@ synack:
                        c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
                }
 
-               if(c->snd.cwnd > c->sndbuf.maxsize) {
-                       c->snd.cwnd = c->sndbuf.maxsize;
+               if(c->snd.cwnd > c->utcp->mtu) {
+                       c->snd.cwnd = c->utcp->mtu;
                }
 
                debug_cwnd(c);
@@ -1748,40 +1044,6 @@ synack:
                default:
                        break;
                }
-       } else {
-               if(!len && is_reliable(c) && c->snd.una != c->snd.last) {
-                       c->dupack++;
-                       debug(c, "duplicate ACK %d\n", c->dupack);
-
-                       if(c->dupack == 3) {
-                               // RFC 5681 fast recovery
-                               debug(c, "fast recovery started\n", c->dupack);
-                               uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
-                               c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
-                               c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mss, c->sndbuf.maxsize);
-
-                               if(c->snd.cwnd > c->sndbuf.maxsize) {
-                                       c->snd.cwnd = c->sndbuf.maxsize;
-                               }
-
-                               debug_cwnd(c);
-
-                               fast_retransmit(c);
-                       } else if(c->dupack > 3) {
-                               c->snd.cwnd += utcp->mss;
-
-                               if(c->snd.cwnd > c->sndbuf.maxsize) {
-                                       c->snd.cwnd = c->sndbuf.maxsize;
-                               }
-
-                               debug_cwnd(c);
-                       }
-
-                       // We got an ACK which indicates the other side did get one of our packets.
-                       // Reset the retransmission timer to avoid going to slow start,
-                       // but don't touch the connection timeout.
-                       start_retransmit_timer(c);
-               }
        }
 
        // 4. Update timers
@@ -1790,10 +1052,6 @@ synack:
                if(c->snd.una == c->snd.last) {
                        stop_retransmit_timer(c);
                        timespec_clear(&c->conn_timeout);
-               } else if(is_reliable(c)) {
-                       start_retransmit_timer(c);
-                       clock_gettime(UTCP_CLOCK, &c->conn_timeout);
-                       c->conn_timeout.tv_sec += utcp->timeout;
                }
        }
 
@@ -1816,7 +1074,6 @@ skip_ack:
                                c->snd.last++;
                                set_state(c, FIN_WAIT_1);
                        } else {
-                               c->do_poll = true;
                                set_state(c, ESTABLISHED);
                        }
 
@@ -1905,7 +1162,7 @@ skip_ack:
 
        // 7. Process FIN stuff
 
-       if((hdr.ctl & FIN) && (!is_reliable(c) || hdr.seq + len == c->rcv.nxt)) {
+       if(hdr.ctl & FIN) {
                switch(c->state) {
                case SYN_SENT:
                case SYN_RECEIVED:
@@ -1960,8 +1217,8 @@ skip_ack:
        // - or we got an ack, so we should maybe send a bit more data
        //   -> sendatleastone = false
 
-       if(is_reliable(c) || hdr.ctl & SYN || hdr.ctl & FIN) {
-               ack(c, has_data);
+       if(hdr.ctl & SYN || hdr.ctl & FIN) {
+               ack(c, NULL, 0);
        }
 
        return 0;
@@ -2053,7 +1310,7 @@ int utcp_shutdown(struct utcp_connection *c, int dir) {
 
        c->snd.last++;
 
-       ack(c, false);
+       ack(c, NULL, 0);
 
        if(!timespec_isset(&c->rtrx_timeout)) {
                start_retransmit_timer(c);
@@ -2074,9 +1331,6 @@ static bool reset_connection(struct utcp_connection *c) {
                return false;
        }
 
-       buffer_clear(&c->sndbuf);
-       buffer_clear(&c->rcvbuf);
-
        switch(c->state) {
        case CLOSED:
                return true;
@@ -2116,11 +1370,7 @@ static bool reset_connection(struct utcp_connection *c) {
 }
 
 static void set_reapable(struct utcp_connection *c) {
-       set_buffer_storage(&c->sndbuf, NULL, min(c->sndbuf.maxsize, DEFAULT_MAXSNDBUFSIZE));
-       set_buffer_storage(&c->rcvbuf, NULL, min(c->rcvbuf.maxsize, DEFAULT_MAXRCVBUFSIZE));
-
        c->recv = NULL;
-       c->poll = NULL;
        c->reapable = true;
 }
 
@@ -2144,11 +1394,6 @@ void utcp_reset_all_connections(struct utcp *utcp) {
                        errno = 0;
                        c->recv(c, NULL, 0);
                }
-
-               if(c->poll && !c->reapable) {
-                       errno = 0;
-                       c->poll(c, 0);
-               }
        }
 
        return;
@@ -2204,17 +1449,11 @@ struct timespec utcp_timeout(struct utcp *utcp) {
                if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
                        errno = ETIMEDOUT;
                        c->state = CLOSED;
-                       buffer_clear(&c->sndbuf);
-                       buffer_clear(&c->rcvbuf);
 
                        if(c->recv) {
                                c->recv(c, NULL, 0);
                        }
 
-                       if(c->poll && !c->reapable) {
-                               c->poll(c, 0);
-                       }
-
                        continue;
                }
 
@@ -2223,19 +1462,6 @@ struct timespec utcp_timeout(struct utcp *utcp) {
                        retransmit(c);
                }
 
-               if(c->poll) {
-                       if((c->state == ESTABLISHED || c->state == CLOSE_WAIT) && c->do_poll) {
-                               c->do_poll = false;
-                               uint32_t len = buffer_free(&c->sndbuf);
-
-                               if(len) {
-                                       c->poll(c, len);
-                               }
-                       } else if(c->state == CLOSED) {
-                               c->poll(c, 0);
-                       }
-               }
-
                if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) {
                        next = c->conn_timeout;
                }
@@ -2308,20 +1534,11 @@ void utcp_exit(struct utcp *utcp) {
                struct utcp_connection *c = utcp->connections[i];
 
                if(!c->reapable) {
-                       buffer_clear(&c->sndbuf);
-                       buffer_clear(&c->rcvbuf);
-
                        if(c->recv) {
                                c->recv(c, NULL, 0);
                        }
-
-                       if(c->poll && !c->reapable) {
-                               c->poll(c, 0);
-                       }
                }
 
-               buffer_exit(&c->rcvbuf);
-               buffer_exit(&c->sndbuf);
                free(c);
        }
 
@@ -2407,65 +1624,6 @@ void utcp_set_user_timeout(struct utcp *u, int timeout) {
        }
 }
 
-size_t utcp_get_sndbuf(struct utcp_connection *c) {
-       return c ? c->sndbuf.maxsize : 0;
-}
-
-size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
-       if(!c) {
-               return 0;
-       }
-
-       switch(c->state) {
-       case SYN_SENT:
-       case SYN_RECEIVED:
-       case ESTABLISHED:
-       case CLOSE_WAIT:
-               return buffer_free(&c->sndbuf);
-
-       default:
-               return 0;
-       }
-}
-
-void utcp_set_sndbuf(struct utcp_connection *c, void *data, size_t size) {
-       if(!c) {
-               return;
-       }
-
-       set_buffer_storage(&c->sndbuf, data, size);
-
-       c->do_poll = is_reliable(c) && buffer_free(&c->sndbuf);
-}
-
-size_t utcp_get_rcvbuf(struct utcp_connection *c) {
-       return c ? c->rcvbuf.maxsize : 0;
-}
-
-size_t utcp_get_rcvbuf_free(struct utcp_connection *c) {
-       if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT)) {
-               return buffer_free(&c->rcvbuf);
-       } else {
-               return 0;
-       }
-}
-
-void utcp_set_rcvbuf(struct utcp_connection *c, void *data, size_t size) {
-       if(!c) {
-               return;
-       }
-
-       set_buffer_storage(&c->rcvbuf, data, size);
-}
-
-size_t utcp_get_sendq(struct utcp_connection *c) {
-       return c->sndbuf.used;
-}
-
-size_t utcp_get_recvq(struct utcp_connection *c) {
-       return c->rcvbuf.used;
-}
-
 bool utcp_get_nodelay(struct utcp_connection *c) {
        return c ? c->nodelay : false;
 }
@@ -2486,23 +1644,12 @@ void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
        }
 }
 
-size_t utcp_get_outq(struct utcp_connection *c) {
-       return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
-}
-
 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
        if(c) {
                c->recv = recv;
        }
 }
 
-void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
-       if(c) {
-               c->poll = poll;
-               c->do_poll = is_reliable(c) && buffer_free(&c->sndbuf);
-       }
-}
-
 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_listen_t listen) {
        if(utcp) {
                utcp->accept = accept;
@@ -2565,10 +1712,6 @@ void utcp_offline(struct utcp *utcp, bool offline) {
        }
 }
 
-void utcp_set_retransmit_cb(struct utcp *utcp, utcp_retransmit_t cb) {
-       utcp->retransmit = cb;
-}
-
 void utcp_set_clock_granularity(long granularity) {
        CLOCK_GRANULARITY = granularity;
 }