A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
emu-ping.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 // Allow ns-3 to ping a real host somewhere, using emulation mode
18 //
19 // ------------
20 // | node n0 |
21 // | |
22 // | --- |
23 // | | | |
24 // | |emu| |
25 // | | | |
26 // | --- |
27 // | | |
28 // ----|-------
29 // |
30 // (device on host system, set to promiscuous mode)
31 // |
32 // --------- (Internet) -------
33 //
34 // To use this example:
35 // 1) You need to decide on a physical device on your real system, and either
36 // overwrite the hard-configured device name below (eth0) or pass this
37 // device name in as a command-line argument
38 // 2) The host device must be set to promiscuous mode
39 // (e.g. "sudo ifconfig eth0 promisc")
40 // 3) Be aware that ns-3 will generate a fake mac address, and that in
41 // some enterprise networks, this may be considered bad form to be
42 // sending packets out of your device with "unauthorized" mac addresses
43 // 4) You will need to assign an IP address to the ns-3 simulation node that
44 // is consistent with the subnet that is active on the host device's link.
45 // That is, you will have to assign an IP address to the ns-3 node as if
46 // it were on your real subnet. Search for "Ipv4Address localIp" and
47 // replace the string "1.2.3.4" with a valid IP address.
48 // 5) You will need to configure a default route in the ns-3 node to tell it
49 // how to get off of your subnet. One thing you could do is a
50 // 'netstat -rn' command and find the IP address of the default gateway
51 // on your host. Search for "Ipv4Address gateway" and replace the string
52 // "1.2.3.4" string with the gateway IP address.
53 
54 #include "ns3/abort.h"
55 #include "ns3/core-module.h"
56 #include "ns3/internet-module.h"
57 #include "ns3/network-module.h"
58 #include "ns3/emu-module.h"
59 #include "ns3/applications-module.h"
60 #include "ns3/ipv4-static-routing-helper.h"
61 #include "ns3/ipv4-list-routing-helper.h"
62 
63 using namespace ns3;
64 
65 NS_LOG_COMPONENT_DEFINE ("PingEmulationExample");
66 
67 static void
68 PingRtt (std::string context, Time rtt)
69 {
70  NS_LOG_UNCOND ("Received Response with RTT = " << rtt);
71 }
72 
73 int
74 main (int argc, char *argv[])
75 {
76  NS_LOG_INFO ("Ping Emulation Example");
77 
78  std::string deviceName ("eth0");
79  std::string remote ("208.77.188.166"); // example.com
80 
81  //
82  // Allow the user to override any of the defaults at run-time, via
83  // command-line arguments
84  //
85  CommandLine cmd;
86  cmd.AddValue ("deviceName", "Device name", deviceName);
87  cmd.AddValue ("remote", "Remote IP address (dotted decimal only please)", remote);
88  cmd.Parse (argc, argv);
89 
90  Ipv4Address remoteIp (remote.c_str ());
91  Ipv4Address localIp ("192.168.1.94");
92  NS_ABORT_MSG_IF (localIp == "1.2.3.4", "You must change the local IP address before running this example");
93 
94  Ipv4Mask localMask ("255.255.255.0");
95 
96  //
97  // Since we are using a real piece of hardware we need to use the realtime
98  // simulator.
99  //
100  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
101 
102  //
103  // Since we are going to be talking to real-world machines, we need to enable
104  // calculation of checksums in our protocols.
105  //
106  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
107 
108  //
109  // In such a simple topology, the use of the helper API can be a hindrance
110  // so we drop down into the low level API and do it manually.
111  //
112  // First we need a single node.
113  //
114  NS_LOG_INFO ("Create Node");
115  Ptr<Node> node = CreateObject<Node> ();
116 
117  //
118  // Create an emu device, allocate a MAC address and point the device to the
119  // Linux device name. The device needs a transmit queueing discipline so
120  // create a droptail queue and give it to the device. Finally, "install"
121  // the device into the node.
122  //
123  // Do understand that the ns-3 allocated MAC address will be sent out over
124  // your network since the emu net device will spoof it. By default, this
125  // address will have an Organizationally Unique Identifier (OUI) of zero.
126  // The Internet Assigned Number Authority IANA
127  //
128  // http://www.iana.org/assignments/ethernet-numbers
129  //
130  // reports that this OUI is unassigned, and so should not conflict with
131  // real hardware on your net. It may raise all kinds of red flags in a
132  // real environment to have packets from a device with an obviously bogus
133  // OUI flying around. Be aware.
134  //
135  NS_LOG_INFO ("Create Device");
136  Ptr<EmuNetDevice> device = CreateObject<EmuNetDevice> ();
137  device->SetAttribute ("Address", Mac48AddressValue (Mac48Address::Allocate ()));
138  device->SetAttribute ("DeviceName", StringValue (deviceName));
139 
140  Ptr<Queue> queue = CreateObject<DropTailQueue> ();
141  device->SetQueue (queue);
142  node->AddDevice (device);
143 
144  //
145  // Add a default internet stack to the node. This gets us the ns-3 versions
146  // of ARP, IPv4, ICMP, UDP and TCP.
147  //
148  NS_LOG_INFO ("Add Internet Stack");
149  InternetStackHelper internetStackHelper;
150  internetStackHelper.Install (node);
151 
152  NS_LOG_INFO ("Create IPv4 Interface");
153  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
154  uint32_t interface = ipv4->AddInterface (device);
155  Ipv4InterfaceAddress address = Ipv4InterfaceAddress (localIp, localMask);
156  ipv4->AddAddress (interface, address);
157  ipv4->SetMetric (interface, 1);
158  ipv4->SetUp (interface);
159 
160  //
161  // When the ping appliation sends its ICMP packet, it will happily send it
162  // down the ns-3 protocol stack. We set the IP address of the destination
163  // to the address corresponding to example.com above. This address is off
164  // our local network so we have got to provide some kind of default route
165  // to ns-3 to be able to get that ICMP packet forwarded off of our network.
166  //
167  // You have got to provide an IP address of a real host that you can send
168  // real packets to and have them forwarded off of your local network. One
169  // thing you could do is a 'netstat -rn' command and find the IP address of
170  // the default gateway on your host and add it below, replacing the
171  // "1.2.3.4" string.
172  //
173  Ipv4Address gateway ("192.168.1.254");
174  NS_ABORT_MSG_IF (gateway == "1.2.3.4", "You must change the gateway IP address before running this example");
175 
176  Ipv4StaticRoutingHelper ipv4RoutingHelper;
177  Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting (ipv4);
178  staticRouting->SetDefaultRoute (gateway, interface);
179 
180  //
181  // Create the ping application. This application knows how to send
182  // ICMP echo requests. Setting up the packet sink manually is a bit
183  // of a hassle and since there is no law that says we cannot mix the
184  // helper API with the low level API, let's just use the helper.
185  //
186  NS_LOG_INFO ("Create V4Ping Appliation");
187  Ptr<V4Ping> app = CreateObject<V4Ping> ();
188  app->SetAttribute ("Remote", Ipv4AddressValue (remoteIp));
189  node->AddApplication (app);
190  app->SetStartTime (Seconds (1.0));
191  app->SetStopTime (Seconds (5.0));
192 
193  //
194  // Give the application a name. This makes life much easier when constructing
195  // config paths.
196  //
197  Names::Add ("app", app);
198 
199  //
200  // Hook a trace to print something when the response comes back.
201  //
202  Config::Connect ("/Names/app/Rtt", MakeCallback (&PingRtt));
203 
204  //
205  // Enable a promiscuous pcap trace to see what is coming and going on our device.
206  //
207  EmuHelper emu;
208  emu.EnablePcap ("emu-ping", device, true);
209 
210  //
211  // Now, do the actual emulation.
212  //
213  NS_LOG_INFO ("Run Emulation.");
214  Simulator::Stop (Seconds (5.0));
215  Simulator::Run ();
217  NS_LOG_INFO ("Done.");
218 }
uint32_t AddApplication(Ptr< Application > application)
Definition: node.cc:147
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:75
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Hold a bool native type.
Definition: boolean.h:38
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
hold variables of type string
Definition: string.h:19
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
build a set of EmuNetDevice objects
Definition: emu-helper.h:46
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Definition: log.h:298
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:616
void SetQueue(Ptr< Queue > queue)
Attach a queue to the EmuNetDevice.
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
virtual void SetUp(uint32_t interface)=0
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
int main(int argc, char *argv[])
Definition: emu-ping.cc:74
static void Bind(std::string name, const AttributeValue &value)
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
#define NS_LOG_UNCOND(msg)
Definition: log.h:377
hold objects of type ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
a class to store IPv4 address information on an interface
Helper class that adds ns3::Ipv4StaticRouting objects.
hold objects of type ns3::Mac48Address
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
void Parse(int argc, char *argv[])
Parse the program arguments.
tuple address
Definition: first.py:37
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:98
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
Ptr< T > GetObject(void) const
Definition: object.h:361
void SetStartTime(Time start)
Specify application start time.
Definition: application.cc:69
static void PingRtt(std::string context, Time rtt)
Definition: emu-ping.cc:68