Skip to content
This repository
Browse code

Fixed an assertion failure bug and a few compile issues with differen…

…t Log levels
  • Loading branch information...
commit 28e634b82a4d96ea287cc51376e4971bde73e873 1 parent 0fa433e
authored November 09, 2012
7  dht/SerializationModule.c
@@ -90,8 +90,6 @@ static int handleOutgoing(struct DHTMessage* message,
90 90
 static int handleIncoming(struct DHTMessage* message,
91 91
                           void* vcontext)
92 92
 {
93  
-    struct SerializationModule_context* context = vcontext;
94  
-
95 93
     message->asDict = message->allocator->malloc(sizeof(Dict), message->allocator);
96 94
 
97 95
     struct Reader* reader =
@@ -99,7 +97,10 @@ static int handleIncoming(struct DHTMessage* message,
99 97
 
100 98
     int ret = SERIALIZER->parseDictionary(reader, message->allocator, message->asDict);
101 99
     if (ret != 0) {
102  
-        Log_info(context->logger, "Failed to parse message [%d]", ret);
  100
+        #ifdef Log_INFO
  101
+            struct SerializationModule_context* context = vcontext;
  102
+            Log_info(context->logger, "Failed to parse message [%d]", ret);
  103
+        #endif
103 104
         return -2;
104 105
     }
105 106
 
105  net/Ducttape.c
@@ -344,6 +344,29 @@ static inline uint8_t sendToSwitch(struct Message* message,
344 344
                                    struct Headers_SwitchHeader* destinationSwitchHeader,
345 345
                                    struct Ducttape_pvt* context)
346 346
 {
  347
+    // Tag the message with the proper handle.
  348
+    struct SessionManager_Session* session = context->session;
  349
+    if (session->version > 0) {
  350
+        // If the session is established, we send their handle for the session,
  351
+        // otherwise we send ours.
  352
+        int state = CryptoAuth_getState(&session->iface);
  353
+        if (state >= CryptoAuth_HANDSHAKE3) {
  354
+            debugHandles(context->logger, session, "Sending run message");
  355
+
  356
+            Message_shift(message, 4);
  357
+            ((uint32_t*)message->bytes)[0] = session->sendHandle_be;
  358
+        } else if (state < CryptoAuth_HANDSHAKE3) {
  359
+            debugHandles(context->logger, session, "Sending start message");
  360
+
  361
+            // the most significant bit in a handle is reserved to tell the recipient if it is
  362
+            // an initiation handle or a running handle which they should look up in their map.
  363
+            Message_shift(message, 4);
  364
+            ((uint32_t*)message->bytes)[0] = session->receiveHandle_be;
  365
+        }
  366
+    } else {
  367
+        debugHandles(context->logger, session, "Sending protocol 0 message");
  368
+    }
  369
+
347 370
     Message_shift(message, Headers_SwitchHeader_SIZE);
348 371
     struct Headers_SwitchHeader* switchHeaderLocation =
349 372
         (struct Headers_SwitchHeader*) message->bytes;
@@ -446,16 +469,14 @@ static inline uint8_t incomingFromTun(struct Message* message,
446 469
     //
447 470
     // The CryptoAuth may send a 120 byte CA header and it might only send a 4 byte
448 471
     // nonce and 16 byte authenticator depending on it's state.
449  
-    if (CryptoAuth_getState(&session->iface) < CryptoAuth_HANDSHAKE3) {
450  
-        // shift, copy, shift because shifting asserts that there is enough buffer space.
451  
-        Message_shift(message, Headers_CryptoAuth_SIZE);
452  
-        Bits_memcpyConst(message->bytes, header, Headers_IP6Header_SIZE);
453  
-        Message_shift(message, -(Headers_IP6Header_SIZE + Headers_CryptoAuth_SIZE));
454  
-    } else {
455  
-        Message_shift(message, 20);
456  
-        Bits_memmoveConst(message->bytes, header, Headers_IP6Header_SIZE);
457  
-        Message_shift(message, -(20 + Headers_IP6Header_SIZE));
458  
-    }
  472
+    // NOTE: We can't check it's state now, we have to assume it will use the 120 byte
  473
+    //       header because we might be in a currently established session which has
  474
+    //       timed out and will be in state HANDSHALE1 after we call sendMessage().
  475
+
  476
+    // shift, copy, shift because shifting asserts that there is enough buffer space.
  477
+    Message_shift(message, Headers_CryptoAuth_SIZE);
  478
+    Bits_memcpyConst(message->bytes, header, Headers_IP6Header_SIZE);
  479
+    Message_shift(message, -(Headers_IP6Header_SIZE + Headers_CryptoAuth_SIZE));
459 480
     // The message is now aligned on the content.
460 481
 
461 482
     // This comes out at outgoingFromCryptoAuth() then outgoingFromMe()
@@ -616,10 +637,36 @@ static inline int core(struct Message* message, struct Ducttape_pvt* context)
616 637
  */
617 638
 static inline uint8_t outgoingFromMe(struct Message* message, struct Ducttape_pvt* context)
618 639
 {
619  
-    // incomingFromTun checks the state of the CA session and puts the IP6 header in the
620  
-    // right place so that it will be right behind the cryptoauth header.
  640
+    // incomingFromTun puts the IP6 header Headers_CryptoAuth_SIZE bytes behind
  641
+    // the beginning of the data, depending on the state of the CA, it might need to be moved.
  642
+
  643
+    if (CryptoAuth_getState(&context->session->iface) >= CryptoAuth_HANDSHAKE3) {
  644
+        //     [ ip6 ][        CA header  [  actual CA header ][ content.....
  645
+        //                                ^-- you are here
  646
+        // actual CA header == 20
  647
+
  648
+        uint8_t* beginningOfCaHeader = message->bytes;
  649
+
  650
+        Message_shift(message, Headers_IP6Header_SIZE + Headers_CryptoAuth_SIZE - 20);
  651
+
  652
+        //     [ ip6 ][        CA header  [  actual CA header ][ content.....
  653
+        //     ^-- moved
  654
+
  655
+        Bits_memcpyConst(beginningOfCaHeader - Headers_IP6Header_SIZE,
  656
+                         message->bytes,
  657
+                         Headers_IP6Header_SIZE);
  658
+
  659
+        //                         [ ip6 ][  actual CA header ][ content.....
  660
+        //     ^-- still here but ip6 header moved
  661
+
  662
+        Message_shift(message, -(Headers_CryptoAuth_SIZE - 20));
  663
+
  664
+        //                         [ ip6 ][  actual CA header ][ content.....
  665
+        //                         ^-- you are here
  666
+    } else {
  667
+        Message_shift(message, Headers_IP6Header_SIZE);
  668
+    }
621 669
 
622  
-    Message_shift(message, Headers_IP6Header_SIZE);
623 670
     struct Headers_IP6Header* header = (struct Headers_IP6Header*) message->bytes;
624 671
 
625 672
     if (!Bits_memcmp(header->destinationAddr, context->myAddr.ip6.bytes, 16)) {
@@ -696,29 +743,7 @@ static uint8_t outgoingFromCryptoAuth(struct Message* message, struct Interface*
696 743
     enum Ducttape_SessionLayer layer = context->layer;
697 744
     context->layer = Ducttape_SessionLayer_INVALID;
698 745
 
699  
-    // Tag the message with the proper handle.
700  
-    struct SessionManager_Session* session = context->session;
701 746
     if (layer == Ducttape_SessionLayer_OUTER) {
702  
-        if (session->version > 0) {
703  
-            // If the session is established, we send their handle for the session,
704  
-            // otherwise we send ours.
705  
-            int state = CryptoAuth_getState(&session->iface);
706  
-            if (state >= CryptoAuth_HANDSHAKE3) {
707  
-                debugHandles(context->logger, session, "Sending run message");
708  
-
709  
-                Message_shift(message, 4);
710  
-                ((uint32_t*)message->bytes)[0] = session->sendHandle_be;
711  
-            } else if (state < CryptoAuth_HANDSHAKE3) {
712  
-                debugHandles(context->logger, session, "Sending start message");
713  
-
714  
-                // the most significant bit in a handle is reserved to tell the recipient if it is
715  
-                // an initiation handle or a running handle which they should look up in their map.
716  
-                Message_shift(message, 4);
717  
-                ((uint32_t*)message->bytes)[0] = session->receiveHandle_be;
718  
-            }
719  
-        } else {
720  
-            debugHandles(context->logger, session, "Sending protocol 0 message");
721  
-        }
722 747
         return sendToSwitch(message, context->switchHeader, context);
723 748
     } else {
724 749
         Log_debug(context->logger, "Sending layer3 message");
@@ -985,6 +1010,16 @@ static uint8_t incomingFromSwitch(struct Message* message, struct Interface* swi
985 1010
         return 0;
986 1011
     }
987 1012
 
  1013
+    if (((uint32_t*)message->bytes)[0] == 0xffffffff) {
  1014
+        // Got a message from a node that doesn't know our key, this should never happen.
  1015
+        #ifdef Log_INFO
  1016
+            uint8_t path[20];
  1017
+            AddrTools_printPath(path, Endian_bigEndianToHost64(switchHeader->label_be));
  1018
+            Log_info(context->logger, "Dropped packet from node [%s] which doesn't know our key",
  1019
+                     path);
  1020
+        #endif
  1021
+    }
  1022
+
988 1023
     // This is needed so that the priority and other information
989 1024
     // from the switch header can be passed on properly.
990 1025
     context->switchHeader = switchHeader;

0 notes on commit 28e634b

Please sign in to comment.
Something went wrong with that request. Please try again.