A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
csma-raw-ip-socket.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 // Network topology
19 // (sender) (receiver)
20 // n0 n1 n2 n3
21 // | | | |
22 // =====================
23 //
24 // Node n0 sends data to node n3 over a raw IP socket. The protocol
25 // number used is 2.
26 // The default traffic rate is to send 1250 bytes/second for 10 kb/s
27 // The data rate can be toggled by command line argument
28 
29 #include <iostream>
30 #include <fstream>
31 #include <string>
32 #include <cassert>
33 
34 #include "ns3/core-module.h"
35 #include "ns3/network-module.h"
36 #include "ns3/csma-module.h"
37 #include "ns3/applications-module.h"
38 #include "ns3/internet-module.h"
39 
40 using namespace ns3;
41 
42 NS_LOG_COMPONENT_DEFINE ("CsmaRawIpSocketExample");
43 
44 static void SinkRx (Ptr<const Packet> p, const Address &ad)
45 {
46  // Enable the below line to see the packet contents printed out at the
47  // receive sink
48  //std::cout << Simulator::Now().GetSeconds () << " " << *p << std::endl;
49 }
50 
51 int
52 main (int argc, char *argv[])
53 {
54 #if 0
55  LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
56 #endif
57  uint32_t dataRate = 10;
58  CommandLine cmd;
59  cmd.AddValue ("dataRate", "application dataRate (Kb/s)", dataRate);
60  cmd.Parse (argc, argv);
61 
62  // Here, we will explicitly create four nodes.
63  NS_LOG_INFO ("Create nodes.");
64  NodeContainer c;
65  c.Create (4);
66 
67  // connect all our nodes to a shared channel.
68  NS_LOG_INFO ("Build Topology.");
69  CsmaHelper csma;
70  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
71  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
72  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
73  NetDeviceContainer devs = csma.Install (c);
74 
75  // add an ip stack to all nodes.
76  NS_LOG_INFO ("Add ip stack.");
77  InternetStackHelper ipStack;
78  ipStack.Install (c);
79 
80  // assign ip addresses
81  NS_LOG_INFO ("Assign ip addresses.");
83  ip.SetBase ("192.168.1.0", "255.255.255.0");
84  Ipv4InterfaceContainer addresses = ip.Assign (devs);
85 
86  NS_LOG_INFO ("Create Source");
87  // IP protocol configuration
88  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
89  InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
90  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
91  onoff.SetConstantRate (DataRate (dataRate*1000));
92  onoff.SetAttribute ("PacketSize", UintegerValue (1250));
93 
94  ApplicationContainer apps = onoff.Install (c.Get (0));
95  apps.Start (Seconds (0.5));
96  apps.Stop (Seconds (11));
97 
98  NS_LOG_INFO ("Create Sink.");
99  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
100  apps = sink.Install (c.Get (3));
101  apps.Start (Seconds (0.0));
102  apps.Stop (Seconds (12.0));
103 
104  NS_LOG_INFO ("Configure Tracing.");
105  // first, pcap tracing in non-promiscuous mode
106  csma.EnablePcapAll ("csma-raw-ip-socket", false);
107  // then, print what the packet sink receives.
108  Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
109  MakeCallback (&SinkRx));
110 
112 
113 
114  NS_LOG_INFO ("Run Simulation.");
115  Simulator::Run ();
117  NS_LOG_INFO ("Done.");
118 }