private.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2018 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. *
  21. * This is included from core/private.h
  22. */
  23. typedef uint32_t lws_wsi_state_t;
  24. /*
  25. * The wsi->role_ops pointer decides almost everything about what role the wsi
  26. * will play, h2, raw, ws, etc.
  27. *
  28. * However there are a few additional flags needed that vary, such as if the
  29. * role is a client or server side, if it has that concept. And the connection
  30. * fulfilling the role, has a separate dynamic state.
  31. *
  32. * 31 16 15 0
  33. * [ role flags ] [ state ]
  34. *
  35. * The role flags part is generally invariant for the lifetime of the wsi,
  36. * although it can change if the connection role itself does, eg, if the
  37. * connection upgrades from H1 -> WS1 the role flags may be changed at that
  38. * point.
  39. *
  40. * The state part reflects the dynamic connection state, and the states are
  41. * reused between roles.
  42. *
  43. * None of the internal role or state representations are made available outside
  44. * of lws internals. Even for lws internals, if you add stuff here, please keep
  45. * the constants inside this header only by adding necessary helpers here and
  46. * use the helpers in the actual code. This is to ease any future refactors.
  47. *
  48. * Notice LWSIFR_ENCAP means we have a parent wsi that actually carries our
  49. * data as a stream inside a different protocol.
  50. */
  51. #define _RS 16
  52. #define LWSIFR_CLIENT (0x1000 << _RS) /* client side */
  53. #define LWSIFR_SERVER (0x2000 << _RS) /* server side */
  54. #define LWSIFR_P_ENCAP_H2 (0x0100 << _RS) /* we are encapsulated by h2 */
  55. enum lwsi_role {
  56. LWSI_ROLE_MASK = (0xffff << _RS),
  57. LWSI_ROLE_ENCAP_MASK = (0x0f00 << _RS),
  58. };
  59. #define lwsi_role(wsi) (wsi->wsistate & LWSI_ROLE_MASK)
  60. #if !defined (_DEBUG)
  61. #define lwsi_set_role(wsi, role) wsi->wsistate = \
  62. (wsi->wsistate & (~LWSI_ROLE_MASK)) | role
  63. #else
  64. void lwsi_set_role(struct lws *wsi, lws_wsi_state_t role);
  65. #endif
  66. #define lwsi_role_client(wsi) (!!(wsi->wsistate & LWSIFR_CLIENT))
  67. #define lwsi_role_server(wsi) (!!(wsi->wsistate & LWSIFR_SERVER))
  68. #define lwsi_role_h2_ENCAPSULATION(wsi) \
  69. ((wsi->wsistate & LWSI_ROLE_ENCAP_MASK) == LWSIFR_P_ENCAP_H2)
  70. /* Pollout wants a callback in this state */
  71. #define LWSIFS_POCB (0x100)
  72. /* Before any protocol connection was established */
  73. #define LWSIFS_NOT_EST (0x200)
  74. enum lwsi_state {
  75. /* Phase 1: pre-transport */
  76. LRS_UNCONNECTED = LWSIFS_NOT_EST | 0,
  77. LRS_WAITING_CONNECT = LWSIFS_NOT_EST | 1,
  78. /* Phase 2: establishing intermediaries on top of transport */
  79. LRS_WAITING_PROXY_REPLY = LWSIFS_NOT_EST | 2,
  80. LRS_WAITING_SSL = LWSIFS_NOT_EST | 3,
  81. LRS_WAITING_SOCKS_GREETING_REPLY = LWSIFS_NOT_EST | 4,
  82. LRS_WAITING_SOCKS_CONNECT_REPLY = LWSIFS_NOT_EST | 5,
  83. LRS_WAITING_SOCKS_AUTH_REPLY = LWSIFS_NOT_EST | 6,
  84. /* Phase 3: establishing tls tunnel */
  85. LRS_SSL_INIT = LWSIFS_NOT_EST | 7,
  86. LRS_SSL_ACK_PENDING = LWSIFS_NOT_EST | 8,
  87. LRS_PRE_WS_SERVING_ACCEPT = LWSIFS_NOT_EST | 9,
  88. /* Phase 4: connected */
  89. LRS_WAITING_SERVER_REPLY = LWSIFS_NOT_EST | 10,
  90. LRS_H2_AWAIT_PREFACE = LWSIFS_NOT_EST | 11,
  91. LRS_H2_AWAIT_SETTINGS = LWSIFS_NOT_EST |
  92. LWSIFS_POCB | 12,
  93. /* Phase 5: protocol logically established */
  94. LRS_H2_CLIENT_SEND_SETTINGS = LWSIFS_POCB | 13,
  95. LRS_H2_WAITING_TO_SEND_HEADERS = LWSIFS_POCB | 14,
  96. LRS_DEFERRING_ACTION = LWSIFS_POCB | 15,
  97. LRS_IDLING = 16,
  98. LRS_H1C_ISSUE_HANDSHAKE = 17,
  99. LRS_H1C_ISSUE_HANDSHAKE2 = 18,
  100. LRS_ISSUE_HTTP_BODY = 19,
  101. LRS_ISSUING_FILE = 20,
  102. LRS_HEADERS = 21,
  103. LRS_BODY = 22,
  104. LRS_ESTABLISHED = LWSIFS_POCB | 23,
  105. /* we are established, but we have embarked on serving a single
  106. * transaction. Other transaction input may be pending, but we will
  107. * not service it while we are busy dealing with the current
  108. * transaction.
  109. *
  110. * When we complete the current transaction, we would reset our state
  111. * back to ESTABLISHED and start to process the next transaction.
  112. */
  113. LRS_DOING_TRANSACTION = LWSIFS_POCB | 24,
  114. /* Phase 6: finishing */
  115. LRS_WAITING_TO_SEND_CLOSE = LWSIFS_POCB | 25,
  116. LRS_RETURNED_CLOSE = LWSIFS_POCB | 26,
  117. LRS_AWAITING_CLOSE_ACK = LWSIFS_POCB | 27,
  118. LRS_FLUSHING_BEFORE_CLOSE = LWSIFS_POCB | 28,
  119. LRS_SHUTDOWN = 29,
  120. /* Phase 7: dead */
  121. LRS_DEAD_SOCKET = 30,
  122. LRS_MASK = 0xffff
  123. };
  124. #define lwsi_state(wsi) ((enum lwsi_state)(wsi->wsistate & LRS_MASK))
  125. #define lwsi_state_PRE_CLOSE(wsi) ((enum lwsi_state)(wsi->wsistate_pre_close & LRS_MASK))
  126. #define lwsi_state_est(wsi) (!(wsi->wsistate & LWSIFS_NOT_EST))
  127. #define lwsi_state_est_PRE_CLOSE(wsi) (!(wsi->wsistate_pre_close & LWSIFS_NOT_EST))
  128. #define lwsi_state_can_handle_POLLOUT(wsi) (wsi->wsistate & LWSIFS_POCB)
  129. #if !defined (_DEBUG)
  130. #define lwsi_set_state(wsi, lrs) wsi->wsistate = \
  131. (wsi->wsistate & (~LRS_MASK)) | lrs
  132. #else
  133. void lwsi_set_state(struct lws *wsi, lws_wsi_state_t lrs);
  134. #endif
  135. /*
  136. * internal role-specific ops
  137. */
  138. struct lws_context_per_thread;
  139. struct lws_role_ops {
  140. const char *name;
  141. const char *alpn;
  142. /*
  143. * After http headers have parsed, this is the last chance for a role
  144. * to upgrade the connection to something else using the headers.
  145. * ws-over-h2 is upgraded from h2 like this.
  146. */
  147. int (*check_upgrades)(struct lws *wsi);
  148. /* role-specific context init during context creation */
  149. int (*init_context)(struct lws_context *context,
  150. const struct lws_context_creation_info *info);
  151. /* role-specific per-vhost init during vhost creation */
  152. int (*init_vhost)(struct lws_vhost *vh,
  153. const struct lws_context_creation_info *info);
  154. /* role-specific per-vhost destructor during vhost destroy */
  155. int (*destroy_vhost)(struct lws_vhost *vh);
  156. /* generic 1Hz callback for the role itself */
  157. int (*periodic_checks)(struct lws_context *context, int tsi,
  158. time_t now);
  159. /* chance for the role to force POLLIN without network activity */
  160. int (*service_flag_pending)(struct lws_context *context, int tsi);
  161. /* an fd using this role has POLLIN signalled */
  162. int (*handle_POLLIN)(struct lws_context_per_thread *pt, struct lws *wsi,
  163. struct lws_pollfd *pollfd);
  164. /* an fd using the role wanted a POLLOUT callback and now has it */
  165. int (*handle_POLLOUT)(struct lws *wsi);
  166. /* perform user pollout */
  167. int (*perform_user_POLLOUT)(struct lws *wsi);
  168. /* do effective callback on writeable */
  169. int (*callback_on_writable)(struct lws *wsi);
  170. /* connection-specific tx credit in bytes */
  171. lws_fileofs_t (*tx_credit)(struct lws *wsi);
  172. /* role-specific write formatting */
  173. int (*write_role_protocol)(struct lws *wsi, unsigned char *buf,
  174. size_t len, enum lws_write_protocol *wp);
  175. /* get encapsulation parent */
  176. struct lws * (*encapsulation_parent)(struct lws *wsi);
  177. /* role-specific destructor */
  178. int (*alpn_negotiated)(struct lws *wsi, const char *alpn);
  179. /* chance for the role to handle close in the protocol */
  180. int (*close_via_role_protocol)(struct lws *wsi,
  181. enum lws_close_status reason);
  182. /* role-specific close processing */
  183. int (*close_role)(struct lws_context_per_thread *pt, struct lws *wsi);
  184. /* role-specific connection close processing */
  185. int (*close_kill_connection)(struct lws *wsi,
  186. enum lws_close_status reason);
  187. /* role-specific destructor */
  188. int (*destroy_role)(struct lws *wsi);
  189. /*
  190. * the callback reasons for WRITEABLE for client, server
  191. * (just client applies if no concept of client or server)
  192. */
  193. uint16_t writeable_cb[2];
  194. /*
  195. * the callback reasons for CLOSE for client, server
  196. * (just client applies if no concept of client or server)
  197. */
  198. uint16_t close_cb[2];
  199. unsigned int file_handle:1; /* role operates on files not sockets */
  200. };
  201. /* core roles */
  202. extern struct lws_role_ops role_ops_raw_skt, role_ops_raw_file, role_ops_listen,
  203. role_ops_pipe;
  204. /* bring in role private declarations */
  205. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  206. #include "roles/http/private.h"
  207. #else
  208. #define lwsi_role_http(wsi) (0)
  209. #endif
  210. #if defined(LWS_ROLE_H1)
  211. #include "roles/h1/private.h"
  212. #else
  213. #define lwsi_role_h1(wsi) (0)
  214. #endif
  215. #if defined(LWS_ROLE_H2)
  216. #include "roles/h2/private.h"
  217. #else
  218. #define lwsi_role_h2(wsi) (0)
  219. #endif
  220. #if defined(LWS_ROLE_WS)
  221. #include "roles/ws/private.h"
  222. #else
  223. #define lwsi_role_ws(wsi) (0)
  224. #endif
  225. #if defined(LWS_ROLE_CGI)
  226. #include "roles/cgi/private.h"
  227. #else
  228. #define lwsi_role_cgi(wsi) (0)
  229. #endif
  230. enum {
  231. LWS_HP_RET_BAIL_OK,
  232. LWS_HP_RET_BAIL_DIE,
  233. LWS_HP_RET_USER_SERVICE,
  234. LWS_HPI_RET_WSI_ALREADY_DIED, /* we closed it */
  235. LWS_HPI_RET_HANDLED, /* no probs */
  236. LWS_HPI_RET_PLEASE_CLOSE_ME, /* close it for us */
  237. LWS_UPG_RET_DONE,
  238. LWS_UPG_RET_CONTINUE,
  239. LWS_UPG_RET_BAIL
  240. };