libwebsockets.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  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 "private-libwebsockets.h"
  22. #ifdef LWS_HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #if defined(WIN32) || defined(_WIN32)
  26. #else
  27. #include <sys/wait.h>
  28. #endif
  29. #ifdef LWS_USE_IPV6
  30. #if defined(WIN32) || defined(_WIN32)
  31. #include <Iphlpapi.h>
  32. #else
  33. #include <net/if.h>
  34. #endif
  35. #endif
  36. int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
  37. static void (*lwsl_emit)(int level, const char *line)
  38. #ifndef LWS_PLAT_OPTEE
  39. = lwsl_emit_stderr
  40. #endif
  41. ;
  42. #ifndef LWS_PLAT_OPTEE
  43. static const char * const log_level_names[] = {
  44. "ERR",
  45. "WARN",
  46. "NOTICE",
  47. "INFO",
  48. "DEBUG",
  49. "PARSER",
  50. "HEADER",
  51. "EXTENSION",
  52. "CLIENT",
  53. "LATENCY",
  54. "USER",
  55. "?",
  56. "?"
  57. };
  58. #endif
  59. void
  60. lws_free_wsi(struct lws *wsi)
  61. {
  62. if (!wsi)
  63. return;
  64. /* Protocol user data may be allocated either internally by lws
  65. * or by specified the user.
  66. * We should only free what we allocated. */
  67. if (wsi->protocol && wsi->protocol->per_session_data_size &&
  68. wsi->user_space && !wsi->user_space_externally_allocated)
  69. lws_free(wsi->user_space);
  70. lws_free_set_NULL(wsi->rxflow_buffer);
  71. lws_free_set_NULL(wsi->trunc_alloc);
  72. /* we may not have an ah, but may be on the waiting list... */
  73. lwsl_info("ah det due to close\n");
  74. lws_header_table_detach(wsi, 0);
  75. wsi->context->count_wsi_allocated--;
  76. lwsl_debug("%s: %p, remaining wsi %d\n", __func__, wsi,
  77. wsi->context->count_wsi_allocated);
  78. lws_free(wsi);
  79. }
  80. void
  81. lws_remove_from_timeout_list(struct lws *wsi)
  82. {
  83. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  84. if (!wsi->timeout_list_prev) /* ie, not part of the list */
  85. return;
  86. lws_pt_lock(pt);
  87. /* if we have a next guy, set his prev to our prev */
  88. if (wsi->timeout_list)
  89. wsi->timeout_list->timeout_list_prev = wsi->timeout_list_prev;
  90. /* set our prev guy to our next guy instead of us */
  91. *wsi->timeout_list_prev = wsi->timeout_list;
  92. /* we're out of the list, we should not point anywhere any more */
  93. wsi->timeout_list_prev = NULL;
  94. wsi->timeout_list = NULL;
  95. lws_pt_unlock(pt);
  96. }
  97. LWS_VISIBLE void
  98. lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
  99. {
  100. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  101. time_t now;
  102. lws_pt_lock(pt);
  103. time(&now);
  104. if (reason && !wsi->timeout_list_prev) {
  105. /* our next guy is current first guy */
  106. wsi->timeout_list = pt->timeout_list;
  107. /* if there is a next guy, set his prev ptr to our next ptr */
  108. if (wsi->timeout_list)
  109. wsi->timeout_list->timeout_list_prev = &wsi->timeout_list;
  110. /* our prev ptr is first ptr */
  111. wsi->timeout_list_prev = &pt->timeout_list;
  112. /* set the first guy to be us */
  113. *wsi->timeout_list_prev = wsi;
  114. }
  115. lwsl_debug("%s: %p: %d secs\n", __func__, wsi, secs);
  116. wsi->pending_timeout_limit = now + secs;
  117. wsi->pending_timeout = reason;
  118. lws_pt_unlock(pt);
  119. if (!reason)
  120. lws_remove_from_timeout_list(wsi);
  121. }
  122. static void
  123. lws_remove_child_from_any_parent(struct lws *wsi)
  124. {
  125. struct lws **pwsi;
  126. if (wsi->parent) {
  127. /* detach ourselves from parent's child list */
  128. pwsi = &wsi->parent->child_list;
  129. while (*pwsi) {
  130. if (*pwsi == wsi) {
  131. //lwsl_notice("%s: detach %p from parent %p\n",
  132. // __func__, wsi, wsi->parent);
  133. *pwsi = wsi->sibling_list;
  134. break;
  135. }
  136. pwsi = &(*pwsi)->sibling_list;
  137. }
  138. if (*pwsi)
  139. lwsl_err("%s: failed to detach from parent\n",
  140. __func__);
  141. }
  142. }
  143. int
  144. lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p)
  145. {
  146. // if (wsi->protocol == p)
  147. // return 0;
  148. if (wsi->protocol)
  149. wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_DROP_PROTOCOL,
  150. wsi->user_space, NULL, 0);
  151. if (!wsi->user_space_externally_allocated)
  152. lws_free_set_NULL(wsi->user_space);
  153. wsi->protocol = p;
  154. if (!p)
  155. return 0;
  156. if (lws_ensure_user_space(wsi))
  157. return 1;
  158. if (wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_BIND_PROTOCOL,
  159. wsi->user_space, NULL, 0))
  160. return 1;
  161. return 0;
  162. }
  163. void
  164. lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
  165. {
  166. struct lws_context_per_thread *pt;
  167. struct lws *wsi1, *wsi2;
  168. struct lws_context *context;
  169. struct lws_tokens eff_buf;
  170. int n, m, ret;
  171. if (!wsi)
  172. return;
  173. lws_access_log(wsi);
  174. #if defined(LWS_WITH_ESP8266)
  175. if (wsi->premature_rx)
  176. lws_free(wsi->premature_rx);
  177. if (wsi->pending_send_completion && !wsi->close_is_pending_send_completion) {
  178. lwsl_notice("delaying close\n");
  179. wsi->close_is_pending_send_completion = 1;
  180. return;
  181. }
  182. #endif
  183. if (wsi->u.hdr.ah)
  184. /* we're closing, losing some rx is OK */
  185. wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
  186. context = wsi->context;
  187. pt = &context->pt[(int)wsi->tsi];
  188. /* if we have children, close them first */
  189. if (wsi->child_list) {
  190. wsi2 = wsi->child_list;
  191. while (wsi2) {
  192. //lwsl_notice("%s: closing %p: close child %p\n",
  193. // __func__, wsi, wsi2);
  194. wsi1 = wsi2->sibling_list;
  195. //lwsl_notice("%s: closing %p: next sibling %p\n",
  196. // __func__, wsi2, wsi1);
  197. wsi2->parent = NULL;
  198. /* stop it doing shutdown processing */
  199. wsi2->socket_is_permanently_unusable = 1;
  200. lws_close_free_wsi(wsi2, reason);
  201. wsi2 = wsi1;
  202. }
  203. wsi->child_list = NULL;
  204. }
  205. if (wsi->mode == LWSCM_RAW_FILEDESC) {
  206. lws_remove_child_from_any_parent(wsi);
  207. remove_wsi_socket_from_fds(wsi);
  208. wsi->protocol->callback(wsi,
  209. LWS_CALLBACK_RAW_CLOSE_FILE,
  210. wsi->user_space, NULL, 0);
  211. goto async_close;
  212. }
  213. #ifdef LWS_WITH_CGI
  214. if (wsi->mode == LWSCM_CGI) {
  215. /* we are not a network connection, but a handler for CGI io */
  216. if (wsi->parent && wsi->parent->cgi)
  217. /* end the binding between us and master */
  218. wsi->parent->cgi->stdwsi[(int)wsi->cgi_channel] = NULL;
  219. wsi->socket_is_permanently_unusable = 1;
  220. lwsl_debug("------ %s: detected cgi fdhandler wsi %p\n", __func__, wsi);
  221. goto just_kill_connection;
  222. }
  223. if (wsi->cgi) {
  224. struct lws_cgi **pcgi = &pt->cgi_list;
  225. /* remove us from the cgi list */
  226. lwsl_debug("%s: remove cgi %p from list\n", __func__, wsi->cgi);
  227. while (*pcgi) {
  228. if (*pcgi == wsi->cgi) {
  229. /* drop us from the pt cgi list */
  230. *pcgi = (*pcgi)->cgi_list;
  231. break;
  232. }
  233. pcgi = &(*pcgi)->cgi_list;
  234. }
  235. /* we have a cgi going, we must kill it */
  236. wsi->cgi->being_closed = 1;
  237. lws_cgi_kill(wsi);
  238. }
  239. #endif
  240. if (wsi->mode == LWSCM_RAW) {
  241. wsi->protocol->callback(wsi,
  242. LWS_CALLBACK_RAW_CLOSE, wsi->user_space, NULL, 0);
  243. wsi->socket_is_permanently_unusable = 1;
  244. goto just_kill_connection;
  245. }
  246. if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
  247. wsi->u.http.fop_fd != NULL) {
  248. lws_vfs_file_close(&wsi->u.http.fop_fd);
  249. wsi->vhost->protocols->callback(wsi,
  250. LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
  251. wsi->told_user_closed = 1;
  252. }
  253. if (wsi->socket_is_permanently_unusable ||
  254. reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY ||
  255. wsi->state == LWSS_SHUTDOWN)
  256. goto just_kill_connection;
  257. wsi->state_pre_close = wsi->state;
  258. switch (wsi->state_pre_close) {
  259. case LWSS_DEAD_SOCKET:
  260. return;
  261. /* we tried the polite way... */
  262. case LWSS_AWAITING_CLOSE_ACK:
  263. goto just_kill_connection;
  264. case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
  265. if (wsi->trunc_len) {
  266. lws_callback_on_writable(wsi);
  267. return;
  268. }
  269. lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
  270. goto just_kill_connection;
  271. default:
  272. if (wsi->trunc_len) {
  273. lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
  274. wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
  275. lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
  276. return;
  277. }
  278. break;
  279. }
  280. if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
  281. wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
  282. goto just_kill_connection;
  283. if (wsi->mode == LWSCM_HTTP_SERVING) {
  284. if (wsi->user_space)
  285. wsi->vhost->protocols->callback(wsi,
  286. LWS_CALLBACK_HTTP_DROP_PROTOCOL,
  287. wsi->user_space, NULL, 0);
  288. wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
  289. wsi->user_space, NULL, 0);
  290. wsi->told_user_closed = 1;
  291. }
  292. if (wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) {
  293. wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_CLIENT_HTTP,
  294. wsi->user_space, NULL, 0);
  295. wsi->told_user_closed = 1;
  296. }
  297. /*
  298. * are his extensions okay with him closing? Eg he might be a mux
  299. * parent and just his ch1 aspect is closing?
  300. */
  301. if (lws_ext_cb_active(wsi,
  302. LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
  303. lwsl_ext("extension vetoed close\n");
  304. return;
  305. }
  306. /*
  307. * flush any tx pending from extensions, since we may send close packet
  308. * if there are problems with send, just nuke the connection
  309. */
  310. do {
  311. ret = 0;
  312. eff_buf.token = NULL;
  313. eff_buf.token_len = 0;
  314. /* show every extension the new incoming data */
  315. m = lws_ext_cb_active(wsi,
  316. LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
  317. if (m < 0) {
  318. lwsl_ext("Extension reports fatal error\n");
  319. goto just_kill_connection;
  320. }
  321. if (m)
  322. /*
  323. * at least one extension told us he has more
  324. * to spill, so we will go around again after
  325. */
  326. ret = 1;
  327. /* assuming they left us something to send, send it */
  328. if (eff_buf.token_len)
  329. if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
  330. eff_buf.token_len) !=
  331. eff_buf.token_len) {
  332. lwsl_debug("close: ext spill failed\n");
  333. goto just_kill_connection;
  334. }
  335. } while (ret);
  336. /*
  337. * signal we are closing, lws_write will
  338. * add any necessary version-specific stuff. If the write fails,
  339. * no worries we are closing anyway. If we didn't initiate this
  340. * close, then our state has been changed to
  341. * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
  342. *
  343. * Likewise if it's a second call to close this connection after we
  344. * sent the close indication to the peer already, we are in state
  345. * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
  346. */
  347. if (wsi->state_pre_close == LWSS_ESTABLISHED &&
  348. (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
  349. (reason != LWS_CLOSE_STATUS_NOSTATUS &&
  350. (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
  351. lwsl_debug("sending close indication...\n");
  352. /* if no prepared close reason, use 1000 and no aux data */
  353. if (!wsi->u.ws.close_in_ping_buffer_len) {
  354. wsi->u.ws.close_in_ping_buffer_len = 2;
  355. wsi->u.ws.ping_payload_buf[LWS_PRE] =
  356. (reason >> 8) & 0xff;
  357. wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
  358. reason & 0xff;
  359. }
  360. #if defined (LWS_WITH_ESP8266)
  361. wsi->close_is_pending_send_completion = 1;
  362. #endif
  363. n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
  364. wsi->u.ws.close_in_ping_buffer_len,
  365. LWS_WRITE_CLOSE);
  366. if (n >= 0) {
  367. /*
  368. * we have sent a nice protocol level indication we
  369. * now wish to close, we should not send anything more
  370. */
  371. wsi->state = LWSS_AWAITING_CLOSE_ACK;
  372. /*
  373. * ...and we should wait for a reply for a bit
  374. * out of politeness
  375. */
  376. lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
  377. lwsl_debug("sent close indication, awaiting ack\n");
  378. return;
  379. }
  380. lwsl_info("close: sending close packet failed, hanging up\n");
  381. /* else, the send failed and we should just hang up */
  382. }
  383. just_kill_connection:
  384. lws_remove_child_from_any_parent(wsi);
  385. #if 0
  386. /* manage the vhost same protocol list entry */
  387. if (wsi->same_vh_protocol_prev) { // we are on the vh list
  388. // make guy who pointed to us, point to what our next was pointing to
  389. *wsi->same_vh_protocol_prev = wsi->same_vh_protocol_next;
  390. // if we had a next guy...
  391. if (wsi->same_vh_protocol_next)
  392. // have him point back to our prev
  393. wsi->same_vh_protocol_next->same_vh_protocol_prev =
  394. wsi->same_vh_protocol_prev;
  395. }
  396. #endif
  397. #if LWS_POSIX
  398. /*
  399. * Testing with ab shows that we have to stage the socket close when
  400. * the system is under stress... shutdown any further TX, change the
  401. * state to one that won't emit anything more, and wait with a timeout
  402. * for the POLLIN to show a zero-size rx before coming back and doing
  403. * the actual close.
  404. */
  405. if (wsi->mode != LWSCM_RAW && wsi->state != LWSS_SHUTDOWN &&
  406. wsi->state != LWSS_CLIENT_UNCONNECTED &&
  407. reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
  408. !wsi->socket_is_permanently_unusable) {
  409. #ifdef LWS_OPENSSL_SUPPORT
  410. if (lws_is_ssl(wsi) && wsi->ssl)
  411. {
  412. lwsl_info("%s: shutting down SSL connection: %p (ssl %p, sock %d, state %d)\n", __func__, wsi, wsi->ssl, (int)(long)wsi->desc.sockfd, wsi->state);
  413. n = SSL_shutdown(wsi->ssl);
  414. if (n == 1) /* If finished the SSL shutdown, then do socket shutdown, else need to retry SSL shutdown */
  415. n = shutdown(wsi->desc.sockfd, SHUT_WR);
  416. else if (n == 0)
  417. lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
  418. else /* n < 0 */
  419. {
  420. int shutdown_error = SSL_get_error(wsi->ssl, n);
  421. lwsl_debug("SSL_shutdown returned %d, SSL_get_error: %d\n", n, shutdown_error);
  422. if (shutdown_error == SSL_ERROR_WANT_READ) {
  423. lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
  424. n = 0;
  425. } else if (shutdown_error == SSL_ERROR_WANT_WRITE) {
  426. lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLOUT);
  427. n = 0;
  428. } else { // actual error occurred, just close the connection
  429. n = shutdown(wsi->desc.sockfd, SHUT_WR);
  430. }
  431. }
  432. }
  433. else
  434. #endif
  435. {
  436. lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, (int)(long)wsi->desc.sockfd, wsi->state);
  437. n = shutdown(wsi->desc.sockfd, SHUT_WR);
  438. }
  439. if (n)
  440. lwsl_debug("closing: shutdown (state %d) ret %d\n", wsi->state, LWS_ERRNO);
  441. // This causes problems with disconnection when the events are half closing connection
  442. // FD_READ | FD_CLOSE (33)
  443. #ifndef _WIN32_WCE
  444. /* libuv: no event available to guarantee completion */
  445. if (!LWS_LIBUV_ENABLED(context)) {
  446. lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
  447. wsi->state = LWSS_SHUTDOWN;
  448. lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
  449. context->timeout_secs);
  450. return;
  451. }
  452. #endif
  453. }
  454. #endif
  455. lwsl_info("%s: real just_kill_connection: %p (sockfd %d)\n", __func__,
  456. wsi, wsi->desc.sockfd);
  457. #ifdef LWS_WITH_HTTP_PROXY
  458. if (wsi->rw) {
  459. lws_rewrite_destroy(wsi->rw);
  460. wsi->rw = NULL;
  461. }
  462. #endif
  463. /*
  464. * we won't be servicing or receiving anything further from this guy
  465. * delete socket from the internal poll list if still present
  466. */
  467. lws_ssl_remove_wsi_from_buffered_list(wsi);
  468. lws_remove_from_timeout_list(wsi);
  469. /* checking return redundant since we anyway close */
  470. if (wsi->desc.sockfd != LWS_SOCK_INVALID)
  471. remove_wsi_socket_from_fds(wsi);
  472. #if defined(LWS_WITH_ESP8266)
  473. espconn_disconnect(wsi->desc.sockfd);
  474. #endif
  475. wsi->state = LWSS_DEAD_SOCKET;
  476. lws_free_set_NULL(wsi->rxflow_buffer);
  477. if (wsi->state_pre_close == LWSS_ESTABLISHED ||
  478. wsi->mode == LWSCM_WS_SERVING ||
  479. wsi->mode == LWSCM_WS_CLIENT) {
  480. if (wsi->u.ws.rx_draining_ext) {
  481. struct lws **w = &pt->rx_draining_ext_list;
  482. wsi->u.ws.rx_draining_ext = 0;
  483. /* remove us from context draining ext list */
  484. while (*w) {
  485. if (*w == wsi) {
  486. *w = wsi->u.ws.rx_draining_ext_list;
  487. break;
  488. }
  489. w = &((*w)->u.ws.rx_draining_ext_list);
  490. }
  491. wsi->u.ws.rx_draining_ext_list = NULL;
  492. }
  493. if (wsi->u.ws.tx_draining_ext) {
  494. struct lws **w = &pt->tx_draining_ext_list;
  495. wsi->u.ws.tx_draining_ext = 0;
  496. /* remove us from context draining ext list */
  497. while (*w) {
  498. if (*w == wsi) {
  499. *w = wsi->u.ws.tx_draining_ext_list;
  500. break;
  501. }
  502. w = &((*w)->u.ws.tx_draining_ext_list);
  503. }
  504. wsi->u.ws.tx_draining_ext_list = NULL;
  505. }
  506. lws_free_set_NULL(wsi->u.ws.rx_ubuf);
  507. if (wsi->trunc_alloc)
  508. /* not going to be completed... nuke it */
  509. lws_free_set_NULL(wsi->trunc_alloc);
  510. wsi->u.ws.ping_payload_len = 0;
  511. wsi->u.ws.ping_pending_flag = 0;
  512. }
  513. /* tell the user it's all over for this guy */
  514. if (wsi->mode != LWSCM_RAW && wsi->protocol && wsi->protocol->callback &&
  515. ((wsi->state_pre_close == LWSS_ESTABLISHED) ||
  516. (wsi->state_pre_close == LWSS_RETURNED_CLOSE_ALREADY) ||
  517. (wsi->state_pre_close == LWSS_AWAITING_CLOSE_ACK) ||
  518. (wsi->state_pre_close == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) ||
  519. (wsi->mode == LWSCM_WS_CLIENT && wsi->state_pre_close == LWSS_HTTP) ||
  520. (wsi->mode == LWSCM_WS_SERVING && wsi->state_pre_close == LWSS_HTTP))) {
  521. if (wsi->user_space) {
  522. lwsl_debug("%s: doing LWS_CALLBACK_HTTP_DROP_PROTOCOL for %p prot %s\n", __func__, wsi, wsi->protocol->name);
  523. wsi->protocol->callback(wsi,
  524. LWS_CALLBACK_HTTP_DROP_PROTOCOL,
  525. wsi->user_space, NULL, 0);
  526. }
  527. lwsl_debug("calling back CLOSED\n");
  528. wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
  529. wsi->user_space, NULL, 0);
  530. } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
  531. lwsl_debug("calling back CLOSED_HTTP\n");
  532. wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
  533. wsi->user_space, NULL, 0 );
  534. } else if ((wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
  535. wsi->mode == LWSCM_WSCL_WAITING_CONNECT) &&
  536. !wsi->already_did_cce) {
  537. wsi->vhost->protocols[0].callback(wsi,
  538. LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
  539. wsi->user_space, NULL, 0);
  540. } else
  541. lwsl_debug("not calling back closed mode=%d state=%d\n",
  542. wsi->mode, wsi->state_pre_close);
  543. /* deallocate any active extension contexts */
  544. if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
  545. lwsl_warn("extension destruction failed\n");
  546. /*
  547. * inform all extensions in case they tracked this guy out of band
  548. * even though not active on him specifically
  549. */
  550. if (lws_ext_cb_all_exts(context, wsi,
  551. LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
  552. lwsl_warn("ext destroy wsi failed\n");
  553. async_close:
  554. wsi->socket_is_permanently_unusable = 1;
  555. #ifdef LWS_USE_LIBUV
  556. if (LWS_LIBUV_ENABLED(context)) {
  557. lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
  558. /* libuv has to do his own close handle processing asynchronously */
  559. lws_libuv_closehandle(wsi);
  560. return;
  561. }
  562. #endif
  563. lws_close_free_wsi_final(wsi);
  564. }
  565. void
  566. lws_close_free_wsi_final(struct lws *wsi)
  567. {
  568. int n;
  569. if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->desc.sockfd)) {
  570. #if LWS_POSIX
  571. //lwsl_err("*** closing sockfd %d\n", wsi->desc.sockfd);
  572. n = compatible_close(wsi->desc.sockfd);
  573. if (n)
  574. lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
  575. #else
  576. compatible_close(wsi->desc.sockfd);
  577. (void)n;
  578. #endif
  579. wsi->desc.sockfd = LWS_SOCK_INVALID;
  580. }
  581. /* outermost destroy notification for wsi (user_space still intact) */
  582. wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
  583. wsi->user_space, NULL, 0);
  584. #ifdef LWS_WITH_CGI
  585. if (wsi->cgi) {
  586. for (n = 0; n < 6; n++) {
  587. if (wsi->cgi->pipe_fds[n / 2][n & 1] == 0)
  588. lwsl_err("ZERO FD IN CGI CLOSE");
  589. if (wsi->cgi->pipe_fds[n / 2][n & 1] >= 0)
  590. close(wsi->cgi->pipe_fds[n / 2][n & 1]);
  591. }
  592. lws_free(wsi->cgi);
  593. }
  594. #endif
  595. lws_free_wsi(wsi);
  596. }
  597. LWS_VISIBLE LWS_EXTERN const char *
  598. lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
  599. {
  600. int n = 0, sl = strlen(name);
  601. while (lws_hdr_copy_fragment(wsi, buf, len,
  602. WSI_TOKEN_HTTP_URI_ARGS, n) >= 0) {
  603. if (!strncmp(buf, name, sl))
  604. return buf + sl;
  605. n++;
  606. }
  607. return NULL;
  608. }
  609. #if LWS_POSIX && !defined(LWS_WITH_ESP32)
  610. LWS_VISIBLE int
  611. interface_to_sa(struct lws_vhost *vh, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
  612. {
  613. int ipv6 = 0;
  614. #ifdef LWS_USE_IPV6
  615. ipv6 = LWS_IPV6_ENABLED(vh);
  616. #endif
  617. (void)vh;
  618. return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
  619. }
  620. #endif
  621. #ifndef LWS_PLAT_OPTEE
  622. #if LWS_POSIX
  623. static int
  624. lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
  625. int name_len, char *rip, int rip_len)
  626. {
  627. #if LWS_POSIX
  628. struct addrinfo ai, *res;
  629. struct sockaddr_in addr4;
  630. if (rip)
  631. rip[0] = '\0';
  632. name[0] = '\0';
  633. addr4.sin_family = AF_UNSPEC;
  634. #ifdef LWS_USE_IPV6
  635. if (LWS_IPV6_ENABLED(vh)) {
  636. if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
  637. lwsl_err("inet_ntop: %s", strerror(LWS_ERRNO));
  638. return -1;
  639. }
  640. // Strip off the IPv4 to IPv6 header if one exists
  641. if (strncmp(rip, "::ffff:", 7) == 0)
  642. memmove(rip, rip + 7, strlen(rip) - 6);
  643. getnameinfo((struct sockaddr *)ads,
  644. sizeof(struct sockaddr_in6), name,
  645. name_len, NULL, 0, 0);
  646. return 0;
  647. } else
  648. #endif
  649. {
  650. struct addrinfo *result;
  651. memset(&ai, 0, sizeof ai);
  652. ai.ai_family = PF_UNSPEC;
  653. ai.ai_socktype = SOCK_STREAM;
  654. ai.ai_flags = AI_CANONNAME;
  655. #if !defined(LWS_WITH_ESP32)
  656. if (getnameinfo((struct sockaddr *)ads,
  657. sizeof(struct sockaddr_in),
  658. name, name_len, NULL, 0, 0))
  659. return -1;
  660. #endif
  661. if (!rip)
  662. return 0;
  663. if (getaddrinfo(name, NULL, &ai, &result))
  664. return -1;
  665. res = result;
  666. while (addr4.sin_family == AF_UNSPEC && res) {
  667. switch (res->ai_family) {
  668. case AF_INET:
  669. addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
  670. addr4.sin_family = AF_INET;
  671. break;
  672. }
  673. res = res->ai_next;
  674. }
  675. freeaddrinfo(result);
  676. }
  677. if (addr4.sin_family == AF_UNSPEC)
  678. return -1;
  679. if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
  680. return -1;
  681. return 0;
  682. #else
  683. (void)vh;
  684. (void)ads;
  685. (void)name;
  686. (void)name_len;
  687. (void)rip;
  688. (void)rip_len;
  689. return -1;
  690. #endif
  691. }
  692. #endif
  693. LWS_VISIBLE const char *
  694. lws_get_peer_simple(struct lws *wsi, char *name, int namelen)
  695. {
  696. #if LWS_POSIX
  697. socklen_t len, olen;
  698. #ifdef LWS_USE_IPV6
  699. struct sockaddr_in6 sin6;
  700. #endif
  701. struct sockaddr_in sin4;
  702. int af = AF_INET;
  703. void *p, *q;
  704. #ifdef LWS_USE_IPV6
  705. if (LWS_IPV6_ENABLED(wsi->vhost)) {
  706. len = sizeof(sin6);
  707. p = &sin6;
  708. af = AF_INET6;
  709. q = &sin6.sin6_addr;
  710. } else
  711. #endif
  712. {
  713. len = sizeof(sin4);
  714. p = &sin4;
  715. q = &sin4.sin_addr;
  716. }
  717. olen = len;
  718. if (getpeername(wsi->desc.sockfd, p, &len) < 0 || len > olen) {
  719. lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
  720. return NULL;
  721. }
  722. return lws_plat_inet_ntop(af, q, name, namelen);
  723. #else
  724. #if defined(LWS_WITH_ESP8266)
  725. return lws_plat_get_peer_simple(wsi, name, namelen);
  726. #else
  727. return NULL;
  728. #endif
  729. #endif
  730. }
  731. #endif
  732. LWS_VISIBLE void
  733. lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
  734. int name_len, char *rip, int rip_len)
  735. {
  736. #ifndef LWS_PLAT_OPTEE
  737. #if LWS_POSIX
  738. socklen_t len;
  739. #ifdef LWS_USE_IPV6
  740. struct sockaddr_in6 sin6;
  741. #endif
  742. struct sockaddr_in sin4;
  743. struct lws_context *context = wsi->context;
  744. int ret = -1;
  745. void *p;
  746. rip[0] = '\0';
  747. name[0] = '\0';
  748. lws_latency_pre(context, wsi);
  749. #ifdef LWS_USE_IPV6
  750. if (LWS_IPV6_ENABLED(wsi->vhost)) {
  751. len = sizeof(sin6);
  752. p = &sin6;
  753. } else
  754. #endif
  755. {
  756. len = sizeof(sin4);
  757. p = &sin4;
  758. }
  759. if (getpeername(fd, p, &len) < 0) {
  760. lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
  761. goto bail;
  762. }
  763. ret = lws_get_addresses(wsi->vhost, p, name, name_len, rip, rip_len);
  764. bail:
  765. lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
  766. #endif
  767. #endif
  768. (void)wsi;
  769. (void)fd;
  770. (void)name;
  771. (void)name_len;
  772. (void)rip;
  773. (void)rip_len;
  774. }
  775. LWS_EXTERN void *
  776. lws_context_user(struct lws_context *context)
  777. {
  778. return context->user_space;
  779. }
  780. LWS_VISIBLE struct lws_vhost *
  781. lws_vhost_get(struct lws *wsi)
  782. {
  783. return wsi->vhost;
  784. }
  785. LWS_VISIBLE struct lws_vhost *
  786. lws_get_vhost(struct lws *wsi)
  787. {
  788. return wsi->vhost;
  789. }
  790. LWS_VISIBLE const struct lws_protocols *
  791. lws_protocol_get(struct lws *wsi)
  792. {
  793. return wsi->protocol;
  794. }
  795. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  796. lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
  797. {
  798. int n;
  799. for (n = 0; n < vh->count_protocols; n++)
  800. if (!strcmp(name, vh->protocols[n].name))
  801. return &vh->protocols[n];
  802. return NULL;
  803. }
  804. LWS_VISIBLE int
  805. lws_callback_all_protocol(struct lws_context *context,
  806. const struct lws_protocols *protocol, int reason)
  807. {
  808. struct lws_context_per_thread *pt = &context->pt[0];
  809. unsigned int n, m = context->count_threads;
  810. struct lws *wsi;
  811. while (m--) {
  812. for (n = 0; n < pt->fds_count; n++) {
  813. wsi = wsi_from_fd(context, pt->fds[n].fd);
  814. if (!wsi)
  815. continue;
  816. if (wsi->protocol == protocol)
  817. protocol->callback(wsi, reason, wsi->user_space,
  818. NULL, 0);
  819. }
  820. pt++;
  821. }
  822. return 0;
  823. }
  824. LWS_VISIBLE int
  825. lws_callback_all_protocol_vhost(struct lws_vhost *vh,
  826. const struct lws_protocols *protocol, int reason)
  827. {
  828. struct lws_context *context = vh->context;
  829. struct lws_context_per_thread *pt = &context->pt[0];
  830. unsigned int n, m = context->count_threads;
  831. struct lws *wsi;
  832. while (m--) {
  833. for (n = 0; n < pt->fds_count; n++) {
  834. wsi = wsi_from_fd(context, pt->fds[n].fd);
  835. if (!wsi)
  836. continue;
  837. if (wsi->vhost == vh && wsi->protocol == protocol)
  838. protocol->callback(wsi, reason, wsi->user_space,
  839. NULL, 0);
  840. }
  841. pt++;
  842. }
  843. return 0;
  844. }
  845. LWS_VISIBLE LWS_EXTERN int
  846. lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
  847. {
  848. int n;
  849. for (n = 0; n < wsi->vhost->count_protocols; n++)
  850. if (wsi->vhost->protocols[n].callback(wsi, reason, NULL, in, len))
  851. return 1;
  852. return 0;
  853. }
  854. LWS_VISIBLE LWS_EXTERN void
  855. lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
  856. {
  857. context->fops = fops;
  858. }
  859. LWS_VISIBLE LWS_EXTERN lws_filepos_t
  860. lws_vfs_tell(lws_fop_fd_t fop_fd)
  861. {
  862. return fop_fd->pos;
  863. }
  864. LWS_VISIBLE LWS_EXTERN lws_filepos_t
  865. lws_vfs_get_length(lws_fop_fd_t fop_fd)
  866. {
  867. return fop_fd->len;
  868. }
  869. LWS_VISIBLE LWS_EXTERN uint32_t
  870. lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
  871. {
  872. return fop_fd->mod_time;
  873. }
  874. LWS_VISIBLE lws_fileofs_t
  875. lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  876. {
  877. lws_fileofs_t ofs;
  878. lwsl_debug("%s: seeking to %ld, len %ld\n", __func__, (long)offset, (long)fop_fd->len);
  879. ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset - fop_fd->pos);
  880. lwsl_debug("%s: result %ld, fop_fd pos %ld\n", __func__, (long)ofs, (long)fop_fd->pos);
  881. return ofs;
  882. }
  883. LWS_VISIBLE lws_fileofs_t
  884. lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  885. {
  886. return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, fop_fd->len + fop_fd->pos + offset);
  887. }
  888. const struct lws_plat_file_ops *
  889. lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
  890. const char **vpath)
  891. {
  892. const struct lws_plat_file_ops *pf;
  893. const char *p = vfs_path;
  894. int n;
  895. *vpath = NULL;
  896. /* no non-platform fops, just use that */
  897. if (!fops->next)
  898. return fops;
  899. /*
  900. * scan the vfs path looking for indications we are to be
  901. * handled by a specific fops
  902. */
  903. while (p && *p) {
  904. if (*p != '/') {
  905. p++;
  906. continue;
  907. }
  908. /* the first one is always platform fops, so skip */
  909. pf = fops->next;
  910. while (pf) {
  911. n = 0;
  912. while (n < ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
  913. if (p >= vfs_path + pf->fi[n].len)
  914. if (!strncmp(p - (pf->fi[n].len - 1),
  915. pf->fi[n].sig,
  916. pf->fi[n].len - 1)) {
  917. *vpath = p + 1;
  918. return pf;
  919. }
  920. n++;
  921. }
  922. pf = pf->next;
  923. }
  924. p++;
  925. }
  926. return fops;
  927. }
  928. LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
  929. lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
  930. lws_fop_flags_t *flags)
  931. {
  932. const char *vpath = "";
  933. const struct lws_plat_file_ops *selected = lws_vfs_select_fops(
  934. fops, vfs_path, &vpath);
  935. return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
  936. }
  937. /**
  938. * lws_now_secs() - seconds since 1970-1-1
  939. *
  940. */
  941. LWS_VISIBLE LWS_EXTERN unsigned long
  942. lws_now_secs(void)
  943. {
  944. struct timeval tv;
  945. gettimeofday(&tv, NULL);
  946. return tv.tv_sec;
  947. }
  948. #if LWS_POSIX
  949. LWS_VISIBLE int
  950. lws_get_socket_fd(struct lws *wsi)
  951. {
  952. return wsi->desc.sockfd;
  953. }
  954. #endif
  955. #ifdef LWS_LATENCY
  956. void
  957. lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
  958. int ret, int completed)
  959. {
  960. unsigned long long u;
  961. char buf[256];
  962. u = time_in_microseconds();
  963. if (!action) {
  964. wsi->latency_start = u;
  965. if (!wsi->action_start)
  966. wsi->action_start = u;
  967. return;
  968. }
  969. if (completed) {
  970. if (wsi->action_start == wsi->latency_start)
  971. sprintf(buf,
  972. "Completion first try lat %lluus: %p: ret %d: %s\n",
  973. u - wsi->latency_start,
  974. (void *)wsi, ret, action);
  975. else
  976. sprintf(buf,
  977. "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
  978. u - wsi->action_start,
  979. u - wsi->latency_start,
  980. (void *)wsi, ret, action);
  981. wsi->action_start = 0;
  982. } else
  983. sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
  984. u - wsi->latency_start, (void *)wsi, ret, action);
  985. if (u - wsi->latency_start > context->worst_latency) {
  986. context->worst_latency = u - wsi->latency_start;
  987. strcpy(context->worst_latency_info, buf);
  988. }
  989. lwsl_latency("%s", buf);
  990. }
  991. #endif
  992. LWS_VISIBLE int
  993. lws_rx_flow_control(struct lws *wsi, int enable)
  994. {
  995. if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
  996. return 0;
  997. lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
  998. wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
  999. return 0;
  1000. }
  1001. LWS_VISIBLE void
  1002. lws_rx_flow_allow_all_protocol(const struct lws_context *context,
  1003. const struct lws_protocols *protocol)
  1004. {
  1005. const struct lws_context_per_thread *pt = &context->pt[0];
  1006. struct lws *wsi;
  1007. unsigned int n, m = context->count_threads;
  1008. while (m--) {
  1009. for (n = 0; n < pt->fds_count; n++) {
  1010. wsi = wsi_from_fd(context, pt->fds[n].fd);
  1011. if (!wsi)
  1012. continue;
  1013. if (wsi->protocol == protocol)
  1014. lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
  1015. }
  1016. pt++;
  1017. }
  1018. }
  1019. LWS_VISIBLE extern const char *
  1020. lws_canonical_hostname(struct lws_context *context)
  1021. {
  1022. return (const char *)context->canonical_hostname;
  1023. }
  1024. int user_callback_handle_rxflow(lws_callback_function callback_function,
  1025. struct lws *wsi,
  1026. enum lws_callback_reasons reason, void *user,
  1027. void *in, size_t len)
  1028. {
  1029. int n;
  1030. n = callback_function(wsi, reason, user, in, len);
  1031. if (!n)
  1032. n = _lws_rx_flow_control(wsi);
  1033. return n;
  1034. }
  1035. #if defined(LWS_WITH_ESP8266)
  1036. #undef strchr
  1037. #define strchr ets_strchr
  1038. #endif
  1039. LWS_VISIBLE int
  1040. lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
  1041. {
  1042. #if !defined(LWS_WITH_ESP8266)
  1043. char *p;
  1044. char authstring[96];
  1045. if (!proxy)
  1046. return -1;
  1047. /* we have to deal with a possible redundant leading http:// */
  1048. if (!strncmp(proxy, "http://", 7))
  1049. proxy += 7;
  1050. p = strchr(proxy, '@');
  1051. if (p) { /* auth is around */
  1052. if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
  1053. goto auth_too_long;
  1054. strncpy(authstring, proxy, p - proxy);
  1055. // null termination not needed on input
  1056. if (lws_b64_encode_string(authstring, (p - proxy),
  1057. vhost->proxy_basic_auth_token,
  1058. sizeof vhost->proxy_basic_auth_token) < 0)
  1059. goto auth_too_long;
  1060. lwsl_info(" Proxy auth in use\n");
  1061. proxy = p + 1;
  1062. } else
  1063. vhost->proxy_basic_auth_token[0] = '\0';
  1064. strncpy(vhost->http_proxy_address, proxy,
  1065. sizeof(vhost->http_proxy_address) - 1);
  1066. vhost->http_proxy_address[
  1067. sizeof(vhost->http_proxy_address) - 1] = '\0';
  1068. p = strchr(vhost->http_proxy_address, ':');
  1069. if (!p && !vhost->http_proxy_port) {
  1070. lwsl_err("http_proxy needs to be ads:port\n");
  1071. return -1;
  1072. } else {
  1073. if (p) {
  1074. *p = '\0';
  1075. vhost->http_proxy_port = atoi(p + 1);
  1076. }
  1077. }
  1078. lwsl_info(" Proxy %s:%u\n", vhost->http_proxy_address,
  1079. vhost->http_proxy_port);
  1080. return 0;
  1081. auth_too_long:
  1082. lwsl_err("proxy auth too long\n");
  1083. #endif
  1084. return -1;
  1085. }
  1086. LWS_VISIBLE const struct lws_protocols *
  1087. lws_get_protocol(struct lws *wsi)
  1088. {
  1089. return wsi->protocol;
  1090. }
  1091. LWS_VISIBLE int
  1092. lws_is_final_fragment(struct lws *wsi)
  1093. {
  1094. lwsl_info("%s: final %d, rx pk length %ld, draining %ld\n", __func__,
  1095. wsi->u.ws.final, (long)wsi->u.ws.rx_packet_length,
  1096. (long)wsi->u.ws.rx_draining_ext);
  1097. return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
  1098. }
  1099. LWS_VISIBLE unsigned char
  1100. lws_get_reserved_bits(struct lws *wsi)
  1101. {
  1102. return wsi->u.ws.rsv;
  1103. }
  1104. int
  1105. lws_ensure_user_space(struct lws *wsi)
  1106. {
  1107. lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
  1108. if (!wsi->protocol)
  1109. return 1;
  1110. /* allocate the per-connection user memory (if any) */
  1111. if (wsi->protocol->per_session_data_size && !wsi->user_space) {
  1112. wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
  1113. if (wsi->user_space == NULL) {
  1114. lwsl_err("Out of memory for conn user space\n");
  1115. return 1;
  1116. }
  1117. } else
  1118. lwsl_info("%s: %p protocol pss %lu, user_space=%p\n",
  1119. __func__, wsi, (long)wsi->protocol->per_session_data_size,
  1120. wsi->user_space);
  1121. return 0;
  1122. }
  1123. LWS_VISIBLE int
  1124. lwsl_timestamp(int level, char *p, int len)
  1125. {
  1126. #ifndef LWS_PLAT_OPTEE
  1127. time_t o_now = time(NULL);
  1128. unsigned long long now;
  1129. struct tm *ptm = NULL;
  1130. #ifndef WIN32
  1131. struct tm tm;
  1132. #endif
  1133. int n;
  1134. #ifndef _WIN32_WCE
  1135. #ifdef WIN32
  1136. ptm = localtime(&o_now);
  1137. #else
  1138. if (localtime_r(&o_now, &tm))
  1139. ptm = &tm;
  1140. #endif
  1141. #endif
  1142. p[0] = '\0';
  1143. for (n = 0; n < LLL_COUNT; n++) {
  1144. if (level != (1 << n))
  1145. continue;
  1146. now = time_in_microseconds() / 100;
  1147. if (ptm)
  1148. n = lws_snprintf(p, len,
  1149. "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
  1150. ptm->tm_year + 1900,
  1151. ptm->tm_mon + 1,
  1152. ptm->tm_mday,
  1153. ptm->tm_hour,
  1154. ptm->tm_min,
  1155. ptm->tm_sec,
  1156. (int)(now % 10000), log_level_names[n]);
  1157. else
  1158. n = lws_snprintf(p, len, "[%llu:%04d] %s: ",
  1159. (unsigned long long) now / 10000,
  1160. (int)(now % 10000), log_level_names[n]);
  1161. return n;
  1162. }
  1163. #endif
  1164. return 0;
  1165. }
  1166. #ifndef LWS_PLAT_OPTEE
  1167. LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
  1168. {
  1169. #if !defined(LWS_WITH_ESP8266)
  1170. char buf[50];
  1171. lwsl_timestamp(level, buf, sizeof(buf));
  1172. fprintf(stderr, "%s%s", buf, line);
  1173. #endif
  1174. }
  1175. #endif
  1176. LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
  1177. {
  1178. #if defined(LWS_WITH_ESP8266)
  1179. char buf[128];
  1180. #else
  1181. char buf[256];
  1182. #endif
  1183. int n;
  1184. if (!(log_level & filter))
  1185. return;
  1186. n = vsnprintf(buf, sizeof(buf) - 1, format, vl);
  1187. (void)n;
  1188. #if defined(LWS_WITH_ESP8266)
  1189. buf[sizeof(buf) - 1] = '\0';
  1190. #else
  1191. /* vnsprintf returns what it would have written, even if truncated */
  1192. if (n > sizeof(buf) - 1)
  1193. n = sizeof(buf) - 1;
  1194. if (n > 0)
  1195. buf[n] = '\0';
  1196. #endif
  1197. lwsl_emit(filter, buf);
  1198. }
  1199. LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
  1200. {
  1201. va_list ap;
  1202. va_start(ap, format);
  1203. _lws_logv(filter, format, ap);
  1204. va_end(ap);
  1205. }
  1206. LWS_VISIBLE void lws_set_log_level(int level,
  1207. void (*func)(int level, const char *line))
  1208. {
  1209. log_level = level;
  1210. if (func)
  1211. lwsl_emit = func;
  1212. }
  1213. LWS_VISIBLE int lwsl_visible(int level)
  1214. {
  1215. return log_level & level;
  1216. }
  1217. LWS_VISIBLE int
  1218. lws_is_ssl(struct lws *wsi)
  1219. {
  1220. #ifdef LWS_OPENSSL_SUPPORT
  1221. return wsi->use_ssl;
  1222. #else
  1223. (void)wsi;
  1224. return 0;
  1225. #endif
  1226. }
  1227. #ifdef LWS_OPENSSL_SUPPORT
  1228. LWS_VISIBLE SSL*
  1229. lws_get_ssl(struct lws *wsi)
  1230. {
  1231. return wsi->ssl;
  1232. }
  1233. #endif
  1234. LWS_VISIBLE int
  1235. lws_partial_buffered(struct lws *wsi)
  1236. {
  1237. return !!wsi->trunc_len;
  1238. }
  1239. void lws_set_protocol_write_pending(struct lws *wsi,
  1240. enum lws_pending_protocol_send pend)
  1241. {
  1242. lwsl_info("setting pps %d\n", pend);
  1243. if (wsi->pps)
  1244. lwsl_err("pps overwrite\n");
  1245. wsi->pps = pend;
  1246. lws_rx_flow_control(wsi, 0);
  1247. lws_callback_on_writable(wsi);
  1248. }
  1249. LWS_VISIBLE size_t
  1250. lws_get_peer_write_allowance(struct lws *wsi)
  1251. {
  1252. #ifdef LWS_USE_HTTP2
  1253. /* only if we are using HTTP2 on this connection */
  1254. if (wsi->mode != LWSCM_HTTP2_SERVING)
  1255. return -1;
  1256. /* user is only interested in how much he can send, or that he can't */
  1257. if (wsi->u.http2.tx_credit <= 0)
  1258. return 0;
  1259. return wsi->u.http2.tx_credit;
  1260. #else
  1261. (void)wsi;
  1262. return -1;
  1263. #endif
  1264. }
  1265. LWS_VISIBLE void
  1266. lws_union_transition(struct lws *wsi, enum connection_mode mode)
  1267. {
  1268. lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
  1269. memset(&wsi->u, 0, sizeof(wsi->u));
  1270. wsi->mode = mode;
  1271. }
  1272. LWS_VISIBLE struct lws_plat_file_ops *
  1273. lws_get_fops(struct lws_context *context)
  1274. {
  1275. return (struct lws_plat_file_ops *)context->fops;
  1276. }
  1277. LWS_VISIBLE LWS_EXTERN struct lws_context *
  1278. lws_get_context(const struct lws *wsi)
  1279. {
  1280. return wsi->context;
  1281. }
  1282. LWS_VISIBLE LWS_EXTERN int
  1283. lws_get_count_threads(struct lws_context *context)
  1284. {
  1285. return context->count_threads;
  1286. }
  1287. LWS_VISIBLE LWS_EXTERN void *
  1288. lws_wsi_user(struct lws *wsi)
  1289. {
  1290. return wsi->user_space;
  1291. }
  1292. LWS_VISIBLE LWS_EXTERN void
  1293. lws_set_wsi_user(struct lws *wsi, void *data)
  1294. {
  1295. if (wsi->user_space_externally_allocated)
  1296. wsi->user_space = data;
  1297. else
  1298. lwsl_err("%s: Cannot set internally-allocated user_space\n",
  1299. __func__);
  1300. }
  1301. LWS_VISIBLE LWS_EXTERN struct lws *
  1302. lws_get_parent(const struct lws *wsi)
  1303. {
  1304. return wsi->parent;
  1305. }
  1306. LWS_VISIBLE LWS_EXTERN struct lws *
  1307. lws_get_child(const struct lws *wsi)
  1308. {
  1309. return wsi->child_list;
  1310. }
  1311. LWS_VISIBLE LWS_EXTERN void
  1312. lws_close_reason(struct lws *wsi, enum lws_close_status status,
  1313. unsigned char *buf, size_t len)
  1314. {
  1315. unsigned char *p, *start;
  1316. int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
  1317. assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
  1318. start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
  1319. *p++ = (((int)status) >> 8) & 0xff;
  1320. *p++ = ((int)status) & 0xff;
  1321. if (buf)
  1322. while (len-- && p < start + budget)
  1323. *p++ = *buf++;
  1324. wsi->u.ws.close_in_ping_buffer_len = p - start;
  1325. }
  1326. LWS_EXTERN int
  1327. _lws_rx_flow_control(struct lws *wsi)
  1328. {
  1329. struct lws *wsic = wsi->child_list;
  1330. /* if he has children, do those if they were changed */
  1331. while (wsic) {
  1332. if (wsic->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)
  1333. _lws_rx_flow_control(wsic);
  1334. wsic = wsic->sibling_list;
  1335. }
  1336. /* there is no pending change */
  1337. if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
  1338. lwsl_debug("%s: no pending change\n", __func__);
  1339. return 0;
  1340. }
  1341. /* stuff is still buffered, not ready to really accept new input */
  1342. if (wsi->rxflow_buffer) {
  1343. /* get ourselves called back to deal with stashed buffer */
  1344. lws_callback_on_writable(wsi);
  1345. return 0;
  1346. }
  1347. /* pending is cleared, we can change rxflow state */
  1348. wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
  1349. lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
  1350. wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
  1351. /* adjust the pollfd for this wsi */
  1352. if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
  1353. if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
  1354. lwsl_info("%s: fail\n", __func__);
  1355. return -1;
  1356. }
  1357. } else
  1358. if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
  1359. return -1;
  1360. return 0;
  1361. }
  1362. LWS_EXTERN int
  1363. lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
  1364. {
  1365. static const unsigned char e0f4[] = {
  1366. 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
  1367. 0x80 | ((4 - 1) << 2) | 1, /* e1 */
  1368. 0x80 | ((4 - 1) << 2) | 1, /* e2 */
  1369. 0x80 | ((4 - 1) << 2) | 1, /* e3 */
  1370. 0x80 | ((4 - 1) << 2) | 1, /* e4 */
  1371. 0x80 | ((4 - 1) << 2) | 1, /* e5 */
  1372. 0x80 | ((4 - 1) << 2) | 1, /* e6 */
  1373. 0x80 | ((4 - 1) << 2) | 1, /* e7 */
  1374. 0x80 | ((4 - 1) << 2) | 1, /* e8 */
  1375. 0x80 | ((4 - 1) << 2) | 1, /* e9 */
  1376. 0x80 | ((4 - 1) << 2) | 1, /* ea */
  1377. 0x80 | ((4 - 1) << 2) | 1, /* eb */
  1378. 0x80 | ((4 - 1) << 2) | 1, /* ec */
  1379. 0x80 | ((2 - 1) << 2) | 1, /* ed */
  1380. 0x80 | ((4 - 1) << 2) | 1, /* ee */
  1381. 0x80 | ((4 - 1) << 2) | 1, /* ef */
  1382. 0x90 | ((3 - 1) << 2) | 2, /* f0 */
  1383. 0x80 | ((4 - 1) << 2) | 2, /* f1 */
  1384. 0x80 | ((4 - 1) << 2) | 2, /* f2 */
  1385. 0x80 | ((4 - 1) << 2) | 2, /* f3 */
  1386. 0x80 | ((1 - 1) << 2) | 2, /* f4 */
  1387. 0, /* s0 */
  1388. 0x80 | ((4 - 1) << 2) | 0, /* s2 */
  1389. 0x80 | ((4 - 1) << 2) | 1, /* s3 */
  1390. };
  1391. unsigned char s = *state;
  1392. while (len--) {
  1393. unsigned char c = *buf++;
  1394. if (!s) {
  1395. if (c >= 0x80) {
  1396. if (c < 0xc2 || c > 0xf4)
  1397. return 1;
  1398. if (c < 0xe0)
  1399. s = 0x80 | ((4 - 1) << 2);
  1400. else
  1401. s = e0f4[c - 0xe0];
  1402. }
  1403. } else {
  1404. if (c < (s & 0xf0) ||
  1405. c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
  1406. return 1;
  1407. s = e0f4[21 + (s & 3)];
  1408. }
  1409. }
  1410. *state = s;
  1411. return 0;
  1412. }
  1413. LWS_VISIBLE LWS_EXTERN int
  1414. lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
  1415. const char **path)
  1416. {
  1417. const char *end;
  1418. static const char *slash = "/";
  1419. /* cut up the location into address, port and path */
  1420. *prot = p;
  1421. while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
  1422. p++;
  1423. if (!*p) {
  1424. end = p;
  1425. p = (char *)*prot;
  1426. *prot = end;
  1427. } else {
  1428. *p = '\0';
  1429. p += 3;
  1430. }
  1431. *ads = p;
  1432. if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
  1433. *port = 80;
  1434. else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
  1435. *port = 443;
  1436. if (*p == '[')
  1437. {
  1438. ++(*ads);
  1439. while (*p && *p != ']')
  1440. p++;
  1441. if (*p)
  1442. *p++ = '\0';
  1443. }
  1444. else
  1445. {
  1446. while (*p && *p != ':' && *p != '/')
  1447. p++;
  1448. }
  1449. if (*p == ':') {
  1450. *p++ = '\0';
  1451. *port = atoi(p);
  1452. while (*p && *p != '/')
  1453. p++;
  1454. }
  1455. *path = slash;
  1456. if (*p) {
  1457. *p++ = '\0';
  1458. if (*p)
  1459. *path = p;
  1460. }
  1461. return 0;
  1462. }
  1463. #ifdef LWS_NO_EXTENSIONS
  1464. /* we need to provide dummy callbacks for internal exts
  1465. * so user code runs when faced with a lib compiled with
  1466. * extensions disabled.
  1467. */
  1468. int
  1469. lws_extension_callback_pm_deflate(struct lws_context *context,
  1470. const struct lws_extension *ext,
  1471. struct lws *wsi,
  1472. enum lws_extension_callback_reasons reason,
  1473. void *user, void *in, size_t len)
  1474. {
  1475. (void)context;
  1476. (void)ext;
  1477. (void)wsi;
  1478. (void)reason;
  1479. (void)user;
  1480. (void)in;
  1481. (void)len;
  1482. return 0;
  1483. }
  1484. #endif
  1485. LWS_EXTERN int
  1486. lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
  1487. const char *iface)
  1488. {
  1489. #if LWS_POSIX
  1490. #ifdef LWS_USE_UNIX_SOCK
  1491. struct sockaddr_un serv_unix;
  1492. #endif
  1493. #ifdef LWS_USE_IPV6
  1494. struct sockaddr_in6 serv_addr6;
  1495. #endif
  1496. struct sockaddr_in serv_addr4;
  1497. #ifndef LWS_PLAT_OPTEE
  1498. socklen_t len = sizeof(struct sockaddr_storage);
  1499. #endif
  1500. int n;
  1501. struct sockaddr_storage sin;
  1502. struct sockaddr *v;
  1503. #ifdef LWS_USE_UNIX_SOCK
  1504. if (LWS_UNIX_SOCK_ENABLED(vhost)) {
  1505. v = (struct sockaddr *)&serv_unix;
  1506. n = sizeof(struct sockaddr_un);
  1507. bzero((char *) &serv_unix, sizeof(serv_unix));
  1508. serv_unix.sun_family = AF_UNIX;
  1509. if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
  1510. lwsl_err("\"%s\" too long for UNIX domain socket\n",
  1511. iface);
  1512. return -1;
  1513. }
  1514. strcpy(serv_unix.sun_path, iface);
  1515. if (serv_unix.sun_path[0] == '@')
  1516. serv_unix.sun_path[0] = '\0';
  1517. } else
  1518. #endif
  1519. #if defined(LWS_USE_IPV6) && !defined(LWS_WITH_ESP32)
  1520. if (LWS_IPV6_ENABLED(vhost)) {
  1521. v = (struct sockaddr *)&serv_addr6;
  1522. n = sizeof(struct sockaddr_in6);
  1523. bzero((char *) &serv_addr6, sizeof(serv_addr6));
  1524. if (iface &&
  1525. interface_to_sa(vhost, iface,
  1526. (struct sockaddr_in *)v, n) < 0) {
  1527. lwsl_err("Unable to find interface %s\n", iface);
  1528. return -1;
  1529. }
  1530. #ifndef WIN32
  1531. if (iface) {
  1532. struct ifaddrs *addrs, *addr;
  1533. char ip[NI_MAXHOST];
  1534. unsigned int i;
  1535. getifaddrs(&addrs);
  1536. for (addr = addrs; addr; addr = addr->ifa_next) {
  1537. if (!addr->ifa_addr ||
  1538. addr->ifa_addr->sa_family != AF_INET6)
  1539. continue;
  1540. getnameinfo(addr->ifa_addr,
  1541. sizeof(struct sockaddr_in6),
  1542. ip, sizeof(ip),
  1543. NULL, 0, NI_NUMERICHOST);
  1544. i = 0;
  1545. while (ip[i])
  1546. if (ip[i++] == '%') {
  1547. ip[i - 1] = '\0';
  1548. break;
  1549. }
  1550. if (!strcmp(ip, iface)) {
  1551. serv_addr6.sin6_scope_id =
  1552. if_nametoindex(addr->ifa_name);
  1553. break;
  1554. }
  1555. }
  1556. freeifaddrs(addrs);
  1557. }
  1558. #endif
  1559. serv_addr6.sin6_family = AF_INET6;
  1560. serv_addr6.sin6_port = htons(port);
  1561. } else
  1562. #endif
  1563. {
  1564. v = (struct sockaddr *)&serv_addr4;
  1565. n = sizeof(serv_addr4);
  1566. bzero((char *) &serv_addr4, sizeof(serv_addr4));
  1567. serv_addr4.sin_addr.s_addr = INADDR_ANY;
  1568. serv_addr4.sin_family = AF_INET;
  1569. #if !defined(LWS_WITH_ESP32)
  1570. if (iface &&
  1571. interface_to_sa(vhost, iface,
  1572. (struct sockaddr_in *)v, n) < 0) {
  1573. lwsl_err("Unable to find interface %s\n", iface);
  1574. return -1;
  1575. }
  1576. #endif
  1577. serv_addr4.sin_port = htons(port);
  1578. } /* ipv4 */
  1579. n = bind(sockfd, v, n);
  1580. #ifdef LWS_USE_UNIX_SOCK
  1581. if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
  1582. lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
  1583. sockfd, iface, n, LWS_ERRNO);
  1584. return -1;
  1585. } else
  1586. #endif
  1587. if (n < 0) {
  1588. lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n",
  1589. sockfd, port, n, LWS_ERRNO);
  1590. return -1;
  1591. }
  1592. #ifndef LWS_PLAT_OPTEE
  1593. if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
  1594. lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
  1595. else
  1596. #endif
  1597. #if defined(LWS_USE_IPV6)
  1598. port = (sin.ss_family == AF_INET6) ?
  1599. ntohs(((struct sockaddr_in6 *) &sin)->sin6_port) :
  1600. ntohs(((struct sockaddr_in *) &sin)->sin_port);
  1601. #else
  1602. port = ntohs(((struct sockaddr_in *) &sin)->sin_port);
  1603. #endif
  1604. #endif
  1605. return port;
  1606. }
  1607. LWS_EXTERN void
  1608. lws_restart_ws_ping_pong_timer(struct lws *wsi)
  1609. {
  1610. if (!wsi->context->ws_ping_pong_interval)
  1611. return;
  1612. if (wsi->state != LWSS_ESTABLISHED)
  1613. return;
  1614. wsi->u.ws.time_next_ping_check = (time_t)lws_now_secs() +
  1615. wsi->context->ws_ping_pong_interval;
  1616. }
  1617. static const char *hex = "0123456789ABCDEF";
  1618. LWS_VISIBLE LWS_EXTERN const char *
  1619. lws_sql_purify(char *escaped, const char *string, int len)
  1620. {
  1621. const char *p = string;
  1622. char *q = escaped;
  1623. while (*p && len-- > 2) {
  1624. if (*p == '\'') {
  1625. *q++ = '\'';
  1626. *q++ = '\'';
  1627. len --;
  1628. p++;
  1629. } else
  1630. *q++ = *p++;
  1631. }
  1632. *q = '\0';
  1633. return escaped;
  1634. }
  1635. LWS_VISIBLE LWS_EXTERN const char *
  1636. lws_json_purify(char *escaped, const char *string, int len)
  1637. {
  1638. const char *p = string;
  1639. char *q = escaped;
  1640. if (!p) {
  1641. escaped[0] = '\0';
  1642. return escaped;
  1643. }
  1644. while (*p && len-- > 6) {
  1645. if (*p == '\"' || *p == '\\' || *p < 0x20) {
  1646. *q++ = '\\';
  1647. *q++ = 'u';
  1648. *q++ = '0';
  1649. *q++ = '0';
  1650. *q++ = hex[((*p) >> 4) & 15];
  1651. *q++ = hex[(*p) & 15];
  1652. len -= 5;
  1653. p++;
  1654. } else
  1655. *q++ = *p++;
  1656. }
  1657. *q = '\0';
  1658. return escaped;
  1659. }
  1660. LWS_VISIBLE LWS_EXTERN const char *
  1661. lws_urlencode(char *escaped, const char *string, int len)
  1662. {
  1663. const char *p = string;
  1664. char *q = escaped;
  1665. while (*p && len-- > 3) {
  1666. if (*p == ' ') {
  1667. *q++ = '+';
  1668. p++;
  1669. continue;
  1670. }
  1671. if ((*p >= '0' && *p <= '9') ||
  1672. (*p >= 'A' && *p <= 'Z') ||
  1673. (*p >= 'a' && *p <= 'z')) {
  1674. *q++ = *p++;
  1675. continue;
  1676. }
  1677. *q++ = '%';
  1678. *q++ = hex[(*p >> 4) & 0xf];
  1679. *q++ = hex[*p & 0xf];
  1680. len -= 2;
  1681. p++;
  1682. }
  1683. *q = '\0';
  1684. return escaped;
  1685. }
  1686. LWS_VISIBLE LWS_EXTERN int
  1687. lws_urldecode(char *string, const char *escaped, int len)
  1688. {
  1689. int state = 0, n;
  1690. char sum = 0;
  1691. while (*escaped && len) {
  1692. switch (state) {
  1693. case 0:
  1694. if (*escaped == '%') {
  1695. state++;
  1696. escaped++;
  1697. continue;
  1698. }
  1699. if (*escaped == '+') {
  1700. escaped++;
  1701. *string++ = ' ';
  1702. len--;
  1703. continue;
  1704. }
  1705. *string++ = *escaped++;
  1706. len--;
  1707. break;
  1708. case 1:
  1709. n = char_to_hex(*escaped);
  1710. if (n < 0)
  1711. return -1;
  1712. escaped++;
  1713. sum = n << 4;
  1714. state++;
  1715. break;
  1716. case 2:
  1717. n = char_to_hex(*escaped);
  1718. if (n < 0)
  1719. return -1;
  1720. escaped++;
  1721. *string++ = sum | n;
  1722. len--;
  1723. state = 0;
  1724. break;
  1725. }
  1726. }
  1727. *string = '\0';
  1728. return 0;
  1729. }
  1730. LWS_VISIBLE LWS_EXTERN int
  1731. lws_finalize_startup(struct lws_context *context)
  1732. {
  1733. struct lws_context_creation_info info;
  1734. info.uid = context->uid;
  1735. info.gid = context->gid;
  1736. if (lws_check_opt(context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
  1737. lws_plat_drop_app_privileges(&info);
  1738. return 0;
  1739. }
  1740. int
  1741. lws_snprintf(char *str, size_t size, const char *format, ...)
  1742. {
  1743. va_list ap;
  1744. int n;
  1745. if (!size)
  1746. return 0;
  1747. va_start(ap, format);
  1748. n = vsnprintf(str, size, format, ap);
  1749. va_end(ap);
  1750. if (n >= size)
  1751. return size;
  1752. return n;
  1753. }
  1754. LWS_VISIBLE LWS_EXTERN int
  1755. lws_is_cgi(struct lws *wsi) {
  1756. #ifdef LWS_WITH_CGI
  1757. return !!wsi->cgi;
  1758. #else
  1759. return 0;
  1760. #endif
  1761. }
  1762. #ifdef LWS_WITH_CGI
  1763. static int
  1764. urlencode(const char *in, int inlen, char *out, int outlen)
  1765. {
  1766. char *start = out, *end = out + outlen;
  1767. while (inlen-- && out < end - 4) {
  1768. if ((*in >= 'A' && *in <= 'Z') ||
  1769. (*in >= 'a' && *in <= 'z') ||
  1770. (*in >= '0' && *in <= '9') ||
  1771. *in == '-' ||
  1772. *in == '_' ||
  1773. *in == '.' ||
  1774. *in == '~') {
  1775. *out++ = *in++;
  1776. continue;
  1777. }
  1778. if (*in == ' ') {
  1779. *out++ = '+';
  1780. in++;
  1781. continue;
  1782. }
  1783. *out++ = '%';
  1784. *out++ = hex[(*in) >> 4];
  1785. *out++ = hex[(*in++) & 15];
  1786. }
  1787. *out = '\0';
  1788. if (out >= end - 4)
  1789. return -1;
  1790. return out - start;
  1791. }
  1792. static struct lws *
  1793. lws_create_basic_wsi(struct lws_context *context, int tsi)
  1794. {
  1795. struct lws *new_wsi;
  1796. if ((unsigned int)context->pt[tsi].fds_count ==
  1797. context->fd_limit_per_thread - 1) {
  1798. lwsl_err("no space for new conn\n");
  1799. return NULL;
  1800. }
  1801. new_wsi = lws_zalloc(sizeof(struct lws));
  1802. if (new_wsi == NULL) {
  1803. lwsl_err("Out of memory for new connection\n");
  1804. return NULL;
  1805. }
  1806. new_wsi->tsi = tsi;
  1807. new_wsi->context = context;
  1808. new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
  1809. new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
  1810. /* initialize the instance struct */
  1811. new_wsi->state = LWSS_CGI;
  1812. new_wsi->mode = LWSCM_CGI;
  1813. new_wsi->hdr_parsing_completed = 0;
  1814. new_wsi->position_in_fds_table = -1;
  1815. /*
  1816. * these can only be set once the protocol is known
  1817. * we set an unestablished connection's protocol pointer
  1818. * to the start of the defauly vhost supported list, so it can look
  1819. * for matching ones during the handshake
  1820. */
  1821. new_wsi->protocol = context->vhost_list->protocols;
  1822. new_wsi->user_space = NULL;
  1823. new_wsi->ietf_spec_revision = 0;
  1824. new_wsi->desc.sockfd = LWS_SOCK_INVALID;
  1825. context->count_wsi_allocated++;
  1826. return new_wsi;
  1827. }
  1828. LWS_VISIBLE LWS_EXTERN int
  1829. lws_cgi(struct lws *wsi, const char * const *exec_array, int script_uri_path_len,
  1830. int timeout_secs, const struct lws_protocol_vhost_options *mp_cgienv)
  1831. {
  1832. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  1833. char *env_array[30], cgi_path[400], e[1024], *p = e,
  1834. *end = p + sizeof(e) - 1, tok[256], *t;
  1835. struct lws_cgi *cgi;
  1836. int n, m, i, uritok = -1;
  1837. /*
  1838. * give the master wsi a cgi struct
  1839. */
  1840. wsi->cgi = lws_zalloc(sizeof(*wsi->cgi));
  1841. if (!wsi->cgi) {
  1842. lwsl_err("%s: OOM\n", __func__);
  1843. return -1;
  1844. }
  1845. cgi = wsi->cgi;
  1846. cgi->wsi = wsi; /* set cgi's owning wsi */
  1847. /* create pipes for [stdin|stdout] and [stderr] */
  1848. for (n = 0; n < 3; n++)
  1849. if (pipe(cgi->pipe_fds[n]) == -1)
  1850. goto bail1;
  1851. /* create cgi wsis for each stdin/out/err fd */
  1852. for (n = 0; n < 3; n++) {
  1853. cgi->stdwsi[n] = lws_create_basic_wsi(wsi->context, wsi->tsi);
  1854. if (!cgi->stdwsi[n])
  1855. goto bail2;
  1856. cgi->stdwsi[n]->cgi_channel = n;
  1857. cgi->stdwsi[n]->vhost = wsi->vhost;
  1858. // lwsl_err("%s: cgi %p: pipe fd %d -> fd %d / %d\n", __func__, wsi, n,
  1859. // cgi->pipe_fds[n][!!(n == 0)], cgi->pipe_fds[n][!(n == 0)]);
  1860. /* read side is 0, stdin we want the write side, others read */
  1861. cgi->stdwsi[n]->desc.sockfd = cgi->pipe_fds[n][!!(n == 0)];
  1862. if (fcntl(cgi->pipe_fds[n][!!(n == 0)], F_SETFL, O_NONBLOCK) < 0) {
  1863. lwsl_err("%s: setting NONBLOCK failed\n", __func__);
  1864. goto bail2;
  1865. }
  1866. }
  1867. for (n = 0; n < 3; n++) {
  1868. lws_libuv_accept(cgi->stdwsi[n], cgi->stdwsi[n]->desc);
  1869. if (insert_wsi_socket_into_fds(wsi->context, cgi->stdwsi[n]))
  1870. goto bail3;
  1871. cgi->stdwsi[n]->parent = wsi;
  1872. cgi->stdwsi[n]->sibling_list = wsi->child_list;
  1873. wsi->child_list = cgi->stdwsi[n];
  1874. }
  1875. lws_change_pollfd(cgi->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT);
  1876. lws_change_pollfd(cgi->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN);
  1877. lws_change_pollfd(cgi->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN);
  1878. lwsl_debug("%s: fds in %d, out %d, err %d\n", __func__,
  1879. cgi->stdwsi[LWS_STDIN]->desc.sockfd,
  1880. cgi->stdwsi[LWS_STDOUT]->desc.sockfd,
  1881. cgi->stdwsi[LWS_STDERR]->desc.sockfd);
  1882. lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, timeout_secs);
  1883. /* the cgi stdout is always sending us http1.x header data first */
  1884. wsi->hdr_state = LCHS_HEADER;
  1885. /* add us to the pt list of active cgis */
  1886. lwsl_debug("%s: adding cgi %p to list\n", __func__, wsi->cgi);
  1887. cgi->cgi_list = pt->cgi_list;
  1888. pt->cgi_list = cgi;
  1889. /* prepare his CGI env */
  1890. n = 0;
  1891. if (lws_is_ssl(wsi))
  1892. env_array[n++] = "HTTPS=ON";
  1893. if (wsi->u.hdr.ah) {
  1894. static const unsigned char meths[] = {
  1895. WSI_TOKEN_GET_URI,
  1896. WSI_TOKEN_POST_URI,
  1897. WSI_TOKEN_OPTIONS_URI,
  1898. WSI_TOKEN_PUT_URI,
  1899. WSI_TOKEN_PATCH_URI,
  1900. WSI_TOKEN_DELETE_URI,
  1901. };
  1902. static const char * const meth_names[] = {
  1903. "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
  1904. };
  1905. for (m = 0; m < ARRAY_SIZE(meths); m++)
  1906. if (lws_hdr_total_length(wsi, meths[m]) >=
  1907. script_uri_path_len) {
  1908. uritok = meths[m];
  1909. break;
  1910. }
  1911. if (uritok < 0)
  1912. goto bail3;
  1913. lws_snprintf(cgi_path, sizeof(cgi_path) - 1, "REQUEST_URI=%s",
  1914. lws_hdr_simple_ptr(wsi, uritok));
  1915. cgi_path[sizeof(cgi_path) - 1] = '\0';
  1916. env_array[n++] = cgi_path;
  1917. env_array[n++] = p;
  1918. p += lws_snprintf(p, end - p, "REQUEST_METHOD=%s",
  1919. meth_names[m]);
  1920. p++;
  1921. env_array[n++] = p;
  1922. p += lws_snprintf(p, end - p, "QUERY_STRING=");
  1923. /* dump the individual URI Arg parameters */
  1924. m = 0;
  1925. while (1) {
  1926. i = lws_hdr_copy_fragment(wsi, tok, sizeof(tok),
  1927. WSI_TOKEN_HTTP_URI_ARGS, m);
  1928. if (i < 0)
  1929. break;
  1930. t = tok;
  1931. while (*t && *t != '=' && p < end - 4)
  1932. *p++ = *t++;
  1933. if (*t == '=')
  1934. *p++ = *t++;
  1935. i = urlencode(t, i- (t - tok), p, end - p);
  1936. if (i > 0) {
  1937. p += i;
  1938. *p++ = '&';
  1939. }
  1940. m++;
  1941. }
  1942. if (m)
  1943. p--;
  1944. *p++ = '\0';
  1945. env_array[n++] = p;
  1946. p += lws_snprintf(p, end - p, "PATH_INFO=%s",
  1947. lws_hdr_simple_ptr(wsi, uritok) +
  1948. script_uri_path_len);
  1949. p++;
  1950. }
  1951. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER)) {
  1952. env_array[n++] = p;
  1953. p += lws_snprintf(p, end - p, "HTTP_REFERER=%s",
  1954. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_REFERER));
  1955. p++;
  1956. }
  1957. if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
  1958. env_array[n++] = p;
  1959. p += lws_snprintf(p, end - p, "HTTP_HOST=%s",
  1960. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
  1961. p++;
  1962. }
  1963. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
  1964. env_array[n++] = p;
  1965. p += lws_snprintf(p, end - p, "HTTP_COOKIE=%s",
  1966. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COOKIE));
  1967. p++;
  1968. }
  1969. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT)) {
  1970. env_array[n++] = p;
  1971. p += lws_snprintf(p, end - p, "USER_AGENT=%s",
  1972. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_USER_AGENT));
  1973. p++;
  1974. }
  1975. if (uritok == WSI_TOKEN_POST_URI) {
  1976. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
  1977. env_array[n++] = p;
  1978. p += lws_snprintf(p, end - p, "CONTENT_TYPE=%s",
  1979. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE));
  1980. p++;
  1981. }
  1982. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
  1983. env_array[n++] = p;
  1984. p += lws_snprintf(p, end - p, "CONTENT_LENGTH=%s",
  1985. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH));
  1986. p++;
  1987. }
  1988. }
  1989. env_array[n++] = p;
  1990. p += lws_snprintf(p, end - p, "SCRIPT_PATH=%s", exec_array[0]) + 1;
  1991. while (mp_cgienv) {
  1992. env_array[n++] = p;
  1993. p += lws_snprintf(p, end - p, "%s=%s", mp_cgienv->name,
  1994. mp_cgienv->value);
  1995. lwsl_debug(" Applying mount-specific cgi env '%s'\n",
  1996. env_array[n - 1]);
  1997. p++;
  1998. mp_cgienv = mp_cgienv->next;
  1999. }
  2000. env_array[n++] = "SERVER_SOFTWARE=libwebsockets";
  2001. env_array[n++] = "PATH=/bin:/usr/bin:/usr/local/bin:/var/www/cgi-bin";
  2002. env_array[n] = NULL;
  2003. #if 0
  2004. for (m = 0; m < n; m++)
  2005. lwsl_err(" %s\n", env_array[m]);
  2006. #endif
  2007. /*
  2008. * Actually having made the env, as a cgi we don't need the ah
  2009. * any more
  2010. */
  2011. if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen)
  2012. lws_header_table_detach(wsi, 0);
  2013. /* we are ready with the redirection pipes... run the thing */
  2014. #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
  2015. cgi->pid = fork();
  2016. #else
  2017. cgi->pid = vfork();
  2018. #endif
  2019. if (cgi->pid < 0) {
  2020. lwsl_err("fork failed, errno %d", errno);
  2021. goto bail3;
  2022. }
  2023. #if defined(__linux__)
  2024. prctl(PR_SET_PDEATHSIG, SIGTERM);
  2025. #endif
  2026. setpgrp(); /* stops on-daemonized main processess getting SIGINT from TTY */
  2027. if (cgi->pid) {
  2028. /* we are the parent process */
  2029. wsi->context->count_cgi_spawned++;
  2030. lwsl_debug("%s: cgi %p spawned PID %d\n", __func__, cgi, cgi->pid);
  2031. return 0;
  2032. }
  2033. /* somewhere we can at least read things and enter it */
  2034. if (chdir("/tmp"))
  2035. lwsl_notice("%s: Failed to chdir\n", __func__);
  2036. /* We are the forked process, redirect and kill inherited things.
  2037. *
  2038. * Because of vfork(), we cannot do anything that changes pages in
  2039. * the parent environment. Stuff that changes kernel state for the
  2040. * process is OK. Stuff that happens after the execvpe() is OK.
  2041. */
  2042. for (n = 0; n < 3; n++)
  2043. if (dup2(cgi->pipe_fds[n][!(n == 0)], n) < 0) {
  2044. lwsl_err("%s: stdin dup2 failed\n", __func__);
  2045. goto bail3;
  2046. }
  2047. #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
  2048. for (m = 0; m < n; m++) {
  2049. p = strchr(env_array[m], '=');
  2050. *p++ = '\0';
  2051. setenv(env_array[m], p, 1);
  2052. }
  2053. execvp(exec_array[0], (char * const *)&exec_array[0]);
  2054. #else
  2055. execvpe(exec_array[0], (char * const *)&exec_array[0], &env_array[0]);
  2056. #endif
  2057. exit(1);
  2058. bail3:
  2059. /* drop us from the pt cgi list */
  2060. pt->cgi_list = cgi->cgi_list;
  2061. while (--n >= 0)
  2062. remove_wsi_socket_from_fds(wsi->cgi->stdwsi[n]);
  2063. bail2:
  2064. for (n = 0; n < 3; n++)
  2065. if (wsi->cgi->stdwsi[n])
  2066. lws_free_wsi(cgi->stdwsi[n]);
  2067. bail1:
  2068. for (n = 0; n < 3; n++) {
  2069. if (cgi->pipe_fds[n][0])
  2070. close(cgi->pipe_fds[n][0]);
  2071. if (cgi->pipe_fds[n][1])
  2072. close(cgi->pipe_fds[n][1]);
  2073. }
  2074. lws_free_set_NULL(wsi->cgi);
  2075. lwsl_err("%s: failed\n", __func__);
  2076. return -1;
  2077. }
  2078. LWS_VISIBLE LWS_EXTERN int
  2079. lws_cgi_write_split_stdout_headers(struct lws *wsi)
  2080. {
  2081. int n, m, match = 0, lp = 0;
  2082. static const char * const content_length = "content-length: ";
  2083. char buf[LWS_PRE + 1024], *start = &buf[LWS_PRE], *p = start,
  2084. *end = &buf[sizeof(buf) - 1 - LWS_PRE], c, l[12];
  2085. if (!wsi->cgi)
  2086. return -1;
  2087. while (wsi->hdr_state != LHCS_PAYLOAD) {
  2088. /* we have to separate header / finalize and
  2089. * payload chunks, since they need to be
  2090. * handled separately
  2091. */
  2092. n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]), &c, 1);
  2093. if (n < 0) {
  2094. if (errno != EAGAIN) {
  2095. lwsl_debug("%s: read says %d\n", __func__, n);
  2096. return -1;
  2097. }
  2098. else
  2099. n = 0;
  2100. }
  2101. if (n) {
  2102. lwsl_debug("-- 0x%02X %c\n", (unsigned char)c, c);
  2103. switch (wsi->hdr_state) {
  2104. case LCHS_HEADER:
  2105. if (!content_length[match] &&
  2106. (c >= '0' && c <= '9') &&
  2107. lp < sizeof(l) - 1) {
  2108. l[lp++] = c;
  2109. l[lp] = '\0';
  2110. wsi->cgi->content_length = atol(l);
  2111. }
  2112. if (tolower(c) == content_length[match])
  2113. match++;
  2114. else
  2115. match = 0;
  2116. /* some cgi only send us \x0a for EOL */
  2117. if (c == '\x0a') {
  2118. wsi->hdr_state = LCHS_SINGLE_0A;
  2119. *p++ = '\x0d';
  2120. }
  2121. *p++ = c;
  2122. if (c == '\x0d') {
  2123. wsi->hdr_state = LCHS_LF1;
  2124. break;
  2125. }
  2126. break;
  2127. case LCHS_LF1:
  2128. *p++ = c;
  2129. if (c == '\x0a') {
  2130. wsi->hdr_state = LCHS_CR2;
  2131. break;
  2132. }
  2133. /* we got \r[^\n]... it's unreasonable */
  2134. lwsl_debug("%s: funny CRLF 0x%02X\n", __func__, (unsigned char)c);
  2135. return -1;
  2136. case LCHS_CR2:
  2137. if (c == '\x0d') {
  2138. /* drop the \x0d */
  2139. wsi->hdr_state = LCHS_LF2;
  2140. break;
  2141. }
  2142. wsi->hdr_state = LCHS_HEADER;
  2143. match = 0;
  2144. *p++ = c;
  2145. break;
  2146. case LCHS_LF2:
  2147. case LCHS_SINGLE_0A:
  2148. m = wsi->hdr_state;
  2149. if (c == '\x0a') {
  2150. lwsl_debug("Content-Length: %ld\n", wsi->cgi->content_length);
  2151. wsi->hdr_state = LHCS_PAYLOAD;
  2152. /* drop the \0xa ... finalize will add it if needed */
  2153. if (lws_finalize_http_header(wsi,
  2154. (unsigned char **)&p,
  2155. (unsigned char *)end))
  2156. return -1;
  2157. break;
  2158. }
  2159. if (m == LCHS_LF2)
  2160. /* we got \r\n\r[^\n]... it's unreasonable */
  2161. return -1;
  2162. /* we got \x0anext header, it's reasonable */
  2163. *p++ = c;
  2164. wsi->hdr_state = LCHS_HEADER;
  2165. break;
  2166. case LHCS_PAYLOAD:
  2167. break;
  2168. }
  2169. }
  2170. /* ran out of input, ended the headers, or filled up the headers buf */
  2171. if (!n || wsi->hdr_state == LHCS_PAYLOAD || (p + 4) == end) {
  2172. m = lws_write(wsi, (unsigned char *)start,
  2173. p - start, LWS_WRITE_HTTP_HEADERS);
  2174. if (m < 0) {
  2175. lwsl_debug("%s: write says %d\n", __func__, m);
  2176. return -1;
  2177. }
  2178. /* writeability becomes uncertain now we wrote
  2179. * something, we must return to the event loop
  2180. */
  2181. return 0;
  2182. }
  2183. }
  2184. n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]),
  2185. start, sizeof(buf) - LWS_PRE);
  2186. if (n < 0 && errno != EAGAIN) {
  2187. lwsl_debug("%s: stdout read says %d\n", __func__, n);
  2188. return -1;
  2189. }
  2190. if (n > 0) {
  2191. m = lws_write(wsi, (unsigned char *)start, n, LWS_WRITE_HTTP);
  2192. //lwsl_notice("write %d\n", m);
  2193. if (m < 0) {
  2194. lwsl_debug("%s: stdout write says %d\n", __func__, m);
  2195. return -1;
  2196. }
  2197. wsi->cgi->content_length_seen += m;
  2198. }
  2199. return 0;
  2200. }
  2201. LWS_VISIBLE LWS_EXTERN int
  2202. lws_cgi_kill(struct lws *wsi)
  2203. {
  2204. struct lws_cgi_args args;
  2205. int status, n;
  2206. lwsl_debug("%s: %p\n", __func__, wsi);
  2207. if (!wsi->cgi)
  2208. return 0;
  2209. if (wsi->cgi->pid > 0) {
  2210. n = waitpid(wsi->cgi->pid, &status, WNOHANG);
  2211. if (n > 0) {
  2212. lwsl_debug("%s: PID %d reaped\n", __func__,
  2213. wsi->cgi->pid);
  2214. goto handled;
  2215. }
  2216. /* kill the process group */
  2217. n = kill(-wsi->cgi->pid, SIGTERM);
  2218. lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
  2219. wsi->cgi->pid, n, errno);
  2220. if (n < 0) {
  2221. /*
  2222. * hum seen errno=3 when process is listed in ps,
  2223. * it seems we don't always retain process grouping
  2224. *
  2225. * Direct these fallback attempt to the exact child
  2226. */
  2227. n = kill(wsi->cgi->pid, SIGTERM);
  2228. if (n < 0) {
  2229. n = kill(wsi->cgi->pid, SIGPIPE);
  2230. if (n < 0) {
  2231. n = kill(wsi->cgi->pid, SIGKILL);
  2232. if (n < 0)
  2233. lwsl_err("%s: SIGKILL PID %d failed errno %d (maybe zombie)\n",
  2234. __func__, wsi->cgi->pid, errno);
  2235. }
  2236. }
  2237. }
  2238. /* He could be unkillable because he's a zombie */
  2239. n = 1;
  2240. while (n > 0) {
  2241. n = waitpid(-wsi->cgi->pid, &status, WNOHANG);
  2242. if (n > 0)
  2243. lwsl_debug("%s: reaped PID %d\n", __func__, n);
  2244. if (n <= 0) {
  2245. n = waitpid(wsi->cgi->pid, &status, WNOHANG);
  2246. if (n > 0)
  2247. lwsl_debug("%s: reaped PID %d\n", __func__, n);
  2248. }
  2249. }
  2250. }
  2251. handled:
  2252. args.stdwsi = &wsi->cgi->stdwsi[0];
  2253. if (wsi->cgi->pid != -1 && user_callback_handle_rxflow(
  2254. wsi->protocol->callback,
  2255. wsi, LWS_CALLBACK_CGI_TERMINATED,
  2256. wsi->user_space,
  2257. (void *)&args, 0)) {
  2258. wsi->cgi->pid = -1;
  2259. if (!wsi->cgi->being_closed)
  2260. lws_close_free_wsi(wsi, 0);
  2261. }
  2262. return 0;
  2263. }
  2264. LWS_EXTERN int
  2265. lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
  2266. {
  2267. struct lws_cgi **pcgi, *cgi = NULL;
  2268. int status, n = 1;
  2269. while (n > 0) {
  2270. /* find finished guys but don't reap yet */
  2271. n = waitpid(-1, &status, WNOHANG);
  2272. if (n <= 0)
  2273. continue;
  2274. lwsl_debug("%s: observed PID %d terminated\n", __func__, n);
  2275. pcgi = &pt->cgi_list;
  2276. /* check all the subprocesses on the cgi list */
  2277. while (*pcgi) {
  2278. /* get the next one first as list may change */
  2279. cgi = *pcgi;
  2280. pcgi = &(*pcgi)->cgi_list;
  2281. if (cgi->pid <= 0)
  2282. continue;
  2283. /* wait for stdout to be drained */
  2284. if (cgi->content_length > cgi->content_length_seen)
  2285. continue;
  2286. if (cgi->content_length) {
  2287. lwsl_debug("%s: wsi %p: expected content length seen: %ld\n",
  2288. __func__, cgi->wsi, cgi->content_length_seen);
  2289. }
  2290. /* reap it */
  2291. waitpid(n, &status, WNOHANG);
  2292. /*
  2293. * he's already terminated so no need for kill()
  2294. * but we should do the terminated cgi callback
  2295. * and close him if he's not already closing
  2296. */
  2297. if (n == cgi->pid) {
  2298. lwsl_debug("%s: found PID %d on cgi list\n",
  2299. __func__, n);
  2300. if (!cgi->content_length) {
  2301. /*
  2302. * well, if he sends chunked... give him 5s after the
  2303. * cgi terminated to send buffered
  2304. */
  2305. cgi->chunked_grace++;
  2306. continue;
  2307. }
  2308. /* defeat kill() */
  2309. cgi->pid = 0;
  2310. lws_cgi_kill(cgi->wsi);
  2311. break;
  2312. }
  2313. cgi = NULL;
  2314. }
  2315. /* if not found on the cgi list, as he's one of ours, reap */
  2316. if (!cgi) {
  2317. lwsl_debug("%s: reading PID %d although no cgi match\n",
  2318. __func__, n);
  2319. waitpid(n, &status, WNOHANG);
  2320. }
  2321. }
  2322. /* disable this to confirm timeout cgi cleanup flow */
  2323. #if 1
  2324. pcgi = &pt->cgi_list;
  2325. /* check all the subprocesses on the cgi list */
  2326. while (*pcgi) {
  2327. /* get the next one first as list may change */
  2328. cgi = *pcgi;
  2329. pcgi = &(*pcgi)->cgi_list;
  2330. if (cgi->pid <= 0)
  2331. continue;
  2332. /* we deferred killing him after reaping his PID */
  2333. if (cgi->chunked_grace) {
  2334. cgi->chunked_grace++;
  2335. if (cgi->chunked_grace < 5)
  2336. continue;
  2337. goto finish_him;
  2338. }
  2339. /* wait for stdout to be drained */
  2340. if (cgi->content_length > cgi->content_length_seen)
  2341. continue;
  2342. if (cgi->content_length)
  2343. lwsl_debug("%s: wsi %p: expected content length seen: %ld\n",
  2344. __func__, cgi->wsi, cgi->content_length_seen);
  2345. /* reap it */
  2346. if (waitpid(cgi->pid, &status, WNOHANG) > 0) {
  2347. if (!cgi->content_length) {
  2348. /*
  2349. * well, if he sends chunked... give him 5s after the
  2350. * cgi terminated to send buffered
  2351. */
  2352. cgi->chunked_grace++;
  2353. continue;
  2354. }
  2355. finish_him:
  2356. lwsl_debug("%s: found PID %d on cgi list\n",
  2357. __func__, cgi->pid);
  2358. /* defeat kill() */
  2359. cgi->pid = 0;
  2360. lws_cgi_kill(cgi->wsi);
  2361. break;
  2362. }
  2363. }
  2364. #endif
  2365. /* general anti zombie defence */
  2366. // n = waitpid(-1, &status, WNOHANG);
  2367. //if (n > 0)
  2368. // lwsl_notice("%s: anti-zombie wait says %d\n", __func__, n);
  2369. return 0;
  2370. }
  2371. #endif
  2372. #ifdef LWS_NO_EXTENSIONS
  2373. LWS_EXTERN int
  2374. lws_set_extension_option(struct lws *wsi, const char *ext_name,
  2375. const char *opt_name, const char *opt_val)
  2376. {
  2377. return -1;
  2378. }
  2379. #endif
  2380. #ifdef LWS_WITH_ACCESS_LOG
  2381. int
  2382. lws_access_log(struct lws *wsi)
  2383. {
  2384. char *p = wsi->access_log.user_agent, ass[512];
  2385. int l;
  2386. if (!wsi->access_log_pending)
  2387. return 0;
  2388. if (!wsi->access_log.header_log)
  2389. return 0;
  2390. if (!p)
  2391. p = "";
  2392. l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu %s\n",
  2393. wsi->access_log.header_log,
  2394. wsi->access_log.response, wsi->access_log.sent, p);
  2395. if (wsi->vhost->log_fd != (int)LWS_INVALID_FILE) {
  2396. if (write(wsi->vhost->log_fd, ass, l) != l)
  2397. lwsl_err("Failed to write log\n");
  2398. } else
  2399. lwsl_err("%s", ass);
  2400. if (wsi->access_log.header_log) {
  2401. lws_free(wsi->access_log.header_log);
  2402. wsi->access_log.header_log = NULL;
  2403. }
  2404. if (wsi->access_log.user_agent) {
  2405. lws_free(wsi->access_log.user_agent);
  2406. wsi->access_log.user_agent = NULL;
  2407. }
  2408. wsi->access_log_pending = 0;
  2409. return 0;
  2410. }
  2411. #endif
  2412. void
  2413. lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs)
  2414. {
  2415. const struct lws_vhost *vh = ctx->vhost_list;
  2416. while (vh) {
  2417. cs->rx += vh->conn_stats.rx;
  2418. cs->tx += vh->conn_stats.tx;
  2419. cs->conn += vh->conn_stats.conn;
  2420. cs->trans += vh->conn_stats.trans;
  2421. cs->ws_upg += vh->conn_stats.ws_upg;
  2422. cs->http2_upg += vh->conn_stats.http2_upg;
  2423. cs->rejected += vh->conn_stats.rejected;
  2424. vh = vh->vhost_next;
  2425. }
  2426. }
  2427. #ifdef LWS_WITH_SERVER_STATUS
  2428. LWS_EXTERN int
  2429. lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
  2430. {
  2431. static const char * const prots[] = {
  2432. "http://",
  2433. "https://",
  2434. "file://",
  2435. "cgi://",
  2436. ">http://",
  2437. ">https://",
  2438. "callback://"
  2439. };
  2440. char *orig = buf, *end = buf + len - 1, first = 1;
  2441. int n = 0;
  2442. if (len < 100)
  2443. return 0;
  2444. buf += lws_snprintf(buf, end - buf,
  2445. "{\n \"name\":\"%s\",\n"
  2446. " \"port\":\"%d\",\n"
  2447. " \"use_ssl\":\"%d\",\n"
  2448. " \"sts\":\"%d\",\n"
  2449. " \"rx\":\"%llu\",\n"
  2450. " \"tx\":\"%llu\",\n"
  2451. " \"conn\":\"%lu\",\n"
  2452. " \"trans\":\"%lu\",\n"
  2453. " \"ws_upg\":\"%lu\",\n"
  2454. " \"rejected\":\"%lu\",\n"
  2455. " \"http2_upg\":\"%lu\""
  2456. ,
  2457. vh->name, vh->listen_port,
  2458. #ifdef LWS_OPENSSL_SUPPORT
  2459. vh->use_ssl,
  2460. #else
  2461. 0,
  2462. #endif
  2463. !!(vh->options & LWS_SERVER_OPTION_STS),
  2464. vh->conn_stats.rx, vh->conn_stats.tx,
  2465. vh->conn_stats.conn, vh->conn_stats.trans,
  2466. vh->conn_stats.ws_upg,
  2467. vh->conn_stats.rejected,
  2468. vh->conn_stats.http2_upg
  2469. );
  2470. if (vh->mount_list) {
  2471. const struct lws_http_mount *m = vh->mount_list;
  2472. buf += lws_snprintf(buf, end - buf, ",\n \"mounts\":[");
  2473. while (m) {
  2474. if (!first)
  2475. buf += lws_snprintf(buf, end - buf, ",");
  2476. buf += lws_snprintf(buf, end - buf,
  2477. "\n {\n \"mountpoint\":\"%s\",\n"
  2478. " \"origin\":\"%s%s\",\n"
  2479. " \"cache_max_age\":\"%d\",\n"
  2480. " \"cache_reuse\":\"%d\",\n"
  2481. " \"cache_revalidate\":\"%d\",\n"
  2482. " \"cache_intermediaries\":\"%d\"\n"
  2483. ,
  2484. m->mountpoint,
  2485. prots[m->origin_protocol],
  2486. m->origin,
  2487. m->cache_max_age,
  2488. m->cache_reusable,
  2489. m->cache_revalidate,
  2490. m->cache_intermediaries);
  2491. if (m->def)
  2492. buf += lws_snprintf(buf, end - buf,
  2493. ",\n \"default\":\"%s\"",
  2494. m->def);
  2495. buf += lws_snprintf(buf, end - buf, "\n }");
  2496. first = 0;
  2497. m = m->mount_next;
  2498. }
  2499. buf += lws_snprintf(buf, end - buf, "\n ]");
  2500. }
  2501. if (vh->protocols) {
  2502. n = 0;
  2503. first = 1;
  2504. buf += lws_snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
  2505. while (n < vh->count_protocols) {
  2506. if (!first)
  2507. buf += lws_snprintf(buf, end - buf, ",");
  2508. buf += lws_snprintf(buf, end - buf,
  2509. "\n {\n \"%s\":{\n"
  2510. " \"status\":\"ok\"\n }\n }"
  2511. ,
  2512. vh->protocols[n].name);
  2513. first = 0;
  2514. n++;
  2515. }
  2516. buf += lws_snprintf(buf, end - buf, "\n ]");
  2517. }
  2518. buf += lws_snprintf(buf, end - buf, "\n}");
  2519. return buf - orig;
  2520. }
  2521. LWS_EXTERN LWS_VISIBLE int
  2522. lws_json_dump_context(const struct lws_context *context, char *buf, int len,
  2523. int hide_vhosts)
  2524. {
  2525. char *orig = buf, *end = buf + len - 1, first = 1;
  2526. const struct lws_vhost *vh = context->vhost_list;
  2527. const struct lws_context_per_thread *pt;
  2528. time_t t = time(NULL);
  2529. int n, listening = 0, cgi_count = 0;
  2530. struct lws_conn_stats cs;
  2531. double d = 0;
  2532. #ifdef LWS_WITH_CGI
  2533. struct lws_cgi * const *pcgi;
  2534. #endif
  2535. #ifdef LWS_USE_LIBUV
  2536. uv_uptime(&d);
  2537. #endif
  2538. buf += lws_snprintf(buf, end - buf, "{ "
  2539. "\"version\":\"%s\",\n"
  2540. "\"uptime\":\"%ld\",\n",
  2541. lws_get_library_version(),
  2542. (long)d);
  2543. #ifdef LWS_HAVE_GETLOADAVG
  2544. {
  2545. double d[3];
  2546. int m;
  2547. m = getloadavg(d, 3);
  2548. for (n = 0; n < m; n++) {
  2549. buf += lws_snprintf(buf, end - buf,
  2550. "\"l%d\":\"%.2f\",\n",
  2551. n + 1, d[n]);
  2552. }
  2553. }
  2554. #endif
  2555. buf += lws_snprintf(buf, end - buf, "\"contexts\":[\n");
  2556. buf += lws_snprintf(buf, end - buf, "{ "
  2557. "\"context_uptime\":\"%ld\",\n"
  2558. "\"cgi_spawned\":\"%d\",\n"
  2559. "\"pt_fd_max\":\"%d\",\n"
  2560. "\"ah_pool_max\":\"%d\",\n"
  2561. "\"deprecated\":\"%d\",\n"
  2562. "\"wsi_alive\":\"%d\",\n",
  2563. (unsigned long)(t - context->time_up),
  2564. context->count_cgi_spawned,
  2565. context->fd_limit_per_thread,
  2566. context->max_http_header_pool,
  2567. context->deprecated,
  2568. context->count_wsi_allocated);
  2569. buf += lws_snprintf(buf, end - buf, "\"pt\":[\n ");
  2570. for (n = 0; n < context->count_threads; n++) {
  2571. pt = &context->pt[n];
  2572. if (n)
  2573. buf += lws_snprintf(buf, end - buf, ",");
  2574. buf += lws_snprintf(buf, end - buf,
  2575. "\n {\n"
  2576. " \"fds_count\":\"%d\",\n"
  2577. " \"ah_pool_inuse\":\"%d\",\n"
  2578. " \"ah_wait_list\":\"%d\"\n"
  2579. " }",
  2580. pt->fds_count,
  2581. pt->ah_count_in_use,
  2582. pt->ah_wait_list_length);
  2583. }
  2584. buf += lws_snprintf(buf, end - buf, "]");
  2585. buf += lws_snprintf(buf, end - buf, ", \"vhosts\":[\n ");
  2586. first = 1;
  2587. vh = context->vhost_list;
  2588. listening = 0;
  2589. cs = context->conn_stats;
  2590. lws_sum_stats(context, &cs);
  2591. while (vh) {
  2592. if (!hide_vhosts) {
  2593. if (!first)
  2594. if(buf != end)
  2595. *buf++ = ',';
  2596. buf += lws_json_dump_vhost(vh, buf, end - buf);
  2597. first = 0;
  2598. }
  2599. if (vh->lserv_wsi)
  2600. listening++;
  2601. vh = vh->vhost_next;
  2602. }
  2603. buf += lws_snprintf(buf, end - buf,
  2604. "],\n\"listen_wsi\":\"%d\",\n"
  2605. " \"rx\":\"%llu\",\n"
  2606. " \"tx\":\"%llu\",\n"
  2607. " \"conn\":\"%lu\",\n"
  2608. " \"trans\":\"%lu\",\n"
  2609. " \"ws_upg\":\"%lu\",\n"
  2610. " \"rejected\":\"%lu\",\n"
  2611. " \"http2_upg\":\"%lu\"",
  2612. listening,
  2613. cs.rx, cs.tx, cs.conn, cs.trans,
  2614. cs.ws_upg, cs.rejected, cs.http2_upg);
  2615. #ifdef LWS_WITH_CGI
  2616. for (n = 0; n < context->count_threads; n++) {
  2617. pt = &context->pt[n];
  2618. pcgi = &pt->cgi_list;
  2619. while (*pcgi) {
  2620. pcgi = &(*pcgi)->cgi_list;
  2621. cgi_count++;
  2622. }
  2623. }
  2624. #endif
  2625. buf += lws_snprintf(buf, end - buf, ",\n \"cgi_alive\":\"%d\"\n ",
  2626. cgi_count);
  2627. buf += lws_snprintf(buf, end - buf, "}");
  2628. buf += lws_snprintf(buf, end - buf, "]}\n ");
  2629. return buf - orig;
  2630. }
  2631. #endif