libwebsockets.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2017 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 "core/private.h"
  22. #ifdef LWS_HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #ifdef LWS_WITH_IPV6
  26. #if defined(WIN32) || defined(_WIN32)
  27. #include <wincrypt.h>
  28. #include <iphlpapi.h>
  29. #else
  30. #include <net/if.h>
  31. #endif
  32. #endif
  33. int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
  34. static void (*lwsl_emit)(int level, const char *line)
  35. #ifndef LWS_PLAT_OPTEE
  36. = lwsl_emit_stderr
  37. #endif
  38. ;
  39. #ifndef LWS_PLAT_OPTEE
  40. static const char * const log_level_names[] = {
  41. "ERR",
  42. "WARN",
  43. "NOTICE",
  44. "INFO",
  45. "DEBUG",
  46. "PARSER",
  47. "HEADER",
  48. "EXTENSION",
  49. "CLIENT",
  50. "LATENCY",
  51. "USER",
  52. "?",
  53. "?"
  54. };
  55. #endif
  56. int lws_open(const char *__file, int __oflag, ...)
  57. {
  58. va_list ap;
  59. int n;
  60. va_start(ap, __oflag);
  61. if (((__oflag & O_CREAT) == O_CREAT)
  62. #if defined(O_TMPFILE)
  63. || ((__oflag & O_TMPFILE) == O_TMPFILE)
  64. #endif
  65. )
  66. /* last arg is really a mode_t. But windows... */
  67. n = open(__file, __oflag, va_arg(ap, uint32_t));
  68. else
  69. n = open(__file, __oflag);
  70. va_end(ap);
  71. lws_plat_apply_FD_CLOEXEC(n);
  72. return n;
  73. }
  74. #if defined (_DEBUG)
  75. void lwsi_set_role(struct lws *wsi, lws_wsi_state_t role)
  76. {
  77. wsi->wsistate = (wsi->wsistate & (~LWSI_ROLE_MASK)) | role;
  78. lwsl_debug("lwsi_set_role(%p, 0x%x)\n", wsi, wsi->wsistate);
  79. }
  80. void lwsi_set_state(struct lws *wsi, lws_wsi_state_t lrs)
  81. {
  82. wsi->wsistate = (wsi->wsistate & (~LRS_MASK)) | lrs;
  83. lwsl_debug("lwsi_set_state(%p, 0x%x)\n", wsi, wsi->wsistate);
  84. }
  85. #endif
  86. signed char char_to_hex(const char c)
  87. {
  88. if (c >= '0' && c <= '9')
  89. return c - '0';
  90. if (c >= 'a' && c <= 'f')
  91. return c - 'a' + 10;
  92. if (c >= 'A' && c <= 'F')
  93. return c - 'A' + 10;
  94. return -1;
  95. }
  96. void
  97. __lws_free_wsi(struct lws *wsi)
  98. {
  99. if (!wsi)
  100. return;
  101. /*
  102. * Protocol user data may be allocated either internally by lws
  103. * or by specified the user. We should only free what we allocated.
  104. */
  105. if (wsi->protocol && wsi->protocol->per_session_data_size &&
  106. wsi->user_space && !wsi->user_space_externally_allocated)
  107. lws_free(wsi->user_space);
  108. lws_buflist_destroy_all_segments(&wsi->buflist);
  109. lws_free_set_NULL(wsi->trunc_alloc);
  110. lws_free_set_NULL(wsi->udp);
  111. if (wsi->vhost && wsi->vhost->lserv_wsi == wsi)
  112. wsi->vhost->lserv_wsi = NULL;
  113. // lws_peer_dump_from_wsi(wsi);
  114. if (wsi->role_ops->destroy_role)
  115. wsi->role_ops->destroy_role(wsi);
  116. #if defined(LWS_WITH_PEER_LIMITS)
  117. lws_peer_track_wsi_close(wsi->context, wsi->peer);
  118. wsi->peer = NULL;
  119. #endif
  120. /* since we will destroy the wsi, make absolutely sure now */
  121. #if defined(LWS_WITH_OPENSSL)
  122. __lws_ssl_remove_wsi_from_buffered_list(wsi);
  123. #endif
  124. __lws_remove_from_timeout_list(wsi);
  125. if (wsi->context->event_loop_ops->destroy_wsi)
  126. wsi->context->event_loop_ops->destroy_wsi(wsi);
  127. wsi->context->count_wsi_allocated--;
  128. lwsl_debug("%s: %p, remaining wsi %d\n", __func__, wsi,
  129. wsi->context->count_wsi_allocated);
  130. lws_free(wsi);
  131. }
  132. void
  133. lws_dll_add_front(struct lws_dll *d, struct lws_dll *phead)
  134. {
  135. if (d->prev)
  136. return;
  137. /* our next guy is current first guy */
  138. d->next = phead->next;
  139. /* if there is a next guy, set his prev ptr to our next ptr */
  140. if (d->next)
  141. d->next->prev = d;
  142. /* our prev ptr is first ptr */
  143. d->prev = phead;
  144. /* set the first guy to be us */
  145. phead->next = d;
  146. }
  147. /* situation is:
  148. *
  149. * HEAD: struct lws_dll * = &entry1
  150. *
  151. * Entry 1: struct lws_dll .pprev = &HEAD , .next = Entry 2
  152. * Entry 2: struct lws_dll .pprev = &entry1 , .next = &entry2
  153. * Entry 3: struct lws_dll .pprev = &entry2 , .next = NULL
  154. *
  155. * Delete Entry1:
  156. *
  157. * - HEAD = &entry2
  158. * - Entry2: .pprev = &HEAD, .next = &entry3
  159. * - Entry3: .pprev = &entry2, .next = NULL
  160. *
  161. * Delete Entry2:
  162. *
  163. * - HEAD = &entry1
  164. * - Entry1: .pprev = &HEAD, .next = &entry3
  165. * - Entry3: .pprev = &entry1, .next = NULL
  166. *
  167. * Delete Entry3:
  168. *
  169. * - HEAD = &entry1
  170. * - Entry1: .pprev = &HEAD, .next = &entry2
  171. * - Entry2: .pprev = &entry1, .next = NULL
  172. *
  173. */
  174. void
  175. lws_dll_remove(struct lws_dll *d)
  176. {
  177. if (!d->prev) /* ie, not part of the list */
  178. return;
  179. /*
  180. * remove us
  181. *
  182. * USp <-> us <-> USn --> USp <-> USn
  183. */
  184. /* if we have a next guy, set his prev to our prev */
  185. if (d->next)
  186. d->next->prev = d->prev;
  187. /* set our prev guy to our next guy instead of us */
  188. if (d->prev)
  189. d->prev->next = d->next;
  190. /* we're out of the list, we should not point anywhere any more */
  191. d->prev = NULL;
  192. d->next = NULL;
  193. }
  194. void
  195. __lws_remove_from_timeout_list(struct lws *wsi)
  196. {
  197. lws_dll_lws_remove(&wsi->dll_timeout);
  198. }
  199. void
  200. lws_remove_from_timeout_list(struct lws *wsi)
  201. {
  202. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  203. lws_pt_lock(pt, __func__);
  204. __lws_remove_from_timeout_list(wsi);
  205. lws_pt_unlock(pt);
  206. }
  207. void
  208. lws_dll_dump(struct lws_dll_lws *head, const char *title)
  209. {
  210. int n = 0;
  211. (void)n;
  212. lwsl_notice("%s: %s (head.next %p)\n", __func__, title, head->next);
  213. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1, head->next) {
  214. struct lws *wsi = lws_container_of(d, struct lws, dll_hrtimer);
  215. (void)wsi;
  216. lwsl_notice(" %d: wsi %p: %llu\n", n++, wsi,
  217. (unsigned long long)wsi->pending_timer);
  218. } lws_end_foreach_dll_safe(d, d1);
  219. }
  220. void
  221. __lws_set_timer_usecs(struct lws *wsi, lws_usec_t usecs)
  222. {
  223. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  224. struct lws_dll_lws *dd = &pt->dll_head_hrtimer;
  225. struct timeval now;
  226. struct lws *wsi1;
  227. int bef = 0;
  228. lws_dll_lws_remove(&wsi->dll_hrtimer);
  229. if (usecs == LWS_SET_TIMER_USEC_CANCEL)
  230. return;
  231. gettimeofday(&now, NULL);
  232. wsi->pending_timer = ((now.tv_sec * 1000000ll) + now.tv_usec) + usecs;
  233. /*
  234. * we sort the hrtimer list with the earliest timeout first
  235. */
  236. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
  237. pt->dll_head_hrtimer.next) {
  238. dd = d;
  239. wsi1 = lws_container_of(d, struct lws, dll_hrtimer);
  240. if (wsi1->pending_timer >= wsi->pending_timer) {
  241. /* d, dprev's next, is >= our time */
  242. bef = 1;
  243. break;
  244. }
  245. } lws_end_foreach_dll_safe(d, d1);
  246. if (bef) {
  247. /*
  248. * we go before dd
  249. * DDp <-> DD <-> DDn --> DDp <-> us <-> DD <-> DDn
  250. */
  251. /* we point forward to dd */
  252. wsi->dll_hrtimer.next = dd;
  253. /* we point back to what dd used to point back to */
  254. wsi->dll_hrtimer.prev = dd->prev;
  255. /* DDp points forward to us now */
  256. dd->prev->next = &wsi->dll_hrtimer;
  257. /* DD points back to us now */
  258. dd->prev = &wsi->dll_hrtimer;
  259. } else {
  260. /*
  261. * we go after dd
  262. * DDp <-> DD <-> DDn --> DDp <-> DD <-> us <-> DDn
  263. */
  264. /* we point forward to what dd used to point forward to */
  265. wsi->dll_hrtimer.next = dd->next;
  266. /* we point back to dd */
  267. wsi->dll_hrtimer.prev = dd;
  268. /* DDn points back to us */
  269. if (dd->next)
  270. dd->next->prev = &wsi->dll_hrtimer;
  271. /* DD points forward to us */
  272. dd->next = &wsi->dll_hrtimer;
  273. }
  274. // lws_dll_dump(&pt->dll_head_hrtimer, "after set_timer_usec");
  275. }
  276. LWS_VISIBLE void
  277. lws_set_timer_usecs(struct lws *wsi, lws_usec_t usecs)
  278. {
  279. __lws_set_timer_usecs(wsi, usecs);
  280. }
  281. lws_usec_t
  282. __lws_hrtimer_service(struct lws_context_per_thread *pt)
  283. {
  284. struct timeval now;
  285. struct lws *wsi;
  286. lws_usec_t t;
  287. gettimeofday(&now, NULL);
  288. t = (now.tv_sec * 1000000ll) + now.tv_usec;
  289. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
  290. pt->dll_head_hrtimer.next) {
  291. wsi = lws_container_of(d, struct lws, dll_hrtimer);
  292. /*
  293. * if we met one in the future, we are done, because the list
  294. * is sorted by time in the future.
  295. */
  296. if (wsi->pending_timer > t)
  297. break;
  298. lws_set_timer_usecs(wsi, LWS_SET_TIMER_USEC_CANCEL);
  299. /* it's time for the timer to be serviced */
  300. if (wsi->protocol &&
  301. wsi->protocol->callback(wsi, LWS_CALLBACK_TIMER,
  302. wsi->user_space, NULL, 0))
  303. __lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  304. "timer cb errored");
  305. } lws_end_foreach_dll_safe(d, d1);
  306. /* return an estimate how many us until next timer hit */
  307. if (!pt->dll_head_hrtimer.next)
  308. return LWS_HRTIMER_NOWAIT;
  309. wsi = lws_container_of(pt->dll_head_hrtimer.next, struct lws, dll_hrtimer);
  310. gettimeofday(&now, NULL);
  311. t = (now.tv_sec * 1000000ll) + now.tv_usec;
  312. if (wsi->pending_timer < t)
  313. return 0;
  314. return wsi->pending_timer - t;
  315. }
  316. void
  317. __lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
  318. {
  319. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  320. time_t now;
  321. time(&now);
  322. lwsl_debug("%s: %p: %d secs\n", __func__, wsi, secs);
  323. wsi->pending_timeout_limit = secs;
  324. wsi->pending_timeout_set = now;
  325. wsi->pending_timeout = reason;
  326. if (!reason)
  327. lws_dll_lws_remove(&wsi->dll_timeout);
  328. else
  329. lws_dll_lws_add_front(&wsi->dll_timeout, &pt->dll_head_timeout);
  330. }
  331. LWS_VISIBLE void
  332. lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
  333. {
  334. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  335. if (secs == LWS_TO_KILL_SYNC) {
  336. lws_remove_from_timeout_list(wsi);
  337. lwsl_debug("synchronously killing %p\n", wsi);
  338. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "to sync kill");
  339. return;
  340. }
  341. if (secs == LWS_TO_KILL_ASYNC)
  342. secs = 0;
  343. lws_pt_lock(pt, __func__);
  344. __lws_set_timeout(wsi, reason, secs);
  345. lws_pt_unlock(pt);
  346. }
  347. int
  348. lws_timed_callback_remove(struct lws_vhost *vh, struct lws_timed_vh_protocol *p)
  349. {
  350. lws_start_foreach_llp(struct lws_timed_vh_protocol **, pt,
  351. vh->timed_vh_protocol_list) {
  352. if (*pt == p) {
  353. *pt = p->next;
  354. lws_free(p);
  355. return 0;
  356. }
  357. } lws_end_foreach_llp(pt, next);
  358. return 1;
  359. }
  360. LWS_VISIBLE LWS_EXTERN int
  361. lws_timed_callback_vh_protocol(struct lws_vhost *vh, const struct lws_protocols *prot,
  362. int reason, int secs)
  363. {
  364. struct lws_timed_vh_protocol *p = (struct lws_timed_vh_protocol *)
  365. lws_malloc(sizeof(*p), "timed_vh");
  366. if (!p)
  367. return 1;
  368. p->protocol = prot;
  369. p->reason = reason;
  370. p->time = lws_now_secs() + secs;
  371. p->next = vh->timed_vh_protocol_list;
  372. vh->timed_vh_protocol_list = p;
  373. return 0;
  374. }
  375. static void
  376. lws_remove_child_from_any_parent(struct lws *wsi)
  377. {
  378. struct lws **pwsi;
  379. int seen = 0;
  380. if (!wsi->parent)
  381. return;
  382. /* detach ourselves from parent's child list */
  383. pwsi = &wsi->parent->child_list;
  384. while (*pwsi) {
  385. if (*pwsi == wsi) {
  386. lwsl_info("%s: detach %p from parent %p\n", __func__,
  387. wsi, wsi->parent);
  388. if (wsi->parent->protocol)
  389. wsi->parent->protocol->callback(wsi,
  390. LWS_CALLBACK_CHILD_CLOSING,
  391. wsi->parent->user_space, wsi, 0);
  392. *pwsi = wsi->sibling_list;
  393. seen = 1;
  394. break;
  395. }
  396. pwsi = &(*pwsi)->sibling_list;
  397. }
  398. if (!seen)
  399. lwsl_err("%s: failed to detach from parent\n", __func__);
  400. wsi->parent = NULL;
  401. }
  402. int
  403. lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p)
  404. {
  405. // if (wsi->protocol == p)
  406. // return 0;
  407. const struct lws_protocols *vp = wsi->vhost->protocols, *vpo;
  408. if (wsi->protocol && wsi->protocol_bind_balance) {
  409. wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_DROP_PROTOCOL,
  410. wsi->user_space, NULL, 0);
  411. wsi->protocol_bind_balance = 0;
  412. }
  413. if (!wsi->user_space_externally_allocated)
  414. lws_free_set_NULL(wsi->user_space);
  415. lws_same_vh_protocol_remove(wsi);
  416. wsi->protocol = p;
  417. if (!p)
  418. return 0;
  419. if (lws_ensure_user_space(wsi))
  420. return 1;
  421. if (p > vp && p < &vp[wsi->vhost->count_protocols])
  422. lws_same_vh_protocol_insert(wsi, (int)(p - vp));
  423. else {
  424. int n = wsi->vhost->count_protocols;
  425. int hit = 0;
  426. vpo = vp;
  427. while (n--) {
  428. if (p->name && vp->name && !strcmp(p->name, vp->name)) {
  429. hit = 1;
  430. lws_same_vh_protocol_insert(wsi, (int)(vp - vpo));
  431. break;
  432. }
  433. vp++;
  434. }
  435. if (!hit)
  436. lwsl_err("%s: %p is not in vhost '%s' protocols list\n",
  437. __func__, p, wsi->vhost->name);
  438. }
  439. if (wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_BIND_PROTOCOL,
  440. wsi->user_space, NULL, 0))
  441. return 1;
  442. wsi->protocol_bind_balance = 1;
  443. return 0;
  444. }
  445. void
  446. __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, const char *caller)
  447. {
  448. struct lws_context_per_thread *pt;
  449. struct lws *wsi1, *wsi2;
  450. struct lws_context *context;
  451. int n;
  452. lwsl_info("%s: %p: caller: %s\n", __func__, wsi, caller);
  453. if (!wsi)
  454. return;
  455. lws_access_log(wsi);
  456. context = wsi->context;
  457. pt = &context->pt[(int)wsi->tsi];
  458. lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_CLOSE, 1);
  459. #if !defined(LWS_NO_CLIENT)
  460. lws_free_set_NULL(wsi->client_hostname_copy);
  461. /* we are no longer an active client connection that can piggyback */
  462. lws_dll_lws_remove(&wsi->dll_active_client_conns);
  463. /*
  464. * if we have wsi in our transaction queue, if we are closing we
  465. * must go through and close all those first
  466. */
  467. if (wsi->vhost) {
  468. if ((int)reason != -1)
  469. lws_vhost_lock(wsi->vhost);
  470. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
  471. wsi->dll_client_transaction_queue_head.next) {
  472. struct lws *w = lws_container_of(d, struct lws,
  473. dll_client_transaction_queue);
  474. __lws_close_free_wsi(w, -1, "trans q leader closing");
  475. } lws_end_foreach_dll_safe(d, d1);
  476. /*
  477. * !!! If we are closing, but we have pending pipelined transaction
  478. * results we already sent headers for, that's going to destroy sync
  479. * for HTTP/1 and leave H2 stream with no live swsi.
  480. *
  481. * However this is normal if we are being closed because the transaction
  482. * queue leader is closing.
  483. */
  484. lws_dll_lws_remove(&wsi->dll_client_transaction_queue);
  485. if ((int)reason !=-1)
  486. lws_vhost_unlock(wsi->vhost);
  487. }
  488. #endif
  489. /* if we have children, close them first */
  490. if (wsi->child_list) {
  491. wsi2 = wsi->child_list;
  492. while (wsi2) {
  493. wsi1 = wsi2->sibling_list;
  494. wsi2->parent = NULL;
  495. /* stop it doing shutdown processing */
  496. wsi2->socket_is_permanently_unusable = 1;
  497. __lws_close_free_wsi(wsi2, reason, "general child recurse");
  498. wsi2 = wsi1;
  499. }
  500. wsi->child_list = NULL;
  501. }
  502. if (wsi->role_ops == &role_ops_raw_file) {
  503. lws_remove_child_from_any_parent(wsi);
  504. __remove_wsi_socket_from_fds(wsi);
  505. wsi->protocol->callback(wsi, wsi->role_ops->close_cb[0],
  506. wsi->user_space, NULL, 0);
  507. goto async_close;
  508. }
  509. wsi->wsistate_pre_close = wsi->wsistate;
  510. #ifdef LWS_WITH_CGI
  511. if (wsi->role_ops == &role_ops_cgi) {
  512. /* we are not a network connection, but a handler for CGI io */
  513. if (wsi->parent && wsi->parent->http.cgi) {
  514. if (wsi->cgi_channel == LWS_STDOUT)
  515. lws_cgi_remove_and_kill(wsi->parent);
  516. /* end the binding between us and master */
  517. wsi->parent->http.cgi->stdwsi[(int)wsi->cgi_channel] = NULL;
  518. }
  519. wsi->socket_is_permanently_unusable = 1;
  520. goto just_kill_connection;
  521. }
  522. if (wsi->http.cgi)
  523. lws_cgi_remove_and_kill(wsi);
  524. #endif
  525. #if !defined(LWS_NO_CLIENT)
  526. lws_client_stash_destroy(wsi);
  527. #endif
  528. if (wsi->role_ops == &role_ops_raw_skt) {
  529. wsi->socket_is_permanently_unusable = 1;
  530. goto just_kill_connection;
  531. }
  532. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  533. if (lwsi_role_http(wsi) && lwsi_role_server(wsi) &&
  534. wsi->http.fop_fd != NULL)
  535. lws_vfs_file_close(&wsi->http.fop_fd);
  536. #endif
  537. if (lwsi_state(wsi) == LRS_DEAD_SOCKET)
  538. return;
  539. if (wsi->socket_is_permanently_unusable ||
  540. reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY ||
  541. lwsi_state(wsi) == LRS_SHUTDOWN)
  542. goto just_kill_connection;
  543. switch (lwsi_state_PRE_CLOSE(wsi)) {
  544. case LRS_DEAD_SOCKET:
  545. return;
  546. /* we tried the polite way... */
  547. case LRS_WAITING_TO_SEND_CLOSE:
  548. case LRS_AWAITING_CLOSE_ACK:
  549. case LRS_RETURNED_CLOSE:
  550. goto just_kill_connection;
  551. case LRS_FLUSHING_BEFORE_CLOSE:
  552. if (wsi->trunc_len) {
  553. lws_callback_on_writable(wsi);
  554. return;
  555. }
  556. lwsl_info("%p: end LRS_FLUSHING_BEFORE_CLOSE\n", wsi);
  557. goto just_kill_connection;
  558. default:
  559. if (wsi->trunc_len) {
  560. lwsl_info("%p: LRS_FLUSHING_BEFORE_CLOSE\n", wsi);
  561. lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE);
  562. __lws_set_timeout(wsi,
  563. PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
  564. return;
  565. }
  566. break;
  567. }
  568. if (lwsi_state(wsi) == LRS_WAITING_CONNECT ||
  569. lwsi_state(wsi) == LRS_H1C_ISSUE_HANDSHAKE)
  570. goto just_kill_connection;
  571. if (!wsi->told_user_closed && lwsi_role_http(wsi) &&
  572. lwsi_role_server(wsi)) {
  573. if (wsi->user_space && wsi->protocol &&
  574. wsi->protocol_bind_balance) {
  575. wsi->protocol->callback(wsi,
  576. LWS_CALLBACK_HTTP_DROP_PROTOCOL,
  577. wsi->user_space, NULL, 0);
  578. wsi->protocol_bind_balance = 0;
  579. }
  580. }
  581. /*
  582. * signal we are closing, lws_write will
  583. * add any necessary version-specific stuff. If the write fails,
  584. * no worries we are closing anyway. If we didn't initiate this
  585. * close, then our state has been changed to
  586. * LRS_RETURNED_CLOSE and we will skip this.
  587. *
  588. * Likewise if it's a second call to close this connection after we
  589. * sent the close indication to the peer already, we are in state
  590. * LRS_AWAITING_CLOSE_ACK and will skip doing this a second time.
  591. */
  592. if (wsi->role_ops->close_via_role_protocol &&
  593. wsi->role_ops->close_via_role_protocol(wsi, reason))
  594. return;
  595. just_kill_connection:
  596. if (wsi->role_ops->close_kill_connection)
  597. wsi->role_ops->close_kill_connection(wsi, reason);
  598. lws_remove_child_from_any_parent(wsi);
  599. n = 0;
  600. if (!wsi->told_user_closed && wsi->user_space &&
  601. wsi->protocol_bind_balance) {
  602. lwsl_debug("%s: %p: DROP_PROTOCOL %s\n", __func__, wsi,
  603. wsi->protocol->name);
  604. wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_DROP_PROTOCOL,
  605. wsi->user_space, NULL, 0);
  606. wsi->protocol_bind_balance = 0;
  607. }
  608. if ((lwsi_state(wsi) == LRS_WAITING_SERVER_REPLY ||
  609. lwsi_state(wsi) == LRS_WAITING_CONNECT) && !wsi->already_did_cce)
  610. wsi->protocol->callback(wsi,
  611. LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
  612. wsi->user_space, NULL, 0);
  613. /*
  614. * Testing with ab shows that we have to stage the socket close when
  615. * the system is under stress... shutdown any further TX, change the
  616. * state to one that won't emit anything more, and wait with a timeout
  617. * for the POLLIN to show a zero-size rx before coming back and doing
  618. * the actual close.
  619. */
  620. if (wsi->role_ops != &role_ops_raw_skt && !lwsi_role_client(wsi) &&
  621. lwsi_state(wsi) != LRS_SHUTDOWN &&
  622. lwsi_state(wsi) != LRS_UNCONNECTED &&
  623. reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
  624. !wsi->socket_is_permanently_unusable) {
  625. #if defined(LWS_WITH_TLS)
  626. if (lws_is_ssl(wsi) && wsi->tls.ssl) {
  627. n = 0;
  628. switch (__lws_tls_shutdown(wsi)) {
  629. case LWS_SSL_CAPABLE_DONE:
  630. case LWS_SSL_CAPABLE_ERROR:
  631. case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
  632. case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
  633. case LWS_SSL_CAPABLE_MORE_SERVICE:
  634. break;
  635. }
  636. } else
  637. #endif
  638. {
  639. lwsl_info("%s: shutdown conn: %p (sock %d, state 0x%x)\n",
  640. __func__, wsi, (int)(long)wsi->desc.sockfd,
  641. lwsi_state(wsi));
  642. if (!wsi->socket_is_permanently_unusable &&
  643. lws_socket_is_valid(wsi->desc.sockfd)) {
  644. wsi->socket_is_permanently_unusable = 1;
  645. n = shutdown(wsi->desc.sockfd, SHUT_WR);
  646. }
  647. }
  648. if (n)
  649. lwsl_debug("closing: shutdown (state 0x%x) ret %d\n",
  650. lwsi_state(wsi), LWS_ERRNO);
  651. /*
  652. * This causes problems on WINCE / ESP32 with disconnection
  653. * when the events are half closing connection
  654. */
  655. #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP32)
  656. /* libuv: no event available to guarantee completion */
  657. if (!wsi->socket_is_permanently_unusable &&
  658. lws_socket_is_valid(wsi->desc.sockfd) &&
  659. lwsi_state(wsi) != LRS_SHUTDOWN &&
  660. context->event_loop_ops->periodic_events_available) {
  661. __lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
  662. lwsi_set_state(wsi, LRS_SHUTDOWN);
  663. __lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
  664. context->timeout_secs);
  665. return;
  666. }
  667. #endif
  668. }
  669. lwsl_debug("%s: real just_kill_connection: %p (sockfd %d)\n", __func__,
  670. wsi, wsi->desc.sockfd);
  671. #ifdef LWS_WITH_HTTP_PROXY
  672. if (wsi->http.rw) {
  673. lws_rewrite_destroy(wsi->http.rw);
  674. wsi->http.rw = NULL;
  675. }
  676. #endif
  677. /*
  678. * we won't be servicing or receiving anything further from this guy
  679. * delete socket from the internal poll list if still present
  680. */
  681. __lws_ssl_remove_wsi_from_buffered_list(wsi);
  682. __lws_remove_from_timeout_list(wsi);
  683. lws_dll_lws_remove(&wsi->dll_hrtimer);
  684. /* don't repeat event loop stuff */
  685. if (wsi->told_event_loop_closed)
  686. return;
  687. /* checking return redundant since we anyway close */
  688. if (wsi->desc.sockfd != LWS_SOCK_INVALID)
  689. __remove_wsi_socket_from_fds(wsi);
  690. else
  691. lws_same_vh_protocol_remove(wsi);
  692. lwsi_set_state(wsi, LRS_DEAD_SOCKET);
  693. lws_buflist_destroy_all_segments(&wsi->buflist);
  694. lws_dll_lws_remove(&wsi->dll_buflist);
  695. if (wsi->role_ops->close_role)
  696. wsi->role_ops->close_role(pt, wsi);
  697. /* tell the user it's all over for this guy */
  698. if (lwsi_state_est_PRE_CLOSE(wsi) && !wsi->told_user_closed &&
  699. wsi->role_ops->close_cb[lwsi_role_server(wsi)]) {
  700. const struct lws_protocols *pro = wsi->protocol;
  701. if (!wsi->protocol)
  702. pro = &wsi->vhost->protocols[0];
  703. if (!wsi->upgraded_to_http2 || !lwsi_role_client(wsi))
  704. /*
  705. * The network wsi for a client h2 connection shouldn't
  706. * call back for its role: the child stream connections
  707. * own the role. Otherwise h2 will call back closed
  708. * one too many times as the children do it and then
  709. * the closing network stream.
  710. */
  711. pro->callback(wsi,
  712. wsi->role_ops->close_cb[lwsi_role_server(wsi)],
  713. wsi->user_space, NULL, 0);
  714. wsi->told_user_closed = 1;
  715. }
  716. async_close:
  717. wsi->socket_is_permanently_unusable = 1;
  718. if (wsi->context->event_loop_ops->wsi_logical_close)
  719. if (wsi->context->event_loop_ops->wsi_logical_close(wsi))
  720. return;
  721. __lws_close_free_wsi_final(wsi);
  722. }
  723. void
  724. __lws_close_free_wsi_final(struct lws *wsi)
  725. {
  726. int n;
  727. if (lws_socket_is_valid(wsi->desc.sockfd) && !lws_ssl_close(wsi)) {
  728. n = compatible_close(wsi->desc.sockfd);
  729. if (n)
  730. lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
  731. wsi->desc.sockfd = LWS_SOCK_INVALID;
  732. }
  733. /* outermost destroy notification for wsi (user_space still intact) */
  734. if (wsi->vhost)
  735. wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
  736. wsi->user_space, NULL, 0);
  737. #ifdef LWS_WITH_CGI
  738. if (wsi->http.cgi) {
  739. for (n = 0; n < 3; n++) {
  740. if (wsi->http.cgi->pipe_fds[n][!!(n == 0)] == 0)
  741. lwsl_err("ZERO FD IN CGI CLOSE");
  742. if (wsi->http.cgi->pipe_fds[n][!!(n == 0)] >= 0)
  743. close(wsi->http.cgi->pipe_fds[n][!!(n == 0)]);
  744. }
  745. lws_free(wsi->http.cgi);
  746. }
  747. #endif
  748. __lws_free_wsi(wsi);
  749. }
  750. void
  751. lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, const char *caller)
  752. {
  753. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  754. lws_pt_lock(pt, __func__);
  755. __lws_close_free_wsi(wsi, reason, caller);
  756. lws_pt_unlock(pt);
  757. }
  758. /* lws_buflist */
  759. int
  760. lws_buflist_append_segment(struct lws_buflist **head, const uint8_t *buf,
  761. size_t len)
  762. {
  763. struct lws_buflist *nbuf;
  764. int first = !*head;
  765. void *p = *head;
  766. int sanity = 1024;
  767. assert(buf);
  768. assert(len);
  769. /* append at the tail */
  770. while (*head) {
  771. if (!--sanity || head == &((*head)->next)) {
  772. lwsl_err("%s: corrupt list points to self\n", __func__);
  773. return -1;
  774. }
  775. head = &((*head)->next);
  776. }
  777. lwsl_info("%s: len %u first %d %p\n", __func__, (uint32_t)len, first, p);
  778. nbuf = (struct lws_buflist *)
  779. lws_malloc(sizeof(**head) + len, __func__);
  780. if (!nbuf) {
  781. lwsl_err("%s: OOM\n", __func__);
  782. return -1;
  783. }
  784. nbuf->len = len;
  785. nbuf->pos = 0;
  786. nbuf->next = NULL;
  787. p = (void *)nbuf->buf;
  788. memcpy(p, buf, len);
  789. *head = nbuf;
  790. return first; /* returns 1 if first segment just created */
  791. }
  792. static int
  793. lws_buflist_destroy_segment(struct lws_buflist **head)
  794. {
  795. struct lws_buflist *old = *head;
  796. assert(*head);
  797. *head = (*head)->next;
  798. old->next = NULL;
  799. lws_free(old);
  800. return !*head; /* returns 1 if last segment just destroyed */
  801. }
  802. void
  803. lws_buflist_destroy_all_segments(struct lws_buflist **head)
  804. {
  805. struct lws_buflist *p = *head, *p1;
  806. while (p) {
  807. p1 = p->next;
  808. p->next = NULL;
  809. lws_free(p);
  810. p = p1;
  811. }
  812. *head = NULL;
  813. }
  814. size_t
  815. lws_buflist_next_segment_len(struct lws_buflist **head, uint8_t **buf)
  816. {
  817. if (!*head) {
  818. if (buf)
  819. *buf = NULL;
  820. return 0;
  821. }
  822. if (!(*head)->len && (*head)->next)
  823. lws_buflist_destroy_segment(head);
  824. if (!*head) {
  825. if (buf)
  826. *buf = NULL;
  827. return 0;
  828. }
  829. assert((*head)->pos < (*head)->len);
  830. if (buf)
  831. *buf = (*head)->buf + (*head)->pos;
  832. return (*head)->len - (*head)->pos;
  833. }
  834. int
  835. lws_buflist_use_segment(struct lws_buflist **head, size_t len)
  836. {
  837. assert(*head);
  838. assert(len);
  839. assert((*head)->pos + len <= (*head)->len);
  840. (*head)->pos += len;
  841. if ((*head)->pos == (*head)->len)
  842. lws_buflist_destroy_segment(head);
  843. if (!*head)
  844. return 0;
  845. return (int)((*head)->len - (*head)->pos);
  846. }
  847. void
  848. lws_buflist_describe(struct lws_buflist **head, void *id)
  849. {
  850. struct lws_buflist *old;
  851. int n = 0;
  852. if (*head == NULL)
  853. lwsl_notice("%p: buflist empty\n", id);
  854. while (*head) {
  855. lwsl_notice("%p: %d: %llu / %llu (%llu left)\n", id, n,
  856. (unsigned long long)(*head)->pos,
  857. (unsigned long long)(*head)->len,
  858. (unsigned long long)(*head)->len - (*head)->pos);
  859. old = *head;
  860. head = &((*head)->next);
  861. if (*head == old) {
  862. lwsl_err("%s: next points to self\n", __func__);
  863. break;
  864. }
  865. n++;
  866. }
  867. }
  868. /* ... */
  869. LWS_VISIBLE LWS_EXTERN const char *
  870. lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
  871. {
  872. int n = 0, sl = (int)strlen(name);
  873. while (lws_hdr_copy_fragment(wsi, buf, len,
  874. WSI_TOKEN_HTTP_URI_ARGS, n) >= 0) {
  875. if (!strncmp(buf, name, sl))
  876. return buf + sl;
  877. n++;
  878. }
  879. return NULL;
  880. }
  881. #if !defined(LWS_WITH_ESP32)
  882. LWS_VISIBLE int
  883. interface_to_sa(struct lws_vhost *vh, const char *ifname,
  884. struct sockaddr_in *addr, size_t addrlen)
  885. {
  886. int ipv6 = 0;
  887. #ifdef LWS_WITH_IPV6
  888. ipv6 = LWS_IPV6_ENABLED(vh);
  889. #endif
  890. (void)vh;
  891. return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
  892. }
  893. #endif
  894. #ifndef LWS_PLAT_OPTEE
  895. static int
  896. lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
  897. int name_len, char *rip, int rip_len)
  898. {
  899. struct addrinfo ai, *res;
  900. struct sockaddr_in addr4;
  901. rip[0] = '\0';
  902. name[0] = '\0';
  903. addr4.sin_family = AF_UNSPEC;
  904. #ifdef LWS_WITH_IPV6
  905. if (LWS_IPV6_ENABLED(vh)) {
  906. if (!lws_plat_inet_ntop(AF_INET6,
  907. &((struct sockaddr_in6 *)ads)->sin6_addr,
  908. rip, rip_len)) {
  909. lwsl_err("inet_ntop: %s", strerror(LWS_ERRNO));
  910. return -1;
  911. }
  912. // Strip off the IPv4 to IPv6 header if one exists
  913. if (strncmp(rip, "::ffff:", 7) == 0)
  914. memmove(rip, rip + 7, strlen(rip) - 6);
  915. getnameinfo((struct sockaddr *)ads, sizeof(struct sockaddr_in6),
  916. name, name_len, NULL, 0, 0);
  917. return 0;
  918. } else
  919. #endif
  920. {
  921. struct addrinfo *result;
  922. memset(&ai, 0, sizeof ai);
  923. ai.ai_family = PF_UNSPEC;
  924. ai.ai_socktype = SOCK_STREAM;
  925. #if !defined(LWS_WITH_ESP32)
  926. if (getnameinfo((struct sockaddr *)ads,
  927. sizeof(struct sockaddr_in),
  928. name, name_len, NULL, 0, 0))
  929. return -1;
  930. #endif
  931. if (getaddrinfo(name, NULL, &ai, &result))
  932. return -1;
  933. res = result;
  934. while (addr4.sin_family == AF_UNSPEC && res) {
  935. switch (res->ai_family) {
  936. case AF_INET:
  937. addr4.sin_addr =
  938. ((struct sockaddr_in *)res->ai_addr)->sin_addr;
  939. addr4.sin_family = AF_INET;
  940. break;
  941. }
  942. res = res->ai_next;
  943. }
  944. freeaddrinfo(result);
  945. }
  946. if (addr4.sin_family == AF_UNSPEC)
  947. return -1;
  948. if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
  949. return -1;
  950. return 0;
  951. }
  952. LWS_VISIBLE const char *
  953. lws_get_peer_simple(struct lws *wsi, char *name, int namelen)
  954. {
  955. socklen_t len, olen;
  956. #ifdef LWS_WITH_IPV6
  957. struct sockaddr_in6 sin6;
  958. #endif
  959. struct sockaddr_in sin4;
  960. int af = AF_INET;
  961. void *p, *q;
  962. wsi = lws_get_network_wsi(wsi);
  963. if (wsi->parent_carries_io)
  964. wsi = wsi->parent;
  965. #ifdef LWS_WITH_IPV6
  966. if (LWS_IPV6_ENABLED(wsi->vhost)) {
  967. len = sizeof(sin6);
  968. p = &sin6;
  969. af = AF_INET6;
  970. q = &sin6.sin6_addr;
  971. } else
  972. #endif
  973. {
  974. len = sizeof(sin4);
  975. p = &sin4;
  976. q = &sin4.sin_addr;
  977. }
  978. olen = len;
  979. if (getpeername(wsi->desc.sockfd, p, &len) < 0 || len > olen) {
  980. lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
  981. return NULL;
  982. }
  983. return lws_plat_inet_ntop(af, q, name, namelen);
  984. }
  985. #endif
  986. LWS_VISIBLE void
  987. lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
  988. int name_len, char *rip, int rip_len)
  989. {
  990. #ifndef LWS_PLAT_OPTEE
  991. socklen_t len;
  992. #ifdef LWS_WITH_IPV6
  993. struct sockaddr_in6 sin6;
  994. #endif
  995. struct sockaddr_in sin4;
  996. struct lws_context *context = wsi->context;
  997. int ret = -1;
  998. void *p;
  999. rip[0] = '\0';
  1000. name[0] = '\0';
  1001. lws_latency_pre(context, wsi);
  1002. #ifdef LWS_WITH_IPV6
  1003. if (LWS_IPV6_ENABLED(wsi->vhost)) {
  1004. len = sizeof(sin6);
  1005. p = &sin6;
  1006. } else
  1007. #endif
  1008. {
  1009. len = sizeof(sin4);
  1010. p = &sin4;
  1011. }
  1012. if (getpeername(fd, p, &len) < 0) {
  1013. lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
  1014. goto bail;
  1015. }
  1016. ret = lws_get_addresses(wsi->vhost, p, name, name_len, rip, rip_len);
  1017. bail:
  1018. lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
  1019. #endif
  1020. (void)wsi;
  1021. (void)fd;
  1022. (void)name;
  1023. (void)name_len;
  1024. (void)rip;
  1025. (void)rip_len;
  1026. }
  1027. LWS_EXTERN void *
  1028. lws_vhost_user(struct lws_vhost *vhost)
  1029. {
  1030. return vhost->user;
  1031. }
  1032. LWS_EXTERN void *
  1033. lws_context_user(struct lws_context *context)
  1034. {
  1035. return context->user_space;
  1036. }
  1037. LWS_VISIBLE struct lws_vhost *
  1038. lws_vhost_get(struct lws *wsi)
  1039. {
  1040. return wsi->vhost;
  1041. }
  1042. LWS_VISIBLE struct lws_vhost *
  1043. lws_get_vhost(struct lws *wsi)
  1044. {
  1045. return wsi->vhost;
  1046. }
  1047. LWS_VISIBLE const struct lws_protocols *
  1048. lws_protocol_get(struct lws *wsi)
  1049. {
  1050. return wsi->protocol;
  1051. }
  1052. LWS_VISIBLE const struct lws_udp *
  1053. lws_get_udp(const struct lws *wsi)
  1054. {
  1055. return wsi->udp;
  1056. }
  1057. LWS_VISIBLE struct lws *
  1058. lws_get_network_wsi(struct lws *wsi)
  1059. {
  1060. if (!wsi)
  1061. return NULL;
  1062. #if defined(LWS_WITH_HTTP2)
  1063. if (!wsi->http2_substream && !wsi->client_h2_substream)
  1064. return wsi;
  1065. while (wsi->h2.parent_wsi)
  1066. wsi = wsi->h2.parent_wsi;
  1067. #endif
  1068. return wsi;
  1069. }
  1070. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1071. lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
  1072. {
  1073. int n;
  1074. for (n = 0; n < vh->count_protocols; n++)
  1075. if (!strcmp(name, vh->protocols[n].name))
  1076. return &vh->protocols[n];
  1077. return NULL;
  1078. }
  1079. LWS_VISIBLE int
  1080. lws_callback_all_protocol(struct lws_context *context,
  1081. const struct lws_protocols *protocol, int reason)
  1082. {
  1083. struct lws_context_per_thread *pt = &context->pt[0];
  1084. unsigned int n, m = context->count_threads;
  1085. struct lws *wsi;
  1086. while (m--) {
  1087. for (n = 0; n < pt->fds_count; n++) {
  1088. wsi = wsi_from_fd(context, pt->fds[n].fd);
  1089. if (!wsi)
  1090. continue;
  1091. if (wsi->protocol == protocol)
  1092. protocol->callback(wsi, reason, wsi->user_space,
  1093. NULL, 0);
  1094. }
  1095. pt++;
  1096. }
  1097. return 0;
  1098. }
  1099. LWS_VISIBLE int
  1100. lws_callback_all_protocol_vhost_args(struct lws_vhost *vh,
  1101. const struct lws_protocols *protocol, int reason,
  1102. void *argp, size_t len)
  1103. {
  1104. struct lws_context *context = vh->context;
  1105. struct lws_context_per_thread *pt = &context->pt[0];
  1106. unsigned int n, m = context->count_threads;
  1107. struct lws *wsi;
  1108. while (m--) {
  1109. for (n = 0; n < pt->fds_count; n++) {
  1110. wsi = wsi_from_fd(context, pt->fds[n].fd);
  1111. if (!wsi)
  1112. continue;
  1113. if (wsi->vhost == vh && (wsi->protocol == protocol ||
  1114. !protocol))
  1115. wsi->protocol->callback(wsi, reason,
  1116. wsi->user_space, argp, len);
  1117. }
  1118. pt++;
  1119. }
  1120. return 0;
  1121. }
  1122. LWS_VISIBLE int
  1123. lws_callback_all_protocol_vhost(struct lws_vhost *vh,
  1124. const struct lws_protocols *protocol, int reason)
  1125. {
  1126. return lws_callback_all_protocol_vhost_args(vh, protocol, reason, NULL, 0);
  1127. }
  1128. LWS_VISIBLE LWS_EXTERN int
  1129. lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
  1130. {
  1131. int n;
  1132. for (n = 0; n < wsi->vhost->count_protocols; n++)
  1133. if (wsi->vhost->protocols[n].callback(wsi, reason, NULL, in, len))
  1134. return 1;
  1135. return 0;
  1136. }
  1137. LWS_VISIBLE LWS_EXTERN int
  1138. lws_callback_vhost_protocols_vhost(struct lws_vhost *vh, int reason, void *in,
  1139. size_t len)
  1140. {
  1141. int n;
  1142. struct lws *wsi = lws_zalloc(sizeof(*wsi), "fake wsi");
  1143. wsi->context = vh->context;
  1144. wsi->vhost = vh;
  1145. for (n = 0; n < wsi->vhost->count_protocols; n++) {
  1146. wsi->protocol = &vh->protocols[n];
  1147. if (wsi->protocol->callback(wsi, reason, NULL, in, len)) {
  1148. lws_free(wsi);
  1149. return 1;
  1150. }
  1151. }
  1152. lws_free(wsi);
  1153. return 0;
  1154. }
  1155. LWS_VISIBLE LWS_EXTERN void
  1156. lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
  1157. {
  1158. context->fops = fops;
  1159. }
  1160. LWS_VISIBLE LWS_EXTERN lws_filepos_t
  1161. lws_vfs_tell(lws_fop_fd_t fop_fd)
  1162. {
  1163. return fop_fd->pos;
  1164. }
  1165. LWS_VISIBLE LWS_EXTERN lws_filepos_t
  1166. lws_vfs_get_length(lws_fop_fd_t fop_fd)
  1167. {
  1168. return fop_fd->len;
  1169. }
  1170. LWS_VISIBLE LWS_EXTERN uint32_t
  1171. lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
  1172. {
  1173. return fop_fd->mod_time;
  1174. }
  1175. LWS_VISIBLE lws_fileofs_t
  1176. lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  1177. {
  1178. lws_fileofs_t ofs;
  1179. ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset - fop_fd->pos);
  1180. return ofs;
  1181. }
  1182. LWS_VISIBLE lws_fileofs_t
  1183. lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  1184. {
  1185. return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, fop_fd->len +
  1186. fop_fd->pos + offset);
  1187. }
  1188. const struct lws_plat_file_ops *
  1189. lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
  1190. const char **vpath)
  1191. {
  1192. const struct lws_plat_file_ops *pf;
  1193. const char *p = vfs_path;
  1194. int n;
  1195. *vpath = NULL;
  1196. /* no non-platform fops, just use that */
  1197. if (!fops->next)
  1198. return fops;
  1199. /*
  1200. * scan the vfs path looking for indications we are to be
  1201. * handled by a specific fops
  1202. */
  1203. while (p && *p) {
  1204. if (*p != '/') {
  1205. p++;
  1206. continue;
  1207. }
  1208. /* the first one is always platform fops, so skip */
  1209. pf = fops->next;
  1210. while (pf) {
  1211. n = 0;
  1212. while (n < (int)LWS_ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
  1213. if (p >= vfs_path + pf->fi[n].len)
  1214. if (!strncmp(p - (pf->fi[n].len - 1),
  1215. pf->fi[n].sig,
  1216. pf->fi[n].len - 1)) {
  1217. *vpath = p + 1;
  1218. return pf;
  1219. }
  1220. n++;
  1221. }
  1222. pf = pf->next;
  1223. }
  1224. p++;
  1225. }
  1226. return fops;
  1227. }
  1228. LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
  1229. lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
  1230. lws_fop_flags_t *flags)
  1231. {
  1232. const char *vpath = "";
  1233. const struct lws_plat_file_ops *selected;
  1234. selected = lws_vfs_select_fops(fops, vfs_path, &vpath);
  1235. return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
  1236. }
  1237. /**
  1238. * lws_now_secs() - seconds since 1970-1-1
  1239. *
  1240. */
  1241. LWS_VISIBLE LWS_EXTERN unsigned long
  1242. lws_now_secs(void)
  1243. {
  1244. struct timeval tv;
  1245. gettimeofday(&tv, NULL);
  1246. return tv.tv_sec;
  1247. }
  1248. LWS_VISIBLE LWS_EXTERN int
  1249. lws_compare_time_t(struct lws_context *context, time_t t1, time_t t2)
  1250. {
  1251. if (t1 < context->time_discontiguity)
  1252. t1 += context->time_fixup;
  1253. if (t2 < context->time_discontiguity)
  1254. t2 += context->time_fixup;
  1255. return (int)(t1 - t2);
  1256. }
  1257. LWS_VISIBLE lws_sockfd_type
  1258. lws_get_socket_fd(struct lws *wsi)
  1259. {
  1260. if (!wsi)
  1261. return -1;
  1262. return wsi->desc.sockfd;
  1263. }
  1264. #ifdef LWS_LATENCY
  1265. void
  1266. lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
  1267. int ret, int completed)
  1268. {
  1269. unsigned long long u;
  1270. char buf[256];
  1271. u = time_in_microseconds();
  1272. if (!action) {
  1273. wsi->latency_start = u;
  1274. if (!wsi->action_start)
  1275. wsi->action_start = u;
  1276. return;
  1277. }
  1278. if (completed) {
  1279. if (wsi->action_start == wsi->latency_start)
  1280. sprintf(buf,
  1281. "Completion first try lat %lluus: %p: ret %d: %s\n",
  1282. u - wsi->latency_start,
  1283. (void *)wsi, ret, action);
  1284. else
  1285. sprintf(buf,
  1286. "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
  1287. u - wsi->action_start,
  1288. u - wsi->latency_start,
  1289. (void *)wsi, ret, action);
  1290. wsi->action_start = 0;
  1291. } else
  1292. sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
  1293. u - wsi->latency_start, (void *)wsi, ret, action);
  1294. if (u - wsi->latency_start > context->worst_latency) {
  1295. context->worst_latency = u - wsi->latency_start;
  1296. strcpy(context->worst_latency_info, buf);
  1297. }
  1298. lwsl_latency("%s", buf);
  1299. }
  1300. #endif
  1301. LWS_VISIBLE int
  1302. lws_rx_flow_control(struct lws *wsi, int _enable)
  1303. {
  1304. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  1305. int en = _enable;
  1306. // h2 ignores rx flow control atm
  1307. if (lwsi_role_h2(wsi) || wsi->http2_substream ||
  1308. lwsi_role_h2_ENCAPSULATION(wsi))
  1309. return 0; // !!!
  1310. lwsl_info("%s: %p 0x%x\n", __func__, wsi, _enable);
  1311. if (!(_enable & LWS_RXFLOW_REASON_APPLIES)) {
  1312. /*
  1313. * convert user bool style to bitmap style... in user simple
  1314. * bool style _enable = 0 = flow control it, = 1 = allow rx
  1315. */
  1316. en = LWS_RXFLOW_REASON_APPLIES | LWS_RXFLOW_REASON_USER_BOOL;
  1317. if (_enable & 1)
  1318. en |= LWS_RXFLOW_REASON_APPLIES_ENABLE_BIT;
  1319. }
  1320. lws_pt_lock(pt, __func__);
  1321. /* any bit set in rxflow_bitmap DISABLEs rxflow control */
  1322. if (en & LWS_RXFLOW_REASON_APPLIES_ENABLE_BIT)
  1323. wsi->rxflow_bitmap &= ~(en & 0xff);
  1324. else
  1325. wsi->rxflow_bitmap |= en & 0xff;
  1326. if ((LWS_RXFLOW_PENDING_CHANGE | (!wsi->rxflow_bitmap)) ==
  1327. wsi->rxflow_change_to)
  1328. goto skip;
  1329. wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !wsi->rxflow_bitmap;
  1330. lwsl_info("%s: %p: bitmap 0x%x: en 0x%x, ch 0x%x\n", __func__, wsi,
  1331. wsi->rxflow_bitmap, en, wsi->rxflow_change_to);
  1332. if (_enable & LWS_RXFLOW_REASON_FLAG_PROCESS_NOW ||
  1333. !wsi->rxflow_will_be_applied) {
  1334. en = __lws_rx_flow_control(wsi);
  1335. lws_pt_unlock(pt);
  1336. return en;
  1337. }
  1338. skip:
  1339. lws_pt_unlock(pt);
  1340. return 0;
  1341. }
  1342. LWS_VISIBLE void
  1343. lws_rx_flow_allow_all_protocol(const struct lws_context *context,
  1344. const struct lws_protocols *protocol)
  1345. {
  1346. const struct lws_context_per_thread *pt = &context->pt[0];
  1347. struct lws *wsi;
  1348. unsigned int n, m = context->count_threads;
  1349. while (m--) {
  1350. for (n = 0; n < pt->fds_count; n++) {
  1351. wsi = wsi_from_fd(context, pt->fds[n].fd);
  1352. if (!wsi)
  1353. continue;
  1354. if (wsi->protocol == protocol)
  1355. lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
  1356. }
  1357. pt++;
  1358. }
  1359. }
  1360. int
  1361. lws_broadcast(struct lws_context *context, int reason, void *in, size_t len)
  1362. {
  1363. struct lws_vhost *v = context->vhost_list;
  1364. struct lws wsi;
  1365. int n, ret = 0;
  1366. memset(&wsi, 0, sizeof(wsi));
  1367. wsi.context = context;
  1368. while (v) {
  1369. const struct lws_protocols *p = v->protocols;
  1370. wsi.vhost = v;
  1371. for (n = 0; n < v->count_protocols; n++) {
  1372. wsi.protocol = p;
  1373. if (p->callback &&
  1374. p->callback(&wsi, reason, NULL, in, len))
  1375. ret |= 1;
  1376. p++;
  1377. }
  1378. v = v->vhost_next;
  1379. }
  1380. return ret;
  1381. }
  1382. LWS_VISIBLE extern const char *
  1383. lws_canonical_hostname(struct lws_context *context)
  1384. {
  1385. return (const char *)context->canonical_hostname;
  1386. }
  1387. LWS_VISIBLE LWS_EXTERN const char *
  1388. lws_get_vhost_name(struct lws_vhost *vhost)
  1389. {
  1390. return vhost->name;
  1391. }
  1392. LWS_VISIBLE LWS_EXTERN int
  1393. lws_get_vhost_port(struct lws_vhost *vhost)
  1394. {
  1395. return vhost->listen_port;
  1396. }
  1397. LWS_VISIBLE LWS_EXTERN void *
  1398. lws_get_vhost_user(struct lws_vhost *vhost)
  1399. {
  1400. return vhost->user;
  1401. }
  1402. LWS_VISIBLE LWS_EXTERN const char *
  1403. lws_get_vhost_iface(struct lws_vhost *vhost)
  1404. {
  1405. return vhost->iface;
  1406. }
  1407. int user_callback_handle_rxflow(lws_callback_function callback_function,
  1408. struct lws *wsi,
  1409. enum lws_callback_reasons reason, void *user,
  1410. void *in, size_t len)
  1411. {
  1412. int n;
  1413. wsi->rxflow_will_be_applied = 1;
  1414. n = callback_function(wsi, reason, user, in, len);
  1415. wsi->rxflow_will_be_applied = 0;
  1416. if (!n)
  1417. n = __lws_rx_flow_control(wsi);
  1418. return n;
  1419. }
  1420. #if !defined(LWS_WITHOUT_CLIENT)
  1421. LWS_VISIBLE int
  1422. lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
  1423. {
  1424. char *p;
  1425. char authstring[96];
  1426. if (!proxy)
  1427. return -1;
  1428. /* we have to deal with a possible redundant leading http:// */
  1429. if (!strncmp(proxy, "http://", 7))
  1430. proxy += 7;
  1431. p = strrchr(proxy, '@');
  1432. if (p) { /* auth is around */
  1433. if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
  1434. goto auth_too_long;
  1435. lws_strncpy(authstring, proxy, p - proxy + 1);
  1436. // null termination not needed on input
  1437. if (lws_b64_encode_string(authstring, lws_ptr_diff(p, proxy),
  1438. vhost->proxy_basic_auth_token,
  1439. sizeof vhost->proxy_basic_auth_token) < 0)
  1440. goto auth_too_long;
  1441. lwsl_info(" Proxy auth in use\n");
  1442. proxy = p + 1;
  1443. } else
  1444. vhost->proxy_basic_auth_token[0] = '\0';
  1445. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  1446. lws_strncpy(vhost->http.http_proxy_address, proxy,
  1447. sizeof(vhost->http.http_proxy_address));
  1448. p = strchr(vhost->http.http_proxy_address, ':');
  1449. if (!p && !vhost->http.http_proxy_port) {
  1450. lwsl_err("http_proxy needs to be ads:port\n");
  1451. return -1;
  1452. } else {
  1453. if (p) {
  1454. *p = '\0';
  1455. vhost->http.http_proxy_port = atoi(p + 1);
  1456. }
  1457. }
  1458. lwsl_info(" Proxy %s:%u\n", vhost->http.http_proxy_address,
  1459. vhost->http.http_proxy_port);
  1460. #endif
  1461. return 0;
  1462. auth_too_long:
  1463. lwsl_err("proxy auth too long\n");
  1464. return -1;
  1465. }
  1466. #endif
  1467. #if defined(LWS_WITH_SOCKS5)
  1468. LWS_VISIBLE int
  1469. lws_set_socks(struct lws_vhost *vhost, const char *socks)
  1470. {
  1471. char *p_at, *p_colon;
  1472. char user[96];
  1473. char password[96];
  1474. if (!socks)
  1475. return -1;
  1476. vhost->socks_user[0] = '\0';
  1477. vhost->socks_password[0] = '\0';
  1478. p_at = strrchr(socks, '@');
  1479. if (p_at) { /* auth is around */
  1480. if ((unsigned int)(p_at - socks) > (sizeof(user)
  1481. + sizeof(password) - 2)) {
  1482. lwsl_err("Socks auth too long\n");
  1483. goto bail;
  1484. }
  1485. p_colon = strchr(socks, ':');
  1486. if (p_colon) {
  1487. if ((unsigned int)(p_colon - socks) > (sizeof(user)
  1488. - 1) ) {
  1489. lwsl_err("Socks user too long\n");
  1490. goto bail;
  1491. }
  1492. if ((unsigned int)(p_at - p_colon) > (sizeof(password)
  1493. - 1) ) {
  1494. lwsl_err("Socks password too long\n");
  1495. goto bail;
  1496. }
  1497. lws_strncpy(vhost->socks_user, socks, p_colon - socks + 1);
  1498. lws_strncpy(vhost->socks_password, p_colon + 1,
  1499. p_at - (p_colon + 1) + 1);
  1500. }
  1501. lwsl_info(" Socks auth, user: %s, password: %s\n",
  1502. vhost->socks_user, vhost->socks_password );
  1503. socks = p_at + 1;
  1504. }
  1505. lws_strncpy(vhost->socks_proxy_address, socks,
  1506. sizeof(vhost->socks_proxy_address));
  1507. p_colon = strchr(vhost->socks_proxy_address, ':');
  1508. if (!p_colon && !vhost->socks_proxy_port) {
  1509. lwsl_err("socks_proxy needs to be address:port\n");
  1510. return -1;
  1511. } else {
  1512. if (p_colon) {
  1513. *p_colon = '\0';
  1514. vhost->socks_proxy_port = atoi(p_colon + 1);
  1515. }
  1516. }
  1517. lwsl_info(" Socks %s:%u\n", vhost->socks_proxy_address,
  1518. vhost->socks_proxy_port);
  1519. return 0;
  1520. bail:
  1521. return -1;
  1522. }
  1523. #endif
  1524. LWS_VISIBLE const struct lws_protocols *
  1525. lws_get_protocol(struct lws *wsi)
  1526. {
  1527. return wsi->protocol;
  1528. }
  1529. int
  1530. lws_ensure_user_space(struct lws *wsi)
  1531. {
  1532. if (!wsi->protocol)
  1533. return 0;
  1534. /* allocate the per-connection user memory (if any) */
  1535. if (wsi->protocol->per_session_data_size && !wsi->user_space) {
  1536. wsi->user_space = lws_zalloc(
  1537. wsi->protocol->per_session_data_size, "user space");
  1538. if (wsi->user_space == NULL) {
  1539. lwsl_err("%s: OOM\n", __func__);
  1540. return 1;
  1541. }
  1542. } else
  1543. lwsl_debug("%s: %p protocol pss %lu, user_space=%p\n", __func__,
  1544. wsi, (long)wsi->protocol->per_session_data_size,
  1545. wsi->user_space);
  1546. return 0;
  1547. }
  1548. LWS_VISIBLE void *
  1549. lws_adjust_protocol_psds(struct lws *wsi, size_t new_size)
  1550. {
  1551. ((struct lws_protocols *)lws_get_protocol(wsi))->per_session_data_size =
  1552. new_size;
  1553. if (lws_ensure_user_space(wsi))
  1554. return NULL;
  1555. return wsi->user_space;
  1556. }
  1557. LWS_VISIBLE int
  1558. lwsl_timestamp(int level, char *p, int len)
  1559. {
  1560. #ifndef LWS_PLAT_OPTEE
  1561. time_t o_now = time(NULL);
  1562. unsigned long long now;
  1563. struct tm *ptm = NULL;
  1564. #ifndef WIN32
  1565. struct tm tm;
  1566. #endif
  1567. int n;
  1568. #ifndef _WIN32_WCE
  1569. #ifdef WIN32
  1570. ptm = localtime(&o_now);
  1571. #else
  1572. if (localtime_r(&o_now, &tm))
  1573. ptm = &tm;
  1574. #endif
  1575. #endif
  1576. p[0] = '\0';
  1577. for (n = 0; n < LLL_COUNT; n++) {
  1578. if (level != (1 << n))
  1579. continue;
  1580. now = time_in_microseconds() / 100;
  1581. if (ptm)
  1582. n = lws_snprintf(p, len,
  1583. "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
  1584. ptm->tm_year + 1900,
  1585. ptm->tm_mon + 1,
  1586. ptm->tm_mday,
  1587. ptm->tm_hour,
  1588. ptm->tm_min,
  1589. ptm->tm_sec,
  1590. (int)(now % 10000), log_level_names[n]);
  1591. else
  1592. n = lws_snprintf(p, len, "[%llu:%04d] %s: ",
  1593. (unsigned long long) now / 10000,
  1594. (int)(now % 10000), log_level_names[n]);
  1595. return n;
  1596. }
  1597. #else
  1598. p[0] = '\0';
  1599. #endif
  1600. return 0;
  1601. }
  1602. #ifndef LWS_PLAT_OPTEE
  1603. static const char * const colours[] = {
  1604. "[31;1m", /* LLL_ERR */
  1605. "[36;1m", /* LLL_WARN */
  1606. "[35;1m", /* LLL_NOTICE */
  1607. "[32;1m", /* LLL_INFO */
  1608. "[34;1m", /* LLL_DEBUG */
  1609. "[33;1m", /* LLL_PARSER */
  1610. "[33m", /* LLL_HEADER */
  1611. "[33m", /* LLL_EXT */
  1612. "[33m", /* LLL_CLIENT */
  1613. "[33;1m", /* LLL_LATENCY */
  1614. "[30;1m", /* LLL_USER */
  1615. };
  1616. LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
  1617. {
  1618. char buf[50];
  1619. static char tty = 3;
  1620. int n, m = LWS_ARRAY_SIZE(colours) - 1;
  1621. if (!tty)
  1622. tty = isatty(2) | 2;
  1623. lwsl_timestamp(level, buf, sizeof(buf));
  1624. if (tty == 3) {
  1625. n = 1 << (LWS_ARRAY_SIZE(colours) - 1);
  1626. while (n) {
  1627. if (level & n)
  1628. break;
  1629. m--;
  1630. n >>= 1;
  1631. }
  1632. fprintf(stderr, "%c%s%s%s%c[0m", 27, colours[m], buf, line, 27);
  1633. } else
  1634. fprintf(stderr, "%s%s", buf, line);
  1635. }
  1636. #endif
  1637. LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
  1638. {
  1639. char buf[256];
  1640. int n;
  1641. if (!(log_level & filter))
  1642. return;
  1643. n = vsnprintf(buf, sizeof(buf) - 1, format, vl);
  1644. (void)n;
  1645. /* vnsprintf returns what it would have written, even if truncated */
  1646. if (n > (int)sizeof(buf) - 1)
  1647. n = sizeof(buf) - 1;
  1648. if (n > 0)
  1649. buf[n] = '\0';
  1650. lwsl_emit(filter, buf);
  1651. }
  1652. LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
  1653. {
  1654. va_list ap;
  1655. va_start(ap, format);
  1656. _lws_logv(filter, format, ap);
  1657. va_end(ap);
  1658. }
  1659. LWS_VISIBLE void lws_set_log_level(int level,
  1660. void (*func)(int level, const char *line))
  1661. {
  1662. log_level = level;
  1663. if (func)
  1664. lwsl_emit = func;
  1665. }
  1666. LWS_VISIBLE int lwsl_visible(int level)
  1667. {
  1668. return log_level & level;
  1669. }
  1670. LWS_VISIBLE void
  1671. lwsl_hexdump_level(int hexdump_level, const void *vbuf, size_t len)
  1672. {
  1673. unsigned char *buf = (unsigned char *)vbuf;
  1674. unsigned int n, m, start;
  1675. char line[80];
  1676. char *p;
  1677. if (!lwsl_visible(hexdump_level))
  1678. return;
  1679. if (!len)
  1680. return;
  1681. if (!vbuf)
  1682. return;
  1683. _lws_log(hexdump_level, "\n");
  1684. for (n = 0; n < len;) {
  1685. start = n;
  1686. p = line;
  1687. p += sprintf(p, "%04X: ", start);
  1688. for (m = 0; m < 16 && n < len; m++)
  1689. p += sprintf(p, "%02X ", buf[n++]);
  1690. while (m++ < 16)
  1691. p += sprintf(p, " ");
  1692. p += sprintf(p, " ");
  1693. for (m = 0; m < 16 && (start + m) < len; m++) {
  1694. if (buf[start + m] >= ' ' && buf[start + m] < 127)
  1695. *p++ = buf[start + m];
  1696. else
  1697. *p++ = '.';
  1698. }
  1699. while (m++ < 16)
  1700. *p++ = ' ';
  1701. *p++ = '\n';
  1702. *p = '\0';
  1703. _lws_log(hexdump_level, "%s", line);
  1704. (void)line;
  1705. }
  1706. _lws_log(hexdump_level, "\n");
  1707. }
  1708. LWS_VISIBLE void
  1709. lwsl_hexdump(const void *vbuf, size_t len)
  1710. {
  1711. #if defined(_DEBUG)
  1712. lwsl_hexdump_level(LLL_DEBUG, vbuf, len);
  1713. #endif
  1714. }
  1715. LWS_VISIBLE int
  1716. lws_is_ssl(struct lws *wsi)
  1717. {
  1718. #if defined(LWS_WITH_TLS)
  1719. return wsi->tls.use_ssl & LCCSCF_USE_SSL;
  1720. #else
  1721. (void)wsi;
  1722. return 0;
  1723. #endif
  1724. }
  1725. #if defined(LWS_WITH_TLS) && !defined(LWS_WITH_MBEDTLS)
  1726. LWS_VISIBLE lws_tls_conn*
  1727. lws_get_ssl(struct lws *wsi)
  1728. {
  1729. return wsi->tls.ssl;
  1730. }
  1731. #endif
  1732. LWS_VISIBLE int
  1733. lws_partial_buffered(struct lws *wsi)
  1734. {
  1735. return !!wsi->trunc_len;
  1736. }
  1737. LWS_VISIBLE lws_fileofs_t
  1738. lws_get_peer_write_allowance(struct lws *wsi)
  1739. {
  1740. if (!wsi->role_ops->tx_credit)
  1741. return -1;
  1742. return wsi->role_ops->tx_credit(wsi);
  1743. }
  1744. LWS_VISIBLE void
  1745. lws_role_transition(struct lws *wsi, enum lwsi_role role, enum lwsi_state state,
  1746. struct lws_role_ops *ops)
  1747. {
  1748. #if defined(_DEBUG)
  1749. const char *name = "(unset)";
  1750. #endif
  1751. wsi->wsistate = role | state;
  1752. if (ops)
  1753. wsi->role_ops = ops;
  1754. #if defined(_DEBUG)
  1755. if (wsi->role_ops)
  1756. name = wsi->role_ops->name;
  1757. lwsl_debug("%s: %p: wsistate 0x%x, ops %s\n", __func__, wsi,
  1758. wsi->wsistate, name);
  1759. #endif
  1760. }
  1761. LWS_VISIBLE struct lws_plat_file_ops *
  1762. lws_get_fops(struct lws_context *context)
  1763. {
  1764. return (struct lws_plat_file_ops *)context->fops;
  1765. }
  1766. LWS_VISIBLE LWS_EXTERN struct lws_context *
  1767. lws_get_context(const struct lws *wsi)
  1768. {
  1769. return wsi->context;
  1770. }
  1771. LWS_VISIBLE LWS_EXTERN int
  1772. lws_get_count_threads(struct lws_context *context)
  1773. {
  1774. return context->count_threads;
  1775. }
  1776. LWS_VISIBLE LWS_EXTERN void *
  1777. lws_wsi_user(struct lws *wsi)
  1778. {
  1779. return wsi->user_space;
  1780. }
  1781. LWS_VISIBLE LWS_EXTERN void
  1782. lws_set_wsi_user(struct lws *wsi, void *data)
  1783. {
  1784. if (wsi->user_space_externally_allocated)
  1785. wsi->user_space = data;
  1786. else
  1787. lwsl_err("%s: Cannot set internally-allocated user_space\n",
  1788. __func__);
  1789. }
  1790. LWS_VISIBLE LWS_EXTERN struct lws *
  1791. lws_get_parent(const struct lws *wsi)
  1792. {
  1793. return wsi->parent;
  1794. }
  1795. LWS_VISIBLE LWS_EXTERN struct lws *
  1796. lws_get_child(const struct lws *wsi)
  1797. {
  1798. return wsi->child_list;
  1799. }
  1800. LWS_VISIBLE LWS_EXTERN void
  1801. lws_set_parent_carries_io(struct lws *wsi)
  1802. {
  1803. wsi->parent_carries_io = 1;
  1804. }
  1805. LWS_VISIBLE LWS_EXTERN void *
  1806. lws_get_opaque_parent_data(const struct lws *wsi)
  1807. {
  1808. return wsi->opaque_parent_data;
  1809. }
  1810. LWS_VISIBLE LWS_EXTERN void
  1811. lws_set_opaque_parent_data(struct lws *wsi, void *data)
  1812. {
  1813. wsi->opaque_parent_data = data;
  1814. }
  1815. LWS_VISIBLE LWS_EXTERN int
  1816. lws_get_child_pending_on_writable(const struct lws *wsi)
  1817. {
  1818. return wsi->parent_pending_cb_on_writable;
  1819. }
  1820. LWS_VISIBLE LWS_EXTERN void
  1821. lws_clear_child_pending_on_writable(struct lws *wsi)
  1822. {
  1823. wsi->parent_pending_cb_on_writable = 0;
  1824. }
  1825. LWS_EXTERN int
  1826. __lws_rx_flow_control(struct lws *wsi)
  1827. {
  1828. struct lws *wsic = wsi->child_list;
  1829. // h2 ignores rx flow control atm
  1830. if (lwsi_role_h2(wsi) || wsi->http2_substream ||
  1831. lwsi_role_h2_ENCAPSULATION(wsi))
  1832. return 0; // !!!
  1833. /* if he has children, do those if they were changed */
  1834. while (wsic) {
  1835. if (wsic->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)
  1836. __lws_rx_flow_control(wsic);
  1837. wsic = wsic->sibling_list;
  1838. }
  1839. /* there is no pending change */
  1840. if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
  1841. return 0;
  1842. /* stuff is still buffered, not ready to really accept new input */
  1843. if (lws_buflist_next_segment_len(&wsi->buflist, NULL)) {
  1844. /* get ourselves called back to deal with stashed buffer */
  1845. lws_callback_on_writable(wsi);
  1846. return 0;
  1847. }
  1848. /* now the pending is cleared, we can change rxflow state */
  1849. wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
  1850. lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
  1851. wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
  1852. /* adjust the pollfd for this wsi */
  1853. if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
  1854. if (__lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
  1855. lwsl_info("%s: fail\n", __func__);
  1856. return -1;
  1857. }
  1858. } else
  1859. if (__lws_change_pollfd(wsi, LWS_POLLIN, 0))
  1860. return -1;
  1861. return 0;
  1862. }
  1863. LWS_EXTERN int
  1864. lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
  1865. {
  1866. static const unsigned char e0f4[] = {
  1867. 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
  1868. 0x80 | ((4 - 1) << 2) | 1, /* e1 */
  1869. 0x80 | ((4 - 1) << 2) | 1, /* e2 */
  1870. 0x80 | ((4 - 1) << 2) | 1, /* e3 */
  1871. 0x80 | ((4 - 1) << 2) | 1, /* e4 */
  1872. 0x80 | ((4 - 1) << 2) | 1, /* e5 */
  1873. 0x80 | ((4 - 1) << 2) | 1, /* e6 */
  1874. 0x80 | ((4 - 1) << 2) | 1, /* e7 */
  1875. 0x80 | ((4 - 1) << 2) | 1, /* e8 */
  1876. 0x80 | ((4 - 1) << 2) | 1, /* e9 */
  1877. 0x80 | ((4 - 1) << 2) | 1, /* ea */
  1878. 0x80 | ((4 - 1) << 2) | 1, /* eb */
  1879. 0x80 | ((4 - 1) << 2) | 1, /* ec */
  1880. 0x80 | ((2 - 1) << 2) | 1, /* ed */
  1881. 0x80 | ((4 - 1) << 2) | 1, /* ee */
  1882. 0x80 | ((4 - 1) << 2) | 1, /* ef */
  1883. 0x90 | ((3 - 1) << 2) | 2, /* f0 */
  1884. 0x80 | ((4 - 1) << 2) | 2, /* f1 */
  1885. 0x80 | ((4 - 1) << 2) | 2, /* f2 */
  1886. 0x80 | ((4 - 1) << 2) | 2, /* f3 */
  1887. 0x80 | ((1 - 1) << 2) | 2, /* f4 */
  1888. 0, /* s0 */
  1889. 0x80 | ((4 - 1) << 2) | 0, /* s2 */
  1890. 0x80 | ((4 - 1) << 2) | 1, /* s3 */
  1891. };
  1892. unsigned char s = *state;
  1893. while (len--) {
  1894. unsigned char c = *buf++;
  1895. if (!s) {
  1896. if (c >= 0x80) {
  1897. if (c < 0xc2 || c > 0xf4)
  1898. return 1;
  1899. if (c < 0xe0)
  1900. s = 0x80 | ((4 - 1) << 2);
  1901. else
  1902. s = e0f4[c - 0xe0];
  1903. }
  1904. } else {
  1905. if (c < (s & 0xf0) ||
  1906. c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
  1907. return 1;
  1908. s = e0f4[21 + (s & 3)];
  1909. }
  1910. }
  1911. *state = s;
  1912. return 0;
  1913. }
  1914. LWS_VISIBLE LWS_EXTERN int
  1915. lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
  1916. const char **path)
  1917. {
  1918. const char *end;
  1919. static const char *slash = "/";
  1920. /* cut up the location into address, port and path */
  1921. *prot = p;
  1922. while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
  1923. p++;
  1924. if (!*p) {
  1925. end = p;
  1926. p = (char *)*prot;
  1927. *prot = end;
  1928. } else {
  1929. *p = '\0';
  1930. p += 3;
  1931. }
  1932. *ads = p;
  1933. if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
  1934. *port = 80;
  1935. else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
  1936. *port = 443;
  1937. if (*p == '[')
  1938. {
  1939. ++(*ads);
  1940. while (*p && *p != ']')
  1941. p++;
  1942. if (*p)
  1943. *p++ = '\0';
  1944. }
  1945. else
  1946. {
  1947. while (*p && *p != ':' && *p != '/')
  1948. p++;
  1949. }
  1950. if (*p == ':') {
  1951. *p++ = '\0';
  1952. *port = atoi(p);
  1953. while (*p && *p != '/')
  1954. p++;
  1955. }
  1956. *path = slash;
  1957. if (*p) {
  1958. *p++ = '\0';
  1959. if (*p)
  1960. *path = p;
  1961. }
  1962. return 0;
  1963. }
  1964. #if defined(LWS_WITHOUT_EXTENSIONS)
  1965. /* we need to provide dummy callbacks for internal exts
  1966. * so user code runs when faced with a lib compiled with
  1967. * extensions disabled.
  1968. */
  1969. LWS_VISIBLE int
  1970. lws_extension_callback_pm_deflate(struct lws_context *context,
  1971. const struct lws_extension *ext,
  1972. struct lws *wsi,
  1973. enum lws_extension_callback_reasons reason,
  1974. void *user, void *in, size_t len)
  1975. {
  1976. (void)context;
  1977. (void)ext;
  1978. (void)wsi;
  1979. (void)reason;
  1980. (void)user;
  1981. (void)in;
  1982. (void)len;
  1983. return 0;
  1984. }
  1985. LWS_EXTERN int
  1986. lws_set_extension_option(struct lws *wsi, const char *ext_name,
  1987. const char *opt_name, const char *opt_val)
  1988. {
  1989. return -1;
  1990. }
  1991. #endif
  1992. LWS_EXTERN int
  1993. lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
  1994. const char *iface)
  1995. {
  1996. #ifdef LWS_WITH_UNIX_SOCK
  1997. struct sockaddr_un serv_unix;
  1998. #endif
  1999. #ifdef LWS_WITH_IPV6
  2000. struct sockaddr_in6 serv_addr6;
  2001. #endif
  2002. struct sockaddr_in serv_addr4;
  2003. #ifndef LWS_PLAT_OPTEE
  2004. socklen_t len = sizeof(struct sockaddr_storage);
  2005. #endif
  2006. int n;
  2007. #if !defined(LWS_WITH_ESP32)
  2008. int m;
  2009. #endif
  2010. struct sockaddr_storage sin;
  2011. struct sockaddr *v;
  2012. #ifdef LWS_WITH_UNIX_SOCK
  2013. if (LWS_UNIX_SOCK_ENABLED(vhost)) {
  2014. v = (struct sockaddr *)&serv_unix;
  2015. n = sizeof(struct sockaddr_un);
  2016. bzero((char *) &serv_unix, sizeof(serv_unix));
  2017. serv_unix.sun_family = AF_UNIX;
  2018. if (!iface)
  2019. return -1;
  2020. if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
  2021. lwsl_err("\"%s\" too long for UNIX domain socket\n",
  2022. iface);
  2023. return -1;
  2024. }
  2025. strcpy(serv_unix.sun_path, iface);
  2026. if (serv_unix.sun_path[0] == '@')
  2027. serv_unix.sun_path[0] = '\0';
  2028. } else
  2029. #endif
  2030. #if defined(LWS_WITH_IPV6) && !defined(LWS_WITH_ESP32)
  2031. if (LWS_IPV6_ENABLED(vhost)) {
  2032. v = (struct sockaddr *)&serv_addr6;
  2033. n = sizeof(struct sockaddr_in6);
  2034. bzero((char *) &serv_addr6, sizeof(serv_addr6));
  2035. if (iface) {
  2036. m = interface_to_sa(vhost, iface,
  2037. (struct sockaddr_in *)v, n);
  2038. if (m == LWS_ITOSA_NOT_USABLE) {
  2039. lwsl_info("%s: netif %s: Not usable\n",
  2040. __func__, iface);
  2041. return m;
  2042. }
  2043. if (m == LWS_ITOSA_NOT_EXIST) {
  2044. lwsl_info("%s: netif %s: Does not exist\n",
  2045. __func__, iface);
  2046. return m;
  2047. }
  2048. serv_addr6.sin6_scope_id = lws_get_addr_scope(iface);
  2049. }
  2050. serv_addr6.sin6_family = AF_INET6;
  2051. serv_addr6.sin6_port = htons(port);
  2052. } else
  2053. #endif
  2054. {
  2055. v = (struct sockaddr *)&serv_addr4;
  2056. n = sizeof(serv_addr4);
  2057. bzero((char *) &serv_addr4, sizeof(serv_addr4));
  2058. serv_addr4.sin_addr.s_addr = INADDR_ANY;
  2059. serv_addr4.sin_family = AF_INET;
  2060. #if !defined(LWS_WITH_ESP32)
  2061. if (iface) {
  2062. m = interface_to_sa(vhost, iface,
  2063. (struct sockaddr_in *)v, n);
  2064. if (m == LWS_ITOSA_NOT_USABLE) {
  2065. lwsl_info("%s: netif %s: Not usable\n",
  2066. __func__, iface);
  2067. return m;
  2068. }
  2069. if (m == LWS_ITOSA_NOT_EXIST) {
  2070. lwsl_info("%s: netif %s: Does not exist\n",
  2071. __func__, iface);
  2072. return m;
  2073. }
  2074. }
  2075. #endif
  2076. serv_addr4.sin_port = htons(port);
  2077. } /* ipv4 */
  2078. /* just checking for the interface extant */
  2079. if (sockfd == LWS_SOCK_INVALID)
  2080. return 0;
  2081. n = bind(sockfd, v, n);
  2082. #ifdef LWS_WITH_UNIX_SOCK
  2083. if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
  2084. lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
  2085. sockfd, iface, n, LWS_ERRNO);
  2086. return -1;
  2087. } else
  2088. #endif
  2089. if (n < 0) {
  2090. lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n",
  2091. sockfd, port, n, LWS_ERRNO);
  2092. return -1;
  2093. }
  2094. #ifndef LWS_PLAT_OPTEE
  2095. if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
  2096. lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
  2097. else
  2098. #endif
  2099. #if defined(LWS_WITH_IPV6)
  2100. port = (sin.ss_family == AF_INET6) ?
  2101. ntohs(((struct sockaddr_in6 *) &sin)->sin6_port) :
  2102. ntohs(((struct sockaddr_in *) &sin)->sin_port);
  2103. #else
  2104. {
  2105. struct sockaddr_in sain;
  2106. memcpy(&sain, &sin, sizeof(sain));
  2107. port = ntohs(sain.sin_port);
  2108. }
  2109. #endif
  2110. return port;
  2111. }
  2112. LWS_VISIBLE LWS_EXTERN int
  2113. lws_get_vhost_listen_port(struct lws_vhost *vhost)
  2114. {
  2115. return vhost->listen_port;
  2116. }
  2117. #if defined(LWS_WITH_IPV6)
  2118. LWS_EXTERN unsigned long
  2119. lws_get_addr_scope(const char *ipaddr)
  2120. {
  2121. unsigned long scope = 0;
  2122. #ifndef WIN32
  2123. struct ifaddrs *addrs, *addr;
  2124. char ip[NI_MAXHOST];
  2125. unsigned int i;
  2126. getifaddrs(&addrs);
  2127. for (addr = addrs; addr; addr = addr->ifa_next) {
  2128. if (!addr->ifa_addr ||
  2129. addr->ifa_addr->sa_family != AF_INET6)
  2130. continue;
  2131. getnameinfo(addr->ifa_addr,
  2132. sizeof(struct sockaddr_in6),
  2133. ip, sizeof(ip),
  2134. NULL, 0, NI_NUMERICHOST);
  2135. i = 0;
  2136. while (ip[i])
  2137. if (ip[i++] == '%') {
  2138. ip[i - 1] = '\0';
  2139. break;
  2140. }
  2141. if (!strcmp(ip, ipaddr)) {
  2142. scope = if_nametoindex(addr->ifa_name);
  2143. break;
  2144. }
  2145. }
  2146. freeifaddrs(addrs);
  2147. #else
  2148. PIP_ADAPTER_ADDRESSES adapter, addrs = NULL;
  2149. PIP_ADAPTER_UNICAST_ADDRESS addr;
  2150. ULONG size = 0;
  2151. DWORD ret;
  2152. struct sockaddr_in6 *sockaddr;
  2153. char ip[NI_MAXHOST];
  2154. unsigned int i;
  2155. int found = 0;
  2156. for (i = 0; i < 5; i++)
  2157. {
  2158. ret = GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX,
  2159. NULL, addrs, &size);
  2160. if ((ret == NO_ERROR) || (ret == ERROR_NO_DATA)) {
  2161. break;
  2162. } else if (ret == ERROR_BUFFER_OVERFLOW)
  2163. {
  2164. if (addrs)
  2165. free(addrs);
  2166. addrs = (IP_ADAPTER_ADDRESSES *)malloc(size);
  2167. } else
  2168. {
  2169. if (addrs)
  2170. {
  2171. free(addrs);
  2172. addrs = NULL;
  2173. }
  2174. lwsl_err("Failed to get IPv6 address table (%d)", ret);
  2175. break;
  2176. }
  2177. }
  2178. if ((ret == NO_ERROR) && (addrs)) {
  2179. adapter = addrs;
  2180. while (adapter && !found) {
  2181. addr = adapter->FirstUnicastAddress;
  2182. while (addr && !found) {
  2183. if (addr->Address.lpSockaddr->sa_family ==
  2184. AF_INET6) {
  2185. sockaddr = (struct sockaddr_in6 *)
  2186. (addr->Address.lpSockaddr);
  2187. lws_plat_inet_ntop(sockaddr->sin6_family,
  2188. &sockaddr->sin6_addr,
  2189. ip, sizeof(ip));
  2190. if (!strcmp(ip, ipaddr)) {
  2191. scope = sockaddr->sin6_scope_id;
  2192. found = 1;
  2193. break;
  2194. }
  2195. }
  2196. addr = addr->Next;
  2197. }
  2198. adapter = adapter->Next;
  2199. }
  2200. }
  2201. if (addrs)
  2202. free(addrs);
  2203. #endif
  2204. return scope;
  2205. }
  2206. #endif
  2207. #if !defined(LWS_NO_SERVER)
  2208. LWS_EXTERN struct lws *
  2209. lws_create_adopt_udp(struct lws_vhost *vhost, int port, int flags,
  2210. const char *protocol_name, struct lws *parent_wsi)
  2211. {
  2212. lws_sock_file_fd_type sock;
  2213. struct addrinfo h, *r, *rp;
  2214. struct lws *wsi = NULL;
  2215. char buf[16];
  2216. int n;
  2217. memset(&h, 0, sizeof(h));
  2218. h.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  2219. h.ai_socktype = SOCK_DGRAM;
  2220. h.ai_protocol = IPPROTO_UDP;
  2221. h.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  2222. lws_snprintf(buf, sizeof(buf), "%u", port);
  2223. n = getaddrinfo(NULL, buf, &h, &r);
  2224. if (n) {
  2225. lwsl_info("%s: getaddrinfo error: %s\n", __func__,
  2226. gai_strerror(n));
  2227. goto bail;
  2228. }
  2229. for (rp = r; rp; rp = rp->ai_next) {
  2230. sock.sockfd = socket(rp->ai_family, rp->ai_socktype,
  2231. rp->ai_protocol);
  2232. if (sock.sockfd >= 0)
  2233. break;
  2234. }
  2235. if (!rp) {
  2236. lwsl_err("%s: unable to create INET socket\n", __func__);
  2237. goto bail1;
  2238. }
  2239. if ((flags & LWS_CAUDP_BIND) && bind(sock.sockfd, rp->ai_addr,
  2240. #if defined(_WIN32)
  2241. (int)rp->ai_addrlen
  2242. #else
  2243. rp->ai_addrlen
  2244. #endif
  2245. ) == -1) {
  2246. lwsl_err("%s: bind failed\n", __func__);
  2247. goto bail2;
  2248. }
  2249. wsi = lws_adopt_descriptor_vhost(vhost, LWS_ADOPT_RAW_SOCKET_UDP, sock,
  2250. protocol_name, parent_wsi);
  2251. if (!wsi)
  2252. lwsl_err("%s: udp adoption failed\n", __func__);
  2253. bail2:
  2254. if (!wsi)
  2255. close((int)sock.sockfd);
  2256. bail1:
  2257. freeaddrinfo(r);
  2258. bail:
  2259. return wsi;
  2260. }
  2261. #endif
  2262. static const char *hex = "0123456789ABCDEF";
  2263. LWS_VISIBLE LWS_EXTERN const char *
  2264. lws_sql_purify(char *escaped, const char *string, int len)
  2265. {
  2266. const char *p = string;
  2267. char *q = escaped;
  2268. while (*p && len-- > 2) {
  2269. if (*p == '\'') {
  2270. *q++ = '\'';
  2271. *q++ = '\'';
  2272. len --;
  2273. p++;
  2274. } else
  2275. *q++ = *p++;
  2276. }
  2277. *q = '\0';
  2278. return escaped;
  2279. }
  2280. LWS_VISIBLE LWS_EXTERN const char *
  2281. lws_json_purify(char *escaped, const char *string, int len)
  2282. {
  2283. const char *p = string;
  2284. char *q = escaped;
  2285. if (!p) {
  2286. escaped[0] = '\0';
  2287. return escaped;
  2288. }
  2289. while (*p && len-- > 6) {
  2290. if (*p == '\"' || *p == '\\' || *p < 0x20) {
  2291. *q++ = '\\';
  2292. *q++ = 'u';
  2293. *q++ = '0';
  2294. *q++ = '0';
  2295. *q++ = hex[((*p) >> 4) & 15];
  2296. *q++ = hex[(*p) & 15];
  2297. len -= 5;
  2298. p++;
  2299. } else
  2300. *q++ = *p++;
  2301. }
  2302. *q = '\0';
  2303. return escaped;
  2304. }
  2305. LWS_VISIBLE LWS_EXTERN void
  2306. lws_filename_purify_inplace(char *filename)
  2307. {
  2308. while (*filename) {
  2309. if (*filename == '.' && filename[1] == '.') {
  2310. *filename = '_';
  2311. filename[1] = '_';
  2312. }
  2313. if (*filename == ':' ||
  2314. *filename == '/' ||
  2315. *filename == '\\' ||
  2316. *filename == '$' ||
  2317. *filename == '%')
  2318. *filename = '_';
  2319. filename++;
  2320. }
  2321. }
  2322. LWS_VISIBLE LWS_EXTERN const char *
  2323. lws_urlencode(char *escaped, const char *string, int len)
  2324. {
  2325. const char *p = string;
  2326. char *q = escaped;
  2327. while (*p && len-- > 3) {
  2328. if (*p == ' ') {
  2329. *q++ = '+';
  2330. p++;
  2331. continue;
  2332. }
  2333. if ((*p >= '0' && *p <= '9') ||
  2334. (*p >= 'A' && *p <= 'Z') ||
  2335. (*p >= 'a' && *p <= 'z')) {
  2336. *q++ = *p++;
  2337. continue;
  2338. }
  2339. *q++ = '%';
  2340. *q++ = hex[(*p >> 4) & 0xf];
  2341. *q++ = hex[*p & 0xf];
  2342. len -= 2;
  2343. p++;
  2344. }
  2345. *q = '\0';
  2346. return escaped;
  2347. }
  2348. LWS_VISIBLE LWS_EXTERN int
  2349. lws_urldecode(char *string, const char *escaped, int len)
  2350. {
  2351. int state = 0, n;
  2352. char sum = 0;
  2353. while (*escaped && len) {
  2354. switch (state) {
  2355. case 0:
  2356. if (*escaped == '%') {
  2357. state++;
  2358. escaped++;
  2359. continue;
  2360. }
  2361. if (*escaped == '+') {
  2362. escaped++;
  2363. *string++ = ' ';
  2364. len--;
  2365. continue;
  2366. }
  2367. *string++ = *escaped++;
  2368. len--;
  2369. break;
  2370. case 1:
  2371. n = char_to_hex(*escaped);
  2372. if (n < 0)
  2373. return -1;
  2374. escaped++;
  2375. sum = n << 4;
  2376. state++;
  2377. break;
  2378. case 2:
  2379. n = char_to_hex(*escaped);
  2380. if (n < 0)
  2381. return -1;
  2382. escaped++;
  2383. *string++ = sum | n;
  2384. len--;
  2385. state = 0;
  2386. break;
  2387. }
  2388. }
  2389. *string = '\0';
  2390. return 0;
  2391. }
  2392. LWS_VISIBLE LWS_EXTERN int
  2393. lws_finalize_startup(struct lws_context *context)
  2394. {
  2395. struct lws_context_creation_info info;
  2396. info.uid = context->uid;
  2397. info.gid = context->gid;
  2398. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  2399. memcpy(info.caps, context->caps, sizeof(info.caps));
  2400. info.count_caps = context->count_caps;
  2401. #endif
  2402. if (lws_check_opt(context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
  2403. lws_plat_drop_app_privileges(&info);
  2404. return 0;
  2405. }
  2406. int
  2407. lws_snprintf(char *str, size_t size, const char *format, ...)
  2408. {
  2409. va_list ap;
  2410. int n;
  2411. if (!size)
  2412. return 0;
  2413. va_start(ap, format);
  2414. n = vsnprintf(str, size, format, ap);
  2415. va_end(ap);
  2416. if (n >= (int)size)
  2417. return (int)size;
  2418. return n;
  2419. }
  2420. char *
  2421. lws_strncpy(char *dest, const char *src, size_t size)
  2422. {
  2423. strncpy(dest, src, size - 1);
  2424. dest[size - 1] = '\0';
  2425. return dest;
  2426. }
  2427. LWS_VISIBLE LWS_EXTERN int
  2428. lws_is_cgi(struct lws *wsi) {
  2429. #ifdef LWS_WITH_CGI
  2430. return !!wsi->http.cgi;
  2431. #else
  2432. return 0;
  2433. #endif
  2434. }
  2435. const struct lws_protocol_vhost_options *
  2436. lws_pvo_search(const struct lws_protocol_vhost_options *pvo, const char *name)
  2437. {
  2438. while (pvo) {
  2439. if (!strcmp(pvo->name, name))
  2440. break;
  2441. pvo = pvo->next;
  2442. }
  2443. return pvo;
  2444. }
  2445. void
  2446. lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs)
  2447. {
  2448. const struct lws_vhost *vh = ctx->vhost_list;
  2449. while (vh) {
  2450. cs->rx += vh->conn_stats.rx;
  2451. cs->tx += vh->conn_stats.tx;
  2452. cs->h1_conn += vh->conn_stats.h1_conn;
  2453. cs->h1_trans += vh->conn_stats.h1_trans;
  2454. cs->h2_trans += vh->conn_stats.h2_trans;
  2455. cs->ws_upg += vh->conn_stats.ws_upg;
  2456. cs->h2_upg += vh->conn_stats.h2_upg;
  2457. cs->h2_alpn += vh->conn_stats.h2_alpn;
  2458. cs->h2_subs += vh->conn_stats.h2_subs;
  2459. cs->rejected += vh->conn_stats.rejected;
  2460. vh = vh->vhost_next;
  2461. }
  2462. }
  2463. const char *
  2464. lws_cmdline_option(int argc, const char **argv, const char *val)
  2465. {
  2466. int n = (int)strlen(val), c = argc;
  2467. while (--c > 0) {
  2468. if (!strncmp(argv[c], val, n)) {
  2469. if (!*(argv[c] + n) && c < argc - 1) {
  2470. /* coverity treats unchecked argv as "tainted" */
  2471. if (!argv[c + 1] || strlen(argv[c + 1]) > 1024)
  2472. return NULL;
  2473. return argv[c + 1];
  2474. }
  2475. return argv[c] + n;
  2476. }
  2477. }
  2478. return NULL;
  2479. }
  2480. #ifdef LWS_WITH_SERVER_STATUS
  2481. LWS_EXTERN int
  2482. lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
  2483. {
  2484. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  2485. static const char * const prots[] = {
  2486. "http://",
  2487. "https://",
  2488. "file://",
  2489. "cgi://",
  2490. ">http://",
  2491. ">https://",
  2492. "callback://"
  2493. };
  2494. #endif
  2495. char *orig = buf, *end = buf + len - 1, first = 1;
  2496. int n = 0;
  2497. if (len < 100)
  2498. return 0;
  2499. buf += lws_snprintf(buf, end - buf,
  2500. "{\n \"name\":\"%s\",\n"
  2501. " \"port\":\"%d\",\n"
  2502. " \"use_ssl\":\"%d\",\n"
  2503. " \"sts\":\"%d\",\n"
  2504. " \"rx\":\"%llu\",\n"
  2505. " \"tx\":\"%llu\",\n"
  2506. " \"h1_conn\":\"%lu\",\n"
  2507. " \"h1_trans\":\"%lu\",\n"
  2508. " \"h2_trans\":\"%lu\",\n"
  2509. " \"ws_upg\":\"%lu\",\n"
  2510. " \"rejected\":\"%lu\",\n"
  2511. " \"h2_upg\":\"%lu\",\n"
  2512. " \"h2_alpn\":\"%lu\",\n"
  2513. " \"h2_subs\":\"%lu\""
  2514. ,
  2515. vh->name, vh->listen_port,
  2516. #if defined(LWS_WITH_TLS)
  2517. vh->tls.use_ssl & LCCSCF_USE_SSL,
  2518. #else
  2519. 0,
  2520. #endif
  2521. !!(vh->options & LWS_SERVER_OPTION_STS),
  2522. vh->conn_stats.rx, vh->conn_stats.tx,
  2523. vh->conn_stats.h1_conn,
  2524. vh->conn_stats.h1_trans,
  2525. vh->conn_stats.h2_trans,
  2526. vh->conn_stats.ws_upg,
  2527. vh->conn_stats.rejected,
  2528. vh->conn_stats.h2_upg,
  2529. vh->conn_stats.h2_alpn,
  2530. vh->conn_stats.h2_subs
  2531. );
  2532. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  2533. if (vh->http.mount_list) {
  2534. const struct lws_http_mount *m = vh->http.mount_list;
  2535. buf += lws_snprintf(buf, end - buf, ",\n \"mounts\":[");
  2536. while (m) {
  2537. if (!first)
  2538. buf += lws_snprintf(buf, end - buf, ",");
  2539. buf += lws_snprintf(buf, end - buf,
  2540. "\n {\n \"mountpoint\":\"%s\",\n"
  2541. " \"origin\":\"%s%s\",\n"
  2542. " \"cache_max_age\":\"%d\",\n"
  2543. " \"cache_reuse\":\"%d\",\n"
  2544. " \"cache_revalidate\":\"%d\",\n"
  2545. " \"cache_intermediaries\":\"%d\"\n"
  2546. ,
  2547. m->mountpoint,
  2548. prots[m->origin_protocol],
  2549. m->origin,
  2550. m->cache_max_age,
  2551. m->cache_reusable,
  2552. m->cache_revalidate,
  2553. m->cache_intermediaries);
  2554. if (m->def)
  2555. buf += lws_snprintf(buf, end - buf,
  2556. ",\n \"default\":\"%s\"",
  2557. m->def);
  2558. buf += lws_snprintf(buf, end - buf, "\n }");
  2559. first = 0;
  2560. m = m->mount_next;
  2561. }
  2562. buf += lws_snprintf(buf, end - buf, "\n ]");
  2563. }
  2564. #endif
  2565. if (vh->protocols) {
  2566. n = 0;
  2567. first = 1;
  2568. buf += lws_snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
  2569. while (n < vh->count_protocols) {
  2570. if (!first)
  2571. buf += lws_snprintf(buf, end - buf, ",");
  2572. buf += lws_snprintf(buf, end - buf,
  2573. "\n {\n \"%s\":{\n"
  2574. " \"status\":\"ok\"\n }\n }"
  2575. ,
  2576. vh->protocols[n].name);
  2577. first = 0;
  2578. n++;
  2579. }
  2580. buf += lws_snprintf(buf, end - buf, "\n ]");
  2581. }
  2582. buf += lws_snprintf(buf, end - buf, "\n}");
  2583. return buf - orig;
  2584. }
  2585. LWS_EXTERN LWS_VISIBLE int
  2586. lws_json_dump_context(const struct lws_context *context, char *buf, int len,
  2587. int hide_vhosts)
  2588. {
  2589. char *orig = buf, *end = buf + len - 1, first = 1;
  2590. const struct lws_vhost *vh = context->vhost_list;
  2591. const struct lws_context_per_thread *pt;
  2592. time_t t = time(NULL);
  2593. int n, listening = 0, cgi_count = 0;
  2594. struct lws_conn_stats cs;
  2595. double d = 0;
  2596. #ifdef LWS_WITH_CGI
  2597. struct lws_cgi * const *pcgi;
  2598. #endif
  2599. #ifdef LWS_WITH_LIBUV
  2600. uv_uptime(&d);
  2601. #endif
  2602. buf += lws_snprintf(buf, end - buf, "{ "
  2603. "\"version\":\"%s\",\n"
  2604. "\"uptime\":\"%ld\",\n",
  2605. lws_get_library_version(),
  2606. (long)d);
  2607. #ifdef LWS_HAVE_GETLOADAVG
  2608. {
  2609. double d[3];
  2610. int m;
  2611. m = getloadavg(d, 3);
  2612. for (n = 0; n < m; n++) {
  2613. buf += lws_snprintf(buf, end - buf,
  2614. "\"l%d\":\"%.2f\",\n",
  2615. n + 1, d[n]);
  2616. }
  2617. }
  2618. #endif
  2619. buf += lws_snprintf(buf, end - buf, "\"contexts\":[\n");
  2620. buf += lws_snprintf(buf, end - buf, "{ "
  2621. "\"context_uptime\":\"%ld\",\n"
  2622. "\"cgi_spawned\":\"%d\",\n"
  2623. "\"pt_fd_max\":\"%d\",\n"
  2624. "\"ah_pool_max\":\"%d\",\n"
  2625. "\"deprecated\":\"%d\",\n"
  2626. "\"wsi_alive\":\"%d\",\n",
  2627. (unsigned long)(t - context->time_up),
  2628. context->count_cgi_spawned,
  2629. context->fd_limit_per_thread,
  2630. context->max_http_header_pool,
  2631. context->deprecated,
  2632. context->count_wsi_allocated);
  2633. buf += lws_snprintf(buf, end - buf, "\"pt\":[\n ");
  2634. for (n = 0; n < context->count_threads; n++) {
  2635. pt = &context->pt[n];
  2636. if (n)
  2637. buf += lws_snprintf(buf, end - buf, ",");
  2638. buf += lws_snprintf(buf, end - buf,
  2639. "\n {\n"
  2640. " \"fds_count\":\"%d\",\n"
  2641. " \"ah_pool_inuse\":\"%d\",\n"
  2642. " \"ah_wait_list\":\"%d\"\n"
  2643. " }",
  2644. pt->fds_count,
  2645. pt->http.ah_count_in_use,
  2646. pt->http.ah_wait_list_length);
  2647. }
  2648. buf += lws_snprintf(buf, end - buf, "]");
  2649. buf += lws_snprintf(buf, end - buf, ", \"vhosts\":[\n ");
  2650. first = 1;
  2651. vh = context->vhost_list;
  2652. listening = 0;
  2653. cs = context->conn_stats;
  2654. lws_sum_stats(context, &cs);
  2655. while (vh) {
  2656. if (!hide_vhosts) {
  2657. if (!first)
  2658. if(buf != end)
  2659. *buf++ = ',';
  2660. buf += lws_json_dump_vhost(vh, buf, end - buf);
  2661. first = 0;
  2662. }
  2663. if (vh->lserv_wsi)
  2664. listening++;
  2665. vh = vh->vhost_next;
  2666. }
  2667. buf += lws_snprintf(buf, end - buf,
  2668. "],\n\"listen_wsi\":\"%d\",\n"
  2669. " \"rx\":\"%llu\",\n"
  2670. " \"tx\":\"%llu\",\n"
  2671. " \"h1_conn\":\"%lu\",\n"
  2672. " \"h1_trans\":\"%lu\",\n"
  2673. " \"h2_trans\":\"%lu\",\n"
  2674. " \"ws_upg\":\"%lu\",\n"
  2675. " \"rejected\":\"%lu\",\n"
  2676. " \"h2_alpn\":\"%lu\",\n"
  2677. " \"h2_subs\":\"%lu\",\n"
  2678. " \"h2_upg\":\"%lu\"",
  2679. listening, cs.rx, cs.tx,
  2680. cs.h1_conn,
  2681. cs.h1_trans,
  2682. cs.h2_trans,
  2683. cs.ws_upg,
  2684. cs.rejected,
  2685. cs.h2_alpn,
  2686. cs.h2_subs,
  2687. cs.h2_upg);
  2688. #ifdef LWS_WITH_CGI
  2689. for (n = 0; n < context->count_threads; n++) {
  2690. pt = &context->pt[n];
  2691. pcgi = &pt->http.cgi_list;
  2692. while (*pcgi) {
  2693. pcgi = &(*pcgi)->cgi_list;
  2694. cgi_count++;
  2695. }
  2696. }
  2697. #endif
  2698. buf += lws_snprintf(buf, end - buf, ",\n \"cgi_alive\":\"%d\"\n ",
  2699. cgi_count);
  2700. buf += lws_snprintf(buf, end - buf, "}");
  2701. buf += lws_snprintf(buf, end - buf, "]}\n ");
  2702. return buf - orig;
  2703. }
  2704. #endif
  2705. #if defined(LWS_WITH_STATS)
  2706. LWS_VISIBLE LWS_EXTERN uint64_t
  2707. lws_stats_get(struct lws_context *context, int index)
  2708. {
  2709. if (index >= LWSSTATS_SIZE)
  2710. return 0;
  2711. return context->lws_stats[index];
  2712. }
  2713. LWS_VISIBLE LWS_EXTERN void
  2714. lws_stats_log_dump(struct lws_context *context)
  2715. {
  2716. struct lws_vhost *v = context->vhost_list;
  2717. int n, m;
  2718. (void)m;
  2719. if (!context->updated)
  2720. return;
  2721. context->updated = 0;
  2722. lwsl_notice("\n");
  2723. lwsl_notice("LWS internal statistics dump ----->\n");
  2724. lwsl_notice("LWSSTATS_C_CONNECTIONS: %8llu\n",
  2725. (unsigned long long)lws_stats_get(context,
  2726. LWSSTATS_C_CONNECTIONS));
  2727. lwsl_notice("LWSSTATS_C_API_CLOSE: %8llu\n",
  2728. (unsigned long long)lws_stats_get(context,
  2729. LWSSTATS_C_API_CLOSE));
  2730. lwsl_notice("LWSSTATS_C_API_READ: %8llu\n",
  2731. (unsigned long long)lws_stats_get(context,
  2732. LWSSTATS_C_API_READ));
  2733. lwsl_notice("LWSSTATS_C_API_LWS_WRITE: %8llu\n",
  2734. (unsigned long long)lws_stats_get(context,
  2735. LWSSTATS_C_API_LWS_WRITE));
  2736. lwsl_notice("LWSSTATS_C_API_WRITE: %8llu\n",
  2737. (unsigned long long)lws_stats_get(context,
  2738. LWSSTATS_C_API_WRITE));
  2739. lwsl_notice("LWSSTATS_C_WRITE_PARTIALS: %8llu\n",
  2740. (unsigned long long)lws_stats_get(context,
  2741. LWSSTATS_C_WRITE_PARTIALS));
  2742. lwsl_notice("LWSSTATS_C_WRITEABLE_CB_REQ: %8llu\n",
  2743. (unsigned long long)lws_stats_get(context,
  2744. LWSSTATS_C_WRITEABLE_CB_REQ));
  2745. lwsl_notice("LWSSTATS_C_WRITEABLE_CB_EFF_REQ: %8llu\n",
  2746. (unsigned long long)lws_stats_get(context,
  2747. LWSSTATS_C_WRITEABLE_CB_EFF_REQ));
  2748. lwsl_notice("LWSSTATS_C_WRITEABLE_CB: %8llu\n",
  2749. (unsigned long long)lws_stats_get(context,
  2750. LWSSTATS_C_WRITEABLE_CB));
  2751. lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_ACCEPT_SPIN: %8llu\n",
  2752. (unsigned long long)lws_stats_get(context,
  2753. LWSSTATS_C_SSL_CONNECTIONS_ACCEPT_SPIN));
  2754. lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_FAILED: %8llu\n",
  2755. (unsigned long long)lws_stats_get(context,
  2756. LWSSTATS_C_SSL_CONNECTIONS_FAILED));
  2757. lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED: %8llu\n",
  2758. (unsigned long long)lws_stats_get(context,
  2759. LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED));
  2760. lwsl_notice("LWSSTATS_C_SSL_CONNS_HAD_RX: %8llu\n",
  2761. (unsigned long long)lws_stats_get(context,
  2762. LWSSTATS_C_SSL_CONNS_HAD_RX));
  2763. lwsl_notice("LWSSTATS_C_PEER_LIMIT_AH_DENIED: %8llu\n",
  2764. (unsigned long long)lws_stats_get(context,
  2765. LWSSTATS_C_PEER_LIMIT_AH_DENIED));
  2766. lwsl_notice("LWSSTATS_C_PEER_LIMIT_WSI_DENIED: %8llu\n",
  2767. (unsigned long long)lws_stats_get(context,
  2768. LWSSTATS_C_PEER_LIMIT_WSI_DENIED));
  2769. lwsl_notice("LWSSTATS_C_TIMEOUTS: %8llu\n",
  2770. (unsigned long long)lws_stats_get(context,
  2771. LWSSTATS_C_TIMEOUTS));
  2772. lwsl_notice("LWSSTATS_C_SERVICE_ENTRY: %8llu\n",
  2773. (unsigned long long)lws_stats_get(context,
  2774. LWSSTATS_C_SERVICE_ENTRY));
  2775. lwsl_notice("LWSSTATS_B_READ: %8llu\n",
  2776. (unsigned long long)lws_stats_get(context, LWSSTATS_B_READ));
  2777. lwsl_notice("LWSSTATS_B_WRITE: %8llu\n",
  2778. (unsigned long long)lws_stats_get(context, LWSSTATS_B_WRITE));
  2779. lwsl_notice("LWSSTATS_B_PARTIALS_ACCEPTED_PARTS: %8llu\n",
  2780. (unsigned long long)lws_stats_get(context,
  2781. LWSSTATS_B_PARTIALS_ACCEPTED_PARTS));
  2782. lwsl_notice("LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY: %8llums\n",
  2783. (unsigned long long)lws_stats_get(context,
  2784. LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY) / 1000);
  2785. if (lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED))
  2786. lwsl_notice(" Avg accept delay: %8llums\n",
  2787. (unsigned long long)(lws_stats_get(context,
  2788. LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY) /
  2789. lws_stats_get(context,
  2790. LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED)) / 1000);
  2791. lwsl_notice("LWSSTATS_MS_SSL_RX_DELAY: %8llums\n",
  2792. (unsigned long long)lws_stats_get(context,
  2793. LWSSTATS_MS_SSL_RX_DELAY) / 1000);
  2794. if (lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX))
  2795. lwsl_notice(" Avg accept-rx delay: %8llums\n",
  2796. (unsigned long long)(lws_stats_get(context,
  2797. LWSSTATS_MS_SSL_RX_DELAY) /
  2798. lws_stats_get(context,
  2799. LWSSTATS_C_SSL_CONNS_HAD_RX)) / 1000);
  2800. lwsl_notice("LWSSTATS_MS_WRITABLE_DELAY: %8lluus\n",
  2801. (unsigned long long)lws_stats_get(context,
  2802. LWSSTATS_MS_WRITABLE_DELAY));
  2803. lwsl_notice("LWSSTATS_MS_WORST_WRITABLE_DELAY: %8lluus\n",
  2804. (unsigned long long)lws_stats_get(context,
  2805. LWSSTATS_MS_WORST_WRITABLE_DELAY));
  2806. if (lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB))
  2807. lwsl_notice(" Avg writable delay: %8lluus\n",
  2808. (unsigned long long)(lws_stats_get(context,
  2809. LWSSTATS_MS_WRITABLE_DELAY) /
  2810. lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB)));
  2811. lwsl_notice("Simultaneous SSL restriction: %8d/%d\n",
  2812. context->simultaneous_ssl,
  2813. context->simultaneous_ssl_restriction);
  2814. lwsl_notice("Live wsi: %8d\n",
  2815. context->count_wsi_allocated);
  2816. context->updated = 1;
  2817. while (v) {
  2818. if (v->lserv_wsi &&
  2819. v->lserv_wsi->position_in_fds_table != LWS_NO_FDS_POS) {
  2820. struct lws_context_per_thread *pt =
  2821. &context->pt[(int)v->lserv_wsi->tsi];
  2822. struct lws_pollfd *pfd;
  2823. pfd = &pt->fds[v->lserv_wsi->position_in_fds_table];
  2824. lwsl_notice(" Listen port %d actual POLLIN: %d\n",
  2825. v->listen_port,
  2826. (int)pfd->events & LWS_POLLIN);
  2827. }
  2828. v = v->vhost_next;
  2829. }
  2830. for (n = 0; n < context->count_threads; n++) {
  2831. struct lws_context_per_thread *pt = &context->pt[n];
  2832. struct lws *wl;
  2833. int m = 0;
  2834. lwsl_notice("PT %d\n", n + 1);
  2835. lws_pt_lock(pt, __func__);
  2836. lwsl_notice(" AH in use / max: %d / %d\n",
  2837. pt->http.ah_count_in_use,
  2838. context->max_http_header_pool);
  2839. wl = pt->http.ah_wait_list;
  2840. while (wl) {
  2841. m++;
  2842. wl = wl->http.ah_wait_list;
  2843. }
  2844. lwsl_notice(" AH wait list count / actual: %d / %d\n",
  2845. pt->http.ah_wait_list_length, m);
  2846. lws_pt_unlock(pt);
  2847. }
  2848. #if defined(LWS_WITH_PEER_LIMITS)
  2849. m = 0;
  2850. for (n = 0; n < (int)context->pl_hash_elements; n++) {
  2851. lws_start_foreach_llp(struct lws_peer **, peer,
  2852. context->pl_hash_table[n]) {
  2853. m++;
  2854. } lws_end_foreach_llp(peer, next);
  2855. }
  2856. lwsl_notice(" Peers: total active %d\n", m);
  2857. if (m > 10) {
  2858. m = 10;
  2859. lwsl_notice(" (showing 10 peers only)\n");
  2860. }
  2861. if (m) {
  2862. for (n = 0; n < (int)context->pl_hash_elements; n++) {
  2863. char buf[72];
  2864. lws_start_foreach_llp(struct lws_peer **, peer,
  2865. context->pl_hash_table[n]) {
  2866. struct lws_peer *df = *peer;
  2867. if (!lws_plat_inet_ntop(df->af, df->addr, buf,
  2868. sizeof(buf) - 1))
  2869. strcpy(buf, "unknown");
  2870. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  2871. lwsl_notice(" peer %s: count wsi: %d, count ah: %d\n",
  2872. buf, df->count_wsi,
  2873. df->http.count_ah);
  2874. #else
  2875. lwsl_notice(" peer %s: count wsi: %d\n",
  2876. buf, df->count_wsi);
  2877. #endif
  2878. if (!--m)
  2879. break;
  2880. } lws_end_foreach_llp(peer, next);
  2881. }
  2882. }
  2883. #endif
  2884. lwsl_notice("\n");
  2885. }
  2886. void
  2887. lws_stats_atomic_bump(struct lws_context * context,
  2888. struct lws_context_per_thread *pt, int index, uint64_t bump)
  2889. {
  2890. lws_pt_stats_lock(pt);
  2891. context->lws_stats[index] += bump;
  2892. if (index != LWSSTATS_C_SERVICE_ENTRY)
  2893. context->updated = 1;
  2894. lws_pt_stats_unlock(pt);
  2895. }
  2896. void
  2897. lws_stats_atomic_max(struct lws_context * context,
  2898. struct lws_context_per_thread *pt, int index, uint64_t val)
  2899. {
  2900. lws_pt_stats_lock(pt);
  2901. if (val > context->lws_stats[index]) {
  2902. context->lws_stats[index] = val;
  2903. context->updated = 1;
  2904. }
  2905. lws_pt_stats_unlock(pt);
  2906. }
  2907. #endif