forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInetInterface.cpp
1053 lines (901 loc) · 27.7 KB
/
InetInterface.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-2022 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* Copyright (c) 2013-2017 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
* Implementation of network interface abstraction layer.
*
*/
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#include <inet/InetInterface.h>
#include <inet/IPPrefix.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DLLUtil.h>
#include <lib/support/SafeInt.h>
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#include <lwip/netif.h>
#include <lwip/sys.h>
#include <lwip/tcpip.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif /* HAVE_SYS_SOCKIO_H */
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/ioctl.h>
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
#include <zephyr/net/net_if.h>
#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#include <inet/UDPEndPointImplOpenThread.h>
#endif
#include <stdio.h>
#include <string.h>
namespace chip {
namespace Inet {
#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
CHIP_ERROR InterfaceId::GetInterfaceName(char * nameBuf, size_t nameBufSize) const
{
if (mPlatformInterface && nameBufSize >= kMaxIfNameLength)
{
nameBuf[0] = 'o';
nameBuf[1] = 't';
nameBuf[2] = 0;
}
else
{
nameBuf[0] = 0;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR InterfaceId::InterfaceNameToId(const char * intfName, InterfaceId & interface)
{
if (strlen(intfName) < 3)
{
return INET_ERROR_UNKNOWN_INTERFACE;
}
char * parseEnd = nullptr;
unsigned long intfNum = strtoul(intfName + 2, &parseEnd, 10);
if (*parseEnd != 0 || intfNum > UINT8_MAX)
{
return INET_ERROR_UNKNOWN_INTERFACE;
}
interface = InterfaceId(intfNum);
if (intfNum == 0)
{
return INET_ERROR_UNKNOWN_INTERFACE;
}
return CHIP_NO_ERROR;
}
bool InterfaceIterator::Next()
{
// TODO : Cleanup #17346
return false;
}
InterfaceAddressIterator::InterfaceAddressIterator()
{
mNetifAddrList = nullptr;
mCurAddr = nullptr;
}
bool InterfaceAddressIterator::HasCurrent()
{
return (mNetifAddrList != nullptr) ? (mCurAddr != nullptr) : Next();
}
bool InterfaceAddressIterator::Next()
{
if (mNetifAddrList == nullptr)
{
mNetifAddrList = otIp6GetUnicastAddresses(Inet::globalOtInstance);
mCurAddr = mNetifAddrList;
}
else if (mCurAddr != nullptr)
{
mCurAddr = mCurAddr->mNext;
}
return (mCurAddr != nullptr);
}
CHIP_ERROR InterfaceAddressIterator::GetAddress(IPAddress & outIPAddress)
{
if (!HasCurrent())
{
return CHIP_ERROR_SENTINEL;
}
outIPAddress = IPAddress(mCurAddr->mAddress);
return CHIP_NO_ERROR;
}
uint8_t InterfaceAddressIterator::GetPrefixLength()
{
// Only 64 bits prefix are supported
return 64;
}
#endif
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
CHIP_ERROR InterfaceId::GetInterfaceName(char * nameBuf, size_t nameBufSize) const
{
if (mPlatformInterface)
{
int status = snprintf(nameBuf, nameBufSize, "%c%c%d", mPlatformInterface->name[0], mPlatformInterface->name[1],
mPlatformInterface->num);
if (status >= static_cast<int>(nameBufSize))
return CHIP_ERROR_BUFFER_TOO_SMALL;
return CHIP_NO_ERROR;
}
if (nameBufSize < 1)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
nameBuf[0] = 0;
return CHIP_NO_ERROR;
}
CHIP_ERROR InterfaceId::InterfaceNameToId(const char * intfName, InterfaceId & interface)
{
if (strlen(intfName) < 3)
{
return INET_ERROR_UNKNOWN_INTERFACE;
}
char * parseEnd;
unsigned long intfNum = strtoul(intfName + 2, &parseEnd, 10);
if (*parseEnd != 0 || intfNum > UINT8_MAX)
{
return INET_ERROR_UNKNOWN_INTERFACE;
}
struct netif * intf;
#if defined(NETIF_FOREACH)
NETIF_FOREACH(intf)
#else
for (intf = netif_list; intf != NULL; intf = intf->next)
#endif
{
if (intf->name[0] == intfName[0] && intf->name[1] == intfName[1] && intf->num == (uint8_t) intfNum)
{
interface = InterfaceId(intf);
return CHIP_NO_ERROR;
}
}
interface = InterfaceId::Null();
return INET_ERROR_UNKNOWN_INTERFACE;
}
bool InterfaceIterator::Next()
{
// Lock LwIP stack
LOCK_TCPIP_CORE();
// Verify the previous netif is still on the list if netifs. If so,
// advance to the next nextif.
struct netif * prevNetif = mCurNetif;
#if defined(NETIF_FOREACH)
NETIF_FOREACH(mCurNetif)
#else
for (mCurNetif = netif_list; mCurNetif != NULL; mCurNetif = mCurNetif->next)
#endif
{
if (mCurNetif == prevNetif)
{
mCurNetif = mCurNetif->next;
break;
}
}
// Unlock LwIP stack
UNLOCK_TCPIP_CORE();
return mCurNetif != NULL;
}
CHIP_ERROR InterfaceIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
return InterfaceId(mCurNetif).GetInterfaceName(nameBuf, nameBufSize);
}
bool InterfaceIterator::IsUp()
{
return HasCurrent() && netif_is_up(mCurNetif);
}
bool InterfaceIterator::SupportsMulticast()
{
return HasCurrent() && (mCurNetif->flags & (NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 | NETIF_FLAG_BROADCAST)) != 0;
}
bool InterfaceIterator::HasBroadcastAddress()
{
return HasCurrent() && (mCurNetif->flags & NETIF_FLAG_BROADCAST) != 0;
}
CHIP_ERROR InterfaceIterator::GetInterfaceType(InterfaceType & type)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR InterfaceIterator::GetHardwareAddress(uint8_t * addressBuffer, uint8_t & addressSize, uint8_t addressBufferSize)
{
VerifyOrReturnError(addressBuffer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(addressBufferSize >= mCurNetif->hwaddr_len, CHIP_ERROR_BUFFER_TOO_SMALL);
addressSize = mCurNetif->hwaddr_len;
memcpy(addressBuffer, mCurNetif->hwaddr, addressSize);
return CHIP_NO_ERROR;
}
bool InterfaceAddressIterator::HasCurrent()
{
return mIntfIter.HasCurrent() && ((mCurAddrIndex != kBeforeStartIndex) || Next());
}
bool InterfaceAddressIterator::Next()
{
mCurAddrIndex++;
while (mIntfIter.HasCurrent())
{
struct netif * curIntf = mIntfIter.GetInterfaceId().GetPlatformInterface();
while (mCurAddrIndex < LWIP_IPV6_NUM_ADDRESSES)
{
if (ip6_addr_isvalid(netif_ip6_addr_state(curIntf, mCurAddrIndex)))
{
return true;
}
mCurAddrIndex++;
}
#if INET_CONFIG_ENABLE_IPV4 && LWIP_IPV4
if (mCurAddrIndex == LWIP_IPV6_NUM_ADDRESSES)
{
if (!ip4_addr_isany(netif_ip4_addr(curIntf)))
{
return true;
}
}
#endif // INET_CONFIG_ENABLE_IPV4 && LWIP_IPV4
mIntfIter.Next();
mCurAddrIndex = 0;
}
return false;
}
CHIP_ERROR InterfaceAddressIterator::GetAddress(IPAddress & outIPAddress)
{
if (!HasCurrent())
{
return CHIP_ERROR_SENTINEL;
}
struct netif * curIntf = mIntfIter.GetInterfaceId().GetPlatformInterface();
if (mCurAddrIndex < LWIP_IPV6_NUM_ADDRESSES)
{
outIPAddress = IPAddress(*netif_ip6_addr(curIntf, mCurAddrIndex));
return CHIP_NO_ERROR;
}
#if INET_CONFIG_ENABLE_IPV4 && LWIP_IPV4
else
{
outIPAddress = IPAddress(*netif_ip4_addr(curIntf));
return CHIP_NO_ERROR;
}
#endif // INET_CONFIG_ENABLE_IPV4 && LWIP_IPV4
return CHIP_ERROR_INTERNAL;
}
uint8_t InterfaceAddressIterator::GetPrefixLength()
{
if (HasCurrent())
{
if (mCurAddrIndex < LWIP_IPV6_NUM_ADDRESSES)
{
return 64;
}
#if INET_CONFIG_ENABLE_IPV4 && LWIP_IPV4
else
{
struct netif * curIntf = mIntfIter.GetInterfaceId().GetPlatformInterface();
return NetmaskToPrefixLength((const uint8_t *) netif_ip4_netmask(curIntf), 4);
}
#endif // INET_CONFIG_ENABLE_IPV4 && LWIP_IPV4
}
return 0;
}
InterfaceId InterfaceAddressIterator::GetInterfaceId()
{
return HasCurrent() ? mIntfIter.GetInterfaceId() : InterfaceId::Null();
}
CHIP_ERROR InterfaceAddressIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
return mIntfIter.GetInterfaceName(nameBuf, nameBufSize);
}
bool InterfaceAddressIterator::IsUp()
{
return HasCurrent() && mIntfIter.IsUp();
}
bool InterfaceAddressIterator::SupportsMulticast()
{
return HasCurrent() && mIntfIter.SupportsMulticast();
}
bool InterfaceAddressIterator::HasBroadcastAddress()
{
return HasCurrent() && mIntfIter.HasBroadcastAddress();
}
CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
{
VerifyOrReturnError(llAddr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
#if !LWIP_IPV6
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif //! LWIP_IPV6
for (struct netif * intf = netif_list; intf != nullptr; intf = intf->next)
{
if ((mPlatformInterface != nullptr) && (mPlatformInterface != intf))
continue;
for (int j = 0; j < LWIP_IPV6_NUM_ADDRESSES; ++j)
{
if (ip6_addr_isvalid(netif_ip6_addr_state(intf, j)) && ip6_addr_islinklocal(netif_ip6_addr(intf, j)))
{
(*llAddr) = IPAddress(*netif_ip6_addr(intf, j));
return CHIP_NO_ERROR;
}
}
if (mPlatformInterface != nullptr)
{
return INET_ERROR_ADDRESS_NOT_FOUND;
}
}
return CHIP_NO_ERROR;
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
CHIP_ERROR InterfaceId::GetInterfaceName(char * nameBuf, size_t nameBufSize) const
{
if (mPlatformInterface)
{
char intfName[IF_NAMESIZE];
if (if_indextoname(mPlatformInterface, intfName) == nullptr)
{
return CHIP_ERROR_POSIX(errno);
}
size_t nameLength = strlen(intfName);
if (nameLength >= nameBufSize)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
Platform::CopyString(nameBuf, nameBufSize, intfName);
return CHIP_NO_ERROR;
}
if (nameBufSize < 1)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
nameBuf[0] = 0;
return CHIP_NO_ERROR;
}
CHIP_ERROR InterfaceId::InterfaceNameToId(const char * intfName, InterfaceId & interface)
{
// First attempt to parse as a numeric ID:
char * parseEnd;
unsigned long intfNum = strtoul(intfName, &parseEnd, 10);
if (*parseEnd == 0)
{
if (intfNum > 0 && intfNum < UINT8_MAX && CanCastTo<InterfaceId::PlatformType>(intfNum))
{
interface = InterfaceId(static_cast<InterfaceId::PlatformType>(intfNum));
return CHIP_NO_ERROR;
}
return INET_ERROR_UNKNOWN_INTERFACE;
}
// Falling back to name -> ID lookup otherwise (e.g. wlan0)
unsigned int intfId = if_nametoindex(intfName);
interface = InterfaceId(intfId);
if (intfId == 0)
{
return (errno == ENXIO) ? INET_ERROR_UNKNOWN_INTERFACE : CHIP_ERROR_POSIX(errno);
}
return CHIP_NO_ERROR;
}
static int sIOCTLSocket = -1;
/**
* @brief Returns a global general purpose socket useful for invoking certain network IOCTLs.
*
* This function is thread-safe on all platforms.
*/
int GetIOCTLSocket()
{
if (sIOCTLSocket == -1)
{
int s;
#ifdef SOCK_CLOEXEC
s = socket(AF_INET, SOCK_STREAM, SOCK_CLOEXEC);
if (s < 0)
#endif
{
s = socket(AF_INET, SOCK_STREAM, 0);
fcntl(s, O_CLOEXEC);
}
if (!__sync_bool_compare_and_swap(&sIOCTLSocket, -1, s))
{
close(s);
}
}
return sIOCTLSocket;
}
/**
* @brief Close the global socket created by \c GetIOCTLSocket.
*
* @details
* This function is provided for cases were leaving the global IOCTL socket
* open would register as a leak.
*
* NB: This function is NOT thread-safe with respect to \c GetIOCTLSocket.
*/
void CloseIOCTLSocket()
{
if (sIOCTLSocket != -1)
{
close(sIOCTLSocket);
sIOCTLSocket = -1;
}
}
InterfaceIterator::InterfaceIterator()
{
mIntfArray = nullptr;
mCurIntf = 0;
mIntfFlags = 0;
mIntfFlagsCached = false;
}
InterfaceIterator::~InterfaceIterator()
{
if (mIntfArray != nullptr)
{
if_freenameindex(mIntfArray);
mIntfArray = nullptr;
}
}
bool InterfaceIterator::HasCurrent()
{
return (mIntfArray != nullptr) ? mIntfArray[mCurIntf].if_index != 0 : Next();
}
bool InterfaceIterator::Next()
{
if (mIntfArray == nullptr)
{
mIntfArray = if_nameindex();
}
else if (mIntfArray[mCurIntf].if_index != 0)
{
mCurIntf++;
mIntfFlags = 0;
mIntfFlagsCached = false;
}
return (mIntfArray != nullptr && mIntfArray[mCurIntf].if_index != 0);
}
InterfaceId InterfaceIterator::GetInterfaceId()
{
return HasCurrent() ? InterfaceId(mIntfArray[mCurIntf].if_index) : InterfaceId::Null();
}
CHIP_ERROR InterfaceIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(strlen(mIntfArray[mCurIntf].if_name) < nameBufSize, CHIP_ERROR_BUFFER_TOO_SMALL);
Platform::CopyString(nameBuf, nameBufSize, mIntfArray[mCurIntf].if_name);
return CHIP_NO_ERROR;
}
bool InterfaceIterator::IsUp()
{
return (GetFlags() & IFF_UP) != 0;
}
bool InterfaceIterator::SupportsMulticast()
{
return (GetFlags() & IFF_MULTICAST) != 0;
}
bool InterfaceIterator::HasBroadcastAddress()
{
return (GetFlags() & IFF_BROADCAST) != 0;
}
short InterfaceIterator::GetFlags()
{
struct ifreq intfData;
if (!mIntfFlagsCached && HasCurrent())
{
Platform::CopyString(intfData.ifr_name, mIntfArray[mCurIntf].if_name);
int res = ioctl(GetIOCTLSocket(), SIOCGIFFLAGS, &intfData);
if (res == 0)
{
mIntfFlags = intfData.ifr_flags;
mIntfFlagsCached = true;
}
#if __MBED__
CloseIOCTLSocket();
#endif
}
return mIntfFlags;
}
CHIP_ERROR InterfaceIterator::GetInterfaceType(InterfaceType & type)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR InterfaceIterator::GetHardwareAddress(uint8_t * addressBuffer, uint8_t & addressSize, uint8_t addressBufferSize)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
InterfaceAddressIterator::InterfaceAddressIterator()
{
mAddrsList = nullptr;
mCurAddr = nullptr;
}
InterfaceAddressIterator::~InterfaceAddressIterator()
{
if (mAddrsList != nullptr)
{
freeifaddrs(mAddrsList);
mAddrsList = mCurAddr = nullptr;
}
}
bool InterfaceAddressIterator::HasCurrent()
{
return (mAddrsList != nullptr) ? (mCurAddr != nullptr) : Next();
}
bool InterfaceAddressIterator::Next()
{
while (true)
{
if (mAddrsList == nullptr)
{
int res = getifaddrs(&mAddrsList);
if (res < 0)
{
return false;
}
mCurAddr = mAddrsList;
}
else if (mCurAddr != nullptr)
{
mCurAddr = mCurAddr->ifa_next;
}
if (mCurAddr == nullptr)
{
return false;
}
if (mCurAddr->ifa_addr != nullptr &&
(mCurAddr->ifa_addr->sa_family == AF_INET6
#if INET_CONFIG_ENABLE_IPV4
|| mCurAddr->ifa_addr->sa_family == AF_INET
#endif // INET_CONFIG_ENABLE_IPV4
))
{
return true;
}
}
}
CHIP_ERROR InterfaceAddressIterator::GetAddress(IPAddress & outIPAddress)
{
return HasCurrent() ? IPAddress::GetIPAddressFromSockAddr(*mCurAddr->ifa_addr, outIPAddress) : CHIP_ERROR_SENTINEL;
}
uint8_t InterfaceAddressIterator::GetPrefixLength()
{
if (HasCurrent())
{
if (mCurAddr->ifa_addr->sa_family == AF_INET6)
{
#if !__MBED__
struct sockaddr_in6 & netmask = *reinterpret_cast<struct sockaddr_in6 *>(mCurAddr->ifa_netmask);
return NetmaskToPrefixLength(netmask.sin6_addr.s6_addr, 16);
#else // __MBED__
// netmask is not available through an API for IPv6 interface in Mbed.
// Default prefix length to 64.
return 64;
#endif // !__MBED__
}
if (mCurAddr->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in & netmask = *reinterpret_cast<struct sockaddr_in *>(mCurAddr->ifa_netmask);
return NetmaskToPrefixLength(reinterpret_cast<const uint8_t *>(&netmask.sin_addr.s_addr), 4);
}
}
return 0;
}
InterfaceId InterfaceAddressIterator::GetInterfaceId()
{
return HasCurrent() ? InterfaceId(if_nametoindex(mCurAddr->ifa_name)) : InterfaceId::Null();
}
CHIP_ERROR InterfaceAddressIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(strlen(mCurAddr->ifa_name) < nameBufSize, CHIP_ERROR_BUFFER_TOO_SMALL);
Platform::CopyString(nameBuf, nameBufSize, mCurAddr->ifa_name);
return CHIP_NO_ERROR;
}
bool InterfaceAddressIterator::IsUp()
{
return HasCurrent() && (mCurAddr->ifa_flags & IFF_UP) != 0;
}
bool InterfaceAddressIterator::SupportsMulticast()
{
return HasCurrent() && (mCurAddr->ifa_flags & IFF_MULTICAST) != 0;
}
bool InterfaceAddressIterator::HasBroadcastAddress()
{
return HasCurrent() && (mCurAddr->ifa_flags & IFF_BROADCAST) != 0;
}
CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
{
VerifyOrReturnError(llAddr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
struct ifaddrs * ifaddr;
const int rv = getifaddrs(&ifaddr);
bool found = false;
if (rv == -1)
{
return INET_ERROR_ADDRESS_NOT_FOUND;
}
for (struct ifaddrs * ifaddr_iter = ifaddr; ifaddr_iter != nullptr; ifaddr_iter = ifaddr_iter->ifa_next)
{
if (ifaddr_iter->ifa_addr != nullptr)
{
if ((ifaddr_iter->ifa_addr->sa_family == AF_INET6) &&
((mPlatformInterface == 0) || (mPlatformInterface == if_nametoindex(ifaddr_iter->ifa_name))))
{
struct in6_addr * sin6_addr = &(reinterpret_cast<struct sockaddr_in6 *>(ifaddr_iter->ifa_addr))->sin6_addr;
if ((sin6_addr->s6_addr[0] == 0xfe) && ((sin6_addr->s6_addr[1] & 0xc0) == 0x80)) // Link Local Address
{
(*llAddr) = IPAddress((reinterpret_cast<struct sockaddr_in6 *>(ifaddr_iter->ifa_addr))->sin6_addr);
found = true;
break;
}
}
}
}
freeifaddrs(ifaddr);
return (found) ? CHIP_NO_ERROR : INET_ERROR_ADDRESS_NOT_FOUND;
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
CHIP_ERROR InterfaceId::GetInterfaceName(char * nameBuf, size_t nameBufSize) const
{
if (mPlatformInterface)
{
net_if * currentInterface = net_if_get_by_index(mPlatformInterface);
if (!currentInterface)
{
return CHIP_ERROR_INCORRECT_STATE;
}
const char * name = net_if_get_device(currentInterface)->name;
size_t nameLength = strlen(name);
if (nameLength >= nameBufSize)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
Platform::CopyString(nameBuf, nameBufSize, name);
return CHIP_NO_ERROR;
}
if (nameBufSize < 1)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
nameBuf[0] = 0;
return CHIP_NO_ERROR;
}
CHIP_ERROR InterfaceId::InterfaceNameToId(const char * intfName, InterfaceId & interface)
{
int currentId = 0;
net_if * currentInterface;
while ((currentInterface = net_if_get_by_index(++currentId)) != nullptr)
{
if (strcmp(net_if_get_device(currentInterface)->name, intfName) == 0)
{
interface = InterfaceId(currentId);
return CHIP_NO_ERROR;
}
}
interface = InterfaceId::Null();
return INET_ERROR_UNKNOWN_INTERFACE;
}
InterfaceIterator::InterfaceIterator() : mCurrentInterface(net_if_get_by_index(mCurrentId)) {}
bool InterfaceIterator::HasCurrent(void)
{
return mCurrentInterface != nullptr;
}
bool InterfaceIterator::Next()
{
mCurrentInterface = net_if_get_by_index(++mCurrentId);
return HasCurrent();
}
InterfaceId InterfaceIterator::GetInterfaceId(void)
{
return HasCurrent() ? InterfaceId(mCurrentId) : InterfaceId::Null();
}
CHIP_ERROR InterfaceIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
return InterfaceId(mCurrentId).GetInterfaceName(nameBuf, nameBufSize);
}
bool InterfaceIterator::IsUp()
{
return HasCurrent() && net_if_is_up(mCurrentInterface);
}
bool InterfaceIterator::SupportsMulticast()
{
return HasCurrent() && NET_IF_MAX_IPV6_MADDR > 0;
}
bool InterfaceIterator::HasBroadcastAddress()
{
// Zephyr seems to handle broadcast address for IPv4 implicitly
return HasCurrent() && INET_CONFIG_ENABLE_IPV4;
}
CHIP_ERROR InterfaceIterator::GetInterfaceType(InterfaceType & type)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
const net_linkaddr * linkAddr = net_if_get_link_addr(mCurrentInterface);
if (!linkAddr)
return CHIP_ERROR_INCORRECT_STATE;
// Do not consider other than WiFi and Thread for now.
if (linkAddr->type == NET_LINK_IEEE802154)
{
type = InterfaceType::Thread;
}
// Zephyr doesn't define WiFi address type, so it shares the same type as Ethernet.
else if (linkAddr->type == NET_LINK_ETHERNET)
{
type = InterfaceType::WiFi;
}
else
{
type = InterfaceType::Unknown;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR InterfaceIterator::GetHardwareAddress(uint8_t * addressBuffer, uint8_t & addressSize, uint8_t addressBufferSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
if (!addressBuffer)
return CHIP_ERROR_INVALID_ARGUMENT;
const net_linkaddr * linkAddr = net_if_get_link_addr(mCurrentInterface);
if (!linkAddr)
return CHIP_ERROR_INCORRECT_STATE;
if (linkAddr->len > addressBufferSize)
return CHIP_ERROR_BUFFER_TOO_SMALL;
addressSize = linkAddr->len;
memcpy(addressBuffer, linkAddr->addr, linkAddr->len);
return CHIP_NO_ERROR;
}
InterfaceAddressIterator::InterfaceAddressIterator() = default;
bool InterfaceAddressIterator::HasCurrent()
{
return mIntfIter.HasCurrent() && (mCurAddrIndex >= 0 || Next());
}
bool InterfaceAddressIterator::Next()
{
while (mIntfIter.HasCurrent())
{
if (mCurAddrIndex == -1) // first address for the current interface
{
const net_if_config * config =
net_if_get_config(net_if_get_by_index(mIntfIter.GetInterfaceId().GetPlatformInterface()));
mIpv6 = config->ip.ipv6;
}
while (++mCurAddrIndex < NET_IF_MAX_IPV6_ADDR)
if (mIpv6->unicast[mCurAddrIndex].is_used)
return true;
mCurAddrIndex = -1;
mIntfIter.Next();
}
return false;
}
CHIP_ERROR InterfaceAddressIterator::GetAddress(IPAddress & outIPAddress)
{
if (HasCurrent())
{
outIPAddress = IPAddress(mIpv6->unicast[mCurAddrIndex].address.in6_addr);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_SENTINEL;
}
uint8_t InterfaceAddressIterator::GetPrefixLength()
{
if (HasCurrent())
{
net_if * const iface = net_if_get_by_index(mIntfIter.GetInterfaceId().GetPlatformInterface());
net_if_ipv6_prefix * const prefix = net_if_ipv6_prefix_get(iface, &mIpv6->unicast[mCurAddrIndex].address.in6_addr);
return prefix ? prefix->len : 128;
}
return 0;
}
InterfaceId InterfaceAddressIterator::GetInterfaceId()
{
return HasCurrent() ? mIntfIter.GetInterfaceId() : InterfaceId::Null();
}
CHIP_ERROR InterfaceAddressIterator::GetInterfaceName(char * nameBuf, size_t nameBufSize)
{
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);
return mIntfIter.GetInterfaceName(nameBuf, nameBufSize);
}
bool InterfaceAddressIterator::IsUp()
{
return HasCurrent() && mIntfIter.IsUp();
}
bool InterfaceAddressIterator::SupportsMulticast()
{
return HasCurrent() && mIntfIter.SupportsMulticast();
}
bool InterfaceAddressIterator::HasBroadcastAddress()
{
return HasCurrent() && mIntfIter.HasBroadcastAddress();
}
CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
{
VerifyOrReturnError(llAddr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
net_if * const iface = mPlatformInterface ? net_if_get_by_index(mPlatformInterface) : net_if_get_default();
VerifyOrReturnError(iface != nullptr, INET_ERROR_ADDRESS_NOT_FOUND);
in6_addr * const ip6_addr = net_if_ipv6_get_ll(iface, NET_ADDR_PREFERRED);
VerifyOrReturnError(ip6_addr != nullptr, INET_ERROR_ADDRESS_NOT_FOUND);
*llAddr = IPAddress(*ip6_addr);
return CHIP_NO_ERROR;
}
#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
// static
InterfaceId InterfaceId::FromIPAddress(const IPAddress & addr)
{
InterfaceAddressIterator addrIter;
for (; addrIter.HasCurrent(); addrIter.Next())
{
IPAddress curAddr;
if ((addrIter.GetAddress(curAddr) == CHIP_NO_ERROR) && (addr == curAddr))
{
return addrIter.GetInterfaceId();
}
}
return InterfaceId::Null();
}
// static
bool InterfaceId::MatchLocalIPv6Subnet(const IPAddress & addr)
{
if (addr.IsIPv6LinkLocal())