To implement Temporally Ordered Routing Algorithm (TORA) in ns3, we need to follow some steps. TORA is mainly designed for highly dynamic mobile ad hoc networks (MANETs). By creating and maintaining routes in a distributed manner we can operate TORA. So, a custom routing protocol has to be created to follows the principles of TORA.
The steps given below is a detailed instruction of how to implement TORA routing in ns-3:
Step-by-Step guide to implement Temporally ordered routing in ns3:
- Set Up ns-3 Environment: Make sure ns3 is installed the system.
- Include Necessary Libraries: Include the required n3 libraries in the script.
- Define Network Topology: Create the nodes and links for the network topology.
- Install Internet Stack: Install the Internet stack on your nodes.
- Implement the TORA Routing Protocol: Create a custom routing protocol class that follows the TORA algorithm.
- Integrate the Custom Routing Protocol: Integrate the custom routing protocol into the ns3 stack.
- Set Up Applications: Install applications to generate traffic and test the routing.
- Run the Simulation: Configure the simulation parameters and run it.
Example Implementation in C++
Here’s a detailed example implement TORA routing in ns3:
- Include Libraries:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/mobility-module.h”
#include “ns3/ipv4-list-routing-helper.h”
- Define Network Topology:
using namespace ns3;
int main(int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse(argc, argv);
NodeContainer nodes;
nodes.Create(4);
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(0.0),
“MinY”, DoubleValue(0.0),
“DeltaX”, DoubleValue(10.0),
“DeltaY”, DoubleValue(10.0),
“GridWidth”, UintegerValue(2),
“LayoutType”, StringValue(“RowFirst”));
mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);
mobility.Install(nodes);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices;
devices.Add(pointToPoint.Install(nodes.Get(0), nodes.Get(1)));
devices.Add(pointToPoint.Install(nodes.Get(1), nodes.Get(2)));
devices.Add(pointToPoint.Install(nodes.Get(2), nodes.Get(3)));
devices.Add(pointToPoint.Install(nodes.Get(3), nodes.Get(0)));
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces01 = address.Assign(devices.Get(0));
address.SetBase(“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces12 = address.Assign(devices.Get(1));
address.SetBase(“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces23 = address.Assign(devices.Get(2));
address.SetBase(“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces30 = address.Assign(devices.Get(3));
- Implement the TORA Routing Protocol:
- Create a new header file tora-routing.h:
#ifndef TORA_ROUTING_H
#define TORA_ROUTING_H
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/ipv4.h”
#include “ns3/net-device.h”
#include “ns3/ptr.h”
#include “ns3/socket.h”
#include <map>
namespace ns3 {
class ToraRouting : public Ipv4RoutingProtocol {
public:
static TypeId GetTypeId(void);
ToraRouting();
virtual ~ToraRouting();
virtual Ptr<Ipv4Route> RouteOutput(
Ptr<Packet> p, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
virtual bool RouteInput(
Ptr<const Packet> p, const Ipv4Header &header,
Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb);
virtual void NotifyInterfaceUp(uint32_t interface);
virtual void NotifyInterfaceDown(uint32_t interface);
virtual void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address);
virtual void NotifyRemoveAddress(uint32_t interface,Ipv4InterfaceAddress address);
virtual void SetIpv4(Ptr<Ipv4> ipv4);
void Start();
private:
void HandleTopologyChange();
void BroadcastQuery(Ptr<Packet> packet, Ipv4Address source);
void BroadcastUpdate(Ptr<Packet> packet, Ipv4Address source);
void ReceivePacket(Ptr<Socket> socket);
Ptr<Ipv4> m_ipv4;
std::map<Ipv4Address, uint32_t> m_height;
std::map<Ipv4Address, Ipv4Address> m_downstream;
std::map<uint32_t, Ptr<Socket>> m_socketMap;
Time m_updateInterval;
EventId m_updateEvent;
};
}
#endif // TORA_ROUTING_H
Create the corresponding implementation file tora-routing.cc:
#include “tora-routing.h”
#include “ns3/log.h”
#include “ns3/ipv4-routing-table-entry.h”
#include “ns3/simulator.h”
#include “ns3/inet-socket-address.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE(“ToraRouting”);
NS_OBJECT_ENSURE_REGISTERED(ToraRouting);
TypeId ToraRouting::GetTypeId(void) {
static TypeId tid = TypeId(“ns3::ToraRouting”)
.SetParent<Ipv4RoutingProtocol>()
.SetGroupName(“Internet”)
.AddConstructor<ToraRouting>();
return tid;
}
ToraRouting::ToraRouting() : m_updateInterval(Seconds(5)) {
NS_LOG_FUNCTION(this);
Simulator::Schedule(Seconds(1.0), &ToraRouting::Start, this);
}
ToraRouting::~ToraRouting() {
NS_LOG_FUNCTION(this);
}
void ToraRouting::SetIpv4(Ptr<Ipv4> ipv4) {
NS_LOG_FUNCTION(this << ipv4);
m_ipv4 = ipv4;
}
void ToraRouting::Start() {
NS_LOG_FUNCTION(this);
HandleTopologyChange();
m_updateEvent = Simulator::Schedule(m_updateInterval, &ToraRouting::Start, this);
}
void ToraRouting::HandleTopologyChange() {
NS_LOG_FUNCTION(this);
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces(); ++i) {
Ipv4Address addr = m_ipv4->GetAddress(i, 0).GetLocal();
m_height[addr] = (addr == m_ipv4->GetAddress(1, 0).GetLocal()) ? 0 : UINT32_MAX;
m_downstream[addr] = Ipv4Address::GetAny();
}
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces(); ++i) {
Ipv4Address addr = m_ipv4->GetAddress(i, 0).GetLocal();
for (uint32_t j = 0; j < m_ipv4->GetNInterfaces(); ++j) {
Ipv4Address neighbor = m_ipv4->GetAddress(j, 0).GetLocal();
if (m_height[addr] > m_height[neighbor] + 1) {
m_height[addr] = m_height[neighbor] + 1;
m_downstream[addr] = neighbor;
}
}
}
Ptr<Packet> packet = Create<Packet>();
for (auto it : m_height) {
Ipv4Address addr = it.first;
uint32_t height = it.second;
packet->AddHeader(Ipv4Header(addr, Ipv4Address::GetBroadcast(), height, 0));
}
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces(); ++i) {
Ptr<Socket> socket = m_socketMap[i];
socket->SendTo(packet, 0, InetSocketAddress(Ipv4Address::GetBroadcast(), 80));
}
}
void ToraRouting::BroadcastQuery(Ptr<Packet> packet, Ipv4Address source) {
NS_LOG_FUNCTION(this << packet << source);
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces(); ++i) {
Ptr<Socket> socket = m_socketMap[i];
socket->SendTo(packet, 0, InetSocketAddress(Ipv4Address::GetBroadcast(), 80));
}
}
void ToraRouting::BroadcastUpdate(Ptr<Packet> packet, Ipv4Address source) {
NS_LOG_FUNCTION(this << packet << source);
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces(); ++i) {
Ptr<Socket> socket = m_socketMap[i];
socket->SendTo(packet, 0, InetSocketAddress(Ipv4Address::GetBroadcast(), 80));
}
}
void ToraRouting::ReceivePacket(Ptr<Socket> socket) {
NS_LOG_FUNCTION(this);
Ptr<Packet> packet = socket->Recv();
Ipv4Header header;
packet->RemoveHeader(header);
Ipv4Address addr = header.GetDestination();
uint32_t height = header.GetIdentification();
if (m_height[addr] > height) {
m_height[addr] = height;
m_downstream[addr] = header.GetSource();
BroadcastUpdate(packet, addr);
}
}
Ptr<Ipv4Route> ToraRouting::RouteOutput(
Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif,
Socket::SocketErrno &sockerr) {
NS_LOG_FUNCTION(this << p << header << oif << sockerr);
Ipv4Address dest = header.GetDestination();
if (m_downstream.find(dest) == m_downstream.end()) {
sockerr = Socket::ERROR_NOROUTETOHOST;
return 0;
}
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(dest);
route->SetGateway(m_downstream[dest]);
route->SetOutputDevice(oif);
return route;
}
bool ToraRouting::RouteInput(
Ptr<const Packet> p, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb) {
NS_LOG_FUNCTION(this << p << header << idev << ucb << mcb << lcb << ecb);
Ipv4Address dest = header.GetDestination();
if (dest == m_ipv4->GetAddress(1, 0).GetLocal()) {
lcb(p, header, idev);
return true;
}
if (m_downstream.find(dest) == m_downstream.end()) {
ecb(p, header, Socket::ERROR_NOROUTETOHOST);
return false;
}
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(dest);
route->SetGateway(m_downstream[dest]);
route->SetOutputDevice(idev);
ucb(route, p, header);
return true;
}
void ToraRouting::NotifyInterfaceUp(uint32_t interface) {
NS_LOG_FUNCTION(this << interface);
Ptr<Socket> socket = Socket::CreateSocket(GetObject<Node>(), TypeId::LookupByName(“ns3::UdpSocketFactory”));
socket->SetAllowBroadcast(true);
socket->BindToNetDevice(m_ipv4->GetNetDevice(interface));
socket->Bind(InetSocketAddress(Ipv4Address::GetAny(), 80));
socket->SetRecvCallback(MakeCallback(&ToraRouting::ReceivePacket, this));
m_socketMap[interface] = socket;
}
void ToraRouting::NotifyInterfaceDown(uint32_t interface) {
NS_LOG_FUNCTION(this << interface);
m_socketMap[interface]->Close();
m_socketMap.erase(interface);
}
void ToraRouting::NotifyAddAddress(uint32_t interface,
Ipv4InterfaceAddress address) {
NS_LOG_FUNCTION(this << interface << address);
}
void ToraRouting::NotifyRemoveAddress(uint32_t interface,
Ipv4InterfaceAddress address) {
NS_LOG_FUNCTION(this << interface << address);
}
}
- Integrate the Custom Routing Protocol:
#include “tora-routing.h”
int main(int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse(argc, argv);
NodeContainer nodes;
nodes.Create(4);
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(0.0),
“MinY”, DoubleValue(0.0),
“DeltaX”, DoubleValue(10.0),
“DeltaY”, DoubleValue(10.0),
“GridWidth”, UintegerValue(2),
“LayoutType”, StringValue(“RowFirst”));
mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);
mobility.Install(nodes);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices;
devices.Add(pointToPoint.Install(nodes.Get(0), nodes.Get(1)));
devices.Add(pointToPoint.Install(nodes.Get(1), nodes.Get(2)));
devices.Add(pointToPoint.Install(nodes.Get(2), nodes.Get(3)));
devices.Add(pointToPoint.Install(nodes.Get(3), nodes.Get(0)));
InternetStackHelper stack;
ToraRoutingHelper toraRouting;
Ipv4ListRoutingHelper list;
list.Add(toraRouting, 0);
stack.SetRoutingHelper(list);
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces01 = address.Assign(devices.Get(0));
address.SetBase(“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces12 = address.Assign(devices.Get(1));
address.SetBase(“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces23 = address.Assign(devices.Get(2));
address.SetBase(“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces30 = address.Assign(devices.Get(3));
// Set up applications
uint16_t port = 9;
UdpEchoServerHelper server(port);
ApplicationContainer apps = server.Install(nodes.Get(3));
apps.Start(Seconds(1.0));
apps.Stop(Seconds(10.0));
UdpEchoClientHelper client(address.GetAddress(3), port);
client.SetAttribute(“MaxPackets”, UintegerValue(1));
client.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));
client.SetAttribute(“PacketSize”, UintegerValue(1024));
apps = client.Install(nodes.Get(0));
apps.Start(Seconds(2.0));
apps.Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Explanation
- Network Topology: The code sets up a network topology with four nodes connected in a ring.
- TORA Routing Protocol: A custom TORA routing protocol class (ToraRouting) is implemented. This class will handle the route maintenance and reacts to topology changes.
- Route Maintenance: The HandleTopologyChange method manages the route maintenance based on TORA principles, updating the height and downstream neighbors.
- Broadcasting Queries and Updates: The BroadcastQuery and BroadcastUpdate methods handle the broadcasting of query and update messages, respectively.
- Receive Packet: The ReceivePacket method processes incoming packets and updates the routing information accordingly.
- Route Input and Output: The RouteInput and RouteOutput methods handle packet routing based on the computed height values and downstream neighbors.
- Integrate Custom Routing Protocol: The custom routing protocol is integrated into the ns3 stack using the ToraRoutingHelper.
- Applications: UdpEchoServer and UdpEchoClient applications are set up to test the routing.
- Running the Code
- Save your script to a file, for example, tora-routing.cc.
- Compile the script using the ns3 build system
./waf configure –enable-examples
./waf build
./waf –run scratch/tora-routing
We had clearly explained that Temporally Ordered Routing Algorithm can be implemented in ns3 by creating accustom routing protocol which follows TORA principles that helps in handling the route maintenance, broadcasting query and updating messages in the network.
Dynamic mobile ad hoc networks (MANETs) projects are worked by us for more details contact us.