lws_client.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*************************************************************************/
  2. /* lws_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 JAVASCRIPT_ENABLED
  31. #include "lws_client.h"
  32. #include "core/io/ip.h"
  33. #include "core/io/stream_peer_ssl.h"
  34. #include "core/project_settings.h"
  35. #if defined(LWS_OPENSSL_SUPPORT)
  36. // Not openssl, just the mbedtls wrapper
  37. #include "openssl/ssl.h"
  38. #endif
  39. Error LWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) {
  40. ERR_FAIL_COND_V(context != NULL, FAILED);
  41. IP_Address addr;
  42. if (!p_host.is_valid_ip_address()) {
  43. addr = IP::get_singleton()->resolve_hostname(p_host);
  44. } else {
  45. addr = p_host;
  46. }
  47. ERR_FAIL_COND_V(!addr.is_valid(), ERR_INVALID_PARAMETER);
  48. // Prepare protocols
  49. _lws_make_protocols(this, &LWSClient::_lws_gd_callback, p_protocols, &_lws_ref);
  50. // Init lws client
  51. struct lws_context_creation_info info;
  52. struct lws_client_connect_info i;
  53. memset(&i, 0, sizeof i);
  54. memset(&info, 0, sizeof info);
  55. info.port = CONTEXT_PORT_NO_LISTEN;
  56. info.protocols = _lws_ref->lws_structs;
  57. info.gid = -1;
  58. info.uid = -1;
  59. //info.ws_ping_pong_interval = 5;
  60. info.user = _lws_ref;
  61. #if defined(LWS_OPENSSL_SUPPORT)
  62. info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  63. #endif
  64. context = lws_create_context(&info);
  65. if (context == NULL) {
  66. _lws_free_ref(_lws_ref);
  67. _lws_ref = NULL;
  68. ERR_EXPLAIN("Unable to create lws context");
  69. ERR_FAIL_V(FAILED);
  70. }
  71. i.context = context;
  72. if (p_protocols.size() > 0)
  73. i.protocol = _lws_ref->lws_names;
  74. else
  75. i.protocol = NULL;
  76. if (p_ssl) {
  77. i.ssl_connection = LCCSCF_USE_SSL;
  78. if (!verify_ssl)
  79. i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
  80. } else {
  81. i.ssl_connection = 0;
  82. }
  83. // These CharStrings needs to survive till we call lws_client_connect_via_info
  84. CharString addr_ch = ((String)addr).ascii();
  85. CharString host_ch = p_host.utf8();
  86. CharString path_ch = p_path.utf8();
  87. i.address = addr_ch.get_data();
  88. i.host = host_ch.get_data();
  89. i.path = path_ch.get_data();
  90. i.port = p_port;
  91. lws_client_connect_via_info(&i);
  92. return OK;
  93. };
  94. int LWSClient::get_max_packet_size() const {
  95. return (1 << _out_buf_size) - PROTO_SIZE;
  96. }
  97. void LWSClient::poll() {
  98. _lws_poll();
  99. }
  100. int LWSClient::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
  101. Ref<LWSPeer> peer = static_cast<Ref<LWSPeer> >(_peer);
  102. LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
  103. switch (reason) {
  104. #if defined(LWS_OPENSSL_SUPPORT)
  105. case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: {
  106. PoolByteArray arr = StreamPeerSSL::get_project_cert_array();
  107. if (arr.size() > 0)
  108. SSL_CTX_add_client_CA((SSL_CTX *)user, d2i_X509(NULL, &arr.read()[0], arr.size()));
  109. else if (verify_ssl)
  110. WARN_PRINTS("No CA cert specified in project settings, SSL will not work");
  111. } break;
  112. #endif
  113. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  114. peer->set_wsi(wsi, _in_buf_size, _in_pkt_size, _out_buf_size, _out_pkt_size);
  115. peer_data->peer_id = 0;
  116. peer_data->force_close = false;
  117. peer_data->clean_close = false;
  118. _on_connect(lws_get_protocol(wsi)->name);
  119. break;
  120. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
  121. _on_error();
  122. destroy_context();
  123. return -1; // We should close the connection (would probably happen anyway)
  124. case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
  125. int code;
  126. String reason2 = peer->get_close_reason(in, len, code);
  127. peer_data->clean_close = true;
  128. _on_close_request(code, reason2);
  129. return 0;
  130. }
  131. case LWS_CALLBACK_CLIENT_CLOSED:
  132. peer->close();
  133. destroy_context();
  134. _on_disconnect(peer_data->clean_close);
  135. return 0; // We can end here
  136. case LWS_CALLBACK_CLIENT_RECEIVE:
  137. peer->read_wsi(in, len);
  138. if (peer->get_available_packet_count() > 0)
  139. _on_peer_packet();
  140. break;
  141. case LWS_CALLBACK_CLIENT_WRITEABLE:
  142. if (peer_data->force_close) {
  143. peer->send_close_status(wsi);
  144. return -1;
  145. }
  146. peer->write_wsi();
  147. break;
  148. default:
  149. break;
  150. }
  151. return 0;
  152. }
  153. Ref<WebSocketPeer> LWSClient::get_peer(int p_peer_id) const {
  154. return _peer;
  155. }
  156. NetworkedMultiplayerPeer::ConnectionStatus LWSClient::get_connection_status() const {
  157. if (context == NULL)
  158. return CONNECTION_DISCONNECTED;
  159. if (_peer->is_connected_to_host())
  160. return CONNECTION_CONNECTED;
  161. return CONNECTION_CONNECTING;
  162. }
  163. void LWSClient::disconnect_from_host(int p_code, String p_reason) {
  164. if (context == NULL)
  165. return;
  166. _peer->close(p_code, p_reason);
  167. };
  168. IP_Address LWSClient::get_connected_host() const {
  169. return IP_Address();
  170. };
  171. uint16_t LWSClient::get_connected_port() const {
  172. return 1025;
  173. };
  174. LWSClient::LWSClient() {
  175. _in_buf_size = nearest_shift((int)GLOBAL_GET(WSC_IN_BUF) - 1) + 10;
  176. _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1);
  177. _out_buf_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_BUF) - 1) + 10;
  178. _out_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_PKT) - 1);
  179. context = NULL;
  180. _lws_ref = NULL;
  181. _peer = Ref<LWSPeer>(memnew(LWSPeer));
  182. };
  183. LWSClient::~LWSClient() {
  184. invalidate_lws_ref(); // We do not want any more callback
  185. disconnect_from_host();
  186. destroy_context();
  187. _peer = Ref<LWSPeer>();
  188. };
  189. #endif // JAVASCRIPT_ENABLED