A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-header.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#ifndef UDP_HEADER_H
10#define UDP_HEADER_H
11
12#include "ns3/header.h"
13#include "ns3/ipv4-address.h"
14#include "ns3/ipv6-address.h"
15
16#include <stdint.h>
17#include <string>
18
19namespace ns3
20{
21/**
22 * \ingroup udp
23 * \brief Packet header for UDP packets
24 *
25 * This class has fields corresponding to those in a network UDP header
26 * (port numbers, payload size, checksum) as well as methods for serialization
27 * to and deserialization from a byte buffer.
28 */
29class UdpHeader : public Header
30{
31 public:
32 /**
33 * \brief Enable checksum calculation for UDP
34 */
35 void EnableChecksums();
36 /**
37 * \param port the destination port for this UdpHeader
38 */
39 void SetDestinationPort(uint16_t port);
40 /**
41 * \param port The source port for this UdpHeader
42 */
43 void SetSourcePort(uint16_t port);
44 /**
45 * \return The source port for this UdpHeader
46 */
47 uint16_t GetSourcePort() const;
48 /**
49 * \return the destination port for this UdpHeader
50 */
51 uint16_t GetDestinationPort() const;
52
53 /**
54 * \param source the ip source to use in the underlying
55 * ip packet.
56 * \param destination the ip destination to use in the
57 * underlying ip packet.
58 * \param protocol the protocol number to use in the underlying
59 * ip packet.
60 *
61 * If you want to use udp checksums, you should call this
62 * method prior to adding the header to a packet.
63 */
64 void InitializeChecksum(Address source, Address destination, uint8_t protocol);
65
66 /**
67 * \param source the ip source to use in the underlying
68 * ip packet.
69 * \param destination the ip destination to use in the
70 * underlying ip packet.
71 * \param protocol the protocol number to use in the underlying
72 * ip packet.
73 *
74 * If you want to use udp checksums, you should call this
75 * method prior to adding the header to a packet.
76 */
77 void InitializeChecksum(Ipv4Address source, Ipv4Address destination, uint8_t protocol);
78
79 /**
80 * \param source the ip source to use in the underlying
81 * ip packet.
82 * \param destination the ip destination to use in the
83 * underlying ip packet.
84 * \param protocol the protocol number to use in the underlying
85 * ip packet.
86 *
87 * If you want to use udp checksums, you should call this
88 * method prior to adding the header to a packet.
89 */
90 void InitializeChecksum(Ipv6Address source, Ipv6Address destination, uint8_t protocol);
91
92 /**
93 * \brief Get the type ID.
94 * \return the object TypeId
95 */
96 static TypeId GetTypeId();
97 TypeId GetInstanceTypeId() const override;
98 void Print(std::ostream& os) const override;
99 uint32_t GetSerializedSize() const override;
100 void Serialize(Buffer::Iterator start) const override;
101 uint32_t Deserialize(Buffer::Iterator start) override;
102
103 /**
104 * \brief Is the UDP checksum correct ?
105 * \returns true if the checksum is correct, false otherwise.
106 */
107 bool IsChecksumOk() const;
108
109 /**
110 * \brief Force the UDP checksum to a given value.
111 *
112 * This might be useful for test purposes or to
113 * restore the UDP checksum when the UDP header
114 * has been compressed (e.g., in 6LoWPAN).
115 * Note that, normally, the header checksum is
116 * calculated on the fly when the packet is
117 * serialized.
118 *
119 * When this option is used, the UDP checksum is written in
120 * the header, regardless of the global ChecksumEnabled option.
121 *
122 * \note The checksum value must be a big endian number.
123 *
124 * \param checksum the checksum to use (big endian).
125 */
126 void ForceChecksum(uint16_t checksum);
127
128 /**
129 * \brief Force the UDP payload length to a given value.
130 *
131 * This might be useful when forging a packet for test
132 * purposes.
133 *
134 * \param payloadSize the payload length to use.
135 */
136 void ForcePayloadSize(uint16_t payloadSize);
137
138 /**
139 * \brief Return the checksum (only known after a Deserialize)
140 * \return The checksum for this UdpHeader
141 */
142 uint16_t GetChecksum() const;
143
144 private:
145 /**
146 * \brief Calculate the header checksum
147 * \param size packet size
148 * \returns the checksum
149 */
150 uint16_t CalculateHeaderChecksum(uint16_t size) const;
151
152 // The magic values below are used only for debugging.
153 // They can be used to easily detect memory corruption
154 // problems so you can see the patterns in memory.
155 uint16_t m_sourcePort{0xfffd}; //!< Source port
156 uint16_t m_destinationPort{0xfffd}; //!< Destination port
157 uint16_t m_payloadSize{0}; //!< Payload size
158 uint16_t m_forcedPayloadSize{0}; //!< Payload size (forced)
159
160 Address m_source; //!< Source IP address
161 Address m_destination; //!< Destination IP address
162 uint8_t m_protocol{17}; //!< Protocol number
163 uint16_t m_checksum{0}; //!< Forced Checksum value
164 bool m_calcChecksum{false}; //!< Flag to calculate checksum
165 bool m_goodChecksum{true}; //!< Flag to indicate that checksum is correct
166};
167
168} // namespace ns3
169
170#endif /* UDP_HEADER */
a polymophic address class
Definition address.h:90
iterator in a Buffer instance
Definition buffer.h:89
Protocol header serialization and deserialization.
Definition header.h:33
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
a unique identifier for an interface.
Definition type-id.h:48
Packet header for UDP packets.
Definition udp-header.h:30
uint32_t GetSerializedSize() const override
void Serialize(Buffer::Iterator start) const override
Address m_destination
Destination IP address.
Definition udp-header.h:161
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition udp-header.cc:73
void EnableChecksums()
Enable checksum calculation for UDP.
Definition udp-header.cc:19
uint8_t m_protocol
Protocol number.
Definition udp-header.h:162
uint16_t m_destinationPort
Destination port.
Definition udp-header.h:156
uint16_t GetDestinationPort() const
Definition udp-header.cc:43
Address m_source
Source IP address.
Definition udp-header.h:160
uint16_t m_payloadSize
Payload size.
Definition udp-header.h:157
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
uint16_t m_sourcePort
Source port.
Definition udp-header.h:155
uint16_t GetSourcePort() const
Definition udp-header.cc:37
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
bool m_calcChecksum
Flag to calculate checksum.
Definition udp-header.h:164
void Print(std::ostream &os) const override
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
bool IsChecksumOk() const
Is the UDP checksum correct ?
uint32_t Deserialize(Buffer::Iterator start) override
uint16_t m_forcedPayloadSize
Payload size (forced)
Definition udp-header.h:158
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
uint16_t m_checksum
Forced Checksum value.
Definition udp-header.h:163
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition udp-header.cc:49
static TypeId GetTypeId()
Get the type ID.
void SetSourcePort(uint16_t port)
Definition udp-header.cc:31
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition udp-header.h:165
void SetDestinationPort(uint16_t port)
Definition udp-header.cc:25
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.