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 static const uint8_t PROT_NUMBER; //!< protocol number (0x11)
61
63 ~UdpL4Protocol() override;
64
65 // Delete copy constructor and assignment operator to avoid misuse
66 UdpL4Protocol(const UdpL4Protocol&) = delete;
68
69 /**
70 * Set node associated with this stack
71 * \param node the node
72 */
73 void SetNode(Ptr<Node> node);
74
75 int GetProtocolNumber() const override;
76
77 /**
78 * \return A smart Socket pointer to a UdpSocket, allocated by this instance
79 * of the UDP protocol
80 */
82
83 /**
84 * \brief Allocate an IPv4 Endpoint
85 * \return the Endpoint
86 */
88 /**
89 * \brief Allocate an IPv4 Endpoint
90 * \param address address to use
91 * \return the Endpoint
92 */
94 /**
95 * \brief Allocate an IPv4 Endpoint
96 * \param boundNetDevice Bound NetDevice (if any)
97 * \param port port to use
98 * \return the Endpoint
99 */
100 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, uint16_t port);
101 /**
102 * \brief Allocate an IPv4 Endpoint
103 * \param boundNetDevice Bound NetDevice (if any)
104 * \param address address to use
105 * \param port port to use
106 * \return the Endpoint
107 */
108 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, Ipv4Address address, uint16_t port);
109 /**
110 * \brief Allocate an IPv4 Endpoint
111 * \param boundNetDevice Bound NetDevice (if any)
112 * \param localAddress local address to use
113 * \param localPort local port to use
114 * \param peerAddress remote address to use
115 * \param peerPort remote port to use
116 * \return the Endpoint
117 */
118 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice,
119 Ipv4Address localAddress,
120 uint16_t localPort,
121 Ipv4Address peerAddress,
122 uint16_t peerPort);
123
124 /**
125 * \brief Allocate an IPv6 Endpoint
126 * \return the Endpoint
127 */
129 /**
130 * \brief Allocate an IPv6 Endpoint
131 * \param address address to use
132 * \return the Endpoint
133 */
135 /**
136 * \brief Allocate an IPv6 Endpoint
137 * \param boundNetDevice Bound NetDevice (if any)
138 * \param port port to use
139 * \return the Endpoint
140 */
141 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, uint16_t port);
142 /**
143 * \brief Allocate an IPv6 Endpoint
144 * \param boundNetDevice Bound NetDevice (if any)
145 * \param address address to use
146 * \param port port to use
147 * \return the Endpoint
148 */
149 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, Ipv6Address address, uint16_t port);
150 /**
151 * \brief Allocate an IPv6 Endpoint
152 * \param boundNetDevice Bound NetDevice (if any)
153 * \param localAddress local address to use
154 * \param localPort local port to use
155 * \param peerAddress remote address to use
156 * \param peerPort remote port to use
157 * \return the Endpoint
158 */
159 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice,
160 Ipv6Address localAddress,
161 uint16_t localPort,
162 Ipv6Address peerAddress,
163 uint16_t peerPort);
164
165 /**
166 * \brief Remove an IPv4 Endpoint.
167 * \param endPoint the end point to remove
168 */
169 void DeAllocate(Ipv4EndPoint* endPoint);
170 /**
171 * \brief Remove an IPv6 Endpoint.
172 * \param endPoint the end point to remove
173 */
174 void DeAllocate(Ipv6EndPoint* endPoint);
175
176 /**
177 * \brief Remove a socket from the internal list
178 *
179 * \param socket socket to remove
180 * \return true if the socket has been removed
181 */
182 bool RemoveSocket(Ptr<UdpSocketImpl> socket);
183
184 // called by UdpSocket.
185 /**
186 * \brief Send a packet via UDP (IPv4)
187 * \param packet The packet to send
188 * \param saddr The source Ipv4Address
189 * \param daddr The destination Ipv4Address
190 * \param sport The source port number
191 * \param dport The destination port number
192 */
193 void Send(Ptr<Packet> packet,
194 Ipv4Address saddr,
195 Ipv4Address daddr,
196 uint16_t sport,
197 uint16_t dport);
198 /**
199 * \brief Send a packet via UDP (IPv4)
200 * \param packet The packet to send
201 * \param saddr The source Ipv4Address
202 * \param daddr The destination Ipv4Address
203 * \param sport The source port number
204 * \param dport The destination port number
205 * \param route The route
206 */
207 void Send(Ptr<Packet> packet,
208 Ipv4Address saddr,
209 Ipv4Address daddr,
210 uint16_t sport,
211 uint16_t dport,
212 Ptr<Ipv4Route> route);
213 /**
214 * \brief Send a packet via UDP (IPv6)
215 * \param packet The packet to send
216 * \param saddr The source Ipv4Address
217 * \param daddr The destination Ipv4Address
218 * \param sport The source port number
219 * \param dport The destination port number
220 */
221 void Send(Ptr<Packet> packet,
222 Ipv6Address saddr,
223 Ipv6Address daddr,
224 uint16_t sport,
225 uint16_t dport);
226 /**
227 * \brief Send a packet via UDP (IPv6)
228 * \param packet The packet to send
229 * \param saddr The source Ipv4Address
230 * \param daddr The destination Ipv4Address
231 * \param sport The source port number
232 * \param dport The destination port number
233 * \param route The route
234 */
235 void Send(Ptr<Packet> packet,
236 Ipv6Address saddr,
237 Ipv6Address daddr,
238 uint16_t sport,
239 uint16_t dport,
240 Ptr<Ipv6Route> route);
241
242 // inherited from Ipv4L4Protocol
244 const Ipv4Header& header,
245 Ptr<Ipv4Interface> interface) override;
247 const Ipv6Header& header,
248 Ptr<Ipv6Interface> interface) override;
249
250 void ReceiveIcmp(Ipv4Address icmpSource,
251 uint8_t icmpTtl,
252 uint8_t icmpType,
253 uint8_t icmpCode,
254 uint32_t icmpInfo,
255 Ipv4Address payloadSource,
256 Ipv4Address payloadDestination,
257 const uint8_t payload[8]) override;
258 void ReceiveIcmp(Ipv6Address icmpSource,
259 uint8_t icmpTtl,
260 uint8_t icmpType,
261 uint8_t icmpCode,
262 uint32_t icmpInfo,
263 Ipv6Address payloadSource,
264 Ipv6Address payloadDestination,
265 const uint8_t payload[8]) override;
266
267 // From IpL4Protocol
270 // From IpL4Protocol
273
274 protected:
275 void DoDispose() override;
276 /*
277 * This function will notify other components connected to the node that a new stack member is
278 * now connected This will be used to notify Layer 3 protocol of layer 4 protocol stack to
279 * connect them together.
280 */
281 void NotifyNewAggregate() override;
282
283 private:
284 Ptr<Node> m_node; //!< The node this stack is associated with
285 Ipv4EndPointDemux* m_endPoints; //!< A list of IPv4 end points.
286 Ipv6EndPointDemux* m_endPoints6; //!< A list of IPv6 end points.
287
288 std::unordered_map<uint64_t, Ptr<UdpSocketImpl>>
289 m_sockets; //!< Unordered map of socket IDs and corresponding sockets
290 uint64_t m_socketIndex{0}; //!< Index of the next socket to be created
291 IpL4Protocol::DownTargetCallback m_downTarget; //!< Callback to send packets over IPv4
292 IpL4Protocol::DownTargetCallback6 m_downTarget6; //!< Callback to send packets over IPv6
293};
294
295} // namespace ns3
296
297#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:48
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 const uint8_t PROT_NUMBER
protocol number (0x11)
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.