A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
udp-header.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #ifndef UDP_HEADER_H
21 #define UDP_HEADER_H
22 
23 #include "ns3/header.h"
24 #include "ns3/ipv4-address.h"
25 #include "ns3/ipv6-address.h"
26 
27 #include <stdint.h>
28 #include <string>
29 
30 namespace ns3
31 {
40 class UdpHeader : public Header
41 {
42  public:
46  void EnableChecksums();
50  void SetDestinationPort(uint16_t port);
54  void SetSourcePort(uint16_t port);
58  uint16_t GetSourcePort() const;
62  uint16_t GetDestinationPort() const;
63 
75  void InitializeChecksum(Address source, Address destination, uint8_t protocol);
76 
88  void InitializeChecksum(Ipv4Address source, Ipv4Address destination, uint8_t protocol);
89 
101  void InitializeChecksum(Ipv6Address source, Ipv6Address destination, uint8_t protocol);
102 
107  static TypeId GetTypeId();
108  TypeId GetInstanceTypeId() const override;
109  void Print(std::ostream& os) const override;
110  uint32_t GetSerializedSize() const override;
111  void Serialize(Buffer::Iterator start) const override;
112  uint32_t Deserialize(Buffer::Iterator start) override;
113 
118  bool IsChecksumOk() const;
119 
137  void ForceChecksum(uint16_t checksum);
138 
147  void ForcePayloadSize(uint16_t payloadSize);
148 
153  uint16_t GetChecksum() const;
154 
155  private:
161  uint16_t CalculateHeaderChecksum(uint16_t size) const;
162 
163  // The magic values below are used only for debugging.
164  // They can be used to easily detect memory corruption
165  // problems so you can see the patterns in memory.
166  uint16_t m_sourcePort{0xfffd};
167  uint16_t m_destinationPort{0xfffd};
168  uint16_t m_payloadSize{0};
169  uint16_t m_forcedPayloadSize{0};
170 
173  uint8_t m_protocol{17};
174  uint16_t m_checksum{0};
175  bool m_calcChecksum{false};
176  bool m_goodChecksum{true};
177 };
178 
179 } // namespace ns3
180 
181 #endif /* UDP_HEADER */
a polymophic address class
Definition: address.h:101
iterator in a Buffer instance
Definition: buffer.h:100
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
a unique identifier for an interface.
Definition: type-id.h:59
Packet header for UDP packets.
Definition: udp-header.h:41
uint32_t GetSerializedSize() const override
Definition: udp-header.cc:159
void Serialize(Buffer::Iterator start) const override
Definition: udp-header.cc:165
Address m_destination
Destination IP address.
Definition: udp-header.h:172
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition: udp-header.cc:84
void EnableChecksums()
Enable checksum calculation for UDP.
Definition: udp-header.cc:30
uint8_t m_protocol
Protocol number.
Definition: udp-header.h:173
uint16_t m_destinationPort
Destination port.
Definition: udp-header.h:167
uint16_t GetDestinationPort() const
Definition: udp-header.cc:54
Address m_source
Source IP address.
Definition: udp-header.h:171
uint16_t m_payloadSize
Payload size.
Definition: udp-header.h:168
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition: udp-header.cc:124
uint16_t m_sourcePort
Source port.
Definition: udp-header.h:166
uint16_t GetSourcePort() const
Definition: udp-header.cc:48
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: udp-header.cc:146
bool m_calcChecksum
Flag to calculate checksum.
Definition: udp-header.h:175
void Print(std::ostream &os) const override
Definition: udp-header.cc:152
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition: udp-header.cc:130
bool IsChecksumOk() const
Is the UDP checksum correct ?
Definition: udp-header.cc:118
uint16_t m_forcedPayloadSize
Payload size (forced)
Definition: udp-header.h:169
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
Definition: udp-header.cc:223
uint16_t m_checksum
Forced Checksum value.
Definition: udp-header.h:174
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition: udp-header.cc:60
static TypeId GetTypeId()
Get the type ID.
Definition: udp-header.cc:136
void SetSourcePort(uint16_t port)
Definition: udp-header.cc:42
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition: udp-header.h:176
void SetDestinationPort(uint16_t port)
Definition: udp-header.cc:36
uint16_t port
Definition: dsdv-manet.cc:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.