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 "src/core/lib/iomgr/tcp_server_utils_posix.h"
26
27 #include <errno.h>
28 #include <ifaddrs.h>
29 #include <stddef.h>
30 #include <string.h>
31
32 #include <string>
33
34 #include "absl/strings/str_cat.h"
35
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
38
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/sockaddr.h"
41 #include "src/core/lib/iomgr/sockaddr_utils.h"
42
43 /* Return the listener in s with address addr or NULL. */
find_listener_with_addr(grpc_tcp_server * s,grpc_resolved_address * addr)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. */
get_unused_port(int * port)61 static grpc_error* 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* err =
67 grpc_create_dualstack_socket(&wild, SOCK_STREAM, 0, &dsmode, &fd);
68 if (err != GRPC_ERROR_NONE) {
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_FROM_STATIC_STRING("Bad port")
89 : GRPC_ERROR_NONE;
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* 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* 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) {
107 return err;
108 } else if (requested_port <= 0) {
109 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("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_FROM_STATIC_STRING("Failed to set port");
134 break;
135 }
136 std::string addr_str = grpc_sockaddr_to_string(&addr, false);
137 gpr_log(GPR_DEBUG,
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);
145 continue;
146 }
147 if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode,
148 &new_sp)) != GRPC_ERROR_NONE) {
149 grpc_error* 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);
152 break;
153 } else {
154 GPR_ASSERT(requested_port == new_sp->port);
155 ++fd_index;
156 if (sp != nullptr) {
157 new_sp->is_sibling = 1;
158 sp->sibling = new_sp;
159 }
160 sp = new_sp;
161 }
162 }
163 freeifaddrs(ifa);
164 if (err != GRPC_ERROR_NONE) {
165 return err;
166 } else if (sp == nullptr) {
167 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("No local addresses");
168 } else {
169 *out_port = sp->port;
170 return GRPC_ERROR_NONE;
171 }
172 }
173
grpc_tcp_server_have_ifaddrs(void)174 bool grpc_tcp_server_have_ifaddrs(void) { return true; }
175
176 #endif /* GRPC_HAVE_IFADDRS */
177