A Discrete-Event Network Simulator
API
socket-options-ipv4.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// Network topology
18//
19// n0 n1
20// | |
21// =================
22// LAN
23//
24// - UDP flows from n0 to n1
25
26#include <fstream>
27#include "ns3/core-module.h"
28#include "ns3/csma-module.h"
29#include "ns3/applications-module.h"
30#include "ns3/internet-module.h"
31#include "ns3/network-module.h"
32
33using namespace ns3;
34
35NS_LOG_COMPONENT_DEFINE ("SocketOptionsIpv4");
36
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
54 uint32_t pktCount, Time pktInterval )
55{
56 if (pktCount > 0)
57 {
58 socket->Send (Create<Packet> (pktSize));
59 Simulator::Schedule (pktInterval, &SendPacket,
60 socket, pktSize,pktCount - 1, pktInterval);
61 }
62 else
63 {
64 socket->Close ();
65 }
66}
67
68int
69main (int argc, char *argv[])
70{
71//
72// Allow the user to override any of the defaults and the above Bind() at
73// run-time, via command-line arguments
74//
75 uint32_t packetSize = 1024;
76 uint32_t packetCount = 10;
77 double packetInterval = 1.0;
78
79 //Socket options for IPv4, currently TOS, TTL, RECVTOS, and RECVTTL
80 uint32_t ipTos = 0;
81 bool ipRecvTos = true;
82 uint32_t ipTtl = 0;
83 bool ipRecvTtl = true;
84
85 CommandLine cmd (__FILE__);
86 cmd.AddValue ("PacketSize", "Packet size in bytes", packetSize);
87 cmd.AddValue ("PacketCount", "Number of packets to send", packetCount);
88 cmd.AddValue ("Interval", "Interval between packets", packetInterval);
89 cmd.AddValue ("IP_TOS", "IP_TOS", ipTos);
90 cmd.AddValue ("IP_RECVTOS", "IP_RECVTOS", ipRecvTos);
91 cmd.AddValue ("IP_TTL", "IP_TTL", ipTtl);
92 cmd.AddValue ("IP_RECVTTL", "IP_RECVTTL", ipRecvTtl);
93 cmd.Parse (argc, argv);
94
95 NS_LOG_INFO ("Create nodes.");
97 n.Create (2);
98
99 InternetStackHelper internet;
100 internet.Install (n);
101
102 Address serverAddress;
103
104 NS_LOG_INFO ("Create channels.");
106 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
107 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
108 csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
109 NetDeviceContainer d = csma.Install (n);
110
111
112 NS_LOG_INFO ("Assign IP Addresses.");
114 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
115 Ipv4InterfaceContainer i = ipv4.Assign (d);
116 serverAddress = Address(i.GetAddress (1));
117
118 NS_LOG_INFO ("Create sockets.");
119 //Receiver socket on n1
120 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
121 Ptr<Socket> recvSink = Socket::CreateSocket (n.Get (1), tid);
122 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 4477);
123 recvSink->SetIpRecvTos (ipRecvTos);
124 recvSink->SetIpRecvTtl (ipRecvTtl);
125 recvSink->Bind (local);
127
128 //Sender socket on n0
129 Ptr<Socket> source = Socket::CreateSocket (n.Get (0), tid);
130 InetSocketAddress remote = InetSocketAddress (i.GetAddress (1), 4477);
131
132 //Set socket options, it is also possible to set the options after the socket has been created/connected.
133 if (ipTos > 0)
134 {
135 source->SetIpTos (ipTos);
136 }
137
138 if (ipTtl > 0)
139 {
140 source->SetIpTtl (ipTtl);
141 }
142 source->Connect (remote);
143
144 AsciiTraceHelper ascii;
145 csma.EnableAsciiAll (ascii.CreateFileStream ("socket-options-ipv4.tr"));
146 csma.EnablePcapAll ("socket-options-ipv4", false);
147
148 //Schedule SendPacket
149 Time interPacketInterval = Seconds (packetInterval);
150 Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
151 Seconds (1.0), &SendPacket,
152 source, packetSize, packetCount, interPacketInterval);
153
154 NS_LOG_INFO ("Run Simulation.");
156 Simulator::Destroy ();
157 NS_LOG_INFO ("Done.");
158}
void Run(ObjectFactory &factory, uint32_t pop, uint32_t total, uint32_t runs, Ptr< RandomVariableStream > eventStream, bool calRev)
Perform the runs for a single scheduler type.
a polymophic address class
Definition: address.h:91
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
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(void) const
Definition: node.cc:109
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
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:513
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition: socket.cc:437
void SetIpRecvTos(bool ipv4RecvTos)
Tells a socket to pass information about IP Type of Service up the stack.
Definition: socket.cc:459
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Close(void)=0
Close a socket.
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 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:526
indicates whether the socket has IP_TOS set.
Definition: socket.h:1263
uint8_t GetTos(void) const
Get the tag's TOS.
Definition: socket.cc:791
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer.
Definition: socket.h:1117
uint8_t GetTtl(void) const
Get the tag's TTL.
Definition: socket.cc:611
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
AttributeValue implementation for Time.
Definition: nstime.h:1309
a unique identifier for an interface.
Definition: type-id.h:59
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1245
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1253
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
csma
Definition: second.py:67
cmd
Definition: second.py:43
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)
Definition: wifi-bianchi.cc:89
static const uint32_t packetSize
Packet size generated at the AP.