-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
TCPEndPoint.cpp
2744 lines (2225 loc) · 81.7 KB
/
TCPEndPoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2013-2018 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file implements the <tt>Inet::TCPEndPoint</tt> class,
* where the CHIP Inet Layer encapsulates methods for interacting
* with TCP transport endpoints (SOCK_DGRAM sockets on Linux and
* BSD-derived systems) or LwIP TCP protocol control blocks, as
* the system is configured accordingly.
*
*/
#define __APPLE_USE_RFC_3542
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#include "TCPEndPoint.h"
#include "InetFaultInjection.h"
#include <inet/InetLayer.h>
#include <support/CodeUtils.h>
#include <support/SafeInt.h>
#include <support/logging/CHIPLogging.h>
#include <system/SystemFaultInjection.h>
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#include <lwip/tcp.h>
#include <lwip/tcpip.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include "arpa-inet-compatibility.h"
#include <stdio.h>
#include <string.h>
#include <utility>
// SOCK_CLOEXEC not defined on all platforms, e.g. iOS/macOS:
#ifdef SOCK_CLOEXEC
#define SOCK_FLAGS SOCK_CLOEXEC
#else
#define SOCK_FLAGS 0
#endif
#if defined(SOL_TCP)
// socket option level for Linux and BSD systems.
#define TCP_SOCKOPT_LEVEL SOL_TCP
#else
// socket option level for macOS & iOS systems.
#define TCP_SOCKOPT_LEVEL IPPROTO_TCP
#endif
#if defined(TCP_KEEPIDLE)
// socket option for Linux and BSD systems.
#define TCP_IDLE_INTERVAL_OPT_NAME TCP_KEEPIDLE
#else
// socket option for macOS & iOS systems.
#define TCP_IDLE_INTERVAL_OPT_NAME TCP_KEEPALIVE
#endif
/*
* This logic to register a null operation callback with the LwIP TCP/IP task
* ensures that the TCP timer loop is started when a connection is established,
* which is necessary to ensure that initial SYN and SYN-ACK packets are
* retransmitted during the 3-way handshake.
*/
#if CHIP_SYSTEM_CONFIG_USE_LWIP
namespace {
void nil_tcpip_callback(void * _aContext) {}
err_t start_tcp_timers(void)
{
return tcpip_callback(nil_tcpip_callback, NULL);
}
} // anonymous namespace
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
namespace chip {
namespace Inet {
chip::System::ObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> TCPEndPoint::sPool;
INET_ERROR TCPEndPoint::Bind(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr)
{
INET_ERROR res = INET_NO_ERROR;
if (State != kState_Ready)
return INET_ERROR_INCORRECT_STATE;
if (addr != IPAddress::Any && addr.Type() != kIPAddressType_Any && addr.Type() != addrType)
return INET_ERROR_WRONG_ADDRESS_TYPE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// Lock LwIP stack
LOCK_TCPIP_CORE();
// Get the appropriate type of PCB.
res = GetPCB(addrType);
// Bind the PCB to the specified address/port.
if (res == INET_NO_ERROR)
{
if (reuseAddr)
{
ip_set_option(mTCP, SOF_REUSEADDR);
}
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip_addr_t ipAddr;
if (addr != IPAddress::Any)
{
ipAddr = addr.ToLwIPAddr();
}
else if (addrType == kIPAddressType_IPv6)
{
ipAddr = ip6_addr_any;
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == kIPAddressType_IPv4)
{
ipAddr = ip_addr_any;
}
#endif // INET_CONFIG_ENABLE_IPV4
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;
res = chip::System::MapErrorLwIP(tcp_bind(mTCP, &ipAddr, port));
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
if (addrType == kIPAddressType_IPv6)
{
ip6_addr_t ipv6Addr = addr.ToIPv6();
res = chip::System::MapErrorLwIP(tcp_bind_ip6(mTCP, &ipv6Addr, port));
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == kIPAddressType_IPv4)
{
ip_addr_t ipv4Addr = addr.ToIPv4();
res = chip::System::MapErrorLwIP(tcp_bind(mTCP, &ipv4Addr, port));
}
#endif // INET_CONFIG_ENABLE_IPV4
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
}
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
res = GetSocket(addrType);
if (res == INET_NO_ERROR && reuseAddr)
{
int n = 1;
setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
#ifdef SO_REUSEPORT
// Enable SO_REUSEPORT. This permits coexistence between an
// untargetted CHIP client and other services that listen on
// a CHIP port on a specific address (such as a CHIP client
// with TARGETTED_LISTEN or TCP proxying services). Note that
// one of the costs of this implementation is the
// non-deterministic connection dispatch when multple clients
// listen on the address wih the same degreee of selectivity,
// e.g. two untargetted-listen CHIP clients, or two
// targetted-listen CHIP clients with the same node id.
if (setsockopt(mSocket, SOL_SOCKET, SO_REUSEPORT, &n, sizeof(n)) != 0)
{
ChipLogError(Inet, "SO_REUSEPORT: %d", errno);
}
#endif // defined(SO_REUSEPORT)
}
if (res == INET_NO_ERROR)
{
if (addrType == kIPAddressType_IPv6)
{
struct sockaddr_in6 sa;
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(port);
sa.sin6_flowinfo = 0;
sa.sin6_addr = addr.ToIPv6();
sa.sin6_scope_id = 0;
if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
res = chip::System::MapErrorPOSIX(errno);
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == kIPAddressType_IPv4)
{
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr = addr.ToIPv4();
if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
res = chip::System::MapErrorPOSIX(errno);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (res == INET_NO_ERROR)
{
State = kState_Bound;
}
return res;
}
INET_ERROR TCPEndPoint::Listen(uint16_t backlog)
{
INET_ERROR res = INET_NO_ERROR;
if (State != kState_Bound)
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// Start listening for incoming connections.
mTCP = tcp_listen(mTCP);
mLwIPEndPointType = kLwIPEndPointType_TCP;
tcp_arg(mTCP, this);
tcp_accept(mTCP, LwIPHandleIncomingConnection);
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (listen(mSocket, backlog) != 0)
res = chip::System::MapErrorPOSIX(errno);
// Wait for ability to read on this endpoint.
mRequestIO.SetRead();
// Wake the thread calling select so that it recognizes the new socket.
SystemLayer().WakeSelect();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (res == INET_NO_ERROR)
{
// Once Listening, bump the reference count. The corresponding call to Release()
// [or on LwIP, DeferredRelease()] will happen in DoClose().
Retain();
State = kState_Listening;
}
return res;
}
INET_ERROR TCPEndPoint::Connect(const IPAddress & addr, uint16_t port, InterfaceId intfId)
{
INET_ERROR res = INET_NO_ERROR;
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
chip::System::Layer & lSystemLayer = SystemLayer();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (State != kState_Ready && State != kState_Bound)
return INET_ERROR_INCORRECT_STATE;
IPAddressType addrType = addr.Type();
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// LwIP does not provides an API for initiating a TCP connection via a specific interface.
// As a work-around, if the destination is an IPv6 link-local address, we bind the PCB
// to the link local address associated with the source interface; however this is only
// viable if the endpoint hasn't already been bound.
if (intfId != INET_NULL_INTERFACEID)
{
IPAddress intfLLAddr;
InetLayer & lInetLayer = Layer();
if (!addr.IsIPv6LinkLocal() || State == kState_Bound)
return INET_ERROR_NOT_IMPLEMENTED;
res = lInetLayer.GetLinkLocalAddr(intfId, &intfLLAddr);
if (res != INET_NO_ERROR)
return res;
res = Bind(kIPAddressType_IPv6, intfLLAddr, 0, true);
if (res != INET_NO_ERROR)
return res;
}
// Lock LwIP stack
LOCK_TCPIP_CORE();
res = GetPCB(addrType);
if (res == INET_NO_ERROR)
{
tcp_arg(mTCP, this);
tcp_err(mTCP, LwIPHandleError);
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip_addr_t lwipAddr = addr.ToLwIPAddr();
res = chip::System::MapErrorLwIP(tcp_connect(mTCP, &lwipAddr, port, LwIPHandleConnectComplete));
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
if (addrType == kIPAddressType_IPv6)
{
ip6_addr_t lwipAddr = addr.ToIPv6();
res = chip::System::MapErrorLwIP(tcp_connect_ip6(mTCP, &lwipAddr, port, LwIPHandleConnectComplete));
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == kIPAddressType_IPv4)
{
ip_addr_t lwipAddr = addr.ToIPv4();
res = chip::System::MapErrorLwIP(tcp_connect(mTCP, &lwipAddr, port, LwIPHandleConnectComplete));
}
#endif // INET_CONFIG_ENABLE_IPV4
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
// Ensure that TCP timers are started
if (res == INET_NO_ERROR)
{
res = start_tcp_timers();
}
if (res == INET_NO_ERROR)
{
State = kState_Connecting;
Retain();
}
}
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
res = GetSocket(addrType);
if (res != INET_NO_ERROR)
return res;
if (intfId == INET_NULL_INTERFACEID)
{
// The behavior when connecting to an IPv6 link-local address without specifying an outbound
// interface is ambiguous. So prevent it in all cases.
if (addr.IsIPv6LinkLocal())
return INET_ERROR_WRONG_ADDRESS_TYPE;
}
else
{
// Try binding to the interface
// If destination is link-local then there is no need to bind to
// interface or address on the interface.
if (!addr.IsIPv6LinkLocal())
{
#ifdef SO_BINDTODEVICE
struct ::ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
res = GetInterfaceName(intfId, ifr.ifr_name, sizeof(ifr.ifr_name));
if (res != INET_NO_ERROR)
return res;
// Attempt to bind to the interface using SO_BINDTODEVICE which requires privileged access.
// If the permission is denied(EACCES) because CHIP is running in a context
// that does not have privileged access, choose a source address on the
// interface to bind the connetion to.
int r = setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
if (r < 0 && errno != EACCES)
{
return res = chip::System::MapErrorPOSIX(errno);
}
if (r < 0)
#endif // SO_BINDTODEVICE
{
// Attempting to initiate a connection via a specific interface is not allowed.
// The only way to do this is to bind the local to an address on the desired
// interface.
res = BindSrcAddrFromIntf(addrType, intfId);
if (res != INET_NO_ERROR)
return res;
}
}
}
// Disable generation of SIGPIPE.
#ifdef SO_NOSIGPIPE
int n = 1;
setsockopt(mSocket, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof(n));
#endif // defined(SO_NOSIGPIPE)
// Enable non-blocking mode for the socket.
int flags = fcntl(mSocket, F_GETFL, 0);
fcntl(mSocket, F_SETFL, flags | O_NONBLOCK);
socklen_t sockaddrsize = 0;
const sockaddr * sockaddrptr = nullptr;
union
{
sockaddr any;
sockaddr_in6 in6;
#if INET_CONFIG_ENABLE_IPV4
sockaddr_in in;
#endif // INET_CONFIG_ENABLE_IPV4
} sa;
memset(&sa, 0, sizeof(sa));
if (addrType == kIPAddressType_IPv6)
{
sa.in6.sin6_family = AF_INET6;
sa.in6.sin6_port = htons(port);
sa.in6.sin6_flowinfo = 0;
sa.in6.sin6_addr = addr.ToIPv6();
sa.in6.sin6_scope_id = intfId;
sockaddrsize = sizeof(sockaddr_in6);
sockaddrptr = reinterpret_cast<const sockaddr *>(&sa.in6);
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == kIPAddressType_IPv4)
{
sa.in.sin_family = AF_INET;
sa.in.sin_port = htons(port);
sa.in.sin_addr = addr.ToIPv4();
sockaddrsize = sizeof(sockaddr_in);
sockaddrptr = reinterpret_cast<const sockaddr *>(&sa.in);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
return INET_ERROR_WRONG_ADDRESS_TYPE;
int conRes = connect(mSocket, sockaddrptr, sockaddrsize);
if (conRes == -1 && errno != EINPROGRESS)
{
res = chip::System::MapErrorPOSIX(errno);
DoClose(res, true);
return res;
}
// Once Connecting or Connected, bump the reference count. The corresponding Release()
// [or on LwIP, DeferredRelease()] will happen in DoClose().
Retain();
if (conRes == 0)
{
State = kState_Connected;
// Wait for ability to read on this endpoint.
mRequestIO.SetRead();
if (OnConnectComplete != nullptr)
OnConnectComplete(this, INET_NO_ERROR);
}
else
{
State = kState_Connecting;
// Wait for ability to write on this endpoint.
mRequestIO.SetWrite();
}
// Wake the thread calling select so that it recognizes the new socket.
lSystemLayer.WakeSelect();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
StartConnectTimerIfSet();
return res;
}
/**
* @brief Set timeout for Connect to succeed or return an error.
*
* @param[in] connTimeoutMsecs
*
* @note
* Setting a value of zero means use system defaults.
*/
void TCPEndPoint::SetConnectTimeout(const uint32_t connTimeoutMsecs)
{
mConnectTimeoutMsecs = connTimeoutMsecs;
}
void TCPEndPoint::StartConnectTimerIfSet()
{
if (mConnectTimeoutMsecs > 0)
{
chip::System::Layer & lSystemLayer = SystemLayer();
lSystemLayer.StartTimer(mConnectTimeoutMsecs, TCPConnectTimeoutHandler, this);
}
}
void TCPEndPoint::StopConnectTimer()
{
chip::System::Layer & lSystemLayer = SystemLayer();
lSystemLayer.CancelTimer(TCPConnectTimeoutHandler, this);
}
void TCPEndPoint::TCPConnectTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState, chip::System::Error aError)
{
TCPEndPoint * tcpEndPoint = reinterpret_cast<TCPEndPoint *>(aAppState);
VerifyOrDie((aSystemLayer != nullptr) && (tcpEndPoint != nullptr));
// Close Connection as we have timed out and Connect has not returned to
// stop this timer.
tcpEndPoint->DoClose(INET_ERROR_TCP_CONNECT_TIMEOUT, false);
}
INET_ERROR TCPEndPoint::GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const
{
INET_ERROR res = INET_NO_ERROR;
if (!IsConnected())
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// Lock LwIP stack
LOCK_TCPIP_CORE();
if (mTCP != NULL)
{
*retPort = mTCP->remote_port;
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
*retAddr = IPAddress::FromLwIPAddr(mTCP->remote_ip);
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
#if INET_CONFIG_ENABLE_IPV4
*retAddr = PCB_ISIPV6(mTCP) ? IPAddress::FromIPv6(mTCP->remote_ip.ip6) : IPAddress::FromIPv4(mTCP->remote_ip.ip4);
#else // !INET_CONFIG_ENABLE_IPV4
*retAddr = IPAddress::FromIPv6(mTCP->remote_ip.ip6);
#endif // !INET_CONFIG_ENABLE_IPV4
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
}
else
res = INET_ERROR_CONNECTION_ABORTED;
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
union
{
sockaddr any;
sockaddr_in in;
sockaddr_in6 in6;
} sa;
memset(&sa, 0, sizeof(sa));
socklen_t saLen = sizeof(sa);
if (getpeername(mSocket, &sa.any, &saLen) != 0)
return chip::System::MapErrorPOSIX(errno);
if (sa.any.sa_family == AF_INET6)
{
*retAddr = IPAddress::FromIPv6(sa.in6.sin6_addr);
*retPort = ntohs(sa.in6.sin6_port);
}
#if INET_CONFIG_ENABLE_IPV4
else if (sa.any.sa_family == AF_INET)
{
*retAddr = IPAddress::FromIPv4(sa.in.sin_addr);
*retPort = ntohs(sa.in.sin_port);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
return INET_ERROR_INCORRECT_STATE;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
return res;
}
INET_ERROR TCPEndPoint::GetLocalInfo(IPAddress * retAddr, uint16_t * retPort)
{
INET_ERROR res = INET_NO_ERROR;
if (!IsConnected())
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// Lock LwIP stack
LOCK_TCPIP_CORE();
if (mTCP != NULL)
{
*retPort = mTCP->local_port;
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
*retAddr = IPAddress::FromLwIPAddr(mTCP->local_ip);
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
#if INET_CONFIG_ENABLE_IPV4
*retAddr = PCB_ISIPV6(mTCP) ? IPAddress::FromIPv6(mTCP->local_ip.ip6) : IPAddress::FromIPv4(mTCP->local_ip.ip4);
#else // !INET_CONFIG_ENABLE_IPV4
*retAddr = IPAddress::FromIPv6(mTCP->local_ip.ip6);
#endif // !INET_CONFIG_ENABLE_IPV4
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
}
else
res = INET_ERROR_CONNECTION_ABORTED;
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
union
{
sockaddr any;
sockaddr_in6 in6;
#if INET_CONFIG_ENABLE_IPV4
sockaddr_in in;
#endif // INET_CONFIG_ENABLE_IPV4
} sa;
memset(&sa, 0, sizeof(sa));
socklen_t saLen = sizeof(sa);
if (getsockname(mSocket, &sa.any, &saLen) != 0)
return chip::System::MapErrorPOSIX(errno);
if (sa.any.sa_family == AF_INET6)
{
*retAddr = IPAddress::FromIPv6(sa.in6.sin6_addr);
*retPort = ntohs(sa.in6.sin6_port);
}
#if INET_CONFIG_ENABLE_IPV4
else if (sa.any.sa_family == AF_INET)
{
*retAddr = IPAddress::FromIPv4(sa.in.sin_addr);
*retPort = ntohs(sa.in.sin_port);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
return INET_ERROR_INCORRECT_STATE;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
return res;
}
INET_ERROR TCPEndPoint::GetInterfaceId(InterfaceId * retInterface)
{
if (!IsConnected())
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// TODO: Does netif_get_by_index(mTCP->netif_idx) do the right thing? I
// can't quite tell whether LwIP supports a specific interface id for TCP at
// all. For now just claim no particular interface id.
*retInterface = INET_NULL_INTERFACEID;
return INET_NO_ERROR;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
union
{
sockaddr any;
sockaddr_in6 in6;
#if INET_CONFIG_ENABLE_IPV4
sockaddr_in in;
#endif // INET_CONFIG_ENABLE_IPV4
} sa;
memset(&sa, 0, sizeof(sa));
socklen_t saLen = sizeof(sa);
if (getpeername(mSocket, &sa.any, &saLen) != 0)
{
return chip::System::MapErrorPOSIX(errno);
}
if (sa.any.sa_family == AF_INET6)
{
if (IPAddress::FromIPv6(sa.in6.sin6_addr).IsIPv6LinkLocal())
{
*retInterface = sa.in6.sin6_scope_id;
}
else
{
// TODO: Is there still a meaningful interface id in this case?
*retInterface = INET_NULL_INTERFACEID;
}
return INET_NO_ERROR;
}
#if INET_CONFIG_ENABLE_IPV4
if (sa.any.sa_family == AF_INET)
{
// No interface id available for IPv4 sockets.
*retInterface = INET_NULL_INTERFACEID;
}
#endif // INET_CONFIG_ENABLE_IPV4
return INET_ERROR_INCORRECT_STATE;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
*retInterface = INET_NULL_INTERFACEID;
return INET_NO_ERROR;
}
INET_ERROR TCPEndPoint::Send(System::PacketBufferHandle && data, bool push)
{
INET_ERROR res = INET_NO_ERROR;
if (State != kState_Connected && State != kState_ReceiveShutdown)
{
return INET_ERROR_INCORRECT_STATE;
}
if (mSendQueue.IsNull())
{
mSendQueue = std::move(data);
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// Wait for ability to write on this endpoint.
mRequestIO.SetWrite();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}
else
{
mSendQueue->AddToEnd(std::move(data));
}
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
if (!mUserTimeoutTimerRunning)
{
// Timer was not running before this send. So, start
// the timer.
StartTCPUserTimeoutTimer();
}
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
if (push)
res = DriveSending();
return res;
}
void TCPEndPoint::DisableReceive()
{
ReceiveEnabled = false;
}
void TCPEndPoint::EnableReceive()
{
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
chip::System::Layer & lSystemLayer = SystemLayer();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
ReceiveEnabled = true;
DriveReceiving();
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// Wake the thread calling select so that it can include the socket
// in the select read fd_set.
lSystemLayer.WakeSelect();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}
/**
* TCPEndPoint::EnableNoDelay
*
* @brief
* Switch off nagle buffering algorithm in TCP by setting the
* TCP_NODELAY socket options.
*
*/
INET_ERROR TCPEndPoint::EnableNoDelay()
{
INET_ERROR res = INET_NO_ERROR;
if (!IsConnected())
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// Lock LwIP stack
LOCK_TCPIP_CORE();
if (mTCP != NULL)
tcp_nagle_disable(mTCP);
else
res = INET_ERROR_CONNECTION_ABORTED;
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
{
int val;
#ifdef TCP_NODELAY
// Disable TCP Nagle buffering by setting TCP_NODELAY socket option to true
val = 1;
if (setsockopt(mSocket, TCP_SOCKOPT_LEVEL, TCP_NODELAY, &val, sizeof(val)) != 0)
return chip::System::MapErrorPOSIX(errno);
#endif // defined(TCP_NODELAY)
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
return res;
}
INET_ERROR TCPEndPoint::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount)
{
INET_ERROR res = INET_NO_ERROR;
if (!IsConnected())
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#if LWIP_TCP_KEEPALIVE
// Lock LwIP stack
LOCK_TCPIP_CORE();
if (mTCP != NULL)
{
// Set the idle interval
mTCP->keep_idle = (uint32_t) interval * 1000;
// Set the probe retransmission interval.
mTCP->keep_intvl = (uint32_t) interval * 1000;
// Set the probe timeout count
mTCP->keep_cnt = timeoutCount;
// Enable keepalives for the connection.
ip_set_option(mTCP, SOF_KEEPALIVE);
}
else
res = INET_ERROR_CONNECTION_ABORTED;
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#else // LWIP_TCP_KEEPALIVE
res = INET_ERROR_NOT_IMPLEMENTED;
#endif // LWIP_TCP_KEEPALIVE
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
{
int val;
// Set the idle interval
val = interval;
if (setsockopt(mSocket, TCP_SOCKOPT_LEVEL, TCP_IDLE_INTERVAL_OPT_NAME, &val, sizeof(val)) != 0)
return chip::System::MapErrorPOSIX(errno);
// Set the probe retransmission interval.
val = interval;
if (setsockopt(mSocket, TCP_SOCKOPT_LEVEL, TCP_KEEPINTVL, &val, sizeof(val)) != 0)
return chip::System::MapErrorPOSIX(errno);
// Set the probe timeout count
val = timeoutCount;
if (setsockopt(mSocket, TCP_SOCKOPT_LEVEL, TCP_KEEPCNT, &val, sizeof(val)) != 0)
return chip::System::MapErrorPOSIX(errno);
// Enable keepalives for the connection.
val = 1; // enable
if (setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) != 0)
return chip::System::MapErrorPOSIX(errno);
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
return res;
}
/**
* TCPEndPoint::DisableKeepAlive
*
* @brief
* Disable TCP keepalive probes on the associated TCP connection.
*
* @note
* This method can only be called when the endpoint is in one of the connected states.
*
* This method does nothing if keepalives have not been enabled on the endpoint.
*/
INET_ERROR TCPEndPoint::DisableKeepAlive()
{
INET_ERROR res = INET_NO_ERROR;
if (!IsConnected())
return INET_ERROR_INCORRECT_STATE;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#if LWIP_TCP_KEEPALIVE
// Lock LwIP stack
LOCK_TCPIP_CORE();
if (mTCP != NULL)
{
// Disable keepalives on the connection.
ip_reset_option(mTCP, SOF_KEEPALIVE);
}
else
res = INET_ERROR_CONNECTION_ABORTED;
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
#else // LWIP_TCP_KEEPALIVE
res = INET_ERROR_NOT_IMPLEMENTED;
#endif // LWIP_TCP_KEEPALIVE
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
{
int val;
// Disable keepalives on the connection.
val = 0; // disable
if (setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) != 0)
return chip::System::MapErrorPOSIX(errno);
}