1. Trang chủ
  2. » Công Nghệ Thông Tin

Ví dụ cài đặt giao thức định tuyến địa lý GPSR trong công cụ mô phỏng NS3

19 487 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 19
Dung lượng 29,74 KB
File đính kèm gpsr.zip (16 KB)

Nội dung

Example su dung NS3 (network simulator NS3) thiet ke va cai dat giao thuc dinh tuyen cho mang khong day su dung thong tin vi tri dia ly (Geographic routing).Ky nang thiet ke va cai dat giao thuc moi trong NS3. Huong dan su dung NS3 va danh gia hieu nang giao thuc dinh thuc dinh tuyen

#define NS_LOG_APPEND_CONTEXT \ if (m_ipv4) { std::clog GetId () PeekPacketTag (tag) && IsMyOwnAddress (origin)) { Ptr packet = p->Copy (); //FIXME ja estou a abusar de tirar tags packet->RemovePacketTag(tag); DeferredRouteOutput (packet, header, ucb, ecb); return true; } if (m_ipv4->IsDestinationAddress (dst, iif)) { Ptr packet = p->Copy (); TypeHeader tHeader (GPSRTYPE_POS); packet->RemoveHeader (tHeader); if (!tHeader.IsValid ()) { NS_LOG_DEBUG ("GPSR message " GetUid () GetNetDevice (1)); while (m_queue.Dequeue (dst, queueEntry)) { DeferredRouteOutputTag tag; Ptr p = ConstCast (queueEntry.GetPacket ()); UnicastForwardCallback ucb = queueEntry.GetUnicastForwardCallback (); Ipv4Header header = queueEntry.GetIpv4Header (); if (header.GetSource () == Ipv4Address ("102.102.102.102")) { route->SetSource (m_ipv4->GetAddress (1, 0).GetLocal ()); header.SetSource (m_ipv4->GetAddress (1, 0).GetLocal ()); } else { route->SetSource (header.GetSource ()); } ucb (route, p, header); } } return true; void RoutingProtocol::RecoveryMode(Ipv4Address dst, Ptr p, UnicastForwardCallback ucb, Ipv4Header header){ Vector Position; Vector previousHop; uint32_t updated; uint64_t positionX; uint64_t positionY; Vector myPos; Vector recPos; Ptr MM = m_ipv4->GetObject (); positionX = MM->GetPosition ().x; positionY = MM->GetPosition ().y; myPos.x = positionX; myPos.y = positionY; TypeHeader tHeader (GPSRTYPE_POS); p->RemoveHeader (tHeader); if (!tHeader.IsValid ()) { NS_LOG_DEBUG ("GPSR message " GetUid () AddHeader (tHeader); updated, recPos.x, Ipv4Address nextHop = m_neighbors.BestAngle (previousHop, myPos); if (nextHop == Ipv4Address::GetZero ()) { return; } Ptr route = Create (); route->SetDestination (dst); route->SetGateway (nextHop); // FIXME: Does not work for multiple interfaces route->SetOutputDevice (m_ipv4->GetNetDevice (1)); route->SetSource (header.GetSource ()); ucb (route, p, header); return; } void RoutingProtocol::NotifyInterfaceUp (uint32_t interface) { NS_LOG_FUNCTION (this GetAddress (interface, 0).GetLocal ()); Ptr l3 = m_ipv4->GetObject (); if (l3->GetNAddresses (interface) > 1) { NS_LOG_WARN ("GPSR does not work with more then one address per each interface."); } Ipv4InterfaceAddress iface = l3->GetAddress (interface, 0); if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) { return; } // Create a socket to listen only on this interface Ptr socket = Socket::CreateSocket (GetObject (), UdpSocketFactory::GetTypeId ()); NS_ASSERT (socket != 0); socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvGPSR, this)); socket->BindToNetDevice (l3->GetNetDevice (interface)); socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), GPSR_PORT)); socket->SetAllowBroadcast (true); socket->SetAttribute ("IpTtl", UintegerValue (1)); m_socketAddresses.insert (std::make_pair (socket, iface)); // Allow neighbor manager use this interface for layer feedback if possible Ptr dev = m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (iface.GetLocal ())); Ptr wifi = dev->GetObject (); if (wifi == 0) { return; } Ptr mac = wifi->GetMac (); if (mac == 0) { return; } mac->TraceConnectWithoutContext ("TxErrHeader", m_neighbors.GetTxErrorCallback ()); } void RoutingProtocol::RecvGPSR (Ptr socket) { NS_LOG_FUNCTION (this RecvFrom (sourceAddress); TypeHeader tHeader (GPSRTYPE_HELLO); packet->RemoveHeader (tHeader); if (!tHeader.IsValid ()) { NS_LOG_DEBUG ("GPSR message " GetUid () GetNetDevice (interface); Ptr wifi = dev->GetObject (); if (wifi != 0) { Ptr mac = wifi->GetMac ()->GetObject (); if (mac != 0) { mac->TraceDisconnectWithoutContext ("TxErrHeader", m_neighbors.GetTxErrorCallback ()); } } // Close socket Ptr socket = FindSocketWithInterfaceAddress (m_ipv4->GetAddress (interface, 0)); NS_ASSERT (socket); socket->Close (); m_socketAddresses.erase (socket); if (m_socketAddresses.empty ()) { NS_LOG_LOGIC ("No gpsr interfaces"); m_neighbors.Clear (); m_locationService->Clear (); return; } } Ptr RoutingProtocol::FindSocketWithInterfaceAddress (Ipv4InterfaceAddress addr ) const { NS_LOG_FUNCTION (this first; Ipv4InterfaceAddress iface = j->second; if (iface == addr) { return socket; } } Ptr socket; return socket; } void RoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) { NS_LOG_FUNCTION (this GetAddress (interface, 0); Ptr socket = FindSocketWithInterfaceAddress (iface); if (!socket) { if (iface.GetLocal () == Ipv4Address ("127.0.0.1")) { return; } // Create a socket to listen only on this interface Ptr socket = Socket::CreateSocket (GetObject (), UdpSocketFactory::GetTypeId ()); NS_ASSERT (socket != 0); socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvGPSR,this)); socket->BindToNetDevice (l3->GetNetDevice (interface)); // Bind to any IP address so that broadcasts can be received socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), GPSR_PORT)); socket->SetAllowBroadcast (true); m_socketAddresses.insert (std::make_pair (socket, iface)); Ptr dev = m_ipv4->GetNetDevice (m_ipv4>GetInterfaceForAddress (iface.GetLocal ())); } } else { NS_LOG_LOGIC ("GPSR does not work with more then one address per each interface Ignore added address"); } } void RoutingProtocol::NotifyRemoveAddress (uint32_t i, Ipv4InterfaceAddress address) { NS_LOG_FUNCTION (this); Ptr socket = FindSocketWithInterfaceAddress (address); if (socket) { ()); m_socketAddresses.erase (socket); Ptr l3 = m_ipv4->GetObject (); if (l3->GetNAddresses (i)) { Ipv4InterfaceAddress iface = l3->GetAddress (i, 0); // Create a socket to listen only on this interface Ptr socket = Socket::CreateSocket (GetObject (), UdpSocketFactory::GetTypeId NS_ASSERT (socket != 0); socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvGPSR, this)); // Bind to any IP address so that broadcasts can be received socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), GPSR_PORT)); socket->SetAllowBroadcast (true); m_socketAddresses.insert (std::make_pair (socket, iface)); // Add local broadcast record to the routing table Ptr dev = m_ipv4->GetNetDevice (m_ipv4>GetInterfaceForAddress (iface.GetLocal ())); } if (m_socketAddresses.empty ()) { NS_LOG_LOGIC ("No gpsr interfaces"); m_neighbors.Clear (); m_locationService->Clear (); return; } } } else { NS_LOG_LOGIC ("Remove address not participating in GPSR operation"); } void RoutingProtocol::SetIpv4 (Ptr ipv4) { NS_ASSERT (ipv4 != 0); NS_ASSERT (m_ipv4 == 0); m_ipv4 = ipv4; HelloIntervalTimer.SetFunction (&RoutingProtocol::HelloTimerExpire, this); HelloIntervalTimer.Schedule (FIRST_JITTER); //Schedule only when it has packets on queue CheckQueueTimer.SetFunction (&RoutingProtocol::CheckQueue, this); Simulator::ScheduleNow (&RoutingProtocol::Start, this); } void RoutingProtocol::HelloTimerExpire () { SendHello (); HelloIntervalTimer.Cancel (); HelloIntervalTimer.Schedule (HelloInterval + JITTER); } void RoutingProtocol::SendHello () { NS_LOG_FUNCTION (this); double positionX; double positionY; Ptr MM = m_ipv4->GetObject (); positionX = MM->GetPosition ().x; positionY = MM->GetPosition ().y; for (std::map::const_iterator j = m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) { Ptr socket = j->first; Ipv4InterfaceAddress iface = j->second; HelloHeader helloHeader (((uint64_t) positionX),((uint64_t) positionY)); Ptr packet = Create (); packet->AddHeader (helloHeader); TypeHeader tHeader (GPSRTYPE_HELLO); packet->AddHeader (tHeader); // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise Ipv4Address destination; if (iface.GetMask () == Ipv4Mask::GetOnes ()) { destination = Ipv4Address ("255.255.255.255"); } else { destination = iface.GetBroadcast (); } socket->SendTo (packet, 0, InetSocketAddress (destination, GPSR_PORT)); } } bool RoutingProtocol::IsMyOwnAddress (Ipv4Address src) { NS_LOG_FUNCTION (this second; if (src == iface.GetLocal ()) { return true; } } return false; } void RoutingProtocol::Start () { NS_LOG_FUNCTION (this); m_queuedAddresses.clear (); //FIXME ajustar timer, meter valor parametrizavel Time tableTime ("2s"); switch (LocationServiceName) { case GPSR_LS_GOD: NS_LOG_DEBUG ("GodLS in use"); m_locationService = CreateObject (); break; case GPSR_LS_RLS: NS_LOG_UNCOND ("RLS not yet implemented"); break; } } Ptr RoutingProtocol::LoopbackRoute (const Ipv4Header & hdr, Ptr oif) { NS_LOG_FUNCTION (this GetNetDevice (0); NS_ASSERT (m_lo != 0); Ptr rt = Create (); rt->SetDestination (hdr.GetDestination ()); std::map::const_iterator j = m_socketAddresses.begin (); if (oif) { // Iterate to find an address on the oif device for (j = m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j) { Ipv4Address addr = j->second.GetLocal (); int32_t interface = m_ipv4->GetInterfaceForAddress (addr); if (oif == m_ipv4->GetNetDevice (static_cast (interface))) { rt->SetSource (addr); break; } } } else { rt->SetSource (j->second.GetLocal ()); } NS_ASSERT_MSG (rt->GetSource () != Ipv4Address (), "Valid GPSR source address not found"); rt->SetGateway (Ipv4Address ("127.0.0.1")); rt->SetOutputDevice (m_lo); return rt; } int RoutingProtocol::GetProtocolNumber (void) const { return GPSR_PORT; } void RoutingProtocol::AddHeaders (Ptr p, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr route) { NS_LOG_FUNCTION (this GetPosition (destination), myPos); } uint16_t positionX = 0; uint16_t positionY = 0; uint32_t hdrTime = 0; if(destination != m_ipv4->GetAddress (1, 0).GetBroadcast ()) { positionX = m_locationService->GetPosition (destination).x; positionY = m_locationService->GetPosition (destination).y; hdrTime = (uint32_t) m_locationService->GetEntryUpdateTime (destination).GetSeconds (); } PositionHeader posHeader (positionX, positionY, (uint64_t) 0, (uint8_t) 0, myPos.x, myPos.y); p->AddHeader (posHeader); TypeHeader tHeader (GPSRTYPE_POS); p->AddHeader (tHeader); hdrTime, (uint64_t) 0, m_downTarget (p, source, destination, protocol, route); } bool RoutingProtocol::Forwarding (Ptr packet, const Ipv4Header & header, UnicastForwardCallback ucb, ErrorCallback ecb) { Ptr p = packet->Copy (); NS_LOG_FUNCTION (this); Ipv4Address dst = header.GetDestination (); Ipv4Address origin = header.GetSource (); m_neighbors.Purge (); uint32_t updated = 0; Vector Position; Vector RecPosition; uint8_t inRec = 0; TypeHeader tHeader (GPSRTYPE_POS); PositionHeader hdr; p->RemoveHeader (tHeader); if (!tHeader.IsValid ()) { NS_LOG_DEBUG ("GPSR message " GetUid () GetPosition ().x; myPos.y = MM->GetPosition ().y; if(inRec == && CalculateDistance (myPos, Position) < CalculateDistance (RecPosition, Position)){ inRec = 0; hdr.SetInRec(0); NS_LOG_LOGIC ("No longer in Recovery to " GetPosition (dst).x; Position.y = m_locationService->GetPosition (dst).y; updated = myUpdated; } Ipv4Address nextHop; if(m_neighbors.isNeighbour (dst)) { nextHop = dst; } else { nextHop = m_neighbors.BestNeighbor (Position, myPos); if (nextHop != Ipv4Address::GetZero ()) { PositionHeader posHeader (Position.x, Position.y, 0, (uint64_t) 0, (uint8_t) 0, myPos.x, myPos.y); p->AddHeader (posHeader); p->AddHeader (tHeader); updated, (uint64_t) Ptr oif = m_ipv4->GetObject (); Ptr route = Create (); route->SetDestination (dst); route->SetSource (header.GetSource ()); route->SetGateway (nextHop); // FIXME: Does not work for multiple interfaces route->SetOutputDevice (m_ipv4->GetNetDevice (1)); route->SetDestination (header.GetDestination ()); NS_ASSERT (route != 0); NS_LOG_DEBUG ("Exist route to " GetDestination () GetOutputDevice () GetPosition ().y; Ipv4Address nextHop; if(m_neighbors.isNeighbour (dst)) { nextHop = dst; } else { nextHop = m_neighbors.BestNeighbor (dstPos, myPos); } if (nextHop != Ipv4Address::GetZero ()) { NS_LOG_DEBUG ("Destination: " SetDestination (dst); if (header.GetSource () == Ipv4Address ("102.102.102.102")) { route->SetSource (m_ipv4->GetAddress (1, 0).GetLocal ()); } else { route->SetSource (header.GetSource ()); } route->SetGateway (nextHop); route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4>GetInterfaceForAddress (route->GetSource ()))); route->SetDestination (header.GetDestination ()); NS_ASSERT (route != 0); NS_LOG_DEBUG ("Exist route to " GetDestination () GetOutputDevice () != oif) { NS_LOG_DEBUG ("Output device doesn't match Dropped."); sockerr = Socket::ERROR_NOROUTETOHOST; return Ptr (); } return route; } else { DeferredRouteOutputTag tag; if (!p->PeekPacketTag (tag)) { p->AddPacketTag (tag); } return LoopbackRoute (header, oif); //in RouteInput the recovery-mode is called } } } } ... MakeBooleanAccessor (&RoutingProtocol::PerimeterMode), MakeBooleanChecker ()) ; return tid; } RoutingProtocol::~RoutingProtocol () { } void RoutingProtocol::DoDispose () { m_ipv4 = 0; Ipv4RoutingProtocol::DoDispose... PositionTable (); } TypeId RoutingProtocol::GetTypeId (void) { static TypeId tid = TypeId ("ns3::gpsr::RoutingProtocol") SetParent () AddConstructor () AddAttribute... NS_OBJECT_ENSURE_REGISTERED (RoutingProtocol); /// UDP Port for GPSR control traffic, not defined by IANA yet const uint32_t RoutingProtocol::GPSR_PORT = 666; RoutingProtocol::RoutingProtocol () : HelloInterval

Ngày đăng: 23/04/2017, 13:23

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w