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
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
48 cmd.Parse(argc, argv);
49
55
57
59 internet.Install(c);
60
61 // Point-to-point links
67
68 // We create the channels first without any IP addressing information
70 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
71 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
77
80
81 // Later, we add IP addresses.
83 ipv4.SetBase("10.1.1.0", "255.255.255.0");
85 ipv4.SetBase("10.1.2.0", "255.255.255.0");
87 ipv4.SetBase("10.10.1.0", "255.255.255.0");
89 ipv4.SetBase("10.10.2.0", "255.255.255.0");
91 ipv4.SetBase("10.20.1.0", "255.255.255.0");
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
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
125 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::UdpSocketFactory"));
126 srcSocket->Bind();
127 srcSocket->SetRecvCallback(MakeCallback(&srcSocketRecv));
128
130 Socket::CreateSocket(nDst, TypeId::LookupByName("ns3::UdpSocketFactory"));
131 uint16_t dstport = 12345;
132 Ipv4Address dstaddr("10.20.1.2");
134 dstSocket->Bind(dst);
135 dstSocket->SetRecvCallback(MakeCallback(&dstSocketRecv));
136
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)
146 // Second via Rtr1 explicitly
149 // Third via Rtr2 explicitly
152 // Fourth again as normal (goes via Rtr1)
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),&BindSock, srcSocket, dDstRtrdDst.Get(0));
160
161 return 0;
162}
163
164void
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.
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
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:66
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 > 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:1344
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)