3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/iomgr/port.h"
23 #ifdef GRPC_HAVE_IFADDRS
25 #include "src/core/lib/iomgr/tcp_server_utils_posix.h"
34 #include "absl/strings/str_cat.h"
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
39 #include "src/core/lib/address_utils/sockaddr_utils.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/iomgr/sockaddr.h"
43 /* Return the listener in s with address addr or NULL. */
44 static grpc_tcp_listener* find_listener_with_addr(grpc_tcp_server* s,
45 grpc_resolved_address* addr) {
48 for (l = s->head; l != nullptr; l = l->next) {
49 if (l->addr.len != addr->len) {
52 if (memcmp(l->addr.addr, addr->addr, addr->len) == 0) {
56 gpr_mu_unlock(&s->mu);
60 /* Bind to "::" to get a port number not used by any address. */
61 static grpc_error_handle get_unused_port(int* port) {
62 grpc_resolved_address wild;
63 grpc_sockaddr_make_wildcard6(0, &wild);
64 grpc_dualstack_mode dsmode;
66 grpc_error_handle err =
67 grpc_create_dualstack_socket(&wild, SOCK_STREAM, 0, &dsmode, &fd);
68 if (err != GRPC_ERROR_NONE) {
71 if (dsmode == GRPC_DSMODE_IPV4) {
72 grpc_sockaddr_make_wildcard4(0, &wild);
74 if (bind(fd, reinterpret_cast<const grpc_sockaddr*>(wild.addr), wild.len) !=
76 err = GRPC_OS_ERROR(errno, "bind");
80 if (getsockname(fd, reinterpret_cast<grpc_sockaddr*>(wild.addr), &wild.len) !=
82 err = GRPC_OS_ERROR(errno, "getsockname");
87 *port = grpc_sockaddr_get_port(&wild);
88 return *port <= 0 ? GRPC_ERROR_CREATE_FROM_STATIC_STRING("Bad port")
92 grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s,
96 struct ifaddrs* ifa = nullptr;
97 struct ifaddrs* ifa_it;
98 unsigned fd_index = 0;
99 grpc_tcp_listener* sp = nullptr;
100 grpc_error_handle err = GRPC_ERROR_NONE;
101 if (requested_port == 0) {
102 /* Note: There could be a race where some local addrs can listen on the
103 selected port and some can't. The sane way to handle this would be to
104 retry by recreating the whole grpc_tcp_server. Backing out individual
105 listeners and orphaning the FDs looks like too much trouble. */
106 if ((err = get_unused_port(&requested_port)) != GRPC_ERROR_NONE) {
108 } else if (requested_port <= 0) {
109 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Bad get_unused_port()");
111 gpr_log(GPR_DEBUG, "Picked unused port %d", requested_port);
113 if (getifaddrs(&ifa) != 0 || ifa == nullptr) {
114 return GRPC_OS_ERROR(errno, "getifaddrs");
116 for (ifa_it = ifa; ifa_it != nullptr; ifa_it = ifa_it->ifa_next) {
117 grpc_resolved_address addr;
118 grpc_dualstack_mode dsmode;
119 grpc_tcp_listener* new_sp = nullptr;
120 const char* ifa_name = (ifa_it->ifa_name ? ifa_it->ifa_name : "<unknown>");
121 if (ifa_it->ifa_addr == nullptr) {
123 } else if (ifa_it->ifa_addr->sa_family == AF_INET) {
124 addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in));
125 } else if (ifa_it->ifa_addr->sa_family == AF_INET6) {
126 addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
130 memcpy(addr.addr, ifa_it->ifa_addr, addr.len);
131 if (!grpc_sockaddr_set_port(&addr, requested_port)) {
132 /* Should never happen, because we check sa_family above. */
133 err = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed to set port");
136 std::string addr_str = grpc_sockaddr_to_string(&addr, false);
138 "Adding local addr from interface %s flags 0x%x to server: %s",
139 ifa_name, ifa_it->ifa_flags, addr_str.c_str());
140 /* We could have multiple interfaces with the same address (e.g., bonding),
141 so look for duplicates. */
142 if (find_listener_with_addr(s, &addr) != nullptr) {
143 gpr_log(GPR_DEBUG, "Skipping duplicate addr %s on interface %s",
144 addr_str.c_str(), ifa_name);
147 if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode,
148 &new_sp)) != GRPC_ERROR_NONE) {
149 grpc_error_handle root_err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
150 absl::StrCat("Failed to add listener: ", addr_str).c_str());
151 err = grpc_error_add_child(root_err, err);
154 GPR_ASSERT(requested_port == new_sp->port);
157 new_sp->is_sibling = 1;
158 sp->sibling = new_sp;
164 if (err != GRPC_ERROR_NONE) {
166 } else if (sp == nullptr) {
167 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("No local addresses");
169 *out_port = sp->port;
170 return GRPC_ERROR_NONE;
174 bool grpc_tcp_server_have_ifaddrs(void) { return true; }
176 #endif /* GRPC_HAVE_IFADDRS */