00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #define NS_LOG_APPEND_CONTEXT \
00033 if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
00034
00035
00036 #include "olsr-agent-impl.h"
00037 #include "ns3/socket-factory.h"
00038 #include "ns3/udp-socket-factory.h"
00039 #include "ns3/simulator.h"
00040 #include "ns3/log.h"
00041 #include "ns3/random-variable.h"
00042 #include "ns3/inet-socket-address.h"
00043 #include "ns3/boolean.h"
00044 #include "ns3/uinteger.h"
00045 #include "ns3/enum.h"
00046 #include "ns3/trace-source-accessor.h"
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #define DELAY(time) (((time) < (Simulator::Now ())) ? Seconds (0.000001) : \
00057 (time - Simulator::Now () + Seconds (0.000001)))
00058
00059
00060
00061
00062
00063
00064
00065
00066 #define OLSR_REFRESH_INTERVAL Seconds (2)
00067
00068
00069
00070
00071
00072 #define OLSR_NEIGHB_HOLD_TIME (Scalar (3) * OLSR_REFRESH_INTERVAL)
00073
00074 #define OLSR_TOP_HOLD_TIME (Scalar (3) * m_tcInterval)
00075
00076 #define OLSR_DUP_HOLD_TIME Seconds (30)
00077
00078 #define OLSR_MID_HOLD_TIME (Scalar (3) * m_midInterval)
00079
00080
00081
00082
00083
00084 #define OLSR_UNSPEC_LINK 0
00085
00086 #define OLSR_ASYM_LINK 1
00087
00088 #define OLSR_SYM_LINK 2
00089
00090 #define OLSR_LOST_LINK 3
00091
00092
00093
00094
00095 #define OLSR_NOT_NEIGH 0
00096
00097 #define OLSR_SYM_NEIGH 1
00098
00099 #define OLSR_MPR_NEIGH 2
00100
00101
00102
00103
00104
00105 #define OLSR_WILL_NEVER 0
00106
00107 #define OLSR_WILL_LOW 1
00108
00109 #define OLSR_WILL_DEFAULT 3
00110
00111 #define OLSR_WILL_HIGH 6
00112
00113 #define OLSR_WILL_ALWAYS 7
00114
00115
00116
00117
00118
00119 #define OLSR_MAXJITTER (m_helloInterval.GetSeconds () / 4)
00120
00121 #define OLSR_MAX_SEQ_NUM 65535
00122
00123 #define JITTER (Seconds (UniformVariable::GetSingleValue (0, OLSR_MAXJITTER)))
00124
00125
00126 #define OLSR_PORT_NUMBER 698
00127
00128 #define OLSR_MAX_MSGS 64
00129
00130
00131 #define OLSR_MAX_HELLOS 12
00132
00133
00134 #define OLSR_MAX_ADDRS 64
00135
00136
00137 namespace ns3 {
00138 namespace olsr {
00139
00140 NS_LOG_COMPONENT_DEFINE ("OlsrAgent");
00141
00142
00143
00144
00145 NS_OBJECT_ENSURE_REGISTERED (AgentImpl);
00146
00147 TypeId
00148 AgentImpl::GetTypeId (void)
00149 {
00150 static TypeId tid = TypeId ("ns3::olsr::AgentImpl")
00151 .SetParent<Agent> ()
00152 .AddConstructor<AgentImpl> ()
00153 .AddAttribute ("HelloInterval", "HELLO messages emission interval.",
00154 TimeValue (Seconds (2)),
00155 MakeTimeAccessor (&AgentImpl::m_helloInterval),
00156 MakeTimeChecker ())
00157 .AddAttribute ("TcInterval", "TC messages emission interval.",
00158 TimeValue (Seconds (5)),
00159 MakeTimeAccessor (&AgentImpl::m_tcInterval),
00160 MakeTimeChecker ())
00161 .AddAttribute ("MidInterval", "MID messages emission interval. Normally it is equal to TcInterval.",
00162 TimeValue (Seconds (5)),
00163 MakeTimeAccessor (&AgentImpl::m_midInterval),
00164 MakeTimeChecker ())
00165 .AddAttribute ("Willingness", "Willingness of a node to carry and forward traffic for other nodes.",
00166 EnumValue (OLSR_WILL_DEFAULT),
00167 MakeEnumAccessor (&AgentImpl::m_willingness),
00168 MakeEnumChecker (OLSR_WILL_NEVER, "never",
00169 OLSR_WILL_LOW, "low",
00170 OLSR_WILL_DEFAULT, "default",
00171 OLSR_WILL_HIGH, "high",
00172 OLSR_WILL_ALWAYS, "always"))
00173 .AddTraceSource ("Rx", "Receive OLSR packet.",
00174 MakeTraceSourceAccessor (&AgentImpl::m_rxPacketTrace))
00175 .AddTraceSource ("Tx", "Send OLSR packet.",
00176 MakeTraceSourceAccessor (&AgentImpl::m_txPacketTrace))
00177 .AddTraceSource ("RoutingTableChanged", "The OLSR routing table has changed.",
00178 MakeTraceSourceAccessor (&AgentImpl::m_routingTableChanged))
00179 ;
00180 return tid;
00181 }
00182
00183
00184 AgentImpl::AgentImpl ()
00185 :
00186 m_helloTimer (Timer::CANCEL_ON_DESTROY),
00187 m_tcTimer (Timer::CANCEL_ON_DESTROY),
00188 m_midTimer (Timer::CANCEL_ON_DESTROY)
00189 {}
00190
00191 AgentImpl::~AgentImpl ()
00192 {}
00193
00194 void
00195 AgentImpl::SetNode (Ptr<Node> node)
00196 {
00197 NS_LOG_DEBUG ("Created olsr::AgentImpl");
00198 m_helloTimer.SetFunction (&AgentImpl::HelloTimerExpire, this);
00199 m_tcTimer.SetFunction (&AgentImpl::TcTimerExpire, this);
00200 m_midTimer.SetFunction (&AgentImpl::MidTimerExpire, this);
00201 m_queuedMessagesTimer.SetFunction (&AgentImpl::SendQueuedMessages, this);
00202
00203 m_packetSequenceNumber = OLSR_MAX_SEQ_NUM;
00204 m_messageSequenceNumber = OLSR_MAX_SEQ_NUM;
00205 m_ansn = OLSR_MAX_SEQ_NUM;
00206
00207 m_linkTupleTimerFirstTime = true;
00208
00209 m_ipv4 = node->GetObject<Ipv4> ();
00210 NS_ASSERT (m_ipv4);
00211 }
00212
00213 void AgentImpl::DoDispose ()
00214 {
00215 m_ipv4 = 0;
00216
00217 for (std::map< Ptr<Socket>, Ipv4Address >::iterator iter = m_socketAddresses.begin ();
00218 iter != m_socketAddresses.end (); iter++)
00219 {
00220 iter->first->Dispose ();
00221 }
00222 m_socketAddresses.clear ();
00223
00224 if (m_routingTable)
00225 {
00226 m_routingTable->Dispose ();
00227 m_routingTable = 0;
00228 }
00229
00230 Object::DoDispose ();
00231 }
00232
00233 void AgentImpl::Start ()
00234 {
00235 if (m_mainAddress == Ipv4Address ())
00236 {
00237 Ipv4Address loopback ("127.0.0.1");
00238 for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
00239 {
00240 Ipv4Address addr = m_ipv4->GetAddress (i);
00241 if (addr != loopback)
00242 {
00243 m_mainAddress = addr;
00244 break;
00245 }
00246 }
00247
00248 NS_ASSERT (m_mainAddress != Ipv4Address ());
00249 }
00250
00251 NS_LOG_DEBUG ("Starting OLSR on node " << m_mainAddress);
00252
00253 m_routingTable = CreateObject<RoutingTable> ();
00254 m_routingTable->SetIpv4 (m_ipv4);
00255 m_routingTable->SetMainAddress (m_mainAddress);
00256
00257
00258 m_ipv4->AddRoutingProtocol (m_routingTable, 10);
00259
00260 Ipv4Address loopback ("127.0.0.1");
00261 for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
00262 {
00263 Ipv4Address addr = m_ipv4->GetAddress (i);
00264 if (addr == loopback)
00265 continue;
00266
00267 if (addr != m_mainAddress)
00268 {
00269
00270
00271
00272 IfaceAssocTuple tuple;
00273 tuple.ifaceAddr = addr;
00274 tuple.mainAddr = m_mainAddress;
00275 AddIfaceAssocTuple (tuple);
00276 NS_ASSERT (GetMainAddress (addr) == m_mainAddress);
00277 }
00278
00279
00280 Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (),
00281 UdpSocketFactory::GetTypeId());
00282 socket->SetRecvCallback (MakeCallback (&AgentImpl::RecvOlsr, this));
00283 if (socket->Bind (InetSocketAddress (addr, OLSR_PORT_NUMBER)))
00284 {
00285 NS_FATAL_ERROR ("Failed to bind() OLSR receive socket");
00286 }
00287 socket->Connect (InetSocketAddress (Ipv4Address (0xffffffff), OLSR_PORT_NUMBER));
00288 m_socketAddresses[socket] = addr;
00289 }
00290
00291 HelloTimerExpire ();
00292 TcTimerExpire ();
00293 MidTimerExpire ();
00294
00295 NS_LOG_DEBUG ("OLSR on node " << m_mainAddress << " started");
00296 }
00297
00298 void AgentImpl::SetMainInterface (uint32_t interface)
00299 {
00300 m_mainAddress = m_ipv4->GetAddress (interface);
00301 }
00302
00303
00304
00305
00306 void
00307 AgentImpl::RecvOlsr (Ptr<Socket> socket)
00308 {
00309 Ptr<Packet> receivedPacket;
00310 Address sourceAddress;
00311 receivedPacket = socket->RecvFrom (sourceAddress);
00312
00313 InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (sourceAddress);
00314 Ipv4Address senderIfaceAddr = inetSourceAddr.GetIpv4 ();
00315 Ipv4Address receiverIfaceAddr = m_socketAddresses[socket];
00316 NS_ASSERT (receiverIfaceAddr != Ipv4Address ());
00317 NS_LOG_DEBUG ("OLSR node " << m_mainAddress << " received a OLSR packet from "
00318 << senderIfaceAddr << " to " << receiverIfaceAddr);
00319
00320
00321
00322 NS_ASSERT (inetSourceAddr.GetPort () == OLSR_PORT_NUMBER);
00323
00324 Ptr<Packet> packet = receivedPacket;
00325
00326 olsr::PacketHeader olsrPacketHeader;
00327 packet->RemoveHeader (olsrPacketHeader);
00328 NS_ASSERT (olsrPacketHeader.GetPacketLength () >= olsrPacketHeader.GetSerializedSize ());
00329 uint32_t sizeLeft = olsrPacketHeader.GetPacketLength () - olsrPacketHeader.GetSerializedSize ();
00330
00331 MessageList messages;
00332
00333 while (sizeLeft)
00334 {
00335 MessageHeader messageHeader;
00336 if (packet->RemoveHeader (messageHeader) == 0)
00337 NS_ASSERT (false);
00338
00339 sizeLeft -= messageHeader.GetSerializedSize ();
00340
00341 NS_LOG_DEBUG ("Olsr Msg received with type "
00342 << std::dec << int (messageHeader.GetMessageType ())
00343 << " TTL=" << int (messageHeader.GetTimeToLive ())
00344 << " origAddr=" << messageHeader.GetOriginatorAddress ());
00345 messages.push_back (messageHeader);
00346 }
00347
00348 m_rxPacketTrace (olsrPacketHeader, messages);
00349
00350 for (MessageList::const_iterator messageIter = messages.begin ();
00351 messageIter != messages.end (); messageIter++)
00352 {
00353 const MessageHeader &messageHeader = *messageIter;
00354
00355
00356
00357 if (messageHeader.GetTimeToLive () == 0
00358 || messageHeader.GetOriginatorAddress () == m_mainAddress)
00359 {
00360 packet->RemoveAtStart (messageHeader.GetSerializedSize ()
00361 - messageHeader.GetSerializedSize ());
00362 continue;
00363 }
00364
00365
00366 bool do_forwarding = true;
00367 DuplicateTuple *duplicated = m_state.FindDuplicateTuple
00368 (messageHeader.GetOriginatorAddress (),
00369 messageHeader.GetMessageSequenceNumber ());
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 if (duplicated == NULL)
00384 {
00385 switch (messageHeader.GetMessageType ())
00386 {
00387 case olsr::MessageHeader::HELLO_MESSAGE:
00388 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
00389 << "s OLSR node " << m_mainAddress
00390 << " received HELLO message of size " << messageHeader.GetSerializedSize ());
00391 ProcessHello (messageHeader, receiverIfaceAddr, senderIfaceAddr);
00392 break;
00393
00394 case olsr::MessageHeader::TC_MESSAGE:
00395 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
00396 << "s OLSR node " << m_mainAddress
00397 << " received TC message of size " << messageHeader.GetSerializedSize ());
00398 ProcessTc (messageHeader, senderIfaceAddr);
00399 break;
00400
00401 case olsr::MessageHeader::MID_MESSAGE:
00402 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
00403 << "s OLSR node " << m_mainAddress
00404 << " received MID message of size " << messageHeader.GetSerializedSize ());
00405 ProcessMid (messageHeader, senderIfaceAddr);
00406 break;
00407
00408 default:
00409 NS_LOG_DEBUG ("OLSR message type " <<
00410 int (messageHeader.GetMessageType ()) <<
00411 " not implemented");
00412 }
00413 }
00414 else
00415 {
00416 NS_LOG_DEBUG ("OLSR message is duplicated, not reading it.");
00417
00418
00419
00420 for (std::vector<Ipv4Address>::const_iterator it = duplicated->ifaceList.begin ();
00421 it != duplicated->ifaceList.end(); it++)
00422 {
00423 if (*it == receiverIfaceAddr)
00424 {
00425 do_forwarding = false;
00426 break;
00427 }
00428 }
00429 }
00430
00431 if (do_forwarding)
00432 {
00433
00434
00435
00436 if (messageHeader.GetMessageType () != olsr::MessageHeader::HELLO_MESSAGE)
00437 {
00438 ForwardDefault (messageHeader, duplicated,
00439 receiverIfaceAddr, inetSourceAddr.GetIpv4 ());
00440 }
00441 }
00442
00443 }
00444
00445
00446 RoutingTableComputation ();
00447 }
00448
00449
00450
00451
00452
00453
00454
00455 int
00456 AgentImpl::Degree (NeighborTuple const &tuple)
00457 {
00458 int degree = 0;
00459 for (TwoHopNeighborSet::const_iterator it = m_state.GetTwoHopNeighbors ().begin ();
00460 it != m_state.GetTwoHopNeighbors ().end (); it++)
00461 {
00462 TwoHopNeighborTuple const &nb2hop_tuple = *it;
00463 if (nb2hop_tuple.neighborMainAddr == tuple.neighborMainAddr)
00464 {
00465 const NeighborTuple *nb_tuple =
00466 m_state.FindNeighborTuple (nb2hop_tuple.neighborMainAddr);
00467 if (nb_tuple == NULL)
00468 degree++;
00469 }
00470 }
00471 return degree;
00472 }
00473
00474
00475
00476
00477 void
00478 AgentImpl::MprComputation()
00479 {
00480 NS_LOG_FUNCTION (this);
00481
00482
00483
00484 MprSet mprSet;
00485
00486
00487
00488
00489 NeighborSet N;
00490 for (NeighborSet::const_iterator neighbor = m_state.GetNeighbors ().begin();
00491 neighbor != m_state.GetNeighbors ().end (); neighbor++)
00492 {
00493 if (neighbor->status == NeighborTuple::STATUS_SYM)
00494 {
00495 N.push_back (*neighbor);
00496 }
00497 }
00498
00499
00500
00501
00502
00503
00504
00505 TwoHopNeighborSet N2;
00506 for (TwoHopNeighborSet::const_iterator twoHopNeigh = m_state.GetTwoHopNeighbors ().begin ();
00507 twoHopNeigh != m_state.GetTwoHopNeighbors ().end (); twoHopNeigh++)
00508 {
00509
00510
00511 if (twoHopNeigh->twoHopNeighborAddr == m_mainAddress)
00512 {
00513 continue;
00514 }
00515
00516
00517
00518 bool ok = false;
00519 for (NeighborSet::const_iterator neigh = N.begin ();
00520 neigh != N.end (); neigh++)
00521 {
00522 if (neigh->neighborMainAddr == twoHopNeigh->neighborMainAddr)
00523 {
00524 if (neigh->willingness == OLSR_WILL_NEVER)
00525 {
00526 ok = false;
00527 break;
00528 }
00529 else
00530 {
00531 ok = true;
00532 break;
00533 }
00534 }
00535 }
00536 if (!ok)
00537 {
00538 continue;
00539 }
00540
00541
00542
00543
00544 for (NeighborSet::const_iterator neigh = N.begin ();
00545 neigh != N.end (); neigh++)
00546 {
00547 if (neigh->neighborMainAddr == twoHopNeigh->twoHopNeighborAddr)
00548 {
00549 ok = false;
00550 break;
00551 }
00552 }
00553
00554 if (ok)
00555 {
00556 N2.push_back (*twoHopNeigh);
00557 }
00558 }
00559
00560 NS_LOG_DEBUG ("Size of N2: " << N2.size ());
00561
00562
00563
00564 for (NeighborSet::const_iterator neighbor = N.begin (); neighbor != N.end (); neighbor++)
00565 {
00566 if (neighbor->willingness == OLSR_WILL_ALWAYS)
00567 {
00568 mprSet.insert (neighbor->neighborMainAddr);
00569
00570
00571 for (TwoHopNeighborSet::iterator twoHopNeigh = N2.begin ();
00572 twoHopNeigh != N2.end (); )
00573 {
00574 if (twoHopNeigh->neighborMainAddr == neighbor->neighborMainAddr)
00575 {
00576 twoHopNeigh = N2.erase (twoHopNeigh);
00577 }
00578 else
00579 {
00580 twoHopNeigh++;
00581 }
00582 }
00583 }
00584 }
00585
00586
00587
00588
00589
00590
00591 std::set<Ipv4Address> coveredTwoHopNeighbors;
00592 for (TwoHopNeighborSet::iterator twoHopNeigh = N2.begin (); twoHopNeigh != N2.end (); twoHopNeigh++)
00593 {
00594 NeighborSet::const_iterator onlyNeighbor = N.end ();
00595
00596 for (NeighborSet::const_iterator neighbor = N.begin ();
00597 neighbor != N.end (); neighbor++)
00598 {
00599 if (neighbor->neighborMainAddr == twoHopNeigh->neighborMainAddr)
00600 {
00601 if (onlyNeighbor == N.end ())
00602 {
00603 onlyNeighbor = neighbor;
00604 }
00605 else
00606 {
00607 onlyNeighbor = N.end ();
00608 break;
00609 }
00610 }
00611 }
00612 if (onlyNeighbor != N.end ())
00613 {
00614 mprSet.insert (onlyNeighbor->neighborMainAddr);
00615 coveredTwoHopNeighbors.insert (twoHopNeigh->twoHopNeighborAddr);
00616 }
00617 }
00618
00619 for (TwoHopNeighborSet::iterator twoHopNeigh = N2.begin ();
00620 twoHopNeigh != N2.end (); )
00621 {
00622 if (coveredTwoHopNeighbors.find (twoHopNeigh->twoHopNeighborAddr) != coveredTwoHopNeighbors.end ())
00623 {
00624 twoHopNeigh = N2.erase (twoHopNeigh);
00625 }
00626 else
00627 {
00628 twoHopNeigh++;
00629 }
00630 }
00631
00632
00633
00634 while (N2.begin () != N2.end ())
00635 {
00636
00637
00638
00639
00640 std::map<int, std::vector<const NeighborTuple *> > reachability;
00641 std::set<int> rs;
00642 for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
00643 {
00644 NeighborTuple const &nb_tuple = *it;
00645 int r = 0;
00646 for (TwoHopNeighborSet::iterator it2 = N2.begin (); it2 != N2.end (); it2++)
00647 {
00648 TwoHopNeighborTuple const &nb2hop_tuple = *it2;
00649 if (nb_tuple.neighborMainAddr == nb2hop_tuple.neighborMainAddr)
00650 r++;
00651 }
00652 rs.insert (r);
00653 reachability[r].push_back (&nb_tuple);
00654 }
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664 NeighborTuple const *max = NULL;
00665 int max_r = 0;
00666 for (std::set<int>::iterator it = rs.begin (); it != rs.end (); it++)
00667 {
00668 int r = *it;
00669 if (r == 0)
00670 {
00671 continue;
00672 }
00673 for (std::vector<const NeighborTuple *>::iterator it2 = reachability[r].begin ();
00674 it2 != reachability[r].end (); it2++)
00675 {
00676 const NeighborTuple *nb_tuple = *it2;
00677 if (max == NULL || nb_tuple->willingness > max->willingness)
00678 {
00679 max = nb_tuple;
00680 max_r = r;
00681 }
00682 else if (nb_tuple->willingness == max->willingness)
00683 {
00684 if (r > max_r)
00685 {
00686 max = nb_tuple;
00687 max_r = r;
00688 }
00689 else if (r == max_r)
00690 {
00691 if (Degree (*nb_tuple) > Degree (*max))
00692 {
00693 max = nb_tuple;
00694 max_r = r;
00695 }
00696 }
00697 }
00698 }
00699 }
00700
00701 if (max != NULL)
00702 {
00703 mprSet.insert (max->neighborMainAddr);
00704 for (TwoHopNeighborSet::iterator twoHopNeigh = N2.begin ();
00705 twoHopNeigh != N2.end (); )
00706 {
00707 if (twoHopNeigh->neighborMainAddr == max->neighborMainAddr)
00708 {
00709 twoHopNeigh = N2.erase (twoHopNeigh);
00710 }
00711 else
00712 {
00713 twoHopNeigh++;
00714 }
00715 }
00716 }
00717 }
00718
00719 #ifdef NS3_LOG_ENABLE
00720 {
00721 std::ostringstream os;
00722 os << "[";
00723 for (MprSet::const_iterator iter = mprSet.begin ();
00724 iter != mprSet.end (); iter++)
00725 {
00726 MprSet::const_iterator next = iter;
00727 next++;
00728 os << *iter;
00729 if (next != mprSet.end ())
00730 os << ", ";
00731 }
00732 os << "]";
00733 NS_LOG_DEBUG ("Computed MPR set for node " << m_mainAddress << ": " << os.str ());
00734 }
00735 #endif
00736
00737 m_state.SetMprSet (mprSet);
00738 }
00739
00740
00741
00742
00743
00744
00745
00746 Ipv4Address
00747 AgentImpl::GetMainAddress (Ipv4Address iface_addr) const
00748 {
00749 const IfaceAssocTuple *tuple =
00750 m_state.FindIfaceAssocTuple (iface_addr);
00751
00752 if (tuple != NULL)
00753 return tuple->mainAddr;
00754 else
00755 return iface_addr;
00756 }
00757
00758
00759
00760
00761 void
00762 AgentImpl::RoutingTableComputation ()
00763 {
00764 NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " s: Node " << m_mainAddress
00765 << ": RoutingTableComputation begin...");
00766
00767
00768 m_routingTable->Clear ();
00769
00770
00771
00772 const NeighborSet &neighborSet = m_state.GetNeighbors ();
00773 for (NeighborSet::const_iterator it = neighborSet.begin ();
00774 it != neighborSet.end(); it++)
00775 {
00776 NeighborTuple const &nb_tuple = *it;
00777 NS_LOG_DEBUG ("Looking at neighbor tuple: " << nb_tuple);
00778 if (nb_tuple.status == NeighborTuple::STATUS_SYM)
00779 {
00780 bool nb_main_addr = false;
00781 const LinkTuple *lt = NULL;
00782 const LinkSet &linkSet = m_state.GetLinks ();
00783 for (LinkSet::const_iterator it2 = linkSet.begin();
00784 it2 != linkSet.end(); it2++)
00785 {
00786 LinkTuple const &link_tuple = *it2;
00787 NS_LOG_DEBUG ("Looking at link tuple: " << link_tuple
00788 << (link_tuple.time >= Simulator::Now ()? "" : " (expired)"));
00789 if ((GetMainAddress (link_tuple.neighborIfaceAddr) == nb_tuple.neighborMainAddr)
00790 && link_tuple.time >= Simulator::Now ())
00791 {
00792 NS_LOG_LOGIC ("Link tuple matches neighbor " << nb_tuple.neighborMainAddr
00793 << " => adding routing table entry to neighbor");
00794 lt = &link_tuple;
00795 m_routingTable->AddEntry (link_tuple.neighborIfaceAddr,
00796 link_tuple.neighborIfaceAddr,
00797 link_tuple.localIfaceAddr,
00798 1);
00799 if (link_tuple.neighborIfaceAddr == nb_tuple.neighborMainAddr)
00800 {
00801 nb_main_addr = true;
00802 }
00803 }
00804 else
00805 {
00806 NS_LOG_LOGIC ("Link tuple: linkMainAddress= " << GetMainAddress (link_tuple.neighborIfaceAddr)
00807 << "; neighborMainAddr = " << nb_tuple.neighborMainAddr
00808 << "; expired=" << int (link_tuple.time < Simulator::Now ())
00809 << " => IGNORE");
00810 }
00811 }
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822 if (!nb_main_addr && lt != NULL)
00823 {
00824 NS_LOG_LOGIC ("no R_dest_addr is equal to the main address of the neighbor "
00825 "=> adding additional routing entry");
00826 m_routingTable->AddEntry(nb_tuple.neighborMainAddr,
00827 lt->neighborIfaceAddr,
00828 lt->localIfaceAddr,
00829 1);
00830 }
00831 }
00832 }
00833
00834
00835
00836
00837
00838
00839 const TwoHopNeighborSet &twoHopNeighbors = m_state.GetTwoHopNeighbors ();
00840 for (TwoHopNeighborSet::const_iterator it = twoHopNeighbors.begin ();
00841 it != twoHopNeighbors.end (); it++)
00842 {
00843 TwoHopNeighborTuple const &nb2hop_tuple = *it;
00844
00845 NS_LOG_LOGIC ("Looking at two-hop neighbor tuple: " << nb2hop_tuple);
00846
00847
00848 if (m_state.FindNeighborTuple (nb2hop_tuple.twoHopNeighborAddr))
00849 {
00850 NS_LOG_LOGIC ("Two-hop neighbor tuple is also neighbor; skipped.");
00851 continue;
00852 }
00853
00854 if (nb2hop_tuple.twoHopNeighborAddr == m_mainAddress)
00855 {
00856 NS_LOG_LOGIC ("Two-hop neighbor is self; skipped.");
00857 continue;
00858 }
00859
00860
00861
00862
00863 bool nb2hopOk = false;
00864 for (NeighborSet::const_iterator neighbor = neighborSet.begin ();
00865 neighbor != neighborSet.end(); neighbor++)
00866 {
00867 if (neighbor->neighborMainAddr == nb2hop_tuple.neighborMainAddr
00868 && neighbor->willingness != OLSR_WILL_NEVER)
00869 {
00870 nb2hopOk = true;
00871 break;
00872 }
00873 }
00874 if (!nb2hopOk)
00875 {
00876 NS_LOG_LOGIC ("Two-hop neighbor tuple skipped: 2-hop neighbor "
00877 << nb2hop_tuple.twoHopNeighborAddr
00878 << " is attached to neighbor " << nb2hop_tuple.neighborMainAddr
00879 << ", which was not found in the Neighbor Set.");
00880 continue;
00881 }
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894 RoutingTableEntry entry;
00895 bool foundEntry = m_routingTable->Lookup (nb2hop_tuple.neighborMainAddr, entry);
00896 if (foundEntry)
00897 {
00898 NS_LOG_LOGIC ("Adding routing entry for two-hop neighbor.");
00899 m_routingTable->AddEntry (nb2hop_tuple.twoHopNeighborAddr,
00900 entry.nextAddr,
00901 entry.interface,
00902 2);
00903 }
00904 else
00905 {
00906 NS_LOG_LOGIC ("NOT adding routing entry for two-hop neighbor ("
00907 << nb2hop_tuple.twoHopNeighborAddr
00908 << " not found in the routing table)");
00909 }
00910 }
00911
00912 for (uint32_t h = 2; ; h++)
00913 {
00914 bool added = false;
00915
00916
00917
00918
00919
00920
00921
00922 const TopologySet &topology = m_state.GetTopologySet ();
00923 for (TopologySet::const_iterator it = topology.begin ();
00924 it != topology.end (); it++)
00925 {
00926 const TopologyTuple &topology_tuple = *it;
00927 NS_LOG_LOGIC ("Looking at topology tuple: " << topology_tuple);
00928
00929 RoutingTableEntry destAddrEntry, lastAddrEntry;
00930 bool have_destAddrEntry = m_routingTable->Lookup (topology_tuple.destAddr, destAddrEntry);
00931 bool have_lastAddrEntry = m_routingTable->Lookup (topology_tuple.lastAddr, lastAddrEntry);
00932 if (!have_destAddrEntry && have_lastAddrEntry && lastAddrEntry.distance == h)
00933 {
00934 NS_LOG_LOGIC ("Adding routing table entry based on the topology tuple.");
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945 m_routingTable->AddEntry (topology_tuple.destAddr,
00946 lastAddrEntry.nextAddr,
00947 lastAddrEntry.interface,
00948 h + 1);
00949 added = true;
00950 }
00951 else
00952 {
00953 NS_LOG_LOGIC ("NOT adding routing table entry based on the topology tuple: "
00954 "have_destAddrEntry=" << have_destAddrEntry
00955 << " have_lastAddrEntry=" << have_lastAddrEntry
00956 << " lastAddrEntry.distance=" << (int) lastAddrEntry.distance
00957 << " (h=" << h << ")");
00958 }
00959 }
00960
00961 if (!added)
00962 break;
00963 }
00964
00965
00966
00967
00968
00969
00970 const IfaceAssocSet &ifaceAssocSet = m_state.GetIfaceAssocSet ();
00971 for (IfaceAssocSet::const_iterator it = ifaceAssocSet.begin ();
00972 it != ifaceAssocSet.end (); it++)
00973 {
00974 IfaceAssocTuple const &tuple = *it;
00975 RoutingTableEntry entry1, entry2;
00976 bool have_entry1 = m_routingTable->Lookup (tuple.mainAddr, entry1);
00977 bool have_entry2 = m_routingTable->Lookup (tuple.ifaceAddr, entry2);
00978 if (have_entry1 && !have_entry2)
00979 {
00980
00981
00982
00983
00984
00985
00986 m_routingTable->AddEntry (tuple.ifaceAddr,
00987 entry1.nextAddr,
00988 entry1.interface,
00989 entry1.distance);
00990 }
00991 }
00992
00993 NS_LOG_DEBUG ("Node " << m_mainAddress << ": RoutingTableComputation end.");
00994 m_routingTableChanged (m_routingTable->GetSize ());
00995 }
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 void
01009 AgentImpl::ProcessHello (const olsr::MessageHeader &msg,
01010 const Ipv4Address &receiverIface,
01011 const Ipv4Address &senderIface)
01012 {
01013 const olsr::MessageHeader::Hello &hello = msg.GetHello ();
01014
01015 LinkSensing (msg, hello, receiverIface, senderIface);
01016
01017 #ifdef NS3_LOG_ENABLE
01018 {
01019 const LinkSet &links = m_state.GetLinks ();
01020 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
01021 << "s ** BEGIN dump Link Set for OLSR Node " << m_mainAddress);
01022 for (LinkSet::const_iterator link = links.begin (); link != links.end (); link++)
01023 {
01024 NS_LOG_DEBUG(*link);
01025 }
01026 NS_LOG_DEBUG ("** END dump Link Set for OLSR Node " << m_mainAddress);
01027
01028 const NeighborSet &neighbors = m_state.GetNeighbors ();
01029 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
01030 << "s ** BEGIN dump Neighbor Set for OLSR Node " << m_mainAddress);
01031 for (NeighborSet::const_iterator neighbor = neighbors.begin (); neighbor != neighbors.end (); neighbor++)
01032 {
01033 NS_LOG_DEBUG(*neighbor);
01034 }
01035 NS_LOG_DEBUG ("** END dump Neighbor Set for OLSR Node " << m_mainAddress);
01036 }
01037 #endif
01038
01039 PopulateNeighborSet (msg, hello);
01040 PopulateTwoHopNeighborSet (msg, hello);
01041
01042 #ifdef NS3_LOG_ENABLE
01043 {
01044 const TwoHopNeighborSet &twoHopNeighbors = m_state.GetTwoHopNeighbors ();
01045 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
01046 << "s ** BEGIN dump TwoHopNeighbor Set for OLSR Node " << m_mainAddress);
01047 for (TwoHopNeighborSet::const_iterator tuple = twoHopNeighbors.begin ();
01048 tuple != twoHopNeighbors.end (); tuple++)
01049 {
01050 NS_LOG_DEBUG(*tuple);
01051 }
01052 NS_LOG_DEBUG ("** END dump TwoHopNeighbor Set for OLSR Node " << m_mainAddress);
01053 }
01054 #endif
01055
01056 MprComputation ();
01057 PopulateMprSelectorSet (msg, hello);
01058 }
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069 void
01070 AgentImpl::ProcessTc (const olsr::MessageHeader &msg,
01071 const Ipv4Address &senderIface)
01072 {
01073 const olsr::MessageHeader::Tc &tc = msg.GetTc ();
01074 Time now = Simulator::Now ();
01075
01076
01077
01078 const LinkTuple *link_tuple = m_state.FindSymLinkTuple (senderIface, now);
01079 if (link_tuple == NULL)
01080 return;
01081
01082
01083
01084
01085
01086
01087 const TopologyTuple *topologyTuple =
01088 m_state.FindNewerTopologyTuple (msg.GetOriginatorAddress (), tc.ansn);
01089 if (topologyTuple != NULL)
01090 return;
01091
01092
01093
01094
01095
01096 m_state.EraseOlderTopologyTuples (msg.GetOriginatorAddress (), tc.ansn);
01097
01098
01099
01100 for (std::vector<Ipv4Address>::const_iterator i = tc.neighborAddresses.begin ();
01101 i != tc.neighborAddresses.end (); i++)
01102 {
01103 const Ipv4Address &addr = *i;
01104
01105
01106
01107
01108
01109 TopologyTuple *topologyTuple =
01110 m_state.FindTopologyTuple (addr, msg.GetOriginatorAddress ());
01111
01112 if (topologyTuple != NULL)
01113 {
01114 topologyTuple->expirationTime = now + msg.GetVTime ();
01115 }
01116 else
01117 {
01118
01119
01120
01121
01122
01123
01124 TopologyTuple topologyTuple;;
01125 topologyTuple.destAddr = addr;
01126 topologyTuple.lastAddr = msg.GetOriginatorAddress ();
01127 topologyTuple.sequenceNumber = tc.ansn;
01128 topologyTuple.expirationTime = now + msg.GetVTime ();
01129 AddTopologyTuple (topologyTuple);
01130
01131
01132 m_events.Track (Simulator::Schedule (DELAY (topologyTuple.expirationTime),
01133 &AgentImpl::TopologyTupleTimerExpire,
01134 this,
01135 topologyTuple.destAddr,
01136 topologyTuple.lastAddr));
01137 }
01138 }
01139
01140 #ifdef NS3_LOG_ENABLE
01141 {
01142 const TopologySet &topology = m_state.GetTopologySet ();
01143 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
01144 << "s ** BEGIN dump TopologySet for OLSR Node " << m_mainAddress);
01145 for (TopologySet::const_iterator tuple = topology.begin ();
01146 tuple != topology.end (); tuple++)
01147 {
01148 NS_LOG_DEBUG (*tuple);
01149 }
01150 NS_LOG_DEBUG ("** END dump TopologySet Set for OLSR Node " << m_mainAddress);
01151 }
01152 #endif
01153 }
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164 void
01165 AgentImpl::ProcessMid (const olsr::MessageHeader &msg,
01166 const Ipv4Address &senderIface)
01167 {
01168 const olsr::MessageHeader::Mid &mid = msg.GetMid ();
01169 Time now = Simulator::Now ();
01170
01171 NS_LOG_DEBUG ("Node " << m_mainAddress << " ProcessMid from " << senderIface);
01172
01173
01174 const LinkTuple *linkTuple = m_state.FindSymLinkTuple (senderIface, now);
01175 if (linkTuple == NULL)
01176 {
01177 NS_LOG_LOGIC ("Node " << m_mainAddress <<
01178 ": the sender interface of this message is not in the "
01179 "symmetric 1-hop neighborhood of this node,"
01180 " the message MUST be discarded.");
01181 return;
01182 }
01183
01184
01185 for (std::vector<Ipv4Address>::const_iterator i = mid.interfaceAddresses.begin ();
01186 i != mid.interfaceAddresses.end (); i++)
01187 {
01188 bool updated = false;
01189 IfaceAssocSet &ifaceAssoc = m_state.GetIfaceAssocSetMutable ();
01190 for (IfaceAssocSet::iterator tuple = ifaceAssoc.begin();
01191 tuple != ifaceAssoc.end(); tuple++)
01192 {
01193 if (tuple->ifaceAddr == *i
01194 && tuple->mainAddr == msg.GetOriginatorAddress ())
01195 {
01196 NS_LOG_LOGIC ("IfaceAssoc updated: " << *tuple);
01197 tuple->time = now + msg.GetVTime ();
01198 updated = true;
01199 }
01200 }
01201 if (!updated)
01202 {
01203 IfaceAssocTuple tuple;
01204 tuple.ifaceAddr = *i;
01205 tuple.mainAddr = msg.GetOriginatorAddress ();
01206 tuple.time = now + msg.GetVTime ();
01207 AddIfaceAssocTuple (tuple);
01208 NS_LOG_LOGIC ("New IfaceAssoc added: " << tuple);
01209
01210 Simulator::Schedule (DELAY (tuple.time),
01211 &AgentImpl::IfaceAssocTupleTimerExpire, this, tuple.ifaceAddr);
01212 }
01213 }
01214
01215
01216
01217
01218 NeighborSet &neighbors = m_state.GetNeighbors ();
01219 for (NeighborSet::iterator neighbor = neighbors.begin (); neighbor != neighbors.end(); neighbor++)
01220 {
01221 neighbor->neighborMainAddr = GetMainAddress (neighbor->neighborMainAddr);
01222 }
01223
01224 TwoHopNeighborSet &twoHopNeighbors = m_state.GetTwoHopNeighbors ();
01225 for (TwoHopNeighborSet::iterator twoHopNeighbor = twoHopNeighbors.begin ();
01226 twoHopNeighbor != twoHopNeighbors.end(); twoHopNeighbor++)
01227 {
01228 twoHopNeighbor->neighborMainAddr = GetMainAddress (twoHopNeighbor->neighborMainAddr);
01229 twoHopNeighbor->twoHopNeighborAddr = GetMainAddress (twoHopNeighbor->twoHopNeighborAddr);
01230 }
01231 NS_LOG_DEBUG ("Node " << m_mainAddress << " ProcessMid from " << senderIface << " -> END.");
01232 }
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246 void
01247 AgentImpl::ForwardDefault (olsr::MessageHeader olsrMessage,
01248 DuplicateTuple *duplicated,
01249 const Ipv4Address &localIface,
01250 const Ipv4Address &senderAddress)
01251 {
01252 Time now = Simulator::Now ();
01253
01254
01255
01256 const LinkTuple *linkTuple = m_state.FindSymLinkTuple (senderAddress, now);
01257 if (linkTuple == NULL)
01258 return;
01259
01260
01261
01262 if (duplicated != NULL && duplicated->retransmitted)
01263 {
01264 NS_LOG_LOGIC (Simulator::Now () << "Node " << m_mainAddress << " does not forward a message received"
01265 " from " << olsrMessage.GetOriginatorAddress () << " because it is duplicated");
01266 return;
01267 }
01268
01269
01270
01271
01272 bool retransmitted = false;
01273 if (olsrMessage.GetTimeToLive () > 1)
01274 {
01275 const MprSelectorTuple *mprselTuple =
01276 m_state.FindMprSelectorTuple (GetMainAddress (senderAddress));
01277 if (mprselTuple != NULL)
01278 {
01279 olsrMessage.SetTimeToLive (olsrMessage.GetTimeToLive () - 1);
01280 olsrMessage.SetHopCount (olsrMessage.GetHopCount () + 1);
01281
01282
01283 QueueMessage (olsrMessage, JITTER);
01284 retransmitted = true;
01285 }
01286 }
01287
01288
01289 if (duplicated != NULL)
01290 {
01291 duplicated->expirationTime = now + OLSR_DUP_HOLD_TIME;
01292 duplicated->retransmitted = retransmitted;
01293 duplicated->ifaceList.push_back (localIface);
01294 }
01295
01296 else
01297 {
01298 DuplicateTuple newDup;
01299 newDup.address = olsrMessage.GetOriginatorAddress ();
01300 newDup.sequenceNumber = olsrMessage.GetMessageSequenceNumber ();
01301 newDup.expirationTime = now + OLSR_DUP_HOLD_TIME;
01302 newDup.retransmitted = retransmitted;
01303 newDup.ifaceList.push_back (localIface);
01304 AddDuplicateTuple (newDup);
01305
01306 Simulator::Schedule (OLSR_DUP_HOLD_TIME,
01307 &AgentImpl::DupTupleTimerExpire, this,
01308 newDup.address, newDup.sequenceNumber);
01309 }
01310 }
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321 void
01322 AgentImpl::QueueMessage (const olsr::MessageHeader &message, Time delay)
01323 {
01324 m_queuedMessages.push_back (message);
01325 if (not m_queuedMessagesTimer.IsRunning ())
01326 {
01327 m_queuedMessagesTimer.SetDelay (delay);
01328 m_queuedMessagesTimer.Schedule ();
01329 }
01330 }
01331
01332 void
01333 AgentImpl::SendPacket (Ptr<Packet> packet,
01334 const MessageList &containedMessages)
01335 {
01336 NS_LOG_DEBUG ("OLSR node " << m_mainAddress << " sending a OLSR packet");
01337
01338
01339 olsr::PacketHeader header;
01340 header.SetPacketLength (header.GetSerializedSize () + packet->GetSize ());
01341 header.SetPacketSequenceNumber (GetPacketSequenceNumber ());
01342 packet->AddHeader (header);
01343
01344
01345 m_txPacketTrace (header, containedMessages);
01346
01347
01348 m_socketAddresses.begin ()->first->Send (packet);
01349 }
01350
01351
01352
01353
01354
01355
01356
01357
01358 void
01359 AgentImpl::SendQueuedMessages ()
01360 {
01361 Ptr<Packet> packet = Create<Packet> ();
01362 int numMessages = 0;
01363
01364 NS_LOG_DEBUG ("Olsr node " << m_mainAddress << ": SendQueuedMessages");
01365
01366 MessageList msglist;
01367
01368 for (std::vector<olsr::MessageHeader>::const_iterator message = m_queuedMessages.begin ();
01369 message != m_queuedMessages.end ();
01370 message++)
01371 {
01372 Ptr<Packet> p = Create<Packet> ();
01373 p->AddHeader (*message);
01374 packet->AddAtEnd (p);
01375 msglist.push_back (*message);
01376 if (++numMessages == OLSR_MAX_MSGS)
01377 {
01378 SendPacket (packet, msglist);
01379 msglist.clear ();
01380
01381 numMessages = 0;
01382 packet = Create<Packet> ();
01383 }
01384 }
01385
01386 if (packet->GetSize ())
01387 {
01388 SendPacket (packet, msglist);
01389 }
01390
01391 m_queuedMessages.clear ();
01392 }
01393
01394
01395
01396
01397 void
01398 AgentImpl::SendHello ()
01399 {
01400 NS_LOG_FUNCTION (this);
01401
01402 olsr::MessageHeader msg;
01403 Time now = Simulator::Now ();
01404
01405 msg.SetVTime (OLSR_NEIGHB_HOLD_TIME);
01406 msg.SetOriginatorAddress (m_mainAddress);
01407 msg.SetTimeToLive (1);
01408 msg.SetHopCount (0);
01409 msg.SetMessageSequenceNumber (GetMessageSequenceNumber ());
01410 olsr::MessageHeader::Hello &hello = msg.GetHello ();
01411
01412 hello.SetHTime (Scalar (3) * m_helloInterval);
01413 hello.willingness = m_willingness;
01414
01415 std::vector<olsr::MessageHeader::Hello::LinkMessage>
01416 &linkMessages = hello.linkMessages;
01417
01418 const LinkSet &links = m_state.GetLinks ();
01419 for (LinkSet::const_iterator link_tuple = links.begin ();
01420 link_tuple != links.end (); link_tuple++)
01421 {
01422 if (!(GetMainAddress (link_tuple->localIfaceAddr) == m_mainAddress
01423 && link_tuple->time >= now))
01424 {
01425 continue;
01426 }
01427
01428 uint8_t link_type, nb_type = 0xff;
01429
01430
01431 if (link_tuple->symTime >= now)
01432 {
01433 link_type = OLSR_SYM_LINK;
01434 }
01435 else if (link_tuple->asymTime >= now)
01436 {
01437 link_type = OLSR_ASYM_LINK;
01438 }
01439 else
01440 {
01441 link_type = OLSR_LOST_LINK;
01442 }
01443
01444 if (m_state.FindMprAddress (GetMainAddress (link_tuple->neighborIfaceAddr)))
01445 {
01446 nb_type = OLSR_MPR_NEIGH;
01447 NS_LOG_DEBUG ("I consider neighbor " << GetMainAddress (link_tuple->neighborIfaceAddr)
01448 << " to be MPR_NEIGH.");
01449 }
01450 else
01451 {
01452 bool ok = false;
01453 for (NeighborSet::const_iterator nb_tuple = m_state.GetNeighbors ().begin ();
01454 nb_tuple != m_state.GetNeighbors ().end ();
01455 nb_tuple++)
01456 {
01457 if (nb_tuple->neighborMainAddr == GetMainAddress (link_tuple->neighborIfaceAddr))
01458 {
01459 if (nb_tuple->status == NeighborTuple::STATUS_SYM)
01460 {
01461 NS_LOG_DEBUG ("I consider neighbor " << GetMainAddress (link_tuple->neighborIfaceAddr)
01462 << " to be SYM_NEIGH.");
01463 nb_type = OLSR_SYM_NEIGH;
01464 }
01465 else if (nb_tuple->status == NeighborTuple::STATUS_NOT_SYM)
01466 {
01467 nb_type = OLSR_NOT_NEIGH;
01468 NS_LOG_DEBUG ("I consider neighbor " << GetMainAddress (link_tuple->neighborIfaceAddr)
01469 << " to be NOT_NEIGH.");
01470 }
01471 else
01472 {
01473 NS_FATAL_ERROR ("There is a neighbor tuple with an unknown status!\n");
01474 }
01475 ok = true;
01476 break;
01477 }
01478 }
01479 if (!ok)
01480 {
01481 NS_LOG_WARN ("I don't know the neighbor " << GetMainAddress (link_tuple->neighborIfaceAddr) << "!!!");
01482 continue;
01483 }
01484 }
01485
01486 olsr::MessageHeader::Hello::LinkMessage linkMessage;
01487 linkMessage.linkCode = (link_type & 0x03) | ((nb_type << 2) & 0x0f);
01488 linkMessage.neighborInterfaceAddresses.push_back
01489 (link_tuple->neighborIfaceAddr);
01490
01491 std::vector<Ipv4Address> interfaces =
01492 m_state.FindNeighborInterfaces (link_tuple->neighborIfaceAddr);
01493
01494 linkMessage.neighborInterfaceAddresses.insert
01495 (linkMessage.neighborInterfaceAddresses.end (),
01496 interfaces.begin (), interfaces.end ());
01497
01498 linkMessages.push_back (linkMessage);
01499 }
01500 NS_LOG_DEBUG ("OLSR HELLO message size: " << int (msg.GetSerializedSize ())
01501 << " (with " << int (linkMessages.size ()) << " link messages)");
01502 QueueMessage (msg, JITTER);
01503 }
01504
01505
01506
01507
01508 void
01509 AgentImpl::SendTc ()
01510 {
01511 NS_LOG_FUNCTION (this);
01512
01513 olsr::MessageHeader msg;
01514
01515 msg.SetVTime (OLSR_TOP_HOLD_TIME);
01516 msg.SetOriginatorAddress (m_mainAddress);
01517 msg.SetTimeToLive (255);
01518 msg.SetHopCount (0);
01519 msg.SetMessageSequenceNumber (GetMessageSequenceNumber ());
01520
01521 olsr::MessageHeader::Tc &tc = msg.GetTc ();
01522 tc.ansn = m_ansn;
01523 for (MprSelectorSet::const_iterator mprsel_tuple = m_state.GetMprSelectors ().begin();
01524 mprsel_tuple != m_state.GetMprSelectors ().end(); mprsel_tuple++)
01525 {
01526 tc.neighborAddresses.push_back (mprsel_tuple->mainAddr);
01527 }
01528 QueueMessage (msg, JITTER);
01529 }
01530
01531
01532
01533
01534 void
01535 AgentImpl::SendMid ()
01536 {
01537 olsr::MessageHeader msg;
01538 olsr::MessageHeader::Mid &mid = msg.GetMid ();
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556 Ipv4Address loopback ("127.0.0.1");
01557 for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
01558 {
01559 Ipv4Address addr = m_ipv4->GetAddress (i);
01560 if (addr != m_mainAddress && addr != loopback)
01561 mid.interfaceAddresses.push_back (addr);
01562 }
01563 if (mid.interfaceAddresses.size () == 0)
01564 return;
01565
01566 msg.SetVTime (OLSR_MID_HOLD_TIME);
01567 msg.SetOriginatorAddress (m_mainAddress);
01568 msg.SetTimeToLive (255);
01569 msg.SetHopCount (0);
01570 msg.SetMessageSequenceNumber (GetMessageSequenceNumber ());
01571
01572 QueueMessage (msg, JITTER);
01573 }
01574
01575
01576
01577
01578 void
01579 AgentImpl::LinkSensing (const olsr::MessageHeader &msg,
01580 const olsr::MessageHeader::Hello &hello,
01581 const Ipv4Address &receiverIface,
01582 const Ipv4Address &senderIface)
01583 {
01584 Time now = Simulator::Now ();
01585 bool updated = false;
01586 bool created = false;
01587 NS_LOG_DEBUG ("@" << now.GetSeconds () << ": Olsr node " << m_mainAddress
01588 << ": LinkSensing(receiverIface=" << receiverIface
01589 << ", senderIface=" << senderIface << ") BEGIN");
01590
01591 NS_ASSERT (msg.GetVTime () > Seconds (0));
01592 LinkTuple *link_tuple = m_state.FindLinkTuple (senderIface);
01593 if (link_tuple == NULL)
01594 {
01595 LinkTuple newLinkTuple;
01596
01597 newLinkTuple.neighborIfaceAddr = senderIface;
01598 newLinkTuple.localIfaceAddr = receiverIface;
01599 newLinkTuple.symTime = now - Seconds (1);
01600 newLinkTuple.time = now + msg.GetVTime ();
01601 link_tuple = &m_state.InsertLinkTuple (newLinkTuple);
01602 created = true;
01603 NS_LOG_LOGIC ("Existing link tuple did not exist => creating new one");
01604 }
01605 else
01606 {
01607 NS_LOG_LOGIC ("Existing link tuple already exists => will update it");
01608 updated = true;
01609 }
01610
01611 link_tuple->asymTime = now + msg.GetVTime ();
01612 for (std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator linkMessage =
01613 hello.linkMessages.begin ();
01614 linkMessage != hello.linkMessages.end ();
01615 linkMessage++)
01616 {
01617 int lt = linkMessage->linkCode & 0x03;
01618 int nt = (linkMessage->linkCode >> 2) & 0x03;
01619
01620 #ifdef NS3_LOG_ENABLE
01621 const char *linkTypeName;
01622 switch (lt)
01623 {
01624 case OLSR_UNSPEC_LINK: linkTypeName = "UNSPEC_LINK"; break;
01625 case OLSR_ASYM_LINK: linkTypeName = "ASYM_LINK"; break;
01626 case OLSR_SYM_LINK: linkTypeName = "SYM_LINK"; break;
01627 case OLSR_LOST_LINK: linkTypeName = "LOST_LINK"; break;
01628 default: linkTypeName = "(invalid value!)";
01629 }
01630
01631 const char *neighborTypeName;
01632 switch (nt)
01633 {
01634 case OLSR_NOT_NEIGH: neighborTypeName = "NOT_NEIGH"; break;
01635 case OLSR_SYM_NEIGH: neighborTypeName = "SYM_NEIGH"; break;
01636 case OLSR_MPR_NEIGH: neighborTypeName = "MPR_NEIGH"; break;
01637 default: neighborTypeName = "(invalid value!)";
01638 }
01639
01640 NS_LOG_DEBUG ("Looking at HELLO link messages with Link Type "
01641 << lt << " (" << linkTypeName
01642 << ") and Neighbor Type " << nt
01643 << " (" << neighborTypeName << ")");
01644 #endif
01645
01646
01647 if ((lt == OLSR_SYM_LINK && nt == OLSR_NOT_NEIGH) ||
01648 (nt != OLSR_SYM_NEIGH && nt != OLSR_MPR_NEIGH
01649 && nt != OLSR_NOT_NEIGH))
01650 {
01651 NS_LOG_LOGIC ("HELLO link code is invalid => IGNORING");
01652 continue;
01653 }
01654
01655 for (std::vector<Ipv4Address>::const_iterator neighIfaceAddr =
01656 linkMessage->neighborInterfaceAddresses.begin ();
01657 neighIfaceAddr != linkMessage->neighborInterfaceAddresses.end ();
01658 neighIfaceAddr++)
01659 {
01660 NS_LOG_DEBUG (" -> Neighbor: " << *neighIfaceAddr);
01661 if (*neighIfaceAddr == receiverIface)
01662 {
01663 if (lt == OLSR_LOST_LINK)
01664 {
01665 NS_LOG_LOGIC ("link is LOST => expiring it");
01666 link_tuple->symTime = now - Seconds (1);
01667 updated = true;
01668 }
01669 else if (lt == OLSR_SYM_LINK || lt == OLSR_ASYM_LINK)
01670 {
01671 NS_LOG_DEBUG (*link_tuple << ": link is SYM or ASYM => should become SYM now"
01672 " (symTime being increased to " << now + msg.GetVTime ());
01673 link_tuple->symTime = now + msg.GetVTime ();
01674 link_tuple->time = link_tuple->symTime + OLSR_NEIGHB_HOLD_TIME;
01675 updated = true;
01676 }
01677 else
01678 {
01679 NS_FATAL_ERROR ("bad link type");
01680 }
01681 break;
01682 }
01683 else
01684 {
01685 NS_LOG_DEBUG (" \\-> *neighIfaceAddr (" << *neighIfaceAddr
01686 << " != receiverIface (" << receiverIface << ") => IGNORING!");
01687 }
01688 }
01689 NS_LOG_DEBUG ("Link tuple updated: " << int (updated));
01690 }
01691 link_tuple->time = std::max(link_tuple->time, link_tuple->asymTime);
01692
01693 if (updated)
01694 {
01695 LinkTupleUpdated (*link_tuple, hello.willingness);
01696 }
01697
01698
01699 if (created && link_tuple != NULL)
01700 {
01701 LinkTupleAdded (*link_tuple, hello.willingness);
01702 m_events.Track (Simulator::Schedule (DELAY (std::min (link_tuple->time, link_tuple->symTime)),
01703 &AgentImpl::LinkTupleTimerExpire, this,
01704 link_tuple->neighborIfaceAddr));
01705 }
01706 NS_LOG_DEBUG ("@" << now.GetSeconds () << ": Olsr node " << m_mainAddress
01707 << ": LinkSensing END");
01708 }
01709
01710
01711
01712
01713 void
01714 AgentImpl::PopulateNeighborSet (const olsr::MessageHeader &msg,
01715 const olsr::MessageHeader::Hello &hello)
01716 {
01717 NeighborTuple *nb_tuple = m_state.FindNeighborTuple (msg.GetOriginatorAddress ());
01718 if (nb_tuple != NULL)
01719 {
01720 nb_tuple->willingness = hello.willingness;
01721 }
01722 }
01723
01724
01725
01726
01727
01728 void
01729 AgentImpl::PopulateTwoHopNeighborSet (const olsr::MessageHeader &msg,
01730 const olsr::MessageHeader::Hello &hello)
01731 {
01732 Time now = Simulator::Now ();
01733
01734 NS_LOG_DEBUG ("Olsr node " << m_mainAddress << ": PopulateTwoHopNeighborSet BEGIN");
01735
01736 for (LinkSet::const_iterator link_tuple = m_state.GetLinks ().begin ();
01737 link_tuple != m_state.GetLinks ().end (); link_tuple++)
01738 {
01739 NS_LOG_LOGIC ("Looking at link tuple: " << *link_tuple);
01740 if (GetMainAddress (link_tuple->neighborIfaceAddr) != msg.GetOriginatorAddress ())
01741 {
01742 NS_LOG_LOGIC ("Link tuple ignored: "
01743 "GetMainAddress (link_tuple->neighborIfaceAddr) != msg.GetOriginatorAddress ()");
01744 NS_LOG_LOGIC ("(GetMainAddress(" << link_tuple->neighborIfaceAddr << "): "
01745 << GetMainAddress (link_tuple->neighborIfaceAddr)
01746 << "; msg.GetOriginatorAddress (): " << msg.GetOriginatorAddress ());
01747 continue;
01748 }
01749
01750 if (link_tuple->symTime < now)
01751 {
01752 NS_LOG_LOGIC ("Link tuple ignored: expired.");
01753 continue;
01754 }
01755
01756 typedef std::vector<olsr::MessageHeader::Hello::LinkMessage> LinkMessageVec;
01757 for (LinkMessageVec::const_iterator linkMessage = hello.linkMessages.begin ();
01758 linkMessage != hello.linkMessages.end (); linkMessage++)
01759 {
01760 int neighborType = (linkMessage->linkCode >> 2) & 0x3;
01761 #ifdef NS3_LOG_ENABLE
01762 const char *neighborTypeNames[3] = { "NOT_NEIGH", "SYM_NEIGH", "MPR_NEIGH" };
01763 const char *neighborTypeName = ((neighborType < 3)?
01764 neighborTypeNames[neighborType]
01765 : "(invalid value)");
01766 NS_LOG_DEBUG ("Looking at Link Message from HELLO message: neighborType="
01767 << neighborType << " (" << neighborTypeName << ")");
01768 #endif
01769
01770 for (std::vector<Ipv4Address>::const_iterator nb2hop_addr_iter =
01771 linkMessage->neighborInterfaceAddresses.begin ();
01772 nb2hop_addr_iter != linkMessage->neighborInterfaceAddresses.end ();
01773 nb2hop_addr_iter++)
01774 {
01775 Ipv4Address nb2hop_addr = GetMainAddress (*nb2hop_addr_iter);
01776 NS_LOG_DEBUG ("Looking at 2-hop neighbor address from HELLO message: "
01777 << *nb2hop_addr_iter
01778 << " (main address is " << nb2hop_addr << ")");
01779 if (neighborType == OLSR_SYM_NEIGH || neighborType == OLSR_MPR_NEIGH)
01780 {
01781
01782
01783
01784 if (nb2hop_addr == m_routingAgentAddr)
01785 {
01786 NS_LOG_LOGIC ("Ignoring 2-hop neighbor (it is the node itself)");
01787 continue;
01788 }
01789
01790
01791 TwoHopNeighborTuple *nb2hop_tuple =
01792 m_state.FindTwoHopNeighborTuple (msg.GetOriginatorAddress (), nb2hop_addr);
01793 NS_LOG_LOGIC ("Adding the 2-hop neighbor"
01794 << (nb2hop_tuple? " (refreshing existing entry)" : ""));
01795 if (nb2hop_tuple == NULL)
01796 {
01797 TwoHopNeighborTuple new_nb2hop_tuple;
01798 new_nb2hop_tuple.neighborMainAddr = msg.GetOriginatorAddress ();
01799 new_nb2hop_tuple.twoHopNeighborAddr = nb2hop_addr;
01800 new_nb2hop_tuple.expirationTime = now + msg.GetVTime ();
01801 AddTwoHopNeighborTuple (new_nb2hop_tuple);
01802
01803 m_events.Track (Simulator::Schedule (DELAY (new_nb2hop_tuple.expirationTime),
01804 &AgentImpl::Nb2hopTupleTimerExpire, this,
01805 new_nb2hop_tuple.neighborMainAddr,
01806 new_nb2hop_tuple.twoHopNeighborAddr));
01807 }
01808 else
01809 {
01810 nb2hop_tuple->expirationTime = now + msg.GetVTime ();
01811 }
01812 }
01813 else if (neighborType == OLSR_NOT_NEIGH)
01814 {
01815
01816
01817
01818
01819
01820 NS_LOG_LOGIC ("2-hop neighbor is NOT_NEIGH => deleting matching 2-hop neighbor state");
01821 m_state.EraseTwoHopNeighborTuples (msg.GetOriginatorAddress (), nb2hop_addr);
01822 }
01823 else
01824 {
01825 NS_LOG_LOGIC ("*** WARNING *** Ignoring link message (inside HELLO) with bad"
01826 " neighbor type value: " << neighborType);
01827 }
01828 }
01829 }
01830 }
01831
01832 NS_LOG_DEBUG ("Olsr node " << m_mainAddress << ": PopulateTwoHopNeighborSet END");
01833 }
01834
01835
01836
01837
01838
01839
01840 void
01841 AgentImpl::PopulateMprSelectorSet (const olsr::MessageHeader &msg,
01842 const olsr::MessageHeader::Hello &hello)
01843 {
01844 NS_LOG_FUNCTION (this);
01845
01846 Time now = Simulator::Now ();
01847
01848 typedef std::vector<olsr::MessageHeader::Hello::LinkMessage> LinkMessageVec;
01849 for (LinkMessageVec::const_iterator linkMessage = hello.linkMessages.begin ();
01850 linkMessage != hello.linkMessages.end ();
01851 linkMessage++)
01852 {
01853 int nt = linkMessage->linkCode >> 2;
01854 if (nt == OLSR_MPR_NEIGH)
01855 {
01856 NS_LOG_DEBUG ("Processing a link message with neighbor type MPR_NEIGH");
01857
01858 for (std::vector<Ipv4Address>::const_iterator nb_iface_addr =
01859 linkMessage->neighborInterfaceAddresses.begin ();
01860 nb_iface_addr != linkMessage->neighborInterfaceAddresses.end ();
01861 nb_iface_addr++)
01862 {
01863 if (GetMainAddress (*nb_iface_addr) == m_mainAddress)
01864 {
01865 NS_LOG_DEBUG ("Adding entry to mpr selector set for neighbor " << *nb_iface_addr);
01866
01867
01868 MprSelectorTuple *existing_mprsel_tuple =
01869 m_state.FindMprSelectorTuple (msg.GetOriginatorAddress ());
01870 if (existing_mprsel_tuple == NULL)
01871 {
01872 MprSelectorTuple mprsel_tuple;
01873
01874 mprsel_tuple.mainAddr = msg.GetOriginatorAddress ();
01875 mprsel_tuple.expirationTime = now + msg.GetVTime ();
01876 AddMprSelectorTuple (mprsel_tuple);
01877
01878
01879 m_events.Track (Simulator::Schedule
01880 (DELAY (mprsel_tuple.expirationTime),
01881 &AgentImpl::MprSelTupleTimerExpire, this,
01882 mprsel_tuple.mainAddr));
01883 }
01884 else
01885 {
01886 existing_mprsel_tuple->expirationTime = now + msg.GetVTime ();
01887 }
01888 }
01889 }
01890 }
01891 }
01892 NS_LOG_DEBUG ("Computed MPR selector set for node " << m_mainAddress << ": " << m_state.PrintMprSelectorSet ());
01893 }
01894
01895
01896 #if 0
01897
01898
01899
01900
01901
01902
01903
01904 void
01905 OLSR::mac_failed(Ptr<Packet> p) {
01906 double now = Simulator::Now ();
01907 struct hdr_ip* ih = HDR_IP(p);
01908 struct hdr_cmn* ch = HDR_CMN(p);
01909
01910 debug("%f: Node %d MAC Layer detects a breakage on link to %d\n",
01911 now,
01912 OLSR::node_id(ra_addr()),
01913 OLSR::node_id(ch->next_hop()));
01914
01915 if ((u_int32_t)ih->daddr() == IP_BROADCAST) {
01916 drop(p, DROP_RTR_MAC_CALLBACK);
01917 return;
01918 }
01919
01920 OLSR_link_tuple* link_tuple = state_.find_link_tuple(ch->next_hop());
01921 if (link_tuple != NULL) {
01922 link_tuple->lost_time() = now + OLSR_NEIGHB_HOLD_TIME;
01923 link_tuple->time() = now + OLSR_NEIGHB_HOLD_TIME;
01924 nb_loss(link_tuple);
01925 }
01926 drop(p, DROP_RTR_MAC_CALLBACK);
01927 }
01928 #endif
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940 void
01941 AgentImpl::NeighborLoss (const LinkTuple &tuple)
01942 {
01943 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
01944 << "s: OLSR Node " << m_mainAddress
01945 << " LinkTuple " << tuple.neighborIfaceAddr << " -> neighbor loss.");
01946 LinkTupleUpdated (tuple, OLSR_WILL_DEFAULT);
01947 m_state.EraseTwoHopNeighborTuples (GetMainAddress (tuple.neighborIfaceAddr));
01948 m_state.EraseMprSelectorTuples (GetMainAddress (tuple.neighborIfaceAddr));
01949
01950 MprComputation ();
01951 RoutingTableComputation ();
01952 }
01953
01954
01955
01956
01957
01958
01959 void
01960 AgentImpl::AddDuplicateTuple (const DuplicateTuple &tuple)
01961 {
01962
01963
01964
01965
01966
01967 m_state.InsertDuplicateTuple (tuple);
01968 }
01969
01970
01971
01972
01973
01974
01975 void
01976 AgentImpl::RemoveDuplicateTuple (const DuplicateTuple &tuple)
01977 {
01978
01979
01980
01981
01982
01983 m_state.EraseDuplicateTuple (tuple);
01984 }
01985
01986 void
01987 AgentImpl::LinkTupleAdded (const LinkTuple &tuple, uint8_t willingness)
01988 {
01989
01990 NeighborTuple nb_tuple;
01991 nb_tuple.neighborMainAddr = GetMainAddress (tuple.neighborIfaceAddr);
01992 nb_tuple.willingness = willingness;
01993
01994 if (tuple.symTime >= Simulator::Now ())
01995 {
01996 nb_tuple.status = NeighborTuple::STATUS_SYM;
01997 }
01998 else
01999 {
02000 nb_tuple.status = NeighborTuple::STATUS_NOT_SYM;
02001 }
02002
02003 AddNeighborTuple (nb_tuple);
02004 }
02005
02006
02007
02008
02009
02010
02011 void
02012 AgentImpl::RemoveLinkTuple (const LinkTuple &tuple)
02013 {
02014 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
02015 << "s: OLSR Node " << m_mainAddress
02016 << " LinkTuple " << tuple << " REMOVED.");
02017
02018 m_state.EraseLinkTuple (tuple);
02019 m_state.EraseNeighborTuple (GetMainAddress (tuple.neighborIfaceAddr));
02020
02021 }
02022
02023
02024
02025
02026
02027
02028
02029 void
02030 AgentImpl::LinkTupleUpdated (const LinkTuple &tuple, uint8_t willingness)
02031 {
02032
02033
02034 NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
02035 << "s: OLSR Node " << m_mainAddress
02036 << " LinkTuple " << tuple << " UPDATED.");
02037
02038 NeighborTuple *nb_tuple =
02039 m_state.FindNeighborTuple (GetMainAddress (tuple.neighborIfaceAddr));
02040
02041 if (nb_tuple == NULL)
02042 {
02043 LinkTupleAdded (tuple, willingness);
02044 nb_tuple = m_state.FindNeighborTuple (GetMainAddress (tuple.neighborIfaceAddr));
02045 }
02046
02047 if (nb_tuple != NULL)
02048 {
02049 #ifdef NS3_LOG_ENABLE
02050 int statusBefore = nb_tuple->status;
02051 #endif
02052 if (tuple.symTime >= Simulator::Now ())
02053 {
02054 nb_tuple->status = NeighborTuple::STATUS_SYM;
02055 NS_LOG_DEBUG (*nb_tuple << "->status = STATUS_SYM; changed:"
02056 << int (statusBefore != nb_tuple->status));
02057 }
02058 else
02059 {
02060 nb_tuple->status = NeighborTuple::STATUS_NOT_SYM;
02061 NS_LOG_DEBUG (*nb_tuple << "->status = STATUS_NOT_SYM; changed:"
02062 << int (statusBefore != nb_tuple->status));
02063 }
02064 }
02065 else
02066 {
02067 NS_LOG_WARN ("ERROR! Wanted to update a NeighborTuple but none was found!");
02068 }
02069 }
02070
02071
02072
02073
02074
02075
02076 void
02077 AgentImpl::AddNeighborTuple (const NeighborTuple &tuple)
02078 {
02079
02080
02081
02082
02083
02084
02085 m_state.InsertNeighborTuple (tuple);
02086 IncrementAnsn ();
02087 }
02088
02089
02090
02091
02092
02093
02094 void
02095 AgentImpl::RemoveNeighborTuple (const NeighborTuple &tuple)
02096 {
02097
02098
02099
02100
02101
02102
02103 m_state.EraseNeighborTuple (tuple);
02104 IncrementAnsn ();
02105 }
02106
02107
02108
02109
02110
02111
02112 void
02113 AgentImpl::AddTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple)
02114 {
02115
02116
02117
02118
02119
02120
02121 m_state.InsertTwoHopNeighborTuple (tuple);
02122 }
02123
02124
02125
02126
02127
02128
02129 void
02130 AgentImpl::RemoveTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple)
02131 {
02132
02133
02134
02135
02136
02137
02138 m_state.EraseTwoHopNeighborTuple (tuple);
02139 }
02140
02141 void
02142 AgentImpl::IncrementAnsn ()
02143 {
02144 m_ansn = (m_ansn + 1) % (OLSR_MAX_SEQ_NUM + 1);
02145 }
02146
02147
02148
02149
02150
02151
02152
02153
02154 void
02155 AgentImpl::AddMprSelectorTuple (const MprSelectorTuple &tuple)
02156 {
02157
02158
02159
02160
02161
02162 m_state.InsertMprSelectorTuple (tuple);
02163 IncrementAnsn ();
02164 }
02165
02166
02167
02168
02169
02170
02171
02172
02173 void
02174 AgentImpl::RemoveMprSelectorTuple (const MprSelectorTuple &tuple)
02175 {
02176
02177
02178
02179
02180
02181 m_state.EraseMprSelectorTuple (tuple);
02182 IncrementAnsn ();
02183 }
02184
02185
02186
02187
02188
02189
02190 void
02191 AgentImpl::AddTopologyTuple (const TopologyTuple &tuple)
02192 {
02193
02194
02195
02196
02197
02198
02199
02200 m_state.InsertTopologyTuple(tuple);
02201 }
02202
02203
02204
02205
02206
02207
02208 void
02209 AgentImpl::RemoveTopologyTuple (const TopologyTuple &tuple)
02210 {
02211
02212
02213
02214
02215
02216
02217
02218 m_state.EraseTopologyTuple (tuple);
02219 }
02220
02221
02222
02223
02224
02225
02226 void
02227 AgentImpl::AddIfaceAssocTuple (const IfaceAssocTuple &tuple)
02228 {
02229
02230
02231
02232
02233
02234
02235 m_state.InsertIfaceAssocTuple (tuple);
02236 }
02237
02238
02239
02240
02241
02242
02243 void
02244 AgentImpl::RemoveIfaceAssocTuple (const IfaceAssocTuple &tuple)
02245 {
02246
02247
02248
02249
02250
02251
02252 m_state.EraseIfaceAssocTuple (tuple);
02253 }
02254
02255
02256 uint16_t AgentImpl::GetPacketSequenceNumber ()
02257 {
02258 m_packetSequenceNumber = (m_packetSequenceNumber + 1) % (OLSR_MAX_SEQ_NUM + 1);
02259 return m_packetSequenceNumber;
02260 }
02261
02262
02263 uint16_t AgentImpl::GetMessageSequenceNumber ()
02264 {
02265 m_messageSequenceNumber = (m_messageSequenceNumber + 1) % (OLSR_MAX_SEQ_NUM + 1);
02266 return m_messageSequenceNumber;
02267 }
02268
02269
02270
02271
02272
02273
02274 void
02275 AgentImpl::HelloTimerExpire ()
02276 {
02277 SendHello ();
02278 m_helloTimer.Schedule (m_helloInterval);
02279 }
02280
02281
02282
02283
02284
02285 void
02286 AgentImpl::TcTimerExpire ()
02287 {
02288 if (m_state.GetMprSelectors ().size () > 0)
02289 {
02290 SendTc ();
02291 }
02292 else
02293 {
02294 NS_LOG_DEBUG ("Not sending any TC, no one selected me as MPR.");
02295 }
02296 m_tcTimer.Schedule (m_tcInterval);
02297 }
02298
02299
02300
02301
02302
02303
02304 void
02305 AgentImpl::MidTimerExpire ()
02306 {
02307 SendMid ();
02308 m_midTimer.Schedule (m_midInterval);
02309 }
02310
02311
02312
02313
02314
02315
02316
02317
02318 void
02319 AgentImpl::DupTupleTimerExpire (Ipv4Address address, uint16_t sequenceNumber)
02320 {
02321 DuplicateTuple *tuple =
02322 m_state.FindDuplicateTuple (address, sequenceNumber);
02323 if (tuple == NULL)
02324 {
02325 return;
02326 }
02327 if (tuple->expirationTime < Simulator::Now ())
02328 {
02329 RemoveDuplicateTuple (*tuple);
02330 }
02331 else
02332 {
02333 m_events.Track (Simulator::Schedule (DELAY (tuple->expirationTime),
02334 &AgentImpl::DupTupleTimerExpire, this,
02335 address, sequenceNumber));
02336 }
02337 }
02338
02339
02340
02341
02342
02343
02344
02345
02346
02347
02348
02349
02350 void
02351 AgentImpl::LinkTupleTimerExpire (Ipv4Address neighborIfaceAddr)
02352 {
02353 Time now = Simulator::Now ();
02354
02355
02356 LinkTuple *tuple = m_state.FindLinkTuple (neighborIfaceAddr);
02357 if (tuple == NULL)
02358 {
02359 return;
02360 }
02361 if (tuple->time < now)
02362 {
02363 RemoveLinkTuple (*tuple);
02364 }
02365 else if (tuple->symTime < now)
02366 {
02367 if (m_linkTupleTimerFirstTime)
02368 m_linkTupleTimerFirstTime = false;
02369 else
02370 NeighborLoss (*tuple);
02371
02372 m_events.Track (Simulator::Schedule (DELAY (tuple->time),
02373 &AgentImpl::LinkTupleTimerExpire, this,
02374 neighborIfaceAddr));
02375 }
02376 else
02377 {
02378 m_events.Track (Simulator::Schedule (DELAY (std::min (tuple->time, tuple->symTime)),
02379 &AgentImpl::LinkTupleTimerExpire, this,
02380 neighborIfaceAddr));
02381 }
02382 }
02383
02384
02385
02386
02387
02388
02389
02390
02391 void
02392 AgentImpl::Nb2hopTupleTimerExpire (Ipv4Address neighborMainAddr, Ipv4Address twoHopNeighborAddr)
02393 {
02394 TwoHopNeighborTuple *tuple;
02395 tuple = m_state.FindTwoHopNeighborTuple (neighborMainAddr, twoHopNeighborAddr);
02396 if (tuple == NULL)
02397 {
02398 return;
02399 }
02400 if (tuple->expirationTime < Simulator::Now ())
02401 {
02402 RemoveTwoHopNeighborTuple (*tuple);
02403 }
02404 else
02405 {
02406 m_events.Track (Simulator::Schedule (DELAY (tuple->expirationTime),
02407 &AgentImpl::Nb2hopTupleTimerExpire,
02408 this, neighborMainAddr, twoHopNeighborAddr));
02409 }
02410 }
02411
02412
02413
02414
02415
02416
02417
02418
02419 void
02420 AgentImpl::MprSelTupleTimerExpire (Ipv4Address mainAddr)
02421 {
02422 MprSelectorTuple *tuple = m_state.FindMprSelectorTuple (mainAddr);
02423 if (tuple == NULL)
02424 {
02425 return;
02426 }
02427 if (tuple->expirationTime < Simulator::Now ())
02428 {
02429 RemoveMprSelectorTuple (*tuple);
02430 }
02431 else
02432 {
02433 m_events.Track (Simulator::Schedule (DELAY (tuple->expirationTime),
02434 &AgentImpl::MprSelTupleTimerExpire,
02435 this, mainAddr));
02436 }
02437 }
02438
02439
02440
02441
02442
02443
02444
02445
02446 void
02447 AgentImpl::TopologyTupleTimerExpire (Ipv4Address destAddr, Ipv4Address lastAddr)
02448 {
02449 TopologyTuple *tuple = m_state.FindTopologyTuple (destAddr, lastAddr);
02450 if (tuple == NULL)
02451 {
02452 return;
02453 }
02454 if (tuple->expirationTime < Simulator::Now ())
02455 {
02456 RemoveTopologyTuple (*tuple);
02457 }
02458 else
02459 {
02460 m_events.Track (Simulator::Schedule (DELAY (tuple->expirationTime),
02461 &AgentImpl::TopologyTupleTimerExpire,
02462 this, tuple->destAddr, tuple->lastAddr));
02463 }
02464 }
02465
02466
02467
02468
02469
02470
02471 void
02472 AgentImpl::IfaceAssocTupleTimerExpire (Ipv4Address ifaceAddr)
02473 {
02474 IfaceAssocTuple *tuple = m_state.FindIfaceAssocTuple (ifaceAddr);
02475 if (tuple == NULL)
02476 {
02477 return;
02478 }
02479 if (tuple->time < Simulator::Now ())
02480 {
02481 RemoveIfaceAssocTuple (*tuple);
02482 }
02483 else
02484 {
02485 m_events.Track (Simulator::Schedule (DELAY (tuple->time),
02486 &AgentImpl::IfaceAssocTupleTimerExpire,
02487 this, ifaceAddr));
02488 }
02489 }
02490
02491 Ptr<const olsr::RoutingTable>
02492 AgentImpl::GetRoutingTable () const
02493 {
02494 return m_routingTable;
02495 }
02496
02497 }}
02498
02499