1 /* Copyright libuv project contributors. All rights reserved.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #if !defined(__CYGWIN__) && !defined(__MSYS__) && !defined(__GNU__)
31 #include <net/if_dl.h>
34 #if defined(__HAIKU__)
35 #define IFF_RUNNING IFF_LINK
38 static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
39 if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
41 if (ent->ifa_addr == NULL)
43 #if !defined(__CYGWIN__) && !defined(__MSYS__) && !defined(__GNU__)
45 * If `exclude_type` is `UV__EXCLUDE_IFPHYS`, return whether `sa_family`
46 * equals `AF_LINK`. Otherwise, the result depends on the operating
47 * system with `AF_LINK` or `PF_INET`.
49 if (exclude_type == UV__EXCLUDE_IFPHYS)
50 return (ent->ifa_addr->sa_family != AF_LINK);
52 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__) || \
55 * On BSD getifaddrs returns information related to the raw underlying
56 * devices. We're not interested in this information.
58 if (ent->ifa_addr->sa_family == AF_LINK)
60 #elif defined(__NetBSD__) || defined(__OpenBSD__)
61 if (ent->ifa_addr->sa_family != PF_INET &&
62 ent->ifa_addr->sa_family != PF_INET6)
68 int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
69 struct ifaddrs* addrs;
71 uv_interface_address_t* address;
72 #if !(defined(__CYGWIN__) || defined(__MSYS__)) && !defined(__GNU__)
79 if (getifaddrs(&addrs) != 0)
80 return UV__ERR(errno);
82 /* Count the number of interfaces */
83 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
84 if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
94 /* Make sure the memory is initiallized to zero using calloc() */
95 *addresses = uv__calloc(*count, sizeof(**addresses));
97 if (*addresses == NULL) {
102 address = *addresses;
104 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
105 if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
108 address->name = uv__strdup(ent->ifa_name);
110 if (ent->ifa_addr->sa_family == AF_INET6) {
111 address->address.address6 = *((struct sockaddr_in6*) ent->ifa_addr);
113 address->address.address4 = *((struct sockaddr_in*) ent->ifa_addr);
116 if (ent->ifa_netmask == NULL) {
117 memset(&address->netmask, 0, sizeof(address->netmask));
118 } else if (ent->ifa_netmask->sa_family == AF_INET6) {
119 address->netmask.netmask6 = *((struct sockaddr_in6*) ent->ifa_netmask);
121 address->netmask.netmask4 = *((struct sockaddr_in*) ent->ifa_netmask);
124 address->is_internal = !!(ent->ifa_flags & IFF_LOOPBACK);
129 #if !(defined(__CYGWIN__) || defined(__MSYS__)) && !defined(__GNU__)
130 /* Fill in physical addresses for each interface */
131 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
132 if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFPHYS))
135 address = *addresses;
137 for (i = 0; i < *count; i++) {
138 if (strcmp(address->name, ent->ifa_name) == 0) {
139 struct sockaddr_dl* sa_addr;
140 sa_addr = (struct sockaddr_dl*)(ent->ifa_addr);
141 memcpy(address->phys_addr, LLADDR(sa_addr), sizeof(address->phys_addr));
154 void uv_free_interface_addresses(uv_interface_address_t* addresses,
158 for (i = 0; i < count; i++) {
159 uv__free(addresses[i].name);