A Discrete-Event Network Simulator
API
socket-bound-static-routing.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation;
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18/* Test program for multi-interface host, static routing
19
20 Destination host (10.20.1.2)
21 |
22 | 10.20.1.0/24
23 DSTRTR
24 10.10.1.0/24 / \ 10.10.2.0/24
25 / \
26 Rtr1 Rtr2
27 10.1.1.0/24 | | 10.1.2.0/24
28 | /
29 \ /
30 Source
31*/
32
33#include <iostream>
34#include <fstream>
35#include <string>
36#include <cassert>
37
38#include "ns3/core-module.h"
39#include "ns3/network-module.h"
40#include "ns3/internet-module.h"
41#include "ns3/point-to-point-module.h"
42#include "ns3/ipv4-static-routing-helper.h"
43#include "ns3/ipv4-list-routing-helper.h"
44
45using namespace ns3;
46
47NS_LOG_COMPONENT_DEFINE ("SocketBoundRoutingExample");
48
49void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
50void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
51void srcSocketRecv (Ptr<Socket> socket);
52void dstSocketRecv (Ptr<Socket> socket);
53
54int
55main (int argc, char *argv[])
56{
57
58 // Allow the user to override any of the defaults and the above
59 // DefaultValue::Bind ()s at run-time, via command-line arguments
60 CommandLine cmd (__FILE__);
61 cmd.Parse (argc, argv);
62
63 Ptr<Node> nSrc = CreateObject<Node> ();
64 Ptr<Node> nDst = CreateObject<Node> ();
65 Ptr<Node> nRtr1 = CreateObject<Node> ();
66 Ptr<Node> nRtr2 = CreateObject<Node> ();
67 Ptr<Node> nDstRtr = CreateObject<Node> ();
68
69 NodeContainer c = NodeContainer (nSrc, nDst, nRtr1, nRtr2, nDstRtr);
70
71 InternetStackHelper internet;
72 internet.Install (c);
73
74 // Point-to-point links
75 NodeContainer nSrcnRtr1 = NodeContainer (nSrc, nRtr1);
76 NodeContainer nSrcnRtr2 = NodeContainer (nSrc, nRtr2);
77 NodeContainer nRtr1nDstRtr = NodeContainer (nRtr1, nDstRtr);
78 NodeContainer nRtr2nDstRtr = NodeContainer (nRtr2, nDstRtr);
79 NodeContainer nDstRtrnDst = NodeContainer (nDstRtr, nDst);
80
81 // We create the channels first without any IP addressing information
83 p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
84 p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
85 NetDeviceContainer dSrcdRtr1 = p2p.Install (nSrcnRtr1);
86 NetDeviceContainer dSrcdRtr2 = p2p.Install (nSrcnRtr2);
87 NetDeviceContainer dRtr1dDstRtr = p2p.Install (nRtr1nDstRtr);
88 NetDeviceContainer dRtr2dDstRtr = p2p.Install (nRtr2nDstRtr);
89 NetDeviceContainer dDstRtrdDst = p2p.Install (nDstRtrnDst);
90
91 Ptr<NetDevice> SrcToRtr1=dSrcdRtr1.Get (0);
92 Ptr<NetDevice> SrcToRtr2=dSrcdRtr2.Get (0);
93
94 // Later, we add IP addresses.
96 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
97 Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign (dSrcdRtr1);
98 ipv4.SetBase ("10.1.2.0", "255.255.255.0");
99 Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign (dSrcdRtr2);
100 ipv4.SetBase ("10.10.1.0", "255.255.255.0");
101 Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign (dRtr1dDstRtr);
102 ipv4.SetBase ("10.10.2.0", "255.255.255.0");
103 Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign (dRtr2dDstRtr);
104 ipv4.SetBase ("10.20.1.0", "255.255.255.0");
105 Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign (dDstRtrdDst);
106
107 Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4> ();
108 Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4> ();
109 Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4> ();
110 Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4> ();
111 Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4> ();
112
113 Ipv4StaticRoutingHelper ipv4RoutingHelper;
114 Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting (ipv4Src);
115 Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr1);
116 Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr2);
117 Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting (ipv4DstRtr);
118 Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting (ipv4Dst);
119
120 // Create static routes from Src to Dst
121 staticRoutingRtr1->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.1.2"), 2);
122 staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.2.2"), 2);
123
124 // Two routes to same destination - setting separate metrics.
125 // You can switch these to see how traffic gets diverted via different routes
126 staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.1.2"), 1,5);
127 staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.2.2"), 2,10);
128
129 // Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
130 staticRoutingDst->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.20.1.1"), 1);
131 staticRoutingDstRtr->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.10.2.1"), 2);
132 staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.2.1"), 1);
133
134 // There are no apps that can utilize the Socket Option so doing the work directly..
135 // Taken from tcp-large-transfer example
136
137 Ptr<Socket> srcSocket = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::UdpSocketFactory"));
138 srcSocket->Bind ();
140
141 Ptr<Socket> dstSocket = 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);
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>(0));
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));
169 Simulator::Run ();
170 Simulator::Destroy ();
171
172 return 0;
173}
174
175void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
176{
177 Ptr<Packet> p = Create<Packet> ();
178 p->AddPaddingAtEnd (100);
179 sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
180 return;
181}
182
184{
185 sock->BindToNetDevice (netdev);
186 return;
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 " << InetSocketAddress::ConvertFrom (from).GetIpv4 ());
197 if (socket->GetBoundNetDevice ())
198 {
199 NS_LOG_INFO ("Socket was bound");
200 }
201 else
202 {
203 NS_LOG_INFO ("Socket was not bound");
204 }
205}
206
207void
209{
210 Address from;
211 Ptr<Packet> packet = socket->RecvFrom (from);
212 packet->RemoveAllPacketTags ();
213 packet->RemoveAllByteTags ();
214 InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
215 NS_LOG_INFO ("Destination Received " << packet->GetSize () << " bytes from " << address.GetIpv4 ());
216 NS_LOG_INFO ("Triggering packet back to source node's interface 1");
217 SendStuff (socket, Ipv4Address ("10.1.1.1"), address.GetPort ());
218}
a polymophic address class
Definition: address.h:91
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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:229
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
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.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:984
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:347
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound NetDevice, if any.
Definition: socket.cc:351
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
Definition: socket.cc:330
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
Hold variables of type string.
Definition: string.h:41
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:385
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
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)