A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-l4-protocol.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Georgia Tech Research Corporation
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Raj Bhattacharjea <raj.b@gatech.edu>
7 */
8
9#ifndef TCP_L4_PROTOCOL_H
10#define TCP_L4_PROTOCOL_H
11
12#include "ip-l4-protocol.h"
13
14#include "ns3/ipv4-address.h"
15#include "ns3/ipv6-address.h"
16#include "ns3/sequence-number.h"
17
18#include <stdint.h>
19#include <unordered_map>
20
21namespace ns3
22{
23
24class Node;
25class Socket;
26class TcpHeader;
27class Ipv4EndPointDemux;
28class Ipv6EndPointDemux;
29class Ipv4Interface;
30class TcpSocketBase;
31class Ipv4EndPoint;
32class Ipv6EndPoint;
33class NetDevice;
34
35/**
36 * @ingroup internet
37 * @defgroup tcp TCP
38 *
39 * This is an implementation of various Transmission Control Protocol flavors.
40 *
41 * Each TCP flavors is studied to match a specific environment, and they
42 * differ mainly in the congestion control algorithms used.
43 *
44 * See \RFC{793} and others.
45 */
46
47/**
48 * @ingroup tcp
49 * @brief TCP socket creation and multiplexing/demultiplexing
50 *
51 * A single instance of this class is held by one instance of class Node.
52 *
53 * The creation of TcpSocket are handled in the method CreateSocket, which is
54 * called by TcpSocketFactory. Upon creation, this class is responsible to
55 * the socket initialization and handle multiplexing/demultiplexing of data
56 * between node's TCP sockets. Demultiplexing is done by receiving
57 * packets from IP, and forwards them up to the right socket. Multiplexing
58 * is done through the SendPacket function, which sends the packet down the stack.
59 *
60 * Moreover, this class allocates "endpoint" objects (ns3::Ipv4EndPoint) for TCP,
61 * and SHOULD checksum packets its receives from the socket layer going down
62 * the stack, but currently checksumming is disabled.
63 *
64 * @see CreateSocket
65 * @see NotifyNewAggregate
66 * @see SendPacket
67 */
68
70{
71 public:
72 /**
73 * @brief Get the type ID.
74 * @return the object TypeId
75 */
76 static TypeId GetTypeId();
77
78 /// Protocol number (see http://www.iana.org/assignments/protocol-numbers)
79 static constexpr uint8_t PROT_NUMBER = 6;
80
82 ~TcpL4Protocol() override;
83
84 // Delete copy constructor and assignment operator to avoid misuse
85 TcpL4Protocol(const TcpL4Protocol&) = delete;
87
88 /**
89 * Set node associated with this stack
90 * @param node the node
91 */
92 void SetNode(Ptr<Node> node);
93
94 // NOTE: API from here should not be removed, only added. Be backward-compatible!
95
96 /**
97 * @brief Create a TCP socket using the TypeId set by SocketType attribute
98 *
99 * @return A smart Socket pointer to a TcpSocket allocated by this instance
100 * of the TCP protocol
101 */
103
104 /**
105 * @brief Create a TCP socket using the specified congestion control algorithm TypeId
106 *
107 * @return A smart Socket pointer to a TcpSocket allocated by this instance
108 * of the TCP protocol
109 *
110 * @warning using a congestionTypeId other than TCP is a bad idea.
111 *
112 * @param congestionTypeId the congestion control algorithm TypeId
113 * @param recoveryTypeId the recovery algorithm TypeId
114 */
115 Ptr<Socket> CreateSocket(TypeId congestionTypeId, TypeId recoveryTypeId);
116
117 /**
118 * @brief Create a TCP socket using the specified congestion control algorithm
119 * @return A smart Socket pointer to a TcpSocket allocated by this instance
120 * of the TCP protocol
121 *
122 * @param congestionTypeId the congestion control algorithm TypeId
123 *
124 */
125 Ptr<Socket> CreateSocket(TypeId congestionTypeId);
126
127 /**
128 * @brief Allocate an IPv4 Endpoint
129 * @return the Endpoint
130 */
132 /**
133 * @brief Allocate an IPv4 Endpoint
134 * @param address address to use
135 * @return the Endpoint
136 */
138 /**
139 * @brief Allocate an IPv4 Endpoint
140 * @param boundNetDevice Bound NetDevice (if any)
141 * @param port port to use
142 * @return the Endpoint
143 */
144 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, uint16_t port);
145 /**
146 * @brief Allocate an IPv4 Endpoint
147 * @param boundNetDevice Bound NetDevice (if any)
148 * @param address address to use
149 * @param port port to use
150 * @return the Endpoint
151 */
152 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, Ipv4Address address, uint16_t port);
153 /**
154 * @brief Allocate an IPv4 Endpoint
155 * @param boundNetDevice Bound NetDevice (if any)
156 * @param localAddress local address to use
157 * @param localPort local port to use
158 * @param peerAddress remote address to use
159 * @param peerPort remote port to use
160 * @return the Endpoint
161 */
162 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice,
163 Ipv4Address localAddress,
164 uint16_t localPort,
165 Ipv4Address peerAddress,
166 uint16_t peerPort);
167 /**
168 * @brief Allocate an IPv6 Endpoint
169 * @return the Endpoint
170 */
172 /**
173 * @brief Allocate an IPv6 Endpoint
174 * @param address address to use
175 * @return the Endpoint
176 */
178 /**
179 * @brief Allocate an IPv6 Endpoint
180 * @param boundNetDevice Bound NetDevice (if any)
181 * @param port port to use
182 * @return the Endpoint
183 */
184 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, uint16_t port);
185 /**
186 * @brief Allocate an IPv6 Endpoint
187 * @param boundNetDevice Bound NetDevice (if any)
188 * @param address address to use
189 * @param port port to use
190 * @return the Endpoint
191 */
192 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, Ipv6Address address, uint16_t port);
193 /**
194 * @brief Allocate an IPv6 Endpoint
195 * @param boundNetDevice Bound NetDevice (if any)
196 * @param localAddress local address to use
197 * @param localPort local port to use
198 * @param peerAddress remote address to use
199 * @param peerPort remote port to use
200 * @return the Endpoint
201 */
202 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice,
203 Ipv6Address localAddress,
204 uint16_t localPort,
205 Ipv6Address peerAddress,
206 uint16_t peerPort);
207
208 /**
209 * @brief Send a packet via TCP (IP-agnostic)
210 *
211 * @param pkt The packet to send
212 * @param outgoing The packet header
213 * @param saddr The source Ipv4Address
214 * @param daddr The destination Ipv4Address
215 * @param oif The output interface bound. Defaults to null (unspecified).
216 */
217 void SendPacket(Ptr<Packet> pkt,
218 const TcpHeader& outgoing,
219 const Address& saddr,
220 const Address& daddr,
221 Ptr<NetDevice> oif = nullptr) const;
222
223 /**
224 * @brief Make a socket fully operational
225 *
226 * Called after a socket has been bound, it is inserted in an internal vector.
227 *
228 * @param socket Socket to be added
229 */
230 void AddSocket(Ptr<TcpSocketBase> socket);
231
232 /**
233 * @brief Remove a socket from the internal list
234 *
235 * @param socket socket to Remove
236 * @return true if the socket has been removed
237 */
238 bool RemoveSocket(Ptr<TcpSocketBase> socket);
239
240 /**
241 * @brief Remove an IPv4 Endpoint.
242 * @param endPoint the end point to remove
243 */
244 void DeAllocate(Ipv4EndPoint* endPoint);
245 /**
246 * @brief Remove an IPv6 Endpoint.
247 * @param endPoint the end point to remove
248 */
249 void DeAllocate(Ipv6EndPoint* endPoint);
250
251 // From IpL4Protocol
253 const Ipv4Header& incomingIpHeader,
254 Ptr<Ipv4Interface> incomingInterface) override;
256 const Ipv6Header& incomingIpHeader,
257 Ptr<Ipv6Interface> incomingInterface) override;
258
259 void ReceiveIcmp(Ipv4Address icmpSource,
260 uint8_t icmpTtl,
261 uint8_t icmpType,
262 uint8_t icmpCode,
263 uint32_t icmpInfo,
264 Ipv4Address payloadSource,
265 Ipv4Address payloadDestination,
266 const uint8_t payload[8]) override;
267 void ReceiveIcmp(Ipv6Address icmpSource,
268 uint8_t icmpTtl,
269 uint8_t icmpType,
270 uint8_t icmpCode,
271 uint32_t icmpInfo,
272 Ipv6Address payloadSource,
273 Ipv6Address payloadDestination,
274 const uint8_t payload[8]) override;
275
278 int GetProtocolNumber() const override;
281
282 protected:
283 void DoDispose() override;
284
285 /**
286 * @brief Setup socket factory and callbacks when aggregated to a node
287 *
288 * This function will notify other components connected to the node that a
289 * new stack member is now connected. This will be used to notify Layer 3
290 * protocol of layer 4 protocol stack to connect them together.
291 * The aggregation is completed by setting the node in the TCP stack, link
292 * it to the ipv4 or ipv6 stack and adding TCP socket factory to the node.
293 */
294 void NotifyNewAggregate() override;
295
296 /**
297 * @brief Get the tcp header of the incoming packet and checks its checksum if needed
298 *
299 * @param packet Received packet
300 * @param incomingTcpHeader Overwritten with the tcp header of the packet
301 * @param source Source address (an underlying Ipv4Address or Ipv6Address)
302 * @param destination Destination address (an underlying Ipv4Address or Ipv6Address)
303 *
304 * @return RX_CSUM_FAILED if the checksum check fails, RX_OK otherwise
305 */
307 TcpHeader& incomingTcpHeader,
308 const Address& source,
309 const Address& destination);
310
311 /**
312 * @brief Check if RST packet should be sent, and in case, send it
313 *
314 * The function is called when no endpoint is found for the received
315 * packet. So TcpL4Protocol do not know to who the packet should be
316 * given to. An RST packet is sent out as reply unless the received packet
317 * has the RST flag set.
318 *
319 * @param incomingHeader TCP header of the incoming packet
320 * @param incomingSAddr Source address of the incoming packet
321 * @param incomingDAddr Destination address of the incoming packet
322 *
323 */
324 void NoEndPointsFound(const TcpHeader& incomingHeader,
325 const Address& incomingSAddr,
326 const Address& incomingDAddr);
327
328 private:
329 Ptr<Node> m_node; //!< the node this stack is associated with
330 Ipv4EndPointDemux* m_endPoints; //!< A list of IPv4 end points.
331 Ipv6EndPointDemux* m_endPoints6; //!< A list of IPv6 end points.
332 TypeId m_rttTypeId; //!< The RTT Estimator TypeId
333 TypeId m_congestionTypeId; //!< The socket TypeId
334 TypeId m_recoveryTypeId; //!< The recovery TypeId
335 std::unordered_map<uint64_t, Ptr<TcpSocketBase>>
336 m_sockets; //!< Unordered map of socket IDs and corresponding sockets
337 uint64_t m_socketIndex{0}; //!< index of the next socket to be created
338 IpL4Protocol::DownTargetCallback m_downTarget; //!< Callback to send packets over IPv4
339 IpL4Protocol::DownTargetCallback6 m_downTarget6; //!< Callback to send packets over IPv6
340
341 /**
342 * @brief Send a packet via TCP (IPv4)
343 *
344 * @param pkt The packet to send
345 * @param outgoing The packet header
346 * @param saddr The source Ipv4Address
347 * @param daddr The destination Ipv4Address
348 * @param oif The output interface bound. Defaults to null (unspecified).
349 */
350 void SendPacketV4(Ptr<Packet> pkt,
351 const TcpHeader& outgoing,
352 const Ipv4Address& saddr,
353 const Ipv4Address& daddr,
354 Ptr<NetDevice> oif = nullptr) const;
355
356 /**
357 * @brief Send a packet via TCP (IPv6)
358 *
359 * @param pkt The packet to send
360 * @param outgoing The packet header
361 * @param saddr The source Ipv4Address
362 * @param daddr The destination Ipv4Address
363 * @param oif The output interface bound. Defaults to null (unspecified).
364 */
365 void SendPacketV6(Ptr<Packet> pkt,
366 const TcpHeader& outgoing,
367 const Ipv6Address& saddr,
368 const Ipv6Address& daddr,
369 Ptr<NetDevice> oif = nullptr) const;
370};
371
372} // namespace ns3
373
374#endif /* TCP_L4_PROTOCOL_H */
a polymophic address class
Definition address.h:90
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.
Header for the Transmission Control Protocol.
Definition tcp-header.h:36
TCP socket creation and multiplexing/demultiplexing.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
TcpL4Protocol(const TcpL4Protocol &)=delete
void SendPacketV4(Ptr< Packet > pkt, const TcpHeader &outgoing, const Ipv4Address &saddr, const Ipv4Address &daddr, Ptr< NetDevice > oif=nullptr) const
Send a packet via TCP (IPv4)
void NoEndPointsFound(const TcpHeader &incomingHeader, const Address &incomingSAddr, const Address &incomingDAddr)
Check if RST packet should be sent, and in case, send it.
bool RemoveSocket(Ptr< TcpSocketBase > socket)
Remove a socket from the internal list.
TypeId m_congestionTypeId
The socket TypeId.
IpL4Protocol::RxStatus PacketReceived(Ptr< Packet > packet, TcpHeader &incomingTcpHeader, const Address &source, const Address &destination)
Get the tcp header of the incoming packet and checks its checksum if needed.
TypeId m_recoveryTypeId
The recovery TypeId.
IpL4Protocol::DownTargetCallback m_downTarget
Callback to send packets over IPv4.
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...
void DoDispose() override
Destructor implementation.
Ipv4EndPointDemux * m_endPoints
A list of IPv4 end points.
Ipv6EndPointDemux * m_endPoints6
A list of IPv6 end points.
Ptr< Node > m_node
the node this stack is associated with
static TypeId GetTypeId()
Get the type ID.
void NotifyNewAggregate() override
Setup socket factory and callbacks when aggregated to a node.
Ipv6EndPoint * Allocate6()
Allocate an IPv6 Endpoint.
void DeAllocate(Ipv4EndPoint *endPoint)
Remove an IPv4 Endpoint.
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.
IpL4Protocol::DownTargetCallback GetDownTarget() const override
This method allows a caller to get the current down target callback set for this L4 protocol (IPv4 ca...
std::unordered_map< uint64_t, Ptr< TcpSocketBase > > m_sockets
Unordered map of socket IDs and corresponding sockets.
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...
Ptr< Socket > CreateSocket()
Create a TCP socket using the TypeId set by SocketType attribute.
void SendPacketV6(Ptr< Packet > pkt, const TcpHeader &outgoing, const Ipv6Address &saddr, const Ipv6Address &daddr, Ptr< NetDevice > oif=nullptr) const
Send a packet via TCP (IPv6)
TcpL4Protocol & operator=(const TcpL4Protocol &)=delete
uint64_t m_socketIndex
index of the next socket to be created
IpL4Protocol::DownTargetCallback6 GetDownTarget6() const override
This method allows a caller to get the current down target callback set for this L4 protocol (IPv6 ca...
void SendPacket(Ptr< Packet > pkt, const TcpHeader &outgoing, const Address &saddr, const Address &daddr, Ptr< NetDevice > oif=nullptr) const
Send a packet via TCP (IP-agnostic)
void AddSocket(Ptr< TcpSocketBase > socket)
Make a socket fully operational.
IpL4Protocol::DownTargetCallback6 m_downTarget6
Callback to send packets over IPv6.
IpL4Protocol::RxStatus Receive(Ptr< Packet > p, const Ipv4Header &incomingIpHeader, Ptr< Ipv4Interface > incomingInterface) override
Called from lower-level layers to send the packet up in the stack.
Ipv4EndPoint * Allocate()
Allocate an IPv4 Endpoint.
static constexpr uint8_t PROT_NUMBER
Protocol number (see http://www.iana.org/assignments/protocol-numbers)
int GetProtocolNumber() const override
Returns the protocol number of this protocol.
TypeId m_rttTypeId
The RTT Estimator TypeId.
a unique identifier for an interface.
Definition type-id.h:49
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.