@@ -94,7 +94,10 @@ static void handleCreate(Circuit* circuit, ControlCell cell) {
9494 printf (" handleCreate\n " );
9595 // initialises the circuit for the to-added cell.
9696 int idx = findCircuitSlot ();
97- if (idx == -1 ) return ;
97+ if (idx == -1 ) {
98+ printf (" No space for new circuit, dropping CREATE cell.\n " );
99+ return ;
100+ }
98101 circuit = &CIRCUITS [idx];
99102 circuit->circID = cell.circID ;
100103 // generate fresh ECDH key pair and derive the shared session key
@@ -121,7 +124,10 @@ static void handleCreated(Circuit* circuit, ControlCell cell) {
121124 // derive shared session key
122125 uint8_t sessionKey[KEY_SIZE ];
123126 ecdh_shared_secret (circuit->pendingPrivateKey , cell.payload , sessionKey); // payload is the public key of the extended node
124- if (!verifyDigest (sessionKey, cell.digest )) return ; // invalid digest, drop message
127+ if (!verifyDigest (sessionKey, cell.digest )) {
128+ printf (" Error: digest verification failed for circuit %d, dropping CREATED cell.\n " , circuit->circID );
129+ return ;
130+ }
125131 // clear private key, only the session key is needed
126132 memset (circuit->pendingPrivateKey , 0 , KEY_SIZE );
127133 memcpy (circuit->hopSessionKeys [circuit->hopCount ].key , sessionKey, KEY_SIZE );
@@ -174,6 +180,7 @@ static void handleDestroy(Circuit* circuit, uint16_t incomingCircID) {
174180 memset (entry, 0 , sizeof (ForwardingEntry)); // clear forwarding entry
175181 }
176182 clearInternalCircuitState (circuit);
183+ printf (" Circuit %d destroyed.\n " , incomingCircID);
177184}
178185
179186
@@ -183,7 +190,7 @@ static void handleDestroy(Circuit* circuit, uint16_t incomingCircID) {
183190void sendExtend (Circuit* circuit) {
184191 printf (" sendExtend\n " );
185192 if (circuit->hopCount >= MAX_HOPS ) {
186- printf (" Maximum hops reached, cannot extend further.\n " );
193+ printf (" Maximum hops reached for circuit %d , cannot extend further.\n " , circuit-> circID );
187194 return ;
188195 };
189196 // generate fresh ECDH key pair, new one for each hop. private stored in circuit until EXTENDED is received
@@ -231,41 +238,14 @@ static void handleExtend(Circuit* circuit, RelayCell cell) {
231238 serialiseAndSendCell (&createCell);
232239}
233240
234- /* *
235- //function to handle a received RELAY EXTENDED cell
236- void handleExtended(Circuit* circuit, RelayCell cell) {
237- printf("handleExtended\n");
238-
239- //derive shared secret key
240- uint8_t sessionKey[KEY_SIZE];
241- ecdh_shared_secret(circuit->pendingPrivateKey, cell.payload, sessionKey); //payload is the public key of the extended node
242-
243- printf("session key: ");
244- for (size_t i = 0; i < KEY_SIZE; i++) printf("%02x", sessionKey[i]);
245- printf("\n");
246-
247- // clear private key, only sessionKey is now needed
248- memset(circuit->pendingPrivateKey, 0, KEY_SIZE);
249-
250- //store session key for actual hop
251- memcpy(circuit->hopSessionKeys[circuit->hopCount].key, sessionKey, KEY_SIZE);
252- circuit->hopCount++;
253-
254- // Now check if we need to extend further
255- if (circuit->hopCount < circuit->totalHops) {
256- sendExtend(circuit); // triggers the next hop
257- } else {
258- circuit->state = CircuitState::READY;
259- printf("!!! CIRCUIT %d WITH %d HOPS IS BUILT.\n", circuit->circID, circuit->hopCount);
260- }
261- }
262- */
263-
264241// forward an EXTENDED cell back to the previous node in the circuit towards the origin.
265242static void forwardCreated (Circuit* circuit, ControlCell cell) {
266243 printf (" forwardCreated\n " );
267244 ForwardingEntry* entry = getReverseForwardingEntry (cell.circID ); // forwarding entry contains the previous circuit ID and previous node ID
268- if (entry == nullptr ) return ; // no entry (and thus no previous node in circuit) found, drop the information cell
245+ if (entry == nullptr ) {
246+ printf (" Error: no forwarding entry found for circuit %d, dropping CREATED cell.\n " , cell.circID );
247+ return ; // no entry (and thus no previous node in circuit) found, drop the information cell
248+ }
269249 ControlCell extendedCell;
270250 fillControlCell (extendedCell, entry->prevCircID , CellCommand::CREATED , nodeID, entry->prevNodeID , cell.payload , KEY_SIZE , cell.digest , HASH_SIZE );
271251 serialiseAndSendCell (&extendedCell);
@@ -336,7 +316,10 @@ static uint8_t* handleData(Circuit* circuit, RelayCell cell, uint8_t* buffer) {
336316 printf (" handleData\n " );
337317 uint8_t digest[HASH_SIZE ];
338318 memcpy (digest, cell.digest , HASH_SIZE );
339- if (!verifyDigest (cell.payload , digest)) return nullptr ; // invalid digest, drop message
319+ if (!verifyDigest (cell.payload , digest)) {
320+ printf (" Error: digest verification failed for circuit %d, dropping DATA cell.\n " , cell.circID );
321+ return nullptr ; // invalid digest, drop message
322+ }
340323 memcpy (buffer, cell.payload , cell.payloadLength );
341324 return buffer;
342325}
@@ -396,97 +379,6 @@ Circuit* buildCircuit(uint16_t destNodeID, uint8_t totalHops, LoraHashTable tabl
396379
397380// ------------------------------------ MESSAGE DISPATCH: CONTROL CELLS ------------------------------------ //
398381
399- /* *
400- //function to listen for incoming LoRa messages and dispatch them based on command
401- void LoRaMessageListener2() {
402- while (true) {
403- std::this_thread::sleep_for(std::chrono::milliseconds(100));
404- uint8_t data[CELL_SIZE]; // buffer for received data
405- uint16_t state = radio_receive_bytes_extern(data, CELL_SIZE);
406- if (state == 0) {
407- ControlCell controlCell = ControlCell::deserialise(data, CELL_SIZE);
408- CellCommand command = controlCell.command;
409- uint16_t circID = controlCell.circID;
410- uint16_t destNodeID = controlCell.destNodeID;
411-
412- printf("Received cell destNodeID: %d, my nodeID: %d\n", destNodeID, nodeID);
413-
414- //a new node announces itself: update table and send aknowledgement
415- if (command == CellCommand::ANNOUNCE) {
416- handleAnnounce(controlCell);
417- continue;
418- } else if (command == CellCommand::ANNOUNCE_ACK && destNodeID == nodeID) { //targeted acknowledegement
419- handleAnnounceAck(controlCell);
420- continue;
421- }
422-
423- //message not destined for own node
424- if (destNodeID == GLOBAL_ID ||destNodeID != nodeID) continue;
425-
426- printf("Command: %d\n", command);
427-
428- //TODO: determine circuit ID and look up circuit to get session keys for decryption
429- Circuit* circuit = getCircuit(circID);
430-
431- if (circuit == nullptr && command == CellCommand::CREATE) { //create a circuit
432- int idx = findCircuitSlot();
433- if (idx == -1) continue; // no space
434- circuit = &CIRCUITS[idx];
435- circuit->circID = circID;
436- circuit->hopCount = 0;
437- circuit->state = CircuitState::IDLE;
438- } else if (circuit == nullptr && command == CellCommand::CREATED) { //see below
439- } else if (circuit == nullptr && command == CellCommand::DESTROY) { //see below
440- } else if (circuit == nullptr && command == CellCommand::RELAY) {
441- //data could be forwarding from the last node: check if given circID is in the forwarding table
442- ForwardingEntry* entry = getReverseForwardingEntry(circID);
443- if (entry != nullptr) {
444- circuit = getCircuit(entry->prevCircID);
445- if (circuit == nullptr) continue; // no circuit, drop
446- dispatchRelay(circuit, data); //forward RELAY cell back to previous node in circuit
447- continue;
448- } else {
449- // no forwarding entry, drop message
450- continue;
451- }
452- } else if (circuit == nullptr) {
453- continue; // unknown circuit, drop
454- }
455-
456-
457-
458- ForwardingEntry* entry;
459- switch (command) {
460- case CREATE:
461- handleCreate(circuit, controlCell);
462- break;
463- case CREATED:
464- entry = getReverseForwardingEntry(circID);
465- if(entry != nullptr) {
466- //Node is relay: forward information
467- circuit = getCircuit(entry->prevCircID);
468- if (circuit == nullptr) break; // no circuit, drop
469- forwardCreated(circuit, controlCell); //forward CREATED cell back to previous node in circuit
470- } else {
471- //Node is origin: handle CREATED
472- if (circuit == nullptr) break; // no circuit, drop
473- handleCreated(circuit, controlCell);
474- }
475- break;
476- case DESTROY:
477- handleDestroy(circuit, circID);
478- break;
479- case RELAY:
480- dispatchRelay(circuit, data); //payload of control cell contains the encrypted relay cell
481- break;
482- default:
483- break;
484- }
485- } else {}
486- }
487- }
488- */
489-
490382// Internal loop of the node: reads incoming information nodes and handles them accordingly.
491383void LoRaMessageListener () {
492384 while (true ) {
@@ -551,7 +443,7 @@ static void dispatchControl(ControlCell& cell, uint8_t* data) {
551443}
552444
553445
554- // ------------------------------------ MESSAGE DISPATCH: CONTROL CELLS ------------------------------------ //
446+ // ------------------------------------ MESSAGE DISPATCH: RELAY CELLS ------------------------------------ //
555447
556448// When a relay cell forwards information to the origin or exit, it rewrites relay
557449// information such as the next circuit ID, next node's ID and its own ID. This is
@@ -654,106 +546,6 @@ static void dispatchRelay(Circuit* circuit, uint8_t* data, uint16_t circID) {
654546}
655547
656548
657- /* *
658- void dispatchRelay2(Circuit* circuit, uint8_t* data) {
659- printf("dispatchRelay\n");
660-
661- uint16_t incomingCircID = (data[0] << 8) | data[1];
662- uint16_t incomingSrcNodeID = (data[5] << 8) | data[6];
663-
664- HopSecretKey circuitKey = circuit->hopSessionKeys[0];
665-
666- uint8_t copy[CELL_SIZE];
667- memcpy(copy, data, CELL_SIZE);
668- // Decrypt the raw buffer first, same region that was encrypted
669- decryptSingleHop(circuitKey.key, circuitKey.nonce, data + RELAY_HEADER_SIZE, CELL_SIZE - RELAY_HEADER_SIZE);
670-
671- // Now deserialise the decrypted buffer
672- RelayCell cell = RelayCell::deserialise(data, CELL_SIZE);
673-
674- // Check if this cell is meant for us
675- uint8_t hash[HASH_SIZE];
676- hashData(cell.payload, hash);
677- bool digestMatch = (memcmp(hash, cell.digest, HASH_SIZE) == 0);
678-
679- printf("digest match: %d\n", digestMatch);
680- printf("relayCommand: %d payloadLength: %d\n", cell.relayCommand, cell.payloadLength);
681-
682- if (!digestMatch) {
683- ForwardingEntry* entry = getForwardingEntry(incomingCircID);
684-
685- if (entry != nullptr && incomingSrcNodeID == entry->prevNodeID) {
686- if (entry == nullptr) return;
687- // rewrite circID and dest towards next hop in circuit
688- data[0] = (entry->nextCircID >> 8) & 0xFF;
689- data[1] = entry->nextCircID & 0xFF;
690- data[3] = (entry->nextNodeID >> 8) & 0xFF;
691- data[4] = entry->nextNodeID & 0xFF;
692- data[5] = (nodeID >> 8) & 0xFF; // rewrite srcNodeID to self
693- data[6] = nodeID & 0xFF;
694- radio_transmit_bytes_extern(data, CELL_SIZE);
695- } else {
696- // reverse lookup toward prev hop
697- entry = getReverseForwardingEntry(incomingCircID);
698- if (entry == nullptr) {
699- //DATA forwarded towards origin, decrypt all layers
700- decryptOnion(
701- circuit->hopCount,
702- circuit->hopSessionKeys,
703- copy + RELAY_HEADER_SIZE,
704- CELL_SIZE - RELAY_HEADER_SIZE
705- );
706- RelayCell copyCell = RelayCell::deserialise(copy, CELL_SIZE);
707- uint8_t buffer[CELL_SIZE - RELAY_HEADER_SIZE - HASH_SIZE];
708- uint8_t* result = handleData(circuit, copyCell, buffer);
709- if (result != nullptr) {
710- printf("Received data: %s\n", result);
711- }
712- return;
713- }
714-
715- copy[0] = (entry->prevCircID >> 8) & 0xFF;
716- copy[1] = entry->prevCircID & 0xFF;
717- copy[3] = (entry->prevNodeID >> 8) & 0xFF;
718- copy[4] = entry->prevNodeID & 0xFF;
719- copy[5] = (nodeID >> 8) & 0xFF; // rewrite srcNodeID to self
720- copy[6] = nodeID & 0xFF;
721- encryptSingleHop(
722- circuit->hopSessionKeys[0].key,
723- circuit->hopSessionKeys[0].nonce,
724- copy + RELAY_HEADER_SIZE,
725- CELL_SIZE - RELAY_HEADER_SIZE
726- );
727- radio_transmit_bytes_extern(copy, CELL_SIZE);
728- }
729- return;
730- }
731-
732- uint8_t* result = nullptr;
733- printf("Relay command: %d\n", cell.relayCommand);
734- switch (cell.relayCommand) {
735- case EXTEND:
736- handleExtend(circuit, cell);
737- break;
738- //case EXTENDED:
739- //handleExtended(circuit, cell);
740- //break;
741- case DATA:
742- uint8_t buffer[CELL_SIZE - RELAY_HEADER_SIZE - HASH_SIZE];
743- result = handleData(circuit, cell, buffer);
744- if (result != nullptr) {
745- printf("Received data: %s\n", result);
746- const char* msg = "Data received successfully!";
747- sendDataBackwards(circuit, cell.srcNodeID, cell.circID, (uint8_t*)msg, strlen(msg) + 1);
748- }
749- break;
750- default:
751- break;
752- }
753- }
754- */
755-
756-
757549// ------------------------------------ HELPFUNCTIONS ------------------------------------ //
758550
759551// abstraction function to fill a control cell with the given parameters
0 commit comments