A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
socket-bound-static-routing.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 */
16
17/* Test program for multi-interface host, static routing
18
19 Destination host (10.20.1.2)
20 |
21 | 10.20.1.0/24
22 DSTRTR
23 10.10.1.0/24 / \ 10.10.2.0/24
24 / \
25 Rtr1 Rtr2
26 10.1.1.0/24 | | 10.1.2.0/24
27 | /
28 \ /
29 Source
30*/
31
32#include "ns3/core-module.h"
33#include "ns3/internet-module.h"
34#include "ns3/ipv4-list-routing-helper.h"
35#include "ns3/ipv4-static-routing-helper.h"
36#include "ns3/network-module.h"
37#include "ns3/point-to-point-module.h"
38
39#include <cassert>
40#include <fstream>
41#include <iostream>
42#include <string>
43
44using namespace ns3;
45
46NS_LOG_COMPONENT_DEFINE("SocketBoundRoutingExample");
47
48void SendStuff(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
49void BindSock(Ptr<Socket> sock, Ptr<NetDevice> netdev);
50void srcSocketRecv(Ptr<Socket> socket);
51void dstSocketRecv(Ptr<Socket> socket);
52
53int
54main(int argc, char* argv[])
55{
56 // Allow the user to override any of the defaults and the above
57 // DefaultValue::Bind ()s at run-time, via command-line arguments
58 CommandLine cmd(__FILE__);
59 cmd.Parse(argc, argv);
60
61 Ptr<Node> nSrc = CreateObject<Node>();
62 Ptr<Node> nDst = CreateObject<Node>();
63 Ptr<Node> nRtr1 = CreateObject<Node>();
64 Ptr<Node> nRtr2 = CreateObject<Node>();
65 Ptr<Node> nDstRtr = CreateObject<Node>();
66
67 NodeContainer c = NodeContainer(nSrc, nDst, nRtr1, nRtr2, nDstRtr);
68
70 internet.Install(c);
71
72 // Point-to-point links
73 NodeContainer nSrcnRtr1 = NodeContainer(nSrc, nRtr1);
74 NodeContainer nSrcnRtr2 = NodeContainer(nSrc, nRtr2);
75 NodeContainer nRtr1nDstRtr = NodeContainer(nRtr1, nDstRtr);
76 NodeContainer nRtr2nDstRtr = NodeContainer(nRtr2, nDstRtr);
77 NodeContainer nDstRtrnDst = NodeContainer(nDstRtr, nDst);
78
79 // We create the channels first without any IP addressing information
81 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
82 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
83 NetDeviceContainer dSrcdRtr1 = p2p.Install(nSrcnRtr1);
84 NetDeviceContainer dSrcdRtr2 = p2p.Install(nSrcnRtr2);
85 NetDeviceContainer dRtr1dDstRtr = p2p.Install(nRtr1nDstRtr);
86 NetDeviceContainer dRtr2dDstRtr = p2p.Install(nRtr2nDstRtr);
87 NetDeviceContainer dDstRtrdDst = p2p.Install(nDstRtrnDst);
88
89 Ptr<NetDevice> SrcToRtr1 = dSrcdRtr1.Get(0);
90 Ptr<NetDevice> SrcToRtr2 = dSrcdRtr2.Get(0);
91
92 // Later, we add IP addresses.
94 ipv4.SetBase("10.1.1.0", "255.255.255.0");
95 Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign(dSrcdRtr1);
96 ipv4.SetBase("10.1.2.0", "255.255.255.0");
97 Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign(dSrcdRtr2);
98 ipv4.SetBase("10.10.1.0", "255.255.255.0");
99 Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign(dRtr1dDstRtr);
100 ipv4.SetBase("10.10.2.0", "255.255.255.0");
101 Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign(dRtr2dDstRtr);
102 ipv4.SetBase("10.20.1.0", "255.255.255.0");
103 Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign(dDstRtrdDst);
104
105 Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4>();
106 Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4>();
107 Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4>();
108 Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4>();
109 Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4>();
110
111 Ipv4StaticRoutingHelper ipv4RoutingHelper;
112 Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting(ipv4Src);
113 Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting(ipv4Rtr1);
114 Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting(ipv4Rtr2);
115 Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting(ipv4DstRtr);
116 Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting(ipv4Dst);
117
118 // Create static routes from Src to Dst
119 staticRoutingRtr1->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.10.1.2"), 2);
120 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.10.2.2"), 2);
121
122 // Two routes to same destination - setting separate metrics.
123 // You can switch these to see how traffic gets diverted via different routes
124 staticRoutingSrc->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.1.1.2"), 1, 5);
125 staticRoutingSrc->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.1.2.2"), 2, 10);
126
127 // Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
128 staticRoutingDst->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.20.1.1"), 1);
129 staticRoutingDstRtr->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.10.2.1"), 2);
130 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.1.2.1"), 1);
131
132 // There are no apps that can utilize the Socket Option so doing the work directly..
133 // Taken from tcp-large-transfer example
134
135 Ptr<Socket> srcSocket =
136 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::UdpSocketFactory"));
137 srcSocket->Bind();
138 srcSocket->SetRecvCallback(MakeCallback(&srcSocketRecv));
139
140 Ptr<Socket> dstSocket =
141 Socket::CreateSocket(nDst, TypeId::LookupByName("ns3::UdpSocketFactory"));
142 uint16_t dstport = 12345;
143 Ipv4Address dstaddr("10.20.1.2");
144 InetSocketAddress dst = InetSocketAddress(dstaddr, dstport);
145 dstSocket->Bind(dst);
146 dstSocket->SetRecvCallback(MakeCallback(&dstSocketRecv));
147
148 AsciiTraceHelper ascii;
149 p2p.EnableAsciiAll(ascii.CreateFileStream("socket-bound-static-routing.tr"));
150 p2p.EnablePcapAll("socket-bound-static-routing");
151
153 LogComponentEnable("SocketBoundRoutingExample", LOG_LEVEL_INFO);
154
155 // First packet as normal (goes via Rtr1)
156 Simulator::Schedule(Seconds(0.1), &SendStuff, srcSocket, dstaddr, dstport);
157 // Second via Rtr1 explicitly
158 Simulator::Schedule(Seconds(1.0), &BindSock, srcSocket, SrcToRtr1);
159 Simulator::Schedule(Seconds(1.1), &SendStuff, srcSocket, dstaddr, dstport);
160 // Third via Rtr2 explicitly
161 Simulator::Schedule(Seconds(2.0), &BindSock, srcSocket, SrcToRtr2);
162 Simulator::Schedule(Seconds(2.1), &SendStuff, srcSocket, dstaddr, dstport);
163 // Fourth again as normal (goes via Rtr1)
164 Simulator::Schedule(Seconds(3.0), &BindSock, srcSocket, Ptr<NetDevice>(nullptr));
165 Simulator::Schedule(Seconds(3.1), &SendStuff, srcSocket, dstaddr, dstport);
166 // If you uncomment what's below, it results in ASSERT failing since you can't
167 // bind to a socket not existing on a node
168 // Simulator::Schedule(Seconds(4.0),&BindSock, srcSocket, dDstRtrdDst.Get(0));
171
172 return 0;
173}
174
175void
176SendStuff(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
177{
178 Ptr<Packet> p = Create<Packet>();
179 p->AddPaddingAtEnd(100);
180 sock->SendTo(p, 0, InetSocketAddress(dstaddr, port));
181}
182
183void
185{
186 sock->BindToNetDevice(netdev);
187}
188
189void
191{
192 Address from;
193 Ptr<Packet> packet = socket->RecvFrom(from);
194 packet->RemoveAllPacketTags();
195 packet->RemoveAllByteTags();
196 NS_LOG_INFO("Source Received " << packet->GetSize() << " bytes from "
198 if (socket->GetBoundNetDevice())
199 {
200 NS_LOG_INFO("Socket was bound");
201 }
202 else
203 {
204 NS_LOG_INFO("Socket was not bound");
205 }
206}
207
208void
210{
211 Address from;
212 Ptr<Packet> packet = socket->RecvFrom(from);
213 packet->RemoveAllPacketTags();
214 packet->RemoveAllByteTags();
216 NS_LOG_INFO("Destination Received " << packet->GetSize() << " bytes from "
217 << address.GetIpv4());
218 NS_LOG_INFO("Triggering packet back to source node's interface 1");
219 SendStuff(socket, Ipv4Address("10.1.1.1"), address.GetPort());
220}
a polymophic address class
Definition: address.h:101
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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.
Definition: command-line.h:232
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.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
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.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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:72
Hold variables of type string.
Definition: string.h:56
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:302
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:706
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
ns cmd
Definition: second.py:40
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)