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