X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=utcp.c;h=95a82c428710ca91f5f2752045eea036bee40c79;hb=05ee8ad65c1c7f1318e1185ddf299a2cce6c6474;hp=7c577018888b0fd99b7cac6f9d66e29b9a0fd91c;hpb=64ef5642d83fc13829262ec6c1bddb6a20d9aa7a;p=utcp diff --git a/utcp.c b/utcp.c index 7c57701..95a82c4 100644 --- a/utcp.c +++ b/utcp.c @@ -88,6 +88,10 @@ static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, si if(len > sizeof hdr) { uint32_t datalen = len - sizeof hdr; uint8_t *str = malloc((datalen << 1) + 7); + if(!str) { + debug("out of memory"); + return; + } memcpy(str, " data=", 6); uint8_t *strptr = str + 6; const uint8_t *data = pkt; @@ -633,7 +637,24 @@ cleanup: free(pkt); } -// Update receive buffer and SACK entries after consuming data. +/* 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 ahead of ^, 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 %zu\n", len); if(len > c->rcvbuf.used) @@ -646,13 +667,13 @@ static void sack_consume(struct utcp_connection *c, size_t len) { 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; + 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[i + 1].len = 0; + c->sacks[NSACKS - 1].len = 0; } else { c->sacks[i].len = 0; break;