packet_peer_udp_winsock.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* packet_peer_udp_winsock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "packet_peer_udp_winsock.h"
  30. #include <winsock2.h>
  31. #include <ws2tcpip.h>
  32. #include "drivers/unix/socket_helpers.h"
  33. int PacketPeerUDPWinsock::get_available_packet_count() const {
  34. Error err = const_cast<PacketPeerUDPWinsock*>(this)->_poll(false);
  35. if (err!=OK)
  36. return 0;
  37. return queue_count;
  38. }
  39. Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
  40. Error err = const_cast<PacketPeerUDPWinsock*>(this)->_poll(false);
  41. if (err!=OK)
  42. return err;
  43. if (queue_count==0)
  44. return ERR_UNAVAILABLE;
  45. uint32_t size;
  46. uint8_t type;
  47. rb.read(&type, 1, true);
  48. if (type == IP_Address::TYPE_IPV4) {
  49. rb.read((uint8_t*)&packet_ip.field8,4,true);
  50. packet_ip.type = IP_Address::TYPE_IPV4;
  51. } else {
  52. rb.read((uint8_t*)&packet_ip.field8,16,true);
  53. packet_ip.type = IP_Address::TYPE_IPV6;
  54. };
  55. rb.read((uint8_t*)&packet_port,4,true);
  56. rb.read((uint8_t*)&size,4,true);
  57. rb.read(packet_buffer,size,true);
  58. --queue_count;
  59. *r_buffer=packet_buffer;
  60. r_buffer_size=size;
  61. return OK;
  62. }
  63. Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer,int p_buffer_size){
  64. int sock = _get_socket();
  65. ERR_FAIL_COND_V( sock == -1, FAILED );
  66. struct sockaddr_storage addr;
  67. size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port);
  68. _set_blocking(true);
  69. errno = 0;
  70. int err;
  71. while ( (err = sendto(sock, (const char*)p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) {
  72. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  73. return FAILED;
  74. };
  75. }
  76. return OK;
  77. }
  78. int PacketPeerUDPWinsock::get_max_packet_size() const{
  79. return 512; // uhm maybe not
  80. }
  81. void PacketPeerUDPWinsock::_set_blocking(bool p_blocking) {
  82. //am no windows expert
  83. //hope this is the right thing
  84. if (blocking==p_blocking)
  85. return;
  86. blocking=p_blocking;
  87. unsigned long par = blocking?0:1;
  88. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  89. perror("setting non-block mode");
  90. //close();
  91. //return -1;
  92. };
  93. }
  94. Error PacketPeerUDPWinsock::listen(int p_port, int p_recv_buffer_size) {
  95. close();
  96. int sock = _get_socket();
  97. if (sock == -1 )
  98. return ERR_CANT_CREATE;
  99. if(ip_type == IP_Address::TYPE_IPV6) {
  100. // Use IPv6 only socket
  101. int yes = 1;
  102. if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&yes, sizeof(yes)) != 0) {
  103. WARN_PRINT("Unable to unset IPv4 address mapping over IPv6");
  104. }
  105. }
  106. struct sockaddr_storage addr = {0};
  107. size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, NULL);
  108. if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
  109. close();
  110. return ERR_UNAVAILABLE;
  111. }
  112. blocking=true;
  113. printf("UDP Connection listening on port %i\n", p_port);
  114. rb.resize(nearest_shift(p_recv_buffer_size));
  115. return OK;
  116. }
  117. void PacketPeerUDPWinsock::close(){
  118. if (sockfd != -1)
  119. ::closesocket(sockfd);
  120. sockfd=-1;
  121. rb.resize(8);
  122. queue_count=0;
  123. }
  124. Error PacketPeerUDPWinsock::wait() {
  125. return _poll(true);
  126. }
  127. Error PacketPeerUDPWinsock::_poll(bool p_wait) {
  128. _set_blocking(p_wait);
  129. struct sockaddr_storage from = {0};
  130. int len = sizeof(struct sockaddr_storage);
  131. int ret;
  132. while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), 0, (struct sockaddr*)&from, &len)) > 0) {
  133. uint32_t port = 0;
  134. if (from.ss_family == AF_INET) {
  135. uint8_t type = (uint8_t)IP_Address::TYPE_IPV4;
  136. rb.write(&type, 1);
  137. struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
  138. rb.write((uint8_t*)&sin_from->sin_addr, 4);
  139. port = ntohs(sin_from->sin_port);
  140. } else if (from.ss_family == AF_INET6) {
  141. uint8_t type = (uint8_t)IP_Address::TYPE_IPV6;
  142. rb.write(&type, 1);
  143. struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
  144. rb.write((uint8_t*)&s6_from->sin6_addr, 16);
  145. port = ntohs(s6_from->sin6_port);
  146. } else {
  147. // WARN_PRINT("Ignoring packet with unknown address family");
  148. uint8_t type = (uint8_t)IP_Address::TYPE_NONE;
  149. rb.write(&type, 1);
  150. };
  151. rb.write((uint8_t*)&port, 4);
  152. rb.write((uint8_t*)&ret, 4);
  153. rb.write(recv_buffer, ret);
  154. len = sizeof(struct sockaddr_storage);
  155. ++queue_count;
  156. };
  157. if (ret == SOCKET_ERROR){
  158. int error = WSAGetLastError();
  159. if (error == WSAEWOULDBLOCK){
  160. // Expected when doing non-blocking sockets, retry later.
  161. }
  162. else if (error == WSAECONNRESET){
  163. // If the remote target does not accept messages, this error may occur, but is harmless.
  164. // Once the remote target gets available, this message will disappear for new messages.
  165. }
  166. else
  167. {
  168. close();
  169. return FAILED;
  170. }
  171. }
  172. if (ret == 0) {
  173. close();
  174. return FAILED;
  175. };
  176. return OK;
  177. }
  178. bool PacketPeerUDPWinsock::is_listening() const{
  179. return sockfd!=-1;
  180. }
  181. IP_Address PacketPeerUDPWinsock::get_packet_address() const {
  182. return packet_ip;
  183. }
  184. int PacketPeerUDPWinsock::get_packet_port() const{
  185. return packet_port;
  186. }
  187. int PacketPeerUDPWinsock::_get_socket() {
  188. if (sockfd != -1)
  189. return sockfd;
  190. sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
  191. return sockfd;
  192. }
  193. void PacketPeerUDPWinsock::set_send_address(const IP_Address& p_address,int p_port) {
  194. peer_addr=p_address;
  195. peer_port=p_port;
  196. }
  197. void PacketPeerUDPWinsock::make_default() {
  198. PacketPeerUDP::_create = PacketPeerUDPWinsock::_create;
  199. };
  200. PacketPeerUDP* PacketPeerUDPWinsock::_create() {
  201. return memnew(PacketPeerUDPWinsock);
  202. };
  203. PacketPeerUDPWinsock::PacketPeerUDPWinsock() {
  204. sockfd=-1;
  205. packet_port=0;
  206. queue_count=0;
  207. peer_port=0;
  208. ip_type = IP_Address::TYPE_ANY;
  209. }
  210. PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
  211. close();
  212. }