26#include "ns3/arp-l3-protocol.h"
27#include "ns3/assert.h"
28#include "ns3/callback.h"
29#include "ns3/config.h"
30#include "ns3/core-config.h"
31#include "ns3/ipv4-click-routing.h"
32#include "ns3/ipv4-l3-click-protocol.h"
36#include "ns3/net-device.h"
38#include "ns3/object.h"
39#include "ns3/packet-socket-factory.h"
40#include "ns3/simulator.h"
41#include "ns3/string.h"
42#include "ns3/trace-helper.h"
52#define INTERFACE_CONTEXT
63ClickInternetStackHelper::ClickInternetStackHelper()
71ClickInternetStackHelper::Initialize()
73 SetTcp(
"ns3::TcpL4Protocol");
76ClickInternetStackHelper::~ClickInternetStackHelper()
80ClickInternetStackHelper::ClickInternetStackHelper(
const ClickInternetStackHelper& o)
82 m_ipv4Enabled = o.m_ipv4Enabled;
83 m_tcpFactory = o.m_tcpFactory;
86ClickInternetStackHelper&
87ClickInternetStackHelper::operator=(
const ClickInternetStackHelper& o)
97ClickInternetStackHelper::Reset()
104ClickInternetStackHelper::SetTcp(
const std::string tid)
106 m_tcpFactory.SetTypeId(tid);
110ClickInternetStackHelper::SetTcp(std::string tid, std::string n0,
const AttributeValue& v0)
112 m_tcpFactory.SetTypeId(tid);
113 m_tcpFactory.Set(n0, v0);
117ClickInternetStackHelper::SetClickFile(NodeContainer c, std::string clickfile)
119 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
121 SetClickFile(*i, clickfile);
126ClickInternetStackHelper::SetClickFile(Ptr<Node> node, std::string clickfile)
128 m_nodeToClickFileMap.insert(std::make_pair(node, clickfile));
132ClickInternetStackHelper::SetDefines(NodeContainer c, std::map<std::string, std::string> defines)
134 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
136 SetDefines(*i, defines);
141ClickInternetStackHelper::SetDefines(Ptr<Node> node, std::map<std::string, std::string> defines)
143 m_nodeToDefinesMap.insert(std::make_pair(node, defines));
147ClickInternetStackHelper::SetRoutingTableElement(NodeContainer c, std::string rt)
149 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
151 SetRoutingTableElement(*i, rt);
156ClickInternetStackHelper::SetRoutingTableElement(Ptr<Node> node, std::string rt)
158 m_nodeToRoutingTableElementMap.insert(std::make_pair(node, rt));
162ClickInternetStackHelper::Install(NodeContainer c)
const
164 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
171ClickInternetStackHelper::InstallAll()
const
173 Install(NodeContainer::GetGlobal());
177ClickInternetStackHelper::CreateAndAggregateObjectFromTypeId(Ptr<Node> node,
178 const std::string typeId)
180 ObjectFactory factory;
181 factory.SetTypeId(typeId);
182 Ptr<Object> protocol = factory.Create<Object>();
183 node->AggregateObject(protocol);
187ClickInternetStackHelper::Install(Ptr<Node> node)
const
191 if (node->GetObject<Ipv4>())
193 NS_FATAL_ERROR(
"ClickInternetStackHelper::Install (): Aggregating "
194 "an InternetStack to a node with an existing Ipv4 object");
198 CreateAndAggregateObjectFromTypeId(node,
"ns3::ArpL3Protocol");
199 CreateAndAggregateObjectFromTypeId(node,
"ns3::Ipv4L3ClickProtocol");
200 CreateAndAggregateObjectFromTypeId(node,
"ns3::Icmpv4L4Protocol");
201 CreateAndAggregateObjectFromTypeId(node,
"ns3::UdpL4Protocol");
202 node->AggregateObject(m_tcpFactory.Create<Object>());
203 Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory>();
204 node->AggregateObject(factory);
206 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
207 Ptr<Ipv4ClickRouting> ipv4Routing = CreateObject<Ipv4ClickRouting>();
208 std::map<Ptr<Node>, std::string>::const_iterator it;
209 it = m_nodeToClickFileMap.find(node);
211 if (it != m_nodeToClickFileMap.end())
213 ipv4Routing->SetClickFile(it->second);
216 std::map<Ptr<Node>, std::map<std::string, std::string>>::const_iterator definesIt;
217 definesIt = m_nodeToDefinesMap.find(node);
218 if (definesIt != m_nodeToDefinesMap.end())
220 ipv4Routing->SetDefines(definesIt->second);
223 it = m_nodeToRoutingTableElementMap.find(node);
224 if (it != m_nodeToRoutingTableElementMap.end())
226 ipv4Routing->SetClickRoutingTableElement(it->second);
228 ipv4->SetRoutingProtocol(ipv4Routing);
229 node->AggregateObject(ipv4Routing);
234ClickInternetStackHelper::Install(std::string nodeName)
const
236 Ptr<Node> node = Names::Find<Node>(nodeName);
241Ipv4L3ProtocolRxTxSink(Ptr<const Packet> p, Ptr<Ipv4> ipv4,
uint32_t interface)
252 if (g_interfaceFileMapIpv4.find(pair) == g_interfaceFileMapIpv4.end())
254 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
258 Ptr<PcapFileWrapper>
file = g_interfaceFileMapIpv4[pair];
259 file->Write(Simulator::Now(), p);
263ClickInternetStackHelper::PcapHooked(Ptr<Ipv4> ipv4)
265 for (InterfaceFileMapIpv4::const_iterator i = g_interfaceFileMapIpv4.begin();
266 i != g_interfaceFileMapIpv4.end();
269 if ((*i).first.first == ipv4)
278ClickInternetStackHelper::EnablePcapIpv4Internal(std::string prefix,
281 bool explicitFilename)
287 NS_LOG_INFO(
"Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
295 PcapHelper pcapHelper;
297 std::string filename;
298 if (explicitFilename)
304 filename = pcapHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
307 Ptr<PcapFileWrapper>
file = pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_RAW);
313 if (!PcapHooked(ipv4))
319 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
321 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
322 "m_ipv4Enabled and ipv4L3Protocol inconsistent");
325 ipv4L3Protocol->TraceConnectWithoutContext(
"Tx",
MakeCallback(&Ipv4L3ProtocolRxTxSink));
327 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
328 "Unable to connect ipv4L3Protocol \"Tx\"");
331 ipv4L3Protocol->TraceConnectWithoutContext(
"Rx",
MakeCallback(&Ipv4L3ProtocolRxTxSink));
333 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
334 "Unable to connect ipv4L3Protocol \"Rx\"");
337 g_interfaceFileMapIpv4[std::make_pair(ipv4, interface)] =
file;
341Ipv4L3ProtocolDropSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
342 const Ipv4Header& header,
343 Ptr<const Packet> packet,
344 Ipv4L3Protocol::DropReason reason,
355 if (g_interfaceStreamMapIpv4.find(pair) == g_interfaceStreamMapIpv4.end())
357 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
361 Ptr<Packet> p = packet->Copy();
362 p->AddHeader(header);
363 *stream->GetStream() <<
"d " << Simulator::Now().GetSeconds() <<
" " << *p << std::endl;
367Ipv4L3ProtocolDropSinkWithContext(Ptr<OutputStreamWrapper> stream,
369 const Ipv4Header& header,
370 Ptr<const Packet> packet,
371 Ipv4L3Protocol::DropReason reason,
382 if (g_interfaceStreamMapIpv4.find(pair) == g_interfaceStreamMapIpv4.end())
384 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
388 Ptr<Packet> p = packet->Copy();
389 p->AddHeader(header);
390#ifdef INTERFACE_CONTEXT
391 *stream->GetStream() <<
"d " << Simulator::Now().GetSeconds() <<
" " << context <<
"("
392 <<
interface << ") " << *p << std::endl;
394 *stream->GetStream() <<
"d " << Simulator::Now().GetSeconds() <<
" " << context <<
" " << *p
400ClickInternetStackHelper::AsciiHooked(Ptr<Ipv4> ipv4)
402 for (InterfaceStreamMapIpv4::const_iterator i = g_interfaceStreamMapIpv4.begin();
403 i != g_interfaceStreamMapIpv4.end();
406 if ((*i).first.first == ipv4)
415ClickInternetStackHelper::EnableAsciiIpv4Internal(Ptr<OutputStreamWrapper> stream,
419 bool explicitFilename)
423 NS_LOG_INFO(
"Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
431 Packet::EnablePrinting();
450 AsciiTraceHelper asciiTraceHelper;
452 std::string filename;
453 if (explicitFilename)
459 filename = asciiTraceHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
462 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
468 if (!AsciiHooked(ipv4))
475 Ptr<ArpL3Protocol> arpL3Protocol = ipv4->GetObject<ArpL3Protocol>();
476 asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol>(arpL3Protocol,
486 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
487 bool result = ipv4L3Protocol->TraceConnectWithoutContext(
491 "ClickInternetStackHelper::EanableAsciiIpv4Internal(): "
492 "Unable to connect ipv4L3Protocol \"Drop\"");
495 g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = theStream;
511 if (!AsciiHooked(ipv4))
513 Ptr<Node> node = ipv4->GetObject<Node>();
514 std::ostringstream oss;
522 oss <<
"/NodeList/" << node->GetId() <<
"/$ns3::ArpL3Protocol/Drop";
523 Config::Connect(oss.str(),
531 oss <<
"/NodeList/" << node->GetId() <<
"/$ns3::Ipv4L3Protocol/Drop";
532 Config::Connect(oss.str(),
MakeBoundCallback(&Ipv4L3ProtocolDropSinkWithContext, stream));
535 g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = stream;
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
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...
std::map< InterfacePairIpv4, Ptr< OutputStreamWrapper > > InterfaceStreamMapIpv4
Ipv4/interface and output stream container.
std::map< InterfacePairIpv4, Ptr< PcapFileWrapper > > InterfaceFileMapIpv4
Ipv4/interface and Pcap file wrapper container.
std::pair< uint32_t, uint32_t > InterfacePairIpv4
Ipv4/interface pair.
static InterfaceFileMapIpv4 g_interfaceFileMapIpv4
A mapping of Ipv4/interface pairs to pcap files.
static InterfaceStreamMapIpv4 g_interfaceStreamMapIpv4
A mapping of Ipv4/interface pairs to ascii streams.