25 #include "ns3/assert.h"
27 #include "ns3/object.h"
28 #include "ns3/names.h"
30 #include "ns3/packet-socket-factory.h"
31 #include "ns3/config.h"
32 #include "ns3/simulator.h"
33 #include "ns3/string.h"
34 #include "ns3/net-device.h"
35 #include "ns3/callback.h"
37 #include "ns3/core-config.h"
38 #include "ns3/arp-l3-protocol.h"
39 #include "ns3/ipv4-click-routing.h"
40 #include "ns3/ipv4-l3-click-protocol.h"
41 #include "ns3/trace-helper.h"
50 #define INTERFACE_CONTEXT
59 ClickInternetStackHelper::ClickInternetStackHelper ()
60 : m_ipv4Enabled (true)
67 ClickInternetStackHelper::Initialize ()
69 SetTcp (
"ns3::TcpL4Protocol");
72 ClickInternetStackHelper::~ClickInternetStackHelper ()
76 ClickInternetStackHelper::ClickInternetStackHelper (
const ClickInternetStackHelper &o)
78 m_ipv4Enabled = o.m_ipv4Enabled;
79 m_tcpFactory = o.m_tcpFactory;
82 ClickInternetStackHelper &
83 ClickInternetStackHelper::operator = (
const ClickInternetStackHelper &o)
100 ClickInternetStackHelper::SetTcp (
const std::string tid)
102 m_tcpFactory.SetTypeId (tid);
106 ClickInternetStackHelper::SetTcp (std::string tid, std::string n0,
const AttributeValue &v0)
108 m_tcpFactory.SetTypeId (tid);
109 m_tcpFactory.Set (n0,v0);
113 ClickInternetStackHelper::SetClickFile (NodeContainer c, std::string clickfile)
115 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
117 SetClickFile (*i, clickfile);
122 ClickInternetStackHelper::SetClickFile (Ptr<Node> node, std::string clickfile)
124 m_nodeToClickFileMap.insert (std::make_pair (node, clickfile));
128 ClickInternetStackHelper::SetDefines (NodeContainer c, std::map<std::string, std::string> defines)
130 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
132 SetDefines (*i, defines);
137 ClickInternetStackHelper::SetDefines (Ptr<Node> node, std::map<std::string, std::string> defines)
139 m_nodeToDefinesMap.insert (std::make_pair (node, defines));
143 ClickInternetStackHelper::SetRoutingTableElement (NodeContainer c, std::string rt)
145 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
147 SetRoutingTableElement (*i, rt);
152 ClickInternetStackHelper::SetRoutingTableElement (Ptr<Node> node, std::string rt)
154 m_nodeToRoutingTableElementMap.insert (std::make_pair (node, rt));
158 ClickInternetStackHelper::Install (NodeContainer c)
const
160 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
167 ClickInternetStackHelper::InstallAll (
void)
const
169 Install (NodeContainer::GetGlobal ());
173 ClickInternetStackHelper::CreateAndAggregateObjectFromTypeId (Ptr<Node> node,
const std::string typeId)
175 ObjectFactory factory;
176 factory.SetTypeId (typeId);
177 Ptr<Object> protocol = factory.Create <Object> ();
178 node->AggregateObject (protocol);
182 ClickInternetStackHelper::Install (Ptr<Node> node)
const
186 if (node->GetObject<Ipv4> () != 0)
188 NS_FATAL_ERROR (
"ClickInternetStackHelper::Install (): Aggregating "
189 "an InternetStack to a node with an existing Ipv4 object");
193 CreateAndAggregateObjectFromTypeId (node,
"ns3::ArpL3Protocol");
194 CreateAndAggregateObjectFromTypeId (node,
"ns3::Ipv4L3ClickProtocol");
195 CreateAndAggregateObjectFromTypeId (node,
"ns3::Icmpv4L4Protocol");
196 CreateAndAggregateObjectFromTypeId (node,
"ns3::UdpL4Protocol");
197 node->AggregateObject (m_tcpFactory.Create<Object> ());
198 Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory> ();
199 node->AggregateObject (factory);
201 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
202 Ptr<Ipv4ClickRouting> ipv4Routing = CreateObject<Ipv4ClickRouting> ();
203 std::map< Ptr<Node>, std::string >::const_iterator it;
204 it = m_nodeToClickFileMap.find (node);
206 if (it != m_nodeToClickFileMap.end ())
208 ipv4Routing->SetClickFile (it->second);
211 std::map<Ptr<Node>, std::map<std::string, std::string> >::const_iterator definesIt;
212 definesIt = m_nodeToDefinesMap.find (node);
213 if (definesIt != m_nodeToDefinesMap.end ())
215 ipv4Routing->SetDefines (definesIt->second);
218 it = m_nodeToRoutingTableElementMap.find (node);
219 if (it != m_nodeToRoutingTableElementMap.end ())
221 ipv4Routing->SetClickRoutingTableElement (it->second);
223 ipv4->SetRoutingProtocol (ipv4Routing);
224 node->AggregateObject (ipv4Routing);
229 ClickInternetStackHelper::Install (std::string nodeName)
const
231 Ptr<Node> node = Names::Find<Node> (nodeName);
246 InterfacePairIpv4 pair = std::make_pair (ipv4, interface);
247 if (g_interfaceFileMapIpv4.find (pair) == g_interfaceFileMapIpv4.end ())
249 NS_LOG_INFO (
"Ignoring packet to/from interface " << interface);
253 Ptr<PcapFileWrapper> file = g_interfaceFileMapIpv4[pair];
258 ClickInternetStackHelper::PcapHooked (Ptr<Ipv4> ipv4)
260 for ( InterfaceFileMapIpv4::const_iterator i = g_interfaceFileMapIpv4.begin ();
261 i != g_interfaceFileMapIpv4.end ();
264 if ((*i).first.first == ipv4)
273 ClickInternetStackHelper::EnablePcapIpv4Internal (std::string prefix, Ptr<Ipv4> ipv4, uint32_t interface,
bool explicitFilename)
279 NS_LOG_INFO (
"Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
287 PcapHelper pcapHelper;
289 std::string filename;
290 if (explicitFilename)
296 filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ipv4, interface);
299 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW);
305 if (!PcapHooked (ipv4))
311 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol> ();
312 NS_ASSERT_MSG (ipv4L3Protocol,
"ClickInternetStackHelper::EnablePcapIpv4Internal(): "
313 "m_ipv4Enabled and ipv4L3Protocol inconsistent");
316 NS_ASSERT_MSG (result ==
true,
"ClickInternetStackHelper::EnablePcapIpv4Internal(): "
317 "Unable to connect ipv4L3Protocol \"Tx\"");
320 NS_ASSERT_MSG (result ==
true,
"ClickInternetStackHelper::EnablePcapIpv4Internal(): "
321 "Unable to connect ipv4L3Protocol \"Rx\"");
324 g_interfaceFileMapIpv4[std::make_pair (ipv4, interface)] = file;
329 Ptr<OutputStreamWrapper> stream,
330 Ipv4Header
const &header,
331 Ptr<const Packet> packet,
332 Ipv4L3Protocol::DropReason reason,
342 InterfacePairIpv4 pair = std::make_pair (ipv4, interface);
343 if (g_interfaceStreamMapIpv4.find (pair) == g_interfaceStreamMapIpv4.end ())
345 NS_LOG_INFO (
"Ignoring packet to/from interface " << interface);
349 Ptr<Packet> p = packet->Copy ();
350 p->AddHeader (header);
356 Ptr<OutputStreamWrapper> stream,
358 Ipv4Header
const &header,
359 Ptr<const Packet> packet,
360 Ipv4L3Protocol::DropReason reason,
370 InterfacePairIpv4 pair = std::make_pair (ipv4, interface);
371 if (g_interfaceStreamMapIpv4.find (pair) == g_interfaceStreamMapIpv4.end ())
373 NS_LOG_INFO (
"Ignoring packet to/from interface " << interface);
377 Ptr<Packet> p = packet->Copy ();
378 p->AddHeader (header);
379 #ifdef INTERFACE_CONTEXT
388 ClickInternetStackHelper::AsciiHooked (Ptr<Ipv4> ipv4)
390 for ( InterfaceStreamMapIpv4::const_iterator i = g_interfaceStreamMapIpv4.begin ();
391 i != g_interfaceStreamMapIpv4.end ();
394 if ((*i).first.first == ipv4)
403 ClickInternetStackHelper::EnableAsciiIpv4Internal (
404 Ptr<OutputStreamWrapper> stream,
408 bool explicitFilename)
412 NS_LOG_INFO (
"Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
420 Packet::EnablePrinting ();
439 AsciiTraceHelper asciiTraceHelper;
441 std::string filename;
442 if (explicitFilename)
448 filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ipv4, interface);
451 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
457 if (!AsciiHooked (ipv4))
464 Ptr<ArpL3Protocol> arpL3Protocol = ipv4->GetObject<ArpL3Protocol> ();
465 asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol> (arpL3Protocol,
"Drop", theStream);
473 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol> ();
474 bool result = ipv4L3Protocol->TraceConnectWithoutContext (
"Drop",
477 NS_ASSERT_MSG (result ==
true,
"ClickInternetStackHelper::EanableAsciiIpv4Internal(): "
478 "Unable to connect ipv4L3Protocol \"Drop\"");
481 g_interfaceStreamMapIpv4[std::make_pair (ipv4, interface)] = theStream;
497 if (!AsciiHooked (ipv4))
499 Ptr<Node> node = ipv4->GetObject<Node> ();
500 std::ostringstream oss;
508 oss <<
"/NodeList/" << node->GetId () <<
"/$ns3::ArpL3Protocol/Drop";
516 oss <<
"/NodeList/" << node->GetId () <<
"/$ns3::Ipv4L3Protocol/Drop";
520 g_interfaceStreamMapIpv4[std::make_pair (ipv4, interface)] = stream;
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static void Ipv4L3ProtocolDropSinkWithoutContext(Ptr< OutputStreamWrapper > stream, Ipv4Header const &header, Ptr< const Packet > packet, Ipv4L3Protocol::DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
Sync function for IPv4 dropped packet - Ascii output.
void Reset(void)
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
static InterfaceFileMapIpv4 g_interfaceFileMapIpv4
A mapping of Ipv4/interface pairs to pcap files.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_FATAL_ERROR(msg)
Fatal error handling.
void Connect(std::string path, const CallbackBase &cb)
static InterfaceStreamMapIpv4 g_interfaceStreamMapIpv4
A mapping of Ipv4/interface pairs to ascii streams.
std::map< InterfacePairIpv4, Ptr< OutputStreamWrapper > > InterfaceStreamMapIpv4
Ipv4/interface and output stream container.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
std::pair< Ptr< Ipv4 >, uint32_t > InterfacePairIpv4
Ipv4/interface pair.
static void Ipv4L3ProtocolDropSinkWithContext(Ptr< OutputStreamWrapper > stream, std::string context, Ipv4Header const &header, Ptr< const Packet > packet, Ipv4L3Protocol::DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
Sync function for IPv4 dropped packet - Ascii output.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
std::map< InterfacePairIpv4, Ptr< PcapFileWrapper > > InterfaceFileMapIpv4
Ipv4/interface and Pcap file wrapper container.
static void Ipv4L3ProtocolRxTxSink(Ptr< const Packet > p, Ptr< Ipv4 > ipv4, uint32_t interface)
Sync function for IPv4 packet - Pcap output.