ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 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 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 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. */ |
| 44 | int 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. */ |
| 49 | int 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. */ |
| 54 | int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out); |
| 55 | |
| 56 | /* Writes 0.0.0.0:port and [::]:port to separate sockaddrs. */ |
| 57 | void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out, |
| 58 | struct sockaddr_in6 *wild6_out); |
| 59 | |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 60 | /* Writes 0.0.0.0:port. */ |
| 61 | void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out); |
| 62 | |
| 63 | /* Writes [::]:port. */ |
| 64 | void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out); |
| 65 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 66 | /* Return the IP port number of a sockaddr */ |
| 67 | int grpc_sockaddr_get_port(const struct sockaddr *addr); |
| 68 | |
| 69 | /* Set IP port number of a sockaddr */ |
| 70 | int grpc_sockaddr_set_port(const struct sockaddr *addr, int port); |
| 71 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 72 | /* 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. */ |
| 84 | int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, |
| 85 | int normalize); |
| 86 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 87 | char *grpc_sockaddr_to_uri(const struct sockaddr *addr); |
| 88 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 89 | #endif /* GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H */ |