From 59b8e90d1fd3e5774c9a2305ac0dc5cf3b80f4a0 Mon Sep 17 00:00:00 2001 From: attermann Date: Mon, 9 Oct 2023 08:58:38 -0600 Subject: [PATCH] WIP update Cleanup. --- src/Destination.cpp | 15 +--- src/Packet.cpp | 197 ++------------------------------------------ src/Packet.h | 22 +---- src/main.cpp | 6 +- 4 files changed, 20 insertions(+), 220 deletions(-) diff --git a/src/Destination.cpp b/src/Destination.cpp index ecbdf8d..c70c3ff 100644 --- a/src/Destination.cpp +++ b/src/Destination.cpp @@ -215,17 +215,11 @@ Packet Destination::announce(const Bytes &app_data, bool path_response, Interfac } debug("Destination::announce: creating announce packet..."); - //announce_packet = RNS.Packet(self, announce_data, RNS.Packet.ANNOUNCE, context = announce_context, attached_interface = attached_interface) - //Packet announce_packet(*this, announce_data, Packet::ANNOUNCE, announce_context, Transport::BROADCAST, Packet::HEADER_1, nullptr, attached_interface); - Packet announce_packet(*this, announce_data, Packet::DATA, announce_context, Transport::BROADCAST, Packet::HEADER_1, nullptr, attached_interface); - extreme("Destination::announce: pre announce packet: " + announce_packet.toString()); + Packet announce_packet(*this, announce_data, Packet::ANNOUNCE, announce_context, Transport::BROADCAST, Packet::HEADER_1, nullptr, attached_interface); if (send) { announce_packet.send(); - extreme("Destination::announce: post announce packet: " + announce_packet.toString()); - // CBA temporarily returning copy of sent packet for testing purposes - //return Packet::NONE; - return announce_packet; + return Packet::NONE; } else { return announce_packet; @@ -281,9 +275,8 @@ void Destination::receive(const Packet &packet) { incoming_link_request(plaintext, packet); } else { - // CBA TEST determine why packet._data is being used instead of packet._raw for incoming packets - //Bytes plaintext(decrypt(packet._data)); - Bytes plaintext(decrypt(packet._raw.mid(19))); + // CBA TODO Why isn't the Packet decrypting itself? + Bytes plaintext(decrypt(packet._data)); extreme("Destination::receive: decrypted data: " + plaintext.toHex()); if (plaintext) { if (packet._packet_type == RNS::Packet::DATA) { diff --git a/src/Packet.cpp b/src/Packet.cpp index 5a0864f..3fa46a4 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -22,38 +22,18 @@ Packet::Packet(const Destination &destination, const Interface &attached_interfa _packet_type = packet_type; _transport_type = transport_type; _context = context; - _transport_id = transport_id; - _data = data; if (_data.size() > MDU) { _truncated = true; _data.resize(MDU); } -/* - if (data) { - // data is plaintext - if (data.size() > MDU) { - _truncated = true; - // CBA TODO add method to truncate - //zdata_len = MDU; - } - _data = _raw + Reticulum::HEADER_MAXSIZE; - memcpy(_data, data.data(), data.size()); - } -*/ _flags = get_packed_flags(); - _create_receipt = create_receipt; } else { extreme("Creating packet without detination..."); _raw = data; -/* - if (data) { - memcpy(_raw, data.data(), data.size()); - } -*/ _packed = true; _fromPacked = true; _create_receipt = false; @@ -249,129 +229,14 @@ but excluding any interface access codes. // | ...destination_2 | context | data ... | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -/* void Packet::pack() { assert(_object); debug("Packet::pack: packing packet..."); - extreme("Packet::pack: pre hops: " + std::to_string(_hops)); - - //memcpy(_destination_hash, _destination->_hash.data(), Reticulum::DESTINATION_LENGTH); - memcpy(_destination_hash, _object->_destination.hash().data(), _object->_destination.hash().size()); - - //uint8_t *ciphertext; - if (_context == LRPROOF) { - // write header - _header = _data - Reticulum::HEADER_MINSIZE; - _header[0] = _flags; - _header[1] = _hops; - //memcpy(header+2, _destination->_link_id, Reticulum::DESTINATION_LENGTH); - debug("Packet::pack: destination link id: " + _object->_destination.link_id().toHex() ); - memcpy(_header+2, _object->_destination.link_id().data(), _object->_destination.link_id().size()); - _header[Reticulum::DESTINATION_LENGTH+2] = _context; - // prepend header to _data in _raw bytes - memcpy(_data-Reticulum::HEADER_MINSIZE, _header, Reticulum::HEADER_MINSIZE); - //ciphertext = _data; - } - else { - if (_header_type == HEADER_1) { - // write header - _header = _data - Reticulum::HEADER_MINSIZE; - _header[0] = _flags; - _header[1] = _hops; - //memcpy(header+2, _destination->_hash.data(), Reticulum::DESTINATION_LENGTH); - debug("Packet::pack: destination hash: " + _object->_destination.hash().toHex() ); - memcpy(_header+2, _object->_destination.hash().data(), _object->_destination.hash().size()); - _header[Reticulum::DESTINATION_LENGTH+2] = _context; - // prepend header to _data in _raw bytes - memcpy(_data-Reticulum::HEADER_MINSIZE, _header, Reticulum::HEADER_MINSIZE); - - if (_packet_type == ANNOUNCE) { - // Announce packets are not encrypted - //ciphertext = _data; - } - else if (_packet_type == LINKREQUEST) { - // Link request packets are not encrypted - //ciphertext = _data; - } - else if (_packet_type == PROOF && _context == RESOURCE_PRF) { - // Resource proofs are not encrypted - //ciphertext = _data; - } - else if (_packet_type == PROOF && _object->_destination.type() == Destination::LINK) { - // Packet proofs over links are not encrypted - //ciphertext = _data; - } - else if (_context == RESOURCE) { - // A resource takes care of encryption - // by itself - //ciphertext = _data; - } - else if (_context == KEEPALIVE) { - // Keepalive packets contain no actual - // data - //ciphertext = _data; - } - else if (_context == CACHE_REQUEST) { - // Cache-requests are not encrypted - //ciphertext = _data; - } - else { - // In all other cases, we encrypt the packet - // with the destination's encryption method - // CBA TODO Figure out how to most efficiently pass in data and receive encrypted data back into _raw bytes - // CBA TODO Ensure that encrypted data does not exceed ENCRYPTED_MDU - // CBA TODO Determine if encrypt method can read from and write to the same bytes - //_data_len = _destination->encrypt(_data, _data, _data_len); - //uint8_t data[_data_len]; - //memcpy(data, _data, _data_len); - //_data_len = _destination->encrypt(_data, data, _data_len); - Bytes plaintext(_data, _data_len); - Bytes ciphertext = _object->_destination.encrypt(plaintext); - memcpy(_data, ciphertext.data(), ciphertext.size()); - _data_len = ciphertext.size(); - } - } - else if (_header_type == HEADER_2) { - if (memcmp(_transport_id, EMPTY_DESTINATION, Reticulum::DESTINATION_LENGTH) == 0) { - throw std::invalid_argument("Packet with header type 2 must have a transport ID"); - } - // write header - _header = _data - Reticulum::HEADER_MAXSIZE; - _header[0] = _flags; - _header[1] = _hops; - memcpy(_header+2, _transport_id, Reticulum::DESTINATION_LENGTH); - //memcpy(header+Reticulum::DESTINATION_LENGTH+2, _destination->_hash.data(), Reticulum::DESTINATION_LENGTH); - debug("Packet::pack: destination hash: " + _object->_destination.hash().toHex() ); - memcpy(_header+Reticulum::DESTINATION_LENGTH+2, _object->_destination.hash().data(), _object->_destination.hash().size()); - _header[2*Reticulum::DESTINATION_LENGTH+2] = _context; - // prepend header to _data in _raw bytes - memcpy(_data-Reticulum::HEADER_MAXSIZE, _header, Reticulum::HEADER_MAXSIZE); - - if (_packet_type == ANNOUNCE) { - // Announce packets are not encrypted - //ciphertext = _data; - } - } - } - - if (_data_len > _mtu) { - throw std::length_error("Packet size of " + std::to_string(_data_len) + " exceeds MTU of " + std::to_string(_mtu) +" bytes"); - } - - _packed = true; - update_hash(); - - extreme("Packet::pack: post hops: " + std::to_string(_hops)); -} -*/ -void Packet::pack() { - assert(_object); - debug("Packet::pack: packing packet..."); - extreme("Packet::pack: pre hops: " + std::to_string(_hops)); _destination_hash = _object->_destination.hash(); _raw.clear(); + _encrypted = false; _raw << _flags; _raw << _hops; @@ -422,6 +287,7 @@ void Packet::pack() { // In all other cases, we encrypt the packet // with the destination's encryption method _raw << _object->_destination.encrypt(_data); + _encrypted = true; } } else if (_header_type == HEADER_2) { @@ -447,61 +313,11 @@ void Packet::pack() { _packed = true; update_hash(); - - extreme("Packet::pack: post hops: " + std::to_string(_hops)); } -/* bool Packet::unpack() { assert(_object); debug("Packet::unpack: unpacking packet..."); - extreme("Packet::unpack: pre hops: " + std::to_string(_hops)); - try { - - // read header - _flags = _raw[0]; - _hops = _raw[1]; - - _header_type = static_cast((_flags & 0b01000000) >> 6); - _transport_type = static_cast((_flags & 0b00110000) >> 4); - _destination_type = static_cast((_flags & 0b00001100) >> 2); - _packet_type = static_cast(_flags & 0b00000011); - - // CBA TODO detect invalid flags and throw error - if (false) { - log("Received malformed packet, dropping it."); - return false; - } - - if (_header_type == HEADER_2) { - memcpy(_transport_id, _raw+2, Reticulum::DESTINATION_LENGTH); - memcpy(_destination_hash, _raw+Reticulum::DESTINATION_LENGTH+2, Reticulum::DESTINATION_LENGTH); - _context = static_cast(_raw[2*Reticulum::DESTINATION_LENGTH+2]); - _data = _raw+2*Reticulum::DESTINATION_LENGTH+3; - } - else { - //memcpy(_transport_id, EMPTY_DESTINATION, Reticulum::DESTINATION_LENGTH); - memcpy(_destination_hash, _raw+2, Reticulum::DESTINATION_LENGTH); - _context = static_cast(_raw[Reticulum::DESTINATION_LENGTH+2]); - _data = _raw+Reticulum::DESTINATION_LENGTH+3; - } - - _packed = false; - update_hash(); - } - catch (std::exception& e) { - error(std::string("Received malformed packet, dropping it. The contained exception was: ") + e.what()); - return false; - } - - extreme("Packet::unpack: post hops: " + std::to_string(_hops)); - return true; -} -*/ -bool Packet::unpack() { - assert(_object); - debug("Packet::unpack: unpacking packet..."); - extreme("Packet::unpack: pre hops: " + std::to_string(_hops)); try { if (_raw.size() < Reticulum::HEADER_MINSIZE) { throw std::length_error("Packet size of " + std::to_string(_raw.size()) + " does not meet minimum header size of " + std::to_string(Reticulum::HEADER_MINSIZE) +" bytes"); @@ -529,12 +345,16 @@ bool Packet::unpack() { _destination_hash.assign(raw+Reticulum::DESTINATION_LENGTH+2, Reticulum::DESTINATION_LENGTH); _context = static_cast(raw[2*Reticulum::DESTINATION_LENGTH+2]); _data.assign(raw+2*Reticulum::DESTINATION_LENGTH+3, _raw.size()-(2*Reticulum::DESTINATION_LENGTH+3)); + // uknown at this point whether data is encrypted or not + _encrypted = true; } else { _transport_id.clear(); _destination_hash.assign(raw+2, Reticulum::DESTINATION_LENGTH); _context = static_cast(raw[Reticulum::DESTINATION_LENGTH+2]); _data.assign(raw+Reticulum::DESTINATION_LENGTH+3, _raw.size()-(Reticulum::DESTINATION_LENGTH+3)); + // uknown at this point whether data is encrypted or not + _encrypted = true; } _packed = false; @@ -545,7 +365,6 @@ bool Packet::unpack() { return false; } - extreme("Packet::unpack: post hops: " + std::to_string(_hops)); return true; } @@ -674,11 +493,11 @@ std::string Packet::toString() { dump += "transport: " + _transport_id.toHex() + "\n"; dump += "destination: " + _destination_hash.toHex() + "\n"; dump += "context_type: " + std::to_string(_header_type) + "\n"; - dump += "plaintext: " + _data.toHex() + "\n"; + dump += "data: " + _data.toHex() + "\n"; dump += " length: " + std::to_string(_data.size()) + "\n"; dump += "raw: " + _raw.toHex() + "\n"; dump += " length: " + std::to_string(_raw.size()) + "\n"; - if (_raw.size() > 0) { + if (_encrypted && _raw.size() > 0) { size_t header_len = Reticulum::HEADER_MINSIZE; if (_header_type == HEADER_2) { header_len = Reticulum::HEADER_MAXSIZE; diff --git a/src/Packet.h b/src/Packet.h index cbd2778..7d795a6 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -145,7 +145,8 @@ namespace RNS { bool _sent = false; bool _create_receipt = false; bool _fromPacked = false; - bool _truncated = false; + bool _truncated = false; // whether data was truncated + bool _encrypted = false; // whether data is encrytpted //z_receipt = nullptr; uint16_t _mtu = Reticulum::MTU; @@ -154,27 +155,12 @@ namespace RNS { float _rssi = 0.0; float _snr = 0.0; - //uint8_t _packet_hash[Reticulum::HASHLENGTH] = {0}; Bytes _packet_hash; -/* - uint8_t _destination_hash[Reticulum::DESTINATION_LENGTH] = {0}; - uint8_t _transport_id[Reticulum::DESTINATION_LENGTH] = {0}; - - // universal packet buffer - uint8_t _raw[Reticulum::MTU]; - // header pointer into universal packet buffer - uint8_t *_header = _raw; - uint16_t _header_len = 0; - // data pointer into universal packet buffer - uint8_t *_data = _raw + Reticulum::HEADER_MAXSIZE; - uint16_t _data_len = 0; - uint8_t _raw[Reticulum::MTU]; -*/ Bytes _destination_hash; Bytes _transport_id; - Bytes _raw; // header + ciphertext - Bytes _data; // plaintext + Bytes _raw; // header + ( plaintext | ciphertext-token ) + Bytes _data; // plaintext | ciphertext private: class Object { diff --git a/src/main.cpp b/src/main.cpp index 0fa7125..9593f3b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,14 +68,16 @@ void setup() { // test path //destination.announce(RNS::bytesFromString(fruits[rand() % 7]), true, nullptr, RNS::bytesFromString("test_tag")); // test packet send - RNS::Packet send_packet = destination.announce(RNS::bytesFromString(fruits[rand() % 7])); - //RNS::Packet packet = destination.announce(RNS::bytesFromString(fruits[rand() % 7]), false, nullptr, RNS::Bytes::NONE, false); + destination.announce(RNS::bytesFromString(fruits[rand() % 7])); // 23.9% (+0.8%) + RNS::Packet send_packet(destination, "The quick brown fox jumps over the lazy dog"); + send_packet.pack(); RNS::extreme("Test send_packet packet: " + send_packet.toString()); // test packet receive RNS::Packet recv_packet(RNS::Destination::NONE, send_packet._raw); + recv_packet.unpack(); RNS::extreme("Test recv_packet packet: " + recv_packet.toString()); destination.receive(recv_packet);