diff --git a/src/Bytes.cpp b/src/Bytes.cpp index 3725a45..317946d 100644 --- a/src/Bytes.cpp +++ b/src/Bytes.cpp @@ -86,7 +86,7 @@ std::string RNS::hexFromByte(uint8_t byte, bool upper /*= true*/) { return hex; } -std::string Bytes::toHex(bool upper /*= true*/) const { +std::string Bytes::toHex(bool upper /*= false*/) const { if (!_data) { return ""; } diff --git a/src/Bytes.h b/src/Bytes.h index 8408c05..030a351 100644 --- a/src/Bytes.h +++ b/src/Bytes.h @@ -202,7 +202,7 @@ namespace RNS { inline const uint8_t* data() const { if (!_data) return nullptr; return _data->data(); } inline std::string toString() const { if (!_data) return ""; return {(const char*)data(), size()}; } - std::string toHex(bool upper = true) const; + std::string toHex(bool upper = false) const; Bytes mid(size_t beginpos, size_t len) const; Bytes mid(size_t beginpos) const; inline Bytes left(size_t len) const { if (!_data) return NONE; if (len > size()) len = size(); return {data(), len}; } diff --git a/src/Destination.cpp b/src/Destination.cpp index c8b944f..3cffb7f 100644 --- a/src/Destination.cpp +++ b/src/Destination.cpp @@ -21,7 +21,7 @@ Destination::Destination(const Identity& identity, const directions direction, c if (strchr(app_name, '.') != nullptr) { throw std::invalid_argument("Dots can't be used in app names"); } - extreme("Destination::Destination: app name: " + std::string(app_name)); + //extreme("Destination::Destination: app name: " + std::string(app_name)); _object->_type = type; _object->_direction = direction; @@ -33,26 +33,26 @@ Destination::Destination(const Identity& identity, const directions direction, c // CBA TODO determine why identity.hexhash is added both here and by expand_name called below fullaspects += "." + _object->_identity.hexhash(); } - extreme("Destination::Destination: full aspects: " + fullaspects); + //extreme("Destination::Destination: full aspects: " + fullaspects); if (_object->_identity && _object->_type == PLAIN) { throw std::invalid_argument("Selected destination type PLAIN cannot hold an identity"); } _object->_name = expand_name(_object->_identity, app_name, fullaspects.c_str()); - extreme("Destination::Destination: name: " + _object->_name); + //extreme("Destination::Destination: name: " + _object->_name); // Generate the destination address hash - extreme("Destination::Destination: creating hash..."); + //extreme("Destination::Destination: creating hash..."); _object->_hash = hash(_object->_identity, app_name, fullaspects.c_str()); _object->_hexhash = _object->_hash.toHex(); - extreme("Destination::Destination: hash: " + _object->_hash.toHex()); - extreme("Destination::Destination: creating name hash..."); + extreme("Destination::Destination: hash: " + _object->_hash.toHex()); + //extreme("Destination::Destination: creating name hash..."); //p self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)] _object->_name_hash = name_hash(app_name, aspects); - extreme("Destination::Destination: name hash: " + _object->_name_hash.toHex()); + //extreme("Destination::Destination: name hash: " + _object->_name_hash.toHex()); - extreme("Destination::Destination: calling register_destination"); + //extreme("Destination::Destination: calling register_destination"); Transport::register_destination(*this); mem("Destination object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); @@ -71,13 +71,13 @@ Destination::Destination(const Identity& identity, const Type::Destination::dire _object->_hash = hash; _object->_hexhash = _object->_hash.toHex(); - extreme("Destination::Destination: hash: " + _object->_hash.toHex()); - extreme("Destination::Destination: creating name hash..."); + extreme("Destination::Destination: hash: " + _object->_hash.toHex()); + //extreme("Destination::Destination: creating name hash..."); //p self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)] _object->_name_hash = name_hash("unknown", "unknown"); - extreme("Destination::Destination: name hash: " + _object->_name_hash.toHex()); + //extreme("Destination::Destination: name hash: " + _object->_name_hash.toHex()); - extreme("Destination::Destination: calling register_destination"); + //extreme("Destination::Destination: calling register_destination"); Transport::register_destination(*this); mem("Destination object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); @@ -123,21 +123,33 @@ Destination::Destination(const Identity& identity, const Type::Destination::dire /* :returns: A tuple containing the app name and a list of aspects, for a full-name string. */ -/*static*/ Bytes Destination::app_and_aspects_from_name(const char* full_name) { - //z components = full_name.split(".") - //z return (components[0], components[1:]) - // MOCK - return {Bytes::NONE}; +/*static*/ std::vector Destination::app_and_aspects_from_name(const char* full_name) { + //p components = full_name.split(".") + //p return (components[0], components[1:]) + std::vector components; + std::string name(full_name); + std::size_t pos = name.find('.'); + components.push_back(name.substr(0, pos)); + if (pos != std::string::npos) { + components.push_back(name.substr(pos+1)); + } + return components; } /* :returns: A destination name in adressable hash form, for a full name string and Identity instance. */ /*static*/ Bytes Destination::hash_from_name_and_identity(const char* full_name, const Identity& identity) { - //z app_name, aspects = Destination.app_and_aspects_from_name(full_name) - //z return Destination.hash(identity, app_name, *aspects) - // MOCK - return {Bytes::NONE}; + //p app_name, aspects = Destination.app_and_aspects_from_name(full_name) + //p return Destination.hash(identity, app_name, *aspects) + std::vector components = app_and_aspects_from_name(full_name); + if (components.size() == 0) { + return {Bytes::NONE}; + } + if (components.size() == 1) { + return hash(identity, components[0].c_str(), ""); + } + return hash(identity, components[0].c_str(), components[1].c_str()); } /* @@ -250,20 +262,20 @@ Packet Destination::announce(const Bytes& app_data, bool path_response, const In } Bytes signed_data; - extreme("Destination::announce: hash: " + _object->_hash.toHex()); - extreme("Destination::announce: public key: " + _object->_identity.get_public_key().toHex()); - extreme("Destination::announce: name hash: " + _object->_name_hash.toHex()); - extreme("Destination::announce: random hash: " + random_hash.toHex()); - extreme("Destination::announce: app data: " + new_app_data.toHex()); - extreme("Destination::announce: app data text:" + new_app_data.toString()); + //extreme("Destination::announce: hash: " + _object->_hash.toHex()); + //extreme("Destination::announce: public key: " + _object->_identity.get_public_key().toHex()); + //extreme("Destination::announce: name hash: " + _object->_name_hash.toHex()); + //extreme("Destination::announce: random hash: " + random_hash.toHex()); + //extreme("Destination::announce: app data: " + new_app_data.toHex()); + //extreme("Destination::announce: app data text:" + new_app_data.toString()); signed_data << _object->_hash << _object->_identity.get_public_key() << _object->_name_hash << random_hash; if (new_app_data) { signed_data << new_app_data; } - extreme("Destination::announce: signed data: " + signed_data.toHex()); + //extreme("Destination::announce: signed data: " + signed_data.toHex()); Bytes signature(_object->_identity.sign(signed_data)); - extreme("Destination::announce: signature: " + signature.toHex()); + //extreme("Destination::announce: signature: " + signature.toHex()); announce_data << _object->_identity.get_public_key() << _object->_name_hash << random_hash << signature; @@ -273,14 +285,14 @@ Packet Destination::announce(const Bytes& app_data, bool path_response, const In _object->_path_responses.insert({tag, {OS::time(), announce_data}}); } - extreme("Destination::announce: announce_data:" + announce_data.toHex()); + //extreme("Destination::announce: announce_data:" + announce_data.toHex()); Type::Packet::context_types announce_context = Type::Packet::CONTEXT_NONE; if (path_response) { announce_context = Type::Packet::PATH_RESPONSE; } - extreme("Destination::announce: creating announce packet..."); + //extreme("Destination::announce: creating announce packet..."); //p announce_packet = RNS.Packet(self, announce_data, RNS.Packet.ANNOUNCE, context = announce_context, attached_interface = attached_interface) //Packet announce_packet(*this, announce_data, Type::Packet::ANNOUNCE, announce_context, Type::Transport::BROADCAST, Type::Packet::HEADER_1, nullptr, attached_interface); Packet announce_packet(*this, attached_interface, announce_data, Type::Packet::ANNOUNCE, announce_context, Type::Transport::BROADCAST, Type::Packet::HEADER_1); @@ -350,7 +362,7 @@ void Destination::receive(const Packet& packet) { else { // CBA TODO Why isn't the Packet decrypting itself? Bytes plaintext(decrypt(packet.data())); - extreme("Destination::receive: decrypted data: " + plaintext.toHex()); + //extreme("Destination::receive: decrypted data: " + plaintext.toHex()); if (plaintext) { if (packet.packet_type() == Type::Packet::DATA) { if (_object->_callbacks._packet) { diff --git a/src/Destination.h b/src/Destination.h index f0cf778..b5ae5f8 100644 --- a/src/Destination.h +++ b/src/Destination.h @@ -90,7 +90,7 @@ namespace RNS { static std::string expand_name(const Identity& identity, const char* app_name, const char* aspects); static Bytes hash(const Identity& identity, const char* app_name, const char* aspects); static Bytes name_hash(const char* app_name, const char* aspects); - static Bytes app_and_aspects_from_name(const char* full_name); + static std::vector app_and_aspects_from_name(const char* full_name); static Bytes hash_from_name_and_identity(const char* full_name, const Identity& identity); public: diff --git a/src/Identity.cpp b/src/Identity.cpp index 3a85350..c89ab00 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -34,22 +34,22 @@ void Identity::createKeys() { // CRYPTO: create encryption private keys _object->_prv = Cryptography::X25519PrivateKey::generate(); _object->_prv_bytes = _object->_prv->private_bytes(); - debug("Identity::createKeys: prv bytes: " + _object->_prv_bytes.toHex()); + //extreme("Identity::createKeys: prv bytes: " + _object->_prv_bytes.toHex()); // CRYPTO: create signature private keys _object->_sig_prv = Cryptography::Ed25519PrivateKey::generate(); _object->_sig_prv_bytes = _object->_sig_prv->private_bytes(); - debug("Identity::createKeys: sig prv bytes: " + _object->_sig_prv_bytes.toHex()); + //extreme("Identity::createKeys: sig prv bytes: " + _object->_sig_prv_bytes.toHex()); // CRYPTO: create encryption public keys _object->_pub = _object->_prv->public_key(); _object->_pub_bytes = _object->_pub->public_bytes(); - debug("Identity::createKeys: pub bytes: " + _object->_pub_bytes.toHex()); + //extreme("Identity::createKeys: pub bytes: " + _object->_pub_bytes.toHex()); // CRYPTO: create signature public keys _object->_sig_pub = _object->_sig_prv->public_key(); _object->_sig_pub_bytes = _object->_sig_pub->public_bytes(); - debug("Identity::createKeys: sig pub bytes: " + _object->_sig_pub_bytes.toHex()); + //extreme("Identity::createKeys: sig pub bytes: " + _object->_sig_pub_bytes.toHex()); update_hashes(); @@ -70,20 +70,20 @@ bool Identity::load_private_key(const Bytes& prv_bytes) { //p self.prv_bytes = prv_bytes[:Identity.KEYSIZE//8//2] _object->_prv_bytes = prv_bytes.left(Type::Identity::KEYSIZE/8/2); _object->_prv = X25519PrivateKey::from_private_bytes(_object->_prv_bytes); - debug("Identity::load_private_key: prv bytes: " + _object->_prv_bytes.toHex()); + //extreme("Identity::load_private_key: prv bytes: " + _object->_prv_bytes.toHex()); //p self.sig_prv_bytes = prv_bytes[Identity.KEYSIZE//8//2:] _object->_sig_prv_bytes = prv_bytes.mid(Type::Identity::KEYSIZE/8/2); _object->_sig_prv = Ed25519PrivateKey::from_private_bytes(_object->_sig_prv_bytes); - debug("Identity::load_private_key: sig prv bytes: " + _object->_sig_prv_bytes.toHex()); + //extreme("Identity::load_private_key: sig prv bytes: " + _object->_sig_prv_bytes.toHex()); _object->_pub = _object->_prv->public_key(); _object->_pub_bytes = _object->_pub->public_bytes(); - debug("Identity::load_private_key: pub bytes: " + _object->_pub_bytes.toHex()); + //extreme("Identity::load_private_key: pub bytes: " + _object->_pub_bytes.toHex()); _object->_sig_pub = _object->_sig_prv->public_key(); _object->_sig_pub_bytes = _object->_sig_pub->public_bytes(); - debug("Identity::load_private_key: sig pub bytes: " + _object->_sig_pub_bytes.toHex()); + //extreme("Identity::load_private_key: sig pub bytes: " + _object->_sig_pub_bytes.toHex()); update_hashes(); @@ -110,11 +110,11 @@ void Identity::load_public_key(const Bytes& pub_bytes) { //_pub_bytes = pub_bytes[:Identity.KEYSIZE//8//2] _object->_pub_bytes = pub_bytes.left(Type::Identity::KEYSIZE/8/2); - debug("Identity::load_public_key: pub bytes: " + _object->_pub_bytes.toHex()); + //extreme("Identity::load_public_key: pub bytes: " + _object->_pub_bytes.toHex()); //_sig_pub_bytes = pub_bytes[Identity.KEYSIZE//8//2:] _object->_sig_pub_bytes = pub_bytes.mid(Type::Identity::KEYSIZE/8/2); - debug("Identity::load_public_key: sig pub bytes: " + _object->_sig_pub_bytes.toHex()); + //extreme("Identity::load_public_key: sig pub bytes: " + _object->_sig_pub_bytes.toHex()); _object->_pub = X25519PublicKey::from_public_bytes(_object->_pub_bytes); _object->_sig_pub = Ed25519PublicKey::from_public_bytes(_object->_sig_pub_bytes); @@ -344,25 +344,25 @@ Recall last heard app_data for a destination hash. try { if (packet.packet_type() == Type::Packet::ANNOUNCE) { Bytes destination_hash = packet.destination_hash(); - extreme("Identity::validate_announce: destination_hash: " + packet.destination_hash().toHex()); + //extreme("Identity::validate_announce: destination_hash: " + packet.destination_hash().toHex()); Bytes public_key = packet.data().left(KEYSIZE/8); - extreme("Identity::validate_announce: public_key: " + public_key.toHex()); + //extreme("Identity::validate_announce: public_key: " + public_key.toHex()); Bytes name_hash = packet.data().mid(KEYSIZE/8, NAME_HASH_LENGTH/8); - extreme("Identity::validate_announce: name_hash: " + name_hash.toHex()); + //extreme("Identity::validate_announce: name_hash: " + name_hash.toHex()); Bytes random_hash = packet.data().mid(KEYSIZE/8 + NAME_HASH_LENGTH/8, RANDOM_HASH_LENGTH/8); - extreme("Identity::validate_announce: random_hash: " + random_hash.toHex()); + //extreme("Identity::validate_announce: random_hash: " + random_hash.toHex()); Bytes signature = packet.data().mid(KEYSIZE/8 + NAME_HASH_LENGTH/8 + RANDOM_HASH_LENGTH/8, SIGLENGTH/8); - extreme("Identity::validate_announce: signature: " + signature.toHex()); + //extreme("Identity::validate_announce: signature: " + signature.toHex()); Bytes app_data; if (packet.data().size() > (KEYSIZE/8 + NAME_HASH_LENGTH/8 + RANDOM_HASH_LENGTH/8 + SIGLENGTH/8)) { app_data = packet.data().mid(KEYSIZE/8 + NAME_HASH_LENGTH/8 + RANDOM_HASH_LENGTH/8 + SIGLENGTH/8); } - extreme("Identity::validate_announce: app_data: " + app_data.toHex()); - extreme("Identity::validate_announce: app_data text: " + app_data.toString()); + //extreme("Identity::validate_announce: app_data: " + app_data.toHex()); + //extreme("Identity::validate_announce: app_data text: " + app_data.toString()); Bytes signed_data; signed_data << packet.destination_hash() << public_key << name_hash << random_hash+app_data; - extreme("Identity::validate_announce: signed_data: " + signed_data.toHex()); + //extreme("Identity::validate_announce: signed_data: " + signed_data.toHex()); if (packet.data().size() <= KEYSIZE/8 + NAME_HASH_LENGTH/8 + RANDOM_HASH_LENGTH/8 + SIGLENGTH/8) { app_data.clear(); @@ -374,8 +374,8 @@ Recall last heard app_data for a destination hash. if (announced_identity.pub() && announced_identity.validate(signature, signed_data)) { Bytes hash_material = name_hash << announced_identity.hash(); Bytes expected_hash = full_hash(hash_material).left(Type::Reticulum::TRUNCATED_HASHLENGTH/8); - extreme("Identity::validate_announce: destination_hash: " + packet.destination_hash().toHex()); - extreme("Identity::validate_announce: expected_hash: " + expected_hash.toHex()); + //extreme("Identity::validate_announce: destination_hash: " + packet.destination_hash().toHex()); + //extreme("Identity::validate_announce: expected_hash: " + expected_hash.toHex()); if (packet.destination_hash() == expected_hash) { // Check if we already have a public key for this destination @@ -448,18 +448,18 @@ Encrypts information for the identity. */ const Bytes Identity::encrypt(const Bytes& plaintext) const { assert(_object); - debug("Identity::encrypt: encrypting data..."); + extreme("Identity::encrypt: encrypting data..."); if (!_object->_pub) { throw std::runtime_error("Encryption failed because identity does not hold a public key"); } Cryptography::X25519PrivateKey::Ptr ephemeral_key = Cryptography::X25519PrivateKey::generate(); Bytes ephemeral_pub_bytes = ephemeral_key->public_key()->public_bytes(); - debug("Identity::encrypt: ephemeral public key: " + ephemeral_pub_bytes.toHex()); + extreme("Identity::encrypt: ephemeral public key: " + ephemeral_pub_bytes.toHex()); // CRYPTO: create shared key for key exchange using own public key //shared_key = ephemeral_key.exchange(self.pub) Bytes shared_key = ephemeral_key->exchange(_object->_pub_bytes); - debug("Identity::encrypt: shared key: " + shared_key.toHex()); + extreme("Identity::encrypt: shared key: " + shared_key.toHex()); Bytes derived_key = Cryptography::hkdf( 32, @@ -467,10 +467,10 @@ const Bytes Identity::encrypt(const Bytes& plaintext) const { get_salt(), get_context() ); - debug("Identity::encrypt: derived key: " + derived_key.toHex()); + extreme("Identity::encrypt: derived key: " + derived_key.toHex()); Cryptography::Fernet fernet(derived_key); - debug("Identity::encrypt: Fernet encrypting data of length " + std::to_string(plaintext.size())); + extreme("Identity::encrypt: Fernet encrypting data of length " + std::to_string(plaintext.size())); extreme("Identity::encrypt: plaintext: " + plaintext.toHex()); Bytes ciphertext = fernet.encrypt(plaintext); extreme("Identity::encrypt: ciphertext: " + ciphertext.toHex()); @@ -488,7 +488,7 @@ Decrypts information for the identity. */ const Bytes Identity::decrypt(const Bytes& ciphertext_token) const { assert(_object); - debug("Identity::decrypt: decrypting data..."); + extreme("Identity::decrypt: decrypting data..."); if (!_object->_prv) { throw std::runtime_error("Decryption failed because identity does not hold a private key"); } @@ -502,12 +502,12 @@ const Bytes Identity::decrypt(const Bytes& ciphertext_token) const { Bytes peer_pub_bytes = ciphertext_token.left(Type::Identity::KEYSIZE/8/2); //peer_pub = X25519PublicKey.from_public_bytes(peer_pub_bytes) //Cryptography::X25519PublicKey::Ptr peer_pub = Cryptography::X25519PublicKey::from_public_bytes(peer_pub_bytes); - debug("Identity::decrypt: peer public key: " + peer_pub_bytes.toHex()); + extreme("Identity::decrypt: peer public key: " + peer_pub_bytes.toHex()); // CRYPTO: create shared key for key exchange using peer public key //shared_key = _object->_prv->exchange(peer_pub); Bytes shared_key = _object->_prv->exchange(peer_pub_bytes); - debug("Identity::decrypt: shared key: " + shared_key.toHex()); + extreme("Identity::decrypt: shared key: " + shared_key.toHex()); Bytes derived_key = Cryptography::hkdf( 32, @@ -515,16 +515,16 @@ const Bytes Identity::decrypt(const Bytes& ciphertext_token) const { get_salt(), get_context() ); - debug("Identity::decrypt: derived key: " + derived_key.toHex()); + extreme("Identity::decrypt: derived key: " + derived_key.toHex()); Cryptography::Fernet fernet(derived_key); //ciphertext = ciphertext_token[Identity.KEYSIZE//8//2:] Bytes ciphertext(ciphertext_token.mid(Type::Identity::KEYSIZE/8/2)); - debug("Identity::decrypt: Fernet decrypting data of length " + std::to_string(ciphertext.size())); + extreme("Identity::decrypt: Fernet decrypting data of length " + std::to_string(ciphertext.size())); extreme("Identity::decrypt: ciphertext: " + ciphertext.toHex()); plaintext = fernet.decrypt(ciphertext); extreme("Identity::decrypt: plaintext: " + plaintext.toHex()); - debug("Identity::decrypt: Fernet decrypted data of length " + std::to_string(plaintext.size())); + //extreme("Identity::decrypt: Fernet decrypted data of length " + std::to_string(plaintext.size())); } catch (std::exception& e) { debug("Decryption by " + toString() + " failed: " + e.what()); diff --git a/src/Identity.h b/src/Identity.h index 41d1798..3f9444a 100644 --- a/src/Identity.h +++ b/src/Identity.h @@ -86,7 +86,7 @@ namespace RNS { inline void update_hashes() { assert(_object); _object->_hash = truncated_hash(get_public_key()); - debug("Identity::update_hashes: hash: " + _object->_hash.toHex()); + extreme("Identity::update_hashes: hash: " + _object->_hash.toHex()); _object->_hexhash = _object->_hash.toHex(); }; bool load(const char* path); diff --git a/src/Packet.cpp b/src/Packet.cpp index dcfc6d3..04c809b 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -58,6 +58,9 @@ uint8_t Packet::get_packed_flags() { packed_flags = (_object->_header_type << 6) | (_object->_transport_type << 4) | (Type::Destination::LINK << 2) | _object->_packet_type; } else { + if (!_object->_destination) { + throw std::logic_error("Packet destination is required"); + } packed_flags = (_object->_header_type << 6) | (_object->_transport_type << 4) | (_object->_destination.type() << 2) | _object->_packet_type; } return packed_flags; @@ -240,6 +243,9 @@ void Packet::pack() { assert(_object); extreme("Packet::pack: packing packet..."); + if (!_object->_destination) { + throw std::logic_error("Packet destination is required"); + } _object->_destination_hash = _object->_destination.hash(); _object->_raw.clear(); @@ -311,6 +317,9 @@ void Packet::pack() { // Announce packets are not encrypted _object->_raw << _object->_data; } + // CBA No default encryption here like with header type HEADER_1 ??? + // CBA Is there any packet type besides ANNOUNCE with header type HEADER_2 ??? + // CBA Safe to assume that all HEADER_2 type packets are in transport and therefore can not and will not be decrypted locally ??? } } @@ -353,7 +362,7 @@ bool Packet::unpack() { _object->_context = static_cast(raw[2*Type::Reticulum::DESTINATION_LENGTH+2]); _object->_data.assign(raw+2*Type::Reticulum::DESTINATION_LENGTH+3, _object->_raw.size()-(2*Type::Reticulum::DESTINATION_LENGTH+3)); // uknown at this point whether data is encrypted or not - _object->_encrypted = true; + _object->_encrypted = false; } else { _object->_transport_id.clear(); @@ -361,7 +370,7 @@ bool Packet::unpack() { _object->_context = static_cast(raw[Type::Reticulum::DESTINATION_LENGTH+2]); _object->_data.assign(raw+Type::Reticulum::DESTINATION_LENGTH+3, _object->_raw.size()-(Type::Reticulum::DESTINATION_LENGTH+3)); // uknown at this point whether data is encrypted or not - _object->_encrypted = true; + _object->_encrypted = false; } _object->_packed = false; @@ -450,6 +459,9 @@ bool Packet::resend() { void Packet::prove(const Destination& destination /*= {Type::NONE}*/) { assert(_object); extreme("Packet::prove: proving packet..."); + if (!_object->_destination) { + throw std::logic_error("Packet destination is required"); + } if (_object->_fromPacked && _object->_destination) { if (_object->_destination.identity() && _object->_destination.identity().prv()) { _object->_destination.identity().prove(*this, destination); @@ -527,7 +539,8 @@ std::string Packet::debugString() const { if (_object->_packed) { //unpack(); } - std::string str = "ht=" + std::to_string(_object->_header_type); + std::string str = "ph=" + _object->_packet_hash.toHex(); + str += " ht=" + std::to_string(_object->_header_type); str += " tt=" + std::to_string(_object->_transport_type); str += " dt=" + std::to_string(_object->_destination_type); str += " pt=" + std::to_string(_object->_packet_type); @@ -540,25 +553,175 @@ std::string Packet::dumpString() const { if (!_object) { return ""; } - if (_object->_packed) { - //unpack(); - } + //if (_object->_packed) { + // unpack(); + //} + bool encrypted = true; std::string dump; - dump = "\n--------------------\n"; + dump = "\n------------------------------------------------------------------------------\n"; + dump += "hash: " + _object->_packet_hash.toHex() + "\n"; dump += "flags: " + hexFromByte(_object->_flags) + "\n"; - dump += " header_type: " + std::to_string(_object->_header_type) + "\n"; - dump += " transport_type: " + std::to_string(_object->_transport_type) + "\n"; - dump += " destination_type: " + std::to_string(_object->_destination_type) + "\n"; - dump += " packet_type: " + std::to_string(_object->_packet_type) + "\n"; + //dump += " header_type: " + std::to_string(_object->_header_type) + "\n"; + dump += " header_type: "; + switch (_object->_header_type) { + case HEADER_1: + dump += "HEADER_1\n"; + break; + case HEADER_2: + encrypted = false; + dump += "HEADER_2\n"; + break; + default: + std::to_string(_object->_header_type) + "\n"; + } + //dump += " transport_type: " + std::to_string(_object->_transport_type) + "\n"; + dump += " transport_type: "; + switch (_object->_transport_type) { + case Type::Transport::BROADCAST: + dump += "BROADCAST\n"; + break; + case Type::Transport::TRANSPORT: + dump += "TRANSPORT\n"; + break; + case Type::Transport::RELAY: + dump += "RELAY\n"; + break; + case Type::Transport::TUNNEL: + dump += "TUNNEL\n"; + break; + case Type::Transport::NONE: + dump += "NONE\n"; + break; + default: + std::to_string(_object->_transport_type) + "\n"; + } + //dump += " destination_type: " + std::to_string(_object->_destination_type) + "\n"; + dump += " destination_type: "; + switch (_object->_destination_type) { + case Type::Destination::SINGLE: + dump += "SINGLE\n"; + break; + case Type::Destination::GROUP: + dump += "GROUP\n"; + break; + case Type::Destination::PLAIN: + dump += "PLAIN\n"; + break; + case Type::Destination::LINK: + dump += "LINK\n"; + break; + default: + std::to_string(_object->_destination_type) + "\n"; + } + //dump += " packet_type: " + std::to_string(_object->_packet_type) + "\n"; + dump += " packet_type: "; + switch (_object->_packet_type) { + case DATA: + dump += "DATA\n"; + break; + case ANNOUNCE: + encrypted = false; + dump += "ANNOUNCE\n"; + break; + case LINKREQUEST: + encrypted = false; + dump += "LINKREQUEST\n"; + break; + case PROOF: + dump += "PROOF\n"; + if (_object->_context == RESOURCE_PRF) { + encrypted = false; + } + if (_object->_destination && _object->_destination.type() == Type::Destination::LINK) { + encrypted = false; + } + break; + default: + std::to_string(_object->_packet_type) + "\n"; + } dump += "hops: " + std::to_string(_object->_hops) + "\n"; dump += "transport: " + _object->_transport_id.toHex() + "\n"; dump += "destination: " + _object->_destination_hash.toHex() + "\n"; - dump += "context_type: " + std::to_string(_object->_header_type) + "\n"; + //dump += "context: " + std::to_string(_object->_context) + "\n"; + dump += "context: "; + switch (_object->_context) { + case CONTEXT_NONE: + dump += "CONTEXT_NONE\n"; + break; + case RESOURCE: + encrypted = false; + dump += "RESOURCE\n"; + break; + case RESOURCE_ADV: + dump += "RESOURCE_ADV\n"; + break; + case RESOURCE_REQ: + dump += "RESOURCE_REQ\n"; + break; + case RESOURCE_HMU: + dump += "RESOURCE_HMU\n"; + break; + case RESOURCE_PRF: + encrypted = false; + dump += "RESOURCE_PRF\n"; + break; + case RESOURCE_ICL: + dump += "RESOURCE_ICL\n"; + break; + case RESOURCE_RCL: + dump += "RESOURCE_RCL\n"; + break; + case CACHE_REQUEST: + encrypted = false; + dump += "CACHE_REQUEST\n"; + break; + case REQUEST: + dump += "REQUEST\n"; + break; + case RESPONSE: + dump += "RESPONSE\n"; + break; + case PATH_RESPONSE: + dump += "PATH_RESPONSE\n"; + break; + case COMMAND: + dump += "COMMAND\n"; + break; + case COMMAND_STATUS: + dump += "COMMAND_STATUS\n"; + break; + case CHANNEL: + dump += "CHANNEL\n"; + break; + case KEEPALIVE: + encrypted = false; + dump += "KEEPALIVE\n"; + break; + case LINKIDENTIFY: + dump += "LINKIDENTIFY\n"; + break; + case LINKCLOSE: + dump += "LINKCLOSE\n"; + break; + case LINKPROOF: + dump += "LINKPROOF\n"; + break; + case LRRTT: + dump += "LRRTT\n"; + break; + case LRPROOF: + encrypted = false; + dump += "LRPROOF\n"; + break; + default: + std::to_string(_object->_context) + "\n"; + } dump += "raw: " + _object->_raw.toHex() + "\n"; dump += " length: " + std::to_string(_object->_raw.size()) + "\n"; dump += "data: " + _object->_data.toHex() + "\n"; dump += " length: " + std::to_string(_object->_data.size()) + "\n"; - if (_object->_encrypted && _object->_raw.size() > 0) { + //if ((encrypted || _object->_encrypted) && _object->_raw.size() > 0) { + if (false) { size_t header_len = Type::Reticulum::HEADER_MINSIZE; if (_object->_header_type == HEADER_2) { header_len = Type::Reticulum::HEADER_MAXSIZE; @@ -571,16 +734,21 @@ std::string Packet::dumpString() const { dump += " length: " + std::to_string(ciphertext.size()) + "\n"; dump += " iv: " + ciphertext.left(16).toHex() + "\n"; dump += " sig: " + ciphertext.right(32).toHex() + "\n"; - dump += " aes ciphertext: " + ciphertext.mid(16, ciphertext.size()-48).toHex() + "\n"; - dump += " length: " + std::to_string(ciphertext.size()-48) + "\n"; + if (ciphertext.size() >= 48) { + dump += " aes ciphertext: " + ciphertext.mid(16, ciphertext.size()-48).toHex() + "\n"; + dump += " length: " + std::to_string(ciphertext.size()-48) + "\n"; + } } - dump += "--------------------\n"; + dump += "------------------------------------------------------------------------------\n"; return dump; } #endif PacketReceipt::PacketReceipt(const Packet& packet) : _object(new Object()) { + if (!packet.destination()) { + throw std::invalid_argument("Packet with destination is required"); + } _object->_hash = packet.get_hash(); _object->_truncated_hash = packet.getTruncatedHash(); _object->_destination = packet.destination(); diff --git a/src/Transport.cpp b/src/Transport.cpp index 7399f61..85eab95 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -81,6 +81,9 @@ using namespace RNS::Utilities; /*static*/ Reticulum Transport::_owner({Type::NONE}); /*static*/ Identity Transport::_identity({Type::NONE}); +// CBA +/*static*/ Transport::Callbacks Transport::_callbacks; + /*static*/ void Transport::start(const Reticulum& reticulum_instance) { info("Transport starting..."); _jobs_running = true; @@ -126,6 +129,7 @@ using namespace RNS::Utilities; path_request_destination.set_packet_callback(path_request_handler); _control_destinations.insert(path_request_destination); _control_hashes.insert(path_request_destination.hash()); + debug("Created transport-specific path request destination " + path_request_destination.hash().toHex()); // Create transport-specific destination for tunnel synthesize Destination tunnel_synthesize_destination({Type::NONE}, Type::Destination::IN, Type::Destination::PLAIN, APP_NAME, "tunnel.synthesize"); @@ -134,6 +138,7 @@ using namespace RNS::Utilities; //p Transport.control_destinations.append(Transport.tunnel_synthesize_handler) _control_destinations.insert(tunnel_synthesize_destination); _control_hashes.insert(tunnel_synthesize_destination.hash()); + debug("Created transport-specific tunnel synthesize destination " + tunnel_synthesize_destination.hash().toHex()); _jobs_running = false; @@ -142,6 +147,7 @@ using namespace RNS::Utilities; //p thread.start() if (Reticulum::transport_enabled()) { + info("Transport mode is enabled"); std::string destination_table_path = Reticulum::_storagepath + "/destination_table"; if (!_owner.is_connected_to_shared_instance() && OS::file_exists(destination_table_path.c_str())) { @@ -205,7 +211,7 @@ using namespace RNS::Utilities; } } else { - RNS::extreme("Transport::start: read failed"); + RNS::extreme("Transport::start: destination table read failed"); } verbose("Loaded " + std::to_string(_destination_table.size()) + " path table entries from storage"); @@ -272,6 +278,7 @@ using namespace RNS::Utilities; Destination probe_destination(_identity, Type::Destination::IN, Type::Destination::SINGLE, APP_NAME, "probe"); probe_destination.accepts_links(false); probe_destination.set_proof_strategy(Type::Destination::PROVE_ALL); + debug("Created probe responder destination " + probe_destination.hash().toHex()); probe_destination.announce(); notice("Transport Instance will respond to probe requests on " + probe_destination.toString()); } @@ -396,6 +403,7 @@ using namespace RNS::Utilities; } else { if (OS::time() > announce_entry._retransmit_timeout) { + extreme("Performing announce processing for " + destination_hash.toHex() + "..."); announce_entry._retransmit_timeout = OS::time() + Type::Transport::PATHFINDER_G + Type::Transport::PATHFINDER_RW; announce_entry._retries += 1; //p packet = announce_entry[5] @@ -685,16 +693,28 @@ using namespace RNS::Utilities; _jobs_running = false; + // CBA send announce retransmission packets for (auto& packet : outgoing) { packet.send(); } + // CBA send link-related path requests for (auto& destination_hash : path_requests) { request_path(destination_hash); } } /*static*/ void Transport::transmit(Interface& interface, const Bytes& raw) { + extreme("Transport::transmit()"); + // CBA + if (_callbacks._transmit_packet) { + try { + _callbacks._transmit_packet(raw, interface); + } + catch (std::exception& e) { + debug("Error while executing transmit packet callback. The contained exception was: " + std::string(e.what())); + } + } try { //if hasattr(interface, "ifac_identity") and interface.ifac_identity != None: if (interface.ifac_identity()) { @@ -1195,11 +1215,13 @@ using namespace RNS::Utilities; } if (_packet_hashlist.find(packet.packet_hash()) == _packet_hashlist.end()) { + extreme("Transport::packet_filter: packet not previously seen"); return true; } else { if (packet.packet_type() == Type::Packet::ANNOUNCE) { if (packet.destination_type() == Type::Destination::SINGLE) { + extreme("Transport::packet_filter: packet previously seen but is SINGLE ANNOUNCE"); return true; } else { @@ -1209,12 +1231,21 @@ using namespace RNS::Utilities; } } - extreme("Filtered packet with hash " + packet.packet_hash().toHex()); + debug("Filtered packet with hash " + packet.packet_hash().toHex()); return false; } /*static*/ void Transport::inbound(const Bytes& raw, const Interface& interface /*= {Type::NONE}*/) { extreme("Transport::inbound()"); + // CBA + if (_callbacks._receive_packet) { + try { + _callbacks._receive_packet(raw, interface); + } + catch (std::exception& e) { + debug("Error while executing receive packet callback. The contained exception was: " + std::string(e.what())); + } + } // TODO /* // If interface access codes are enabled, @@ -1339,7 +1370,21 @@ using namespace RNS::Utilities; packet.hops(packet.hops() - 1); } - if (packet_filter(packet)) { + //if (packet_filter(packet)) { + // CBA + bool accept = true; + if (_callbacks._filter_packet) { + try { + accept = _callbacks._filter_packet(packet); + } + catch (std::exception& e) { + debug("Error while executing filter packet callback. The contained exception was: " + std::string(e.what())); + } + } + if (accept) { + accept = packet_filter(packet); + } + if (accept) { extreme("Transport::inbound: Packet accepted by filter"); _packet_hashlist.insert(packet.packet_hash()); cache(packet); @@ -1351,10 +1396,10 @@ using namespace RNS::Utilities; //p for_local_client = (packet.packet_type != RNS.Packet.ANNOUNCE) and (packet.destination_hash in Transport.destination_table and Transport.destination_table[packet.destination_hash][2] == 0) //p for_local_client_link = (packet.packet_type != RNS.Packet.ANNOUNCE) and (packet.destination_hash in Transport.link_table and Transport.link_table[packet.destination_hash][4] in Transport.local_client_interfaces) //p for_local_client_link |= (packet.packet_type != RNS.Packet.ANNOUNCE) and (packet.destination_hash in Transport.link_table and Transport.link_table[packet.destination_hash][2] in Transport.local_client_interfaces) + + // If packet is anything besides ANNOUNCE then determine if it's destinated for a local destination or link bool for_local_client = false; bool for_local_client_link = false; - - // If packet is anything besides ANNOUNCE then determine if it's destinated for a local destinaiton or link if (packet.packet_type() != Type::Packet::ANNOUNCE) { auto destination_iter = _destination_table.find(packet.destination_hash()); if (destination_iter != _destination_table.end()) { @@ -1428,10 +1473,14 @@ using namespace RNS::Utilities; } } + //////////////////////////////// + // TRANSPORT HANDLING + //////////////////////////////// + // General transport handling. Takes care of directing // packets according to transport tables and recording // entries in reverse and link tables. - if (Reticulum::transport_enabled() || from_local_client or for_local_client or for_local_client_link) { + if (Reticulum::transport_enabled() || from_local_client || for_local_client || for_local_client_link) { extreme("Transport::inbound: Performing general transport handling"); // If there is no transport id, but the packet is @@ -1442,6 +1491,7 @@ using namespace RNS::Utilities; // able), and reinsert, so the normal transport // implementation can handle the packet. if (!packet.transport_id() && for_local_client) { + extreme("Transport::inbound: Regenerating transport id"); packet.transport_id(_identity.hash()); } @@ -1459,6 +1509,7 @@ using namespace RNS::Utilities; // are the designated next hop, and process it // accordingly if we are. if (packet.transport_id() && packet.packet_type() != Type::Packet::ANNOUNCE) { + extreme("Transport::inbound: Packet is in transport..."); if (packet.transport_id() == _identity.hash()) { extreme("Transport::inbound: We are designated next-hop"); auto destination_iter = _destination_table.find(packet.destination_hash()); @@ -1544,14 +1595,21 @@ using namespace RNS::Utilities; extreme("Got packet in transport, but no known path to final destination " + packet.destination_hash().toHex() + ". Dropping packet."); } } + else { + extreme("Transport::inbound: We are not designated next-hop so not transporting"); + } + } + else { + extreme("Transport::inbound: Either packet is announce or packet has no next-hop (possibly for a local destination)"); } // Link transport handling. Directs packets according // to entries in the link tables if (packet.packet_type() != Type::Packet::ANNOUNCE && packet.packet_type() != Type::Packet::LINKREQUEST && packet.context() != Type::Packet::LRPROOF) { + extreme("Transport::inbound: Checking if packet is meant for link transport..."); auto link_iter = _link_table.find(packet.destination_hash()); if (link_iter != _link_table.end()) { - extreme("Transport::inbound: Handling link transport"); + extreme("Transport::inbound: Found link entry, handling link transport"); LinkEntry link_entry = (*link_iter).second; // If receiving and outbound interface is // the same for this link, direction doesn't @@ -1561,6 +1619,7 @@ using namespace RNS::Utilities; // But check that taken hops matches one // of the expectede values. if (packet.hops() == link_entry._remaining_hops || packet.hops() == link_entry._hops) { + extreme("Transport::inbound: Link inbound/outbound interfaes are same, transporting on same interface"); outbound_interface = link_entry._outbound_interface; } } @@ -1571,18 +1630,21 @@ using namespace RNS::Utilities; if (packet.receiving_interface() == link_entry._outbound_interface) { // Also check that expected hop count matches if (packet.hops() == link_entry._remaining_hops) { + extreme("Transport::inbound: Link transporting on inbound interface"); outbound_interface = link_entry._receiving_interface; } } else if (packet.receiving_interface() == link_entry._receiving_interface) { // Also check that expected hop count matches if (packet.hops() == link_entry._hops) { + extreme("Transport::inbound: Link transporting on outbound interface"); outbound_interface = link_entry._outbound_interface; } } } if (outbound_interface) { + extreme("Transport::inbound: Transmitting link transport packet"); Bytes new_raw; //new_raw = packet.raw[0:1] new_raw << packet.raw().left(1); @@ -1600,6 +1662,10 @@ using namespace RNS::Utilities; } } + //////////////////////////////// + // LOCAL HANDLING + //////////////////////////////// + // Announce handling. Handles logic related to incoming // announces, queueing rebroadcasts of these, and removal // of queued announce rebroadcasts once handed to the next node. @@ -1973,7 +2039,7 @@ using namespace RNS::Utilities; _destination_table.insert({packet.destination_hash(), destination_table_entry}); debug("Destination " + packet.destination_hash().toHex() + " is now " + std::to_string(announce_hops) + " hops away via " + received_from.toHex() + " on " + packet.receiving_interface().toString()); - extreme("Transport::inbound: Destination " + packet.destination_hash().toHex() + " has data: " + packet.data().toHex()); + //extreme("Transport::inbound: Destination " + packet.destination_hash().toHex() + " has data: " + packet.data().toHex()); //extreme("Transport::inbound: Destination " + packet.destination_hash().toHex() + " has text: " + packet.data().toString()); // TODO @@ -2088,10 +2154,10 @@ using namespace RNS::Utilities; auto iter = _destinations.find(packet.destination_hash()); if (iter != _destinations.end()) { // Data is for a local destination - debug("Transport::inbound: Packet destination " + packet.destination_hash().toHex() + " found, destination is local"); + debug("Packet destination " + packet.destination_hash().toHex() + " found, destination is local"); auto& destination = (*iter).second; if (destination.type() == packet.destination_type()) { - debug("Transport::inbound: Packet destination type " + std::to_string(packet.destination_type()) + " matched, processing"); + extreme("Transport::inbound: Packet destination type " + std::to_string(packet.destination_type()) + " matched, processing"); #endif packet.destination(destination); #if defined(DESTINATIONS_SET) @@ -2121,7 +2187,7 @@ using namespace RNS::Utilities; } } else { - debug("Transport::inbound: Packet destination " + packet.destination_hash().toHex() + " not found, not handling locally"); + debug("Transport::inbound: Local destination " + packet.destination_hash().toHex() + " not found, not handling packet locally"); } } } @@ -2204,7 +2270,7 @@ using namespace RNS::Utilities; proof_hash = packet.data().left(Type::Identity::HASHLENGTH/8); } - // Check if this proof neds to be transported + // Check if this proof needs to be transported if ((Reticulum::transport_enabled() || from_local_client || proof_for_local_client) && _reverse_table.find(packet.destination_hash()) != _reverse_table.end()) { ReverseEntry reverse_entry = (*_reverse_table.find(packet.destination_hash())).second; if (packet.receiving_interface() == reverse_entry._outbound_interface) { @@ -2221,14 +2287,17 @@ using namespace RNS::Utilities; debug("Proof received on wrong interface, not transporting it."); } } + else { + extreme("Proof is not candidate for transporting"); + } + std::list cull_receipts; for (auto& receipt : _receipts) { bool receipt_validated = false; if (proof_hash) { // Only test validation if hash matches if (receipt.hash() == proof_hash) { - // TODO - //z receipt_validated = receipt.validate_proof_packet(packet); + receipt_validated = receipt.validate_proof_packet(packet); } } else { @@ -2237,14 +2306,19 @@ using namespace RNS::Utilities; // In case of an implicit proof, we have // to check every single outstanding receipt - // TODO - //z receipt_validated = receipt.validate_proof_packet(packet); + receipt_validated = receipt.validate_proof_packet(packet); } - // CBA TODO requires modifying of collection while ioterating which is forbidden - //z if (receipt_validated) { - //z if receipt in Transport.receipts: - //z Transport.receipts.remove(receipt) + // CBA TODO requires modifying of collection while iterating which is forbidden + if (receipt_validated) { + //p if receipt in Transport.receipts: + //p Transport.receipts.remove(receipt) + cull_receipts.push_back(receipt); + } + } + // CBA since modifying of collection while iterating is forbidden + for (auto& receipt : _receipts) { + cull_receipts.remove(receipt); } } } @@ -2825,7 +2899,7 @@ will announce it. // destination being requested. if (data.size() >= Type::Identity::TRUNCATED_HASHLENGTH/8) { Bytes destination_hash = data.left(Type::Identity::TRUNCATED_HASHLENGTH/8); - extreme("Transport::path_request_handler: destination_hash: " + destination_hash.toHex()); + //extreme("Transport::path_request_handler: destination_hash: " + destination_hash.toHex()); // If there is also enough bytes for a transport // instance ID and at least one tag byte, we // assume the next bytes to be the trasport ID @@ -2833,7 +2907,7 @@ will announce it. Bytes requesting_transport_instance; if (data.size() > (Type::Identity::TRUNCATED_HASHLENGTH/8)*2) { requesting_transport_instance = data.mid(Type::Identity::TRUNCATED_HASHLENGTH/8, Type::Identity::TRUNCATED_HASHLENGTH/8); - extreme("Transport::path_request_handler: requesting_transport_instance: " + requesting_transport_instance.toHex()); + //extreme("Transport::path_request_handler: requesting_transport_instance: " + requesting_transport_instance.toHex()); } Bytes tag_bytes; @@ -2845,13 +2919,13 @@ will announce it. } if (tag_bytes) { - extreme("Transport::path_request_handler: tag_bytes: " + tag_bytes.toHex()); + //extreme("Transport::path_request_handler: tag_bytes: " + tag_bytes.toHex()); if (tag_bytes.size() > Type::Identity::TRUNCATED_HASHLENGTH/8) { tag_bytes = tag_bytes.left(Type::Identity::TRUNCATED_HASHLENGTH/8); } Bytes unique_tag = destination_hash + tag_bytes; - extreme("Transport::path_request_handler: unique_tag: " + unique_tag.toHex()); + //extreme("Transport::path_request_handler: unique_tag: " + unique_tag.toHex()); if (_discovery_pr_tags.find(unique_tag) == _discovery_pr_tags.end()) { _discovery_pr_tags.insert(unique_tag); @@ -2885,6 +2959,7 @@ will announce it. if (attached_interface) { if (Reticulum::transport_enabled() && (attached_interface.mode() & Interface::DISCOVER_PATHS_FOR) > 0) { + extreme("Transport::path_request_handler: interface allows searching for unknown paths"); should_search_for_unknown = true; } @@ -2931,6 +3006,7 @@ will announce it. } //p elif (RNS.Reticulum.transport_enabled() or is_from_local_client) and (destination_hash in Transport.destination_table): else if ((Reticulum::transport_enabled() || is_from_local_client) && destination_iter != _destination_table.end()) { + extreme("Transport::path_request_handler: entry found for destination " + destination_hash.toHex()); DestinationEntry& destination_entry = (*destination_iter).second; const Packet& packet = destination_entry._announce_packet; const Bytes& next_hop = destination_entry._received_from; @@ -3029,6 +3105,7 @@ will announce it. } } else if (should_search_for_unknown) { + extreme("Transport::path_request_handler: searching for unknown path to " + destination_hash.toHex()); if (_discovery_path_requests.find(destination_hash) != _discovery_path_requests.end()) { debug("There is already a waiting path request for destination " + destination_hash.toHex() + " on behalf of path request" + interface_str); } @@ -3051,12 +3128,17 @@ will announce it. #elif defined(INTERFACES_MAP) for (auto& [hash, interface] : _interfaces) { #endif - if (interface != attached_interface) { + // CBA TEST consider forwarding path requests even on requestor interface + //if (interface != attached_interface) { + if (true) { extreme("Transport::path_request: requesting path on interface " + interface.toString()); // Use the previously extracted tag from this path request // on the new path requests as well, to avoid potential loops request_path(destination_hash, interface, tag, true); } + else { + extreme("Transport::path_request: not requesting path on same interface " + interface.toString()); + } } } } diff --git a/src/Transport.h b/src/Transport.h index 65a81fd..2437750 100644 --- a/src/Transport.h +++ b/src/Transport.h @@ -55,6 +55,19 @@ namespace RNS { */ class Transport { + public: + class Callbacks { + public: + using receive_packet = void(*)(const Bytes& raw, const Interface& interface); + using transmit_packet = void(*)(const Bytes& raw, const Interface& interface); + using filter_packet = bool(*)(const Packet& packet); + public: + receive_packet _receive_packet = nullptr; + transmit_packet _transmit_packet = nullptr; + filter_packet _filter_packet = nullptr; + friend class Transport; + }; + public: // CBA TODO Analyze safety of using Inrerface references here // CBA TODO Analyze safety of using Packet references here @@ -77,7 +90,6 @@ namespace RNS { uint8_t _hops = 0; double _expires = 0; std::set _random_blobs; - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? Interface _receiving_interface = {Type::NONE}; //const Packet& _announce_packet; Packet _announce_packet = {Type::NONE}; @@ -129,8 +141,6 @@ namespace RNS { const Packet _packet = {Type::NONE}; uint8_t _local_rebroadcasts = 0; bool _block_rebroadcasts = false; - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? - //const Interface& _attached_interface; const Interface _attached_interface = {Type::NONE}; }; @@ -152,12 +162,8 @@ namespace RNS { public: double _timestamp = 0; const Bytes _next_hop; - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? - //const Interface& _outbound_interface; const Interface _outbound_interface = {Type::NONE}; uint8_t _remaining_hops = 0; - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? - //const Interface& _receiving_interface; Interface _receiving_interface = {Type::NONE}; uint8_t _hops = 0; const Bytes _destination_hash; @@ -175,11 +181,7 @@ namespace RNS { { } public: - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? - //const Interface& _receiving_interface; Interface _receiving_interface = {Type::NONE}; - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? - //const Interface& _outbound_interface; const Interface _outbound_interface = {Type::NONE}; double _timestamp = 0; }; @@ -196,8 +198,6 @@ namespace RNS { public: const Bytes _destination_hash; double _timeout = 0; - // CBA TODO does this need to be a reference in order for virtual method callbacks to work? - //const Interface& _requesting_interface; const Interface _requesting_interface = {Type::NONE}; }; @@ -255,6 +255,13 @@ namespace RNS { static Destination find_destination_from_hash(const Bytes& destination_hash); + // getters/setters + static inline void set_receive_packet_callback(Callbacks::receive_packet callback) { _callbacks._receive_packet = callback; } + static inline void set_transmit_packet_callback(Callbacks::transmit_packet callback) { _callbacks._transmit_packet = callback; } + static inline void set_filter_packet_callback(Callbacks::filter_packet callback) { _callbacks._filter_packet = callback; } + static inline const Reticulum& reticulum() { return _owner; } + static inline const Identity& identity() { return _identity; } + private: // CBA MUST use references to interfaces here in order for virtul overrides for send/receive to work #if defined(INTERFACES_SET) @@ -335,6 +342,9 @@ namespace RNS { static Reticulum _owner; static Identity _identity; + + // CBA + static Callbacks _callbacks; }; } diff --git a/src/Utilities/OS.cpp b/src/Utilities/OS.cpp index e0052ac..4fbef9c 100644 --- a/src/Utilities/OS.cpp +++ b/src/Utilities/OS.cpp @@ -37,6 +37,14 @@ void listDir(char * dir){ error("SPIFFS filesystem mount failed"); } else { + uint32_t size = SPIFFS.totalBytes() / (1024 * 1024); + Serial.print("size: "); + Serial.print(size); + Serial.println(" MB"); + uint32_t used = SPIFFS.usedBytes() / (1024 * 1024); + Serial.print("used: "); + Serial.print(used); + Serial.println(" MB"); // ensure filesystem is writable and format if not Bytes test; if (!OS::write_file(test, "/test")) { @@ -44,7 +52,7 @@ void listDir(char * dir){ SPIFFS.format(); } else { - OS::remove_file("test"); + OS::remove_file("/test"); } listDir("/"); debug("SPIFFS filesystem is ready"); diff --git a/src/main.cpp b/src/main.cpp index ebd1f75..5e18ce4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,7 +43,8 @@ #define LORA_INTERFACE //#define RETICULUM_PACKET_TEST -#define USER_BUTTON_PIN 38 +#define BUTTON_PIN 38 +//#define BUTTON_PIN_MASK GPIO_SEL_38 // Let's define an app name. We'll use this for all // destinations we create. Since this basic example @@ -310,7 +311,7 @@ void reticulum_teardown() { void userKey(void) { //delay(10); - if (digitalRead(USER_BUTTON_PIN) == LOW) { + if (digitalRead(BUTTON_PIN) == LOW) { //Serial.print("T-Beam USER button press\n"); send_announce = true; } @@ -324,8 +325,8 @@ void setup() { Serial.print("Hello from T-Beam on PlatformIO!\n"); // Setup user button - pinMode(USER_BUTTON_PIN, INPUT); - attachInterrupt(USER_BUTTON_PIN, userKey, FALLING); + pinMode(BUTTON_PIN, INPUT); + attachInterrupt(BUTTON_PIN, userKey, FALLING); // Setup filesystem if (!SPIFFS.begin(true, "")){