+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;
+
+ pkt = malloc(c->utcp->mtu);
+
+ if(!pkt) {
+ return;
+ }
+
+ 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;
+ }
+
+ free(pkt);
+}
+
+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");
+ stop_retransmit_timer(c);
+ 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 SYN_SENT:
+ // Send our SYN again
+ pkt->hdr.seq = c->snd.iss;
+ pkt->hdr.ack = 0;
+ pkt->hdr.ctl = SYN;
+ 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, "rtrx", pkt, sizeof(pkt->hdr) + 4);
+ utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
+ break;
+
+ case SYN_RECEIVED:
+ // Send SYNACK again
+ pkt->hdr.seq = c->snd.nxt;
+ pkt->hdr.ack = c->rcv.nxt;
+ pkt->hdr.ctl = SYN | ACK;
+ print_packet(c, "rtrx", pkt, sizeof(pkt->hdr));
+ utcp->send(utcp, pkt, sizeof(pkt->hdr));
+ break;
+
+ 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;
+ }
+
+ // 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);
+
+ 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:
+ case LISTEN:
+ case TIME_WAIT:
+ case FIN_WAIT_2:
+ // We shouldn't need to retransmit anything in this state.
+#ifdef UTCP_DEBUG
+ abort();
+#endif
+ stop_retransmit_timer(c);
+ goto cleanup;
+ }
+
+ start_retransmit_timer(c);
+ utcp->rto *= 2;
+
+ if(utcp->rto > MAX_RTO) {
+ utcp->rto = MAX_RTO;
+ }
+
+ c->rtt_start.tv_sec = 0; // invalidate RTT timer
+ c->dupack = 0; // cancel any ongoing fast recovery
+
+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 || (size_t)rxd < len) {
+ abort();
+ }
+
+ // 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) {
+ // Check if we can process out-of-order data now.
+ if(c->sacks[0].len && len >= c->sacks[0].offset) { // TODO: handle overlap with second SACK
+ debug(c, "incoming packet len %lu connected with SACK at %u\n", (unsigned long)len, c->sacks[0].offset);
+ buffer_put_at(&c->rcvbuf, 0, data, len); // TODO: handle return value
+ len = max(len, c->sacks[0].offset + c->sacks[0].len);
+ data = c->rcvbuf.data;
+ }
+
+ if(c->recv) {
+ ssize_t rxd = c->recv(c, data, len);
+
+ if(rxd < 0 || (size_t)rxd != len) {
+ // TODO: handle the application not accepting all data.
+ abort();
+ }
+ }
+
+ if(c->rcvbuf.used) {
+ sack_consume(c, len);
+ }
+
+ c->rcv.nxt += len;
+}
+
+
+static void handle_incoming_data(struct utcp_connection *c, uint32_t seq, const void *data, size_t len) {
+ if(!is_reliable(c)) {
+ c->recv(c, data, len);
+ c->rcv.nxt = seq + len;
+ return;
+ }
+
+ uint32_t offset = seqdiff(seq, c->rcv.nxt);
+
+ if(offset + len > c->rcvbuf.maxsize) {
+ abort();
+ }
+
+ if(offset) {
+ handle_out_of_order(c, offset, data, len);
+ } else {
+ handle_in_order(c, data, len);
+ }