A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
csma-broadcast.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 // 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/discard port, which
28 // is replicated and received on both n1 and n2
29 
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 #include <cassert>
34 
35 #include "ns3/core-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/csma-module.h"
38 #include "ns3/applications-module.h"
39 #include "ns3/internet-module.h"
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("CsmaBroadcastExample");
44 
45 int
46 main (int argc, char *argv[])
47 {
48  // Users may find it convenient to turn on explicit debugging
49  // for selected modules; the below lines suggest how to do this
50 #if 0
51  LogComponentEnable ("CsmaBroadcastExample", LOG_LEVEL_INFO);
52 #endif
53  LogComponentEnable ("CsmaBroadcastExample", LOG_PREFIX_TIME);
54 
55  // Allow the user to override any of the defaults and the above
56  // Bind()s at run-time, via command-line arguments
57  CommandLine cmd;
58  cmd.Parse (argc, argv);
59 
60  NS_LOG_INFO ("Create nodes.");
61  NodeContainer c;
62  c.Create (3);
63  NodeContainer c0 = NodeContainer (c.Get (0), c.Get (1));
64  NodeContainer c1 = NodeContainer (c.Get (0), c.Get (2));
65 
66  NS_LOG_INFO ("Build Topology.");
67  CsmaHelper csma;
68  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
69  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
70 
71  NetDeviceContainer n0 = csma.Install (c0);
72  NetDeviceContainer n1 = csma.Install (c1);
73 
74  InternetStackHelper internet;
75  internet.Install (c);
76 
77  NS_LOG_INFO ("Assign IP Addresses.");
78  Ipv4AddressHelper ipv4;
79  ipv4.SetBase ("10.1.0.0", "255.255.255.0");
80  ipv4.Assign (n0);
81  ipv4.SetBase ("192.168.1.0", "255.255.255.0");
82  ipv4.Assign (n1);
83 
84 
85  // RFC 863 discard port ("9") indicates packet should be thrown away
86  // by the system. We allow this silent discard to be overridden
87  // by the PacketSink application.
88  uint16_t port = 9;
89 
90  // Create the OnOff application to send UDP datagrams of size
91  // 512 bytes (default) at a rate of 500 Kb/s (default) from n0
92  NS_LOG_INFO ("Create Applications.");
93  OnOffHelper onoff ("ns3::UdpSocketFactory",
94  Address (InetSocketAddress (Ipv4Address ("255.255.255.255"), port)));
95  onoff.SetConstantRate (DataRate ("500kb/s"));
96 
97  ApplicationContainer app = onoff.Install (c0.Get (0));
98  // Start the application
99  app.Start (Seconds (1.0));
100  app.Stop (Seconds (10.0));
101 
102  // Create an optional packet sink to receive these packets
103  PacketSinkHelper sink ("ns3::UdpSocketFactory",
105  app = sink.Install (c0.Get (1));
106  app.Add (sink.Install (c1.Get (1)));
107  app.Start (Seconds (1.0));
108  app.Stop (Seconds (10.0));
109 
110  // Configure ascii tracing of all enqueue, dequeue, and NetDevice receive
111  // events on all devices. Trace output will be sent to the file
112  // "csma-one-subnet.tr"
113  AsciiTraceHelper ascii;
114  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-broadcast.tr"));
115 
116  // Also configure some tcpdump traces; each interface will be traced
117  // The output files will be named
118  // csma-broadcast-<nodeId>-<interfaceId>.pcap
119  // and can be read by the "tcpdump -tt -r" command
120  csma.EnablePcapAll ("csma-broadcast", false);
121 
122  NS_LOG_INFO ("Run Simulation.");
123  Simulator::Run ();
125  NS_LOG_INFO ("Done.");
126 }