|
|
| 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 |
// Example of the sending of a datagram to a broadcast address |
| 19 |
// |
| 20 |
// Network topology |
| 21 |
// ============== |
| 22 |
// | | |
| 23 |
// n0 n1 n2 |
| 24 |
// | | |
| 25 |
// ========== |
| 26 |
// |
| 27 |
// n0 originates UDP broadcast to 255.255.255.255, which is replicated |
| 28 |
// and received on both n1 and n2 |
| 29 |
|
| 30 |
#include <iostream> |
| 31 |
#include <fstream> |
| 32 |
#include <string> |
| 33 |
#include <cassert> |
| 34 |
|
| 35 |
#include "ns3/command-line.h" |
| 36 |
#include "ns3/default-value.h" |
| 37 |
#include "ns3/ptr.h" |
| 38 |
#include "ns3/random-variable.h" |
| 39 |
#include "ns3/debug.h" |
| 40 |
|
| 41 |
#include "ns3/simulator.h" |
| 42 |
#include "ns3/nstime.h" |
| 43 |
#include "ns3/data-rate.h" |
| 44 |
|
| 45 |
#include "ns3/ascii-trace.h" |
| 46 |
#include "ns3/pcap-trace.h" |
| 47 |
#include "ns3/internet-node.h" |
| 48 |
#include "ns3/csma-channel.h" |
| 49 |
#include "ns3/csma-net-device.h" |
| 50 |
#include "ns3/csma-topology.h" |
| 51 |
#include "ns3/csma-ipv4-topology.h" |
| 52 |
#include "ns3/eui48-address.h" |
| 53 |
#include "ns3/ipv4-address.h" |
| 54 |
#include "ns3/inet-socket-address.h" |
| 55 |
#include "ns3/ipv4.h" |
| 56 |
#include "ns3/socket.h" |
| 57 |
#include "ns3/ipv4-route.h" |
| 58 |
#include "ns3/onoff-application.h" |
| 59 |
|
| 60 |
|
| 61 |
using namespace ns3; |
| 62 |
|
| 63 |
|
| 64 |
int main (int argc, char *argv[]) |
| 65 |
{ |
| 66 |
|
| 67 |
// Users may find it convenient to turn on explicit debugging |
| 68 |
// for selected modules; the below lines suggest how to do this |
| 69 |
#if 0 |
| 70 |
DebugComponentEnable("CsmaNetDevice"); |
| 71 |
DebugComponentEnable("Ipv4L3Protocol"); |
| 72 |
DebugComponentEnable("NetDevice"); |
| 73 |
DebugComponentEnable("Channel"); |
| 74 |
DebugComponentEnable("CsmaChannel"); |
| 75 |
DebugComponentEnable("PacketSocket"); |
| 76 |
#endif |
| 77 |
|
| 78 |
// Set up some default values for the simulation. Use the Bind() |
| 79 |
// technique to tell the system what subclass of Queue to use, |
| 80 |
// and what the queue limit is |
| 81 |
|
| 82 |
// The below Bind command tells the queue factory which class to |
| 83 |
// instantiate, when the queue factory is invoked in the topology code |
| 84 |
DefaultValue::Bind ("Queue", "DropTailQueue"); |
| 85 |
|
| 86 |
// Allow the user to override any of the defaults and the above |
| 87 |
// Bind()s at run-time, via command-line arguments |
| 88 |
CommandLine::Parse (argc, argv); |
| 89 |
|
| 90 |
// Here, we will explicitly create four nodes. In more sophisticated |
| 91 |
// topologies, we could configure a node factory. |
| 92 |
Ptr<Node> n0 = Create<InternetNode> (); |
| 93 |
Ptr<Node> n1 = Create<InternetNode> (); |
| 94 |
Ptr<Node> n2 = Create<InternetNode> (); |
| 95 |
|
| 96 |
// We create the channels first without any IP addressing information |
| 97 |
Ptr<CsmaChannel> channel0 = |
| 98 |
CsmaTopology::CreateCsmaChannel( |
| 99 |
DataRate(5000000), MilliSeconds(2)); |
| 100 |
|
| 101 |
// We create the channels first without any IP addressing information |
| 102 |
Ptr<CsmaChannel> channel1 = |
| 103 |
CsmaTopology::CreateCsmaChannel( |
| 104 |
DataRate(5000000), MilliSeconds(2)); |
| 105 |
|
| 106 |
uint32_t n0ifIndex0 = CsmaIpv4Topology::AddIpv4CsmaNode (n0, channel0, |
| 107 |
Eui48Address("10:54:23:54:0:50")); |
| 108 |
uint32_t n0ifIndex1 = CsmaIpv4Topology::AddIpv4CsmaNode (n0, channel1, |
| 109 |
Eui48Address("10:54:23:54:0:51")); |
| 110 |
uint32_t n1ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n1, channel0, |
| 111 |
Eui48Address("10:54:23:54:23:51")); |
| 112 |
uint32_t n2ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n2, channel1, |
| 113 |
Eui48Address("10:54:23:54:23:52")); |
| 114 |
|
| 115 |
// Later, we add IP addresses. |
| 116 |
CsmaIpv4Topology::AddIpv4Address ( |
| 117 |
n0, n0ifIndex0, Ipv4Address("10.1.0.1"), Ipv4Mask("255.255.0.0")); |
| 118 |
|
| 119 |
CsmaIpv4Topology::AddIpv4Address ( |
| 120 |
n1, n1ifIndex, Ipv4Address("10.1.0.2"), Ipv4Mask("255.255.0.0")); |
| 121 |
|
| 122 |
CsmaIpv4Topology::AddIpv4Address ( |
| 123 |
n0, n0ifIndex1, Ipv4Address("192.168.1.1"), Ipv4Mask("255.255.255.0")); |
| 124 |
|
| 125 |
CsmaIpv4Topology::AddIpv4Address ( |
| 126 |
n2, n2ifIndex, Ipv4Address("192.168.1.2"), Ipv4Mask("255.255.255.0")); |
| 127 |
|
| 128 |
// Create the OnOff application to send UDP datagrams of size |
| 129 |
// 210 bytes at a rate of 448 Kb/s |
| 130 |
// from n0 to n1 |
| 131 |
Ptr<OnOffApplication> ooff = Create<OnOffApplication> ( |
| 132 |
n0, |
| 133 |
InetSocketAddress ("255.255.255.255", 80), |
| 134 |
"Udp", |
| 135 |
ConstantVariable(1), |
| 136 |
ConstantVariable(0)); |
| 137 |
// Start the application |
| 138 |
ooff->Start(Seconds(1.0)); |
| 139 |
ooff->Stop (Seconds(10.0)); |
| 140 |
|
| 141 |
|
| 142 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
| 143 |
// Trace output will be sent to the csma-broadcast.tr file |
| 144 |
AsciiTrace asciitrace ("csma-broadcast.tr"); |
| 145 |
asciitrace.TraceAllNetDeviceRx (); |
| 146 |
asciitrace.TraceAllQueues (); |
| 147 |
|
| 148 |
// Also configure some tcpdump traces; each interface will be traced |
| 149 |
// The output files will be named |
| 150 |
// simple-point-to-point.pcap-<nodeId>-<interfaceId> |
| 151 |
// and can be read by the "tcpdump -r" command (use "-tt" option to |
| 152 |
// display timestamps correctly) |
| 153 |
PcapTrace pcaptrace ("csma-broadcast.pcap"); |
| 154 |
pcaptrace.TraceAllIp (); |
| 155 |
|
| 156 |
Simulator::Run (); |
| 157 |
|
| 158 |
Simulator::Destroy (); |
| 159 |
} |