lws_client.cpp 6.5 KB

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