A Discrete-Event Network Simulator
API
socket-options-ipv4.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // Network topology
17 //
18 // n0 n1
19 // | |
20 // =================
21 // LAN
22 //
23 // - UDP flows from n0 to n1
24 
25 #include "ns3/core-module.h"
26 #include "ns3/csma-module.h"
27 #include "ns3/internet-module.h"
28 #include "ns3/network-module.h"
29 
30 #include <fstream>
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE("SocketOptionsIpv4");
35 
36 void
38 {
39  NS_LOG_INFO("Received one packet!");
40  Ptr<Packet> packet = socket->Recv();
41  SocketIpTosTag tosTag;
42  if (packet->RemovePacketTag(tosTag))
43  {
44  NS_LOG_INFO(" TOS = " << (uint32_t)tosTag.GetTos());
45  }
46  SocketIpTtlTag ttlTag;
47  if (packet->RemovePacketTag(ttlTag))
48  {
49  NS_LOG_INFO(" TTL = " << (uint32_t)ttlTag.GetTtl());
50  }
51 }
52 
53 static void
54 SendPacket(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
55 {
56  if (pktCount > 0)
57  {
58  socket->Send(Create<Packet>(pktSize));
59  Simulator::Schedule(pktInterval, &SendPacket, socket, pktSize, pktCount - 1, pktInterval);
60  }
61  else
62  {
63  socket->Close();
64  }
65 }
66 
67 int
68 main(int argc, char* argv[])
69 {
70  //
71  // Allow the user to override any of the defaults and the above Bind() at
72  // run-time, via command-line arguments
73  //
74  uint32_t packetSize = 1024;
75  uint32_t packetCount = 10;
76  double packetInterval = 1.0;
77 
78  // Socket options for IPv4, currently TOS, TTL, RECVTOS, and RECVTTL
79  uint32_t ipTos = 0;
80  bool ipRecvTos = true;
81  uint32_t ipTtl = 0;
82  bool ipRecvTtl = true;
83 
84  CommandLine cmd(__FILE__);
85  cmd.AddValue("PacketSize", "Packet size in bytes", packetSize);
86  cmd.AddValue("PacketCount", "Number of packets to send", packetCount);
87  cmd.AddValue("Interval", "Interval between packets", packetInterval);
88  cmd.AddValue("IP_TOS", "IP_TOS", ipTos);
89  cmd.AddValue("IP_RECVTOS", "IP_RECVTOS", ipRecvTos);
90  cmd.AddValue("IP_TTL", "IP_TTL", ipTtl);
91  cmd.AddValue("IP_RECVTTL", "IP_RECVTTL", ipRecvTtl);
92  cmd.Parse(argc, argv);
93 
94  NS_LOG_INFO("Create nodes.");
95  NodeContainer n;
96  n.Create(2);
97 
99  internet.Install(n);
100 
101  Address serverAddress;
102 
103  NS_LOG_INFO("Create channels.");
105  csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
106  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
107  csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
108  NetDeviceContainer d = csma.Install(n);
109 
110  NS_LOG_INFO("Assign IP Addresses.");
112  ipv4.SetBase("10.1.1.0", "255.255.255.0");
113  Ipv4InterfaceContainer i = ipv4.Assign(d);
114  serverAddress = Address(i.GetAddress(1));
115 
116  NS_LOG_INFO("Create sockets.");
117  // Receiver socket on n1
118  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
119  Ptr<Socket> recvSink = Socket::CreateSocket(n.Get(1), tid);
121  recvSink->SetIpRecvTos(ipRecvTos);
122  recvSink->SetIpRecvTtl(ipRecvTtl);
123  recvSink->Bind(local);
125 
126  // Sender socket on n0
127  Ptr<Socket> source = Socket::CreateSocket(n.Get(0), tid);
128  InetSocketAddress remote = InetSocketAddress(i.GetAddress(1), 4477);
129 
130  // Set socket options, it is also possible to set the options after the socket has been
131  // created/connected.
132  if (ipTos > 0)
133  {
134  source->SetIpTos(ipTos);
135  }
136 
137  if (ipTtl > 0)
138  {
139  source->SetIpTtl(ipTtl);
140  }
141  source->Connect(remote);
142 
143  AsciiTraceHelper ascii;
144  csma.EnableAsciiAll(ascii.CreateFileStream("socket-options-ipv4.tr"));
145  csma.EnablePcapAll("socket-options-ipv4", false);
146 
147  // Schedule SendPacket
148  Time interPacketInterval = Seconds(packetInterval);
150  Seconds(1.0),
151  &SendPacket,
152  source,
153  packetSize,
154  packetCount,
155  interPacketInterval);
156 
157  NS_LOG_INFO("Run Simulation.");
158  Simulator::Run();
160  NS_LOG_INFO("Done.");
161 
162  return 0;
163 }
a polymophic address class
Definition: address.h:101
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetId() const
Definition: node.cc:117
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:967
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual void SetIpTtl(uint8_t ipTtl)
Manually set IP Time to Live field.
Definition: socket.cc:510
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition: socket.cc:434
void SetIpRecvTos(bool ipv4RecvTos)
Tells a socket to pass information about IP Type of Service up the stack.
Definition: socket.cc:456
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetIpRecvTtl(bool ipv4RecvTtl)
Tells a socket to pass information about IP_TTL up the stack.
Definition: socket.cc:523
indicates whether the socket has IP_TOS set.
Definition: socket.h:1269
uint8_t GetTos() const
Get the tag's TOS.
Definition: socket.cc:804
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer.
Definition: socket.h:1122
uint8_t GetTtl() const
Get the tag's TTL.
Definition: socket.cc:611
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
csma
Definition: second.py:63
cmd
Definition: second.py:40
void ReceivePacket(Ptr< Socket > socket)
static void SendPacket(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
uint32_t pktSize
packet size used for the simulation (in bytes)
static const uint32_t packetSize
Packet size generated at the AP.