+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 SYN_SENT:
+ // Send our SYN again
+ 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:
+ // Send SYNACK again
+ 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:
+ 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 = seqdiff(c->snd.last, c->snd.una);
+ if(len > utcp->mtu)
+ len = utcp->mtu;
+ if(fin_wanted(c, c->snd.una + len)) {
+ len--;
+ pkt->hdr.ctl |= FIN;
+ }
+ c->snd.nxt = c->snd.una + len;
+ c->snd.cwnd = utcp->mtu; // reduce cwnd on retransmit
+ 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;
+
+ 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
+
+cleanup:
+ free(pkt);
+}
+
+// Update receive buffer and SACK entries after consuming data.
+static void sack_consume(struct utcp_connection *c, size_t len) {
+ debug("sack_consume %zu\n", len);
+ if(len > c->rcvbuf.used)
+ abort();
+
+ buffer_get(&c->rcvbuf, NULL, 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].offset = 0;
+ c->sacks[i].len -= len - c->sacks[i].offset;
+ i++;
+ } else {
+ if(i < NSACKS - 1) {
+ memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof c->sacks[i]);
+ c->sacks[i + 1].len = 0;
+ } else {
+ c->sacks[i].len = 0;
+ break;
+ }
+ }
+ }
+
+ for(int i = 0; i < NSACKS && c->sacks[i].len; i++)
+ debug("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("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)
+ 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("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("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;
+ }
+ break;
+ } else { // merge
+ debug("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("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("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("incoming packet len %zu connected with SACK at %u\n", 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 != 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) {
+ 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);
+}
+
+