A Discrete-Event Network Simulator
API
socket-options-ipv6.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 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("SocketOptionsIpv6");
36 
38 {
39  NS_LOG_INFO ("Received one packet!");
40  Ptr<Packet> packet = socket->Recv ();
41  SocketIpv6TclassTag tclassTag;
42  if (packet->RemovePacketTag (tclassTag))
43  {
44  NS_LOG_INFO (" TCLASS = " << (uint32_t)tclassTag.GetTclass ());
45  }
46  SocketIpv6HopLimitTag hoplimitTag;
47  if (packet->RemovePacketTag (hoplimitTag))
48  {
49  NS_LOG_INFO (" HOPLIMIT = " << (uint32_t)hoplimitTag.GetHopLimit ());
50  }
51 }
52 
53 static void SendPacket (Ptr<Socket> socket, uint32_t pktSize,
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 
68 int
69 main (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 IPv6, currently TCLASS, HOPLIMIT, RECVTCLASS, and RECVHOPLIMIT
80  uint32_t ipv6Tclass = 0;
81  bool ipv6RecvTclass = true;
82  uint32_t ipv6Hoplimit = 0;
83  bool ipv6RecvHoplimit = 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 ("IPV6_TCLASS", "IPV6_TCLASS", ipv6Tclass);
90  cmd.AddValue ("IPV6_RECVTCLASS", "IPV6_RECVTCLASS", ipv6RecvTclass);
91  cmd.AddValue ("IPV6_HOPLIMIT", "IPV6_HOPLIMIT", ipv6Hoplimit);
92  cmd.AddValue ("IPV6_RECVHOPLIMIT", "IPV6_RECVHOPLIMIT", ipv6RecvHoplimit);
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.");
113  Ipv6AddressHelper ipv6;
114  ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64));
115  Ipv6InterfaceContainer i6 = ipv6.Assign (d);
116  serverAddress = Address(i6.GetAddress (1,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);
123  recvSink->SetIpv6RecvTclass (ipv6RecvTclass);
124  recvSink->SetIpv6RecvHopLimit (ipv6RecvHoplimit);
125  recvSink->Bind (local);
127 
128  //Sender socket on n0
129  Ptr<Socket> source = Socket::CreateSocket (n.Get (0), tid);
130  Inet6SocketAddress remote = Inet6SocketAddress (i6.GetAddress (1,1), 4477);
131  //Set socket options, it is also possible to set the options after the socket has been created/connected.
132  if (ipv6Tclass != 0)
133  {
134  source->SetIpv6Tclass (ipv6Tclass);
135  }
136 
137  if (ipv6Hoplimit > 0)
138  {
139  source->SetIpv6HopLimit (ipv6Hoplimit);
140  }
141  source->Connect (remote);
142 
143  AsciiTraceHelper ascii;
144  csma.EnableAsciiAll (ascii.CreateFileStream ("socket-options-ipv6.tr"));
145  csma.EnablePcapAll ("socket-options-ipv6", false);
146 
147  //Schedule SendPacket
148  Time interPacketInterval = Seconds (packetInterval);
150  Seconds (1.0), &SendPacket,
151  source, packetSize, packetCount, interPacketInterval);
152 
153  NS_LOG_INFO ("Run Simulation.");
154  Simulator::Run ();
156  NS_LOG_INFO ("Done.");
157 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::Socket::Bind
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
ns3::Simulator::ScheduleWithContext
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
ns3::Node::GetId
uint32_t GetId(void) const
Definition: node.cc:109
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Socket::SetIpv6RecvTclass
void SetIpv6RecvTclass(bool ipv6RecvTclass)
Tells a socket to pass information about IPv6 Traffic Class up the stack.
Definition: socket.cc:501
ns3::Socket::GetNode
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
SendPacket
static void SendPacket(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Definition: socket-options-ipv6.cc:53
ns3::Ipv6AddressHelper::SetBase
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Definition: ipv6-address-helper.cc:69
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::Ipv6AddressHelper
Helper class to auto-assign global IPv6 unicast addresses.
Definition: ipv6-address-helper.h:83
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
ns3::SocketIpv6TclassTag::GetTclass
uint8_t GetTclass(void) const
Get the tag's Tclass.
Definition: socket.cc:906
ns3::Ipv6AddressHelper::Assign
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Definition: ipv6-address-helper.cc:210
ns3::Ptr< Socket >
ns3::DataRate
Class for representing data rates.
Definition: data-rate.h:89
ns3::SocketIpv6HopLimitTag::GetHopLimit
uint8_t GetHopLimit(void) const
Get the tag's Hop Limit.
Definition: socket.cc:671
ns3::Socket::Send
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
ns3::Socket::SetRecvCallback
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::AsciiTraceHelper::CreateFileStream
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.
Definition: trace-helper.cc:191
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::Ipv6InterfaceContainer::GetAddress
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Definition: ipv6-interface-container.cc:57
ns3::SocketIpv6HopLimitTag
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer.
Definition: socket.h:1165
ns3::Ipv6InterfaceContainer
Keep track of a set of IPv6 interfaces.
Definition: ipv6-interface-container.h:42
ns3::SocketIpv6TclassTag
indicates whether the socket has IPV6_TCLASS set.
Definition: socket.h:1356
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::Socket::Close
virtual int Close(void)=0
Close a socket.
second.cmd
cmd
Definition: second.py:35
ns3::MakeCallback
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:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::Socket::SetIpv6RecvHopLimit
void SetIpv6RecvHopLimit(bool ipv6RecvHopLimit)
Tells a socket to pass information about IPv6 Hop Limit up the stack.
Definition: socket.cc:551
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
packetSize
static const uint32_t packetSize
Definition: wifi-power-adaptation-distance.cc:113
ns3::Socket::Connect
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Inet6SocketAddress
An Inet6 address class.
Definition: inet6-socket-address.h:37
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::Socket::SetIpv6Tclass
void SetIpv6Tclass(int ipTclass)
Manually set IPv6 Traffic Class field.
Definition: socket.cc:471
ns3::Ipv6Address::GetAny
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Definition: ipv6-address.cc:897
pktSize
uint32_t pktSize
packet size used for the simulation (in bytes)
Definition: wifi-bianchi.cc:86
ReceivePacket
void ReceivePacket(Ptr< Socket > socket)
Definition: socket-options-ipv6.cc:37
ns3::Packet::RemovePacketTag
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Socket::SetIpv6HopLimit
virtual void SetIpv6HopLimit(uint8_t ipHopLimit)
Manually set IPv6 Hop Limit.
Definition: socket.cc:538
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Socket::CreateSocket
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:71
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
second.csma
csma
Definition: second.py:63
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
ns3::Socket::Recv
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.