sockaddr_utils.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. *
  3. * Copyright 2015, 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. #include "src/core/iomgr/sockaddr_utils.h"
  34. #include <errno.h>
  35. #include <string.h>
  36. #ifdef GPR_POSIX_SOCKET
  37. #include <sys/un.h>
  38. #endif
  39. #include <grpc/support/alloc.h>
  40. #include <grpc/support/host_port.h>
  41. #include <grpc/support/log.h>
  42. #include <grpc/support/port_platform.h>
  43. #include <grpc/support/string_util.h>
  44. #include "src/core/support/string.h"
  45. static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0xff, 0xff};
  47. int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
  48. struct sockaddr_in *addr4_out) {
  49. GPR_ASSERT(addr != (struct sockaddr *)addr4_out);
  50. if (addr->sa_family == AF_INET6) {
  51. const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
  52. if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix,
  53. sizeof(kV4MappedPrefix)) == 0) {
  54. if (addr4_out != NULL) {
  55. /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
  56. memset(addr4_out, 0, sizeof(*addr4_out));
  57. addr4_out->sin_family = AF_INET;
  58. /* s6_addr32 would be nice, but it's non-standard. */
  59. memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4);
  60. addr4_out->sin_port = addr6->sin6_port;
  61. }
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. }
  67. int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
  68. struct sockaddr_in6 *addr6_out) {
  69. GPR_ASSERT(addr != (struct sockaddr *)addr6_out);
  70. if (addr->sa_family == AF_INET) {
  71. const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
  72. memset(addr6_out, 0, sizeof(*addr6_out));
  73. addr6_out->sin6_family = AF_INET6;
  74. memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
  75. memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
  76. addr6_out->sin6_port = addr4->sin_port;
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) {
  82. struct sockaddr_in addr4_normalized;
  83. if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) {
  84. addr = (struct sockaddr *)&addr4_normalized;
  85. }
  86. if (addr->sa_family == AF_INET) {
  87. /* Check for 0.0.0.0 */
  88. const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
  89. if (addr4->sin_addr.s_addr != 0) {
  90. return 0;
  91. }
  92. *port_out = ntohs(addr4->sin_port);
  93. return 1;
  94. } else if (addr->sa_family == AF_INET6) {
  95. /* Check for :: */
  96. const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
  97. int i;
  98. for (i = 0; i < 16; i++) {
  99. if (addr6->sin6_addr.s6_addr[i] != 0) {
  100. return 0;
  101. }
  102. }
  103. *port_out = ntohs(addr6->sin6_port);
  104. return 1;
  105. } else {
  106. return 0;
  107. }
  108. }
  109. void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
  110. struct sockaddr_in6 *wild6_out) {
  111. grpc_sockaddr_make_wildcard4(port, wild4_out);
  112. grpc_sockaddr_make_wildcard6(port, wild6_out);
  113. }
  114. void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) {
  115. memset(wild_out, 0, sizeof(*wild_out));
  116. wild_out->sin_family = AF_INET;
  117. wild_out->sin_port = htons(port);
  118. }
  119. void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) {
  120. memset(wild_out, 0, sizeof(*wild_out));
  121. wild_out->sin6_family = AF_INET6;
  122. wild_out->sin6_port = htons(port);
  123. }
  124. int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
  125. int normalize) {
  126. const int save_errno = errno;
  127. struct sockaddr_in addr_normalized;
  128. char ntop_buf[INET6_ADDRSTRLEN];
  129. const void *ip = NULL;
  130. int port;
  131. int ret;
  132. *out = NULL;
  133. if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
  134. addr = (const struct sockaddr *)&addr_normalized;
  135. }
  136. if (addr->sa_family == AF_INET) {
  137. const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
  138. ip = &addr4->sin_addr;
  139. port = ntohs(addr4->sin_port);
  140. } else if (addr->sa_family == AF_INET6) {
  141. const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
  142. ip = &addr6->sin6_addr;
  143. port = ntohs(addr6->sin6_port);
  144. }
  145. if (ip != NULL &&
  146. inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) {
  147. ret = gpr_join_host_port(out, ntop_buf, port);
  148. } else {
  149. ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family);
  150. }
  151. /* This is probably redundant, but we wouldn't want to log the wrong error. */
  152. errno = save_errno;
  153. return ret;
  154. }
  155. char *grpc_sockaddr_to_uri(const struct sockaddr *addr) {
  156. char *temp;
  157. char *result;
  158. struct sockaddr_in addr_normalized;
  159. if (grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
  160. addr = (const struct sockaddr *)&addr_normalized;
  161. }
  162. switch (addr->sa_family) {
  163. case AF_INET:
  164. grpc_sockaddr_to_string(&temp, addr, 0);
  165. gpr_asprintf(&result, "ipv4:%s", temp);
  166. gpr_free(temp);
  167. return result;
  168. case AF_INET6:
  169. grpc_sockaddr_to_string(&temp, addr, 0);
  170. gpr_asprintf(&result, "ipv6:%s", temp);
  171. gpr_free(temp);
  172. return result;
  173. #ifdef GPR_POSIX_SOCKET
  174. case AF_UNIX:
  175. gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path);
  176. return result;
  177. #endif
  178. }
  179. return NULL;
  180. }
  181. int grpc_sockaddr_get_port(const struct sockaddr *addr) {
  182. switch (addr->sa_family) {
  183. case AF_INET:
  184. return ntohs(((struct sockaddr_in *)addr)->sin_port);
  185. case AF_INET6:
  186. return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
  187. case AF_UNIX:
  188. return 1;
  189. default:
  190. gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port",
  191. addr->sa_family);
  192. return 0;
  193. }
  194. }
  195. int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
  196. switch (addr->sa_family) {
  197. case AF_INET:
  198. ((struct sockaddr_in *)addr)->sin_port = htons(port);
  199. return 1;
  200. case AF_INET6:
  201. ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
  202. return 1;
  203. default:
  204. gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port",
  205. addr->sa_family);
  206. return 0;
  207. }
  208. }