WIP: Refactor pointer/reference style.

This commit is contained in:
attermann 2023-11-20 10:51:56 -07:00
parent d09331ec0e
commit 263f589801
29 changed files with 347 additions and 347 deletions

View file

@ -21,7 +21,7 @@ void Bytes::ownData() {
} }
} }
int8_t Bytes::compare(const Bytes &bytes) const { int8_t Bytes::compare(const Bytes& bytes) const {
if (_data == bytes._data) { if (_data == bytes._data) {
return 0; return 0;
} }
@ -76,12 +76,12 @@ const char hex_lower_chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
std::string RNS::hexFromByte(uint8_t byte, bool upper /*= true*/) { std::string RNS::hexFromByte(uint8_t byte, bool upper /*= true*/) {
std::string hex; std::string hex;
if (upper) { if (upper) {
hex += hex_upper_chars[ (byte & 0xF0) >> 4]; hex += hex_upper_chars[ (byte& 0xF0) >> 4];
hex += hex_upper_chars[ (byte & 0x0F) >> 0]; hex += hex_upper_chars[ (byte& 0x0F) >> 0];
} }
else { else {
hex += hex_lower_chars[ (byte & 0xF0) >> 4]; hex += hex_lower_chars[ (byte& 0xF0) >> 4];
hex += hex_lower_chars[ (byte & 0x0F) >> 0]; hex += hex_lower_chars[ (byte& 0x0F) >> 0];
} }
return hex; return hex;
} }
@ -93,12 +93,12 @@ std::string Bytes::toHex(bool upper /*= true*/) const {
std::string hex; std::string hex;
for (uint8_t byte : *_data) { for (uint8_t byte : *_data) {
if (upper) { if (upper) {
hex += hex_upper_chars[ (byte & 0xF0) >> 4]; hex += hex_upper_chars[ (byte& 0xF0) >> 4];
hex += hex_upper_chars[ (byte & 0x0F) >> 0]; hex += hex_upper_chars[ (byte& 0x0F) >> 0];
} }
else { else {
hex += hex_lower_chars[ (byte & 0xF0) >> 4]; hex += hex_lower_chars[ (byte& 0xF0) >> 4];
hex += hex_lower_chars[ (byte & 0x0F) >> 0]; hex += hex_lower_chars[ (byte& 0x0F) >> 0];
} }
} }
return hex; return hex;

View file

@ -34,7 +34,7 @@ namespace RNS {
Bytes(NoneConstructor none) { Bytes(NoneConstructor none) {
//extreme("Bytes object created from NONE, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get())); //extreme("Bytes object created from NONE, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
} }
Bytes(const Bytes &bytes) { Bytes(const Bytes& bytes) {
//extreme("Bytes is using shared data"); //extreme("Bytes is using shared data");
assign(bytes); assign(bytes);
//extreme("Bytes object copy created from bytes \"" + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get())); //extreme("Bytes object copy created from bytes \"" + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
@ -47,7 +47,7 @@ namespace RNS {
assign(string); assign(string);
//extreme(std::string("Bytes object created from string \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get())); //extreme(std::string("Bytes object created from string \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
} }
Bytes(const std::string &string) { Bytes(const std::string& string) {
assign(string); assign(string);
//extreme(std::string("Bytes object created from std::string \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get())); //extreme(std::string("Bytes object created from std::string \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
} }
@ -55,30 +55,30 @@ namespace RNS {
//extreme(std::string("Bytes object destroyed \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get())); //extreme(std::string("Bytes object destroyed \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
} }
inline Bytes& operator = (const Bytes &bytes) { inline Bytes& operator = (const Bytes& bytes) {
assign(bytes); assign(bytes);
return *this; return *this;
} }
inline Bytes& operator += (const Bytes &bytes) { inline Bytes& operator += (const Bytes& bytes) {
append(bytes); append(bytes);
return *this; return *this;
} }
inline Bytes operator + (const Bytes &bytes) const { inline Bytes operator + (const Bytes& bytes) const {
Bytes newbytes(*this); Bytes newbytes(*this);
newbytes.append(bytes); newbytes.append(bytes);
return newbytes; return newbytes;
} }
inline bool operator == (const Bytes &bytes) const { inline bool operator == (const Bytes& bytes) const {
return compare(bytes) == 0; return compare(bytes) == 0;
} }
inline bool operator != (const Bytes &bytes) const { inline bool operator != (const Bytes& bytes) const {
return compare(bytes) != 0; return compare(bytes) != 0;
} }
inline bool operator < (const Bytes &bytes) const { inline bool operator < (const Bytes& bytes) const {
return compare(bytes) < 0; return compare(bytes) < 0;
} }
inline bool operator > (const Bytes &bytes) const { inline bool operator > (const Bytes& bytes) const {
return compare(bytes) > 0; return compare(bytes) > 0;
} }
inline operator bool() const { inline operator bool() const {
@ -144,7 +144,7 @@ namespace RNS {
newData(); newData();
_data->insert(_data->begin(), (uint8_t *)string, (uint8_t *)string + strlen(string)); _data->insert(_data->begin(), (uint8_t *)string, (uint8_t *)string + strlen(string));
} }
inline void assign(const std::string &string) { assign(string.c_str()); } inline void assign(const std::string& string) { assign(string.c_str()); }
void assignHex(const char* hex); void assignHex(const char* hex);
inline void append(const Bytes& bytes) { inline void append(const Bytes& bytes) {
@ -171,7 +171,7 @@ namespace RNS {
ownData(); ownData();
_data->insert(_data->end(), (uint8_t *)string, (uint8_t *)string + strlen(string)); _data->insert(_data->end(), (uint8_t *)string, (uint8_t *)string + strlen(string));
} }
inline void append(const std::string &string) { append(string.c_str()); } inline void append(const std::string& string) { append(string.c_str()); }
inline void append(uint8_t byte) { inline void append(uint8_t byte) {
ownData(); ownData();
_data->push_back(byte); _data->push_back(byte);
@ -193,7 +193,7 @@ namespace RNS {
} }
public: public:
int8_t compare(const Bytes &bytes) const; int8_t compare(const Bytes& bytes) const;
inline size_t size() const { if (!_data) return 0; return _data->size(); } inline size_t size() const { if (!_data) return 0; return _data->size(); }
inline bool empty() const { if (!_data) return true; return _data->empty(); } inline bool empty() const { if (!_data) return true; return _data->empty(); }
inline size_t capacity() const { if (!_data) return 0; return _data->capacity(); } inline size_t capacity() const { if (!_data) return 0; return _data->capacity(); }
@ -248,12 +248,12 @@ namespace RNS {
} }
inline RNS::Bytes& operator << (RNS::Bytes &lhbytes, const RNS::Bytes &rhbytes) { inline RNS::Bytes& operator << (RNS::Bytes& lhbytes, const RNS::Bytes& rhbytes) {
lhbytes.append(rhbytes); lhbytes.append(rhbytes);
return lhbytes; return lhbytes;
} }
inline RNS::Bytes& operator << (RNS::Bytes &lhbytes, uint8_t rhbyte) { inline RNS::Bytes& operator << (RNS::Bytes& lhbytes, uint8_t rhbyte) {
lhbytes.append(rhbyte); lhbytes.append(rhbyte);
return lhbytes; return lhbytes;
} }

View file

@ -11,7 +11,7 @@ namespace RNS { namespace Cryptography {
class AES_128_CBC { class AES_128_CBC {
public: public:
static inline const Bytes encrypt(const Bytes &plaintext, const Bytes &key, const Bytes &iv) { static inline const Bytes encrypt(const Bytes& plaintext, const Bytes& key, const Bytes& iv) {
CBC<AES128> cbc; CBC<AES128> cbc;
cbc.setKey(key.data(), key.size()); cbc.setKey(key.data(), key.size());
cbc.setIV(iv.data(), iv.size()); cbc.setIV(iv.data(), iv.size());
@ -20,7 +20,7 @@ namespace RNS { namespace Cryptography {
return ciphertext; return ciphertext;
} }
static inline const Bytes decrypt(const Bytes &ciphertext, const Bytes &key, const Bytes &iv) { static inline const Bytes decrypt(const Bytes& ciphertext, const Bytes& key, const Bytes& iv) {
CBC<AES128> cbc; CBC<AES128> cbc;
cbc.setKey(key.data(), key.size()); cbc.setKey(key.data(), key.size());
cbc.setIV(iv.data(), iv.size()); cbc.setIV(iv.data(), iv.size());
@ -30,7 +30,7 @@ namespace RNS { namespace Cryptography {
} }
// EXPERIMENTAL - overwrites passed buffer // EXPERIMENTAL - overwrites passed buffer
static inline void inplace_encrypt(Bytes &plaintext, const Bytes &key, const Bytes &iv) { static inline void inplace_encrypt(Bytes& plaintext, const Bytes& key, const Bytes& iv) {
CBC<AES128> cbc; CBC<AES128> cbc;
cbc.setKey(key.data(), key.size()); cbc.setKey(key.data(), key.size());
cbc.setIV(iv.data(), iv.size()); cbc.setIV(iv.data(), iv.size());
@ -38,7 +38,7 @@ namespace RNS { namespace Cryptography {
} }
// EXPERIMENTAL - overwrites passed buffer // EXPERIMENTAL - overwrites passed buffer
static inline void inplace_decrypt(Bytes &ciphertext, const Bytes &key, const Bytes &iv) { static inline void inplace_decrypt(Bytes& ciphertext, const Bytes& key, const Bytes& iv) {
CBC<AES128> cbc; CBC<AES128> cbc;
cbc.setKey(key.data(), key.size()); cbc.setKey(key.data(), key.size());
cbc.setIV(iv.data(), iv.size()); cbc.setIV(iv.data(), iv.size());

View file

@ -17,7 +17,7 @@ namespace RNS { namespace Cryptography {
class Ed25519PublicKey { class Ed25519PublicKey {
public: public:
Ed25519PublicKey(const Bytes &publicKey) { Ed25519PublicKey(const Bytes& publicKey) {
_publicKey = publicKey; _publicKey = publicKey;
} }
~Ed25519PublicKey() {} ~Ed25519PublicKey() {}
@ -26,15 +26,15 @@ namespace RNS { namespace Cryptography {
public: public:
// creates a new instance with specified seed // creates a new instance with specified seed
static inline Ptr from_public_bytes(const Bytes &publicKey) { static inline Ptr from_public_bytes(const Bytes& publicKey) {
return Ptr(new Ed25519PublicKey(publicKey)); return Ptr(new Ed25519PublicKey(publicKey));
} }
inline const Bytes &public_bytes() { inline const Bytes& public_bytes() {
return _publicKey; return _publicKey;
} }
inline bool verify(const Bytes &signature, const Bytes &message) { inline bool verify(const Bytes& signature, const Bytes& message) {
return Ed25519::verify(signature.data(), _publicKey.data(), message.data(), message.size()); return Ed25519::verify(signature.data(), _publicKey.data(), message.data(), message.size());
} }
@ -46,7 +46,7 @@ namespace RNS { namespace Cryptography {
class Ed25519PrivateKey { class Ed25519PrivateKey {
public: public:
Ed25519PrivateKey(const Bytes &privateKey) { Ed25519PrivateKey(const Bytes& privateKey) {
if (privateKey) { if (privateKey) {
// use specified private key // use specified private key
_privateKey = privateKey; _privateKey = privateKey;
@ -71,11 +71,11 @@ namespace RNS { namespace Cryptography {
} }
// creates a new instance with specified seed // creates a new instance with specified seed
static inline Ptr from_private_bytes(const Bytes &privateKey) { static inline Ptr from_private_bytes(const Bytes& privateKey) {
return Ptr(new Ed25519PrivateKey(privateKey)); return Ptr(new Ed25519PrivateKey(privateKey));
} }
inline const Bytes &private_bytes() { inline const Bytes& private_bytes() {
return _privateKey; return _privateKey;
} }
@ -84,7 +84,7 @@ namespace RNS { namespace Cryptography {
return Ed25519PublicKey::from_public_bytes(_publicKey); return Ed25519PublicKey::from_public_bytes(_publicKey);
} }
inline const Bytes sign(const Bytes &message) { inline const Bytes sign(const Bytes& message) {
//zreturn _sk.sign(message); //zreturn _sk.sign(message);
Bytes signature; Bytes signature;
Ed25519::sign(signature.writable(64), _privateKey.data(), _publicKey.data(), message.data(), message.size()); Ed25519::sign(signature.writable(64), _privateKey.data(), _publicKey.data(), message.data(), message.size());

View file

@ -11,7 +11,7 @@
using namespace RNS; using namespace RNS;
using namespace RNS::Cryptography; using namespace RNS::Cryptography;
Fernet::Fernet(const Bytes &key) { Fernet::Fernet(const Bytes& key) {
if (!key) { if (!key) {
throw std::invalid_argument("Fernet key cannot be None"); throw std::invalid_argument("Fernet key cannot be None");
@ -33,7 +33,7 @@ Fernet::~Fernet() {
extreme("Fernet object destroyed"); extreme("Fernet object destroyed");
} }
bool Fernet::verify_hmac(const Bytes &token) { bool Fernet::verify_hmac(const Bytes& token) {
if (token.size() <= 32) { if (token.size() <= 32) {
throw std::invalid_argument("Cannot verify HMAC on token of only " + std::to_string(token.size()) + " bytes"); throw std::invalid_argument("Cannot verify HMAC on token of only " + std::to_string(token.size()) + " bytes");
@ -49,7 +49,7 @@ bool Fernet::verify_hmac(const Bytes &token) {
return (received_hmac == expected_hmac); return (received_hmac == expected_hmac);
} }
const Bytes Fernet::encrypt(const Bytes &data) { const Bytes Fernet::encrypt(const Bytes& data) {
debug("Fernet::encrypt: plaintext length: " + std::to_string(data.size())); debug("Fernet::encrypt: plaintext length: " + std::to_string(data.size()));
Bytes iv = random(16); Bytes iv = random(16);
@ -76,7 +76,7 @@ const Bytes Fernet::encrypt(const Bytes &data) {
} }
const Bytes Fernet::decrypt(const Bytes &token) { const Bytes Fernet::decrypt(const Bytes& token) {
debug("Fernet::decrypt: token length: " + std::to_string(token.size())); debug("Fernet::decrypt: token length: " + std::to_string(token.size()));
if (token.size() < 48) { if (token.size() < 48) {
@ -109,7 +109,7 @@ const Bytes Fernet::decrypt(const Bytes &token) {
debug("Fernet::decrypt: plaintext length: " + std::to_string(plaintext.size())); debug("Fernet::decrypt: plaintext length: " + std::to_string(plaintext.size()));
return plaintext; return plaintext;
} }
catch (std::exception &e) { catch (std::exception& e) {
warning("Could not decrypt Fernet token"); warning("Could not decrypt Fernet token");
throw std::runtime_error("Could not decrypt Fernet token"); throw std::runtime_error("Could not decrypt Fernet token");
} }

View file

@ -25,13 +25,13 @@ namespace RNS { namespace Cryptography {
static inline const Bytes generate_key() { return random(32); } static inline const Bytes generate_key() { return random(32); }
public: public:
Fernet(const Bytes &key); Fernet(const Bytes& key);
~Fernet(); ~Fernet();
public: public:
bool verify_hmac(const Bytes &token); bool verify_hmac(const Bytes& token);
const Bytes encrypt(const Bytes &data); const Bytes encrypt(const Bytes& data);
const Bytes decrypt(const Bytes &token); const Bytes decrypt(const Bytes& token);
private: private:
Bytes _signing_key; Bytes _signing_key;

View file

@ -5,7 +5,7 @@
using namespace RNS; using namespace RNS;
const Bytes RNS::Cryptography::hkdf(size_t length, const Bytes &derive_from, const Bytes &salt /*= {Bytes::NONE}*/, const Bytes &context /*= {Bytes::NONE}*/) { const Bytes RNS::Cryptography::hkdf(size_t length, const Bytes& derive_from, const Bytes& salt /*= {Bytes::NONE}*/, const Bytes& context /*= {Bytes::NONE}*/) {
if (length <= 0) { if (length <= 0) {
throw std::invalid_argument("Invalid output key length"); throw std::invalid_argument("Invalid output key length");

View file

@ -4,6 +4,6 @@
namespace RNS { namespace Cryptography { namespace RNS { namespace Cryptography {
const Bytes hkdf(size_t length, const Bytes &derive_from, const Bytes &salt = {Bytes::NONE}, const Bytes &context = {Bytes::NONE}); const Bytes hkdf(size_t length, const Bytes& derive_from, const Bytes& salt = {Bytes::NONE}, const Bytes& context = {Bytes::NONE});
} } } }

View file

@ -28,7 +28,7 @@ namespace RNS { namespace Cryptography {
msg: bytes or buffer, Initial input for the hash or None. msg: bytes or buffer, Initial input for the hash or None.
digest: The underlying hash algorithm to use digest: The underlying hash algorithm to use
*/ */
HMAC(const Bytes &key, const Bytes &msg = {Bytes::NONE}, Digest digest = DIGEST_SHA256) { HMAC(const Bytes& key, const Bytes& msg = {Bytes::NONE}, Digest digest = DIGEST_SHA256) {
if (digest == DIGEST_NONE) { if (digest == DIGEST_NONE) {
throw std::invalid_argument("Cannot derive key from empty input material"); throw std::invalid_argument("Cannot derive key from empty input material");
@ -56,7 +56,7 @@ namespace RNS { namespace Cryptography {
/* /*
Feed data from msg into this hashing object. Feed data from msg into this hashing object.
*/ */
void update(const Bytes &msg) { void update(const Bytes& msg) {
assert(_hash); assert(_hash);
_hash->update(msg.data(), msg.size()); _hash->update(msg.data(), msg.size());
} }
@ -83,7 +83,7 @@ namespace RNS { namespace Cryptography {
method, and can ask for the hash value at any time by calling its digest() method, and can ask for the hash value at any time by calling its digest()
method. method.
*/ */
static inline Ptr generate(const Bytes &key, const Bytes &msg = {Bytes::NONE}, Digest digest = DIGEST_SHA256) { static inline Ptr generate(const Bytes& key, const Bytes& msg = {Bytes::NONE}, Digest digest = DIGEST_SHA256) {
return Ptr(new HMAC(key, msg, digest)); return Ptr(new HMAC(key, msg, digest));
} }
@ -99,7 +99,7 @@ namespace RNS { namespace Cryptography {
msg: bytes or buffer, Input message. msg: bytes or buffer, Input message.
digest: The underlying hash algorithm to use. digest: The underlying hash algorithm to use.
*/ */
inline const Bytes digest(const Bytes &key, const Bytes &msg, HMAC::Digest digest = HMAC::DIGEST_SHA256) { inline const Bytes digest(const Bytes& key, const Bytes& msg, HMAC::Digest digest = HMAC::DIGEST_SHA256) {
HMAC hmac(key, msg, digest); HMAC hmac(key, msg, digest);
hmac.update(msg); hmac.update(msg);
return hmac.digest(); return hmac.digest();

View file

@ -14,7 +14,7 @@ uses Python's internal SHA-256 implementation. All SHA-256
calls in RNS end up here. calls in RNS end up here.
*/ */
const Bytes RNS::Cryptography::sha256(const Bytes &data) { const Bytes RNS::Cryptography::sha256(const Bytes& data) {
//extreme("Cryptography::sha256: data: " + data.toHex() ); //extreme("Cryptography::sha256: data: " + data.toHex() );
SHA256 digest; SHA256 digest;
digest.reset(); digest.reset();
@ -25,7 +25,7 @@ const Bytes RNS::Cryptography::sha256(const Bytes &data) {
return hash; return hash;
} }
const Bytes RNS::Cryptography::sha512(const Bytes &data) { const Bytes RNS::Cryptography::sha512(const Bytes& data) {
SHA512 digest; SHA512 digest;
digest.reset(); digest.reset();
digest.update(data.data(), data.size()); digest.update(data.data(), data.size());

View file

@ -6,7 +6,7 @@
namespace RNS { namespace Cryptography { namespace RNS { namespace Cryptography {
const Bytes sha256(const Bytes &data); const Bytes sha256(const Bytes& data);
const Bytes sha512(const Bytes &data); const Bytes sha512(const Bytes& data);
} } } }

View file

@ -13,20 +13,20 @@ namespace RNS { namespace Cryptography {
static const size_t BLOCKSIZE = 16; static const size_t BLOCKSIZE = 16;
static inline const Bytes pad(const Bytes &data, size_t bs = BLOCKSIZE) { static inline const Bytes pad(const Bytes& data, size_t bs = BLOCKSIZE) {
Bytes padded(data); Bytes padded(data);
inplace_pad(padded, bs); inplace_pad(padded, bs);
return padded; return padded;
} }
static inline const Bytes unpad(const Bytes &data, size_t bs = BLOCKSIZE) { static inline const Bytes unpad(const Bytes& data, size_t bs = BLOCKSIZE) {
Bytes unpadded(data); Bytes unpadded(data);
inplace_unpad(unpadded, bs); inplace_unpad(unpadded, bs);
return unpadded; return unpadded;
} }
// updates passed buffer // updates passed buffer
static inline void inplace_pad(Bytes &data, size_t bs = BLOCKSIZE) { static inline void inplace_pad(Bytes& data, size_t bs = BLOCKSIZE) {
size_t len = data.size(); size_t len = data.size();
//debug("PKCS7::pad: len: " + std::to_string(len)); //debug("PKCS7::pad: len: " + std::to_string(len));
size_t padlen = bs - (len % bs); size_t padlen = bs - (len % bs);
@ -45,7 +45,7 @@ namespace RNS { namespace Cryptography {
} }
// updates passed buffer // updates passed buffer
static inline void inplace_unpad(Bytes &data, size_t bs = BLOCKSIZE) { static inline void inplace_unpad(Bytes& data, size_t bs = BLOCKSIZE) {
size_t len = data.size(); size_t len = data.size();
//debug("PKCS7::unpad: len: " + std::to_string(len)); //debug("PKCS7::unpad: len: " + std::to_string(len));
// read last byte which is pad length // read last byte which is pad length

View file

@ -15,11 +15,11 @@ namespace RNS { namespace Cryptography {
public: public:
/* /*
X25519PublicKey(const Bytes &x) { X25519PublicKey(const Bytes& x) {
_x = x; _x = x;
} }
*/ */
X25519PublicKey(const Bytes &publicKey) { X25519PublicKey(const Bytes& publicKey) {
_publicKey = publicKey; _publicKey = publicKey;
} }
~X25519PublicKey() {} ~X25519PublicKey() {}
@ -29,11 +29,11 @@ namespace RNS { namespace Cryptography {
public: public:
// creates a new instance with specified seed // creates a new instance with specified seed
/* /*
static inline Ptr from_public_bytes(const Bytes &data) { static inline Ptr from_public_bytes(const Bytes& data) {
return Ptr(new X25519PublicKey(_unpack_number(data))); return Ptr(new X25519PublicKey(_unpack_number(data)));
} }
*/ */
static inline Ptr from_public_bytes(const Bytes &publicKey) { static inline Ptr from_public_bytes(const Bytes& publicKey) {
return Ptr(new X25519PublicKey(publicKey)); return Ptr(new X25519PublicKey(publicKey));
} }
@ -64,11 +64,11 @@ namespace RNS { namespace Cryptography {
public: public:
/* /*
X25519PrivateKey(const Bytes &a) { X25519PrivateKey(const Bytes& a) {
_a = a; _a = a;
} }
*/ */
X25519PrivateKey(const Bytes &privateKey) { X25519PrivateKey(const Bytes& privateKey) {
if (privateKey) { if (privateKey) {
// use specified private key // use specified private key
_privateKey = privateKey; _privateKey = privateKey;
@ -102,11 +102,11 @@ namespace RNS { namespace Cryptography {
// creates a new instance with specified seed // creates a new instance with specified seed
/* /*
static inline Ptr from_private_bytes(const Bytes &data) { static inline Ptr from_private_bytes(const Bytes& data) {
return Ptr(new X25519PrivateKey(_fix_secret(_unpack_number(data)))); return Ptr(new X25519PrivateKey(_fix_secret(_unpack_number(data))));
} }
*/ */
static inline Ptr from_private_bytes(const Bytes &privateKey) { static inline Ptr from_private_bytes(const Bytes& privateKey) {
return Ptr(new X25519PrivateKey(privateKey)); return Ptr(new X25519PrivateKey(privateKey));
} }
@ -115,7 +115,7 @@ namespace RNS { namespace Cryptography {
return _pack_number(_a); return _pack_number(_a);
} }
*/ */
inline const Bytes &private_bytes() { inline const Bytes& private_bytes() {
return _privateKey; return _privateKey;
} }
@ -130,7 +130,7 @@ namespace RNS { namespace Cryptography {
} }
/* /*
inline const Bytes exchange(const Bytes &peer_public_key) { inline const Bytes exchange(const Bytes& peer_public_key) {
if isinstance(peer_public_key, bytes): if isinstance(peer_public_key, bytes):
peer_public_key = X25519PublicKey.from_public_bytes(peer_public_key) peer_public_key = X25519PublicKey.from_public_bytes(peer_public_key)
@ -168,7 +168,7 @@ namespace RNS { namespace Cryptography {
return shared return shared
} }
*/ */
inline const Bytes exchange(const Bytes &peer_public_key) { inline const Bytes exchange(const Bytes& peer_public_key) {
debug("X25519PublicKey::exchange: public key: " + _publicKey.toHex()); debug("X25519PublicKey::exchange: public key: " + _publicKey.toHex());
debug("X25519PublicKey::exchange: peer public key: " + peer_public_key.toHex()); debug("X25519PublicKey::exchange: peer public key: " + peer_public_key.toHex());
debug("X25519PublicKey::exchange: pre private key: " + _privateKey.toHex()); debug("X25519PublicKey::exchange: pre private key: " + _privateKey.toHex());
@ -181,7 +181,7 @@ namespace RNS { namespace Cryptography {
return sharedKey; return sharedKey;
} }
inline bool verify(const Bytes &peer_public_key) { inline bool verify(const Bytes& peer_public_key) {
debug("X25519PublicKey::exchange: public key: " + _publicKey.toHex()); debug("X25519PublicKey::exchange: public key: " + _publicKey.toHex());
debug("X25519PublicKey::exchange: peer public key: " + peer_public_key.toHex()); debug("X25519PublicKey::exchange: peer public key: " + peer_public_key.toHex());
debug("X25519PublicKey::exchange: pre private key: " + _privateKey.toHex()); debug("X25519PublicKey::exchange: pre private key: " + _privateKey.toHex());

View file

@ -12,7 +12,7 @@
using namespace RNS; using namespace RNS;
using namespace RNS::Type::Destination; using namespace RNS::Type::Destination;
Destination::Destination(const Identity &identity, const directions direction, const types type, const char* app_name, const char *aspects) : _object(new Object(identity)) { Destination::Destination(const Identity& identity, const directions direction, const types type, const char* app_name, const char *aspects) : _object(new Object(identity)) {
assert(_object); assert(_object);
// Check input values and build name string // Check input values and build name string
@ -58,7 +58,7 @@ Destination::Destination(const Identity &identity, const directions direction, c
/* /*
:returns: A destination name in adressable hash form, for an app_name and a number of aspects. :returns: A destination name in adressable hash form, for an app_name and a number of aspects.
*/ */
/*static*/ Bytes Destination::hash(const Identity &identity, const char *app_name, const char *aspects) { /*static*/ Bytes Destination::hash(const Identity& identity, const char *app_name, const char *aspects) {
//name_hash = Identity::full_hash(Destination.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)] //name_hash = Identity::full_hash(Destination.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)]
//addr_hash_material = name_hash //addr_hash_material = name_hash
Bytes addr_hash_material = Identity::truncated_hash(expand_name({Type::NONE}, app_name, aspects)); Bytes addr_hash_material = Identity::truncated_hash(expand_name({Type::NONE}, app_name, aspects));
@ -77,7 +77,7 @@ Destination::Destination(const Identity &identity, const directions direction, c
/* /*
:returns: A string containing the full human-readable name of the destination, for an app_name and a number of aspects. :returns: A string containing the full human-readable name of the destination, for an app_name and a number of aspects.
*/ */
/*static*/ std::string Destination::expand_name(const Identity &identity, const char *app_name, const char *aspects) { /*static*/ std::string Destination::expand_name(const Identity& identity, const char *app_name, const char *aspects) {
if (strchr(app_name, '.') != nullptr) { if (strchr(app_name, '.') != nullptr) {
throw std::invalid_argument("Dots can't be used in app names"); throw std::invalid_argument("Dots can't be used in app names");
@ -103,8 +103,8 @@ relevant interfaces. Application specific data can be added to the announce.
:param app_data: *bytes* containing the app_data. :param app_data: *bytes* containing the app_data.
:param path_response: Internal flag used by :ref:`RNS.Transport<api-transport>`. Ignore. :param path_response: Internal flag used by :ref:`RNS.Transport<api-transport>`. Ignore.
*/ */
//Packet Destination::announce(const Bytes &app_data /*= {}*/, bool path_response /*= false*/, const Interface &attached_interface /*= {Type::NONE}*/, const Bytes &tag /*= {}*/, bool send /*= true*/) { //Packet Destination::announce(const Bytes& app_data /*= {}*/, bool path_response /*= false*/, const Interface& attached_interface /*= {Type::NONE}*/, const Bytes& tag /*= {}*/, bool send /*= true*/) {
Packet Destination::announce(const Bytes &app_data, bool path_response, const Interface &attached_interface, const Bytes &tag /*= {}*/, bool send /*= true*/) { Packet Destination::announce(const Bytes& app_data, bool path_response, const Interface& attached_interface, const Bytes& tag /*= {}*/, bool send /*= true*/) {
assert(_object); assert(_object);
debug("Destination::announce: announcing destination..."); debug("Destination::announce: announcing destination...");
@ -120,9 +120,9 @@ Packet Destination::announce(const Bytes &app_data, bool path_response, const In
auto it = _object->_path_responses.begin(); auto it = _object->_path_responses.begin();
while (it != _object->_path_responses.end()) { while (it != _object->_path_responses.end()) {
// vector // vector
//Response &entry = *it; //Response& entry = *it;
// map // map
PathResponse &entry = (*it).second; PathResponse& entry = (*it).second;
if (now > (entry.first + PR_TAG_WINDOW)) { if (now > (entry.first + PR_TAG_WINDOW)) {
it = _object->_path_responses.erase(it); it = _object->_path_responses.erase(it);
} }
@ -227,7 +227,7 @@ Packet Destination::announce(const Bytes &app_data, bool path_response, const In
} }
} }
Packet Destination::announce(const Bytes &app_data /*= {}*/, bool path_response /*= false*/) { Packet Destination::announce(const Bytes& app_data /*= {}*/, bool path_response /*= false*/) {
return announce(app_data, path_response, {Type::NONE}); return announce(app_data, path_response, {Type::NONE});
} }
@ -242,7 +242,7 @@ Registers a request handler.
:raises: ``ValueError`` if any of the supplied arguments are invalid. :raises: ``ValueError`` if any of the supplied arguments are invalid.
*/ */
/* /*
void Destination::register_request_handler(const Bytes &path, response_generator = None, request_policies allow = ALLOW_NONE, allowed_list = None) { void Destination::register_request_handler(const Bytes& path, response_generator = None, request_policies allow = ALLOW_NONE, allowed_list = None) {
if path == None or path == "": if path == None or path == "":
raise ValueError("Invalid path specified") raise ValueError("Invalid path specified")
elif not callable(response_generator): elif not callable(response_generator):
@ -263,7 +263,7 @@ Deregisters a request handler.
:returns: True if the handler was deregistered, otherwise False. :returns: True if the handler was deregistered, otherwise False.
*/ */
/* /*
bool Destination::deregister_request_handler(const Bytes &path) { bool Destination::deregister_request_handler(const Bytes& path) {
path_hash = RNS.Identity.truncated_hash(path.encode("utf-8")) path_hash = RNS.Identity.truncated_hash(path.encode("utf-8"))
if path_hash in self.request_handlers: if path_hash in self.request_handlers:
self.request_handlers.pop(path_hash) self.request_handlers.pop(path_hash)
@ -273,7 +273,7 @@ bool Destination::deregister_request_handler(const Bytes &path) {
} }
*/ */
void Destination::receive(const Packet &packet) { void Destination::receive(const Packet& packet) {
assert(_object); assert(_object);
if (packet.packet_type() == Type::Packet::LINKREQUEST) { if (packet.packet_type() == Type::Packet::LINKREQUEST) {
Bytes plaintext(packet.data()); Bytes plaintext(packet.data());
@ -289,7 +289,7 @@ void Destination::receive(const Packet &packet) {
try { try {
_object->_callbacks._packet(plaintext, packet); _object->_callbacks._packet(plaintext, packet);
} }
catch (std::exception &e) { catch (std::exception& e) {
debug("Error while executing receive callback from " + toString() + ". The contained exception was: " + e.what()); debug("Error while executing receive callback from " + toString() + ". The contained exception was: " + e.what());
} }
} }
@ -298,7 +298,7 @@ void Destination::receive(const Packet &packet) {
} }
} }
void Destination::incoming_link_request(const Bytes &data, const Packet &packet) { void Destination::incoming_link_request(const Bytes& data, const Packet& packet) {
assert(_object); assert(_object);
if (_object->_accept_link_requests) { if (_object->_accept_link_requests) {
//z link = Link::validate_request(data, packet); //z link = Link::validate_request(data, packet);
@ -314,7 +314,7 @@ Encrypts information for ``RNS.Destination.SINGLE`` or ``RNS.Destination.GROUP``
:param plaintext: A *bytes-like* containing the plaintext to be encrypted. :param plaintext: A *bytes-like* containing the plaintext to be encrypted.
:raises: ``ValueError`` if destination does not hold a necessary key for encryption. :raises: ``ValueError`` if destination does not hold a necessary key for encryption.
*/ */
const Bytes Destination::encrypt(const Bytes &data) { const Bytes Destination::encrypt(const Bytes& data) {
assert(_object); assert(_object);
debug("Destination::encrypt: encrypting data..."); debug("Destination::encrypt: encrypting data...");
@ -348,7 +348,7 @@ Decrypts information for ``RNS.Destination.SINGLE`` or ``RNS.Destination.GROUP``
:param ciphertext: *Bytes* containing the ciphertext to be decrypted. :param ciphertext: *Bytes* containing the ciphertext to be decrypted.
:raises: ``ValueError`` if destination does not hold a necessary key for decryption. :raises: ``ValueError`` if destination does not hold a necessary key for decryption.
*/ */
const Bytes Destination::decrypt(const Bytes &data) { const Bytes Destination::decrypt(const Bytes& data) {
assert(_object); assert(_object);
debug("Destination::decrypt: decrypting data..."); debug("Destination::decrypt: decrypting data...");
@ -382,7 +382,7 @@ Signs information for ``RNS.Destination.SINGLE`` type destination.
:param message: *Bytes* containing the message to be signed. :param message: *Bytes* containing the message to be signed.
:returns: A *bytes-like* containing the message signature, or *None* if the destination could not sign the message. :returns: A *bytes-like* containing the message signature, or *None* if the destination could not sign the message.
*/ */
const Bytes Destination::sign(const Bytes &message) { const Bytes Destination::sign(const Bytes& message) {
assert(_object); assert(_object);
if (_object->_type == SINGLE && _object->_identity) { if (_object->_type == SINGLE && _object->_identity) {
return _object->_identity.sign(message); return _object->_identity.sign(message);

View file

@ -39,10 +39,10 @@ namespace RNS {
public: public:
class Callbacks { class Callbacks {
public: public:
using link_established = void(*)(const Link &link); using link_established = void(*)(const Link& link);
//using packet = void(*)(uint8_t *data, uint16_t data_len, Packet *packet); //using packet = void(*)(uint8_t *data, uint16_t data_len, Packet *packet);
using packet = void(*)(const Bytes &data, const Packet &packet); using packet = void(*)(const Bytes& data, const Packet& packet);
using proof_requested = bool(*)(const Packet &packet); using proof_requested = bool(*)(const Packet& packet);
public: public:
link_established _link_established = nullptr; link_established _link_established = nullptr;
packet _packet = nullptr; packet _packet = nullptr;
@ -58,15 +58,15 @@ namespace RNS {
Destination(Type::NoneConstructor none) { Destination(Type::NoneConstructor none) {
extreme("Destination NONE object created"); extreme("Destination NONE object created");
} }
Destination(const Destination &destination) : _object(destination._object) { Destination(const Destination& destination) : _object(destination._object) {
extreme("Destination object copy created"); extreme("Destination object copy created");
} }
Destination(const Identity &identity, const Type::Destination::directions direction, const Type::Destination::types type, const char* app_name, const char *aspects); Destination(const Identity& identity, const Type::Destination::directions direction, const Type::Destination::types type, const char* app_name, const char *aspects);
virtual ~Destination() { virtual ~Destination() {
extreme("Destination object destroyed"); extreme("Destination object destroyed");
} }
inline Destination& operator = (const Destination &destination) { inline Destination& operator = (const Destination& destination) {
_object = destination._object; _object = destination._object;
extreme("Destination object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Destination object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
return *this; return *this;
@ -74,18 +74,18 @@ namespace RNS {
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const Destination &destination) const { inline bool operator < (const Destination& destination) const {
return _object.get() < destination._object.get(); return _object.get() < destination._object.get();
} }
public: public:
static Bytes hash(const Identity &identity, const char *app_name, const char *aspects); static Bytes hash(const Identity& identity, const char *app_name, const char *aspects);
static std::string expand_name(const Identity &identity, const char *app_name, const char *aspects); static std::string expand_name(const Identity& identity, const char *app_name, const char *aspects);
public: public:
//Packet announce(const Bytes &app_data = {}, bool path_response = false, const Interface &attached_interface = {Type::NONE}, const Bytes &tag = {}, bool send = true); //Packet announce(const Bytes& app_data = {}, bool path_response = false, const Interface& attached_interface = {Type::NONE}, const Bytes& tag = {}, bool send = true);
Packet announce(const Bytes &app_data, bool path_response, const Interface &attached_interface, const Bytes &tag = {}, bool send = true); Packet announce(const Bytes& app_data, bool path_response, const Interface& attached_interface, const Bytes& tag = {}, bool send = true);
Packet announce(const Bytes &app_data = {}, bool path_response = false); Packet announce(const Bytes& app_data = {}, bool path_response = false);
/* /*
Set or query whether the destination accepts incoming link requests. Set or query whether the destination accepts incoming link requests.
@ -141,31 +141,31 @@ namespace RNS {
_object->_proof_strategy = proof_strategy; _object->_proof_strategy = proof_strategy;
} }
void receive(const Packet &packet); void receive(const Packet& packet);
void incoming_link_request(const Bytes &data, const Packet &packet); void incoming_link_request(const Bytes& data, const Packet& packet);
const Bytes encrypt(const Bytes &data); const Bytes encrypt(const Bytes& data);
const Bytes decrypt(const Bytes &data); const Bytes decrypt(const Bytes& data);
const Bytes sign(const Bytes &message); const Bytes sign(const Bytes& message);
// getters/setters // getters/setters
inline Type::Destination::types type() const { assert(_object); return _object->_type; } inline Type::Destination::types type() const { assert(_object); return _object->_type; }
inline Type::Destination::directions direction() const { assert(_object); return _object->_direction; } inline Type::Destination::directions direction() const { assert(_object); return _object->_direction; }
inline Type::Destination::proof_strategies proof_strategy() const { assert(_object); return _object->_proof_strategy; } inline Type::Destination::proof_strategies proof_strategy() const { assert(_object); return _object->_proof_strategy; }
inline const Bytes &hash() const { assert(_object); return _object->_hash; } inline const Bytes& hash() const { assert(_object); return _object->_hash; }
inline const Bytes &link_id() const { assert(_object); return _object->_link_id; } inline const Bytes& link_id() const { assert(_object); return _object->_link_id; }
inline uint16_t mtu() const { assert(_object); return _object->_mtu; } inline uint16_t mtu() const { assert(_object); return _object->_mtu; }
inline void mtu(uint16_t mtu) { assert(_object); _object->_mtu = mtu; } inline void mtu(uint16_t mtu) { assert(_object); _object->_mtu = mtu; }
inline Type::Link::status status() const { assert(_object); return _object->_status; } inline Type::Link::status status() const { assert(_object); return _object->_status; }
inline const Callbacks &callbacks() const { assert(_object); return _object->_callbacks; } inline const Callbacks& callbacks() const { assert(_object); return _object->_callbacks; }
inline const Identity &identity() const { assert(_object); return _object->_identity; } inline const Identity& identity() const { assert(_object); return _object->_identity; }
inline std::string toString() const { assert(_object); return "{Destination:" + _object->_hash.toHex() + "}"; } inline std::string toString() const { assert(_object); return "{Destination:" + _object->_hash.toHex() + "}"; }
private: private:
class Object { class Object {
public: public:
Object(const Identity &identity) : _identity(identity) {} Object(const Identity& identity) : _identity(identity) {}
virtual ~Object() {} virtual ~Object() {}
private: private:
bool _accept_link_requests = true; bool _accept_link_requests = true;

View file

@ -50,7 +50,7 @@ void Identity::createKeys() {
} }
/*static*/ bool Identity::validate_announce(const Packet &packet) { /*static*/ bool Identity::validate_announce(const Packet& packet) {
/* /*
try: try:
if packet.packet_type == RNS.Packet.ANNOUNCE: if packet.packet_type == RNS.Packet.ANNOUNCE:
@ -132,7 +132,7 @@ Encrypts information for the identity.
:returns: Ciphertext token as *bytes*. :returns: Ciphertext token as *bytes*.
:raises: *KeyError* if the instance does not hold a public key. :raises: *KeyError* if the instance does not hold a public key.
*/ */
const Bytes Identity::encrypt(const Bytes &plaintext) { const Bytes Identity::encrypt(const Bytes& plaintext) {
assert(_object); assert(_object);
debug("Identity::encrypt: encrypting data..."); debug("Identity::encrypt: encrypting data...");
if (!_object->_pub) { if (!_object->_pub) {
@ -172,7 +172,7 @@ Decrypts information for the identity.
:returns: Plaintext as *bytes*, or *None* if decryption fails. :returns: Plaintext as *bytes*, or *None* if decryption fails.
:raises: *KeyError* if the instance does not hold a private key. :raises: *KeyError* if the instance does not hold a private key.
*/ */
const Bytes Identity::decrypt(const Bytes &ciphertext_token) { const Bytes Identity::decrypt(const Bytes& ciphertext_token) {
assert(_object); assert(_object);
debug("Identity::decrypt: decrypting data..."); debug("Identity::decrypt: decrypting data...");
if (!_object->_prv) { if (!_object->_prv) {
@ -212,7 +212,7 @@ const Bytes Identity::decrypt(const Bytes &ciphertext_token) {
extreme("Identity::decrypt: plaintext: " + plaintext.toHex()); extreme("Identity::decrypt: plaintext: " + plaintext.toHex());
debug("Identity::decrypt: Fernet decrypted data of length " + std::to_string(plaintext.size())); debug("Identity::decrypt: Fernet decrypted data of length " + std::to_string(plaintext.size()));
} }
catch (std::exception &e) { catch (std::exception& e) {
debug("Decryption by " + toString() + " failed: " + e.what()); debug("Decryption by " + toString() + " failed: " + e.what());
} }
@ -226,7 +226,7 @@ Signs information by the identity.
:returns: Signature as *bytes*. :returns: Signature as *bytes*.
:raises: *KeyError* if the instance does not hold a private key. :raises: *KeyError* if the instance does not hold a private key.
*/ */
const Bytes Identity::sign(const Bytes &message) { const Bytes Identity::sign(const Bytes& message) {
assert(_object); assert(_object);
if (!_object->_sig_prv) { if (!_object->_sig_prv) {
throw std::runtime_error("Signing failed because identity does not hold a private key"); throw std::runtime_error("Signing failed because identity does not hold a private key");
@ -234,7 +234,7 @@ const Bytes Identity::sign(const Bytes &message) {
try { try {
return _object->_sig_prv->sign(message); return _object->_sig_prv->sign(message);
} }
catch (std::exception &e) { catch (std::exception& e) {
error("The identity " + toString() + " could not sign the requested message. The contained exception was: " + e.what()); error("The identity " + toString() + " could not sign the requested message. The contained exception was: " + e.what());
throw e; throw e;
} }
@ -248,14 +248,14 @@ Validates the signature of a signed message.
:returns: True if the signature is valid, otherwise False. :returns: True if the signature is valid, otherwise False.
:raises: *KeyError* if the instance does not hold a public key. :raises: *KeyError* if the instance does not hold a public key.
*/ */
bool Identity::validate(const Bytes &signature, const Bytes &message) { bool Identity::validate(const Bytes& signature, const Bytes& message) {
assert(_object); assert(_object);
if (_object->_pub) { if (_object->_pub) {
try { try {
_object->_sig_pub->verify(signature, message); _object->_sig_pub->verify(signature, message);
return true; return true;
} }
catch (std::exception &e) { catch (std::exception& e) {
return false; return false;
} }
} }
@ -264,7 +264,7 @@ bool Identity::validate(const Bytes &signature, const Bytes &message) {
} }
} }
void Identity::prove(const Packet &packet, const Destination &destination /*= {Type::NONE}*/) { void Identity::prove(const Packet& packet, const Destination& destination /*= {Type::NONE}*/) {
assert(_object); assert(_object);
Bytes signature(sign(packet.packet_hash())); Bytes signature(sign(packet.packet_hash()));
Bytes proof_data; Bytes proof_data;
@ -283,6 +283,6 @@ void Identity::prove(const Packet &packet, const Destination &destination /*= {T
proof.send(); proof.send();
} }
void Identity::prove(const Packet &packet) { void Identity::prove(const Packet& packet) {
prove(packet, {Type::NONE}); prove(packet, {Type::NONE});
} }

View file

@ -23,7 +23,7 @@ namespace RNS {
Identity(Type::NoneConstructor none) { Identity(Type::NoneConstructor none) {
extreme("Identity NONE object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Identity NONE object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
} }
Identity(const Identity &identity) : _object(identity._object) { Identity(const Identity& identity) : _object(identity._object) {
extreme("Identity object copy created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Identity object copy created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
} }
Identity(bool create_keys = true); Identity(bool create_keys = true);
@ -31,7 +31,7 @@ namespace RNS {
extreme("Identity object destroyed, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Identity object destroyed, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
} }
inline Identity& operator = (const Identity &identity) { inline Identity& operator = (const Identity& identity) {
_object = identity._object; _object = identity._object;
extreme("Identity object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Identity object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
return *this; return *this;
@ -39,7 +39,7 @@ namespace RNS {
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const Identity &identity) const { inline bool operator < (const Identity& identity) const {
return _object.get() < identity._object.get(); return _object.get() < identity._object.get();
} }
@ -65,7 +65,7 @@ namespace RNS {
:param data: Data to be hashed as *bytes*. :param data: Data to be hashed as *bytes*.
:returns: SHA-256 hash as *bytes* :returns: SHA-256 hash as *bytes*
*/ */
static inline const Bytes full_hash(const Bytes &data) { static inline const Bytes full_hash(const Bytes& data) {
return Cryptography::sha256(data); return Cryptography::sha256(data);
} }
/* /*
@ -74,7 +74,7 @@ namespace RNS {
:param data: Data to be hashed as *bytes*. :param data: Data to be hashed as *bytes*.
:returns: Truncated SHA-256 hash as *bytes* :returns: Truncated SHA-256 hash as *bytes*
*/ */
static inline const Bytes truncated_hash(const Bytes &data) { static inline const Bytes truncated_hash(const Bytes& data) {
//return Identity.full_hash(data)[:(Identity.TRUNCATED_HASHLENGTH//8)] //return Identity.full_hash(data)[:(Identity.TRUNCATED_HASHLENGTH//8)]
return full_hash(data).left(Type::Identity::TRUNCATED_HASHLENGTH/8); return full_hash(data).left(Type::Identity::TRUNCATED_HASHLENGTH/8);
} }
@ -88,26 +88,26 @@ namespace RNS {
return truncated_hash(Cryptography::random(Type::Identity::TRUNCATED_HASHLENGTH/8)); return truncated_hash(Cryptography::random(Type::Identity::TRUNCATED_HASHLENGTH/8));
} }
static bool validate_announce(const Packet &packet); static bool validate_announce(const Packet& packet);
inline const Bytes get_salt() { assert(_object); return _object->_hash; } inline const Bytes get_salt() { assert(_object); return _object->_hash; }
inline const Bytes get_context() { return {Bytes::NONE}; } inline const Bytes get_context() { return {Bytes::NONE}; }
const Bytes encrypt(const Bytes &plaintext); const Bytes encrypt(const Bytes& plaintext);
const Bytes decrypt(const Bytes &ciphertext_token); const Bytes decrypt(const Bytes& ciphertext_token);
const Bytes sign(const Bytes &message); const Bytes sign(const Bytes& message);
bool validate(const Bytes &signature, const Bytes &message); bool validate(const Bytes& signature, const Bytes& message);
// CBA following default for reference value requires inclusiion of header // CBA following default for reference value requires inclusiion of header
//void prove(const Packet &packet, const Destination &destination = {Type::NONE}); //void prove(const Packet& packet, const Destination& destination = {Type::NONE});
void prove(const Packet &packet, const Destination &destination); void prove(const Packet& packet, const Destination& destination);
void prove(const Packet &packet); void prove(const Packet& packet);
// getters/setters // getters/setters
inline const Bytes &encryptionPrivateKey() const { assert(_object); return _object->_prv_bytes; } inline const Bytes& encryptionPrivateKey() const { assert(_object); return _object->_prv_bytes; }
inline const Bytes &signingPrivateKey() const { assert(_object); return _object->_sig_prv_bytes; } inline const Bytes& signingPrivateKey() const { assert(_object); return _object->_sig_prv_bytes; }
inline const Bytes &encryptionPublicKey() const { assert(_object); return _object->_prv_bytes; } inline const Bytes& encryptionPublicKey() const { assert(_object); return _object->_prv_bytes; }
inline const Bytes &signingPublicKey() const { assert(_object); return _object->_sig_prv_bytes; } inline const Bytes& signingPublicKey() const { assert(_object); return _object->_sig_prv_bytes; }
inline const Bytes &hash() const { assert(_object); return _object->_hash; } inline const Bytes& hash() const { assert(_object); return _object->_hash; }
inline std::string hexhash() const { assert(_object); return _object->_hexhash; } inline std::string hexhash() const { assert(_object); return _object->_hexhash; }
inline std::string toString() const { assert(_object); return "{Identity:" + _object->_hash.toHex() + "}"; } inline std::string toString() const { assert(_object); return "{Identity:" + _object->_hash.toHex() + "}"; }

View file

@ -6,7 +6,7 @@
using namespace RNS; using namespace RNS;
using namespace RNS::Type::Interface; using namespace RNS::Type::Interface;
/*virtual*/ inline void Interface::processIncoming(const Bytes &data) { /*virtual*/ inline void Interface::processIncoming(const Bytes& data) {
extreme("Interface::processIncoming: data: " + data.toHex()); extreme("Interface::processIncoming: data: " + data.toHex());
assert(_object); assert(_object);
_object->_rxb += data.size(); _object->_rxb += data.size();
@ -15,7 +15,7 @@ using namespace RNS::Type::Interface;
Transport::inbound(data, *this); Transport::inbound(data, *this);
} }
/*virtual*/ inline void Interface::processOutgoing(const Bytes &data) { /*virtual*/ inline void Interface::processOutgoing(const Bytes& data) {
extreme("Interface::processOutgoing: data: " + data.toHex()); extreme("Interface::processOutgoing: data: " + data.toHex());
assert(_object); assert(_object);
_object->_txb += data.size(); _object->_txb += data.size();

View file

@ -16,7 +16,7 @@ namespace RNS {
class AnnounceEntry { class AnnounceEntry {
public: public:
AnnounceEntry() {} AnnounceEntry() {}
AnnounceEntry(const Bytes &destination, uint64_t time, uint8_t hops, uint64_t emitted, const Bytes &raw) : AnnounceEntry(const Bytes& destination, uint64_t time, uint8_t hops, uint64_t emitted, const Bytes& raw) :
_destination(destination), _destination(destination),
_time(time), _time(time),
_hops(hops), _hops(hops),
@ -39,7 +39,7 @@ namespace RNS {
Interface(Type::NoneConstructor none) { Interface(Type::NoneConstructor none) {
extreme("Interface object NONE created"); extreme("Interface object NONE created");
} }
Interface(const Interface &interface) : _object(interface._object) { Interface(const Interface& interface) : _object(interface._object) {
extreme("Interface object copy created"); extreme("Interface object copy created");
} }
Interface() : _object(new Object()) { Interface() : _object(new Object()) {
@ -52,7 +52,7 @@ namespace RNS {
extreme("Interface object destroyed"); extreme("Interface object destroyed");
} }
inline Interface& operator = (const Interface &interface) { inline Interface& operator = (const Interface& interface) {
_object = interface._object; _object = interface._object;
extreme("Interface object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Interface object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
return *this; return *this;
@ -60,7 +60,7 @@ namespace RNS {
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const Interface &interface) const { inline bool operator < (const Interface& interface) const {
return _object.get() < interface._object.get(); return _object.get() < interface._object.get();
} }
@ -69,10 +69,10 @@ namespace RNS {
void process_announce_queue(); void process_announce_queue();
inline void detach() {} inline void detach() {}
virtual void processIncoming(const Bytes &data); virtual void processIncoming(const Bytes& data);
virtual void processOutgoing(const Bytes &data); virtual void processOutgoing(const Bytes& data);
inline void add_announce(AnnounceEntry &entry) { assert(_object); _object->_announce_queue.push_back(entry); } inline void add_announce(AnnounceEntry& entry) { assert(_object); _object->_announce_queue.push_back(entry); }
// getters/setters // getters/setters
protected: protected:
@ -87,13 +87,13 @@ namespace RNS {
inline bool FWD() const { assert(_object); return _object->_FWD; } inline bool FWD() const { assert(_object); return _object->_FWD; }
inline bool RPT() const { assert(_object); return _object->_RPT; } inline bool RPT() const { assert(_object); return _object->_RPT; }
inline std::string name() const { assert(_object); return _object->_name; } inline std::string name() const { assert(_object); return _object->_name; }
inline const Bytes &ifac_identity() const { assert(_object); return _object->_ifac_identity; } inline const Bytes& ifac_identity() const { assert(_object); return _object->_ifac_identity; }
inline Type::Interface::modes mode() const { assert(_object); return _object->_mode; } inline Type::Interface::modes mode() const { assert(_object); return _object->_mode; }
inline uint32_t bitrate() const { assert(_object); return _object->_bitrate; } inline uint32_t bitrate() const { assert(_object); return _object->_bitrate; }
inline uint64_t announce_allowed_at() const { assert(_object); return _object->_announce_allowed_at; } inline uint64_t announce_allowed_at() const { assert(_object); return _object->_announce_allowed_at; }
inline void announce_allowed_at(uint64_t announce_allowed_at) { assert(_object); _object->_announce_allowed_at = announce_allowed_at; } inline void announce_allowed_at(uint64_t announce_allowed_at) { assert(_object); _object->_announce_allowed_at = announce_allowed_at; }
inline float announce_cap() const { assert(_object); return _object->_announce_cap; } inline float announce_cap() const { assert(_object); return _object->_announce_cap; }
inline std::list<AnnounceEntry> &announce_queue() const { assert(_object); return _object->_announce_queue; } inline std::list<AnnounceEntry>& announce_queue() const { assert(_object); return _object->_announce_queue; }
virtual inline std::string toString() const { assert(_object); return "Interface[" + _object->_name + "]"; } virtual inline std::string toString() const { assert(_object); return "Interface[" + _object->_name + "]"; }
@ -118,7 +118,7 @@ namespace RNS {
uint64_t _announce_allowed_at = 0; uint64_t _announce_allowed_at = 0;
float _announce_cap = 0.0; float _announce_cap = 0.0;
std::list<AnnounceEntry> _announce_queue; std::list<AnnounceEntry> _announce_queue;
//Transport &_owner; //Transport& _owner;
friend class Interface; friend class Interface;
}; };
std::shared_ptr<Object> _object; std::shared_ptr<Object> _object;

View file

@ -13,11 +13,11 @@ Link::Link() : _object(new Object()) {
} }
void Link::set_link_id(const Packet &packet) { void Link::set_link_id(const Packet& packet) {
assert(_object); assert(_object);
_object->_link_id = packet.getTruncatedHash(); _object->_link_id = packet.getTruncatedHash();
_object->_hash = _object->_link_id; _object->_hash = _object->_link_id;
} }
void Link::receive(const Packet &packet) { void Link::receive(const Packet& packet) {
} }

View file

@ -22,7 +22,7 @@ namespace RNS {
Link(Type::NoneConstructor none) { Link(Type::NoneConstructor none) {
extreme("Link NONE object created"); extreme("Link NONE object created");
} }
Link(const Link &link) : _object(link._object) { Link(const Link& link) : _object(link._object) {
extreme("Link object copy created"); extreme("Link object copy created");
} }
Link(); Link();
@ -30,24 +30,24 @@ namespace RNS {
extreme("Link object destroyed"); extreme("Link object destroyed");
} }
inline Link& operator = (const Link &link) { inline Link& operator = (const Link& link) {
_object = link._object; _object = link._object;
return *this; return *this;
} }
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const Link &link) const { inline bool operator < (const Link& link) const {
return _object.get() < link._object.get(); return _object.get() < link._object.get();
} }
public: public:
void set_link_id(const Packet &packet); void set_link_id(const Packet& packet);
void receive(const Packet &packet); void receive(const Packet& packet);
// getters/setters // getters/setters
inline const Bytes &link_id() const { assert(_object); return _object->_link_id; } inline const Bytes& link_id() const { assert(_object); return _object->_link_id; }
inline const Bytes &hash() const { assert(_object); return _object->_hash; } inline const Bytes& hash() const { assert(_object); return _object->_hash; }
inline std::string toString() const { assert(_object); return "{Link: unknown}"; } inline std::string toString() const { assert(_object); return "{Link: unknown}"; }

View file

@ -28,33 +28,33 @@ namespace RNS {
#ifndef NATIVE #ifndef NATIVE
inline void log(const String msg, LogLevel level = LOG_NOTICE) { doLog(msg.c_str(), level); } inline void log(const String msg, LogLevel level = LOG_NOTICE) { doLog(msg.c_str(), level); }
#endif #endif
inline void log(const std::string &msg, LogLevel level = LOG_NOTICE) { doLog(msg.c_str(), level); } inline void log(const std::string& msg, LogLevel level = LOG_NOTICE) { doLog(msg.c_str(), level); }
inline void critical(const char *msg) { doLog(msg, LOG_CRITICAL); } inline void critical(const char *msg) { doLog(msg, LOG_CRITICAL); }
inline void critical(const std::string &msg) { doLog(msg.c_str(), LOG_CRITICAL); } inline void critical(const std::string& msg) { doLog(msg.c_str(), LOG_CRITICAL); }
inline void error(const char *msg) { doLog(msg, LOG_ERROR); } inline void error(const char *msg) { doLog(msg, LOG_ERROR); }
inline void error(const std::string &msg) { doLog(msg.c_str(), LOG_ERROR); } inline void error(const std::string& msg) { doLog(msg.c_str(), LOG_ERROR); }
inline void warning(const char *msg) { doLog(msg, LOG_WARNING); } inline void warning(const char *msg) { doLog(msg, LOG_WARNING); }
inline void warning(const std::string &msg) { doLog(msg.c_str(), LOG_WARNING); } inline void warning(const std::string& msg) { doLog(msg.c_str(), LOG_WARNING); }
inline void notice(const char *msg) { doLog(msg, LOG_NOTICE); } inline void notice(const char *msg) { doLog(msg, LOG_NOTICE); }
inline void notice(const std::string &msg) { doLog(msg.c_str(), LOG_NOTICE); } inline void notice(const std::string& msg) { doLog(msg.c_str(), LOG_NOTICE); }
inline void info(const char *msg) { doLog(msg, LOG_INFO); } inline void info(const char *msg) { doLog(msg, LOG_INFO); }
inline void info(const std::string &msg) { doLog(msg.c_str(), LOG_INFO); } inline void info(const std::string& msg) { doLog(msg.c_str(), LOG_INFO); }
inline void verbose(const char *msg) { doLog(msg, LOG_VERBOSE); } inline void verbose(const char *msg) { doLog(msg, LOG_VERBOSE); }
inline void verbose(const std::string &msg) { doLog(msg.c_str(), LOG_VERBOSE); } inline void verbose(const std::string& msg) { doLog(msg.c_str(), LOG_VERBOSE); }
inline void debug(const char *msg) { doLog(msg, LOG_DEBUG); } inline void debug(const char *msg) { doLog(msg, LOG_DEBUG); }
inline void debug(const std::string &msg) { doLog(msg.c_str(), LOG_DEBUG); } inline void debug(const std::string& msg) { doLog(msg.c_str(), LOG_DEBUG); }
inline void extreme(const char *msg) { doLog(msg, LOG_EXTREME); } inline void extreme(const char *msg) { doLog(msg, LOG_EXTREME); }
inline void extreme(const std::string &msg) { doLog(msg.c_str(), LOG_EXTREME); } inline void extreme(const std::string& msg) { doLog(msg.c_str(), LOG_EXTREME); }
void head(const char *msg, LogLevel level = LOG_NOTICE); void head(const char *msg, LogLevel level = LOG_NOTICE);
inline void head(const std::string &msg, LogLevel level = LOG_NOTICE) { head(msg.c_str(), level); } inline void head(const std::string& msg, LogLevel level = LOG_NOTICE) { head(msg.c_str(), level); }
} }

View file

@ -11,7 +11,7 @@ using namespace RNS;
using namespace RNS::Type::PacketReceipt; using namespace RNS::Type::PacketReceipt;
using namespace RNS::Type::Packet; using namespace RNS::Type::Packet;
Packet::Packet(const Destination &destination, const Interface &attached_interface, const Bytes &data, types packet_type /*= DATA*/, context_types context /*= CONTEXT_NONE*/, Type::Transport::types transport_type /*= Type::Transport::BROADCAST*/, header_types header_type /*= HEADER_1*/, const Bytes &transport_id /*= {Bytes::NONE}*/, bool create_receipt /*= true*/) : _object(new Object(destination, attached_interface)) { Packet::Packet(const Destination& destination, const Interface& attached_interface, const Bytes& data, types packet_type /*= DATA*/, context_types context /*= CONTEXT_NONE*/, Type::Transport::types transport_type /*= Type::Transport::BROADCAST*/, header_types header_type /*= HEADER_1*/, const Bytes& transport_id /*= {Bytes::NONE}*/, bool create_receipt /*= true*/) : _object(new Object(destination, attached_interface)) {
if (_object->_destination) { if (_object->_destination) {
extreme("Creating packet with destination..."); extreme("Creating packet with destination...");
@ -444,7 +444,7 @@ bool Packet::resend() {
} }
} }
void Packet::prove(const Destination &destination /*= {Type::NONE}*/) { void Packet::prove(const Destination& destination /*= {Type::NONE}*/) {
/* /*
assert(_object); assert(_object);
if (_object->_fromPacked && _object->_destination) { if (_object->_fromPacked && _object->_destination) {
@ -495,7 +495,7 @@ const Bytes Packet::get_hashable_part() const {
// Generates a special destination that allows Reticulum // Generates a special destination that allows Reticulum
// to direct the proof back to the proved packet's sender // to direct the proof back to the proved packet's sender
//ProofDestination &Packet::generate_proof_destination() { //ProofDestination& Packet::generate_proof_destination() {
// return ProofDestination(); // return ProofDestination();
//} //}

View file

@ -31,8 +31,8 @@ namespace RNS {
public: public:
class Callbacks { class Callbacks {
public: public:
using delivery = void(*)(const PacketReceipt &packet_receipt); using delivery = void(*)(const PacketReceipt& packet_receipt);
using timeout = void(*)(const PacketReceipt &packet_receipt); using timeout = void(*)(const PacketReceipt& packet_receipt);
public: public:
delivery _delivery = nullptr; delivery _delivery = nullptr;
timeout _timeout = nullptr; timeout _timeout = nullptr;
@ -41,18 +41,18 @@ namespace RNS {
public: public:
PacketReceipt(Type::NoneConstructor none) {} PacketReceipt(Type::NoneConstructor none) {}
PacketReceipt(const PacketReceipt &packet_receipt) : _object(packet_receipt._object) {} PacketReceipt(const PacketReceipt& packet_receipt) : _object(packet_receipt._object) {}
PacketReceipt() : _object(new Object()) {} PacketReceipt() : _object(new Object()) {}
PacketReceipt(const Packet &packet) {} PacketReceipt(const Packet& packet) {}
inline PacketReceipt& operator = (const PacketReceipt &packet_receipt) { inline PacketReceipt& operator = (const PacketReceipt& packet_receipt) {
_object = packet_receipt._object; _object = packet_receipt._object;
return *this; return *this;
} }
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const PacketReceipt &packet_receipt) const { inline bool operator < (const PacketReceipt& packet_receipt) const {
return _object.get() < packet_receipt._object.get(); return _object.get() < packet_receipt._object.get();
} }
@ -126,14 +126,14 @@ namespace RNS {
Packet(Type::NoneConstructor none) { Packet(Type::NoneConstructor none) {
extreme("Packet NONE object created"); extreme("Packet NONE object created");
} }
Packet(const Packet &packet) : _object(packet._object) { Packet(const Packet& packet) : _object(packet._object) {
extreme("Packet object copy created"); extreme("Packet object copy created");
} }
Packet(const Destination &destination, const Interface &attached_interface, const Bytes &data, Type::Packet::types packet_type = Type::Packet::DATA, Type::Packet::context_types context = Type::Packet::CONTEXT_NONE, Type::Transport::types transport_type = Type::Transport::BROADCAST, Type::Packet::header_types header_type = Type::Packet::HEADER_1, const Bytes &transport_id = {Bytes::NONE}, bool create_receipt = true); Packet(const Destination& destination, const Interface& attached_interface, const Bytes& data, Type::Packet::types packet_type = Type::Packet::DATA, Type::Packet::context_types context = Type::Packet::CONTEXT_NONE, Type::Transport::types transport_type = Type::Transport::BROADCAST, Type::Packet::header_types header_type = Type::Packet::HEADER_1, const Bytes& transport_id = {Bytes::NONE}, bool create_receipt = true);
Packet(const Destination &destination, const Bytes &data, Type::Packet::types packet_type = Type::Packet::DATA, Type::Packet::context_types context = Type::Packet::CONTEXT_NONE, Type::Transport::types transport_type = Type::Transport::BROADCAST, Type::Packet::header_types header_type = Type::Packet::HEADER_1, const Bytes &transport_id = {Bytes::NONE}, bool create_receipt = true) : Packet(destination, {Type::NONE}, data, packet_type, context, transport_type, header_type, transport_id, create_receipt) {} Packet(const Destination& destination, const Bytes& data, Type::Packet::types packet_type = Type::Packet::DATA, Type::Packet::context_types context = Type::Packet::CONTEXT_NONE, Type::Transport::types transport_type = Type::Transport::BROADCAST, Type::Packet::header_types header_type = Type::Packet::HEADER_1, const Bytes& transport_id = {Bytes::NONE}, bool create_receipt = true) : Packet(destination, {Type::NONE}, data, packet_type, context, transport_type, header_type, transport_id, create_receipt) {}
virtual ~Packet(); virtual ~Packet();
inline Packet& operator = (const Packet &packet) { inline Packet& operator = (const Packet& packet) {
_object = packet._object; _object = packet._object;
extreme("Packet object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Packet object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
return *this; return *this;
@ -141,7 +141,7 @@ namespace RNS {
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const Packet &packet) const { inline bool operator < (const Packet& packet) const {
return _object.get() < packet._object.get(); return _object.get() < packet._object.get();
} }
@ -160,21 +160,21 @@ namespace RNS {
bool unpack(); bool unpack();
bool send(); bool send();
bool resend(); bool resend();
void prove(const Destination &destination = {Type::NONE}); void prove(const Destination& destination = {Type::NONE});
void update_hash(); void update_hash();
const Bytes get_hash() const; const Bytes get_hash() const;
const Bytes getTruncatedHash() const; const Bytes getTruncatedHash() const;
const Bytes get_hashable_part() const; const Bytes get_hashable_part() const;
//zProofDestination &generate_proof_destination(); //zProofDestination& generate_proof_destination();
// getters/setters // getters/setters
inline const Destination &destination() const { assert(_object); return _object->_destination; } inline const Destination& destination() const { assert(_object); return _object->_destination; }
inline void destination(const Destination &destination) { assert(_object); _object->_destination = destination; } inline void destination(const Destination& destination) { assert(_object); _object->_destination = destination; }
inline const Link &link() const { assert(_object); return _object->_link; } inline const Link& link() const { assert(_object); return _object->_link; }
inline void link(const Link &link) { assert(_object); _object->_link = link; } inline void link(const Link& link) { assert(_object); _object->_link = link; }
inline const Interface &attached_interface() const { assert(_object); return _object->_attached_interface; } inline const Interface& attached_interface() const { assert(_object); return _object->_attached_interface; }
inline const Interface &receiving_interface() const { assert(_object); return _object->_receiving_interface; } inline const Interface& receiving_interface() const { assert(_object); return _object->_receiving_interface; }
inline void receiving_interface(const Interface &receiving_interface) { assert(_object); _object->_receiving_interface = receiving_interface; } inline void receiving_interface(const Interface& receiving_interface) { assert(_object); _object->_receiving_interface = receiving_interface; }
inline Type::Packet::header_types header_type() const { assert(_object); return _object->_header_type; } inline Type::Packet::header_types header_type() const { assert(_object); return _object->_header_type; }
inline Type::Transport::types transport_type() const { assert(_object); return _object->_transport_type; } inline Type::Transport::types transport_type() const { assert(_object); return _object->_transport_type; }
inline Type::Destination::types destination_type() const { assert(_object); return _object->_destination_type; } inline Type::Destination::types destination_type() const { assert(_object); return _object->_destination_type; }
@ -185,17 +185,17 @@ namespace RNS {
inline time_t sent_at() const { assert(_object); return _object->_sent_at; } inline time_t sent_at() const { assert(_object); return _object->_sent_at; }
inline void sent_at(time_t sent_at) { assert(_object); _object->_sent_at = sent_at; } inline void sent_at(time_t sent_at) { assert(_object); _object->_sent_at = sent_at; }
inline bool create_receipt() const { assert(_object); return _object->_create_receipt; } inline bool create_receipt() const { assert(_object); return _object->_create_receipt; }
inline const PacketReceipt &receipt() const { assert(_object); return _object->_receipt; } inline const PacketReceipt& receipt() const { assert(_object); return _object->_receipt; }
inline void receipt(const PacketReceipt &receipt) { assert(_object); _object->_receipt = receipt; } inline void receipt(const PacketReceipt& receipt) { assert(_object); _object->_receipt = receipt; }
inline uint8_t flags() const { assert(_object); return _object->_flags; } inline uint8_t flags() const { assert(_object); return _object->_flags; }
inline uint8_t hops() const { assert(_object); return _object->_hops; } inline uint8_t hops() const { assert(_object); return _object->_hops; }
inline void hops(uint8_t hops) { assert(_object); _object->_hops = hops; } inline void hops(uint8_t hops) { assert(_object); _object->_hops = hops; }
inline const Bytes &packet_hash() const { assert(_object); return _object->_packet_hash; } inline const Bytes& packet_hash() const { assert(_object); return _object->_packet_hash; }
inline const Bytes &destination_hash() const { assert(_object); return _object->_destination_hash; } inline const Bytes& destination_hash() const { assert(_object); return _object->_destination_hash; }
inline const Bytes &transport_id() const { assert(_object); return _object->_transport_id; } inline const Bytes& transport_id() const { assert(_object); return _object->_transport_id; }
inline void transport_id(const Bytes &transport_id) { assert(_object); _object->_transport_id = transport_id; } inline void transport_id(const Bytes& transport_id) { assert(_object); _object->_transport_id = transport_id; }
inline const Bytes &raw() const { assert(_object); return _object->_raw; } inline const Bytes& raw() const { assert(_object); return _object->_raw; }
inline const Bytes &data() const { assert(_object); return _object->_data; } inline const Bytes& data() const { assert(_object); return _object->_data; }
inline std::string toString() const { assert(_object); return "{Packet:" + _object->_packet_hash.toHex() + "}"; } inline std::string toString() const { assert(_object); return "{Packet:" + _object->_packet_hash.toHex() + "}"; }
@ -204,7 +204,7 @@ namespace RNS {
private: private:
class Object { class Object {
public: public:
Object(const Destination &destination, const Interface &attached_interface) : _destination(destination), _attached_interface(attached_interface) {} Object(const Destination& destination, const Interface& attached_interface) : _destination(destination), _attached_interface(attached_interface) {}
virtual ~Object() {} virtual ~Object() {}
private: private:
Destination _destination = {Type::NONE}; Destination _destination = {Type::NONE};

View file

@ -21,13 +21,13 @@ namespace RNS {
Reticulum(Type::NoneConstructor none) { Reticulum(Type::NoneConstructor none) {
extreme("Reticulum NONE object created"); extreme("Reticulum NONE object created");
} }
Reticulum(const Reticulum &reticulum) : _object(reticulum._object) { Reticulum(const Reticulum& reticulum) : _object(reticulum._object) {
extreme("Reticulum object copy created"); extreme("Reticulum object copy created");
} }
Reticulum(); Reticulum();
virtual ~Reticulum(); virtual ~Reticulum();
inline Reticulum& operator = (const Reticulum &reticulum) { inline Reticulum& operator = (const Reticulum& reticulum) {
_object = reticulum._object; _object = reticulum._object;
extreme("Reticulum object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); extreme("Reticulum object copy created by assignment, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
return *this; return *this;
@ -35,7 +35,7 @@ namespace RNS {
inline operator bool() const { inline operator bool() const {
return _object.get() != nullptr; return _object.get() != nullptr;
} }
inline bool operator < (const Reticulum &reticulum) const { inline bool operator < (const Reticulum& reticulum) const {
return _object.get() < reticulum._object.get(); return _object.get() < reticulum._object.get();
} }

View file

@ -6,15 +6,15 @@
#include <map> #include <map>
#include <assert.h> #include <assert.h>
//void testBytesDefault(const RNS::Bytes &bytes = {RNS::Bytes::NONE}) { //void testBytesDefault(const RNS::Bytes& bytes = {RNS::Bytes::NONE}) {
void testBytesDefault(const RNS::Bytes &bytes = {}) { void testBytesDefault(const RNS::Bytes& bytes = {}) {
assert(!bytes); assert(!bytes);
assert(bytes.size() == 0); assert(bytes.size() == 0);
assert(bytes.data() == nullptr); assert(bytes.data() == nullptr);
} }
RNS::Bytes ref("Test"); RNS::Bytes ref("Test");
const RNS::Bytes &testBytesReference() { const RNS::Bytes& testBytesReference() {
// NOTE: Can NOT return local instance as reference!!! // NOTE: Can NOT return local instance as reference!!!
//RNS::Bytes ref("Test"); //RNS::Bytes ref("Test");
RNS::extreme("returning..."); RNS::extreme("returning...");

View file

@ -57,7 +57,7 @@ using namespace RNS::Utilities;
/*static*/ Reticulum Transport::_owner({Type::NONE}); /*static*/ Reticulum Transport::_owner({Type::NONE});
/*static*/ Identity Transport::_identity({Type::NONE}); /*static*/ Identity Transport::_identity({Type::NONE});
/*static*/ void Transport::start(const Reticulum &reticulum_instance) { /*static*/ void Transport::start(const Reticulum& reticulum_instance) {
_jobs_running = true; _jobs_running = true;
_owner = reticulum_instance; _owner = reticulum_instance;
@ -86,7 +86,7 @@ using namespace RNS::Utilities;
packet_hashlist = umsgpack.unpackb(file.read()); packet_hashlist = umsgpack.unpackb(file.read());
file.close(); file.close();
} }
catch (std::exception &e) { catch (std::exception& e) {
error("Could not load packet hashlist from storage, the contained exception was: " + e.what()); error("Could not load packet hashlist from storage, the contained exception was: " + e.what());
} }
} }
@ -247,7 +247,7 @@ using namespace RNS::Utilities;
// Process active and pending link lists // Process active and pending link lists
if (time(nullptr) > _links_last_checked + Transport.links_check_interval) { if (time(nullptr) > _links_last_checked + Transport.links_check_interval) {
for (auto &link : _pending_links) { for (auto& link : _pending_links) {
if link.status() == RNS.Link.CLOSED: if link.status() == RNS.Link.CLOSED:
// If we are not a Transport Instance, finding a pending link // If we are not a Transport Instance, finding a pending link
// that was never activated will trigger an expiry of the path // that was never activated will trigger an expiry of the path
@ -270,7 +270,7 @@ using namespace RNS::Utilities;
Transport.pending_links.remove(link) Transport.pending_links.remove(link)
} }
for (auto &link : _active_links) { for (auto& link : _active_links) {
if link.status == RNS.Link.CLOSED: if link.status == RNS.Link.CLOSED:
Transport.active_links.remove(link) Transport.active_links.remove(link)
} }
@ -551,23 +551,23 @@ using namespace RNS::Utilities;
pass pass
*/ */
} }
catch (std::exception &e) { catch (std::exception& e) {
error("An exception occurred while running Transport jobs."); error("An exception occurred while running Transport jobs.");
error("The contained exception was: " + std::string(e.what())); error("The contained exception was: " + std::string(e.what()));
} }
_jobs_running = false; _jobs_running = false;
for (auto &packet : outgoing) { for (auto& packet : outgoing) {
packet.send(); packet.send();
} }
for (auto &destination_hash : path_requests) { for (auto& destination_hash : path_requests) {
request_path(destination_hash); request_path(destination_hash);
} }
} }
/*static*/ void Transport::transmit(Interface &interface, const Bytes &raw) { /*static*/ void Transport::transmit(Interface& interface, const Bytes& raw) {
try { try {
//if hasattr(interface, "ifac_identity") and interface.ifac_identity != None: //if hasattr(interface, "ifac_identity") and interface.ifac_identity != None:
if (interface.ifac_identity()) { if (interface.ifac_identity()) {
@ -612,12 +612,12 @@ using namespace RNS::Utilities;
interface.processOutgoing(raw); interface.processOutgoing(raw);
} }
} }
catch (std::exception &e) { catch (std::exception& e) {
error("Error while transmitting on " + interface.toString() + ". The contained exception was: " + e.what()); error("Error while transmitting on " + interface.toString() + ". The contained exception was: " + e.what());
} }
} }
/*static*/ bool Transport::outbound(Packet &packet) { /*static*/ bool Transport::outbound(Packet& packet) {
extreme("Transport::outbound()"); extreme("Transport::outbound()");
extreme("Transport::outbound: destination=" + packet.destination_hash().toHex() + " hops=" + std::to_string(packet.hops())); extreme("Transport::outbound: destination=" + packet.destination_hash().toHex() + " hops=" + std::to_string(packet.hops()));
@ -638,7 +638,7 @@ using namespace RNS::Utilities;
extreme("Transport::outbound: Path to destination is known"); extreme("Transport::outbound: Path to destination is known");
//outbound_interface = Transport.destination_table[packet.destination_hash][5] //outbound_interface = Transport.destination_table[packet.destination_hash][5]
DestinationEntry destination_entry = (*_destination_table.find(packet.destination_hash())).second; DestinationEntry destination_entry = (*_destination_table.find(packet.destination_hash())).second;
Interface &outbound_interface = destination_entry._receiving_interface; Interface& outbound_interface = destination_entry._receiving_interface;
// If there's more than one hop to the destination, and we know // If there's more than one hop to the destination, and we know
// a path, we insert the packet into transport by adding the next // a path, we insert the packet into transport by adding the next
@ -714,7 +714,7 @@ using namespace RNS::Utilities;
else { else {
extreme("Transport::outbound: Path to destination is unknown"); extreme("Transport::outbound: Path to destination is unknown");
bool stored_hash = false; bool stored_hash = false;
for (const Interface &interface : _interfaces) { for (const Interface& interface : _interfaces) {
extreme("Transport::outbound: Checking interface " + interface.toString()); extreme("Transport::outbound: Checking interface " + interface.toString());
if (interface.OUT()) { if (interface.OUT()) {
bool should_transmit = true; bool should_transmit = true;
@ -744,7 +744,7 @@ using namespace RNS::Utilities;
//local_destination = next((d for d in Transport.destinations if d.hash == packet.destination_hash), None) //local_destination = next((d for d in Transport.destinations if d.hash == packet.destination_hash), None)
//Destination local_destination({Type::NONE}); //Destination local_destination({Type::NONE});
bool found_local = false; bool found_local = false;
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == packet.destination_hash()) { if (destination.hash() == packet.destination_hash()) {
//local_destination = destination; //local_destination = destination;
found_local = true; found_local = true;
@ -757,7 +757,7 @@ using namespace RNS::Utilities;
//extreme("Allowing announce broadcast on roaming-mode interface from instance-local destination"); //extreme("Allowing announce broadcast on roaming-mode interface from instance-local destination");
} }
else { else {
const Interface &from_interface = next_hop_interface(packet.destination_hash()); const Interface& from_interface = next_hop_interface(packet.destination_hash());
//if from_interface == None or not hasattr(from_interface, "mode"): //if from_interface == None or not hasattr(from_interface, "mode"):
if (!from_interface || from_interface.mode() == Type::Interface::MODE_NONE) { if (!from_interface || from_interface.mode() == Type::Interface::MODE_NONE) {
should_transmit = false; should_transmit = false;
@ -788,7 +788,7 @@ using namespace RNS::Utilities;
// CBA TODO confirm that above pattern just selects the first matching destination // CBA TODO confirm that above pattern just selects the first matching destination
//Destination local_destination({Typeestination::NONE}); //Destination local_destination({Typeestination::NONE});
bool found_local = false; bool found_local = false;
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == packet.destination_hash()) { if (destination.hash() == packet.destination_hash()) {
//local_destination = destination; //local_destination = destination;
found_local = true; found_local = true;
@ -801,7 +801,7 @@ using namespace RNS::Utilities;
//extreme("Allowing announce broadcast on boundary-mode interface from instance-local destination"); //extreme("Allowing announce broadcast on boundary-mode interface from instance-local destination");
} }
else { else {
const Interface &from_interface = next_hop_interface(packet.destination_hash()); const Interface& from_interface = next_hop_interface(packet.destination_hash());
if (!from_interface || from_interface.mode() == Type::Interface::MODE_NONE) { if (!from_interface || from_interface.mode() == Type::Interface::MODE_NONE) {
should_transmit = false; should_transmit = false;
if (!from_interface) { if (!from_interface) {
@ -846,7 +846,7 @@ using namespace RNS::Utilities;
should_transmit = false; should_transmit = false;
if (interface.announce_queue().size() < Type::Reticulum::MAX_QUEUED_ANNOUNCES) { if (interface.announce_queue().size() < Type::Reticulum::MAX_QUEUED_ANNOUNCES) {
bool should_queue = true; bool should_queue = true;
for (auto &entry : interface.announce_queue()) { for (auto& entry : interface.announce_queue()) {
if (entry._destination == packet.destination_hash()) { if (entry._destination == packet.destination_hash()) {
uint64_t emission_timestamp = announce_emitted(packet); uint64_t emission_timestamp = announce_emitted(packet);
should_queue = false; should_queue = false;
@ -967,7 +967,7 @@ using namespace RNS::Utilities;
return sent; return sent;
} }
/*static*/ bool Transport::packet_filter(const Packet &packet) { /*static*/ bool Transport::packet_filter(const Packet& packet) {
// TODO: Think long and hard about this. // TODO: Think long and hard about this.
// Is it even strictly necessary with the current // Is it even strictly necessary with the current
// transport rules? // transport rules?
@ -1041,7 +1041,7 @@ using namespace RNS::Utilities;
return false; return false;
} }
/*static*/ void Transport::inbound(const Bytes &raw, const Interface &interface /*= {Type::NONE}*/) { /*static*/ void Transport::inbound(const Bytes& raw, const Interface& interface /*= {Type::NONE}*/) {
extreme("Transport::inbound()"); extreme("Transport::inbound()");
/* /*
// If interface access codes are enabled, // If interface access codes are enabled,
@ -1214,7 +1214,7 @@ using namespace RNS::Utilities;
if (packet.destination_type() == Type::Destination::PLAIN && packet.transport_type() == Type::Transport::BROADCAST) { if (packet.destination_type() == Type::Destination::PLAIN && packet.transport_type() == Type::Transport::BROADCAST) {
// Send to all interfaces except the originator // Send to all interfaces except the originator
if (from_local_client) { if (from_local_client) {
for (const Interface &interface : _interfaces) { for (const Interface& interface : _interfaces) {
if (interface != packet.receiving_interface()) { if (interface != packet.receiving_interface()) {
extreme("Transport::inbound: Broadcasting packet on " + interface.toString()); extreme("Transport::inbound: Broadcasting packet on " + interface.toString());
transmit(const_cast<Interface&>(interface), packet.raw()); transmit(const_cast<Interface&>(interface), packet.raw());
@ -1224,7 +1224,7 @@ using namespace RNS::Utilities;
// If the packet was not from a local client, send // If the packet was not from a local client, send
// it directly to all local clients // it directly to all local clients
else { else {
for (auto &interface : _local_client_interfaces) { for (auto& interface : _local_client_interfaces) {
extreme("Transport::inbound: Broadcasting packet on " + interface.toString()); extreme("Transport::inbound: Broadcasting packet on " + interface.toString());
transmit(const_cast<Interface&>(interface), packet.raw()); transmit(const_cast<Interface&>(interface), packet.raw());
} }
@ -1303,7 +1303,7 @@ using namespace RNS::Utilities;
new_raw << packet.raw().mid(2); new_raw << packet.raw().mid(2);
} }
Interface &outbound_interface = destination_entry._receiving_interface; Interface& outbound_interface = destination_entry._receiving_interface;
if (packet.packet_type() == Type::Packet::LINKREQUEST) { if (packet.packet_type() == Type::Packet::LINKREQUEST) {
uint64_t now = OS::time(); uint64_t now = OS::time();
@ -1403,7 +1403,7 @@ using namespace RNS::Utilities;
//p local_destination = next((d for d in Transport.destinations if d.hash == packet.destination_hash), None) //p local_destination = next((d for d in Transport.destinations if d.hash == packet.destination_hash), None)
//Destination local_destination({Type::NONE}); //Destination local_destination({Type::NONE});
bool found_local = false; bool found_local = false;
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == packet.destination_hash()) { if (destination.hash() == packet.destination_hash()) {
//local_destination = destination; //local_destination = destination;
found_local = true; found_local = true;
@ -1421,8 +1421,8 @@ using namespace RNS::Utilities;
// another node. If it is, we're removing the // another node. If it is, we're removing the
// announce in question from our pending table // announce in question from our pending table
if (Reticulum::transport_enabled() && _announce_table.count(packet.destination_hash()) > 0) { if (Reticulum::transport_enabled() && _announce_table.count(packet.destination_hash()) > 0) {
//AnnounceEntry &announce_entry = _announce_table[packet.destination_hash()]; //AnnounceEntry& announce_entry = _announce_table[packet.destination_hash()];
AnnounceEntry &announce_entry = (*_announce_table.find(packet.destination_hash())).second; AnnounceEntry& announce_entry = (*_announce_table.find(packet.destination_hash())).second;
if ((packet.hops() - 1) == announce_entry._hops) { if ((packet.hops() - 1) == announce_entry._hops) {
debug("Heard a local rebroadcast of announce for " + packet.destination_hash().toHex()); debug("Heard a local rebroadcast of announce for " + packet.destination_hash().toHex());
@ -1454,7 +1454,7 @@ using namespace RNS::Utilities;
// local to this system, and that hops are less than the max // local to this system, and that hops are less than the max
//if (not any(packet.destination_hash == d.hash for d in Transport.destinations) and packet.hops < Transport.PATHFINDER_M+1): //if (not any(packet.destination_hash == d.hash for d in Transport.destinations) and packet.hops < Transport.PATHFINDER_M+1):
bool found_local = false; bool found_local = false;
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == packet.destination_hash()) { if (destination.hash() == packet.destination_hash()) {
found_local = true; found_local = true;
break; break;
@ -1499,7 +1499,7 @@ using namespace RNS::Utilities;
uint64_t path_expires = destination_entry._expires; uint64_t path_expires = destination_entry._expires;
uint64_t path_announce_emitted = 0; uint64_t path_announce_emitted = 0;
for (const Bytes &path_random_blob : destination_entry._random_blobs) { for (const Bytes& path_random_blob : destination_entry._random_blobs) {
//path_announce_emitted = std::max(path_announce_emitted, int.from_bytes(path_random_blob[5:10], "big")) //path_announce_emitted = std::max(path_announce_emitted, int.from_bytes(path_random_blob[5:10], "big"))
//zpath_announce_emitted = std::max(path_announce_emitted, int.from_bytes(path_random_blob[5:10], "big")) //zpath_announce_emitted = std::max(path_announce_emitted, int.from_bytes(path_random_blob[5:10], "big"))
if (path_announce_emitted >= announce_emitted) { if (path_announce_emitted >= announce_emitted) {
@ -1666,7 +1666,7 @@ using namespace RNS::Utilities;
// TODO: Shouldn't the context be PATH_RESPONSE in the first case here? // TODO: Shouldn't the context be PATH_RESPONSE in the first case here?
if (from_local_client(packet) && packet.context() == Packet.PATH_RESPONSE) { if (from_local_client(packet) && packet.context() == Packet.PATH_RESPONSE) {
for (auto &local_interface : _local_client_interfaces) { for (auto& local_interface : _local_client_interfaces) {
if packet.receiving_interface() != local_interface) { if packet.receiving_interface() != local_interface) {
Packet new_announce( Packet new_announce(
announce_destination, announce_destination,
@ -1685,7 +1685,7 @@ using namespace RNS::Utilities;
} }
} }
else { else {
for (auto &local_interface : _local_client_interfaces) { for (auto& local_interface : _local_client_interfaces) {
if (packet.receiving_interface() != local_interface) { if (packet.receiving_interface() != local_interface) {
Packet new_announce( Packet new_announce(
announce_destination, announce_destination,
@ -1764,7 +1764,7 @@ using namespace RNS::Utilities;
// Call externally registered callbacks from apps // Call externally registered callbacks from apps
// wanting to know when an announce arrives // wanting to know when an announce arrives
if (packet.context() != Type::Packet::PATH_RESPONSE) { if (packet.context() != Type::Packet::PATH_RESPONSE) {
for (auto &handler : Transport.announce_handlers) { for (auto& handler : Transport.announce_handlers) {
try { try {
// Check that the announced destination matches // Check that the announced destination matches
// the handlers aspect filter // the handlers aspect filter
@ -1789,7 +1789,7 @@ using namespace RNS::Utilities;
); );
} }
} }
catch (std::exception &e) { catch (std::exception& e) {
error("Error while processing external announce callback."); error("Error while processing external announce callback.");
error("The contained exception was: " + e.what(); error("The contained exception was: " + e.what();
} }
@ -1811,7 +1811,7 @@ using namespace RNS::Utilities;
else if (packet.packet_type() == Type::Packet::LINKREQUEST) { else if (packet.packet_type() == Type::Packet::LINKREQUEST) {
extreme("Transport::inbound: Packet is LINKREQUEST"); extreme("Transport::inbound: Packet is LINKREQUEST");
if (!packet.transport_id() || packet.transport_id() == _identity.hash()) { if (!packet.transport_id() || packet.transport_id() == _identity.hash()) {
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == packet.destination_hash() && destination.type() == packet.destination_type()) { if (destination.hash() == packet.destination_hash() && destination.type() == packet.destination_type()) {
packet.destination(destination); packet.destination(destination);
// CBA iterator over std::set is always const so need to make temporarily mutable // CBA iterator over std::set is always const so need to make temporarily mutable
@ -1826,7 +1826,7 @@ using namespace RNS::Utilities;
else if (packet.packet_type() == Type::Packet::DATA) { else if (packet.packet_type() == Type::Packet::DATA) {
extreme("Transport::inbound: Packet is DATA"); extreme("Transport::inbound: Packet is DATA");
if (packet.destination_type() == Type::Destination::LINK) { if (packet.destination_type() == Type::Destination::LINK) {
for (auto &link : _active_links) { for (auto& link : _active_links) {
if (link.link_id() == packet.destination_hash()) { if (link.link_id() == packet.destination_hash()) {
packet.link(link); packet.link(link);
const_cast<Link&>(link).receive(packet); const_cast<Link&>(link).receive(packet);
@ -1834,7 +1834,7 @@ using namespace RNS::Utilities;
} }
} }
else { else {
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == packet.destination_hash() && destination.type() == packet.destination_type()) { if (destination.hash() == packet.destination_hash() && destination.type() == packet.destination_type()) {
packet.destination(destination); packet.destination(destination);
const_cast<Destination&>(destination).receive(packet); const_cast<Destination&>(destination).receive(packet);
@ -1849,7 +1849,7 @@ using namespace RNS::Utilities;
packet.prove(); packet.prove();
} }
} }
catch (std::exception &e) { catch (std::exception& e) {
error(std::string("Error while executing proof request callback. The contained exception was: ") + e.what()); error(std::string("Error while executing proof request callback. The contained exception was: ") + e.what());
} }
} }
@ -1952,7 +1952,7 @@ using namespace RNS::Utilities;
_jobs_locked = false; _jobs_locked = false;
} }
/*static*/ void Transport::synthesize_tunnel(const Interface &interface) { /*static*/ void Transport::synthesize_tunnel(const Interface& interface) {
/* /*
Bytes interface_hash = interface.get_hash(); Bytes interface_hash = interface.get_hash();
Bytes public_key = _identity.get_public_key(); Bytes public_key = _identity.get_public_key();
@ -1975,7 +1975,7 @@ using namespace RNS::Utilities;
*/ */
} }
/*static*/ void Transport::tunnel_synthesize_handler(const Bytes &data, const Packet &packet) { /*static*/ void Transport::tunnel_synthesize_handler(const Bytes& data, const Packet& packet) {
/* /*
try: try:
expected_length = RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8+RNS.Identity.SIGLENGTH//8 expected_length = RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8+RNS.Identity.SIGLENGTH//8
@ -2001,7 +2001,7 @@ using namespace RNS::Utilities;
*/ */
} }
/*static*/ void Transport::handle_tunnel(const Bytes &tunnel_id, const Interface &interface) { /*static*/ void Transport::handle_tunnel(const Bytes& tunnel_id, const Interface& interface) {
/* /*
expires = time.time() + Transport.DESTINATION_TIMEOUT expires = time.time() + Transport.DESTINATION_TIMEOUT
if not tunnel_id in Transport.tunnels: if not tunnel_id in Transport.tunnels:
@ -2055,17 +2055,17 @@ using namespace RNS::Utilities;
*/ */
} }
/*static*/ void Transport::register_interface(Interface &interface) { /*static*/ void Transport::register_interface(Interface& interface) {
extreme("Transport: Registering interface " + interface.toString()); extreme("Transport: Registering interface " + interface.toString());
_interfaces.push_back(interface); _interfaces.push_back(interface);
extreme("Transport: Listing all registered interfaces..."); extreme("Transport: Listing all registered interfaces...");
for (Interface &found_interface : _interfaces) { for (Interface& found_interface : _interfaces) {
extreme("Transport: Found interface " + found_interface.toString()); extreme("Transport: Found interface " + found_interface.toString());
} }
// CBA TODO set or add transport as listener on interface to receive incoming packets // CBA TODO set or add transport as listener on interface to receive incoming packets
} }
/*static*/ void Transport::deregister_interface(const Interface &interface) { /*static*/ void Transport::deregister_interface(const Interface& interface) {
extreme("Transport: Deregistering interface " + interface.toString()); extreme("Transport: Deregistering interface " + interface.toString());
//if (_interfaces.find(interface) != _interfaces.end()) { //if (_interfaces.find(interface) != _interfaces.end()) {
// _interfaces.erase(interface); // _interfaces.erase(interface);
@ -2078,11 +2078,11 @@ using namespace RNS::Utilities;
} }
} }
/*static*/ void Transport::register_destination(Destination &destination) { /*static*/ void Transport::register_destination(Destination& destination) {
extreme("Transport: Registering destination " + destination.toString()); extreme("Transport: Registering destination " + destination.toString());
destination.mtu(Type::Reticulum::MTU); destination.mtu(Type::Reticulum::MTU);
if (destination.direction() == Type::Destination::IN) { if (destination.direction() == Type::Destination::IN) {
for (auto &registered_destination : _destinations) { for (auto& registered_destination : _destinations) {
if (destination.hash() == registered_destination.hash()) { if (destination.hash() == registered_destination.hash()) {
//raise KeyError("Attempt to register an already registered destination.") //raise KeyError("Attempt to register an already registered destination.")
throw std::runtime_error("Attempt to register an already registered destination."); throw std::runtime_error("Attempt to register an already registered destination.");
@ -2099,14 +2099,14 @@ using namespace RNS::Utilities;
} }
} }
/*static*/ void Transport::deregister_destination(const Destination &destination) { /*static*/ void Transport::deregister_destination(const Destination& destination) {
extreme("Transport: Deregistering destination " + destination.toString()); extreme("Transport: Deregistering destination " + destination.toString());
if (_destinations.find(destination) != _destinations.end()) { if (_destinations.find(destination) != _destinations.end()) {
_destinations.erase(destination); _destinations.erase(destination);
} }
} }
/*static*/ void Transport::register_link(const Link &link) { /*static*/ void Transport::register_link(const Link& link) {
/* /*
extreme("Transport: Registering link " + link.toString()); extreme("Transport: Registering link " + link.toString());
if (link.initiator()) { if (link.initiator()) {
@ -2118,7 +2118,7 @@ using namespace RNS::Utilities;
*/ */
} }
/*static*/ void Transport::activate_link(Link &link) { /*static*/ void Transport::activate_link(Link& link) {
/* /*
extreme("Transport: Activating link " + link.toString()); extreme("Transport: Activating link " + link.toString());
if (_pending_links.find(link) != _pending_links.end()) { if (_pending_links.find(link) != _pending_links.end()) {
@ -2159,8 +2159,8 @@ Deregisters an announce handler.
} }
} }
/*static*/ Interface Transport::find_interface_from_hash(const Bytes &interface_hash) { /*static*/ Interface Transport::find_interface_from_hash(const Bytes& interface_hash) {
for (const Interface &interface : _interfaces) { for (const Interface& interface : _interfaces) {
if (interface.get_hash() == interface_hash) { if (interface.get_hash() == interface_hash) {
return interface; return interface;
} }
@ -2169,7 +2169,7 @@ Deregisters an announce handler.
return {Type::NONE}; return {Type::NONE};
} }
/*static*/ bool Transport::should_cache(const Packet &packet) { /*static*/ bool Transport::should_cache(const Packet& packet) {
// TODO: Rework the caching system. It's currently // TODO: Rework the caching system. It's currently
// not very useful to even cache Resource proofs, // not very useful to even cache Resource proofs,
// disabling it for now, until redesigned. // disabling it for now, until redesigned.
@ -2184,7 +2184,7 @@ Deregisters an announce handler.
// means that they have not had their hop count // means that they have not had their hop count
// increased yet! Take note of this when reading from // increased yet! Take note of this when reading from
// the packet cache. // the packet cache.
/*static*/ void Transport::cache(const Packet &packet, bool force_cache /*= false*/) { /*static*/ void Transport::cache(const Packet& packet, bool force_cache /*= false*/) {
/* /*
if (should_cache(packet) || force_cache) { if (should_cache(packet) || force_cache) {
try { try {
@ -2200,14 +2200,14 @@ Deregisters an announce handler.
file.write(umsgpack.packb([packet.raw, interface_reference])) file.write(umsgpack.packb([packet.raw, interface_reference]))
file.close() file.close()
} }
catch (std::exception &e) { catch (std::exception& e) {
error("Error writing packet to cache. The contained exception was: " + e.what()); error("Error writing packet to cache. The contained exception was: " + e.what());
} }
} }
*/ */
} }
/*static*/ Packet Transport::get_cached_packet(const Bytes &packet_hash) { /*static*/ Packet Transport::get_cached_packet(const Bytes& packet_hash) {
/* /*
try { try {
//packet_hash = RNS.hexrep(packet_hash, delimit=False) //packet_hash = RNS.hexrep(packet_hash, delimit=False)
@ -2230,7 +2230,7 @@ Deregisters an announce handler.
else: else:
return None return None
} }
catch (std::exception &e) { catch (std::exception& e) {
error("Exception occurred while getting cached packet."); error("Exception occurred while getting cached packet.");
error("The contained exception was: " + e.what()); error("The contained exception was: " + e.what());
} }
@ -2239,9 +2239,9 @@ Deregisters an announce handler.
return {Type::NONE}; return {Type::NONE};
} }
/*static*/ bool Transport::cache_request_packet(const Packet &packet) { /*static*/ bool Transport::cache_request_packet(const Packet& packet) {
if (packet.data().size() == Type::Identity::HASHLENGTH/8) { if (packet.data().size() == Type::Identity::HASHLENGTH/8) {
const Packet &cached_packet = get_cached_packet(packet.data()); const Packet& cached_packet = get_cached_packet(packet.data());
if (cached_packet) { if (cached_packet) {
// If the packet was retrieved from the local // If the packet was retrieved from the local
@ -2260,8 +2260,8 @@ Deregisters an announce handler.
} }
} }
/*static*/ void Transport::cache_request(const Bytes &packet_hash, const Destination &destination) { /*static*/ void Transport::cache_request(const Bytes& packet_hash, const Destination& destination) {
const Packet &cached_packet = get_cached_packet(packet_hash); const Packet& cached_packet = get_cached_packet(packet_hash);
if (cached_packet) { if (cached_packet) {
// The packet was found in the local cache, // The packet was found in the local cache,
// replay it to the Transport instance. // replay it to the Transport instance.
@ -2279,7 +2279,7 @@ Deregisters an announce handler.
:param destination_hash: A destination hash as *bytes*. :param destination_hash: A destination hash as *bytes*.
:returns: *True* if a path to the destination is known, otherwise *False*. :returns: *True* if a path to the destination is known, otherwise *False*.
*/ */
/*static*/ bool Transport::has_path(const Bytes &destination_hash) { /*static*/ bool Transport::has_path(const Bytes& destination_hash) {
if (_destination_table.find(destination_hash) != _destination_table.end()) { if (_destination_table.find(destination_hash) != _destination_table.end()) {
return true; return true;
} }
@ -2292,7 +2292,7 @@ Deregisters an announce handler.
:param destination_hash: A destination hash as *bytes*. :param destination_hash: A destination hash as *bytes*.
:returns: The number of hops to the specified destination, or ``RNS.Transport.PATHFINDER_M`` if the number of hops is unknown. :returns: The number of hops to the specified destination, or ``RNS.Transport.PATHFINDER_M`` if the number of hops is unknown.
*/ */
/*static*/ uint8_t Transport::hops_to(const Bytes &destination_hash) { /*static*/ uint8_t Transport::hops_to(const Bytes& destination_hash) {
auto iter = _destination_table.find(destination_hash); auto iter = _destination_table.find(destination_hash);
if (iter != _destination_table.end()) { if (iter != _destination_table.end()) {
DestinationEntry destination_entry = (*iter).second; DestinationEntry destination_entry = (*iter).second;
@ -2307,7 +2307,7 @@ Deregisters an announce handler.
:param destination_hash: A destination hash as *bytes*. :param destination_hash: A destination hash as *bytes*.
:returns: The destination hash as *bytes* for the next hop to the specified destination, or *None* if the next hop is unknown. :returns: The destination hash as *bytes* for the next hop to the specified destination, or *None* if the next hop is unknown.
*/ */
/*static*/ Bytes Transport::next_hop(const Bytes &destination_hash) { /*static*/ Bytes Transport::next_hop(const Bytes& destination_hash) {
auto iter = _destination_table.find(destination_hash); auto iter = _destination_table.find(destination_hash);
if (iter != _destination_table.end()) { if (iter != _destination_table.end()) {
DestinationEntry destination_entry = (*iter).second; DestinationEntry destination_entry = (*iter).second;
@ -2322,7 +2322,7 @@ Deregisters an announce handler.
:param destination_hash: A destination hash as *bytes*. :param destination_hash: A destination hash as *bytes*.
:returns: The interface for the next hop to the specified destination, or *None* if the interface is unknown. :returns: The interface for the next hop to the specified destination, or *None* if the interface is unknown.
*/ */
/*static*/ Interface Transport::next_hop_interface(const Bytes &destination_hash) { /*static*/ Interface Transport::next_hop_interface(const Bytes& destination_hash) {
auto iter = _destination_table.find(destination_hash); auto iter = _destination_table.find(destination_hash);
if (iter != _destination_table.end()) { if (iter != _destination_table.end()) {
DestinationEntry destination_entry = (*iter).second; DestinationEntry destination_entry = (*iter).second;
@ -2333,7 +2333,7 @@ Deregisters an announce handler.
} }
} }
/*static*/ bool Transport::expire_path(const Bytes &destination_hash) { /*static*/ bool Transport::expire_path(const Bytes& destination_hash) {
auto iter = _destination_table.find(destination_hash); auto iter = _destination_table.find(destination_hash);
if (iter != _destination_table.end()) { if (iter != _destination_table.end()) {
DestinationEntry destination_entry = (*iter).second; DestinationEntry destination_entry = (*iter).second;
@ -2354,8 +2354,8 @@ will announce it.
:param destination_hash: A destination hash as *bytes*. :param destination_hash: A destination hash as *bytes*.
:param on_interface: If specified, the path request will only be sent on this interface. In normal use, Reticulum handles this automatically, and this parameter should not be used. :param on_interface: If specified, the path request will only be sent on this interface. In normal use, Reticulum handles this automatically, and this parameter should not be used.
*/ */
///*static*/ void Transport::request_path(const Bytes &destination_hash, const Interface &on_interface /*= {Type::NONE}*/, const Bytes &tag /*= {}*/, bool recursive /*= false*/) { ///*static*/ void Transport::request_path(const Bytes& destination_hash, const Interface& on_interface /*= {Type::NONE}*/, const Bytes& tag /*= {}*/, bool recursive /*= false*/) {
/*static*/ void Transport::request_path(const Bytes &destination_hash, const Interface &on_interface, const Bytes &tag /*= {}*/, bool recursive /*= false*/) { /*static*/ void Transport::request_path(const Bytes& destination_hash, const Interface& on_interface, const Bytes& tag /*= {}*/, bool recursive /*= false*/) {
/* /*
if tag == None: if tag == None:
request_tag = RNS.Identity.get_random_hash() request_tag = RNS.Identity.get_random_hash()
@ -2399,11 +2399,11 @@ will announce it.
*/ */
} }
/*static*/ void Transport::request_path(const Bytes &destination_hash) { /*static*/ void Transport::request_path(const Bytes& destination_hash) {
return request_path(destination_hash, {Type::NONE}); return request_path(destination_hash, {Type::NONE});
} }
/*static*/ void Transport::path_request_handler(const Bytes &data, const Packet &packet) { /*static*/ void Transport::path_request_handler(const Bytes& data, const Packet& packet) {
/* /*
try: try:
// If there is at least bytes enough for a destination // If there is at least bytes enough for a destination
@ -2455,7 +2455,7 @@ will announce it.
*/ */
} }
/*static*/ void Transport::path_request(const Bytes &destination_hash, bool is_from_local_client, const Interface &attached_interface, const Bytes &requestor_transport_id /*= {}*/, const Bytes &tag /*= {}*/) { /*static*/ void Transport::path_request(const Bytes& destination_hash, bool is_from_local_client, const Interface& attached_interface, const Bytes& requestor_transport_id /*= {}*/, const Bytes& tag /*= {}*/) {
/* /*
should_search_for_unknown = False should_search_for_unknown = False
@ -2480,7 +2480,7 @@ will announce it.
//local_destination = next((d for d in Transport.destinations if d.hash == destination_hash), None) //local_destination = next((d for d in Transport.destinations if d.hash == destination_hash), None)
Destination local_destination({Type::NONE}); Destination local_destination({Type::NONE});
for (auto &destination : _destinations) { for (auto& destination : _destinations) {
if (destination.hash() == destination_hash) { if (destination.hash() == destination_hash) {
local_destination = destination; local_destination = destination;
break; break;
@ -2573,7 +2573,7 @@ will announce it.
*/ */
} }
/*static*/ bool Transport::from_local_client(const Packet &packet) { /*static*/ bool Transport::from_local_client(const Packet& packet) {
/* /*
if hasattr(packet.receiving_interface, "parent_interface"): if hasattr(packet.receiving_interface, "parent_interface"):
return Transport.is_local_client_interface(packet.receiving_interface) return Transport.is_local_client_interface(packet.receiving_interface)
@ -2584,7 +2584,7 @@ will announce it.
return false; return false;
} }
/*static*/ bool Transport::is_local_client_interface(const Interface &interface) { /*static*/ bool Transport::is_local_client_interface(const Interface& interface) {
/* /*
if hasattr(interface, "parent_interface"): if hasattr(interface, "parent_interface"):
if hasattr(interface.parent_interface, "is_local_shared_instance"): if hasattr(interface.parent_interface, "is_local_shared_instance"):
@ -2598,7 +2598,7 @@ will announce it.
return false; return false;
} }
/*static*/ bool Transport::interface_to_shared_instance(const Interface &interface) { /*static*/ bool Transport::interface_to_shared_instance(const Interface& interface) {
/* /*
if hasattr(interface, "is_connected_to_shared_instance"): if hasattr(interface, "is_connected_to_shared_instance"):
return True return True
@ -2679,7 +2679,7 @@ will announce it.
*/ */
} }
/*static*/ bool Transport::announce_emitted(const Packet &packet) { /*static*/ bool Transport::announce_emitted(const Packet& packet) {
/* /*
random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8+10] random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8+10]
announce_emitted = int.from_bytes(random_blob[5:10], "big") announce_emitted = int.from_bytes(random_blob[5:10], "big")

View file

@ -23,10 +23,10 @@ namespace RNS {
class AnnounceHandler { class AnnounceHandler {
public: public:
AnnounceHandler(const std::string &aspect_filter) { AnnounceHandler(const std::string& aspect_filter) {
_aspect_filter = aspect_filter; _aspect_filter = aspect_filter;
} }
virtual void received_announce(const Bytes &destination_hash, const Identity &announced_identity, const Bytes &app_data) = 0; virtual void received_announce(const Bytes& destination_hash, const Identity& announced_identity, const Bytes& app_data) = 0;
private: private:
std::string _aspect_filter; std::string _aspect_filter;
}; };
@ -41,7 +41,7 @@ namespace RNS {
private: private:
class DestinationEntry { class DestinationEntry {
public: public:
DestinationEntry(uint64_t time, Bytes received_from, uint8_t announce_hops, uint64_t expires, std::set<Bytes> random_blobs, Interface &receiving_interface, const Packet &packet) : DestinationEntry(uint64_t time, Bytes received_from, uint8_t announce_hops, uint64_t expires, std::set<Bytes> random_blobs, Interface& receiving_interface, const Packet& packet) :
_timestamp(time), _timestamp(time),
_received_from(received_from), _received_from(received_from),
_hops(announce_hops), _hops(announce_hops),
@ -57,13 +57,13 @@ namespace RNS {
uint8_t _hops = 0; uint8_t _hops = 0;
uint64_t _expires = 0; uint64_t _expires = 0;
std::set<Bytes> _random_blobs; std::set<Bytes> _random_blobs;
Interface &_receiving_interface; Interface& _receiving_interface;
const Packet &_packet; const Packet& _packet;
}; };
class AnnounceEntry { class AnnounceEntry {
public: public:
AnnounceEntry(uint64_t timestamp, uint16_t retransmit_timeout, uint8_t retries, Bytes received_from, uint8_t hops, const Packet &packet, uint8_t local_rebroadcasts, bool block_rebroadcasts, const Interface &attached_interface) : AnnounceEntry(uint64_t timestamp, uint16_t retransmit_timeout, uint8_t retries, Bytes received_from, uint8_t hops, const Packet& packet, uint8_t local_rebroadcasts, bool block_rebroadcasts, const Interface& attached_interface) :
_timestamp(timestamp), _timestamp(timestamp),
_retransmit_timeout(retransmit_timeout), _retransmit_timeout(retransmit_timeout),
_retries(retries), _retries(retries),
@ -81,15 +81,15 @@ namespace RNS {
uint8_t _retries = 0; uint8_t _retries = 0;
Bytes _received_from; Bytes _received_from;
uint8_t _hops = 0; uint8_t _hops = 0;
const Packet &_packet; const Packet& _packet;
uint8_t _local_rebroadcasts = 0; uint8_t _local_rebroadcasts = 0;
bool _block_rebroadcasts = false; bool _block_rebroadcasts = false;
const Interface &_attached_interface; const Interface& _attached_interface;
}; };
class LinkEntry { class LinkEntry {
public: public:
LinkEntry(uint64_t timestamp, const Bytes &next_hop, const Interface &outbound_interface, uint8_t remaining_hops, const Interface &receiving_interface, uint8_t hops, const Bytes &destination_hash, bool validated, uint64_t proof_timeout) : LinkEntry(uint64_t timestamp, const Bytes& next_hop, const Interface& outbound_interface, uint8_t remaining_hops, const Interface& receiving_interface, uint8_t hops, const Bytes& destination_hash, bool validated, uint64_t proof_timeout) :
_timestamp(timestamp), _timestamp(timestamp),
_next_hop(next_hop), _next_hop(next_hop),
_outbound_interface(outbound_interface), _outbound_interface(outbound_interface),
@ -104,9 +104,9 @@ namespace RNS {
public: public:
uint64_t _timestamp = 0; uint64_t _timestamp = 0;
Bytes _next_hop; Bytes _next_hop;
const Interface &_outbound_interface; const Interface& _outbound_interface;
uint8_t _remaining_hops = 0; uint8_t _remaining_hops = 0;
const Interface &_receiving_interface; const Interface& _receiving_interface;
uint8_t _hops = 0; uint8_t _hops = 0;
Bytes _destination_hash; Bytes _destination_hash;
bool _validated = false; bool _validated = false;
@ -115,15 +115,15 @@ namespace RNS {
class ReverseEntry { class ReverseEntry {
public: public:
ReverseEntry(const Interface &receiving_interface, const Interface &outbound_interface, uint64_t timestamp) : ReverseEntry(const Interface& receiving_interface, const Interface& outbound_interface, uint64_t timestamp) :
_receiving_interface(receiving_interface), _receiving_interface(receiving_interface),
_outbound_interface(outbound_interface), _outbound_interface(outbound_interface),
_timestamp(timestamp) _timestamp(timestamp)
{ {
} }
public: public:
const Interface &_receiving_interface; const Interface& _receiving_interface;
const Interface &_outbound_interface; const Interface& _outbound_interface;
uint64_t _timestamp = 0; uint64_t _timestamp = 0;
}; };
@ -173,50 +173,50 @@ namespace RNS {
static const uint8_t MAX_RATE_TIMESTAMPS = 16; // Maximum number of announce timestamps to keep per destination static const uint8_t MAX_RATE_TIMESTAMPS = 16; // Maximum number of announce timestamps to keep per destination
public: public:
static void start(const Reticulum &reticulum_instance); static void start(const Reticulum& reticulum_instance);
static void jobloop(); static void jobloop();
static void jobs(); static void jobs();
static void transmit(Interface &interface, const Bytes &raw); static void transmit(Interface& interface, const Bytes& raw);
static bool outbound(Packet &packet); static bool outbound(Packet& packet);
static bool packet_filter(const Packet &packet); static bool packet_filter(const Packet& packet);
//static void inbound(const Bytes &raw, const Interface &interface = {Type::NONE}); //static void inbound(const Bytes& raw, const Interface& interface = {Type::NONE});
static void inbound(const Bytes &raw, const Interface &interface); static void inbound(const Bytes& raw, const Interface& interface);
static void inbound(const Bytes &raw); static void inbound(const Bytes& raw);
static void synthesize_tunnel(const Interface &interface); static void synthesize_tunnel(const Interface& interface);
static void tunnel_synthesize_handler(const Bytes &data, const Packet &packet); static void tunnel_synthesize_handler(const Bytes& data, const Packet& packet);
static void handle_tunnel(const Bytes &tunnel_id, const Interface &interface); static void handle_tunnel(const Bytes& tunnel_id, const Interface& interface);
static void register_interface(Interface &interface); static void register_interface(Interface& interface);
static void deregister_interface(const Interface &interface); static void deregister_interface(const Interface& interface);
static void register_destination(Destination &destination); static void register_destination(Destination& destination);
static void deregister_destination(const Destination &destination); static void deregister_destination(const Destination& destination);
static void register_link(const Link &link); static void register_link(const Link& link);
static void activate_link(Link &link); static void activate_link(Link& link);
static void register_announce_handler(HAnnounceHandler handler); static void register_announce_handler(HAnnounceHandler handler);
static void deregister_announce_handler(HAnnounceHandler handler); static void deregister_announce_handler(HAnnounceHandler handler);
static Interface find_interface_from_hash(const Bytes &interface_hash); static Interface find_interface_from_hash(const Bytes& interface_hash);
static bool should_cache(const Packet &packet); static bool should_cache(const Packet& packet);
static void cache(const Packet &packet, bool force_cache = false); static void cache(const Packet& packet, bool force_cache = false);
static Packet get_cached_packet(const Bytes &packet_hash); static Packet get_cached_packet(const Bytes& packet_hash);
static bool cache_request_packet(const Packet &packet); static bool cache_request_packet(const Packet& packet);
static void cache_request(const Bytes &packet_hash, const Destination &destination); static void cache_request(const Bytes& packet_hash, const Destination& destination);
static bool has_path(const Bytes &destination_hash); static bool has_path(const Bytes& destination_hash);
static uint8_t hops_to(const Bytes &destination_hash); static uint8_t hops_to(const Bytes& destination_hash);
static Bytes next_hop(const Bytes &destination_hash); static Bytes next_hop(const Bytes& destination_hash);
static Interface next_hop_interface(const Bytes &destination_hash); static Interface next_hop_interface(const Bytes& destination_hash);
static bool expire_path(const Bytes &destination_hash); static bool expire_path(const Bytes& destination_hash);
//static void request_path(const Bytes &destination_hash, const Interface &on_interface = {Type::NONE}, const Bytes &tag = {}, bool recursive = false); //static void request_path(const Bytes& destination_hash, const Interface& on_interface = {Type::NONE}, const Bytes& tag = {}, bool recursive = false);
static void request_path(const Bytes &destination_hash, const Interface &on_interface, const Bytes &tag = {}, bool recursive = false); static void request_path(const Bytes& destination_hash, const Interface& on_interface, const Bytes& tag = {}, bool recursive = false);
static void request_path(const Bytes &destination_hash); static void request_path(const Bytes& destination_hash);
static void path_request_handler(const Bytes &data, const Packet &packet); static void path_request_handler(const Bytes& data, const Packet& packet);
static void path_request(const Bytes &destination_hash, bool is_from_local_client, const Interface &attached_interface, const Bytes &requestor_transport_id = {}, const Bytes &tag = {}); static void path_request(const Bytes& destination_hash, bool is_from_local_client, const Interface& attached_interface, const Bytes& requestor_transport_id = {}, const Bytes& tag = {});
static bool from_local_client(const Packet &packet); static bool from_local_client(const Packet& packet);
static bool is_local_client_interface(const Interface &interface); static bool is_local_client_interface(const Interface& interface);
static bool interface_to_shared_instance(const Interface &interface); static bool interface_to_shared_instance(const Interface& interface);
static void detach_interfaces(); static void detach_interfaces();
static void shared_connection_disappeared(); static void shared_connection_disappeared();
static void shared_connection_reappeared(); static void shared_connection_reappeared();
static void drop_announce_queues(); static void drop_announce_queues();
static bool announce_emitted(const Packet &packet); static bool announce_emitted(const Packet& packet);
static void save_packet_hashlist(); static void save_packet_hashlist();
static void save_path_table(); static void save_path_table();
static void save_tunnel_table(); static void save_tunnel_table();

View file

@ -46,10 +46,10 @@ public:
virtual ~TestInterface() { virtual ~TestInterface() {
name("deleted"); name("deleted");
} }
virtual void processIncoming(const RNS::Bytes &data) { virtual void processIncoming(const RNS::Bytes& data) {
RNS::extreme("TestInterface.processIncoming: data: " + data.toHex()); RNS::extreme("TestInterface.processIncoming: data: " + data.toHex());
} }
virtual void processOutgoing(const RNS::Bytes &data) { virtual void processOutgoing(const RNS::Bytes& data) {
RNS::extreme("TestInterface.processOutgoing: data: " + data.toHex()); RNS::extreme("TestInterface.processOutgoing: data: " + data.toHex());
} }
virtual inline std::string toString() const { return "TestInterface[" + name() + "]"; } virtual inline std::string toString() const { return "TestInterface[" + name() + "]"; }
@ -57,28 +57,28 @@ public:
class TestLoopbackInterface : public RNS::Interface { class TestLoopbackInterface : public RNS::Interface {
public: public:
TestLoopbackInterface(RNS::Interface &loopback_interface) : RNS::Interface("TestLoopbackInterface"), _loopback_interface(loopback_interface) { TestLoopbackInterface(RNS::Interface& loopback_interface) : RNS::Interface("TestLoopbackInterface"), _loopback_interface(loopback_interface) {
IN(true); IN(true);
OUT(true); OUT(true);
} }
TestLoopbackInterface(RNS::Interface &loopback_interface, const char *name) : RNS::Interface(name), _loopback_interface(loopback_interface) { TestLoopbackInterface(RNS::Interface& loopback_interface, const char *name) : RNS::Interface(name), _loopback_interface(loopback_interface) {
IN(true); IN(true);
OUT(true); OUT(true);
} }
virtual ~TestLoopbackInterface() { virtual ~TestLoopbackInterface() {
name("deleted"); name("deleted");
} }
virtual void processIncoming(const RNS::Bytes &data) { virtual void processIncoming(const RNS::Bytes& data) {
RNS::extreme("TestLoopbackInterface.processIncoming: data: " + data.toHex()); RNS::extreme("TestLoopbackInterface.processIncoming: data: " + data.toHex());
_loopback_interface.processOutgoing(data); _loopback_interface.processOutgoing(data);
} }
virtual void processOutgoing(const RNS::Bytes &data) { virtual void processOutgoing(const RNS::Bytes& data) {
RNS::extreme("TestLoopbackInterface.processOutgoing: data: " + data.toHex()); RNS::extreme("TestLoopbackInterface.processOutgoing: data: " + data.toHex());
_loopback_interface.processIncoming(data); _loopback_interface.processIncoming(data);
} }
virtual inline std::string toString() const { return "TestLoopbackInterface[" + name() + "]"; } virtual inline std::string toString() const { return "TestLoopbackInterface[" + name() + "]"; }
private: private:
RNS::Interface &_loopback_interface; RNS::Interface& _loopback_interface;
}; };
class TestOutInterface : public RNS::Interface { class TestOutInterface : public RNS::Interface {
@ -94,7 +94,7 @@ public:
virtual ~TestOutInterface() { virtual ~TestOutInterface() {
name("(deleted)"); name("(deleted)");
} }
virtual void processOutgoing(const RNS::Bytes &data) { virtual void processOutgoing(const RNS::Bytes& data) {
RNS::head("TestOutInterface.processOutgoing: data: " + data.toHex(), RNS::LOG_EXTREME); RNS::head("TestOutInterface.processOutgoing: data: " + data.toHex(), RNS::LOG_EXTREME);
RNS::Interface::processOutgoing(data); RNS::Interface::processOutgoing(data);
} }
@ -114,14 +114,14 @@ public:
virtual ~TestInInterface() { virtual ~TestInInterface() {
name("(deleted)"); name("(deleted)");
} }
virtual void processIncoming(const RNS::Bytes &data) { virtual void processIncoming(const RNS::Bytes& data) {
RNS::head("TestInInterface.processIncoming: data: " + data.toHex(), RNS::LOG_EXTREME); RNS::head("TestInInterface.processIncoming: data: " + data.toHex(), RNS::LOG_EXTREME);
RNS::Interface::processIncoming(data); RNS::Interface::processIncoming(data);
} }
virtual inline std::string toString() const { return "TestInInterface[" + name() + "]"; } virtual inline std::string toString() const { return "TestInInterface[" + name() + "]"; }
}; };
void onPacket(const RNS::Bytes &data, const RNS::Packet &packet) { void onPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
RNS::head("onPacket: data: " + data.toHex(), RNS::LOG_EXTREME); RNS::head("onPacket: data: " + data.toHex(), RNS::LOG_EXTREME);
RNS::head("onPacket: data string: \"" + data.toString() + "\"", RNS::LOG_EXTREME); RNS::head("onPacket: data string: \"" + data.toString() + "\"", RNS::LOG_EXTREME);
//RNS::head("onPacket: " + packet.debugString(), RNS::LOG_EXTREME); //RNS::head("onPacket: " + packet.debugString(), RNS::LOG_EXTREME);
@ -186,14 +186,14 @@ void setup() {
{ {
std::map<RNS::Bytes, RNS::Destination&> destinations; std::map<RNS::Bytes, RNS::Destination&> destinations;
destinations.insert({destination.hash(), destination}); destinations.insert({destination.hash(), destination});
//for (RNS::Destination &destination : destinations) { //for (RNS::Destination& destination : destinations) {
for (auto &[hash, destination] : destinations) { for (auto& [hash, destination] : destinations) {
RNS::extreme("Iterated destination: " + destination.toString()); RNS::extreme("Iterated destination: " + destination.toString());
} }
RNS::Bytes hash = destination.hash(); RNS::Bytes hash = destination.hash();
auto iter = destinations.find(hash); auto iter = destinations.find(hash);
if (iter != destinations.end()) { if (iter != destinations.end()) {
RNS::Destination &destination = (*iter).second; RNS::Destination& destination = (*iter).second;
RNS::extreme("Found destination: " + destination.toString()); RNS::extreme("Found destination: " + destination.toString());
} }
return; return;
@ -261,7 +261,7 @@ int main(void) {
std::set<std::reference_wrapper<RNS::Interface>, std::less<RNS::Interface>> interfaces; std::set<std::reference_wrapper<RNS::Interface>, std::less<RNS::Interface>> interfaces;
interfaces.insert(testinterface); interfaces.insert(testinterface);
for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) { for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) {
RNS::Interface &interface = (*iter); RNS::Interface& interface = (*iter);
RNS::extreme("Found interface: " + interface.toString()); RNS::extreme("Found interface: " + interface.toString());
RNS::Bytes data; RNS::Bytes data;
const_cast<RNS::Interface&>(interface).processOutgoing(data); const_cast<RNS::Interface&>(interface).processOutgoing(data);
@ -274,7 +274,7 @@ int main(void) {
std::set<std::reference_wrapper<RNS::Interface>, std::less<RNS::Interface>> interfaces; std::set<std::reference_wrapper<RNS::Interface>, std::less<RNS::Interface>> interfaces;
interfaces.insert(testinterface); interfaces.insert(testinterface);
for (auto &interface : interfaces) { for (auto& interface : interfaces) {
RNS::extreme("Found interface: " + interface.toString()); RNS::extreme("Found interface: " + interface.toString());
RNS::Bytes data; RNS::Bytes data;
const_cast<RNS::Interface&>(interface).processOutgoing(data); const_cast<RNS::Interface&>(interface).processOutgoing(data);
@ -288,7 +288,7 @@ int main(void) {
std::list<std::reference_wrapper<RNS::Interface>> interfaces; std::list<std::reference_wrapper<RNS::Interface>> interfaces;
interfaces.push_back(testinterface); interfaces.push_back(testinterface);
for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) { for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) {
RNS::Interface &interface = (*iter); RNS::Interface& interface = (*iter);
RNS::extreme("Found interface: " + interface.toString()); RNS::extreme("Found interface: " + interface.toString());
RNS::Bytes data; RNS::Bytes data;
const_cast<RNS::Interface&>(interface).processOutgoing(data); const_cast<RNS::Interface&>(interface).processOutgoing(data);
@ -301,8 +301,8 @@ int main(void) {
std::list<std::reference_wrapper<RNS::Interface>> interfaces; std::list<std::reference_wrapper<RNS::Interface>> interfaces;
interfaces.push_back(testinterface); interfaces.push_back(testinterface);
//for (auto &interface : interfaces) { //for (auto& interface : interfaces) {
for (RNS::Interface &interface : interfaces) { for (RNS::Interface& interface : interfaces) {
RNS::extreme("Found interface: " + interface.toString()); RNS::extreme("Found interface: " + interface.toString());
RNS::Bytes data; RNS::Bytes data;
const_cast<RNS::Interface&>(interface).processOutgoing(data); const_cast<RNS::Interface&>(interface).processOutgoing(data);
@ -316,14 +316,14 @@ int main(void) {
TestInterface testinterface; TestInterface testinterface;
interfaces.push_back(testinterface); interfaces.push_back(testinterface);
for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) { for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) {
RNS::Interface &interface = (*iter); RNS::Interface& interface = (*iter);
RNS::extreme("1 Found interface: " + interface.toString()); RNS::extreme("1 Found interface: " + interface.toString());
RNS::Bytes data; RNS::Bytes data;
const_cast<RNS::Interface&>(interface).processOutgoing(data); const_cast<RNS::Interface&>(interface).processOutgoing(data);
} }
} }
for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) { for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) {
RNS::Interface &interface = (*iter); RNS::Interface& interface = (*iter);
RNS::extreme("2 Found interface: " + interface.toString()); RNS::extreme("2 Found interface: " + interface.toString());
RNS::Bytes data; RNS::Bytes data;
const_cast<RNS::Interface&>(interface).processOutgoing(data); const_cast<RNS::Interface&>(interface).processOutgoing(data);