A Discrete-Event Network Simulator
API
socket-options-ipv6.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/applications-module.h"
26#include "ns3/core-module.h"
27#include "ns3/csma-module.h"
28#include "ns3/internet-module.h"
29#include "ns3/network-module.h"
30
31#include <fstream>
32
33using namespace ns3;
34
35NS_LOG_COMPONENT_DEFINE("SocketOptionsIpv6");
36
37void
39{
40 NS_LOG_INFO("Received one packet!");
41 Ptr<Packet> packet = socket->Recv();
42 SocketIpv6TclassTag tclassTag;
43 if (packet->RemovePacketTag(tclassTag))
44 {
45 NS_LOG_INFO(" TCLASS = " << (uint32_t)tclassTag.GetTclass());
46 }
47 SocketIpv6HopLimitTag hoplimitTag;
48 if (packet->RemovePacketTag(hoplimitTag))
49 {
50 NS_LOG_INFO(" HOPLIMIT = " << (uint32_t)hoplimitTag.GetHopLimit());
51 }
52}
53
54static void
55SendPacket(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
56{
57 if (pktCount > 0)
58 {
59 socket->Send(Create<Packet>(pktSize));
60 Simulator::Schedule(pktInterval, &SendPacket, 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 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 NS_LOG_INFO("Assign IP Addresses.");
113 ipv6.SetBase("2001:0000:f00d:cafe::", Ipv6Prefix(64));
114 Ipv6InterfaceContainer i6 = ipv6.Assign(d);
115 serverAddress = Address(i6.GetAddress(1, 1));
116
117 NS_LOG_INFO("Create sockets.");
118 // Receiver socket on n1
119 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
120 Ptr<Socket> recvSink = Socket::CreateSocket(n.Get(1), tid);
121 Inet6SocketAddress local = Inet6SocketAddress(Ipv6Address::GetAny(), 4477);
122 recvSink->SetIpv6RecvTclass(ipv6RecvTclass);
123 recvSink->SetIpv6RecvHopLimit(ipv6RecvHoplimit);
124 recvSink->Bind(local);
126
127 // Sender socket on n0
128 Ptr<Socket> source = Socket::CreateSocket(n.Get(0), tid);
129 Inet6SocketAddress remote = Inet6SocketAddress(i6.GetAddress(1, 1), 4477);
130 // Set socket options, it is also possible to set the options after the socket has been
131 // 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);
149 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
150 Seconds(1.0),
151 &SendPacket,
152 source,
154 packetCount,
155 interPacketInterval);
156
157 NS_LOG_INFO("Run Simulation.");
158 Simulator::Run();
159 Simulator::Destroy();
160 NS_LOG_INFO("Done.");
161
162 return 0;
163}
a polymophic address class
Definition: address.h:100
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
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
AttributeValue implementation for DataRate.
An Inet6 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...
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
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:986
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual void SetIpv6HopLimit(uint8_t ipHopLimit)
Manually set IPv6 Hop Limit.
Definition: socket.cc:533
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetIpv6RecvHopLimit(bool ipv6RecvHopLimit)
Tells a socket to pass information about IPv6 Hop Limit up the stack.
Definition: socket.cc:546
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
void SetIpv6Tclass(int ipTclass)
Manually set IPv6 Traffic Class field.
Definition: socket.cc:466
void SetIpv6RecvTclass(bool ipv6RecvTclass)
Tells a socket to pass information about IPv6 Traffic Class up the stack.
Definition: socket.cc:496
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer.
Definition: socket.h:1170
uint8_t GetHopLimit() const
Get the tag's Hop Limit.
Definition: socket.cc:672
indicates whether the socket has IPV6_TCLASS set.
Definition: socket.h:1364
uint8_t GetTclass() const
Get the tag's Tclass.
Definition: socket.cc:914
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:59
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:1336
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1348
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:707
csma
Definition: second.py:56
cmd
Definition: second.py:33
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.