blob: 2b22f11b49dcf9c9fd6e063063307835ff1314d0 [file] [log] [blame]
ctiller18b49ab2014-12-09 14:39:16 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
ctiller18b49ab2014-12-09 14:39:16 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H
35#define GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H
ctiller18b49ab2014-12-09 14:39:16 -080036
murgatroid997871f732016-09-23 13:49:05 -070037#include "src/core/lib/iomgr/resolve_address.h"
ctiller18b49ab2014-12-09 14:39:16 -080038
39/* Returns true if addr is an IPv4-mapped IPv6 address within the
40 ::ffff:0.0.0.0/96 range, or false otherwise.
41
42 If addr4_out is non-NULL, the inner IPv4 address will be copied here when
43 returning true. */
murgatroid997871f732016-09-23 13:49:05 -070044int grpc_sockaddr_is_v4mapped(const grpc_resolved_address *addr,
45 grpc_resolved_address *addr4_out);
ctiller18b49ab2014-12-09 14:39:16 -080046
47/* If addr is an AF_INET address, writes the corresponding ::ffff:0.0.0.0/96
48 address to addr6_out and returns true. Otherwise returns false. */
murgatroid997871f732016-09-23 13:49:05 -070049int grpc_sockaddr_to_v4mapped(const grpc_resolved_address *addr,
50 grpc_resolved_address *addr6_out);
ctiller18b49ab2014-12-09 14:39:16 -080051
52/* If addr is ::, 0.0.0.0, or ::ffff:0.0.0.0, writes the port number to
53 *port_out (if not NULL) and returns true, otherwise returns false. */
murgatroid997871f732016-09-23 13:49:05 -070054int grpc_sockaddr_is_wildcard(const grpc_resolved_address *addr, int *port_out);
ctiller18b49ab2014-12-09 14:39:16 -080055
56/* Writes 0.0.0.0:port and [::]:port to separate sockaddrs. */
murgatroid997871f732016-09-23 13:49:05 -070057void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address *wild4_out,
58 grpc_resolved_address *wild6_out);
ctiller18b49ab2014-12-09 14:39:16 -080059
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +010060/* Writes 0.0.0.0:port. */
murgatroid997871f732016-09-23 13:49:05 -070061void grpc_sockaddr_make_wildcard4(int port, grpc_resolved_address *wild_out);
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +010062
63/* Writes [::]:port. */
murgatroid997871f732016-09-23 13:49:05 -070064void grpc_sockaddr_make_wildcard6(int port, grpc_resolved_address *wild_out);
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +010065
ctiller570d1f42015-01-12 16:29:52 -080066/* Return the IP port number of a sockaddr */
murgatroid997871f732016-09-23 13:49:05 -070067int grpc_sockaddr_get_port(const grpc_resolved_address *addr);
ctiller570d1f42015-01-12 16:29:52 -080068
69/* Set IP port number of a sockaddr */
murgatroid997871f732016-09-23 13:49:05 -070070int grpc_sockaddr_set_port(const grpc_resolved_address *addr, int port);
ctiller570d1f42015-01-12 16:29:52 -080071
ctiller18b49ab2014-12-09 14:39:16 -080072/* Converts a sockaddr into a newly-allocated human-readable string.
73
74 Currently, only the AF_INET and AF_INET6 families are recognized.
75 If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are
76 displayed as plain IPv4.
77
78 Usage is similar to gpr_asprintf: returns the number of bytes written
79 (excluding the final '\0'), and *out points to a string which must later be
80 destroyed using gpr_free().
81
82 In the unlikely event of an error, returns -1 and sets *out to NULL.
83 The existing value of errno is always preserved. */
murgatroid997871f732016-09-23 13:49:05 -070084int grpc_sockaddr_to_string(char **out, const grpc_resolved_address *addr,
ctiller18b49ab2014-12-09 14:39:16 -080085 int normalize);
86
David Garcia Quintas01291502017-02-07 13:26:41 -080087/* Returns the URI string corresponding to \a addr */
murgatroid997871f732016-09-23 13:49:05 -070088char *grpc_sockaddr_to_uri(const grpc_resolved_address *addr);
Craig Tiller1b22b9d2015-07-20 13:42:22 -070089
David Garcia Quintas01291502017-02-07 13:26:41 -080090/* Returns the URI scheme corresponding to \a addr */
91const char *grpc_sockaddr_get_uri_scheme(const grpc_resolved_address *addr);
92
Craig Tiller9a4dddd2016-03-25 17:08:13 -070093#endif /* GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H */