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_CORE_LIB_IOMGR_SOCKADDR_UTILS_H 20 #define GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include "src/core/lib/iomgr/resolve_address.h" 25 26 /* Returns true if addr is an IPv4-mapped IPv6 address within the 27 ::ffff:0.0.0.0/96 range, or false otherwise. 28 29 If addr4_out is non-NULL, the inner IPv4 address will be copied here when 30 returning true. */ 31 int grpc_sockaddr_is_v4mapped(const grpc_resolved_address* addr, 32 grpc_resolved_address* addr4_out); 33 34 /* If addr is an AF_INET address, writes the corresponding ::ffff:0.0.0.0/96 35 address to addr6_out and returns true. Otherwise returns false. */ 36 int grpc_sockaddr_to_v4mapped(const grpc_resolved_address* addr, 37 grpc_resolved_address* addr6_out); 38 39 /* If addr is ::, 0.0.0.0, or ::ffff:0.0.0.0, writes the port number to 40 *port_out (if not NULL) and returns true, otherwise returns false. */ 41 int grpc_sockaddr_is_wildcard(const grpc_resolved_address* addr, int* port_out); 42 43 /* Writes 0.0.0.0:port and [::]:port to separate sockaddrs. */ 44 void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address* wild4_out, 45 grpc_resolved_address* wild6_out); 46 47 /* Writes 0.0.0.0:port. */ 48 void grpc_sockaddr_make_wildcard4(int port, grpc_resolved_address* wild_out); 49 50 /* Writes [::]:port. */ 51 void grpc_sockaddr_make_wildcard6(int port, grpc_resolved_address* wild_out); 52 53 /* Return the IP port number of a sockaddr */ 54 int grpc_sockaddr_get_port(const grpc_resolved_address* addr); 55 56 /* Set IP port number of a sockaddr */ 57 int grpc_sockaddr_set_port(const grpc_resolved_address* addr, int port); 58 59 /* Converts a sockaddr into a newly-allocated human-readable string. 60 61 Currently, only the AF_INET and AF_INET6 families are recognized. 62 If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are 63 displayed as plain IPv4. 64 65 Usage is similar to gpr_asprintf: returns the number of bytes written 66 (excluding the final '\0'), and *out points to a string which must later be 67 destroyed using gpr_free(). 68 69 In the unlikely event of an error, returns -1 and sets *out to NULL. 70 The existing value of errno is always preserved. */ 71 int grpc_sockaddr_to_string(char** out, const grpc_resolved_address* addr, 72 int normalize); 73 74 void grpc_string_to_sockaddr(grpc_resolved_address* out, char* addr, int port); 75 76 /* Returns the URI string corresponding to \a addr */ 77 char* grpc_sockaddr_to_uri(const grpc_resolved_address* addr); 78 79 /* Returns the URI scheme corresponding to \a addr */ 80 const char* grpc_sockaddr_get_uri_scheme(const grpc_resolved_address* addr); 81 82 int grpc_sockaddr_get_family(const grpc_resolved_address* resolved_addr); 83 84 #endif /* GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H */ 85