瀏覽代碼

* simple_secure_chat: bug fixes

Scott Powell 1 年之前
父節點
當前提交
d06532d6f1
共有 3 個文件被更改,包括 17 次插入6 次删除
  1. 10 6
      examples/simple_secure_chat/main.cpp
  2. 2 0
      platformio.ini
  3. 5 0
      src/Mesh.cpp

+ 10 - 6
examples/simple_secure_chat/main.cpp

@@ -53,7 +53,6 @@ struct ContactInfo {
 class MyMesh : public mesh::Mesh {
 public:
   SimpleSeenTable* _table;
-  mesh::LocalIdentity self_id;
   ContactInfo contacts[MAX_CONTACTS];
   int num_contacts;
 
@@ -128,11 +127,11 @@ protected:
       // len can be > original length, but 'text' will be padded with zeroes
       data[len] = 0; // need to make a C string again, with null terminator
 
-      Serial.printf("MSG -> from %s\n", from.name);
+      Serial.printf("(%s) MSG -> from %s\n", packet->isRouteFlood() ? "FLOOD" : "DIRECT", from.name);
       Serial.printf("   %s\n", (const char *) &data[4]);
 
       uint32_t ack_hash;    // calc truncated hash of the message timestamp + text + sender pub_key, to prove to sender that we got it
-      mesh::Utils::sha256((uint8_t *) &ack_hash, 4, data, len, from.id.pub_key, PUB_KEY_SIZE);
+      mesh::Utils::sha256((uint8_t *) &ack_hash, 4, data, 4 + strlen((char *)&data[4]), from.id.pub_key, PUB_KEY_SIZE);
 
       if (packet->isRouteFlood()) {
         // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the ACK
@@ -190,6 +189,10 @@ protected:
       // NOTE: the same ACK can be received multiple times!
       expected_ack_crc = 0;  // reset our expected hash, now that we have received ACK
       txt_send_timeout = 0;
+    } else {
+      uint32_t crc;
+      memcpy(&crc, data, 4);
+      MESH_DEBUG_PRINTLN("  unknown ACK received: %08X (expected: %08X)", crc, expected_ack_crc);
     }
   }
 
@@ -209,10 +212,10 @@ public:
     uint8_t temp[4+MAX_TEXT_LEN+1];
     uint32_t timestamp = getRTCClock()->getCurrentTime();
     memcpy(temp, &timestamp, 4);   // mostly an extra blob to help make packet_hash unique
-    memcpy(&temp[4], text, text_len);
+    memcpy(&temp[4], text, text_len + 1);
 
     // calc expected ACK reply
-    mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, (const uint8_t *) temp, 4 + text_len, self_id.pub_key, PUB_KEY_SIZE);
+    mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, temp, 4 + text_len, self_id.pub_key, PUB_KEY_SIZE);
 
     return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, 4 + text_len);
   }
@@ -303,11 +306,12 @@ void loop() {
         if (recipient.out_path_len < 0) {
           the_mesh.sendFlood(pkt);
           txt_send_timeout = the_mesh.futureMillis(FLOOD_SEND_TIMEOUT_MILLIS);
+          Serial.println("   (message sent - FLOOD)");
         } else {
           the_mesh.sendDirect(pkt, recipient.out_path, recipient.out_path_len);
           txt_send_timeout = the_mesh.futureMillis(DIRECT_TIMEOUT_FACTOR*recipient.out_path_len + DIRECT_TIMEOUT_BASE);
+          Serial.println("   (message sent - DIRECT)");
         }
-        Serial.println("   (message sent)");
       } else {
         Serial.println("   ERROR: unable to create packet.");
       }

+ 2 - 0
platformio.ini

@@ -73,6 +73,7 @@ build_flags =
   ${Heltec_lora32_v3.build_flags} 
   -D RUN_AS_ALICE=true
 ; -D NODE_ID=1
+  -D MESH_DEBUG=1
 build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<../examples/simple_secure_chat/main.cpp>
 
 [env:Heltec_v3_chat_bob]
@@ -81,6 +82,7 @@ build_flags =
   ${Heltec_lora32_v3.build_flags} 
   -D RUN_AS_ALICE=false
 ; -D NODE_ID=3
+  -D MESH_DEBUG=1
 build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<../examples/simple_secure_chat/main.cpp>
 
 [env:Heltec_v3_test_admin]

+ 5 - 0
src/Mesh.cpp

@@ -70,6 +70,7 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
           // scan contacts DB, for all matching hashes of 'src_hash' (max 4 matches supported ATM)
           int num = searchPeersByHash(&src_hash);
           // for each matching contact, try to decrypt data
+          bool found = false;
           for (int j = 0; j < num; j++) {
             uint8_t secret[PUB_KEY_SIZE];
             getPeerSharedSecret(secret, j);
@@ -89,9 +90,13 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
               } else {
                 onPeerDataRecv(pkt, pkt->getPayloadType(), j, data, len);
               }
+              found = true;
               break;
             }
           }
+          if (!found) {
+            MESH_DEBUG_PRINTLN("recv matches no peers, src_hash=%02X", (uint32_t)src_hash);
+          }
         }
         action = routeRecvPacket(pkt);
       }