wslay_frame.c 10 KB

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