wslay.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * Wslay - The WebSocket Library
  3. *
  4. * Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef WSLAY_H
  26. #define WSLAY_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <sys/types.h>
  33. /*
  34. * wslay/wslayver.h is generated from wslay/wslayver.h.in by
  35. * configure. The projects which do not use autotools can set
  36. * WSLAY_VERSION macro from outside to avoid to generating wslayver.h
  37. */
  38. #ifndef WSLAY_VERSION
  39. # include <wslay/wslayver.h>
  40. #endif /* WSLAY_VERSION */
  41. enum wslay_error {
  42. WSLAY_ERR_WANT_READ = -100,
  43. WSLAY_ERR_WANT_WRITE = -101,
  44. WSLAY_ERR_PROTO = -200,
  45. WSLAY_ERR_INVALID_ARGUMENT = -300,
  46. WSLAY_ERR_INVALID_CALLBACK = -301,
  47. WSLAY_ERR_NO_MORE_MSG = -302,
  48. WSLAY_ERR_CALLBACK_FAILURE = -400,
  49. WSLAY_ERR_WOULDBLOCK = -401,
  50. WSLAY_ERR_NOMEM = -500
  51. };
  52. /*
  53. * Status codes defined in RFC6455
  54. */
  55. enum wslay_status_code {
  56. WSLAY_CODE_NORMAL_CLOSURE = 1000,
  57. WSLAY_CODE_GOING_AWAY = 1001,
  58. WSLAY_CODE_PROTOCOL_ERROR = 1002,
  59. WSLAY_CODE_UNSUPPORTED_DATA = 1003,
  60. WSLAY_CODE_NO_STATUS_RCVD = 1005,
  61. WSLAY_CODE_ABNORMAL_CLOSURE = 1006,
  62. WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA = 1007,
  63. WSLAY_CODE_POLICY_VIOLATION = 1008,
  64. WSLAY_CODE_MESSAGE_TOO_BIG = 1009,
  65. WSLAY_CODE_MANDATORY_EXT = 1010,
  66. WSLAY_CODE_INTERNAL_SERVER_ERROR = 1011,
  67. WSLAY_CODE_TLS_HANDSHAKE = 1015
  68. };
  69. enum wslay_io_flags {
  70. /*
  71. * There is more data to send.
  72. */
  73. WSLAY_MSG_MORE = 1
  74. };
  75. /*
  76. * Callback function used by wslay_frame_send() function when it needs
  77. * to send data. The implementation of this function must send at most
  78. * len bytes of data in data. flags is the bitwise OR of zero or more
  79. * of the following flag:
  80. *
  81. * WSLAY_MSG_MORE
  82. * There is more data to send
  83. *
  84. * It provides some hints to tune performance and behaviour. user_data
  85. * is one given in wslay_frame_context_init() function. The
  86. * implementation of this function must return the number of bytes
  87. * sent. If there is an error, return -1. The return value 0 is also
  88. * treated an error by the library.
  89. */
  90. typedef ssize_t (*wslay_frame_send_callback)(const uint8_t *data, size_t len,
  91. int flags, void *user_data);
  92. /*
  93. * Callback function used by wslay_frame_recv() function when it needs
  94. * more data. The implementation of this function must fill at most
  95. * len bytes of data into buf. The memory area of buf is allocated by
  96. * library and not be freed by the application code. flags is always 0
  97. * in this version. user_data is one given in
  98. * wslay_frame_context_init() function. The implementation of this
  99. * function must return the number of bytes filled. If there is an
  100. * error, return -1. The return value 0 is also treated an error by
  101. * the library.
  102. */
  103. typedef ssize_t (*wslay_frame_recv_callback)(uint8_t *buf, size_t len,
  104. int flags, void *user_data);
  105. /*
  106. * Callback function used by wslay_frame_send() function when it needs
  107. * new mask key. The implementation of this function must write
  108. * exactly len bytes of mask key to buf. user_data is one given in
  109. * wslay_frame_context_init() function. The implementation of this
  110. * function return 0 on success. If there is an error, return -1.
  111. */
  112. typedef int (*wslay_frame_genmask_callback)(uint8_t *buf, size_t len,
  113. void *user_data);
  114. struct wslay_frame_callbacks {
  115. wslay_frame_send_callback send_callback;
  116. wslay_frame_recv_callback recv_callback;
  117. wslay_frame_genmask_callback genmask_callback;
  118. };
  119. /*
  120. * The opcode defined in RFC6455.
  121. */
  122. enum wslay_opcode {
  123. WSLAY_CONTINUATION_FRAME = 0x0u,
  124. WSLAY_TEXT_FRAME = 0x1u,
  125. WSLAY_BINARY_FRAME = 0x2u,
  126. WSLAY_CONNECTION_CLOSE = 0x8u,
  127. WSLAY_PING = 0x9u,
  128. WSLAY_PONG = 0xau
  129. };
  130. /*
  131. * Macro that returns 1 if opcode is control frame opcode, otherwise
  132. * returns 0.
  133. */
  134. #define wslay_is_ctrl_frame(opcode) ((opcode >> 3) & 1)
  135. /*
  136. * Macros that represent and return reserved bits: RSV1, RSV2, RSV3.
  137. * These macros assume that rsv is constructed by ((RSV1 << 2) |
  138. * (RSV2 << 1) | RSV3)
  139. */
  140. #define WSLAY_RSV_NONE ((uint8_t) 0)
  141. #define WSLAY_RSV1_BIT (((uint8_t) 1) << 2)
  142. #define WSLAY_RSV2_BIT (((uint8_t) 1) << 1)
  143. #define WSLAY_RSV3_BIT (((uint8_t) 1) << 0)
  144. #define wslay_get_rsv1(rsv) ((rsv >> 2) & 1)
  145. #define wslay_get_rsv2(rsv) ((rsv >> 1) & 1)
  146. #define wslay_get_rsv3(rsv) (rsv & 1)
  147. struct wslay_frame_iocb {
  148. /* 1 for fragmented final frame, 0 for otherwise */
  149. uint8_t fin;
  150. /*
  151. * reserved 3 bits. rsv = ((RSV1 << 2) | (RSV << 1) | RSV3).
  152. * RFC6455 requires 0 unless extensions are negotiated.
  153. */
  154. uint8_t rsv;
  155. /* 4 bit opcode */
  156. uint8_t opcode;
  157. /* payload length [0, 2**63-1] */
  158. uint64_t payload_length;
  159. /* 1 for masked frame, 0 for unmasked */
  160. uint8_t mask;
  161. /* part of payload data */
  162. const uint8_t *data;
  163. /* bytes of data defined above */
  164. size_t data_length;
  165. };
  166. struct wslay_frame_context;
  167. typedef struct wslay_frame_context *wslay_frame_context_ptr;
  168. /*
  169. * Initializes ctx using given callbacks and user_data. This function
  170. * allocates memory for struct wslay_frame_context and stores the
  171. * result to *ctx. The callback functions specified in callbacks are
  172. * copied to ctx. user_data is stored in ctx and it will be passed to
  173. * callback functions. When the user code finished using ctx, it must
  174. * call wslay_frame_context_free to deallocate memory.
  175. */
  176. int wslay_frame_context_init(wslay_frame_context_ptr *ctx,
  177. const struct wslay_frame_callbacks *callbacks,
  178. void *user_data);
  179. /*
  180. * Deallocates memory pointed by ctx.
  181. */
  182. void wslay_frame_context_free(wslay_frame_context_ptr ctx);
  183. /*
  184. * Send WebSocket frame specified in iocb. ctx must be initialized
  185. * using wslay_frame_context_init() function. iocb->fin must be 1 if
  186. * this is a fin frame, otherwise 0. iocb->rsv is reserved bits.
  187. * iocb->opcode must be the opcode of this frame. iocb->mask must be
  188. * 1 if this is masked frame, otherwise 0. iocb->payload_length is
  189. * the payload_length of this frame. iocb->data must point to the
  190. * payload data to be sent. iocb->data_length must be the length of
  191. * the data. This function calls send_callback function if it needs
  192. * to send bytes. This function calls gen_mask_callback function if
  193. * it needs new mask key. This function returns the number of payload
  194. * bytes sent. Please note that it does not include any number of
  195. * header bytes. If it cannot send any single bytes of payload, it
  196. * returns WSLAY_ERR_WANT_WRITE. If the library detects error in iocb,
  197. * this function returns WSLAY_ERR_INVALID_ARGUMENT. If callback
  198. * functions report a failure, this function returns
  199. * WSLAY_ERR_INVALID_CALLBACK. This function does not always send all
  200. * given data in iocb. If there are remaining data to be sent, adjust
  201. * data and data_length in iocb accordingly and call this function
  202. * again.
  203. */
  204. ssize_t wslay_frame_send(wslay_frame_context_ptr ctx,
  205. struct wslay_frame_iocb *iocb);
  206. /*
  207. * Receives WebSocket frame and stores it in iocb. This function
  208. * returns the number of payload bytes received. This does not
  209. * include header bytes. In this case, iocb will be populated as
  210. * follows: iocb->fin is 1 if received frame is fin frame, otherwise
  211. * 0. iocb->rsv is reserved bits of received frame. iocb->opcode is
  212. * opcode of received frame. iocb->mask is 1 if received frame is
  213. * masked, otherwise 0. iocb->payload_length is the payload length of
  214. * received frame. iocb->data is pointed to the buffer containing
  215. * received payload data. This buffer is allocated by the library and
  216. * must be read-only. iocb->data_length is the number of payload
  217. * bytes recieved. This function calls recv_callback if it needs to
  218. * receive additional bytes. If it cannot receive any single bytes of
  219. * payload, it returns WSLAY_ERR_WANT_READ. If the library detects
  220. * protocol violation in a received frame, this function returns
  221. * WSLAY_ERR_PROTO. If callback functions report a failure, this
  222. * function returns WSLAY_ERR_INVALID_CALLBACK. This function does
  223. * not always receive whole frame in a single call. If there are
  224. * remaining data to be received, call this function again. This
  225. * function ensures frame alignment.
  226. */
  227. ssize_t wslay_frame_recv(wslay_frame_context_ptr ctx,
  228. struct wslay_frame_iocb *iocb);
  229. struct wslay_event_context;
  230. /* Pointer to the event-based API context */
  231. typedef struct wslay_event_context *wslay_event_context_ptr;
  232. struct wslay_event_on_msg_recv_arg {
  233. /* reserved bits: rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3 */
  234. uint8_t rsv;
  235. /* opcode */
  236. uint8_t opcode;
  237. /* received message */
  238. const uint8_t *msg;
  239. /* message length */
  240. size_t msg_length;
  241. /*
  242. * Status code iff opcode == WSLAY_CONNECTION_CLOSE. If no status
  243. * code is included in the close control frame, it is set to 0.
  244. */
  245. uint16_t status_code;
  246. };
  247. /*
  248. * Callback function invoked by wslay_event_recv() when a message is
  249. * completely received.
  250. */
  251. typedef void (*wslay_event_on_msg_recv_callback)
  252. (wslay_event_context_ptr ctx,
  253. const struct wslay_event_on_msg_recv_arg *arg, void *user_data);
  254. struct wslay_event_on_frame_recv_start_arg {
  255. /* fin bit; 1 for final frame, or 0. */
  256. uint8_t fin;
  257. /* reserved bits: rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3 */
  258. uint8_t rsv;
  259. /* opcode of the frame */
  260. uint8_t opcode;
  261. /* payload length of ths frame */
  262. uint64_t payload_length;
  263. };
  264. /*
  265. * Callback function invoked by wslay_event_recv() when a new frame
  266. * starts to be received. This callback function is only invoked once
  267. * for each frame.
  268. */
  269. typedef void (*wslay_event_on_frame_recv_start_callback)
  270. (wslay_event_context_ptr ctx,
  271. const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data);
  272. struct wslay_event_on_frame_recv_chunk_arg {
  273. /* chunk of payload data */
  274. const uint8_t *data;
  275. /* length of data */
  276. size_t data_length;
  277. };
  278. /*
  279. * Callback function invoked by wslay_event_recv() when a chunk of
  280. * frame payload is received.
  281. */
  282. typedef void (*wslay_event_on_frame_recv_chunk_callback)
  283. (wslay_event_context_ptr ctx,
  284. const struct wslay_event_on_frame_recv_chunk_arg *arg, void *user_data);
  285. /*
  286. * Callback function invoked by wslay_event_recv() when a frame is
  287. * completely received.
  288. */
  289. typedef void (*wslay_event_on_frame_recv_end_callback)
  290. (wslay_event_context_ptr ctx, void *user_data);
  291. /*
  292. * Callback function invoked by wslay_event_recv() when it wants to
  293. * receive more data from peer. The implementation of this callback
  294. * function must read data at most len bytes from peer and store them
  295. * in buf and return the number of bytes read. flags is always 0 in
  296. * this version.
  297. *
  298. * If there is an error, return -1 and set error code
  299. * WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay
  300. * event-based API on the whole assumes non-blocking I/O. If the cause
  301. * of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK
  302. * instead. This is important because it tells wslay_event_recv() to
  303. * stop receiving further data and return.
  304. */
  305. typedef ssize_t (*wslay_event_recv_callback)(wslay_event_context_ptr ctx,
  306. uint8_t *buf, size_t len,
  307. int flags, void *user_data);
  308. /*
  309. * Callback function invoked by wslay_event_send() when it wants to
  310. * send more data to peer. The implementation of this callback
  311. * function must send data at most len bytes to peer and return the
  312. * number of bytes sent. flags is the bitwise OR of zero or more of
  313. * the following flag:
  314. *
  315. * WSLAY_MSG_MORE
  316. * There is more data to send
  317. *
  318. * It provides some hints to tune performance and behaviour.
  319. *
  320. * If there is an error, return -1 and set error code
  321. * WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay
  322. * event-based API on the whole assumes non-blocking I/O. If the cause
  323. * of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK
  324. * instead. This is important because it tells wslay_event_send() to
  325. * stop sending data and return.
  326. */
  327. typedef ssize_t (*wslay_event_send_callback)(wslay_event_context_ptr ctx,
  328. const uint8_t *data, size_t len,
  329. int flags, void *user_data);
  330. /*
  331. * Callback function invoked by wslay_event_send() when it wants new
  332. * mask key. As described in RFC6455, only the traffic from WebSocket
  333. * client is masked, so this callback function is only needed if an
  334. * event-based API is initialized for WebSocket client use.
  335. */
  336. typedef int (*wslay_event_genmask_callback)(wslay_event_context_ptr ctx,
  337. uint8_t *buf, size_t len,
  338. void *user_data);
  339. struct wslay_event_callbacks {
  340. wslay_event_recv_callback recv_callback;
  341. wslay_event_send_callback send_callback;
  342. wslay_event_genmask_callback genmask_callback;
  343. wslay_event_on_frame_recv_start_callback on_frame_recv_start_callback;
  344. wslay_event_on_frame_recv_chunk_callback on_frame_recv_chunk_callback;
  345. wslay_event_on_frame_recv_end_callback on_frame_recv_end_callback;
  346. wslay_event_on_msg_recv_callback on_msg_recv_callback;
  347. };
  348. /*
  349. * Initializes ctx as WebSocket Server. user_data is an arbitrary
  350. * pointer, which is directly passed to each callback functions as
  351. * user_data argument.
  352. *
  353. * On success, returns 0. On error, returns one of following negative
  354. * values:
  355. *
  356. * WSLAY_ERR_NOMEM
  357. * Out of memory.
  358. */
  359. int wslay_event_context_server_init
  360. (wslay_event_context_ptr *ctx,
  361. const struct wslay_event_callbacks *callbacks, void *user_data);
  362. /*
  363. * Initializes ctx as WebSocket client. user_data is an arbitrary
  364. * pointer, which is directly passed to each callback functions as
  365. * user_data argument.
  366. *
  367. * On success, returns 0. On error, returns one of following negative
  368. * values:
  369. *
  370. * WSLAY_ERR_NOMEM
  371. * Out of memory.
  372. */
  373. int wslay_event_context_client_init
  374. (wslay_event_context_ptr *ctx,
  375. const struct wslay_event_callbacks *callbacks, void *user_data);
  376. /*
  377. * Releases allocated resources for ctx.
  378. */
  379. void wslay_event_context_free(wslay_event_context_ptr ctx);
  380. /*
  381. * Sets a bit mask of allowed reserved bits.
  382. * Currently only permitted values are WSLAY_RSV1_BIT to allow PMCE
  383. * extension (see RFC-7692) or WSLAY_RSV_NONE to disable.
  384. *
  385. * Default: WSLAY_RSV_NONE
  386. */
  387. void wslay_event_config_set_allowed_rsv_bits(wslay_event_context_ptr ctx,
  388. uint8_t rsv);
  389. /*
  390. * Enables or disables buffering of an entire message for non-control
  391. * frames. If val is 0, buffering is enabled. Otherwise, buffering is
  392. * disabled. If wslay_event_on_msg_recv_callback is invoked when
  393. * buffering is disabled, the msg_length member of struct
  394. * wslay_event_on_msg_recv_arg is set to 0.
  395. *
  396. * The control frames are always buffered regardless of this function call.
  397. *
  398. * This function must not be used after the first invocation of
  399. * wslay_event_recv() function.
  400. */
  401. void wslay_event_config_set_no_buffering(wslay_event_context_ptr ctx, int val);
  402. /*
  403. * Sets maximum length of a message that can be received. The length
  404. * of message is checked by wslay_event_recv() function. If the length
  405. * of a message is larger than this value, reading operation is
  406. * disabled (same effect with wslay_event_shutdown_read() call) and
  407. * close control frame with WSLAY_CODE_MESSAGE_TOO_BIG is queued. If
  408. * buffering for non-control frames is disabled, the library checks
  409. * each frame payload length and does not check length of entire
  410. * message.
  411. *
  412. * The default value is (1u << 31)-1.
  413. */
  414. void wslay_event_config_set_max_recv_msg_length(wslay_event_context_ptr ctx,
  415. uint64_t val);
  416. /*
  417. * Sets callbacks to ctx. The callbacks previouly set by this function
  418. * or wslay_event_context_server_init() or
  419. * wslay_event_context_client_init() are replaced with callbacks.
  420. */
  421. void wslay_event_config_set_callbacks
  422. (wslay_event_context_ptr ctx, const struct wslay_event_callbacks *callbacks);
  423. /*
  424. * Receives messages from peer. When receiving
  425. * messages, it uses wslay_event_recv_callback function. Single call
  426. * of this function receives multiple messages until
  427. * wslay_event_recv_callback function sets error code
  428. * WSLAY_ERR_WOULDBLOCK.
  429. *
  430. * When close control frame is received, this function automatically
  431. * queues close control frame. Also this function calls
  432. * wslay_event_set_read_enabled() with second argument 0 to disable
  433. * further read from peer.
  434. *
  435. * When ping control frame is received, this function automatically
  436. * queues pong control frame.
  437. *
  438. * In case of a fatal errror which leads to negative return code, this
  439. * function calls wslay_event_set_read_enabled() with second argument
  440. * 0 to disable further read from peer.
  441. *
  442. * wslay_event_recv() returns 0 if it succeeds, or one of the
  443. * following negative error codes:
  444. *
  445. * WSLAY_ERR_CALLBACK_FAILURE
  446. * User defined callback function is failed.
  447. *
  448. * WSLAY_ERR_NOMEM
  449. * Out of memory.
  450. *
  451. * When negative error code is returned, application must not make any
  452. * further call of wslay_event_recv() and must close WebSocket
  453. * connection.
  454. */
  455. int wslay_event_recv(wslay_event_context_ptr ctx);
  456. /*
  457. * Sends queued messages to peer. When sending a
  458. * message, it uses wslay_event_send_callback function. Single call of
  459. * wslay_event_send() sends multiple messages until
  460. * wslay_event_send_callback sets error code WSLAY_ERR_WOULDBLOCK.
  461. *
  462. * If ctx is initialized for WebSocket client use, wslay_event_send()
  463. * uses wslay_event_genmask_callback to get new mask key.
  464. *
  465. * When a message queued using wslay_event_queue_fragmented_msg() is
  466. * sent, wslay_event_send() invokes
  467. * wslay_event_fragmented_msg_callback for that message.
  468. *
  469. * After close control frame is sent, this function calls
  470. * wslay_event_set_write_enabled() with second argument 0 to disable
  471. * further transmission to peer.
  472. *
  473. * If there are any pending messages, wslay_event_want_write() returns
  474. * 1, otherwise returns 0.
  475. *
  476. * In case of a fatal errror which leads to negative return code, this
  477. * function calls wslay_event_set_write_enabled() with second argument
  478. * 0 to disable further transmission to peer.
  479. *
  480. * wslay_event_send() returns 0 if it succeeds, or one of the
  481. * following negative error codes:
  482. *
  483. * WSLAY_ERR_CALLBACK_FAILURE
  484. * User defined callback function is failed.
  485. *
  486. * WSLAY_ERR_NOMEM
  487. * Out of memory.
  488. *
  489. * When negative error code is returned, application must not make any
  490. * further call of wslay_event_send() and must close WebSocket
  491. * connection.
  492. */
  493. int wslay_event_send(wslay_event_context_ptr ctx);
  494. struct wslay_event_msg {
  495. uint8_t opcode;
  496. const uint8_t *msg;
  497. size_t msg_length;
  498. };
  499. /*
  500. * Queues message specified in arg.
  501. *
  502. * This function supports both control and non-control messages and
  503. * the given message is sent without fragmentation. If fragmentation
  504. * is needed, use wslay_event_queue_fragmented_msg() function instead.
  505. *
  506. * This function just queues a message and does not send
  507. * it. wslay_event_send() function call sends these queued messages.
  508. *
  509. * wslay_event_queue_msg() returns 0 if it succeeds, or returns the
  510. * following negative error codes:
  511. *
  512. * WSLAY_ERR_NO_MORE_MSG
  513. * Could not queue given message. The one of possible reason is that
  514. * close control frame has been queued/sent and no further queueing
  515. * message is not allowed.
  516. *
  517. * WSLAY_ERR_INVALID_ARGUMENT
  518. * The given message is invalid.
  519. *
  520. * WSLAY_ERR_NOMEM
  521. * Out of memory.
  522. */
  523. int wslay_event_queue_msg(wslay_event_context_ptr ctx,
  524. const struct wslay_event_msg *arg);
  525. /*
  526. * Extended version of wslay_event_queue_msg which allows to set reserved bits.
  527. */
  528. int wslay_event_queue_msg_ex(wslay_event_context_ptr ctx,
  529. const struct wslay_event_msg *arg, uint8_t rsv);
  530. /*
  531. * Specify "source" to generate message.
  532. */
  533. union wslay_event_msg_source {
  534. int fd;
  535. void *data;
  536. };
  537. /*
  538. * Callback function called by wslay_event_send() to read message data
  539. * from source. The implementation of
  540. * wslay_event_fragmented_msg_callback must store at most len bytes of
  541. * data to buf and return the number of stored bytes. If all data is
  542. * read (i.e., EOF), set *eof to 1. If no data can be generated at the
  543. * moment, return 0. If there is an error, return -1 and set error
  544. * code WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error().
  545. */
  546. typedef ssize_t (*wslay_event_fragmented_msg_callback)
  547. (wslay_event_context_ptr ctx,
  548. uint8_t *buf, size_t len, const union wslay_event_msg_source *source,
  549. int *eof, void *user_data);
  550. struct wslay_event_fragmented_msg {
  551. /* opcode */
  552. uint8_t opcode;
  553. /* "source" to generate message data */
  554. union wslay_event_msg_source source;
  555. /* Callback function to read message data from source. */
  556. wslay_event_fragmented_msg_callback read_callback;
  557. };
  558. /*
  559. * Queues a fragmented message specified in arg.
  560. *
  561. * This function supports non-control messages only. For control frames,
  562. * use wslay_event_queue_msg() or wslay_event_queue_close().
  563. *
  564. * This function just queues a message and does not send
  565. * it. wslay_event_send() function call sends these queued messages.
  566. *
  567. * wslay_event_queue_fragmented_msg() returns 0 if it succeeds, or
  568. * returns the following negative error codes:
  569. *
  570. * WSLAY_ERR_NO_MORE_MSG
  571. * Could not queue given message. The one of possible reason is that
  572. * close control frame has been queued/sent and no further queueing
  573. * message is not allowed.
  574. *
  575. * WSLAY_ERR_INVALID_ARGUMENT
  576. * The given message is invalid.
  577. *
  578. * WSLAY_ERR_NOMEM
  579. * Out of memory.
  580. */
  581. int wslay_event_queue_fragmented_msg
  582. (wslay_event_context_ptr ctx, const struct wslay_event_fragmented_msg *arg);
  583. /*
  584. * Extended version of wslay_event_queue_fragmented_msg which allows to set
  585. * reserved bits.
  586. */
  587. int wslay_event_queue_fragmented_msg_ex(wslay_event_context_ptr ctx,
  588. const struct wslay_event_fragmented_msg *arg, uint8_t rsv);
  589. /*
  590. * Queues close control frame. This function is provided just for
  591. * convenience. wslay_event_queue_msg() can queue a close control
  592. * frame as well. status_code is the status code of close control
  593. * frame. reason is the close reason encoded in UTF-8. reason_length
  594. * is the length of reason in bytes. reason_length must be less than
  595. * 123 bytes.
  596. *
  597. * If status_code is 0, reason and reason_length is not used and close
  598. * control frame with zero-length payload will be queued.
  599. *
  600. * This function just queues a message and does not send
  601. * it. wslay_event_send() function call sends these queued messages.
  602. *
  603. * wslay_event_queue_close() returns 0 if it succeeds, or returns the
  604. * following negative error codes:
  605. *
  606. * WSLAY_ERR_NO_MORE_MSG
  607. * Could not queue given message. The one of possible reason is that
  608. * close control frame has been queued/sent and no further queueing
  609. * message is not allowed.
  610. *
  611. * WSLAY_ERR_INVALID_ARGUMENT
  612. * The given message is invalid.
  613. *
  614. * WSLAY_ERR_NOMEM
  615. * Out of memory.
  616. */
  617. int wslay_event_queue_close(wslay_event_context_ptr ctx,
  618. uint16_t status_code,
  619. const uint8_t *reason, size_t reason_length);
  620. /*
  621. * Sets error code to tell the library there is an error. This
  622. * function is typically used in user defined callback functions. See
  623. * the description of callback function to know which error code
  624. * should be used.
  625. */
  626. void wslay_event_set_error(wslay_event_context_ptr ctx, int val);
  627. /*
  628. * Query whehter the library want to read more data from peer.
  629. *
  630. * wslay_event_want_read() returns 1 if the library want to read more
  631. * data from peer, or returns 0.
  632. */
  633. int wslay_event_want_read(wslay_event_context_ptr ctx);
  634. /*
  635. * Query whehter the library want to send more data to peer.
  636. *
  637. * wslay_event_want_write() returns 1 if the library want to send more
  638. * data to peer, or returns 0.
  639. */
  640. int wslay_event_want_write(wslay_event_context_ptr ctx);
  641. /*
  642. * Prevents the event-based API context from reading any further data
  643. * from peer.
  644. *
  645. * This function may be used with wslay_event_queue_close() if the
  646. * application detects error in the data received and wants to fail
  647. * WebSocket connection.
  648. */
  649. void wslay_event_shutdown_read(wslay_event_context_ptr ctx);
  650. /*
  651. * Prevents the event-based API context from sending any further data
  652. * to peer.
  653. */
  654. void wslay_event_shutdown_write(wslay_event_context_ptr ctx);
  655. /*
  656. * Returns 1 if the event-based API context allows read operation, or
  657. * return 0.
  658. *
  659. * After wslay_event_shutdown_read() is called,
  660. * wslay_event_get_read_enabled() returns 0.
  661. */
  662. int wslay_event_get_read_enabled(wslay_event_context_ptr ctx);
  663. /*
  664. * Returns 1 if the event-based API context allows write operation, or
  665. * return 0.
  666. *
  667. * After wslay_event_shutdown_write() is called,
  668. * wslay_event_get_write_enabled() returns 0.
  669. */
  670. int wslay_event_get_write_enabled(wslay_event_context_ptr ctx);
  671. /*
  672. * Returns 1 if a close control frame has been received from peer, or
  673. * returns 0.
  674. */
  675. int wslay_event_get_close_received(wslay_event_context_ptr ctx);
  676. /*
  677. * Returns 1 if a close control frame has been sent to peer, or
  678. * returns 0.
  679. */
  680. int wslay_event_get_close_sent(wslay_event_context_ptr ctx);
  681. /*
  682. * Returns status code received in close control frame. If no close
  683. * control frame has not been received, returns
  684. * WSLAY_CODE_ABNORMAL_CLOSURE. If received close control frame has no
  685. * status code, returns WSLAY_CODE_NO_STATUS_RCVD.
  686. */
  687. uint16_t wslay_event_get_status_code_received(wslay_event_context_ptr ctx);
  688. /*
  689. * Returns status code sent in close control frame. If no close
  690. * control frame has not been sent, returns
  691. * WSLAY_CODE_ABNORMAL_CLOSURE. If sent close control frame has no
  692. * status code, returns WSLAY_CODE_NO_STATUS_RCVD.
  693. */
  694. uint16_t wslay_event_get_status_code_sent(wslay_event_context_ptr ctx);
  695. /*
  696. * Returns the number of queued messages.
  697. */
  698. size_t wslay_event_get_queued_msg_count(wslay_event_context_ptr ctx);
  699. /*
  700. * Returns the sum of queued message length. It only counts the
  701. * message length queued using wslay_event_queue_msg() or
  702. * wslay_event_queue_close().
  703. */
  704. size_t wslay_event_get_queued_msg_length(wslay_event_context_ptr ctx);
  705. #ifdef __cplusplus
  706. }
  707. #endif
  708. #endif /* WSLAY_H */