A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
socket-bound-static-routing.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6/* Test program for multi-interface host, static routing
7
8 Destination host (10.20.1.2)
9 |
10 | 10.20.1.0/24
11 DSTRTR
12 10.10.1.0/24 / \ 10.10.2.0/24
13 / \
14 Rtr1 Rtr2
15 10.1.1.0/24 | | 10.1.2.0/24
16 | /
17 \ /
18 Source
19*/
20
21#include "ns3/core-module.h"
22#include "ns3/internet-module.h"
23#include "ns3/ipv4-list-routing-helper.h"
24#include "ns3/ipv4-static-routing-helper.h"
25#include "ns3/network-module.h"
26#include "ns3/point-to-point-module.h"
27
28#include <cassert>
29#include <fstream>
30#include <iostream>
31#include <string>
32
33using namespace ns3;
34
35NS_LOG_COMPONENT_DEFINE("SocketBoundRoutingExample");
36
37void SendStuff(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
38void BindSock(Ptr<Socket> sock, Ptr<NetDevice> netdev);
39void srcSocketRecv(Ptr<Socket> socket);
40void dstSocketRecv(Ptr<Socket> socket);
41
42int
43main(int argc, char* argv[])
44{
45 // Allow the user to override any of the defaults and the above
46 // DefaultValue::Bind ()s at run-time, via command-line arguments
47 CommandLine cmd(__FILE__);
48 cmd.Parse(argc, argv);
49
54 Ptr<Node> nDstRtr = CreateObject<Node>();
55
56 NodeContainer c = NodeContainer(nSrc, nDst, nRtr1, nRtr2, nDstRtr);
57
59 internet.Install(c);
60
61 // Point-to-point links
62 NodeContainer nSrcnRtr1 = NodeContainer(nSrc, nRtr1);
63 NodeContainer nSrcnRtr2 = NodeContainer(nSrc, nRtr2);
64 NodeContainer nRtr1nDstRtr = NodeContainer(nRtr1, nDstRtr);
65 NodeContainer nRtr2nDstRtr = NodeContainer(nRtr2, nDstRtr);
66 NodeContainer nDstRtrnDst = NodeContainer(nDstRtr, nDst);
67
68 // We create the channels first without any IP addressing information
70 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
71 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
72 NetDeviceContainer dSrcdRtr1 = p2p.Install(nSrcnRtr1);
73 NetDeviceContainer dSrcdRtr2 = p2p.Install(nSrcnRtr2);
74 NetDeviceContainer dRtr1dDstRtr = p2p.Install(nRtr1nDstRtr);
75 NetDeviceContainer dRtr2dDstRtr = p2p.Install(nRtr2nDstRtr);
76 NetDeviceContainer dDstRtrdDst = p2p.Install(nDstRtrnDst);
77
78 Ptr<NetDevice> SrcToRtr1 = dSrcdRtr1.Get(0);
79 Ptr<NetDevice> SrcToRtr2 = dSrcdRtr2.Get(0);
80
81 // Later, we add IP addresses.
83 ipv4.SetBase("10.1.1.0", "255.255.255.0");
84 Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign(dSrcdRtr1);
85 ipv4.SetBase("10.1.2.0", "255.255.255.0");
86 Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign(dSrcdRtr2);
87 ipv4.SetBase("10.10.1.0", "255.255.255.0");
88 Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign(dRtr1dDstRtr);
89 ipv4.SetBase("10.10.2.0", "255.255.255.0");
90 Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign(dRtr2dDstRtr);
91 ipv4.SetBase("10.20.1.0", "255.255.255.0");
92 Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign(dDstRtrdDst);
93
94 Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4>();
95 Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4>();
96 Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4>();
97 Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4>();
98 Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4>();
99
100 Ipv4StaticRoutingHelper ipv4RoutingHelper;
101 Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting(ipv4Src);
102 Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting(ipv4Rtr1);
103 Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting(ipv4Rtr2);
104 Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting(ipv4DstRtr);
105 Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting(ipv4Dst);
106
107 // Create static routes from Src to Dst
108 staticRoutingRtr1->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.10.1.2"), 2);
109 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.10.2.2"), 2);
110
111 // Two routes to same destination - setting separate metrics.
112 // You can switch these to see how traffic gets diverted via different routes
113 staticRoutingSrc->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.1.1.2"), 1, 5);
114 staticRoutingSrc->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.1.2.2"), 2, 10);
115
116 // Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
117 staticRoutingDst->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.20.1.1"), 1);
118 staticRoutingDstRtr->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.10.2.1"), 2);
119 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.1.2.1"), 1);
120
121 // There are no apps that can utilize the Socket Option so doing the work directly..
122 // Taken from tcp-large-transfer example
123
124 Ptr<Socket> srcSocket =
125 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::UdpSocketFactory"));
126 srcSocket->Bind();
127 srcSocket->SetRecvCallback(MakeCallback(&srcSocketRecv));
128
129 Ptr<Socket> dstSocket =
130 Socket::CreateSocket(nDst, TypeId::LookupByName("ns3::UdpSocketFactory"));
131 uint16_t dstport = 12345;
132 Ipv4Address dstaddr("10.20.1.2");
133 InetSocketAddress dst = InetSocketAddress(dstaddr, dstport);
134 dstSocket->Bind(dst);
135 dstSocket->SetRecvCallback(MakeCallback(&dstSocketRecv));
136
137 AsciiTraceHelper ascii;
138 p2p.EnableAsciiAll(ascii.CreateFileStream("socket-bound-static-routing.tr"));
139 p2p.EnablePcapAll("socket-bound-static-routing");
140
142 LogComponentEnable("SocketBoundRoutingExample", LOG_LEVEL_INFO);
143
144 // First packet as normal (goes via Rtr1)
145 Simulator::Schedule(Seconds(0.1), &SendStuff, srcSocket, dstaddr, dstport);
146 // Second via Rtr1 explicitly
147 Simulator::Schedule(Seconds(1.0), &BindSock, srcSocket, SrcToRtr1);
148 Simulator::Schedule(Seconds(1.1), &SendStuff, srcSocket, dstaddr, dstport);
149 // Third via Rtr2 explicitly
150 Simulator::Schedule(Seconds(2.0), &BindSock, srcSocket, SrcToRtr2);
151 Simulator::Schedule(Seconds(2.1), &SendStuff, srcSocket, dstaddr, dstport);
152 // Fourth again as normal (goes via Rtr1)
153 Simulator::Schedule(Seconds(3.0), &BindSock, srcSocket, Ptr<NetDevice>(nullptr));
154 Simulator::Schedule(Seconds(3.1), &SendStuff, srcSocket, dstaddr, dstport);
155 // If you uncomment what's below, it results in ASSERT failing since you can't
156 // bind to a socket not existing on a node
157 // Simulator::Schedule(Seconds(4.0),&BindSock, srcSocket, dDstRtrdDst.Get(0));
160
161 return 0;
162}
163
164void
165SendStuff(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
166{
168 p->AddPaddingAtEnd(100);
169 sock->SendTo(p, 0, InetSocketAddress(dstaddr, port));
170}
171
172void
174{
175 sock->BindToNetDevice(netdev);
176}
177
178void
180{
181 Address from;
182 Ptr<Packet> packet = socket->RecvFrom(from);
183 packet->RemoveAllPacketTags();
184 packet->RemoveAllByteTags();
185 NS_LOG_INFO("Source Received " << packet->GetSize() << " bytes from "
187 if (socket->GetBoundNetDevice())
188 {
189 NS_LOG_INFO("Socket was bound");
190 }
191 else
192 {
193 NS_LOG_INFO("Socket was not bound");
194 }
195}
196
197void
199{
200 Address from;
201 Ptr<Packet> packet = socket->RecvFrom(from);
202 packet->RemoveAllPacketTags();
203 packet->RemoveAllByteTags();
205 NS_LOG_INFO("Destination Received " << packet->GetSize() << " bytes from "
206 << address.GetIpv4());
207 NS_LOG_INFO("Triggering packet back to source node's interface 1");
208 SendStuff(socket, Ipv4Address("10.1.1.1"), address.GetPort());
209}
a polymophic address class
Definition address.h:90
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
an Inet address class
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class that adds ns3::Ipv4StaticRouting objects.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Hold variables of type string.
Definition string.h:45
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
uint16_t port
Definition dsdv-manet.cc:33
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
void srcSocketRecv(Ptr< Socket > socket)
void dstSocketRecv(Ptr< Socket > socket)
void SendStuff(Ptr< Socket > sock, Ipv4Address dstaddr, uint16_t port)
void BindSock(Ptr< Socket > sock, Ptr< NetDevice > netdev)