• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/alloc.h>
27 #include <ifaddrs.h>
28 #include <stddef.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 
32 #include <string>
33 
34 #include "absl/log/check.h"
35 #include "absl/log/log.h"
36 #include "absl/strings/str_cat.h"
37 #include "src/core/lib/address_utils/sockaddr_utils.h"
38 #include "src/core/lib/iomgr/error.h"
39 #include "src/core/lib/iomgr/sockaddr.h"
40 #include "src/core/lib/iomgr/tcp_server_utils_posix.h"
41 #include "src/core/util/crash.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_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 
grpc_is_ipv4_available()91 static bool grpc_is_ipv4_available() {
92   const int fd = socket(AF_INET, SOCK_DGRAM, 0);
93   if (fd >= 0) close(fd);
94   return fd >= 0;
95 }
96 
grpc_tcp_server_add_all_local_addrs(grpc_tcp_server * s,unsigned port_index,int requested_port,int * out_port)97 grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s,
98                                                       unsigned port_index,
99                                                       int requested_port,
100                                                       int* out_port) {
101   struct ifaddrs* ifa = nullptr;
102   struct ifaddrs* ifa_it;
103   unsigned fd_index = 0;
104   grpc_tcp_listener* sp = nullptr;
105   grpc_error_handle err;
106   if (requested_port == 0) {
107     // Note: There could be a race where some local addrs can listen on the
108     // selected port and some can't. The sane way to handle this would be to
109     // retry by recreating the whole grpc_tcp_server. Backing out individual
110     // listeners and orphaning the FDs looks like too much trouble.
111     if ((err = get_unused_port(&requested_port)) != absl::OkStatus()) {
112       return err;
113     } else if (requested_port <= 0) {
114       return GRPC_ERROR_CREATE("Bad get_unused_port()");
115     }
116     VLOG(2) << "Picked unused port " << requested_port;
117   }
118 
119   static bool v4_available = grpc_is_ipv4_available();
120 
121   if (getifaddrs(&ifa) != 0 || ifa == nullptr) {
122     return GRPC_OS_ERROR(errno, "getifaddrs");
123   }
124   for (ifa_it = ifa; ifa_it != nullptr; ifa_it = ifa_it->ifa_next) {
125     grpc_resolved_address addr;
126     grpc_dualstack_mode dsmode;
127     grpc_tcp_listener* new_sp = nullptr;
128     const char* ifa_name = (ifa_it->ifa_name ? ifa_it->ifa_name : "<unknown>");
129     if (ifa_it->ifa_addr == nullptr) {
130       continue;
131     } else if (ifa_it->ifa_addr->sa_family == AF_INET) {
132       if (!v4_available) {
133         continue;
134       }
135       addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in));
136     } else if (ifa_it->ifa_addr->sa_family == AF_INET6) {
137       addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
138     } else {
139       continue;
140     }
141     memcpy(addr.addr, ifa_it->ifa_addr, addr.len);
142     if (!grpc_sockaddr_set_port(&addr, requested_port)) {
143       // Should never happen, because we check sa_family above.
144       err = GRPC_ERROR_CREATE("Failed to set port");
145       break;
146     }
147     auto addr_str = grpc_sockaddr_to_string(&addr, false);
148     if (!addr_str.ok()) {
149       return GRPC_ERROR_CREATE(addr_str.status().ToString());
150     }
151     VLOG(2) << absl::StrFormat(
152         "Adding local addr from interface %s flags 0x%x to server: %s",
153         ifa_name, ifa_it->ifa_flags, addr_str->c_str());
154     // We could have multiple interfaces with the same address (e.g., bonding),
155     // so look for duplicates.
156     if (find_listener_with_addr(s, &addr) != nullptr) {
157       VLOG(2) << "Skipping duplicate addr " << *addr_str << " on interface "
158               << ifa_name;
159       continue;
160     }
161     if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode,
162                                         &new_sp)) != absl::OkStatus()) {
163       grpc_error_handle root_err = GRPC_ERROR_CREATE(
164           absl::StrCat("Failed to add listener: ", addr_str.value()));
165       err = grpc_error_add_child(root_err, err);
166       break;
167     } else {
168       CHECK(requested_port == new_sp->port);
169       ++fd_index;
170       if (sp != nullptr) {
171         new_sp->is_sibling = 1;
172         sp->sibling = new_sp;
173       }
174       sp = new_sp;
175     }
176   }
177   freeifaddrs(ifa);
178   if (!err.ok()) {
179     return err;
180   } else if (sp == nullptr) {
181     return GRPC_ERROR_CREATE("No local addresses");
182   } else {
183     *out_port = sp->port;
184     return absl::OkStatus();
185   }
186 }
187 
grpc_tcp_server_have_ifaddrs(void)188 bool grpc_tcp_server_have_ifaddrs(void) { return true; }
189 
190 #endif  // GRPC_HAVE_IFADDRS
191