Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 34 | #include "src/core/iomgr/sockaddr_utils.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | #include <errno.h> |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 37 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 39 | #include "src/core/support/string.h" |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 40 | #include <grpc/support/host_port.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/port_platform.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame^] | 43 | #include <grpc/support/string_util.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 44 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 45 | static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, |
| 46 | 0, 0, 0, 0, 0xff, 0xff}; |
| 47 | |
| 48 | int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr, |
| 49 | struct sockaddr_in *addr4_out) { |
| 50 | GPR_ASSERT(addr != (struct sockaddr *)addr4_out); |
| 51 | if (addr->sa_family == AF_INET6) { |
| 52 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 53 | if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix, |
| 54 | sizeof(kV4MappedPrefix)) == 0) { |
| 55 | if (addr4_out != NULL) { |
| 56 | /* Normalize ::ffff:0.0.0.0/96 to IPv4. */ |
| 57 | memset(addr4_out, 0, sizeof(*addr4_out)); |
| 58 | addr4_out->sin_family = AF_INET; |
| 59 | /* s6_addr32 would be nice, but it's non-standard. */ |
| 60 | memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4); |
| 61 | addr4_out->sin_port = addr6->sin6_port; |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr, |
| 70 | struct sockaddr_in6 *addr6_out) { |
| 71 | GPR_ASSERT(addr != (struct sockaddr *)addr6_out); |
| 72 | if (addr->sa_family == AF_INET) { |
| 73 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 74 | memset(addr6_out, 0, sizeof(*addr6_out)); |
| 75 | addr6_out->sin6_family = AF_INET6; |
| 76 | memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12); |
| 77 | memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4); |
| 78 | addr6_out->sin6_port = addr4->sin_port; |
| 79 | return 1; |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) { |
| 85 | struct sockaddr_in addr4_normalized; |
| 86 | if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) { |
| 87 | addr = (struct sockaddr *)&addr4_normalized; |
| 88 | } |
| 89 | if (addr->sa_family == AF_INET) { |
| 90 | /* Check for 0.0.0.0 */ |
| 91 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 92 | if (addr4->sin_addr.s_addr != 0) { |
| 93 | return 0; |
| 94 | } |
| 95 | *port_out = ntohs(addr4->sin_port); |
| 96 | return 1; |
| 97 | } else if (addr->sa_family == AF_INET6) { |
| 98 | /* Check for :: */ |
| 99 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 100 | int i; |
| 101 | for (i = 0; i < 16; i++) { |
| 102 | if (addr6->sin6_addr.s6_addr[i] != 0) { |
| 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | *port_out = ntohs(addr6->sin6_port); |
| 107 | return 1; |
| 108 | } else { |
| 109 | return 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out, |
| 114 | struct sockaddr_in6 *wild6_out) { |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 115 | grpc_sockaddr_make_wildcard4(port, wild4_out); |
| 116 | grpc_sockaddr_make_wildcard6(port, wild6_out); |
| 117 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 118 | |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 119 | void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) { |
| 120 | memset(wild_out, 0, sizeof(*wild_out)); |
| 121 | wild_out->sin_family = AF_INET; |
| 122 | wild_out->sin_port = htons(port); |
| 123 | } |
| 124 | |
| 125 | void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) { |
| 126 | memset(wild_out, 0, sizeof(*wild_out)); |
| 127 | wild_out->sin6_family = AF_INET6; |
| 128 | wild_out->sin6_port = htons(port); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, |
| 132 | int normalize) { |
| 133 | const int save_errno = errno; |
| 134 | struct sockaddr_in addr_normalized; |
| 135 | char ntop_buf[INET6_ADDRSTRLEN]; |
| 136 | const void *ip = NULL; |
| 137 | int port; |
| 138 | int ret; |
| 139 | |
| 140 | *out = NULL; |
| 141 | if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) { |
| 142 | addr = (const struct sockaddr *)&addr_normalized; |
| 143 | } |
| 144 | if (addr->sa_family == AF_INET) { |
| 145 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 146 | ip = &addr4->sin_addr; |
| 147 | port = ntohs(addr4->sin_port); |
| 148 | } else if (addr->sa_family == AF_INET6) { |
| 149 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 150 | ip = &addr6->sin6_addr; |
| 151 | port = ntohs(addr6->sin6_port); |
| 152 | } |
| 153 | if (ip != NULL && |
| 154 | inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) { |
| 155 | ret = gpr_join_host_port(out, ntop_buf, port); |
| 156 | } else { |
| 157 | ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family); |
| 158 | } |
| 159 | /* This is probably redundant, but we wouldn't want to log the wrong error. */ |
| 160 | errno = save_errno; |
| 161 | return ret; |
| 162 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 163 | |
| 164 | int grpc_sockaddr_get_port(const struct sockaddr *addr) { |
| 165 | switch (addr->sa_family) { |
| 166 | case AF_INET: |
| 167 | return ntohs(((struct sockaddr_in *)addr)->sin_port); |
| 168 | case AF_INET6: |
| 169 | return ntohs(((struct sockaddr_in6 *)addr)->sin6_port); |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 170 | case AF_UNIX: |
| 171 | return 1; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 172 | default: |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 173 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) { |
| 179 | switch (addr->sa_family) { |
| 180 | case AF_INET: |
| 181 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 182 | return 1; |
| 183 | case AF_INET6: |
| 184 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
| 185 | return 1; |
| 186 | default: |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 187 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 188 | return 0; |
| 189 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 190 | } |