wslay_frame.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. {
  35. *ctx = (wslay_frame_context_ptr)malloc(sizeof(struct wslay_frame_context));
  36. if(*ctx == NULL) {
  37. return -1;
  38. }
  39. memset(*ctx, 0, sizeof(struct wslay_frame_context));
  40. (*ctx)->istate = RECV_HEADER1;
  41. (*ctx)->ireqread = 2;
  42. (*ctx)->ostate = PREP_HEADER;
  43. (*ctx)->user_data = user_data;
  44. (*ctx)->ibufmark = (*ctx)->ibuflimit = (*ctx)->ibuf;
  45. (*ctx)->callbacks = *callbacks;
  46. return 0;
  47. }
  48. void wslay_frame_context_free(wslay_frame_context_ptr ctx)
  49. {
  50. free(ctx);
  51. }
  52. ssize_t wslay_frame_send(wslay_frame_context_ptr ctx,
  53. struct wslay_frame_iocb *iocb)
  54. {
  55. if(iocb->data_length > iocb->payload_length) {
  56. return WSLAY_ERR_INVALID_ARGUMENT;
  57. }
  58. if(ctx->ostate == PREP_HEADER) {
  59. uint8_t *hdptr = ctx->oheader;
  60. memset(ctx->oheader, 0, sizeof(ctx->oheader));
  61. *hdptr |= (iocb->fin << 7) & 0x80u;
  62. *hdptr |= (iocb->rsv << 4) & 0x70u;
  63. *hdptr |= iocb->opcode & 0xfu;
  64. ++hdptr;
  65. *hdptr |= (iocb->mask << 7) & 0x80u;
  66. if(wslay_is_ctrl_frame(iocb->opcode) && iocb->payload_length > 125) {
  67. return WSLAY_ERR_INVALID_ARGUMENT;
  68. }
  69. if(iocb->payload_length < 126) {
  70. *hdptr |= iocb->payload_length;
  71. ++hdptr;
  72. } else if(iocb->payload_length < (1 << 16)) {
  73. uint16_t len = htons(iocb->payload_length);
  74. *hdptr |= 126;
  75. ++hdptr;
  76. memcpy(hdptr, &len, 2);
  77. hdptr += 2;
  78. } else if(iocb->payload_length < (1ull << 63)) {
  79. uint64_t len = hton64(iocb->payload_length);
  80. *hdptr |= 127;
  81. ++hdptr;
  82. memcpy(hdptr, &len, 8);
  83. hdptr += 8;
  84. } else {
  85. /* Too large payload length */
  86. return WSLAY_ERR_INVALID_ARGUMENT;
  87. }
  88. if(iocb->mask) {
  89. if(ctx->callbacks.genmask_callback(ctx->omaskkey, 4,
  90. ctx->user_data) != 0) {
  91. return WSLAY_ERR_INVALID_CALLBACK;
  92. } else {
  93. ctx->omask = 1;
  94. memcpy(hdptr, ctx->omaskkey, 4);
  95. hdptr += 4;
  96. }
  97. }
  98. ctx->ostate = SEND_HEADER;
  99. ctx->oheadermark = ctx->oheader;
  100. ctx->oheaderlimit = hdptr;
  101. ctx->opayloadlen = iocb->payload_length;
  102. ctx->opayloadoff = 0;
  103. }
  104. if(ctx->ostate == SEND_HEADER) {
  105. ptrdiff_t len = ctx->oheaderlimit-ctx->oheadermark;
  106. ssize_t r;
  107. int flags = 0;
  108. if(iocb->data_length > 0) {
  109. flags |= WSLAY_MSG_MORE;
  110. };
  111. r = ctx->callbacks.send_callback(ctx->oheadermark, len, flags,
  112. ctx->user_data);
  113. if(r > 0) {
  114. if(r > len) {
  115. return WSLAY_ERR_INVALID_CALLBACK;
  116. } else {
  117. ctx->oheadermark += r;
  118. if(ctx->oheadermark == ctx->oheaderlimit) {
  119. ctx->ostate = SEND_PAYLOAD;
  120. } else {
  121. return WSLAY_ERR_WANT_WRITE;
  122. }
  123. }
  124. } else {
  125. return WSLAY_ERR_WANT_WRITE;
  126. }
  127. }
  128. if(ctx->ostate == SEND_PAYLOAD) {
  129. size_t totallen = 0;
  130. if(iocb->data_length > 0) {
  131. if(ctx->omask) {
  132. uint8_t temp[4096];
  133. const uint8_t *datamark = iocb->data,
  134. *datalimit = iocb->data+iocb->data_length;
  135. while(datamark < datalimit) {
  136. size_t datalen = datalimit - datamark;
  137. const uint8_t *writelimit = datamark+
  138. wslay_min(sizeof(temp), datalen);
  139. size_t writelen = writelimit-datamark;
  140. ssize_t r;
  141. size_t i;
  142. for(i = 0; i < writelen; ++i) {
  143. temp[i] = datamark[i]^ctx->omaskkey[(ctx->opayloadoff+i)%4];
  144. }
  145. r = ctx->callbacks.send_callback(temp, writelen, 0, ctx->user_data);
  146. if(r > 0) {
  147. if((size_t)r > writelen) {
  148. return WSLAY_ERR_INVALID_CALLBACK;
  149. } else {
  150. datamark += r;
  151. ctx->opayloadoff += r;
  152. totallen += r;
  153. }
  154. } else {
  155. if(totallen > 0) {
  156. break;
  157. } else {
  158. return WSLAY_ERR_WANT_WRITE;
  159. }
  160. }
  161. }
  162. } else {
  163. ssize_t r;
  164. r = ctx->callbacks.send_callback(iocb->data, iocb->data_length, 0,
  165. ctx->user_data);
  166. if(r > 0) {
  167. if((size_t)r > iocb->data_length) {
  168. return WSLAY_ERR_INVALID_CALLBACK;
  169. } else {
  170. ctx->opayloadoff += r;
  171. totallen = r;
  172. }
  173. } else {
  174. return WSLAY_ERR_WANT_WRITE;
  175. }
  176. }
  177. }
  178. if(ctx->opayloadoff == ctx->opayloadlen) {
  179. ctx->ostate = PREP_HEADER;
  180. }
  181. return totallen;
  182. }
  183. return WSLAY_ERR_INVALID_ARGUMENT;
  184. }
  185. static void wslay_shift_ibuf(wslay_frame_context_ptr ctx)
  186. {
  187. ptrdiff_t len = ctx->ibuflimit-ctx->ibufmark;
  188. memmove(ctx->ibuf, ctx->ibufmark, len);
  189. ctx->ibuflimit = ctx->ibuf+len;
  190. ctx->ibufmark = ctx->ibuf;
  191. }
  192. static ssize_t wslay_recv(wslay_frame_context_ptr ctx)
  193. {
  194. ssize_t r;
  195. if(ctx->ibufmark != ctx->ibuf) {
  196. wslay_shift_ibuf(ctx);
  197. }
  198. r = ctx->callbacks.recv_callback
  199. (ctx->ibuflimit, ctx->ibuf+sizeof(ctx->ibuf)-ctx->ibuflimit,
  200. 0, ctx->user_data);
  201. if(r > 0) {
  202. ctx->ibuflimit += r;
  203. } else {
  204. r = WSLAY_ERR_WANT_READ;
  205. }
  206. return r;
  207. }
  208. #define WSLAY_AVAIL_IBUF(ctx) ((size_t)(ctx->ibuflimit - ctx->ibufmark))
  209. ssize_t wslay_frame_recv(wslay_frame_context_ptr ctx,
  210. struct wslay_frame_iocb *iocb)
  211. {
  212. ssize_t r;
  213. if(ctx->istate == RECV_HEADER1) {
  214. uint8_t fin, opcode, rsv, payloadlen;
  215. if(WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  216. if((r = wslay_recv(ctx)) <= 0) {
  217. return r;
  218. }
  219. }
  220. if(WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  221. return WSLAY_ERR_WANT_READ;
  222. }
  223. fin = (ctx->ibufmark[0] >> 7) & 1;
  224. rsv = (ctx->ibufmark[0] >> 4) & 7;
  225. opcode = ctx->ibufmark[0] & 0xfu;
  226. ctx->iom.opcode = opcode;
  227. ctx->iom.fin = fin;
  228. ctx->iom.rsv = rsv;
  229. ++ctx->ibufmark;
  230. ctx->imask = (ctx->ibufmark[0] >> 7) & 1;
  231. payloadlen = ctx->ibufmark[0] & 0x7fu;
  232. ++ctx->ibufmark;
  233. if(wslay_is_ctrl_frame(opcode) && (payloadlen > 125 || !fin)) {
  234. return WSLAY_ERR_PROTO;
  235. }
  236. if(payloadlen == 126) {
  237. ctx->istate = RECV_EXT_PAYLOADLEN;
  238. ctx->ireqread = 2;
  239. } else if(payloadlen == 127) {
  240. ctx->istate = RECV_EXT_PAYLOADLEN;
  241. ctx->ireqread = 8;
  242. } else {
  243. ctx->ipayloadlen = payloadlen;
  244. ctx->ipayloadoff = 0;
  245. if(ctx->imask) {
  246. ctx->istate = RECV_MASKKEY;
  247. ctx->ireqread = 4;
  248. } else {
  249. ctx->istate = RECV_PAYLOAD;
  250. }
  251. }
  252. }
  253. if(ctx->istate == RECV_EXT_PAYLOADLEN) {
  254. if(WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  255. if((r = wslay_recv(ctx)) <= 0) {
  256. return r;
  257. }
  258. if(WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  259. return WSLAY_ERR_WANT_READ;
  260. }
  261. }
  262. ctx->ipayloadlen = 0;
  263. ctx->ipayloadoff = 0;
  264. memcpy((uint8_t*)&ctx->ipayloadlen+(8-ctx->ireqread),
  265. ctx->ibufmark, ctx->ireqread);
  266. ctx->ipayloadlen = ntoh64(ctx->ipayloadlen);
  267. ctx->ibufmark += ctx->ireqread;
  268. if(ctx->ireqread == 8) {
  269. if(ctx->ipayloadlen < (1 << 16) ||
  270. ctx->ipayloadlen & (1ull << 63)) {
  271. return WSLAY_ERR_PROTO;
  272. }
  273. } else if(ctx->ipayloadlen < 126) {
  274. return WSLAY_ERR_PROTO;
  275. }
  276. if(ctx->imask) {
  277. ctx->istate = RECV_MASKKEY;
  278. ctx->ireqread = 4;
  279. } else {
  280. ctx->istate = RECV_PAYLOAD;
  281. }
  282. }
  283. if(ctx->istate == RECV_MASKKEY) {
  284. if(WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  285. if((r = wslay_recv(ctx)) <= 0) {
  286. return r;
  287. }
  288. if(WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
  289. return WSLAY_ERR_WANT_READ;
  290. }
  291. }
  292. memcpy(ctx->imaskkey, ctx->ibufmark, 4);
  293. ctx->ibufmark += 4;
  294. ctx->istate = RECV_PAYLOAD;
  295. }
  296. if(ctx->istate == RECV_PAYLOAD) {
  297. uint8_t *readlimit, *readmark;
  298. uint64_t rempayloadlen = ctx->ipayloadlen-ctx->ipayloadoff;
  299. if(WSLAY_AVAIL_IBUF(ctx) == 0 && rempayloadlen > 0) {
  300. if((r = wslay_recv(ctx)) <= 0) {
  301. return r;
  302. }
  303. }
  304. readmark = ctx->ibufmark;
  305. readlimit = WSLAY_AVAIL_IBUF(ctx) < rempayloadlen ?
  306. ctx->ibuflimit : ctx->ibufmark+rempayloadlen;
  307. if(ctx->imask) {
  308. for(; ctx->ibufmark != readlimit;
  309. ++ctx->ibufmark, ++ctx->ipayloadoff) {
  310. ctx->ibufmark[0] ^= ctx->imaskkey[ctx->ipayloadoff % 4];
  311. }
  312. } else {
  313. ctx->ibufmark = readlimit;
  314. ctx->ipayloadoff += readlimit-readmark;
  315. }
  316. iocb->fin = ctx->iom.fin;
  317. iocb->rsv = ctx->iom.rsv;
  318. iocb->opcode = ctx->iom.opcode;
  319. iocb->payload_length = ctx->ipayloadlen;
  320. iocb->mask = ctx->imask;
  321. iocb->data = readmark;
  322. iocb->data_length = ctx->ibufmark-readmark;
  323. if(ctx->ipayloadlen == ctx->ipayloadoff) {
  324. ctx->istate = RECV_HEADER1;
  325. ctx->ireqread = 2;
  326. }
  327. return iocb->data_length;
  328. }
  329. return WSLAY_ERR_INVALID_ARGUMENT;
  330. }