]> git.seodisparate.com - UDPConnection/commitdiff
Fix handling of timed out packets
authorStephen Seo <seo.disparate@gmail.com>
Tue, 19 Feb 2019 04:28:16 +0000 (13:28 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 19 Feb 2019 04:28:16 +0000 (13:28 +0900)
src/UDPConnection.c
src/UDPConnection.h

index be0659c030249132249dc13e663707e609bcd8c4..00d222678c7b35960b69af7771d8c6ac9bac6ca9 100644 (file)
@@ -350,7 +350,7 @@ void UDPC_update(UDPC_Context *ctx)
 
     UDPC_INTERNAL_update_rtt(ctx, cd, rseq, &us.tsNow);
     cd->received = us.tsNow;
-    UDPC_INTERNAL_check_pkt_timeout(cd, rseq, ack, &us.tsNow);
+    UDPC_INTERNAL_check_pkt_timeout(ctx, cd, rseq, ack, &us.tsNow);
 
     int isOutOfOrder = 0;
     uint32_t diff = 0;
@@ -683,6 +683,7 @@ void UDPC_INTERNAL_update_rtt(
 }
 
 void UDPC_INTERNAL_check_pkt_timeout(
+    UDPC_Context *ctx,
     UDPC_INTERNAL_ConnectionData *cd,
     uint32_t rseq,
     uint32_t ack,
@@ -707,15 +708,29 @@ void UDPC_INTERNAL_check_pkt_timeout(
                 float seconds = UDPC_ts_diff_to_seconds(tsNow, &pinfo->sent);
                 if(seconds >= UDPC_PACKET_TIMEOUT_SEC)
                 {
+                    if(pinfo->size <= 20)
+                    {
+                        UDPC_INTERNAL_log(ctx, 0,
+                            "ERROR: Timed out sentPkt (%d) to %s has size at most 20",
+                            rseq,
+                            UDPC_INTERNAL_atostr(ctx, cd->addr));
+                        pinfo->flags |= 0x4; // treat as resent to avoid reprinting error
+                        break;
+                    }
                     // packet timed out, resending
                     UDPC_INTERNAL_PacketInfo newPkt = {
                         cd->addr,
                         0,
                         0,
-                        pinfo->data,
-                        pinfo->size,
+                        NULL,
+                        0,
                         {0, 0}
                     };
+                    newPkt.size = pinfo->size - 20;
+                    newPkt.data = malloc(newPkt.size);
+                    memcpy(newPkt.data, pinfo->data + 20, newPkt.size);
+                    free(pinfo->data);
+
                     pinfo->flags |= 0x4;
                     pinfo->data = NULL;
                     pinfo->size = 0;
index 03ec1a610d3dad439db4e09dd44532356b8370ca..a1ab794294e508eb2ff9c15fa3825e34ccc84507 100644 (file)
@@ -67,7 +67,7 @@ typedef struct {
     float rtt;
 } UDPC_INTERNAL_ConnectionData;
 
-/// This struct should not be modified, only passed to functions that require it
+/// This struct should not be used externally, only passed to functions that require it
 typedef struct {
     /*
      * 0x1 - is threaded
@@ -140,6 +140,7 @@ void UDPC_INTERNAL_update_rtt(
     struct timespec *tsNow);
 
 void UDPC_INTERNAL_check_pkt_timeout(
+    UDPC_Context *ctx,
     UDPC_INTERNAL_ConnectionData *cd,
     uint32_t rseq,
     uint32_t ack,