author | Tom Henderson <tomh@tomh.org> |
Fri, 20 Aug 2010 12:17:19 -0700 | |
changeset 6589 | 9c325569fb01 |
parent 6009 | e1b696a1ed28 |
permissions | -rw-r--r-- |
4433
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
2 |
/* |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
3 |
* This program is free software; you can redistribute it and/or modify |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
4 |
* it under the terms of the GNU General Public License version 2 as |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
5 |
* published by the Free Software Foundation; |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
6 |
* |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
7 |
* This program is distributed in the hope that it will be useful, |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
10 |
* GNU General Public License for more details. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
11 |
* |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
12 |
* You should have received a copy of the GNU General Public License |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
13 |
* along with this program; if not, write to the Free Software |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
15 |
*/ |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
16 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
17 |
// Allow ns-3 to ping a real host somewhere, using emulation mode |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
18 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
19 |
// ------------ |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
20 |
// | node n0 | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
21 |
// | | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
22 |
// | --- | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
23 |
// | | | | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
24 |
// | |emu| | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
25 |
// | | | | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
26 |
// | --- | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
27 |
// | | | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
28 |
// ----|------- |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
29 |
// | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
30 |
// (device on host system, set to promiscuous mode) |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
31 |
// | |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
32 |
// --------- (Internet) ------- |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
33 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
34 |
// To use this example: |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
35 |
// 1) You need to decide on a physical device on your real system, and either |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
36 |
// overwrite the hard-configured device name below (eth0) or pass this |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
37 |
// device name in as a command-line argument |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
38 |
// 2) The host device must be set to promiscuous mode |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
39 |
// (e.g. "sudo ifconfig eth0 promisc") |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
40 |
// 3) Be aware that ns-3 will generate a fake mac address, and that in |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
41 |
// some enterprise networks, this may be considered bad form to be |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
42 |
// sending packets out of your device with "unauthorized" mac addresses |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
43 |
// 4) You will need to assign an IP address to the ns-3 simulation node that |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
44 |
// is consistent with the subnet that is active on the host device's link. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
45 |
// That is, you will have to assign an IP address to the ns-3 node as if |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
46 |
// it were on your real subnet. Search for "Ipv4Address localIp" and |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
47 |
// replace the string "1.2.3.4" with a valid IP address. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
48 |
// 5) You will need to configure a default route in the ns-3 node to tell it |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
49 |
// how to get off of your subnet. One thing you could do is a |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
50 |
// 'netstat -rn' command and find the IP address of the default gateway |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
51 |
// on your host. Search for "Ipv4Address gateway" and replace the string |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
52 |
// "1.2.3.4" string with the gateway IP address. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
53 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
54 |
#include "ns3/abort.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
55 |
#include "ns3/core-module.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
56 |
#include "ns3/simulator-module.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
57 |
#include "ns3/node-module.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
58 |
#include "ns3/emu-module.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
59 |
#include "ns3/v4ping-module.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
60 |
#include "ns3/helper-module.h" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
61 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
62 |
using namespace ns3; |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
63 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
64 |
NS_LOG_COMPONENT_DEFINE ("PingEmulationExample"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
65 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
66 |
static void |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
67 |
PingRtt (std::string context, Time rtt) |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
68 |
{ |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
69 |
NS_LOG_UNCOND ("Received Response with RTT = " << rtt); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
70 |
} |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
71 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
72 |
int |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
73 |
main (int argc, char *argv[]) |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
74 |
{ |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
75 |
NS_LOG_INFO ("Ping Emulation Example"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
76 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
77 |
std::string deviceName ("eth0"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
78 |
std::string remote ("208.77.188.166"); // example.com |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
79 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
80 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
81 |
// Allow the user to override any of the defaults at run-time, via |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
82 |
// command-line arguments |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
83 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
84 |
CommandLine cmd; |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
85 |
cmd.AddValue("deviceName", "Device name", deviceName); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
86 |
cmd.AddValue("remote", "Remote IP address (dotted decimal only please)", remote); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
87 |
cmd.Parse (argc, argv); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
88 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
89 |
Ipv4Address remoteIp (remote.c_str ()); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
90 |
Ipv4Address localIp ("1.2.3.4"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
91 |
NS_ABORT_MSG_IF (localIp == "1.2.3.4", "You must change the local IP address before running this example"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
92 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
93 |
Ipv4Mask localMask ("255.255.255.0"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
94 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
95 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
96 |
// Since we are using a real piece of hardware we need to use the realtime |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
97 |
// simulator. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
98 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
99 |
GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl")); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
100 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
101 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
102 |
// Since we are going to be talking to real-world machines, we need to enable |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
103 |
// calculation of checksums in our protocols. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
104 |
// |
4568
8a9c81e59c92
fix examples and documentation for CalcChecksum changes
Tom Henderson <tomh@tomh.org>
parents:
4473
diff
changeset
|
105 |
GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true)); |
4433
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
106 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
107 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
108 |
// In such a simple topology, the use of the helper API can be a hindrance |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
109 |
// so we drop down into the low level API and do it manually. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
110 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
111 |
// First we need a single node. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
112 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
113 |
NS_LOG_INFO ("Create Node"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
114 |
Ptr<Node> node = CreateObject<Node> (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
115 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
116 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
117 |
// Create an emu device, allocate a MAC address and point the device to the |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
118 |
// Linux device name. The device needs a transmit queueing discipline so |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
119 |
// create a droptail queue and give it to the device. Finally, "install" |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
120 |
// the device into the node. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
121 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
122 |
// Do understand that the ns-3 allocated MAC address will be sent out over |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
123 |
// your network since the emu net device will spoof it. By default, this |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
124 |
// address will have an Organizationally Unique Identifier (OUI) of zero. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
125 |
// The Internet Assigned Number Authority IANA |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
126 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
127 |
// http://www.iana.org/assignments/ethernet-numbers |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
128 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
129 |
// reports that this OUI is unassigned, and so should not conflict with |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
130 |
// real hardware on your net. It may raise all kinds of red flags in a |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
131 |
// real environment to have packets from a device with an obviously bogus |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
132 |
// OUI flying around. Be aware. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
133 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
134 |
NS_LOG_INFO ("Create Device"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
135 |
Ptr<EmuNetDevice> device = CreateObject<EmuNetDevice> (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
136 |
device->SetAttribute ("Address", Mac48AddressValue (Mac48Address::Allocate ())); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
137 |
device->SetAttribute ("DeviceName", StringValue (deviceName)); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
138 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
139 |
Ptr<Queue> queue = CreateObject<DropTailQueue> (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
140 |
device->SetQueue (queue); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
141 |
node->AddDevice (device); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
142 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
143 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
144 |
// Add a default internet stack to the node. This gets us the ns-3 versions |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
145 |
// of ARP, IPv4, ICMP, UDP and TCP. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
146 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
147 |
NS_LOG_INFO ("Add Internet Stack"); |
4473
39ac17168023
examples/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4433
diff
changeset
|
148 |
InternetStackHelper internetStackHelper; |
39ac17168023
examples/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4433
diff
changeset
|
149 |
internetStackHelper.Install (node); |
4433
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
150 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
151 |
NS_LOG_INFO ("Create IPv4 Interface"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
152 |
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
153 |
uint32_t interface = ipv4->AddInterface (device); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
154 |
Ipv4InterfaceAddress address = Ipv4InterfaceAddress (localIp, localMask); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
155 |
ipv4->AddAddress (interface, address); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
156 |
ipv4->SetMetric (interface, 1); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
157 |
ipv4->SetUp (interface); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
158 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
159 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
160 |
// When the ping appliation sends its ICMP packet, it will happily send it |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
161 |
// down the ns-3 protocol stack. We set the IP address of the destination |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
162 |
// to the address corresponding to example.com above. This address is off |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
163 |
// our local network so we have got to provide some kind of default route |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
164 |
// to ns-3 to be able to get that ICMP packet forwarded off of our network. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
165 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
166 |
// You have got to provide an IP address of a real host that you can send |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
167 |
// real packets to and have them forwarded off of your local network. One |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
168 |
// thing you could do is a 'netstat -rn' command and find the IP address of |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
169 |
// the default gateway on your host and add it below, replacing the |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
170 |
// "1.2.3.4" string. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
171 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
172 |
Ipv4Address gateway ("1.2.3.4"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
173 |
NS_ABORT_MSG_IF (gateway == "1.2.3.4", "You must change the gateway IP address before running this example"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
174 |
|
4473
39ac17168023
examples/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4433
diff
changeset
|
175 |
Ipv4StaticRoutingHelper ipv4RoutingHelper; |
39ac17168023
examples/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4433
diff
changeset
|
176 |
Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting (ipv4); |
39ac17168023
examples/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4433
diff
changeset
|
177 |
staticRouting->SetDefaultRoute (gateway, interface); |
4433
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
178 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
179 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
180 |
// Create the ping application. This application knows how to send |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
181 |
// ICMP echo requests. Setting up the packet sink manually is a bit |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
182 |
// of a hassle and since there is no law that says we cannot mix the |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
183 |
// helper API with the low level API, let's just use the helper. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
184 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
185 |
NS_LOG_INFO ("Create V4Ping Appliation"); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
186 |
Ptr<V4Ping> app = CreateObject<V4Ping> (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
187 |
app->SetAttribute ("Remote", Ipv4AddressValue (remoteIp)); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
188 |
node->AddApplication (app); |
5524
efed7493f2c1
Make applications generate traffic within their associated context/node
Guillaume Seguin <guillaume@segu.in>
parents:
5369
diff
changeset
|
189 |
app->SetStartTime (Seconds (1.0)); |
efed7493f2c1
Make applications generate traffic within their associated context/node
Guillaume Seguin <guillaume@segu.in>
parents:
5369
diff
changeset
|
190 |
app->SetStopTime (Seconds (5.0)); |
4433
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
191 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
192 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
193 |
// Give the application a name. This makes life much easier when constructing |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
194 |
// config paths. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
195 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
196 |
Names::Add ("app", app); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
197 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
198 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
199 |
// Hook a trace to print something when the response comes back. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
200 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
201 |
Config::Connect ("/Names/app/Rtt", MakeCallback (&PingRtt)); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
202 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
203 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
204 |
// Enable a promiscuous pcap trace to see what is coming and going on our device. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
205 |
// |
6009
e1b696a1ed28
redo pcap tracing
Craig Dowell <craigdo@ee.washington.edu>
parents:
5524
diff
changeset
|
206 |
EmuHelper emu; |
e1b696a1ed28
redo pcap tracing
Craig Dowell <craigdo@ee.washington.edu>
parents:
5524
diff
changeset
|
207 |
emu.EnablePcap ("emu-ping", device, true); |
4433
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
208 |
|
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
209 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
210 |
// Now, do the actual emulation. |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
211 |
// |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
212 |
NS_LOG_INFO ("Run Emulation."); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
213 |
Simulator::Stop (Seconds (5.0)); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
214 |
Simulator::Run (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
215 |
Simulator::Destroy (); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
216 |
NS_LOG_INFO ("Done."); |
41a18c7aecfc
remove Control-M from example
Tom Henderson <tomh@tomh.org>
parents:
4418
diff
changeset
|
217 |
} |