A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
45 using namespace ns3;
46 
47 NS_LOG_COMPONENT_DEFINE ("SocketBoundRoutingExample");
48 
49 void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
50 void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
51 void srcSocketRecv (Ptr<Socket> socket);
52 void dstSocketRecv (Ptr<Socket> socket);
53 
54 int
55 main (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;
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.
95  Ipv4AddressHelper ipv4;
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 ();
171 
172  return 0;
173 }
174 
175 void 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 
189 void
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 
207 void
209 {
210  Address from;
211  Ptr<Packet> packet = socket->RecvFrom (from);
212  packet->RemoveAllPacketTags ();
213  packet->RemoveAllByteTags ();
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 }