DESERT 3.6.0
Loading...
Searching...
No Matches
uwflooding-sec.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwflooding-sec.h"
40#include "uwsecurity_clmsg.h"
41#include <clmsg-discovery.h>
42#include "uwstats-utilities.h"
43#include "uwphysical.h"
44
45extern packet_t PT_UWFLOODING;
46
53static class UwFloodingModuleClass : public TclClass
54{
55public:
57 : TclClass("Module/UW/FLOODINGSEC")
58 {
59 }
60
61 TclObject *
62 create(int, const char *const *)
63 {
64 return (new UwFloodingSec());
65 }
67
71static class UwFloodingPktClass : public PacketHeaderClass
72{
73public:
75 : PacketHeaderClass("PacketHeader/FLOODING", sizeof(hdr_uwflooding))
76 {
77 this->bind();
78 bind_offset(&hdr_uwflooding::offset_);
79 }
81
82
84 UwFloodingSec* m, double alpha_val, int dbg)
85 : TimerHandler()
86 , module(m)
87 , neighbor_addr(neigh_addr)
88 , unconfirmed_pkts()
89 , is_running(false)
90 , waiting_uid(-1)
91 , avg_snr(0)
92 , alpha(alpha_val)
93 , is_first_pkt(true)
94 , debug(dbg)
95{
96
97}
98
102
103bool
105{
106 std::map<int, double>::iterator it = unconfirmed_pkts.find(uid);
107 if (it == unconfirmed_pkts.end()) {
108 if (debug)
109 std::cout << NOW << "::addUnconfirmedPkt::Node="
110 << (int)module->ipAddr_ << "::Neighbor="
111 << (int)neighbor_addr << "::uid=" << uid
112 << "::inserted unconfirmed packet" << std::endl;
113
114 unconfirmed_pkts[uid] = expire_time;
115 if (!is_running) {
116 if (NOW < expire_time) {
117 if (debug)
118 std::cout << NOW << "::addUnconfirmedPkt::Node="
119 << (int) module->ipAddr_ << "::Neighbor="
120 << (int) neighbor_addr << "::expire time="
121 << expire_time << std::endl;
122 resched(expire_time-NOW);
123 waiting_uid = uid;
124 is_running = true;
125 } else {
126 std::cerr<<NOW << "NeighborReputationHandler::addUnconfirmedPkt"
127 << "::error:expire time already past"<< std::endl;
128 }
129 }
130 return true;
131 }
132 return false;
133}
134
135bool
137{
138 std::map<int,double>::iterator it_uid = unconfirmed_pkts.find(uid);
139 if (it_uid != unconfirmed_pkts.end()) {
140 if (debug)
141 std::cout << NOW << "::checkUnconfirmedPkt::Node="
142 <<(int)module->ipAddr_ << "::Neighbor="
143 << (int)neighbor_addr << "::uid=" << uid
144 << "::removed from the map" << std::endl;
145
146 unconfirmed_pkts.erase(it_uid);
147 if (module->reputation) {
148 ChannelBasedMetricsReputation metrics =
149 ChannelBasedMetricsReputation(true, avg_snr, last_noise,
150 inst_noise);
151 module->reputation->updateReputation(neighbor_addr, &metrics);
152 }
153 if (waiting_uid == uid) {
154 int next_uid;
155 double next_time;
156 if (getNextPacket(next_uid, next_time)) {
157 waiting_uid = next_uid;
158 is_running = true;
159 resched(next_time - NOW);
160 if (debug)
161 std::cout << NOW << "::checkUnconfirmedPkt::Node="
162 << (int) module->ipAddr_ << "::Neighbor="
163 << (int) neighbor_addr << "::next uid=" << next_uid
164 << "::expire time=" << next_time << std::endl;
165 } else {
166 waiting_uid = -1;
167 is_running = false;
168 force_cancel();
169 }
170 }
171 return true;
172 }
173 return false;
174}
175
176void
178{
179 if (debug)
180 std::cout << NOW << "::NeighborReputationHandler::expire::Node="
181 << (int)module->ipAddr_ << "::Neighbor=" << (int)neighbor_addr
182 << "::expected_uid=" << waiting_uid << "::timer expired"
183 << std::endl;
184
186 module->retrieveInstantNoise(neighbor_addr);
187 if (module->reputation) {
188 ChannelBasedMetricsReputation metrics =
189 ChannelBasedMetricsReputation(false, avg_snr, last_noise,
190 inst_noise);
191 module->reputation->updateReputation(neighbor_addr, &metrics);
192 }
194 int next_uid;
195 double next_time;
196 if (getNextPacket(next_uid, next_time)) {
197 waiting_uid = next_uid;
198 is_running = true;
199 resched(next_time-NOW);
200 if (debug)
201 std::cout << NOW << "::NeighborReputationHandler::expire::Node="
202 << (int)module->ipAddr_ << "::Neighbor="
203 << (int)neighbor_addr << "::next uid=" << next_uid
204 << "::expire time=" << next_time << std::endl;
205 } else {
206 waiting_uid = -1;
207 is_running = false;
208 if (debug)
209 std::cout << NOW << "::NeighborReputationHandler::expire::Node="
210 << (int)module->ipAddr_ << "::Neighbor="
211 << (int)neighbor_addr << "::no packet to schedule"
212 << std::endl;
213 }
214}
215
216bool
217NeighborReputationHandler::getNextPacket(int& uid, double& exp_time) const
218{
219 std::map<int, double>::const_iterator it = unconfirmed_pkts.begin();
220 if (it != unconfirmed_pkts.end()) {
221 for (std::map<int, double>::const_iterator it_f = unconfirmed_pkts.begin();
222 it_f != unconfirmed_pkts.end(); it_f++) {
223 if (it_f->second < it->second) {
224 it = it_f;
225 }
226 }
227 if (NOW <= it->second) {
228 uid = it->first;
229 exp_time = it->second;
230 return true;
231 }
232 return false;
233 } else {
234 return false;
235 }
236}
237
238void
240{
241 for (std::map<int, double>::iterator it = unconfirmed_pkts.begin();
242 it != unconfirmed_pkts.end(); it++) {
243 if (it->second < NOW ) {
244 if (debug)
245 std::cout << NOW << "::removeOldPackets::Node="
246 << (int)module->ipAddr_ << "::Neighbor="
247 << (int)neighbor_addr << "::packet with uid="
248 << it->first << "::not forwarded" << std::endl;
249
250 unconfirmed_pkts.erase(it);
251 if (module->reputation) {
252 ChannelBasedMetricsReputation metrics =
253 ChannelBasedMetricsReputation(false, avg_snr,last_noise,
254 inst_noise);
255 module->reputation->updateReputation(neighbor_addr, &metrics);
256 }
257 }
258 }
259}
260
261void
263 double last_noise_val)
264{
265 if (is_first_pkt) {
266 avg_snr = val_snr;
267 is_first_pkt = false;
268 }
269 else {
270 avg_snr = (1-alpha)*avg_snr + alpha*val_snr;
271 }
272 last_noise = last_noise_val;
273 inst_noise = last_noise_val;
274 if (debug)
275 std::cout << NOW << "::Node=" << (int)module->ipAddr_ << "::Neighbor="
276 << (int)neighbor_addr <<"::average snr=" << avg_snr
277 << "::val snr=" << val_snr << std::endl;
278}
279
280void
282{
283 inst_noise = inst_noise_val;
284 if (debug)
285 std::cout << NOW << "::Node=" << (int)module->ipAddr_ << "::Neighbor="
286 << (int)neighbor_addr << "::last rx noise=" << last_noise
287 <<"::inst noise =" << inst_noise << std::endl;
288}
289
291 : ipAddr_(0)
292 , ttl_(10)
293 , maximum_cache_time_(60)
294 , optimize_(1)
295 , packets_forwarded_(0)
296 , trace_path_(false)
297 , trace_file_path_name_((char *) "trace")
298 , ttl_traffic_map()
299 , use_reputation(false)
300 , neighbor()
301 , neighbor_tmr()
302 , fwd_to(30)
303 , reputation(nullptr)
304 , alpha_snr(0.5)
305 , valid_phy_id(false)
306 , stats_phy_id(0)
307{ // Binding to TCL variables.
308 bind("ttl_", &ttl_);
309 bind("maximum_cache_time_", &maximum_cache_time_);
310 bind("optimize_", &optimize_);
311 bind("forward_timeout_", &fwd_to);
312 bind("alpha_snr_", &alpha_snr);
313} /* UwFlooding::UwFlooding */
314
316{
317} /* UwFloodingSec::~UwFloodingSec */
318
319int
321{
322 if (m->type() == CLMSG_OVERHEARING_PKT) {
323 ClMsgOverhearingPkt *msg = dynamic_cast<ClMsgOverhearingPkt *>(m);
324 Packet* pkt;
325 if (msg->getPacket(pkt)) {
326 hdr_cmn* ch = HDR_CMN(pkt);
327 if (use_reputation && !ch->error()) {
328 sendStatsClMsg(ch->prev_hop_);
329 checkUnconfirmedPkt(ch->prev_hop_, ch->uid());
330 }
331 }
332 return 0;
333 }
334 return Module::recvSyncClMsg(m);
335} /* UwFlooding::recvSyncClMsg */
336
337void
339{
340 if (valid_phy_id) {
341 ClMsgStats stats_clmsg = ClMsgStats(stats_phy_id, UNICAST);
342 sendSyncClMsg(&stats_clmsg);
343 if (stats_clmsg.getStats()->type_id == (int)StatsEnum::STATS_PHY_LAYER){
344 const UwPhysicalStats* stats = dynamic_cast<const UwPhysicalStats*>(
345 stats_clmsg.getStats());
346 if (stats != 0) {
347 if (!stats->has_error) {
348 double snr_db = 10 * log10(stats->last_rx_power /
349 (stats->last_noise_power + stats->last_interf_power));
350 if (debug_)
351 std::cout << NOW << "::Node=" << (int) ipAddr_
352 << "::Neighbor=" << neighbor_addr
353 << "::Received packet with"
354 << "::rx power=" << stats->last_rx_power
355 << "::noise=" << stats->last_noise_power
356 << "::interference="
357 << stats->last_interf_power
358 << "::SNR=" << snr_db << std::endl;
359 if (use_reputation) {
360 addToNeighbor(neighbor_addr);
361 neighbor_timer_map::iterator it =
362 neighbor_tmr.find(neighbor_addr);
363 it->second.updateChannelMetrics(snr_db,
364 stats->last_noise_power);
365 }
366 }
367 }
368 }
369 }
370}
371
372void
374{
375 if (valid_phy_id) {
376 ClMsgStats stats_clmsg = ClMsgStats(stats_phy_id, UNICAST);
377 sendSyncClMsg(&stats_clmsg);
378 if (stats_clmsg.getStats()->type_id == (int)StatsEnum::STATS_PHY_LAYER){
379 const UwPhysicalStats* stats = dynamic_cast<const UwPhysicalStats*>(
380 stats_clmsg.getStats());
381 if (stats != 0) {
382 if (use_reputation) {
383 neighbor_timer_map::iterator it =
384 neighbor_tmr.find(neighbor_addr);
385 if (it != neighbor_tmr.end())
386 it->second.updateInstantNoise(stats->instant_noise_power);
387 }
388 }
389 }
390 }
391}
392
393int
395{
396 return Module::recvAsyncClMsg(m);
397} /* UwFlooding::recvAsyncClMsg */
398
399int
400UwFloodingSec::command(int argc, const char *const *argv)
401{
402 Tcl &tcl = Tcl::instance();
403
404 if (argc == 2) {
405 if (strcasecmp(argv[1], "getpacketsforwarded") == 0) {
406 tcl.resultf("%lu", packets_forwarded_);
407 return TCL_OK;
408 } else if (strcasecmp(argv[1], "getfloodingheadersize") == 0) {
409 tcl.resultf("%d", sizeof(hdr_uwflooding));
410 return TCL_OK;
411 } else if (strcasecmp(argv[1], "printNeighbor") == 0) {
413 return TCL_OK;
414 }
415 } else if (argc == 3) {
416 if (strcasecmp(argv[1], "addr") == 0) {
417 ipAddr_ = static_cast<uint8_t>(atoi(argv[2]));
418 if (ipAddr_ == 0) {
419 fprintf(stderr, "0 is not a valid IP address");
420 return TCL_ERROR;
421 }
422 return TCL_OK;
423 } else if (strcasecmp(argv[1], "trace") == 0) {
424 string tmp_ = ((char *) argv[2]);
425 trace_file_path_name_ = new char[tmp_.length() + 1];
426 strcpy(trace_file_path_name_, tmp_.c_str());
427 if (trace_file_path_name_ == NULL) {
428 fprintf(stderr, "Empty string for the trace file name");
429 return TCL_ERROR;
430 }
431 trace_path_ = true;
432 remove(trace_file_path_name_);
434 trace_file_path_.close();
435 return TCL_OK;
436 } else if (strcasecmp(argv[1], "setReputation") == 0) {
437 reputation = dynamic_cast<UwReputationInterface *>(TclObject::lookup(argv[2]));
438 if (!reputation) {
439 return TCL_ERROR;
440 }
441 use_reputation = true;
442 return TCL_OK;
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;
451 stats_phy_id = phy_layer.getId();
452 valid_phy_id = true;
453 return TCL_OK;
454 }
455 } else if (argc == 4) {
456 if (strcasecmp(argv[1], "addTtlPerTraffic") == 0) {
457 ttl_traffic_map[static_cast<uint16_t>(atoi(argv[2]))] =
458 static_cast<uint8_t>(atoi(argv[3]));
459 return TCL_OK;
460 }
461 }
462 return Module::command(argc, argv);
463} /* UwFlooding::command */
464
465void
467{
468 hdr_cmn *ch = HDR_CMN(p);
469 hdr_uwip *iph = HDR_UWIP(p);
471
472 if (!ch->error()) {
473 if (ch->direction() == hdr_cmn::UP) {
474 if (use_reputation) {
475 prev_hop_temp = ch->prev_hop_;
476 addToNeighbor(ch->prev_hop_);
477 }
478 if (trace_path_)
479 this->writePathInTrace(p, "RECV_DTA");
480 if (iph->daddr() == 0) {
481 std::cerr << "Destination address not set." << std::endl;
482 if (trace_path_)
483 this->writePathInTrace(p, "FREE_DTA");
484 Packet::free(p);
485 return;
486 } else if (iph->daddr() == ipAddr_) {
487 flh->ttl()--;
488 if (trace_path_)
489 this->writePathInTrace(p, "SDUP_DTA");
490 sendUp(p);
491 return;
492 } else if (iph->saddr() == ipAddr_) {
493 // cerr << "I am the source: free." << endl;
494 if (trace_path_)
495 this->writePathInTrace(p, "FREE_DTA");
496 Packet::free(p);
497 return;
498 } else if (iph->daddr() == UWIP_BROADCAST) {
499 // sendUp always: the destination is in broadcast.
500 ch->size() -= sizeof(hdr_uwflooding);
501 if (trace_path_)
502 this->writePathInTrace(p, "SDUP_DTA");
503 sendUp(p->copy());
504
505 // SendDown
506 ch->direction() = hdr_cmn::DOWN;
507 ch->prev_hop_ = ipAddr_;
508 ch->next_hop() = UWIP_BROADCAST;
509 flh->ttl()--;
510 ch->size() += sizeof(hdr_uwflooding);
511 if (flh->ttl() <= 0) {
512 if (trace_path_)
513 this->writePathInTrace(p, "DROP_TTL");
514 drop(p, 1, TTL_EQUALS_TO_ZERO);
515 return;
516 } else {
517 if (optimize_) {
518 map_forwarded_packets::iterator it2 =
519 my_forwarded_packets_.find(iph->saddr());
520 if (it2 != my_forwarded_packets_.end()) {
521 map_packets::iterator it3 =
522 it2->second.find(ch->uid());
523
524 if (it3 == it2->second.end()) { // Known source and
525 // new packet -> add
526 // it to the map and
527 // forward.
528 it2->second.insert(std::pair<uint16_t, double>(
529 ch->uid(), ch->timestamp()));
531 if (trace_path_)
532 this->writePathInTrace(p, "FRWD_DTA");
533 sendDown(p);
534 return;
535 } else if (Scheduler::instance().clock() -
536 it3->second >
537 maximum_cache_time_) { // Packet already
538 // processed by not
539 // valid maximum
540 // cache timer ->
541 // update the cache
542 // time and forward.
543 it3->second = Scheduler::instance().clock();
545 if (trace_path_)
546 this->writePathInTrace(p, "FRWD_DTA");
547 sendDown(p);
548 return;
549 } else {
550 if (trace_path_)
551 this->writePathInTrace(p, "FREE_DTA");
552 Packet::free(p);
553 return;
554 }
555 } else {
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));
563 if (trace_path_)
564 this->writePathInTrace(p, "FRWD_DTA");
565 sendDown(p);
566 return;
567 }
568 } else {
570 if (trace_path_)
571 this->writePathInTrace(p, "FRWD_DTA");
572 sendDown(p);
573 return;
574 }
575 }
576 } else if (iph->daddr() != ipAddr_) {
577 // SendDown
578 ch->direction() = hdr_cmn::DOWN;
579 ch->prev_hop_ = ipAddr_;
580 ch->next_hop() = UWIP_BROADCAST;
581 flh->ttl()--;
582 if (flh->ttl() <= 0) {
583 if (trace_path_)
584 this->writePathInTrace(p, "DROP_TTL");
585 drop(p, 1, TTL_EQUALS_TO_ZERO);
586 return;
587 } else {
588 if (optimize_) {
589 map_forwarded_packets::iterator it2 =
590 my_forwarded_packets_.find(iph->saddr());
591 if (it2 != my_forwarded_packets_.end()) {
592 map_packets::iterator it3 =
593 it2->second.find(ch->uid());
594
595 if (it3 == it2->second.end()) { // Known source and
596 // new packet -> add
597 // it to the map and
598 // forward.
599 it2->second.insert(std::pair<uint16_t, double>(
600 ch->uid(), ch->timestamp()));
602 if (trace_path_)
603 this->writePathInTrace(p, "FRWD_DTA");
604 sendDown(p);
605 return;
606 } else if (Scheduler::instance().clock() -
607 it3->second >
608 maximum_cache_time_) { // Packet already
609 // processed by not
610 // valid maximum
611 // cache timer ->
612 // update the cache
613 // time and forward.
614 it3->second = Scheduler::instance().clock();
616 if (trace_path_)
617 this->writePathInTrace(p, "FRWD_DTA");
618 sendDown(p);
619 return;
620 } else {
621 if (trace_path_)
622 this->writePathInTrace(p, "FREE_DTA");
623 Packet::free(p);
624 return;
625 }
626 } else {
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));
634 if (trace_path_)
635 this->writePathInTrace(p, "FRWD_DTA");
636 sendDown(p);
637 return;
638 }
639 } else {
640 if (trace_path_)
641 this->writePathInTrace(p, "FRWD_DTA");
642 sendDown(p);
643 return;
644 }
645 }
646 } else {
647 cerr << "State machine ERROR." << endl;
648 if (trace_path_)
649 this->writePathInTrace(p, "FREE_DTA");
650 Packet::free(p);
651 return;
652 }
653 } else if (ch->direction() == hdr_cmn::DOWN) {
654 if (trace_path_)
655 this->writePathInTrace(p, "RECV_DTA");
656 if (iph->daddr() == 0) {
657 std::cerr << "Destination address equals to 0." << std::endl;
658 if (trace_path_)
659 this->writePathInTrace(p, "FREE_DTA");
660 Packet::free(p);
661 return;
662 }
663 if (iph->daddr() == ipAddr_) {
664 if (trace_path_)
665 this->writePathInTrace(p, "SDUP_DTA");
666 sendUp(p);
667 return;
668 } else { // iph->daddr() != ipAddr_
669 ch->prev_hop_ = ipAddr_;
670 ch->next_hop() = UWIP_BROADCAST;
671 ch->size() += sizeof(hdr_uwflooding);
672 flh->ttl() = getTTL(p);
673 if (trace_path_)
674 this->writePathInTrace(p, "FRWD_DTA");
675 sendDown(p);
676 return;
677 }
678 } else {
679 cerr << "Direction different from UP or DOWN." << endl;
680 if (trace_path_)
681 this->writePathInTrace(p, "FREE_DTA");
682 Packet::free(p);
683 return;
684 }
685 } else {
686 if (trace_path_)
687 this->writePathInTrace(p, "FREE_DTA");
688 Packet::free(p);
689 return;
690 }
691} /* UwFlooding::recv */
692
693uint8_t
694UwFloodingSec::getTTL(Packet* p) const
695{
696 hdr_uwcbr *uwcbrh = HDR_UWCBR(p);
697 auto it = ttl_traffic_map.find(uwcbrh->traffic_type());
698 if(it != ttl_traffic_map.end()) {
699 return it->second;
700 }
701 return ttl_;
702}
703
704void
705UwFloodingSec::writePathInTrace(const Packet *p, const string &_info)
706{
707 hdr_uwip *iph = HDR_UWIP(p);
708 hdr_cmn *ch = HDR_CMN(p);
710
711 trace_file_path_.open(trace_file_path_name_, fstream::app);
712 osstream_.clear();
713 osstream_.str("");
714 osstream_ << _info;
715 osstream_ << '\t';
716 osstream_ << Scheduler::instance().clock();
717 osstream_ << '\t';
718 osstream_ << static_cast<uint32_t>(ch->uid() & 0x0000ffff);
719 osstream_ << '\t';
721 osstream_ << '\t';
722 osstream_ << static_cast<uint32_t>(ch->prev_hop_ & 0x000000ff);
723 osstream_ << '\t';
724 osstream_ << static_cast<uint32_t>(ch->next_hop() & 0x000000ff);
725 osstream_ << '\t';
727 osstream_ << '\t';
729 osstream_ << '\t';
730 osstream_ << ch->direction();
731 osstream_ << '\t';
732 osstream_ << ch->ptype();
733 trace_file_path_ << osstream_.str() << endl;
734 trace_file_path_.close();
735} /* UwFlooding::writePathInTrace */
736
737string
738UwFloodingSec::printIP(const nsaddr_t &ip_)
739{
741 out << ((ip_ & 0xff000000) >> 24);
742 out << ".";
743 out << ((ip_ & 0x00ff0000) >> 16);
744 out << ".";
745 out << ((ip_ & 0x0000ff00) >> 8);
746 out << ".";
747 out << ((ip_ & 0x000000ff));
748 return out.str();
749} /* UwFlooding::printIP */
750
751
752void
753UwFloodingSec::addToNeighbor(uint8_t neighbor_addr)
754{
755 if (reputation) {
756 reputation->addNode(neighbor_addr);
757 }
758 //TODO vedere se togliere neighbor map e usare solo la mappa dei timer
759 if (neighbor.find(neighbor_addr) != neighbor.end()) {
760 neighbor[neighbor_addr]++;
761 } else {
762 neighbor[neighbor_addr] = 1;
764 this, alpha_snr, debug_);
765 neighbor_tmr.insert(std::make_pair(neighbor_addr,tmp));
766 }
767}
768
769void
771{
772 std::cout << "Node IP " << (int)ipAddr_ << std::endl;
773 for (neighbor_map::iterator it = neighbor.begin(); it != neighbor.end(); it++) {
774 RepStatus status;
775 if (reputation) {
776 std::cout << "Neighbor=" << (int) it->first << "::received="
777 << it->second << "::"
778 << reputation->getStatusReport((int) it->first)
779 << std::endl;
780 } else {
781 std::cout << "Neighbor=" << (int) it->first << "::received="
782 << it->second << std::endl;
783 }
784 }
785 std::cout << std::endl;
786}
787
788void
789UwFloodingSec::sendDown(Packet* p, double delay)
790{
791 if (use_reputation) {
792 hdr_cmn* ch = HDR_CMN(p);
793 hdr_uwip* iph = HDR_UWIP(p);
794 int uid = ch->uid();
795 neighbor_timer_map::iterator it = neighbor_tmr.begin();
796 for (; it != neighbor_tmr.end(); it++) {
797 if (it->first != iph->daddr() && it->first != prev_hop_temp) {
798 it->second.addUnconfirmedPkt(uid, NOW+fwd_to);
799 }
800 }
801 prev_hop_temp = 0; //invalid ip
802 }
803 Module::sendDown(p);
804}
805
806bool
807UwFloodingSec::checkUnconfirmedPkt(uint8_t neighbor_addr, int uid)
808{
809 neighbor_timer_map::iterator it = neighbor_tmr.find(neighbor_addr);
810 if (it != neighbor_tmr.end()) {
811 return it->second.checkUnconfirmedPkt(uid);
812 }
813 return false;
814}
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.
Definition uwphysical.h:90
double last_noise_power
Power of the last received packet.
Definition uwphysical.h:91
double last_interf_power
Noise power measured on query.
Definition uwphysical.h:93
double instant_noise_power
Noise power of the last received packet.
Definition uwphysical.h:92
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.
Definition uwip-module.h:67
uint8_t & daddr()
Reference to the daddr_ variable.
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:92
#define HDR_UWCBR(p)
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 uwip-module.h:59
#define HDR_UWIP(P)
Definition uwip-module.h:57
Definition of UwPhysical class.
Utilities to manage stats.