WIP: Links working with example app.

This commit is contained in:
Chad Attermann committed 2025-08-24 14:26:29 -06:00
1 parent c3edde10e9
commit 0c902054db
10 files changed
+223 -151

No files matched your search

+56 -50
View File
@@ -68,7 +68,7 @@ void server_packet_received(const RNS::Bytes& message, const RNS::Packet& packet
RNS::Bytes reply_data(reply_text);
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
//RNS::Packet(RNS::Type::NONE, latest_client_link, reply_data).send();
RNS::Packet(latest_client_link.link_destination(), reply_data).send();
RNS::Packet(latest_client_link, reply_data).send();
}
// When a client establishes a link to our server
@@ -120,7 +120,6 @@ void server() {
// Initialize and start Reticulum
RNS::Reticulum reticulum = RNS::Reticulum();
//reticulum.transport_enabled(true);
reticulum.start();
// Randomly create a new identity for our link example
@@ -161,13 +160,14 @@ void server() {
while (true) {
reticulum.loop();
udp_interface.loop();
/*
int c;
while ((c = getchar()) != '\n' && c != EOF);
server_destination.announce();
RNS::log("Sent announce from "+destination.hash().toHex());
*/
// Non-blocking input
char ch;
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
server_destination.announce();
RNS::log("Sent announce from "+server_destination.hash().toHex());
}
}
}
}
@@ -251,7 +251,7 @@ void client_loop() {
if (data.size() <= RNS::Type::Link::MDU) {
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
//RNS::Packet(RNS::Type::NONE, server_link, data).send();
RNS::Packet(server_link.link_destination(), data).send();
RNS::Packet(server_link, data).send();
}
else {
RNS::log(
@@ -304,7 +304,6 @@ void client(const char* destination_hexhash) {
// Initialize and start Reticulum
RNS::Reticulum reticulum = RNS::Reticulum();
//reticulum.transport_enabled(true);
reticulum.start();
// Check if we know a path to the destination
@@ -353,60 +352,63 @@ void client(const char* destination_hexhash) {
// Wait for the link to become active
RNS::log("Waiting for link to become active...");
//while (!server_link) {
while (true) {
printf(".");
while (!server_link) {
reticulum.loop();
udp_interface.loop();
RNS::Utilities::OS::sleep(0.1);
}
RNS::log("Link is active");
bool should_quit = false;
std::string text;
printf("> ");
fflush(STDIN_FILENO);
bool should_quit = false;
while (!should_quit) {
//printf(".");
reticulum.loop();
udp_interface.loop();
/*
try {
printf("> ");
char *line = NULL;
size_t size;
if (getline(&line, &size, stdin) == -1) {
//printf("No line\n");
continue;
}
std::string text(line);
// Check if we should quit the example
if (text == "quit" || text == "q" || text == "exit") {
should_quit = true;
server_link.teardown();
}
// Non-blocking input
char ch;
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
//printf("Text: %s", text.c_str());
// If not, send the entered text over the link
if (text != "") {
RNS::Bytes data(text);
if (data.size() <= RNS::Type::Link::MDU) {
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
RNS::Packet(RNS::Type::NONE, server_link, data).send();
// Check if we should quit the example
if (text == "quit" || text == "q" || text == "exit") {
should_quit = true;
server_link.teardown();
}
else {
RNS::log(
"Cannot send this packet, the data size of "+
std::to_string(data.size())+" bytes exceeds the link packet MDU of "+
std::to_string(RNS::Type::Link::MDU)+" bytes",
RNS::LOG_ERROR
);
// If not, send the entered text over the link
if (text != "") {
RNS::Bytes data(text);
if (data.size() <= RNS::Type::Link::MDU) {
printf("(sending data: %s)\n", text.c_str());
// CBA TODO: Add Packet constructor that accepts Link but doesn't require Destination
//RNS::Packet(RNS::Type::NONE, server_link, data).send();
RNS::Packet(server_link, data).send();
}
else {
RNS::log(
"Cannot send this packet, the data size of "+
std::to_string(data.size())+" bytes exceeds the link packet MDU of "+
std::to_string(RNS::Type::Link::MDU)+" bytes",
RNS::LOG_ERROR
);
}
}
text.clear();
printf("> ");
fflush(STDIN_FILENO);
} else {
text += ch;
}
}
catch (std::exception& e) {
//RNS::log("Error while sending data over the link: "+e.what());
RNS::log("Error while sending data over the link: ");
should_quit = true;
server_link.teardown();
}
*/
RNS::Utilities::OS::sleep(0.1);
}
}
@@ -442,6 +444,10 @@ int main(int argc, char *argv[]) {
// Register the signal handler for SIGINT
signal(SIGINT, cleanup_handler);
// Setup non-blocking input
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
if (argc <= 1) {
printf("\nMust specify a destination for client mode, or \"-s\" or \"--server\" for server mode.\n\n");
return -1;
+1 -4
View File
@@ -18,9 +18,6 @@ Token::Token(const Bytes& key, token_mode mode /*= AES*/) {
throw std::invalid_argument("Token key cannot be None");
}
if (key.size() != 32) {
throw std::invalid_argument("Token key must be 32 bytes, not " + std::to_string(key.size()));
}
if (mode == MODE_AES) {
if (key.size() == 32) {
_mode = MODE_AES_128_CBC;
@@ -37,7 +34,7 @@ Token::Token(const Bytes& key, token_mode mode /*= AES*/) {
_encryption_key = key.mid(32);
}
else {
throw std::invalid_argument("Token key must be 32 bytes, not " + std::to_string(key.size()));
throw std::invalid_argument("Token key must be 128 or 256 bits, not " + std::to_string(key.size()*8));
}
}
else {
+5 -4
View File
@@ -382,10 +382,11 @@ void Destination::receive(const Packet& packet) {
void Destination::incoming_link_request(const Bytes& data, const Packet& packet) {
assert(_object);
if (_object->_accept_link_requests) {
//z link = Link::validate_request(data, packet);
//z if (link) {
//z _links.append(link);
//z }
TRACE("***** Accepting link request");
RNS::Link link = Link::validate_request(*this, data, packet);
if (link) {
_object->_links.insert(link);
}
}
}
+14 -12
View File
@@ -189,23 +189,25 @@ namespace RNS {
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 const Bytes& hash() const { assert(_object); return _object->_hash; }
inline const Bytes& link_id() const { assert(_object); return _object->_link_id; }
inline uint16_t mtu() const { assert(_object); return _object->_mtu; }
inline Type::Link::status status() const { assert(_object); return _object->_status; }
// CBA LINK
//inline const Bytes& link_id() const { assert(_object); return _object->_link_id; }
//inline Type::Link::status status() const { assert(_object); return _object->_status; }
inline const Callbacks& callbacks() const { assert(_object); return _object->_callbacks; }
inline const Identity& identity() const { assert(_object); return _object->_identity; }
inline const std::map<Bytes, PathResponse>& path_responses() const { assert(_object); return _object->_path_responses; }
inline const std::map<Bytes, RequestHandler>& request_handlers() const { assert(_object); return _object->_request_handlers; }
// setters
// CBA Don't allow changing destination after construction since it's used as key in collections
// CBA Don't allow changing destination hash after construction since it's used as key in collections
//inline void hash(const Bytes& hash) { assert(_object); _object->_hash = hash; _object->_hexhash = _object->_hash.toHex(); }
inline void type(Type::Destination::types type) { assert(_object); _object->_type = type; }
inline void link_id(const Bytes& id) { assert(_object); _object->_link_id = id; }
inline void mtu(uint16_t mtu) { assert(_object); _object->_mtu = mtu; }
inline void last_outbound(double time) { assert(_object); _object->_last_outbound = time; }
inline void increment_tx() { assert(_object); ++_object->_tx; }
inline void increment_txbytes(uint16_t bytes) { assert(_object); _object->_txbytes += bytes; }
// CBA LINK
//inline void link_id(const Bytes& id) { assert(_object); _object->_link_id = id; }
//inline void last_outbound(double time) { assert(_object); _object->_last_outbound = time; }
//inline void increment_tx() { assert(_object); ++_object->_tx; }
//inline void increment_txbytes(uint16_t bytes) { assert(_object); _object->_txbytes += bytes; }
private:
class Object {
@@ -240,13 +242,13 @@ namespace RNS {
// CBA LINK
// CBA _link_id is expected by Packet but only present in Link
// CBA TODO determine if Link needs to inherit from Destination or vice-versa
Bytes _link_id;
//Bytes _link_id;
Type::Link::status _status;
//Type::Link::status _status;
double _last_outbound = 0.0;
uint16_t _tx = 0;
uint32_t _txbytes = 0;
//double _last_outbound = 0.0;
//uint16_t _tx = 0;
//uint32_t _txbytes = 0;
friend class Destination;
};
+50 -13
View File
@@ -106,8 +106,8 @@ Link::Link(const Destination& destination /*= {Type::NONE}*/, Callbacks::establi
}
// CBA LINK
_object->_link_destination = Destination({Type::NONE}, Type::Destination::OUT, Type::Destination::LINK, hash());
_object->_link_destination.link_id(_object->_link_id);
//_object->_link_destination = Destination({Type::NONE}, Type::Destination::OUT, Type::Destination::LINK, hash());
//_object->_link_destination.link_id(_object->_link_id);
MEM("Link object created");
}
@@ -259,8 +259,7 @@ void Link::prove() {
Bytes proof_data = signature + _object->_pub_bytes;
// CBA LINK
// CBA TODO: Determine which approach is better, passing liunk to packet or passing _link_destination
Packet proof(_object->_link_destination, proof_data, Type::Packet::PROOF, Type::Packet::LRPROOF);
//Packet proof(_object->_link_destination, *this, proof_data, Type::Packet::PROOF, Type::Packet::LRPROOF);
Packet proof(*this, proof_data, Type::Packet::PROOF, Type::Packet::LRPROOF);
proof.send();
_object->_establishment_cost += proof.raw().size();
had_outbound();
@@ -277,7 +276,7 @@ void Link::prove_packet(const Packet& packet) {
// proof_data = packet.packet_hash + signature
Bytes proof_data = packet.packet_hash() + signature;
Packet proof(_object->_link_destination, proof_data, Type::Packet::PROOF);
Packet proof(*this, proof_data, Type::Packet::PROOF);
proof.send();
had_outbound();
}
@@ -346,7 +345,16 @@ void Link::validate_proof(const Packet& packet) {
Bytes rtt_data(packer.data(), packer.size());
TRACEF("***** RTT data size: %d", rtt_data.size());
//p rtt_packet = RNS.Packet(self, rtt_data, context=RNS.Packet.LRRTT)
Packet rtt_packet(_object->_link_destination, rtt_data, Type::Packet::DATA, Type::Packet::LRRTT);
Packet rtt_packet(*this, rtt_data, Type::Packet::DATA, Type::Packet::LRRTT);
TRACEF("***** RTT packet data: %s", rtt_packet.data().toHex().c_str());
rtt_packet.pack();
Packet test_packet(RNS::Destination(RNS::Type::NONE), rtt_packet.raw());
test_packet.unpack();
TRACEF("***** RTT test packet destination hash: %s", test_packet.destination_hash().toHex().c_str());
TRACEF("***** RTT test packet data size: %d", test_packet.data().size());
TRACEF("***** RTT test packet data: %s", test_packet.data().toHex().c_str());
Bytes plaintext = decrypt(test_packet.data());
TRACEF("***** RTT test packet plaintext: %s", plaintext.toHex().c_str());
rtt_packet.send();
had_outbound();
@@ -391,7 +399,7 @@ void Link::identify(const Identity& identity) {
const Bytes signature(identity.sign(signed_data));
const Bytes proof_data(identity.get_public_key() + signature);
Packet proof(_object->_link_destination, proof_data, Type::Packet::DATA, Type::Packet::LINKIDENTIFY);
Packet proof(*this, proof_data, Type::Packet::DATA, Type::Packet::LINKIDENTIFY);
proof.send();
had_outbound();
}
@@ -423,7 +431,7 @@ const RNS::RequestReceipt Link::request(const Bytes& path, const Bytes& data /*=
}
if (packed_request.size() <= MDU) {
Packet request_packet(_object->_link_destination, packed_request, Type::Packet::DATA, Type::Packet::REQUEST);
Packet request_packet(*this, packed_request, Type::Packet::DATA, Type::Packet::REQUEST);
PacketReceipt packet_receipt = request_packet.send();
if (!packet_receipt) {
@@ -636,7 +644,7 @@ be used if a new link to the same destination is established.
void Link::teardown() {
assert(_object);
if (_object->_status != Type::Link::PENDING && _object->_status != Type::Link::CLOSED) {
Packet teardown_packet(_object->_link_destination, _object->_link_id, Type::Packet::DATA, Type::Packet::LINKCLOSE);
Packet teardown_packet(*this, _object->_link_id, Type::Packet::DATA, Type::Packet::LINKCLOSE);
teardown_packet.send();
had_outbound();
}
@@ -789,7 +797,7 @@ void Link::__watchdog_job() {
void Link::send_keepalive() {
assert(_object);
//p keepalive_packet = RNS.Packet(self, bytes([0xFF]), context=RNS.Packet.KEEPALIVE)
RNS::Packet keepalive_packet(_object->_link_destination, Bytes("\xFF"), Type::Packet::DATA, Type::Packet::KEEPALIVE);
RNS::Packet keepalive_packet(*this, Bytes("\xFF"), Type::Packet::DATA, Type::Packet::KEEPALIVE);
keepalive_packet.send();
had_outbound(true);
}
@@ -837,7 +845,7 @@ void Link::handle_request(const Bytes& request_id, const ResourceRequest& resour
if (packed_response.size() <= MDU) {
//p RNS.Packet(self, packed_response, Type::Packet::DATA, context = Type::Packet::RESPONSE).send()
RNS::Packet response_packet(_object->_link_destination, packed_response, Type::Packet::DATA, Type::Packet::RESPONSE);
RNS::Packet response_packet(*this, packed_response, Type::Packet::DATA, Type::Packet::RESPONSE);
response_packet.send();
}
else {
@@ -1203,7 +1211,7 @@ void Link::receive(const Packet& packet) {
{
if (!_object->_initiator && packet.data() == "\xFF") {
//p keepalive_packet = RNS.Packet(self, bytes([0xFE]), context=RNS.Packet.KEEPALIVE)
Packet keepalive_packet(_object->_link_destination, Bytes("\xFE"), Type::Packet::DATA, Type::Packet::KEEPALIVE);
Packet keepalive_packet(*this, Bytes("\xFE"), Type::Packet::DATA, Type::Packet::KEEPALIVE);
keepalive_packet.send();
had_outbound(true);
}
@@ -1257,13 +1265,14 @@ void Link::receive(const Packet& packet) {
const Bytes Link::encrypt(const Bytes& plaintext) {
assert(_object);
TRACE("Link::encrypt: encrypting data...");
try {
if (!_object->_token) {
try {
_object->_token.reset(new Token(_object->_derived_key));
}
catch (std::exception& e) {
ERRORF("Could not instantiate Token while performin encryption on link %s. The contained exception was: %s", toString().c_str(), e.what());
ERRORF("Could not instantiate Token while performing encryption on link %s. The contained exception was: %s", toString().c_str(), e.what());
throw e;
}
}
@@ -1277,6 +1286,7 @@ const Bytes Link::encrypt(const Bytes& plaintext) {
const Bytes Link::decrypt(const Bytes& ciphertext) {
assert(_object);
TRACE("Link::decrypt: decrypting data...");
try {
if (!_object->_token) {
_object->_token.reset(new Token(_object->_derived_key));
@@ -1421,16 +1431,23 @@ std::string Link::toString() const {
// getters
double Link::rtt() const {
assert(_object);
return _object->_rtt;
}
const Destination& Link::destination() const {
assert(_object);
return _object->_destination;
}
// CBA LINK
/*
const Destination& Link::link_destination() const {
assert(_object);
return _object->_link_destination;
}
*/
const Bytes& Link::link_id() const {
assert(_object);
@@ -1462,6 +1479,11 @@ uint16_t Link::establishment_cost() const {
return _object->_establishment_cost;
}
uint8_t Link::traffic_timeout_factor() const {
assert(_object);
return _object->_traffic_timeout_factor;
}
double Link::request_time() const {
assert(_object);
return _object->_request_time;
@@ -1519,6 +1541,21 @@ void Link::last_inbound(double time) {
_object->_last_inbound = time;
}
void Link::last_outbound(double time) {
assert(_object);
_object->_last_outbound = time;
}
void Link::increment_tx() {
assert(_object);
_object->_tx++;
}
void Link::increment_txbytes(uint16_t bytes) {
assert(_object);
_object->_txbytes += bytes;
}
void Link::status(Type::Link::status status) {
assert(_object);
_object->_status = status;
+6 -1
View File
@@ -230,9 +230,10 @@ namespace RNS {
std::string toString() const;
// getters
double rtt() const;
const Destination& destination() const;
// CBA LINK
const Destination& link_destination() const;
//const Destination& link_destination() const;
const Interface& attached_interface() const;
const Bytes& link_id() const;
const Bytes& hash() const;
@@ -240,6 +241,7 @@ namespace RNS {
Type::Link::status status() const;
double establishment_timeout() const;
uint16_t establishment_cost() const;
uint8_t traffic_timeout_factor() const;
double request_time() const;
double last_inbound() const;
std::set<RequestReceipt>& pending_requests() const;
@@ -253,6 +255,9 @@ namespace RNS {
void establishment_cost(uint16_t cost);
void request_time(double time);
void last_inbound(double time);
void last_outbound(double time);
void increment_tx();
void increment_txbytes(uint16_t bytes);
void status(Type::Link::status status);
protected:
+5 -3
View File
@@ -28,7 +28,7 @@ namespace RNS {
Destination _destination;
// CBA LINK
Destination _link_destination = {Type::NONE};
//Destination _link_destination = {Type::NONE};
Bytes _link_id;
Bytes _hash;
@@ -43,6 +43,7 @@ namespace RNS {
Type::Link::resource_strategy _resource_strategy = Type::Link::ACCEPT_NONE;
double _last_inbound = 0.0;
double _last_outbound = 0.0;
double _last_keepalive = 0.0;
double _last_proof = 0.0;
double _last_data = 0.0;
uint16_t _tx = 0;
@@ -52,13 +53,14 @@ namespace RNS {
float _rssi = 0.0;
float _snr = 0.0;
float _q = 0.0;
uint16_t _traffic_timeout_factor = Type::Link::TRAFFIC_TIMEOUT_FACTOR;
uint8_t _traffic_timeout_factor = Type::Link::TRAFFIC_TIMEOUT_FACTOR;
uint16_t _keepalive_timeout_factor = Type::Link::KEEPALIVE_TIMEOUT_FACTOR;
uint16_t _keepalive = Type::Link::KEEPALIVE;
uint16_t _stale_time = Type::Link::STALE_TIME;
bool _watchdog_lock = false;
double _activated_at = 0.0;
Type::Destination::types _type = Type::Destination::LINK;
// CBA LINK
//Type::Destination::types _type = Type::Destination::LINK;
Destination _owner = {Type::NONE};
bool _initiator = false;
uint8_t _expected_hops = 0;
+66 -54
View File
@@ -1,5 +1,6 @@
#include "Packet.h"
#include "Reticulum.h"
#include "Transport.h"
#include "Identity.h"
#include "Log.h"
@@ -47,49 +48,44 @@ Packet::Packet(const Destination& destination, const Interface& attached_interfa
}
else {
TRACE("Creating packet without destination...");
// CBA NOTE: This variant is for creating a new packet from a received raw buffer
_object->_raw = data;
_object->_packed = true;
_object->_fromPacked = true;
_object->_create_receipt = false;
}
/*p TODO
if destination and destination.type == RNS.Destination.LINK:
self.MTU = destination.mtu
else:
self.MTU = RNS.Reticulum.MTU
*/
// CBA LINK
if (_object->_destination && _object->_destination.type() == Type::Destination::LINK) {
// CBA TODO: Determine if this should actually be calling Link::mtu() !!!
_object->_MTU = _object->_destination.mtu();
}
MEM("Packet object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
// CBA LINK
//Packet::Packet(const Link& link, const Bytes& data, Type::Packet::types packet_type /*= Type::Packet::DATA*/, Type::Packet::context_types context /*= Type::Packet::CONTEXT_NONE*/, context_flag context_flag /*= FLAG_UNSET*/) :
/*
_object(new Object(link.destination(), link))
Packet::Packet(const Link& link, const Bytes& data, Type::Packet::types packet_type /*= Type::Packet::DATA*/, Type::Packet::context_types context /*= Type::Packet::CONTEXT_NONE*/, context_flag context_flag /*= FLAG_UNSET*/) :
//_object(new Object(link))
//Packet(link.destination(), data, packet_type, context, Type::Transport::BROADCAST, Type::Packet::HEADER_1, {Bytes::NONE}, true, context_flag)
// CBA Must use a destination that targets the Link itself instead of the original destination used to create the link
Packet(Destination({Type::NONE}, Type::Destination::OUT, Type::Destination::LINK, link.hash()), data, packet_type, context, Type::Transport::BROADCAST, Type::Packet::HEADER_1, {Bytes::NONE}, true, context_flag)
{
TRACE("Creating packet with link...");
_object->_destination_link = link;
_object->_MTU = link.mtu();
// CBA HACK: Need to re-build packed flags since Link was assigned
_object->_flags = get_packed_flags();
MEM("Packet link object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
*/
uint8_t Packet::get_packed_flags() {
assert(_object);
uint8_t packed_flags = 0;
if (_object->_context == LRPROOF) {
TRACE("***** Packing with LINK type");
packed_flags = (_object->_header_type << 6) | (_object->_transport_type << 4) | (Type::Destination::LINK << 2) | _object->_packet_type;
}
else {
if (!_object->_destination) {
throw std::logic_error("Packet destination is required");
}
TRACEF("***** Packing with %d type", _object->_destination.type());
packed_flags = (_object->_header_type << 6) | (_object->_transport_type << 4) | (_object->_destination.type() << 2) | _object->_packet_type;
}
return packed_flags;
@@ -277,64 +273,73 @@ void Packet::pack() {
}
_object->_destination_hash = _object->_destination.hash();
_object->_raw.clear();
_object->_header.clear();
_object->_encrypted = false;
_object->_raw << _object->_flags;
_object->_raw << _object->_hops;
_object->_header << _object->_flags;
_object->_header << _object->_hops;
// CBA LINK
if (_object->_context == LRPROOF) {
TRACE("Packet::pack: destination link id: " + _object->_destination.link_id().toHex() );
_object->_raw << _object->_destination.link_id();
_object->_raw << (uint8_t)_object->_context;
_object->_raw << _object->_data;
if (!_object->_destination_link) throw std::invalid_argument("Packet is not associated with a Link");
TRACE("Packet::pack: destination link id: " + _object->_destination_link.link_id().toHex() );
_object->_header << _object->_destination_link.link_id();
_object->_ciphertext = _object->_data;
}
else {
if (_object->_header_type == HEADER_1) {
TRACE("Packet::pack: destination hash: " + _object->_destination.hash().toHex() );
_object->_raw << _object->_destination.hash();
_object->_raw << (uint8_t)_object->_context;
_object->_header << _object->_destination.hash();
if (_object->_packet_type == ANNOUNCE) {
// Announce packets are not encrypted
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
else if (_object->_packet_type == LINKREQUEST) {
// Link request packets are not encrypted
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
else if (_object->_packet_type == PROOF && _object->_context == RESOURCE_PRF) {
// Resource proofs are not encrypted
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
// CBA LINK
//p elif self.packet_type == Packet.PROOF and self.destination.type == RNS.Destination.LINK:
else if (_object->_packet_type == PROOF && _object->_destination.type() == Type::Destination::LINK) {
//else if (_object->_packet_type == PROOF && _object->_destination_link) {
// Packet proofs over links are not encrypted
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
else if (_object->_context == RESOURCE) {
// A resource takes care of encryption
// by itself
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
else if (_object->_context == KEEPALIVE) {
// Keepalive packets contain no actual
// data
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
else if (_object->_context == CACHE_REQUEST) {
// Cache-requests are not encrypted
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
else {
// In all other cases, we encrypt the packet
// with the destination's encryption method
// CBA LINK
// CBA TODO Determine if we need to use link (_destination_link) to encrypt here
// (if not then likely there is no need for the added _destination_link)
_object->_raw << _object->_destination.encrypt(_object->_data);
if (_object->_destination_link) {
_object->_ciphertext = _object->_destination_link.encrypt(_object->_data);
TRACEF("***** Link data: %s", _object->_ciphertext.toHex().c_str());
}
else {
_object->_ciphertext = _object->_destination.encrypt(_object->_data);
TRACEF("***** Destination Data: %s", _object->_ciphertext.toHex().c_str());
}
// CBA RATCHET
/*p TODO
if hasattr(self.destination, "latest_ratchet_id"):
self.ratchet_id = self.destination.latest_ratchet_id
*/
_object->_encrypted = true;
}
}
@@ -344,13 +349,12 @@ void Packet::pack() {
}
TRACE("Packet::pack: transport id: " + _object->_transport_id.toHex() );
TRACE("Packet::pack: destination hash: " + _object->_destination.hash().toHex() );
_object->_raw << _object->_transport_id;
_object->_raw << _object->_destination.hash();
_object->_raw << (uint8_t)_object->_context;
_object->_header << _object->_transport_id;
_object->_header << _object->_destination.hash();
if (_object->_packet_type == ANNOUNCE) {
// Announce packets are not encrypted
_object->_raw << _object->_data;
_object->_ciphertext = _object->_data;
}
// CBA No default encryption here like with header type HEADER_1 ???
// CBA Is there any packet type besides ANNOUNCE with header type HEADER_2 ???
@@ -358,6 +362,9 @@ void Packet::pack() {
}
}
_object->_header << (uint8_t)_object->_context;
_object->_raw = _object->_header + _object->_ciphertext;
if (_object->_raw.size() > _object->_MTU) {
throw std::length_error("Packet size of " + std::to_string(_object->_raw.size()) + " exceeds MTU of " + std::to_string(_object->_MTU) +" bytes");
}
@@ -431,15 +438,16 @@ PacketReceipt Packet::send() {
throw std::logic_error("Packet was already sent");
}
// CBA LINK
//p if self.destination.type == RNS.Destination.LINK:
if (_object->_destination.type() == Type::Destination::LINK) {
//if (_object->_destination_link) {
if (_object->_destination.status() == Type::Link::CLOSED) {
if (!_object->_destination_link) throw std::invalid_argument("Packet is not associated with a Link");
if (_object->_destination_link.status() == Type::Link::CLOSED) {
throw std::runtime_error("Attempt to transmit over a closed link");
}
else {
_object->_destination.last_outbound(OS::time());
_object->_destination.increment_tx();
_object->_destination.increment_txbytes(_object->_data.size());
_object->_destination_link.last_outbound(OS::time());
_object->_destination_link.increment_tx();
_object->_destination_link.increment_txbytes(_object->_data.size());
}
}
@@ -493,14 +501,15 @@ bool Packet::resend() {
void Packet::prove(const Destination& destination /*= {Type::NONE}*/) {
assert(_object);
TRACE("Packet::prove: proving packet...");
if (!_object->_destination) {
throw std::logic_error("Packet destination is required");
}
// CBA LINK
// CBA TODO: Determine under which circumstances to use _destination and which to use _link since it's unclear from this logic
//p if self.fromPacked and hasattr(self, "destination") and self.destination:
if (_object->_fromPacked && _object->_destination) {
if (_object->_destination.identity() && _object->_destination.identity().prv()) {
_object->_destination.identity().prove(*this, destination);
}
}
//p elif self.fromPacked and hasattr(self, "link") and self.link:
else if (_object->_fromPacked && _object->_link) {
_object->_link.prove_packet(*this);
}
@@ -670,7 +679,6 @@ std::string Packet::dumpString() const {
}
// CBA LINK
if (_object->_destination && _object->_destination.type() == Type::Destination::LINK) {
//if (_object->_destination && _object->_destination_link) {
encrypted = false;
}
break;
@@ -794,13 +802,15 @@ PacketReceipt::PacketReceipt(const Packet& packet) : _object(new Object()) {
// CBA LINK
if (packet.destination().type() == Type::Destination::LINK) {
//if (packet.destination().destination_link()) {
// CBA BUG? Destination does not have rtt or traffic_timeout_factor members
//z _object->_timeout = packet.destination().rtt() * packet.destination().traffic_timeout_factor();
_object->_timeout = TIMEOUT_PER_HOP * Transport::hops_to(_object->_destination.hash());
if (!packet.destination_link()) throw std::invalid_argument("Packet is not associated with a Link");
//p self.timeout = max(packet.destination.rtt * packet.destination.traffic_timeout_factor, RNS.Link.TRAFFIC_TIMEOUT_MIN_MS/1000)
_object->_timeout = std::max(packet.destination_link().rtt() * packet.destination_link().traffic_timeout_factor(), (double)RNS::Type::Link::TRAFFIC_TIMEOUT_MIN_MS/1000);
}
else {
_object->_timeout = TIMEOUT_PER_HOP * Transport::hops_to(_object->_destination.hash());
//p self.timeout = RNS.Reticulum.get_instance().get_first_hop_timeout(self.destination.hash)
//p self.timeout += Packet.TIMEOUT_PER_HOP * RNS.Transport.hops_to(self.destination.hash)
_object->_timeout = RNS::Reticulum::get_instance().get_first_hop_timeout(_object->_destination.hash());
_object->_timeout += TIMEOUT_PER_HOP * Transport::hops_to(_object->_destination.hash());
}
}
@@ -885,6 +895,8 @@ bool PacketReceipt::validate_proof(const Bytes& proof) {
bool PacketReceipt::validate_proof(const Bytes& proof, const Packet& proof_packet) {
assert(_object);
TRACE("PacketReceipt::validate_proof: validating proof...");
// CBA LINK
// CBA TODO: Determine whether to use destination.identity or link.identity here!!!
if (proof.size() == EXPL_LENGTH) {
// This is an explicit proof
Bytes proof_hash = proof.left(Type::Identity::HASHLENGTH/8);
+7 -4
View File
@@ -174,7 +174,6 @@ namespace RNS {
Type::Packet::context_flag context_flag = Type::Packet::FLAG_UNSET
) : Packet(destination, {Type::NONE}, data, packet_type, context, transport_type, header_type, transport_id, create_receipt, context_flag) {}
// CBA LINK
/*
Packet(
const Link& link,
const Bytes& data,
@@ -182,7 +181,6 @@ namespace RNS {
Type::Packet::context_types context = Type::Packet::CONTEXT_NONE,
Type::Packet::context_flag context_flag = Type::Packet::FLAG_UNSET
);
*/
virtual ~Packet() {
MEM("Packet object destroyed, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
@@ -248,7 +246,7 @@ namespace RNS {
inline const Bytes& raw() const { assert(_object); return _object->_raw; }
inline const Bytes& data() const { assert(_object); return _object->_data; }
// CBA LINK
//inline const Link& destination_link() const { assert(_object); return _object->_destination_link; }
inline const Link& destination_link() const { assert(_object); return _object->_destination_link; }
//CBA Following method is only used by Resource to access decrypted resource advertisement form Link. Consider a better way.
inline const Bytes& plaintext() { assert(_object); return _object->_plaintext; }
@@ -276,12 +274,14 @@ namespace RNS {
Object(const Destination& destination, const Interface& attached_interface) : _destination(destination), _attached_interface(attached_interface) { MEM("Packet::Data object created, this: " + std::to_string((uintptr_t)this)); }
// CBA LINK
//Object(const Destination& destination, const Link& destination_link) : _destination(destination), _destination_link(destination_link) { MEM("Packet::Data object created, this: " + std::to_string((uintptr_t)this)); }
//Object(const Link& link) : _destination(link.destination()), _destination_link(link) { MEM("Packet::Data object created, this: " + std::to_string((uintptr_t)this)); }
virtual ~Object() { MEM("Packet::Data object destroyed, this: " + std::to_string((uintptr_t)this)); }
private:
Destination _destination = {Type::NONE};
// CBA LINK
//Link _destination_link = {Type::NONE};
// CBA TODO: Determine if _link (assigned late by Transport) and _destination_link (assigned in constructor) can be one and the same !!!
Link _destination_link = {Type::NONE};
Link _link = {Type::NONE};
@@ -324,6 +324,9 @@ namespace RNS {
Bytes _plaintext; // used exclusively to relay decrypted resource advertisement form Link to Resource
Bytes _header;
Bytes _ciphertext;
friend class Packet;
};
std::shared_ptr<Object> _object;
+13 -6
View File
@@ -255,7 +255,8 @@ using namespace RNS::Utilities;
// Process active and pending link lists
if (OS::time() > (_links_last_checked + _links_check_interval)) {
for (auto& link : _pending_links) {
std::set<Link> pending_links(_pending_links);
for (auto& link : pending_links) {
if (link.status() == Type::Link::CLOSED) {
// If we are not a Transport Instance, finding a pending link
// that was never activated will trigger an expiry of the path
@@ -287,7 +288,8 @@ using namespace RNS::Utilities;
_pending_links.erase(link);
}
}
for (auto& link : _active_links) {
std::set<Link> active_links(_active_links);
for (auto& link : active_links) {
if (link.status() == Type::Link::CLOSED) {
_active_links.erase(link);
}
@@ -798,7 +800,8 @@ using namespace RNS::Utilities;
bool should_transmit = true;
if (packet.destination().type() == Type::Destination::LINK) {
if (packet.destination().status() == Type::Link::CLOSED) {
if (!packet.destination_link()) throw std::invalid_argument("Packet is not associated with a Link");
if (packet.destination_link().status() == Type::Link::CLOSED) {
TRACE("Transport::outbound: Pscket destination is link-closed, not transmitting");
should_transmit = false;
}
@@ -2057,6 +2060,7 @@ using namespace RNS::Utilities;
auto& destination = (*iter).second;
if (destination.type() == packet.destination_type()) {
#endif
TRACE("Transport::inbound: Found local destination for LINKREQUEST");
packet.destination(destination);
// CBA iterator over std::set is always const so need to make temporarily mutable
//destination.receive(packet);
@@ -2076,7 +2080,8 @@ using namespace RNS::Utilities;
if (packet.destination_type() == Type::Destination::LINK) {
// Data is destined for a link
TRACE("Transport::inbound: Packet is DATA for a LINK");
for (auto& link : _active_links) {
std::set<Link> active_links(_active_links);
for (auto& link : active_links) {
if (link.link_id() == packet.destination_hash()) {
TRACE("Transport::inbound: Packet is DATA for an active LINK");
packet.link(link);
@@ -2196,7 +2201,8 @@ using namespace RNS::Utilities;
}
else if (packet.context() == Type::Packet::RESOURCE_PRF) {
TRACE("Transport::inbound: Packet is RESOURCE PROOF");
for (auto& link : _active_links) {
std::set<Link> active_links(_active_links);
for (auto& link : active_links) {
if (link.link_id() == packet.destination_hash()) {
const_cast<Link&>(link).receive(packet);
}
@@ -2205,7 +2211,8 @@ using namespace RNS::Utilities;
else {
TRACE("Transport::inbound: Packet is regular PROOF");
if (packet.destination_type() == Type::Destination::LINK) {
for (auto& link : _active_links) {
std::set<Link> active_links(_active_links);
for (auto& link : active_links) {
if (link.link_id() == packet.destination_hash()) {
packet.link(link);
}