1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef UDP_HEADER_H
22 #define UDP_HEADER_H
23 
24 #include <stdint.h>
25 #include <string>
26 #include "ns3/header.h"
27 #include "ns3/ipv4-address.h"
28 #include "ns3/ipv6-address.h"
29 
30 namespace ns3 {
31 /**
32  * \ingroup udp
33  * \brief Packet header for UDP packets
34  *
35  * This class has fields corresponding to those in a network UDP header
36  * (port numbers, payload size, checksum) as well as methods for serialization
37  * to and deserialization from a byte buffer.
38  */
39 class UdpHeader : public Header
40 {
41 public:
42 
43   /**
44    * \brief Constructor
45    *
46    * Creates a null header
47    */
48   UdpHeader ();
49   ~UdpHeader ();
50 
51   /**
52    * \brief Enable checksum calculation for UDP
53    */
54   void EnableChecksums (void);
55   /**
56    * \param port the destination port for this UdpHeader
57    */
58   void SetDestinationPort (uint16_t port);
59   /**
60    * \param port The source port for this UdpHeader
61    */
62   void SetSourcePort (uint16_t port);
63   /**
64    * \return The source port for this UdpHeader
65    */
66   uint16_t GetSourcePort (void) const;
67   /**
68    * \return the destination port for this UdpHeader
69    */
70   uint16_t GetDestinationPort (void) const;
71 
72   /**
73    * \param source the ip source to use in the underlying
74    *        ip packet.
75    * \param destination the ip destination to use in the
76    *        underlying ip packet.
77    * \param protocol the protocol number to use in the underlying
78    *        ip packet.
79    *
80    * If you want to use udp checksums, you should call this
81    * method prior to adding the header to a packet.
82    */
83   void InitializeChecksum (Address source,
84                            Address destination,
85                            uint8_t protocol);
86 
87   /**
88    * \param source the ip source to use in the underlying
89    *        ip packet.
90    * \param destination the ip destination to use in the
91    *        underlying ip packet.
92    * \param protocol the protocol number to use in the underlying
93    *        ip packet.
94    *
95    * If you want to use udp checksums, you should call this
96    * method prior to adding the header to a packet.
97    */
98   void InitializeChecksum (Ipv4Address source,
99                            Ipv4Address destination,
100                            uint8_t protocol);
101 
102   /**
103    * \param source the ip source to use in the underlying
104    *        ip packet.
105    * \param destination the ip destination to use in the
106    *        underlying ip packet.
107    * \param protocol the protocol number to use in the underlying
108    *        ip packet.
109    *
110    * If you want to use udp checksums, you should call this
111    * method prior to adding the header to a packet.
112    */
113   void InitializeChecksum (Ipv6Address source,
114                            Ipv6Address destination,
115                            uint8_t protocol);
116 
117   /**
118    * \brief Get the type ID.
119    * \return the object TypeId
120    */
121   static TypeId GetTypeId (void);
122   virtual TypeId GetInstanceTypeId (void) const;
123   virtual void Print (std::ostream &os) const;
124   virtual uint32_t GetSerializedSize (void) const;
125   virtual void Serialize (Buffer::Iterator start) const;
126   virtual uint32_t Deserialize (Buffer::Iterator start);
127 
128   /**
129    * \brief Is the UDP checksum correct ?
130    * \returns true if the checksum is correct, false otherwise.
131    */
132   bool IsChecksumOk (void) const;
133 
134   /**
135    * \brief Force the UDP checksum to a given value.
136    *
137    * This might be useful for test purposes or to
138    * restore the UDP checksum when the UDP header
139    * has been compressed (e.g., in 6LoWPAN).
140    * Note that, normally, the header checksum is
141    * calculated on the fly when the packet is
142    * serialized.
143    *
144    * When this option is used, the UDP checksum is written in
145    * the header, regardless of the global ChecksumEnabled option.
146    *
147    * \note The checksum value must be a big endian number.
148    *
149    * \param checksum the checksum to use (big endian).
150    */
151   void ForceChecksum (uint16_t checksum);
152 
153   /**
154    * \brief Force the UDP payload length to a given value.
155    *
156    * This might be useful when forging a packet for test
157    * purposes.
158    *
159    * \param payloadSize the payload length to use.
160    */
161   void ForcePayloadSize (uint16_t payloadSize);
162 
163   /**
164    * \brief Return the checksum (only known after a Deserialize)
165    * \return The checksum for this UdpHeader
166    */
167   uint16_t GetChecksum ();
168 
169 private:
170   /**
171    * \brief Calculate the header checksum
172    * \param size packet size
173    * \returns the checksum
174    */
175   uint16_t CalculateHeaderChecksum (uint16_t size) const;
176   uint16_t m_sourcePort;      //!< Source port
177   uint16_t m_destinationPort; //!< Destination port
178   uint16_t m_payloadSize;     //!< Payload size
179 
180   Address m_source;           //!< Source IP address
181   Address m_destination;      //!< Destination IP address
182   uint8_t m_protocol;         //!< Protocol number
183   uint16_t m_checksum;        //!< Forced Checksum value
184   bool m_calcChecksum;        //!< Flag to calculate checksum
185   bool m_goodChecksum;        //!< Flag to indicate that checksum is correct
186 };
187 
188 } // namespace ns3
189 
190 #endif /* UDP_HEADER */
191