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