libwebsockets.h 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. /** @file */
  22. #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
  23. #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
  24. #ifdef __cplusplus
  25. #include <cstddef>
  26. #include <cstdarg>
  27. #
  28. extern "C" {
  29. #else
  30. #include <stdarg.h>
  31. #endif
  32. #include "lws_config.h"
  33. /*
  34. * CARE: everything using cmake defines needs to be below here
  35. */
  36. #if defined(LWS_WITH_ESP8266)
  37. struct sockaddr_in;
  38. #define LWS_POSIX 0
  39. #else
  40. #define LWS_POSIX 1
  41. #endif
  42. #if defined(LWS_HAS_INTPTR_T)
  43. #include <stdint.h>
  44. #define lws_intptr_t intptr_t
  45. #else
  46. typedef unsigned long long lws_intptr_t;
  47. #endif
  48. #if defined(WIN32) || defined(_WIN32)
  49. #ifndef WIN32_LEAN_AND_MEAN
  50. #define WIN32_LEAN_AND_MEAN
  51. #endif
  52. #include <winsock2.h>
  53. #include <ws2tcpip.h>
  54. #include <stddef.h>
  55. #include <basetsd.h>
  56. #ifndef _WIN32_WCE
  57. #include <fcntl.h>
  58. #else
  59. #define _O_RDONLY 0x0000
  60. #define O_RDONLY _O_RDONLY
  61. #endif
  62. // Visual studio older than 2015 and WIN_CE has only _stricmp
  63. #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
  64. #define strcasecmp _stricmp
  65. #elif !defined(__MINGW32__)
  66. #define strcasecmp stricmp
  67. #endif
  68. #define getdtablesize() 30000
  69. #define LWS_INLINE __inline
  70. #define LWS_VISIBLE
  71. #define LWS_WARN_UNUSED_RESULT
  72. #define LWS_WARN_DEPRECATED
  73. #define LWS_FORMAT(string_index)
  74. #ifdef LWS_DLL
  75. #ifdef LWS_INTERNAL
  76. #define LWS_EXTERN extern __declspec(dllexport)
  77. #else
  78. #define LWS_EXTERN extern __declspec(dllimport)
  79. #endif
  80. #else
  81. #define LWS_EXTERN
  82. #endif
  83. #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
  84. #define LWS_O_RDONLY _O_RDONLY
  85. #define LWS_O_WRONLY _O_WRONLY
  86. #define LWS_O_CREAT _O_CREAT
  87. #define LWS_O_TRUNC _O_TRUNC
  88. #if !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER < 1900) /* Visual Studio 2015 already defines this in <stdio.h> */
  89. #define lws_snprintf _snprintf
  90. #endif
  91. #ifndef __func__
  92. #define __func__ __FUNCTION__
  93. #endif
  94. #if !defined(__MINGW32__) &&(!defined(_MSC_VER) || _MSC_VER < 1900) && !defined(snprintf)
  95. #define snprintf(buf,len, format,...) _snprintf_s(buf, len,len, format, __VA_ARGS__)
  96. #endif
  97. #else /* NOT WIN32 */
  98. #include <unistd.h>
  99. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  100. #include <sys/capability.h>
  101. #endif
  102. #if defined(__NetBSD__) || defined(__FreeBSD__)
  103. #include <netinet/in.h>
  104. #endif
  105. #define LWS_INLINE inline
  106. #define LWS_O_RDONLY O_RDONLY
  107. #define LWS_O_WRONLY O_WRONLY
  108. #define LWS_O_CREAT O_CREAT
  109. #define LWS_O_TRUNC O_TRUNC
  110. #if !defined(LWS_WITH_ESP8266) && !defined(OPTEE_TA) && !defined(LWS_WITH_ESP32)
  111. #include <poll.h>
  112. #include <netdb.h>
  113. #define LWS_INVALID_FILE -1
  114. #else
  115. #define getdtablesize() (30)
  116. #if defined(LWS_WITH_ESP32)
  117. #define LWS_INVALID_FILE NULL
  118. #else
  119. #define LWS_INVALID_FILE NULL
  120. #endif
  121. #endif
  122. #if defined(__GNUC__)
  123. /* warn_unused_result attribute only supported by GCC 3.4 or later */
  124. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  125. #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  126. #else
  127. #define LWS_WARN_UNUSED_RESULT
  128. #endif
  129. #define LWS_VISIBLE __attribute__((visibility("default")))
  130. #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
  131. #define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
  132. #else
  133. #define LWS_VISIBLE
  134. #define LWS_WARN_UNUSED_RESULT
  135. #define LWS_WARN_DEPRECATED
  136. #define LWS_FORMAT(string_index)
  137. #endif
  138. #if defined(__ANDROID__)
  139. #include <unistd.h>
  140. #define getdtablesize() sysconf(_SC_OPEN_MAX)
  141. #endif
  142. #endif
  143. #ifdef LWS_WITH_LIBEV
  144. #include <ev.h>
  145. #endif /* LWS_WITH_LIBEV */
  146. #ifdef LWS_WITH_LIBUV
  147. #include "uv.h"
  148. #ifdef LWS_HAVE_UV_VERSION_H
  149. #include <uv-version.h>
  150. #endif
  151. #endif /* LWS_WITH_LIBUV */
  152. #ifdef LWS_WITH_LIBEVENT
  153. #include <event2/event.h>
  154. #endif /* LWS_WITH_LIBEVENT */
  155. #ifndef LWS_EXTERN
  156. #define LWS_EXTERN extern
  157. #endif
  158. #ifdef _WIN32
  159. #define random rand
  160. #else
  161. #if !defined(OPTEE_TA)
  162. #include <sys/time.h>
  163. #include <unistd.h>
  164. #endif
  165. #endif
  166. #ifdef LWS_OPENSSL_SUPPORT
  167. #ifdef USE_WOLFSSL
  168. #ifdef USE_OLD_CYASSL
  169. #include <cyassl/openssl/ssl.h>
  170. #include <cyassl/error-ssl.h>
  171. #else
  172. #include <wolfssl/openssl/ssl.h>
  173. #include <wolfssl/error-ssl.h>
  174. #endif /* not USE_OLD_CYASSL */
  175. #else
  176. #if defined(LWS_WITH_MBEDTLS)
  177. #if defined(LWS_WITH_ESP32)
  178. /* this filepath is passed to us but without quotes or <> */
  179. #undef MBEDTLS_CONFIG_FILE
  180. #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
  181. #endif
  182. #include <mbedtls/ssl.h>
  183. #endif
  184. #include "openssl/ssl.h"
  185. #if !defined(LWS_WITH_MBEDTLS)
  186. #include "openssl/err.h"
  187. #endif
  188. #endif /* not USE_WOLFSSL */
  189. #endif
  190. #define CONTEXT_PORT_NO_LISTEN -1
  191. #define CONTEXT_PORT_NO_LISTEN_SERVER -2
  192. /** \defgroup log Logging
  193. *
  194. * ##Logging
  195. *
  196. * Lws provides flexible and filterable logging facilities, which can be
  197. * used inside lws and in user code.
  198. *
  199. * Log categories may be individually filtered bitwise, and directed to built-in
  200. * sinks for syslog-compatible logging, or a user-defined function.
  201. */
  202. ///@{
  203. enum lws_log_levels {
  204. LLL_ERR = 1 << 0,
  205. LLL_WARN = 1 << 1,
  206. LLL_NOTICE = 1 << 2,
  207. LLL_INFO = 1 << 3,
  208. LLL_DEBUG = 1 << 4,
  209. LLL_PARSER = 1 << 5,
  210. LLL_HEADER = 1 << 6,
  211. LLL_EXT = 1 << 7,
  212. LLL_CLIENT = 1 << 8,
  213. LLL_LATENCY = 1 << 9,
  214. LLL_USER = 1 << 10,
  215. LLL_COUNT = 11 /* set to count of valid flags */
  216. };
  217. LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...) LWS_FORMAT(2);
  218. LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
  219. /**
  220. * lwsl_timestamp: generate logging timestamp string
  221. *
  222. * \param level: logging level
  223. * \param p: char * buffer to take timestamp
  224. * \param len: length of p
  225. *
  226. * returns length written in p
  227. */
  228. LWS_VISIBLE LWS_EXTERN int
  229. lwsl_timestamp(int level, char *p, int len);
  230. /* these guys are unconditionally included */
  231. #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
  232. #define lwsl_user(...) _lws_log(LLL_USER, __VA_ARGS__)
  233. #if !defined(LWS_WITH_NO_LOGS)
  234. /* notice and warn are usually included by being compiled in */
  235. #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
  236. #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
  237. #endif
  238. /*
  239. * weaker logging can be deselected by telling CMake to build in RELEASE mode
  240. * that gets rid of the overhead of checking while keeping _warn and _err
  241. * active
  242. */
  243. #if defined(LWS_WITH_ESP8266)
  244. #undef _DEBUG
  245. #endif
  246. #ifdef _DEBUG
  247. #if defined(LWS_WITH_NO_LOGS)
  248. /* notice, warn and log are always compiled in */
  249. #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
  250. #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
  251. #endif
  252. #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
  253. #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
  254. #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
  255. #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
  256. #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
  257. #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
  258. #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
  259. #else /* no debug */
  260. #if defined(LWS_WITH_NO_LOGS)
  261. #define lwsl_warn(...) do {} while(0)
  262. #define lwsl_notice(...) do {} while(0)
  263. #endif
  264. #define lwsl_info(...) do {} while(0)
  265. #define lwsl_debug(...) do {} while(0)
  266. #define lwsl_parser(...) do {} while(0)
  267. #define lwsl_header(...) do {} while(0)
  268. #define lwsl_ext(...) do {} while(0)
  269. #define lwsl_client(...) do {} while(0)
  270. #define lwsl_latency(...) do {} while(0)
  271. #endif
  272. /**
  273. * lwsl_hexdump() - helper to hexdump a buffer
  274. *
  275. * \param level: one of LLL_ constants
  276. * \param buf: buffer start to dump
  277. * \param len: length of buffer to dump
  278. *
  279. * If \p level is visible, does a nice hexdump -C style dump of \p buf for
  280. * \p len bytes. This can be extremely convenient while debugging.
  281. */
  282. LWS_VISIBLE LWS_EXTERN void
  283. lwsl_hexdump_level(int level, const void *vbuf, size_t len);
  284. /**
  285. * lwsl_hexdump() - helper to hexdump a buffer (DEBUG builds only)
  286. *
  287. * \param buf: buffer start to dump
  288. * \param len: length of buffer to dump
  289. *
  290. * Calls through to lwsl_hexdump_level(LLL_DEBUG, ... for compatability.
  291. * It's better to use lwsl_hexdump_level(level, ... directly so you can control
  292. * the visibility.
  293. */
  294. LWS_VISIBLE LWS_EXTERN void
  295. lwsl_hexdump(const void *buf, size_t len);
  296. /**
  297. * lws_is_be() - returns nonzero if the platform is Big Endian
  298. */
  299. static LWS_INLINE int lws_is_be(void) {
  300. const int probe = ~0xff;
  301. return *(const char *)&probe;
  302. }
  303. /**
  304. * lws_set_log_level() - Set the logging bitfield
  305. * \param level: OR together the LLL_ debug contexts you want output from
  306. * \param log_emit_function: NULL to leave it as it is, or a user-supplied
  307. * function to perform log string emission instead of
  308. * the default stderr one.
  309. *
  310. * log level defaults to "err", "warn" and "notice" contexts enabled and
  311. * emission on stderr. If stderr is a tty (according to isatty()) then
  312. * the output is coloured according to the log level using ANSI escapes.
  313. */
  314. LWS_VISIBLE LWS_EXTERN void
  315. lws_set_log_level(int level,
  316. void (*log_emit_function)(int level, const char *line));
  317. /**
  318. * lwsl_emit_syslog() - helper log emit function writes to system log
  319. *
  320. * \param level: one of LLL_ log level indexes
  321. * \param line: log string
  322. *
  323. * You use this by passing the function pointer to lws_set_log_level(), to set
  324. * it as the log emit function, it is not called directly.
  325. */
  326. LWS_VISIBLE LWS_EXTERN void
  327. lwsl_emit_syslog(int level, const char *line);
  328. /**
  329. * lwsl_visible() - returns true if the log level should be printed
  330. *
  331. * \param level: one of LLL_ log level indexes
  332. *
  333. * This is useful if you have to do work to generate the log content, you
  334. * can skip the work if the log level used to print it is not actually
  335. * enabled at runtime.
  336. */
  337. LWS_VISIBLE LWS_EXTERN int
  338. lwsl_visible(int level);
  339. ///@}
  340. #include <stddef.h>
  341. #ifndef lws_container_of
  342. #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
  343. #endif
  344. struct lws;
  345. #ifndef ARRAY_SIZE
  346. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  347. #endif
  348. /* api change list for user code to test against */
  349. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
  350. /* the struct lws_protocols has the id field present */
  351. #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
  352. /* you can call lws_get_peer_write_allowance */
  353. #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
  354. /* extra parameter introduced in 917f43ab821 */
  355. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
  356. /* File operations stuff exists */
  357. #define LWS_FEATURE_FOPS
  358. #if defined(_WIN32)
  359. typedef SOCKET lws_sockfd_type;
  360. typedef HANDLE lws_filefd_type;
  361. #define lws_sockfd_valid(sfd) (!!sfd)
  362. struct lws_pollfd {
  363. lws_sockfd_type fd; /**< file descriptor */
  364. SHORT events; /**< which events to respond to */
  365. SHORT revents; /**< which events happened */
  366. };
  367. #define LWS_POLLHUP (FD_CLOSE)
  368. #define LWS_POLLIN (FD_READ | FD_ACCEPT)
  369. #define LWS_POLLOUT (FD_WRITE)
  370. #else
  371. #if defined(LWS_WITH_ESP8266)
  372. #include <user_interface.h>
  373. #include <espconn.h>
  374. typedef struct espconn * lws_sockfd_type;
  375. typedef void * lws_filefd_type;
  376. #define lws_sockfd_valid(sfd) (!!sfd)
  377. struct pollfd {
  378. lws_sockfd_type fd; /**< fd related to */
  379. short events; /**< which POLL... events to respond to */
  380. short revents; /**< which POLL... events occurred */
  381. };
  382. #define POLLIN 0x0001
  383. #define POLLPRI 0x0002
  384. #define POLLOUT 0x0004
  385. #define POLLERR 0x0008
  386. #define POLLHUP 0x0010
  387. #define POLLNVAL 0x0020
  388. struct lws_vhost;
  389. lws_sockfd_type esp8266_create_tcp_listen_socket(struct lws_vhost *vh);
  390. void esp8266_tcp_stream_accept(lws_sockfd_type fd, struct lws *wsi);
  391. #include <os_type.h>
  392. #include <osapi.h>
  393. #include "ets_sys.h"
  394. int ets_snprintf(char *str, size_t size, const char *format, ...) LWS_FORMAT(3);
  395. #define snprintf ets_snprintf
  396. typedef os_timer_t uv_timer_t;
  397. typedef void uv_cb_t(uv_timer_t *);
  398. void os_timer_disarm(void *);
  399. void os_timer_setfn(os_timer_t *, os_timer_func_t *, void *);
  400. void ets_timer_arm_new(os_timer_t *, int, int, int);
  401. //void os_timer_arm(os_timer_t *, int, int);
  402. #define UV_VERSION_MAJOR 1
  403. #define lws_uv_getloop(a, b) (NULL)
  404. static inline void uv_timer_init(void *l, uv_timer_t *t)
  405. {
  406. (void)l;
  407. memset(t, 0, sizeof(*t));
  408. os_timer_disarm(t);
  409. }
  410. static inline void uv_timer_start(uv_timer_t *t, uv_cb_t *cb, int first, int rep)
  411. {
  412. os_timer_setfn(t, (os_timer_func_t *)cb, t);
  413. /* ms, repeat */
  414. os_timer_arm(t, first, !!rep);
  415. }
  416. static inline void uv_timer_stop(uv_timer_t *t)
  417. {
  418. os_timer_disarm(t);
  419. }
  420. #else
  421. #if defined(LWS_WITH_ESP32)
  422. typedef int lws_sockfd_type;
  423. typedef int lws_filefd_type;
  424. #define lws_sockfd_valid(sfd) (sfd >= 0)
  425. struct pollfd {
  426. lws_sockfd_type fd; /**< fd related to */
  427. short events; /**< which POLL... events to respond to */
  428. short revents; /**< which POLL... events occurred */
  429. };
  430. #define POLLIN 0x0001
  431. #define POLLPRI 0x0002
  432. #define POLLOUT 0x0004
  433. #define POLLERR 0x0008
  434. #define POLLHUP 0x0010
  435. #define POLLNVAL 0x0020
  436. #include <freertos/FreeRTOS.h>
  437. #include <freertos/event_groups.h>
  438. #include <string.h>
  439. #include "esp_wifi.h"
  440. #include "esp_system.h"
  441. #include "esp_event.h"
  442. #include "esp_event_loop.h"
  443. #include "nvs.h"
  444. #include "driver/gpio.h"
  445. #include "esp_spi_flash.h"
  446. #include "freertos/timers.h"
  447. #if !defined(CONFIG_FREERTOS_HZ)
  448. #define CONFIG_FREERTOS_HZ 100
  449. #endif
  450. typedef TimerHandle_t uv_timer_t;
  451. typedef void uv_cb_t(uv_timer_t *);
  452. typedef void * uv_handle_t;
  453. struct timer_mapping {
  454. uv_cb_t *cb;
  455. uv_timer_t *t;
  456. };
  457. #define UV_VERSION_MAJOR 1
  458. #define lws_uv_getloop(a, b) (NULL)
  459. static inline void uv_timer_init(void *l, uv_timer_t *t)
  460. {
  461. (void)l;
  462. *t = NULL;
  463. }
  464. extern void esp32_uvtimer_cb(TimerHandle_t t);
  465. static inline void uv_timer_start(uv_timer_t *t, uv_cb_t *cb, int first, int rep)
  466. {
  467. struct timer_mapping *tm = (struct timer_mapping *)malloc(sizeof(*tm));
  468. if (!tm)
  469. return;
  470. tm->t = t;
  471. tm->cb = cb;
  472. *t = xTimerCreate("x", pdMS_TO_TICKS(first), !!rep, tm,
  473. (TimerCallbackFunction_t)esp32_uvtimer_cb);
  474. xTimerStart(*t, 0);
  475. }
  476. static inline void uv_timer_stop(uv_timer_t *t)
  477. {
  478. xTimerStop(*t, 0);
  479. }
  480. static inline void uv_close(uv_handle_t *h, void *v)
  481. {
  482. free(pvTimerGetTimerID((uv_timer_t)h));
  483. xTimerDelete(*(uv_timer_t *)h, 0);
  484. }
  485. /* ESP32 helper declarations */
  486. #include <mdns.h>
  487. #include <esp_partition.h>
  488. #define LWS_PLUGIN_STATIC
  489. #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
  490. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
  491. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
  492. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
  493. /* user code provides these */
  494. extern void
  495. lws_esp32_identify_physical_device(void);
  496. /* lws-plat-esp32 provides these */
  497. typedef void (*lws_cb_scan_done)(uint16_t count, wifi_ap_record_t *recs, void *arg);
  498. enum genled_state {
  499. LWSESP32_GENLED__INIT,
  500. LWSESP32_GENLED__LOST_NETWORK,
  501. LWSESP32_GENLED__NO_NETWORK,
  502. LWSESP32_GENLED__CONN_AP,
  503. LWSESP32_GENLED__GOT_IP,
  504. LWSESP32_GENLED__OK,
  505. };
  506. struct lws_group_member {
  507. struct lws_group_member *next;
  508. uint64_t last_seen;
  509. char model[16];
  510. char role[16];
  511. char host[32];
  512. char mac[20];
  513. int width, height;
  514. struct ip4_addr addr;
  515. struct ip6_addr addrv6;
  516. uint8_t flags;
  517. };
  518. #define LWS_SYSTEM_GROUP_MEMBER_ADD 1
  519. #define LWS_SYSTEM_GROUP_MEMBER_CHANGE 2
  520. #define LWS_SYSTEM_GROUP_MEMBER_REMOVE 3
  521. #define LWS_GROUP_FLAG_SELF 1
  522. struct lws_esp32 {
  523. char sta_ip[16];
  524. char sta_mask[16];
  525. char sta_gw[16];
  526. char serial[16];
  527. char opts[16];
  528. char model[16];
  529. char group[16];
  530. char role[16];
  531. char ssid[4][16];
  532. char password[4][32];
  533. char active_ssid[32];
  534. char access_pw[16];
  535. char hostname[32];
  536. char mac[20];
  537. mdns_server_t *mdns;
  538. char region;
  539. char inet;
  540. char conn_ap;
  541. enum genled_state genled;
  542. uint64_t genled_t;
  543. lws_cb_scan_done scan_consumer;
  544. void *scan_consumer_arg;
  545. struct lws_group_member *first;
  546. int extant_group_members;
  547. };
  548. struct lws_esp32_image {
  549. uint32_t romfs;
  550. uint32_t romfs_len;
  551. uint32_t json;
  552. uint32_t json_len;
  553. };
  554. extern struct lws_esp32 lws_esp32;
  555. struct lws_vhost;
  556. extern esp_err_t
  557. lws_esp32_event_passthru(void *ctx, system_event_t *event);
  558. extern void
  559. lws_esp32_wlan_config(void);
  560. extern void
  561. lws_esp32_wlan_start_ap(void);
  562. extern void
  563. lws_esp32_wlan_start_station(void);
  564. struct lws_context_creation_info;
  565. extern void
  566. lws_esp32_set_creation_defaults(struct lws_context_creation_info *info);
  567. extern struct lws_context *
  568. lws_esp32_init(struct lws_context_creation_info *, struct lws_vhost **pvh);
  569. extern int
  570. lws_esp32_wlan_nvs_get(int retry);
  571. extern esp_err_t
  572. lws_nvs_set_str(nvs_handle handle, const char* key, const char* value);
  573. extern void
  574. lws_esp32_restart_guided(uint32_t type);
  575. extern const esp_partition_t *
  576. lws_esp_ota_get_boot_partition(void);
  577. extern int
  578. lws_esp32_get_image_info(const esp_partition_t *part, struct lws_esp32_image *i, char *json, int json_len);
  579. extern int
  580. lws_esp32_leds_network_indication(void);
  581. extern uint32_t lws_esp32_get_reboot_type(void);
  582. extern uint16_t lws_esp32_sine_interp(int n);
  583. /* required in external code by esp32 plat (may just return if no leds) */
  584. extern void lws_esp32_leds_timer_cb(TimerHandle_t th);
  585. #else
  586. typedef int lws_sockfd_type;
  587. typedef int lws_filefd_type;
  588. #define lws_sockfd_valid(sfd) (sfd >= 0)
  589. #endif
  590. #endif
  591. #define lws_pollfd pollfd
  592. #define LWS_POLLHUP (POLLHUP|POLLERR)
  593. #define LWS_POLLIN (POLLIN)
  594. #define LWS_POLLOUT (POLLOUT)
  595. #endif
  596. #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
  597. /* ... */
  598. #define ssize_t SSIZE_T
  599. #endif
  600. #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
  601. #include <sys/types.h>
  602. #include <sys/stat.h>
  603. #endif
  604. #if defined(LWS_HAVE_STDINT_H)
  605. #include <stdint.h>
  606. #else
  607. #if defined(WIN32) || defined(_WIN32)
  608. /* !!! >:-[ */
  609. typedef unsigned __int32 uint32_t;
  610. typedef unsigned __int16 uint16_t;
  611. typedef unsigned __int8 uint8_t;
  612. #else
  613. typedef unsigned int uint32_t;
  614. typedef unsigned short uint16_t;
  615. typedef unsigned char uint8_t;
  616. #endif
  617. #endif
  618. typedef unsigned long long lws_filepos_t;
  619. typedef long long lws_fileofs_t;
  620. typedef uint32_t lws_fop_flags_t;
  621. /** struct lws_pollargs - argument structure for all external poll related calls
  622. * passed in via 'in' */
  623. struct lws_pollargs {
  624. lws_sockfd_type fd; /**< applicable socket descriptor */
  625. int events; /**< the new event mask */
  626. int prev_events; /**< the previous event mask */
  627. };
  628. struct lws_tokens;
  629. struct lws_token_limits;
  630. /*! \defgroup wsclose Websocket Close
  631. *
  632. * ##Websocket close frame control
  633. *
  634. * When we close a ws connection, we can send a reason code and a short
  635. * UTF-8 description back with the close packet.
  636. */
  637. ///@{
  638. /*
  639. * NOTE: These public enums are part of the abi. If you want to add one,
  640. * add it at where specified so existing users are unaffected.
  641. */
  642. /** enum lws_close_status - RFC6455 close status codes */
  643. enum lws_close_status {
  644. LWS_CLOSE_STATUS_NOSTATUS = 0,
  645. LWS_CLOSE_STATUS_NORMAL = 1000,
  646. /**< 1000 indicates a normal closure, meaning that the purpose for
  647. which the connection was established has been fulfilled. */
  648. LWS_CLOSE_STATUS_GOINGAWAY = 1001,
  649. /**< 1001 indicates that an endpoint is "going away", such as a server
  650. going down or a browser having navigated away from a page. */
  651. LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
  652. /**< 1002 indicates that an endpoint is terminating the connection due
  653. to a protocol error. */
  654. LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
  655. /**< 1003 indicates that an endpoint is terminating the connection
  656. because it has received a type of data it cannot accept (e.g., an
  657. endpoint that understands only text data MAY send this if it
  658. receives a binary message). */
  659. LWS_CLOSE_STATUS_RESERVED = 1004,
  660. /**< Reserved. The specific meaning might be defined in the future. */
  661. LWS_CLOSE_STATUS_NO_STATUS = 1005,
  662. /**< 1005 is a reserved value and MUST NOT be set as a status code in a
  663. Close control frame by an endpoint. It is designated for use in
  664. applications expecting a status code to indicate that no status
  665. code was actually present. */
  666. LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
  667. /**< 1006 is a reserved value and MUST NOT be set as a status code in a
  668. Close control frame by an endpoint. It is designated for use in
  669. applications expecting a status code to indicate that the
  670. connection was closed abnormally, e.g., without sending or
  671. receiving a Close control frame. */
  672. LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
  673. /**< 1007 indicates that an endpoint is terminating the connection
  674. because it has received data within a message that was not
  675. consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
  676. data within a text message). */
  677. LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
  678. /**< 1008 indicates that an endpoint is terminating the connection
  679. because it has received a message that violates its policy. This
  680. is a generic status code that can be returned when there is no
  681. other more suitable status code (e.g., 1003 or 1009) or if there
  682. is a need to hide specific details about the policy. */
  683. LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
  684. /**< 1009 indicates that an endpoint is terminating the connection
  685. because it has received a message that is too big for it to
  686. process. */
  687. LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
  688. /**< 1010 indicates that an endpoint (client) is terminating the
  689. connection because it has expected the server to negotiate one or
  690. more extension, but the server didn't return them in the response
  691. message of the WebSocket handshake. The list of extensions that
  692. are needed SHOULD appear in the /reason/ part of the Close frame.
  693. Note that this status code is not used by the server, because it
  694. can fail the WebSocket handshake instead */
  695. LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
  696. /**< 1011 indicates that a server is terminating the connection because
  697. it encountered an unexpected condition that prevented it from
  698. fulfilling the request. */
  699. LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
  700. /**< 1015 is a reserved value and MUST NOT be set as a status code in a
  701. Close control frame by an endpoint. It is designated for use in
  702. applications expecting a status code to indicate that the
  703. connection was closed due to a failure to perform a TLS handshake
  704. (e.g., the server certificate can't be verified). */
  705. /****** add new things just above ---^ ******/
  706. LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
  707. };
  708. /**
  709. * lws_close_reason - Set reason and aux data to send with Close packet
  710. * If you are going to return nonzero from the callback
  711. * requesting the connection to close, you can optionally
  712. * call this to set the reason the peer will be told if
  713. * possible.
  714. *
  715. * \param wsi: The websocket connection to set the close reason on
  716. * \param status: A valid close status from websocket standard
  717. * \param buf: NULL or buffer containing up to 124 bytes of auxiliary data
  718. * \param len: Length of data in \param buf to send
  719. */
  720. LWS_VISIBLE LWS_EXTERN void
  721. lws_close_reason(struct lws *wsi, enum lws_close_status status,
  722. unsigned char *buf, size_t len);
  723. ///@}
  724. struct lws;
  725. struct lws_context;
  726. /* needed even with extensions disabled for create context */
  727. struct lws_extension;
  728. /*! \defgroup lwsmeta lws-meta
  729. *
  730. * ##lws-meta protocol
  731. *
  732. * The protocol wraps other muxed connections inside one tcp connection.
  733. *
  734. * Commands are assigned from 0x41 up (so they are valid unicode)
  735. */
  736. ///@{
  737. enum lws_meta_commands {
  738. LWS_META_CMD_OPEN_SUBCHANNEL = 'A',
  739. /**< Client requests to open new subchannel
  740. */
  741. LWS_META_CMD_OPEN_RESULT,
  742. /**< Result of client request to open new subchannel */
  743. LWS_META_CMD_CLOSE_NOTIFY,
  744. /**< Notification of subchannel closure */
  745. LWS_META_CMD_CLOSE_RQ,
  746. /**< client requests to close a subchannel */
  747. LWS_META_CMD_WRITE,
  748. /**< connection writes something to specific channel index */
  749. /****** add new things just above ---^ ******/
  750. };
  751. /* channel numbers are transported offset by 0x20 so they are valid unicode */
  752. #define LWS_META_TRANSPORT_OFFSET 0x20
  753. ///@}
  754. /*! \defgroup usercb User Callback
  755. *
  756. * ##User protocol callback
  757. *
  758. * The protocol callback is the primary way lws interacts with
  759. * user code. For one of a list of a few dozen reasons the callback gets
  760. * called at some event to be handled.
  761. *
  762. * All of the events can be ignored, returning 0 is taken as "OK" and returning
  763. * nonzero in most cases indicates that the connection should be closed.
  764. */
  765. ///@{
  766. struct lws_ssl_info {
  767. int where;
  768. int ret;
  769. };
  770. /*
  771. * NOTE: These public enums are part of the abi. If you want to add one,
  772. * add it at where specified so existing users are unaffected.
  773. */
  774. /** enum lws_callback_reasons - reason you're getting a protocol callback */
  775. enum lws_callback_reasons {
  776. LWS_CALLBACK_ESTABLISHED = 0,
  777. /**< (VH) after the server completes a handshake with an incoming
  778. * client. If you built the library with ssl support, in is a
  779. * pointer to the ssl struct associated with the connection or NULL.*/
  780. LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 1,
  781. /**< the request client connection has been unable to complete a
  782. * handshake with the remote server. If in is non-NULL, you can
  783. * find an error string of length len where it points to
  784. *
  785. * Diagnostic strings that may be returned include
  786. *
  787. * "getaddrinfo (ipv6) failed"
  788. * "unknown address family"
  789. * "getaddrinfo (ipv4) failed"
  790. * "set socket opts failed"
  791. * "insert wsi failed"
  792. * "lws_ssl_client_connect1 failed"
  793. * "lws_ssl_client_connect2 failed"
  794. * "Peer hung up"
  795. * "read failed"
  796. * "HS: URI missing"
  797. * "HS: Redirect code but no Location"
  798. * "HS: URI did not parse"
  799. * "HS: Redirect failed"
  800. * "HS: Server did not return 200"
  801. * "HS: OOM"
  802. * "HS: disallowed by client filter"
  803. * "HS: disallowed at ESTABLISHED"
  804. * "HS: ACCEPT missing"
  805. * "HS: ws upgrade response not 101"
  806. * "HS: UPGRADE missing"
  807. * "HS: Upgrade to something other than websocket"
  808. * "HS: CONNECTION missing"
  809. * "HS: UPGRADE malformed"
  810. * "HS: PROTOCOL malformed"
  811. * "HS: Cannot match protocol"
  812. * "HS: EXT: list too big"
  813. * "HS: EXT: failed setting defaults"
  814. * "HS: EXT: failed parsing defaults"
  815. * "HS: EXT: failed parsing options"
  816. * "HS: EXT: Rejects server options"
  817. * "HS: EXT: unknown ext"
  818. * "HS: Accept hash wrong"
  819. * "HS: Rejected by filter cb"
  820. * "HS: OOM"
  821. * "HS: SO_SNDBUF failed"
  822. * "HS: Rejected at CLIENT_ESTABLISHED"
  823. */
  824. LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH = 2,
  825. /**< this is the last chance for the client user code to examine the
  826. * http headers and decide to reject the connection. If the
  827. * content in the headers is interesting to the
  828. * client (url, etc) it needs to copy it out at
  829. * this point since it will be destroyed before
  830. * the CLIENT_ESTABLISHED call */
  831. LWS_CALLBACK_CLIENT_ESTABLISHED = 3,
  832. /**< after your client connection completed
  833. * a handshake with the remote server */
  834. LWS_CALLBACK_CLOSED = 4,
  835. /**< when the websocket session ends */
  836. LWS_CALLBACK_CLOSED_HTTP = 5,
  837. /**< when a HTTP (non-websocket) session ends */
  838. LWS_CALLBACK_RECEIVE = 6,
  839. /**< data has appeared for this server endpoint from a
  840. * remote client, it can be found at *in and is
  841. * len bytes long */
  842. LWS_CALLBACK_RECEIVE_PONG = 7,
  843. /**< servers receive PONG packets with this callback reason */
  844. LWS_CALLBACK_CLIENT_RECEIVE = 8,
  845. /**< data has appeared from the server for the client connection, it
  846. * can be found at *in and is len bytes long */
  847. LWS_CALLBACK_CLIENT_RECEIVE_PONG = 9,
  848. /**< clients receive PONG packets with this callback reason */
  849. LWS_CALLBACK_CLIENT_WRITEABLE = 10,
  850. /**< If you call lws_callback_on_writable() on a connection, you will
  851. * get one of these callbacks coming when the connection socket
  852. * is able to accept another write packet without blocking.
  853. * If it already was able to take another packet without blocking,
  854. * you'll get this callback at the next call to the service loop
  855. * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
  856. * and servers get LWS_CALLBACK_SERVER_WRITEABLE. */
  857. LWS_CALLBACK_SERVER_WRITEABLE = 11,
  858. /**< See LWS_CALLBACK_CLIENT_WRITEABLE */
  859. LWS_CALLBACK_HTTP = 12,
  860. /**< an http request has come from a client that is not
  861. * asking to upgrade the connection to a websocket
  862. * one. This is a chance to serve http content,
  863. * for example, to send a script to the client
  864. * which will then open the websockets connection.
  865. * in points to the URI path requested and
  866. * lws_serve_http_file() makes it very
  867. * simple to send back a file to the client.
  868. * Normally after sending the file you are done
  869. * with the http connection, since the rest of the
  870. * activity will come by websockets from the script
  871. * that was delivered by http, so you will want to
  872. * return 1; to close and free up the connection. */
  873. LWS_CALLBACK_HTTP_BODY = 13,
  874. /**< the next len bytes data from the http
  875. * request body HTTP connection is now available in in. */
  876. LWS_CALLBACK_HTTP_BODY_COMPLETION = 14,
  877. /**< the expected amount of http request body has been delivered */
  878. LWS_CALLBACK_HTTP_FILE_COMPLETION = 15,
  879. /**< a file requested to be sent down http link has completed. */
  880. LWS_CALLBACK_HTTP_WRITEABLE = 16,
  881. /**< you can write more down the http protocol link now. */
  882. LWS_CALLBACK_FILTER_NETWORK_CONNECTION = 17,
  883. /**< called when a client connects to
  884. * the server at network level; the connection is accepted but then
  885. * passed to this callback to decide whether to hang up immediately
  886. * or not, based on the client IP. in contains the connection
  887. * socket's descriptor. Since the client connection information is
  888. * not available yet, wsi still pointing to the main server socket.
  889. * Return non-zero to terminate the connection before sending or
  890. * receiving anything. Because this happens immediately after the
  891. * network connection from the client, there's no websocket protocol
  892. * selected yet so this callback is issued only to protocol 0. */
  893. LWS_CALLBACK_FILTER_HTTP_CONNECTION = 18,
  894. /**< called when the request has
  895. * been received and parsed from the client, but the response is
  896. * not sent yet. Return non-zero to disallow the connection.
  897. * user is a pointer to the connection user space allocation,
  898. * in is the URI, eg, "/"
  899. * In your handler you can use the public APIs
  900. * lws_hdr_total_length() / lws_hdr_copy() to access all of the
  901. * headers using the header enums lws_token_indexes from
  902. * libwebsockets.h to check for and read the supported header
  903. * presence and content before deciding to allow the http
  904. * connection to proceed or to kill the connection. */
  905. LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED = 19,
  906. /**< A new client just had
  907. * been connected, accepted, and instantiated into the pool. This
  908. * callback allows setting any relevant property to it. Because this
  909. * happens immediately after the instantiation of a new client,
  910. * there's no websocket protocol selected yet so this callback is
  911. * issued only to protocol 0. Only wsi is defined, pointing to the
  912. * new client, and the return value is ignored. */
  913. LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION = 20,
  914. /**< called when the handshake has
  915. * been received and parsed from the client, but the response is
  916. * not sent yet. Return non-zero to disallow the connection.
  917. * user is a pointer to the connection user space allocation,
  918. * in is the requested protocol name
  919. * In your handler you can use the public APIs
  920. * lws_hdr_total_length() / lws_hdr_copy() to access all of the
  921. * headers using the header enums lws_token_indexes from
  922. * libwebsockets.h to check for and read the supported header
  923. * presence and content before deciding to allow the handshake
  924. * to proceed or to kill the connection. */
  925. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS = 21,
  926. /**< if configured for
  927. * including OpenSSL support, this callback allows your user code
  928. * to perform extra SSL_CTX_load_verify_locations() or similar
  929. * calls to direct OpenSSL where to find certificates the client
  930. * can use to confirm the remote server identity. user is the
  931. * OpenSSL SSL_CTX* */
  932. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS = 22,
  933. /**< if configured for
  934. * including OpenSSL support, this callback allows your user code
  935. * to load extra certificates into the server which allow it to
  936. * verify the validity of certificates returned by clients. user
  937. * is the server's OpenSSL SSL_CTX* */
  938. LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION = 23,
  939. /**< if the libwebsockets vhost was created with the option
  940. * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
  941. * callback is generated during OpenSSL verification of the cert
  942. * sent from the client. It is sent to protocol[0] callback as
  943. * no protocol has been negotiated on the connection yet.
  944. * Notice that the libwebsockets context and wsi are both NULL
  945. * during this callback. See
  946. * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  947. * to understand more detail about the OpenSSL callback that
  948. * generates this libwebsockets callback and the meanings of the
  949. * arguments passed. In this callback, user is the x509_ctx,
  950. * in is the ssl pointer and len is preverify_ok
  951. * Notice that this callback maintains libwebsocket return
  952. * conventions, return 0 to mean the cert is OK or 1 to fail it.
  953. * This also means that if you don't handle this callback then
  954. * the default callback action of returning 0 allows the client
  955. * certificates. */
  956. LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER = 24,
  957. /**< this callback happens
  958. * when a client handshake is being compiled. user is NULL,
  959. * in is a char **, it's pointing to a char * which holds the
  960. * next location in the header buffer where you can add
  961. * headers, and len is the remaining space in the header buffer,
  962. * which is typically some hundreds of bytes. So, to add a canned
  963. * cookie, your handler code might look similar to:
  964. *
  965. * char **p = (char **)in;
  966. *
  967. * if (len < 100)
  968. * return 1;
  969. *
  970. * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
  971. *
  972. * return 0;
  973. *
  974. * Notice if you add anything, you just have to take care about
  975. * the CRLF on the line you added. Obviously this callback is
  976. * optional, if you don't handle it everything is fine.
  977. *
  978. * Notice the callback is coming to protocols[0] all the time,
  979. * because there is no specific protocol negotiated yet. */
  980. LWS_CALLBACK_CONFIRM_EXTENSION_OKAY = 25,
  981. /**< When the server handshake code
  982. * sees that it does support a requested extension, before
  983. * accepting the extension by additing to the list sent back to
  984. * the client it gives this callback just to check that it's okay
  985. * to use that extension. It calls back to the requested protocol
  986. * and with in being the extension name, len is 0 and user is
  987. * valid. Note though at this time the ESTABLISHED callback hasn't
  988. * happened yet so if you initialize user content there, user
  989. * content during this callback might not be useful for anything. */
  990. LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED = 26,
  991. /**< When a client
  992. * connection is being prepared to start a handshake to a server,
  993. * each supported extension is checked with protocols[0] callback
  994. * with this reason, giving the user code a chance to suppress the
  995. * claim to support that extension by returning non-zero. If
  996. * unhandled, by default 0 will be returned and the extension
  997. * support included in the header to the server. Notice this
  998. * callback comes to protocols[0]. */
  999. LWS_CALLBACK_PROTOCOL_INIT = 27,
  1000. /**< One-time call per protocol, per-vhost using it, so it can
  1001. * do initial setup / allocations etc */
  1002. LWS_CALLBACK_PROTOCOL_DESTROY = 28,
  1003. /**< One-time call per protocol, per-vhost using it, indicating
  1004. * this protocol won't get used at all after this callback, the
  1005. * vhost is getting destroyed. Take the opportunity to
  1006. * deallocate everything that was allocated by the protocol. */
  1007. LWS_CALLBACK_WSI_CREATE = 29,
  1008. /**< outermost (earliest) wsi create notification to protocols[0] */
  1009. LWS_CALLBACK_WSI_DESTROY = 30,
  1010. /**< outermost (latest) wsi destroy notification to protocols[0] */
  1011. LWS_CALLBACK_GET_THREAD_ID = 31,
  1012. /**< lws can accept callback when writable requests from other
  1013. * threads, if you implement this callback and return an opaque
  1014. * current thread ID integer. */
  1015. /* external poll() management support */
  1016. LWS_CALLBACK_ADD_POLL_FD = 32,
  1017. /**< lws normally deals with its poll() or other event loop
  1018. * internally, but in the case you are integrating with another
  1019. * server you will need to have lws sockets share a
  1020. * polling array with the other server. This and the other
  1021. * POLL_FD related callbacks let you put your specialized
  1022. * poll array interface code in the callback for protocol 0, the
  1023. * first protocol you support, usually the HTTP protocol in the
  1024. * serving case.
  1025. * This callback happens when a socket needs to be
  1026. * added to the polling loop: in points to a struct
  1027. * lws_pollargs; the fd member of the struct is the file
  1028. * descriptor, and events contains the active events
  1029. *
  1030. * If you are using the internal lws polling / event loop
  1031. * you can just ignore these callbacks. */
  1032. LWS_CALLBACK_DEL_POLL_FD = 33,
  1033. /**< This callback happens when a socket descriptor
  1034. * needs to be removed from an external polling array. in is
  1035. * again the struct lws_pollargs containing the fd member
  1036. * to be removed. If you are using the internal polling
  1037. * loop, you can just ignore it. */
  1038. LWS_CALLBACK_CHANGE_MODE_POLL_FD = 34,
  1039. /**< This callback happens when lws wants to modify the events for
  1040. * a connection.
  1041. * in is the struct lws_pollargs with the fd to change.
  1042. * The new event mask is in events member and the old mask is in
  1043. * the prev_events member.
  1044. * If you are using the internal polling loop, you can just ignore
  1045. * it. */
  1046. LWS_CALLBACK_LOCK_POLL = 35,
  1047. /**< These allow the external poll changes driven
  1048. * by lws to participate in an external thread locking
  1049. * scheme around the changes, so the whole thing is threadsafe.
  1050. * These are called around three activities in the library,
  1051. * - inserting a new wsi in the wsi / fd table (len=1)
  1052. * - deleting a wsi from the wsi / fd table (len=1)
  1053. * - changing a wsi's POLLIN/OUT state (len=0)
  1054. * Locking and unlocking external synchronization objects when
  1055. * len == 1 allows external threads to be synchronized against
  1056. * wsi lifecycle changes if it acquires the same lock for the
  1057. * duration of wsi dereference from the other thread context. */
  1058. LWS_CALLBACK_UNLOCK_POLL = 36,
  1059. /**< See LWS_CALLBACK_LOCK_POLL, ignore if using lws internal poll */
  1060. LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY = 37,
  1061. /**< if configured for including OpenSSL support but no private key
  1062. * file has been specified (ssl_private_key_filepath is NULL), this is
  1063. * called to allow the user to set the private key directly via
  1064. * libopenssl and perform further operations if required; this might be
  1065. * useful in situations where the private key is not directly accessible
  1066. * by the OS, for example if it is stored on a smartcard.
  1067. * user is the server's OpenSSL SSL_CTX* */
  1068. LWS_CALLBACK_WS_PEER_INITIATED_CLOSE = 38,
  1069. /**< The peer has sent an unsolicited Close WS packet. in and
  1070. * len are the optional close code (first 2 bytes, network
  1071. * order) and the optional additional information which is not
  1072. * defined in the standard, and may be a string or non-human- readable data.
  1073. * If you return 0 lws will echo the close and then close the
  1074. * connection. If you return nonzero lws will just close the
  1075. * connection. */
  1076. LWS_CALLBACK_WS_EXT_DEFAULTS = 39,
  1077. /**< Gives client connections an opportunity to adjust negotiated
  1078. * extension defaults. `user` is the extension name that was
  1079. * negotiated (eg, "permessage-deflate"). `in` points to a
  1080. * buffer and `len` is the buffer size. The user callback can
  1081. * set the buffer to a string describing options the extension
  1082. * should parse. Or just ignore for defaults. */
  1083. LWS_CALLBACK_CGI = 40,
  1084. /**< CGI: CGI IO events on stdin / out / err are sent here on
  1085. * protocols[0]. The provided `lws_callback_http_dummy()`
  1086. * handles this and the callback should be directed there if
  1087. * you use CGI. */
  1088. LWS_CALLBACK_CGI_TERMINATED = 41,
  1089. /**< CGI: The related CGI process ended, this is called before
  1090. * the wsi is closed. Used to, eg, terminate chunking.
  1091. * The provided `lws_callback_http_dummy()`
  1092. * handles this and the callback should be directed there if
  1093. * you use CGI. The child PID that terminated is in len. */
  1094. LWS_CALLBACK_CGI_STDIN_DATA = 42,
  1095. /**< CGI: Data is, to be sent to the CGI process stdin, eg from
  1096. * a POST body. The provided `lws_callback_http_dummy()`
  1097. * handles this and the callback should be directed there if
  1098. * you use CGI. */
  1099. LWS_CALLBACK_CGI_STDIN_COMPLETED = 43,
  1100. /**< CGI: no more stdin is coming. The provided
  1101. * `lws_callback_http_dummy()` handles this and the callback
  1102. * should be directed there if you use CGI. */
  1103. LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP = 44,
  1104. /**< The HTTP client connection has succeeded, and is now
  1105. * connected to the server */
  1106. LWS_CALLBACK_CLOSED_CLIENT_HTTP = 45,
  1107. /**< The HTTP client connection is closing */
  1108. LWS_CALLBACK_RECEIVE_CLIENT_HTTP = 46,
  1109. /**< This simply indicates data was received on the HTTP client
  1110. * connection. It does NOT drain or provide the data.
  1111. * This exists to neatly allow a proxying type situation,
  1112. * where this incoming data will go out on another connection.
  1113. * If the outgoing connection stalls, we should stall processing
  1114. * the incoming data. So a handler for this in that case should
  1115. * simply set a flag to indicate there is incoming data ready
  1116. * and ask for a writeable callback on the outgoing connection.
  1117. * In the writable callback he can check the flag and then get
  1118. * and drain the waiting incoming data using lws_http_client_read().
  1119. * This will use callbacks to LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ
  1120. * to get and drain the incoming data, where it should be sent
  1121. * back out on the outgoing connection. */
  1122. LWS_CALLBACK_COMPLETED_CLIENT_HTTP = 47,
  1123. /**< The client transaction completed... at the moment this
  1124. * is the same as closing since transaction pipelining on
  1125. * client side is not yet supported. */
  1126. LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ = 48,
  1127. /**< This is generated by lws_http_client_read() used to drain
  1128. * incoming data. In the case the incoming data was chunked,
  1129. * it will be split into multiple smaller callbacks for each
  1130. * chunk block, removing the chunk headers. If not chunked,
  1131. * it will appear all in one callback. */
  1132. LWS_CALLBACK_HTTP_BIND_PROTOCOL = 49,
  1133. /**< By default, all HTTP handling is done in protocols[0].
  1134. * However you can bind different protocols (by name) to
  1135. * different parts of the URL space using callback mounts. This
  1136. * callback occurs in the new protocol when a wsi is bound
  1137. * to that protocol. Any protocol allocation related to the
  1138. * http transaction processing should be created then.
  1139. * These specific callbacks are necessary because with HTTP/1.1,
  1140. * a single connection may perform at series of different
  1141. * transactions at different URLs, thus the lifetime of the
  1142. * protocol bind is just for one transaction, not connection. */
  1143. LWS_CALLBACK_HTTP_DROP_PROTOCOL = 50,
  1144. /**< This is called when a transaction is unbound from a protocol.
  1145. * It indicates the connection completed its transaction and may
  1146. * do something different now. Any protocol allocation related
  1147. * to the http transaction processing should be destroyed. */
  1148. LWS_CALLBACK_CHECK_ACCESS_RIGHTS = 51,
  1149. /**< This gives the user code a chance to forbid an http access.
  1150. * `in` points to a `struct lws_process_html_args`, which
  1151. * describes the URL, and a bit mask describing the type of
  1152. * authentication required. If the callback returns nonzero,
  1153. * the transaction ends with HTTP_STATUS_UNAUTHORIZED. */
  1154. LWS_CALLBACK_PROCESS_HTML = 52,
  1155. /**< This gives your user code a chance to mangle outgoing
  1156. * HTML. `in` points to a `struct lws_process_html_args`
  1157. * which describes the buffer containing outgoing HTML.
  1158. * The buffer may grow up to `.max_len` (currently +128
  1159. * bytes per buffer).
  1160. * */
  1161. LWS_CALLBACK_ADD_HEADERS = 53,
  1162. /**< This gives your user code a chance to add headers to a
  1163. * transaction bound to your protocol. `in` points to a
  1164. * `struct lws_process_html_args` describing a buffer and length
  1165. * you can add headers into using the normal lws apis.
  1166. *
  1167. * Only `args->p` and `args->len` are valid, and `args->p` should
  1168. * be moved on by the amount of bytes written, if any. Eg
  1169. *
  1170. * case LWS_CALLBACK_ADD_HEADERS:
  1171. *
  1172. * struct lws_process_html_args *args =
  1173. * (struct lws_process_html_args *)in;
  1174. *
  1175. * if (lws_add_http_header_by_name(wsi,
  1176. * (unsigned char *)"set-cookie:",
  1177. * (unsigned char *)cookie, cookie_len,
  1178. * (unsigned char **)&args->p,
  1179. * (unsigned char *)args->p + args->max_len))
  1180. * return 1;
  1181. *
  1182. * break;
  1183. */
  1184. LWS_CALLBACK_SESSION_INFO = 54,
  1185. /**< This is only generated by user code using generic sessions.
  1186. * It's used to get a `struct lws_session_info` filled in by
  1187. * generic sessions with information about the logged-in user.
  1188. * See the messageboard sample for an example of how to use. */
  1189. LWS_CALLBACK_GS_EVENT = 55,
  1190. /**< Indicates an event happened to the Generic Sessions session.
  1191. * `in` contains a `struct lws_gs_event_args` describing the event. */
  1192. LWS_CALLBACK_HTTP_PMO = 56,
  1193. /**< per-mount options for this connection, called before
  1194. * the normal LWS_CALLBACK_HTTP when the mount has per-mount
  1195. * options.
  1196. */
  1197. LWS_CALLBACK_CLIENT_HTTP_WRITEABLE = 57,
  1198. /**< when doing an HTTP type client connection, you can call
  1199. * lws_client_http_body_pending(wsi, 1) from
  1200. * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER to get these callbacks
  1201. * sending the HTTP headers.
  1202. *
  1203. * From this callback, when you have sent everything, you should let
  1204. * lws know by calling lws_client_http_body_pending(wsi, 0)
  1205. */
  1206. LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION = 58,
  1207. /**< Similar to LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION
  1208. * this callback is called during OpenSSL verification of the cert
  1209. * sent from the server to the client. It is sent to protocol[0]
  1210. * callback as no protocol has been negotiated on the connection yet.
  1211. * Notice that the wsi is set because lws_client_connect_via_info was
  1212. * successful.
  1213. *
  1214. * See http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  1215. * to understand more detail about the OpenSSL callback that
  1216. * generates this libwebsockets callback and the meanings of the
  1217. * arguments passed. In this callback, user is the x509_ctx,
  1218. * in is the ssl pointer and len is preverify_ok.
  1219. *
  1220. * THIS IS NOT RECOMMENDED BUT if a cert validation error shall be
  1221. * overruled and cert shall be accepted as ok,
  1222. * X509_STORE_CTX_set_error((X509_STORE_CTX*)user, X509_V_OK); must be
  1223. * called and return value must be 0 to mean the cert is OK;
  1224. * returning 1 will fail the cert in any case.
  1225. *
  1226. * This also means that if you don't handle this callback then
  1227. * the default callback action of returning 0 will not accept the
  1228. * certificate in case of a validation error decided by the SSL lib.
  1229. *
  1230. * This is expected and secure behaviour when validating certificates.
  1231. *
  1232. * Note: LCCSCF_ALLOW_SELFSIGNED and
  1233. * LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK still work without this
  1234. * callback being implemented.
  1235. */
  1236. LWS_CALLBACK_RAW_RX = 59,
  1237. /**< RAW mode connection RX */
  1238. LWS_CALLBACK_RAW_CLOSE = 60,
  1239. /**< RAW mode connection is closing */
  1240. LWS_CALLBACK_RAW_WRITEABLE = 61,
  1241. /**< RAW mode connection may be written */
  1242. LWS_CALLBACK_RAW_ADOPT = 62,
  1243. /**< RAW mode connection was adopted (equivalent to 'wsi created') */
  1244. LWS_CALLBACK_RAW_ADOPT_FILE = 63,
  1245. /**< RAW mode file was adopted (equivalent to 'wsi created') */
  1246. LWS_CALLBACK_RAW_RX_FILE = 64,
  1247. /**< RAW mode file has something to read */
  1248. LWS_CALLBACK_RAW_WRITEABLE_FILE = 65,
  1249. /**< RAW mode file is writeable */
  1250. LWS_CALLBACK_RAW_CLOSE_FILE = 66,
  1251. /**< RAW mode wsi that adopted a file is closing */
  1252. LWS_CALLBACK_SSL_INFO = 67,
  1253. /**< SSL connections only. An event you registered an
  1254. * interest in at the vhost has occurred on a connection
  1255. * using the vhost. in is a pointer to a
  1256. * struct lws_ssl_info containing information about the
  1257. * event*/
  1258. LWS_CALLBACK_CHILD_WRITE_VIA_PARENT = 68,
  1259. /**< Child has been marked with parent_carries_io attribute, so
  1260. * lws_write directs the to this callback at the parent,
  1261. * in is a struct lws_write_passthru containing the args
  1262. * the lws_write() was called with.
  1263. */
  1264. LWS_CALLBACK_CHILD_CLOSING = 69,
  1265. /**< Sent to parent to notify them a child is closing / being
  1266. * destroyed. in is the child wsi.
  1267. */
  1268. LWS_CALLBACK_CGI_PROCESS_ATTACH = 70,
  1269. /**< CGI: Sent when the CGI process is spawned for the wsi. The
  1270. * len parameter is the PID of the child process */
  1271. /****** add new things just above ---^ ******/
  1272. LWS_CALLBACK_USER = 1000,
  1273. /**< user code can use any including above without fear of clashes */
  1274. };
  1275. /**
  1276. * typedef lws_callback_function() - User server actions
  1277. * \param wsi: Opaque websocket instance pointer
  1278. * \param reason: The reason for the call
  1279. * \param user: Pointer to per-session user data allocated by library
  1280. * \param in: Pointer used for some callback reasons
  1281. * \param len: Length set for some callback reasons
  1282. *
  1283. * This callback is the way the user controls what is served. All the
  1284. * protocol detail is hidden and handled by the library.
  1285. *
  1286. * For each connection / session there is user data allocated that is
  1287. * pointed to by "user". You set the size of this user data area when
  1288. * the library is initialized with lws_create_server.
  1289. */
  1290. typedef int
  1291. lws_callback_function(struct lws *wsi, enum lws_callback_reasons reason,
  1292. void *user, void *in, size_t len);
  1293. #define LWS_CB_REASON_AUX_BF__CGI 1
  1294. #define LWS_CB_REASON_AUX_BF__PROXY 2
  1295. #define LWS_CB_REASON_AUX_BF__CGI_CHUNK_END 4
  1296. #define LWS_CB_REASON_AUX_BF__CGI_HEADERS 8
  1297. ///@}
  1298. /*! \defgroup generic hash
  1299. * ## Generic Hash related functions
  1300. *
  1301. * Lws provides generic hash / digest accessors that abstract the ones
  1302. * provided by whatever OpenSSL library you are linking against.
  1303. *
  1304. * It lets you use the same code if you build against mbedtls or OpenSSL
  1305. * for example.
  1306. */
  1307. ///@{
  1308. #ifdef LWS_OPENSSL_SUPPORT
  1309. #if defined(LWS_WITH_MBEDTLS)
  1310. #include <mbedtls/sha1.h>
  1311. #include <mbedtls/sha256.h>
  1312. #include <mbedtls/sha512.h>
  1313. #endif
  1314. #define LWS_GENHASH_TYPE_SHA1 0
  1315. #define LWS_GENHASH_TYPE_SHA256 1
  1316. #define LWS_GENHASH_TYPE_SHA512 2
  1317. struct lws_genhash_ctx {
  1318. uint8_t type;
  1319. #if defined(LWS_WITH_MBEDTLS)
  1320. union {
  1321. mbedtls_sha1_context sha1;
  1322. mbedtls_sha256_context sha256;
  1323. mbedtls_sha512_context sha512;
  1324. } u;
  1325. #else
  1326. const EVP_MD *evp_type;
  1327. EVP_MD_CTX *mdctx;
  1328. #endif
  1329. };
  1330. /** lws_genhash_size() - get hash size in bytes
  1331. *
  1332. * \param type: one of LWS_GENHASH_TYPE_...
  1333. *
  1334. * Returns number of bytes in this type of hash
  1335. */
  1336. LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT
  1337. lws_genhash_size(int type);
  1338. /** lws_genhash_init() - prepare your struct lws_genhash_ctx for use
  1339. *
  1340. * \param ctx: your struct lws_genhash_ctx
  1341. * \param type: one of LWS_GENHASH_TYPE_...
  1342. *
  1343. * Initializes the hash context for the type you requested
  1344. */
  1345. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1346. lws_genhash_init(struct lws_genhash_ctx *ctx, int type);
  1347. /** lws_genhash_update() - digest len bytes of the buffer starting at in
  1348. *
  1349. * \param ctx: your struct lws_genhash_ctx
  1350. * \param in: start of the bytes to digest
  1351. * \param len: count of bytes to digest
  1352. *
  1353. * Updates the state of your hash context to reflect digesting len bytes from in
  1354. */
  1355. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1356. lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len);
  1357. /** lws_genhash_destroy() - copy out the result digest and destroy the ctx
  1358. *
  1359. * \param ctx: your struct lws_genhash_ctx
  1360. * \param result: NULL, or where to copy the result hash
  1361. *
  1362. * Finalizes the hash and copies out the digest. Destroys any allocations such
  1363. * that ctx can safely go out of scope after calling this.
  1364. *
  1365. * NULL result is supported so that you can destroy the ctx cleanly on error
  1366. * conditions, where there is no valid result.
  1367. */
  1368. LWS_VISIBLE LWS_EXTERN int
  1369. lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result);
  1370. #endif
  1371. ///@}
  1372. /*! \defgroup extensions Extension related functions
  1373. * ##Extension releated functions
  1374. *
  1375. * Ws defines optional extensions, lws provides the ability to implement these
  1376. * in user code if so desired.
  1377. *
  1378. * We provide one extensions permessage-deflate.
  1379. */
  1380. ///@{
  1381. /*
  1382. * NOTE: These public enums are part of the abi. If you want to add one,
  1383. * add it at where specified so existing users are unaffected.
  1384. */
  1385. enum lws_extension_callback_reasons {
  1386. LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT = 0,
  1387. LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT = 1,
  1388. LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT = 2,
  1389. LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT = 3,
  1390. LWS_EXT_CB_CONSTRUCT = 4,
  1391. LWS_EXT_CB_CLIENT_CONSTRUCT = 5,
  1392. LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE = 6,
  1393. LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION = 7,
  1394. LWS_EXT_CB_DESTROY = 8,
  1395. LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING = 9,
  1396. LWS_EXT_CB_ANY_WSI_ESTABLISHED = 10,
  1397. LWS_EXT_CB_PACKET_RX_PREPARSE = 11,
  1398. LWS_EXT_CB_PACKET_TX_PRESEND = 12,
  1399. LWS_EXT_CB_PACKET_TX_DO_SEND = 13,
  1400. LWS_EXT_CB_HANDSHAKE_REPLY_TX = 14,
  1401. LWS_EXT_CB_FLUSH_PENDING_TX = 15,
  1402. LWS_EXT_CB_EXTENDED_PAYLOAD_RX = 16,
  1403. LWS_EXT_CB_CAN_PROXY_CLIENT_CONNECTION = 17,
  1404. LWS_EXT_CB_1HZ = 18,
  1405. LWS_EXT_CB_REQUEST_ON_WRITEABLE = 19,
  1406. LWS_EXT_CB_IS_WRITEABLE = 20,
  1407. LWS_EXT_CB_PAYLOAD_TX = 21,
  1408. LWS_EXT_CB_PAYLOAD_RX = 22,
  1409. LWS_EXT_CB_OPTION_DEFAULT = 23,
  1410. LWS_EXT_CB_OPTION_SET = 24,
  1411. LWS_EXT_CB_OPTION_CONFIRM = 25,
  1412. LWS_EXT_CB_NAMED_OPTION_SET = 26,
  1413. /****** add new things just above ---^ ******/
  1414. };
  1415. /** enum lws_ext_options_types */
  1416. enum lws_ext_options_types {
  1417. EXTARG_NONE, /**< does not take an argument */
  1418. EXTARG_DEC, /**< requires a decimal argument */
  1419. EXTARG_OPT_DEC /**< may have an optional decimal argument */
  1420. /* Add new things just above here ---^
  1421. * This is part of the ABI, don't needlessly break compatibility */
  1422. };
  1423. /** struct lws_ext_options - Option arguments to the extension. These are
  1424. * used in the negotiation at ws upgrade time.
  1425. * The helper function lws_ext_parse_options()
  1426. * uses these to generate callbacks */
  1427. struct lws_ext_options {
  1428. const char *name; /**< Option name, eg, "server_no_context_takeover" */
  1429. enum lws_ext_options_types type; /**< What kind of args the option can take */
  1430. /* Add new things just above here ---^
  1431. * This is part of the ABI, don't needlessly break compatibility */
  1432. };
  1433. /** struct lws_ext_option_arg */
  1434. struct lws_ext_option_arg {
  1435. const char *option_name; /**< may be NULL, option_index used then */
  1436. int option_index; /**< argument ordinal to use if option_name missing */
  1437. const char *start; /**< value */
  1438. int len; /**< length of value */
  1439. };
  1440. /**
  1441. * typedef lws_extension_callback_function() - Hooks to allow extensions to operate
  1442. * \param context: Websockets context
  1443. * \param ext: This extension
  1444. * \param wsi: Opaque websocket instance pointer
  1445. * \param reason: The reason for the call
  1446. * \param user: Pointer to ptr to per-session user data allocated by library
  1447. * \param in: Pointer used for some callback reasons
  1448. * \param len: Length set for some callback reasons
  1449. *
  1450. * Each extension that is active on a particular connection receives
  1451. * callbacks during the connection lifetime to allow the extension to
  1452. * operate on websocket data and manage itself.
  1453. *
  1454. * Libwebsockets takes care of allocating and freeing "user" memory for
  1455. * each active extension on each connection. That is what is pointed to
  1456. * by the user parameter.
  1457. *
  1458. * LWS_EXT_CB_CONSTRUCT: called when the server has decided to
  1459. * select this extension from the list provided by the client,
  1460. * just before the server will send back the handshake accepting
  1461. * the connection with this extension active. This gives the
  1462. * extension a chance to initialize its connection context found
  1463. * in user.
  1464. *
  1465. * LWS_EXT_CB_CLIENT_CONSTRUCT: same as LWS_EXT_CB_CONSTRUCT
  1466. * but called when client is instantiating this extension. Some
  1467. * extensions will work the same on client and server side and then
  1468. * you can just merge handlers for both CONSTRUCTS.
  1469. *
  1470. * LWS_EXT_CB_DESTROY: called when the connection the extension was
  1471. * being used on is about to be closed and deallocated. It's the
  1472. * last chance for the extension to deallocate anything it has
  1473. * allocated in the user data (pointed to by user) before the
  1474. * user data is deleted. This same callback is used whether you
  1475. * are in client or server instantiation context.
  1476. *
  1477. * LWS_EXT_CB_PACKET_RX_PREPARSE: when this extension was active on
  1478. * a connection, and a packet of data arrived at the connection,
  1479. * it is passed to this callback to give the extension a chance to
  1480. * change the data, eg, decompress it. user is pointing to the
  1481. * extension's private connection context data, in is pointing
  1482. * to an lws_tokens struct, it consists of a char * pointer called
  1483. * token, and an int called token_len. At entry, these are
  1484. * set to point to the received buffer and set to the content
  1485. * length. If the extension will grow the content, it should use
  1486. * a new buffer allocated in its private user context data and
  1487. * set the pointed-to lws_tokens members to point to its buffer.
  1488. *
  1489. * LWS_EXT_CB_PACKET_TX_PRESEND: this works the same way as
  1490. * LWS_EXT_CB_PACKET_RX_PREPARSE above, except it gives the
  1491. * extension a chance to change websocket data just before it will
  1492. * be sent out. Using the same lws_token pointer scheme in in,
  1493. * the extension can change the buffer and the length to be
  1494. * transmitted how it likes. Again if it wants to grow the
  1495. * buffer safely, it should copy the data into its own buffer and
  1496. * set the lws_tokens token pointer to it.
  1497. *
  1498. * LWS_EXT_CB_ARGS_VALIDATE:
  1499. */
  1500. typedef int
  1501. lws_extension_callback_function(struct lws_context *context,
  1502. const struct lws_extension *ext, struct lws *wsi,
  1503. enum lws_extension_callback_reasons reason,
  1504. void *user, void *in, size_t len);
  1505. /** struct lws_extension - An extension we support */
  1506. struct lws_extension {
  1507. const char *name; /**< Formal extension name, eg, "permessage-deflate" */
  1508. lws_extension_callback_function *callback; /**< Service callback */
  1509. const char *client_offer; /**< String containing exts and options client offers */
  1510. /* Add new things just above here ---^
  1511. * This is part of the ABI, don't needlessly break compatibility */
  1512. };
  1513. /**
  1514. * lws_set_extension_option(): set extension option if possible
  1515. *
  1516. * \param wsi: websocket connection
  1517. * \param ext_name: name of ext, like "permessage-deflate"
  1518. * \param opt_name: name of option, like "rx_buf_size"
  1519. * \param opt_val: value to set option to
  1520. */
  1521. LWS_VISIBLE LWS_EXTERN int
  1522. lws_set_extension_option(struct lws *wsi, const char *ext_name,
  1523. const char *opt_name, const char *opt_val);
  1524. #ifndef LWS_NO_EXTENSIONS
  1525. /* lws_get_internal_extensions() - DEPRECATED
  1526. *
  1527. * \Deprecated There is no longer a set internal extensions table. The table is provided
  1528. * by user code along with application-specific settings. See the test
  1529. * client and server for how to do.
  1530. */
  1531. static LWS_INLINE LWS_WARN_DEPRECATED const struct lws_extension *
  1532. lws_get_internal_extensions(void) { return NULL; }
  1533. /**
  1534. * lws_ext_parse_options() - deal with parsing negotiated extension options
  1535. *
  1536. * \param ext: related extension struct
  1537. * \param wsi: websocket connection
  1538. * \param ext_user: per-connection extension private data
  1539. * \param opts: list of supported options
  1540. * \param o: option string to parse
  1541. * \param len: length
  1542. */
  1543. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1544. lws_ext_parse_options(const struct lws_extension *ext, struct lws *wsi,
  1545. void *ext_user, const struct lws_ext_options *opts,
  1546. const char *o, int len);
  1547. #endif
  1548. /** lws_extension_callback_pm_deflate() - extension for RFC7692
  1549. *
  1550. * \param context: lws context
  1551. * \param ext: related lws_extension struct
  1552. * \param wsi: websocket connection
  1553. * \param reason: incoming callback reason
  1554. * \param user: per-connection extension private data
  1555. * \param in: pointer parameter
  1556. * \param len: length parameter
  1557. *
  1558. * Built-in callback implementing RFC7692 permessage-deflate
  1559. */
  1560. LWS_EXTERN
  1561. int lws_extension_callback_pm_deflate(
  1562. struct lws_context *context, const struct lws_extension *ext,
  1563. struct lws *wsi, enum lws_extension_callback_reasons reason,
  1564. void *user, void *in, size_t len);
  1565. /*
  1566. * The internal exts are part of the public abi
  1567. * If we add more extensions, publish the callback here ------v
  1568. */
  1569. ///@}
  1570. /*! \defgroup Protocols-and-Plugins Protocols and Plugins
  1571. * \ingroup lwsapi
  1572. *
  1573. * ##Protocol and protocol plugin -related apis
  1574. *
  1575. * Protocols bind ws protocol names to a custom callback specific to that
  1576. * protocol implementaion.
  1577. *
  1578. * A list of protocols can be passed in at context creation time, but it is
  1579. * also legal to leave that NULL and add the protocols and their callback code
  1580. * using plugins.
  1581. *
  1582. * Plugins are much preferable compared to cut and pasting code into an
  1583. * application each time, since they can be used standalone.
  1584. */
  1585. ///@{
  1586. /** struct lws_protocols - List of protocols and handlers client or server
  1587. * supports. */
  1588. struct lws_protocols {
  1589. const char *name;
  1590. /**< Protocol name that must match the one given in the client
  1591. * Javascript new WebSocket(url, 'protocol') name. */
  1592. lws_callback_function *callback;
  1593. /**< The service callback used for this protocol. It allows the
  1594. * service action for an entire protocol to be encapsulated in
  1595. * the protocol-specific callback */
  1596. size_t per_session_data_size;
  1597. /**< Each new connection using this protocol gets
  1598. * this much memory allocated on connection establishment and
  1599. * freed on connection takedown. A pointer to this per-connection
  1600. * allocation is passed into the callback in the 'user' parameter */
  1601. size_t rx_buffer_size;
  1602. /**< lws allocates this much space for rx data and informs callback
  1603. * when something came. Due to rx flow control, the callback may not
  1604. * be able to consume it all without having to return to the event
  1605. * loop. That is supported in lws.
  1606. *
  1607. * If .tx_packet_size is 0, this also controls how much may be sent at once
  1608. * for backwards compatibility.
  1609. */
  1610. unsigned int id;
  1611. /**< ignored by lws, but useful to contain user information bound
  1612. * to the selected protocol. For example if this protocol was
  1613. * called "myprotocol-v2", you might set id to 2, and the user
  1614. * code that acts differently according to the version can do so by
  1615. * switch (wsi->protocol->id), user code might use some bits as
  1616. * capability flags based on selected protocol version, etc. */
  1617. void *user; /**< ignored by lws, but user code can pass a pointer
  1618. here it can later access from the protocol callback */
  1619. size_t tx_packet_size;
  1620. /**< 0 indicates restrict send() size to .rx_buffer_size for backwards-
  1621. * compatibility.
  1622. * If greater than zero, a single send() is restricted to this amount
  1623. * and any remainder is buffered by lws and sent afterwards also in
  1624. * these size chunks. Since that is expensive, it's preferable
  1625. * to restrict one fragment you are trying to send to match this
  1626. * size.
  1627. */
  1628. /* Add new things just above here ---^
  1629. * This is part of the ABI, don't needlessly break compatibility */
  1630. };
  1631. struct lws_vhost;
  1632. /**
  1633. * lws_vhost_name_to_protocol() - get vhost's protocol object from its name
  1634. *
  1635. * \param vh: vhost to search
  1636. * \param name: protocol name
  1637. *
  1638. * Returns NULL or a pointer to the vhost's protocol of the requested name
  1639. */
  1640. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1641. lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name);
  1642. /**
  1643. * lws_get_protocol() - Returns a protocol pointer from a websocket
  1644. * connection.
  1645. * \param wsi: pointer to struct websocket you want to know the protocol of
  1646. *
  1647. *
  1648. * Some apis can act on all live connections of a given protocol,
  1649. * this is how you can get a pointer to the active protocol if needed.
  1650. */
  1651. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1652. lws_get_protocol(struct lws *wsi);
  1653. /** lws_protocol_get() - deprecated: use lws_get_protocol */
  1654. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1655. lws_protocol_get(struct lws *wsi) LWS_WARN_DEPRECATED;
  1656. /**
  1657. * lws_protocol_vh_priv_zalloc() - Allocate and zero down a protocol's per-vhost
  1658. * storage
  1659. * \param vhost: vhost the instance is related to
  1660. * \param prot: protocol the instance is related to
  1661. * \param size: bytes to allocate
  1662. *
  1663. * Protocols often find it useful to allocate a per-vhost struct, this is a
  1664. * helper to be called in the per-vhost init LWS_CALLBACK_PROTOCOL_INIT
  1665. */
  1666. LWS_VISIBLE LWS_EXTERN void *
  1667. lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
  1668. int size);
  1669. /**
  1670. * lws_protocol_vh_priv_get() - retreive a protocol's per-vhost storage
  1671. *
  1672. * \param vhost: vhost the instance is related to
  1673. * \param prot: protocol the instance is related to
  1674. *
  1675. * Recover a pointer to the allocated per-vhost storage for the protocol created
  1676. * by lws_protocol_vh_priv_zalloc() earlier
  1677. */
  1678. LWS_VISIBLE LWS_EXTERN void *
  1679. lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot);
  1680. /**
  1681. * lws_adjust_protocol_psds - change a vhost protocol's per session data size
  1682. *
  1683. * \param wsi: a connection with the protocol to change
  1684. * \param new_size: the new size of the per session data size for the protocol
  1685. *
  1686. * Returns user_space for the wsi, after allocating
  1687. *
  1688. * This should not be used except to initalize a vhost protocol's per session
  1689. * data size one time, before any connections are accepted.
  1690. *
  1691. * Sometimes the protocol wraps another protocol and needs to discover and set
  1692. * its per session data size at runtime.
  1693. */
  1694. LWS_VISIBLE LWS_EXTERN void *
  1695. lws_adjust_protocol_psds(struct lws *wsi, size_t new_size);
  1696. /**
  1697. * lws_finalize_startup() - drop initial process privileges
  1698. *
  1699. * \param context: lws context
  1700. *
  1701. * This is called after the end of the vhost protocol initializations, but
  1702. * you may choose to call it earlier
  1703. */
  1704. LWS_VISIBLE LWS_EXTERN int
  1705. lws_finalize_startup(struct lws_context *context);
  1706. LWS_VISIBLE LWS_EXTERN int
  1707. lws_protocol_init(struct lws_context *context);
  1708. #ifdef LWS_WITH_PLUGINS
  1709. /* PLUGINS implies LIBUV */
  1710. #define LWS_PLUGIN_API_MAGIC 180
  1711. /** struct lws_plugin_capability - how a plugin introduces itself to lws */
  1712. struct lws_plugin_capability {
  1713. unsigned int api_magic; /**< caller fills this in, plugin fills rest */
  1714. const struct lws_protocols *protocols; /**< array of supported protocols provided by plugin */
  1715. int count_protocols; /**< how many protocols */
  1716. const struct lws_extension *extensions; /**< array of extensions provided by plugin */
  1717. int count_extensions; /**< how many extensions */
  1718. };
  1719. typedef int (*lws_plugin_init_func)(struct lws_context *,
  1720. struct lws_plugin_capability *);
  1721. typedef int (*lws_plugin_destroy_func)(struct lws_context *);
  1722. /** struct lws_plugin */
  1723. struct lws_plugin {
  1724. struct lws_plugin *list; /**< linked list */
  1725. #if (UV_VERSION_MAJOR > 0)
  1726. uv_lib_t lib; /**< shared library pointer */
  1727. #else
  1728. void *l; /**< so we can compile on ancient libuv */
  1729. #endif
  1730. char name[64]; /**< name of the plugin */
  1731. struct lws_plugin_capability caps; /**< plugin capabilities */
  1732. };
  1733. #endif
  1734. ///@}
  1735. /*! \defgroup generic-sessions plugin: generic-sessions
  1736. * \ingroup Protocols-and-Plugins
  1737. *
  1738. * ##Plugin Generic-sessions related
  1739. *
  1740. * generic-sessions plugin provides a reusable, generic session and login /
  1741. * register / forgot password framework including email verification.
  1742. */
  1743. ///@{
  1744. #define LWSGS_EMAIL_CONTENT_SIZE 16384
  1745. /**< Maximum size of email we might send */
  1746. /* SHA-1 binary and hexified versions */
  1747. /** typedef struct lwsgw_hash_bin */
  1748. typedef struct { unsigned char bin[20]; /**< binary representation of hash */} lwsgw_hash_bin;
  1749. /** typedef struct lwsgw_hash */
  1750. typedef struct { char id[41]; /**< ascii hex representation of hash */ } lwsgw_hash;
  1751. /** enum lwsgs_auth_bits */
  1752. enum lwsgs_auth_bits {
  1753. LWSGS_AUTH_LOGGED_IN = 1, /**< user is logged in as somebody */
  1754. LWSGS_AUTH_ADMIN = 2, /**< logged in as the admin user */
  1755. LWSGS_AUTH_VERIFIED = 4, /**< user has verified his email */
  1756. LWSGS_AUTH_FORGOT_FLOW = 8, /**< he just completed "forgot password" flow */
  1757. };
  1758. /** struct lws_session_info - information about user session status */
  1759. struct lws_session_info {
  1760. char username[32]; /**< username logged in as, or empty string */
  1761. char email[100]; /**< email address associated with login, or empty string */
  1762. char ip[72]; /**< ip address session was started from */
  1763. unsigned int mask; /**< access rights mask associated with session
  1764. * see enum lwsgs_auth_bits */
  1765. char session[42]; /**< session id string, usable as opaque uid when not logged in */
  1766. };
  1767. /** enum lws_gs_event */
  1768. enum lws_gs_event {
  1769. LWSGSE_CREATED, /**< a new user was created */
  1770. LWSGSE_DELETED /**< an existing user was deleted */
  1771. };
  1772. /** struct lws_gs_event_args */
  1773. struct lws_gs_event_args {
  1774. enum lws_gs_event event; /**< which event happened */
  1775. const char *username; /**< which username the event happened to */
  1776. const char *email; /**< the email address of that user */
  1777. };
  1778. ///@}
  1779. /*! \defgroup context-and-vhost context and vhost related functions
  1780. * ##Context and Vhost releated functions
  1781. * \ingroup lwsapi
  1782. *
  1783. *
  1784. * LWS requires that there is one context, in which you may define multiple
  1785. * vhosts. Each vhost is a virtual host, with either its own listen port
  1786. * or sharing an existing one. Each vhost has its own SSL context that can
  1787. * be set up individually or left disabled.
  1788. *
  1789. * If you don't care about multiple "site" support, you can ignore it and
  1790. * lws will create a single default vhost at context creation time.
  1791. */
  1792. ///@{
  1793. /*
  1794. * NOTE: These public enums are part of the abi. If you want to add one,
  1795. * add it at where specified so existing users are unaffected.
  1796. */
  1797. /** enum lws_context_options - context and vhost options */
  1798. enum lws_context_options {
  1799. LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = (1 << 1) |
  1800. (1 << 12),
  1801. /**< (VH) Don't allow the connection unless the client has a
  1802. * client cert that we recognize; provides
  1803. * LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
  1804. LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = (1 << 2),
  1805. /**< (CTX) Don't try to get the server's hostname */
  1806. LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = (1 << 3) |
  1807. (1 << 12),
  1808. /**< (VH) Allow non-SSL (plaintext) connections on the same
  1809. * port as SSL is listening... undermines the security of SSL;
  1810. * provides LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
  1811. LWS_SERVER_OPTION_LIBEV = (1 << 4),
  1812. /**< (CTX) Use libev event loop */
  1813. LWS_SERVER_OPTION_DISABLE_IPV6 = (1 << 5),
  1814. /**< (VH) Disable IPV6 support */
  1815. LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = (1 << 6),
  1816. /**< (VH) Don't load OS CA certs, you will need to load your
  1817. * own CA cert(s) */
  1818. LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = (1 << 7),
  1819. /**< (VH) Accept connections with no valid Cert (eg, selfsigned) */
  1820. LWS_SERVER_OPTION_VALIDATE_UTF8 = (1 << 8),
  1821. /**< (VH) Check UT-8 correctness */
  1822. LWS_SERVER_OPTION_SSL_ECDH = (1 << 9) |
  1823. (1 << 12),
  1824. /**< (VH) initialize ECDH ciphers */
  1825. LWS_SERVER_OPTION_LIBUV = (1 << 10),
  1826. /**< (CTX) Use libuv event loop */
  1827. LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS = (1 << 11) |
  1828. (1 << 12),
  1829. /**< (VH) Use http redirect to force http to https
  1830. * (deprecated: use mount redirection) */
  1831. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT = (1 << 12),
  1832. /**< (CTX) Initialize the SSL library at all */
  1833. LWS_SERVER_OPTION_EXPLICIT_VHOSTS = (1 << 13),
  1834. /**< (CTX) Only create the context when calling context
  1835. * create api, implies user code will create its own vhosts */
  1836. LWS_SERVER_OPTION_UNIX_SOCK = (1 << 14),
  1837. /**< (VH) Use Unix socket */
  1838. LWS_SERVER_OPTION_STS = (1 << 15),
  1839. /**< (VH) Send Strict Transport Security header, making
  1840. * clients subsequently go to https even if user asked for http */
  1841. LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY = (1 << 16),
  1842. /**< (VH) Enable LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE to take effect */
  1843. LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE = (1 << 17),
  1844. /**< (VH) if set, only ipv6 allowed on the vhost */
  1845. LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN = (1 << 18),
  1846. /**< (CTX) Libuv only: Do not spin on SIGSEGV / SIGFPE. A segfault
  1847. * normally makes the lib spin so you can attach a debugger to it
  1848. * even if it happened without a debugger in place. You can disable
  1849. * that by giving this option.
  1850. */
  1851. LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN = (1 << 19),
  1852. /**< For backwards-compatibility reasons, by default
  1853. * lws prepends "http://" to the origin you give in the client
  1854. * connection info struct. If you give this flag when you create
  1855. * the context, only the string you give in the client connect
  1856. * info for .origin (if any) will be used directly.
  1857. */
  1858. LWS_SERVER_OPTION_FALLBACK_TO_RAW = (1 << 20),
  1859. /**< (VH) if invalid http is coming in the first line, */
  1860. LWS_SERVER_OPTION_LIBEVENT = (1 << 21),
  1861. /**< (CTX) Use libevent event loop */
  1862. LWS_SERVER_OPTION_ONLY_RAW = (1 << 22),
  1863. /**< (VH) All connections to this vhost / port are RAW as soon as
  1864. * the connection is accepted, no HTTP is going to be coming.
  1865. */
  1866. LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE = (1 << 23),
  1867. /**< (VH) Set to allow multiple listen sockets on one interface +
  1868. * address + port. The default is to strictly allow only one
  1869. * listen socket at a time. This is automatically selected if you
  1870. * have multiple service threads.
  1871. */
  1872. LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX = (1 << 24),
  1873. /**< (VH) Force setting up the vhost SSL_CTX, even though the user
  1874. * code doesn't explicitly provide a cert in the info struct. It
  1875. * implies the user code is going to provide a cert at the
  1876. * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS callback, which
  1877. * provides the vhost SSL_CTX * in the user parameter.
  1878. */
  1879. /****** add new things just above ---^ ******/
  1880. };
  1881. #define lws_check_opt(c, f) (((c) & (f)) == (f))
  1882. struct lws_plat_file_ops;
  1883. /** struct lws_context_creation_info - parameters to create context and /or vhost with
  1884. *
  1885. * This is also used to create vhosts.... if LWS_SERVER_OPTION_EXPLICIT_VHOSTS
  1886. * is not given, then for backwards compatibility one vhost is created at
  1887. * context-creation time using the info from this struct.
  1888. *
  1889. * If LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, then no vhosts are created
  1890. * at the same time as the context, they are expected to be created afterwards.
  1891. */
  1892. struct lws_context_creation_info {
  1893. int port;
  1894. /**< VHOST: Port to listen on. Use CONTEXT_PORT_NO_LISTEN to suppress
  1895. * listening for a client. Use CONTEXT_PORT_NO_LISTEN_SERVER if you are
  1896. * writing a server but you are using \ref sock-adopt instead of the
  1897. * built-in listener */
  1898. const char *iface;
  1899. /**< VHOST: NULL to bind the listen socket to all interfaces, or the
  1900. * interface name, eg, "eth2"
  1901. * If options specifies LWS_SERVER_OPTION_UNIX_SOCK, this member is
  1902. * the pathname of a UNIX domain socket. you can use the UNIX domain
  1903. * sockets in abstract namespace, by prepending an at symbol to the
  1904. * socket name. */
  1905. const struct lws_protocols *protocols;
  1906. /**< VHOST: Array of structures listing supported protocols and a protocol-
  1907. * specific callback for each one. The list is ended with an
  1908. * entry that has a NULL callback pointer. */
  1909. const struct lws_extension *extensions;
  1910. /**< VHOST: NULL or array of lws_extension structs listing the
  1911. * extensions this context supports. */
  1912. const struct lws_token_limits *token_limits;
  1913. /**< CONTEXT: NULL or struct lws_token_limits pointer which is initialized
  1914. * with a token length limit for each possible WSI_TOKEN_ */
  1915. const char *ssl_private_key_password;
  1916. /**< VHOST: NULL or the passphrase needed for the private key. (For
  1917. * backwards compatibility, this can also be used to pass the client
  1918. * cert passphrase when setting up a vhost client SSL context, but it is
  1919. * preferred to use .client_ssl_private_key_password for that.) */
  1920. const char *ssl_cert_filepath;
  1921. /**< VHOST: If libwebsockets was compiled to use ssl, and you want
  1922. * to listen using SSL, set to the filepath to fetch the
  1923. * server cert from, otherwise NULL for unencrypted. (For backwards
  1924. * compatibility, this can also be used to pass the client certificate
  1925. * when setting up a vhost client SSL context, but it is preferred to
  1926. * use .client_ssl_cert_filepath for that.) */
  1927. const char *ssl_private_key_filepath;
  1928. /**< VHOST: filepath to private key if wanting SSL mode;
  1929. * if this is set to NULL but ssl_cert_filepath is set, the
  1930. * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
  1931. * to allow setting of the private key directly via openSSL
  1932. * library calls. (For backwards compatibility, this can also be used
  1933. * to pass the client cert private key filepath when setting up a
  1934. * vhost client SSL context, but it is preferred to use
  1935. * .client_ssl_private_key_filepath for that.) */
  1936. const char *ssl_ca_filepath;
  1937. /**< VHOST: CA certificate filepath or NULL. (For backwards
  1938. * compatibility, this can also be used to pass the client CA
  1939. * filepath when setting up a vhost client SSL context,
  1940. * but it is preferred to use .client_ssl_ca_filepath for that.) */
  1941. const char *ssl_cipher_list;
  1942. /**< VHOST: List of valid ciphers to use (eg,
  1943. * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
  1944. * or you can leave it as NULL to get "DEFAULT" (For backwards
  1945. * compatibility, this can also be used to pass the client cipher
  1946. * list when setting up a vhost client SSL context,
  1947. * but it is preferred to use .client_ssl_cipher_list for that.)*/
  1948. const char *http_proxy_address;
  1949. /**< VHOST: If non-NULL, attempts to proxy via the given address.
  1950. * If proxy auth is required, use format "username:password\@server:port" */
  1951. unsigned int http_proxy_port;
  1952. /**< VHOST: If http_proxy_address was non-NULL, uses this port */
  1953. int gid;
  1954. /**< CONTEXT: group id to change to after setting listen socket, or -1. */
  1955. int uid;
  1956. /**< CONTEXT: user id to change to after setting listen socket, or -1. */
  1957. unsigned int options;
  1958. /**< VHOST + CONTEXT: 0, or LWS_SERVER_OPTION_... bitfields */
  1959. void *user;
  1960. /**< VHOST + CONTEXT: optional user pointer that will be associated
  1961. * with the context when creating the context (and can be retrieved by
  1962. * lws_context_user(context), or with the vhost when creating the vhost
  1963. * (and can be retrieved by lws_vhost_user(vhost)). You will need to
  1964. * use LWS_SERVER_OPTION_EXPLICIT_VHOSTS and create the vhost separately
  1965. * if you care about giving the context and vhost different user pointer
  1966. * values.
  1967. */
  1968. int ka_time;
  1969. /**< CONTEXT: 0 for no TCP keepalive, otherwise apply this keepalive
  1970. * timeout to all libwebsocket sockets, client or server */
  1971. int ka_probes;
  1972. /**< CONTEXT: if ka_time was nonzero, after the timeout expires how many
  1973. * times to try to get a response from the peer before giving up
  1974. * and killing the connection */
  1975. int ka_interval;
  1976. /**< CONTEXT: if ka_time was nonzero, how long to wait before each ka_probes
  1977. * attempt */
  1978. #ifdef LWS_OPENSSL_SUPPORT
  1979. SSL_CTX *provided_client_ssl_ctx;
  1980. /**< CONTEXT: If non-null, swap out libwebsockets ssl
  1981. * implementation for the one provided by provided_ssl_ctx.
  1982. * Libwebsockets no longer is responsible for freeing the context
  1983. * if this option is selected. */
  1984. #else /* maintain structure layout either way */
  1985. void *provided_client_ssl_ctx; /**< dummy if ssl disabled */
  1986. #endif
  1987. short max_http_header_data;
  1988. /**< CONTEXT: The max amount of header payload that can be handled
  1989. * in an http request (unrecognized header payload is dropped) */
  1990. short max_http_header_pool;
  1991. /**< CONTEXT: The max number of connections with http headers that
  1992. * can be processed simultaneously (the corresponding memory is
  1993. * allocated for the lifetime of the context). If the pool is
  1994. * busy new incoming connections must wait for accept until one
  1995. * becomes free. */
  1996. unsigned int count_threads;
  1997. /**< CONTEXT: how many contexts to create in an array, 0 = 1 */
  1998. unsigned int fd_limit_per_thread;
  1999. /**< CONTEXT: nonzero means restrict each service thread to this
  2000. * many fds, 0 means the default which is divide the process fd
  2001. * limit by the number of threads. */
  2002. unsigned int timeout_secs;
  2003. /**< VHOST: various processes involving network roundtrips in the
  2004. * library are protected from hanging forever by timeouts. If
  2005. * nonzero, this member lets you set the timeout used in seconds.
  2006. * Otherwise a default timeout is used. */
  2007. const char *ecdh_curve;
  2008. /**< VHOST: if NULL, defaults to initializing server with "prime256v1" */
  2009. const char *vhost_name;
  2010. /**< VHOST: name of vhost, must match external DNS name used to
  2011. * access the site, like "warmcat.com" as it's used to match
  2012. * Host: header and / or SNI name for SSL. */
  2013. const char * const *plugin_dirs;
  2014. /**< CONTEXT: NULL, or NULL-terminated array of directories to
  2015. * scan for lws protocol plugins at context creation time */
  2016. const struct lws_protocol_vhost_options *pvo;
  2017. /**< VHOST: pointer to optional linked list of per-vhost
  2018. * options made accessible to protocols */
  2019. int keepalive_timeout;
  2020. /**< VHOST: (default = 0 = 60s) seconds to allow remote
  2021. * client to hold on to an idle HTTP/1.1 connection */
  2022. const char *log_filepath;
  2023. /**< VHOST: filepath to append logs to... this is opened before
  2024. * any dropping of initial privileges */
  2025. const struct lws_http_mount *mounts;
  2026. /**< VHOST: optional linked list of mounts for this vhost */
  2027. const char *server_string;
  2028. /**< CONTEXT: string used in HTTP headers to identify server
  2029. * software, if NULL, "libwebsockets". */
  2030. unsigned int pt_serv_buf_size;
  2031. /**< CONTEXT: 0 = default of 4096. This buffer is used by
  2032. * various service related features including file serving, it
  2033. * defines the max chunk of file that can be sent at once.
  2034. * At the risk of lws having to buffer failed large sends, it
  2035. * can be increased to, eg, 128KiB to improve throughput. */
  2036. unsigned int max_http_header_data2;
  2037. /**< CONTEXT: if max_http_header_data is 0 and this
  2038. * is nonzero, this will be used in place of the default. It's
  2039. * like this for compatibility with the original short version,
  2040. * this is unsigned int length. */
  2041. long ssl_options_set;
  2042. /**< VHOST: Any bits set here will be set as SSL options */
  2043. long ssl_options_clear;
  2044. /**< VHOST: Any bits set here will be cleared as SSL options */
  2045. unsigned short ws_ping_pong_interval;
  2046. /**< CONTEXT: 0 for none, else interval in seconds between sending
  2047. * PINGs on idle websocket connections. When the PING is sent,
  2048. * the PONG must come within the normal timeout_secs timeout period
  2049. * or the connection will be dropped.
  2050. * Any RX or TX traffic on the connection restarts the interval timer,
  2051. * so a connection which always sends or receives something at intervals
  2052. * less than the interval given here will never send PINGs / expect
  2053. * PONGs. Conversely as soon as the ws connection is established, an
  2054. * idle connection will do the PING / PONG roundtrip as soon as
  2055. * ws_ping_pong_interval seconds has passed without traffic
  2056. */
  2057. const struct lws_protocol_vhost_options *headers;
  2058. /**< VHOST: pointer to optional linked list of per-vhost
  2059. * canned headers that are added to server responses */
  2060. const struct lws_protocol_vhost_options *reject_service_keywords;
  2061. /**< CONTEXT: Optional list of keywords and rejection codes + text.
  2062. *
  2063. * The keywords are checked for existing in the user agent string.
  2064. *
  2065. * Eg, "badrobot" "404 Not Found"
  2066. */
  2067. void *external_baggage_free_on_destroy;
  2068. /**< CONTEXT: NULL, or pointer to something externally malloc'd, that
  2069. * should be freed when the context is destroyed. This allows you to
  2070. * automatically sync the freeing action to the context destruction
  2071. * action, so there is no need for an external free() if the context
  2072. * succeeded to create.
  2073. */
  2074. const char *client_ssl_private_key_password;
  2075. /**< VHOST: Client SSL context init: NULL or the passphrase needed
  2076. * for the private key */
  2077. const char *client_ssl_cert_filepath;
  2078. /**< VHOST: Client SSL context init:T he certificate the client
  2079. * should present to the peer on connection */
  2080. const char *client_ssl_private_key_filepath;
  2081. /**< VHOST: Client SSL context init: filepath to client private key
  2082. * if this is set to NULL but client_ssl_cert_filepath is set, you
  2083. * can handle the LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS
  2084. * callback of protocols[0] to allow setting of the private key directly
  2085. * via openSSL library calls */
  2086. const char *client_ssl_ca_filepath;
  2087. /**< VHOST: Client SSL context init: CA certificate filepath or NULL */
  2088. const char *client_ssl_cipher_list;
  2089. /**< VHOST: Client SSL context init: List of valid ciphers to use (eg,
  2090. * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
  2091. * or you can leave it as NULL to get "DEFAULT" */
  2092. const struct lws_plat_file_ops *fops;
  2093. /**< CONTEXT: NULL, or pointer to an array of fops structs, terminated
  2094. * by a sentinel with NULL .open.
  2095. *
  2096. * If NULL, lws provides just the platform file operations struct for
  2097. * backwards compatibility.
  2098. */
  2099. int simultaneous_ssl_restriction;
  2100. /**< CONTEXT: 0 (no limit) or limit of simultaneous SSL sessions possible.*/
  2101. const char *socks_proxy_address;
  2102. /**< VHOST: If non-NULL, attempts to proxy via the given address.
  2103. * If proxy auth is required, use format "username:password\@server:port" */
  2104. unsigned int socks_proxy_port;
  2105. /**< VHOST: If socks_proxy_address was non-NULL, uses this port */
  2106. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  2107. cap_value_t caps[4];
  2108. /**< CONTEXT: array holding Linux capabilities you want to
  2109. * continue to be available to the server after it transitions
  2110. * to a noprivileged user. Usually none are needed but for, eg,
  2111. * .bind_iface, CAP_NET_RAW is required. This gives you a way
  2112. * to still have the capability but drop root.
  2113. */
  2114. char count_caps;
  2115. /**< CONTEXT: count of Linux capabilities in .caps[]. 0 means
  2116. * no capabilities will be inherited from root (the default) */
  2117. #endif
  2118. int bind_iface;
  2119. /**< VHOST: nonzero to strictly bind sockets to the interface name in
  2120. * .iface (eg, "eth2"), using SO_BIND_TO_DEVICE.
  2121. *
  2122. * Requires SO_BINDTODEVICE support from your OS and CAP_NET_RAW
  2123. * capability.
  2124. *
  2125. * Notice that common things like access network interface IP from
  2126. * your local machine use your lo / loopback interface and will be
  2127. * disallowed by this.
  2128. */
  2129. int ssl_info_event_mask;
  2130. /**< VHOST: mask of ssl events to be reported on LWS_CALLBACK_SSL_INFO
  2131. * callback for connections on this vhost. The mask values are of
  2132. * the form SSL_CB_ALERT, defined in openssl/ssl.h. The default of
  2133. * 0 means no info events will be reported.
  2134. */
  2135. unsigned int timeout_secs_ah_idle;
  2136. /**< VHOST: seconds to allow a client to hold an ah without using it.
  2137. * 0 defaults to 10s. */
  2138. unsigned short ip_limit_ah;
  2139. /**< CONTEXT: max number of ah a single IP may use simultaneously
  2140. * 0 is no limit. This is a soft limit: if the limit is
  2141. * reached, connections from that IP will wait in the ah
  2142. * waiting list and not be able to acquire an ah until
  2143. * a connection belonging to the IP relinquishes one it
  2144. * already has.
  2145. */
  2146. unsigned short ip_limit_wsi;
  2147. /**< CONTEXT: max number of wsi a single IP may use simultaneously.
  2148. * 0 is no limit. This is a hard limit, connections from
  2149. * the same IP will simply be dropped once it acquires the
  2150. * amount of simultaneous wsi / accepted connections
  2151. * given here.
  2152. */
  2153. uint32_t http2_settings[7];
  2154. /**< CONTEXT: after context creation http2_settings[1] thru [6] have
  2155. * been set to the lws platform default values.
  2156. * VHOST: if http2_settings[0] is nonzero, the values given in
  2157. * http2_settings[1]..[6] are used instead of the lws
  2158. * platform default values.
  2159. * Just leave all at 0 if you don't care.
  2160. */
  2161. /* Add new things just above here ---^
  2162. * This is part of the ABI, don't needlessly break compatibility
  2163. *
  2164. * The below is to ensure later library versions with new
  2165. * members added above will see 0 (default) even if the app
  2166. * was not built against the newer headers.
  2167. */
  2168. void *_unused[8]; /**< dummy */
  2169. };
  2170. /**
  2171. * lws_create_context() - Create the websocket handler
  2172. * \param info: pointer to struct with parameters
  2173. *
  2174. * This function creates the listening socket (if serving) and takes care
  2175. * of all initialization in one step.
  2176. *
  2177. * If option LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, no vhost is
  2178. * created; you're expected to create your own vhosts afterwards using
  2179. * lws_create_vhost(). Otherwise a vhost named "default" is also created
  2180. * using the information in the vhost-related members, for compatibility.
  2181. *
  2182. * After initialization, it returns a struct lws_context * that
  2183. * represents this server. After calling, user code needs to take care
  2184. * of calling lws_service() with the context pointer to get the
  2185. * server's sockets serviced. This must be done in the same process
  2186. * context as the initialization call.
  2187. *
  2188. * The protocol callback functions are called for a handful of events
  2189. * including http requests coming in, websocket connections becoming
  2190. * established, and data arriving; it's also called periodically to allow
  2191. * async transmission.
  2192. *
  2193. * HTTP requests are sent always to the FIRST protocol in protocol, since
  2194. * at that time websocket protocol has not been negotiated. Other
  2195. * protocols after the first one never see any HTTP callback activity.
  2196. *
  2197. * The server created is a simple http server by default; part of the
  2198. * websocket standard is upgrading this http connection to a websocket one.
  2199. *
  2200. * This allows the same server to provide files like scripts and favicon /
  2201. * images or whatever over http and dynamic data over websockets all in
  2202. * one place; they're all handled in the user callback.
  2203. */
  2204. LWS_VISIBLE LWS_EXTERN struct lws_context *
  2205. lws_create_context(struct lws_context_creation_info *info);
  2206. /**
  2207. * lws_context_destroy() - Destroy the websocket context
  2208. * \param context: Websocket context
  2209. *
  2210. * This function closes any active connections and then frees the
  2211. * context. After calling this, any further use of the context is
  2212. * undefined.
  2213. */
  2214. LWS_VISIBLE LWS_EXTERN void
  2215. lws_context_destroy(struct lws_context *context);
  2216. LWS_VISIBLE LWS_EXTERN void
  2217. lws_context_destroy2(struct lws_context *context);
  2218. typedef int (*lws_reload_func)(void);
  2219. /**
  2220. * lws_context_deprecate() - Deprecate the websocket context
  2221. *
  2222. * \param context: Websocket context
  2223. * \param cb: Callback notified when old context listen sockets are closed
  2224. *
  2225. * This function is used on an existing context before superceding it
  2226. * with a new context.
  2227. *
  2228. * It closes any listen sockets in the context, so new connections are
  2229. * not possible.
  2230. *
  2231. * And it marks the context to be deleted when the number of active
  2232. * connections into it falls to zero.
  2233. *
  2234. * Otherwise if you attach the deprecated context to the replacement
  2235. * context when it has been created using lws_context_attach_deprecated()
  2236. * both any deprecated and the new context will service their connections.
  2237. *
  2238. * This is aimed at allowing seamless configuration reloads.
  2239. *
  2240. * The callback cb will be called after the listen sockets are actually
  2241. * closed and may be reopened. In the callback the new context should be
  2242. * configured and created. (With libuv, socket close happens async after
  2243. * more loop events).
  2244. */
  2245. LWS_VISIBLE LWS_EXTERN void
  2246. lws_context_deprecate(struct lws_context *context, lws_reload_func cb);
  2247. LWS_VISIBLE LWS_EXTERN int
  2248. lws_context_is_deprecated(struct lws_context *context);
  2249. /**
  2250. * lws_set_proxy() - Setups proxy to lws_context.
  2251. * \param vhost: pointer to struct lws_vhost you want set proxy for
  2252. * \param proxy: pointer to c string containing proxy in format address:port
  2253. *
  2254. * Returns 0 if proxy string was parsed and proxy was setup.
  2255. * Returns -1 if proxy is NULL or has incorrect format.
  2256. *
  2257. * This is only required if your OS does not provide the http_proxy
  2258. * environment variable (eg, OSX)
  2259. *
  2260. * IMPORTANT! You should call this function right after creation of the
  2261. * lws_context and before call to connect. If you call this
  2262. * function after connect behavior is undefined.
  2263. * This function will override proxy settings made on lws_context
  2264. * creation with genenv() call.
  2265. */
  2266. LWS_VISIBLE LWS_EXTERN int
  2267. lws_set_proxy(struct lws_vhost *vhost, const char *proxy);
  2268. /**
  2269. * lws_set_socks() - Setup socks to lws_context.
  2270. * \param vhost: pointer to struct lws_vhost you want set socks for
  2271. * \param socks: pointer to c string containing socks in format address:port
  2272. *
  2273. * Returns 0 if socks string was parsed and socks was setup.
  2274. * Returns -1 if socks is NULL or has incorrect format.
  2275. *
  2276. * This is only required if your OS does not provide the socks_proxy
  2277. * environment variable (eg, OSX)
  2278. *
  2279. * IMPORTANT! You should call this function right after creation of the
  2280. * lws_context and before call to connect. If you call this
  2281. * function after connect behavior is undefined.
  2282. * This function will override proxy settings made on lws_context
  2283. * creation with genenv() call.
  2284. */
  2285. LWS_VISIBLE LWS_EXTERN int
  2286. lws_set_socks(struct lws_vhost *vhost, const char *socks);
  2287. struct lws_vhost;
  2288. /**
  2289. * lws_create_vhost() - Create a vhost (virtual server context)
  2290. * \param context: pointer to result of lws_create_context()
  2291. * \param info: pointer to struct with parameters
  2292. *
  2293. * This function creates a virtual server (vhost) using the vhost-related
  2294. * members of the info struct. You can create many vhosts inside one context
  2295. * if you created the context with the option LWS_SERVER_OPTION_EXPLICIT_VHOSTS
  2296. */
  2297. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  2298. lws_create_vhost(struct lws_context *context,
  2299. struct lws_context_creation_info *info);
  2300. /**
  2301. * lws_vhost_destroy() - Destroy a vhost (virtual server context)
  2302. *
  2303. * \param vh: pointer to result of lws_create_vhost()
  2304. *
  2305. * This function destroys a vhost. Normally, if you just want to exit,
  2306. * then lws_destroy_context() will take care of everything. If you want
  2307. * to destroy an individual vhost and all connections and allocations, you
  2308. * can do it with this.
  2309. *
  2310. * If the vhost has a listen sockets shared by other vhosts, it will be given
  2311. * to one of the vhosts sharing it rather than closed.
  2312. */
  2313. LWS_VISIBLE LWS_EXTERN void
  2314. lws_vhost_destroy(struct lws_vhost *vh);
  2315. /**
  2316. * lwsws_get_config_globals() - Parse a JSON server config file
  2317. * \param info: pointer to struct with parameters
  2318. * \param d: filepath of the config file
  2319. * \param config_strings: storage for the config strings extracted from JSON,
  2320. * the pointer is incremented as strings are stored
  2321. * \param len: pointer to the remaining length left in config_strings
  2322. * the value is decremented as strings are stored
  2323. *
  2324. * This function prepares a n lws_context_creation_info struct with global
  2325. * settings from a file d.
  2326. *
  2327. * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
  2328. */
  2329. LWS_VISIBLE LWS_EXTERN int
  2330. lwsws_get_config_globals(struct lws_context_creation_info *info, const char *d,
  2331. char **config_strings, int *len);
  2332. /**
  2333. * lwsws_get_config_vhosts() - Create vhosts from a JSON server config file
  2334. * \param context: pointer to result of lws_create_context()
  2335. * \param info: pointer to struct with parameters
  2336. * \param d: filepath of the config file
  2337. * \param config_strings: storage for the config strings extracted from JSON,
  2338. * the pointer is incremented as strings are stored
  2339. * \param len: pointer to the remaining length left in config_strings
  2340. * the value is decremented as strings are stored
  2341. *
  2342. * This function creates vhosts into a context according to the settings in
  2343. *JSON files found in directory d.
  2344. *
  2345. * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
  2346. */
  2347. LWS_VISIBLE LWS_EXTERN int
  2348. lwsws_get_config_vhosts(struct lws_context *context,
  2349. struct lws_context_creation_info *info, const char *d,
  2350. char **config_strings, int *len);
  2351. /** lws_vhost_get() - \deprecated deprecated: use lws_get_vhost() */
  2352. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  2353. lws_vhost_get(struct lws *wsi) LWS_WARN_DEPRECATED;
  2354. /**
  2355. * lws_get_vhost() - return the vhost a wsi belongs to
  2356. *
  2357. * \param wsi: which connection
  2358. */
  2359. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  2360. lws_get_vhost(struct lws *wsi);
  2361. /**
  2362. * lws_json_dump_vhost() - describe vhost state and stats in JSON
  2363. *
  2364. * \param vh: the vhost
  2365. * \param buf: buffer to fill with JSON
  2366. * \param len: max length of buf
  2367. */
  2368. LWS_VISIBLE LWS_EXTERN int
  2369. lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len);
  2370. /**
  2371. * lws_json_dump_context() - describe context state and stats in JSON
  2372. *
  2373. * \param context: the context
  2374. * \param buf: buffer to fill with JSON
  2375. * \param len: max length of buf
  2376. * \param hide_vhosts: nonzero to not provide per-vhost mount etc information
  2377. *
  2378. * Generates a JSON description of vhost state into buf
  2379. */
  2380. LWS_VISIBLE LWS_EXTERN int
  2381. lws_json_dump_context(const struct lws_context *context, char *buf, int len,
  2382. int hide_vhosts);
  2383. /**
  2384. * lws_vhost_user() - get the user data associated with the vhost
  2385. * \param vhost: Websocket vhost
  2386. *
  2387. * This returns the optional user pointer that can be attached to
  2388. * a vhost when it was created. Lws never dereferences this pointer, it only
  2389. * sets it when the vhost is created, and returns it using this api.
  2390. */
  2391. LWS_VISIBLE LWS_EXTERN void *
  2392. lws_vhost_user(struct lws_vhost *vhost);
  2393. /**
  2394. * lws_context_user() - get the user data associated with the context
  2395. * \param context: Websocket context
  2396. *
  2397. * This returns the optional user allocation that can be attached to
  2398. * the context the sockets live in at context_create time. It's a way
  2399. * to let all sockets serviced in the same context share data without
  2400. * using globals statics in the user code.
  2401. */
  2402. LWS_VISIBLE LWS_EXTERN void *
  2403. lws_context_user(struct lws_context *context);
  2404. /*! \defgroup vhost-mounts Vhost mounts and options
  2405. * \ingroup context-and-vhost-creation
  2406. *
  2407. * ##Vhost mounts and options
  2408. */
  2409. ///@{
  2410. /** struct lws_protocol_vhost_options - linked list of per-vhost protocol
  2411. * name=value options
  2412. *
  2413. * This provides a general way to attach a linked-list of name=value pairs,
  2414. * which can also have an optional child link-list using the options member.
  2415. */
  2416. struct lws_protocol_vhost_options {
  2417. const struct lws_protocol_vhost_options *next; /**< linked list */
  2418. const struct lws_protocol_vhost_options *options; /**< child linked-list of more options for this node */
  2419. const char *name; /**< name of name=value pair */
  2420. const char *value; /**< value of name=value pair */
  2421. };
  2422. /** enum lws_mount_protocols
  2423. * This specifies the mount protocol for a mountpoint, whether it is to be
  2424. * served from a filesystem, or it is a cgi etc.
  2425. */
  2426. enum lws_mount_protocols {
  2427. LWSMPRO_HTTP = 0, /**< http reverse proxy */
  2428. LWSMPRO_HTTPS = 1, /**< https reverse proxy */
  2429. LWSMPRO_FILE = 2, /**< serve from filesystem directory */
  2430. LWSMPRO_CGI = 3, /**< pass to CGI to handle */
  2431. LWSMPRO_REDIR_HTTP = 4, /**< redirect to http:// url */
  2432. LWSMPRO_REDIR_HTTPS = 5, /**< redirect to https:// url */
  2433. LWSMPRO_CALLBACK = 6, /**< hand by named protocol's callback */
  2434. };
  2435. /** struct lws_http_mount
  2436. *
  2437. * arguments for mounting something in a vhost's url namespace
  2438. */
  2439. struct lws_http_mount {
  2440. const struct lws_http_mount *mount_next;
  2441. /**< pointer to next struct lws_http_mount */
  2442. const char *mountpoint;
  2443. /**< mountpoint in http pathspace, eg, "/" */
  2444. const char *origin;
  2445. /**< path to be mounted, eg, "/var/www/warmcat.com" */
  2446. const char *def;
  2447. /**< default target, eg, "index.html" */
  2448. const char *protocol;
  2449. /**<"protocol-name" to handle mount */
  2450. const struct lws_protocol_vhost_options *cgienv;
  2451. /**< optional linked-list of cgi options. These are created
  2452. * as environment variables for the cgi process
  2453. */
  2454. const struct lws_protocol_vhost_options *extra_mimetypes;
  2455. /**< optional linked-list of mimetype mappings */
  2456. const struct lws_protocol_vhost_options *interpret;
  2457. /**< optional linked-list of files to be interpreted */
  2458. int cgi_timeout;
  2459. /**< seconds cgi is allowed to live, if cgi://mount type */
  2460. int cache_max_age;
  2461. /**< max-age for reuse of client cache of files, seconds */
  2462. unsigned int auth_mask;
  2463. /**< bits set here must be set for authorized client session */
  2464. unsigned int cache_reusable:1; /**< set if client cache may reuse this */
  2465. unsigned int cache_revalidate:1; /**< set if client cache should revalidate on use */
  2466. unsigned int cache_intermediaries:1; /**< set if intermediaries are allowed to cache */
  2467. unsigned char origin_protocol; /**< one of enum lws_mount_protocols */
  2468. unsigned char mountpoint_len; /**< length of mountpoint string */
  2469. const char *basic_auth_login_file;
  2470. /**<NULL, or filepath to use to check basic auth logins against */
  2471. /* Add new things just above here ---^
  2472. * This is part of the ABI, don't needlessly break compatibility
  2473. *
  2474. * The below is to ensure later library versions with new
  2475. * members added above will see 0 (default) even if the app
  2476. * was not built against the newer headers.
  2477. */
  2478. void *_unused[2]; /**< dummy */
  2479. };
  2480. ///@}
  2481. ///@}
  2482. /*! \defgroup client Client related functions
  2483. * ##Client releated functions
  2484. * \ingroup lwsapi
  2485. *
  2486. * */
  2487. ///@{
  2488. /** enum lws_client_connect_ssl_connection_flags - flags that may be used
  2489. * with struct lws_client_connect_info ssl_connection member to control if
  2490. * and how SSL checks apply to the client connection being created
  2491. */
  2492. enum lws_client_connect_ssl_connection_flags {
  2493. LCCSCF_USE_SSL = (1 << 0),
  2494. LCCSCF_ALLOW_SELFSIGNED = (1 << 1),
  2495. LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK = (1 << 2),
  2496. LCCSCF_ALLOW_EXPIRED = (1 << 3)
  2497. };
  2498. /** struct lws_client_connect_info - parameters to connect with when using
  2499. * lws_client_connect_via_info() */
  2500. struct lws_client_connect_info {
  2501. struct lws_context *context;
  2502. /**< lws context to create connection in */
  2503. const char *address;
  2504. /**< remote address to connect to */
  2505. int port;
  2506. /**< remote port to connect to */
  2507. int ssl_connection;
  2508. /**< nonzero for ssl */
  2509. const char *path;
  2510. /**< uri path */
  2511. const char *host;
  2512. /**< content of host header */
  2513. const char *origin;
  2514. /**< content of origin header */
  2515. const char *protocol;
  2516. /**< list of ws protocols we could accept */
  2517. int ietf_version_or_minus_one;
  2518. /**< deprecated: currently leave at 0 or -1 */
  2519. void *userdata;
  2520. /**< if non-NULL, use this as wsi user_data instead of malloc it */
  2521. const void *client_exts;
  2522. /**< UNUSED... provide in info.extensions at context creation time */
  2523. const char *method;
  2524. /**< if non-NULL, do this http method instead of ws[s] upgrade.
  2525. * use "GET" to be a simple http client connection */
  2526. struct lws *parent_wsi;
  2527. /**< if another wsi is responsible for this connection, give it here.
  2528. * this is used to make sure if the parent closes so do any
  2529. * child connections first. */
  2530. const char *uri_replace_from;
  2531. /**< if non-NULL, when this string is found in URIs in
  2532. * text/html content-encoding, it's replaced with uri_replace_to */
  2533. const char *uri_replace_to;
  2534. /**< see uri_replace_from */
  2535. struct lws_vhost *vhost;
  2536. /**< vhost to bind to (used to determine related SSL_CTX) */
  2537. struct lws **pwsi;
  2538. /**< if not NULL, store the new wsi here early in the connection
  2539. * process. Although we return the new wsi, the call to create the
  2540. * client connection does progress the connection somewhat and may
  2541. * meet an error that will result in the connection being scrubbed and
  2542. * NULL returned. While the wsi exists though, he may process a
  2543. * callback like CLIENT_CONNECTION_ERROR with his wsi: this gives the
  2544. * user callback a way to identify which wsi it is that faced the error
  2545. * even before the new wsi is returned and even if ultimately no wsi
  2546. * is returned.
  2547. */
  2548. const char *iface;
  2549. /**< NULL to allow routing on any interface, or interface name or IP
  2550. * to bind the socket to */
  2551. /* Add new things just above here ---^
  2552. * This is part of the ABI, don't needlessly break compatibility
  2553. *
  2554. * The below is to ensure later library versions with new
  2555. * members added above will see 0 (default) even if the app
  2556. * was not built against the newer headers.
  2557. */
  2558. void *_unused[4]; /**< dummy */
  2559. };
  2560. /**
  2561. * lws_client_connect_via_info() - Connect to another websocket server
  2562. * \param ccinfo: pointer to lws_client_connect_info struct
  2563. *
  2564. * This function creates a connection to a remote server using the
  2565. * information provided in ccinfo.
  2566. */
  2567. LWS_VISIBLE LWS_EXTERN struct lws *
  2568. lws_client_connect_via_info(struct lws_client_connect_info * ccinfo);
  2569. /**
  2570. * lws_client_connect() - Connect to another websocket server
  2571. * \deprecated DEPRECATED use lws_client_connect_via_info
  2572. * \param clients: Websocket context
  2573. * \param address: Remote server address, eg, "myserver.com"
  2574. * \param port: Port to connect to on the remote server, eg, 80
  2575. * \param ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
  2576. * signed certs
  2577. * \param path: Websocket path on server
  2578. * \param host: Hostname on server
  2579. * \param origin: Socket origin name
  2580. * \param protocol: Comma-separated list of protocols being asked for from
  2581. * the server, or just one. The server will pick the one it
  2582. * likes best. If you don't want to specify a protocol, which is
  2583. * legal, use NULL here.
  2584. * \param ietf_version_or_minus_one: -1 to ask to connect using the default, latest
  2585. * protocol supported, or the specific protocol ordinal
  2586. *
  2587. * This function creates a connection to a remote server
  2588. */
  2589. /* deprecated, use lws_client_connect_via_info() */
  2590. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  2591. lws_client_connect(struct lws_context *clients, const char *address,
  2592. int port, int ssl_connection, const char *path,
  2593. const char *host, const char *origin, const char *protocol,
  2594. int ietf_version_or_minus_one) LWS_WARN_DEPRECATED;
  2595. /* deprecated, use lws_client_connect_via_info() */
  2596. /**
  2597. * lws_client_connect_extended() - Connect to another websocket server
  2598. * \deprecated DEPRECATED use lws_client_connect_via_info
  2599. * \param clients: Websocket context
  2600. * \param address: Remote server address, eg, "myserver.com"
  2601. * \param port: Port to connect to on the remote server, eg, 80
  2602. * \param ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
  2603. * signed certs
  2604. * \param path: Websocket path on server
  2605. * \param host: Hostname on server
  2606. * \param origin: Socket origin name
  2607. * \param protocol: Comma-separated list of protocols being asked for from
  2608. * the server, or just one. The server will pick the one it
  2609. * likes best.
  2610. * \param ietf_version_or_minus_one: -1 to ask to connect using the default, latest
  2611. * protocol supported, or the specific protocol ordinal
  2612. * \param userdata: Pre-allocated user data
  2613. *
  2614. * This function creates a connection to a remote server
  2615. */
  2616. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  2617. lws_client_connect_extended(struct lws_context *clients, const char *address,
  2618. int port, int ssl_connection, const char *path,
  2619. const char *host, const char *origin,
  2620. const char *protocol, int ietf_version_or_minus_one,
  2621. void *userdata) LWS_WARN_DEPRECATED;
  2622. /**
  2623. * lws_init_vhost_client_ssl() - also enable client SSL on an existing vhost
  2624. *
  2625. * \param info: client ssl related info
  2626. * \param vhost: which vhost to initialize client ssl operations on
  2627. *
  2628. * You only need to call this if you plan on using SSL client connections on
  2629. * the vhost. For non-SSL client connections, it's not necessary to call this.
  2630. *
  2631. * The following members of info are used during the call
  2632. *
  2633. * - options must have LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT set,
  2634. * otherwise the call does nothing
  2635. * - provided_client_ssl_ctx must be NULL to get a generated client
  2636. * ssl context, otherwise you can pass a prepared one in by setting it
  2637. * - ssl_cipher_list may be NULL or set to the client valid cipher list
  2638. * - ssl_ca_filepath may be NULL or client cert filepath
  2639. * - ssl_cert_filepath may be NULL or client cert filepath
  2640. * - ssl_private_key_filepath may be NULL or client cert private key
  2641. *
  2642. * You must create your vhost explicitly if you want to use this, so you have
  2643. * a pointer to the vhost. Create the context first with the option flag
  2644. * LWS_SERVER_OPTION_EXPLICIT_VHOSTS and then call lws_create_vhost() with
  2645. * the same info struct.
  2646. */
  2647. LWS_VISIBLE LWS_EXTERN int
  2648. lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
  2649. struct lws_vhost *vhost);
  2650. /**
  2651. * lws_http_client_read() - consume waiting received http client data
  2652. *
  2653. * \param wsi: client connection
  2654. * \param buf: pointer to buffer pointer - fill with pointer to your buffer
  2655. * \param len: pointer to chunk length - fill with max length of buffer
  2656. *
  2657. * This is called when the user code is notified client http data has arrived.
  2658. * The user code may choose to delay calling it to consume the data, for example
  2659. * waiting until an onward connection is writeable.
  2660. *
  2661. * For non-chunked connections, up to len bytes of buf are filled with the
  2662. * received content. len is set to the actual amount filled before return.
  2663. *
  2664. * For chunked connections, the linear buffer content contains the chunking
  2665. * headers and it cannot be passed in one lump. Instead, this function will
  2666. * call back LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ with in pointing to the
  2667. * chunk start and len set to the chunk length. There will be as many calls
  2668. * as there are chunks or partial chunks in the buffer.
  2669. */
  2670. LWS_VISIBLE LWS_EXTERN int
  2671. lws_http_client_read(struct lws *wsi, char **buf, int *len);
  2672. /**
  2673. * lws_http_client_http_response() - get last HTTP response code
  2674. *
  2675. * \param wsi: client connection
  2676. *
  2677. * Returns the last server response code, eg, 200 for client http connections.
  2678. */
  2679. LWS_VISIBLE LWS_EXTERN unsigned int
  2680. lws_http_client_http_response(struct lws *wsi);
  2681. LWS_VISIBLE LWS_EXTERN void
  2682. lws_client_http_body_pending(struct lws *wsi, int something_left_to_send);
  2683. /**
  2684. * lws_client_http_body_pending() - control if client connection neeeds to send body
  2685. *
  2686. * \param wsi: client connection
  2687. * \param something_left_to_send: nonzero if need to send more body, 0 (default)
  2688. * if nothing more to send
  2689. *
  2690. * If you will send payload data with your HTTP client connection, eg, for POST,
  2691. * when you set the related http headers in
  2692. * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER callback you should also call
  2693. * this API with something_left_to_send nonzero, and call
  2694. * lws_callback_on_writable(wsi);
  2695. *
  2696. * After sending the headers, lws will call your callback with
  2697. * LWS_CALLBACK_CLIENT_HTTP_WRITEABLE reason when writable. You can send the
  2698. * next part of the http body payload, calling lws_callback_on_writable(wsi);
  2699. * if there is more to come, or lws_client_http_body_pending(wsi, 0); to
  2700. * let lws know the last part is sent and the connection can move on.
  2701. */
  2702. ///@}
  2703. /** \defgroup service Built-in service loop entry
  2704. *
  2705. * ##Built-in service loop entry
  2706. *
  2707. * If you're not using libev / libuv, these apis are needed to enter the poll()
  2708. * wait in lws and service any connections with pending events.
  2709. */
  2710. ///@{
  2711. /**
  2712. * lws_service() - Service any pending websocket activity
  2713. * \param context: Websocket context
  2714. * \param timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
  2715. * service otherwise block and service immediately, returning
  2716. * after the timeout if nothing needed service.
  2717. *
  2718. * This function deals with any pending websocket traffic, for three
  2719. * kinds of event. It handles these events on both server and client
  2720. * types of connection the same.
  2721. *
  2722. * 1) Accept new connections to our context's server
  2723. *
  2724. * 2) Call the receive callback for incoming frame data received by
  2725. * server or client connections.
  2726. *
  2727. * You need to call this service function periodically to all the above
  2728. * functions to happen; if your application is single-threaded you can
  2729. * just call it in your main event loop.
  2730. *
  2731. * Alternatively you can fork a new process that asynchronously handles
  2732. * calling this service in a loop. In that case you are happy if this
  2733. * call blocks your thread until it needs to take care of something and
  2734. * would call it with a large nonzero timeout. Your loop then takes no
  2735. * CPU while there is nothing happening.
  2736. *
  2737. * If you are calling it in a single-threaded app, you don't want it to
  2738. * wait around blocking other things in your loop from happening, so you
  2739. * would call it with a timeout_ms of 0, so it returns immediately if
  2740. * nothing is pending, or as soon as it services whatever was pending.
  2741. */
  2742. LWS_VISIBLE LWS_EXTERN int
  2743. lws_service(struct lws_context *context, int timeout_ms);
  2744. /**
  2745. * lws_service_tsi() - Service any pending websocket activity
  2746. *
  2747. * \param context: Websocket context
  2748. * \param timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
  2749. * service otherwise block and service immediately, returning
  2750. * after the timeout if nothing needed service.
  2751. * \param tsi: Thread service index, starting at 0
  2752. *
  2753. * Same as lws_service(), but for a specific thread service index. Only needed
  2754. * if you are spawning multiple service threads.
  2755. */
  2756. LWS_VISIBLE LWS_EXTERN int
  2757. lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
  2758. /**
  2759. * lws_cancel_service_pt() - Cancel servicing of pending socket activity
  2760. * on one thread
  2761. * \param wsi: Cancel service on the thread this wsi is serviced by
  2762. *
  2763. * This function lets a call to lws_service() waiting for a timeout
  2764. * immediately return.
  2765. *
  2766. * It works by creating a phony event and then swallowing it silently.
  2767. *
  2768. * The reason it may be needed is when waiting in poll(), changes to
  2769. * the event masks are ignored by the OS until poll() is reentered. This
  2770. * lets you halt the poll() wait and make the reentry happen immediately
  2771. * instead of having the wait out the rest of the poll timeout.
  2772. */
  2773. LWS_VISIBLE LWS_EXTERN void
  2774. lws_cancel_service_pt(struct lws *wsi);
  2775. /**
  2776. * lws_cancel_service() - Cancel wait for new pending socket activity
  2777. * \param context: Websocket context
  2778. *
  2779. * This function let a call to lws_service() waiting for a timeout
  2780. * immediately return.
  2781. *
  2782. * What it basically does is provide a fake event that will be swallowed,
  2783. * so the wait in poll() is ended. That's useful because poll() doesn't
  2784. * attend to changes in POLLIN/OUT/ERR until it re-enters the wait.
  2785. */
  2786. LWS_VISIBLE LWS_EXTERN void
  2787. lws_cancel_service(struct lws_context *context);
  2788. /**
  2789. * lws_service_fd() - Service polled socket with something waiting
  2790. * \param context: Websocket context
  2791. * \param pollfd: The pollfd entry describing the socket fd and which events
  2792. * happened, or NULL to tell lws to do only timeout servicing.
  2793. *
  2794. * This function takes a pollfd that has POLLIN or POLLOUT activity and
  2795. * services it according to the state of the associated
  2796. * struct lws.
  2797. *
  2798. * The one call deals with all "service" that might happen on a socket
  2799. * including listen accepts, http files as well as websocket protocol.
  2800. *
  2801. * If a pollfd says it has something, you can just pass it to
  2802. * lws_service_fd() whether it is a socket handled by lws or not.
  2803. * If it sees it is a lws socket, the traffic will be handled and
  2804. * pollfd->revents will be zeroed now.
  2805. *
  2806. * If the socket is foreign to lws, it leaves revents alone. So you can
  2807. * see if you should service yourself by checking the pollfd revents
  2808. * after letting lws try to service it.
  2809. *
  2810. * You should also call this with pollfd = NULL to just allow the
  2811. * once-per-second global timeout checks; if less than a second since the last
  2812. * check it returns immediately then.
  2813. */
  2814. LWS_VISIBLE LWS_EXTERN int
  2815. lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd);
  2816. /**
  2817. * lws_service_fd_tsi() - Service polled socket in specific service thread
  2818. * \param context: Websocket context
  2819. * \param pollfd: The pollfd entry describing the socket fd and which events
  2820. * happened.
  2821. * \param tsi: thread service index
  2822. *
  2823. * Same as lws_service_fd() but used with multiple service threads
  2824. */
  2825. LWS_VISIBLE LWS_EXTERN int
  2826. lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd,
  2827. int tsi);
  2828. /**
  2829. * lws_service_adjust_timeout() - Check for any connection needing forced service
  2830. * \param context: Websocket context
  2831. * \param timeout_ms: The original poll timeout value. You can just set this
  2832. * to 1 if you don't really have a poll timeout.
  2833. * \param tsi: thread service index
  2834. *
  2835. * Under some conditions connections may need service even though there is no
  2836. * pending network action on them, this is "forced service". For default
  2837. * poll() and libuv / libev, the library takes care of calling this and
  2838. * dealing with it for you. But for external poll() integration, you need
  2839. * access to the apis.
  2840. *
  2841. * If anybody needs "forced service", returned timeout is zero. In that case,
  2842. * you can call lws_service_tsi() with a timeout of -1 to only service
  2843. * guys who need forced service.
  2844. */
  2845. LWS_VISIBLE LWS_EXTERN int
  2846. lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
  2847. /* Backwards compatibility */
  2848. #define lws_plat_service_tsi lws_service_tsi
  2849. LWS_VISIBLE LWS_EXTERN int
  2850. lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
  2851. ///@}
  2852. /*! \defgroup http HTTP
  2853. Modules related to handling HTTP
  2854. */
  2855. //@{
  2856. /*! \defgroup httpft HTTP File transfer
  2857. * \ingroup http
  2858. APIs for sending local files in response to HTTP requests
  2859. */
  2860. //@{
  2861. /**
  2862. * lws_get_mimetype() - Determine mimetype to use from filename
  2863. *
  2864. * \param file: filename
  2865. * \param m: NULL, or mount context
  2866. *
  2867. * This uses a canned list of known filetypes first, if no match and m is
  2868. * non-NULL, then tries a list of per-mount file suffix to mimtype mappings.
  2869. *
  2870. * Returns either NULL or a pointer to the mimetype matching the file.
  2871. */
  2872. LWS_VISIBLE LWS_EXTERN const char *
  2873. lws_get_mimetype(const char *file, const struct lws_http_mount *m);
  2874. /**
  2875. * lws_serve_http_file() - Send a file back to the client using http
  2876. * \param wsi: Websocket instance (available from user callback)
  2877. * \param file: The file to issue over http
  2878. * \param content_type: The http content type, eg, text/html
  2879. * \param other_headers: NULL or pointer to header string
  2880. * \param other_headers_len: length of the other headers if non-NULL
  2881. *
  2882. * This function is intended to be called from the callback in response
  2883. * to http requests from the client. It allows the callback to issue
  2884. * local files down the http link in a single step.
  2885. *
  2886. * Returning <0 indicates error and the wsi should be closed. Returning
  2887. * >0 indicates the file was completely sent and
  2888. * lws_http_transaction_completed() called on the wsi (and close if != 0)
  2889. * ==0 indicates the file transfer is started and needs more service later,
  2890. * the wsi should be left alone.
  2891. */
  2892. LWS_VISIBLE LWS_EXTERN int
  2893. lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
  2894. const char *other_headers, int other_headers_len);
  2895. LWS_VISIBLE LWS_EXTERN int
  2896. lws_serve_http_file_fragment(struct lws *wsi);
  2897. //@}
  2898. enum http_status {
  2899. HTTP_STATUS_CONTINUE = 100,
  2900. HTTP_STATUS_OK = 200,
  2901. HTTP_STATUS_NO_CONTENT = 204,
  2902. HTTP_STATUS_PARTIAL_CONTENT = 206,
  2903. HTTP_STATUS_MOVED_PERMANENTLY = 301,
  2904. HTTP_STATUS_FOUND = 302,
  2905. HTTP_STATUS_SEE_OTHER = 303,
  2906. HTTP_STATUS_NOT_MODIFIED = 304,
  2907. HTTP_STATUS_BAD_REQUEST = 400,
  2908. HTTP_STATUS_UNAUTHORIZED,
  2909. HTTP_STATUS_PAYMENT_REQUIRED,
  2910. HTTP_STATUS_FORBIDDEN,
  2911. HTTP_STATUS_NOT_FOUND,
  2912. HTTP_STATUS_METHOD_NOT_ALLOWED,
  2913. HTTP_STATUS_NOT_ACCEPTABLE,
  2914. HTTP_STATUS_PROXY_AUTH_REQUIRED,
  2915. HTTP_STATUS_REQUEST_TIMEOUT,
  2916. HTTP_STATUS_CONFLICT,
  2917. HTTP_STATUS_GONE,
  2918. HTTP_STATUS_LENGTH_REQUIRED,
  2919. HTTP_STATUS_PRECONDITION_FAILED,
  2920. HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
  2921. HTTP_STATUS_REQ_URI_TOO_LONG,
  2922. HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
  2923. HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
  2924. HTTP_STATUS_EXPECTATION_FAILED,
  2925. HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
  2926. HTTP_STATUS_NOT_IMPLEMENTED,
  2927. HTTP_STATUS_BAD_GATEWAY,
  2928. HTTP_STATUS_SERVICE_UNAVAILABLE,
  2929. HTTP_STATUS_GATEWAY_TIMEOUT,
  2930. HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
  2931. };
  2932. /*! \defgroup html-chunked-substitution HTML Chunked Substitution
  2933. * \ingroup http
  2934. *
  2935. * ##HTML chunked Substitution
  2936. *
  2937. * APIs for receiving chunks of text, replacing a set of variable names via
  2938. * a callback, and then prepending and appending HTML chunked encoding
  2939. * headers.
  2940. */
  2941. //@{
  2942. struct lws_process_html_args {
  2943. char *p; /**< pointer to the buffer containing the data */
  2944. int len; /**< length of the original data at p */
  2945. int max_len; /**< maximum length we can grow the data to */
  2946. int final; /**< set if this is the last chunk of the file */
  2947. };
  2948. typedef const char *(*lws_process_html_state_cb)(void *data, int index);
  2949. struct lws_process_html_state {
  2950. char *start; /**< pointer to start of match */
  2951. char swallow[16]; /**< matched character buffer */
  2952. int pos; /**< position in match */
  2953. void *data; /**< opaque pointer */
  2954. const char * const *vars; /**< list of variable names */
  2955. int count_vars; /**< count of variable names */
  2956. lws_process_html_state_cb replace; /**< called on match to perform substitution */
  2957. };
  2958. /*! lws_chunked_html_process() - generic chunked substitution
  2959. * \param args: buffer to process using chunked encoding
  2960. * \param s: current processing state
  2961. */
  2962. LWS_VISIBLE LWS_EXTERN int
  2963. lws_chunked_html_process(struct lws_process_html_args *args,
  2964. struct lws_process_html_state *s);
  2965. //@}
  2966. /** \defgroup HTTP-headers-read HTTP headers: read
  2967. * \ingroup http
  2968. *
  2969. * ##HTTP header releated functions
  2970. *
  2971. * In lws the client http headers are temporarily stored in a pool, only for the
  2972. * duration of the http part of the handshake. It's because in most cases,
  2973. * the header content is ignored for the whole rest of the connection lifetime
  2974. * and would then just be taking up space needlessly.
  2975. *
  2976. * During LWS_CALLBACK_HTTP when the URI path is delivered is the last time
  2977. * the http headers are still allocated, you can use these apis then to
  2978. * look at and copy out interesting header content (cookies, etc)
  2979. *
  2980. * Notice that the header total length reported does not include a terminating
  2981. * '\0', however you must allocate for it when using the _copy apis. So the
  2982. * length reported for a header containing "123" is 3, but you must provide
  2983. * a buffer of length 4 so that "123\0" may be copied into it, or the copy
  2984. * will fail with a nonzero return code.
  2985. *
  2986. * In the special case of URL arguments, like ?x=1&y=2, the arguments are
  2987. * stored in a token named for the method, eg, WSI_TOKEN_GET_URI if it
  2988. * was a GET or WSI_TOKEN_POST_URI if POST. You can check the total
  2989. * length to confirm the method.
  2990. *
  2991. * For URL arguments, each argument is stored urldecoded in a "fragment", so
  2992. * you can use the fragment-aware api lws_hdr_copy_fragment() to access each
  2993. * argument in turn: the fragments contain urldecoded strings like x=1 or y=2.
  2994. *
  2995. * As a convenience, lws has an api that will find the fragment with a
  2996. * given name= part, lws_get_urlarg_by_name().
  2997. */
  2998. ///@{
  2999. /** struct lws_tokens
  3000. * you need these to look at headers that have been parsed if using the
  3001. * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
  3002. * list below is absent, .token = NULL and token_len = 0. Otherwise .token
  3003. * points to .token_len chars containing that header content.
  3004. */
  3005. struct lws_tokens {
  3006. char *token; /**< pointer to start of the token */
  3007. int token_len; /**< length of the token's value */
  3008. };
  3009. /* enum lws_token_indexes
  3010. * these have to be kept in sync with lextable.h / minilex.c
  3011. *
  3012. * NOTE: These public enums are part of the abi. If you want to add one,
  3013. * add it at where specified so existing users are unaffected.
  3014. */
  3015. enum lws_token_indexes {
  3016. WSI_TOKEN_GET_URI = 0,
  3017. WSI_TOKEN_POST_URI = 1,
  3018. WSI_TOKEN_OPTIONS_URI = 2,
  3019. WSI_TOKEN_HOST = 3,
  3020. WSI_TOKEN_CONNECTION = 4,
  3021. WSI_TOKEN_UPGRADE = 5,
  3022. WSI_TOKEN_ORIGIN = 6,
  3023. WSI_TOKEN_DRAFT = 7,
  3024. WSI_TOKEN_CHALLENGE = 8,
  3025. WSI_TOKEN_EXTENSIONS = 9,
  3026. WSI_TOKEN_KEY1 = 10,
  3027. WSI_TOKEN_KEY2 = 11,
  3028. WSI_TOKEN_PROTOCOL = 12,
  3029. WSI_TOKEN_ACCEPT = 13,
  3030. WSI_TOKEN_NONCE = 14,
  3031. WSI_TOKEN_HTTP = 15,
  3032. WSI_TOKEN_HTTP2_SETTINGS = 16,
  3033. WSI_TOKEN_HTTP_ACCEPT = 17,
  3034. WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
  3035. WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
  3036. WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
  3037. WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
  3038. WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
  3039. WSI_TOKEN_HTTP_PRAGMA = 23,
  3040. WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
  3041. WSI_TOKEN_HTTP_AUTHORIZATION = 25,
  3042. WSI_TOKEN_HTTP_COOKIE = 26,
  3043. WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
  3044. WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
  3045. WSI_TOKEN_HTTP_DATE = 29,
  3046. WSI_TOKEN_HTTP_RANGE = 30,
  3047. WSI_TOKEN_HTTP_REFERER = 31,
  3048. WSI_TOKEN_KEY = 32,
  3049. WSI_TOKEN_VERSION = 33,
  3050. WSI_TOKEN_SWORIGIN = 34,
  3051. WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
  3052. WSI_TOKEN_HTTP_COLON_METHOD = 36,
  3053. WSI_TOKEN_HTTP_COLON_PATH = 37,
  3054. WSI_TOKEN_HTTP_COLON_SCHEME = 38,
  3055. WSI_TOKEN_HTTP_COLON_STATUS = 39,
  3056. WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
  3057. WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
  3058. WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
  3059. WSI_TOKEN_HTTP_AGE = 43,
  3060. WSI_TOKEN_HTTP_ALLOW = 44,
  3061. WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
  3062. WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
  3063. WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
  3064. WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
  3065. WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
  3066. WSI_TOKEN_HTTP_ETAG = 50,
  3067. WSI_TOKEN_HTTP_EXPECT = 51,
  3068. WSI_TOKEN_HTTP_EXPIRES = 52,
  3069. WSI_TOKEN_HTTP_FROM = 53,
  3070. WSI_TOKEN_HTTP_IF_MATCH = 54,
  3071. WSI_TOKEN_HTTP_IF_RANGE = 55,
  3072. WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
  3073. WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
  3074. WSI_TOKEN_HTTP_LINK = 58,
  3075. WSI_TOKEN_HTTP_LOCATION = 59,
  3076. WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
  3077. WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
  3078. WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
  3079. WSI_TOKEN_HTTP_REFRESH = 63,
  3080. WSI_TOKEN_HTTP_RETRY_AFTER = 64,
  3081. WSI_TOKEN_HTTP_SERVER = 65,
  3082. WSI_TOKEN_HTTP_SET_COOKIE = 66,
  3083. WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
  3084. WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
  3085. WSI_TOKEN_HTTP_USER_AGENT = 69,
  3086. WSI_TOKEN_HTTP_VARY = 70,
  3087. WSI_TOKEN_HTTP_VIA = 71,
  3088. WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
  3089. WSI_TOKEN_PATCH_URI = 73,
  3090. WSI_TOKEN_PUT_URI = 74,
  3091. WSI_TOKEN_DELETE_URI = 75,
  3092. WSI_TOKEN_HTTP_URI_ARGS = 76,
  3093. WSI_TOKEN_PROXY = 77,
  3094. WSI_TOKEN_HTTP_X_REAL_IP = 78,
  3095. WSI_TOKEN_HTTP1_0 = 79,
  3096. WSI_TOKEN_X_FORWARDED_FOR = 80,
  3097. WSI_TOKEN_CONNECT = 81,
  3098. WSI_TOKEN_HEAD_URI = 82,
  3099. WSI_TOKEN_TE = 83,
  3100. /****** add new things just above ---^ ******/
  3101. /* use token storage to stash these internally, not for
  3102. * user use */
  3103. _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
  3104. _WSI_TOKEN_CLIENT_PEER_ADDRESS,
  3105. _WSI_TOKEN_CLIENT_URI,
  3106. _WSI_TOKEN_CLIENT_HOST,
  3107. _WSI_TOKEN_CLIENT_ORIGIN,
  3108. _WSI_TOKEN_CLIENT_METHOD,
  3109. _WSI_TOKEN_CLIENT_IFACE,
  3110. /* always last real token index*/
  3111. WSI_TOKEN_COUNT,
  3112. /* parser state additions, no storage associated */
  3113. WSI_TOKEN_NAME_PART,
  3114. WSI_TOKEN_SKIPPING,
  3115. WSI_TOKEN_SKIPPING_SAW_CR,
  3116. WSI_PARSING_COMPLETE,
  3117. WSI_INIT_TOKEN_MUXURL,
  3118. };
  3119. struct lws_token_limits {
  3120. unsigned short token_limit[WSI_TOKEN_COUNT]; /**< max chars for this token */
  3121. };
  3122. /**
  3123. * lws_token_to_string() - returns a textual representation of a hdr token index
  3124. *
  3125. * \param token: token index
  3126. */
  3127. LWS_VISIBLE LWS_EXTERN const unsigned char *
  3128. lws_token_to_string(enum lws_token_indexes token);
  3129. /**
  3130. * lws_hdr_total_length: report length of all fragments of a header totalled up
  3131. * The returned length does not include the space for a
  3132. * terminating '\0'
  3133. *
  3134. * \param wsi: websocket connection
  3135. * \param h: which header index we are interested in
  3136. */
  3137. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3138. lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
  3139. /**
  3140. * lws_hdr_fragment_length: report length of a single fragment of a header
  3141. * The returned length does not include the space for a
  3142. * terminating '\0'
  3143. *
  3144. * \param wsi: websocket connection
  3145. * \param h: which header index we are interested in
  3146. * \param frag_idx: which fragment of h we want to get the length of
  3147. */
  3148. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3149. lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx);
  3150. /**
  3151. * lws_hdr_copy() - copy a single fragment of the given header to a buffer
  3152. * The buffer length len must include space for an additional
  3153. * terminating '\0', or it will fail returning -1.
  3154. *
  3155. * \param wsi: websocket connection
  3156. * \param dest: destination buffer
  3157. * \param len: length of destination buffer
  3158. * \param h: which header index we are interested in
  3159. *
  3160. * copies the whole, aggregated header, even if it was delivered in
  3161. * several actual headers piece by piece
  3162. */
  3163. LWS_VISIBLE LWS_EXTERN int
  3164. lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
  3165. /**
  3166. * lws_hdr_copy_fragment() - copy a single fragment of the given header to a buffer
  3167. * The buffer length len must include space for an additional
  3168. * terminating '\0', or it will fail returning -1.
  3169. * If the requested fragment index is not present, it fails
  3170. * returning -1.
  3171. *
  3172. * \param wsi: websocket connection
  3173. * \param dest: destination buffer
  3174. * \param len: length of destination buffer
  3175. * \param h: which header index we are interested in
  3176. * \param frag_idx: which fragment of h we want to copy
  3177. *
  3178. * Normally this is only useful
  3179. * to parse URI arguments like ?x=1&y=2, token index WSI_TOKEN_HTTP_URI_ARGS
  3180. * fragment 0 will contain "x=1" and fragment 1 "y=2"
  3181. */
  3182. LWS_VISIBLE LWS_EXTERN int
  3183. lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
  3184. enum lws_token_indexes h, int frag_idx);
  3185. /**
  3186. * lws_get_urlarg_by_name() - return pointer to arg value if present
  3187. * \param wsi: the connection to check
  3188. * \param name: the arg name, like "token="
  3189. * \param buf: the buffer to receive the urlarg (including the name= part)
  3190. * \param len: the length of the buffer to receive the urlarg
  3191. *
  3192. * Returns NULL if not found or a pointer inside buf to just after the
  3193. * name= part.
  3194. */
  3195. LWS_VISIBLE LWS_EXTERN const char *
  3196. lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
  3197. ///@}
  3198. /*! \defgroup HTTP-headers-create HTTP headers: create
  3199. *
  3200. * ## HTTP headers: Create
  3201. *
  3202. * These apis allow you to create HTTP response headers in a way compatible with
  3203. * both HTTP/1.x and HTTP/2.
  3204. *
  3205. * They each append to a buffer taking care about the buffer end, which is
  3206. * passed in as a pointer. When data is written to the buffer, the current
  3207. * position p is updated accordingly.
  3208. *
  3209. * All of these apis are LWS_WARN_UNUSED_RESULT as they can run out of space
  3210. * and fail with nonzero return.
  3211. */
  3212. ///@{
  3213. #define LWSAHH_CODE_MASK ((1 << 16) - 1)
  3214. #define LWSAHH_FLAG_NO_SERVER_NAME (1 << 30)
  3215. /**
  3216. * lws_add_http_header_status() - add the HTTP response status code
  3217. *
  3218. * \param wsi: the connection to check
  3219. * \param code: an HTTP code like 200, 404 etc (see enum http_status)
  3220. * \param p: pointer to current position in buffer pointer
  3221. * \param end: pointer to end of buffer
  3222. *
  3223. * Adds the initial response code, so should be called first.
  3224. *
  3225. * Code may additionally take OR'd flags:
  3226. *
  3227. * LWSAHH_FLAG_NO_SERVER_NAME: don't apply server name header this time
  3228. */
  3229. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3230. lws_add_http_header_status(struct lws *wsi,
  3231. unsigned int code, unsigned char **p,
  3232. unsigned char *end);
  3233. /**
  3234. * lws_add_http_header_by_name() - append named header and value
  3235. *
  3236. * \param wsi: the connection to check
  3237. * \param name: the hdr name, like "my-header"
  3238. * \param value: the value after the = for this header
  3239. * \param length: the length of the value
  3240. * \param p: pointer to current position in buffer pointer
  3241. * \param end: pointer to end of buffer
  3242. *
  3243. * Appends name: value to the headers
  3244. */
  3245. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3246. lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
  3247. const unsigned char *value, int length,
  3248. unsigned char **p, unsigned char *end);
  3249. /**
  3250. * lws_add_http_header_by_token() - append given header and value
  3251. *
  3252. * \param wsi: the connection to check
  3253. * \param token: the token index for the hdr
  3254. * \param value: the value after the = for this header
  3255. * \param length: the length of the value
  3256. * \param p: pointer to current position in buffer pointer
  3257. * \param end: pointer to end of buffer
  3258. *
  3259. * Appends name=value to the headers, but is able to take advantage of better
  3260. * HTTP/2 coding mechanisms where possible.
  3261. */
  3262. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3263. lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
  3264. const unsigned char *value, int length,
  3265. unsigned char **p, unsigned char *end);
  3266. /**
  3267. * lws_add_http_header_content_length() - append content-length helper
  3268. *
  3269. * \param wsi: the connection to check
  3270. * \param content_length: the content length to use
  3271. * \param p: pointer to current position in buffer pointer
  3272. * \param end: pointer to end of buffer
  3273. *
  3274. * Appends content-length: content_length to the headers
  3275. */
  3276. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3277. lws_add_http_header_content_length(struct lws *wsi,
  3278. lws_filepos_t content_length,
  3279. unsigned char **p, unsigned char *end);
  3280. /**
  3281. * lws_finalize_http_header() - terminate header block
  3282. *
  3283. * \param wsi: the connection to check
  3284. * \param p: pointer to current position in buffer pointer
  3285. * \param end: pointer to end of buffer
  3286. *
  3287. * Indicates no more headers will be added
  3288. */
  3289. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3290. lws_finalize_http_header(struct lws *wsi, unsigned char **p,
  3291. unsigned char *end);
  3292. ///@}
  3293. /** \defgroup form-parsing Form Parsing
  3294. * \ingroup http
  3295. * ##POSTed form parsing functions
  3296. *
  3297. * These lws_spa (stateful post arguments) apis let you parse and urldecode
  3298. * POSTed form arguments, both using simple urlencoded and multipart transfer
  3299. * encoding.
  3300. *
  3301. * It's capable of handling file uploads as well a named input parsing,
  3302. * and the apis are the same for both form upload styles.
  3303. *
  3304. * You feed it a list of parameter names and it creates pointers to the
  3305. * urldecoded arguments: file upload parameters pass the file data in chunks to
  3306. * a user-supplied callback as they come.
  3307. *
  3308. * Since it's stateful, it handles the incoming data needing more than one
  3309. * POST_BODY callback and has no limit on uploaded file size.
  3310. */
  3311. ///@{
  3312. /** enum lws_spa_fileupload_states */
  3313. enum lws_spa_fileupload_states {
  3314. LWS_UFS_CONTENT,
  3315. /**< a chunk of file content has arrived */
  3316. LWS_UFS_FINAL_CONTENT,
  3317. /**< the last chunk (possibly zero length) of file content has arrived */
  3318. LWS_UFS_OPEN
  3319. /**< a new file is starting to arrive */
  3320. };
  3321. /**
  3322. * lws_spa_fileupload_cb() - callback to receive file upload data
  3323. *
  3324. * \param data: opt_data pointer set in lws_spa_create
  3325. * \param name: name of the form field being uploaded
  3326. * \param filename: original filename from client
  3327. * \param buf: start of data to receive
  3328. * \param len: length of data to receive
  3329. * \param state: information about how this call relates to file
  3330. *
  3331. * Notice name and filename shouldn't be trusted, as they are passed from
  3332. * HTTP provided by the client.
  3333. */
  3334. typedef int (*lws_spa_fileupload_cb)(void *data, const char *name,
  3335. const char *filename, char *buf, int len,
  3336. enum lws_spa_fileupload_states state);
  3337. /** struct lws_spa - opaque urldecode parser capable of handling multipart
  3338. * and file uploads */
  3339. struct lws_spa;
  3340. /**
  3341. * lws_spa_create() - create urldecode parser
  3342. *
  3343. * \param wsi: lws connection (used to find Content Type)
  3344. * \param param_names: array of form parameter names, like "username"
  3345. * \param count_params: count of param_names
  3346. * \param max_storage: total amount of form parameter values we can store
  3347. * \param opt_cb: NULL, or callback to receive file upload data.
  3348. * \param opt_data: NULL, or user pointer provided to opt_cb.
  3349. *
  3350. * Creates a urldecode parser and initializes it.
  3351. *
  3352. * opt_cb can be NULL if you just want normal name=value parsing, however
  3353. * if one or more entries in your form are bulk data (file transfer), you
  3354. * can provide this callback and filter on the name callback parameter to
  3355. * treat that urldecoded data separately. The callback should return -1
  3356. * in case of fatal error, and 0 if OK.
  3357. */
  3358. LWS_VISIBLE LWS_EXTERN struct lws_spa *
  3359. lws_spa_create(struct lws *wsi, const char * const *param_names,
  3360. int count_params, int max_storage, lws_spa_fileupload_cb opt_cb,
  3361. void *opt_data);
  3362. /**
  3363. * lws_spa_process() - parses a chunk of input data
  3364. *
  3365. * \param spa: the parser object previously created
  3366. * \param in: incoming, urlencoded data
  3367. * \param len: count of bytes valid at \param in
  3368. */
  3369. LWS_VISIBLE LWS_EXTERN int
  3370. lws_spa_process(struct lws_spa *spa, const char *in, int len);
  3371. /**
  3372. * lws_spa_finalize() - indicate incoming data completed
  3373. *
  3374. * \param spa: the parser object previously created
  3375. */
  3376. LWS_VISIBLE LWS_EXTERN int
  3377. lws_spa_finalize(struct lws_spa *spa);
  3378. /**
  3379. * lws_spa_get_length() - return length of parameter value
  3380. *
  3381. * \param spa: the parser object previously created
  3382. * \param n: parameter ordinal to return length of value for
  3383. */
  3384. LWS_VISIBLE LWS_EXTERN int
  3385. lws_spa_get_length(struct lws_spa *spa, int n);
  3386. /**
  3387. * lws_spa_get_string() - return pointer to parameter value
  3388. * \param spa: the parser object previously created
  3389. * \param n: parameter ordinal to return pointer to value for
  3390. */
  3391. LWS_VISIBLE LWS_EXTERN const char *
  3392. lws_spa_get_string(struct lws_spa *spa, int n);
  3393. /**
  3394. * lws_spa_destroy() - destroy parser object
  3395. *
  3396. * \param spa: the parser object previously created
  3397. */
  3398. LWS_VISIBLE LWS_EXTERN int
  3399. lws_spa_destroy(struct lws_spa *spa);
  3400. ///@}
  3401. /*! \defgroup urlendec Urlencode and Urldecode
  3402. * \ingroup http
  3403. *
  3404. * ##HTML chunked Substitution
  3405. *
  3406. * APIs for receiving chunks of text, replacing a set of variable names via
  3407. * a callback, and then prepending and appending HTML chunked encoding
  3408. * headers.
  3409. */
  3410. //@{
  3411. /**
  3412. * lws_urlencode() - like strncpy but with urlencoding
  3413. *
  3414. * \param escaped: output buffer
  3415. * \param string: input buffer ('/0' terminated)
  3416. * \param len: output buffer max length
  3417. *
  3418. * Because urlencoding expands the output string, it's not
  3419. * possible to do it in-place, ie, with escaped == string
  3420. */
  3421. LWS_VISIBLE LWS_EXTERN const char *
  3422. lws_urlencode(char *escaped, const char *string, int len);
  3423. /*
  3424. * URLDECODE 1 / 2
  3425. *
  3426. * This simple urldecode only operates until the first '\0' and requires the
  3427. * data to exist all at once
  3428. */
  3429. /**
  3430. * lws_urldecode() - like strncpy but with urldecoding
  3431. *
  3432. * \param string: output buffer
  3433. * \param escaped: input buffer ('\0' terminated)
  3434. * \param len: output buffer max length
  3435. *
  3436. * This is only useful for '\0' terminated strings
  3437. *
  3438. * Since urldecoding only shrinks the output string, it is possible to
  3439. * do it in-place, ie, string == escaped
  3440. *
  3441. * Returns 0 if completed OK or nonzero for urldecode violation (non-hex chars
  3442. * where hex required, etc)
  3443. */
  3444. LWS_VISIBLE LWS_EXTERN int
  3445. lws_urldecode(char *string, const char *escaped, int len);
  3446. ///@}
  3447. /**
  3448. * lws_return_http_status() - Return simple http status
  3449. * \param wsi: Websocket instance (available from user callback)
  3450. * \param code: Status index, eg, 404
  3451. * \param html_body: User-readable HTML description < 1KB, or NULL
  3452. *
  3453. * Helper to report HTTP errors back to the client cleanly and
  3454. * consistently
  3455. */
  3456. LWS_VISIBLE LWS_EXTERN int
  3457. lws_return_http_status(struct lws *wsi, unsigned int code,
  3458. const char *html_body);
  3459. /**
  3460. * lws_http_redirect() - write http redirect into buffer
  3461. *
  3462. * \param wsi: websocket connection
  3463. * \param code: HTTP response code (eg, 301)
  3464. * \param loc: where to redirect to
  3465. * \param len: length of loc
  3466. * \param p: pointer current position in buffer (updated as we write)
  3467. * \param end: pointer to end of buffer
  3468. */
  3469. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3470. lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
  3471. unsigned char **p, unsigned char *end);
  3472. /**
  3473. * lws_http_transaction_completed() - wait for new http transaction or close
  3474. * \param wsi: websocket connection
  3475. *
  3476. * Returns 1 if the HTTP connection must close now
  3477. * Returns 0 and resets connection to wait for new HTTP header /
  3478. * transaction if possible
  3479. */
  3480. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3481. lws_http_transaction_completed(struct lws *wsi);
  3482. ///@}
  3483. /*! \defgroup pur Sanitize / purify SQL and JSON helpers
  3484. *
  3485. * ##Sanitize / purify SQL and JSON helpers
  3486. *
  3487. * APIs for escaping untrusted JSON and SQL safely before use
  3488. */
  3489. //@{
  3490. /**
  3491. * lws_sql_purify() - like strncpy but with escaping for sql quotes
  3492. *
  3493. * \param escaped: output buffer
  3494. * \param string: input buffer ('/0' terminated)
  3495. * \param len: output buffer max length
  3496. *
  3497. * Because escaping expands the output string, it's not
  3498. * possible to do it in-place, ie, with escaped == string
  3499. */
  3500. LWS_VISIBLE LWS_EXTERN const char *
  3501. lws_sql_purify(char *escaped, const char *string, int len);
  3502. /**
  3503. * lws_json_purify() - like strncpy but with escaping for json chars
  3504. *
  3505. * \param escaped: output buffer
  3506. * \param string: input buffer ('/0' terminated)
  3507. * \param len: output buffer max length
  3508. *
  3509. * Because escaping expands the output string, it's not
  3510. * possible to do it in-place, ie, with escaped == string
  3511. */
  3512. LWS_VISIBLE LWS_EXTERN const char *
  3513. lws_json_purify(char *escaped, const char *string, int len);
  3514. ///@}
  3515. /*! \defgroup ev libev helpers
  3516. *
  3517. * ##libev helpers
  3518. *
  3519. * APIs specific to libev event loop itegration
  3520. */
  3521. ///@{
  3522. #ifdef LWS_WITH_LIBEV
  3523. typedef void (lws_ev_signal_cb_t)(EV_P_ struct ev_signal *w, int revents);
  3524. LWS_VISIBLE LWS_EXTERN int
  3525. lws_ev_sigint_cfg(struct lws_context *context, int use_ev_sigint,
  3526. lws_ev_signal_cb_t *cb);
  3527. LWS_VISIBLE LWS_EXTERN int
  3528. lws_ev_initloop(struct lws_context *context, struct ev_loop *loop, int tsi);
  3529. LWS_VISIBLE LWS_EXTERN void
  3530. lws_ev_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents);
  3531. #endif /* LWS_WITH_LIBEV */
  3532. ///@}
  3533. /*! \defgroup uv libuv helpers
  3534. *
  3535. * ##libuv helpers
  3536. *
  3537. * APIs specific to libuv event loop itegration
  3538. */
  3539. ///@{
  3540. #ifdef LWS_WITH_LIBUV
  3541. LWS_VISIBLE LWS_EXTERN int
  3542. lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
  3543. uv_signal_cb cb);
  3544. LWS_VISIBLE LWS_EXTERN void
  3545. lws_libuv_run(const struct lws_context *context, int tsi);
  3546. LWS_VISIBLE LWS_EXTERN void
  3547. lws_libuv_stop(struct lws_context *context);
  3548. LWS_VISIBLE LWS_EXTERN void
  3549. lws_libuv_stop_without_kill(const struct lws_context *context, int tsi);
  3550. LWS_VISIBLE LWS_EXTERN int
  3551. lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi);
  3552. LWS_VISIBLE LWS_EXTERN uv_loop_t *
  3553. lws_uv_getloop(struct lws_context *context, int tsi);
  3554. LWS_VISIBLE LWS_EXTERN void
  3555. lws_uv_sigint_cb(uv_signal_t *watcher, int signum);
  3556. LWS_VISIBLE LWS_EXTERN void
  3557. lws_close_all_handles_in_loop(uv_loop_t *loop);
  3558. #endif /* LWS_WITH_LIBUV */
  3559. ///@}
  3560. /*! \defgroup event libevent helpers
  3561. *
  3562. * ##libevent helpers
  3563. *
  3564. * APIs specific to libevent event loop itegration
  3565. */
  3566. ///@{
  3567. #ifdef LWS_WITH_LIBEVENT
  3568. typedef void (lws_event_signal_cb_t) (evutil_socket_t sock_fd, short revents,
  3569. void *ctx);
  3570. LWS_VISIBLE LWS_EXTERN int
  3571. lws_event_sigint_cfg(struct lws_context *context, int use_event_sigint,
  3572. lws_event_signal_cb_t cb);
  3573. LWS_VISIBLE LWS_EXTERN int
  3574. lws_event_initloop(struct lws_context *context, struct event_base *loop,
  3575. int tsi);
  3576. LWS_VISIBLE LWS_EXTERN void
  3577. lws_event_sigint_cb(evutil_socket_t sock_fd, short revents,
  3578. void *ctx);
  3579. #endif /* LWS_WITH_LIBEVENT */
  3580. ///@}
  3581. /*! \defgroup timeout Connection timeouts
  3582. APIs related to setting connection timeouts
  3583. */
  3584. //@{
  3585. /*
  3586. * NOTE: These public enums are part of the abi. If you want to add one,
  3587. * add it at where specified so existing users are unaffected.
  3588. */
  3589. enum pending_timeout {
  3590. NO_PENDING_TIMEOUT = 0,
  3591. PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE = 1,
  3592. PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE = 2,
  3593. PENDING_TIMEOUT_ESTABLISH_WITH_SERVER = 3,
  3594. PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE = 4,
  3595. PENDING_TIMEOUT_AWAITING_PING = 5,
  3596. PENDING_TIMEOUT_CLOSE_ACK = 6,
  3597. PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE = 7,
  3598. PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE = 8,
  3599. PENDING_TIMEOUT_SSL_ACCEPT = 9,
  3600. PENDING_TIMEOUT_HTTP_CONTENT = 10,
  3601. PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND = 11,
  3602. PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE = 12,
  3603. PENDING_TIMEOUT_SHUTDOWN_FLUSH = 13,
  3604. PENDING_TIMEOUT_CGI = 14,
  3605. PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE = 15,
  3606. PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING = 16,
  3607. PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG = 17,
  3608. PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD = 18,
  3609. PENDING_TIMEOUT_AWAITING_SOCKS_GREETING_REPLY = 19,
  3610. PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY = 20,
  3611. PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY = 21,
  3612. PENDING_TIMEOUT_KILLED_BY_SSL_INFO = 22,
  3613. PENDING_TIMEOUT_KILLED_BY_PARENT = 23,
  3614. PENDING_TIMEOUT_CLOSE_SEND = 24,
  3615. PENDING_TIMEOUT_HOLDING_AH = 25,
  3616. /****** add new things just above ---^ ******/
  3617. PENDING_TIMEOUT_USER_REASON_BASE = 1000
  3618. };
  3619. #define LWS_TO_KILL_ASYNC -1
  3620. /**< If LWS_TO_KILL_ASYNC is given as the timeout sec in a lws_set_timeout()
  3621. * call, then the connection is marked to be killed at the next timeout
  3622. * check. This is how you should force-close the wsi being serviced if
  3623. * you are doing it outside the callback (where you should close by nonzero
  3624. * return).
  3625. */
  3626. #define LWS_TO_KILL_SYNC -2
  3627. /**< If LWS_TO_KILL_SYNC is given as the timeout sec in a lws_set_timeout()
  3628. * call, then the connection is closed before returning (which may delete
  3629. * the wsi). This should only be used where the wsi being closed is not the
  3630. * wsi currently being serviced.
  3631. */
  3632. /**
  3633. * lws_set_timeout() - marks the wsi as subject to a timeout
  3634. *
  3635. * You will not need this unless you are doing something special
  3636. *
  3637. * \param wsi: Websocket connection instance
  3638. * \param reason: timeout reason
  3639. * \param secs: how many seconds. You may set to LWS_TO_KILL_ASYNC to
  3640. * force the connection to timeout at the next opportunity, or
  3641. * LWS_TO_KILL_SYNC to close it synchronously if you know the
  3642. * wsi is not the one currently being serviced.
  3643. */
  3644. LWS_VISIBLE LWS_EXTERN void
  3645. lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
  3646. ///@}
  3647. /*! \defgroup sending-data Sending data
  3648. APIs related to writing data on a connection
  3649. */
  3650. //@{
  3651. #if !defined(LWS_SIZEOFPTR)
  3652. #define LWS_SIZEOFPTR (sizeof (void *))
  3653. #endif
  3654. #if defined(__x86_64__)
  3655. #define _LWS_PAD_SIZE 16 /* Intel recommended for best performance */
  3656. #else
  3657. #define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target arch */
  3658. #endif
  3659. #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
  3660. ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
  3661. /* last 2 is for lws-meta */
  3662. #define LWS_PRE _LWS_PAD(4 + 10 + 2)
  3663. /* used prior to 1.7 and retained for backward compatibility */
  3664. #define LWS_SEND_BUFFER_PRE_PADDING LWS_PRE
  3665. #define LWS_SEND_BUFFER_POST_PADDING 0
  3666. /*
  3667. * NOTE: These public enums are part of the abi. If you want to add one,
  3668. * add it at where specified so existing users are unaffected.
  3669. */
  3670. enum lws_write_protocol {
  3671. LWS_WRITE_TEXT = 0,
  3672. /**< Send a ws TEXT message,the pointer must have LWS_PRE valid
  3673. * memory behind it. The receiver expects only valid utf-8 in the
  3674. * payload */
  3675. LWS_WRITE_BINARY = 1,
  3676. /**< Send a ws BINARY message, the pointer must have LWS_PRE valid
  3677. * memory behind it. Any sequence of bytes is valid */
  3678. LWS_WRITE_CONTINUATION = 2,
  3679. /**< Continue a previous ws message, the pointer must have LWS_PRE valid
  3680. * memory behind it */
  3681. LWS_WRITE_HTTP = 3,
  3682. /**< Send HTTP content */
  3683. /* LWS_WRITE_CLOSE is handled by lws_close_reason() */
  3684. LWS_WRITE_PING = 5,
  3685. LWS_WRITE_PONG = 6,
  3686. /* Same as write_http but we know this write ends the transaction */
  3687. LWS_WRITE_HTTP_FINAL = 7,
  3688. /* HTTP2 */
  3689. LWS_WRITE_HTTP_HEADERS = 8,
  3690. /**< Send http headers (http2 encodes this payload and LWS_WRITE_HTTP
  3691. * payload differently, http 1.x links also handle this correctly. so
  3692. * to be compatible with both in the future,header response part should
  3693. * be sent using this regardless of http version expected)
  3694. */
  3695. LWS_WRITE_HTTP_HEADERS_CONTINUATION = 9,
  3696. /**< Continuation of http/2 headers
  3697. */
  3698. /****** add new things just above ---^ ******/
  3699. /* flags */
  3700. LWS_WRITE_NO_FIN = 0x40,
  3701. /**< This part of the message is not the end of the message */
  3702. LWS_WRITE_H2_STREAM_END = 0x80,
  3703. /**< Flag indicates this packet should go out with STREAM_END if h2
  3704. * STREAM_END is allowed on DATA or HEADERS.
  3705. */
  3706. LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
  3707. /**< client packet payload goes out on wire unmunged
  3708. * only useful for security tests since normal servers cannot
  3709. * decode the content if used */
  3710. };
  3711. /* used with LWS_CALLBACK_CHILD_WRITE_VIA_PARENT */
  3712. struct lws_write_passthru {
  3713. struct lws *wsi;
  3714. unsigned char *buf;
  3715. size_t len;
  3716. enum lws_write_protocol wp;
  3717. };
  3718. /**
  3719. * lws_write() - Apply protocol then write data to client
  3720. * \param wsi: Websocket instance (available from user callback)
  3721. * \param buf: The data to send. For data being sent on a websocket
  3722. * connection (ie, not default http), this buffer MUST have
  3723. * LWS_PRE bytes valid BEFORE the pointer.
  3724. * This is so the protocol header data can be added in-situ.
  3725. * \param len: Count of the data bytes in the payload starting from buf
  3726. * \param protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
  3727. * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
  3728. * data on a websockets connection. Remember to allow the extra
  3729. * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
  3730. * are used.
  3731. *
  3732. * This function provides the way to issue data back to the client
  3733. * for both http and websocket protocols.
  3734. *
  3735. * IMPORTANT NOTICE!
  3736. *
  3737. * When sending with websocket protocol
  3738. *
  3739. * LWS_WRITE_TEXT,
  3740. * LWS_WRITE_BINARY,
  3741. * LWS_WRITE_CONTINUATION,
  3742. * LWS_WRITE_PING,
  3743. * LWS_WRITE_PONG
  3744. *
  3745. * the send buffer has to have LWS_PRE bytes valid BEFORE
  3746. * the buffer pointer you pass to lws_write().
  3747. *
  3748. * This allows us to add protocol info before and after the data, and send as
  3749. * one packet on the network without payload copying, for maximum efficiency.
  3750. *
  3751. * So for example you need this kind of code to use lws_write with a
  3752. * 128-byte payload
  3753. *
  3754. * char buf[LWS_PRE + 128];
  3755. *
  3756. * // fill your part of the buffer... for example here it's all zeros
  3757. * memset(&buf[LWS_PRE], 0, 128);
  3758. *
  3759. * lws_write(wsi, &buf[LWS_PRE], 128, LWS_WRITE_TEXT);
  3760. *
  3761. * When sending HTTP, with
  3762. *
  3763. * LWS_WRITE_HTTP,
  3764. * LWS_WRITE_HTTP_HEADERS
  3765. * LWS_WRITE_HTTP_FINAL
  3766. *
  3767. * there is no protocol data prepended, and don't need to take care about the
  3768. * LWS_PRE bytes valid before the buffer pointer.
  3769. *
  3770. * LWS_PRE is at least the frame nonce + 2 header + 8 length
  3771. * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off.
  3772. * The example apps no longer use it.
  3773. *
  3774. * Pad LWS_PRE to the CPU word size, so that word references
  3775. * to the address immediately after the padding won't cause an unaligned access
  3776. * error. Sometimes for performance reasons the recommended padding is even
  3777. * larger than sizeof(void *).
  3778. *
  3779. * In the case of sending using websocket protocol, be sure to allocate
  3780. * valid storage before and after buf as explained above. This scheme
  3781. * allows maximum efficiency of sending data and protocol in a single
  3782. * packet while not burdening the user code with any protocol knowledge.
  3783. *
  3784. * Return may be -1 for a fatal error needing connection close, or the
  3785. * number of bytes sent.
  3786. *
  3787. * Truncated Writes
  3788. * ================
  3789. *
  3790. * The OS may not accept everything you asked to write on the connection.
  3791. *
  3792. * Posix defines POLLOUT indication from poll() to show that the connection
  3793. * will accept more write data, but it doesn't specifiy how much. It may just
  3794. * accept one byte of whatever you wanted to send.
  3795. *
  3796. * LWS will buffer the remainder automatically, and send it out autonomously.
  3797. *
  3798. * During that time, WRITABLE callbacks will be suppressed.
  3799. *
  3800. * This is to handle corner cases where unexpectedly the OS refuses what we
  3801. * usually expect it to accept. You should try to send in chunks that are
  3802. * almost always accepted in order to avoid the inefficiency of the buffering.
  3803. */
  3804. LWS_VISIBLE LWS_EXTERN int
  3805. lws_write(struct lws *wsi, unsigned char *buf, size_t len,
  3806. enum lws_write_protocol protocol);
  3807. /* helper for case where buffer may be const */
  3808. #define lws_write_http(wsi, buf, len) \
  3809. lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
  3810. ///@}
  3811. /** \defgroup callback-when-writeable Callback when writeable
  3812. *
  3813. * ##Callback When Writeable
  3814. *
  3815. * lws can only write data on a connection when it is able to accept more
  3816. * data without blocking.
  3817. *
  3818. * So a basic requirement is we should only use the lws_write() apis when the
  3819. * connection we want to write on says that he can accept more data.
  3820. *
  3821. * When lws cannot complete your send at the time, it will buffer the data
  3822. * and send it in the background, suppressing any further WRITEABLE callbacks
  3823. * on that connection until it completes. So it is important to write new
  3824. * things in a new writeable callback.
  3825. *
  3826. * These apis reflect the various ways we can indicate we would like to be
  3827. * called back when one or more connections is writeable.
  3828. */
  3829. ///@{
  3830. /**
  3831. * lws_callback_on_writable() - Request a callback when this socket
  3832. * becomes able to be written to without
  3833. * blocking
  3834. *
  3835. * \param wsi: Websocket connection instance to get callback for
  3836. *
  3837. * - Which: only this wsi
  3838. * - When: when the individual connection becomes writeable
  3839. * - What: LWS_CALLBACK_*_WRITEABLE
  3840. */
  3841. LWS_VISIBLE LWS_EXTERN int
  3842. lws_callback_on_writable(struct lws *wsi);
  3843. /**
  3844. * lws_callback_on_writable_all_protocol() - Request a callback for all
  3845. * connections using the given protocol when it
  3846. * becomes possible to write to each socket without
  3847. * blocking in turn.
  3848. *
  3849. * \param context: lws_context
  3850. * \param protocol: Protocol whose connections will get callbacks
  3851. *
  3852. * - Which: connections using this protocol on ANY VHOST
  3853. * - When: when the individual connection becomes writeable
  3854. * - What: LWS_CALLBACK_*_WRITEABLE
  3855. */
  3856. LWS_VISIBLE LWS_EXTERN int
  3857. lws_callback_on_writable_all_protocol(const struct lws_context *context,
  3858. const struct lws_protocols *protocol);
  3859. /**
  3860. * lws_callback_on_writable_all_protocol_vhost() - Request a callback for
  3861. * all connections on same vhost using the given protocol
  3862. * when it becomes possible to write to each socket without
  3863. * blocking in turn.
  3864. *
  3865. * \param vhost: Only consider connections on this lws_vhost
  3866. * \param protocol: Protocol whose connections will get callbacks
  3867. *
  3868. * - Which: connections using this protocol on GIVEN VHOST ONLY
  3869. * - When: when the individual connection becomes writeable
  3870. * - What: LWS_CALLBACK_*_WRITEABLE
  3871. */
  3872. LWS_VISIBLE LWS_EXTERN int
  3873. lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
  3874. const struct lws_protocols *protocol);
  3875. /**
  3876. * lws_callback_all_protocol() - Callback all connections using
  3877. * the given protocol with the given reason
  3878. *
  3879. * \param context: lws_context
  3880. * \param protocol: Protocol whose connections will get callbacks
  3881. * \param reason: Callback reason index
  3882. *
  3883. * - Which: connections using this protocol on ALL VHOSTS
  3884. * - When: before returning
  3885. * - What: reason
  3886. *
  3887. * This isn't normally what you want... normally any update of connection-
  3888. * specific information can wait until a network-related callback like rx,
  3889. * writable, or close.
  3890. */
  3891. LWS_VISIBLE LWS_EXTERN int
  3892. lws_callback_all_protocol(struct lws_context *context,
  3893. const struct lws_protocols *protocol, int reason);
  3894. /**
  3895. * lws_callback_all_protocol_vhost() - Callback all connections using
  3896. * the given protocol with the given reason. This is
  3897. * deprecated since v2.4: use lws_callback_all_protocol_vhost_args
  3898. *
  3899. * \param vh: Vhost whose connections will get callbacks
  3900. * \param protocol: Which protocol to match. NULL means all.
  3901. * \param reason: Callback reason index
  3902. *
  3903. * - Which: connections using this protocol on GIVEN VHOST ONLY
  3904. * - When: now
  3905. * - What: reason
  3906. */
  3907. LWS_VISIBLE LWS_EXTERN int
  3908. lws_callback_all_protocol_vhost(struct lws_vhost *vh,
  3909. const struct lws_protocols *protocol, int reason)
  3910. LWS_WARN_DEPRECATED;
  3911. /**
  3912. * lws_callback_all_protocol_vhost_args() - Callback all connections using
  3913. * the given protocol with the given reason and args
  3914. *
  3915. * \param vh: Vhost whose connections will get callbacks
  3916. * \param protocol: Which protocol to match. NULL means all.
  3917. * \param reason: Callback reason index
  3918. * \param argp: Callback "in" parameter
  3919. * \param len: Callback "len" parameter
  3920. *
  3921. * - Which: connections using this protocol on GIVEN VHOST ONLY
  3922. * - When: now
  3923. * - What: reason
  3924. */
  3925. LWS_VISIBLE int
  3926. lws_callback_all_protocol_vhost_args(struct lws_vhost *vh,
  3927. const struct lws_protocols *protocol, int reason,
  3928. void *argp, size_t len);
  3929. /**
  3930. * lws_callback_vhost_protocols() - Callback all protocols enabled on a vhost
  3931. * with the given reason
  3932. *
  3933. * \param wsi: wsi whose vhost will get callbacks
  3934. * \param reason: Callback reason index
  3935. * \param in: in argument to callback
  3936. * \param len: len argument to callback
  3937. *
  3938. * - Which: connections using this protocol on same VHOST as wsi ONLY
  3939. * - When: now
  3940. * - What: reason
  3941. */
  3942. LWS_VISIBLE LWS_EXTERN int
  3943. lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len);
  3944. LWS_VISIBLE LWS_EXTERN int
  3945. lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
  3946. void *user, void *in, size_t len);
  3947. /**
  3948. * lws_get_socket_fd() - returns the socket file descriptor
  3949. *
  3950. * You will not need this unless you are doing something special
  3951. *
  3952. * \param wsi: Websocket connection instance
  3953. */
  3954. LWS_VISIBLE LWS_EXTERN int
  3955. lws_get_socket_fd(struct lws *wsi);
  3956. /**
  3957. * lws_get_peer_write_allowance() - get the amount of data writeable to peer
  3958. * if known
  3959. *
  3960. * \param wsi: Websocket connection instance
  3961. *
  3962. * if the protocol does not have any guidance, returns -1. Currently only
  3963. * http2 connections get send window information from this API. But your code
  3964. * should use it so it can work properly with any protocol.
  3965. *
  3966. * If nonzero return is the amount of payload data the peer or intermediary has
  3967. * reported it has buffer space for. That has NO relationship with the amount
  3968. * of buffer space your OS can accept on this connection for a write action.
  3969. *
  3970. * This number represents the maximum you could send to the peer or intermediary
  3971. * on this connection right now without the protocol complaining.
  3972. *
  3973. * lws manages accounting for send window updates and payload writes
  3974. * automatically, so this number reflects the situation at the peer or
  3975. * intermediary dynamically.
  3976. */
  3977. LWS_VISIBLE LWS_EXTERN size_t
  3978. lws_get_peer_write_allowance(struct lws *wsi);
  3979. ///@}
  3980. enum {
  3981. /*
  3982. * Flags for enable and disable rxflow with reason bitmap and with
  3983. * backwards-compatible single bool
  3984. */
  3985. LWS_RXFLOW_REASON_USER_BOOL = (1 << 0),
  3986. LWS_RXFLOW_REASON_HTTP_RXBUFFER = (1 << 6),
  3987. LWS_RXFLOW_REASON_H2_PPS_PENDING = (1 << 7),
  3988. LWS_RXFLOW_REASON_APPLIES = (1 << 14),
  3989. LWS_RXFLOW_REASON_APPLIES_ENABLE_BIT = (1 << 13),
  3990. LWS_RXFLOW_REASON_APPLIES_ENABLE = LWS_RXFLOW_REASON_APPLIES |
  3991. LWS_RXFLOW_REASON_APPLIES_ENABLE_BIT,
  3992. LWS_RXFLOW_REASON_APPLIES_DISABLE = LWS_RXFLOW_REASON_APPLIES,
  3993. LWS_RXFLOW_REASON_FLAG_PROCESS_NOW = (1 << 12),
  3994. };
  3995. /**
  3996. * lws_rx_flow_control() - Enable and disable socket servicing for
  3997. * received packets.
  3998. *
  3999. * If the output side of a server process becomes choked, this allows flow
  4000. * control for the input side.
  4001. *
  4002. * \param wsi: Websocket connection instance to get callback for
  4003. * \param enable: 0 = disable read servicing for this connection, 1 = enable
  4004. *
  4005. * If you need more than one additive reason for rxflow control, you can give
  4006. * iLWS_RXFLOW_REASON_APPLIES_ENABLE or _DISABLE together with one or more of
  4007. * b5..b0 set to idicate which bits to enable or disable. If any bits are
  4008. * enabled, rx on the connection is suppressed.
  4009. *
  4010. * LWS_RXFLOW_REASON_FLAG_PROCESS_NOW flag may also be given to force any change
  4011. * in rxflowbstatus to benapplied immediately, this should be used when you are
  4012. * changing a wsi flow control state from outside a callback on that wsi.
  4013. */
  4014. LWS_VISIBLE LWS_EXTERN int
  4015. lws_rx_flow_control(struct lws *wsi, int enable);
  4016. /**
  4017. * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
  4018. *
  4019. * When the user server code realizes it can accept more input, it can
  4020. * call this to have the RX flow restriction removed from all connections using
  4021. * the given protocol.
  4022. * \param context: lws_context
  4023. * \param protocol: all connections using this protocol will be allowed to receive
  4024. */
  4025. LWS_VISIBLE LWS_EXTERN void
  4026. lws_rx_flow_allow_all_protocol(const struct lws_context *context,
  4027. const struct lws_protocols *protocol);
  4028. /**
  4029. * lws_remaining_packet_payload() - Bytes to come before "overall"
  4030. * rx packet is complete
  4031. * \param wsi: Websocket instance (available from user callback)
  4032. *
  4033. * This function is intended to be called from the callback if the
  4034. * user code is interested in "complete packets" from the client.
  4035. * libwebsockets just passes through payload as it comes and issues a buffer
  4036. * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
  4037. * callback handler can use this API to find out if the buffer it has just
  4038. * been given is the last piece of a "complete packet" from the client --
  4039. * when that is the case lws_remaining_packet_payload() will return
  4040. * 0.
  4041. *
  4042. * Many protocols won't care becuse their packets are always small.
  4043. */
  4044. LWS_VISIBLE LWS_EXTERN size_t
  4045. lws_remaining_packet_payload(struct lws *wsi);
  4046. /** \defgroup sock-adopt Socket adoption helpers
  4047. * ##Socket adoption helpers
  4048. *
  4049. * When integrating with an external app with its own event loop, these can
  4050. * be used to accept connections from someone else's listening socket.
  4051. *
  4052. * When using lws own event loop, these are not needed.
  4053. */
  4054. ///@{
  4055. /**
  4056. * lws_adopt_socket() - adopt foreign socket as if listen socket accepted it
  4057. * for the default vhost of context.
  4058. *
  4059. * \param context: lws context
  4060. * \param accept_fd: fd of already-accepted socket to adopt
  4061. *
  4062. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4063. * returns NULL, having cleaned up any new wsi pieces.
  4064. *
  4065. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4066. * to ws or just serve http.
  4067. */
  4068. LWS_VISIBLE LWS_EXTERN struct lws *
  4069. lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd);
  4070. /**
  4071. * lws_adopt_socket_vhost() - adopt foreign socket as if listen socket accepted it
  4072. * for vhost
  4073. *
  4074. * \param vh: lws vhost
  4075. * \param accept_fd: fd of already-accepted socket to adopt
  4076. *
  4077. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4078. * returns NULL, having cleaned up any new wsi pieces.
  4079. *
  4080. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4081. * to ws or just serve http.
  4082. */
  4083. LWS_VISIBLE LWS_EXTERN struct lws *
  4084. lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
  4085. typedef enum {
  4086. LWS_ADOPT_RAW_FILE_DESC = 0, /* convenience constant */
  4087. LWS_ADOPT_HTTP = 1, /* flag: absent implies RAW */
  4088. LWS_ADOPT_SOCKET = 2, /* flag: absent implies file descr */
  4089. LWS_ADOPT_ALLOW_SSL = 4, /* flag: if set requires LWS_ADOPT_SOCKET */
  4090. LWS_ADOPT_WS_PARENTIO = 8, /* flag: ws mode parent handles IO
  4091. * if given must be only flag
  4092. * wsi put directly into ws mode
  4093. */
  4094. } lws_adoption_type;
  4095. typedef union {
  4096. lws_sockfd_type sockfd;
  4097. lws_filefd_type filefd;
  4098. } lws_sock_file_fd_type;
  4099. /*
  4100. * lws_adopt_descriptor_vhost() - adopt foreign socket or file descriptor
  4101. * if socket descriptor, should already have been accepted from listen socket
  4102. *
  4103. * \param vhost: lws vhost
  4104. * \param type: OR-ed combinations of lws_adoption_type flags
  4105. * \param fd: union with either .sockfd or .filefd set
  4106. * \param vh_prot_name: NULL or vh protocol name to bind raw connection to
  4107. * \param parent: NULL or struct lws to attach new_wsi to as a child
  4108. *
  4109. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4110. * returns NULL, having cleaned up any new wsi pieces.
  4111. *
  4112. * If LWS_ADOPT_SOCKET is set, LWS adopts the socket in http serving mode, it's
  4113. * ready to accept an upgrade to ws or just serve http.
  4114. *
  4115. * parent may be NULL, if given it should be an existing wsi that will become the
  4116. * parent of the new wsi created by this call.
  4117. */
  4118. LWS_VISIBLE LWS_EXTERN struct lws *
  4119. lws_adopt_descriptor_vhost(struct lws_vhost *vh, lws_adoption_type type,
  4120. lws_sock_file_fd_type fd, const char *vh_prot_name,
  4121. struct lws *parent);
  4122. /**
  4123. * lws_adopt_socket_readbuf() - adopt foreign socket and first rx as if listen socket accepted it
  4124. * for the default vhost of context.
  4125. * \param context: lws context
  4126. * \param accept_fd: fd of already-accepted socket to adopt
  4127. * \param readbuf: NULL or pointer to data that must be drained before reading from
  4128. * accept_fd
  4129. * \param len: The length of the data held at \param readbuf
  4130. *
  4131. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4132. * returns NULL, having cleaned up any new wsi pieces.
  4133. *
  4134. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4135. * to ws or just serve http.
  4136. *
  4137. * If your external code did not already read from the socket, you can use
  4138. * lws_adopt_socket() instead.
  4139. *
  4140. * This api is guaranteed to use the data at \param readbuf first, before reading from
  4141. * the socket.
  4142. *
  4143. * readbuf is limited to the size of the ah rx buf, currently 2048 bytes.
  4144. */
  4145. LWS_VISIBLE LWS_EXTERN struct lws *
  4146. lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
  4147. const char *readbuf, size_t len);
  4148. /**
  4149. * lws_adopt_socket_vhost_readbuf() - adopt foreign socket and first rx as if listen socket
  4150. * accepted it for vhost.
  4151. * \param vhost: lws vhost
  4152. * \param accept_fd: fd of already-accepted socket to adopt
  4153. * \param readbuf: NULL or pointer to data that must be drained before reading from
  4154. * accept_fd
  4155. * \param len: The length of the data held at \param readbuf
  4156. *
  4157. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4158. * returns NULL, having cleaned up any new wsi pieces.
  4159. *
  4160. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4161. * to ws or just serve http.
  4162. *
  4163. * If your external code did not already read from the socket, you can use
  4164. * lws_adopt_socket() instead.
  4165. *
  4166. * This api is guaranteed to use the data at \param readbuf first, before reading from
  4167. * the socket.
  4168. *
  4169. * readbuf is limited to the size of the ah rx buf, currently 2048 bytes.
  4170. */
  4171. LWS_VISIBLE LWS_EXTERN struct lws *
  4172. lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, lws_sockfd_type accept_fd,
  4173. const char *readbuf, size_t len);
  4174. ///@}
  4175. /** \defgroup net Network related helper APIs
  4176. * ##Network related helper APIs
  4177. *
  4178. * These wrap miscellaneous useful network-related functions
  4179. */
  4180. ///@{
  4181. /**
  4182. * lws_canonical_hostname() - returns this host's hostname
  4183. *
  4184. * This is typically used by client code to fill in the host parameter
  4185. * when making a client connection. You can only call it after the context
  4186. * has been created.
  4187. *
  4188. * \param context: Websocket context
  4189. */
  4190. LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  4191. lws_canonical_hostname(struct lws_context *context);
  4192. /**
  4193. * lws_get_peer_addresses() - Get client address information
  4194. * \param wsi: Local struct lws associated with
  4195. * \param fd: Connection socket descriptor
  4196. * \param name: Buffer to take client address name
  4197. * \param name_len: Length of client address name buffer
  4198. * \param rip: Buffer to take client address IP dotted quad
  4199. * \param rip_len: Length of client address IP buffer
  4200. *
  4201. * This function fills in name and rip with the name and IP of
  4202. * the client connected with socket descriptor fd. Names may be
  4203. * truncated if there is not enough room. If either cannot be
  4204. * determined, they will be returned as valid zero-length strings.
  4205. */
  4206. LWS_VISIBLE LWS_EXTERN void
  4207. lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
  4208. int name_len, char *rip, int rip_len);
  4209. /**
  4210. * lws_get_peer_simple() - Get client address information without RDNS
  4211. *
  4212. * \param wsi: Local struct lws associated with
  4213. * \param name: Buffer to take client address name
  4214. * \param namelen: Length of client address name buffer
  4215. *
  4216. * This provides a 123.123.123.123 type IP address in name from the
  4217. * peer that has connected to wsi
  4218. */
  4219. LWS_VISIBLE LWS_EXTERN const char *
  4220. lws_get_peer_simple(struct lws *wsi, char *name, int namelen);
  4221. #if !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32)
  4222. /**
  4223. * lws_interface_to_sa() - Convert interface name or IP to sockaddr struct
  4224. *
  4225. * \param ipv6: Allow IPV6 addresses
  4226. * \param ifname: Interface name or IP
  4227. * \param addr: struct sockaddr_in * to be written
  4228. * \param addrlen: Length of addr
  4229. *
  4230. * This converts a textual network interface name to a sockaddr usable by
  4231. * other network functions
  4232. */
  4233. LWS_VISIBLE LWS_EXTERN int
  4234. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  4235. size_t addrlen);
  4236. ///@}
  4237. #endif
  4238. /** \defgroup misc Miscellaneous APIs
  4239. * ##Miscellaneous APIs
  4240. *
  4241. * Various APIs outside of other categories
  4242. */
  4243. ///@{
  4244. /**
  4245. * lws_start_foreach_ll(): linkedlist iterator helper start
  4246. *
  4247. * \param type: type of iteration, eg, struct xyz *
  4248. * \param it: iterator var name to create
  4249. * \param start: start of list
  4250. *
  4251. * This helper creates an iterator and starts a while (it) {
  4252. * loop. The iterator runs through the linked list starting at start and
  4253. * ends when it gets a NULL.
  4254. * The while loop should be terminated using lws_start_foreach_ll().
  4255. */
  4256. #define lws_start_foreach_ll(type, it, start)\
  4257. { \
  4258. type it = start; \
  4259. while (it) {
  4260. /**
  4261. * lws_end_foreach_ll(): linkedlist iterator helper end
  4262. *
  4263. * \param it: same iterator var name given when starting
  4264. * \param nxt: member name in the iterator pointing to next list element
  4265. *
  4266. * This helper is the partner for lws_start_foreach_ll() that ends the
  4267. * while loop.
  4268. */
  4269. #define lws_end_foreach_ll(it, nxt) \
  4270. it = it->nxt; \
  4271. } \
  4272. }
  4273. /**
  4274. * lws_start_foreach_llp(): linkedlist pointer iterator helper start
  4275. *
  4276. * \param type: type of iteration, eg, struct xyz **
  4277. * \param it: iterator var name to create
  4278. * \param start: start of list
  4279. *
  4280. * This helper creates an iterator and starts a while (it) {
  4281. * loop. The iterator runs through the linked list starting at the
  4282. * address of start and ends when it gets a NULL.
  4283. * The while loop should be terminated using lws_start_foreach_llp().
  4284. *
  4285. * This helper variant iterates using a pointer to the previous linked-list
  4286. * element. That allows you to easily delete list members by rewriting the
  4287. * previous pointer to the element's next pointer.
  4288. */
  4289. #define lws_start_foreach_llp(type, it, start)\
  4290. { \
  4291. type it = &(start); \
  4292. while (*(it)) {
  4293. /**
  4294. * lws_end_foreach_llp(): linkedlist pointer iterator helper end
  4295. *
  4296. * \param it: same iterator var name given when starting
  4297. * \param nxt: member name in the iterator pointing to next list element
  4298. *
  4299. * This helper is the partner for lws_start_foreach_llp() that ends the
  4300. * while loop.
  4301. */
  4302. #define lws_end_foreach_llp(it, nxt) \
  4303. it = &(*(it))->nxt; \
  4304. } \
  4305. }
  4306. /**
  4307. * lws_snprintf(): snprintf that truncates the returned length too
  4308. *
  4309. * \param str: destination buffer
  4310. * \param size: bytes left in destination buffer
  4311. * \param format: format string
  4312. * \param ...: args for format
  4313. *
  4314. * This lets you correctly truncate buffers by concatenating lengths, if you
  4315. * reach the limit the reported length doesn't exceed the limit.
  4316. */
  4317. LWS_VISIBLE LWS_EXTERN int
  4318. lws_snprintf(char *str, size_t size, const char *format, ...) LWS_FORMAT(3);
  4319. /**
  4320. * lws_get_random(): fill a buffer with platform random data
  4321. *
  4322. * \param context: the lws context
  4323. * \param buf: buffer to fill
  4324. * \param len: how much to fill
  4325. *
  4326. * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
  4327. * it's interested to see if the frame it's dealing with was sent in binary
  4328. * mode.
  4329. */
  4330. LWS_VISIBLE LWS_EXTERN int
  4331. lws_get_random(struct lws_context *context, void *buf, int len);
  4332. /**
  4333. * lws_daemonize(): make current process run in the background
  4334. *
  4335. * \param _lock_path: the filepath to write the lock file
  4336. *
  4337. * Spawn lws as a background process, taking care of various things
  4338. */
  4339. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4340. lws_daemonize(const char *_lock_path);
  4341. /**
  4342. * lws_get_library_version(): return string describing the version of lws
  4343. *
  4344. * On unix, also includes the git describe
  4345. */
  4346. LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  4347. lws_get_library_version(void);
  4348. /**
  4349. * lws_wsi_user() - get the user data associated with the connection
  4350. * \param wsi: lws connection
  4351. *
  4352. * Not normally needed since it's passed into the callback
  4353. */
  4354. LWS_VISIBLE LWS_EXTERN void *
  4355. lws_wsi_user(struct lws *wsi);
  4356. /**
  4357. * lws_wsi_set_user() - set the user data associated with the client connection
  4358. * \param wsi: lws connection
  4359. * \param user: user data
  4360. *
  4361. * By default lws allocates this and it's not legal to externally set it
  4362. * yourself. However client connections may have it set externally when the
  4363. * connection is created... if so, this api can be used to modify it at
  4364. * runtime additionally.
  4365. */
  4366. LWS_VISIBLE LWS_EXTERN void
  4367. lws_set_wsi_user(struct lws *wsi, void *user);
  4368. /**
  4369. * lws_parse_uri: cut up prot:/ads:port/path into pieces
  4370. * Notice it does so by dropping '\0' into input string
  4371. * and the leading / on the path is consequently lost
  4372. *
  4373. * \param p: incoming uri string.. will get written to
  4374. * \param prot: result pointer for protocol part (https://)
  4375. * \param ads: result pointer for address part
  4376. * \param port: result pointer for port part
  4377. * \param path: result pointer for path part
  4378. */
  4379. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4380. lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
  4381. const char **path);
  4382. /**
  4383. * lws_now_secs(): return seconds since 1970-1-1
  4384. */
  4385. LWS_VISIBLE LWS_EXTERN unsigned long
  4386. lws_now_secs(void);
  4387. /**
  4388. * lws_get_context - Allow getting lws_context from a Websocket connection
  4389. * instance
  4390. *
  4391. * With this function, users can access context in the callback function.
  4392. * Otherwise users may have to declare context as a global variable.
  4393. *
  4394. * \param wsi: Websocket connection instance
  4395. */
  4396. LWS_VISIBLE LWS_EXTERN struct lws_context * LWS_WARN_UNUSED_RESULT
  4397. lws_get_context(const struct lws *wsi);
  4398. /**
  4399. * lws_get_count_threads(): how many service threads the context uses
  4400. *
  4401. * \param context: the lws context
  4402. *
  4403. * By default this is always 1, if you asked for more than lws can handle it
  4404. * will clip the number of threads. So you can use this to find out how many
  4405. * threads are actually in use.
  4406. */
  4407. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4408. lws_get_count_threads(struct lws_context *context);
  4409. /**
  4410. * lws_get_parent() - get parent wsi or NULL
  4411. * \param wsi: lws connection
  4412. *
  4413. * Specialized wsi like cgi stdin/out/err are associated to a parent wsi,
  4414. * this allows you to get their parent.
  4415. */
  4416. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  4417. lws_get_parent(const struct lws *wsi);
  4418. /**
  4419. * lws_get_child() - get child wsi or NULL
  4420. * \param wsi: lws connection
  4421. *
  4422. * Allows you to find a related wsi from the parent wsi.
  4423. */
  4424. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  4425. lws_get_child(const struct lws *wsi);
  4426. /**
  4427. * lws_parent_carries_io() - mark wsi as needing to send messages via parent
  4428. *
  4429. * \param wsi: child lws connection
  4430. */
  4431. LWS_VISIBLE LWS_EXTERN void
  4432. lws_set_parent_carries_io(struct lws *wsi);
  4433. LWS_VISIBLE LWS_EXTERN void *
  4434. lws_get_opaque_parent_data(const struct lws *wsi);
  4435. LWS_VISIBLE LWS_EXTERN void
  4436. lws_set_opaque_parent_data(struct lws *wsi, void *data);
  4437. LWS_VISIBLE LWS_EXTERN int
  4438. lws_get_child_pending_on_writable(const struct lws *wsi);
  4439. LWS_VISIBLE LWS_EXTERN void
  4440. lws_clear_child_pending_on_writable(struct lws *wsi);
  4441. LWS_VISIBLE LWS_EXTERN int
  4442. lws_get_close_length(struct lws *wsi);
  4443. LWS_VISIBLE LWS_EXTERN unsigned char *
  4444. lws_get_close_payload(struct lws *wsi);
  4445. /**
  4446. * lws_get_network_wsi() - Returns wsi that has the tcp connection for this wsi
  4447. *
  4448. * \param wsi: wsi you have
  4449. *
  4450. * Returns wsi that has the tcp connection (which may be the incoming wsi)
  4451. *
  4452. * HTTP/1 connections will always return the incoming wsi
  4453. * HTTP/2 connections may return a different wsi that has the tcp connection
  4454. */
  4455. LWS_VISIBLE LWS_EXTERN
  4456. struct lws *lws_get_network_wsi(struct lws *wsi);
  4457. /*
  4458. * \deprecated DEPRECATED Note: this is not normally needed as a user api.
  4459. * It's provided in case it is
  4460. * useful when integrating with other app poll loop service code.
  4461. */
  4462. LWS_VISIBLE LWS_EXTERN int
  4463. lws_read(struct lws *wsi, unsigned char *buf, lws_filepos_t len);
  4464. /**
  4465. * lws_set_allocator() - custom allocator support
  4466. *
  4467. * \param realloc
  4468. *
  4469. * Allows you to replace the allocator (and deallocator) used by lws
  4470. */
  4471. LWS_VISIBLE LWS_EXTERN void
  4472. lws_set_allocator(void *(*realloc)(void *ptr, size_t size, const char *reason));
  4473. ///@}
  4474. /** \defgroup wsstatus Websocket status APIs
  4475. * ##Websocket connection status APIs
  4476. *
  4477. * These provide information about ws connection or message status
  4478. */
  4479. ///@{
  4480. /**
  4481. * lws_send_pipe_choked() - tests if socket is writable or not
  4482. * \param wsi: lws connection
  4483. *
  4484. * Allows you to check if you can write more on the socket
  4485. */
  4486. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4487. lws_send_pipe_choked(struct lws *wsi);
  4488. /**
  4489. * lws_is_final_fragment() - tests if last part of ws message
  4490. *
  4491. * \param wsi: lws connection
  4492. */
  4493. LWS_VISIBLE LWS_EXTERN int
  4494. lws_is_final_fragment(struct lws *wsi);
  4495. /**
  4496. * lws_is_first_fragment() - tests if first part of ws message
  4497. *
  4498. * \param wsi: lws connection
  4499. */
  4500. LWS_VISIBLE LWS_EXTERN int
  4501. lws_is_first_fragment(struct lws *wsi);
  4502. /**
  4503. * lws_get_reserved_bits() - access reserved bits of ws frame
  4504. * \param wsi: lws connection
  4505. */
  4506. LWS_VISIBLE LWS_EXTERN unsigned char
  4507. lws_get_reserved_bits(struct lws *wsi);
  4508. /**
  4509. * lws_partial_buffered() - find out if lws buffered the last write
  4510. * \param wsi: websocket connection to check
  4511. *
  4512. * Returns 1 if you cannot use lws_write because the last
  4513. * write on this connection is still buffered, and can't be cleared without
  4514. * returning to the service loop and waiting for the connection to be
  4515. * writeable again.
  4516. *
  4517. * If you will try to do >1 lws_write call inside a single
  4518. * WRITEABLE callback, you must check this after every write and bail if
  4519. * set, ask for a new writeable callback and continue writing from there.
  4520. *
  4521. * This is never set at the start of a writeable callback, but any write
  4522. * may set it.
  4523. */
  4524. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4525. lws_partial_buffered(struct lws *wsi);
  4526. /**
  4527. * lws_frame_is_binary(): true if the current frame was sent in binary mode
  4528. *
  4529. * \param wsi: the connection we are inquiring about
  4530. *
  4531. * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
  4532. * it's interested to see if the frame it's dealing with was sent in binary
  4533. * mode.
  4534. */
  4535. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4536. lws_frame_is_binary(struct lws *wsi);
  4537. /**
  4538. * lws_is_ssl() - Find out if connection is using SSL
  4539. * \param wsi: websocket connection to check
  4540. *
  4541. * Returns 0 if the connection is not using SSL, 1 if using SSL and
  4542. * using verified cert, and 2 if using SSL but the cert was not
  4543. * checked (appears for client wsi told to skip check on connection)
  4544. */
  4545. LWS_VISIBLE LWS_EXTERN int
  4546. lws_is_ssl(struct lws *wsi);
  4547. /**
  4548. * lws_is_cgi() - find out if this wsi is running a cgi process
  4549. * \param wsi: lws connection
  4550. */
  4551. LWS_VISIBLE LWS_EXTERN int
  4552. lws_is_cgi(struct lws *wsi);
  4553. #ifdef LWS_OPENSSL_SUPPORT
  4554. /**
  4555. * lws_get_ssl() - Return wsi's SSL context structure
  4556. * \param wsi: websocket connection
  4557. *
  4558. * Returns pointer to the SSL library's context structure
  4559. */
  4560. LWS_VISIBLE LWS_EXTERN SSL*
  4561. lws_get_ssl(struct lws *wsi);
  4562. #endif
  4563. ///@}
  4564. /** \defgroup lws_ring LWS Ringbuffer APIs
  4565. * ##lws_ring: generic ringbuffer struct
  4566. *
  4567. * Provides an abstract ringbuffer api supporting one head and one or an
  4568. * unlimited number of tails.
  4569. *
  4570. * All of the members are opaque and manipulated by lws_ring_...() apis.
  4571. *
  4572. * The lws_ring and its buffer is allocated at runtime on the heap, using
  4573. *
  4574. * - lws_ring_create()
  4575. * - lws_ring_destroy()
  4576. *
  4577. * It may contain any type, the size of the "element" stored in the ring
  4578. * buffer and the number of elements is given at creation time.
  4579. *
  4580. * When you create the ringbuffer, you can optionally provide an element
  4581. * destroy callback that frees any allocations inside the element. This is then
  4582. * automatically called for elements with no tail behind them, ie, elements
  4583. * which don't have any pending consumer are auto-freed.
  4584. *
  4585. * Whole elements may be inserted into the ringbuffer and removed from it, using
  4586. *
  4587. * - lws_ring_insert()
  4588. * - lws_ring_consume()
  4589. *
  4590. * You can find out how many whole elements are free or waiting using
  4591. *
  4592. * - lws_ring_get_count_free_elements()
  4593. * - lws_ring_get_count_waiting_elements()
  4594. *
  4595. * In addition there are special purpose optional byte-centric apis
  4596. *
  4597. * - lws_ring_next_linear_insert_range()
  4598. * - lws_ring_bump_head()
  4599. *
  4600. * which let you, eg, read() directly into the ringbuffer without needing
  4601. * an intermediate bounce buffer.
  4602. *
  4603. * The accessors understand that the ring wraps, and optimizes insertion and
  4604. * consumption into one or two memcpy()s depending on if the head or tail
  4605. * wraps.
  4606. *
  4607. * lws_ring only supports a single head, but optionally multiple tails with
  4608. * an API to inform it when the "oldest" tail has moved on. You can give
  4609. * NULL where-ever an api asks for a tail pointer, and it will use an internal
  4610. * single tail pointer for convenience.
  4611. *
  4612. * The "oldest tail", which is the only tail if you give it NULL instead of
  4613. * some other tail, is used to track which elements in the ringbuffer are
  4614. * still unread by anyone.
  4615. *
  4616. * - lws_ring_update_oldest_tail()
  4617. */
  4618. ///@{
  4619. struct lws_ring;
  4620. /**
  4621. * lws_ring_create(): create a new ringbuffer
  4622. *
  4623. * \param element_len: the size in bytes of one element in the ringbuffer
  4624. * \param count: the number of elements the ringbuffer can contain
  4625. * \param destroy_element: NULL, or callback to be called for each element
  4626. * that is removed from the ringbuffer due to the
  4627. * oldest tail moving beyond it
  4628. *
  4629. * Creates the ringbuffer and allocates the storage. Returns the new
  4630. * lws_ring *, or NULL if the allocation failed.
  4631. *
  4632. * If non-NULL, destroy_element will get called back for every element that is
  4633. * retired from the ringbuffer after the oldest tail has gone past it, and for
  4634. * any element still left in the ringbuffer when it is destroyed. It replaces
  4635. * all other element destruction code in your user code.
  4636. */
  4637. LWS_VISIBLE LWS_EXTERN struct lws_ring *
  4638. lws_ring_create(size_t element_len, size_t count,
  4639. void (*destroy_element)(void *element));
  4640. /**
  4641. * lws_ring_destroy(): destroy a previously created ringbuffer
  4642. *
  4643. * \param ring: the struct lws_ring to destroy
  4644. *
  4645. * Destroys the ringbuffer allocation and the struct lws_ring itself.
  4646. */
  4647. LWS_VISIBLE LWS_EXTERN void
  4648. lws_ring_destroy(struct lws_ring *ring);
  4649. /**
  4650. * lws_ring_get_count_free_elements(): return how many elements can fit
  4651. * in the free space
  4652. *
  4653. * \param ring: the struct lws_ring to report on
  4654. *
  4655. * Returns how much room is left in the ringbuffer for whole element insertion.
  4656. */
  4657. LWS_VISIBLE LWS_EXTERN size_t
  4658. lws_ring_get_count_free_elements(struct lws_ring *ring);
  4659. /**
  4660. * lws_ring_get_count_waiting_elements(): return how many elements can be consumed
  4661. *
  4662. * \param ring: the struct lws_ring to report on
  4663. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  4664. *
  4665. * Returns how many elements are waiting to be consumed from the perspective
  4666. * of the tail pointer given.
  4667. */
  4668. LWS_VISIBLE LWS_EXTERN size_t
  4669. lws_ring_get_count_waiting_elements(struct lws_ring *ring, uint32_t *tail);
  4670. /**
  4671. * lws_ring_insert(): attempt to insert up to max_count elements from src
  4672. *
  4673. * \param ring: the struct lws_ring to report on
  4674. * \param src: the array of elements to be inserted
  4675. * \param max_count: the number of available elements at src
  4676. *
  4677. * Attempts to insert as many of the elements at src as possible, up to the
  4678. * maximum max_count. Returns the number of elements actually inserted.
  4679. */
  4680. LWS_VISIBLE LWS_EXTERN size_t
  4681. lws_ring_insert(struct lws_ring *ring, const void *src, size_t max_count);
  4682. /**
  4683. * lws_ring_consume(): attempt to copy out and remove up to max_count elements
  4684. * to src
  4685. *
  4686. * \param ring: the struct lws_ring to report on
  4687. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  4688. * \param dest: the array of elements to be inserted. or NULL for no copy
  4689. * \param max_count: the number of available elements at src
  4690. *
  4691. * Attempts to copy out as many waiting elements as possible into dest, from
  4692. * the perspective of the given tail, up to max_count. If dest is NULL, the
  4693. * copying out is not done but the elements are logically consumed as usual.
  4694. * NULL dest is useful in combination with lws_ring_get_element(), where you
  4695. * can use the element direct from the ringbuffer and then call this with NULL
  4696. * dest to logically consume it.
  4697. *
  4698. * Increments the tail position according to how many elements could be
  4699. * consumed.
  4700. *
  4701. * Returns the number of elements consumed.
  4702. */
  4703. LWS_VISIBLE LWS_EXTERN size_t
  4704. lws_ring_consume(struct lws_ring *ring, uint32_t *tail, void *dest,
  4705. size_t max_count);
  4706. /**
  4707. * lws_ring_get_element(): get a pointer to the next waiting element for tail
  4708. *
  4709. * \param ring: the struct lws_ring to report on
  4710. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  4711. *
  4712. * Points to the next element that tail would consume, directly in the
  4713. * ringbuffer. This lets you write() or otherwise use the element without
  4714. * having to copy it out somewhere first.
  4715. *
  4716. * After calling this, you must call lws_ring_consume(ring, &tail, NULL, 1)
  4717. * which will logically consume the element you used up and increment your
  4718. * tail (tail may also be NULL there if you use a single tail).
  4719. *
  4720. * Returns NULL if no waiting element, or a const void * pointing to it.
  4721. */
  4722. LWS_VISIBLE LWS_EXTERN const void *
  4723. lws_ring_get_element(struct lws_ring *ring, uint32_t *tail);
  4724. /**
  4725. * lws_ring_update_oldest_tail(): free up elements older than tail for reuse
  4726. *
  4727. * \param ring: the struct lws_ring to report on
  4728. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  4729. *
  4730. * If you are using multiple tails, you must use this API to inform the
  4731. * lws_ring when none of the tails still need elements in the fifo any more,
  4732. * by updating it when the "oldest" tail has moved on.
  4733. */
  4734. LWS_VISIBLE LWS_EXTERN void
  4735. lws_ring_update_oldest_tail(struct lws_ring *ring, uint32_t tail);
  4736. /**
  4737. * lws_ring_get_oldest_tail(): get current oldest available data index
  4738. *
  4739. * \param ring: the struct lws_ring to report on
  4740. *
  4741. * If you are initializing a new ringbuffer consumer, you can set its tail to
  4742. * this to start it from the oldest ringbuffer entry still available.
  4743. */
  4744. LWS_VISIBLE LWS_EXTERN uint32_t
  4745. lws_ring_get_oldest_tail(struct lws_ring *ring);
  4746. /**
  4747. * lws_ring_next_linear_insert_range(): used to write directly into the ring
  4748. *
  4749. * \param ring: the struct lws_ring to report on
  4750. * \param start: pointer to a void * set to the start of the next ringbuffer area
  4751. * \param bytes: pointer to a size_t set to the max length you may use from *start
  4752. *
  4753. * This provides a low-level, bytewise access directly into the ringbuffer
  4754. * allowing direct insertion of data without having to use a bounce buffer.
  4755. *
  4756. * The api reports the position and length of the next linear range that can
  4757. * be written in the ringbuffer, ie, up to the point it would wrap, and sets
  4758. * *start and *bytes accordingly. You can then, eg, directly read() into
  4759. * *start for up to *bytes, and use lws_ring_bump_head() to update the lws_ring
  4760. * with what you have done.
  4761. *
  4762. * Returns nonzero if no insertion is currently possible.
  4763. */
  4764. LWS_VISIBLE LWS_EXTERN int
  4765. lws_ring_next_linear_insert_range(struct lws_ring *ring, void **start,
  4766. size_t *bytes);
  4767. /**
  4768. * lws_ring_bump_head(): used to write directly into the ring
  4769. *
  4770. * \param ring: the struct lws_ring to operate on
  4771. * \param bytes: the number of bytes you inserted at the current head
  4772. */
  4773. LWS_VISIBLE LWS_EXTERN void
  4774. lws_ring_bump_head(struct lws_ring *ring, size_t bytes);
  4775. ///@}
  4776. /** \defgroup sha SHA and B64 helpers
  4777. * ##SHA and B64 helpers
  4778. *
  4779. * These provide SHA-1 and B64 helper apis
  4780. */
  4781. ///@{
  4782. #ifdef LWS_SHA1_USE_OPENSSL_NAME
  4783. #define lws_SHA1 SHA1
  4784. #else
  4785. /**
  4786. * lws_SHA1(): make a SHA-1 digest of a buffer
  4787. *
  4788. * \param d: incoming buffer
  4789. * \param n: length of incoming buffer
  4790. * \param md: buffer for message digest (must be >= 20 bytes)
  4791. *
  4792. * Reduces any size buffer into a 20-byte SHA-1 hash.
  4793. */
  4794. LWS_VISIBLE LWS_EXTERN unsigned char *
  4795. lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
  4796. #endif
  4797. /**
  4798. * lws_b64_encode_string(): encode a string into base 64
  4799. *
  4800. * \param in: incoming buffer
  4801. * \param in_len: length of incoming buffer
  4802. * \param out: result buffer
  4803. * \param out_size: length of result buffer
  4804. *
  4805. * Encodes a string using b64
  4806. */
  4807. LWS_VISIBLE LWS_EXTERN int
  4808. lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
  4809. /**
  4810. * lws_b64_decode_string(): decode a string from base 64
  4811. *
  4812. * \param in: incoming buffer
  4813. * \param out: result buffer
  4814. * \param out_size: length of result buffer
  4815. *
  4816. * Decodes a string using b64
  4817. */
  4818. LWS_VISIBLE LWS_EXTERN int
  4819. lws_b64_decode_string(const char *in, char *out, int out_size);
  4820. ///@}
  4821. /*! \defgroup cgi cgi handling
  4822. *
  4823. * ##CGI handling
  4824. *
  4825. * These functions allow low-level control over stdin/out/err of the cgi.
  4826. *
  4827. * However for most cases, binding the cgi to http in and out, the default
  4828. * lws implementation already does the right thing.
  4829. */
  4830. enum lws_enum_stdinouterr {
  4831. LWS_STDIN = 0,
  4832. LWS_STDOUT = 1,
  4833. LWS_STDERR = 2,
  4834. };
  4835. enum lws_cgi_hdr_state {
  4836. LCHS_HEADER,
  4837. LCHS_CR1,
  4838. LCHS_LF1,
  4839. LCHS_CR2,
  4840. LCHS_LF2,
  4841. LHCS_RESPONSE,
  4842. LHCS_DUMP_HEADERS,
  4843. LHCS_PAYLOAD,
  4844. LCHS_SINGLE_0A,
  4845. };
  4846. struct lws_cgi_args {
  4847. struct lws **stdwsi; /**< get fd with lws_get_socket_fd() */
  4848. enum lws_enum_stdinouterr ch; /**< channel index */
  4849. unsigned char *data; /**< for messages with payload */
  4850. enum lws_cgi_hdr_state hdr_state; /**< track where we are in cgi headers */
  4851. int len; /**< length */
  4852. };
  4853. #ifdef LWS_WITH_CGI
  4854. /**
  4855. * lws_cgi: spawn network-connected cgi process
  4856. *
  4857. * \param wsi: connection to own the process
  4858. * \param exec_array: array of "exec-name" "arg1" ... "argn" NULL
  4859. * \param script_uri_path_len: how many chars on the left of the uri are the
  4860. * path to the cgi, or -1 to spawn without URL-related env vars
  4861. * \param timeout_secs: seconds script should be allowed to run
  4862. * \param mp_cgienv: pvo list with per-vhost cgi options to put in env
  4863. */
  4864. LWS_VISIBLE LWS_EXTERN int
  4865. lws_cgi(struct lws *wsi, const char * const *exec_array,
  4866. int script_uri_path_len, int timeout_secs,
  4867. const struct lws_protocol_vhost_options *mp_cgienv);
  4868. /**
  4869. * lws_cgi_write_split_stdout_headers: write cgi output accounting for header part
  4870. *
  4871. * \param wsi: connection to own the process
  4872. */
  4873. LWS_VISIBLE LWS_EXTERN int
  4874. lws_cgi_write_split_stdout_headers(struct lws *wsi);
  4875. /**
  4876. * lws_cgi_kill: terminate cgi process associated with wsi
  4877. *
  4878. * \param wsi: connection to own the process
  4879. */
  4880. LWS_VISIBLE LWS_EXTERN int
  4881. lws_cgi_kill(struct lws *wsi);
  4882. /**
  4883. * lws_cgi_get_stdwsi: get wsi for stdin, stdout, or stderr
  4884. *
  4885. * \param wsi: parent wsi that has cgi
  4886. * \param ch: which of LWS_STDIN, LWS_STDOUT or LWS_STDERR
  4887. */
  4888. LWS_VISIBLE LWS_EXTERN struct lws *
  4889. lws_cgi_get_stdwsi(struct lws *wsi, enum lws_enum_stdinouterr ch);
  4890. #endif
  4891. ///@}
  4892. /*! \defgroup fops file operation wrapping
  4893. *
  4894. * ##File operation wrapping
  4895. *
  4896. * Use these helper functions if you want to access a file from the perspective
  4897. * of a specific wsi, which is usually the case. If you just want contextless
  4898. * file access, use the fops callbacks directly with NULL wsi instead of these
  4899. * helpers.
  4900. *
  4901. * If so, then it calls the platform handler or user overrides where present
  4902. * (as defined in info->fops)
  4903. *
  4904. * The advantage from all this is user code can be portable for file operations
  4905. * without having to deal with differences between platforms.
  4906. */
  4907. //@{
  4908. /** struct lws_plat_file_ops - Platform-specific file operations
  4909. *
  4910. * These provide platform-agnostic ways to deal with filesystem access in the
  4911. * library and in the user code.
  4912. */
  4913. #if defined(LWS_WITH_ESP32)
  4914. /* sdk preprocessor defs? compiler issue? gets confused with member names */
  4915. #define LWS_FOP_OPEN _open
  4916. #define LWS_FOP_CLOSE _close
  4917. #define LWS_FOP_SEEK_CUR _seek_cur
  4918. #define LWS_FOP_READ _read
  4919. #define LWS_FOP_WRITE _write
  4920. #else
  4921. #define LWS_FOP_OPEN open
  4922. #define LWS_FOP_CLOSE close
  4923. #define LWS_FOP_SEEK_CUR seek_cur
  4924. #define LWS_FOP_READ read
  4925. #define LWS_FOP_WRITE write
  4926. #endif
  4927. #define LWS_FOP_FLAGS_MASK ((1 << 23) - 1)
  4928. #define LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP (1 << 24)
  4929. #define LWS_FOP_FLAG_COMPR_IS_GZIP (1 << 25)
  4930. #define LWS_FOP_FLAG_MOD_TIME_VALID (1 << 26)
  4931. #define LWS_FOP_FLAG_VIRTUAL (1 << 27)
  4932. struct lws_plat_file_ops;
  4933. struct lws_fop_fd {
  4934. lws_filefd_type fd;
  4935. /**< real file descriptor related to the file... */
  4936. const struct lws_plat_file_ops *fops;
  4937. /**< fops that apply to this fop_fd */
  4938. void *filesystem_priv;
  4939. /**< ignored by lws; owned by the fops handlers */
  4940. lws_filepos_t pos;
  4941. /**< generic "position in file" */
  4942. lws_filepos_t len;
  4943. /**< generic "length of file" */
  4944. lws_fop_flags_t flags;
  4945. /**< copy of the returned flags */
  4946. uint32_t mod_time;
  4947. /**< optional "modification time of file", only valid if .open()
  4948. * set the LWS_FOP_FLAG_MOD_TIME_VALID flag */
  4949. };
  4950. typedef struct lws_fop_fd *lws_fop_fd_t;
  4951. struct lws_fops_index {
  4952. const char *sig; /* NULL or vfs signature, eg, ".zip/" */
  4953. uint8_t len; /* length of above string */
  4954. };
  4955. struct lws_plat_file_ops {
  4956. lws_fop_fd_t (*LWS_FOP_OPEN)(const struct lws_plat_file_ops *fops,
  4957. const char *filename, const char *vpath,
  4958. lws_fop_flags_t *flags);
  4959. /**< Open file (always binary access if plat supports it)
  4960. * vpath may be NULL, or if the fops understands it, the point at which
  4961. * the filename's virtual part starts.
  4962. * *flags & LWS_FOP_FLAGS_MASK should be set to O_RDONLY or O_RDWR.
  4963. * If the file may be gzip-compressed,
  4964. * LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP is set. If it actually is
  4965. * gzip-compressed, then the open handler should OR
  4966. * LWS_FOP_FLAG_COMPR_IS_GZIP on to *flags before returning.
  4967. */
  4968. int (*LWS_FOP_CLOSE)(lws_fop_fd_t *fop_fd);
  4969. /**< close file AND set the pointer to NULL */
  4970. lws_fileofs_t (*LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd,
  4971. lws_fileofs_t offset_from_cur_pos);
  4972. /**< seek from current position */
  4973. int (*LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  4974. uint8_t *buf, lws_filepos_t len);
  4975. /**< Read from file, on exit *amount is set to amount actually read */
  4976. int (*LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  4977. uint8_t *buf, lws_filepos_t len);
  4978. /**< Write to file, on exit *amount is set to amount actually written */
  4979. struct lws_fops_index fi[3];
  4980. /**< vfs path signatures implying use of this fops */
  4981. const struct lws_plat_file_ops *next;
  4982. /**< NULL or next fops in list */
  4983. /* Add new things just above here ---^
  4984. * This is part of the ABI, don't needlessly break compatibility */
  4985. };
  4986. /**
  4987. * lws_get_fops() - get current file ops
  4988. *
  4989. * \param context: context
  4990. */
  4991. LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * LWS_WARN_UNUSED_RESULT
  4992. lws_get_fops(struct lws_context *context);
  4993. LWS_VISIBLE LWS_EXTERN void
  4994. lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops);
  4995. /**
  4996. * lws_vfs_tell() - get current file position
  4997. *
  4998. * \param fop_fd: fop_fd we are asking about
  4999. */
  5000. LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
  5001. lws_vfs_tell(lws_fop_fd_t fop_fd);
  5002. /**
  5003. * lws_vfs_get_length() - get current file total length in bytes
  5004. *
  5005. * \param fop_fd: fop_fd we are asking about
  5006. */
  5007. LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
  5008. lws_vfs_get_length(lws_fop_fd_t fop_fd);
  5009. /**
  5010. * lws_vfs_get_mod_time() - get time file last modified
  5011. *
  5012. * \param fop_fd: fop_fd we are asking about
  5013. */
  5014. LWS_VISIBLE LWS_EXTERN uint32_t LWS_WARN_UNUSED_RESULT
  5015. lws_vfs_get_mod_time(lws_fop_fd_t fop_fd);
  5016. /**
  5017. * lws_vfs_file_seek_set() - seek relative to start of file
  5018. *
  5019. * \param fop_fd: fop_fd we are seeking in
  5020. * \param offset: offset from start of file
  5021. */
  5022. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  5023. lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
  5024. /**
  5025. * lws_vfs_file_seek_end() - seek relative to end of file
  5026. *
  5027. * \param fop_fd: fop_fd we are seeking in
  5028. * \param offset: offset from start of file
  5029. */
  5030. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  5031. lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
  5032. extern struct lws_plat_file_ops fops_zip;
  5033. /**
  5034. * lws_plat_file_open() - open vfs filepath
  5035. *
  5036. * \param fops: file ops struct that applies to this descriptor
  5037. * \param vfs_path: filename to open
  5038. * \param flags: pointer to open flags
  5039. *
  5040. * The vfs_path is scanned for known fops signatures, and the open directed
  5041. * to any matching fops open.
  5042. *
  5043. * User code should use this api to perform vfs opens.
  5044. *
  5045. * returns semi-opaque handle
  5046. */
  5047. LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
  5048. lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
  5049. lws_fop_flags_t *flags);
  5050. /**
  5051. * lws_plat_file_close() - close file
  5052. *
  5053. * \param fop_fd: file handle to close
  5054. */
  5055. static LWS_INLINE int
  5056. lws_vfs_file_close(lws_fop_fd_t *fop_fd)
  5057. {
  5058. return (*fop_fd)->fops->LWS_FOP_CLOSE(fop_fd);
  5059. }
  5060. /**
  5061. * lws_plat_file_seek_cur() - close file
  5062. *
  5063. *
  5064. * \param fop_fd: file handle
  5065. * \param offset: position to seek to
  5066. */
  5067. static LWS_INLINE lws_fileofs_t
  5068. lws_vfs_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  5069. {
  5070. return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset);
  5071. }
  5072. /**
  5073. * lws_plat_file_read() - read from file
  5074. *
  5075. * \param fop_fd: file handle
  5076. * \param amount: how much to read (rewritten by call)
  5077. * \param buf: buffer to write to
  5078. * \param len: max length
  5079. */
  5080. static LWS_INLINE int LWS_WARN_UNUSED_RESULT
  5081. lws_vfs_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  5082. uint8_t *buf, lws_filepos_t len)
  5083. {
  5084. return fop_fd->fops->LWS_FOP_READ(fop_fd, amount, buf, len);
  5085. }
  5086. /**
  5087. * lws_plat_file_write() - write from file
  5088. *
  5089. * \param fop_fd: file handle
  5090. * \param amount: how much to write (rewritten by call)
  5091. * \param buf: buffer to read from
  5092. * \param len: max length
  5093. */
  5094. static LWS_INLINE int LWS_WARN_UNUSED_RESULT
  5095. lws_vfs_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  5096. uint8_t *buf, lws_filepos_t len)
  5097. {
  5098. return fop_fd->fops->LWS_FOP_WRITE(fop_fd, amount, buf, len);
  5099. }
  5100. /* these are the platform file operations implementations... they can
  5101. * be called directly and used in fops arrays
  5102. */
  5103. LWS_VISIBLE LWS_EXTERN lws_fop_fd_t
  5104. _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
  5105. const char *vpath, lws_fop_flags_t *flags);
  5106. LWS_VISIBLE LWS_EXTERN int
  5107. _lws_plat_file_close(lws_fop_fd_t *fop_fd);
  5108. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  5109. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
  5110. LWS_VISIBLE LWS_EXTERN int
  5111. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  5112. uint8_t *buf, lws_filepos_t len);
  5113. LWS_VISIBLE LWS_EXTERN int
  5114. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  5115. uint8_t *buf, lws_filepos_t len);
  5116. LWS_VISIBLE LWS_EXTERN int
  5117. lws_alloc_vfs_file(struct lws_context *context, const char *filename,
  5118. uint8_t **buf, lws_filepos_t *amount);
  5119. //@}
  5120. /** \defgroup smtp SMTP related functions
  5121. * ##SMTP related functions
  5122. * \ingroup lwsapi
  5123. *
  5124. * These apis let you communicate with a local SMTP server to send email from
  5125. * lws. It handles all the SMTP sequencing and protocol actions.
  5126. *
  5127. * Your system should have postfix, sendmail or another MTA listening on port
  5128. * 25 and able to send email using the "mail" commandline app. Usually distro
  5129. * MTAs are configured for this by default.
  5130. *
  5131. * It runs via its own libuv events if initialized (which requires giving it
  5132. * a libuv loop to attach to).
  5133. *
  5134. * It operates using three callbacks, on_next() queries if there is a new email
  5135. * to send, on_get_body() asks for the body of the email, and on_sent() is
  5136. * called after the email is successfully sent.
  5137. *
  5138. * To use it
  5139. *
  5140. * - create an lws_email struct
  5141. *
  5142. * - initialize data, loop, the email_* strings, max_content_size and
  5143. * the callbacks
  5144. *
  5145. * - call lws_email_init()
  5146. *
  5147. * When you have at least one email to send, call lws_email_check() to
  5148. * schedule starting to send it.
  5149. */
  5150. //@{
  5151. #ifdef LWS_WITH_SMTP
  5152. /** enum lwsgs_smtp_states - where we are in SMTP protocol sequence */
  5153. enum lwsgs_smtp_states {
  5154. LGSSMTP_IDLE, /**< awaiting new email */
  5155. LGSSMTP_CONNECTING, /**< opening tcp connection to MTA */
  5156. LGSSMTP_CONNECTED, /**< tcp connection to MTA is connected */
  5157. LGSSMTP_SENT_HELO, /**< sent the HELO */
  5158. LGSSMTP_SENT_FROM, /**< sent FROM */
  5159. LGSSMTP_SENT_TO, /**< sent TO */
  5160. LGSSMTP_SENT_DATA, /**< sent DATA request */
  5161. LGSSMTP_SENT_BODY, /**< sent the email body */
  5162. LGSSMTP_SENT_QUIT, /**< sent the session quit */
  5163. };
  5164. /** struct lws_email - abstract context for performing SMTP operations */
  5165. struct lws_email {
  5166. void *data;
  5167. /**< opaque pointer set by user code and available to the callbacks */
  5168. uv_loop_t *loop;
  5169. /**< the libuv loop we will work on */
  5170. char email_smtp_ip[32]; /**< Fill before init, eg, "127.0.0.1" */
  5171. char email_helo[32]; /**< Fill before init, eg, "myserver.com" */
  5172. char email_from[100]; /**< Fill before init or on_next */
  5173. char email_to[100]; /**< Fill before init or on_next */
  5174. unsigned int max_content_size;
  5175. /**< largest possible email body size */
  5176. /* Fill all the callbacks before init */
  5177. int (*on_next)(struct lws_email *email);
  5178. /**< (Fill in before calling lws_email_init)
  5179. * called when idle, 0 = another email to send, nonzero is idle.
  5180. * If you return 0, all of the email_* char arrays must be set
  5181. * to something useful. */
  5182. int (*on_sent)(struct lws_email *email);
  5183. /**< (Fill in before calling lws_email_init)
  5184. * called when transfer of the email to the SMTP server was
  5185. * successful, your callback would remove the current email
  5186. * from its queue */
  5187. int (*on_get_body)(struct lws_email *email, char *buf, int len);
  5188. /**< (Fill in before calling lws_email_init)
  5189. * called when the body part of the queued email is about to be
  5190. * sent to the SMTP server. */
  5191. /* private things */
  5192. uv_timer_t timeout_email; /**< private */
  5193. enum lwsgs_smtp_states estate; /**< private */
  5194. uv_connect_t email_connect_req; /**< private */
  5195. uv_tcp_t email_client; /**< private */
  5196. time_t email_connect_started; /**< private */
  5197. char email_buf[256]; /**< private */
  5198. char *content; /**< private */
  5199. };
  5200. /**
  5201. * lws_email_init() - Initialize a struct lws_email
  5202. *
  5203. * \param email: struct lws_email to init
  5204. * \param loop: libuv loop to use
  5205. * \param max_content: max email content size
  5206. *
  5207. * Prepares a struct lws_email for use ending SMTP
  5208. */
  5209. LWS_VISIBLE LWS_EXTERN int
  5210. lws_email_init(struct lws_email *email, uv_loop_t *loop, int max_content);
  5211. /**
  5212. * lws_email_check() - Request check for new email
  5213. *
  5214. * \param email: struct lws_email context to check
  5215. *
  5216. * Schedules a check for new emails in 1s... call this when you have queued an
  5217. * email for send.
  5218. */
  5219. LWS_VISIBLE LWS_EXTERN void
  5220. lws_email_check(struct lws_email *email);
  5221. /**
  5222. * lws_email_destroy() - stop using the struct lws_email
  5223. *
  5224. * \param email: the struct lws_email context
  5225. *
  5226. * Stop sending email using email and free allocations
  5227. */
  5228. LWS_VISIBLE LWS_EXTERN void
  5229. lws_email_destroy(struct lws_email *email);
  5230. #endif
  5231. //@}
  5232. /*
  5233. * Stats are all uint64_t numbers that start at 0.
  5234. * Index names here have the convention
  5235. *
  5236. * _C_ counter
  5237. * _B_ byte count
  5238. * _MS_ millisecond count
  5239. */
  5240. enum {
  5241. LWSSTATS_C_CONNECTIONS, /**< count incoming connections */
  5242. LWSSTATS_C_API_CLOSE, /**< count calls to close api */
  5243. LWSSTATS_C_API_READ, /**< count calls to read from socket api */
  5244. LWSSTATS_C_API_LWS_WRITE, /**< count calls to lws_write API */
  5245. LWSSTATS_C_API_WRITE, /**< count calls to write API */
  5246. LWSSTATS_C_WRITE_PARTIALS, /**< count of partial writes */
  5247. LWSSTATS_C_WRITEABLE_CB_REQ, /**< count of writable callback requests */
  5248. LWSSTATS_C_WRITEABLE_CB_EFF_REQ, /**< count of effective writable callback requests */
  5249. LWSSTATS_C_WRITEABLE_CB, /**< count of writable callbacks */
  5250. LWSSTATS_C_SSL_CONNECTIONS_FAILED, /**< count of failed SSL connections */
  5251. LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED, /**< count of accepted SSL connections */
  5252. LWSSTATS_C_SSL_CONNECTIONS_ACCEPT_SPIN, /**< count of SSL_accept() attempts */
  5253. LWSSTATS_C_SSL_CONNS_HAD_RX, /**< count of accepted SSL conns that have had some RX */
  5254. LWSSTATS_C_TIMEOUTS, /**< count of timed-out connections */
  5255. LWSSTATS_C_SERVICE_ENTRY, /**< count of entries to lws service loop */
  5256. LWSSTATS_B_READ, /**< aggregate bytes read */
  5257. LWSSTATS_B_WRITE, /**< aggregate bytes written */
  5258. LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, /**< aggreate of size of accepted write data from new partials */
  5259. LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY, /**< aggregate delay in accepting connection */
  5260. LWSSTATS_MS_WRITABLE_DELAY, /**< aggregate delay between asking for writable and getting cb */
  5261. LWSSTATS_MS_WORST_WRITABLE_DELAY, /**< single worst delay between asking for writable and getting cb */
  5262. LWSSTATS_MS_SSL_RX_DELAY, /**< aggregate delay between ssl accept complete and first RX */
  5263. LWSSTATS_C_PEER_LIMIT_AH_DENIED, /**< number of times we would have given an ah but for the peer limit */
  5264. LWSSTATS_C_PEER_LIMIT_WSI_DENIED, /**< number of times we would have given a wsi but for the peer limit */
  5265. /* Add new things just above here ---^
  5266. * This is part of the ABI, don't needlessly break compatibility */
  5267. LWSSTATS_SIZE
  5268. };
  5269. #if defined(LWS_WITH_STATS)
  5270. LWS_VISIBLE LWS_EXTERN uint64_t
  5271. lws_stats_get(struct lws_context *context, int index);
  5272. LWS_VISIBLE LWS_EXTERN void
  5273. lws_stats_log_dump(struct lws_context *context);
  5274. #else
  5275. static LWS_INLINE uint64_t
  5276. lws_stats_get(struct lws_context *context, int index) { return 0; }
  5277. static LWS_INLINE void
  5278. lws_stats_log_dump(struct lws_context *context) { }
  5279. #endif
  5280. #ifdef __cplusplus
  5281. }
  5282. #endif
  5283. #endif