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