DESERT 3.5.1
Loading...
Searching...
No Matches
sun-ipr-node-pathest-search.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2021 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
42#include "sun-ipr-node.h"
44
45extern packet_t PT_SUN_DATA;
46extern packet_t PT_SUN_PATH_EST;
47
48/* This function is used to send a path search request.
49 * This function sends a packet that will gather information about the path.
50 * This function will be invoked only by a node that has to transmit to the
51 * sink.
52 */
53void
55{
56 if (STACK_TRACE)
57 std::cout << "> searchPath()" << std::endl;
58
59 if (getNumberOfHopToSink() == 1) { // Do nothing, the node is directly connected with the sink
60 if (printDebug_ > 5)
61 std::cout << "[" << NOW << "]::Node[IP:" << this->printIP(ipAddr_)
62 << "||hops:" << this->getNumberOfHopToSink()
63 << "]::PATH_SEARCH::END_NODE"
64 << std::endl;
65 return;
66 } else if (this->getNumberOfHopToSink() == 0) {
67 // Update internal variables.
68 this->clearHops();
69 this->setNumberOfHopToSink(0);
70 // Create and send a new path establishment packet.
71 Packet *p = Packet::alloc();
72 this->initPktPathEstSearch(p);
74 hdr_cmn *ch = HDR_CMN(p);
75 if (printDebug_ > 5) {
76 std::cout << "[" << NOW << "]::Node[IP:" << this->printIP(ipAddr_)
77 << "||hops:" << this->getNumberOfHopToSink()
78 << "]::SENDING_SEARCH_PATH"
79 << "::UID:" << ch->uid()
80 << std::endl;
81 }
82
83 if (trace_)
84 this->tracePacket(p, "SEND_SRC");
85
86 sendDown(p, this->getDelay(period_status_));
87 return;
88 }
89} /* SunIPRoutingNode::searchPath */
90
91/* This function initializes a new path establishment packet
92 * previously allocated.
93 */
94void
96{
97 if (STACK_TRACE)
98 std::cout << "> initPktPathEstSearch()" << std::endl;
99
100 // Common header.
101 hdr_cmn *ch = HDR_CMN(p);
102 ch->uid() = sunuid_++;
103 ch->ptype() = PT_SUN_PATH_EST;
104 // ch->size() = 0; // Look down.
105 ch->direction() = hdr_cmn::DOWN;
106 ch->next_hop() = UWIP_BROADCAST;
107 ch->prev_hop_ = ipAddr_;
108
109 // IP header.
110 hdr_uwip *iph = HDR_UWIP(p);
111 iph->daddr() = UWIP_BROADCAST; // A path search packet is sent in broadcast.
112 // iph->dport() = 0; // Not needed: no applications above.
113 // iph->saddr() = 0; // Set by IPModule.
114 // iph->sport() = 0; // Not needed: no applications above.
115 // iph->ttl() = 0; // Set by IPModule.
116
117 // Path establishment header.
119 hpest->ptype() = PATH_SEARCH;
120 hpest->list_of_hops_length() = 0;
121 for (int i = 0; i < MAX_HOP_NUMBER; i++) {
122 hpest->list_of_hops()[i] = 0;
123 }
124 hpest->quality() = 0;
125
126 ch->size() += sizeof(hdr_sun_path_est);
127 ch->timestamp() = Scheduler::instance().clock();
128} /* SunIPRoutingNode::initPktPathEstSearch */
129
130/* This functions is used by a node that receives a path establishment packet.
131 * This function is used to process this kind of packets.
132 */
133void
135{
136 if (STACK_TRACE)
137 std::cout << "> replyPathEstSearch()" << std::endl;
138 hdr_cmn *ch = HDR_CMN(p);
139 hdr_uwip *iph = HDR_UWIP(p);
141
142 if (hpest->ptype_ == PATH_SEARCH) {
143 if ((this->isMyIpInList(p)) ||
144 (iph->saddr() == ipAddr_)) { // My Ip is already in list or the
145 // packet is returned to the
146 // source: drop.
147 if (printDebug_ > 5) {
148 std::cout << "[" << NOW << "]::Node[IP:" << this->printIP(ipAddr_)
149 << "||hops:" << this->getNumberOfHopToSink()
150 << "]::PATH_SEARCH::DROP_ALREADY_PROCESSED_FROM:" << printIP(iph->saddr())
151 << "::UID:" << ch->uid()
152 << std::endl;
153 }
155 return;
156 } else { // This is the first time that the current node sees this
157 // request.
158 if (this->addMyIpInList(p) ==
159 false) { // The queue is full, impossible to add my IP.
161 return;
162 } else { // The IP of the current node has been added to the queue.
163 this->updateQuality(p);
164 if (this->getNumberOfHopToSink() ==
165 1) { // Node directly connected with the sink: create an
166 // answer to the path establishment request.
167 this->answerPath(p);
168 Packet::free(p);
169 return;
170 } else { // I'm a relay node, forward the packet in BCAST.
171 if (printDebug_ > 5) {
172 std::cout << "[" << NOW << "]::Node[IP:" << this->printIP(ipAddr_)
173 << "||hops:" << this->getNumberOfHopToSink()
174 << "]::PATH_SEARCH::FORWARD"
175 << "::PREV_HOP:" << printIP(ch->prev_hop_)
176 << "::SOURCE:" << printIP(iph->saddr())
177 << "::UID:" << ch->uid()
178 << std::endl;
179 }
180 ch->next_hop() = UWIP_BROADCAST;
181 ch->prev_hop_ = ipAddr_;
182 iph->daddr() = UWIP_BROADCAST;
184 if (trace_)
185 this->tracePacket(p, "FRWD_SRC");
186 sendDown(p, this->getDelay(period_status_));
187 return;
188 }
189 }
190 }
191 } else {
192 Packet::free(p);
193 return;
194 }
195} /* SunIPRoutingNode::replyPathEstSearch */
196
197/* This function tries to add the IP of the current node at the end of the list
198 * of IP in PathEstablishment header. It can do it if there is at least one free
199 * block.
200 * If the blocks are all full the function drops the packet.
201 * Packet p is a path establishment packet.
202 * The function returns 1 if it added the IP, 0 otherwise.
203 */
204const bool
206{
207 if (STACK_TRACE)
208 std::cout << "> addMyIpInList()" << std::endl;
210
211 if (hpest->pointer() >= MAX_HOP_NUMBER) { // Impossible to add new hops.
212 return false;
213 } else {
214 // Time to add current node IP at the end of the list.
215 hpest->list_of_hops()[hpest->pointer()] = ipAddr_;
216 hpest->list_of_hops_length()++;
217 hpest->pointer()++;
218 return true;
219 }
220} /* SunIPRoutingNode::addMyIpInList */
221
222/* This function checks if the IP of the current node is already in the list
223 * inside a Path Establishment packet header.
224 * If yes it returns true, otherwise it returns false.
225 * It is used to check if a path is already processed.
226 */
227const bool
228SunIPRoutingNode::isMyIpInList(const Packet *p) const
229{
230 if (STACK_TRACE)
231 std::cout << "> isMyIpInList()" << std::endl;
233
234 for (int i = 0; i <= hpest->list_of_hops_length(); i++) {
235 if (hpest->list_of_hops()[i] == ipAddr_)
236 return true;
237 }
238 return false;
239} /* SunIPRoutingNode::isMyIpInList */
240
241/* This function updates the quality field in the packet p.
242 * The behaviour depends on the metric.
243 */
244void
246{
247 if (STACK_TRACE)
248 std::cout << "> updateQuality()" << std::endl;
250 hdr_MPhy *ph = HDR_MPHY(p);
251 if (metrics_ == HOPCOUNT) {
252 hpest->quality() += 1;
253 } else if (metrics_ == SNR) {
254 double snr_ = 10 * log10(ph->Pr / ph->Pn);
255 if (snr_ < hpest->quality()) {
256 hpest->quality() = snr_;
257 if (this->getNumberOfHopToSink() == 1) { // Check also for the SNR
258 // from the sink to the
259 // current node.
260 if (!this->isZero(snr_to_sink_ - MIN_SNR)) {
261 if (snr_to_sink_ < hpest->quality()) {
262 hpest->quality() = snr_to_sink_;
263 }
264 }
265 }
266 }
267 } else if (metrics_ == LESSCONGESTED) {
268 if (this->getPacketsLastMinute() == 0) {
269 hpest->quality() = hpest->quality() + 0;
270 } else if (this->getPacketsLastMinute() > 0) {
271 hpest->quality() = hpest->quality() + this->getLoad();
272 }
273 } else {
274 std::cerr << "The metric_ field of SUN was not set up." << std::endl;
275 }
276} /* SunIPRoutingNode::updateQuality */
virtual void replyPathEstSearch(Packet *)
Replies to Path Establishment Search packets.
static string printIP(const nsaddr_t &)
Returns a string with an IP in the classic form "x.x.x.x" converting an ns2 nsaddr_t address.
ostringstream osstream_
Used to convert to string.
virtual void clearHops()
Clears all the route information of the current node.
virtual const int getPacketsLastMinute() const
Returns the number of packets processed by the current node in the last interval of time (MINUTE).
bool trace_
Flag used to enable or disable the trace file for nodes,.
virtual const int & getNumberOfHopToSink() const
Returns the number of hops that separate the node to the sink.
virtual const bool isMyIpInList(const Packet *) const
Checks if the IP of the current node is in the header of the packet passed as argument.
virtual void initPktPathEstSearch(Packet *) const
Initializes a Path Establishment Search packet (previously allocated).
static long number_of_pathestablishment_
Comulative number of Path Establishment packets processed by SunIPRoutingNode objects.
double snr_to_sink_
SNR between the sink and the current node.
virtual void updateQuality(Packet *)
Updates the field quality in the packet passed as parameter.
double period_status_
Period of the Poisson traffic for status and ack packets.
virtual const int & setNumberOfHopToSink(const int &)
Sets the number of hops that the current node needs to reach the sink.
const double getDelay(const double &period_) const
Returns a delay value to use in transmission.
virtual void searchPath()
Sends a Path Establishment Packet with the option field sets to Search.
int printDebug_
Flag to enable or disable dirrefent levels of debug.
virtual void tracePacket(const Packet *const, const string &position="UNDEF___")
Traces a packet.
virtual const double getLoad() const
Returns the load index of the current node combining the information from getPacketsLastMinute() and ...
nsaddr_t ipAddr_
IP of the current node.
virtual const bool addMyIpInList(Packet *)
Adds the IP of the current node in the header of a Path Establishment packet passed as argument.
int metrics_
Metric used by the current node.
virtual void answerPath(const Packet *)
Creates and sends an Path Establishment Answer packet.
const bool isZero(const double &value) const
Evaluates is the number passed as input is equal to zero.
hdr_sun_path_est describes path establishment packets used by UWSUN
float & quality()
Reference to the quality_ variable.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:95
#define PATH_SEARCH
#define HDR_SUN_PATH_EST(p)
Common structures and definition used by SUN.
#define MIN_SNR
Reference variable for min values for the SNR.
#define DROP_PATH_ESTABLISHMENT_SEARCH_PACKET_ALREADY_PROCESSED
Reason for a drop in a UWSUN module.
#define STACK_TRACE
Used to keep track of methods call.
#define DROP_PATH_ESTABLISHMENT_SEARCH_PACKET_HOP_LIST_FULL
Reason for a drop in a UWSUN module.
static int sunuid_
Unique identifier for UWSUN packets.
Dinamic source routing protocol, this file contains Nodes specifications.
static const int MAX_HOP_NUMBER
Maximum number of hops contained in a SUN Path Establishment packet.
static const uint8_t UWIP_BROADCAST
Variable used to represent a broadcast UWIP.
Definition uwip-module.h:62
#define HDR_UWIP(P)
Definition uwip-module.h:58