40#include "uwsecurity_clmsg.h"
41#include <clmsg-discovery.h>
57 : TclClass(
"Module/UW/FLOODINGSEC")
75 : PacketHeaderClass(
"PacketHeader/FLOODING", sizeof(
hdr_uwflooding))
87 , neighbor_addr(neigh_addr)
109 std::cout << NOW <<
"::addUnconfirmedPkt::Node="
110 << (int)module->ipAddr_ <<
"::Neighbor="
112 <<
"::inserted unconfirmed packet" << std::endl;
116 if (NOW < expire_time) {
118 std::cout << NOW <<
"::addUnconfirmedPkt::Node="
119 << (int) module->
ipAddr_ <<
"::Neighbor="
121 << expire_time << std::endl;
122 resched(expire_time-NOW);
126 std::cerr<<NOW <<
"NeighborReputationHandler::addUnconfirmedPkt"
127 <<
"::error:expire time already past"<< std::endl;
141 std::cout << NOW <<
"::checkUnconfirmedPkt::Node="
142 <<(int)module->ipAddr_ <<
"::Neighbor="
144 <<
"::removed from the map" << std::endl;
147 if (module->reputation) {
148 ChannelBasedMetricsReputation metrics =
151 module->reputation->updateReputation(neighbor_addr, &metrics);
159 resched(next_time - NOW);
161 std::cout << NOW <<
"::checkUnconfirmedPkt::Node="
162 << (int) module->ipAddr_ <<
"::Neighbor="
164 <<
"::expire time=" << next_time << std::endl;
180 std::cout << NOW <<
"::NeighborReputationHandler::expire::Node="
181 << (int)module->ipAddr_ <<
"::Neighbor=" << (
int)
neighbor_addr
182 <<
"::expected_uid=" <<
waiting_uid <<
"::timer expired"
186 module->retrieveInstantNoise(neighbor_addr);
187 if (module->reputation) {
188 ChannelBasedMetricsReputation metrics =
191 module->reputation->updateReputation(neighbor_addr, &metrics);
199 resched(next_time-NOW);
201 std::cout << NOW <<
"::NeighborReputationHandler::expire::Node="
202 << (int)module->ipAddr_ <<
"::Neighbor="
204 <<
"::expire time=" << next_time << std::endl;
209 std::cout << NOW <<
"::NeighborReputationHandler::expire::Node="
210 << (int)module->
ipAddr_ <<
"::Neighbor="
223 if (it_f->second < it->second) {
227 if (NOW <= it->second) {
229 exp_time = it->second;
243 if (it->second < NOW ) {
245 std::cout << NOW <<
"::removeOldPackets::Node="
246 << (int)module->
ipAddr_ <<
"::Neighbor="
248 << it->first <<
"::not forwarded" << std::endl;
251 if (module->reputation) {
252 ChannelBasedMetricsReputation metrics =
255 module->reputation->updateReputation(neighbor_addr, &metrics);
263 double last_noise_val)
275 std::cout << NOW <<
"::Node=" << (int)module->ipAddr_ <<
"::Neighbor="
277 <<
"::val snr=" << val_snr << std::endl;
285 std::cout << NOW <<
"::Node=" << (int)module->
ipAddr_ <<
"::Neighbor="
287 <<
"::inst noise =" <<
inst_noise << std::endl;
293 , maximum_cache_time_(60)
295 , packets_forwarded_(0)
297 , trace_file_path_name_((char *)
"trace")
299 , use_reputation(false)
303 , reputation(nullptr)
305 , valid_phy_id(false)
311 bind(
"forward_timeout_", &
fwd_to);
322 if (m->type() == CLMSG_OVERHEARING_PKT) {
323 ClMsgOverhearingPkt *msg =
dynamic_cast<ClMsgOverhearingPkt *
>(m);
325 if (msg->getPacket(pkt)) {
326 hdr_cmn* ch = HDR_CMN(pkt);
334 return Module::recvSyncClMsg(m);
341 ClMsgStats stats_clmsg = ClMsgStats(
stats_phy_id, UNICAST);
342 sendSyncClMsg(&stats_clmsg);
345 stats_clmsg.getStats());
351 std::cout << NOW <<
"::Node=" << (int)
ipAddr_
352 <<
"::Neighbor=" << neighbor_addr
353 <<
"::Received packet with"
358 <<
"::SNR=" << snr_db << std::endl;
361 neighbor_timer_map::iterator it =
363 it->second.updateChannelMetrics(snr_db,
376 ClMsgStats stats_clmsg = ClMsgStats(
stats_phy_id, UNICAST);
377 sendSyncClMsg(&stats_clmsg);
380 stats_clmsg.getStats());
383 neighbor_timer_map::iterator it =
396 return Module::recvAsyncClMsg(m);
402 Tcl &tcl = Tcl::instance();
405 if (strcasecmp(argv[1],
"getpacketsforwarded") == 0) {
408 }
else if (strcasecmp(argv[1],
"getfloodingheadersize") == 0) {
411 }
else if (strcasecmp(argv[1],
"printNeighbor") == 0) {
415 }
else if (argc == 3) {
416 if (strcasecmp(argv[1],
"addr") == 0) {
417 ipAddr_ =
static_cast<uint8_t
>(atoi(argv[2]));
419 fprintf(stderr,
"0 is not a valid IP address");
423 }
else if (strcasecmp(argv[1],
"trace") == 0) {
424 string tmp_ = ((
char *) argv[2]);
428 fprintf(stderr,
"Empty string for the trace file name");
436 }
else if (strcasecmp(argv[1],
"setReputation") == 0) {
437 reputation =
dynamic_cast<UwReputationInterface *
>(TclObject::lookup(argv[2]));
443 }
else if (strcasecmp(argv[1],
"setPhyTag") == 0) {
444 std::string tag = argv[2];
445 ClMsgDiscovery disc_m;
446 disc_m.addSenderData((
const PlugIn*)
this, getLayer(), getId(),
447 getStackId(), name() , getTag());
448 sendSyncClMsg(&disc_m);
449 DiscoveryStorage phy_layer_storage = disc_m.findTag(argv[2]);
450 DiscoveryData phy_layer = (*phy_layer_storage.begin()).second;
455 }
else if (argc == 4) {
456 if (strcasecmp(argv[1],
"addTtlPerTraffic") == 0) {
458 static_cast<uint8_t
>(atoi(argv[3]));
462 return Module::command(argc, argv);
468 hdr_cmn *ch = HDR_CMN(p);
473 if (ch->direction() == hdr_cmn::UP) {
480 if (iph->
daddr() == 0) {
481 std::cerr <<
"Destination address not set." << std::endl;
506 ch->direction() = hdr_cmn::DOWN;
511 if (flh->
ttl() <= 0) {
518 map_forwarded_packets::iterator it2 =
521 map_packets::iterator it3 =
522 it2->second.find(ch->uid());
524 if (it3 == it2->second.end()) {
528 it2->second.insert(std::pair<uint16_t, double>(
529 ch->uid(), ch->timestamp()));
535 }
else if (Scheduler::instance().clock() -
543 it3->second = Scheduler::instance().clock();
556 std::map<uint16_t, double> tmp_map;
557 tmp_map.insert(std::pair<uint16_t, double>(
558 ch->uid(), Scheduler::instance().clock()));
560 std::pair<uint8_t, map_packets>(
561 iph->
saddr(), tmp_map));
578 ch->direction() = hdr_cmn::DOWN;
582 if (flh->
ttl() <= 0) {
589 map_forwarded_packets::iterator it2 =
592 map_packets::iterator it3 =
593 it2->second.find(ch->uid());
595 if (it3 == it2->second.end()) {
599 it2->second.insert(std::pair<uint16_t, double>(
600 ch->uid(), ch->timestamp()));
606 }
else if (Scheduler::instance().clock() -
614 it3->second = Scheduler::instance().clock();
627 std::map<uint16_t, double> tmp_map;
628 tmp_map.insert(std::pair<uint16_t, double>(
629 ch->uid(), Scheduler::instance().clock()));
631 std::pair<uint8_t, map_packets>(
632 iph->
saddr(), tmp_map));
647 cerr <<
"State machine ERROR." << endl;
653 }
else if (ch->direction() == hdr_cmn::DOWN) {
656 if (iph->
daddr() == 0) {
657 std::cerr <<
"Destination address equals to 0." << std::endl;
679 cerr <<
"Direction different from UP or DOWN." << endl;
708 hdr_cmn *ch = HDR_CMN(p);
716 osstream_ << Scheduler::instance().clock();
741 out << ((
ip_ & 0xff000000) >> 24);
743 out << ((
ip_ & 0x00ff0000) >> 16);
745 out << ((
ip_ & 0x0000ff00) >> 8);
747 out << ((
ip_ & 0x000000ff));
772 std::cout <<
"Node IP " << (
int)
ipAddr_ << std::endl;
776 std::cout <<
"Neighbor=" << (
int)
it->first <<
"::received="
777 <<
it->second <<
"::"
781 std::cout <<
"Neighbor=" << (
int)
it->first <<
"::received="
782 <<
it->second << std::endl;
785 std::cout << std::endl;
809 neighbor_timer_map::iterator
it =
neighbor_tmr.find(neighbor_addr);
811 return it->second.checkUnconfirmedPkt(uid);
#define HDR_UWFLOODING(p)
This class defines the timer used to check the packet forwarding by the neighbor and handle reputatio...
double inst_noise
Instantaneous noise level.
double avg_snr
Uid of the packet that should be forwarded.
bool addUnconfirmedPkt(int uid, double expire_time)
Add in the map the unconfirmed packets with the corresponidg timeout.
void updateInstantNoise(double inst_noise_val)
Update the value of the instantaneous noise.
std::map< int, double > unconfirmed_pkts
Map with the uid of the unconfirmed packets, with the corresponding forwarding timeout.
bool is_running
True if a timer has been already scheduled and not expired yet.
double alpha
weight for the new SNR value.
virtual void expire(Event *e)
Method called when the timer expire.
virtual ~NeighborReputationHandler()
Class destructor.
void removeOldPackets()
Removes packet with an old expire time.
NeighborReputationHandler(uint8_t neigh_addr, UwFloodingSec *m, double alpha_val, int dbg)
Class constructor.
double last_noise
Boolean variable to check if the packet is the first received one or not.
UwFloodingSec *uint8_t neighbor_addr
< Pointer to the uwflooding module.
void updateChannelMetrics(double val_snr, double last_noise_val)
Update average SNR.
bool getNextPacket(int &uid, double &exp_time) const
Returns as reference the uid of the next packet that id going to expire.
bool checkUnconfirmedPkt(int uid)
Check if the packet with the given uid is an unconfirmed one and set it as confirmed.
Adds the module for SunIPRoutingSink in ns2.
TclObject * create(int, const char *const *)
Adds the header for hdr_uwflooding packets in ns2.
UwFloodingSec class is used to represent the routing layer of a node.
double alpha_snr
Value to be used by the NeighborReputationHandler object to combine new snr values and average snr.
void retrieveInstantNoise(int neighbor_addr)
Send ClMsgStats to retreive instantaneous noise.
virtual int recvSyncClMsg(ClMessage *)
Cross-Layer messages synchronous interpreter.
std::map< uint16_t, uint8_t > ttl_traffic_map
Map with ttl per traffic.
double fwd_to
Time out within which the forwarding is expected.
bool valid_phy_id
True if the id of the phy layer from which obtain the statistics is a valid one.
virtual int recvAsyncClMsg(ClMessage *)
Cross-Layer messages asynchronous interpreter.
uint8_t prev_hop_temp
Previous hop IP address of the last received packet.
virtual int command(int, const char *const *)
TCL command interpreter.
neighbor_map neighbor
Map with the neighbor.
int stats_phy_id
id of the physical layer from which collect the statistics.
int optimize_
Flag used to enable the mechanism to drop packets processed twice.
ostringstream osstream_
Used to convert to string.
virtual ~UwFloodingSec()
Destructor of UwFloodingSec class.
void addToNeighbor(uint8_t neighbor_addr)
Adds a node in the neighbor map, updating the number of packets received from that node.
neighbor_timer_map neighbor_tmr
Map with the neighbor timer.
virtual void sendStatsClMsg(int neighbor_addr)
Send ClMsgStats message when triggered by the reception of an overherd packets.
int ttl_
Time to leave of the UWFLOODING packets.
virtual void recv(Packet *)
Performs the reception of packets from upper and lower layers.
bool checkUnconfirmedPkt(uint8_t neighbor_addr, int uid)
Check if the received packets is an unconfirmed one.
ofstream trace_file_path_
Ofstream used to write the path trace file in the disk.
UwReputationInterface * reputation
Reputation of the neighbor.
bool use_reputation
True if the reputation system is used.
void printNeighbor()
Prints the neighbor list togheter with the number of packets received from the neighbor.
bool trace_path_
Flag used to enable or disable the path trace file for nodes,.
char * trace_file_path_name_
Name of the trace file that contains the list of paths of the data packets received.
map_forwarded_packets my_forwarded_packets_
Map of the packet forwarded.
long packets_forwarded_
Number of packets forwarded by this module.
virtual void writePathInTrace(const Packet *, const string &)
Writes in the Path Trace file the path contained in the Packet.
friend class NeighborReputationHandler
UwFloodingSec()
Constructor of UwFloodingSec class.
double maximum_cache_time_
Validity time of a packet entry.
uint8_t getTTL(Packet *p) const
Get the value of the TTL.
void sendDown(Packet *p, double delay=0)
Send down packets and start forwarding timer.
static string printIP(const nsaddr_t &)
Return a string with an IP in the classic form "x.x.x.x" converting an ns2 nsaddr_t address.
double last_rx_power
True if last packet wasn't correctly received.
double last_noise_power
Power of the last received packet.
double last_interf_power
Noise power measured on query.
double instant_noise_power
Noise power of the last received packet.
hdr_uwcbr describes UWCBR packets.
uint16_t & traffic_type()
Reference to the rftt_ variable.
hdr_uwflooding describes packets used by UWFLOODING.
static int offset_
Required by the PacketHeaderManager.
uint8_t & ttl()
Reference to the uid_ variable.
hdr_uwip describes UWIP packets.
uint8_t & daddr()
Reference to the daddr_ variable.
uint8_t & saddr()
Reference to the saddr_ variable.
UwFloodingPktClass class_uwflooding_pkt
UwFloodingModuleClass class_module_uwflooding
Flooding based routing protocol with security enhanced.
#define TTL_EQUALS_TO_ZERO
Reason for a drop in a UWFLOODING module.
static const uint8_t UWIP_BROADCAST
Variable used to represent a broadcast UWIP.
Definition of UwPhysical class.
Utilities to manage stats.