blob: b49cc50491cf0fa61a45096b1a8f3d12a7ded308 [file] [log] [blame]
ctiller18b49ab2014-12-09 14:39:16 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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
34#ifndef __GRPC_INTERNAL_IOMGR_SOCKADDR_UTILS_H__
35#define __GRPC_INTERNAL_IOMGR_SOCKADDR_UTILS_H__
36
37#include "src/core/iomgr/sockaddr.h"
38
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. */
44int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
45 struct sockaddr_in *addr4_out);
46
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. */
49int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
50 struct sockaddr_in6 *addr6_out);
51
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. */
54int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out);
55
56/* Writes 0.0.0.0:port and [::]:port to separate sockaddrs. */
57void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
58 struct sockaddr_in6 *wild6_out);
59
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +010060/* Writes 0.0.0.0:port. */
61void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out);
62
63/* Writes [::]:port. */
64void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out);
65
ctiller570d1f42015-01-12 16:29:52 -080066/* Return the IP port number of a sockaddr */
67int grpc_sockaddr_get_port(const struct sockaddr *addr);
68
69/* Set IP port number of a sockaddr */
70int grpc_sockaddr_set_port(const struct sockaddr *addr, int port);
71
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. */
84int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
85 int normalize);
86
87#endif /* __GRPC_INTERNAL_IOMGR_SOCKADDR_UTILS_H__ */