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 static const uint8_t PROT_NUMBER; //!< protocol number (0x6)
78
80 ~TcpL4Protocol() override;
81
82 // Delete copy constructor and assignment operator to avoid misuse
83 TcpL4Protocol(const TcpL4Protocol&) = delete;
85
86 /**
87 * Set node associated with this stack
88 * @param node the node
89 */
90 void SetNode(Ptr<Node> node);
91
92 // NOTE: API from here should not be removed, only added. Be backward-compatible!
93
94 /**
95 * @brief Create a TCP socket using the TypeId set by SocketType attribute
96 *
97 * @return A smart Socket pointer to a TcpSocket allocated by this instance
98 * of the TCP protocol
99 */
101
102 /**
103 * @brief Create a TCP socket using the specified congestion control algorithm TypeId
104 *
105 * @return A smart Socket pointer to a TcpSocket allocated by this instance
106 * of the TCP protocol
107 *
108 * @warning using a congestionTypeId other than TCP is a bad idea.
109 *
110 * @param congestionTypeId the congestion control algorithm TypeId
111 * @param recoveryTypeId the recovery algorithm TypeId
112 */
113 Ptr<Socket> CreateSocket(TypeId congestionTypeId, TypeId recoveryTypeId);
114
115 /**
116 * @brief Create a TCP socket using the specified congestion control algorithm
117 * @return A smart Socket pointer to a TcpSocket allocated by this instance
118 * of the TCP protocol
119 *
120 * @param congestionTypeId the congestion control algorithm TypeId
121 *
122 */
123 Ptr<Socket> CreateSocket(TypeId congestionTypeId);
124
125 /**
126 * @brief Allocate an IPv4 Endpoint
127 * @return the Endpoint
128 */
130 /**
131 * @brief Allocate an IPv4 Endpoint
132 * @param address address to use
133 * @return the Endpoint
134 */
136 /**
137 * @brief Allocate an IPv4 Endpoint
138 * @param boundNetDevice Bound NetDevice (if any)
139 * @param port port to use
140 * @return the Endpoint
141 */
142 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, uint16_t port);
143 /**
144 * @brief Allocate an IPv4 Endpoint
145 * @param boundNetDevice Bound NetDevice (if any)
146 * @param address address to use
147 * @param port port to use
148 * @return the Endpoint
149 */
150 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice, Ipv4Address address, uint16_t port);
151 /**
152 * @brief Allocate an IPv4 Endpoint
153 * @param boundNetDevice Bound NetDevice (if any)
154 * @param localAddress local address to use
155 * @param localPort local port to use
156 * @param peerAddress remote address to use
157 * @param peerPort remote port to use
158 * @return the Endpoint
159 */
160 Ipv4EndPoint* Allocate(Ptr<NetDevice> boundNetDevice,
161 Ipv4Address localAddress,
162 uint16_t localPort,
163 Ipv4Address peerAddress,
164 uint16_t peerPort);
165 /**
166 * @brief Allocate an IPv6 Endpoint
167 * @return the Endpoint
168 */
170 /**
171 * @brief Allocate an IPv6 Endpoint
172 * @param address address to use
173 * @return the Endpoint
174 */
176 /**
177 * @brief Allocate an IPv6 Endpoint
178 * @param boundNetDevice Bound NetDevice (if any)
179 * @param port port to use
180 * @return the Endpoint
181 */
182 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, uint16_t port);
183 /**
184 * @brief Allocate an IPv6 Endpoint
185 * @param boundNetDevice Bound NetDevice (if any)
186 * @param address address to use
187 * @param port port to use
188 * @return the Endpoint
189 */
190 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice, Ipv6Address address, uint16_t port);
191 /**
192 * @brief Allocate an IPv6 Endpoint
193 * @param boundNetDevice Bound NetDevice (if any)
194 * @param localAddress local address to use
195 * @param localPort local port to use
196 * @param peerAddress remote address to use
197 * @param peerPort remote port to use
198 * @return the Endpoint
199 */
200 Ipv6EndPoint* Allocate6(Ptr<NetDevice> boundNetDevice,
201 Ipv6Address localAddress,
202 uint16_t localPort,
203 Ipv6Address peerAddress,
204 uint16_t peerPort);
205
206 /**
207 * @brief Send a packet via TCP (IP-agnostic)
208 *
209 * @param pkt The packet to send
210 * @param outgoing The packet header
211 * @param saddr The source Ipv4Address
212 * @param daddr The destination Ipv4Address
213 * @param oif The output interface bound. Defaults to null (unspecified).
214 */
215 void SendPacket(Ptr<Packet> pkt,
216 const TcpHeader& outgoing,
217 const Address& saddr,
218 const Address& daddr,
219 Ptr<NetDevice> oif = nullptr) const;
220
221 /**
222 * @brief Make a socket fully operational
223 *
224 * Called after a socket has been bound, it is inserted in an internal vector.
225 *
226 * @param socket Socket to be added
227 */
228 void AddSocket(Ptr<TcpSocketBase> socket);
229
230 /**
231 * @brief Remove a socket from the internal list
232 *
233 * @param socket socket to Remove
234 * @return true if the socket has been removed
235 */
236 bool RemoveSocket(Ptr<TcpSocketBase> socket);
237
238 /**
239 * @brief Remove an IPv4 Endpoint.
240 * @param endPoint the end point to remove
241 */
242 void DeAllocate(Ipv4EndPoint* endPoint);
243 /**
244 * @brief Remove an IPv6 Endpoint.
245 * @param endPoint the end point to remove
246 */
247 void DeAllocate(Ipv6EndPoint* endPoint);
248
249 // From IpL4Protocol
251 const Ipv4Header& incomingIpHeader,
252 Ptr<Ipv4Interface> incomingInterface) override;
254 const Ipv6Header& incomingIpHeader,
255 Ptr<Ipv6Interface> incomingInterface) override;
256
257 void ReceiveIcmp(Ipv4Address icmpSource,
258 uint8_t icmpTtl,
259 uint8_t icmpType,
260 uint8_t icmpCode,
261 uint32_t icmpInfo,
262 Ipv4Address payloadSource,
263 Ipv4Address payloadDestination,
264 const uint8_t payload[8]) override;
265 void ReceiveIcmp(Ipv6Address icmpSource,
266 uint8_t icmpTtl,
267 uint8_t icmpType,
268 uint8_t icmpCode,
269 uint32_t icmpInfo,
270 Ipv6Address payloadSource,
271 Ipv6Address payloadDestination,
272 const uint8_t payload[8]) override;
273
276 int GetProtocolNumber() const override;
279
280 protected:
281 void DoDispose() override;
282
283 /**
284 * @brief Setup socket factory and callbacks when aggregated to a node
285 *
286 * This function will notify other components connected to the node that a
287 * new stack member is now connected. This will be used to notify Layer 3
288 * protocol of layer 4 protocol stack to connect them together.
289 * The aggregation is completed by setting the node in the TCP stack, link
290 * it to the ipv4 or ipv6 stack and adding TCP socket factory to the node.
291 */
292 void NotifyNewAggregate() override;
293
294 /**
295 * @brief Get the tcp header of the incoming packet and checks its checksum if needed
296 *
297 * @param packet Received packet
298 * @param incomingTcpHeader Overwritten with the tcp header of the packet
299 * @param source Source address (an underlying Ipv4Address or Ipv6Address)
300 * @param destination Destination address (an underlying Ipv4Address or Ipv6Address)
301 *
302 * @return RX_CSUM_FAILED if the checksum check fails, RX_OK otherwise
303 */
305 TcpHeader& incomingTcpHeader,
306 const Address& source,
307 const Address& destination);
308
309 /**
310 * @brief Check if RST packet should be sent, and in case, send it
311 *
312 * The function is called when no endpoint is found for the received
313 * packet. So TcpL4Protocol do not know to who the packet should be
314 * given to. An RST packet is sent out as reply unless the received packet
315 * has the RST flag set.
316 *
317 * @param incomingHeader TCP header of the incoming packet
318 * @param incomingSAddr Source address of the incoming packet
319 * @param incomingDAddr Destination address of the incoming packet
320 *
321 */
322 void NoEndPointsFound(const TcpHeader& incomingHeader,
323 const Address& incomingSAddr,
324 const Address& incomingDAddr);
325
326 private:
327 Ptr<Node> m_node; //!< the node this stack is associated with
328 Ipv4EndPointDemux* m_endPoints; //!< A list of IPv4 end points.
329 Ipv6EndPointDemux* m_endPoints6; //!< A list of IPv6 end points.
330 TypeId m_rttTypeId; //!< The RTT Estimator TypeId
331 TypeId m_congestionTypeId; //!< The socket TypeId
332 TypeId m_recoveryTypeId; //!< The recovery TypeId
333 std::unordered_map<uint64_t, Ptr<TcpSocketBase>>
334 m_sockets; //!< Unordered map of socket IDs and corresponding sockets
335 uint64_t m_socketIndex{0}; //!< index of the next socket to be created
336 IpL4Protocol::DownTargetCallback m_downTarget; //!< Callback to send packets over IPv4
337 IpL4Protocol::DownTargetCallback6 m_downTarget6; //!< Callback to send packets over IPv6
338
339 /**
340 * @brief Send a packet via TCP (IPv4)
341 *
342 * @param pkt The packet to send
343 * @param outgoing The packet header
344 * @param saddr The source Ipv4Address
345 * @param daddr The destination Ipv4Address
346 * @param oif The output interface bound. Defaults to null (unspecified).
347 */
348 void SendPacketV4(Ptr<Packet> pkt,
349 const TcpHeader& outgoing,
350 const Ipv4Address& saddr,
351 const Ipv4Address& daddr,
352 Ptr<NetDevice> oif = nullptr) const;
353
354 /**
355 * @brief Send a packet via TCP (IPv6)
356 *
357 * @param pkt The packet to send
358 * @param outgoing The packet header
359 * @param saddr The source Ipv4Address
360 * @param daddr The destination Ipv4Address
361 * @param oif The output interface bound. Defaults to null (unspecified).
362 */
363 void SendPacketV6(Ptr<Packet> pkt,
364 const TcpHeader& outgoing,
365 const Ipv6Address& saddr,
366 const Ipv6Address& daddr,
367 Ptr<NetDevice> oif = nullptr) const;
368};
369
370} // namespace ns3
371
372#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...
static const uint8_t PROT_NUMBER
protocol number (0x6)
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.
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:48
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.