Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2021 Project CHIP Authors
4 : * Copyright (c) 2013-2017 Nest Labs, Inc.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * This header file defines the <tt>Inet::TCPEndPoint</tt>
22 : * class, where the CHIP Inet Layer encapsulates methods for
23 : * interacting with TCP transport endpoints (SOCK_DGRAM sockets
24 : * on Linux and BSD-derived systems) or LwIP TCP protocol
25 : * control blocks, as the system is configured accordingly.
26 : */
27 :
28 : #pragma once
29 :
30 : #include <inet/EndPointBasis.h>
31 : #include <inet/IPAddress.h>
32 : #include <inet/InetInterface.h>
33 : #include <inet/InetLayer.h>
34 : #include <system/SystemLayer.h>
35 : #include <system/SystemPacketBuffer.h>
36 :
37 : #include <utility>
38 :
39 : namespace chip {
40 :
41 : namespace Transport {
42 : class TCPTest;
43 : };
44 :
45 : namespace Inet {
46 :
47 : class TCPTest;
48 :
49 : /**
50 : * @brief Objects of this class represent TCP transport endpoints.
51 : *
52 : * @details
53 : * CHIP Inet Layer encapsulates methods for interacting with TCP transport
54 : * endpoints (SOCK_STREAM sockets on Linux and BSD-derived systems) or LwIP
55 : * TCP protocol control blocks, as the system is configured accordingly.
56 : */
57 : class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
58 : {
59 : public:
60 : /**
61 : * @brief Bind the endpoint to an interface IP address.
62 : *
63 : * @param[in] addrType the protocol version of the IP address
64 : * @param[in] addr the IP address (must be an interface address)
65 : * @param[in] port the TCP port
66 : * @param[in] reuseAddr option to share binding with other endpoints
67 : *
68 : * @retval CHIP_NO_ERROR success: endpoint bound to address
69 : * @retval CHIP_ERROR_INCORRECT_STATE endpoint has been bound previously
70 : * @retval CHIP_ERROR_NO_MEMORY insufficient memory for endpoint
71 : *
72 : * @retval INET_ERROR_WRONG_PROTOCOL_TYPE
73 : * \c addrType does not match \c IPVer.
74 : *
75 : * @retval INET_ERROR_WRONG_ADDRESS_TYPE
76 : * \c addrType is \c IPAddressType::kAny, or the type of \c addr is not
77 : * equal to \c addrType.
78 : *
79 : * @retval other another system or platform error
80 : *
81 : * @details
82 : * Binds the endpoint to the specified network interface IP address.
83 : *
84 : * On LwIP, this method must not be called with the LwIP stack lock
85 : * already acquired.
86 : */
87 : CHIP_ERROR Bind(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr = false);
88 :
89 : /**
90 : * @brief Prepare the endpoint to receive TCP messages.
91 : *
92 : * @param[in] backlog maximum depth of connection acceptance queue
93 : *
94 : * @retval CHIP_NO_ERROR success: endpoint ready to receive messages.
95 : * @retval CHIP_ERROR_INCORRECT_STATE endpoint is already listening.
96 : *
97 : * @details
98 : * If \c mState is already \c State::kListening, then no operation is
99 : * performed, otherwise the \c mState is set to \c State::kListening and
100 : * the endpoint is prepared to received TCP messages, according to the
101 : * semantics of the platform.
102 : *
103 : * On some platforms, the \c backlog argument is not used (the depth of
104 : * the queue is fixed; only one connection may be accepted at a time).
105 : *
106 : * On LwIP systems, this method must not be called with the LwIP stack
107 : * lock already acquired
108 : */
109 : CHIP_ERROR Listen(uint16_t backlog);
110 :
111 : /**
112 : * @brief Initiate a TCP connection.
113 : *
114 : * @param[in] addr the destination IP address
115 : * @param[in] port the destination TCP port
116 : * @param[in] intfId an optional network interface indicator
117 : *
118 : * @retval CHIP_NO_ERROR success: \c msg is queued for transmit.
119 : * @retval CHIP_ERROR_NOT_IMPLEMENTED system implementation not complete.
120 : *
121 : * @retval INET_ERROR_WRONG_ADDRESS_TYPE
122 : * the destination address and the bound interface address do not
123 : * have matching protocol versions or address type, or the destination
124 : * address is an IPv6 link-local address and \c intfId is not specified.
125 : *
126 : * @retval other another system or platform error
127 : *
128 : * @details
129 : * If possible, then this method initiates a TCP connection to the
130 : * destination \c addr (with \c intfId used as the scope
131 : * identifier for IPv6 link-local destinations) and \c port.
132 : */
133 : CHIP_ERROR Connect(const IPAddress & addr, uint16_t port, InterfaceId intfId = InterfaceId::Null());
134 :
135 : /**
136 : * @brief Extract IP address and TCP port of remote endpoint.
137 : *
138 : * @param[out] retAddr IP address of remote endpoint.
139 : * @param[out] retPort TCP port of remote endpoint.
140 : *
141 : * @retval CHIP_NO_ERROR success: address and port extracted.
142 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
143 : * @retval CHIP_ERROR_CONNECTION_ABORTED TCP connection no longer open.
144 : *
145 : * @details
146 : * Do not use \c nullptr for either argument.
147 : */
148 : virtual CHIP_ERROR GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const = 0;
149 :
150 : /**
151 : * @brief Extract IP address and TCP port of local endpoint.
152 : *
153 : * @param[out] retAddr IP address of local endpoint.
154 : * @param[out] retPort TCP port of local endpoint.
155 : *
156 : * @retval CHIP_NO_ERROR success: address and port extracted.
157 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
158 : * @retval CHIP_ERROR_CONNECTION_ABORTED TCP connection no longer open.
159 : *
160 : * @details
161 : * Do not use \c nullptr for either argument.
162 : */
163 : virtual CHIP_ERROR GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const = 0;
164 :
165 : /**
166 : * @brief Extract the interface id of the TCP endpoint.
167 : *
168 : * @param[out] retInterface The interface id.
169 : *
170 : * @retval CHIP_NO_ERROR success: address and port extracted.
171 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
172 : * @retval CHIP_ERROR_CONNECTION_ABORTED TCP connection no longer open.
173 : */
174 : virtual CHIP_ERROR GetInterfaceId(InterfaceId * retInterface) = 0;
175 :
176 : /**
177 : * @brief Send message text on TCP connection.
178 : *
179 : * @param[out] data Message text to send.
180 : * @param[out] push If \c true, then send immediately, otherwise queue.
181 : *
182 : * @retval CHIP_NO_ERROR success: address and port extracted.
183 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
184 : */
185 : CHIP_ERROR Send(chip::System::PacketBufferHandle && data, bool push = true);
186 :
187 : /**
188 : * Disable reception.
189 : *
190 : * Disable all event handlers. Data sent to an endpoint that disables
191 : * reception will be acknowledged until the receive window is exhausted.
192 : */
193 : void DisableReceive() { mReceiveEnabled = false; }
194 :
195 : /**
196 : * Enable reception.
197 : *
198 : * Enable all event handlers. Data sent to an endpoint that disables
199 : * reception will be acknowledged until the receive window is exhausted.
200 : */
201 : void EnableReceive()
202 : {
203 : mReceiveEnabled = true;
204 : DriveReceiving();
205 : }
206 :
207 : /**
208 : * Switch off Nagle buffering algorithm.
209 : */
210 : virtual CHIP_ERROR EnableNoDelay() = 0;
211 :
212 : /**
213 : * @brief
214 : * Enable TCP keepalive probes on the associated TCP connection.
215 : *
216 : * @param[in] interval
217 : * The interval (in seconds) between keepalive probes. This value also controls
218 : * the time between last data packet sent and the transmission of the first keepalive
219 : * probe.
220 : *
221 : * @param[in] timeoutCount
222 : * The maximum number of unacknowledged probes before the connection will be deemed
223 : * to have failed.
224 : *
225 : * @retval CHIP_NO_ERROR success: address and port extracted.
226 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
227 : * @retval CHIP_ERROR_CONNECTION_ABORTED TCP connection no longer open.
228 : * @retval CHIP_ERROR_NOT_IMPLEMENTED system implementation not complete.
229 : *
230 : * @retval other another system or platform error
231 : *
232 : * @note
233 : * This method can only be called when the endpoint is in one of the connected states.
234 : *
235 : * This method can be called multiple times to adjust the keepalive interval or timeout
236 : * count.
237 : *
238 : * @details
239 : * Start automatically transmitting TCP "keep-alive" probe segments every
240 : * \c interval seconds. The connection will abort automatically after
241 : * receiving a negative response, or after sending \c timeoutCount
242 : * probe segments without receiving a positive response.
243 : *
244 : * See RFC 1122, section 4.2.3.6 for specification details.
245 : */
246 : virtual CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) = 0;
247 :
248 : /**
249 : * @brief Disable the TCP "keep-alive" option.
250 : *
251 : * This method can only be called when the endpoint is in one of the connected states.
252 : * This method does nothing if keepalives have not been enabled on the endpoint.
253 : *
254 : * @retval CHIP_NO_ERROR success: address and port extracted.
255 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
256 : * @retval CHIP_ERROR_CONNECTION_ABORTED TCP connection no longer open.
257 : * @retval CHIP_ERROR_NOT_IMPLEMENTED system implementation not complete.
258 : *
259 : * @retval other another system or platform error
260 : */
261 : virtual CHIP_ERROR DisableKeepAlive() = 0;
262 :
263 : /**
264 : * @brief Acknowledge receipt of message text.
265 : *
266 : * @param[in] len number of bytes to acknowledge.
267 : *
268 : * @retval CHIP_NO_ERROR success: reception acknowledged.
269 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
270 : * @retval CHIP_ERROR_CONNECTION_ABORTED TCP connection no longer open.
271 : *
272 : * @details
273 : * Use this method to acknowledge reception of all or part of the data
274 : * received. The operational semantics are undefined if \c len is larger
275 : * than the total outstanding unacknowledged received data.
276 : */
277 : virtual CHIP_ERROR AckReceive(uint16_t len) = 0;
278 :
279 : /**
280 : * @brief Set the receive queue, for testing.
281 : *
282 : * @param[out] data Message text to push.
283 : *
284 : * @retval CHIP_NO_ERROR success: reception acknowledged.
285 : * @retval CHIP_ERROR_INCORRECT_STATE TCP connection not established.
286 : *
287 : * @details
288 : * This method may only be called by data reception event handlers to
289 : * put data on the receive queue for unit test purposes.
290 : */
291 : CHIP_ERROR SetReceivedDataForTesting(chip::System::PacketBufferHandle && data);
292 :
293 : /**
294 : * @brief Extract the length of the data awaiting first transmit.
295 : *
296 : * @return Number of untransmitted bytes in the transmit queue.
297 : */
298 : uint32_t PendingSendLength();
299 :
300 : /**
301 : * @brief Extract the length of the unacknowledged receive data.
302 : *
303 : * @return Number of bytes in the receive queue that have not yet been
304 : * acknowledged with <tt>AckReceive(uint16_t len)</tt>.
305 : */
306 : uint32_t PendingReceiveLength();
307 :
308 : /**
309 : * @brief Initiate TCP half close, in other words, finished with sending.
310 : */
311 : void Shutdown();
312 :
313 : /**
314 : * @brief Initiate TCP full close, in other words, finished with both send and
315 : * receive.
316 : */
317 : void Close();
318 :
319 : /**
320 : * @brief Abortively close the endpoint, in other words, send RST packets.
321 : */
322 : void Abort();
323 :
324 : /**
325 : * @brief Initiate (or continue) TCP full close, ignoring errors.
326 : *
327 : * @details
328 : * The object is returned to the free pool, and all remaining user
329 : * references are subsequently invalid.
330 : */
331 : void Free();
332 :
333 : /**
334 : * @brief Extract whether TCP connection is established.
335 : */
336 52 : bool IsConnected() const { return IsConnected(mState); }
337 :
338 : /**
339 : * Set timeout for Connect to succeed or return an error.
340 : */
341 : void SetConnectTimeout(const uint32_t connTimeoutMsecs) { mConnectTimeoutMsecs = connTimeoutMsecs; }
342 :
343 : #if INET_TCP_IDLE_CHECK_INTERVAL > 0
344 : /**
345 : * @brief Set timer event for idle activity.
346 : *
347 : * @param[in] timeoutMS The timeout in milliseconds
348 : *
349 : * @details
350 : * Set the idle timer interval to \c timeoutMS milliseconds. A zero
351 : * time interval implies the idle timer is disabled.
352 : */
353 : void SetIdleTimeout(uint32_t timeoutMS);
354 : #endif // INET_TCP_IDLE_CHECK_INTERVAL > 0
355 :
356 : /**
357 : * @brief Note activity, in other words, reset the idle timer.
358 : *
359 : * @details
360 : * Reset the idle timer to zero.
361 : */
362 12 : void MarkActive()
363 : {
364 : #if INET_TCP_IDLE_CHECK_INTERVAL > 0
365 12 : mRemainingIdleTime = mIdleTimeout;
366 : #endif // INET_TCP_IDLE_CHECK_INTERVAL > 0
367 12 : }
368 :
369 : /**
370 : * @brief Set the TCP TCP_USER_TIMEOUT socket option.
371 : *
372 : * @param[in] userTimeoutMillis Tcp user timeout value in milliseconds.
373 : *
374 : * @retval CHIP_NO_ERROR success: address and port extracted.
375 : * @retval CHIP_ERROR_NOT_IMPLEMENTED system implementation not complete.
376 : *
377 : * @retval other another system or platform error
378 : *
379 : * @details
380 : * When the value is greater than 0, it specifies the maximum amount of
381 : * time in milliseconds that transmitted data may remain
382 : * unacknowledged before TCP will forcibly close the
383 : * corresponding connection. If the option value is specified as 0,
384 : * TCP will to use the system default.
385 : * See RFC 5482, for further details.
386 : *
387 : * @note
388 : * This method can only be called when the endpoint is in one of the connected states.
389 : *
390 : * This method can be called multiple times to adjust the keepalive interval or timeout
391 : * count.
392 : */
393 : CHIP_ERROR SetUserTimeout(uint32_t userTimeoutMillis);
394 :
395 : /**
396 : * @brief Type of connection establishment event handling function.
397 : *
398 : * @param[in] endPoint The TCP endpoint associated with the event.
399 : * @param[in] err \c CHIP_NO_ERROR if success, else another code.
400 : *
401 : * @details
402 : * Provide a function of this type to the \c OnConnectComplete delegate
403 : * member to process connection establishment events on \c endPoint. The
404 : * \c err argument distinguishes successful connections from failures.
405 : */
406 : typedef void (*OnConnectCompleteFunct)(TCPEndPoint * endPoint, CHIP_ERROR err);
407 :
408 : /**
409 : * The endpoint's connection establishment event handling function
410 : * delegate.
411 : */
412 : OnConnectCompleteFunct OnConnectComplete;
413 :
414 : /**
415 : * @brief Type of data reception event handling function.
416 : *
417 : * @param[in] endPoint The TCP endpoint associated with the event.
418 : * @param[in] data The data received.
419 : *
420 : * @retval CHIP_NO_ERROR If the received data can be handled by higher layers.
421 : * @retval other If the received data can not be used, and higher layers will not see it.
422 : *
423 : * @details
424 : * Provide a function of this type to the \c OnDataReceived delegate
425 : * member to process data reception events on \c endPoint where \c data
426 : * is the message text received.
427 : *
428 : * If this function returns an error, the connection will be closed, since higher layers
429 : * are not able to process the data for a better response.
430 : */
431 : typedef CHIP_ERROR (*OnDataReceivedFunct)(TCPEndPoint * endPoint, chip::System::PacketBufferHandle && data);
432 :
433 : /**
434 : * The endpoint's message text reception event handling function delegate.
435 : */
436 : OnDataReceivedFunct OnDataReceived;
437 :
438 : /**
439 : * @brief Type of data transmission event handling function.
440 : *
441 : * @param[in] endPoint The TCP endpoint associated with the event.
442 : * @param[in] len Number of bytes added to the transmit window.
443 : *
444 : * @details
445 : * Provide a function of this type to the \c OnDataSent delegate
446 : * member to process data transmission events on \c endPoint where \c len
447 : * is the length of the message text added to the TCP transmit window,
448 : * which are eligible for sending by the underlying network stack.
449 : */
450 : typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, uint16_t len);
451 :
452 : /**
453 : * The endpoint's message text transmission event handling function
454 : * delegate.
455 : */
456 : OnDataSentFunct OnDataSent;
457 :
458 : /**
459 : * @brief Type of connection establishment event handling function.
460 : *
461 : * @param[in] endPoint The TCP endpoint associated with the event.
462 : * @param[in] err \c CHIP_NO_ERROR if success, else another code.
463 : *
464 : * @details
465 : * Provide a function of this type to the \c OnConnectionClosed delegate
466 : * member to process connection termination events on \c endPoint. The
467 : * \c err argument distinguishes successful terminations from failures.
468 : */
469 : typedef void (*OnConnectionClosedFunct)(TCPEndPoint * endPoint, CHIP_ERROR err);
470 :
471 : /** The endpoint's close event handling function delegate. */
472 : OnConnectionClosedFunct OnConnectionClosed;
473 :
474 : /**
475 : * @brief Type of half-close reception event handling function.
476 : *
477 : * @param[in] endPoint The TCP endpoint associated with the event.
478 : *
479 : * @details
480 : * Provide a function of this type to the \c OnPeerClose delegate member
481 : * to process connection termination events on \c endPoint.
482 : */
483 : typedef void (*OnPeerCloseFunct)(TCPEndPoint * endPoint);
484 :
485 : /** The endpoint's half-close receive event handling function delegate. */
486 : OnPeerCloseFunct OnPeerClose;
487 :
488 : /**
489 : * @brief Type of connection received event handling function.
490 : *
491 : * @param[in] listeningEndPoint The listening TCP endpoint.
492 : * @param[in] conEndPoint The newly received TCP endpoint.
493 : * @param[in] peerAddr The IP address of the remote peer.
494 : * @param[in] peerPort The TCP port of the remote peer.
495 : *
496 : * @details
497 : * Provide a function of this type to the \c OnConnectionReceived delegate
498 : * member to process connection reception events on \c listeningEndPoint.
499 : * The newly received endpoint \c conEndPoint is located at IP address
500 : * \c peerAddr and TCP port \c peerPort.
501 : */
502 : typedef void (*OnConnectionReceivedFunct)(TCPEndPoint * listeningEndPoint, TCPEndPoint * conEndPoint,
503 : const IPAddress & peerAddr, uint16_t peerPort);
504 :
505 : /** The endpoint's connection receive event handling function delegate. */
506 : OnConnectionReceivedFunct OnConnectionReceived;
507 :
508 : /**
509 : * @brief Type of connection acceptance error event handling function.
510 : *
511 : * @param[in] endPoint The TCP endpoint associated with the event.
512 : * @param[in] err The reason for the error.
513 : *
514 : * @details
515 : * Provide a function of this type to the \c OnAcceptError delegate
516 : * member to process connection acceptance error events on \c endPoint. The
517 : * \c err argument provides specific detail about the type of the error.
518 : */
519 : typedef void (*OnAcceptErrorFunct)(TCPEndPoint * endPoint, CHIP_ERROR err);
520 :
521 : /**
522 : * The endpoint's connection acceptance event handling function delegate.
523 : */
524 : OnAcceptErrorFunct OnAcceptError;
525 :
526 : /**
527 : * Size of the largest TCP packet that can be received.
528 : */
529 : constexpr static size_t kMaxReceiveMessageSize = System::PacketBuffer::kMaxSizeWithoutReserve;
530 :
531 : protected:
532 : friend class ::chip::Transport::TCPTest;
533 : friend class TCPTest;
534 :
535 : TCPEndPoint(EndPointManager<TCPEndPoint> & endPointManager) :
536 : EndPointBasis(endPointManager), OnConnectComplete(nullptr), OnDataReceived(nullptr), OnDataSent(nullptr),
537 : OnConnectionClosed(nullptr), OnPeerClose(nullptr), OnConnectionReceived(nullptr), OnAcceptError(nullptr),
538 : mState(State::kReady), mReceiveEnabled(true), mConnectTimeoutMsecs(0) // Initialize to zero for using system defaults.
539 : #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
540 : ,
541 : mUserTimeoutMillis(INET_CONFIG_DEFAULT_TCP_USER_TIMEOUT_MSEC), mUserTimeoutTimerRunning(false)
542 : #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
543 : {}
544 :
545 12 : virtual ~TCPEndPoint() = default;
546 :
547 : /**
548 : * Basic dynamic state of the underlying endpoint.
549 : *
550 : * Objects are initialized in the "ready" state, proceed to subsequent
551 : * states corresponding to a simplification of the states of the TCP
552 : * transport state machine.
553 : */
554 : enum class State : uint8_t
555 : {
556 : kReady = 0, /**< Endpoint initialized, but not bound. */
557 : kBound = 1, /**< Endpoint bound, but not listening. */
558 : kListening = 2, /**< Endpoint receiving connections. */
559 : kConnecting = 3, /**< Endpoint attempting to connect. */
560 : kConnected = 4, /**< Endpoint connected, ready for tx/rx. */
561 : kSendShutdown = 5, /**< Endpoint initiated its half-close. */
562 : kReceiveShutdown = 6, /**< Endpoint responded to half-close. */
563 : kClosing = 7, /**< Endpoint closing bidirectionally. */
564 : kClosed = 8 /**< Endpoint closed, ready for release. */
565 : } mState;
566 :
567 : /** Control switch indicating whether the application is receiving data. */
568 : bool mReceiveEnabled;
569 :
570 : chip::System::PacketBufferHandle mRcvQueue;
571 : chip::System::PacketBufferHandle mSendQueue;
572 : #if INET_TCP_IDLE_CHECK_INTERVAL > 0
573 : static void HandleIdleTimer(System::Layer * aSystemLayer, void * aAppState);
574 : static bool IsIdleTimerRunning(EndPointManager<TCPEndPoint> & endPointManager);
575 : uint16_t mIdleTimeout; // in units of INET_TCP_IDLE_CHECK_INTERVAL; zero means no timeout
576 : uint16_t mRemainingIdleTime; // in units of INET_TCP_IDLE_CHECK_INTERVAL
577 : #endif // INET_TCP_IDLE_CHECK_INTERVAL > 0
578 :
579 : uint32_t mConnectTimeoutMsecs; // This is the timeout to wait for a Connect call to succeed or
580 : // return an error; zero means use system defaults.
581 :
582 : #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
583 : uint32_t mUserTimeoutMillis; // The configured TCP user timeout value in milliseconds.
584 : // If 0, assume not set.
585 : bool mUserTimeoutTimerRunning; // Indicates whether the TCP UserTimeout timer has been started.
586 :
587 : static void TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState);
588 : virtual void TCPUserTimeoutHandler() = 0;
589 :
590 : void StartTCPUserTimeoutTimer();
591 : void StopTCPUserTimeoutTimer();
592 : void RestartTCPUserTimeoutTimer();
593 : void ScheduleNextTCPUserTimeoutPoll(uint32_t aTimeOut);
594 : #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
595 :
596 : TCPEndPoint(const TCPEndPoint &) = delete;
597 :
598 : CHIP_ERROR DriveSending();
599 : void DriveReceiving();
600 : void HandleConnectComplete(CHIP_ERROR err);
601 : void HandleAcceptError(CHIP_ERROR err);
602 : void DoClose(CHIP_ERROR err, bool suppressCallback);
603 : static bool IsConnected(State state);
604 :
605 : static void TCPConnectTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState);
606 :
607 : void StartConnectTimerIfSet();
608 : void StopConnectTimer();
609 :
610 : friend class TCPEndPointDeletor;
611 :
612 : /*
613 : * Implementation helpers for shared methods.
614 : */
615 : virtual CHIP_ERROR BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) = 0;
616 : virtual CHIP_ERROR ListenImpl(uint16_t backlog) = 0;
617 : virtual CHIP_ERROR ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) = 0;
618 : virtual CHIP_ERROR SendQueuedImpl(bool queueWasEmpty) = 0;
619 : virtual CHIP_ERROR SetUserTimeoutImpl(uint32_t userTimeoutMillis) = 0;
620 : virtual CHIP_ERROR DriveSendingImpl() = 0;
621 : virtual void HandleConnectCompleteImpl() = 0;
622 : virtual void DoCloseImpl(CHIP_ERROR err, State oldState) = 0;
623 : };
624 :
625 : template <>
626 : struct EndPointProperties<TCPEndPoint>
627 : {
628 : static constexpr char kName[] = "TCP";
629 : static constexpr size_t kNumEndPoints = INET_CONFIG_NUM_TCP_ENDPOINTS;
630 : static constexpr int kSystemStatsKey = System::Stats::kInetLayer_NumTCPEps;
631 : };
632 :
633 : } // namespace Inet
634 : } // namespace chip
|