Add some tags to packets passing through PacketSocket.
--- a/src/network/utils/packet-socket.cc Fri Jan 04 18:50:04 2013 +0100
+++ b/src/network/utils/packet-socket.cc Wed Jan 09 13:45:23 2013 +0100
@@ -158,11 +158,12 @@
dev = 0;
}
m_node->RegisterProtocolHandler (MakeCallback (&PacketSocket::ForwardUp, this),
- address.GetProtocol (), dev);
+ address.GetProtocol (), dev, (0 == dev));
m_state = STATE_BOUND;
m_protocol = address.GetProtocol ();
m_isSingleDevice = address.IsSingleDevice ();
m_device = address.GetSingleDevice ();
+ m_boundnetdevice = dev;
return 0;
}
@@ -381,8 +382,6 @@
{
return;
}
-
-
PacketSocketAddress address;
address.SetPhysicalAddress (from);
address.SetSingleDevice (device->GetIfIndex ());
@@ -391,9 +390,16 @@
if ((m_rxAvailable + packet->GetSize ()) <= m_rcvBufSize)
{
Ptr<Packet> copy = packet->Copy ();
+ DeviceNameTag dnt;
+ dnt.SetDeviceName (device->GetTypeId ().GetName ());
+ PacketSocketTag pst;
+ pst.SetPacketType (packetType);
+ pst.SetDestAddress (to);
SocketAddressTag tag;
tag.SetAddress (address);
- copy->AddPacketTag (tag);
+ copy->AddPacketTag (tag); // Attach From Physical Address
+ copy->AddPacketTag (pst); // Attach Packet Type and Dest Address
+ copy->AddPacketTag (dnt); // Attach device source name
m_deliveryQueue.push (copy);
m_rxAvailable += packet->GetSize ();
NS_LOG_LOGIC ("UID is " << packet->GetUid () << " PacketSocket " << this);
@@ -498,4 +504,147 @@
return false;
}
+/***************************************************************
+ * PacketSocket Tags
+ ***************************************************************/
+
+PacketSocketTag::PacketSocketTag ()
+{
+}
+
+void
+PacketSocketTag::SetPacketType(NetDevice::PacketType t)
+{
+ m_packetType = t;
+}
+
+NetDevice::PacketType
+PacketSocketTag::GetPacketType (void) const
+{
+ return m_packetType;
+}
+
+void
+PacketSocketTag::SetDestAddress(Address a)
+{
+ m_destAddr = a;
+}
+
+Address
+PacketSocketTag::GetDestAddress (void) const
+{
+ return m_destAddr;
+}
+
+NS_OBJECT_ENSURE_REGISTERED (PacketSocketTag);
+
+TypeId
+PacketSocketTag::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("ns3::PacketSocketTag")
+ .SetParent<Tag> ()
+ .AddConstructor<PacketSocketTag> ()
+ ;
+ return tid;
+}
+TypeId
+PacketSocketTag::GetInstanceTypeId (void) const
+{
+ return GetTypeId ();
+}
+uint32_t
+PacketSocketTag::GetSerializedSize (void) const
+{
+ return 1 + m_destAddr.GetSerializedSize();
+}
+void
+PacketSocketTag::Serialize (TagBuffer i) const
+{
+ i.WriteU8 (m_packetType);
+ m_destAddr.Serialize (i);
+}
+void
+PacketSocketTag::Deserialize (TagBuffer i)
+{
+ m_packetType = (NetDevice::PacketType) i.ReadU8 ();
+ m_destAddr.Deserialize (i);
+}
+void
+PacketSocketTag::Print (std::ostream &os) const
+{
+ os << "packetType=" << m_packetType;
+}
+
+/***************************************************************
+ * DeviceName Tags
+ ***************************************************************/
+
+DeviceNameTag::DeviceNameTag ()
+{
+}
+
+void
+DeviceNameTag::SetDeviceName (std::string n)
+{
+ if ( n.substr(0,5) == "ns3::" )
+ {
+ n = n.substr (5);
+ }
+ m_deviceName = n;
+}
+
+std::string
+DeviceNameTag::GetDeviceName (void) const
+{
+ return m_deviceName;
+}
+
+NS_OBJECT_ENSURE_REGISTERED (DeviceNameTag);
+
+TypeId
+DeviceNameTag::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("ns3::DeviceNameTag")
+ .SetParent<Tag> ()
+ .AddConstructor<DeviceNameTag> ();
+ return tid;
+}
+TypeId
+DeviceNameTag::GetInstanceTypeId (void) const
+{
+ return GetTypeId ();
+}
+uint32_t
+DeviceNameTag::GetSerializedSize (void) const
+{
+ uint32_t s = 1 + m_deviceName.size();
+ return ( s >= PACKET_TAG_MAX_SIZE)?PACKET_TAG_MAX_SIZE:s;
+}
+void
+DeviceNameTag::Serialize (TagBuffer i) const
+{
+ const char *n = m_deviceName.c_str();
+ uint8_t l = (uint8_t) strlen (n);
+
+ if ( ( 1 + l ) > PACKET_TAG_MAX_SIZE ) l = PACKET_TAG_MAX_SIZE - 1;
+
+ i.WriteU8 (l);
+ i.Write ( (uint8_t*) n , (uint32_t) l );
+}
+void
+DeviceNameTag::Deserialize (TagBuffer i)
+{
+ uint8_t l = i.ReadU8();
+ char buf[256];
+
+ i.Read ( (uint8_t* ) buf, (uint32_t) l);
+ m_deviceName = std::string (buf, l);
+}
+void
+DeviceNameTag::Print (std::ostream &os) const
+{
+ os << "DeviceName=" << m_deviceName;
+}
+
+
} // namespace ns3
--- a/src/network/utils/packet-socket.h Fri Jan 04 18:50:04 2013 +0100
+++ b/src/network/utils/packet-socket.h Wed Jan 09 13:45:23 2013 +0100
@@ -141,6 +141,82 @@
};
+/**
+ * \brief This class implements a tag that carries the dest address of a packet and the packet type.
+ *
+ */
+class PacketSocketTag : public Tag
+{
+public:
+ /**
+ * Create an empty PacketSocketTag
+ */
+ PacketSocketTag ();
+ /**
+ * Set the packet type
+ * @param t the packet type of the corresponding packet
+ */
+ void SetPacketType (NetDevice::PacketType t);
+ /**
+ * Get the packet type
+ * @return the packet type of the corresponding packet
+ */
+ NetDevice::PacketType GetPacketType (void) const;
+ /**
+ * Set the destination address of the corresponding packet
+ * @param a the destination address of the corresponding packet
+ */
+ void SetDestAddress(Address a);
+ /**
+ * Get the destination address of the corresponding packet
+ * @return the destination address of the corresponding packet
+ */
+ Address GetDestAddress (void) const;
+
+ static TypeId GetTypeId (void);
+ virtual TypeId GetInstanceTypeId (void) const;
+ virtual uint32_t GetSerializedSize (void) const;
+ virtual void Serialize (TagBuffer i) const;
+ virtual void Deserialize (TagBuffer i);
+ virtual void Print (std::ostream &os) const;
+
+private:
+ std::string m_deviceName;
+ NetDevice::PacketType m_packetType;
+ Address m_destAddr;
+};
+/**
+ * \brief This class implements a tag that carries the ns3 device name from where a packet is coming.
+ *
+ */
+class DeviceNameTag : public Tag
+{
+public:
+ /**
+ * Create an empty DeviceNameTag
+ */
+ DeviceNameTag ();
+ /**
+ * Set the device name
+ * @param n the device name from where the corresponding packet is coming.
+ */
+ void SetDeviceName (std::string n);
+ /**
+ * Get the device name from where the corresponding packet is coming.
+ * @return the device name from where the corresponding packet is coming.
+ */
+ std::string GetDeviceName (void) const;
+ static TypeId GetTypeId (void);
+ virtual TypeId GetInstanceTypeId (void) const;
+ virtual uint32_t GetSerializedSize (void) const;
+ virtual void Serialize (TagBuffer i) const;
+ virtual void Deserialize (TagBuffer i);
+ virtual void Print (std::ostream &os) const;
+
+private:
+ std::string m_deviceName;
+};
+
} // namespace ns3
#endif /* PACKET_SOCKET_H */