ops-ws.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include <core/private.h>
  22. #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
  23. /*
  24. * client-parser.c: lws_ws_client_rx_sm() needs to be roughly kept in
  25. * sync with changes here, esp related to ext draining
  26. */
  27. int
  28. lws_ws_rx_sm(struct lws *wsi, char already_processed, unsigned char c)
  29. {
  30. int callback_action = LWS_CALLBACK_RECEIVE;
  31. int ret = 0;
  32. unsigned short close_code;
  33. struct lws_tokens ebuf;
  34. unsigned char *pp;
  35. int n = 0;
  36. #if !defined(LWS_WITHOUT_EXTENSIONS)
  37. int rx_draining_ext = 0;
  38. int lin;
  39. #endif
  40. ebuf.token = NULL;
  41. ebuf.len = 0;
  42. if (wsi->socket_is_permanently_unusable)
  43. return -1;
  44. switch (wsi->lws_rx_parse_state) {
  45. case LWS_RXPS_NEW:
  46. #if !defined(LWS_WITHOUT_EXTENSIONS)
  47. if (wsi->ws->rx_draining_ext) {
  48. ebuf.token = NULL;
  49. ebuf.len = 0;
  50. lws_remove_wsi_from_draining_ext_list(wsi);
  51. rx_draining_ext = 1;
  52. lwsl_debug("%s: doing draining flow\n", __func__);
  53. goto drain_extension;
  54. }
  55. #endif
  56. switch (wsi->ws->ietf_spec_revision) {
  57. case 13:
  58. /*
  59. * no prepended frame key any more
  60. */
  61. wsi->ws->all_zero_nonce = 1;
  62. goto handle_first;
  63. default:
  64. lwsl_warn("lws_ws_rx_sm: unknown spec version %d\n",
  65. wsi->ws->ietf_spec_revision);
  66. break;
  67. }
  68. break;
  69. case LWS_RXPS_04_mask_1:
  70. wsi->ws->mask[1] = c;
  71. if (c)
  72. wsi->ws->all_zero_nonce = 0;
  73. wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
  74. break;
  75. case LWS_RXPS_04_mask_2:
  76. wsi->ws->mask[2] = c;
  77. if (c)
  78. wsi->ws->all_zero_nonce = 0;
  79. wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
  80. break;
  81. case LWS_RXPS_04_mask_3:
  82. wsi->ws->mask[3] = c;
  83. if (c)
  84. wsi->ws->all_zero_nonce = 0;
  85. /*
  86. * start from the zero'th byte in the XOR key buffer since
  87. * this is the start of a frame with a new key
  88. */
  89. wsi->ws->mask_idx = 0;
  90. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
  91. break;
  92. /*
  93. * 04 logical framing from the spec (all this is masked when incoming
  94. * and has to be unmasked)
  95. *
  96. * We ignore the possibility of extension data because we don't
  97. * negotiate any extensions at the moment.
  98. *
  99. * 0 1 2 3
  100. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  101. * +-+-+-+-+-------+-+-------------+-------------------------------+
  102. * |F|R|R|R| opcode|R| Payload len | Extended payload length |
  103. * |I|S|S|S| (4) |S| (7) | (16/63) |
  104. * |N|V|V|V| |V| | (if payload len==126/127) |
  105. * | |1|2|3| |4| | |
  106. * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  107. * | Extended payload length continued, if payload len == 127 |
  108. * + - - - - - - - - - - - - - - - +-------------------------------+
  109. * | | Extension data |
  110. * +-------------------------------+ - - - - - - - - - - - - - - - +
  111. * : :
  112. * +---------------------------------------------------------------+
  113. * : Application data :
  114. * +---------------------------------------------------------------+
  115. *
  116. * We pass payload through to userland as soon as we get it, ignoring
  117. * FIN. It's up to userland to buffer it up if it wants to see a
  118. * whole unfragmented block of the original size (which may be up to
  119. * 2^63 long!)
  120. */
  121. case LWS_RXPS_04_FRAME_HDR_1:
  122. handle_first:
  123. wsi->ws->opcode = c & 0xf;
  124. wsi->ws->rsv = c & 0x70;
  125. wsi->ws->final = !!((c >> 7) & 1);
  126. wsi->ws->defeat_check_utf8 = 0;
  127. if (((wsi->ws->opcode) & 8) && !wsi->ws->final) {
  128. lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR,
  129. (uint8_t *)"frag ctl", 8);
  130. return -1;
  131. }
  132. switch (wsi->ws->opcode) {
  133. case LWSWSOPC_TEXT_FRAME:
  134. wsi->ws->check_utf8 = lws_check_opt(
  135. wsi->context->options,
  136. LWS_SERVER_OPTION_VALIDATE_UTF8);
  137. /* fallthru */
  138. case LWSWSOPC_BINARY_FRAME:
  139. if (wsi->ws->opcode == LWSWSOPC_BINARY_FRAME)
  140. wsi->ws->check_utf8 = 0;
  141. if (wsi->ws->continuation_possible) {
  142. lws_close_reason(wsi,
  143. LWS_CLOSE_STATUS_PROTOCOL_ERR,
  144. (uint8_t *)"bad cont", 8);
  145. return -1;
  146. }
  147. wsi->ws->rsv_first_msg = (c & 0x70);
  148. wsi->ws->frame_is_binary =
  149. wsi->ws->opcode == LWSWSOPC_BINARY_FRAME;
  150. wsi->ws->first_fragment = 1;
  151. wsi->ws->continuation_possible = !wsi->ws->final;
  152. break;
  153. case LWSWSOPC_CONTINUATION:
  154. if (!wsi->ws->continuation_possible) {
  155. lws_close_reason(wsi,
  156. LWS_CLOSE_STATUS_PROTOCOL_ERR,
  157. (uint8_t *)"bad cont", 8);
  158. return -1;
  159. }
  160. break;
  161. case LWSWSOPC_CLOSE:
  162. wsi->ws->check_utf8 = 0;
  163. wsi->ws->utf8 = 0;
  164. break;
  165. case 3:
  166. case 4:
  167. case 5:
  168. case 6:
  169. case 7:
  170. case 0xb:
  171. case 0xc:
  172. case 0xd:
  173. case 0xe:
  174. case 0xf:
  175. lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR,
  176. (uint8_t *)"bad opc", 7);
  177. lwsl_info("illegal opcode\n");
  178. return -1;
  179. }
  180. if (wsi->ws->owed_a_fin &&
  181. (wsi->ws->opcode == LWSWSOPC_TEXT_FRAME ||
  182. wsi->ws->opcode == LWSWSOPC_BINARY_FRAME)) {
  183. lwsl_info("hey you owed us a FIN\n");
  184. lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR,
  185. (uint8_t *)"bad fin", 7);
  186. return -1;
  187. }
  188. if ((!(wsi->ws->opcode & 8)) && wsi->ws->final) {
  189. wsi->ws->continuation_possible = 0;
  190. wsi->ws->owed_a_fin = 0;
  191. }
  192. if (!wsi->ws->final)
  193. wsi->ws->owed_a_fin = 1;
  194. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
  195. if (wsi->ws->rsv &&
  196. (
  197. #if !defined(LWS_WITHOUT_EXTENSIONS)
  198. !wsi->ws->count_act_ext ||
  199. #endif
  200. (wsi->ws->rsv & ~0x40))) {
  201. lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR,
  202. (uint8_t *)"rsv bits", 8);
  203. return -1;
  204. }
  205. break;
  206. case LWS_RXPS_04_FRAME_HDR_LEN:
  207. wsi->ws->this_frame_masked = !!(c & 0x80);
  208. switch (c & 0x7f) {
  209. case 126:
  210. /* control frames are not allowed to have big lengths */
  211. if (wsi->ws->opcode & 8)
  212. goto illegal_ctl_length;
  213. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
  214. break;
  215. case 127:
  216. /* control frames are not allowed to have big lengths */
  217. if (wsi->ws->opcode & 8)
  218. goto illegal_ctl_length;
  219. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
  220. break;
  221. default:
  222. wsi->ws->rx_packet_length = c & 0x7f;
  223. if (wsi->ws->this_frame_masked)
  224. wsi->lws_rx_parse_state =
  225. LWS_RXPS_07_COLLECT_FRAME_KEY_1;
  226. else
  227. if (wsi->ws->rx_packet_length) {
  228. wsi->lws_rx_parse_state =
  229. LWS_RXPS_WS_FRAME_PAYLOAD;
  230. } else {
  231. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  232. goto spill;
  233. }
  234. break;
  235. }
  236. break;
  237. case LWS_RXPS_04_FRAME_HDR_LEN16_2:
  238. wsi->ws->rx_packet_length = c << 8;
  239. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
  240. break;
  241. case LWS_RXPS_04_FRAME_HDR_LEN16_1:
  242. wsi->ws->rx_packet_length |= c;
  243. if (wsi->ws->this_frame_masked)
  244. wsi->lws_rx_parse_state =
  245. LWS_RXPS_07_COLLECT_FRAME_KEY_1;
  246. else {
  247. wsi->lws_rx_parse_state =
  248. LWS_RXPS_WS_FRAME_PAYLOAD;
  249. }
  250. break;
  251. case LWS_RXPS_04_FRAME_HDR_LEN64_8:
  252. if (c & 0x80) {
  253. lwsl_warn("b63 of length must be zero\n");
  254. /* kill the connection */
  255. return -1;
  256. }
  257. #if defined __LP64__
  258. wsi->ws->rx_packet_length = ((size_t)c) << 56;
  259. #else
  260. wsi->ws->rx_packet_length = 0;
  261. #endif
  262. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
  263. break;
  264. case LWS_RXPS_04_FRAME_HDR_LEN64_7:
  265. #if defined __LP64__
  266. wsi->ws->rx_packet_length |= ((size_t)c) << 48;
  267. #endif
  268. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
  269. break;
  270. case LWS_RXPS_04_FRAME_HDR_LEN64_6:
  271. #if defined __LP64__
  272. wsi->ws->rx_packet_length |= ((size_t)c) << 40;
  273. #endif
  274. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
  275. break;
  276. case LWS_RXPS_04_FRAME_HDR_LEN64_5:
  277. #if defined __LP64__
  278. wsi->ws->rx_packet_length |= ((size_t)c) << 32;
  279. #endif
  280. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
  281. break;
  282. case LWS_RXPS_04_FRAME_HDR_LEN64_4:
  283. wsi->ws->rx_packet_length |= ((size_t)c) << 24;
  284. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
  285. break;
  286. case LWS_RXPS_04_FRAME_HDR_LEN64_3:
  287. wsi->ws->rx_packet_length |= ((size_t)c) << 16;
  288. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
  289. break;
  290. case LWS_RXPS_04_FRAME_HDR_LEN64_2:
  291. wsi->ws->rx_packet_length |= ((size_t)c) << 8;
  292. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
  293. break;
  294. case LWS_RXPS_04_FRAME_HDR_LEN64_1:
  295. wsi->ws->rx_packet_length |= ((size_t)c);
  296. if (wsi->ws->this_frame_masked)
  297. wsi->lws_rx_parse_state =
  298. LWS_RXPS_07_COLLECT_FRAME_KEY_1;
  299. else
  300. wsi->lws_rx_parse_state = LWS_RXPS_WS_FRAME_PAYLOAD;
  301. break;
  302. case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
  303. wsi->ws->mask[0] = c;
  304. if (c)
  305. wsi->ws->all_zero_nonce = 0;
  306. wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
  307. break;
  308. case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
  309. wsi->ws->mask[1] = c;
  310. if (c)
  311. wsi->ws->all_zero_nonce = 0;
  312. wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
  313. break;
  314. case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
  315. wsi->ws->mask[2] = c;
  316. if (c)
  317. wsi->ws->all_zero_nonce = 0;
  318. wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
  319. break;
  320. case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
  321. wsi->ws->mask[3] = c;
  322. if (c)
  323. wsi->ws->all_zero_nonce = 0;
  324. wsi->lws_rx_parse_state = LWS_RXPS_WS_FRAME_PAYLOAD;
  325. wsi->ws->mask_idx = 0;
  326. if (wsi->ws->rx_packet_length == 0) {
  327. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  328. goto spill;
  329. }
  330. break;
  331. case LWS_RXPS_WS_FRAME_PAYLOAD:
  332. assert(wsi->ws->rx_ubuf);
  333. if (wsi->ws->rx_ubuf_head + LWS_PRE >= wsi->ws->rx_ubuf_alloc) {
  334. lwsl_err("Attempted overflow \n");
  335. return -1;
  336. }
  337. if (!(already_processed & ALREADY_PROCESSED_IGNORE_CHAR)) {
  338. if (wsi->ws->all_zero_nonce)
  339. wsi->ws->rx_ubuf[LWS_PRE +
  340. (wsi->ws->rx_ubuf_head++)] = c;
  341. else
  342. wsi->ws->rx_ubuf[LWS_PRE +
  343. (wsi->ws->rx_ubuf_head++)] =
  344. c ^ wsi->ws->mask[(wsi->ws->mask_idx++) & 3];
  345. --wsi->ws->rx_packet_length;
  346. }
  347. if (!wsi->ws->rx_packet_length) {
  348. lwsl_debug("%s: ws fragment length exhausted\n",
  349. __func__);
  350. /* spill because we have the whole frame */
  351. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  352. goto spill;
  353. }
  354. #if !defined(LWS_WITHOUT_EXTENSIONS)
  355. if (wsi->ws->rx_draining_ext) {
  356. lwsl_debug("%s: UNTIL_EXHAUSTED draining\n", __func__);
  357. goto drain_extension;
  358. }
  359. #endif
  360. /*
  361. * if there's no protocol max frame size given, we are
  362. * supposed to default to context->pt_serv_buf_size
  363. */
  364. if (!wsi->protocol->rx_buffer_size &&
  365. wsi->ws->rx_ubuf_head != wsi->context->pt_serv_buf_size)
  366. break;
  367. if (wsi->protocol->rx_buffer_size &&
  368. wsi->ws->rx_ubuf_head != wsi->protocol->rx_buffer_size)
  369. break;
  370. /* spill because we filled our rx buffer */
  371. spill:
  372. /*
  373. * is this frame a control packet we should take care of at this
  374. * layer? If so service it and hide it from the user callback
  375. */
  376. lwsl_parser("spill on %s\n", wsi->protocol->name);
  377. switch (wsi->ws->opcode) {
  378. case LWSWSOPC_CLOSE:
  379. if (wsi->ws->peer_has_sent_close)
  380. break;
  381. wsi->ws->peer_has_sent_close = 1;
  382. pp = (unsigned char *)&wsi->ws->rx_ubuf[LWS_PRE];
  383. if (lws_check_opt(wsi->context->options,
  384. LWS_SERVER_OPTION_VALIDATE_UTF8) &&
  385. wsi->ws->rx_ubuf_head > 2 &&
  386. lws_check_utf8(&wsi->ws->utf8, pp + 2,
  387. wsi->ws->rx_ubuf_head - 2))
  388. goto utf8_fail;
  389. /* is this an acknowledgment of our close? */
  390. if (lwsi_state(wsi) == LRS_AWAITING_CLOSE_ACK) {
  391. /*
  392. * fine he has told us he is closing too, let's
  393. * finish our close
  394. */
  395. lwsl_parser("seen client close ack\n");
  396. return -1;
  397. }
  398. if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
  399. /* if he sends us 2 CLOSE, kill him */
  400. return -1;
  401. if (lws_partial_buffered(wsi)) {
  402. /*
  403. * if we're in the middle of something,
  404. * we can't do a normal close response and
  405. * have to just close our end.
  406. */
  407. wsi->socket_is_permanently_unusable = 1;
  408. lwsl_parser("Closing on peer close "
  409. "due to pending tx\n");
  410. return -1;
  411. }
  412. if (wsi->ws->rx_ubuf_head >= 2) {
  413. close_code = (pp[0] << 8) | pp[1];
  414. if (close_code < 1000 ||
  415. close_code == 1004 ||
  416. close_code == 1005 ||
  417. close_code == 1006 ||
  418. close_code == 1012 ||
  419. close_code == 1013 ||
  420. close_code == 1014 ||
  421. close_code == 1015 ||
  422. (close_code >= 1016 && close_code < 3000)
  423. ) {
  424. pp[0] = (LWS_CLOSE_STATUS_PROTOCOL_ERR >> 8) & 0xff;
  425. pp[1] = LWS_CLOSE_STATUS_PROTOCOL_ERR & 0xff;
  426. }
  427. }
  428. if (user_callback_handle_rxflow(
  429. wsi->protocol->callback, wsi,
  430. LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
  431. wsi->user_space,
  432. &wsi->ws->rx_ubuf[LWS_PRE],
  433. wsi->ws->rx_ubuf_head))
  434. return -1;
  435. lwsl_parser("server sees client close packet\n");
  436. lwsi_set_state(wsi, LRS_RETURNED_CLOSE);
  437. /* deal with the close packet contents as a PONG */
  438. wsi->ws->payload_is_close = 1;
  439. goto process_as_ping;
  440. case LWSWSOPC_PING:
  441. lwsl_info("received %d byte ping, sending pong\n",
  442. wsi->ws->rx_ubuf_head);
  443. if (wsi->ws->ping_pending_flag) {
  444. /*
  445. * there is already a pending ping payload
  446. * we should just log and drop
  447. */
  448. lwsl_parser("DROP PING since one pending\n");
  449. goto ping_drop;
  450. }
  451. process_as_ping:
  452. /* control packets can only be < 128 bytes long */
  453. if (wsi->ws->rx_ubuf_head > 128 - 3) {
  454. lwsl_parser("DROP PING payload too large\n");
  455. goto ping_drop;
  456. }
  457. /* stash the pong payload */
  458. memcpy(wsi->ws->ping_payload_buf + LWS_PRE,
  459. &wsi->ws->rx_ubuf[LWS_PRE],
  460. wsi->ws->rx_ubuf_head);
  461. wsi->ws->ping_payload_len = wsi->ws->rx_ubuf_head;
  462. wsi->ws->ping_pending_flag = 1;
  463. /* get it sent as soon as possible */
  464. lws_callback_on_writable(wsi);
  465. ping_drop:
  466. wsi->ws->rx_ubuf_head = 0;
  467. return 0;
  468. case LWSWSOPC_PONG:
  469. lwsl_info("received pong\n");
  470. lwsl_hexdump(&wsi->ws->rx_ubuf[LWS_PRE],
  471. wsi->ws->rx_ubuf_head);
  472. if (wsi->pending_timeout ==
  473. PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG) {
  474. lwsl_info("received expected PONG on wsi %p\n",
  475. wsi);
  476. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  477. }
  478. /* issue it */
  479. callback_action = LWS_CALLBACK_RECEIVE_PONG;
  480. break;
  481. case LWSWSOPC_TEXT_FRAME:
  482. case LWSWSOPC_BINARY_FRAME:
  483. case LWSWSOPC_CONTINUATION:
  484. break;
  485. default:
  486. lwsl_parser("unknown opc %x\n", wsi->ws->opcode);
  487. return -1;
  488. }
  489. /*
  490. * No it's real payload, pass it up to the user callback.
  491. * It's nicely buffered with the pre-padding taken care of
  492. * so it can be sent straight out again using lws_write
  493. */
  494. ebuf.token = &wsi->ws->rx_ubuf[LWS_PRE];
  495. ebuf.len = wsi->ws->rx_ubuf_head;
  496. if (wsi->ws->opcode == LWSWSOPC_PONG && !ebuf.len)
  497. goto already_done;
  498. #if !defined(LWS_WITHOUT_EXTENSIONS)
  499. drain_extension:
  500. #endif
  501. // lwsl_notice("%s: passing %d to ext\n", __func__, ebuf.len);
  502. if (lwsi_state(wsi) == LRS_RETURNED_CLOSE ||
  503. lwsi_state(wsi) == LRS_AWAITING_CLOSE_ACK)
  504. goto already_done;
  505. #if !defined(LWS_WITHOUT_EXTENSIONS)
  506. lin = ebuf.len;
  507. //if (lin)
  508. // lwsl_hexdump_notice(ebuf.token, ebuf.len);
  509. n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &ebuf, 0);
  510. lwsl_debug("%s: ext says %d / ebuf.len %d\n", __func__,
  511. n, ebuf.len);
  512. if (wsi->ws->rx_draining_ext)
  513. already_processed &= ~ALREADY_PROCESSED_NO_CB;
  514. #endif
  515. /*
  516. * ebuf may be pointing somewhere completely different now,
  517. * it's the output
  518. */
  519. #if !defined(LWS_WITHOUT_EXTENSIONS)
  520. if (n < 0) {
  521. /*
  522. * we may rely on this to get RX, just drop connection
  523. */
  524. wsi->socket_is_permanently_unusable = 1;
  525. return -1;
  526. }
  527. #endif
  528. if (
  529. #if !defined(LWS_WITHOUT_EXTENSIONS)
  530. rx_draining_ext &&
  531. #endif
  532. ebuf.len == 0)
  533. goto already_done;
  534. if (
  535. #if !defined(LWS_WITHOUT_EXTENSIONS)
  536. n &&
  537. #endif
  538. ebuf.len)
  539. /* extension had more... main loop will come back */
  540. lws_add_wsi_to_draining_ext_list(wsi);
  541. else
  542. lws_remove_wsi_from_draining_ext_list(wsi);
  543. if (wsi->ws->check_utf8 && !wsi->ws->defeat_check_utf8) {
  544. if (lws_check_utf8(&wsi->ws->utf8,
  545. (unsigned char *)ebuf.token,
  546. ebuf.len)) {
  547. lws_close_reason(wsi,
  548. LWS_CLOSE_STATUS_INVALID_PAYLOAD,
  549. (uint8_t *)"bad utf8", 8);
  550. goto utf8_fail;
  551. }
  552. /* we are ending partway through utf-8 character? */
  553. if (!wsi->ws->rx_packet_length && wsi->ws->final &&
  554. wsi->ws->utf8 && !n) {
  555. lwsl_info("FINAL utf8 error\n");
  556. lws_close_reason(wsi,
  557. LWS_CLOSE_STATUS_INVALID_PAYLOAD,
  558. (uint8_t *)"partial utf8", 12);
  559. utf8_fail:
  560. lwsl_notice("utf8 error\n");
  561. lwsl_hexdump_notice(ebuf.token, ebuf.len);
  562. return -1;
  563. }
  564. }
  565. if (!wsi->wsistate_pre_close && (ebuf.len >= 0 ||
  566. callback_action == LWS_CALLBACK_RECEIVE_PONG)) {
  567. if (ebuf.len)
  568. ebuf.token[ebuf.len] = '\0';
  569. if (wsi->protocol->callback &&
  570. !(already_processed & ALREADY_PROCESSED_NO_CB)) {
  571. if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
  572. lwsl_info("Doing pong callback\n");
  573. ret = user_callback_handle_rxflow(
  574. wsi->protocol->callback,
  575. wsi, (enum lws_callback_reasons)
  576. callback_action,
  577. wsi->user_space,
  578. ebuf.token,
  579. ebuf.len);
  580. }
  581. wsi->ws->first_fragment = 0;
  582. }
  583. #if !defined(LWS_WITHOUT_EXTENSIONS)
  584. if (!lin)
  585. break;
  586. #endif
  587. already_done:
  588. wsi->ws->rx_ubuf_head = 0;
  589. break;
  590. }
  591. return ret;
  592. illegal_ctl_length:
  593. lwsl_warn("Control frame with xtended length is illegal\n");
  594. /* kill the connection */
  595. return -1;
  596. }
  597. LWS_VISIBLE size_t
  598. lws_remaining_packet_payload(struct lws *wsi)
  599. {
  600. return wsi->ws->rx_packet_length;
  601. }
  602. LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
  603. {
  604. return wsi->ws->frame_is_binary;
  605. }
  606. void
  607. lws_add_wsi_to_draining_ext_list(struct lws *wsi)
  608. {
  609. #if !defined(LWS_WITHOUT_EXTENSIONS)
  610. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  611. if (wsi->ws->rx_draining_ext)
  612. return;
  613. lwsl_debug("%s: RX EXT DRAINING: Adding to list\n", __func__);
  614. wsi->ws->rx_draining_ext = 1;
  615. wsi->ws->rx_draining_ext_list = pt->ws.rx_draining_ext_list;
  616. pt->ws.rx_draining_ext_list = wsi;
  617. #endif
  618. }
  619. void
  620. lws_remove_wsi_from_draining_ext_list(struct lws *wsi)
  621. {
  622. #if !defined(LWS_WITHOUT_EXTENSIONS)
  623. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  624. struct lws **w = &pt->ws.rx_draining_ext_list;
  625. if (!wsi->ws->rx_draining_ext)
  626. return;
  627. lwsl_debug("%s: RX EXT DRAINING: Removing from list\n", __func__);
  628. wsi->ws->rx_draining_ext = 0;
  629. /* remove us from context draining ext list */
  630. while (*w) {
  631. if (*w == wsi) {
  632. /* if us, point it instead to who we were pointing to */
  633. *w = wsi->ws->rx_draining_ext_list;
  634. break;
  635. }
  636. w = &((*w)->ws->rx_draining_ext_list);
  637. }
  638. wsi->ws->rx_draining_ext_list = NULL;
  639. #endif
  640. }
  641. LWS_EXTERN void
  642. lws_restart_ws_ping_pong_timer(struct lws *wsi)
  643. {
  644. if (!wsi->context->ws_ping_pong_interval ||
  645. !lwsi_role_ws(wsi))
  646. return;
  647. wsi->ws->time_next_ping_check = (time_t)lws_now_secs();
  648. }
  649. static int
  650. lws_0405_frame_mask_generate(struct lws *wsi)
  651. {
  652. int n;
  653. /* fetch the per-frame nonce */
  654. n = lws_get_random(lws_get_context(wsi), wsi->ws->mask, 4);
  655. if (n != 4) {
  656. lwsl_parser("Unable to read from random device %s %d\n",
  657. SYSTEM_RANDOM_FILEPATH, n);
  658. return 1;
  659. }
  660. /* start masking from first byte of masking key buffer */
  661. wsi->ws->mask_idx = 0;
  662. return 0;
  663. }
  664. int
  665. lws_server_init_wsi_for_ws(struct lws *wsi)
  666. {
  667. int n;
  668. lwsi_set_state(wsi, LRS_ESTABLISHED);
  669. lws_restart_ws_ping_pong_timer(wsi);
  670. /*
  671. * create the frame buffer for this connection according to the
  672. * size mentioned in the protocol definition. If 0 there, use
  673. * a big default for compatibility
  674. */
  675. n = (int)wsi->protocol->rx_buffer_size;
  676. if (!n)
  677. n = wsi->context->pt_serv_buf_size;
  678. n += LWS_PRE;
  679. wsi->ws->rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */, "rx_ubuf");
  680. if (!wsi->ws->rx_ubuf) {
  681. lwsl_err("Out of Mem allocating rx buffer %d\n", n);
  682. return 1;
  683. }
  684. wsi->ws->rx_ubuf_alloc = n;
  685. lwsl_debug("Allocating RX buffer %d\n", n);
  686. #if !defined(LWS_WITH_ESP32)
  687. if (!wsi->h2_stream_carries_ws)
  688. if (setsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_SNDBUF,
  689. (const char *)&n, sizeof n)) {
  690. lwsl_warn("Failed to set SNDBUF to %d", n);
  691. return 1;
  692. }
  693. #endif
  694. /* notify user code that we're ready to roll */
  695. if (wsi->protocol->callback)
  696. if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
  697. wsi->user_space,
  698. #ifdef LWS_WITH_TLS
  699. wsi->tls.ssl,
  700. #else
  701. NULL,
  702. #endif
  703. wsi->h2_stream_carries_ws))
  704. return 1;
  705. lwsl_debug("ws established\n");
  706. return 0;
  707. }
  708. LWS_VISIBLE int
  709. lws_is_final_fragment(struct lws *wsi)
  710. {
  711. #if !defined(LWS_WITHOUT_EXTENSIONS)
  712. lwsl_debug("%s: final %d, rx pk length %ld, draining %ld\n", __func__,
  713. wsi->ws->final, (long)wsi->ws->rx_packet_length,
  714. (long)wsi->ws->rx_draining_ext);
  715. return wsi->ws->final && !wsi->ws->rx_packet_length &&
  716. !wsi->ws->rx_draining_ext;
  717. #else
  718. return wsi->ws->final && !wsi->ws->rx_packet_length;
  719. #endif
  720. }
  721. LWS_VISIBLE int
  722. lws_is_first_fragment(struct lws *wsi)
  723. {
  724. return wsi->ws->first_fragment;
  725. }
  726. LWS_VISIBLE unsigned char
  727. lws_get_reserved_bits(struct lws *wsi)
  728. {
  729. return wsi->ws->rsv;
  730. }
  731. LWS_VISIBLE LWS_EXTERN int
  732. lws_get_close_length(struct lws *wsi)
  733. {
  734. return wsi->ws->close_in_ping_buffer_len;
  735. }
  736. LWS_VISIBLE LWS_EXTERN unsigned char *
  737. lws_get_close_payload(struct lws *wsi)
  738. {
  739. return &wsi->ws->ping_payload_buf[LWS_PRE];
  740. }
  741. LWS_VISIBLE LWS_EXTERN void
  742. lws_close_reason(struct lws *wsi, enum lws_close_status status,
  743. unsigned char *buf, size_t len)
  744. {
  745. unsigned char *p, *start;
  746. int budget = sizeof(wsi->ws->ping_payload_buf) - LWS_PRE;
  747. assert(lwsi_role_ws(wsi));
  748. start = p = &wsi->ws->ping_payload_buf[LWS_PRE];
  749. *p++ = (((int)status) >> 8) & 0xff;
  750. *p++ = ((int)status) & 0xff;
  751. if (buf)
  752. while (len-- && p < start + budget)
  753. *p++ = *buf++;
  754. wsi->ws->close_in_ping_buffer_len = lws_ptr_diff(p, start);
  755. }
  756. static int
  757. lws_is_ws_with_ext(struct lws *wsi)
  758. {
  759. #if defined(LWS_WITHOUT_EXTENSIONS)
  760. return 0;
  761. #else
  762. return lwsi_role_ws(wsi) && !!wsi->ws->count_act_ext;
  763. #endif
  764. }
  765. static int
  766. rops_handle_POLLIN_ws(struct lws_context_per_thread *pt, struct lws *wsi,
  767. struct lws_pollfd *pollfd)
  768. {
  769. unsigned int pending = 0;
  770. struct lws_tokens ebuf;
  771. char buffered = 0;
  772. int n = 0, m;
  773. #if defined(LWS_WITH_HTTP2)
  774. struct lws *wsi1;
  775. #endif
  776. if (!wsi->ws) {
  777. lwsl_err("ws role wsi with no ws\n");
  778. return 1;
  779. }
  780. // lwsl_notice("%s: %s\n", __func__, wsi->protocol->name);
  781. //lwsl_info("%s: wsistate 0x%x, pollout %d\n", __func__,
  782. // wsi->wsistate, pollfd->revents & LWS_POLLOUT);
  783. /*
  784. * something went wrong with parsing the handshake, and
  785. * we ended up back in the event loop without completing it
  786. */
  787. if (lwsi_state(wsi) == LRS_PRE_WS_SERVING_ACCEPT) {
  788. wsi->socket_is_permanently_unusable = 1;
  789. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  790. }
  791. ebuf.token = NULL;
  792. ebuf.len = 0;
  793. if (lwsi_state(wsi) == LRS_WAITING_CONNECT) {
  794. #if !defined(LWS_NO_CLIENT)
  795. if ((pollfd->revents & LWS_POLLOUT) &&
  796. lws_handle_POLLOUT_event(wsi, pollfd)) {
  797. lwsl_debug("POLLOUT event closed it\n");
  798. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  799. }
  800. n = lws_client_socket_service(wsi, pollfd, NULL);
  801. if (n)
  802. return LWS_HPI_RET_WSI_ALREADY_DIED;
  803. #endif
  804. return LWS_HPI_RET_HANDLED;
  805. }
  806. //lwsl_notice("%s: wsi->ws->tx_draining_ext %d revents 0x%x 0x%x %d\n",
  807. //__func__, wsi->ws->tx_draining_ext, pollfd->revents, wsi->wsistate,
  808. //lwsi_state_can_handle_POLLOUT(wsi));
  809. /* 1: something requested a callback when it was OK to write */
  810. if ((pollfd->revents & LWS_POLLOUT) &&
  811. lwsi_state_can_handle_POLLOUT(wsi) &&
  812. lws_handle_POLLOUT_event(wsi, pollfd)) {
  813. if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
  814. lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE);
  815. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  816. }
  817. if (lwsi_state(wsi) == LRS_RETURNED_CLOSE ||
  818. lwsi_state(wsi) == LRS_WAITING_TO_SEND_CLOSE) {
  819. /*
  820. * we stopped caring about anything except control
  821. * packets. Force flow control off, defeat tx
  822. * draining.
  823. */
  824. lws_rx_flow_control(wsi, 1);
  825. #if !defined(LWS_WITHOUT_EXTENSIONS)
  826. if (wsi->ws)
  827. wsi->ws->tx_draining_ext = 0;
  828. #endif
  829. }
  830. #if !defined(LWS_WITHOUT_EXTENSIONS)
  831. if (wsi->ws->tx_draining_ext)
  832. /*
  833. * We cannot deal with new RX until the TX ext path has
  834. * been drained. It's because new rx will, eg, crap on
  835. * the wsi rx buf that may be needed to retain state.
  836. *
  837. * TX ext drain path MUST go through event loop to avoid
  838. * blocking.
  839. */
  840. return LWS_HPI_RET_HANDLED;
  841. #endif
  842. if (lws_is_flowcontrolled(wsi)) {
  843. /* We cannot deal with any kind of new RX because we are
  844. * RX-flowcontrolled.
  845. */
  846. lwsl_info("flowcontrolled\n");
  847. return LWS_HPI_RET_HANDLED;
  848. }
  849. #if defined(LWS_WITH_HTTP2)
  850. if (wsi->http2_substream || wsi->upgraded_to_http2) {
  851. wsi1 = lws_get_network_wsi(wsi);
  852. if (wsi1 && lws_has_buffered_out(wsi1))
  853. /* We cannot deal with any kind of new RX
  854. * because we are dealing with a partial send
  855. * (new RX may trigger new http_action() that
  856. * expect to be able to send)
  857. */
  858. return LWS_HPI_RET_HANDLED;
  859. }
  860. #endif
  861. #if !defined(LWS_WITHOUT_EXTENSIONS)
  862. /* 2: RX Extension needs to be drained
  863. */
  864. if (wsi->ws->rx_draining_ext) {
  865. lwsl_debug("%s: RX EXT DRAINING: Service\n", __func__);
  866. #ifndef LWS_NO_CLIENT
  867. if (lwsi_role_client(wsi)) {
  868. n = lws_ws_client_rx_sm(wsi, 0);
  869. if (n < 0)
  870. /* we closed wsi */
  871. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  872. } else
  873. #endif
  874. n = lws_ws_rx_sm(wsi, ALREADY_PROCESSED_IGNORE_CHAR, 0);
  875. return LWS_HPI_RET_HANDLED;
  876. }
  877. if (wsi->ws->rx_draining_ext)
  878. /*
  879. * We have RX EXT content to drain, but can't do it
  880. * right now. That means we cannot do anything lower
  881. * priority either.
  882. */
  883. return LWS_HPI_RET_HANDLED;
  884. #endif
  885. /* 3: buflist needs to be drained
  886. */
  887. read:
  888. //lws_buflist_describe(&wsi->buflist, wsi);
  889. ebuf.len = (int)lws_buflist_next_segment_len(&wsi->buflist,
  890. (uint8_t **)&ebuf.token);
  891. if (ebuf.len) {
  892. lwsl_info("draining buflist (len %d)\n", ebuf.len);
  893. buffered = 1;
  894. goto drain;
  895. }
  896. if (!(pollfd->revents & pollfd->events & LWS_POLLIN) && !wsi->http.ah)
  897. return LWS_HPI_RET_HANDLED;
  898. if (lws_is_flowcontrolled(wsi)) {
  899. lwsl_info("%s: %p should be rxflow (bm 0x%x)..\n",
  900. __func__, wsi, wsi->rxflow_bitmap);
  901. return LWS_HPI_RET_HANDLED;
  902. }
  903. if (!(lwsi_role_client(wsi) &&
  904. (lwsi_state(wsi) != LRS_ESTABLISHED &&
  905. lwsi_state(wsi) != LRS_AWAITING_CLOSE_ACK &&
  906. lwsi_state(wsi) != LRS_H2_WAITING_TO_SEND_HEADERS))) {
  907. /*
  908. * In case we are going to react to this rx by scheduling
  909. * writes, we need to restrict the amount of rx to the size
  910. * the protocol reported for rx buffer.
  911. *
  912. * Otherwise we get a situation we have to absorb possibly a
  913. * lot of reads before we get a chance to drain them by writing
  914. * them, eg, with echo type tests in autobahn.
  915. */
  916. buffered = 0;
  917. ebuf.token = (char *)pt->serv_buf;
  918. if (lwsi_role_ws(wsi))
  919. ebuf.len = wsi->ws->rx_ubuf_alloc;
  920. else
  921. ebuf.len = wsi->context->pt_serv_buf_size;
  922. if ((unsigned int)ebuf.len > wsi->context->pt_serv_buf_size)
  923. ebuf.len = wsi->context->pt_serv_buf_size;
  924. if ((int)pending > ebuf.len)
  925. pending = ebuf.len;
  926. ebuf.len = lws_ssl_capable_read(wsi, (uint8_t *)ebuf.token,
  927. pending ? (int)pending :
  928. ebuf.len);
  929. switch (ebuf.len) {
  930. case 0:
  931. lwsl_info("%s: zero length read\n",
  932. __func__);
  933. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  934. case LWS_SSL_CAPABLE_MORE_SERVICE:
  935. lwsl_info("SSL Capable more service\n");
  936. return LWS_HPI_RET_HANDLED;
  937. case LWS_SSL_CAPABLE_ERROR:
  938. lwsl_info("%s: LWS_SSL_CAPABLE_ERROR\n",
  939. __func__);
  940. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  941. }
  942. // lwsl_notice("Actual RX %d\n", ebuf.len);
  943. lws_restart_ws_ping_pong_timer(wsi);
  944. /*
  945. * coverity thinks ssl_capable_read() may read over
  946. * 2GB. Dissuade it...
  947. */
  948. ebuf.len &= 0x7fffffff;
  949. }
  950. drain:
  951. /*
  952. * give any active extensions a chance to munge the buffer
  953. * before parse. We pass in a pointer to an lws_tokens struct
  954. * prepared with the default buffer and content length that's in
  955. * there. Rather than rewrite the default buffer, extensions
  956. * that expect to grow the buffer can adapt .token to
  957. * point to their own per-connection buffer in the extension
  958. * user allocation. By default with no extensions or no
  959. * extension callback handling, just the normal input buffer is
  960. * used then so it is efficient.
  961. */
  962. m = 0;
  963. do {
  964. /* service incoming data */
  965. //lws_buflist_describe(&wsi->buflist, wsi);
  966. if (ebuf.len) {
  967. #if defined(LWS_ROLE_H2)
  968. if (lwsi_role_h2(wsi) && lwsi_state(wsi) != LRS_BODY)
  969. n = lws_read_h2(wsi, (unsigned char *)ebuf.token,
  970. ebuf.len);
  971. else
  972. #endif
  973. n = lws_read_h1(wsi, (unsigned char *)ebuf.token,
  974. ebuf.len);
  975. if (n < 0) {
  976. /* we closed wsi */
  977. n = 0;
  978. return LWS_HPI_RET_WSI_ALREADY_DIED;
  979. }
  980. //lws_buflist_describe(&wsi->buflist, wsi);
  981. //lwsl_notice("%s: consuming %d / %d\n", __func__, n, ebuf.len);
  982. if (lws_buflist_aware_consume(wsi, &ebuf, n, buffered))
  983. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  984. }
  985. ebuf.token = NULL;
  986. ebuf.len = 0;
  987. } while (m);
  988. if (wsi->http.ah
  989. #if !defined(LWS_NO_CLIENT)
  990. && !wsi->client_h2_alpn
  991. #endif
  992. ) {
  993. lwsl_info("%s: %p: detaching ah\n", __func__, wsi);
  994. lws_header_table_detach(wsi, 0);
  995. }
  996. pending = lws_ssl_pending(wsi);
  997. if (pending) {
  998. if (lws_is_ws_with_ext(wsi))
  999. pending = pending > wsi->ws->rx_ubuf_alloc ?
  1000. wsi->ws->rx_ubuf_alloc : pending;
  1001. else
  1002. pending = pending > wsi->context->pt_serv_buf_size ?
  1003. wsi->context->pt_serv_buf_size : pending;
  1004. goto read;
  1005. }
  1006. if (buffered && /* were draining, now nothing left */
  1007. !lws_buflist_next_segment_len(&wsi->buflist, NULL)) {
  1008. lwsl_info("%s: %p flow buf: drained\n", __func__, wsi);
  1009. /* having drained the rxflow buffer, can rearm POLLIN */
  1010. #ifdef LWS_NO_SERVER
  1011. n =
  1012. #endif
  1013. __lws_rx_flow_control(wsi);
  1014. /* n ignored, needed for NO_SERVER case */
  1015. }
  1016. /* n = 0 */
  1017. return LWS_HPI_RET_HANDLED;
  1018. }
  1019. int rops_handle_POLLOUT_ws(struct lws *wsi)
  1020. {
  1021. int write_type = LWS_WRITE_PONG;
  1022. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1023. struct lws_tokens ebuf;
  1024. int ret, m;
  1025. #endif
  1026. int n;
  1027. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1028. lwsl_debug("%s: %s: wsi->ws->tx_draining_ext %d\n", __func__,
  1029. wsi->protocol->name, wsi->ws->tx_draining_ext);
  1030. #endif
  1031. /* Priority 3: pending control packets (pong or close)
  1032. *
  1033. * 3a: close notification packet requested from close api
  1034. */
  1035. if (lwsi_state(wsi) == LRS_WAITING_TO_SEND_CLOSE) {
  1036. lwsl_debug("sending close packet\n");
  1037. lwsl_hexdump_debug(&wsi->ws->ping_payload_buf[LWS_PRE],
  1038. wsi->ws->close_in_ping_buffer_len);
  1039. wsi->waiting_to_send_close_frame = 0;
  1040. n = lws_write(wsi, &wsi->ws->ping_payload_buf[LWS_PRE],
  1041. wsi->ws->close_in_ping_buffer_len,
  1042. LWS_WRITE_CLOSE);
  1043. if (n >= 0) {
  1044. if (wsi->close_needs_ack) {
  1045. lwsi_set_state(wsi, LRS_AWAITING_CLOSE_ACK);
  1046. lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK,
  1047. 5);
  1048. lwsl_debug("sent close, await ack\n");
  1049. return LWS_HP_RET_BAIL_OK;
  1050. }
  1051. wsi->close_needs_ack = 0;
  1052. lwsi_set_state(wsi, LRS_RETURNED_CLOSE);
  1053. }
  1054. return LWS_HP_RET_BAIL_DIE;
  1055. }
  1056. /* else, the send failed and we should just hang up */
  1057. if ((lwsi_role_ws(wsi) && wsi->ws->ping_pending_flag) ||
  1058. (lwsi_state(wsi) == LRS_RETURNED_CLOSE &&
  1059. wsi->ws->payload_is_close)) {
  1060. if (wsi->ws->payload_is_close)
  1061. write_type = LWS_WRITE_CLOSE;
  1062. else {
  1063. if (wsi->wsistate_pre_close) {
  1064. /* we started close flow, forget pong */
  1065. wsi->ws->ping_pending_flag = 0;
  1066. return LWS_HP_RET_BAIL_OK;
  1067. }
  1068. lwsl_info("issuing pong %d on wsi %p\n",
  1069. wsi->ws->ping_payload_len, wsi);
  1070. }
  1071. n = lws_write(wsi, &wsi->ws->ping_payload_buf[LWS_PRE],
  1072. wsi->ws->ping_payload_len, write_type);
  1073. if (n < 0)
  1074. return LWS_HP_RET_BAIL_DIE;
  1075. /* well he is sent, mark him done */
  1076. wsi->ws->ping_pending_flag = 0;
  1077. if (wsi->ws->payload_is_close) {
  1078. // assert(0);
  1079. /* oh... a close frame was it... then we are done */
  1080. return LWS_HP_RET_BAIL_DIE;
  1081. }
  1082. /* otherwise for PING, leave POLLOUT active either way */
  1083. return LWS_HP_RET_BAIL_OK;
  1084. }
  1085. if (!wsi->socket_is_permanently_unusable && wsi->ws->send_check_ping) {
  1086. lwsl_info("issuing ping on wsi %p\n", wsi);
  1087. wsi->ws->send_check_ping = 0;
  1088. n = lws_write(wsi, &wsi->ws->ping_payload_buf[LWS_PRE],
  1089. 0, LWS_WRITE_PING);
  1090. if (n < 0)
  1091. return LWS_HP_RET_BAIL_DIE;
  1092. /*
  1093. * we apparently were able to send the PING in a reasonable time
  1094. * now reset the clock on our peer to be able to send the
  1095. * PONG in a reasonable time.
  1096. */
  1097. lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG,
  1098. wsi->context->timeout_secs);
  1099. return LWS_HP_RET_BAIL_OK;
  1100. }
  1101. /* Priority 4: if we are closing, not allowed to send more data frags
  1102. * which means user callback or tx ext flush banned now
  1103. */
  1104. if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
  1105. return LWS_HP_RET_USER_SERVICE;
  1106. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1107. /* Priority 5: Tx path extension with more to send
  1108. *
  1109. * These are handled as new fragments each time around
  1110. * So while we must block new writeable callback to enforce
  1111. * payload ordering, but since they are always complete
  1112. * fragments control packets can interleave OK.
  1113. */
  1114. if (wsi->ws->tx_draining_ext) {
  1115. lwsl_ext("SERVICING TX EXT DRAINING\n");
  1116. if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
  1117. return LWS_HP_RET_BAIL_DIE;
  1118. /* leave POLLOUT active */
  1119. return LWS_HP_RET_BAIL_OK;
  1120. }
  1121. /* Priority 6: extensions
  1122. */
  1123. if (!wsi->ws->extension_data_pending && !wsi->ws->tx_draining_ext) {
  1124. lwsl_ext("%s: !wsi->ws->extension_data_pending\n", __func__);
  1125. return LWS_HP_RET_USER_SERVICE;
  1126. }
  1127. /*
  1128. * check in on the active extensions, see if they
  1129. * had pending stuff to spill... they need to get the
  1130. * first look-in otherwise sequence will be disordered
  1131. *
  1132. * NULL, zero-length ebuf means just spill pending
  1133. */
  1134. ret = 1;
  1135. if (wsi->role_ops == &role_ops_raw_skt ||
  1136. wsi->role_ops == &role_ops_raw_file)
  1137. ret = 0;
  1138. while (ret == 1) {
  1139. /* default to nobody has more to spill */
  1140. ret = 0;
  1141. ebuf.token = NULL;
  1142. ebuf.len = 0;
  1143. /* give every extension a chance to spill */
  1144. m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_PRESEND,
  1145. &ebuf, 0);
  1146. if (m < 0) {
  1147. lwsl_err("ext reports fatal error\n");
  1148. return LWS_HP_RET_BAIL_DIE;
  1149. }
  1150. if (m)
  1151. /*
  1152. * at least one extension told us he has more
  1153. * to spill, so we will go around again after
  1154. */
  1155. ret = 1;
  1156. /* assuming they gave us something to send, send it */
  1157. if (ebuf.len) {
  1158. n = lws_issue_raw(wsi, (unsigned char *)ebuf.token,
  1159. ebuf.len);
  1160. if (n < 0) {
  1161. lwsl_info("closing from POLLOUT spill\n");
  1162. return LWS_HP_RET_BAIL_DIE;
  1163. }
  1164. /*
  1165. * Keep amount spilled small to minimize chance of this
  1166. */
  1167. if (n != ebuf.len) {
  1168. lwsl_err("Unable to spill ext %d vs %d\n",
  1169. ebuf.len, n);
  1170. return LWS_HP_RET_BAIL_DIE;
  1171. }
  1172. } else
  1173. continue;
  1174. /* no extension has more to spill */
  1175. if (!ret)
  1176. continue;
  1177. /*
  1178. * There's more to spill from an extension, but we just sent
  1179. * something... did that leave the pipe choked?
  1180. */
  1181. if (!lws_send_pipe_choked(wsi))
  1182. /* no we could add more */
  1183. continue;
  1184. lwsl_info("choked in POLLOUT service\n");
  1185. /*
  1186. * Yes, he's choked. Leave the POLLOUT masked on so we will
  1187. * come back here when he is unchoked. Don't call the user
  1188. * callback to enforce ordering of spilling, he'll get called
  1189. * when we come back here and there's nothing more to spill.
  1190. */
  1191. return LWS_HP_RET_BAIL_OK;
  1192. }
  1193. wsi->ws->extension_data_pending = 0;
  1194. #endif
  1195. return LWS_HP_RET_USER_SERVICE;
  1196. }
  1197. static int
  1198. rops_periodic_checks_ws(struct lws_context *context, int tsi, time_t now)
  1199. {
  1200. struct lws_vhost *vh;
  1201. if (!context->ws_ping_pong_interval ||
  1202. context->last_ws_ping_pong_check_s >= now + 10)
  1203. return 0;
  1204. vh = context->vhost_list;
  1205. context->last_ws_ping_pong_check_s = now;
  1206. while (vh) {
  1207. int n;
  1208. lws_vhost_lock(vh);
  1209. for (n = 0; n < vh->count_protocols; n++) {
  1210. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
  1211. vh->same_vh_protocol_heads[n].next) {
  1212. struct lws *wsi = lws_container_of(d,
  1213. struct lws, same_vh_protocol);
  1214. if (lwsi_role_ws(wsi) &&
  1215. !wsi->socket_is_permanently_unusable &&
  1216. !wsi->ws->send_check_ping &&
  1217. wsi->ws->time_next_ping_check &&
  1218. lws_compare_time_t(context, now,
  1219. wsi->ws->time_next_ping_check) >
  1220. context->ws_ping_pong_interval) {
  1221. lwsl_info("req pp on wsi %p\n", wsi);
  1222. wsi->ws->send_check_ping = 1;
  1223. lws_set_timeout(wsi,
  1224. PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING,
  1225. context->timeout_secs);
  1226. lws_callback_on_writable(wsi);
  1227. wsi->ws->time_next_ping_check = now;
  1228. }
  1229. } lws_end_foreach_dll_safe(d, d1);
  1230. }
  1231. lws_vhost_unlock(vh);
  1232. vh = vh->vhost_next;
  1233. }
  1234. return 0;
  1235. }
  1236. static int
  1237. rops_service_flag_pending_ws(struct lws_context *context, int tsi)
  1238. {
  1239. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1240. struct lws_context_per_thread *pt = &context->pt[tsi];
  1241. struct lws *wsi;
  1242. int forced = 0;
  1243. /* POLLIN faking (the pt lock is taken by the parent) */
  1244. /*
  1245. * 1) For all guys with already-available ext data to drain, if they are
  1246. * not flowcontrolled, fake their POLLIN status
  1247. */
  1248. wsi = pt->ws.rx_draining_ext_list;
  1249. while (wsi && wsi->position_in_fds_table != LWS_NO_FDS_POS) {
  1250. pt->fds[wsi->position_in_fds_table].revents |=
  1251. pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
  1252. if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN)
  1253. forced = 1;
  1254. wsi = wsi->ws->rx_draining_ext_list;
  1255. }
  1256. return forced;
  1257. #else
  1258. return 0;
  1259. #endif
  1260. }
  1261. static int
  1262. rops_close_via_role_protocol_ws(struct lws *wsi, enum lws_close_status reason)
  1263. {
  1264. if (!wsi->ws)
  1265. return 0;
  1266. if (!wsi->ws->close_in_ping_buffer_len && /* already a reason */
  1267. (reason == LWS_CLOSE_STATUS_NOSTATUS ||
  1268. reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY))
  1269. return 0;
  1270. lwsl_debug("%s: sending close indication...\n", __func__);
  1271. /* if no prepared close reason, use 1000 and no aux data */
  1272. if (!wsi->ws->close_in_ping_buffer_len) {
  1273. wsi->ws->close_in_ping_buffer_len = 2;
  1274. wsi->ws->ping_payload_buf[LWS_PRE] = (reason >> 8) & 0xff;
  1275. wsi->ws->ping_payload_buf[LWS_PRE + 1] = reason & 0xff;
  1276. }
  1277. wsi->waiting_to_send_close_frame = 1;
  1278. wsi->close_needs_ack = 1;
  1279. lwsi_set_state(wsi, LRS_WAITING_TO_SEND_CLOSE);
  1280. __lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_SEND, 5);
  1281. lws_callback_on_writable(wsi);
  1282. return 1;
  1283. }
  1284. static int
  1285. rops_close_role_ws(struct lws_context_per_thread *pt, struct lws *wsi)
  1286. {
  1287. if (!wsi->ws)
  1288. return 0;
  1289. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1290. if (wsi->ws->rx_draining_ext) {
  1291. struct lws **w = &pt->ws.rx_draining_ext_list;
  1292. wsi->ws->rx_draining_ext = 0;
  1293. /* remove us from context draining ext list */
  1294. while (*w) {
  1295. if (*w == wsi) {
  1296. *w = wsi->ws->rx_draining_ext_list;
  1297. break;
  1298. }
  1299. w = &((*w)->ws->rx_draining_ext_list);
  1300. }
  1301. wsi->ws->rx_draining_ext_list = NULL;
  1302. }
  1303. if (wsi->ws->tx_draining_ext) {
  1304. struct lws **w = &pt->ws.tx_draining_ext_list;
  1305. lwsl_ext("%s: CLEARING tx_draining_ext\n", __func__);
  1306. wsi->ws->tx_draining_ext = 0;
  1307. /* remove us from context draining ext list */
  1308. while (*w) {
  1309. if (*w == wsi) {
  1310. *w = wsi->ws->tx_draining_ext_list;
  1311. break;
  1312. }
  1313. w = &((*w)->ws->tx_draining_ext_list);
  1314. }
  1315. wsi->ws->tx_draining_ext_list = NULL;
  1316. }
  1317. #endif
  1318. lws_free_set_NULL(wsi->ws->rx_ubuf);
  1319. wsi->ws->ping_payload_len = 0;
  1320. wsi->ws->ping_pending_flag = 0;
  1321. /* deallocate any active extension contexts */
  1322. if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
  1323. lwsl_warn("extension destruction failed\n");
  1324. return 0;
  1325. }
  1326. static int
  1327. rops_write_role_protocol_ws(struct lws *wsi, unsigned char *buf, size_t len,
  1328. enum lws_write_protocol *wp)
  1329. {
  1330. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1331. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  1332. enum lws_write_protocol wpt;
  1333. #endif
  1334. int masked7 = lwsi_role_client(wsi);
  1335. unsigned char is_masked_bit = 0;
  1336. unsigned char *dropmask = NULL;
  1337. struct lws_tokens ebuf;
  1338. size_t orig_len = len;
  1339. int pre = 0, n = 0;
  1340. // lwsl_err("%s: wp 0x%x len %d\n", __func__, *wp, (int)len);
  1341. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1342. if (wsi->ws->tx_draining_ext) {
  1343. /* remove us from the list */
  1344. struct lws **w = &pt->ws.tx_draining_ext_list;
  1345. lwsl_ext("%s: CLEARING tx_draining_ext\n", __func__);
  1346. wsi->ws->tx_draining_ext = 0;
  1347. /* remove us from context draining ext list */
  1348. while (*w) {
  1349. if (*w == wsi) {
  1350. *w = wsi->ws->tx_draining_ext_list;
  1351. break;
  1352. }
  1353. w = &((*w)->ws->tx_draining_ext_list);
  1354. }
  1355. wsi->ws->tx_draining_ext_list = NULL;
  1356. wpt = *wp;
  1357. *wp = (wsi->ws->tx_draining_stashed_wp & 0xc0)|
  1358. LWS_WRITE_CONTINUATION;
  1359. /*
  1360. * When we are just flushing (len == 0), we can trust the
  1361. * stashed wp info completely. Otherwise adjust it to the
  1362. * FIN status of the incoming packet.
  1363. */
  1364. if (!(wpt & LWS_WRITE_NO_FIN) && len)
  1365. *wp &= ~LWS_WRITE_NO_FIN;
  1366. lwsl_ext("FORCED draining wp to 0x%02X "
  1367. "(stashed 0x%02X, incoming 0x%02X)\n", *wp,
  1368. wsi->ws->tx_draining_stashed_wp, wpt);
  1369. // assert(0);
  1370. }
  1371. #endif
  1372. lws_restart_ws_ping_pong_timer(wsi);
  1373. if (((*wp) & 0x1f) == LWS_WRITE_HTTP ||
  1374. ((*wp) & 0x1f) == LWS_WRITE_HTTP_FINAL ||
  1375. ((*wp) & 0x1f) == LWS_WRITE_HTTP_HEADERS_CONTINUATION ||
  1376. ((*wp) & 0x1f) == LWS_WRITE_HTTP_HEADERS)
  1377. goto send_raw;
  1378. /* if we are continuing a frame that already had its header done */
  1379. if (wsi->ws->inside_frame) {
  1380. lwsl_debug("INSIDE FRAME\n");
  1381. goto do_more_inside_frame;
  1382. }
  1383. wsi->ws->clean_buffer = 1;
  1384. /*
  1385. * give a chance to the extensions to modify payload
  1386. * the extension may decide to produce unlimited payload erratically
  1387. * (eg, compression extension), so we require only that if he produces
  1388. * something, it will be a complete fragment of the length known at
  1389. * the time (just the fragment length known), and if he has
  1390. * more we will come back next time he is writeable and allow him to
  1391. * produce more fragments until he's drained.
  1392. *
  1393. * This allows what is sent each time it is writeable to be limited to
  1394. * a size that can be sent without partial sends or blocking, allows
  1395. * interleaving of control frames and other connection service.
  1396. */
  1397. ebuf.token = (char *)buf;
  1398. ebuf.len = (int)len;
  1399. switch ((int)*wp) {
  1400. case LWS_WRITE_PING:
  1401. case LWS_WRITE_PONG:
  1402. case LWS_WRITE_CLOSE:
  1403. break;
  1404. default:
  1405. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1406. // lwsl_notice("LWS_EXT_CB_PAYLOAD_TX\n");
  1407. // m = (int)ebuf.len;
  1408. /* returns 0 if no more tx pending, 1 if more pending */
  1409. n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &ebuf, *wp);
  1410. if (n < 0)
  1411. return -1;
  1412. // lwsl_notice("ext processed %d plaintext into %d compressed"
  1413. // " (wp 0x%x)\n", m, (int)ebuf.len, *wp);
  1414. if (n && ebuf.len) {
  1415. lwsl_ext("write drain len %d (wp 0x%x) SETTING "
  1416. "tx_draining_ext\n", (int)ebuf.len, *wp);
  1417. /* extension requires further draining */
  1418. wsi->ws->tx_draining_ext = 1;
  1419. wsi->ws->tx_draining_ext_list =
  1420. pt->ws.tx_draining_ext_list;
  1421. pt->ws.tx_draining_ext_list = wsi;
  1422. /* we must come back to do more */
  1423. lws_callback_on_writable(wsi);
  1424. /*
  1425. * keep a copy of the write type for the overall
  1426. * action that has provoked generation of these
  1427. * fragments, so the last guy can use its FIN state.
  1428. */
  1429. wsi->ws->tx_draining_stashed_wp = *wp;
  1430. /* this is definitely not actually the last fragment
  1431. * because the extension asserted he has more coming
  1432. * So make sure this intermediate one doesn't go out
  1433. * with a FIN.
  1434. */
  1435. *wp |= LWS_WRITE_NO_FIN;
  1436. }
  1437. #endif
  1438. if (ebuf.len && wsi->ws->stashed_write_pending) {
  1439. wsi->ws->stashed_write_pending = 0;
  1440. *wp = ((*wp) & 0xc0) | (int)wsi->ws->stashed_write_type;
  1441. }
  1442. }
  1443. /*
  1444. * an extension did something we need to keep... for example, if
  1445. * compression extension, it has already updated its state according
  1446. * to this being issued
  1447. */
  1448. if ((char *)buf != ebuf.token) {
  1449. /*
  1450. * ext might eat it, but not have anything to issue yet.
  1451. * In that case we have to follow his lead, but stash and
  1452. * replace the write type that was lost here the first time.
  1453. */
  1454. if (len && !ebuf.len) {
  1455. if (!wsi->ws->stashed_write_pending)
  1456. wsi->ws->stashed_write_type =
  1457. (char)(*wp) & 0x3f;
  1458. wsi->ws->stashed_write_pending = 1;
  1459. return (int)len;
  1460. }
  1461. /*
  1462. * extension recreated it:
  1463. * need to buffer this if not all sent
  1464. */
  1465. wsi->ws->clean_buffer = 0;
  1466. }
  1467. buf = (unsigned char *)ebuf.token;
  1468. len = ebuf.len;
  1469. if (!buf) {
  1470. lwsl_err("null buf (%d)\n", (int)len);
  1471. return -1;
  1472. }
  1473. switch (wsi->ws->ietf_spec_revision) {
  1474. case 13:
  1475. if (masked7) {
  1476. pre += 4;
  1477. dropmask = &buf[0 - pre];
  1478. is_masked_bit = 0x80;
  1479. }
  1480. switch ((*wp) & 0xf) {
  1481. case LWS_WRITE_TEXT:
  1482. n = LWSWSOPC_TEXT_FRAME;
  1483. break;
  1484. case LWS_WRITE_BINARY:
  1485. n = LWSWSOPC_BINARY_FRAME;
  1486. break;
  1487. case LWS_WRITE_CONTINUATION:
  1488. n = LWSWSOPC_CONTINUATION;
  1489. break;
  1490. case LWS_WRITE_CLOSE:
  1491. n = LWSWSOPC_CLOSE;
  1492. break;
  1493. case LWS_WRITE_PING:
  1494. n = LWSWSOPC_PING;
  1495. break;
  1496. case LWS_WRITE_PONG:
  1497. n = LWSWSOPC_PONG;
  1498. break;
  1499. default:
  1500. lwsl_warn("lws_write: unknown write opc / wp\n");
  1501. return -1;
  1502. }
  1503. if (!((*wp) & LWS_WRITE_NO_FIN))
  1504. n |= 1 << 7;
  1505. if (len < 126) {
  1506. pre += 2;
  1507. buf[-pre] = n;
  1508. buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
  1509. } else {
  1510. if (len < 65536) {
  1511. pre += 4;
  1512. buf[-pre] = n;
  1513. buf[-pre + 1] = 126 | is_masked_bit;
  1514. buf[-pre + 2] = (unsigned char)(len >> 8);
  1515. buf[-pre + 3] = (unsigned char)len;
  1516. } else {
  1517. pre += 10;
  1518. buf[-pre] = n;
  1519. buf[-pre + 1] = 127 | is_masked_bit;
  1520. #if defined __LP64__
  1521. buf[-pre + 2] = (len >> 56) & 0x7f;
  1522. buf[-pre + 3] = len >> 48;
  1523. buf[-pre + 4] = len >> 40;
  1524. buf[-pre + 5] = len >> 32;
  1525. #else
  1526. buf[-pre + 2] = 0;
  1527. buf[-pre + 3] = 0;
  1528. buf[-pre + 4] = 0;
  1529. buf[-pre + 5] = 0;
  1530. #endif
  1531. buf[-pre + 6] = (unsigned char)(len >> 24);
  1532. buf[-pre + 7] = (unsigned char)(len >> 16);
  1533. buf[-pre + 8] = (unsigned char)(len >> 8);
  1534. buf[-pre + 9] = (unsigned char)len;
  1535. }
  1536. }
  1537. break;
  1538. }
  1539. do_more_inside_frame:
  1540. /*
  1541. * Deal with masking if we are in client -> server direction and
  1542. * the wp demands it
  1543. */
  1544. if (masked7) {
  1545. if (!wsi->ws->inside_frame)
  1546. if (lws_0405_frame_mask_generate(wsi)) {
  1547. lwsl_err("frame mask generation failed\n");
  1548. return -1;
  1549. }
  1550. /*
  1551. * in v7, just mask the payload
  1552. */
  1553. if (dropmask) { /* never set if already inside frame */
  1554. for (n = 4; n < (int)len + 4; n++)
  1555. dropmask[n] = dropmask[n] ^ wsi->ws->mask[
  1556. (wsi->ws->mask_idx++) & 3];
  1557. /* copy the frame nonce into place */
  1558. memcpy(dropmask, wsi->ws->mask, 4);
  1559. }
  1560. }
  1561. if (lwsi_role_h2_ENCAPSULATION(wsi)) {
  1562. struct lws *encap = lws_get_network_wsi(wsi);
  1563. assert(encap != wsi);
  1564. return encap->role_ops->write_role_protocol(wsi, buf - pre,
  1565. len + pre, wp);
  1566. }
  1567. switch ((*wp) & 0x1f) {
  1568. case LWS_WRITE_TEXT:
  1569. case LWS_WRITE_BINARY:
  1570. case LWS_WRITE_CONTINUATION:
  1571. if (!wsi->h2_stream_carries_ws) {
  1572. /*
  1573. * give any active extensions a chance to munge the
  1574. * buffer before send. We pass in a pointer to an
  1575. * lws_tokens struct prepared with the default buffer
  1576. * and content length that's in there. Rather than
  1577. * rewrite the default buffer, extensions that expect
  1578. * to grow the buffer can adapt .token to point to their
  1579. * own per-connection buffer in the extension user
  1580. * allocation. By default with no extensions or no
  1581. * extension callback handling, just the normal input
  1582. * buffer is used then so it is efficient.
  1583. *
  1584. * callback returns 1 in case it wants to spill more
  1585. * buffers
  1586. *
  1587. * This takes care of holding the buffer if send is
  1588. * incomplete, ie, if wsi->ws->clean_buffer is 0
  1589. * (meaning an extension meddled with the buffer). If
  1590. * wsi->ws->clean_buffer is 1, it will instead return
  1591. * to the user code how much OF THE USER BUFFER was
  1592. * consumed.
  1593. */
  1594. n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
  1595. wsi->ws->inside_frame = 1;
  1596. if (n <= 0)
  1597. return n;
  1598. if (n == (int)len + pre) {
  1599. /* everything in the buffer was handled
  1600. * (or rebuffered...) */
  1601. wsi->ws->inside_frame = 0;
  1602. return (int)orig_len;
  1603. }
  1604. /*
  1605. * it is how many bytes of user buffer got sent... may
  1606. * be < orig_len in which case callback when writable
  1607. * has already been arranged and user code can call
  1608. * lws_write() again with the rest later.
  1609. */
  1610. return n - pre;
  1611. }
  1612. break;
  1613. default:
  1614. break;
  1615. }
  1616. send_raw:
  1617. return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
  1618. }
  1619. static int
  1620. rops_close_kill_connection_ws(struct lws *wsi, enum lws_close_status reason)
  1621. {
  1622. /* deal with ws encapsulation in h2 */
  1623. #if defined(LWS_WITH_HTTP2)
  1624. if (wsi->http2_substream && wsi->h2_stream_carries_ws)
  1625. return role_ops_h2.close_kill_connection(wsi, reason);
  1626. return 0;
  1627. #else
  1628. return 0;
  1629. #endif
  1630. }
  1631. static int
  1632. rops_callback_on_writable_ws(struct lws *wsi)
  1633. {
  1634. #if defined(LWS_WITH_HTTP2)
  1635. if (lwsi_role_h2_ENCAPSULATION(wsi)) {
  1636. /* we know then that it has an h2 parent */
  1637. struct lws *enc = role_ops_h2.encapsulation_parent(wsi);
  1638. assert(enc);
  1639. if (enc->role_ops->callback_on_writable(wsi))
  1640. return 1;
  1641. }
  1642. #endif
  1643. return 0;
  1644. }
  1645. static int
  1646. rops_init_vhost_ws(struct lws_vhost *vh,
  1647. const struct lws_context_creation_info *info)
  1648. {
  1649. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1650. #ifdef LWS_WITH_PLUGINS
  1651. struct lws_plugin *plugin = vh->context->plugin_list;
  1652. int m;
  1653. if (vh->context->plugin_extension_count) {
  1654. m = 0;
  1655. while (info->extensions && info->extensions[m].callback)
  1656. m++;
  1657. /*
  1658. * give the vhost a unified list of extensions including the
  1659. * ones that came from plugins
  1660. */
  1661. vh->ws.extensions = lws_zalloc(sizeof(struct lws_extension) *
  1662. (m + vh->context->plugin_extension_count + 1),
  1663. "extensions");
  1664. if (!vh->ws.extensions)
  1665. return 1;
  1666. memcpy((struct lws_extension *)vh->ws.extensions, info->extensions,
  1667. sizeof(struct lws_extension) * m);
  1668. plugin = vh->context->plugin_list;
  1669. while (plugin) {
  1670. memcpy((struct lws_extension *)&vh->ws.extensions[m],
  1671. plugin->caps.extensions,
  1672. sizeof(struct lws_extension) *
  1673. plugin->caps.count_extensions);
  1674. m += plugin->caps.count_extensions;
  1675. plugin = plugin->list;
  1676. }
  1677. } else
  1678. #endif
  1679. vh->ws.extensions = info->extensions;
  1680. #endif
  1681. return 0;
  1682. }
  1683. static int
  1684. rops_destroy_vhost_ws(struct lws_vhost *vh)
  1685. {
  1686. #ifdef LWS_WITH_PLUGINS
  1687. #if !defined(LWS_WITHOUT_EXTENSIONS)
  1688. if (vh->context->plugin_extension_count)
  1689. lws_free((void *)vh->ws.extensions);
  1690. #endif
  1691. #endif
  1692. return 0;
  1693. }
  1694. static int
  1695. rops_destroy_role_ws(struct lws *wsi)
  1696. {
  1697. lws_free_set_NULL(wsi->ws);
  1698. return 0;
  1699. }
  1700. struct lws_role_ops role_ops_ws = {
  1701. /* role name */ "ws",
  1702. /* alpn id */ NULL,
  1703. /* check_upgrades */ NULL,
  1704. /* init_context */ NULL,
  1705. /* init_vhost */ rops_init_vhost_ws,
  1706. /* destroy_vhost */ rops_destroy_vhost_ws,
  1707. /* periodic_checks */ rops_periodic_checks_ws,
  1708. /* service_flag_pending */ rops_service_flag_pending_ws,
  1709. /* handle_POLLIN */ rops_handle_POLLIN_ws,
  1710. /* handle_POLLOUT */ rops_handle_POLLOUT_ws,
  1711. /* perform_user_POLLOUT */ NULL,
  1712. /* callback_on_writable */ rops_callback_on_writable_ws,
  1713. /* tx_credit */ NULL,
  1714. /* write_role_protocol */ rops_write_role_protocol_ws,
  1715. /* encapsulation_parent */ NULL,
  1716. /* alpn_negotiated */ NULL,
  1717. /* close_via_role_protocol */ rops_close_via_role_protocol_ws,
  1718. /* close_role */ rops_close_role_ws,
  1719. /* close_kill_connection */ rops_close_kill_connection_ws,
  1720. /* destroy_role */ rops_destroy_role_ws,
  1721. /* adoption_bind */ NULL,
  1722. /* client_bind */ NULL,
  1723. /* writeable cb clnt, srv */ { LWS_CALLBACK_CLIENT_WRITEABLE,
  1724. LWS_CALLBACK_SERVER_WRITEABLE },
  1725. /* close cb clnt, srv */ { LWS_CALLBACK_CLIENT_CLOSED,
  1726. LWS_CALLBACK_CLOSED },
  1727. /* protocol_bind cb c, srv */ { LWS_CALLBACK_WS_CLIENT_BIND_PROTOCOL,
  1728. LWS_CALLBACK_WS_SERVER_BIND_PROTOCOL },
  1729. /* protocol_unbind cb c, srv */ { LWS_CALLBACK_WS_CLIENT_DROP_PROTOCOL,
  1730. LWS_CALLBACK_WS_SERVER_DROP_PROTOCOL },
  1731. /* file handles */ 0
  1732. };