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()
75ClickInternetStackHelper::~ClickInternetStackHelper()
79ClickInternetStackHelper::ClickInternetStackHelper(
const ClickInternetStackHelper& o)
81 m_ipv4Enabled = o.m_ipv4Enabled;
84ClickInternetStackHelper&
85ClickInternetStackHelper::operator=(
const ClickInternetStackHelper& o)
95ClickInternetStackHelper::Reset()
102ClickInternetStackHelper::SetClickFile(NodeContainer c, std::string clickfile)
104 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
106 SetClickFile(*i, clickfile);
111ClickInternetStackHelper::SetClickFile(Ptr<Node> node, std::string clickfile)
113 m_nodeToClickFileMap.insert(std::make_pair(node, clickfile));
117ClickInternetStackHelper::SetDefines(NodeContainer c, std::map<std::string, std::string> defines)
119 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
121 SetDefines(*i, defines);
126ClickInternetStackHelper::SetDefines(Ptr<Node> node, std::map<std::string, std::string> defines)
128 m_nodeToDefinesMap.insert(std::make_pair(node, defines));
132ClickInternetStackHelper::SetRoutingTableElement(NodeContainer c, std::string rt)
134 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
136 SetRoutingTableElement(*i, rt);
141ClickInternetStackHelper::SetRoutingTableElement(Ptr<Node> node, std::string rt)
143 m_nodeToRoutingTableElementMap.insert(std::make_pair(node, rt));
147ClickInternetStackHelper::Install(NodeContainer c)
const
149 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
156ClickInternetStackHelper::InstallAll()
const
158 Install(NodeContainer::GetGlobal());
162ClickInternetStackHelper::CreateAndAggregateObjectFromTypeId(Ptr<Node> node,
163 const std::string typeId)
165 ObjectFactory factory;
166 factory.SetTypeId(typeId);
167 Ptr<Object> protocol = factory.Create<Object>();
168 node->AggregateObject(protocol);
172ClickInternetStackHelper::Install(Ptr<Node> node)
const
176 if (node->GetObject<Ipv4>())
178 NS_FATAL_ERROR(
"ClickInternetStackHelper::Install (): Aggregating "
179 "an InternetStack to a node with an existing Ipv4 object");
183 CreateAndAggregateObjectFromTypeId(node,
"ns3::ArpL3Protocol");
184 CreateAndAggregateObjectFromTypeId(node,
"ns3::Ipv4L3ClickProtocol");
185 CreateAndAggregateObjectFromTypeId(node,
"ns3::Icmpv4L4Protocol");
186 CreateAndAggregateObjectFromTypeId(node,
"ns3::UdpL4Protocol");
187 CreateAndAggregateObjectFromTypeId(node,
"ns3::TcpL4Protocol");
188 Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory>();
189 node->AggregateObject(factory);
191 Ptr<Ipv4>
ipv4 = node->GetObject<Ipv4>();
192 Ptr<Ipv4ClickRouting> ipv4Routing = CreateObject<Ipv4ClickRouting>();
193 std::map<Ptr<Node>, std::string>::const_iterator it;
194 it = m_nodeToClickFileMap.find(node);
196 if (it != m_nodeToClickFileMap.end())
198 ipv4Routing->SetClickFile(it->second);
201 std::map<Ptr<Node>, std::map<std::string, std::string>>::const_iterator definesIt;
202 definesIt = m_nodeToDefinesMap.find(node);
203 if (definesIt != m_nodeToDefinesMap.end())
205 ipv4Routing->SetDefines(definesIt->second);
208 it = m_nodeToRoutingTableElementMap.find(node);
209 if (it != m_nodeToRoutingTableElementMap.end())
211 ipv4Routing->SetClickRoutingTableElement(it->second);
213 ipv4->SetRoutingProtocol(ipv4Routing);
214 node->AggregateObject(ipv4Routing);
219ClickInternetStackHelper::Install(std::string nodeName)
const
221 Ptr<Node> node = Names::Find<Node>(nodeName);
226Ipv4L3ProtocolRxTxSink(Ptr<const Packet> p, Ptr<Ipv4> ipv4,
uint32_t interface)
237 if (g_interfaceFileMapIpv4.find(pair) == g_interfaceFileMapIpv4.end())
239 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
243 Ptr<PcapFileWrapper>
file = g_interfaceFileMapIpv4[pair];
244 file->Write(Simulator::Now(), p);
248ClickInternetStackHelper::PcapHooked(Ptr<Ipv4> ipv4)
250 for (InterfaceFileMapIpv4::const_iterator i = g_interfaceFileMapIpv4.begin();
251 i != g_interfaceFileMapIpv4.end();
254 if ((*i).first.first == ipv4)
263ClickInternetStackHelper::EnablePcapIpv4Internal(std::string prefix,
266 bool explicitFilename)
272 NS_LOG_INFO(
"Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
280 PcapHelper pcapHelper;
282 std::string filename;
283 if (explicitFilename)
289 filename = pcapHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
292 Ptr<PcapFileWrapper>
file = pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_RAW);
298 if (!PcapHooked(ipv4))
304 Ptr<Ipv4L3Protocol> ipv4L3Protocol =
ipv4->GetObject<Ipv4L3Protocol>();
306 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
307 "m_ipv4Enabled and ipv4L3Protocol inconsistent");
310 ipv4L3Protocol->TraceConnectWithoutContext(
"Tx",
MakeCallback(&Ipv4L3ProtocolRxTxSink));
312 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
313 "Unable to connect ipv4L3Protocol \"Tx\"");
316 ipv4L3Protocol->TraceConnectWithoutContext(
"Rx",
MakeCallback(&Ipv4L3ProtocolRxTxSink));
318 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
319 "Unable to connect ipv4L3Protocol \"Rx\"");
322 g_interfaceFileMapIpv4[std::make_pair(ipv4, interface)] =
file;
326Ipv4L3ProtocolDropSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
327 const Ipv4Header& header,
328 Ptr<const Packet> packet,
329 Ipv4L3Protocol::DropReason reason,
340 if (g_interfaceStreamMapIpv4.find(pair) == g_interfaceStreamMapIpv4.end())
342 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
346 Ptr<Packet> p = packet->Copy();
347 p->AddHeader(header);
348 *stream->GetStream() <<
"d " << Simulator::Now().GetSeconds() <<
" " << *p << std::endl;
352Ipv4L3ProtocolDropSinkWithContext(Ptr<OutputStreamWrapper> stream,
354 const Ipv4Header& header,
355 Ptr<const Packet> packet,
356 Ipv4L3Protocol::DropReason reason,
367 if (g_interfaceStreamMapIpv4.find(pair) == g_interfaceStreamMapIpv4.end())
369 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
373 Ptr<Packet> p = packet->Copy();
374 p->AddHeader(header);
375#ifdef INTERFACE_CONTEXT
376 *stream->GetStream() <<
"d " << Simulator::Now().GetSeconds() <<
" " << context <<
"("
377 <<
interface << ") " << *p << std::endl;
379 *stream->GetStream() <<
"d " << Simulator::Now().GetSeconds() <<
" " << context <<
" " << *p
385ClickInternetStackHelper::AsciiHooked(Ptr<Ipv4> ipv4)
387 for (InterfaceStreamMapIpv4::const_iterator i = g_interfaceStreamMapIpv4.begin();
388 i != g_interfaceStreamMapIpv4.end();
391 if ((*i).first.first == ipv4)
400ClickInternetStackHelper::EnableAsciiIpv4Internal(Ptr<OutputStreamWrapper> stream,
404 bool explicitFilename)
408 NS_LOG_INFO(
"Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
416 Packet::EnablePrinting();
435 AsciiTraceHelper asciiTraceHelper;
437 std::string filename;
438 if (explicitFilename)
444 filename = asciiTraceHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
447 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
453 if (!AsciiHooked(ipv4))
460 Ptr<ArpL3Protocol> arpL3Protocol =
ipv4->GetObject<ArpL3Protocol>();
461 asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol>(arpL3Protocol,
471 Ptr<Ipv4L3Protocol> ipv4L3Protocol =
ipv4->GetObject<Ipv4L3Protocol>();
472 bool result = ipv4L3Protocol->TraceConnectWithoutContext(
476 "ClickInternetStackHelper::EnableAsciiIpv4Internal(): "
477 "Unable to connect ipv4L3Protocol \"Drop\"");
480 g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = theStream;
496 if (!AsciiHooked(ipv4))
498 Ptr<Node> node =
ipv4->GetObject<Node>();
499 std::ostringstream oss;
507 oss <<
"/NodeList/" << node->GetId() <<
"/$ns3::ArpL3Protocol/Drop";
508 Config::Connect(oss.str(),
516 oss <<
"/NodeList/" << node->GetId() <<
"/$ns3::Ipv4L3Protocol/Drop";
517 Config::Connect(oss.str(),
MakeBoundCallback(&Ipv4L3ProtocolDropSinkWithContext, stream));
520 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.