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