• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 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 #ifndef GRPC_SRC_CORE_LIB_ADDRESS_UTILS_SOCKADDR_UTILS_H
20 #define GRPC_SRC_CORE_LIB_ADDRESS_UTILS_SOCKADDR_UTILS_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <stdint.h>
24 
25 #include <string>
26 
27 #include "absl/status/statusor.h"
28 #include "src/core/lib/iomgr/resolved_address.h"
29 
30 // Returns true if addr is an IPv4-mapped IPv6 address within the
31 // ::ffff:0.0.0.0/96 range, or false otherwise.
32 
33 // If addr4_out is non-NULL, the inner IPv4 address will be copied here when
34 // returning true.
35 int grpc_sockaddr_is_v4mapped(const grpc_resolved_address* addr,
36                               grpc_resolved_address* addr4_out);
37 
38 // If addr is an AF_INET address, writes the corresponding ::ffff:0.0.0.0/96
39 // address to addr6_out and returns true.  Otherwise returns false.
40 int grpc_sockaddr_to_v4mapped(const grpc_resolved_address* addr,
41                               grpc_resolved_address* addr6_out);
42 
43 // If addr is ::, 0.0.0.0, or ::ffff:0.0.0.0, writes the port number to
44 // port_out (if not NULL) and returns true, otherwise returns false.
45 int grpc_sockaddr_is_wildcard(const grpc_resolved_address* addr, int* port_out);
46 
47 // Writes 0.0.0.0:port and [::]:port to separate sockaddrs.
48 void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address* wild4_out,
49                                   grpc_resolved_address* wild6_out);
50 
51 // Writes 0.0.0.0:port.
52 void grpc_sockaddr_make_wildcard4(int port, grpc_resolved_address* wild_out);
53 
54 // Writes [::]:port.
55 void grpc_sockaddr_make_wildcard6(int port, grpc_resolved_address* wild_out);
56 
57 // Return the IP port number of a sockaddr
58 int grpc_sockaddr_get_port(const grpc_resolved_address* addr);
59 
60 // Set IP port number of a sockaddr
61 int grpc_sockaddr_set_port(grpc_resolved_address* addr, int port);
62 
63 // Converts a sockaddr into a newly-allocated human-readable string.
64 //
65 // Currently, only the AF_INET, AF_INET6, and AF_UNIX families are recognized.
66 // If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are
67 // displayed as plain IPv4.
68 GRPC_MUST_USE_RESULT absl::StatusOr<std::string> grpc_sockaddr_to_string(
69     const grpc_resolved_address* addr, bool normalize);
70 
71 // Returns the URI string corresponding to \a addr
72 absl::StatusOr<std::string> grpc_sockaddr_to_uri(
73     const grpc_resolved_address* addr);
74 
75 // Returns the URI scheme corresponding to \a addr
76 const char* grpc_sockaddr_get_uri_scheme(const grpc_resolved_address* addr);
77 
78 int grpc_sockaddr_get_family(const grpc_resolved_address* resolved_addr);
79 
80 std::string grpc_sockaddr_get_packed_host(
81     const grpc_resolved_address* resolved_addr);
82 
83 // Applies a mask of \a mask_bits to IPv4/IPv6 addresses. Has no effect if the
84 // address type is not IPv4/IPv6.
85 void grpc_sockaddr_mask_bits(grpc_resolved_address* address,
86                              uint32_t mask_bits);
87 
88 // If \a address is IPv4/IPv6, checks if the IP address falls in the CIDR
89 // specified by \a subnet_address and \a mask_bits.
90 // Returns false if \a address is not an IPv4/IPv6 address. The ports (if set)
91 // are ignored for matching purposes. Note that, \a subnet_address should be
92 // normalized, i.e., `grpc_sockaddr_mask_bits` should have been called on it if
93 // necessary.
94 bool grpc_sockaddr_match_subnet(const grpc_resolved_address* address,
95                                 const grpc_resolved_address* subnet_address,
96                                 uint32_t mask_bits);
97 
98 #endif  // GRPC_SRC_CORE_LIB_ADDRESS_UTILS_SOCKADDR_UTILS_H
99