bug 207: ipv4-header.h needed in src/node module.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed Jun 04 10:18:57 2008 -0700 (20 months ago)
changeset 3216b36bb98d766e
parent 3215 c5a74196e87a
child 3217 9a72c241348e
bug 207: ipv4-header.h needed in src/node module.
src/internet-node/ipv4-header.cc
src/internet-node/ipv4-header.h
src/internet-node/ipv4-l3-protocol.cc
src/internet-node/ipv4-l3-protocol.h
src/internet-node/ipv4-static-routing.h
src/internet-node/wscript
src/node/ipv4-header.cc
src/node/ipv4-header.h
src/node/ipv4.h
src/node/wscript
     1.1 --- a/src/internet-node/ipv4-header.cc	Wed Jun 04 10:09:29 2008 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,331 +0,0 @@
     1.4 -/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     1.5 -/*
     1.6 - * Copyright (c) 2005 INRIA
     1.7 - *
     1.8 - * This program is free software; you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License version 2 as
    1.10 - * published by the Free Software Foundation;
    1.11 - *
    1.12 - * This program is distributed in the hope that it will be useful,
    1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 - * GNU General Public License for more details.
    1.16 - *
    1.17 - * You should have received a copy of the GNU General Public License
    1.18 - * along with this program; if not, write to the Free Software
    1.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 - *
    1.21 - * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    1.22 - */
    1.23 -
    1.24 -#include "ns3/assert.h"
    1.25 -#include "ns3/log.h"
    1.26 -#include "ns3/header.h"
    1.27 -#include "ipv4-header.h"
    1.28 -
    1.29 -NS_LOG_COMPONENT_DEFINE ("Ipv4Header");
    1.30 -
    1.31 -namespace ns3 {
    1.32 -
    1.33 -NS_OBJECT_ENSURE_REGISTERED (Ipv4Header);
    1.34 -
    1.35 -bool Ipv4Header::m_calcChecksum = false;
    1.36 -
    1.37 -Ipv4Header::Ipv4Header ()
    1.38 -  : m_payloadSize (0),
    1.39 -    m_identification (0),
    1.40 -    m_tos (0),
    1.41 -    m_ttl (0),
    1.42 -    m_protocol (0),
    1.43 -    m_flags (0),
    1.44 -    m_fragmentOffset (0),
    1.45 -    m_goodChecksum (true)
    1.46 -{}
    1.47 -
    1.48 -void 
    1.49 -Ipv4Header::EnableChecksums (void)
    1.50 -{
    1.51 -  m_calcChecksum = true;
    1.52 -}
    1.53 -
    1.54 -void 
    1.55 -Ipv4Header::SetPayloadSize (uint16_t size)
    1.56 -{
    1.57 -  m_payloadSize = size;
    1.58 -}
    1.59 -uint16_t 
    1.60 -Ipv4Header::GetPayloadSize (void) const
    1.61 -{
    1.62 -  return m_payloadSize;
    1.63 -}
    1.64 -
    1.65 -uint16_t 
    1.66 -Ipv4Header::GetIdentification (void) const
    1.67 -{
    1.68 -  return m_identification;
    1.69 -}
    1.70 -void 
    1.71 -Ipv4Header::SetIdentification (uint16_t identification)
    1.72 -{
    1.73 -  m_identification = identification;
    1.74 -}
    1.75 -
    1.76 -
    1.77 -
    1.78 -void 
    1.79 -Ipv4Header::SetTos (uint8_t tos)
    1.80 -{
    1.81 -  m_tos = tos;
    1.82 -}
    1.83 -uint8_t 
    1.84 -Ipv4Header::GetTos (void) const
    1.85 -{
    1.86 -  return m_tos;
    1.87 -}
    1.88 -void 
    1.89 -Ipv4Header::SetMoreFragments (void)
    1.90 -{
    1.91 -  m_flags |= MORE_FRAGMENTS;
    1.92 -}
    1.93 -void
    1.94 -Ipv4Header::SetLastFragment (void)
    1.95 -{
    1.96 -  m_flags &= ~MORE_FRAGMENTS;
    1.97 -}
    1.98 -bool 
    1.99 -Ipv4Header::IsLastFragment (void) const
   1.100 -{
   1.101 -  return !(m_flags & MORE_FRAGMENTS);
   1.102 -}
   1.103 -
   1.104 -void 
   1.105 -Ipv4Header::SetDontFragment (void)
   1.106 -{
   1.107 -  m_flags |= DONT_FRAGMENT;
   1.108 -}
   1.109 -void 
   1.110 -Ipv4Header::SetMayFragment (void)
   1.111 -{
   1.112 -  m_flags &= ~DONT_FRAGMENT;
   1.113 -}
   1.114 -bool 
   1.115 -Ipv4Header::IsDontFragment (void) const
   1.116 -{
   1.117 -  return (m_flags & DONT_FRAGMENT);
   1.118 -}
   1.119 -
   1.120 -void 
   1.121 -Ipv4Header::SetFragmentOffset (uint16_t offset)
   1.122 -{
   1.123 -  NS_ASSERT (!(offset & (~0x3fff)));
   1.124 -  m_fragmentOffset = offset;
   1.125 -}
   1.126 -uint16_t 
   1.127 -Ipv4Header::GetFragmentOffset (void) const
   1.128 -{
   1.129 -  NS_ASSERT (!(m_fragmentOffset & (~0x3fff)));
   1.130 -  return m_fragmentOffset;
   1.131 -}
   1.132 -
   1.133 -void 
   1.134 -Ipv4Header::SetTtl (uint8_t ttl)
   1.135 -{
   1.136 -  m_ttl = ttl;
   1.137 -}
   1.138 -uint8_t 
   1.139 -Ipv4Header::GetTtl (void) const
   1.140 -{
   1.141 -  return m_ttl;
   1.142 -}
   1.143 -  
   1.144 -uint8_t 
   1.145 -Ipv4Header::GetProtocol (void) const
   1.146 -{
   1.147 -  return m_protocol;
   1.148 -}
   1.149 -void 
   1.150 -Ipv4Header::SetProtocol (uint8_t protocol)
   1.151 -{
   1.152 -  m_protocol = protocol;
   1.153 -}
   1.154 -
   1.155 -void 
   1.156 -Ipv4Header::SetSource (Ipv4Address source)
   1.157 -{
   1.158 -  m_source = source;
   1.159 -}
   1.160 -Ipv4Address
   1.161 -Ipv4Header::GetSource (void) const
   1.162 -{
   1.163 -  return m_source;
   1.164 -}
   1.165 -
   1.166 -void 
   1.167 -Ipv4Header::SetDestination (Ipv4Address dst)
   1.168 -{
   1.169 -  m_destination = dst;
   1.170 -}
   1.171 -Ipv4Address
   1.172 -Ipv4Header::GetDestination (void) const
   1.173 -{
   1.174 -  return m_destination;
   1.175 -}
   1.176 -
   1.177 -
   1.178 -bool
   1.179 -Ipv4Header::IsChecksumOk (void) const
   1.180 -{
   1.181 -  return m_goodChecksum;
   1.182 -}
   1.183 -
   1.184 -
   1.185 -TypeId 
   1.186 -Ipv4Header::GetTypeId (void)
   1.187 -{
   1.188 -  static TypeId tid = TypeId ("ns3::Ipv4Header")
   1.189 -    .SetParent<Header> ()
   1.190 -    .AddConstructor<Ipv4Header> ()
   1.191 -    ;
   1.192 -  return tid;
   1.193 -}
   1.194 -TypeId 
   1.195 -Ipv4Header::GetInstanceTypeId (void) const
   1.196 -{
   1.197 -  return GetTypeId ();
   1.198 -}
   1.199 -void 
   1.200 -Ipv4Header::Print (std::ostream &os) const
   1.201 -{
   1.202 -  // ipv4, right ?
   1.203 -  std::string flags;
   1.204 -  if (m_flags == 0)
   1.205 -    {
   1.206 -      flags = "none";
   1.207 -    }
   1.208 -  else if (m_flags & MORE_FRAGMENTS &&
   1.209 -           m_flags & DONT_FRAGMENT)
   1.210 -    {
   1.211 -      flags = "MF|DF";
   1.212 -    }
   1.213 -  else if (m_flags & DONT_FRAGMENT)
   1.214 -    {
   1.215 -      flags = "DF";
   1.216 -    }
   1.217 -  else if (m_flags & MORE_FRAGMENTS)
   1.218 -    {
   1.219 -      flags = "MF";
   1.220 -    }
   1.221 -  else
   1.222 -    {
   1.223 -      flags = "XX";
   1.224 -    }
   1.225 -  os << "tos 0x" << std::hex << m_tos << std::dec << " "
   1.226 -     << "ttl " << m_ttl << " "
   1.227 -     << "id " << m_identification << " "
   1.228 -     << "offset " << m_fragmentOffset << " "
   1.229 -     << "flags [" << flags << "] "
   1.230 -     << "length: " << (m_payloadSize + 5 * 4)
   1.231 -     << " " 
   1.232 -     << m_source << " > " << m_destination
   1.233 -    ;
   1.234 -}
   1.235 -uint32_t 
   1.236 -Ipv4Header::GetSerializedSize (void) const
   1.237 -{
   1.238 -  return 5 * 4;
   1.239 -}
   1.240 -
   1.241 -void
   1.242 -Ipv4Header::Serialize (Buffer::Iterator start) const
   1.243 -{
   1.244 -  Buffer::Iterator i = start;
   1.245 -  
   1.246 -  uint8_t verIhl = (4 << 4) | (5);
   1.247 -  i.WriteU8 (verIhl);
   1.248 -  i.WriteU8 (m_tos);
   1.249 -  i.WriteHtonU16 (m_payloadSize + 5*4);
   1.250 -  i.WriteHtonU16 (m_identification);
   1.251 -  uint32_t fragmentOffset = m_fragmentOffset / 8;
   1.252 -  uint8_t flagsFrag = (fragmentOffset >> 8) & 0x1f;
   1.253 -  if (m_flags & DONT_FRAGMENT) 
   1.254 -    {
   1.255 -      flagsFrag |= (1<<6);
   1.256 -    }
   1.257 -  if (m_flags & MORE_FRAGMENTS) 
   1.258 -    {
   1.259 -      flagsFrag |= (1<<5);
   1.260 -    }
   1.261 -  i.WriteU8 (flagsFrag);
   1.262 -  uint8_t frag = fragmentOffset & 0xff;
   1.263 -  i.WriteU8 (frag);
   1.264 -  i.WriteU8 (m_ttl);
   1.265 -  i.WriteU8 (m_protocol);
   1.266 -  i.WriteHtonU16 (0);
   1.267 -  i.WriteHtonU32 (m_source.Get ());
   1.268 -  i.WriteHtonU32 (m_destination.Get ());
   1.269 -
   1.270 -  if (m_calcChecksum) 
   1.271 -    {
   1.272 -#if 0
   1.273 -      // XXX we need to add Buffer::Iterator::PeekData method
   1.274 -      uint8_t *data = start.PeekData ();
   1.275 -      uint16_t checksum = UtilsChecksumCalculate (0, data, GetSize ());
   1.276 -      checksum = UtilsChecksumComplete (checksum);
   1.277 -      NS_LOG_LOGIC ("checksum=" <<checksum);
   1.278 -      i = start;
   1.279 -      i.Next (10);
   1.280 -      i.WriteU16 (checksum);
   1.281 -#endif
   1.282 -    }
   1.283 -}
   1.284 -uint32_t
   1.285 -Ipv4Header::Deserialize (Buffer::Iterator start)
   1.286 -{
   1.287 -  Buffer::Iterator i = start;
   1.288 -  uint8_t verIhl = i.ReadU8 ();
   1.289 -  uint8_t ihl = verIhl & 0x0f; 
   1.290 -  uint16_t headerSize = ihl * 4;
   1.291 -  NS_ASSERT ((verIhl >> 4) == 4);
   1.292 -  m_tos = i.ReadU8 ();
   1.293 -  uint16_t size = i.ReadNtohU16 ();
   1.294 -  m_payloadSize = size - headerSize;
   1.295 -  m_identification = i.ReadNtohU16 ();
   1.296 -  uint8_t flags = i.ReadU8 ();
   1.297 -  m_flags = 0;
   1.298 -  if (flags & (1<<6)) 
   1.299 -    {
   1.300 -      m_flags |= DONT_FRAGMENT;
   1.301 -    }
   1.302 -  if (flags & (1<<5)) 
   1.303 -    {
   1.304 -      m_flags |= MORE_FRAGMENTS;
   1.305 -    }
   1.306 -  //XXXX I think we should clear some bits in fragmentOffset !
   1.307 -  i.Prev ();
   1.308 -  m_fragmentOffset = i.ReadNtohU16 ();
   1.309 -  m_fragmentOffset *= 8;
   1.310 -  m_ttl = i.ReadU8 ();
   1.311 -  m_protocol = i.ReadU8 ();
   1.312 -  i.Next (2); // checksum
   1.313 -  m_source.Set (i.ReadNtohU32 ());
   1.314 -  m_destination.Set (i.ReadNtohU32 ());
   1.315 -
   1.316 -  if (m_calcChecksum) 
   1.317 -    {
   1.318 -#if 0
   1.319 -      uint8_t *data = start.PeekData ();
   1.320 -      uint16_t localChecksum = UtilsChecksumCalculate (0, data, headerSize);
   1.321 -      if (localChecksum == 0xffff) 
   1.322 -        {
   1.323 -          m_goodChecksum = true;
   1.324 -        } 
   1.325 -      else 
   1.326 -        {
   1.327 -          m_goodChecksum = false;
   1.328 -        }
   1.329 -#endif
   1.330 -    }
   1.331 -  return GetSerializedSize ();
   1.332 -}
   1.333 -
   1.334 -}; // namespace ns3
     2.1 --- a/src/internet-node/ipv4-header.h	Wed Jun 04 10:09:29 2008 -0700
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,171 +0,0 @@
     2.4 -/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     2.5 -/*
     2.6 - * Copyright (c) 2005 INRIA
     2.7 - *
     2.8 - * This program is free software; you can redistribute it and/or modify
     2.9 - * it under the terms of the GNU General Public License version 2 as
    2.10 - * published by the Free Software Foundation;
    2.11 - *
    2.12 - * This program is distributed in the hope that it will be useful,
    2.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 - * GNU General Public License for more details.
    2.16 - *
    2.17 - * You should have received a copy of the GNU General Public License
    2.18 - * along with this program; if not, write to the Free Software
    2.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.20 - *
    2.21 - * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    2.22 - */
    2.23 -
    2.24 -#ifndef IPV4_HEADER_H
    2.25 -#define IPV4_HEADER_H
    2.26 -
    2.27 -#include "ns3/header.h"
    2.28 -#include "ns3/ipv4-address.h"
    2.29 -
    2.30 -namespace ns3 {
    2.31 -/**
    2.32 - * \brief Packet header for IPv4
    2.33 - */
    2.34 -class Ipv4Header : public Header 
    2.35 -{
    2.36 -public:
    2.37 -  /**
    2.38 -   * \brief Construct a null IPv4 header
    2.39 -   */
    2.40 -  Ipv4Header ();
    2.41 -  /**
    2.42 -   * \brief Enable checksum calculation for IP (XXX currently has no effect)
    2.43 -   */
    2.44 -  static void EnableChecksums (void);
    2.45 -  /**
    2.46 -   * \param size the size of the payload in bytes
    2.47 -   */
    2.48 -  void SetPayloadSize (uint16_t size);
    2.49 -  /**
    2.50 -   * \param identification the Identification field of IPv4 packets.
    2.51 -   *
    2.52 -   * By default, set to zero.
    2.53 -   */
    2.54 -  void SetIdentification (uint16_t identification);
    2.55 -  /**
    2.56 -   * \param tos the 8 bits of Ipv4 TOS.
    2.57 -   */
    2.58 -  void SetTos (uint8_t tos);
    2.59 -  /**
    2.60 -   * This packet is not the last packet of a fragmented ipv4 packet.
    2.61 -   */
    2.62 -  void SetMoreFragments (void);
    2.63 -  /**
    2.64 -   * This packet is the last packet of a fragmented ipv4 packet.
    2.65 -   */
    2.66 -  void SetLastFragment (void);
    2.67 -  /**
    2.68 -   * Don't fragment this packet: if you need to anyway, drop it.
    2.69 -   */
    2.70 -  void SetDontFragment (void);
    2.71 -  /**
    2.72 -   * If you need to fragment this packet, you can do it.
    2.73 -   */
    2.74 -  void SetMayFragment (void);
    2.75 -  /**
    2.76 -   * \param offset the ipv4 fragment offset
    2.77 -   */
    2.78 -  void SetFragmentOffset (uint16_t offset);
    2.79 -  /**
    2.80 -   * \param ttl the ipv4 TTL
    2.81 -   */
    2.82 -  void SetTtl (uint8_t ttl);
    2.83 -  /**
    2.84 -   * \param num the ipv4 protocol field
    2.85 -   */
    2.86 -  void SetProtocol (uint8_t num);
    2.87 -  /**
    2.88 -   * \param source the source of this packet
    2.89 -   */
    2.90 -  void SetSource (Ipv4Address source);
    2.91 -  /**
    2.92 -   * \param destination the destination of this packet.
    2.93 -   */
    2.94 -  void SetDestination (Ipv4Address destination);
    2.95 -  /**
    2.96 -   * \returns the size of the payload in bytes
    2.97 -   */
    2.98 -  uint16_t GetPayloadSize (void) const;
    2.99 -  /**
   2.100 -   * \returns the identification field of this packet.
   2.101 -   */
   2.102 -  uint16_t GetIdentification (void) const;
   2.103 -  /**
   2.104 -   * \returns the TOS field of this packet.
   2.105 -   */
   2.106 -  uint8_t GetTos (void) const;
   2.107 -  /**
   2.108 -   * \returns true if this is the last fragment of a packet, false otherwise.
   2.109 -   */
   2.110 -  bool IsLastFragment (void) const;
   2.111 -  /**
   2.112 -   * \returns true if this is this packet can be fragmented.
   2.113 -   */  
   2.114 -  bool IsDontFragment (void) const;
   2.115 -  /**
   2.116 -   * \returns the offset of this fragment.
   2.117 -   */
   2.118 -  uint16_t GetFragmentOffset (void) const;
   2.119 -  /**
   2.120 -   * \returns the TTL field of this packet
   2.121 -   */
   2.122 -  uint8_t GetTtl (void) const;
   2.123 -  /**
   2.124 -   * \returns the protocol field of this packet
   2.125 -   */
   2.126 -  uint8_t GetProtocol (void) const;
   2.127 -  /**
   2.128 -   * \returns the source address of this packet
   2.129 -   */
   2.130 -  Ipv4Address GetSource (void) const;
   2.131 -  /**
   2.132 -   * \returns the destination address of this packet
   2.133 -   */
   2.134 -  Ipv4Address GetDestination (void) const;
   2.135 -  
   2.136 -  /**
   2.137 -   * \returns true if the upv4 checksum is correct, false otherwise.
   2.138 -   *
   2.139 -   * If Ipv4Header::EnableChecksums has not been called prior to
   2.140 -   * creating this packet, this method will always return true.
   2.141 -   */
   2.142 -  bool IsChecksumOk (void) const;
   2.143 -
   2.144 -  static TypeId GetTypeId (void);
   2.145 -  virtual TypeId GetInstanceTypeId (void) const;
   2.146 -  virtual void Print (std::ostream &os) const;
   2.147 -  virtual uint32_t GetSerializedSize (void) const;
   2.148 -  virtual void Serialize (Buffer::Iterator start) const;
   2.149 -  virtual uint32_t Deserialize (Buffer::Iterator start);
   2.150 -private:
   2.151 -
   2.152 -  enum FlagsE {
   2.153 -    DONT_FRAGMENT = (1<<0),
   2.154 -    MORE_FRAGMENTS = (1<<1)
   2.155 -  };
   2.156 -
   2.157 -  static bool m_calcChecksum;
   2.158 -
   2.159 -  uint16_t m_payloadSize;
   2.160 -  uint16_t m_identification;
   2.161 -  uint32_t m_tos : 8;
   2.162 -  uint32_t m_ttl : 8;
   2.163 -  uint32_t m_protocol : 8;
   2.164 -  uint32_t m_flags : 3;
   2.165 -  uint16_t m_fragmentOffset : 13;
   2.166 -  Ipv4Address m_source;
   2.167 -  Ipv4Address m_destination;
   2.168 -  bool m_goodChecksum;
   2.169 -};
   2.170 -
   2.171 -} // namespace ns3
   2.172 -
   2.173 -
   2.174 -#endif /* IPV4_HEADER_H */
     3.1 --- a/src/internet-node/ipv4-l3-protocol.cc	Wed Jun 04 10:09:29 2008 -0700
     3.2 +++ b/src/internet-node/ipv4-l3-protocol.cc	Wed Jun 04 10:18:57 2008 -0700
     3.3 @@ -29,11 +29,11 @@
     3.4  #include "ns3/uinteger.h"
     3.5  #include "ns3/trace-source-accessor.h"
     3.6  #include "ns3/object-vector.h"
     3.7 +#include "ns3/ipv4-header.h"
     3.8  #include "arp-l3-protocol.h"
     3.9  
    3.10  #include "ipv4-l3-protocol.h"
    3.11  #include "ipv4-l4-protocol.h"
    3.12 -#include "ipv4-header.h"
    3.13  #include "ipv4-interface.h"
    3.14  #include "ipv4-loopback-interface.h"
    3.15  #include "arp-ipv4-interface.h"
     4.1 --- a/src/internet-node/ipv4-l3-protocol.h	Wed Jun 04 10:09:29 2008 -0700
     4.2 +++ b/src/internet-node/ipv4-l3-protocol.h	Wed Jun 04 10:18:57 2008 -0700
     4.3 @@ -27,7 +27,7 @@
     4.4  #include "ns3/ptr.h"
     4.5  #include "ns3/ipv4.h"
     4.6  #include "ns3/traced-callback.h"
     4.7 -#include "ipv4-header.h"
     4.8 +#include "ns3/ipv4-header.h"
     4.9  #include "ipv4-static-routing.h"
    4.10  
    4.11  namespace ns3 {
     5.1 --- a/src/internet-node/ipv4-static-routing.h	Wed Jun 04 10:09:29 2008 -0700
     5.2 +++ b/src/internet-node/ipv4-static-routing.h	Wed Jun 04 10:18:57 2008 -0700
     5.3 @@ -25,7 +25,7 @@
     5.4  #include <list>
     5.5  #include <stdint.h>
     5.6  #include "ns3/ipv4-address.h"
     5.7 -#include "ipv4-header.h"
     5.8 +#include "ns3/ipv4-header.h"
     5.9  #include "ns3/ptr.h"
    5.10  #include "ns3/ipv4.h"
    5.11  
     6.1 --- a/src/internet-node/wscript	Wed Jun 04 10:09:29 2008 -0700
     6.2 +++ b/src/internet-node/wscript	Wed Jun 04 10:18:57 2008 -0700
     6.3 @@ -7,7 +7,6 @@
     6.4          'internet-stack.cc',
     6.5          'ipv4-l4-demux.cc',
     6.6          'ipv4-l4-protocol.cc',
     6.7 -        'ipv4-header.cc',
     6.8          'udp-header.cc',
     6.9          'tcp-header.cc',
    6.10          'ipv4-checksum.cc',
    6.11 @@ -37,7 +36,6 @@
    6.12      headers.module = 'internet-node'
    6.13      headers.source = [
    6.14          'internet-stack.h',
    6.15 -        'ipv4-header.h',
    6.16          'udp-header.h',
    6.17          'tcp-header.h',
    6.18          'sequence-number.h',
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/node/ipv4-header.cc	Wed Jun 04 10:18:57 2008 -0700
     7.3 @@ -0,0 +1,331 @@
     7.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     7.5 +/*
     7.6 + * Copyright (c) 2005 INRIA
     7.7 + *
     7.8 + * This program is free software; you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License version 2 as
    7.10 + * published by the Free Software Foundation;
    7.11 + *
    7.12 + * This program is distributed in the hope that it will be useful,
    7.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.15 + * GNU General Public License for more details.
    7.16 + *
    7.17 + * You should have received a copy of the GNU General Public License
    7.18 + * along with this program; if not, write to the Free Software
    7.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    7.20 + *
    7.21 + * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    7.22 + */
    7.23 +
    7.24 +#include "ns3/assert.h"
    7.25 +#include "ns3/log.h"
    7.26 +#include "ns3/header.h"
    7.27 +#include "ipv4-header.h"
    7.28 +
    7.29 +NS_LOG_COMPONENT_DEFINE ("Ipv4Header");
    7.30 +
    7.31 +namespace ns3 {
    7.32 +
    7.33 +NS_OBJECT_ENSURE_REGISTERED (Ipv4Header);
    7.34 +
    7.35 +bool Ipv4Header::m_calcChecksum = false;
    7.36 +
    7.37 +Ipv4Header::Ipv4Header ()
    7.38 +  : m_payloadSize (0),
    7.39 +    m_identification (0),
    7.40 +    m_tos (0),
    7.41 +    m_ttl (0),
    7.42 +    m_protocol (0),
    7.43 +    m_flags (0),
    7.44 +    m_fragmentOffset (0),
    7.45 +    m_goodChecksum (true)
    7.46 +{}
    7.47 +
    7.48 +void 
    7.49 +Ipv4Header::EnableChecksums (void)
    7.50 +{
    7.51 +  m_calcChecksum = true;
    7.52 +}
    7.53 +
    7.54 +void 
    7.55 +Ipv4Header::SetPayloadSize (uint16_t size)
    7.56 +{
    7.57 +  m_payloadSize = size;
    7.58 +}
    7.59 +uint16_t 
    7.60 +Ipv4Header::GetPayloadSize (void) const
    7.61 +{
    7.62 +  return m_payloadSize;
    7.63 +}
    7.64 +
    7.65 +uint16_t 
    7.66 +Ipv4Header::GetIdentification (void) const
    7.67 +{
    7.68 +  return m_identification;
    7.69 +}
    7.70 +void 
    7.71 +Ipv4Header::SetIdentification (uint16_t identification)
    7.72 +{
    7.73 +  m_identification = identification;
    7.74 +}
    7.75 +
    7.76 +
    7.77 +
    7.78 +void 
    7.79 +Ipv4Header::SetTos (uint8_t tos)
    7.80 +{
    7.81 +  m_tos = tos;
    7.82 +}
    7.83 +uint8_t 
    7.84 +Ipv4Header::GetTos (void) const
    7.85 +{
    7.86 +  return m_tos;
    7.87 +}
    7.88 +void 
    7.89 +Ipv4Header::SetMoreFragments (void)
    7.90 +{
    7.91 +  m_flags |= MORE_FRAGMENTS;
    7.92 +}
    7.93 +void
    7.94 +Ipv4Header::SetLastFragment (void)
    7.95 +{
    7.96 +  m_flags &= ~MORE_FRAGMENTS;
    7.97 +}
    7.98 +bool 
    7.99 +Ipv4Header::IsLastFragment (void) const
   7.100 +{
   7.101 +  return !(m_flags & MORE_FRAGMENTS);
   7.102 +}
   7.103 +
   7.104 +void 
   7.105 +Ipv4Header::SetDontFragment (void)
   7.106 +{
   7.107 +  m_flags |= DONT_FRAGMENT;
   7.108 +}
   7.109 +void 
   7.110 +Ipv4Header::SetMayFragment (void)
   7.111 +{
   7.112 +  m_flags &= ~DONT_FRAGMENT;
   7.113 +}
   7.114 +bool 
   7.115 +Ipv4Header::IsDontFragment (void) const
   7.116 +{
   7.117 +  return (m_flags & DONT_FRAGMENT);
   7.118 +}
   7.119 +
   7.120 +void 
   7.121 +Ipv4Header::SetFragmentOffset (uint16_t offset)
   7.122 +{
   7.123 +  NS_ASSERT (!(offset & (~0x3fff)));
   7.124 +  m_fragmentOffset = offset;
   7.125 +}
   7.126 +uint16_t 
   7.127 +Ipv4Header::GetFragmentOffset (void) const
   7.128 +{
   7.129 +  NS_ASSERT (!(m_fragmentOffset & (~0x3fff)));
   7.130 +  return m_fragmentOffset;
   7.131 +}
   7.132 +
   7.133 +void 
   7.134 +Ipv4Header::SetTtl (uint8_t ttl)
   7.135 +{
   7.136 +  m_ttl = ttl;
   7.137 +}
   7.138 +uint8_t 
   7.139 +Ipv4Header::GetTtl (void) const
   7.140 +{
   7.141 +  return m_ttl;
   7.142 +}
   7.143 +  
   7.144 +uint8_t 
   7.145 +Ipv4Header::GetProtocol (void) const
   7.146 +{
   7.147 +  return m_protocol;
   7.148 +}
   7.149 +void 
   7.150 +Ipv4Header::SetProtocol (uint8_t protocol)
   7.151 +{
   7.152 +  m_protocol = protocol;
   7.153 +}
   7.154 +
   7.155 +void 
   7.156 +Ipv4Header::SetSource (Ipv4Address source)
   7.157 +{
   7.158 +  m_source = source;
   7.159 +}
   7.160 +Ipv4Address
   7.161 +Ipv4Header::GetSource (void) const
   7.162 +{
   7.163 +  return m_source;
   7.164 +}
   7.165 +
   7.166 +void 
   7.167 +Ipv4Header::SetDestination (Ipv4Address dst)
   7.168 +{
   7.169 +  m_destination = dst;
   7.170 +}
   7.171 +Ipv4Address
   7.172 +Ipv4Header::GetDestination (void) const
   7.173 +{
   7.174 +  return m_destination;
   7.175 +}
   7.176 +
   7.177 +
   7.178 +bool
   7.179 +Ipv4Header::IsChecksumOk (void) const
   7.180 +{
   7.181 +  return m_goodChecksum;
   7.182 +}
   7.183 +
   7.184 +
   7.185 +TypeId 
   7.186 +Ipv4Header::GetTypeId (void)
   7.187 +{
   7.188 +  static TypeId tid = TypeId ("ns3::Ipv4Header")
   7.189 +    .SetParent<Header> ()
   7.190 +    .AddConstructor<Ipv4Header> ()
   7.191 +    ;
   7.192 +  return tid;
   7.193 +}
   7.194 +TypeId 
   7.195 +Ipv4Header::GetInstanceTypeId (void) const
   7.196 +{
   7.197 +  return GetTypeId ();
   7.198 +}
   7.199 +void 
   7.200 +Ipv4Header::Print (std::ostream &os) const
   7.201 +{
   7.202 +  // ipv4, right ?
   7.203 +  std::string flags;
   7.204 +  if (m_flags == 0)
   7.205 +    {
   7.206 +      flags = "none";
   7.207 +    }
   7.208 +  else if (m_flags & MORE_FRAGMENTS &&
   7.209 +           m_flags & DONT_FRAGMENT)
   7.210 +    {
   7.211 +      flags = "MF|DF";
   7.212 +    }
   7.213 +  else if (m_flags & DONT_FRAGMENT)
   7.214 +    {
   7.215 +      flags = "DF";
   7.216 +    }
   7.217 +  else if (m_flags & MORE_FRAGMENTS)
   7.218 +    {
   7.219 +      flags = "MF";
   7.220 +    }
   7.221 +  else
   7.222 +    {
   7.223 +      flags = "XX";
   7.224 +    }
   7.225 +  os << "tos 0x" << std::hex << m_tos << std::dec << " "
   7.226 +     << "ttl " << m_ttl << " "
   7.227 +     << "id " << m_identification << " "
   7.228 +     << "offset " << m_fragmentOffset << " "
   7.229 +     << "flags [" << flags << "] "
   7.230 +     << "length: " << (m_payloadSize + 5 * 4)
   7.231 +     << " " 
   7.232 +     << m_source << " > " << m_destination
   7.233 +    ;
   7.234 +}
   7.235 +uint32_t 
   7.236 +Ipv4Header::GetSerializedSize (void) const
   7.237 +{
   7.238 +  return 5 * 4;
   7.239 +}
   7.240 +
   7.241 +void
   7.242 +Ipv4Header::Serialize (Buffer::Iterator start) const
   7.243 +{
   7.244 +  Buffer::Iterator i = start;
   7.245 +  
   7.246 +  uint8_t verIhl = (4 << 4) | (5);
   7.247 +  i.WriteU8 (verIhl);
   7.248 +  i.WriteU8 (m_tos);
   7.249 +  i.WriteHtonU16 (m_payloadSize + 5*4);
   7.250 +  i.WriteHtonU16 (m_identification);
   7.251 +  uint32_t fragmentOffset = m_fragmentOffset / 8;
   7.252 +  uint8_t flagsFrag = (fragmentOffset >> 8) & 0x1f;
   7.253 +  if (m_flags & DONT_FRAGMENT) 
   7.254 +    {
   7.255 +      flagsFrag |= (1<<6);
   7.256 +    }
   7.257 +  if (m_flags & MORE_FRAGMENTS) 
   7.258 +    {
   7.259 +      flagsFrag |= (1<<5);
   7.260 +    }
   7.261 +  i.WriteU8 (flagsFrag);
   7.262 +  uint8_t frag = fragmentOffset & 0xff;
   7.263 +  i.WriteU8 (frag);
   7.264 +  i.WriteU8 (m_ttl);
   7.265 +  i.WriteU8 (m_protocol);
   7.266 +  i.WriteHtonU16 (0);
   7.267 +  i.WriteHtonU32 (m_source.Get ());
   7.268 +  i.WriteHtonU32 (m_destination.Get ());
   7.269 +
   7.270 +  if (m_calcChecksum) 
   7.271 +    {
   7.272 +#if 0
   7.273 +      // XXX we need to add Buffer::Iterator::PeekData method
   7.274 +      uint8_t *data = start.PeekData ();
   7.275 +      uint16_t checksum = UtilsChecksumCalculate (0, data, GetSize ());
   7.276 +      checksum = UtilsChecksumComplete (checksum);
   7.277 +      NS_LOG_LOGIC ("checksum=" <<checksum);
   7.278 +      i = start;
   7.279 +      i.Next (10);
   7.280 +      i.WriteU16 (checksum);
   7.281 +#endif
   7.282 +    }
   7.283 +}
   7.284 +uint32_t
   7.285 +Ipv4Header::Deserialize (Buffer::Iterator start)
   7.286 +{
   7.287 +  Buffer::Iterator i = start;
   7.288 +  uint8_t verIhl = i.ReadU8 ();
   7.289 +  uint8_t ihl = verIhl & 0x0f; 
   7.290 +  uint16_t headerSize = ihl * 4;
   7.291 +  NS_ASSERT ((verIhl >> 4) == 4);
   7.292 +  m_tos = i.ReadU8 ();
   7.293 +  uint16_t size = i.ReadNtohU16 ();
   7.294 +  m_payloadSize = size - headerSize;
   7.295 +  m_identification = i.ReadNtohU16 ();
   7.296 +  uint8_t flags = i.ReadU8 ();
   7.297 +  m_flags = 0;
   7.298 +  if (flags & (1<<6)) 
   7.299 +    {
   7.300 +      m_flags |= DONT_FRAGMENT;
   7.301 +    }
   7.302 +  if (flags & (1<<5)) 
   7.303 +    {
   7.304 +      m_flags |= MORE_FRAGMENTS;
   7.305 +    }
   7.306 +  //XXXX I think we should clear some bits in fragmentOffset !
   7.307 +  i.Prev ();
   7.308 +  m_fragmentOffset = i.ReadNtohU16 ();
   7.309 +  m_fragmentOffset *= 8;
   7.310 +  m_ttl = i.ReadU8 ();
   7.311 +  m_protocol = i.ReadU8 ();
   7.312 +  i.Next (2); // checksum
   7.313 +  m_source.Set (i.ReadNtohU32 ());
   7.314 +  m_destination.Set (i.ReadNtohU32 ());
   7.315 +
   7.316 +  if (m_calcChecksum) 
   7.317 +    {
   7.318 +#if 0
   7.319 +      uint8_t *data = start.PeekData ();
   7.320 +      uint16_t localChecksum = UtilsChecksumCalculate (0, data, headerSize);
   7.321 +      if (localChecksum == 0xffff) 
   7.322 +        {
   7.323 +          m_goodChecksum = true;
   7.324 +        } 
   7.325 +      else 
   7.326 +        {
   7.327 +          m_goodChecksum = false;
   7.328 +        }
   7.329 +#endif
   7.330 +    }
   7.331 +  return GetSerializedSize ();
   7.332 +}
   7.333 +
   7.334 +}; // namespace ns3
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/node/ipv4-header.h	Wed Jun 04 10:18:57 2008 -0700
     8.3 @@ -0,0 +1,171 @@
     8.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     8.5 +/*
     8.6 + * Copyright (c) 2005 INRIA
     8.7 + *
     8.8 + * This program is free software; you can redistribute it and/or modify
     8.9 + * it under the terms of the GNU General Public License version 2 as
    8.10 + * published by the Free Software Foundation;
    8.11 + *
    8.12 + * This program is distributed in the hope that it will be useful,
    8.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8.15 + * GNU General Public License for more details.
    8.16 + *
    8.17 + * You should have received a copy of the GNU General Public License
    8.18 + * along with this program; if not, write to the Free Software
    8.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    8.20 + *
    8.21 + * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    8.22 + */
    8.23 +
    8.24 +#ifndef IPV4_HEADER_H
    8.25 +#define IPV4_HEADER_H
    8.26 +
    8.27 +#include "ns3/header.h"
    8.28 +#include "ns3/ipv4-address.h"
    8.29 +
    8.30 +namespace ns3 {
    8.31 +/**
    8.32 + * \brief Packet header for IPv4
    8.33 + */
    8.34 +class Ipv4Header : public Header 
    8.35 +{
    8.36 +public:
    8.37 +  /**
    8.38 +   * \brief Construct a null IPv4 header
    8.39 +   */
    8.40 +  Ipv4Header ();
    8.41 +  /**
    8.42 +   * \brief Enable checksum calculation for IP (XXX currently has no effect)
    8.43 +   */
    8.44 +  static void EnableChecksums (void);
    8.45 +  /**
    8.46 +   * \param size the size of the payload in bytes
    8.47 +   */
    8.48 +  void SetPayloadSize (uint16_t size);
    8.49 +  /**
    8.50 +   * \param identification the Identification field of IPv4 packets.
    8.51 +   *
    8.52 +   * By default, set to zero.
    8.53 +   */
    8.54 +  void SetIdentification (uint16_t identification);
    8.55 +  /**
    8.56 +   * \param tos the 8 bits of Ipv4 TOS.
    8.57 +   */
    8.58 +  void SetTos (uint8_t tos);
    8.59 +  /**
    8.60 +   * This packet is not the last packet of a fragmented ipv4 packet.
    8.61 +   */
    8.62 +  void SetMoreFragments (void);
    8.63 +  /**
    8.64 +   * This packet is the last packet of a fragmented ipv4 packet.
    8.65 +   */
    8.66 +  void SetLastFragment (void);
    8.67 +  /**
    8.68 +   * Don't fragment this packet: if you need to anyway, drop it.
    8.69 +   */
    8.70 +  void SetDontFragment (void);
    8.71 +  /**
    8.72 +   * If you need to fragment this packet, you can do it.
    8.73 +   */
    8.74 +  void SetMayFragment (void);
    8.75 +  /**
    8.76 +   * \param offset the ipv4 fragment offset
    8.77 +   */
    8.78 +  void SetFragmentOffset (uint16_t offset);
    8.79 +  /**
    8.80 +   * \param ttl the ipv4 TTL
    8.81 +   */
    8.82 +  void SetTtl (uint8_t ttl);
    8.83 +  /**
    8.84 +   * \param num the ipv4 protocol field
    8.85 +   */
    8.86 +  void SetProtocol (uint8_t num);
    8.87 +  /**
    8.88 +   * \param source the source of this packet
    8.89 +   */
    8.90 +  void SetSource (Ipv4Address source);
    8.91 +  /**
    8.92 +   * \param destination the destination of this packet.
    8.93 +   */
    8.94 +  void SetDestination (Ipv4Address destination);
    8.95 +  /**
    8.96 +   * \returns the size of the payload in bytes
    8.97 +   */
    8.98 +  uint16_t GetPayloadSize (void) const;
    8.99 +  /**
   8.100 +   * \returns the identification field of this packet.
   8.101 +   */
   8.102 +  uint16_t GetIdentification (void) const;
   8.103 +  /**
   8.104 +   * \returns the TOS field of this packet.
   8.105 +   */
   8.106 +  uint8_t GetTos (void) const;
   8.107 +  /**
   8.108 +   * \returns true if this is the last fragment of a packet, false otherwise.
   8.109 +   */
   8.110 +  bool IsLastFragment (void) const;
   8.111 +  /**
   8.112 +   * \returns true if this is this packet can be fragmented.
   8.113 +   */  
   8.114 +  bool IsDontFragment (void) const;
   8.115 +  /**
   8.116 +   * \returns the offset of this fragment.
   8.117 +   */
   8.118 +  uint16_t GetFragmentOffset (void) const;
   8.119 +  /**
   8.120 +   * \returns the TTL field of this packet
   8.121 +   */
   8.122 +  uint8_t GetTtl (void) const;
   8.123 +  /**
   8.124 +   * \returns the protocol field of this packet
   8.125 +   */
   8.126 +  uint8_t GetProtocol (void) const;
   8.127 +  /**
   8.128 +   * \returns the source address of this packet
   8.129 +   */
   8.130 +  Ipv4Address GetSource (void) const;
   8.131 +  /**
   8.132 +   * \returns the destination address of this packet
   8.133 +   */
   8.134 +  Ipv4Address GetDestination (void) const;
   8.135 +  
   8.136 +  /**
   8.137 +   * \returns true if the upv4 checksum is correct, false otherwise.
   8.138 +   *
   8.139 +   * If Ipv4Header::EnableChecksums has not been called prior to
   8.140 +   * creating this packet, this method will always return true.
   8.141 +   */
   8.142 +  bool IsChecksumOk (void) const;
   8.143 +
   8.144 +  static TypeId GetTypeId (void);
   8.145 +  virtual TypeId GetInstanceTypeId (void) const;
   8.146 +  virtual void Print (std::ostream &os) const;
   8.147 +  virtual uint32_t GetSerializedSize (void) const;
   8.148 +  virtual void Serialize (Buffer::Iterator start) const;
   8.149 +  virtual uint32_t Deserialize (Buffer::Iterator start);
   8.150 +private:
   8.151 +
   8.152 +  enum FlagsE {
   8.153 +    DONT_FRAGMENT = (1<<0),
   8.154 +    MORE_FRAGMENTS = (1<<1)
   8.155 +  };
   8.156 +
   8.157 +  static bool m_calcChecksum;
   8.158 +
   8.159 +  uint16_t m_payloadSize;
   8.160 +  uint16_t m_identification;
   8.161 +  uint32_t m_tos : 8;
   8.162 +  uint32_t m_ttl : 8;
   8.163 +  uint32_t m_protocol : 8;
   8.164 +  uint32_t m_flags : 3;
   8.165 +  uint16_t m_fragmentOffset : 13;
   8.166 +  Ipv4Address m_source;
   8.167 +  Ipv4Address m_destination;
   8.168 +  bool m_goodChecksum;
   8.169 +};
   8.170 +
   8.171 +} // namespace ns3
   8.172 +
   8.173 +
   8.174 +#endif /* IPV4_HEADER_H */
     9.1 --- a/src/node/ipv4.h	Wed Jun 04 10:09:29 2008 -0700
     9.2 +++ b/src/node/ipv4.h	Wed Jun 04 10:18:57 2008 -0700
     9.3 @@ -32,8 +32,7 @@
     9.4  class NetDevice;
     9.5  class Packet;
     9.6  class Ipv4Route;
     9.7 -class Ipv4Header; // XXX: ipv4-header.h needs to move from module
     9.8 -                  // "internet-node" to module "node"
     9.9 +class Ipv4Header;
    9.10  
    9.11  /**
    9.12   * \ingroup node
    10.1 --- a/src/node/wscript	Wed Jun 04 10:09:29 2008 -0700
    10.2 +++ b/src/node/wscript	Wed Jun 04 10:18:57 2008 -0700
    10.3 @@ -11,6 +11,7 @@
    10.4          'node.cc',
    10.5          'ipv4-address.cc',
    10.6          'ipv4-address-generator.cc',
    10.7 +        'ipv4-header.cc',
    10.8          'net-device.cc',
    10.9  	'address-utils.cc',
   10.10          'llc-snap-header.cc',
   10.11 @@ -46,6 +47,7 @@
   10.12          'node.h',
   10.13          'ipv4-address.h',
   10.14          'ipv4-address-generator.h',
   10.15 +        'ipv4-header.h',
   10.16          'net-device.h',
   10.17  	'address-utils.h',
   10.18          'ipv4-route.h',