+/* 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("sack_consume %lu\n", (unsigned long)len);
+ if(len > c->rcvbuf.used) {
+ debug("All SACK entries consumed");
+ c->sacks[0].len = 0;
+ return;
+ }
+
+ 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].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("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;
+ } else {
+ debug("SACK entries full, dropping packet\n");
+ }
+ 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 %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 != 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);
+}
+