Debian Codegraph @

src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/port.h"
22
23 #ifdef GRPC_HAVE_IFADDRS
24
25 #include <errno.h>
26 #include <ifaddrs.h>
27 #include <stddef.h>
28 #include <string.h>
29 #include <sys/socket.h>
30
31 #include <string>
32
33 #include "absl/strings/str_cat.h"
34
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37
38 #include "src/core/lib/address_utils/sockaddr_utils.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/sockaddr.h"
41 #include "src/core/lib/iomgr/tcp_server_utils_posix.h"
42
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) {
46 grpc_tcp_listener* l;
47 gpr_mu_lock(&s->mu);
48 for (l = s->head; l != nullptr; l = l->next) {
49 if (l->addr.len != addr->len) {
50 continue;
51 }
52 if (memcmp(l->addr.addr, addr->addr, addr->len) == 0) {
53 break;
54 }
55 }
56 gpr_mu_unlock(&s->mu);
57 return l;
58 }
59
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;
65 int fd;
66 grpc_error_handle err =
67 grpc_create_dualstack_socket(&wild, SOCK_STREAM, 0, &dsmode, &fd);
68 if (!err.ok()) {
69 return err;
70 }
71 if (dsmode == GRPC_DSMODE_IPV4) {
72 grpc_sockaddr_make_wildcard4(0, &wild);
73 }
74 if (bind(fd, reinterpret_cast<const grpc_sockaddr*>(wild.addr), wild.len) !=
75 0) {
76 err = GRPC_OS_ERROR(errno, "bind");
77 close(fd);
78 return err;
79 }
80 if (getsockname(fd, reinterpret_cast<grpc_sockaddr*>(wild.addr), &wild.len) !=
81 0) {
82 err = GRPC_OS_ERROR(errno, "getsockname");
83 close(fd);
84 return err;
85 }
86 close(fd);
87 *port = grpc_sockaddr_get_port(&wild);
88 return *port <= 0 ? GRPC_ERROR_CREATE("Bad port") : absl::OkStatus();
89 }
90
91 grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s,
92 unsigned port_index,
93 int requested_port,
94 int* out_port) {
95 struct ifaddrs* ifa = nullptr;
96 struct ifaddrs* ifa_it;
97 unsigned fd_index = 0;
98 grpc_tcp_listener* sp = nullptr;
99 grpc_error_handle err;
100 if (requested_port == 0) {
101 /* Note: There could be a race where some local addrs can listen on the
102 selected port and some can't. The sane way to handle this would be to
103 retry by recreating the whole grpc_tcp_server. Backing out individual
104 listeners and orphaning the FDs looks like too much trouble. */
105 if ((err = get_unused_port(&requested_port)) != absl::OkStatus()) {
106 return err;
107 } else if (requested_port <= 0) {
108 return GRPC_ERROR_CREATE("Bad get_unused_port()");
109 }
110 gpr_log(GPR_DEBUG, "Picked unused port %d", requested_port);
111 }
112 if (getifaddrs(&ifa) != 0 || ifa == nullptr) {
113 return GRPC_OS_ERROR(errno, "getifaddrs");
114 }
115 for (ifa_it = ifa; ifa_it != nullptr; ifa_it = ifa_it->ifa_next) {
116 grpc_resolved_address addr;
117 grpc_dualstack_mode dsmode;
118 grpc_tcp_listener* new_sp = nullptr;
119 const char* ifa_name = (ifa_it->ifa_name ? ifa_it->ifa_name : "<unknown>");
120 if (ifa_it->ifa_addr == nullptr) {
121 continue;
122 } else if (ifa_it->ifa_addr->sa_family == AF_INET) {
123 addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in));
124 } else if (ifa_it->ifa_addr->sa_family == AF_INET6) {
125 addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
126 } else {
127 continue;
128 }
129 memcpy(addr.addr, ifa_it->ifa_addr, addr.len);
130 if (!grpc_sockaddr_set_port(&addr, requested_port)) {
131 /* Should never happen, because we check sa_family above. */
132 err = GRPC_ERROR_CREATE("Failed to set port");
133 break;
134 }
135 auto addr_str = grpc_sockaddr_to_string(&addr, false);
136 if (!addr_str.ok()) {
137 return GRPC_ERROR_CREATE(addr_str.status().ToString());
138 }
139 gpr_log(GPR_DEBUG,
140 "Adding local addr from interface %s flags 0x%x to server: %s",
141 ifa_name, ifa_it->ifa_flags, addr_str->c_str());
142 /* We could have multiple interfaces with the same address (e.g., bonding),
143 so look for duplicates. */
144 if (find_listener_with_addr(s, &addr) != nullptr) {
145 gpr_log(GPR_DEBUG, "Skipping duplicate addr %s on interface %s",
146 addr_str->c_str(), ifa_name);
147 continue;
148 }
149 if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode,
150 &new_sp)) != absl::OkStatus()) {
151 grpc_error_handle root_err = GRPC_ERROR_CREATE(
152 absl::StrCat("Failed to add listener: ", addr_str.value()));
153 err = grpc_error_add_child(root_err, err);
154 break;
155 } else {
156 GPR_ASSERT(requested_port == new_sp->port);
157 ++fd_index;
158 if (sp != nullptr) {
159 new_sp->is_sibling = 1;
160 sp->sibling = new_sp;
161 }
162 sp = new_sp;
163 }
164 }
165 freeifaddrs(ifa);
166 if (!err.ok()) {
167 return err;
168 } else if (sp == nullptr) {
169 return GRPC_ERROR_CREATE("No local addresses");
170 } else {
171 *out_port = sp->port;
172 return absl::OkStatus();
173 }
174 }
175
176 bool grpc_tcp_server_have_ifaddrs(void) { return true; }
177
178 #endif /* GRPC_HAVE_IFADDRS */