3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include "TapAddrAutoconf.h"
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
36 int CollectTapAddresses(std::vector<char *> & addresses, const char * ifName)
38 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
39 struct ifaddrs *addrsList, *curAddr;
40 const int err = getifaddrs(&addrsList);
48 if (strcmp(ifName, curAddr->ifa_name) == 0)
50 char buf[INET6_ADDRSTRLEN];
53 rv = inet_ntop(curAddr->ifa_addr->sa_family,
54 curAddr->ifa_addr->sa_family == AF_INET6
55 ? (const void *) &(((sockaddr_in6 *) curAddr->ifa_addr)->sin6_addr)
56 : (const void *) &(((sockaddr_in *) curAddr->ifa_addr)->sin_addr),
60 tmp = (char *) malloc(strlen(buf));
62 addresses.push_back(tmp);
65 curAddr = curAddr->ifa_next;
67 freeifaddrs(addrsList);
76 #error "Unsupported configuration: requires CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS"