Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | |
2 /* | |
3 * Copyright (c) 2013 Mohammed J.F. Alenazi | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 2 as | |
7 * published by the Free Software Foundation; | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 * | |
18 * Author: Mohammed J.F. Alenazi <malenazi@ittc.ku.edu> | |
19 * | |
20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director | |
21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets | |
22 * Information and Telecommunication Technology Center (ITTC) | |
23 * and Department of Electrical Engineering and Computer Science | |
24 * The University of Kansas Lawrence, KS USA. | |
25 * | |
26 * Work supported by King Saud University and the ITTC at The University of Kans as. | |
27 */ | |
28 | |
29 | |
30 #include "epidemic-routing-protocol.h" | |
31 #include <vector> | |
32 #include "ns3/boolean.h" | |
33 #include "ns3/config.h" | |
34 #include "ns3/log.h" | |
35 #include "ns3/inet-socket-address.h" | |
36 #include "ns3/random-variable-stream.h" | |
37 #include "ns3/udp-socket-factory.h" | |
38 #include "ns3/boolean.h" | |
39 #include "ns3/double.h" | |
40 #include "ns3/uinteger.h" | |
41 #include "ns3/udp-header.h" | |
42 #include <iostream> | |
43 #include <algorithm> | |
44 #include <functional> | |
45 #include "ns3/ipv4-route.h" | |
46 #include "ns3/socket.h" | |
47 #include "ns3/log.h" | |
48 | |
49 | |
50 using namespace std; | |
51 | |
52 namespace ns3 { | |
53 namespace Epidemic { | |
54 NS_OBJECT_ENSURE_REGISTERED (RoutingProtocol); | |
55 NS_LOG_COMPONENT_DEFINE ("EpidemicRoutingProtocol"); | |
56 | |
57 | |
58 TypeId RoutingProtocol::GetTypeId (void) | |
59 { | |
60 static TypeId tid = TypeId ("ns3::epidemic::RoutingProtocol") | |
61 .SetParent<Ipv4RoutingProtocol> () | |
62 .AddConstructor<RoutingProtocol> () | |
63 .AddAttribute ("HopCount","Maximum number of times a packet will be flooded. ", | |
64 UintegerValue (64), | |
65 MakeUintegerAccessor (&RoutingProtocol::m_hopCount), | |
66 MakeUintegerChecker<uint32_t> ()) | |
67 .AddAttribute ("QueueLength","Maximum number of packets that a queue can hol d.", | |
68 UintegerValue (64), | |
69 MakeUintegerAccessor (&RoutingProtocol::m_maxQueueLen), | |
70 MakeUintegerChecker<uint32_t> ()) | |
71 .AddAttribute ("QueueEntryExpireTime","Maximum time a packet can live in " | |
72 "the epidemic queues since it's generated at the source.", | |
73 TimeValue (Seconds (100)), | |
74 MakeTimeAccessor (&RoutingProtocol::m_queueEntryExpireTime), | |
75 MakeTimeChecker ()) | |
76 .AddAttribute ("BeaconInterval","Time in seconds after which a beacon packet is broadcast.", | |
77 TimeValue (Seconds (1)), | |
78 MakeTimeAccessor (&RoutingProtocol::m_beaconInterval), | |
79 MakeTimeChecker ()) | |
80 .AddAttribute ("BeaconRandomness","Upper bond of the uniform distribution ra ndom time " | |
Peter Barnes
2014/09/12 20:33:49
"bound"
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
81 "added to avoid collisions. Measured in milliseconds", | |
82 UintegerValue (100), | |
83 MakeUintegerAccessor (&RoutingProtocol::m_beaconRandomness), | |
84 MakeUintegerChecker<uint32_t> ()); | |
85 | |
86 return tid; | |
87 } | |
88 | |
89 | |
90 | |
91 | |
92 RoutingProtocol::RoutingProtocol () | |
93 : m_hopCount (0), | |
94 m_maxQueueLen (0), | |
95 m_queueEntryExpireTime (Seconds (0)), | |
96 m_beaconInterval (Seconds (0)), | |
97 m_beaconRandomness (0), | |
98 m_dataPacketCounter (0), | |
99 m_queue (m_maxQueueLen) | |
100 { | |
101 } | |
102 | |
103 RoutingProtocol::~RoutingProtocol () | |
104 { | |
105 } | |
106 | |
107 void | |
108 RoutingProtocol::DoDispose () | |
109 { | |
110 m_ipv4 = 0; | |
111 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::iterator iter = m_socketAddr esses.begin (); iter | |
112 != m_socketAddresses.end (); iter++) | |
113 { | |
114 iter->first->Close (); | |
115 } | |
116 m_socketAddresses.clear (); | |
117 Ipv4RoutingProtocol::DoDispose (); | |
118 } | |
119 | |
120 void | |
121 RoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const | |
122 { | |
123 /* | |
124 * There is no routing table | |
125 */ | |
126 *stream->GetStream () << "No Routing table "; | |
127 } | |
128 | |
129 | |
130 | |
131 | |
132 void | |
133 RoutingProtocol::Start () | |
134 { | |
135 NS_LOG_FUNCTION (this ); | |
136 m_queue.SetMaxQueueLen (m_maxQueueLen); | |
137 m_beaconTimer.SetFunction (&RoutingProtocol::SendBeacons,this); | |
138 m_beaconRandomnessDistro = CreateObject<UniformRandomVariable> (); | |
Peter Barnes
2014/09/12 20:33:49
Add
m_beaconRandomnessDistro->SetAttribute ("Max"
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
139 m_beaconTimer.Schedule (m_beaconInterval + MilliSeconds (m_beaconRandomnessDis tro->GetValue () * m_beaconRandomness)); | |
Peter Barnes
2014/09/12 20:33:49
(With added line above):
… m_beaconInterval + Mill
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
140 } | |
141 | |
142 /* | |
143 a function used to send a packet for a given address | |
144 */ | |
145 | |
146 void | |
147 RoutingProtocol::SendPacket (Ptr<Packet> p,InetSocketAddress addr) | |
148 { | |
149 NS_LOG_FUNCTION (this << *p); | |
150 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = m_socketA ddresses.begin (); | |
151 j != m_socketAddresses.end (); ++j) | |
152 { | |
153 Ipv4InterfaceAddress iface = j->second; | |
154 if (iface.GetLocal () == m_mainAddress) | |
155 { | |
156 Ptr<Socket> socket = j->first; | |
157 NS_LOG_LOGIC ("Packet " << p << " is sent to" << addr ); | |
158 socket->SendTo (p,0, addr); | |
159 } | |
160 } | |
161 NS_LOG_FUNCTION (this << *p); | |
162 } | |
163 | |
164 | |
165 | |
166 void | |
167 RoutingProtocol::BroadcastPacket (Ptr<Packet> p) | |
168 { | |
169 NS_LOG_FUNCTION (this); | |
170 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = m_socketA ddresses.begin (); | |
171 j != m_socketAddresses.end (); ++j) | |
172 { | |
173 Ptr<Socket> socket = j->first; | |
174 Ipv4InterfaceAddress iface = j->second; | |
175 // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise | |
176 Ipv4Address destination; | |
177 if (iface.GetMask () == Ipv4Mask::GetOnes ()) | |
178 { | |
179 destination = Ipv4Address ("255.255.255.255"); | |
180 } | |
181 else | |
182 { | |
183 destination = iface.GetBroadcast (); | |
184 } | |
185 NS_LOG_LOGIC ("Packet " << p << " is sent to" << destination ); | |
186 socket->SendTo (p, 0, InetSocketAddress (destination, EPIDEMIC_PORT)); | |
187 } | |
188 } | |
189 | |
190 | |
191 | |
192 void | |
193 RoutingProtocol::SendPacketFromQueue (Ipv4Address dst,QueueEntry queueEntry) | |
194 { | |
195 NS_LOG_FUNCTION (this << "Queue Sending Packet " << dst ); | |
196 Ptr<Packet> p = ConstCast<Packet> (queueEntry.GetPacket ()); | |
197 UnicastForwardCallback ucb = queueEntry.GetUnicastForwardCallback (); | |
198 Ipv4Header header = queueEntry.GetIpv4Header (); | |
199 /* | |
200 * Since Epidemic routing has a control mechanism to drop packets based on ho p count, | |
201 * IP TTL dropping mechanism is avoided by incrementing TTL. | |
202 */ | |
203 header.SetTtl (header.GetTtl () + 1); | |
204 header.SetPayloadSize (p->GetSize ()); | |
205 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); | |
206 rt->SetSource (header.GetSource ()); | |
207 rt->SetDestination (header.GetDestination ()); | |
208 rt->SetGateway (dst); | |
209 if (m_ipv4->GetInterfaceForAddress (m_mainAddress) != -1) | |
210 { | |
211 Ptr<Node> node = m_ipv4->GetObject<Node> (); | |
212 rt->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (m_mainAddress))); | |
213 | |
214 } | |
215 | |
216 Ptr<Packet> copy = p->Copy (); | |
217 ucb (rt, copy, header); | |
218 | |
219 } | |
220 | |
221 void | |
222 RoutingProtocol::SendBeacons () | |
223 { | |
224 NS_LOG_FUNCTION (this << "Broadcasting Beacons"); | |
225 Ptr<Packet> packet = Create<Packet> (); | |
226 EpidemicHeader header; | |
227 // This number does not have any effect but it has to be more than 1 to avoid dropping at the receiver | |
228 header.SetHopCount (m_hopCount); | |
229 packet->AddHeader (header); | |
230 TypeHeader tHeader (EPIDEMIC_TYPE_BEACON); | |
231 packet->AddHeader (tHeader); | |
232 EpidemicTag tempTag (1); | |
Peter Barnes
2014/09/12 20:33:49
The magic value 1 should be an enum constant in cl
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
233 // Packet tag is added and will be removed before local delivery | |
234 packet->AddPacketTag (tempTag); | |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
What do you mean by "before
mjf.alenazi
2014/09/18 00:34:26
Before the data packet is sent to transport layer
| |
235 | |
236 Simulator::Schedule (MilliSeconds (m_beaconRandomnessDistro->GetValue () * m_b eaconRandomness),&RoutingProtocol::BroadcastPacket,this, packet); | |
Peter Barnes
2014/09/12 20:33:49
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
237 m_beaconTimer.Schedule (m_beaconInterval + MilliSeconds (m_beaconRandomnessDis tro->GetValue () * m_beaconRandomness)); | |
Peter Barnes
2014/09/12 20:33:49
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
238 } | |
239 | |
240 | |
241 uint32_t | |
242 RoutingProtocol::FindOutputDeviceForAddress (Ipv4Address dst) | |
243 { | |
244 Ptr<Node> mynode = m_ipv4->GetObject<Node> (); | |
245 for (uint32_t i = 0; i < mynode->GetNDevices (); i++) | |
246 { | |
247 Ipv4InterfaceAddress iface = m_ipv4->GetAddress (m_ipv4->GetInterfaceForDe vice (mynode->GetDevice (i)),0); | |
248 if (dst.CombineMask (iface.GetMask ()) == iface.GetLocal ().CombineMask (i face.GetMask ())) | |
249 { | |
250 return i; | |
251 } | |
252 } | |
253 return -1; | |
254 } | |
255 | |
256 | |
257 uint32_t | |
258 RoutingProtocol::FindLoopbackDevice () | |
259 { | |
260 Ptr<Node> mynode = m_ipv4->GetObject<Node> (); | |
261 for (uint32_t i = 0; i < mynode->GetNDevices (); i++) | |
262 { | |
263 Ipv4InterfaceAddress iface = m_ipv4->GetAddress (m_ipv4->GetInterfaceForDe vice (mynode->GetDevice (i)),0); | |
264 if (iface.GetLocal () == Ipv4Address ("127.0.0.1") ) | |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:25
I could not do it. Can you help me with this one?
| |
265 { | |
266 return i; | |
267 } | |
268 } | |
269 return -1; | |
270 } | |
271 | |
272 | |
273 bool | |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:26
I tried pv4::IsDestinationAddress() but it did not
| |
274 RoutingProtocol::IsMyOwnAddress (Ipv4Address src) | |
275 { | |
276 NS_LOG_FUNCTION (this << src); | |
277 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = | |
278 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) | |
279 { | |
280 Ipv4InterfaceAddress iface = j->second; | |
281 if (src == iface.GetLocal ()) | |
282 { | |
283 return true; | |
284 } | |
285 } | |
286 return false; | |
287 } | |
288 | |
289 | |
290 Ptr<Ipv4Route> | |
291 RoutingProtocol::RouteOutput (Ptr<Packet> p, | |
292 const Ipv4Header &header, | |
293 Ptr<NetDevice> oif, | |
294 Socket::SocketErrno &sockerr) | |
295 { | |
296 | |
297 NS_LOG_FUNCTION (this << "Packet Size" << p->GetSize () << " Packet " << p->G etUid () | |
298 << " reached node " << m_mainAddress << " source " << h eader.GetSource () | |
299 << " going to " << header.GetDestination ()); | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 if (IsMyOwnAddress (header.GetDestination ())) | |
306 { | |
307 NS_LOG_LOGIC ("Local delivery a packet" << p->GetUid () << " has arrived d estination " | |
308 << " At node " << m_mainAddress << " " << header); | |
309 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); | |
310 rt->SetSource (m_mainAddress); | |
311 rt->SetDestination (header.GetDestination ()); | |
312 return rt; | |
313 } | |
314 else | |
315 { | |
316 | |
317 /* | |
318 * Control packets generated at this node, are tagged with EpidemicTag. | |
319 */ | |
320 EpidemicTag tag; | |
321 p->PeekPacketTag (tag); | |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:25
Before the data packet is sent to transport layer
| |
322 Ptr<Ipv4Route> rt = Create<Ipv4Route> (); | |
323 rt->SetSource (m_mainAddress); | |
324 rt->SetDestination (header.GetDestination ()); | |
325 rt->SetGateway (header.GetDestination ()); | |
326 | |
327 if (m_ipv4->GetInterfaceForAddress (m_mainAddress) != -1) | |
328 { | |
329 // NS_LOG_DEBUG ( "Returning a route with a destination:" << header.Ge tDestination () ); | |
330 | |
331 if ( tag.GetEpidemicTag () != -1) | |
Peter Barnes
2014/09/12 20:33:49
(See patch-4 comment)
The magic value 1 should be
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
332 { | |
333 /* | |
334 * if the packet is not tagged, it means a data packet | |
Peter Barnes
2014/09/12 20:33:49
(See patch-4 comment)
mjf.alenazi
2014/09/18 00:34:25
I am just trying to see if the packet is generated
| |
335 * Thus, data packet is supposed to be looped back to store it in the epidemic queue. | |
336 */ | |
337 //NS_LOG_DEBUG ( "New Data packet looped back " << p->GetSize () << " queue size " << m_queue.GetSize ()); | |
338 rt->SetOutputDevice (m_ipv4->GetNetDevice (FindLoopbackDevice ())) ; | |
339 } | |
340 else | |
341 { | |
342 /* | |
343 * if the packet is tagged, it is an Epidemic control packet | |
344 * Thus, find the corresponding output device | |
345 */ | |
346 NS_LOG_DEBUG ( "Epidemic triggered packets :" << header.GetDestina tion () << | |
347 " found " << FindOutputDeviceForAddress (header.Ge tDestination ())); | |
348 rt->SetOutputDevice (m_ipv4->GetNetDevice (FindOutputDeviceForAddr ess (header.GetDestination ()))); | |
349 } | |
350 } | |
351 return rt; | |
352 } | |
353 | |
354 } | |
355 | |
356 | |
357 bool | |
358 RoutingProtocol::RouteInput (Ptr<const Packet> p, | |
359 const Ipv4Header &header, | |
360 Ptr<const NetDevice> idev, | |
361 UnicastForwardCallback ucb, | |
362 MulticastForwardCallback mcb, | |
363 LocalDeliverCallback lcb, | |
364 ErrorCallback ecb) | |
365 { | |
366 NS_LOG_FUNCTION (this << header << *p ); | |
367 NS_ASSERT (m_ipv4 != 0); | |
368 NS_ASSERT (p != 0); | |
369 // Check if input device supports IP | |
370 NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0); | |
371 /* | |
372 If there are no interfaces, ignore the packet and return false | |
373 */ | |
374 if (m_socketAddresses.empty ()) | |
375 { | |
376 NS_LOG_ERROR ("No interfaces"); | |
377 return false; | |
378 } | |
379 | |
380 | |
381 | |
382 | |
383 | |
384 if (header.GetTtl () < 1) | |
385 { | |
386 NS_LOG_DEBUG ("TTL expired, Packet is dropped " << p->GetUid ()); | |
387 return false; | |
388 } | |
389 | |
390 | |
391 if (header.GetProtocol () == 1) | |
392 { | |
393 NS_LOG_DEBUG ("Does not deliver ICMP packets " << p->GetUid ()); | |
394 return false; | |
395 } | |
396 | |
397 /* | |
398 * Check all the interfaces local addresses for local delivery | |
399 */ | |
400 | |
401 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = | |
402 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) | |
403 { | |
404 Ipv4InterfaceAddress iface = j->second; | |
405 int32_t iif = m_ipv4->GetInterfaceForDevice (idev); | |
406 if (m_ipv4->GetInterfaceForAddress (iface.GetLocal ()) == iif) | |
407 { | |
408 if (header.GetDestination () == iface.GetBroadcast () || header.GetDes tination () == m_mainAddress ) | |
409 { | |
410 EpidemicTag tag; | |
411 p->PeekPacketTag (tag); | |
412 Ptr<Packet> local_copy = p->Copy (); | |
413 bool duplicatePacket = false; | |
414 /* | |
415 * If this is a data packet, add it to the epidemic queue in order to avoid | |
416 * receiving duplicates of the same packet. | |
417 */ | |
418 if (tag.GetEpidemicTag () < 1) | |
419 { | |
420 Ptr<Packet> copy = p->Copy (); | |
421 QueueEntry newEntry (copy, header, ucb, ecb); | |
422 EpidemicHeader current_epidemicHeader; | |
423 copy->PeekHeader (current_epidemicHeader); | |
424 newEntry.SetExpireTime (m_queueEntryExpireTime + current_epide micHeader.GetTimeStamp ()); | |
425 newEntry.SetPacketID (current_epidemicHeader.GetPacketID ()); | |
426 EpidemicHeader epidemicHeader; | |
427 local_copy->RemoveHeader (epidemicHeader); | |
428 // Try to see the packet has been delivered i.e. in the epidem ic buffer | |
429 if (m_queue.Find (current_epidemicHeader.GetPacketID ()).GetPa cketID () == 0) | |
430 { | |
431 m_queue.Enqueue (newEntry); | |
432 // Remove the packet header for delivery | |
433 } | |
434 else | |
435 { | |
436 duplicatePacket = true; | |
437 } | |
438 } | |
439 /* | |
440 Deliver the packet locally | |
441 */ | |
442 if (!duplicatePacket) | |
443 { | |
444 local_copy->RemoveAllPacketTags (); | |
445 lcb (local_copy, header, iif); | |
446 } | |
447 return true; | |
448 | |
449 } | |
450 } | |
451 } | |
452 | |
453 /* | |
454 If the packet does not have an epidemic header, create one and attach it to th e packet. | |
455 This condition is called one the packet is originated locally and does not hav e an epidemic header | |
456 */ | |
457 | |
458 Ptr<Packet> copy = p->Copy (); | |
459 NS_LOG_LOGIC ("Creating Epidemic packet " << p->GetUid () << " Src " << head er.GetSource () << " Dest " << | |
460 header.GetDestination () << " Size before" << copy->GetSize ()); | |
461 /* | |
462 * The global packet id format: 16 bit(Source IP):16bit(source packet counter) | |
463 */ | |
464 uint16_t hostID = header.GetSource ().Get () & 0xFFFF; | |
465 m_dataPacketCounter++; | |
466 uint32_t global_packet_ID = hostID; | |
467 global_packet_ID = global_packet_ID << 16 | m_dataPacketCounter; | |
468 | |
469 | |
470 | |
471 NS_LOG_LOGIC ("Adding Epidemic packet header " << p->GetUid () ); | |
472 //ADD EPIDEMIC HEADER | |
Peter Barnes
2014/09/12 20:33:50
(See patch-3 comment)
mjf.alenazi
2014/09/18 00:34:25
Done.
| |
473 EpidemicHeader new_epidemicHeader; | |
474 new_epidemicHeader.SetPacketID (global_packet_ID); | |
475 new_epidemicHeader.SetTimeStamp (Simulator::Now ()); | |
476 new_epidemicHeader.SetHopCount (m_hopCount); | |
477 | |
478 // Adding the data packet to the queue | |
479 QueueEntry newEntry (copy, header, ucb, ecb); | |
480 newEntry.SetPacketID (global_packet_ID); | |
481 | |
482 if (IsMyOwnAddress (header.GetSource ())) | |
483 { | |
484 // If the packet is generated in this node, add the epidemic header | |
485 copy->AddHeader (new_epidemicHeader); | |
486 // If the packet is generated in this node, make the Expire time start fro m now + the user specified period | |
487 newEntry.SetExpireTime (m_queueEntryExpireTime + Simulator::Now ()); | |
488 | |
489 } | |
490 else | |
491 { | |
492 // If the packet is generated in another node, read the epidemic header | |
493 EpidemicHeader current_epidemicHeader; | |
494 copy->RemoveHeader (current_epidemicHeader); | |
495 if (current_epidemicHeader.GetHopCount () <= 1 | |
496 || (current_epidemicHeader.GetTimeStamp () + m_queueEntryExpireTime) < Simulator::Now () ) | |
497 { | |
498 // Exit the function to not add the packet to the queue since the flo od count limit is reached | |
499 NS_LOG_DEBUG ("Exit the function and not add the packet to the queue since the flood count limit is reached"); | |
500 return true; | |
501 } | |
502 // If the packet is generated in another node, use the timestamp from the epidemic header | |
503 newEntry.SetExpireTime (m_queueEntryExpireTime + current_epidemicHeader.Ge tTimeStamp ()); | |
504 // If the packet is generated in another node, use the PacketID from the e pidemic header | |
505 newEntry.SetPacketID (current_epidemicHeader.GetPacketID ()); | |
506 //Decrease the packet flood counter | |
507 current_epidemicHeader.SetHopCount (current_epidemicHeader.GetHopCount () - 1); | |
508 // Add the updated header | |
509 copy->AddHeader (current_epidemicHeader); | |
510 } | |
511 | |
512 m_queue.Enqueue (newEntry); | |
513 return true; | |
514 | |
515 } | |
516 | |
517 void | |
518 RoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4) | |
519 { | |
520 NS_LOG_FUNCTION (this); | |
521 m_ipv4 = ipv4; | |
522 Simulator::ScheduleNow (&RoutingProtocol::Start,this); | |
523 } | |
524 | |
525 void | |
526 RoutingProtocol::NotifyInterfaceUp (uint32_t i) | |
527 { | |
528 NS_LOG_FUNCTION (this << i); | |
529 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | |
530 Ipv4InterfaceAddress iface = l3->GetAddress (i,0); | |
531 if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) | |
532 { | |
533 return; | |
534 } | |
535 if (m_mainAddress == Ipv4Address ()) | |
536 { | |
537 m_mainAddress = iface.GetLocal (); | |
538 } | |
539 | |
540 /* | |
541 Create a socket to be used for epidemic routing port | |
542 */ | |
543 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); | |
544 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (),tid); | |
545 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic,this)); | |
546 socket->BindToNetDevice (l3->GetNetDevice (i)); | |
547 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT)); | |
548 socket->SetAllowBroadcast (true); | |
549 m_socketAddresses.insert (std::make_pair (socket,iface)); | |
550 } | |
551 | |
552 void | |
553 RoutingProtocol::NotifyInterfaceDown (uint32_t i) | |
554 { | |
555 NS_LOG_FUNCTION (this << m_ipv4->GetAddress (i, 0).GetLocal ()); | |
556 // Disable layer 2 link state monitoring (if possible) | |
557 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | |
558 Ptr<NetDevice> dev = l3->GetNetDevice (i); | |
559 // Close socket | |
560 Ptr<Socket> socket = FindSocketWithInterfaceAddress (m_ipv4->GetAddress (i, 0) ); | |
561 NS_ASSERT (socket); | |
562 socket->Close (); | |
563 m_socketAddresses.erase (socket); | |
564 } | |
565 | |
566 void | |
567 RoutingProtocol::NotifyAddAddress (uint32_t i,Ipv4InterfaceAddress address) | |
568 { | |
569 NS_LOG_FUNCTION (this << " interface " << i << " address " << address); | |
570 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | |
571 if (!l3->IsUp (i)) | |
572 { | |
573 return; | |
574 } | |
575 if (l3->GetNAddresses (i) == 1) | |
576 { | |
577 Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); | |
578 Ptr<Socket> socket = FindSocketWithInterfaceAddress (iface); | |
579 if (!socket) | |
580 { | |
581 if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) | |
582 { | |
583 return; | |
584 } | |
585 // Create a socket to listen only on this interface | |
586 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (), | |
587 UdpSocketFactory::GetTypeId ()); | |
588 NS_ASSERT (socket != 0); | |
589 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic, this)); | |
590 socket->BindToNetDevice (l3->GetNetDevice (i)); | |
591 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT )); | |
592 socket->SetAllowBroadcast (true); | |
593 m_socketAddresses.insert (std::make_pair (socket, iface)); | |
594 } | |
595 } | |
596 else | |
597 { | |
598 NS_LOG_LOGIC ("Epidemic does not work with more then one address per each interface. Ignore added address"); | |
Peter Barnes
2014/09/12 20:33:49
(See patch-3 comment)
Where in the docs is this re
mjf.alenazi
2014/09/18 00:34:25
in LIMITATIONS in epidemic.rst
On 2014/09/12 20:
| |
599 } | |
600 | |
601 | |
602 | |
603 } | |
604 | |
605 void | |
606 RoutingProtocol::NotifyRemoveAddress (uint32_t i,Ipv4InterfaceAddress address) | |
607 { | |
608 | |
609 NS_LOG_FUNCTION (this); | |
610 Ptr<Socket> socket = FindSocketWithInterfaceAddress (address); | |
611 if (socket) | |
612 { | |
613 m_socketAddresses.erase (socket); | |
614 Ptr<Ipv4L3Protocol> l3 = m_ipv4->GetObject<Ipv4L3Protocol> (); | |
615 if (l3->GetNAddresses (i)) | |
616 { | |
617 Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); | |
618 // Create a socket to listen only on this interface | |
619 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (), | |
620 UdpSocketFactory::GetTypeId ()); | |
621 NS_ASSERT (socket != 0); | |
622 socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvEpidemic, this)); | |
623 // Bind to any IP address so that broadcasts can be received | |
624 socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), EPIDEMIC_PORT )); | |
625 socket->SetAllowBroadcast (true); | |
626 m_socketAddresses.insert (std::make_pair (socket, iface)); | |
627 | |
628 } | |
629 | |
630 } | |
631 else | |
632 { | |
633 NS_LOG_LOGIC ("Remove address not participating in Epidemic operation"); | |
634 } | |
635 } | |
636 | |
637 | |
638 | |
639 void | |
640 RoutingProtocol::SendDisjointPackets (EpidemicSummaryVectorHeader packet_SMV,Ipv 4Address dest) | |
641 { | |
642 NS_LOG_FUNCTION (this << " send to " << dest); | |
643 /* | |
644 This function is used to find send the packets listed in the vector list | |
645 */ | |
646 EpidemicSummaryVectorHeader list = m_queue.FindDisjointPackets (packet_SMV); | |
647 for (std::vector<uint32_t>::iterator | |
648 i = list.m_packets.begin (); | |
649 i != list.m_packets.end (); | |
650 ++i) | |
651 { | |
652 QueueEntry newEntry = m_queue.Find (*i); | |
653 if (newEntry.GetPacket ()) | |
654 { | |
655 | |
656 Simulator::Schedule (MilliSeconds (m_beaconRandomnessDistro->GetValue () * m_beaconRandomness), | |
Peter Barnes
2014/09/12 20:33:49
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
657 &RoutingProtocol::SendPacketFromQueue,this,dest, newEntry); | |
658 } | |
659 } | |
660 } | |
661 | |
662 | |
663 | |
664 Ptr<Socket> | |
665 RoutingProtocol::FindSocketWithInterfaceAddress (Ipv4InterfaceAddress addr ) con st | |
666 { | |
667 NS_LOG_FUNCTION (this << addr); | |
668 for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j = | |
669 m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) | |
670 { | |
671 Ptr<Socket> socket = j->first; | |
672 Ipv4InterfaceAddress iface = j->second; | |
673 if (iface == addr) | |
674 { | |
675 return socket; | |
676 } | |
677 } | |
678 Ptr<Socket> socket; | |
679 return socket; | |
680 } | |
681 | |
682 | |
683 void | |
684 RoutingProtocol::SendSummaryVector (Ipv4Address dest,bool firstNode) | |
685 { | |
686 NS_LOG_FUNCTION (this << " sent to " << dest); | |
687 // Creating the packet | |
688 Ptr<Packet> packet_summary = Create<Packet> (); | |
689 EpidemicSummaryVectorHeader header_summary = m_queue.GetSummaryVector (); | |
690 TypeHeader tHeader; | |
691 if (firstNode) | |
692 { | |
693 tHeader.SetInstanceType (EPIDEMIC_TYPE_REPLY); | |
694 } | |
695 else | |
696 { | |
697 tHeader.SetInstanceType (EPIDEMIC_TYPE_REPLY_BACK); | |
698 } | |
699 packet_summary->AddHeader (header_summary); | |
700 EpidemicTag tempTag (1); | |
701 packet_summary->AddHeader (tHeader); | |
702 packet_summary->AddPacketTag (tempTag); | |
703 // Send the summary vector | |
704 // NS_LOG_DEBUG ("Sending the summary vector 2 packet " << packet_summary->GetU id () << " to " << dest ); | |
705 InetSocketAddress addr = InetSocketAddress (dest, EPIDEMIC_PORT); | |
706 Simulator::Schedule (MilliSeconds (m_beaconRandomnessDistro->GetValue () * m_b eaconRandomness), | |
Peter Barnes
2014/09/12 20:33:50
(See comment line 139)
mjf.alenazi
2014/09/18 00:34:26
Done.
| |
707 &RoutingProtocol::SendPacket,this, packet_summary, addr); | |
708 | |
709 } | |
710 | |
711 | |
712 | |
713 void | |
714 RoutingProtocol::RecvEpidemic (Ptr<Socket> socket) | |
715 { | |
716 NS_LOG_FUNCTION (this << socket); | |
717 m_queue.DropExpiredPackets (); | |
718 Address address; | |
719 Ptr<Packet> packet = socket->RecvFrom (address); | |
720 TypeHeader tHeader (EPIDEMIC_TYPE_BEACON); | |
721 packet->RemoveHeader (tHeader); | |
722 | |
723 InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (address); | |
724 Ipv4Address sender = inetSourceAddr.GetIpv4 (); | |
725 Ptr<Packet> packet_copy = packet->Copy (); | |
726 if (tHeader.GetMessageType () == EPIDEMIC_TYPE_BEACON) | |
727 { | |
728 NS_LOG_LOGIC ("Got a beacon from " << sender << " " << packet->GetUid () < < " " << m_mainAddress); | |
729 // Anti-entropy session | |
730 // Check if you have the smaller address | |
731 if (m_mainAddress.Get () < sender.Get ()) | |
732 { | |
733 SendSummaryVector (sender,true); | |
734 } | |
735 } | |
736 else if (tHeader.GetMessageType () == EPIDEMIC_TYPE_REPLY) | |
737 { | |
738 NS_LOG_LOGIC ("Got a A reply from " << sender << " " << packet->GetUid () << " " << m_mainAddress); | |
739 EpidemicSummaryVectorHeader packet_SMV; | |
740 packet->RemoveHeader (packet_SMV); | |
741 SendDisjointPackets (packet_SMV, sender); | |
742 SendSummaryVector (sender,false); | |
743 } | |
744 else if (tHeader.GetMessageType () == EPIDEMIC_TYPE_REPLY_BACK) | |
745 { | |
746 NS_LOG_LOGIC ("Got a A reply back from " << sender << " " << packet->GetUi d () << " " << m_mainAddress); | |
747 EpidemicSummaryVectorHeader packet_SMV; | |
748 packet->RemoveHeader (packet_SMV); | |
749 SendDisjointPackets (packet_SMV, sender); | |
750 | |
751 } | |
752 } | |
753 } //end namespace epidemic | |
754 } //end namespace ns3 | |
OLD | NEW |