socket_helpers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* socket_helpers.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SOCKET_HELPERS_H
  31. #define SOCKET_HELPERS_H
  32. #include <string.h>
  33. #if defined(__MINGW32__) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4)
  34. // Workaround for mingw-w64 < 4.0
  35. #ifndef IPV6_V6ONLY
  36. #define IPV6_V6ONLY 27
  37. #endif
  38. #endif
  39. // helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this
  40. static size_t _set_sockaddr(struct sockaddr_storage *p_addr, const IP_Address &p_ip, int p_port, IP::Type p_sock_type = IP::TYPE_ANY) {
  41. memset(p_addr, 0, sizeof(struct sockaddr_storage));
  42. ERR_FAIL_COND_V(!p_ip.is_valid(), 0);
  43. // IPv6 socket
  44. if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
  45. // IPv6 only socket with IPv4 address
  46. ERR_FAIL_COND_V(p_sock_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
  47. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  48. addr6->sin6_family = AF_INET6;
  49. addr6->sin6_port = htons(p_port);
  50. copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
  51. return sizeof(sockaddr_in6);
  52. } else { // IPv4 socket
  53. // IPv4 socket with IPv6 address
  54. ERR_FAIL_COND_V(!p_ip.is_ipv4(), 0);
  55. uint32_t ipv4 = *((uint32_t *)p_ip.get_ipv4());
  56. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  57. addr4->sin_family = AF_INET;
  58. addr4->sin_port = htons(p_port); // short, network byte order
  59. copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16);
  60. return sizeof(sockaddr_in);
  61. };
  62. };
  63. static size_t _set_listen_sockaddr(struct sockaddr_storage *p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) {
  64. memset(p_addr, 0, sizeof(struct sockaddr_storage));
  65. if (p_sock_type == IP::TYPE_IPV4) {
  66. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  67. addr4->sin_family = AF_INET;
  68. addr4->sin_port = htons(p_port);
  69. if (p_bind_address.is_valid()) {
  70. copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4);
  71. } else {
  72. addr4->sin_addr.s_addr = INADDR_ANY;
  73. }
  74. return sizeof(sockaddr_in);
  75. } else {
  76. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  77. addr6->sin6_family = AF_INET6;
  78. addr6->sin6_port = htons(p_port);
  79. if (p_bind_address.is_valid()) {
  80. copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16);
  81. } else {
  82. addr6->sin6_addr = in6addr_any;
  83. }
  84. return sizeof(sockaddr_in6);
  85. };
  86. };
  87. static int _socket_create(IP::Type &p_type, int type, int protocol) {
  88. ERR_FAIL_COND_V(p_type > IP::TYPE_ANY || p_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
  89. int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
  90. int sockfd = socket(family, type, protocol);
  91. if (sockfd == -1 && p_type == IP::TYPE_ANY) {
  92. // Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
  93. // in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
  94. p_type = IP::TYPE_IPV4;
  95. family = AF_INET;
  96. sockfd = socket(family, type, protocol);
  97. }
  98. ERR_FAIL_COND_V(sockfd == -1, -1);
  99. if (family == AF_INET6) {
  100. // Select IPv4 over IPv6 mapping
  101. int opt = p_type != IP::TYPE_ANY;
  102. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&opt, sizeof(opt)) != 0) {
  103. WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6");
  104. }
  105. }
  106. return sockfd;
  107. }
  108. static void _set_ip_addr_port(IP_Address &r_ip, int &r_port, struct sockaddr_storage *p_addr) {
  109. if (p_addr->ss_family == AF_INET) {
  110. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  111. r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
  112. r_port = ntohs(addr4->sin_port);
  113. } else if (p_addr->ss_family == AF_INET6) {
  114. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  115. r_ip.set_ipv6(addr6->sin6_addr.s6_addr);
  116. r_port = ntohs(addr6->sin6_port);
  117. };
  118. };
  119. #endif