packet_peer_udp_posix.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-2017 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. #include "drivers/unix/socket_helpers.h"
  51. int PacketPeerUDPPosix::get_available_packet_count() const {
  52. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  53. if (err!=OK)
  54. return 0;
  55. return queue_count;
  56. }
  57. Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
  58. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  59. if (err!=OK)
  60. return err;
  61. if (queue_count==0)
  62. return ERR_UNAVAILABLE;
  63. uint32_t size;
  64. uint8_t type;
  65. rb.read(&type, 1, true);
  66. if (type == IP::TYPE_IPV4) {
  67. uint8_t ip[4];
  68. rb.read(ip,4,true);
  69. packet_ip.set_ipv4(ip);
  70. } else {
  71. uint8_t ipv6[16];
  72. rb.read(ipv6,16,true);
  73. packet_ip.set_ipv6(ipv6);
  74. };
  75. rb.read((uint8_t*)&packet_port,4,true);
  76. rb.read((uint8_t*)&size,4,true);
  77. rb.read(packet_buffer,size,true);
  78. --queue_count;
  79. *r_buffer=packet_buffer;
  80. r_buffer_size=size;
  81. return OK;
  82. }
  83. Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
  84. ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
  85. if (sock_type==IP::TYPE_NONE)
  86. sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  87. int sock = _get_socket();
  88. ERR_FAIL_COND_V( sock == -1, FAILED );
  89. struct sockaddr_storage addr;
  90. size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
  91. errno = 0;
  92. int err;
  93. while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) {
  94. if (errno != EAGAIN) {
  95. return FAILED;
  96. }
  97. }
  98. return OK;
  99. }
  100. int PacketPeerUDPPosix::get_max_packet_size() const{
  101. return 512; // uhm maybe not
  102. }
  103. Error PacketPeerUDPPosix::listen(int p_port, IP_Address p_bind_address, int p_recv_buffer_size) {
  104. ERR_FAIL_COND_V(sockfd!=-1,ERR_ALREADY_IN_USE);
  105. ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(),ERR_INVALID_PARAMETER);
  106. #ifdef __OpenBSD__
  107. sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
  108. #else
  109. sock_type = IP::TYPE_ANY;
  110. #endif
  111. if(p_bind_address.is_valid())
  112. sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  113. int sock = _get_socket();
  114. if (sock == -1 )
  115. return ERR_CANT_CREATE;
  116. sockaddr_storage addr = {0};
  117. size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
  118. if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
  119. close();
  120. return ERR_UNAVAILABLE;
  121. }
  122. rb.resize(nearest_shift(p_recv_buffer_size));
  123. return OK;
  124. }
  125. void PacketPeerUDPPosix::close(){
  126. if (sockfd != -1)
  127. ::close(sockfd);
  128. sockfd=-1;
  129. sock_type = IP::TYPE_NONE;
  130. rb.resize(8);
  131. queue_count=0;
  132. }
  133. Error PacketPeerUDPPosix::wait() {
  134. return _poll(true);
  135. }
  136. Error PacketPeerUDPPosix::_poll(bool p_wait) {
  137. if (sockfd==-1) {
  138. return FAILED;
  139. }
  140. struct sockaddr_storage from = {0};
  141. socklen_t len = sizeof(struct sockaddr_storage);
  142. int ret;
  143. 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) {
  144. uint32_t port = 0;
  145. if (from.ss_family == AF_INET) {
  146. uint8_t type = (uint8_t)IP::TYPE_IPV4;
  147. rb.write(&type, 1);
  148. struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
  149. rb.write((uint8_t*)&sin_from->sin_addr, 4);
  150. port = ntohs(sin_from->sin_port);
  151. } else if (from.ss_family == AF_INET6) {
  152. uint8_t type = (uint8_t)IP::TYPE_IPV6;
  153. rb.write(&type, 1);
  154. struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
  155. rb.write((uint8_t*)&s6_from->sin6_addr, 16);
  156. port = ntohs(s6_from->sin6_port);
  157. } else {
  158. // WARN_PRINT("Ignoring packet with unknown address family");
  159. uint8_t type = (uint8_t)IP::TYPE_NONE;
  160. rb.write(&type, 1);
  161. };
  162. rb.write((uint8_t*)&port, 4);
  163. rb.write((uint8_t*)&ret, 4);
  164. rb.write(recv_buffer, ret);
  165. len = sizeof(struct sockaddr_storage);
  166. ++queue_count;
  167. };
  168. // TODO: Should ECONNRESET be handled here?
  169. if (ret == 0 || (ret == -1 && errno != EAGAIN) ) {
  170. close();
  171. return FAILED;
  172. };
  173. return OK;
  174. }
  175. bool PacketPeerUDPPosix::is_listening() const{
  176. return sockfd!=-1;
  177. }
  178. IP_Address PacketPeerUDPPosix::get_packet_address() const {
  179. return packet_ip;
  180. }
  181. int PacketPeerUDPPosix::get_packet_port() const{
  182. return packet_port;
  183. }
  184. int PacketPeerUDPPosix::_get_socket() {
  185. ERR_FAIL_COND_V(sock_type==IP::TYPE_NONE, -1);
  186. if (sockfd != -1)
  187. return sockfd;
  188. sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
  189. return sockfd;
  190. }
  191. void PacketPeerUDPPosix::set_dest_address(const IP_Address& p_address,int p_port) {
  192. peer_addr=p_address;
  193. peer_port=p_port;
  194. }
  195. PacketPeerUDP* PacketPeerUDPPosix::_create() {
  196. return memnew(PacketPeerUDPPosix);
  197. };
  198. void PacketPeerUDPPosix::make_default() {
  199. PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
  200. };
  201. PacketPeerUDPPosix::PacketPeerUDPPosix() {
  202. sockfd=-1;
  203. packet_port=0;
  204. queue_count=0;
  205. peer_port=0;
  206. sock_type = IP::TYPE_NONE;
  207. }
  208. PacketPeerUDPPosix::~PacketPeerUDPPosix() {
  209. close();
  210. }
  211. #endif