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::SetRoutingTableElement (NodeContainer c, std::string rt)
130 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
132 SetRoutingTableElement (*i, rt);
137 ClickInternetStackHelper::SetRoutingTableElement (Ptr<Node> node, std::string rt)
139 m_nodeToRoutingTableElementMap.insert (std::make_pair (node, rt));
143 ClickInternetStackHelper::Install (NodeContainer c)
const
145 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
152 ClickInternetStackHelper::InstallAll (
void)
const
154 Install (NodeContainer::GetGlobal ());
158 ClickInternetStackHelper::CreateAndAggregateObjectFromTypeId (Ptr<Node> node,
const std::string typeId)
160 ObjectFactory factory;
161 factory.SetTypeId (typeId);
162 Ptr<Object> protocol = factory.Create <Object> ();
163 node->AggregateObject (protocol);
167 ClickInternetStackHelper::Install (Ptr<Node> node)
const
171 if (node->GetObject<Ipv4> () != 0)
173 NS_FATAL_ERROR (
"ClickInternetStackHelper::Install (): Aggregating "
174 "an InternetStack to a node with an existing Ipv4 object");
178 CreateAndAggregateObjectFromTypeId (node,
"ns3::ArpL3Protocol");
179 CreateAndAggregateObjectFromTypeId (node,
"ns3::Ipv4L3ClickProtocol");
180 CreateAndAggregateObjectFromTypeId (node,
"ns3::Icmpv4L4Protocol");
181 CreateAndAggregateObjectFromTypeId (node,
"ns3::UdpL4Protocol");
182 node->AggregateObject (m_tcpFactory.Create<Object> ());
183 Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory> ();
184 node->AggregateObject (factory);
186 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
187 Ptr<Ipv4ClickRouting> ipv4Routing = CreateObject<Ipv4ClickRouting> ();
188 std::map< Ptr<Node>, std::string >::const_iterator it;
189 it = m_nodeToClickFileMap.find (node);
191 if (it != m_nodeToClickFileMap.end ())
193 ipv4Routing->SetClickFile (it->second);
196 it = m_nodeToRoutingTableElementMap.find (node);
197 if (it != m_nodeToRoutingTableElementMap.end ())
199 ipv4Routing->SetClickRoutingTableElement (it->second);
201 ipv4->SetRoutingProtocol (ipv4Routing);
202 node->AggregateObject (ipv4Routing);
207 ClickInternetStackHelper::Install (std::string nodeName)
const
209 Ptr<Node> node = Names::Find<Node> (nodeName);
227 NS_LOG_INFO (
"Ignoring packet to/from interface " << interface);
236 ClickInternetStackHelper::PcapHooked (Ptr<Ipv4> ipv4)
242 if ((*i).first.first == ipv4)
251 ClickInternetStackHelper::EnablePcapIpv4Internal (std::string prefix, Ptr<Ipv4> ipv4, uint32_t interface,
bool explicitFilename)
257 NS_LOG_INFO (
"Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
265 PcapHelper pcapHelper;
267 std::string filename;
268 if (explicitFilename)
274 filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ipv4, interface);
277 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW);
283 if (!PcapHooked (ipv4))
289 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol> ();
290 NS_ASSERT_MSG (ipv4L3Protocol,
"ClickInternetStackHelper::EnablePcapIpv4Internal(): "
291 "m_ipv4Enabled and ipv4L3Protocol inconsistent");
294 NS_ASSERT_MSG (result ==
true,
"ClickInternetStackHelper::EnablePcapIpv4Internal(): "
295 "Unable to connect ipv4L3Protocol \"Tx\"");
298 NS_ASSERT_MSG (result ==
true,
"ClickInternetStackHelper::EnablePcapIpv4Internal(): "
299 "Unable to connect ipv4L3Protocol \"Rx\"");
307 Ptr<OutputStreamWrapper> stream,
308 Ipv4Header
const &header,
309 Ptr<const Packet> packet,
310 Ipv4L3Protocol::DropReason reason,
323 NS_LOG_INFO (
"Ignoring packet to/from interface " << interface);
327 Ptr<Packet> p = packet->Copy ();
328 p->AddHeader (header);
334 Ptr<OutputStreamWrapper> stream,
336 Ipv4Header
const &header,
337 Ptr<const Packet> packet,
338 Ipv4L3Protocol::DropReason reason,
351 NS_LOG_INFO (
"Ignoring packet to/from interface " << interface);
355 Ptr<Packet> p = packet->Copy ();
356 p->AddHeader (header);
357 #ifdef INTERFACE_CONTEXT
366 ClickInternetStackHelper::AsciiHooked (Ptr<Ipv4> ipv4)
372 if ((*i).first.first == ipv4)
381 ClickInternetStackHelper::EnableAsciiIpv4Internal (
382 Ptr<OutputStreamWrapper> stream,
386 bool explicitFilename)
390 NS_LOG_INFO (
"Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
398 Packet::EnablePrinting ();
417 AsciiTraceHelper asciiTraceHelper;
419 std::string filename;
420 if (explicitFilename)
426 filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ipv4, interface);
429 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
435 if (!AsciiHooked (ipv4))
442 Ptr<ArpL3Protocol> arpL3Protocol = ipv4->GetObject<ArpL3Protocol> ();
443 asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol> (arpL3Protocol,
"Drop", theStream);
451 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol> ();
452 bool result = ipv4L3Protocol->TraceConnectWithoutContext (
"Drop",
455 NS_ASSERT_MSG (result ==
true,
"ClickInternetStackHelper::EanableAsciiIpv4Internal(): "
456 "Unable to connect ipv4L3Protocol \"Drop\"");
475 if (!AsciiHooked (ipv4))
477 Ptr<Node> node = ipv4->GetObject<Node> ();
478 std::ostringstream oss;
486 oss <<
"/NodeList/" << node->GetId () <<
"/$ns3::ArpL3Protocol/Drop";
494 oss <<
"/NodeList/" << node->GetId () <<
"/$ns3::Ipv4L3Protocol/Drop";