27template <
typename NIC>
35 typedef std::uint16_t
Port;
45 Header() : _from_port(0), _to_port(0), _size(0) {}
53 unsigned int size()
const {
return _size; }
54 void size(
unsigned int s) { _size =
s; }
112 return reinterpret_cast<T*
>(
214 bool requires_authentication(
const void*
data,
unsigned int size);
219 struct NeighborRSUInfo {
224 NeighborRSUInfo(
unsigned int id,
const MacKeyType&
k,
const Address&
addr)
225 : rsu_id(id), key(
k), address(
addr) {}
227 std::vector<NeighborRSUInfo> _neighbor_rsus;
228 std::mutex _neighbor_rsus_mutex;
237template <
typename NIC>
240template <
typename NIC>
243template <
typename NIC>
246template <
typename NIC>
251template <
typename NIC>
256template <
typename NIC>
261template <
typename NIC>
266template <
typename NIC>
271template <
typename NIC>
273 return (_paddr ==
a._paddr) && (_port ==
a._port);
276template <
typename NIC>
279 return mac_addr +
":" + std::to_string(_port);
283template <
typename NIC>
287 throw std::invalid_argument(
"NIC pointer cannot be null");
295template <
typename NIC>
297 _nic->detach(
this, PROTO);
301template <
typename NIC>
305 if (!
data || size == 0) {
306 db<Protocol>(
WRN) <<
"[Protocol] SEND - Invalid data or size. Dropping message.\n";
321 case ProtocolMessage::Type::RESPONSE:
type_name =
"RESPONSE";
break;
322 case ProtocolMessage::Type::INTEREST:
type_name =
"INTEREST";
break;
323 case ProtocolMessage::Type::STATUS:
type_name =
"STATUS";
break;
324 case ProtocolMessage::Type::REQ:
type_name =
"REQ";
break;
325 case ProtocolMessage::Type::KEY_RESPONSE:
type_name =
"KEY_RESPONSE";
break;
329 <<
" (" <<
type_name <<
"), size: " << size <<
" bytes\n";
333 if (requires_authentication(
data, size)) {
334 db<Protocol>(
INF) <<
"[Protocol] SEND - Message requires authentication - checking key availability\n";
338 if (_entity_type == EntityType::VEHICLE) {
340 if (_vehicle_rsu_manager) {
341 auto known_rsus = _vehicle_rsu_manager->get_known_rsus();
348 for (
size_t j = 0;
j < 16; ++
j) {
357 db<Protocol>(
WRN) <<
"[Protocol] SEND - Vehicle has no RSU manager - no authentication possible\n";
365 for (
size_t i = 0;
i < 16; ++
i) {
375 db<Protocol>(
WRN) <<
"[Protocol] SEND - DROPPING authenticated message - no authentication keys available\n";
379 db<Protocol>(
INF) <<
"[Protocol] SEND - Authentication keys available - proceeding with message send\n";
381 db<Protocol>(
INF) <<
"[Protocol] SEND - Message does NOT require authentication\n";
407 packet->timestamps()->tx_timestamp = TimestampType::min();
416 packet->authentication()->has_mac =
false;
417 packet->authentication()->mac.fill(0);
420 if (requires_authentication(
data, size)) {
421 db<Protocol>(
INF) <<
"[Protocol] SEND - Calculating Hybrid MAC for authenticated message\n";
427 db<Protocol>(
INF) <<
"[Protocol] SEND - MAC Auth Header: from_port=" <<
packet->header()->from_port()
428 <<
", to_port=" <<
packet->header()->to_port() <<
", size=" <<
packet->header()->size() <<
"\n";
430 <<
", y=" <<
packet->coordinates()->y <<
", radius=" <<
packet->coordinates()->radius <<
"\n";
434 for (
size_t i = 0;
i < 16; ++
i) {
447 for (
size_t i = 0;
i < 16; ++
i) {
456 packet->authentication()->has_mac =
true;
458 db<Protocol>(
INF) <<
"[Protocol] SEND - Added MAC authentication to outgoing packet\n";
460 db<Protocol>(
INF) <<
"[Protocol] SEND - No authentication required - MAC left as zeros\n";
465 db<Protocol>(
INF) <<
"[Protocol] SEND - NIC::send() returned " <<
result <<
", clock_synchronized=" <<
packet->timestamps()->is_clock_synchronized <<
"\n";
471template <
typename NIC>
476 db<Protocol>(
WRN) <<
"[Protocol] receive() called with a null buffer.\n";
486 db<Protocol>(
TRC) <<
"Protocol<NIC>::receive() called after release!\n";
494 db<Protocol>(
WRN) <<
"[Protocol] No data received or error occurred.\n";
503 from->port(
pkt->header()->from_port());
517template <
typename NIC>
519 _observed.attach(
obs, address.
port());
523template <
typename NIC>
525 _observed.detach(
obs, address.
port());
529template <
typename NIC>
534 db<Protocol>(
INF) <<
"[Protocol] data received, but buffer is null. Releasing buffer.\n";
555 if (_entity_type == EntityType::RSU &&
payload_size > 0) {
567 (
raw_msg_type ==
static_cast<uint8_t>(ProtocolMessage::Type::STATUS)) ?
"STATUS" :
"KEY_RESPONSE";
578 db<Protocol>(
INF) <<
"[Protocol] Received packet with sender_clock_synchronized="
598 if (is_authenticated_message_type(
msg_type)) {
603 pkt->timestamps(),
pkt->coordinates(),
pkt->authentication()->mac)) {
604 db<Protocol>(
WRN) <<
"[Protocol] Hybrid MAC verification failed - packet may be tampered\n";
607 if (_entity_type == EntityType::VEHICLE && _vehicle_rsu_manager) {
608 db<Protocol>(
INF) <<
"[Protocol] Sending REQ message to leader RSU for failed authentication\n";
610 pkt->header(),
pkt->timestamps(),
pkt->coordinates());
612 db<Protocol>(
INF) <<
"[Protocol] RSU dropping message with failed MAC\n";
619 db<Protocol>(
INF) <<
"[Protocol] Hybrid MAC verification successful - packet integrity confirmed\n";
632 if (
raw_msg_type ==
static_cast<uint8_t>(ProtocolMessage::Type::REQ) && _entity_type == EntityType::RSU) {
640 if (
raw_msg_type ==
static_cast<uint8_t>(ProtocolMessage::Type::KEY_RESPONSE) && _entity_type == EntityType::VEHICLE) {
641 db<Protocol>(
INF) <<
"[Protocol] Intercepted KEY_RESPONSE message at vehicle\n";
650 if (!Protocol::_observed.notify(
buf)) {
651 db<Protocol>(
INF) <<
"[Protocol] data received, but no one was notified for port. Releasing buffer.\n";
659template <
typename NIC>
668 if (_entity_type != EntityType::VEHICLE || !_vehicle_rsu_manager) {
669 db<Protocol>(
INF) <<
"[Protocol] Ignoring STATUS message (not a vehicle or no RSU manager)\n";
675 if (
status_msg.message_type() != ProtocolMessage::Type::STATUS) {
676 db<Protocol>(
WRN) <<
"[Protocol] Failed to deserialize STATUS message\n";
682 unsigned int value_size =
status_msg.value_size();
683 if (value_size < (
sizeof(
double) * 3 +
sizeof(
MacKeyType))) {
684 db<Protocol>(
WRN) <<
"[Protocol] STATUS message payload too small: " << value_size <<
"\n";
690 for (
size_t i = 0;
i < value_size; ++
i) {
713 if (_vehicle_rsu_manager) {
721template <
typename NIC>
729template <
typename NIC>
732 _nic->setRadius(radius);
733 db<Protocol>(
INF) <<
"[Protocol] NIC transmission radius set to " << radius <<
"m\n";
738template <
typename NIC>
769 Port from_port =
header->from_port();
770 Port to_port =
header->to_port();
791 db<Protocol>(
INF) <<
"[Protocol] Hybrid MAC - Header: from_port=" << from_port
807 for (
size_t i = 0;
i < 16; ++
i) {
813 for (
size_t i = 0;
i < 16; ++
i) {
823template <
typename NIC>
829 for (
size_t i = 0;
i < 16; ++
i) {
838 <<
", to_port=" <<
header->to_port() <<
", size=" <<
header->size() <<
"\n";
840 <<
" (tx_timestamp excluded from MAC calculation)\n";
856 if (_entity_type == EntityType::VEHICLE && _vehicle_rsu_manager) {
857 auto known_rsus = _vehicle_rsu_manager->get_known_rsus();
858 auto neighbor_keys = _vehicle_rsu_manager->get_neighbor_rsu_keys();
861 <<
" known RSU keys and " <<
neighbor_keys.size() <<
" neighbor RSU keys\n";
869 for (
size_t i = 0;
i < 16; ++
i) {
880 for (
size_t i = 0;
i < 16; ++
i) {
888 db<Protocol>(
TRC) <<
"[Protocol] Hybrid MAC verified with known RSU " <<
rsu.address.to_string() <<
" key\n";
899 for (
size_t i = 0;
i < 16; ++
i) {
910 for (
size_t i = 0;
i < 16; ++
i) {
918 db<Protocol>(
TRC) <<
"[Protocol] Hybrid MAC verified with neighbor RSU key " <<
idx <<
"\n";
923 db<Protocol>(
TRC) <<
"[Protocol] Hybrid MAC verification failed - no matching RSU or neighbor key found\n";
932 for (
size_t i = 0;
i < 16; ++
i) {
943 for (
size_t i = 0;
i < 16; ++
i) {
952 db<Protocol>(
TRC) <<
"[Protocol] Hybrid MAC verification " << (
is_valid ?
"successful" :
"failed") <<
" with leader key\n";
956template <
typename NIC>
958 if (size < 1)
return false;
963 return is_authenticated_message_type(
static_cast<typename ProtocolMessage::Type
>(
msg_type));
966template <
typename NIC>
971 return (type == ProtocolMessage::Type::RESPONSE);
975template <
typename NIC>
981 if (_entity_type != EntityType::RSU) {
982 db<Protocol>(
WRN) <<
"[Protocol] Non-RSU entity received REQ message - ignoring\n";
987 if (
req_msg.message_type() != ProtocolMessage::Type::REQ) {
1010 offset +=
sizeof(Header);
1013 offset +=
sizeof(TimestampFields);
1028 <<
") from header is too large for the received REQ payload size (" <<
req_value_size <<
"). Dropping.\n";
1048 for (
size_t i = 0;
i < 16; ++
i) {
1067 std::lock_guard<std::mutex>
lock(_neighbor_rsus_mutex);
1068 db<Protocol>(
INF) <<
"[Protocol] REQ - Searching through " << _neighbor_rsus.size() <<
" neighbor RSUs for a matching key.\n";
1069 for (
const auto&
neighbor : _neighbor_rsus) {
1072 for (
size_t i = 0;
i < 16; ++
i) {
1085 for (
size_t i = 0;
i < 16; ++
i) {
1106 ProtocolMessage*
resp_msg =
new ProtocolMessage(ProtocolMessage::Type::KEY_RESPONSE,
1107 address(), 0, ProtocolMessage::ZERO,
1116 db<Protocol>(
INF) <<
"[Protocol] Successfully sent KEY_RESPONSE message.\n";
1123 db<Protocol>(
INF) <<
"[Protocol] REQ - No matching neighbor RSU key found for the failed message.\n";
1128 for (
size_t i = 0;
i < 16; ++
i) {
1139 for (
size_t i = 0;
i < 16; ++
i) {
1149template <
typename NIC>
1151 db<Protocol>(
INF) <<
"[Protocol] Vehicle processing KEY_RESPONSE message\n";
1154 if (_entity_type != EntityType::VEHICLE || !_vehicle_rsu_manager) {
1155 db<Protocol>(
WRN) <<
"[Protocol] Non-vehicle entity or no RSU manager for KEY_RESPONSE message - ignoring\n";
1161 if (
resp_msg.message_type() != ProtocolMessage::Type::KEY_RESPONSE) {
1162 db<Protocol>(
WRN) <<
"[Protocol] Failed to deserialize KEY_RESPONSE message\n";
1171 db<Protocol>(
WRN) <<
"[Protocol] KEY_RESPONSE message payload size mismatch: expected "
1182 for (
size_t i = 0;
i < 16; ++
i) {
1190 _vehicle_rsu_manager->add_neighbor_rsu_key(
neighbor_key);
1192 db<Protocol>(
INF) <<
"[Protocol] Successfully added neighbor RSU key to vehicle storage\n";
1196template <
typename NIC>
1200 db<Protocol>(
INF) <<
"[Protocol] Preparing REQ message for leader RSU\n";
1203 if (_entity_type != EntityType::VEHICLE || !_vehicle_rsu_manager) {
1204 db<Protocol>(
WRN) <<
"[Protocol] Non-vehicle entity or no RSU manager - cannot send REQ\n";
1209 auto current_leader = _vehicle_rsu_manager->get_current_leader();
1211 db<Protocol>(
WRN) <<
"[Protocol] No current leader RSU - cannot send REQ message\n";
1224 offset +=
sizeof(Header);
1228 offset +=
sizeof(TimestampFields);
1242 ProtocolMessage*
req_msg =
new ProtocolMessage(ProtocolMessage::Type::REQ,
1243 address(), 0, ProtocolMessage::ZERO,
1250 db<Protocol>(
INF) <<
"[Protocol] Successfully sent REQ message to leader\n";
1252 db<Protocol>(
WRN) <<
"[Protocol] Failed to send REQ message to leader\n";
1259template <
typename NIC>
1263template <
typename NIC>
1270template <
typename NIC>
1272 if (_entity_type == EntityType::VEHICLE) {
1273 _vehicle_rsu_manager =
manager;
1274 db<Protocol>(
INF) <<
"[Protocol] RSU manager attached to vehicle protocol\n";
1276 db<Protocol>(
WRN) <<
"[Protocol] Attempted to attach RSU manager to non-vehicle entity\n";
1281template <
typename NIC>
1283 if (_entity_type != EntityType::RSU) {
1284 db<Protocol>(
WRN) <<
"[Protocol] Attempted to add neighbor RSU to non-RSU entity\n";
1288 std::lock_guard<std::mutex>
lock(_neighbor_rsus_mutex);
1291 for (
const auto&
neighbor : _neighbor_rsus) {
1293 db<Protocol>(
INF) <<
"[Protocol] Neighbor RSU " << rsu_id <<
" already known\n";
1298 _neighbor_rsus.emplace_back(rsu_id, key, address);
1300 db<Protocol>(
INF) <<
"[Protocol] Added neighbor RSU " << rsu_id <<
" to protocol (total: " << _neighbor_rsus.size() <<
")\n";
1303template <
typename NIC>
1305 if (_entity_type != EntityType::RSU) {
1309 std::lock_guard<std::mutex>
lock(_neighbor_rsus_mutex);
1310 _neighbor_rsus.clear();
1311 db<Protocol>(
INF) <<
"[Protocol] Cleared all neighbor RSUs from protocol\n";
1314template <
typename NIC>
static Clock & getInstance()
Get the singleton instance.
Definition clock.h:145
void attach(Observer *o, Condition c)
Definition observed.h:35
static std::string mac_to_string(Address addr)
Definition ethernet.h:36
static const Ethernet::Address BROADCAST
Definition ethernet.h:54
static constexpr unsigned int HEADER_SIZE
Definition ethernet.h:26
static constexpr unsigned int MTU
Definition ethernet.h:12
static double euclideanDistance(double x1, double y1, double x2, double y2)
Definition geo_utils.h:16
MacKeyType getGroupMacKey() const
Get the current group MAC key.
Definition leaderKeyStorage.h:124
static LeaderKeyStorage & getInstance()
Get the singleton instance.
Definition leaderKeyStorage.h:54
static void getCurrentCoordinates(double &x, double &y)
Definition location_service.h:91
Template class for network messages with Clock integration.
Definition message.h:31
Type
Definition message.h:35
Ethernet::Protocol Protocol_Number
Definition nic.h:36
Definition protocol.h:134
void port(Port port)
Definition protocol.h:257
Null
Definition protocol.h:136
@ NULL_VALUE
Definition protocol.h:136
bool operator==(const Address &a) const
Definition protocol.h:272
const Port & port() const
Definition protocol.h:262
Address()
Definition protocol.h:238
static const Address BROADCAST
Definition protocol.h:151
const std::string to_string() const
Definition protocol.h:277
const Physical_Address & paddr() const
Definition protocol.h:252
T * data()
Definition protocol.h:111
AuthenticationFields * authentication()
Definition protocol.h:104
TimestampFields * timestamps()
Definition protocol.h:92
static constexpr unsigned int sync_status_offset()
Definition protocol.h:118
static constexpr unsigned int tx_timestamp_offset()
Definition protocol.h:122
Coordinates * coordinates()
Definition protocol.h:98
Packet()
Definition protocol.h:88
Header * header()
Definition protocol.h:90
int receive(Buffer *buf, Address *from, void *data, unsigned int size)
Definition protocol.h:472
Conditional_Data_Observer< Buffer, Port > Observer
Definition protocol.h:39
int send(Address from, Address to, const void *data, unsigned int size)
Definition protocol.h:302
void free(Buffer *buf)
Definition protocol.h:722
void setRadius(double radius)
Definition protocol.h:730
NIC::Address Physical_Address
Definition protocol.h:34
std::uint16_t Port
Definition protocol.h:35
void set_vehicle_rsu_manager(VehicleRSUManager< Protocol< NIC > > *manager)
Definition protocol.h:1271
EntityType
Definition protocol.h:161
std::uint8_t Data[MTU]
Definition protocol.h:83
void clear_neighbor_rsus()
Definition protocol.h:1304
NIC::DataBuffer Buffer
Definition protocol.h:33
~Protocol()
Definition protocol.h:296
static void detach(Observer *obs, Address address)
Definition protocol.h:524
static const NIC::Protocol_Number PROTO
Definition protocol.h:31
Protocol(NIC *nic, EntityType entity_type=EntityType::UNKNOWN)
Definition protocol.h:284
void add_neighbor_rsu(unsigned int rsu_id, const MacKeyType &key, const Address &address)
Definition protocol.h:1282
Message< Protocol< NIC > > ProtocolMessage
Definition protocol.h:36
static const unsigned int MTU
Definition protocol.h:82
static void attach(Observer *obs, Address address)
Definition protocol.h:518
Address address() const
Definition protocol.h:1315
Conditionally_Data_Observed< Buffer, Port > Observed
Definition protocol.h:40
class Protocol::Address __attribute__
Definition vehicleRSUManager.h:33
uint32_t LeaderIdType
Definition clock.h:26
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > TimestampType
Definition clock.h:16
@ INF
Definition debug.h:208
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
@ TRC
Definition debug.h:231
@ WRN
Definition debug.h:185
Protocol prot
Definition ethernet.h:2
std::uint8_t payload[MTU]
Definition ethernet.h:3
std::array< uint8_t, 16 > MacKeyType
Definition leaderKeyStorage.h:15
Coordinates * coordinates()
Definition protocol.h:11
TimestampFields * timestamps()
Definition protocol.h:5
T * data()
Definition protocol.h:24
Header * header()
Definition protocol.h:3
Packet()
Definition protocol.h:1
Definition location_service.h:15
double x
Definition location_service.h:16
double radius
Definition location_service.h:18
double y
Definition location_service.h:17
bool has_mac
Definition protocol.h:75
MacKeyType mac
Definition protocol.h:74
AuthenticationFields()
Definition protocol.h:77
bool is_clock_synchronized
Definition protocol.h:64
TimestampFields()
Definition protocol.h:67
TimestampType tx_timestamp
Definition protocol.h:65
LeaderIdType sender_id
Definition clock.h:31