00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #ifndef UDP_HEADER_H 00022 #define UDP_HEADER_H 00023 00024 #include <stdint.h> 00025 #include <string> 00026 #include "ns3/header.h" 00027 #include "ns3/ipv4-address.h" 00028 00029 namespace ns3 { 00030 /** 00031 * \ingroup udp 00032 * \brief Packet header for UDP packets 00033 * 00034 * This class has fields corresponding to those in a network UDP header 00035 * (port numbers, payload size, checksum) as well as methods for serialization 00036 * to and deserialization from a byte buffer. 00037 */ 00038 class UdpHeader : public Header 00039 { 00040 public: 00041 00042 /** 00043 * \brief Constructor 00044 * 00045 * Creates a null header 00046 */ 00047 UdpHeader (); 00048 ~UdpHeader (); 00049 00050 /** 00051 * \brief Enable checksum calculation for UDP (XXX currently has no effect) 00052 */ 00053 void EnableChecksums (void); 00054 /** 00055 * \param port the destination port for this UdpHeader 00056 */ 00057 void SetDestinationPort (uint16_t port); 00058 /** 00059 * \param port The source port for this UdpHeader 00060 */ 00061 void SetSourcePort (uint16_t port); 00062 /** 00063 * \return The source port for this UdpHeader 00064 */ 00065 uint16_t GetSourcePort (void) const; 00066 /** 00067 * \return the destination port for this UdpHeader 00068 */ 00069 uint16_t GetDestinationPort (void) const; 00070 00071 /** 00072 * \param source the ip source to use in the underlying 00073 * ip packet. 00074 * \param destination the ip destination to use in the 00075 * underlying ip packet. 00076 * \param protocol the protocol number to use in the underlying 00077 * ip packet. 00078 * 00079 * If you want to use udp checksums, you should call this 00080 * method prior to adding the header to a packet. 00081 */ 00082 void InitializeChecksum (Ipv4Address source, 00083 Ipv4Address destination, 00084 uint8_t protocol); 00085 00086 static TypeId GetTypeId (void); 00087 virtual TypeId GetInstanceTypeId (void) const; 00088 virtual void Print (std::ostream &os) const; 00089 virtual uint32_t GetSerializedSize (void) const; 00090 virtual void Serialize (Buffer::Iterator start) const; 00091 virtual uint32_t Deserialize (Buffer::Iterator start); 00092 00093 /** 00094 * \brief Is the UDP checksum correct ? 00095 * \returns true if the checksum is correct, false otherwise. 00096 */ 00097 bool IsChecksumOk (void) const; 00098 00099 private: 00100 uint16_t CalculateHeaderChecksum (uint16_t size) const; 00101 uint16_t m_sourcePort; 00102 uint16_t m_destinationPort; 00103 uint16_t m_payloadSize; 00104 00105 Ipv4Address m_source; 00106 Ipv4Address m_destination; 00107 uint8_t m_protocol; 00108 bool m_calcChecksum; 00109 bool m_goodChecksum; 00110 }; 00111 00112 } // namespace ns3 00113 00114 #endif /* UDP_HEADER */