wslay_frame.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_frame.h"
  26. #include <stddef.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include "wslay_net.h"
  30. #define wslay_min(A, B) (((A) < (B)) ? (A) : (B))
  31. int wslay_frame_context_init(wslay_frame_context_ptr *ctx,
  32. const struct wslay_frame_callbacks *callbacks,
  33. void *user_data) {
  34. *ctx = malloc(sizeof(struct wslay_frame_context));
  35. if (*ctx == NULL) {
  36. return -1;
  37. }
  38. memset(*ctx, 0, sizeof(struct wslay_frame_context));
  39. (*ctx)->istate = RECV_HEADER1;
  40. (*ctx)->ireqread = 2;
  41. (*ctx)->ostate = PREP_HEADER;
  42. (*ctx)->user_data = user_data;
  43. (*ctx)->ibufmark = (*ctx)->ibuflimit = (*ctx)->ibuf;
  44. (*ctx)->callbacks = *callbacks;
  45. return 0;
  46. }
  47. void wslay_frame_context_free(wslay_frame_context_ptr ctx) { free(ctx); }
  48. ssize_t wslay_frame_send(wslay_frame_context_ptr ctx,
  49. struct wslay_frame_iocb *iocb) {
  50. if (iocb->data_length > iocb->payload_length) {
  51. return WSLAY_ERR_INVALID_ARGUMENT;
  52. }
  53. if (ctx->ostate == PREP_HEADER) {
  54. uint8_t *hdptr = ctx->oheader;
  55. memset(ctx->oheader, 0, sizeof(ctx->oheader));
  56. *hdptr |= (uint8_t)((uint8_t)(iocb->fin << 7) & 0x80u);
  57. *hdptr |= (uint8_t)((uint8_t)(iocb->rsv << 4) & 0x70u);
  58. /* Suppress stubborn gcc-10 warning */
  59. *hdptr |= (uint8_t)((uint8_t)(iocb->opcode << 0) & 0xfu);
  60. ++hdptr;
  61. *hdptr |= (uint8_t)((uint8_t)(iocb->mask << 7) & 0x80u);
  62. if (wslay_is_ctrl_frame(iocb->opcode) && iocb->payload_length > 125) {
  63. return WSLAY_ERR_INVALID_ARGUMENT;
  64. }
  65. if (iocb->payload_length < 126) {
  66. *hdptr |= (uint8_t)iocb->payload_length;
  67. ++hdptr;
  68. } else if (iocb->payload_length < (1 << 16)) {
  69. uint16_t len = htons((uint16_t)iocb->payload_length);
  70. *hdptr |= 126;
  71. ++hdptr;
  72. memcpy(hdptr, &len, 2);
  73. hdptr += 2;
  74. } else if (iocb->payload_length < (1ull << 63)) {
  75. uint64_t len = hton64(iocb->payload_length);
  76. *hdptr |= 127;
  77. ++hdptr;
  78. memcpy(hdptr, &len, 8);
  79. hdptr += 8;
  80. } else {
  81. /* Too large payload length */
  82. return WSLAY_ERR_INVALID_ARGUMENT;
  83. }
  84. if (iocb->mask) {
  85. if (ctx->callbacks.genmask_callback(ctx->omaskkey, 4, ctx->user_data) !=
  86. 0) {
  87. return WSLAY_ERR_INVALID_CALLBACK;
  88. } else {
  89. ctx->omask = 1;
  90. memcpy(hdptr, ctx->omaskkey, 4);
  91. hdptr += 4;
  92. }
  93. }
  94. ctx->ostate = SEND_HEADER;
  95. ctx->oheadermark = ctx->oheader;
  96. ctx->oheaderlimit = hdptr;
  97. ctx->opayloadlen = iocb->payload_length;
  98. ctx->opayloadoff = 0;
  99. }
  100. if (ctx->ostate == SEND_HEADER) {
  101. ptrdiff_t len = ctx->oheaderlimit - ctx->oheadermark;
  102. ssize_t r;
  103. int flags = 0;
  104. if (iocb->data_length > 0) {
  105. flags |= WSLAY_MSG_MORE;
  106. }
  107. r = ctx->callbacks.send_callback(ctx->oheadermark, (size_t)len, flags,
  108. ctx->user_data);
  109. if (r > 0) {
  110. if (r > len) {
  111. return WSLAY_ERR_INVALID_CALLBACK;
  112. } else {
  113. ctx->oheadermark += r;
  114. if (ctx->oheadermark == ctx->oheaderlimit) {
  115. ctx->ostate = SEND_PAYLOAD;
  116. } else {
  117. return WSLAY_ERR_WANT_WRITE;
  118. }
  119. }
  120. } else {
  121. return WSLAY_ERR_WANT_WRITE;
  122. }
  123. }
  124. if (ctx->ostate == SEND_PAYLOAD) {
  125. size_t totallen = 0;
  126. if (iocb->data_length > 0) {
  127. if (ctx->omask) {
  128. uint8_t temp[4096];
  129. const uint8_t *datamark = iocb->data,
  130. *datalimit = iocb->data + iocb->data_length;
  131. while (datamark < datalimit) {
  132. size_t datalen = (size_t)(datalimit - datamark);
  133. const uint8_t *writelimit =
  134. datamark + wslay_min(sizeof(temp), datalen);
  135. size_t writelen = (size_t)(writelimit - datamark);
  136. ssize_t r;
  137. size_t i;
  138. for (i = 0; i < writelen; ++i) {
  139. temp[i] = datamark[i] ^ ctx->omaskkey[(ctx->opayloadoff + i) % 4];
  140. }
  141. r = ctx->callbacks.send_callback(temp, writelen, 0, ctx->user_data);
  142. if (r > 0) {
  143. if ((size_t)r > writelen) {
  144. return WSLAY_ERR_INVALID_CALLBACK;
  145. } else {
  146. datamark += r;
  147. ctx->opayloadoff += (uint64_t)r;
  148. totallen += (size_t)r;
  149. }
  150. } else {
  151. if (totallen > 0) {
  152. break;
  153. } else {
  154. return WSLAY_ERR_WANT_WRITE;
  155. }
  156. }
  157. }
  158. } else {
  159. ssize_t r;
  160. r = ctx->callbacks.send_callback(iocb->data, iocb->data_length, 0,
  161. ctx->user_data);
  162. if (r > 0) {
  163. if ((size_t)r > iocb->data_length) {
  164. return WSLAY_ERR_INVALID_CALLBACK;
  165. } else {
  166. ctx->opayloadoff += (uint64_t)r;
  167. totallen = (size_t)r;
  168. }
  169. } else {
  170. return WSLAY_ERR_WANT_WRITE;
  171. }
  172. }
  173. }
  174. if (ctx->opayloadoff == ctx->opayloadlen) {
  175. ctx->ostate = PREP_HEADER;
  176. }
  177. return (ssize_t)totallen;
  178. }
  179. return WSLAY_ERR_INVALID_ARGUMENT;
  180. }
  181. ssize_t wslay_frame_write(wslay_frame_context_ptr ctx,
  182. struct wslay_frame_iocb *iocb, uint8_t *buf,
  183. size_t buflen, size_t *pwpayloadlen) {
  184. uint8_t *buf_last = buf;
  185. size_t i;
  186. size_t hdlen;
  187. *pwpayloadlen = 0;
  188. if (iocb->data_length > iocb->payload_length) {
  189. return WSLAY_ERR_INVALID_ARGUMENT;
  190. }
  191. switch (ctx->ostate) {
  192. case PREP_HEADER:
  193. case PREP_HEADER_NOBUF:
  194. hdlen = 2;
  195. if (iocb->payload_length < 126) {
  196. /* nothing to do */
  197. } else if (iocb->payload_length < (1 << 16)) {
  198. hdlen += 2;
  199. } else if (iocb->payload_length < (1ull << 63)) {
  200. hdlen += 8;
  201. }
  202. if (iocb->mask) {
  203. hdlen += 4;
  204. }
  205. if (buflen < hdlen) {
  206. ctx->ostate = PREP_HEADER_NOBUF;
  207. return 0;
  208. }
  209. memset(buf_last, 0, hdlen);
  210. *buf_last |= (uint8_t)((uint8_t)(iocb->fin << 7) & 0x80u);
  211. *buf_last |= (uint8_t)((uint8_t)(iocb->rsv << 4) & 0x70u);
  212. /* Suppress stubborn gcc-10 warning */
  213. *buf_last |= (uint8_t)((uint8_t)(iocb->opcode << 0) & 0xfu);
  214. ++buf_last;
  215. *buf_last |= (uint8_t)((uint8_t)(iocb->mask << 7) & 0x80u);
  216. if (wslay_is_ctrl_frame(iocb->opcode) && iocb->payload_length > 125) {
  217. return WSLAY_ERR_INVALID_ARGUMENT;
  218. }
  219. if (iocb->payload_length < 126) {
  220. *buf_last |= (uint8_t)iocb->payload_length;
  221. ++buf_last;
  222. } else if (iocb->payload_length < (1 << 16)) {
  223. uint16_t len = htons((uint16_t)iocb->payload_length);
  224. *buf_last |= 126;
  225. ++buf_last;
  226. memcpy(buf_last, &len, 2);
  227. buf_last += 2;
  228. } else if (iocb->payload_length < (1ull << 63)) {
  229. uint64_t len = hton64(iocb->payload_length);
  230. *buf_last |= 127;
  231. ++buf_last;
  232. memcpy(buf_last, &len, 8);
  233. buf_last += 8;
  234. } else {
  235. /* Too large payload length */
  236. return WSLAY_ERR_INVALID_ARGUMENT;
  237. }
  238. if (iocb->mask) {
  239. if (ctx->callbacks.genmask_callback(ctx->omaskkey, 4, ctx->user_data) !=
  240. 0) {
  241. return WSLAY_ERR_INVALID_CALLBACK;
  242. } else {
  243. ctx->omask = 1;
  244. memcpy(buf_last, ctx->omaskkey, 4);
  245. buf_last += 4;
  246. }
  247. }
  248. ctx->ostate = SEND_PAYLOAD;
  249. ctx->opayloadlen = iocb->payload_length;
  250. ctx->opayloadoff = 0;
  251. buflen -= (size_t)(buf_last - buf);
  252. /* fall through */
  253. case SEND_PAYLOAD:
  254. if (iocb->data_length > 0) {
  255. size_t writelen = wslay_min(buflen, iocb->data_length);
  256. if (ctx->omask) {
  257. for (i = 0; i < writelen; ++i) {
  258. *buf_last++ =
  259. iocb->data[i] ^ ctx->omaskkey[(ctx->opayloadoff + i) % 4];
  260. }
  261. } else {
  262. memcpy(buf_last, iocb->data, writelen);
  263. buf_last += writelen;
  264. }
  265. ctx->opayloadoff += writelen;
  266. *pwpayloadlen = writelen;
  267. }
  268. if (ctx->opayloadoff == ctx->opayloadlen) {
  269. ctx->ostate = PREP_HEADER;
  270. }
  271. return buf_last - buf;
  272. default:
  273. return WSLAY_ERR_INVALID_ARGUMENT;
  274. }
  275. }
  276. static void wslay_shift_ibuf(wslay_frame_context_ptr ctx) {
  277. ptrdiff_t len = ctx->ibuflimit - ctx->ibufmark;
  278. memmove(ctx->ibuf, ctx->ibufmark, (size_t)len);
  279. ctx->ibuflimit = ctx->ibuf + len;
  280. ctx->ibufmark = ctx->ibuf;
  281. }
  282. static ssize_t wslay_recv(wslay_frame_context_ptr ctx) {
  283. ssize_t r;
  284. if (ctx->ibufmark != ctx->ibuf) {
  285. wslay_shift_ibuf(ctx);
  286. }
  287. r = ctx->callbacks.recv_callback(
  288. ctx->ibuflimit, (size_t)(ctx->ibuf + sizeof(ctx->ibuf) - ctx->ibuflimit),
  289. 0, ctx->user_data);
  290. if (r > 0) {
  291. ctx->ibuflimit += r;
  292. } else {
  293. r = WSLAY_ERR_WANT_READ;
  294. }
  295. return r;
  296. }
  297. #define WSLAY_AVAIL_IBUF(ctx) ((size_t)(ctx->ibuflimit - ctx->ibufmark))
  298. ssize_t wslay_frame_recv(wslay_frame_context_ptr ctx,
  299. struct wslay_frame_iocb *iocb) {
  300. ssize_t r;
  301. if (ctx->istate == RECV_HEADER1) {
  302. uint8_t fin, opcode, rsv, payloadlen;
  303. if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  304. if ((r = wslay_recv(ctx)) <= 0) {
  305. return r;
  306. }
  307. }
  308. if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  309. return WSLAY_ERR_WANT_READ;
  310. }
  311. fin = (ctx->ibufmark[0] >> 7) & 1;
  312. rsv = (ctx->ibufmark[0] >> 4) & 7;
  313. opcode = ctx->ibufmark[0] & 0xfu;
  314. ctx->iom.opcode = opcode;
  315. ctx->iom.fin = fin;
  316. ctx->iom.rsv = rsv;
  317. ++ctx->ibufmark;
  318. ctx->imask = (ctx->ibufmark[0] >> 7) & 1;
  319. payloadlen = ctx->ibufmark[0] & 0x7fu;
  320. ++ctx->ibufmark;
  321. if (wslay_is_ctrl_frame(opcode) && (payloadlen > 125 || !fin)) {
  322. return WSLAY_ERR_PROTO;
  323. }
  324. if (payloadlen == 126) {
  325. ctx->istate = RECV_EXT_PAYLOADLEN;
  326. ctx->ireqread = 2;
  327. } else if (payloadlen == 127) {
  328. ctx->istate = RECV_EXT_PAYLOADLEN;
  329. ctx->ireqread = 8;
  330. } else {
  331. ctx->ipayloadlen = payloadlen;
  332. ctx->ipayloadoff = 0;
  333. if (ctx->imask) {
  334. ctx->istate = RECV_MASKKEY;
  335. ctx->ireqread = 4;
  336. } else {
  337. ctx->istate = RECV_PAYLOAD;
  338. }
  339. }
  340. }
  341. if (ctx->istate == RECV_EXT_PAYLOADLEN) {
  342. if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  343. if ((r = wslay_recv(ctx)) <= 0) {
  344. return r;
  345. }
  346. if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  347. return WSLAY_ERR_WANT_READ;
  348. }
  349. }
  350. ctx->ipayloadlen = 0;
  351. ctx->ipayloadoff = 0;
  352. memcpy((uint8_t *)&ctx->ipayloadlen + (8 - ctx->ireqread), ctx->ibufmark,
  353. ctx->ireqread);
  354. ctx->ipayloadlen = ntoh64(ctx->ipayloadlen);
  355. ctx->ibufmark += ctx->ireqread;
  356. if (ctx->ireqread == 8) {
  357. if (ctx->ipayloadlen < (1 << 16) || ctx->ipayloadlen & (1ull << 63)) {
  358. return WSLAY_ERR_PROTO;
  359. }
  360. } else if (ctx->ipayloadlen < 126) {
  361. return WSLAY_ERR_PROTO;
  362. }
  363. if (ctx->imask) {
  364. ctx->istate = RECV_MASKKEY;
  365. ctx->ireqread = 4;
  366. } else {
  367. ctx->istate = RECV_PAYLOAD;
  368. }
  369. }
  370. if (ctx->istate == RECV_MASKKEY) {
  371. if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  372. if ((r = wslay_recv(ctx)) <= 0) {
  373. return r;
  374. }
  375. if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  376. return WSLAY_ERR_WANT_READ;
  377. }
  378. }
  379. memcpy(ctx->imaskkey, ctx->ibufmark, 4);
  380. ctx->ibufmark += 4;
  381. ctx->istate = RECV_PAYLOAD;
  382. }
  383. if (ctx->istate == RECV_PAYLOAD) {
  384. uint8_t *readlimit, *readmark;
  385. uint64_t rempayloadlen = ctx->ipayloadlen - ctx->ipayloadoff;
  386. if (WSLAY_AVAIL_IBUF(ctx) == 0 && rempayloadlen > 0) {
  387. if ((r = wslay_recv(ctx)) <= 0) {
  388. return r;
  389. }
  390. }
  391. readmark = ctx->ibufmark;
  392. readlimit = WSLAY_AVAIL_IBUF(ctx) < rempayloadlen
  393. ? ctx->ibuflimit
  394. : ctx->ibufmark + rempayloadlen;
  395. if (ctx->imask) {
  396. for (; ctx->ibufmark != readlimit; ++ctx->ibufmark, ++ctx->ipayloadoff) {
  397. ctx->ibufmark[0] ^= ctx->imaskkey[ctx->ipayloadoff % 4];
  398. }
  399. } else {
  400. ctx->ibufmark = readlimit;
  401. ctx->ipayloadoff += (uint64_t)(readlimit - readmark);
  402. }
  403. iocb->fin = ctx->iom.fin;
  404. iocb->rsv = ctx->iom.rsv;
  405. iocb->opcode = ctx->iom.opcode;
  406. iocb->payload_length = ctx->ipayloadlen;
  407. iocb->mask = ctx->imask;
  408. iocb->data = readmark;
  409. iocb->data_length = (size_t)(ctx->ibufmark - readmark);
  410. if (ctx->ipayloadlen == ctx->ipayloadoff) {
  411. ctx->istate = RECV_HEADER1;
  412. ctx->ireqread = 2;
  413. }
  414. return (ssize_t)iocb->data_length;
  415. }
  416. return WSLAY_ERR_INVALID_ARGUMENT;
  417. }