wslay_event.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. #include "wslay_event.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include "wslay_queue.h"
  30. #include "wslay_frame.h"
  31. #include "wslay_net.h"
  32. /* Start of utf8 dfa */
  33. /* Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  34. * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  35. *
  36. * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  37. *
  38. * Permission is hereby granted, free of charge, to any person
  39. * obtaining a copy of this software and associated documentation
  40. * files (the "Software"), to deal in the Software without
  41. * restriction, including without limitation the rights to use, copy,
  42. * modify, merge, publish, distribute, sublicense, and/or sell copies
  43. * of the Software, and to permit persons to whom the Software is
  44. * furnished to do so, subject to the following conditions:
  45. *
  46. * The above copyright notice and this permission notice shall be
  47. * included in all copies or substantial portions of the Software.
  48. *
  49. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  50. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  51. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  52. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  53. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  54. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  55. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  56. * SOFTWARE.
  57. */
  58. #define UTF8_ACCEPT 0
  59. #define UTF8_REJECT 12
  60. static const uint8_t utf8d[] = {
  61. /*
  62. * The first part of the table maps bytes to character classes that
  63. * to reduce the size of the transition table and create bitmasks.
  64. */
  65. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  66. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  67. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  68. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  69. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  70. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  71. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  72. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  73. /*
  74. * The second part is a transition table that maps a combination
  75. * of a state of the automaton and a character class to a state.
  76. */
  77. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  78. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  79. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  80. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  81. 12,36,12,12,12,12,12,12,12,12,12,12,
  82. };
  83. static uint32_t
  84. decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
  85. uint32_t type = utf8d[byte];
  86. *codep = (*state != UTF8_ACCEPT) ?
  87. (byte & 0x3fu) | (*codep << 6) :
  88. (0xff >> type) & (byte);
  89. *state = utf8d[256 + *state + type];
  90. return *state;
  91. }
  92. /* End of utf8 dfa */
  93. static ssize_t wslay_event_frame_recv_callback(uint8_t *buf, size_t len,
  94. int flags, void *user_data)
  95. {
  96. struct wslay_event_frame_user_data *e =
  97. (struct wslay_event_frame_user_data*)user_data;
  98. return e->ctx->callbacks.recv_callback(e->ctx, buf, len, flags, e->user_data);
  99. }
  100. static ssize_t wslay_event_frame_send_callback(const uint8_t *data, size_t len,
  101. int flags, void *user_data)
  102. {
  103. struct wslay_event_frame_user_data *e =
  104. (struct wslay_event_frame_user_data*)user_data;
  105. return e->ctx->callbacks.send_callback(e->ctx, data, len, flags,
  106. e->user_data);
  107. }
  108. static int wslay_event_frame_genmask_callback(uint8_t *buf, size_t len,
  109. void *user_data)
  110. {
  111. struct wslay_event_frame_user_data *e =
  112. (struct wslay_event_frame_user_data*)user_data;
  113. return e->ctx->callbacks.genmask_callback(e->ctx, buf, len, e->user_data);
  114. }
  115. static int wslay_event_byte_chunk_init
  116. (struct wslay_event_byte_chunk **chunk, size_t len)
  117. {
  118. *chunk = (struct wslay_event_byte_chunk*)malloc
  119. (sizeof(struct wslay_event_byte_chunk));
  120. if(*chunk == NULL) {
  121. return WSLAY_ERR_NOMEM;
  122. }
  123. memset(*chunk, 0, sizeof(struct wslay_event_byte_chunk));
  124. if(len) {
  125. (*chunk)->data = (uint8_t*)malloc(len);
  126. if((*chunk)->data == NULL) {
  127. free(*chunk);
  128. return WSLAY_ERR_NOMEM;
  129. }
  130. (*chunk)->data_length = len;
  131. }
  132. return 0;
  133. }
  134. static void wslay_event_byte_chunk_free(struct wslay_event_byte_chunk *c)
  135. {
  136. if(!c) {
  137. return;
  138. }
  139. free(c->data);
  140. free(c);
  141. }
  142. static void wslay_event_byte_chunk_copy(struct wslay_event_byte_chunk *c,
  143. size_t off,
  144. const uint8_t *data, size_t data_length)
  145. {
  146. memcpy(c->data+off, data, data_length);
  147. }
  148. static void wslay_event_imsg_set(struct wslay_event_imsg *m,
  149. uint8_t fin, uint8_t rsv, uint8_t opcode)
  150. {
  151. m->fin = fin;
  152. m->rsv = rsv;
  153. m->opcode = opcode;
  154. m->msg_length = 0;
  155. }
  156. static void wslay_event_imsg_chunks_free(struct wslay_event_imsg *m)
  157. {
  158. if(!m->chunks) {
  159. return;
  160. }
  161. while(!wslay_queue_empty(m->chunks)) {
  162. wslay_event_byte_chunk_free(wslay_queue_top(m->chunks));
  163. wslay_queue_pop(m->chunks);
  164. }
  165. }
  166. static void wslay_event_imsg_reset(struct wslay_event_imsg *m)
  167. {
  168. m->opcode = 0xffu;
  169. m->utf8state = UTF8_ACCEPT;
  170. wslay_event_imsg_chunks_free(m);
  171. }
  172. static int wslay_event_imsg_append_chunk(struct wslay_event_imsg *m, size_t len)
  173. {
  174. if(len == 0) {
  175. return 0;
  176. } else {
  177. int r;
  178. struct wslay_event_byte_chunk *chunk;
  179. if((r = wslay_event_byte_chunk_init(&chunk, len)) != 0) {
  180. return r;
  181. }
  182. if((r = wslay_queue_push(m->chunks, chunk)) != 0) {
  183. return r;
  184. }
  185. m->msg_length += len;
  186. return 0;
  187. }
  188. }
  189. static int wslay_event_omsg_non_fragmented_init
  190. (struct wslay_event_omsg **m, uint8_t opcode, uint8_t rsv,
  191. const uint8_t *msg, size_t msg_length)
  192. {
  193. *m = (struct wslay_event_omsg*)malloc(sizeof(struct wslay_event_omsg));
  194. if(!*m) {
  195. return WSLAY_ERR_NOMEM;
  196. }
  197. memset(*m, 0, sizeof(struct wslay_event_omsg));
  198. (*m)->fin = 1;
  199. (*m)->opcode = opcode;
  200. (*m)->rsv = rsv;
  201. (*m)->type = WSLAY_NON_FRAGMENTED;
  202. if(msg_length) {
  203. (*m)->data = (uint8_t*)malloc(msg_length);
  204. if(!(*m)->data) {
  205. free(*m);
  206. return WSLAY_ERR_NOMEM;
  207. }
  208. memcpy((*m)->data, msg, msg_length);
  209. (*m)->data_length = msg_length;
  210. }
  211. return 0;
  212. }
  213. static int wslay_event_omsg_fragmented_init
  214. (struct wslay_event_omsg **m, uint8_t opcode, uint8_t rsv,
  215. const union wslay_event_msg_source source,
  216. wslay_event_fragmented_msg_callback read_callback)
  217. {
  218. *m = (struct wslay_event_omsg*)malloc(sizeof(struct wslay_event_omsg));
  219. if(!*m) {
  220. return WSLAY_ERR_NOMEM;
  221. }
  222. memset(*m, 0, sizeof(struct wslay_event_omsg));
  223. (*m)->opcode = opcode;
  224. (*m)->rsv = rsv;
  225. (*m)->type = WSLAY_FRAGMENTED;
  226. (*m)->source = source;
  227. (*m)->read_callback = read_callback;
  228. return 0;
  229. }
  230. static void wslay_event_omsg_free(struct wslay_event_omsg *m)
  231. {
  232. if(!m) {
  233. return;
  234. }
  235. free(m->data);
  236. free(m);
  237. }
  238. static uint8_t* wslay_event_flatten_queue(struct wslay_queue *queue, size_t len)
  239. {
  240. if(len == 0) {
  241. return NULL;
  242. } else {
  243. size_t off = 0;
  244. uint8_t *buf = (uint8_t*)malloc(len);
  245. if(!buf) {
  246. return NULL;
  247. }
  248. while(!wslay_queue_empty(queue)) {
  249. struct wslay_event_byte_chunk *chunk = wslay_queue_top(queue);
  250. memcpy(buf+off, chunk->data, chunk->data_length);
  251. off += chunk->data_length;
  252. wslay_event_byte_chunk_free(chunk);
  253. wslay_queue_pop(queue);
  254. assert(off <= len);
  255. }
  256. assert(len == off);
  257. return buf;
  258. }
  259. }
  260. static int wslay_event_is_msg_queueable(wslay_event_context_ptr ctx)
  261. {
  262. return ctx->write_enabled && (ctx->close_status & WSLAY_CLOSE_QUEUED) == 0;
  263. }
  264. int wslay_event_queue_close(wslay_event_context_ptr ctx, uint16_t status_code,
  265. const uint8_t *reason, size_t reason_length)
  266. {
  267. if(!wslay_event_is_msg_queueable(ctx)) {
  268. return WSLAY_ERR_NO_MORE_MSG;
  269. } else if(reason_length > 123) {
  270. return WSLAY_ERR_INVALID_ARGUMENT;
  271. } else {
  272. uint8_t msg[128];
  273. size_t msg_length;
  274. struct wslay_event_msg arg;
  275. uint16_t ncode;
  276. int r;
  277. if(status_code == 0) {
  278. msg_length = 0;
  279. } else {
  280. ncode = htons(status_code);
  281. memcpy(msg, &ncode, 2);
  282. if(reason_length) {
  283. memcpy(msg+2, reason, reason_length);
  284. }
  285. msg_length = reason_length+2;
  286. }
  287. arg.opcode = WSLAY_CONNECTION_CLOSE;
  288. arg.msg = msg;
  289. arg.msg_length = msg_length;
  290. r = wslay_event_queue_msg(ctx, &arg);
  291. if(r == 0) {
  292. ctx->close_status |= WSLAY_CLOSE_QUEUED;
  293. }
  294. return r;
  295. }
  296. }
  297. static int wslay_event_queue_close_wrapper
  298. (wslay_event_context_ptr ctx, uint16_t status_code,
  299. const uint8_t *reason, size_t reason_length)
  300. {
  301. int r;
  302. ctx->read_enabled = 0;
  303. if((r = wslay_event_queue_close(ctx, status_code, reason, reason_length)) &&
  304. r != WSLAY_ERR_NO_MORE_MSG) {
  305. return r;
  306. }
  307. return 0;
  308. }
  309. static int wslay_event_verify_rsv_bits(wslay_event_context_ptr ctx, uint8_t rsv)
  310. {
  311. return ((rsv & ~ctx->allowed_rsv_bits) == 0);
  312. }
  313. int wslay_event_queue_msg(wslay_event_context_ptr ctx,
  314. const struct wslay_event_msg *arg)
  315. {
  316. return wslay_event_queue_msg_ex(ctx, arg, WSLAY_RSV_NONE);
  317. }
  318. int wslay_event_queue_msg_ex(wslay_event_context_ptr ctx,
  319. const struct wslay_event_msg *arg, uint8_t rsv)
  320. {
  321. int r;
  322. struct wslay_event_omsg *omsg;
  323. if(!wslay_event_is_msg_queueable(ctx)) {
  324. return WSLAY_ERR_NO_MORE_MSG;
  325. }
  326. /* RSV1 is not allowed for control frames */
  327. if((wslay_is_ctrl_frame(arg->opcode) &&
  328. (arg->msg_length > 125 || wslay_get_rsv1(rsv)))
  329. || !wslay_event_verify_rsv_bits(ctx, rsv)) {
  330. return WSLAY_ERR_INVALID_ARGUMENT;
  331. }
  332. if((r = wslay_event_omsg_non_fragmented_init
  333. (&omsg, arg->opcode, rsv, arg->msg, arg->msg_length)) != 0) {
  334. return r;
  335. }
  336. if(wslay_is_ctrl_frame(arg->opcode)) {
  337. if((r = wslay_queue_push(ctx->send_ctrl_queue, omsg)) != 0) {
  338. return r;
  339. }
  340. } else {
  341. if((r = wslay_queue_push(ctx->send_queue, omsg)) != 0) {
  342. return r;
  343. }
  344. }
  345. ++ctx->queued_msg_count;
  346. ctx->queued_msg_length += arg->msg_length;
  347. return 0;
  348. }
  349. int wslay_event_queue_fragmented_msg
  350. (wslay_event_context_ptr ctx, const struct wslay_event_fragmented_msg *arg)
  351. {
  352. return wslay_event_queue_fragmented_msg_ex(ctx, arg, WSLAY_RSV_NONE);
  353. }
  354. int wslay_event_queue_fragmented_msg_ex(wslay_event_context_ptr ctx,
  355. const struct wslay_event_fragmented_msg *arg, uint8_t rsv)
  356. {
  357. int r;
  358. struct wslay_event_omsg *omsg;
  359. if(!wslay_event_is_msg_queueable(ctx)) {
  360. return WSLAY_ERR_NO_MORE_MSG;
  361. }
  362. if(wslay_is_ctrl_frame(arg->opcode) ||
  363. !wslay_event_verify_rsv_bits(ctx, rsv)) {
  364. return WSLAY_ERR_INVALID_ARGUMENT;
  365. }
  366. if((r = wslay_event_omsg_fragmented_init
  367. (&omsg, arg->opcode, rsv, arg->source, arg->read_callback)) != 0) {
  368. return r;
  369. }
  370. if((r = wslay_queue_push(ctx->send_queue, omsg)) != 0) {
  371. return r;
  372. }
  373. ++ctx->queued_msg_count;
  374. return 0;
  375. }
  376. void wslay_event_config_set_callbacks
  377. (wslay_event_context_ptr ctx, const struct wslay_event_callbacks *callbacks)
  378. {
  379. ctx->callbacks = *callbacks;
  380. }
  381. static int wslay_event_context_init
  382. (wslay_event_context_ptr *ctx,
  383. const struct wslay_event_callbacks *callbacks,
  384. void *user_data)
  385. {
  386. int i, r;
  387. struct wslay_frame_callbacks frame_callbacks = {
  388. wslay_event_frame_send_callback,
  389. wslay_event_frame_recv_callback,
  390. wslay_event_frame_genmask_callback
  391. };
  392. *ctx = (wslay_event_context_ptr)malloc(sizeof(struct wslay_event_context));
  393. if(!*ctx) {
  394. return WSLAY_ERR_NOMEM;
  395. }
  396. memset(*ctx, 0, sizeof(struct wslay_event_context));
  397. wslay_event_config_set_callbacks(*ctx, callbacks);
  398. (*ctx)->user_data = user_data;
  399. (*ctx)->frame_user_data.ctx = *ctx;
  400. (*ctx)->frame_user_data.user_data = user_data;
  401. if((r = wslay_frame_context_init(&(*ctx)->frame_ctx, &frame_callbacks,
  402. &(*ctx)->frame_user_data)) != 0) {
  403. wslay_event_context_free(*ctx);
  404. return r;
  405. }
  406. (*ctx)->read_enabled = (*ctx)->write_enabled = 1;
  407. (*ctx)->send_queue = wslay_queue_new();
  408. if(!(*ctx)->send_queue) {
  409. wslay_event_context_free(*ctx);
  410. return WSLAY_ERR_NOMEM;
  411. }
  412. (*ctx)->send_ctrl_queue = wslay_queue_new();
  413. if(!(*ctx)->send_ctrl_queue) {
  414. wslay_event_context_free(*ctx);
  415. return WSLAY_ERR_NOMEM;
  416. }
  417. (*ctx)->queued_msg_count = 0;
  418. (*ctx)->queued_msg_length = 0;
  419. for(i = 0; i < 2; ++i) {
  420. wslay_event_imsg_reset(&(*ctx)->imsgs[i]);
  421. (*ctx)->imsgs[i].chunks = wslay_queue_new();
  422. if(!(*ctx)->imsgs[i].chunks) {
  423. wslay_event_context_free(*ctx);
  424. return WSLAY_ERR_NOMEM;
  425. }
  426. }
  427. (*ctx)->imsg = &(*ctx)->imsgs[0];
  428. (*ctx)->obufmark = (*ctx)->obuflimit = (*ctx)->obuf;
  429. (*ctx)->status_code_sent = WSLAY_CODE_ABNORMAL_CLOSURE;
  430. (*ctx)->status_code_recv = WSLAY_CODE_ABNORMAL_CLOSURE;
  431. (*ctx)->max_recv_msg_length = (1u << 31)-1;
  432. return 0;
  433. }
  434. int wslay_event_context_server_init
  435. (wslay_event_context_ptr *ctx,
  436. const struct wslay_event_callbacks *callbacks,
  437. void *user_data)
  438. {
  439. int r;
  440. if((r = wslay_event_context_init(ctx, callbacks, user_data)) != 0) {
  441. return r;
  442. }
  443. (*ctx)->server = 1;
  444. return 0;
  445. }
  446. int wslay_event_context_client_init
  447. (wslay_event_context_ptr *ctx,
  448. const struct wslay_event_callbacks *callbacks,
  449. void *user_data)
  450. {
  451. int r;
  452. if((r = wslay_event_context_init(ctx, callbacks, user_data)) != 0) {
  453. return r;
  454. }
  455. (*ctx)->server = 0;
  456. return 0;
  457. }
  458. void wslay_event_context_free(wslay_event_context_ptr ctx)
  459. {
  460. int i;
  461. if(!ctx) {
  462. return;
  463. }
  464. for(i = 0; i < 2; ++i) {
  465. wslay_event_imsg_chunks_free(&ctx->imsgs[i]);
  466. wslay_queue_free(ctx->imsgs[i].chunks);
  467. }
  468. if(ctx->send_queue) {
  469. while(!wslay_queue_empty(ctx->send_queue)) {
  470. wslay_event_omsg_free(wslay_queue_top(ctx->send_queue));
  471. wslay_queue_pop(ctx->send_queue);
  472. }
  473. wslay_queue_free(ctx->send_queue);
  474. }
  475. if(ctx->send_ctrl_queue) {
  476. while(!wslay_queue_empty(ctx->send_ctrl_queue)) {
  477. wslay_event_omsg_free(wslay_queue_top(ctx->send_ctrl_queue));
  478. wslay_queue_pop(ctx->send_ctrl_queue);
  479. }
  480. wslay_queue_free(ctx->send_ctrl_queue);
  481. }
  482. wslay_frame_context_free(ctx->frame_ctx);
  483. wslay_event_omsg_free(ctx->omsg);
  484. free(ctx);
  485. }
  486. static void wslay_event_call_on_frame_recv_start_callback
  487. (wslay_event_context_ptr ctx, const struct wslay_frame_iocb *iocb)
  488. {
  489. if(ctx->callbacks.on_frame_recv_start_callback) {
  490. struct wslay_event_on_frame_recv_start_arg arg;
  491. arg.fin = iocb->fin;
  492. arg.rsv = iocb->rsv;
  493. arg.opcode = iocb->opcode;
  494. arg.payload_length = iocb->payload_length;
  495. ctx->callbacks.on_frame_recv_start_callback(ctx, &arg, ctx->user_data);
  496. }
  497. }
  498. static void wslay_event_call_on_frame_recv_chunk_callback
  499. (wslay_event_context_ptr ctx, const struct wslay_frame_iocb *iocb)
  500. {
  501. if(ctx->callbacks.on_frame_recv_chunk_callback) {
  502. struct wslay_event_on_frame_recv_chunk_arg arg;
  503. arg.data = iocb->data;
  504. arg.data_length = iocb->data_length;
  505. ctx->callbacks.on_frame_recv_chunk_callback(ctx, &arg, ctx->user_data);
  506. }
  507. }
  508. static void wslay_event_call_on_frame_recv_end_callback
  509. (wslay_event_context_ptr ctx)
  510. {
  511. if(ctx->callbacks.on_frame_recv_end_callback) {
  512. ctx->callbacks.on_frame_recv_end_callback(ctx, ctx->user_data);
  513. }
  514. }
  515. static int wslay_event_is_valid_status_code(uint16_t status_code)
  516. {
  517. return (1000 <= status_code && status_code <= 1011 &&
  518. status_code != 1004 && status_code != 1005 && status_code != 1006) ||
  519. (3000 <= status_code && status_code <= 4999);
  520. }
  521. static int wslay_event_config_get_no_buffering(wslay_event_context_ptr ctx)
  522. {
  523. return (ctx->config & WSLAY_CONFIG_NO_BUFFERING) > 0;
  524. }
  525. int wslay_event_recv(wslay_event_context_ptr ctx)
  526. {
  527. struct wslay_frame_iocb iocb;
  528. ssize_t r;
  529. while(ctx->read_enabled) {
  530. memset(&iocb, 0, sizeof(iocb));
  531. r = wslay_frame_recv(ctx->frame_ctx, &iocb);
  532. if(r >= 0) {
  533. int new_frame = 0;
  534. /* RSV1 is not allowed on control and continuation frames */
  535. if((!wslay_event_verify_rsv_bits(ctx, iocb.rsv)) ||
  536. (wslay_get_rsv1(iocb.rsv) && (wslay_is_ctrl_frame(iocb.opcode) ||
  537. iocb.opcode == WSLAY_CONTINUATION_FRAME)) ||
  538. (ctx->server && !iocb.mask) || (!ctx->server && iocb.mask)) {
  539. if((r = wslay_event_queue_close_wrapper
  540. (ctx, WSLAY_CODE_PROTOCOL_ERROR, NULL, 0)) != 0) {
  541. return r;
  542. }
  543. break;
  544. }
  545. if(ctx->imsg->opcode == 0xffu) {
  546. if(iocb.opcode == WSLAY_TEXT_FRAME ||
  547. iocb.opcode == WSLAY_BINARY_FRAME ||
  548. iocb.opcode == WSLAY_CONNECTION_CLOSE ||
  549. iocb.opcode == WSLAY_PING ||
  550. iocb.opcode == WSLAY_PONG) {
  551. wslay_event_imsg_set(ctx->imsg, iocb.fin, iocb.rsv, iocb.opcode);
  552. new_frame = 1;
  553. } else {
  554. if((r = wslay_event_queue_close_wrapper
  555. (ctx, WSLAY_CODE_PROTOCOL_ERROR, NULL, 0)) != 0) {
  556. return r;
  557. }
  558. break;
  559. }
  560. } else if(ctx->ipayloadlen == 0 && ctx->ipayloadoff == 0) {
  561. if(iocb.opcode == WSLAY_CONTINUATION_FRAME) {
  562. ctx->imsg->fin = iocb.fin;
  563. } else if(iocb.opcode == WSLAY_CONNECTION_CLOSE ||
  564. iocb.opcode == WSLAY_PING ||
  565. iocb.opcode == WSLAY_PONG) {
  566. ctx->imsg = &ctx->imsgs[1];
  567. wslay_event_imsg_set(ctx->imsg, iocb.fin, iocb.rsv, iocb.opcode);
  568. } else {
  569. if((r = wslay_event_queue_close_wrapper
  570. (ctx, WSLAY_CODE_PROTOCOL_ERROR, NULL, 0)) != 0) {
  571. return r;
  572. }
  573. break;
  574. }
  575. new_frame = 1;
  576. }
  577. if(new_frame) {
  578. if(ctx->imsg->msg_length+iocb.payload_length >
  579. ctx->max_recv_msg_length) {
  580. if((r = wslay_event_queue_close_wrapper
  581. (ctx, WSLAY_CODE_MESSAGE_TOO_BIG, NULL, 0)) != 0) {
  582. return r;
  583. }
  584. break;
  585. }
  586. ctx->ipayloadlen = iocb.payload_length;
  587. wslay_event_call_on_frame_recv_start_callback(ctx, &iocb);
  588. if(!wslay_event_config_get_no_buffering(ctx) ||
  589. wslay_is_ctrl_frame(iocb.opcode)) {
  590. if((r = wslay_event_imsg_append_chunk(ctx->imsg,
  591. iocb.payload_length)) != 0) {
  592. ctx->read_enabled = 0;
  593. return r;
  594. }
  595. }
  596. }
  597. /* If RSV1 bit is set then it is too early for utf-8 validation */
  598. if((!wslay_get_rsv1(ctx->imsg->rsv) &&
  599. ctx->imsg->opcode == WSLAY_TEXT_FRAME) ||
  600. ctx->imsg->opcode == WSLAY_CONNECTION_CLOSE) {
  601. size_t i;
  602. if(ctx->imsg->opcode == WSLAY_CONNECTION_CLOSE) {
  603. i = 2;
  604. } else {
  605. i = 0;
  606. }
  607. for(; i < iocb.data_length; ++i) {
  608. uint32_t codep;
  609. if(decode(&ctx->imsg->utf8state, &codep,
  610. iocb.data[i]) == UTF8_REJECT) {
  611. if((r = wslay_event_queue_close_wrapper
  612. (ctx, WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA, NULL, 0)) != 0) {
  613. return r;
  614. }
  615. break;
  616. }
  617. }
  618. }
  619. if(ctx->imsg->utf8state == UTF8_REJECT) {
  620. break;
  621. }
  622. wslay_event_call_on_frame_recv_chunk_callback(ctx, &iocb);
  623. if(iocb.data_length > 0) {
  624. if(!wslay_event_config_get_no_buffering(ctx) ||
  625. wslay_is_ctrl_frame(iocb.opcode)) {
  626. struct wslay_event_byte_chunk *chunk;
  627. chunk = wslay_queue_tail(ctx->imsg->chunks);
  628. wslay_event_byte_chunk_copy(chunk, ctx->ipayloadoff,
  629. iocb.data, iocb.data_length);
  630. }
  631. ctx->ipayloadoff += iocb.data_length;
  632. }
  633. if(ctx->ipayloadoff == ctx->ipayloadlen) {
  634. if(ctx->imsg->fin &&
  635. (ctx->imsg->opcode == WSLAY_TEXT_FRAME ||
  636. ctx->imsg->opcode == WSLAY_CONNECTION_CLOSE) &&
  637. ctx->imsg->utf8state != UTF8_ACCEPT) {
  638. if((r = wslay_event_queue_close_wrapper
  639. (ctx, WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA, NULL, 0)) != 0) {
  640. return r;
  641. }
  642. break;
  643. }
  644. wslay_event_call_on_frame_recv_end_callback(ctx);
  645. if(ctx->imsg->fin) {
  646. if(ctx->callbacks.on_msg_recv_callback ||
  647. ctx->imsg->opcode == WSLAY_CONNECTION_CLOSE ||
  648. ctx->imsg->opcode == WSLAY_PING) {
  649. struct wslay_event_on_msg_recv_arg arg;
  650. uint16_t status_code = 0;
  651. uint8_t *msg = NULL;
  652. size_t msg_length = 0;
  653. if(!wslay_event_config_get_no_buffering(ctx) ||
  654. wslay_is_ctrl_frame(iocb.opcode)) {
  655. msg = wslay_event_flatten_queue(ctx->imsg->chunks,
  656. ctx->imsg->msg_length);
  657. if(ctx->imsg->msg_length && !msg) {
  658. ctx->read_enabled = 0;
  659. return WSLAY_ERR_NOMEM;
  660. }
  661. msg_length = ctx->imsg->msg_length;
  662. }
  663. if(ctx->imsg->opcode == WSLAY_CONNECTION_CLOSE) {
  664. const uint8_t *reason;
  665. size_t reason_length;
  666. if(ctx->imsg->msg_length >= 2) {
  667. memcpy(&status_code, msg, 2);
  668. status_code = ntohs(status_code);
  669. if(!wslay_event_is_valid_status_code(status_code)) {
  670. free(msg);
  671. if((r = wslay_event_queue_close_wrapper
  672. (ctx, WSLAY_CODE_PROTOCOL_ERROR, NULL, 0)) != 0) {
  673. return r;
  674. }
  675. break;
  676. }
  677. reason = msg+2;
  678. reason_length = ctx->imsg->msg_length-2;
  679. } else {
  680. reason = NULL;
  681. reason_length = 0;
  682. }
  683. ctx->close_status |= WSLAY_CLOSE_RECEIVED;
  684. ctx->status_code_recv =
  685. status_code == 0 ? WSLAY_CODE_NO_STATUS_RCVD : status_code;
  686. if((r = wslay_event_queue_close_wrapper
  687. (ctx, status_code, reason, reason_length)) != 0) {
  688. free(msg);
  689. return r;
  690. }
  691. } else if(ctx->imsg->opcode == WSLAY_PING) {
  692. struct wslay_event_msg pong_arg;
  693. pong_arg.opcode = WSLAY_PONG;
  694. pong_arg.msg = msg;
  695. pong_arg.msg_length = ctx->imsg->msg_length;
  696. if((r = wslay_event_queue_msg(ctx, &pong_arg)) &&
  697. r != WSLAY_ERR_NO_MORE_MSG) {
  698. ctx->read_enabled = 0;
  699. free(msg);
  700. return r;
  701. }
  702. }
  703. if(ctx->callbacks.on_msg_recv_callback) {
  704. arg.rsv = ctx->imsg->rsv;
  705. arg.opcode = ctx->imsg->opcode;
  706. arg.msg = msg;
  707. arg.msg_length = msg_length;
  708. arg.status_code = status_code;
  709. ctx->error = 0;
  710. ctx->callbacks.on_msg_recv_callback(ctx, &arg, ctx->user_data);
  711. }
  712. free(msg);
  713. }
  714. wslay_event_imsg_reset(ctx->imsg);
  715. if(ctx->imsg == &ctx->imsgs[1]) {
  716. ctx->imsg = &ctx->imsgs[0];
  717. }
  718. }
  719. ctx->ipayloadlen = ctx->ipayloadoff = 0;
  720. }
  721. } else {
  722. if(r != WSLAY_ERR_WANT_READ ||
  723. (ctx->error != WSLAY_ERR_WOULDBLOCK && ctx->error != 0)) {
  724. if((r = wslay_event_queue_close_wrapper(ctx, 0, NULL, 0)) != 0) {
  725. return r;
  726. }
  727. return WSLAY_ERR_CALLBACK_FAILURE;
  728. }
  729. break;
  730. }
  731. }
  732. return 0;
  733. }
  734. static void wslay_event_on_non_fragmented_msg_popped
  735. (wslay_event_context_ptr ctx)
  736. {
  737. ctx->omsg->fin = 1;
  738. ctx->opayloadlen = ctx->omsg->data_length;
  739. ctx->opayloadoff = 0;
  740. }
  741. static struct wslay_event_omsg* wslay_event_send_ctrl_queue_pop
  742. (wslay_event_context_ptr ctx)
  743. {
  744. /*
  745. * If Close control frame is queued, we don't send any control frame
  746. * other than Close.
  747. */
  748. if(ctx->close_status & WSLAY_CLOSE_QUEUED) {
  749. while(!wslay_queue_empty(ctx->send_ctrl_queue)) {
  750. struct wslay_event_omsg *msg = wslay_queue_top(ctx->send_ctrl_queue);
  751. wslay_queue_pop(ctx->send_ctrl_queue);
  752. if(msg->opcode == WSLAY_CONNECTION_CLOSE) {
  753. return msg;
  754. } else {
  755. wslay_event_omsg_free(msg);
  756. }
  757. }
  758. return NULL;
  759. } else {
  760. struct wslay_event_omsg *msg = wslay_queue_top(ctx->send_ctrl_queue);
  761. wslay_queue_pop(ctx->send_ctrl_queue);
  762. return msg;
  763. }
  764. }
  765. int wslay_event_send(wslay_event_context_ptr ctx)
  766. {
  767. struct wslay_frame_iocb iocb;
  768. ssize_t r;
  769. while(ctx->write_enabled &&
  770. (!wslay_queue_empty(ctx->send_queue) ||
  771. !wslay_queue_empty(ctx->send_ctrl_queue) || ctx->omsg)) {
  772. if(!ctx->omsg) {
  773. if(wslay_queue_empty(ctx->send_ctrl_queue)) {
  774. ctx->omsg = wslay_queue_top(ctx->send_queue);
  775. wslay_queue_pop(ctx->send_queue);
  776. } else {
  777. ctx->omsg = wslay_event_send_ctrl_queue_pop(ctx);
  778. if(ctx->omsg == NULL) {
  779. break;
  780. }
  781. }
  782. if(ctx->omsg->type == WSLAY_NON_FRAGMENTED) {
  783. wslay_event_on_non_fragmented_msg_popped(ctx);
  784. }
  785. } else if(!wslay_is_ctrl_frame(ctx->omsg->opcode) &&
  786. ctx->frame_ctx->ostate == PREP_HEADER &&
  787. !wslay_queue_empty(ctx->send_ctrl_queue)) {
  788. if((r = wslay_queue_push_front(ctx->send_queue, ctx->omsg)) != 0) {
  789. ctx->write_enabled = 0;
  790. return r;
  791. }
  792. ctx->omsg = wslay_event_send_ctrl_queue_pop(ctx);
  793. if(ctx->omsg == NULL) {
  794. break;
  795. }
  796. /* ctrl message has WSLAY_NON_FRAGMENTED */
  797. wslay_event_on_non_fragmented_msg_popped(ctx);
  798. }
  799. if(ctx->omsg->type == WSLAY_NON_FRAGMENTED) {
  800. memset(&iocb, 0, sizeof(iocb));
  801. iocb.fin = 1;
  802. iocb.opcode = ctx->omsg->opcode;
  803. iocb.rsv = ctx->omsg->rsv;
  804. iocb.mask = ctx->server^1;
  805. iocb.data = ctx->omsg->data+ctx->opayloadoff;
  806. iocb.data_length = ctx->opayloadlen-ctx->opayloadoff;
  807. iocb.payload_length = ctx->opayloadlen;
  808. r = wslay_frame_send(ctx->frame_ctx, &iocb);
  809. if(r >= 0) {
  810. ctx->opayloadoff += r;
  811. if(ctx->opayloadoff == ctx->opayloadlen) {
  812. --ctx->queued_msg_count;
  813. ctx->queued_msg_length -= ctx->omsg->data_length;
  814. if(ctx->omsg->opcode == WSLAY_CONNECTION_CLOSE) {
  815. uint16_t status_code = 0;
  816. ctx->write_enabled = 0;
  817. ctx->close_status |= WSLAY_CLOSE_SENT;
  818. if(ctx->omsg->data_length >= 2) {
  819. memcpy(&status_code, ctx->omsg->data, 2);
  820. status_code = ntohs(status_code);
  821. }
  822. ctx->status_code_sent =
  823. status_code == 0 ? WSLAY_CODE_NO_STATUS_RCVD : status_code;
  824. }
  825. wslay_event_omsg_free(ctx->omsg);
  826. ctx->omsg = NULL;
  827. } else {
  828. break;
  829. }
  830. } else {
  831. if(r != WSLAY_ERR_WANT_WRITE ||
  832. (ctx->error != WSLAY_ERR_WOULDBLOCK && ctx->error != 0)) {
  833. ctx->write_enabled = 0;
  834. return WSLAY_ERR_CALLBACK_FAILURE;
  835. }
  836. break;
  837. }
  838. } else {
  839. if(ctx->omsg->fin == 0 && ctx->obuflimit == ctx->obufmark) {
  840. int eof = 0;
  841. r = ctx->omsg->read_callback(ctx, ctx->obuf, sizeof(ctx->obuf),
  842. &ctx->omsg->source,
  843. &eof, ctx->user_data);
  844. if(r == 0 && eof == 0) {
  845. break;
  846. } else if(r < 0) {
  847. ctx->write_enabled = 0;
  848. return WSLAY_ERR_CALLBACK_FAILURE;
  849. }
  850. ctx->obuflimit = ctx->obuf+r;
  851. if(eof) {
  852. ctx->omsg->fin = 1;
  853. }
  854. ctx->opayloadlen = r;
  855. ctx->opayloadoff = 0;
  856. }
  857. memset(&iocb, 0, sizeof(iocb));
  858. iocb.fin = ctx->omsg->fin;
  859. iocb.opcode = ctx->omsg->opcode;
  860. iocb.rsv = ctx->omsg->rsv;
  861. iocb.mask = ctx->server ? 0 : 1;
  862. iocb.data = ctx->obufmark;
  863. iocb.data_length = ctx->obuflimit-ctx->obufmark;
  864. iocb.payload_length = ctx->opayloadlen;
  865. r = wslay_frame_send(ctx->frame_ctx, &iocb);
  866. if(r >= 0) {
  867. ctx->obufmark += r;
  868. if(ctx->obufmark == ctx->obuflimit) {
  869. ctx->obufmark = ctx->obuflimit = ctx->obuf;
  870. if(ctx->omsg->fin) {
  871. --ctx->queued_msg_count;
  872. wslay_event_omsg_free(ctx->omsg);
  873. ctx->omsg = NULL;
  874. } else {
  875. ctx->omsg->opcode = WSLAY_CONTINUATION_FRAME;
  876. /* RSV1 is not set on continuation frames */
  877. ctx->omsg->rsv = ctx->omsg->rsv & ~WSLAY_RSV1_BIT;
  878. }
  879. } else {
  880. break;
  881. }
  882. } else {
  883. if(r != WSLAY_ERR_WANT_WRITE ||
  884. (ctx->error != WSLAY_ERR_WOULDBLOCK &&
  885. ctx->error != 0)) {
  886. ctx->write_enabled = 0;
  887. return WSLAY_ERR_CALLBACK_FAILURE;
  888. }
  889. break;
  890. }
  891. }
  892. }
  893. return 0;
  894. }
  895. void wslay_event_set_error(wslay_event_context_ptr ctx, int val)
  896. {
  897. ctx->error = val;
  898. }
  899. int wslay_event_want_read(wslay_event_context_ptr ctx)
  900. {
  901. return ctx->read_enabled;
  902. }
  903. int wslay_event_want_write(wslay_event_context_ptr ctx)
  904. {
  905. return ctx->write_enabled &&
  906. (!wslay_queue_empty(ctx->send_queue) ||
  907. !wslay_queue_empty(ctx->send_ctrl_queue) || ctx->omsg);
  908. }
  909. void wslay_event_shutdown_read(wslay_event_context_ptr ctx)
  910. {
  911. ctx->read_enabled = 0;
  912. }
  913. void wslay_event_shutdown_write(wslay_event_context_ptr ctx)
  914. {
  915. ctx->write_enabled = 0;
  916. }
  917. int wslay_event_get_read_enabled(wslay_event_context_ptr ctx)
  918. {
  919. return ctx->read_enabled;
  920. }
  921. int wslay_event_get_write_enabled(wslay_event_context_ptr ctx)
  922. {
  923. return ctx->write_enabled;
  924. }
  925. int wslay_event_get_close_received(wslay_event_context_ptr ctx)
  926. {
  927. return (ctx->close_status & WSLAY_CLOSE_RECEIVED) > 0;
  928. }
  929. int wslay_event_get_close_sent(wslay_event_context_ptr ctx)
  930. {
  931. return (ctx->close_status & WSLAY_CLOSE_SENT) > 0;
  932. }
  933. void wslay_event_config_set_allowed_rsv_bits(wslay_event_context_ptr ctx,
  934. uint8_t rsv)
  935. {
  936. /* We currently only allow WSLAY_RSV1_BIT or WSLAY_RSV_NONE */
  937. ctx->allowed_rsv_bits = rsv & WSLAY_RSV1_BIT;
  938. }
  939. void wslay_event_config_set_no_buffering(wslay_event_context_ptr ctx, int val)
  940. {
  941. if(val) {
  942. ctx->config |= WSLAY_CONFIG_NO_BUFFERING;
  943. } else {
  944. ctx->config &= ~WSLAY_CONFIG_NO_BUFFERING;
  945. }
  946. }
  947. void wslay_event_config_set_max_recv_msg_length(wslay_event_context_ptr ctx,
  948. uint64_t val)
  949. {
  950. ctx->max_recv_msg_length = val;
  951. }
  952. uint16_t wslay_event_get_status_code_received(wslay_event_context_ptr ctx)
  953. {
  954. return ctx->status_code_recv;
  955. }
  956. uint16_t wslay_event_get_status_code_sent(wslay_event_context_ptr ctx)
  957. {
  958. return ctx->status_code_sent;
  959. }
  960. size_t wslay_event_get_queued_msg_count(wslay_event_context_ptr ctx)
  961. {
  962. return ctx->queued_msg_count;
  963. }
  964. size_t wslay_event_get_queued_msg_length(wslay_event_context_ptr ctx)
  965. {
  966. return ctx->queued_msg_length;
  967. }