packet_peer_udp_posix.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* packet_peer_udp_posix.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_posix.h"
  30. #ifdef UNIX_ENABLED
  31. #include <errno.h>
  32. #include <unistd.h>
  33. #include <netdb.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <stdio.h>
  38. #ifndef NO_FCNTL
  39. #ifdef __HAIKU__
  40. #include <fcntl.h>
  41. #else
  42. #include <sys/fcntl.h>
  43. #endif
  44. #else
  45. #include <sys/ioctl.h>
  46. #endif
  47. #ifdef JAVASCRIPT_ENABLED
  48. #include <arpa/inet.h>
  49. #endif
  50. int PacketPeerUDPPosix::get_available_packet_count() const {
  51. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  52. if (err!=OK)
  53. return 0;
  54. return queue_count;
  55. }
  56. Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
  57. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  58. if (err!=OK)
  59. return err;
  60. if (queue_count==0)
  61. return ERR_UNAVAILABLE;
  62. uint32_t size;
  63. uint8_t type;
  64. rb.read(&type, 1, true);
  65. if (type == IP_Address::TYPE_IPV4) {
  66. rb.read((uint8_t*)&packet_ip.field8,4,true);
  67. packet_ip.type = IP_Address::TYPE_IPV4;
  68. } else {
  69. rb.read((uint8_t*)&packet_ip.field8,16,true);
  70. packet_ip.type = IP_Address::TYPE_IPV6;
  71. };
  72. rb.read((uint8_t*)&packet_port,4,true);
  73. rb.read((uint8_t*)&size,4,true);
  74. rb.read(packet_buffer,size,true);
  75. --queue_count;
  76. *r_buffer=packet_buffer;
  77. r_buffer_size=size;
  78. return OK;
  79. }
  80. Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
  81. ERR_FAIL_COND_V(peer_addr.type == IP_Address::TYPE_NONE, ERR_UNCONFIGURED);
  82. int sock = _get_socket(peer_addr.type);
  83. ERR_FAIL_COND_V( sock == -1, FAILED );
  84. struct sockaddr_storage addr;
  85. if (peer_addr.type == IP_Address::TYPE_IPV4) {
  86. struct sockaddr_in* addr_in = (struct sockaddr_in*)&addr;
  87. addr_in->sin_family = AF_INET;
  88. addr_in->sin_port = htons(peer_port);
  89. addr_in->sin_addr = *((struct in_addr*)&peer_addr.field32[0]);
  90. } else {
  91. struct sockaddr_in6* addr_in6 = (struct sockaddr_in6*)&addr;
  92. addr_in6->sin6_family = AF_INET;
  93. addr_in6->sin6_port = htons(peer_port);
  94. copymem(&addr_in6->sin6_addr.s6_addr, peer_addr.field8, 16);
  95. };
  96. errno = 0;
  97. int err;
  98. while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, sizeof(addr))) != p_buffer_size) {
  99. if (errno != EAGAIN) {
  100. return FAILED;
  101. }
  102. }
  103. return OK;
  104. }
  105. int PacketPeerUDPPosix::get_max_packet_size() const{
  106. return 512; // uhm maybe not
  107. }
  108. Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_address_type, int p_recv_buffer_size) {
  109. ERR_FAIL_COND_V(p_address_type != IP_Address::TYPE_IPV4 && p_address_type != IP_Address::TYPE_IPV6, ERR_INVALID_PARAMETER);
  110. close();
  111. int sock = _get_socket(p_address_type);
  112. if (sock == -1 )
  113. return ERR_CANT_CREATE;
  114. sockaddr_storage addr = {0};
  115. if (p_address_type == IP_Address::TYPE_IPV4) {
  116. struct sockaddr_in* addr4 = (struct sockaddr_in*)&addr;
  117. addr4->sin_family = AF_INET;
  118. addr4->sin_port = htons(p_port);
  119. addr4->sin_addr.s_addr = INADDR_ANY;
  120. } else {
  121. struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&addr;
  122. addr6->sin6_family = AF_INET6;
  123. addr6->sin6_port = htons(p_port);
  124. addr6->sin6_addr = in6addr_any;
  125. };
  126. if (bind(sock, (struct sockaddr*)&addr, sizeof(sockaddr_storage)) == -1 ) {
  127. close();
  128. return ERR_UNAVAILABLE;
  129. }
  130. rb.resize(nearest_shift(p_recv_buffer_size));
  131. return OK;
  132. }
  133. void PacketPeerUDPPosix::close(){
  134. if (sockfd != -1)
  135. ::close(sockfd);
  136. sockfd=-1;
  137. rb.resize(8);
  138. queue_count=0;
  139. }
  140. Error PacketPeerUDPPosix::wait() {
  141. return _poll(true);
  142. }
  143. Error PacketPeerUDPPosix::_poll(bool p_wait) {
  144. struct sockaddr_storage from = {0};
  145. socklen_t len = sizeof(struct sockaddr_storage);
  146. int ret;
  147. while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
  148. uint32_t port = 0;
  149. if (from.ss_family == AF_INET) {
  150. uint8_t type = (uint8_t)IP_Address::TYPE_IPV4;
  151. rb.write(&type, 1);
  152. struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
  153. rb.write((uint8_t*)&sin_from->sin_addr, 4);
  154. port = sin_from->sin_port;
  155. } else if (from.ss_family == AF_INET6) {
  156. uint8_t type = (uint8_t)IP_Address::TYPE_IPV6;
  157. rb.write(&type, 1);
  158. struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
  159. rb.write((uint8_t*)&s6_from->sin6_addr, 16);
  160. port = s6_from->sin6_port;
  161. } else {
  162. // WARN_PRINT("Ignoring packet with unknown address family");
  163. uint8_t type = (uint8_t)IP_Address::TYPE_NONE;
  164. rb.write(&type, 1);
  165. };
  166. rb.write((uint8_t*)&port, 4);
  167. rb.write((uint8_t*)&ret, 4);
  168. rb.write(recv_buffer, ret);
  169. ++queue_count;
  170. };
  171. // TODO: Should ECONNRESET be handled here?
  172. if (ret == 0 || (ret == -1 && errno != EAGAIN) ) {
  173. close();
  174. return FAILED;
  175. };
  176. return OK;
  177. }
  178. bool PacketPeerUDPPosix::is_listening() const{
  179. return sockfd!=-1;
  180. }
  181. IP_Address PacketPeerUDPPosix::get_packet_address() const {
  182. return packet_ip;
  183. }
  184. int PacketPeerUDPPosix::get_packet_port() const{
  185. return packet_port;
  186. }
  187. int PacketPeerUDPPosix::_get_socket(IP_Address::AddrType p_type) {
  188. if (sockfd != -1)
  189. return sockfd;
  190. int family = p_type == IP_Address::TYPE_IPV6 ? AF_INET6 : AF_INET;
  191. sockfd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
  192. ERR_FAIL_COND_V( sockfd == -1, -1 );
  193. //fcntl(sockfd, F_SETFL, O_NONBLOCK);
  194. return sockfd;
  195. }
  196. void PacketPeerUDPPosix::set_send_address(const IP_Address& p_address,int p_port) {
  197. peer_addr=p_address;
  198. peer_port=p_port;
  199. }
  200. PacketPeerUDP* PacketPeerUDPPosix::_create() {
  201. return memnew(PacketPeerUDPPosix);
  202. };
  203. void PacketPeerUDPPosix::make_default() {
  204. PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
  205. };
  206. PacketPeerUDPPosix::PacketPeerUDPPosix() {
  207. sockfd=-1;
  208. packet_port=0;
  209. queue_count=0;
  210. peer_port=0;
  211. }
  212. PacketPeerUDPPosix::~PacketPeerUDPPosix() {
  213. close();
  214. }
  215. #endif