private-lib-roles-ws.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * This is included from private-lib-core.h if LWS_ROLE_WS
  25. */
  26. extern const struct lws_role_ops role_ops_ws;
  27. #define lwsi_role_ws(wsi) (wsi->role_ops == &role_ops_ws)
  28. enum lws_rx_parse_state {
  29. LWS_RXPS_NEW,
  30. LWS_RXPS_04_mask_1,
  31. LWS_RXPS_04_mask_2,
  32. LWS_RXPS_04_mask_3,
  33. LWS_RXPS_04_FRAME_HDR_1,
  34. LWS_RXPS_04_FRAME_HDR_LEN,
  35. LWS_RXPS_04_FRAME_HDR_LEN16_2,
  36. LWS_RXPS_04_FRAME_HDR_LEN16_1,
  37. LWS_RXPS_04_FRAME_HDR_LEN64_8,
  38. LWS_RXPS_04_FRAME_HDR_LEN64_7,
  39. LWS_RXPS_04_FRAME_HDR_LEN64_6,
  40. LWS_RXPS_04_FRAME_HDR_LEN64_5,
  41. LWS_RXPS_04_FRAME_HDR_LEN64_4,
  42. LWS_RXPS_04_FRAME_HDR_LEN64_3,
  43. LWS_RXPS_04_FRAME_HDR_LEN64_2,
  44. LWS_RXPS_04_FRAME_HDR_LEN64_1,
  45. LWS_RXPS_07_COLLECT_FRAME_KEY_1,
  46. LWS_RXPS_07_COLLECT_FRAME_KEY_2,
  47. LWS_RXPS_07_COLLECT_FRAME_KEY_3,
  48. LWS_RXPS_07_COLLECT_FRAME_KEY_4,
  49. LWS_RXPS_WS_FRAME_PAYLOAD
  50. };
  51. enum lws_websocket_opcodes_07 {
  52. LWSWSOPC_CONTINUATION = 0,
  53. LWSWSOPC_TEXT_FRAME = 1,
  54. LWSWSOPC_BINARY_FRAME = 2,
  55. LWSWSOPC_NOSPEC__MUX = 7,
  56. /* control extensions 8+ */
  57. LWSWSOPC_CLOSE = 8,
  58. LWSWSOPC_PING = 9,
  59. LWSWSOPC_PONG = 0xa,
  60. };
  61. /* this is not usable directly by user code any more, lws_close_reason() */
  62. #define LWS_WRITE_CLOSE 4
  63. #define ALREADY_PROCESSED_IGNORE_CHAR 1
  64. #define ALREADY_PROCESSED_NO_CB 2
  65. #if !defined(LWS_WITHOUT_EXTENSIONS)
  66. struct lws_vhost_role_ws {
  67. const struct lws_extension *extensions;
  68. };
  69. struct lws_pt_role_ws {
  70. struct lws *rx_draining_ext_list;
  71. struct lws *tx_draining_ext_list;
  72. };
  73. #endif
  74. struct _lws_websocket_related {
  75. unsigned char *rx_ubuf;
  76. #if !defined(LWS_WITHOUT_EXTENSIONS)
  77. const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
  78. void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
  79. struct lws *rx_draining_ext_list;
  80. struct lws *tx_draining_ext_list;
  81. #endif
  82. #if defined(LWS_WITH_HTTP_PROXY)
  83. struct lws_dll2_owner proxy_owner;
  84. char actual_protocol[16];
  85. size_t proxy_buffered;
  86. #endif
  87. /* Also used for close content... control opcode == < 128 */
  88. uint8_t ping_payload_buf[128 - 3 + LWS_PRE];
  89. unsigned int final:1;
  90. unsigned int frame_is_binary:1;
  91. unsigned int all_zero_nonce:1;
  92. unsigned int this_frame_masked:1;
  93. unsigned int inside_frame:1; /* next write will be more of frame */
  94. unsigned int clean_buffer:1; /* buffer not rewritten by extension */
  95. unsigned int payload_is_close:1; /* process as PONG, but it is close */
  96. unsigned int ping_pending_flag:1;
  97. unsigned int continuation_possible:1;
  98. unsigned int owed_a_fin:1;
  99. unsigned int check_utf8:1;
  100. unsigned int defeat_check_utf8:1;
  101. unsigned int stashed_write_pending:1;
  102. unsigned int send_check_ping:1;
  103. unsigned int first_fragment:1;
  104. unsigned int peer_has_sent_close:1;
  105. #if !defined(LWS_WITHOUT_EXTENSIONS)
  106. unsigned int extension_data_pending:1;
  107. unsigned int rx_draining_ext:1;
  108. unsigned int tx_draining_ext:1;
  109. unsigned int pmd_trailer_application:1;
  110. #endif
  111. uint8_t mask[4];
  112. size_t rx_packet_length;
  113. uint32_t rx_ubuf_head;
  114. uint32_t rx_ubuf_alloc;
  115. uint8_t ping_payload_len;
  116. uint8_t mask_idx;
  117. uint8_t opcode;
  118. uint8_t rsv;
  119. uint8_t rsv_first_msg;
  120. /* zero if no info, or length including 2-byte close code */
  121. uint8_t close_in_ping_buffer_len;
  122. uint8_t utf8;
  123. uint8_t stashed_write_type;
  124. uint8_t tx_draining_stashed_wp;
  125. uint8_t ietf_spec_revision;
  126. #if !defined(LWS_WITHOUT_EXTENSIONS)
  127. uint8_t count_act_ext;
  128. #endif
  129. };
  130. /*
  131. * we need to separately track what's happening with both compressed rx in
  132. * and with inflated rx out that will be passed to the user code
  133. */
  134. struct lws_ext_pm_deflate_rx_ebufs {
  135. struct lws_tokens eb_in;
  136. struct lws_tokens eb_out;
  137. };
  138. int
  139. lws_ws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
  140. #if !defined(LWS_WITHOUT_EXTENSIONS)
  141. LWS_VISIBLE void
  142. lws_context_init_extensions(const struct lws_context_creation_info *info,
  143. struct lws_context *context);
  144. LWS_EXTERN int
  145. lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
  146. void *v, size_t len);
  147. LWS_EXTERN int
  148. lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
  149. LWS_EXTERN int
  150. lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
  151. void *arg, int len);
  152. #endif
  153. int
  154. handshake_0405(struct lws_context *context, struct lws *wsi);
  155. int
  156. lws_process_ws_upgrade(struct lws *wsi);
  157. int
  158. lws_process_ws_upgrade2(struct lws *wsi);
  159. extern const struct lws_protocols lws_ws_proxy;
  160. int
  161. lws_server_init_wsi_for_ws(struct lws *wsi);
  162. void
  163. lws_sul_wsping_cb(lws_sorted_usec_list_t *sul);