rws_socket.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef __RWS_SOCKET_H__
  23. #define __RWS_SOCKET_H__ 1
  24. #include "rws_common.h"
  25. #if defined(RWS_OS_WINDOWS)
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #else
  29. #include <netdb.h>
  30. #include <sys/socket.h>
  31. #include <sys/types.h>
  32. //#include <netinet/tcp.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #endif
  36. #include <assert.h>
  37. #include <errno.h>
  38. #include "rws_error.h"
  39. #include "rws_thread.h"
  40. #include "rws_frame.h"
  41. #include "rws_list.h"
  42. #ifdef LIBRWS_USING_MBED_TLS
  43. #include "mbedtls/net.h"
  44. #include "mbedtls/ssl.h"
  45. #include "mbedtls/certs.h"
  46. #include "mbedtls/entropy.h"
  47. #include "mbedtls/ctr_drbg.h"
  48. #include "mbedtls/debug.h"
  49. #endif
  50. #if defined(RWS_OS_WINDOWS)
  51. typedef SOCKET rws_socket_t;
  52. #define RWS_INVALID_SOCKET INVALID_SOCKET
  53. #define RWS_SOCK_CLOSE(sock) closesocket(sock)
  54. #else
  55. typedef int rws_socket_t;
  56. #define RWS_INVALID_SOCKET -1
  57. #define RWS_SOCK_CLOSE(sock) close(sock)
  58. #endif
  59. #ifdef LIBRWS_USING_MBED_TLS
  60. typedef struct _rws_ssl_struct
  61. {
  62. mbedtls_ssl_context ssl_ctx; /* mbedtls ssl context */
  63. mbedtls_net_context net_ctx; /* Fill in socket id */
  64. mbedtls_ssl_config ssl_conf; /* SSL configuration */
  65. mbedtls_entropy_context entropy;
  66. mbedtls_ctr_drbg_context ctr_drbg;
  67. mbedtls_x509_crt_profile profile;
  68. mbedtls_x509_crt cacert;
  69. mbedtls_x509_crt clicert;
  70. mbedtls_pk_context pkey;
  71. } _rws_ssl;
  72. #endif
  73. static const char *k_rws_socket_min_http_ver = "1.1";
  74. static const char *k_rws_socket_sec_websocket_accept = "Sec-WebSocket-Accept";
  75. typedef struct _rws_socket_struct
  76. {
  77. int port;
  78. rws_socket_t socket;
  79. char *scheme;
  80. char *host;
  81. char *path;
  82. char *sec_ws_accept; // "Sec-WebSocket-Accept" field from handshake
  83. rws_thread work_thread;
  84. int command;
  85. unsigned int next_message_id;
  86. rws_bool is_connected; // sock connected + handshake done
  87. void *user_object;
  88. rws_on_socket on_connected;
  89. rws_on_socket on_disconnected;
  90. rws_on_socket_recvd_text on_recvd_text;
  91. rws_on_socket_recvd_bin on_recvd_bin;
  92. void *received;
  93. size_t received_size; // size of 'received' memory
  94. size_t received_len; // length of actualy readed message
  95. _rws_list *send_frames;
  96. _rws_list *recvd_frames;
  97. _rws_error *error;
  98. rws_mutex work_mutex;
  99. rws_mutex send_mutex;
  100. int custom_mode;
  101. char *header_buff;
  102. char *payload_buff;
  103. #ifdef LIBRWS_USING_MBED_TLS
  104. const char *server_cert; /**< Server certification. */
  105. const char *client_cert; /**< Client certification. */
  106. const char *client_pk; /**< Client private key. */
  107. int server_cert_len; /**< Server certification lenght, server_cert buffer size. */
  108. int client_cert_len; /**< Client certification lenght, client_cert buffer size. */
  109. int client_pk_len; /**< Client private key lenght, client_pk buffer size. */
  110. _rws_ssl *ssl; /**< SSL content. */
  111. #endif
  112. } _rws_socket;
  113. rws_bool rws_socket_process_handshake_responce(_rws_socket *s);
  114. // receive raw data from socket
  115. rws_bool rws_socket_recv(_rws_socket *s);
  116. /* RT-Thread Team add */
  117. rws_bool rws_socket_recv_custom(_rws_socket *s);
  118. // send raw data to socket
  119. rws_bool rws_socket_send(_rws_socket *s, const void *data, const size_t data_size);
  120. _rws_frame *rws_socket_last_unfin_recvd_frame_by_opcode(_rws_socket *s, const rws_opcode opcode);
  121. void rws_socket_process_bin_or_text_frame(_rws_socket *s, _rws_frame *frame);
  122. void rws_socket_process_ping_frame(_rws_socket *s, _rws_frame *frame);
  123. void rws_socket_process_conn_close_frame(_rws_socket *s, _rws_frame *frame);
  124. void rws_socket_process_received_frame(_rws_socket *s, _rws_frame *frame);
  125. void rws_socket_idle_recv(_rws_socket *s);
  126. void rws_socket_idle_send(_rws_socket *s);
  127. void rws_socket_wait_handshake_responce(_rws_socket *s);
  128. unsigned int rws_socket_get_next_message_id(_rws_socket *s);
  129. void rws_socket_send_ping(_rws_socket *s);
  130. void rws_socket_send_disconnect(_rws_socket *s);
  131. void rws_socket_send_handshake(_rws_socket *s);
  132. struct addrinfo *rws_socket_connect_getaddr_info(_rws_socket *s);
  133. void rws_socket_connect_to_host(_rws_socket *s);
  134. rws_bool rws_socket_create_start_work_thread(_rws_socket *s);
  135. void rws_socket_close(_rws_socket *s);
  136. void rws_socket_resize_received(_rws_socket *s, const size_t size);
  137. void rws_socket_append_recvd_frames(_rws_socket *s, _rws_frame *frame);
  138. void rws_socket_append_send_frames(_rws_socket *s, _rws_frame *frame);
  139. rws_bool rws_socket_send_text_priv(_rws_socket *s, const char *text);
  140. rws_bool rws_socket_send_bin_priv(_rws_socket *s, const char *data, size_t len, rws_opcode opcode, rws_bool is_fin);
  141. void rws_socket_inform_recvd_frames(_rws_socket *s);
  142. void rws_socket_set_option(rws_socket_t s, int option, int value);
  143. void rws_socket_delete_all_frames_in_list(_rws_list *list_with_frames);
  144. void rws_socket_check_write_error(_rws_socket *s, int error_num);
  145. void rws_socket_delete(_rws_socket *s);
  146. #ifdef LIBRWS_USING_MBED_TLS
  147. void rws_debug(void *ctx, int level, const char *file, int line, const char *str);
  148. int rws_ssl_conn(_rws_socket *s);
  149. int rws_ssl_close(_rws_socket *s);
  150. #endif
  151. #define COMMAND_IDLE -1
  152. #define COMMAND_NONE 0
  153. #define COMMAND_CONNECT_TO_HOST 1
  154. #define COMMAND_SEND_HANDSHAKE 2
  155. #define COMMAND_WAIT_HANDSHAKE_RESPONCE 3
  156. #define COMMAND_INFORM_CONNECTED 4
  157. #define COMMAND_INFORM_DISCONNECTED 5
  158. #define COMMAND_DISCONNECT 6
  159. #define COMMAND_END 9999
  160. #endif