socket_helpers.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* socket_helpers.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://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. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  56. addr4->sin_family = AF_INET;
  57. addr4->sin_port = htons(p_port); // short, network byte order
  58. copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16);
  59. return sizeof(sockaddr_in);
  60. };
  61. };
  62. 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) {
  63. memset(p_addr, 0, sizeof(struct sockaddr_storage));
  64. if (p_sock_type == IP::TYPE_IPV4) {
  65. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  66. addr4->sin_family = AF_INET;
  67. addr4->sin_port = htons(p_port);
  68. if (p_bind_address.is_valid()) {
  69. copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4);
  70. } else {
  71. addr4->sin_addr.s_addr = INADDR_ANY;
  72. }
  73. return sizeof(sockaddr_in);
  74. } else {
  75. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  76. addr6->sin6_family = AF_INET6;
  77. addr6->sin6_port = htons(p_port);
  78. if (p_bind_address.is_valid()) {
  79. copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16);
  80. } else {
  81. addr6->sin6_addr = in6addr_any;
  82. }
  83. return sizeof(sockaddr_in6);
  84. };
  85. };
  86. static int _socket_create(IP::Type &p_type, int type, int protocol) {
  87. ERR_FAIL_COND_V(p_type > IP::TYPE_ANY || p_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
  88. int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
  89. int sockfd = socket(family, type, protocol);
  90. if (sockfd == -1 && p_type == IP::TYPE_ANY) {
  91. // Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
  92. // in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
  93. p_type = IP::TYPE_IPV4;
  94. family = AF_INET;
  95. sockfd = socket(family, type, protocol);
  96. }
  97. ERR_FAIL_COND_V(sockfd == -1, -1);
  98. if (family == AF_INET6) {
  99. // Select IPv4 over IPv6 mapping
  100. int opt = p_type != IP::TYPE_ANY;
  101. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&opt, sizeof(opt)) != 0) {
  102. WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6");
  103. }
  104. }
  105. return sockfd;
  106. }
  107. static void _set_ip_addr_port(IP_Address &r_ip, int &r_port, struct sockaddr_storage *p_addr) {
  108. if (p_addr->ss_family == AF_INET) {
  109. struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
  110. r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
  111. r_port = ntohs(addr4->sin_port);
  112. } else if (p_addr->ss_family == AF_INET6) {
  113. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  114. r_ip.set_ipv6(addr6->sin6_addr.s6_addr);
  115. r_port = ntohs(addr6->sin6_port);
  116. };
  117. };
  118. #endif