implement Packet::PeekHeader and Packet::PeekTrailer to avoid evil workarounds when receiving tcp packets.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 01 Jul 2008 11:00:29 -0700
changeset 3364 8e6ac6061680
parent 3363 33d1ca2e4ba4
child 3365 6409d2460601
child 3368 95bbf601167d
child 3435 1d704c128f1f
implement Packet::PeekHeader and Packet::PeekTrailer to avoid evil workarounds when receiving tcp packets.
src/common/packet.cc
src/common/packet.h
src/internet-stack/tcp-l4-protocol.cc
--- a/src/common/packet.cc	Tue Jul 01 10:52:11 2008 -0700
+++ b/src/common/packet.cc	Tue Jul 01 11:00:29 2008 -0700
@@ -199,6 +199,13 @@
   m_metadata.RemoveHeader (header, deserialized);
   return deserialized;
 }
+uint32_t
+Packet::PeekHeader (Header &header)
+{
+  NS_LOG_FUNCTION (this << &header);
+  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
+  return deserialized;
+}
 void
 Packet::AddTrailer (const Trailer &trailer)
 {
@@ -224,6 +231,13 @@
   m_metadata.RemoveTrailer (trailer, deserialized);
   return deserialized;
 }
+uint32_t
+Packet::PeekTrailer (Trailer &trailer)
+{
+  NS_LOG_FUNCTION (this << &trailer);
+  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
+  return deserialized;
+}
 
 void 
 Packet::AddAtEnd (Ptr<const Packet> packet)
--- a/src/common/packet.h	Tue Jul 01 10:52:11 2008 -0700
+++ b/src/common/packet.h	Tue Jul 01 11:00:29 2008 -0700
@@ -187,7 +187,7 @@
   uint32_t GetSize (void) const;
   /**
    * Add header to this packet. This method invokes the
-   * GetSerializedSize and Serialize
+   * Header::GetSerializedSize and Header::Serialize
    * methods to reserve space in the buffer and request the 
    * header to serialize itself in the packet buffer.
    *
@@ -196,15 +196,23 @@
   void AddHeader (const Header & header);
   /**
    * Deserialize and remove the header from the internal buffer.
-   * This method invokes Deserialize.
+   * This method invokes Header::Deserialize.
    *
    * \param header a reference to the header to remove from the internal buffer.
    * \returns the number of bytes removed from the packet.
    */
   uint32_t RemoveHeader (Header &header);
   /**
+   * Deserialize but does _not_ remove the header from the internal buffer.
+   * This method invokes Header::Deserialize.
+   *
+   * \param header a reference to the header to read from the internal buffer.
+   * \returns the number of bytes read from the packet.
+   */  
+  uint32_t PeekHeader (Header &header);
+  /**
    * Add trailer to this packet. This method invokes the
-   * GetSerializedSize and Serialize
+   * Trailer::GetSerializedSize and Trailer::Serialize
    * methods to reserve space in the buffer and request the trailer 
    * to serialize itself in the packet buffer.
    *
@@ -220,6 +228,14 @@
    */
   uint32_t RemoveTrailer (Trailer &trailer);
   /**
+   * Deserialize but does _not_ remove a trailer from the internal buffer.
+   * This method invokes the Trailer::Deserialize method.
+   *
+   * \param trailer a reference to the trailer to read from the internal buffer.
+   * \returns the number of bytes read from the end of the packet.
+   */
+  uint32_t PeekTrailer (Trailer &trailer);
+  /**
    * \param os output stream in which the data should be printed.
    *
    * Iterate over the tags present in this packet, and
--- a/src/internet-stack/tcp-l4-protocol.cc	Tue Jul 01 10:52:11 2008 -0700
+++ b/src/internet-stack/tcp-l4-protocol.cc	Tue Jul 01 11:00:29 2008 -0700
@@ -449,13 +449,8 @@
   {
     tcpHeader.EnableChecksums();
   }
-  /* XXX very dirty but needs this to AddHeader again because of checksum */
-  tcpHeader.SetLength(5); /* XXX TCP without options */
-  tcpHeader.SetPayloadSize(packet->GetSize() - tcpHeader.GetSerializedSize());
-  tcpHeader.InitializeChecksum(source, destination, PROT_NUMBER);
 
-  //these two do a peek, so that the packet can be forwarded up
-  packet->RemoveHeader (tcpHeader);
+  packet->PeekHeader (tcpHeader);
 
   NS_LOG_LOGIC("TcpL4Protocol " << this
                << " receiving seq " << tcpHeader.GetSequenceNumber()
@@ -469,7 +464,6 @@
     return;
   }
 
-  packet->AddHeader (tcpHeader);
   NS_LOG_LOGIC ("TcpL4Protocol "<<this<<" received a packet");
   Ipv4EndPointDemux::EndPoints endPoints =
     m_endPoints->Lookup (destination, tcpHeader.GetDestinationPort (),