A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
socket-options-ipv6.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Network topology
6//
7// n0 n1
8// | |
9// =================
10// LAN
11//
12// - UDP flows from n0 to n1
13
14#include "ns3/core-module.h"
15#include "ns3/csma-module.h"
16#include "ns3/internet-module.h"
17#include "ns3/network-module.h"
18
19#include <fstream>
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("SocketOptionsIpv6");
24
25void
27{
28 NS_LOG_INFO("Received one packet!");
29 Ptr<Packet> packet = socket->Recv();
30 SocketIpv6TclassTag tclassTag;
31 if (packet->RemovePacketTag(tclassTag))
32 {
33 NS_LOG_INFO(" TCLASS = " << (uint32_t)tclassTag.GetTclass());
34 }
35 SocketIpv6HopLimitTag hoplimitTag;
36 if (packet->RemovePacketTag(hoplimitTag))
37 {
38 NS_LOG_INFO(" HOPLIMIT = " << (uint32_t)hoplimitTag.GetHopLimit());
39 }
40}
41
42static void
43SendPacket(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
44{
45 if (pktCount > 0)
46 {
47 socket->Send(Create<Packet>(pktSize));
48 Simulator::Schedule(pktInterval, &SendPacket, socket, pktSize, pktCount - 1, pktInterval);
49 }
50 else
51 {
52 socket->Close();
53 }
54}
55
56int
57main(int argc, char* argv[])
58{
59 //
60 // Allow the user to override any of the defaults and the above Bind() at
61 // run-time, via command-line arguments
62 //
63 uint32_t packetSize = 1024;
64 uint32_t packetCount = 10;
65 double packetInterval = 1.0;
66
67 // Socket options for IPv6, currently TCLASS, HOPLIMIT, RECVTCLASS, and RECVHOPLIMIT
68 uint32_t ipv6Tclass = 0;
69 bool ipv6RecvTclass = true;
70 uint32_t ipv6Hoplimit = 0;
71 bool ipv6RecvHoplimit = true;
72
73 CommandLine cmd(__FILE__);
74 cmd.AddValue("PacketSize", "Packet size in bytes", packetSize);
75 cmd.AddValue("PacketCount", "Number of packets to send", packetCount);
76 cmd.AddValue("Interval", "Interval between packets", packetInterval);
77 cmd.AddValue("IPV6_TCLASS", "IPV6_TCLASS", ipv6Tclass);
78 cmd.AddValue("IPV6_RECVTCLASS", "IPV6_RECVTCLASS", ipv6RecvTclass);
79 cmd.AddValue("IPV6_HOPLIMIT", "IPV6_HOPLIMIT", ipv6Hoplimit);
80 cmd.AddValue("IPV6_RECVHOPLIMIT", "IPV6_RECVHOPLIMIT", ipv6RecvHoplimit);
81 cmd.Parse(argc, argv);
82
83 NS_LOG_INFO("Create nodes.");
85 n.Create(2);
86
88 internet.Install(n);
89
90 Address serverAddress;
91
92 NS_LOG_INFO("Create channels.");
94 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
95 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
96 csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
97 NetDeviceContainer d = csma.Install(n);
98
99 NS_LOG_INFO("Assign IP Addresses.");
101 ipv6.SetBase("2001:0000:f00d:cafe::", Ipv6Prefix(64));
102 Ipv6InterfaceContainer i6 = ipv6.Assign(d);
103 serverAddress = Address(i6.GetAddress(1, 1));
104
105 NS_LOG_INFO("Create sockets.");
106 // Receiver socket on n1
107 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
108 Ptr<Socket> recvSink = Socket::CreateSocket(n.Get(1), tid);
110 recvSink->SetIpv6RecvTclass(ipv6RecvTclass);
111 recvSink->SetIpv6RecvHopLimit(ipv6RecvHoplimit);
112 recvSink->Bind(local);
113 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
114
115 // Sender socket on n0
116 Ptr<Socket> source = Socket::CreateSocket(n.Get(0), tid);
117 Inet6SocketAddress remote = Inet6SocketAddress(i6.GetAddress(1, 1), 4477);
118 // Set socket options, it is also possible to set the options after the socket has been
119 // created/connected.
120 if (ipv6Tclass != 0)
121 {
122 source->SetIpv6Tclass(ipv6Tclass);
123 }
124
125 if (ipv6Hoplimit > 0)
126 {
127 source->SetIpv6HopLimit(ipv6Hoplimit);
128 }
129 source->Connect(remote);
130
131 AsciiTraceHelper ascii;
132 csma.EnableAsciiAll(ascii.CreateFileStream("socket-options-ipv6.tr"));
133 csma.EnablePcapAll("socket-options-ipv6", false);
134
135 // Schedule SendPacket
136 Time interPacketInterval = Seconds(packetInterval);
137 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
138 Seconds(1.0),
139 &SendPacket,
140 source,
142 packetCount,
143 interPacketInterval);
144
145 NS_LOG_INFO("Run Simulation.");
148 NS_LOG_INFO("Done.");
149
150 return 0;
151}
a polymophic address class
Definition address.h:90
Manage ASCII trace files for device models.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
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.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
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.
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.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
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:61
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer.
Definition socket.h:1161
uint8_t GetHopLimit() const
Get the tag's Hop Limit.
Definition socket.cc:663
indicates whether the socket has IPV6_TCLASS set.
Definition socket.h:1355
uint8_t GetTclass() const
Get the tag's Tclass.
Definition socket.cc:905
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:684
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.