1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Bucknell University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Authors: L. Felipe Perrone (perrone@bucknell.edu)
19  *          Tiago G. Rodrigues (tgr002@bucknell.edu)
20  *
21  * Modified by: Mitch Watrous (watrous@u.washington.edu)
22  * Adapted to Ipv6 by: Tommaso Pecorella (tommaso.pecorella@unifi.it)
23  */
24 
25 #include "ns3/ipv6-packet-probe.h"
26 #include "ns3/object.h"
27 #include "ns3/log.h"
28 #include "ns3/names.h"
29 #include "ns3/config.h"
30 #include "ns3/trace-source-accessor.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("Ipv6PacketProbe");
35 
36 NS_OBJECT_ENSURE_REGISTERED (Ipv6PacketProbe);
37 
38 TypeId
GetTypeId()39 Ipv6PacketProbe::GetTypeId ()
40 {
41   static TypeId tid = TypeId ("ns3::Ipv6PacketProbe")
42     .SetParent<Probe> ()
43     .SetGroupName ("Internet")
44     .AddConstructor<Ipv6PacketProbe> ()
45     .AddTraceSource ( "Output",
46                       "The packet plus its IPv6 object and interface "
47                       "that serve as the output for this probe",
48                       MakeTraceSourceAccessor (&Ipv6PacketProbe::m_output),
49                       "ns3::Ipv6L3Protocol::TxRxTracedCallback")
50     .AddTraceSource ( "OutputBytes",
51                       "The number of bytes in the packet",
52                       MakeTraceSourceAccessor (&Ipv6PacketProbe::m_outputBytes),
53                       "ns3::Packet::SizeTracedCallback")
54   ;
55   return tid;
56 }
57 
Ipv6PacketProbe()58 Ipv6PacketProbe::Ipv6PacketProbe ()
59 {
60   NS_LOG_FUNCTION (this);
61   m_packet    = 0;
62   m_packetSizeOld = 0;
63   m_ipv6      = 0;
64   m_interface = 0;
65 }
66 
~Ipv6PacketProbe()67 Ipv6PacketProbe::~Ipv6PacketProbe ()
68 {
69   NS_LOG_FUNCTION (this);
70 }
71 
72 void
SetValue(Ptr<const Packet> packet,Ptr<Ipv6> ipv6,uint32_t interface)73 Ipv6PacketProbe::SetValue (Ptr<const Packet> packet, Ptr<Ipv6> ipv6, uint32_t interface)
74 {
75   NS_LOG_FUNCTION (this << packet << ipv6 << interface);
76   m_packet    = packet;
77   m_ipv6      = ipv6;
78   m_interface = interface;
79   m_output (packet, ipv6, interface);
80 
81   uint32_t packetSizeNew = packet->GetSize ();
82   m_outputBytes (m_packetSizeOld, packetSizeNew);
83   m_packetSizeOld = packetSizeNew;
84 }
85 
86 void
SetValueByPath(std::string path,Ptr<const Packet> packet,Ptr<Ipv6> ipv6,uint32_t interface)87 Ipv6PacketProbe::SetValueByPath (std::string path, Ptr<const Packet> packet, Ptr<Ipv6> ipv6, uint32_t interface)
88 {
89   NS_LOG_FUNCTION (path << packet << ipv6 << interface);
90   Ptr<Ipv6PacketProbe> probe = Names::Find<Ipv6PacketProbe> (path);
91   NS_ASSERT_MSG (probe, "Error:  Can't find probe for path " << path);
92   probe->SetValue (packet, ipv6, interface);
93 }
94 
95 bool
ConnectByObject(std::string traceSource,Ptr<Object> obj)96 Ipv6PacketProbe::ConnectByObject (std::string traceSource, Ptr<Object> obj)
97 {
98   NS_LOG_FUNCTION (this << traceSource << obj);
99   NS_LOG_DEBUG ("Name of probe (if any) in names database: " << Names::FindPath (obj));
100   bool connected = obj->TraceConnectWithoutContext (traceSource, MakeCallback (&ns3::Ipv6PacketProbe::TraceSink, this));
101   return connected;
102 }
103 
104 void
ConnectByPath(std::string path)105 Ipv6PacketProbe::ConnectByPath (std::string path)
106 {
107   NS_LOG_FUNCTION (this << path);
108   NS_LOG_DEBUG ("Name of probe to search for in config database: " << path);
109   Config::ConnectWithoutContext (path, MakeCallback (&ns3::Ipv6PacketProbe::TraceSink, this));
110 }
111 
112 void
TraceSink(Ptr<const Packet> packet,Ptr<Ipv6> ipv6,uint32_t interface)113 Ipv6PacketProbe::TraceSink (Ptr<const Packet> packet, Ptr<Ipv6> ipv6, uint32_t interface)
114 {
115   NS_LOG_FUNCTION (this << packet << ipv6 << interface);
116   if (IsEnabled ())
117     {
118       m_packet    = packet;
119       m_ipv6      = ipv6;
120       m_interface = interface;
121       m_output (packet, ipv6, interface);
122 
123       uint32_t packetSizeNew = packet->GetSize ();
124       m_outputBytes (m_packetSizeOld, packetSizeNew);
125       m_packetSizeOld = packetSizeNew;
126     }
127 }
128 
129 } // namespace ns3
130