diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index 653068c8c..affe814bb 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -30,6 +30,7 @@ #include "ns3/trace-source-accessor.h" #include "ns3/object-vector.h" #include "ns3/ipv4-header.h" +#include "ns3/boolean.h" #include "arp-l3-protocol.h" #include "ipv4-l3-protocol.h" @@ -57,6 +58,11 @@ Ipv4L3Protocol::GetTypeId (void) UintegerValue (64), MakeUintegerAccessor (&Ipv4L3Protocol::m_defaultTtl), MakeUintegerChecker ()) + .AddAttribute ("CalcChecksum", "If true, we calculate the checksum of outgoing packets" + " and verify the checksum of incoming packets.", + BooleanValue (false), + MakeBooleanAccessor (&Ipv4L3Protocol::m_calcChecksum), + MakeBooleanChecker ()) .AddTraceSource ("Tx", "Send ipv4 packet to outgoing interface.", MakeTraceSourceAccessor (&Ipv4L3Protocol::m_txTrace)) .AddTraceSource ("Rx", "Receive ipv4 packet from incoming interface.", @@ -464,10 +470,15 @@ Ipv4L3Protocol::Receive( Ptr device, Ptr packet, uint16_t pro index++; } Ipv4Header ipHeader; + if (m_calcChecksum) + { + ipHeader.EnableChecksum (); + } packet->RemoveHeader (ipHeader); if (!ipHeader.IsChecksumOk ()) { + m_dropTrace (packet); return; } @@ -490,6 +501,11 @@ Ipv4L3Protocol::Send (Ptr packet, Ipv4Header ipHeader; + if (m_calcChecksum) + { + ipHeader.EnableChecksum (); + } + ipHeader.SetSource (source); ipHeader.SetDestination (destination); ipHeader.SetProtocol (protocol); diff --git a/src/internet-node/ipv4-l3-protocol.h b/src/internet-node/ipv4-l3-protocol.h index 7b65269d7..40f0be5bd 100644 --- a/src/internet-node/ipv4-l3-protocol.h +++ b/src/internet-node/ipv4-l3-protocol.h @@ -191,6 +191,7 @@ private: Ipv4InterfaceList m_interfaces; uint32_t m_nInterfaces; uint8_t m_defaultTtl; + bool m_calcChecksum; uint16_t m_identification; Ptr m_node; TracedCallback, uint32_t> m_txTrace; diff --git a/src/node/ipv4-header.cc b/src/node/ipv4-header.cc index a9660c8dd..758358f98 100644 --- a/src/node/ipv4-header.cc +++ b/src/node/ipv4-header.cc @@ -29,10 +29,9 @@ namespace ns3 { NS_OBJECT_ENSURE_REGISTERED (Ipv4Header); -bool Ipv4Header::m_calcChecksum = false; - Ipv4Header::Ipv4Header () - : m_payloadSize (0), + : m_calcChecksum (false), + m_payloadSize (0), m_identification (0), m_tos (0), m_ttl (0), @@ -43,7 +42,7 @@ Ipv4Header::Ipv4Header () {} void -Ipv4Header::EnableChecksums (void) +Ipv4Header::EnableChecksum (void) { m_calcChecksum = true; } diff --git a/src/node/ipv4-header.h b/src/node/ipv4-header.h index 54f6a8474..59ae97fe4 100644 --- a/src/node/ipv4-header.h +++ b/src/node/ipv4-header.h @@ -36,9 +36,9 @@ public: */ Ipv4Header (); /** - * \brief Enable checksum calculation for IP (XXX currently has no effect) + * \brief Enable checksum calculation for this header. */ - static void EnableChecksums (void); + void EnableChecksum (void); /** * \param size the size of the payload in bytes */ @@ -134,7 +134,7 @@ public: * \returns true if the ipv4 checksum is correct, false otherwise. * * If Ipv4Header::EnableChecksums has not been called prior to - * creating this packet, this method will always return true. + * deserializing this header, this method will always return true. */ bool IsChecksumOk (void) const; @@ -152,7 +152,7 @@ private: MORE_FRAGMENTS = (1<<1) }; - static bool m_calcChecksum; + bool m_calcChecksum; uint16_t m_payloadSize; uint16_t m_identification;