A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-l4-protocol.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006,2007 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_L4_PROTOCOL_H
10#define UDP_L4_PROTOCOL_H
11
12#include "ip-l4-protocol.h"
13
14#include "ns3/packet.h"
15#include "ns3/ptr.h"
16
17#include <stdint.h>
18#include <unordered_map>
19
20namespace ns3
21{
22
23class Node;
24class Socket;
25class Ipv4EndPointDemux;
26class Ipv4EndPoint;
27class Ipv6EndPointDemux;
28class Ipv6EndPoint;
29class UdpSocketImpl;
30class NetDevice;
31
32/**
33 * @ingroup internet
34 * @defgroup udp UDP
35 *
36 * This is an implementation of the User Datagram Protocol described in
37 * \RFC{768}. It implements a connectionless, unreliable datagram packet
38 * service. Packets may be reordered or duplicated before they arrive.
39 * UDP generates and checks checksums to catch transmission errors.
40 *
41 * The following options are not presently part of this implementation:
42 * UDP_CORK, MSG_DONTROUTE, path MTU discovery control (e.g.
43 * IP_MTU_DISCOVER). MTU handling is also weak in ns-3 for the moment;
44 * it is best to send datagrams that do not exceed 1500 byte MTU (e.g.
45 * 1472 byte UDP datagrams)
46 */
47
48/**
49 * @ingroup udp
50 * @brief Implementation of the UDP protocol
51 */
53{
54 public:
55 /**
56 * @brief Get the type ID.
57 * @return the object TypeId
58 */
59 static TypeId GetTypeId();
60
61 /// Protocol number (see http://www.iana.org/assignments/protocol-numbers)
62 static constexpr uint8_t PROT_NUMBER = 17;
63
65 ~UdpL4Protocol() override;
66
67 // Delete copy constructor and assignment operator to avoid misuse
68 UdpL4Protocol(const UdpL4Protocol&) = delete;
70
71 /**
72 * Set node associated with this stack
73 * @param node the node
74 */
75 void SetNode(Ptr<Node> node);
76
77 int GetProtocolNumber() const override;
78
79 /**
80 * @return A smart Socket pointer to a UdpSocket, allocated by this instance
81 * of the UDP protocol
82 */
84
85 /**
86 * @brief Allocate an IPv4 Endpoint
87 * @return the Endpoint
88 */
90 /**
91 * @brief Allocate an IPv4 Endpoint
92 * @param address address to use
93 * @return the Endpoint
94 */
96 /**
97 * @brief Allocate an IPv4 Endpoint
98 * @param boundNetDevice Bound NetDevice (if any)
99 * @param port port to use
100 * @return the Endpoint
101 */
102 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, uint16_t port);
103 /**
104 * @brief Allocate an IPv4 Endpoint
105 * @param boundNetDevice Bound NetDevice (if any)
106 * @param address address to use
107 * @param port port to use
108 * @return the Endpoint
109 */
110 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, Ipv4Address address, uint16_t port);
111 /**
112 * @brief Allocate an IPv4 Endpoint
113 * @param boundNetDevice Bound NetDevice (if any)
114 * @param localAddress local address to use
115 * @param localPort local port to use
116 * @param peerAddress remote address to use
117 * @param peerPort remote port to use
118 * @return the Endpoint
119 */
120 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice,
121 Ipv4Address localAddress,
122 uint16_t localPort,
123 Ipv4Address peerAddress,
124 uint16_t peerPort);
125
126 /**
127 * @brief Allocate an IPv6 Endpoint
128 * @return the Endpoint
129 */
131 /**
132 * @brief Allocate an IPv6 Endpoint
133 * @param address address to use
134 * @return the Endpoint
135 */
137 /**
138 * @brief Allocate an IPv6 Endpoint
139 * @param boundNetDevice Bound NetDevice (if any)
140 * @param port port to use
141 * @return the Endpoint
142 */
143 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, uint16_t port);
144 /**
145 * @brief Allocate an IPv6 Endpoint
146 * @param boundNetDevice Bound NetDevice (if any)
147 * @param address address to use
148 * @param port port to use
149 * @return the Endpoint
150 */
151 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, Ipv6Address address, uint16_t port);
152 /**
153 * @brief Allocate an IPv6 Endpoint
154 * @param boundNetDevice Bound NetDevice (if any)
155 * @param localAddress local address to use
156 * @param localPort local port to use
157 * @param peerAddress remote address to use
158 * @param peerPort remote port to use
159 * @return the Endpoint
160 */
161 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice,
162 Ipv6Address localAddress,
163 uint16_t localPort,
164 Ipv6Address peerAddress,
165 uint16_t peerPort);
166
167 /**
168 * @brief Remove an IPv4 Endpoint.
169 * @param endPoint the end point to remove
170 */
171 void DeAllocate(Ipv4EndPoint* endPoint);
172 /**
173 * @brief Remove an IPv6 Endpoint.
174 * @param endPoint the end point to remove
175 */
176 void DeAllocate(Ipv6EndPoint* endPoint);
177
178 /**
179 * @brief Remove a socket from the internal list
180 *
181 * @param socket socket to remove
182 * @return true if the socket has been removed
183 */
184 bool RemoveSocket(Ptr<UdpSocketImpl> socket);
185
186 // called by UdpSocket.
187 /**
188 * @brief Send a packet via UDP (IPv4)
189 * @param packet The packet to send
190 * @param saddr The source Ipv4Address
191 * @param daddr The destination Ipv4Address
192 * @param sport The source port number
193 * @param dport The destination port number
194 */
195 void Send(Ptr<Packet> packet,
196 Ipv4Address saddr,
197 Ipv4Address daddr,
198 uint16_t sport,
199 uint16_t dport);
200 /**
201 * @brief Send a packet via UDP (IPv4)
202 * @param packet The packet to send
203 * @param saddr The source Ipv4Address
204 * @param daddr The destination Ipv4Address
205 * @param sport The source port number
206 * @param dport The destination port number
207 * @param route The route
208 */
209 void Send(Ptr<Packet> packet,
210 Ipv4Address saddr,
211 Ipv4Address daddr,
212 uint16_t sport,
213 uint16_t dport,
214 Ptr<Ipv4Route> route);
215 /**
216 * @brief Send a packet via UDP (IPv6)
217 * @param packet The packet to send
218 * @param saddr The source Ipv4Address
219 * @param daddr The destination Ipv4Address
220 * @param sport The source port number
221 * @param dport The destination port number
222 */
223 void Send(Ptr<Packet> packet,
224 Ipv6Address saddr,
225 Ipv6Address daddr,
226 uint16_t sport,
227 uint16_t dport);
228 /**
229 * @brief Send a packet via UDP (IPv6)
230 * @param packet The packet to send
231 * @param saddr The source Ipv4Address
232 * @param daddr The destination Ipv4Address
233 * @param sport The source port number
234 * @param dport The destination port number
235 * @param route The route
236 */
237 void Send(Ptr<Packet> packet,
238 Ipv6Address saddr,
239 Ipv6Address daddr,
240 uint16_t sport,
241 uint16_t dport,
242 Ptr<Ipv6Route> route);
243
244 // inherited from Ipv4L4Protocol
246 const Ipv4Header& header,
247 Ptr<Ipv4Interface> interface) override;
249 const Ipv6Header& header,
250 Ptr<Ipv6Interface> interface) override;
251
252 void ReceiveIcmp(Ipv4Address icmpSource,
253 uint8_t icmpTtl,
254 uint8_t icmpType,
255 uint8_t icmpCode,
256 uint32_t icmpInfo,
257 Ipv4Address payloadSource,
258 Ipv4Address payloadDestination,
259 const uint8_t payload[8]) override;
260 void ReceiveIcmp(Ipv6Address icmpSource,
261 uint8_t icmpTtl,
262 uint8_t icmpType,
263 uint8_t icmpCode,
264 uint32_t icmpInfo,
265 Ipv6Address payloadSource,
266 Ipv6Address payloadDestination,
267 const uint8_t payload[8]) override;
268
269 // From IpL4Protocol
272 // From IpL4Protocol
275
276 protected:
277 void DoDispose() override;
278 /*
279 * This function will notify other components connected to the node that a new stack member is
280 * now connected This will be used to notify Layer 3 protocol of layer 4 protocol stack to
281 * connect them together.
282 */
283 void NotifyNewAggregate() override;
284
285 private:
286 Ptr<Node> m_node; //!< The node this stack is associated with
287 Ipv4EndPointDemux* m_endPoints; //!< A list of IPv4 end points.
288 Ipv6EndPointDemux* m_endPoints6; //!< A list of IPv6 end points.
289
290 std::unordered_map<uint64_t, Ptr<UdpSocketImpl>>
291 m_sockets; //!< Unordered map of socket IDs and corresponding sockets
292 uint64_t m_socketIndex{0}; //!< Index of the next socket to be created
293 IpL4Protocol::DownTargetCallback m_downTarget; //!< Callback to send packets over IPv4
294 IpL4Protocol::DownTargetCallback6 m_downTarget6; //!< Callback to send packets over IPv6
295};
296
297} // namespace ns3
298
299#endif /* UDP_L4_PROTOCOL_H */
L4 Protocol abstract base class.
RxStatus
Rx status codes.
Ipv4 addresses are stored in host order in this class.
Demultiplexes packets to various transport layer endpoints.
A representation of an internet endpoint/connection.
Packet header for IPv4.
Definition ipv4-header.h:23
Describes an IPv6 address.
Demultiplexer for end points.
A representation of an IPv6 endpoint/connection.
Packet header for IPv6.
Definition ipv6-header.h:24
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:49
Implementation of the UDP protocol.
void SetDownTarget6(IpL4Protocol::DownTargetCallback6 cb) override
This method allows a caller to set the current down target callback set for this L4 protocol (IPv6 ca...
Ptr< Socket > CreateSocket()
void Send(Ptr< Packet > packet, Ipv4Address saddr, Ipv4Address daddr, uint16_t sport, uint16_t dport)
Send a packet via UDP (IPv4)
Ipv6EndPoint * Allocate6()
Allocate an IPv6 Endpoint.
void SetDownTarget(IpL4Protocol::DownTargetCallback cb) override
This method allows a caller to set the current down target callback set for this L4 protocol (IPv4 ca...
std::unordered_map< uint64_t, Ptr< UdpSocketImpl > > m_sockets
Unordered map of socket IDs and corresponding sockets.
UdpL4Protocol(const UdpL4Protocol &)=delete
Ipv6EndPointDemux * m_endPoints6
A list of IPv6 end points.
int GetProtocolNumber() const override
Returns the protocol number of this protocol.
Ptr< Node > m_node
The node this stack is associated with.
Ipv4EndPointDemux * m_endPoints
A list of IPv4 end points.
void ReceiveIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo, Ipv4Address payloadSource, Ipv4Address payloadDestination, const uint8_t payload[8]) override
Called from lower-level layers to send the ICMP packet up in the stack.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
IpL4Protocol::DownTargetCallback GetDownTarget() const override
This method allows a caller to get the current down target callback set for this L4 protocol (IPv4 ca...
static TypeId GetTypeId()
Get the type ID.
IpL4Protocol::DownTargetCallback6 GetDownTarget6() const override
This method allows a caller to get the current down target callback set for this L4 protocol (IPv6 ca...
Ipv4EndPoint * Allocate()
Allocate an IPv4 Endpoint.
IpL4Protocol::RxStatus Receive(Ptr< Packet > p, const Ipv4Header &header, Ptr< Ipv4Interface > interface) override
Called from lower-level layers to send the packet up in the stack.
bool RemoveSocket(Ptr< UdpSocketImpl > socket)
Remove a socket from the internal list.
IpL4Protocol::DownTargetCallback m_downTarget
Callback to send packets over IPv4.
IpL4Protocol::DownTargetCallback6 m_downTarget6
Callback to send packets over IPv6.
UdpL4Protocol & operator=(const UdpL4Protocol &)=delete
void DoDispose() override
Destructor implementation.
void NotifyNewAggregate() override
Notify all Objects aggregated to this one of a new Object being aggregated.
void DeAllocate(Ipv4EndPoint *endPoint)
Remove an IPv4 Endpoint.
static constexpr uint8_t PROT_NUMBER
Protocol number (see http://www.iana.org/assignments/protocol-numbers)
uint64_t m_socketIndex
Index of the next socket to be created.
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.