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/gprpp/crash.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/iomgr/sockaddr.h"
42 #include "src/core/lib/iomgr/tcp_server_utils_posix.h"
43
44 // Return the listener in s with address addr or NULL.
find_listener_with_addr(grpc_tcp_server * s,grpc_resolved_address * addr)45 static grpc_tcp_listener* find_listener_with_addr(grpc_tcp_server* s,
46 grpc_resolved_address* addr) {
47 grpc_tcp_listener* l;
48 gpr_mu_lock(&s->mu);
49 for (l = s->head; l != nullptr; l = l->next) {
50 if (l->addr.len != addr->len) {
51 continue;
52 }
53 if (memcmp(l->addr.addr, addr->addr, addr->len) == 0) {
54 break;
55 }
56 }
57 gpr_mu_unlock(&s->mu);
58 return l;
59 }
60
61 // Bind to "::" to get a port number not used by any address.
get_unused_port(int * port)62 static grpc_error_handle get_unused_port(int* port) {
63 grpc_resolved_address wild;
64 grpc_sockaddr_make_wildcard6(0, &wild);
65 grpc_dualstack_mode dsmode;
66 int fd;
67 grpc_error_handle err =
68 grpc_create_dualstack_socket(&wild, SOCK_STREAM, 0, &dsmode, &fd);
69 if (!err.ok()) {
70 return err;
71 }
72 if (dsmode == GRPC_DSMODE_IPV4) {
73 grpc_sockaddr_make_wildcard4(0, &wild);
74 }
75 if (bind(fd, reinterpret_cast<const grpc_sockaddr*>(wild.addr), wild.len) !=
76 0) {
77 err = GRPC_OS_ERROR(errno, "bind");
78 close(fd);
79 return err;
80 }
81 if (getsockname(fd, reinterpret_cast<grpc_sockaddr*>(wild.addr), &wild.len) !=
82 0) {
83 err = GRPC_OS_ERROR(errno, "getsockname");
84 close(fd);
85 return err;
86 }
87 close(fd);
88 *port = grpc_sockaddr_get_port(&wild);
89 return *port <= 0 ? GRPC_ERROR_CREATE("Bad port") : absl::OkStatus();
90 }
91
grpc_tcp_server_add_all_local_addrs(grpc_tcp_server * s,unsigned port_index,int requested_port,int * out_port)92 grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s,
93 unsigned port_index,
94 int requested_port,
95 int* out_port) {
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;
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)) != absl::OkStatus()) {
107 return err;
108 } else if (requested_port <= 0) {
109 return GRPC_ERROR_CREATE("Bad get_unused_port()");
110 }
111 gpr_log(GPR_DEBUG, "Picked unused port %d", requested_port);
112 }
113 if (getifaddrs(&ifa) != 0 || ifa == nullptr) {
114 return GRPC_OS_ERROR(errno, "getifaddrs");
115 }
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) {
122 continue;
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));
127 } else {
128 continue;
129 }
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("Failed to set port");
134 break;
135 }
136 auto addr_str = grpc_sockaddr_to_string(&addr, false);
137 if (!addr_str.ok()) {
138 return GRPC_ERROR_CREATE(addr_str.status().ToString());
139 }
140 gpr_log(GPR_DEBUG,
141 "Adding local addr from interface %s flags 0x%x to server: %s",
142 ifa_name, ifa_it->ifa_flags, addr_str->c_str());
143 // We could have multiple interfaces with the same address (e.g., bonding),
144 // so look for duplicates.
145 if (find_listener_with_addr(s, &addr) != nullptr) {
146 gpr_log(GPR_DEBUG, "Skipping duplicate addr %s on interface %s",
147 addr_str->c_str(), ifa_name);
148 continue;
149 }
150 if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode,
151 &new_sp)) != absl::OkStatus()) {
152 grpc_error_handle root_err = GRPC_ERROR_CREATE(
153 absl::StrCat("Failed to add listener: ", addr_str.value()));
154 err = grpc_error_add_child(root_err, err);
155 break;
156 } else {
157 GPR_ASSERT(requested_port == new_sp->port);
158 ++fd_index;
159 if (sp != nullptr) {
160 new_sp->is_sibling = 1;
161 sp->sibling = new_sp;
162 }
163 sp = new_sp;
164 }
165 }
166 freeifaddrs(ifa);
167 if (!err.ok()) {
168 return err;
169 } else if (sp == nullptr) {
170 return GRPC_ERROR_CREATE("No local addresses");
171 } else {
172 *out_port = sp->port;
173 return absl::OkStatus();
174 }
175 }
176
grpc_tcp_server_have_ifaddrs(void)177 bool grpc_tcp_server_have_ifaddrs(void) { return true; }
178
179 #endif // GRPC_HAVE_IFADDRS
180