Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
ahedberg | 8afb88d | 2016-03-22 13:57:47 -0400 | [diff] [blame] | 3 | * Copyright 2016, 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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/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 | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.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 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 45 | #include "src/core/lib/iomgr/sockaddr.h" |
murgatroid99 | 3196363 | 2016-08-09 14:00:41 -0700 | [diff] [blame] | 46 | #include "src/core/lib/iomgr/socket_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 47 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
| 48 | #include "src/core/lib/support/string.h" |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 49 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 50 | static const uint8_t kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, |
| 51 | 0, 0, 0, 0, 0xff, 0xff}; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 52 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 53 | int grpc_sockaddr_is_v4mapped(const grpc_resolved_address *resolved_addr, |
| 54 | grpc_resolved_address *resolved_addr4_out) { |
| 55 | GPR_ASSERT(resolved_addr != resolved_addr4_out); |
| 56 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 57 | struct sockaddr_in *addr4_out = |
| 58 | (struct sockaddr_in *)resolved_addr4_out->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 59 | if (addr->sa_family == AF_INET6) { |
| 60 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 61 | if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix, |
| 62 | sizeof(kV4MappedPrefix)) == 0) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 63 | if (resolved_addr4_out != NULL) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 64 | /* Normalize ::ffff:0.0.0.0/96 to IPv4. */ |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 65 | memset(resolved_addr4_out, 0, sizeof(*resolved_addr4_out)); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 66 | addr4_out->sin_family = AF_INET; |
| 67 | /* s6_addr32 would be nice, but it's non-standard. */ |
| 68 | memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4); |
| 69 | addr4_out->sin_port = addr6->sin6_port; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 70 | resolved_addr4_out->len = sizeof(struct sockaddr_in); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 71 | } |
| 72 | return 1; |
| 73 | } |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 78 | int grpc_sockaddr_to_v4mapped(const grpc_resolved_address *resolved_addr, |
| 79 | grpc_resolved_address *resolved_addr6_out) { |
| 80 | GPR_ASSERT(resolved_addr != resolved_addr6_out); |
| 81 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 82 | struct sockaddr_in6 *addr6_out = |
| 83 | (struct sockaddr_in6 *)resolved_addr6_out->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 84 | if (addr->sa_family == AF_INET) { |
| 85 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 86 | memset(resolved_addr6_out, 0, sizeof(*resolved_addr6_out)); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 87 | addr6_out->sin6_family = AF_INET6; |
| 88 | memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12); |
| 89 | memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4); |
| 90 | addr6_out->sin6_port = addr4->sin_port; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 91 | resolved_addr6_out->len = sizeof(struct sockaddr_in6); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 97 | int grpc_sockaddr_is_wildcard(const grpc_resolved_address *resolved_addr, |
| 98 | int *port_out) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 99 | const struct sockaddr *addr; |
| 100 | grpc_resolved_address addr4_normalized; |
| 101 | if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr4_normalized)) { |
| 102 | resolved_addr = &addr4_normalized; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 103 | } |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 104 | addr = (const struct sockaddr *)resolved_addr->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 105 | if (addr->sa_family == AF_INET) { |
| 106 | /* Check for 0.0.0.0 */ |
| 107 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 108 | if (addr4->sin_addr.s_addr != 0) { |
| 109 | return 0; |
| 110 | } |
| 111 | *port_out = ntohs(addr4->sin_port); |
| 112 | return 1; |
| 113 | } else if (addr->sa_family == AF_INET6) { |
| 114 | /* Check for :: */ |
| 115 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 116 | int i; |
| 117 | for (i = 0; i < 16; i++) { |
| 118 | if (addr6->sin6_addr.s6_addr[i] != 0) { |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | *port_out = ntohs(addr6->sin6_port); |
| 123 | return 1; |
| 124 | } else { |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 129 | void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address *wild4_out, |
| 130 | grpc_resolved_address *wild6_out) { |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 131 | grpc_sockaddr_make_wildcard4(port, wild4_out); |
| 132 | grpc_sockaddr_make_wildcard6(port, wild6_out); |
| 133 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 134 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 135 | void grpc_sockaddr_make_wildcard4(int port, |
| 136 | grpc_resolved_address *resolved_wild_out) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 137 | struct sockaddr_in *wild_out = (struct sockaddr_in *)resolved_wild_out->addr; |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 138 | GPR_ASSERT(port >= 0 && port < 65536); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 139 | memset(resolved_wild_out, 0, sizeof(*resolved_wild_out)); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 140 | wild_out->sin_family = AF_INET; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 141 | wild_out->sin_port = htons((uint16_t)port); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 142 | resolved_wild_out->len = sizeof(struct sockaddr_in); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 143 | } |
| 144 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 145 | void grpc_sockaddr_make_wildcard6(int port, |
| 146 | grpc_resolved_address *resolved_wild_out) { |
| 147 | struct sockaddr_in6 *wild_out = |
| 148 | (struct sockaddr_in6 *)resolved_wild_out->addr; |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 149 | GPR_ASSERT(port >= 0 && port < 65536); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 150 | memset(resolved_wild_out, 0, sizeof(*resolved_wild_out)); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 151 | wild_out->sin6_family = AF_INET6; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 152 | wild_out->sin6_port = htons((uint16_t)port); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 153 | resolved_wild_out->len = sizeof(struct sockaddr_in6); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 154 | } |
| 155 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 156 | int grpc_sockaddr_to_string(char **out, |
| 157 | const grpc_resolved_address *resolved_addr, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 158 | int normalize) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 159 | const struct sockaddr *addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 160 | const int save_errno = errno; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 161 | grpc_resolved_address addr_normalized; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 162 | char ntop_buf[INET6_ADDRSTRLEN]; |
| 163 | const void *ip = NULL; |
| 164 | int port; |
Yuchen Zeng | aa76d3d | 2017-02-15 14:00:01 -0800 | [diff] [blame^] | 165 | uint32_t sin6_scope_id = 0; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 166 | int ret; |
| 167 | |
| 168 | *out = NULL; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 169 | if (normalize && grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) { |
| 170 | resolved_addr = &addr_normalized; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | } |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 172 | addr = (const struct sockaddr *)resolved_addr->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 173 | if (addr->sa_family == AF_INET) { |
| 174 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 175 | ip = &addr4->sin_addr; |
| 176 | port = ntohs(addr4->sin_port); |
| 177 | } else if (addr->sa_family == AF_INET6) { |
| 178 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 179 | ip = &addr6->sin6_addr; |
| 180 | port = ntohs(addr6->sin6_port); |
Yuchen Zeng | aa76d3d | 2017-02-15 14:00:01 -0800 | [diff] [blame^] | 181 | sin6_scope_id = addr6->sin6_scope_id; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 182 | } |
| 183 | if (ip != NULL && |
murgatroid99 | 7e924a0 | 2016-08-10 10:06:43 -0700 | [diff] [blame] | 184 | grpc_inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) { |
Yuchen Zeng | aa76d3d | 2017-02-15 14:00:01 -0800 | [diff] [blame^] | 185 | if (sin6_scope_id != 0) { |
| 186 | char *host_with_scope; |
| 187 | /* Enclose sin6_scope_id with the format defined in RFC 6784 section 2. */ |
| 188 | gpr_asprintf(&host_with_scope, "%s%%25%" PRIu32, ntop_buf, sin6_scope_id); |
| 189 | ret = gpr_join_host_port(out, host_with_scope, port); |
| 190 | gpr_free(host_with_scope); |
| 191 | } else { |
| 192 | ret = gpr_join_host_port(out, ntop_buf, port); |
| 193 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 194 | } else { |
| 195 | ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family); |
| 196 | } |
| 197 | /* This is probably redundant, but we wouldn't want to log the wrong error. */ |
| 198 | errno = save_errno; |
| 199 | return ret; |
| 200 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 201 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 202 | char *grpc_sockaddr_to_uri(const grpc_resolved_address *resolved_addr) { |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 203 | char *temp; |
| 204 | char *result; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 205 | grpc_resolved_address addr_normalized; |
| 206 | const struct sockaddr *addr; |
Paul Marks | 63541a1 | 2015-08-04 15:05:00 -0700 | [diff] [blame] | 207 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 208 | if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) { |
| 209 | resolved_addr = &addr_normalized; |
Paul Marks | 63541a1 | 2015-08-04 15:05:00 -0700 | [diff] [blame] | 210 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 211 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 212 | addr = (const struct sockaddr *)resolved_addr->addr; |
| 213 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 214 | switch (addr->sa_family) { |
| 215 | case AF_INET: |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 216 | grpc_sockaddr_to_string(&temp, resolved_addr, 0); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 217 | gpr_asprintf(&result, "ipv4:%s", temp); |
| 218 | gpr_free(temp); |
| 219 | return result; |
| 220 | case AF_INET6: |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 221 | grpc_sockaddr_to_string(&temp, resolved_addr, 0); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 222 | gpr_asprintf(&result, "ipv6:%s", temp); |
| 223 | gpr_free(temp); |
| 224 | return result; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 225 | default: |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 226 | return grpc_sockaddr_to_uri_unix_if_possible(resolved_addr); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 227 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 228 | } |
| 229 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 230 | int grpc_sockaddr_get_port(const grpc_resolved_address *resolved_addr) { |
| 231 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 232 | switch (addr->sa_family) { |
| 233 | case AF_INET: |
| 234 | return ntohs(((struct sockaddr_in *)addr)->sin_port); |
| 235 | case AF_INET6: |
| 236 | return ntohs(((struct sockaddr_in6 *)addr)->sin6_port); |
| 237 | default: |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 238 | if (grpc_is_unix_socket(resolved_addr)) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 239 | return 1; |
| 240 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 241 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", |
| 242 | addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | } |
| 246 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 247 | int grpc_sockaddr_set_port(const grpc_resolved_address *resolved_addr, |
| 248 | int port) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 249 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 250 | switch (addr->sa_family) { |
| 251 | case AF_INET: |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 252 | GPR_ASSERT(port >= 0 && port < 65536); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 253 | ((struct sockaddr_in *)addr)->sin_port = htons((uint16_t)port); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 254 | return 1; |
| 255 | case AF_INET6: |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 256 | GPR_ASSERT(port >= 0 && port < 65536); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 257 | ((struct sockaddr_in6 *)addr)->sin6_port = htons((uint16_t)port); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 258 | return 1; |
| 259 | default: |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 260 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", |
| 261 | addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 262 | return 0; |
| 263 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 264 | } |