libwebsockets.h 251 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 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. extern "C" {
  28. #else
  29. #include <stdarg.h>
  30. #endif
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include "lws_config.h"
  34. /*
  35. * CARE: everything using cmake defines needs to be below here
  36. */
  37. #if defined(LWS_HAS_INTPTR_T)
  38. #include <stdint.h>
  39. #define lws_intptr_t intptr_t
  40. #else
  41. typedef unsigned long long lws_intptr_t;
  42. #endif
  43. #if defined(WIN32) || defined(_WIN32)
  44. #ifndef WIN32_LEAN_AND_MEAN
  45. #define WIN32_LEAN_AND_MEAN
  46. #endif
  47. #include <winsock2.h>
  48. #include <ws2tcpip.h>
  49. #include <stddef.h>
  50. #include <basetsd.h>
  51. #include <io.h>
  52. #ifndef _WIN32_WCE
  53. #include <fcntl.h>
  54. #else
  55. #define _O_RDONLY 0x0000
  56. #define O_RDONLY _O_RDONLY
  57. #endif
  58. #define LWS_INLINE __inline
  59. #define LWS_VISIBLE
  60. #define LWS_WARN_UNUSED_RESULT
  61. #define LWS_WARN_DEPRECATED
  62. #define LWS_FORMAT(string_index)
  63. #ifdef LWS_DLL
  64. #ifdef LWS_INTERNAL
  65. #define LWS_EXTERN extern __declspec(dllexport)
  66. #else
  67. #define LWS_EXTERN extern __declspec(dllimport)
  68. #endif
  69. #else
  70. #define LWS_EXTERN
  71. #endif
  72. #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
  73. #define LWS_O_RDONLY _O_RDONLY
  74. #define LWS_O_WRONLY _O_WRONLY
  75. #define LWS_O_CREAT _O_CREAT
  76. #define LWS_O_TRUNC _O_TRUNC
  77. #ifndef __func__
  78. #define __func__ __FUNCTION__
  79. #endif
  80. #else /* NOT WIN32 */
  81. #include <unistd.h>
  82. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  83. #include <sys/capability.h>
  84. #endif
  85. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
  86. #include <sys/socket.h>
  87. #include <netinet/in.h>
  88. #endif
  89. #define LWS_INLINE inline
  90. #define LWS_O_RDONLY O_RDONLY
  91. #define LWS_O_WRONLY O_WRONLY
  92. #define LWS_O_CREAT O_CREAT
  93. #define LWS_O_TRUNC O_TRUNC
  94. #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_WITH_ESP32)
  95. #include <poll.h>
  96. #include <netdb.h>
  97. #define LWS_INVALID_FILE -1
  98. #else
  99. #define getdtablesize() (30)
  100. #if defined(LWS_WITH_ESP32)
  101. #define LWS_INVALID_FILE NULL
  102. #else
  103. #define LWS_INVALID_FILE NULL
  104. #endif
  105. #endif
  106. #if defined(__GNUC__)
  107. /* warn_unused_result attribute only supported by GCC 3.4 or later */
  108. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  109. #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  110. #else
  111. #define LWS_WARN_UNUSED_RESULT
  112. #endif
  113. #define LWS_VISIBLE __attribute__((visibility("default")))
  114. #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
  115. #define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
  116. #else
  117. #define LWS_VISIBLE
  118. #define LWS_WARN_UNUSED_RESULT
  119. #define LWS_WARN_DEPRECATED
  120. #define LWS_FORMAT(string_index)
  121. #endif
  122. #if defined(__ANDROID__)
  123. #include <netinet/in.h>
  124. #include <unistd.h>
  125. #define getdtablesize() sysconf(_SC_OPEN_MAX)
  126. #endif
  127. #endif
  128. #if defined(LWS_WITH_LIBEV)
  129. #include <ev.h>
  130. #endif /* LWS_WITH_LIBEV */
  131. #ifdef LWS_WITH_LIBUV
  132. #include <uv.h>
  133. #ifdef LWS_HAVE_UV_VERSION_H
  134. #include <uv-version.h>
  135. #endif
  136. #ifdef LWS_HAVE_NEW_UV_VERSION_H
  137. #include <uv/version.h>
  138. #endif
  139. #endif /* LWS_WITH_LIBUV */
  140. #if defined(LWS_WITH_LIBEVENT)
  141. #include <event2/event.h>
  142. #endif /* LWS_WITH_LIBEVENT */
  143. #ifndef LWS_EXTERN
  144. #define LWS_EXTERN extern
  145. #endif
  146. #ifdef _WIN32
  147. #define random rand
  148. #else
  149. #if !defined(OPTEE_TA)
  150. #include <sys/time.h>
  151. #include <unistd.h>
  152. #endif
  153. #endif
  154. #if defined(LWS_WITH_TLS)
  155. #ifdef USE_WOLFSSL
  156. #ifdef USE_OLD_CYASSL
  157. #ifdef _WIN32
  158. /*
  159. * Include user-controlled settings for windows from
  160. * <wolfssl-root>/IDE/WIN/user_settings.h
  161. */
  162. #include <IDE/WIN/user_settings.h>
  163. #include <cyassl/ctaocrypt/settings.h>
  164. #else
  165. #include <cyassl/options.h>
  166. #endif
  167. #include <cyassl/openssl/ssl.h>
  168. #include <cyassl/error-ssl.h>
  169. #else
  170. #ifdef _WIN32
  171. /*
  172. * Include user-controlled settings for windows from
  173. * <wolfssl-root>/IDE/WIN/user_settings.h
  174. */
  175. #include <IDE/WIN/user_settings.h>
  176. #include <wolfssl/wolfcrypt/settings.h>
  177. #else
  178. #include <wolfssl/options.h>
  179. #endif
  180. #include <wolfssl/openssl/ssl.h>
  181. #include <wolfssl/error-ssl.h>
  182. #endif /* not USE_OLD_CYASSL */
  183. #else
  184. #if defined(LWS_WITH_MBEDTLS)
  185. #if defined(LWS_WITH_ESP32)
  186. /* this filepath is passed to us but without quotes or <> */
  187. #undef MBEDTLS_CONFIG_FILE
  188. #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
  189. #endif
  190. #include <mbedtls/ssl.h>
  191. #else
  192. #include <openssl/ssl.h>
  193. #if !defined(LWS_WITH_MBEDTLS)
  194. #include <openssl/err.h>
  195. #endif
  196. #endif
  197. #endif /* not USE_WOLFSSL */
  198. #endif
  199. /*
  200. * Helpers for pthread mutex in user code... if lws is built for
  201. * multiple service threads, these resolve to pthread mutex
  202. * operations. In the case LWS_MAX_SMP is 1 (the default), they
  203. * are all NOPs and no pthread type or api is referenced.
  204. */
  205. #if LWS_MAX_SMP > 1
  206. #include <pthread.h>
  207. #define lws_pthread_mutex(name) pthread_mutex_t name;
  208. static LWS_INLINE void
  209. lws_pthread_mutex_init(pthread_mutex_t *lock)
  210. {
  211. pthread_mutex_init(lock, NULL);
  212. }
  213. static LWS_INLINE void
  214. lws_pthread_mutex_destroy(pthread_mutex_t *lock)
  215. {
  216. pthread_mutex_destroy(lock);
  217. }
  218. static LWS_INLINE void
  219. lws_pthread_mutex_lock(pthread_mutex_t *lock)
  220. {
  221. pthread_mutex_lock(lock);
  222. }
  223. static LWS_INLINE void
  224. lws_pthread_mutex_unlock(pthread_mutex_t *lock)
  225. {
  226. pthread_mutex_unlock(lock);
  227. }
  228. #else
  229. #define lws_pthread_mutex(name)
  230. #define lws_pthread_mutex_init(_a)
  231. #define lws_pthread_mutex_destroy(_a)
  232. #define lws_pthread_mutex_lock(_a)
  233. #define lws_pthread_mutex_unlock(_a)
  234. #endif
  235. #define CONTEXT_PORT_NO_LISTEN -1
  236. #define CONTEXT_PORT_NO_LISTEN_SERVER -2
  237. /** \defgroup log Logging
  238. *
  239. * ##Logging
  240. *
  241. * Lws provides flexible and filterable logging facilities, which can be
  242. * used inside lws and in user code.
  243. *
  244. * Log categories may be individually filtered bitwise, and directed to built-in
  245. * sinks for syslog-compatible logging, or a user-defined function.
  246. */
  247. ///@{
  248. enum lws_log_levels {
  249. LLL_ERR = 1 << 0,
  250. LLL_WARN = 1 << 1,
  251. LLL_NOTICE = 1 << 2,
  252. LLL_INFO = 1 << 3,
  253. LLL_DEBUG = 1 << 4,
  254. LLL_PARSER = 1 << 5,
  255. LLL_HEADER = 1 << 6,
  256. LLL_EXT = 1 << 7,
  257. LLL_CLIENT = 1 << 8,
  258. LLL_LATENCY = 1 << 9,
  259. LLL_USER = 1 << 10,
  260. LLL_COUNT = 11 /* set to count of valid flags */
  261. };
  262. LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...) LWS_FORMAT(2);
  263. LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
  264. /**
  265. * lwsl_timestamp: generate logging timestamp string
  266. *
  267. * \param level: logging level
  268. * \param p: char * buffer to take timestamp
  269. * \param len: length of p
  270. *
  271. * returns length written in p
  272. */
  273. LWS_VISIBLE LWS_EXTERN int
  274. lwsl_timestamp(int level, char *p, int len);
  275. /* these guys are unconditionally included */
  276. #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
  277. #define lwsl_user(...) _lws_log(LLL_USER, __VA_ARGS__)
  278. #if !defined(LWS_WITH_NO_LOGS)
  279. /* notice and warn are usually included by being compiled in */
  280. #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
  281. #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
  282. #endif
  283. /*
  284. * weaker logging can be deselected by telling CMake to build in RELEASE mode
  285. * that gets rid of the overhead of checking while keeping _warn and _err
  286. * active
  287. */
  288. #ifdef _DEBUG
  289. #if defined(LWS_WITH_NO_LOGS)
  290. /* notice, warn and log are always compiled in */
  291. #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
  292. #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
  293. #endif
  294. #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
  295. #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
  296. #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
  297. #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
  298. #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
  299. #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
  300. #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
  301. #else /* no debug */
  302. #if defined(LWS_WITH_NO_LOGS)
  303. #define lwsl_warn(...) do {} while(0)
  304. #define lwsl_notice(...) do {} while(0)
  305. #endif
  306. #define lwsl_info(...) do {} while(0)
  307. #define lwsl_debug(...) do {} while(0)
  308. #define lwsl_parser(...) do {} while(0)
  309. #define lwsl_header(...) do {} while(0)
  310. #define lwsl_ext(...) do {} while(0)
  311. #define lwsl_client(...) do {} while(0)
  312. #define lwsl_latency(...) do {} while(0)
  313. #endif
  314. #define lwsl_hexdump_err(...) lwsl_hexdump_level(LLL_ERR, __VA_ARGS__)
  315. #define lwsl_hexdump_warn(...) lwsl_hexdump_level(LLL_WARN, __VA_ARGS__)
  316. #define lwsl_hexdump_notice(...) lwsl_hexdump_level(LLL_NOTICE, __VA_ARGS__)
  317. #define lwsl_hexdump_info(...) lwsl_hexdump_level(LLL_INFO, __VA_ARGS__)
  318. #define lwsl_hexdump_debug(...) lwsl_hexdump_level(LLL_DEBUG, __VA_ARGS__)
  319. /**
  320. * lwsl_hexdump_level() - helper to hexdump a buffer at a selected debug level
  321. *
  322. * \param level: one of LLL_ constants
  323. * \param vbuf: buffer start to dump
  324. * \param len: length of buffer to dump
  325. *
  326. * If \p level is visible, does a nice hexdump -C style dump of \p vbuf for
  327. * \p len bytes. This can be extremely convenient while debugging.
  328. */
  329. LWS_VISIBLE LWS_EXTERN void
  330. lwsl_hexdump_level(int level, const void *vbuf, size_t len);
  331. /**
  332. * lwsl_hexdump() - helper to hexdump a buffer (DEBUG builds only)
  333. *
  334. * \param buf: buffer start to dump
  335. * \param len: length of buffer to dump
  336. *
  337. * Calls through to lwsl_hexdump_level(LLL_DEBUG, ... for compatability.
  338. * It's better to use lwsl_hexdump_level(level, ... directly so you can control
  339. * the visibility.
  340. */
  341. LWS_VISIBLE LWS_EXTERN void
  342. lwsl_hexdump(const void *buf, size_t len);
  343. /**
  344. * lws_is_be() - returns nonzero if the platform is Big Endian
  345. */
  346. static LWS_INLINE int lws_is_be(void) {
  347. const int probe = ~0xff;
  348. return *(const char *)&probe;
  349. }
  350. /**
  351. * lws_set_log_level() - Set the logging bitfield
  352. * \param level: OR together the LLL_ debug contexts you want output from
  353. * \param log_emit_function: NULL to leave it as it is, or a user-supplied
  354. * function to perform log string emission instead of
  355. * the default stderr one.
  356. *
  357. * log level defaults to "err", "warn" and "notice" contexts enabled and
  358. * emission on stderr. If stderr is a tty (according to isatty()) then
  359. * the output is coloured according to the log level using ANSI escapes.
  360. */
  361. LWS_VISIBLE LWS_EXTERN void
  362. lws_set_log_level(int level,
  363. void (*log_emit_function)(int level, const char *line));
  364. /**
  365. * lwsl_emit_syslog() - helper log emit function writes to system log
  366. *
  367. * \param level: one of LLL_ log level indexes
  368. * \param line: log string
  369. *
  370. * You use this by passing the function pointer to lws_set_log_level(), to set
  371. * it as the log emit function, it is not called directly.
  372. */
  373. LWS_VISIBLE LWS_EXTERN void
  374. lwsl_emit_syslog(int level, const char *line);
  375. /**
  376. * lwsl_visible() - returns true if the log level should be printed
  377. *
  378. * \param level: one of LLL_ log level indexes
  379. *
  380. * This is useful if you have to do work to generate the log content, you
  381. * can skip the work if the log level used to print it is not actually
  382. * enabled at runtime.
  383. */
  384. LWS_VISIBLE LWS_EXTERN int
  385. lwsl_visible(int level);
  386. ///@}
  387. #include <stddef.h>
  388. #ifndef lws_container_of
  389. #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
  390. #endif
  391. struct lws;
  392. typedef int64_t lws_usec_t;
  393. /* api change list for user code to test against */
  394. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
  395. /* the struct lws_protocols has the id field present */
  396. #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
  397. /* you can call lws_get_peer_write_allowance */
  398. #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
  399. /* extra parameter introduced in 917f43ab821 */
  400. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
  401. /* File operations stuff exists */
  402. #define LWS_FEATURE_FOPS
  403. #if defined(_WIN32)
  404. typedef SOCKET lws_sockfd_type;
  405. typedef HANDLE lws_filefd_type;
  406. struct lws_pollfd {
  407. lws_sockfd_type fd; /**< file descriptor */
  408. SHORT events; /**< which events to respond to */
  409. SHORT revents; /**< which events happened */
  410. };
  411. #define LWS_POLLHUP (FD_CLOSE)
  412. #define LWS_POLLIN (FD_READ | FD_ACCEPT)
  413. #define LWS_POLLOUT (FD_WRITE)
  414. #else
  415. #if defined(LWS_WITH_ESP32)
  416. typedef int lws_sockfd_type;
  417. typedef int lws_filefd_type;
  418. struct pollfd {
  419. lws_sockfd_type fd; /**< fd related to */
  420. short events; /**< which POLL... events to respond to */
  421. short revents; /**< which POLL... events occurred */
  422. };
  423. #define POLLIN 0x0001
  424. #define POLLPRI 0x0002
  425. #define POLLOUT 0x0004
  426. #define POLLERR 0x0008
  427. #define POLLHUP 0x0010
  428. #define POLLNVAL 0x0020
  429. #include <freertos/FreeRTOS.h>
  430. #include <freertos/event_groups.h>
  431. #include <string.h>
  432. #include "esp_wifi.h"
  433. #include "esp_system.h"
  434. #include "esp_event.h"
  435. #include "esp_event_loop.h"
  436. #include "nvs.h"
  437. #include "driver/gpio.h"
  438. #include "esp_spi_flash.h"
  439. #include "freertos/timers.h"
  440. #if !defined(CONFIG_FREERTOS_HZ)
  441. #define CONFIG_FREERTOS_HZ 100
  442. #endif
  443. typedef TimerHandle_t uv_timer_t;
  444. typedef void uv_cb_t(uv_timer_t *);
  445. typedef void * uv_handle_t;
  446. struct timer_mapping {
  447. uv_cb_t *cb;
  448. uv_timer_t *t;
  449. };
  450. #define UV_VERSION_MAJOR 1
  451. #define lws_uv_getloop(a, b) (NULL)
  452. static LWS_INLINE void uv_timer_init(void *l, uv_timer_t *t)
  453. {
  454. (void)l;
  455. *t = NULL;
  456. }
  457. extern void esp32_uvtimer_cb(TimerHandle_t t);
  458. static LWS_INLINE void uv_timer_start(uv_timer_t *t, uv_cb_t *cb, int first, int rep)
  459. {
  460. struct timer_mapping *tm = (struct timer_mapping *)malloc(sizeof(*tm));
  461. if (!tm)
  462. return;
  463. tm->t = t;
  464. tm->cb = cb;
  465. *t = xTimerCreate("x", pdMS_TO_TICKS(first), !!rep, tm,
  466. (TimerCallbackFunction_t)esp32_uvtimer_cb);
  467. xTimerStart(*t, 0);
  468. }
  469. static LWS_INLINE void uv_timer_stop(uv_timer_t *t)
  470. {
  471. xTimerStop(*t, 0);
  472. }
  473. static LWS_INLINE void uv_close(uv_handle_t *h, void *v)
  474. {
  475. free(pvTimerGetTimerID((uv_timer_t)h));
  476. xTimerDelete(*(uv_timer_t *)h, 0);
  477. }
  478. /* ESP32 helper declarations */
  479. #include <mdns.h>
  480. #include <esp_partition.h>
  481. #define LWS_PLUGIN_STATIC
  482. #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
  483. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
  484. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
  485. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
  486. /* user code provides these */
  487. extern void
  488. lws_esp32_identify_physical_device(void);
  489. /* lws-plat-esp32 provides these */
  490. typedef void (*lws_cb_scan_done)(uint16_t count, wifi_ap_record_t *recs, void *arg);
  491. enum genled_state {
  492. LWSESP32_GENLED__INIT,
  493. LWSESP32_GENLED__LOST_NETWORK,
  494. LWSESP32_GENLED__NO_NETWORK,
  495. LWSESP32_GENLED__CONN_AP,
  496. LWSESP32_GENLED__GOT_IP,
  497. LWSESP32_GENLED__OK,
  498. };
  499. struct lws_group_member {
  500. struct lws_group_member *next;
  501. uint64_t last_seen;
  502. char model[16];
  503. char role[16];
  504. char host[32];
  505. char mac[20];
  506. int width, height;
  507. struct ip4_addr addr;
  508. struct ip6_addr addrv6;
  509. uint8_t flags;
  510. };
  511. #define LWS_SYSTEM_GROUP_MEMBER_ADD 1
  512. #define LWS_SYSTEM_GROUP_MEMBER_CHANGE 2
  513. #define LWS_SYSTEM_GROUP_MEMBER_REMOVE 3
  514. #define LWS_GROUP_FLAG_SELF 1
  515. struct lws_esp32 {
  516. char sta_ip[16];
  517. char sta_mask[16];
  518. char sta_gw[16];
  519. char serial[16];
  520. char opts[16];
  521. char model[16];
  522. char group[16];
  523. char role[16];
  524. char ssid[4][64];
  525. char password[4][64];
  526. char active_ssid[64];
  527. char access_pw[16];
  528. char hostname[32];
  529. char mac[20];
  530. char le_dns[64];
  531. char le_email[64];
  532. char region;
  533. char inet;
  534. char conn_ap;
  535. enum genled_state genled;
  536. uint64_t genled_t;
  537. lws_cb_scan_done scan_consumer;
  538. void *scan_consumer_arg;
  539. struct lws_group_member *first;
  540. int extant_group_members;
  541. char acme;
  542. char upload;
  543. volatile char button_is_down;
  544. };
  545. struct lws_esp32_image {
  546. uint32_t romfs;
  547. uint32_t romfs_len;
  548. uint32_t json;
  549. uint32_t json_len;
  550. };
  551. extern struct lws_esp32 lws_esp32;
  552. struct lws_vhost;
  553. extern esp_err_t
  554. lws_esp32_event_passthru(void *ctx, system_event_t *event);
  555. extern void
  556. lws_esp32_wlan_config(void);
  557. extern void
  558. lws_esp32_wlan_start_ap(void);
  559. extern void
  560. lws_esp32_wlan_start_station(void);
  561. struct lws_context_creation_info;
  562. extern void
  563. lws_esp32_set_creation_defaults(struct lws_context_creation_info *info);
  564. extern struct lws_context *
  565. lws_esp32_init(struct lws_context_creation_info *, struct lws_vhost **pvh);
  566. extern int
  567. lws_esp32_wlan_nvs_get(int retry);
  568. extern esp_err_t
  569. lws_nvs_set_str(nvs_handle handle, const char* key, const char* value);
  570. extern void
  571. lws_esp32_restart_guided(uint32_t type);
  572. extern const esp_partition_t *
  573. lws_esp_ota_get_boot_partition(void);
  574. extern int
  575. lws_esp32_get_image_info(const esp_partition_t *part, struct lws_esp32_image *i, char *json, int json_len);
  576. extern int
  577. lws_esp32_leds_network_indication(void);
  578. extern uint32_t lws_esp32_get_reboot_type(void);
  579. extern uint16_t lws_esp32_sine_interp(int n);
  580. /* required in external code by esp32 plat (may just return if no leds) */
  581. extern void lws_esp32_leds_timer_cb(TimerHandle_t th);
  582. #else
  583. typedef int lws_sockfd_type;
  584. typedef int lws_filefd_type;
  585. #endif
  586. #define lws_pollfd pollfd
  587. #define LWS_POLLHUP (POLLHUP|POLLERR)
  588. #define LWS_POLLIN (POLLIN)
  589. #define LWS_POLLOUT (POLLOUT)
  590. #endif
  591. #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
  592. /* ... */
  593. #define ssize_t SSIZE_T
  594. #endif
  595. #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
  596. #include <sys/types.h>
  597. #include <sys/stat.h>
  598. #endif
  599. #if defined(LWS_HAVE_STDINT_H)
  600. #include <stdint.h>
  601. #else
  602. #if defined(WIN32) || defined(_WIN32)
  603. /* !!! >:-[ */
  604. typedef unsigned __int32 uint32_t;
  605. typedef unsigned __int16 uint16_t;
  606. typedef unsigned __int8 uint8_t;
  607. #else
  608. typedef unsigned int uint32_t;
  609. typedef unsigned short uint16_t;
  610. typedef unsigned char uint8_t;
  611. #endif
  612. #endif
  613. typedef unsigned long long lws_filepos_t;
  614. typedef long long lws_fileofs_t;
  615. typedef uint32_t lws_fop_flags_t;
  616. /** struct lws_pollargs - argument structure for all external poll related calls
  617. * passed in via 'in' */
  618. struct lws_pollargs {
  619. lws_sockfd_type fd; /**< applicable socket descriptor */
  620. int events; /**< the new event mask */
  621. int prev_events; /**< the previous event mask */
  622. };
  623. struct lws_tokens;
  624. struct lws_token_limits;
  625. /*! \defgroup wsclose Websocket Close
  626. *
  627. * ##Websocket close frame control
  628. *
  629. * When we close a ws connection, we can send a reason code and a short
  630. * UTF-8 description back with the close packet.
  631. */
  632. ///@{
  633. /*
  634. * NOTE: These public enums are part of the abi. If you want to add one,
  635. * add it at where specified so existing users are unaffected.
  636. */
  637. /** enum lws_close_status - RFC6455 close status codes */
  638. enum lws_close_status {
  639. LWS_CLOSE_STATUS_NOSTATUS = 0,
  640. LWS_CLOSE_STATUS_NORMAL = 1000,
  641. /**< 1000 indicates a normal closure, meaning that the purpose for
  642. which the connection was established has been fulfilled. */
  643. LWS_CLOSE_STATUS_GOINGAWAY = 1001,
  644. /**< 1001 indicates that an endpoint is "going away", such as a server
  645. going down or a browser having navigated away from a page. */
  646. LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
  647. /**< 1002 indicates that an endpoint is terminating the connection due
  648. to a protocol error. */
  649. LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
  650. /**< 1003 indicates that an endpoint is terminating the connection
  651. because it has received a type of data it cannot accept (e.g., an
  652. endpoint that understands only text data MAY send this if it
  653. receives a binary message). */
  654. LWS_CLOSE_STATUS_RESERVED = 1004,
  655. /**< Reserved. The specific meaning might be defined in the future. */
  656. LWS_CLOSE_STATUS_NO_STATUS = 1005,
  657. /**< 1005 is a reserved value and MUST NOT be set as a status code in a
  658. Close control frame by an endpoint. It is designated for use in
  659. applications expecting a status code to indicate that no status
  660. code was actually present. */
  661. LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
  662. /**< 1006 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 the
  665. connection was closed abnormally, e.g., without sending or
  666. receiving a Close control frame. */
  667. LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
  668. /**< 1007 indicates that an endpoint is terminating the connection
  669. because it has received data within a message that was not
  670. consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
  671. data within a text message). */
  672. LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
  673. /**< 1008 indicates that an endpoint is terminating the connection
  674. because it has received a message that violates its policy. This
  675. is a generic status code that can be returned when there is no
  676. other more suitable status code (e.g., 1003 or 1009) or if there
  677. is a need to hide specific details about the policy. */
  678. LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
  679. /**< 1009 indicates that an endpoint is terminating the connection
  680. because it has received a message that is too big for it to
  681. process. */
  682. LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
  683. /**< 1010 indicates that an endpoint (client) is terminating the
  684. connection because it has expected the server to negotiate one or
  685. more extension, but the server didn't return them in the response
  686. message of the WebSocket handshake. The list of extensions that
  687. are needed SHOULD appear in the /reason/ part of the Close frame.
  688. Note that this status code is not used by the server, because it
  689. can fail the WebSocket handshake instead */
  690. LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
  691. /**< 1011 indicates that a server is terminating the connection because
  692. it encountered an unexpected condition that prevented it from
  693. fulfilling the request. */
  694. LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
  695. /**< 1015 is a reserved value and MUST NOT be set as a status code in a
  696. Close control frame by an endpoint. It is designated for use in
  697. applications expecting a status code to indicate that the
  698. connection was closed due to a failure to perform a TLS handshake
  699. (e.g., the server certificate can't be verified). */
  700. LWS_CLOSE_STATUS_CLIENT_TRANSACTION_DONE = 2000,
  701. /****** add new things just above ---^ ******/
  702. LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
  703. };
  704. /**
  705. * lws_close_reason - Set reason and aux data to send with Close packet
  706. * If you are going to return nonzero from the callback
  707. * requesting the connection to close, you can optionally
  708. * call this to set the reason the peer will be told if
  709. * possible.
  710. *
  711. * \param wsi: The websocket connection to set the close reason on
  712. * \param status: A valid close status from websocket standard
  713. * \param buf: NULL or buffer containing up to 124 bytes of auxiliary data
  714. * \param len: Length of data in \param buf to send
  715. */
  716. LWS_VISIBLE LWS_EXTERN void
  717. lws_close_reason(struct lws *wsi, enum lws_close_status status,
  718. unsigned char *buf, size_t len);
  719. ///@}
  720. struct lws;
  721. struct lws_context;
  722. /* needed even with extensions disabled for create context */
  723. struct lws_extension;
  724. /*! \defgroup usercb User Callback
  725. *
  726. * ##User protocol callback
  727. *
  728. * The protocol callback is the primary way lws interacts with
  729. * user code. For one of a list of a few dozen reasons the callback gets
  730. * called at some event to be handled.
  731. *
  732. * All of the events can be ignored, returning 0 is taken as "OK" and returning
  733. * nonzero in most cases indicates that the connection should be closed.
  734. */
  735. ///@{
  736. struct lws_ssl_info {
  737. int where;
  738. int ret;
  739. };
  740. enum lws_cert_update_state {
  741. LWS_CUS_IDLE,
  742. LWS_CUS_STARTING,
  743. LWS_CUS_SUCCESS,
  744. LWS_CUS_FAILED,
  745. LWS_CUS_CREATE_KEYS,
  746. LWS_CUS_REG,
  747. LWS_CUS_AUTH,
  748. LWS_CUS_CHALLENGE,
  749. LWS_CUS_CREATE_REQ,
  750. LWS_CUS_REQ,
  751. LWS_CUS_CONFIRM,
  752. LWS_CUS_ISSUE,
  753. };
  754. enum {
  755. LWS_TLS_REQ_ELEMENT_COUNTRY,
  756. LWS_TLS_REQ_ELEMENT_STATE,
  757. LWS_TLS_REQ_ELEMENT_LOCALITY,
  758. LWS_TLS_REQ_ELEMENT_ORGANIZATION,
  759. LWS_TLS_REQ_ELEMENT_COMMON_NAME,
  760. LWS_TLS_REQ_ELEMENT_EMAIL,
  761. LWS_TLS_REQ_ELEMENT_COUNT,
  762. LWS_TLS_SET_DIR_URL = LWS_TLS_REQ_ELEMENT_COUNT,
  763. LWS_TLS_SET_AUTH_PATH,
  764. LWS_TLS_SET_CERT_PATH,
  765. LWS_TLS_SET_KEY_PATH,
  766. LWS_TLS_TOTAL_COUNT
  767. };
  768. struct lws_acme_cert_aging_args {
  769. struct lws_vhost *vh;
  770. const char *element_overrides[LWS_TLS_TOTAL_COUNT]; /* NULL = use pvo */
  771. };
  772. /*
  773. * NOTE: These public enums are part of the abi. If you want to add one,
  774. * add it at where specified so existing users are unaffected.
  775. */
  776. /** enum lws_callback_reasons - reason you're getting a protocol callback */
  777. enum lws_callback_reasons {
  778. /* ---------------------------------------------------------------------
  779. * ----- Callbacks related to wsi and protocol binding lifecycle -----
  780. */
  781. LWS_CALLBACK_PROTOCOL_INIT = 27,
  782. /**< One-time call per protocol, per-vhost using it, so it can
  783. * do initial setup / allocations etc */
  784. LWS_CALLBACK_PROTOCOL_DESTROY = 28,
  785. /**< One-time call per protocol, per-vhost using it, indicating
  786. * this protocol won't get used at all after this callback, the
  787. * vhost is getting destroyed. Take the opportunity to
  788. * deallocate everything that was allocated by the protocol. */
  789. LWS_CALLBACK_WSI_CREATE = 29,
  790. /**< outermost (earliest) wsi create notification to protocols[0] */
  791. LWS_CALLBACK_WSI_DESTROY = 30,
  792. /**< outermost (latest) wsi destroy notification to protocols[0] */
  793. LWS_CALLBACK_HTTP_BIND_PROTOCOL = 49,
  794. /**< By default, all HTTP handling is done in protocols[0].
  795. * However you can bind different protocols (by name) to
  796. * different parts of the URL space using callback mounts. This
  797. * callback occurs in the new protocol when a wsi is bound
  798. * to that protocol. Any protocol allocation related to the
  799. * http transaction processing should be created then.
  800. * These specific callbacks are necessary because with HTTP/1.1,
  801. * a single connection may perform at series of different
  802. * transactions at different URLs, thus the lifetime of the
  803. * protocol bind is just for one transaction, not connection. */
  804. LWS_CALLBACK_HTTP_DROP_PROTOCOL = 50,
  805. /**< This is called when a transaction is unbound from a protocol.
  806. * It indicates the connection completed its transaction and may
  807. * do something different now. Any protocol allocation related
  808. * to the http transaction processing should be destroyed. */
  809. /* ---------------------------------------------------------------------
  810. * ----- Callbacks related to Server TLS -----
  811. */
  812. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS = 21,
  813. /**< if configured for
  814. * including OpenSSL support, this callback allows your user code
  815. * to perform extra SSL_CTX_load_verify_locations() or similar
  816. * calls to direct OpenSSL where to find certificates the client
  817. * can use to confirm the remote server identity. user is the
  818. * OpenSSL SSL_CTX* */
  819. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS = 22,
  820. /**< if configured for
  821. * including OpenSSL support, this callback allows your user code
  822. * to load extra certificates into the server which allow it to
  823. * verify the validity of certificates returned by clients. user
  824. * is the server's OpenSSL SSL_CTX* and in is the lws_vhost */
  825. LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION = 23,
  826. /**< if the libwebsockets vhost was created with the option
  827. * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
  828. * callback is generated during OpenSSL verification of the cert
  829. * sent from the client. It is sent to protocol[0] callback as
  830. * no protocol has been negotiated on the connection yet.
  831. * Notice that the libwebsockets context and wsi are both NULL
  832. * during this callback. See
  833. * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  834. * to understand more detail about the OpenSSL callback that
  835. * generates this libwebsockets callback and the meanings of the
  836. * arguments passed. In this callback, user is the x509_ctx,
  837. * in is the ssl pointer and len is preverify_ok
  838. * Notice that this callback maintains libwebsocket return
  839. * conventions, return 0 to mean the cert is OK or 1 to fail it.
  840. * This also means that if you don't handle this callback then
  841. * the default callback action of returning 0 allows the client
  842. * certificates. */
  843. LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY = 37,
  844. /**< if configured for including OpenSSL support but no private key
  845. * file has been specified (ssl_private_key_filepath is NULL), this is
  846. * called to allow the user to set the private key directly via
  847. * libopenssl and perform further operations if required; this might be
  848. * useful in situations where the private key is not directly accessible
  849. * by the OS, for example if it is stored on a smartcard.
  850. * user is the server's OpenSSL SSL_CTX* */
  851. LWS_CALLBACK_SSL_INFO = 67,
  852. /**< SSL connections only. An event you registered an
  853. * interest in at the vhost has occurred on a connection
  854. * using the vhost. in is a pointer to a
  855. * struct lws_ssl_info containing information about the
  856. * event*/
  857. /* ---------------------------------------------------------------------
  858. * ----- Callbacks related to Client TLS -----
  859. */
  860. LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION = 58,
  861. /**< Similar to LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION
  862. * this callback is called during OpenSSL verification of the cert
  863. * sent from the server to the client. It is sent to protocol[0]
  864. * callback as no protocol has been negotiated on the connection yet.
  865. * Notice that the wsi is set because lws_client_connect_via_info was
  866. * successful.
  867. *
  868. * See http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  869. * to understand more detail about the OpenSSL callback that
  870. * generates this libwebsockets callback and the meanings of the
  871. * arguments passed. In this callback, user is the x509_ctx,
  872. * in is the ssl pointer and len is preverify_ok.
  873. *
  874. * THIS IS NOT RECOMMENDED BUT if a cert validation error shall be
  875. * overruled and cert shall be accepted as ok,
  876. * X509_STORE_CTX_set_error((X509_STORE_CTX*)user, X509_V_OK); must be
  877. * called and return value must be 0 to mean the cert is OK;
  878. * returning 1 will fail the cert in any case.
  879. *
  880. * This also means that if you don't handle this callback then
  881. * the default callback action of returning 0 will not accept the
  882. * certificate in case of a validation error decided by the SSL lib.
  883. *
  884. * This is expected and secure behaviour when validating certificates.
  885. *
  886. * Note: LCCSCF_ALLOW_SELFSIGNED and
  887. * LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK still work without this
  888. * callback being implemented.
  889. */
  890. /* ---------------------------------------------------------------------
  891. * ----- Callbacks related to HTTP Server -----
  892. */
  893. LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED = 19,
  894. /**< A new client has been accepted by the ws server. This
  895. * callback allows setting any relevant property to it. Because this
  896. * happens immediately after the instantiation of a new client,
  897. * there's no websocket protocol selected yet so this callback is
  898. * issued only to protocol 0. Only wsi is defined, pointing to the
  899. * new client, and the return value is ignored. */
  900. LWS_CALLBACK_HTTP = 12,
  901. /**< an http request has come from a client that is not
  902. * asking to upgrade the connection to a websocket
  903. * one. This is a chance to serve http content,
  904. * for example, to send a script to the client
  905. * which will then open the websockets connection.
  906. * in points to the URI path requested and
  907. * lws_serve_http_file() makes it very
  908. * simple to send back a file to the client.
  909. * Normally after sending the file you are done
  910. * with the http connection, since the rest of the
  911. * activity will come by websockets from the script
  912. * that was delivered by http, so you will want to
  913. * return 1; to close and free up the connection. */
  914. LWS_CALLBACK_HTTP_BODY = 13,
  915. /**< the next len bytes data from the http
  916. * request body HTTP connection is now available in in. */
  917. LWS_CALLBACK_HTTP_BODY_COMPLETION = 14,
  918. /**< the expected amount of http request body has been delivered */
  919. LWS_CALLBACK_HTTP_FILE_COMPLETION = 15,
  920. /**< a file requested to be sent down http link has completed. */
  921. LWS_CALLBACK_HTTP_WRITEABLE = 16,
  922. /**< you can write more down the http protocol link now. */
  923. LWS_CALLBACK_CLOSED_HTTP = 5,
  924. /**< when a HTTP (non-websocket) session ends */
  925. LWS_CALLBACK_FILTER_HTTP_CONNECTION = 18,
  926. /**< called when the request has
  927. * been received and parsed from the client, but the response is
  928. * not sent yet. Return non-zero to disallow the connection.
  929. * user is a pointer to the connection user space allocation,
  930. * in is the URI, eg, "/"
  931. * In your handler you can use the public APIs
  932. * lws_hdr_total_length() / lws_hdr_copy() to access all of the
  933. * headers using the header enums lws_token_indexes from
  934. * libwebsockets.h to check for and read the supported header
  935. * presence and content before deciding to allow the http
  936. * connection to proceed or to kill the connection. */
  937. LWS_CALLBACK_ADD_HEADERS = 53,
  938. /**< This gives your user code a chance to add headers to a server
  939. * transaction bound to your protocol. `in` points to a
  940. * `struct lws_process_html_args` describing a buffer and length
  941. * you can add headers into using the normal lws apis.
  942. *
  943. * (see LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER to add headers to
  944. * a client transaction)
  945. *
  946. * Only `args->p` and `args->len` are valid, and `args->p` should
  947. * be moved on by the amount of bytes written, if any. Eg
  948. *
  949. * case LWS_CALLBACK_ADD_HEADERS:
  950. *
  951. * struct lws_process_html_args *args =
  952. * (struct lws_process_html_args *)in;
  953. *
  954. * if (lws_add_http_header_by_name(wsi,
  955. * (unsigned char *)"set-cookie:",
  956. * (unsigned char *)cookie, cookie_len,
  957. * (unsigned char **)&args->p,
  958. * (unsigned char *)args->p + args->max_len))
  959. * return 1;
  960. *
  961. * break;
  962. */
  963. LWS_CALLBACK_CHECK_ACCESS_RIGHTS = 51,
  964. /**< This gives the user code a chance to forbid an http access.
  965. * `in` points to a `struct lws_process_html_args`, which
  966. * describes the URL, and a bit mask describing the type of
  967. * authentication required. If the callback returns nonzero,
  968. * the transaction ends with HTTP_STATUS_UNAUTHORIZED. */
  969. LWS_CALLBACK_PROCESS_HTML = 52,
  970. /**< This gives your user code a chance to mangle outgoing
  971. * HTML. `in` points to a `struct lws_process_html_args`
  972. * which describes the buffer containing outgoing HTML.
  973. * The buffer may grow up to `.max_len` (currently +128
  974. * bytes per buffer).
  975. */
  976. /* ---------------------------------------------------------------------
  977. * ----- Callbacks related to HTTP Client -----
  978. */
  979. LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP = 44,
  980. /**< The HTTP client connection has succeeded, and is now
  981. * connected to the server */
  982. LWS_CALLBACK_CLOSED_CLIENT_HTTP = 45,
  983. /**< The HTTP client connection is closing */
  984. LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ = 48,
  985. /**< This is generated by lws_http_client_read() used to drain
  986. * incoming data. In the case the incoming data was chunked, it will
  987. * be split into multiple smaller callbacks for each chunk block,
  988. * removing the chunk headers. If not chunked, it will appear all in
  989. * one callback. */
  990. LWS_CALLBACK_RECEIVE_CLIENT_HTTP = 46,
  991. /**< This simply indicates data was received on the HTTP client
  992. * connection. It does NOT drain or provide the data.
  993. * This exists to neatly allow a proxying type situation,
  994. * where this incoming data will go out on another connection.
  995. * If the outgoing connection stalls, we should stall processing
  996. * the incoming data. So a handler for this in that case should
  997. * simply set a flag to indicate there is incoming data ready
  998. * and ask for a writeable callback on the outgoing connection.
  999. * In the writable callback he can check the flag and then get
  1000. * and drain the waiting incoming data using lws_http_client_read().
  1001. * This will use callbacks to LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ
  1002. * to get and drain the incoming data, where it should be sent
  1003. * back out on the outgoing connection. */
  1004. LWS_CALLBACK_COMPLETED_CLIENT_HTTP = 47,
  1005. /**< The client transaction completed... at the moment this
  1006. * is the same as closing since transaction pipelining on
  1007. * client side is not yet supported. */
  1008. LWS_CALLBACK_CLIENT_HTTP_WRITEABLE = 57,
  1009. /**< when doing an HTTP type client connection, you can call
  1010. * lws_client_http_body_pending(wsi, 1) from
  1011. * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER to get these callbacks
  1012. * sending the HTTP headers.
  1013. *
  1014. * From this callback, when you have sent everything, you should let
  1015. * lws know by calling lws_client_http_body_pending(wsi, 0)
  1016. */
  1017. /* ---------------------------------------------------------------------
  1018. * ----- Callbacks related to Websocket Server -----
  1019. */
  1020. LWS_CALLBACK_ESTABLISHED = 0,
  1021. /**< (VH) after the server completes a handshake with an incoming
  1022. * client. If you built the library with ssl support, in is a
  1023. * pointer to the ssl struct associated with the connection or NULL.
  1024. *
  1025. * b0 of len is set if the connection was made using ws-over-h2
  1026. */
  1027. LWS_CALLBACK_CLOSED = 4,
  1028. /**< when the websocket session ends */
  1029. LWS_CALLBACK_SERVER_WRITEABLE = 11,
  1030. /**< See LWS_CALLBACK_CLIENT_WRITEABLE */
  1031. LWS_CALLBACK_RECEIVE = 6,
  1032. /**< data has appeared for this server endpoint from a
  1033. * remote client, it can be found at *in and is
  1034. * len bytes long */
  1035. LWS_CALLBACK_RECEIVE_PONG = 7,
  1036. /**< servers receive PONG packets with this callback reason */
  1037. LWS_CALLBACK_WS_PEER_INITIATED_CLOSE = 38,
  1038. /**< The peer has sent an unsolicited Close WS packet. in and
  1039. * len are the optional close code (first 2 bytes, network
  1040. * order) and the optional additional information which is not
  1041. * defined in the standard, and may be a string or non human-readable
  1042. * data.
  1043. * If you return 0 lws will echo the close and then close the
  1044. * connection. If you return nonzero lws will just close the
  1045. * connection. */
  1046. LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION = 20,
  1047. /**< called when the handshake has
  1048. * been received and parsed from the client, but the response is
  1049. * not sent yet. Return non-zero to disallow the connection.
  1050. * user is a pointer to the connection user space allocation,
  1051. * in is the requested protocol name
  1052. * In your handler you can use the public APIs
  1053. * lws_hdr_total_length() / lws_hdr_copy() to access all of the
  1054. * headers using the header enums lws_token_indexes from
  1055. * libwebsockets.h to check for and read the supported header
  1056. * presence and content before deciding to allow the handshake
  1057. * to proceed or to kill the connection. */
  1058. LWS_CALLBACK_CONFIRM_EXTENSION_OKAY = 25,
  1059. /**< When the server handshake code
  1060. * sees that it does support a requested extension, before
  1061. * accepting the extension by additing to the list sent back to
  1062. * the client it gives this callback just to check that it's okay
  1063. * to use that extension. It calls back to the requested protocol
  1064. * and with in being the extension name, len is 0 and user is
  1065. * valid. Note though at this time the ESTABLISHED callback hasn't
  1066. * happened yet so if you initialize user content there, user
  1067. * content during this callback might not be useful for anything. */
  1068. /* ---------------------------------------------------------------------
  1069. * ----- Callbacks related to Websocket Client -----
  1070. */
  1071. LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 1,
  1072. /**< the request client connection has been unable to complete a
  1073. * handshake with the remote server. If in is non-NULL, you can
  1074. * find an error string of length len where it points to
  1075. *
  1076. * Diagnostic strings that may be returned include
  1077. *
  1078. * "getaddrinfo (ipv6) failed"
  1079. * "unknown address family"
  1080. * "getaddrinfo (ipv4) failed"
  1081. * "set socket opts failed"
  1082. * "insert wsi failed"
  1083. * "lws_ssl_client_connect1 failed"
  1084. * "lws_ssl_client_connect2 failed"
  1085. * "Peer hung up"
  1086. * "read failed"
  1087. * "HS: URI missing"
  1088. * "HS: Redirect code but no Location"
  1089. * "HS: URI did not parse"
  1090. * "HS: Redirect failed"
  1091. * "HS: Server did not return 200"
  1092. * "HS: OOM"
  1093. * "HS: disallowed by client filter"
  1094. * "HS: disallowed at ESTABLISHED"
  1095. * "HS: ACCEPT missing"
  1096. * "HS: ws upgrade response not 101"
  1097. * "HS: UPGRADE missing"
  1098. * "HS: Upgrade to something other than websocket"
  1099. * "HS: CONNECTION missing"
  1100. * "HS: UPGRADE malformed"
  1101. * "HS: PROTOCOL malformed"
  1102. * "HS: Cannot match protocol"
  1103. * "HS: EXT: list too big"
  1104. * "HS: EXT: failed setting defaults"
  1105. * "HS: EXT: failed parsing defaults"
  1106. * "HS: EXT: failed parsing options"
  1107. * "HS: EXT: Rejects server options"
  1108. * "HS: EXT: unknown ext"
  1109. * "HS: Accept hash wrong"
  1110. * "HS: Rejected by filter cb"
  1111. * "HS: OOM"
  1112. * "HS: SO_SNDBUF failed"
  1113. * "HS: Rejected at CLIENT_ESTABLISHED"
  1114. */
  1115. LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH = 2,
  1116. /**< this is the last chance for the client user code to examine the
  1117. * http headers and decide to reject the connection. If the
  1118. * content in the headers is interesting to the
  1119. * client (url, etc) it needs to copy it out at
  1120. * this point since it will be destroyed before
  1121. * the CLIENT_ESTABLISHED call */
  1122. LWS_CALLBACK_CLIENT_ESTABLISHED = 3,
  1123. /**< after your client connection completed the websocket upgrade
  1124. * handshake with the remote server */
  1125. LWS_CALLBACK_CLIENT_CLOSED = 75,
  1126. /**< when a client websocket session ends */
  1127. LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER = 24,
  1128. /**< this callback happens
  1129. * when a client handshake is being compiled. user is NULL,
  1130. * in is a char **, it's pointing to a char * which holds the
  1131. * next location in the header buffer where you can add
  1132. * headers, and len is the remaining space in the header buffer,
  1133. * which is typically some hundreds of bytes. So, to add a canned
  1134. * cookie, your handler code might look similar to:
  1135. *
  1136. * char **p = (char **)in;
  1137. *
  1138. * if (len < 100)
  1139. * return 1;
  1140. *
  1141. * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
  1142. *
  1143. * return 0;
  1144. *
  1145. * Notice if you add anything, you just have to take care about
  1146. * the CRLF on the line you added. Obviously this callback is
  1147. * optional, if you don't handle it everything is fine.
  1148. *
  1149. * Notice the callback is coming to protocols[0] all the time,
  1150. * because there is no specific protocol negotiated yet.
  1151. *
  1152. * See LWS_CALLBACK_ADD_HEADERS for adding headers to server
  1153. * transactions.
  1154. */
  1155. LWS_CALLBACK_CLIENT_RECEIVE = 8,
  1156. /**< data has appeared from the server for the client connection, it
  1157. * can be found at *in and is len bytes long */
  1158. LWS_CALLBACK_CLIENT_RECEIVE_PONG = 9,
  1159. /**< clients receive PONG packets with this callback reason */
  1160. LWS_CALLBACK_CLIENT_WRITEABLE = 10,
  1161. /**< If you call lws_callback_on_writable() on a connection, you will
  1162. * get one of these callbacks coming when the connection socket
  1163. * is able to accept another write packet without blocking.
  1164. * If it already was able to take another packet without blocking,
  1165. * you'll get this callback at the next call to the service loop
  1166. * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
  1167. * and servers get LWS_CALLBACK_SERVER_WRITEABLE. */
  1168. LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED = 26,
  1169. /**< When a ws client
  1170. * connection is being prepared to start a handshake to a server,
  1171. * each supported extension is checked with protocols[0] callback
  1172. * with this reason, giving the user code a chance to suppress the
  1173. * claim to support that extension by returning non-zero. If
  1174. * unhandled, by default 0 will be returned and the extension
  1175. * support included in the header to the server. Notice this
  1176. * callback comes to protocols[0]. */
  1177. LWS_CALLBACK_WS_EXT_DEFAULTS = 39,
  1178. /**< Gives client connections an opportunity to adjust negotiated
  1179. * extension defaults. `user` is the extension name that was
  1180. * negotiated (eg, "permessage-deflate"). `in` points to a
  1181. * buffer and `len` is the buffer size. The user callback can
  1182. * set the buffer to a string describing options the extension
  1183. * should parse. Or just ignore for defaults. */
  1184. LWS_CALLBACK_FILTER_NETWORK_CONNECTION = 17,
  1185. /**< called when a client connects to
  1186. * the server at network level; the connection is accepted but then
  1187. * passed to this callback to decide whether to hang up immediately
  1188. * or not, based on the client IP. in contains the connection
  1189. * socket's descriptor. Since the client connection information is
  1190. * not available yet, wsi still pointing to the main server socket.
  1191. * Return non-zero to terminate the connection before sending or
  1192. * receiving anything. Because this happens immediately after the
  1193. * network connection from the client, there's no websocket protocol
  1194. * selected yet so this callback is issued only to protocol 0. */
  1195. /* ---------------------------------------------------------------------
  1196. * ----- Callbacks related to external poll loop integration -----
  1197. */
  1198. LWS_CALLBACK_GET_THREAD_ID = 31,
  1199. /**< lws can accept callback when writable requests from other
  1200. * threads, if you implement this callback and return an opaque
  1201. * current thread ID integer. */
  1202. /* external poll() management support */
  1203. LWS_CALLBACK_ADD_POLL_FD = 32,
  1204. /**< lws normally deals with its poll() or other event loop
  1205. * internally, but in the case you are integrating with another
  1206. * server you will need to have lws sockets share a
  1207. * polling array with the other server. This and the other
  1208. * POLL_FD related callbacks let you put your specialized
  1209. * poll array interface code in the callback for protocol 0, the
  1210. * first protocol you support, usually the HTTP protocol in the
  1211. * serving case.
  1212. * This callback happens when a socket needs to be
  1213. * added to the polling loop: in points to a struct
  1214. * lws_pollargs; the fd member of the struct is the file
  1215. * descriptor, and events contains the active events
  1216. *
  1217. * If you are using the internal lws polling / event loop
  1218. * you can just ignore these callbacks. */
  1219. LWS_CALLBACK_DEL_POLL_FD = 33,
  1220. /**< This callback happens when a socket descriptor
  1221. * needs to be removed from an external polling array. in is
  1222. * again the struct lws_pollargs containing the fd member
  1223. * to be removed. If you are using the internal polling
  1224. * loop, you can just ignore it. */
  1225. LWS_CALLBACK_CHANGE_MODE_POLL_FD = 34,
  1226. /**< This callback happens when lws wants to modify the events for
  1227. * a connection.
  1228. * in is the struct lws_pollargs with the fd to change.
  1229. * The new event mask is in events member and the old mask is in
  1230. * the prev_events member.
  1231. * If you are using the internal polling loop, you can just ignore
  1232. * it. */
  1233. LWS_CALLBACK_LOCK_POLL = 35,
  1234. /**< These allow the external poll changes driven
  1235. * by lws to participate in an external thread locking
  1236. * scheme around the changes, so the whole thing is threadsafe.
  1237. * These are called around three activities in the library,
  1238. * - inserting a new wsi in the wsi / fd table (len=1)
  1239. * - deleting a wsi from the wsi / fd table (len=1)
  1240. * - changing a wsi's POLLIN/OUT state (len=0)
  1241. * Locking and unlocking external synchronization objects when
  1242. * len == 1 allows external threads to be synchronized against
  1243. * wsi lifecycle changes if it acquires the same lock for the
  1244. * duration of wsi dereference from the other thread context. */
  1245. LWS_CALLBACK_UNLOCK_POLL = 36,
  1246. /**< See LWS_CALLBACK_LOCK_POLL, ignore if using lws internal poll */
  1247. /* ---------------------------------------------------------------------
  1248. * ----- Callbacks related to CGI serving -----
  1249. */
  1250. LWS_CALLBACK_CGI = 40,
  1251. /**< CGI: CGI IO events on stdin / out / err are sent here on
  1252. * protocols[0]. The provided `lws_callback_http_dummy()`
  1253. * handles this and the callback should be directed there if
  1254. * you use CGI. */
  1255. LWS_CALLBACK_CGI_TERMINATED = 41,
  1256. /**< CGI: The related CGI process ended, this is called before
  1257. * the wsi is closed. Used to, eg, terminate chunking.
  1258. * The provided `lws_callback_http_dummy()`
  1259. * handles this and the callback should be directed there if
  1260. * you use CGI. The child PID that terminated is in len. */
  1261. LWS_CALLBACK_CGI_STDIN_DATA = 42,
  1262. /**< CGI: Data is, to be sent to the CGI process stdin, eg from
  1263. * a POST body. The provided `lws_callback_http_dummy()`
  1264. * handles this and the callback should be directed there if
  1265. * you use CGI. */
  1266. LWS_CALLBACK_CGI_STDIN_COMPLETED = 43,
  1267. /**< CGI: no more stdin is coming. The provided
  1268. * `lws_callback_http_dummy()` handles this and the callback
  1269. * should be directed there if you use CGI. */
  1270. LWS_CALLBACK_CGI_PROCESS_ATTACH = 70,
  1271. /**< CGI: Sent when the CGI process is spawned for the wsi. The
  1272. * len parameter is the PID of the child process */
  1273. /* ---------------------------------------------------------------------
  1274. * ----- Callbacks related to Generic Sessions -----
  1275. */
  1276. LWS_CALLBACK_SESSION_INFO = 54,
  1277. /**< This is only generated by user code using generic sessions.
  1278. * It's used to get a `struct lws_session_info` filled in by
  1279. * generic sessions with information about the logged-in user.
  1280. * See the messageboard sample for an example of how to use. */
  1281. LWS_CALLBACK_GS_EVENT = 55,
  1282. /**< Indicates an event happened to the Generic Sessions session.
  1283. * `in` contains a `struct lws_gs_event_args` describing the event. */
  1284. LWS_CALLBACK_HTTP_PMO = 56,
  1285. /**< per-mount options for this connection, called before
  1286. * the normal LWS_CALLBACK_HTTP when the mount has per-mount
  1287. * options.
  1288. */
  1289. /* ---------------------------------------------------------------------
  1290. * ----- Callbacks related to RAW sockets -----
  1291. */
  1292. LWS_CALLBACK_RAW_RX = 59,
  1293. /**< RAW mode connection RX */
  1294. LWS_CALLBACK_RAW_CLOSE = 60,
  1295. /**< RAW mode connection is closing */
  1296. LWS_CALLBACK_RAW_WRITEABLE = 61,
  1297. /**< RAW mode connection may be written */
  1298. LWS_CALLBACK_RAW_ADOPT = 62,
  1299. /**< RAW mode connection was adopted (equivalent to 'wsi created') */
  1300. /* ---------------------------------------------------------------------
  1301. * ----- Callbacks related to RAW file handles -----
  1302. */
  1303. LWS_CALLBACK_RAW_ADOPT_FILE = 63,
  1304. /**< RAW mode file was adopted (equivalent to 'wsi created') */
  1305. LWS_CALLBACK_RAW_RX_FILE = 64,
  1306. /**< This is the indication the RAW mode file has something to read.
  1307. * This doesn't actually do the read of the file and len is always
  1308. * 0... your code should do the read having been informed there is
  1309. * something to read now. */
  1310. LWS_CALLBACK_RAW_WRITEABLE_FILE = 65,
  1311. /**< RAW mode file is writeable */
  1312. LWS_CALLBACK_RAW_CLOSE_FILE = 66,
  1313. /**< RAW mode wsi that adopted a file is closing */
  1314. /* ---------------------------------------------------------------------
  1315. * ----- Callbacks related to generic wsi events -----
  1316. */
  1317. LWS_CALLBACK_TIMER = 73,
  1318. /**< When the time elapsed after a call to
  1319. * lws_set_timer_usecs(wsi, usecs) is up, the wsi will get one of
  1320. * these callbacks. The deadline can be continuously extended into the
  1321. * future by later calls to lws_set_timer_usecs() before the deadline
  1322. * expires, or cancelled by lws_set_timer_usecs(wsi, -1);
  1323. * See the note on lws_set_timer_usecs() about which event loops are
  1324. * supported. */
  1325. LWS_CALLBACK_EVENT_WAIT_CANCELLED = 71,
  1326. /**< This is sent to every protocol of every vhost in response
  1327. * to lws_cancel_service() or lws_cancel_service_pt(). This
  1328. * callback is serialized in the lws event loop normally, even
  1329. * if the lws_cancel_service[_pt]() call was from a different
  1330. * thread. */
  1331. LWS_CALLBACK_CHILD_CLOSING = 69,
  1332. /**< Sent to parent to notify them a child is closing / being
  1333. * destroyed. in is the child wsi.
  1334. */
  1335. LWS_CALLBACK_CHILD_WRITE_VIA_PARENT = 68,
  1336. /**< Child has been marked with parent_carries_io attribute, so
  1337. * lws_write directs the to this callback at the parent,
  1338. * in is a struct lws_write_passthru containing the args
  1339. * the lws_write() was called with.
  1340. */
  1341. /* ---------------------------------------------------------------------
  1342. * ----- Callbacks related to TLS certificate management -----
  1343. */
  1344. LWS_CALLBACK_VHOST_CERT_AGING = 72,
  1345. /**< When a vhost TLS cert has its expiry checked, this callback
  1346. * is broadcast to every protocol of every vhost in case the
  1347. * protocol wants to take some action with this information.
  1348. * \p in is a pointer to a struct lws_acme_cert_aging_args,
  1349. * and \p len is the number of days left before it expires, as
  1350. * a (ssize_t). In the struct lws_acme_cert_aging_args, vh
  1351. * points to the vhost the cert aging information applies to,
  1352. * and element_overrides[] is an optional way to update information
  1353. * from the pvos... NULL in an index means use the information from
  1354. * from the pvo for the cert renewal, non-NULL in the array index
  1355. * means use that pointer instead for the index. */
  1356. LWS_CALLBACK_VHOST_CERT_UPDATE = 74,
  1357. /**< When a vhost TLS cert is being updated, progress is
  1358. * reported to the vhost in question here, including completion
  1359. * and failure. in points to optional JSON, and len represents the
  1360. * connection state using enum lws_cert_update_state */
  1361. /****** add new things just above ---^ ******/
  1362. LWS_CALLBACK_USER = 1000,
  1363. /**< user code can use any including above without fear of clashes */
  1364. };
  1365. /**
  1366. * typedef lws_callback_function() - User server actions
  1367. * \param wsi: Opaque websocket instance pointer
  1368. * \param reason: The reason for the call
  1369. * \param user: Pointer to per-session user data allocated by library
  1370. * \param in: Pointer used for some callback reasons
  1371. * \param len: Length set for some callback reasons
  1372. *
  1373. * This callback is the way the user controls what is served. All the
  1374. * protocol detail is hidden and handled by the library.
  1375. *
  1376. * For each connection / session there is user data allocated that is
  1377. * pointed to by "user". You set the size of this user data area when
  1378. * the library is initialized with lws_create_server.
  1379. */
  1380. typedef int
  1381. lws_callback_function(struct lws *wsi, enum lws_callback_reasons reason,
  1382. void *user, void *in, size_t len);
  1383. #define LWS_CB_REASON_AUX_BF__CGI 1
  1384. #define LWS_CB_REASON_AUX_BF__PROXY 2
  1385. #define LWS_CB_REASON_AUX_BF__CGI_CHUNK_END 4
  1386. #define LWS_CB_REASON_AUX_BF__CGI_HEADERS 8
  1387. ///@}
  1388. struct lws_vhost;
  1389. /*! \defgroup generic hash
  1390. * ## Generic Hash related functions
  1391. *
  1392. * Lws provides generic hash / digest accessors that abstract the ones
  1393. * provided by whatever OpenSSL library you are linking against.
  1394. *
  1395. * It lets you use the same code if you build against mbedtls or OpenSSL
  1396. * for example.
  1397. */
  1398. ///@{
  1399. #if defined(LWS_WITH_TLS)
  1400. #if defined(LWS_WITH_MBEDTLS)
  1401. #include <mbedtls/sha1.h>
  1402. #include <mbedtls/sha256.h>
  1403. #include <mbedtls/sha512.h>
  1404. #endif
  1405. enum lws_genhash_types {
  1406. LWS_GENHASH_TYPE_SHA1,
  1407. LWS_GENHASH_TYPE_SHA256,
  1408. LWS_GENHASH_TYPE_SHA384,
  1409. LWS_GENHASH_TYPE_SHA512,
  1410. };
  1411. enum lws_genhmac_types {
  1412. LWS_GENHMAC_TYPE_SHA256,
  1413. LWS_GENHMAC_TYPE_SHA384,
  1414. LWS_GENHMAC_TYPE_SHA512,
  1415. };
  1416. #define LWS_GENHASH_LARGEST 64
  1417. struct lws_genhash_ctx {
  1418. uint8_t type;
  1419. #if defined(LWS_WITH_MBEDTLS)
  1420. union {
  1421. mbedtls_sha1_context sha1;
  1422. mbedtls_sha256_context sha256;
  1423. mbedtls_sha512_context sha512; /* 384 also uses this */
  1424. const mbedtls_md_info_t *hmac;
  1425. } u;
  1426. #else
  1427. const EVP_MD *evp_type;
  1428. EVP_MD_CTX *mdctx;
  1429. #endif
  1430. };
  1431. struct lws_genhmac_ctx {
  1432. uint8_t type;
  1433. #if defined(LWS_WITH_MBEDTLS)
  1434. const mbedtls_md_info_t *hmac;
  1435. mbedtls_md_context_t ctx;
  1436. #else
  1437. const EVP_MD *evp_type;
  1438. EVP_MD_CTX *ctx;
  1439. #endif
  1440. };
  1441. /** lws_genhash_size() - get hash size in bytes
  1442. *
  1443. * \param type: one of LWS_GENHASH_TYPE_...
  1444. *
  1445. * Returns number of bytes in this type of hash
  1446. */
  1447. LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT
  1448. lws_genhash_size(enum lws_genhash_types type);
  1449. /** lws_genhmac_size() - get hash size in bytes
  1450. *
  1451. * \param type: one of LWS_GENHASH_TYPE_...
  1452. *
  1453. * Returns number of bytes in this type of hmac
  1454. */
  1455. LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT
  1456. lws_genhmac_size(enum lws_genhmac_types type);
  1457. /** lws_genhash_init() - prepare your struct lws_genhash_ctx for use
  1458. *
  1459. * \param ctx: your struct lws_genhash_ctx
  1460. * \param type: one of LWS_GENHASH_TYPE_...
  1461. *
  1462. * Initializes the hash context for the type you requested
  1463. */
  1464. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1465. lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type);
  1466. /** lws_genhash_update() - digest len bytes of the buffer starting at in
  1467. *
  1468. * \param ctx: your struct lws_genhash_ctx
  1469. * \param in: start of the bytes to digest
  1470. * \param len: count of bytes to digest
  1471. *
  1472. * Updates the state of your hash context to reflect digesting len bytes from in
  1473. */
  1474. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1475. lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len);
  1476. /** lws_genhash_destroy() - copy out the result digest and destroy the ctx
  1477. *
  1478. * \param ctx: your struct lws_genhash_ctx
  1479. * \param result: NULL, or where to copy the result hash
  1480. *
  1481. * Finalizes the hash and copies out the digest. Destroys any allocations such
  1482. * that ctx can safely go out of scope after calling this.
  1483. *
  1484. * NULL result is supported so that you can destroy the ctx cleanly on error
  1485. * conditions, where there is no valid result.
  1486. */
  1487. LWS_VISIBLE LWS_EXTERN int
  1488. lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result);
  1489. /** lws_genhmac_init() - prepare your struct lws_genhmac_ctx for use
  1490. *
  1491. * \param ctx: your struct lws_genhmac_ctx
  1492. * \param type: one of LWS_GENHMAC_TYPE_...
  1493. * \param key: pointer to the start of the HMAC key
  1494. * \param key_len: length of the HMAC key
  1495. *
  1496. * Initializes the hash context for the type you requested
  1497. *
  1498. * If the return is nonzero, it failed and there is nothing needing to be
  1499. * destroyed.
  1500. */
  1501. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1502. lws_genhmac_init(struct lws_genhmac_ctx *ctx, enum lws_genhmac_types type,
  1503. const uint8_t *key, size_t key_len);
  1504. /** lws_genhmac_update() - digest len bytes of the buffer starting at in
  1505. *
  1506. * \param ctx: your struct lws_genhmac_ctx
  1507. * \param in: start of the bytes to digest
  1508. * \param len: count of bytes to digest
  1509. *
  1510. * Updates the state of your hash context to reflect digesting len bytes from in
  1511. *
  1512. * If the return is nonzero, it failed and needs destroying.
  1513. */
  1514. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1515. lws_genhmac_update(struct lws_genhmac_ctx *ctx, const void *in, size_t len);
  1516. /** lws_genhmac_destroy() - copy out the result digest and destroy the ctx
  1517. *
  1518. * \param ctx: your struct lws_genhmac_ctx
  1519. * \param result: NULL, or where to copy the result hash
  1520. *
  1521. * Finalizes the hash and copies out the digest. Destroys any allocations such
  1522. * that ctx can safely go out of scope after calling this.
  1523. *
  1524. * NULL result is supported so that you can destroy the ctx cleanly on error
  1525. * conditions, where there is no valid result.
  1526. */
  1527. LWS_VISIBLE LWS_EXTERN int
  1528. lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result);
  1529. ///@}
  1530. /*! \defgroup generic RSA
  1531. * ## Generic RSA related functions
  1532. *
  1533. * Lws provides generic RSA functions that abstract the ones
  1534. * provided by whatever OpenSSL library you are linking against.
  1535. *
  1536. * It lets you use the same code if you build against mbedtls or OpenSSL
  1537. * for example.
  1538. */
  1539. ///@{
  1540. enum enum_jwk_tok {
  1541. JWK_KEY_E,
  1542. JWK_KEY_N,
  1543. JWK_KEY_D,
  1544. JWK_KEY_P,
  1545. JWK_KEY_Q,
  1546. JWK_KEY_DP,
  1547. JWK_KEY_DQ,
  1548. JWK_KEY_QI,
  1549. JWK_KTY, /* also serves as count of real elements */
  1550. JWK_KEY,
  1551. };
  1552. #define LWS_COUNT_RSA_ELEMENTS JWK_KTY
  1553. struct lws_genrsa_ctx {
  1554. #if defined(LWS_WITH_MBEDTLS)
  1555. mbedtls_rsa_context *ctx;
  1556. #else
  1557. BIGNUM *bn[LWS_COUNT_RSA_ELEMENTS];
  1558. RSA *rsa;
  1559. #endif
  1560. };
  1561. struct lws_genrsa_element {
  1562. uint8_t *buf;
  1563. uint16_t len;
  1564. };
  1565. struct lws_genrsa_elements {
  1566. struct lws_genrsa_element e[LWS_COUNT_RSA_ELEMENTS];
  1567. };
  1568. /** lws_jwk_destroy_genrsa_elements() - Free allocations in genrsa_elements
  1569. *
  1570. * \param el: your struct lws_genrsa_elements
  1571. *
  1572. * This is a helper for user code making use of struct lws_genrsa_elements
  1573. * where the elements are allocated on the heap, it frees any non-NULL
  1574. * buf element and sets the buf to NULL.
  1575. *
  1576. * NB: lws_genrsa_public_... apis do not need this as they take care of the key
  1577. * creation and destruction themselves.
  1578. */
  1579. LWS_VISIBLE LWS_EXTERN void
  1580. lws_jwk_destroy_genrsa_elements(struct lws_genrsa_elements *el);
  1581. /** lws_genrsa_public_decrypt_create() - Create RSA public decrypt context
  1582. *
  1583. * \param ctx: your struct lws_genrsa_ctx
  1584. * \param el: struct prepared with key element data
  1585. *
  1586. * Creates an RSA context with a public key associated with it, formed from
  1587. * the key elements in \p el.
  1588. *
  1589. * Returns 0 for OK or nonzero for error.
  1590. *
  1591. * This and related APIs operate identically with OpenSSL or mbedTLS backends.
  1592. */
  1593. LWS_VISIBLE LWS_EXTERN int
  1594. lws_genrsa_create(struct lws_genrsa_ctx *ctx, struct lws_genrsa_elements *el);
  1595. /** lws_genrsa_new_keypair() - Create new RSA keypair
  1596. *
  1597. * \param context: your struct lws_context (may be used for RNG)
  1598. * \param ctx: your struct lws_genrsa_ctx
  1599. * \param el: struct to get the new key element data allocated into it
  1600. * \param bits: key size, eg, 4096
  1601. *
  1602. * Creates a new RSA context and generates a new keypair into it, with \p bits
  1603. * bits.
  1604. *
  1605. * Returns 0 for OK or nonzero for error.
  1606. *
  1607. * This and related APIs operate identically with OpenSSL or mbedTLS backends.
  1608. */
  1609. LWS_VISIBLE LWS_EXTERN int
  1610. lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
  1611. struct lws_genrsa_elements *el, int bits);
  1612. /** lws_genrsa_public_decrypt() - Perform RSA public decryption
  1613. *
  1614. * \param ctx: your struct lws_genrsa_ctx
  1615. * \param in: encrypted input
  1616. * \param in_len: length of encrypted input
  1617. * \param out: decrypted output
  1618. * \param out_max: size of output buffer
  1619. *
  1620. * Performs the decryption.
  1621. *
  1622. * Returns <0 for error, or length of decrypted data.
  1623. *
  1624. * This and related APIs operate identically with OpenSSL or mbedTLS backends.
  1625. */
  1626. LWS_VISIBLE LWS_EXTERN int
  1627. lws_genrsa_public_decrypt(struct lws_genrsa_ctx *ctx, const uint8_t *in,
  1628. size_t in_len, uint8_t *out, size_t out_max);
  1629. /** lws_genrsa_public_verify() - Perform RSA public verification
  1630. *
  1631. * \param ctx: your struct lws_genrsa_ctx
  1632. * \param in: unencrypted payload (usually a recomputed hash)
  1633. * \param hash_type: one of LWS_GENHASH_TYPE_
  1634. * \param sig: pointer to the signature we received with the payload
  1635. * \param sig_len: length of the signature we are checking in bytes
  1636. *
  1637. * Returns <0 for error, or 0 if signature matches the payload + key.
  1638. *
  1639. * This and related APIs operate identically with OpenSSL or mbedTLS backends.
  1640. */
  1641. LWS_VISIBLE LWS_EXTERN int
  1642. lws_genrsa_public_verify(struct lws_genrsa_ctx *ctx, const uint8_t *in,
  1643. enum lws_genhash_types hash_type,
  1644. const uint8_t *sig, size_t sig_len);
  1645. /** lws_genrsa_public_sign() - Create RSA signature
  1646. *
  1647. * \param ctx: your struct lws_genrsa_ctx
  1648. * \param in: precomputed hash
  1649. * \param hash_type: one of LWS_GENHASH_TYPE_
  1650. * \param sig: pointer to buffer to take signature
  1651. * \param sig_len: length of the buffer (must be >= length of key N)
  1652. *
  1653. * Returns <0 for error, or 0 for success.
  1654. *
  1655. * This and related APIs operate identically with OpenSSL or mbedTLS backends.
  1656. */
  1657. LWS_VISIBLE LWS_EXTERN int
  1658. lws_genrsa_public_sign(struct lws_genrsa_ctx *ctx, const uint8_t *in,
  1659. enum lws_genhash_types hash_type, uint8_t *sig,
  1660. size_t sig_len);
  1661. /** lws_genrsa_public_decrypt_destroy() - Destroy RSA public decrypt context
  1662. *
  1663. * \param ctx: your struct lws_genrsa_ctx
  1664. *
  1665. * Destroys any allocations related to \p ctx.
  1666. *
  1667. * This and related APIs operate identically with OpenSSL or mbedTLS backends.
  1668. */
  1669. LWS_VISIBLE LWS_EXTERN void
  1670. lws_genrsa_destroy(struct lws_genrsa_ctx *ctx);
  1671. /** lws_genrsa_render_pkey_asn1() - Exports public or private key to ASN1/DER
  1672. *
  1673. * \param ctx: your struct lws_genrsa_ctx
  1674. * \param _private: 0 = public part only, 1 = all parts of the key
  1675. * \param pkey_asn1: pointer to buffer to take the ASN1
  1676. * \param pkey_asn1_len: max size of the pkey_asn1_len
  1677. *
  1678. * Returns length of pkey_asn1 written, or -1 for error.
  1679. */
  1680. LWS_VISIBLE LWS_EXTERN int
  1681. lws_genrsa_render_pkey_asn1(struct lws_genrsa_ctx *ctx, int _private,
  1682. uint8_t *pkey_asn1, size_t pkey_asn1_len);
  1683. ///@}
  1684. /*! \defgroup jwk JSON Web Keys
  1685. * ## JSON Web Keys API
  1686. *
  1687. * Lws provides an API to parse JSON Web Keys into a struct lws_genrsa_elements.
  1688. *
  1689. * "oct" and "RSA" type keys are supported. For "oct" keys, they are held in
  1690. * the "e" member of the struct lws_genrsa_elements.
  1691. *
  1692. * Keys elements are allocated on the heap. You must destroy the allocations
  1693. * in the struct lws_genrsa_elements by calling
  1694. * lws_jwk_destroy_genrsa_elements() when you are finished with it.
  1695. */
  1696. ///@{
  1697. struct lws_jwk {
  1698. char keytype[5]; /**< "oct" or "RSA" */
  1699. struct lws_genrsa_elements el; /**< OCTet key is in el.e */
  1700. };
  1701. /** lws_jwk_import() - Create a JSON Web key from the textual representation
  1702. *
  1703. * \param s: the JWK object to create
  1704. * \param in: a single JWK JSON stanza in utf-8
  1705. * \param len: the length of the JWK JSON stanza in bytes
  1706. *
  1707. * Creates an lws_jwk struct filled with data from the JSON representation.
  1708. * "oct" and "rsa" key types are supported.
  1709. *
  1710. * For "oct" type keys, it is loaded into el.e.
  1711. */
  1712. LWS_VISIBLE LWS_EXTERN int
  1713. lws_jwk_import(struct lws_jwk *s, const char *in, size_t len);
  1714. /** lws_jwk_destroy() - Destroy a JSON Web key
  1715. *
  1716. * \param s: the JWK object to destroy
  1717. *
  1718. * All allocations in the lws_jwk are destroyed
  1719. */
  1720. LWS_VISIBLE LWS_EXTERN void
  1721. lws_jwk_destroy(struct lws_jwk *s);
  1722. /** lws_jwk_export() - Export a JSON Web key to a textual representation
  1723. *
  1724. * \param s: the JWK object to export
  1725. * \param _private: 0 = just export public parts, 1 = export everything
  1726. * \param p: the buffer to write the exported JWK to
  1727. * \param len: the length of the buffer \p p in bytes
  1728. *
  1729. * Returns length of the used part of the buffer if OK, or -1 for error.
  1730. *
  1731. * Serializes the content of the JWK into a char buffer.
  1732. */
  1733. LWS_VISIBLE LWS_EXTERN int
  1734. lws_jwk_export(struct lws_jwk *s, int _private, char *p, size_t len);
  1735. /** lws_jwk_load() - Import a JSON Web key from a file
  1736. *
  1737. * \param s: the JWK object to load into
  1738. * \param filename: filename to load from
  1739. *
  1740. * Returns 0 for OK or -1 for failure
  1741. */
  1742. LWS_VISIBLE int
  1743. lws_jwk_load(struct lws_jwk *s, const char *filename);
  1744. /** lws_jwk_save() - Export a JSON Web key to a file
  1745. *
  1746. * \param s: the JWK object to save from
  1747. * \param filename: filename to save to
  1748. *
  1749. * Returns 0 for OK or -1 for failure
  1750. */
  1751. LWS_VISIBLE int
  1752. lws_jwk_save(struct lws_jwk *s, const char *filename);
  1753. /** lws_jwk_rfc7638_fingerprint() - jwk to RFC7638 compliant fingerprint
  1754. *
  1755. * \param s: the JWK object to fingerprint
  1756. * \param digest32: buffer to take 32-byte digest
  1757. *
  1758. * Returns 0 for OK or -1 for failure
  1759. */
  1760. LWS_VISIBLE int
  1761. lws_jwk_rfc7638_fingerprint(struct lws_jwk *s, char *digest32);
  1762. ///@}
  1763. /*! \defgroup jws JSON Web Signature
  1764. * ## JSON Web Signature API
  1765. *
  1766. * Lws provides an API to check and create RFC7515 JSON Web Signatures
  1767. *
  1768. * SHA256/384/512 HMAC, and RSA 256/384/512 are supported.
  1769. *
  1770. * The API uses your TLS library crypto, but works exactly the same no matter
  1771. * what you TLS backend is.
  1772. */
  1773. ///@{
  1774. LWS_VISIBLE LWS_EXTERN int
  1775. lws_jws_confirm_sig(const char *in, size_t len, struct lws_jwk *jwk);
  1776. /**
  1777. * lws_jws_sign_from_b64() - add b64 sig to b64 hdr + payload
  1778. *
  1779. * \param b64_hdr: protected header encoded in b64, may be NULL
  1780. * \param hdr_len: bytes in b64 coding of protected header
  1781. * \param b64_pay: payload encoded in b64
  1782. * \param pay_len: bytes in b64 coding of payload
  1783. * \param b64_sig: buffer to write the b64 encoded signature into
  1784. * \param sig_len: max bytes we can write at b64_sig
  1785. * \param hash_type: one of LWS_GENHASH_TYPE_SHA[256|384|512]
  1786. * \param jwk: the struct lws_jwk containing the signing key
  1787. *
  1788. * This adds a b64-coded JWS signature of the b64-encoded protected header
  1789. * and b64-encoded payload, at \p b64_sig. The signature will be as large
  1790. * as the N element of the RSA key when the RSA key is used, eg, 512 bytes for
  1791. * a 4096-bit key, and then b64-encoding on top.
  1792. *
  1793. * In some special cases, there is only payload to sign and no header, in that
  1794. * case \p b64_hdr may be NULL, and only the payload will be hashed before
  1795. * signing.
  1796. *
  1797. * Returns the length of the encoded signature written to \p b64_sig, or -1.
  1798. */
  1799. LWS_VISIBLE LWS_EXTERN int
  1800. lws_jws_sign_from_b64(const char *b64_hdr, size_t hdr_len, const char *b64_pay,
  1801. size_t pay_len, char *b64_sig, size_t sig_len,
  1802. enum lws_genhash_types hash_type, struct lws_jwk *jwk);
  1803. /**
  1804. * lws_jws_create_packet() - add b64 sig to b64 hdr + payload
  1805. *
  1806. * \param jwk: the struct lws_jwk containing the signing key
  1807. * \param payload: unencoded payload JSON
  1808. * \param len: length of unencoded payload JSON
  1809. * \param nonce: Nonse string to include in protected header
  1810. * \param out: buffer to take signed packet
  1811. * \param out_len: size of \p out buffer
  1812. *
  1813. * This creates a "flattened" JWS packet from the jwk and the plaintext
  1814. * payload, and signs it. The packet is written into \p out.
  1815. *
  1816. * This does the whole packet assembly and signing, calling through to
  1817. * lws_jws_sign_from_b64() as part of the process.
  1818. *
  1819. * Returns the length written to \p out, or -1.
  1820. */
  1821. LWS_VISIBLE LWS_EXTERN int
  1822. lws_jws_create_packet(struct lws_jwk *jwk, const char *payload, size_t len,
  1823. const char *nonce, char *out, size_t out_len);
  1824. /**
  1825. * lws_jws_base64_enc() - encode input data into b64url data
  1826. *
  1827. * \param in: the incoming plaintext
  1828. * \param in_len: the length of the incoming plaintext in bytes
  1829. * \param out: the buffer to store the b64url encoded data to
  1830. * \param out_max: the length of \p out in bytes
  1831. *
  1832. * Returns either -1 if problems, or the number of bytes written to \p out.
  1833. */
  1834. LWS_VISIBLE LWS_EXTERN int
  1835. lws_jws_base64_enc(const char *in, size_t in_len, char *out, size_t out_max);
  1836. ///@}
  1837. #endif
  1838. /*! \defgroup extensions Extension related functions
  1839. * ##Extension releated functions
  1840. *
  1841. * Ws defines optional extensions, lws provides the ability to implement these
  1842. * in user code if so desired.
  1843. *
  1844. * We provide one extensions permessage-deflate.
  1845. */
  1846. ///@{
  1847. /*
  1848. * NOTE: These public enums are part of the abi. If you want to add one,
  1849. * add it at where specified so existing users are unaffected.
  1850. */
  1851. enum lws_extension_callback_reasons {
  1852. LWS_EXT_CB_CONSTRUCT = 4,
  1853. LWS_EXT_CB_CLIENT_CONSTRUCT = 5,
  1854. LWS_EXT_CB_DESTROY = 8,
  1855. LWS_EXT_CB_PACKET_TX_PRESEND = 12,
  1856. LWS_EXT_CB_PAYLOAD_TX = 21,
  1857. LWS_EXT_CB_PAYLOAD_RX = 22,
  1858. LWS_EXT_CB_OPTION_DEFAULT = 23,
  1859. LWS_EXT_CB_OPTION_SET = 24,
  1860. LWS_EXT_CB_OPTION_CONFIRM = 25,
  1861. LWS_EXT_CB_NAMED_OPTION_SET = 26,
  1862. /****** add new things just above ---^ ******/
  1863. };
  1864. /** enum lws_ext_options_types */
  1865. enum lws_ext_options_types {
  1866. EXTARG_NONE, /**< does not take an argument */
  1867. EXTARG_DEC, /**< requires a decimal argument */
  1868. EXTARG_OPT_DEC /**< may have an optional decimal argument */
  1869. /* Add new things just above here ---^
  1870. * This is part of the ABI, don't needlessly break compatibility */
  1871. };
  1872. /** struct lws_ext_options - Option arguments to the extension. These are
  1873. * used in the negotiation at ws upgrade time.
  1874. * The helper function lws_ext_parse_options()
  1875. * uses these to generate callbacks */
  1876. struct lws_ext_options {
  1877. const char *name; /**< Option name, eg, "server_no_context_takeover" */
  1878. enum lws_ext_options_types type; /**< What kind of args the option can take */
  1879. /* Add new things just above here ---^
  1880. * This is part of the ABI, don't needlessly break compatibility */
  1881. };
  1882. /** struct lws_ext_option_arg */
  1883. struct lws_ext_option_arg {
  1884. const char *option_name; /**< may be NULL, option_index used then */
  1885. int option_index; /**< argument ordinal to use if option_name missing */
  1886. const char *start; /**< value */
  1887. int len; /**< length of value */
  1888. };
  1889. /**
  1890. * typedef lws_extension_callback_function() - Hooks to allow extensions to operate
  1891. * \param context: Websockets context
  1892. * \param ext: This extension
  1893. * \param wsi: Opaque websocket instance pointer
  1894. * \param reason: The reason for the call
  1895. * \param user: Pointer to ptr to per-session user data allocated by library
  1896. * \param in: Pointer used for some callback reasons
  1897. * \param len: Length set for some callback reasons
  1898. *
  1899. * Each extension that is active on a particular connection receives
  1900. * callbacks during the connection lifetime to allow the extension to
  1901. * operate on websocket data and manage itself.
  1902. *
  1903. * Libwebsockets takes care of allocating and freeing "user" memory for
  1904. * each active extension on each connection. That is what is pointed to
  1905. * by the user parameter.
  1906. *
  1907. * LWS_EXT_CB_CONSTRUCT: called when the server has decided to
  1908. * select this extension from the list provided by the client,
  1909. * just before the server will send back the handshake accepting
  1910. * the connection with this extension active. This gives the
  1911. * extension a chance to initialize its connection context found
  1912. * in user.
  1913. *
  1914. * LWS_EXT_CB_CLIENT_CONSTRUCT: same as LWS_EXT_CB_CONSTRUCT
  1915. * but called when client is instantiating this extension. Some
  1916. * extensions will work the same on client and server side and then
  1917. * you can just merge handlers for both CONSTRUCTS.
  1918. *
  1919. * LWS_EXT_CB_DESTROY: called when the connection the extension was
  1920. * being used on is about to be closed and deallocated. It's the
  1921. * last chance for the extension to deallocate anything it has
  1922. * allocated in the user data (pointed to by user) before the
  1923. * user data is deleted. This same callback is used whether you
  1924. * are in client or server instantiation context.
  1925. *
  1926. * LWS_EXT_CB_PACKET_TX_PRESEND: this works the same way as
  1927. * LWS_EXT_CB_PACKET_RX_PREPARSE above, except it gives the
  1928. * extension a chance to change websocket data just before it will
  1929. * be sent out. Using the same lws_token pointer scheme in in,
  1930. * the extension can change the buffer and the length to be
  1931. * transmitted how it likes. Again if it wants to grow the
  1932. * buffer safely, it should copy the data into its own buffer and
  1933. * set the lws_tokens token pointer to it.
  1934. *
  1935. * LWS_EXT_CB_ARGS_VALIDATE:
  1936. */
  1937. typedef int
  1938. lws_extension_callback_function(struct lws_context *context,
  1939. const struct lws_extension *ext, struct lws *wsi,
  1940. enum lws_extension_callback_reasons reason,
  1941. void *user, void *in, size_t len);
  1942. /** struct lws_extension - An extension we support */
  1943. struct lws_extension {
  1944. const char *name; /**< Formal extension name, eg, "permessage-deflate" */
  1945. lws_extension_callback_function *callback; /**< Service callback */
  1946. const char *client_offer; /**< String containing exts and options client offers */
  1947. /* Add new things just above here ---^
  1948. * This is part of the ABI, don't needlessly break compatibility */
  1949. };
  1950. /**
  1951. * lws_set_extension_option(): set extension option if possible
  1952. *
  1953. * \param wsi: websocket connection
  1954. * \param ext_name: name of ext, like "permessage-deflate"
  1955. * \param opt_name: name of option, like "rx_buf_size"
  1956. * \param opt_val: value to set option to
  1957. */
  1958. LWS_VISIBLE LWS_EXTERN int
  1959. lws_set_extension_option(struct lws *wsi, const char *ext_name,
  1960. const char *opt_name, const char *opt_val);
  1961. /**
  1962. * lws_ext_parse_options() - deal with parsing negotiated extension options
  1963. *
  1964. * \param ext: related extension struct
  1965. * \param wsi: websocket connection
  1966. * \param ext_user: per-connection extension private data
  1967. * \param opts: list of supported options
  1968. * \param o: option string to parse
  1969. * \param len: length
  1970. */
  1971. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1972. lws_ext_parse_options(const struct lws_extension *ext, struct lws *wsi,
  1973. void *ext_user, const struct lws_ext_options *opts,
  1974. const char *o, int len);
  1975. /** lws_extension_callback_pm_deflate() - extension for RFC7692
  1976. *
  1977. * \param context: lws context
  1978. * \param ext: related lws_extension struct
  1979. * \param wsi: websocket connection
  1980. * \param reason: incoming callback reason
  1981. * \param user: per-connection extension private data
  1982. * \param in: pointer parameter
  1983. * \param len: length parameter
  1984. *
  1985. * Built-in callback implementing RFC7692 permessage-deflate
  1986. */
  1987. LWS_EXTERN
  1988. int lws_extension_callback_pm_deflate(
  1989. struct lws_context *context, const struct lws_extension *ext,
  1990. struct lws *wsi, enum lws_extension_callback_reasons reason,
  1991. void *user, void *in, size_t len);
  1992. /*
  1993. * The internal exts are part of the public abi
  1994. * If we add more extensions, publish the callback here ------v
  1995. */
  1996. ///@}
  1997. /*! \defgroup Protocols-and-Plugins Protocols and Plugins
  1998. * \ingroup lwsapi
  1999. *
  2000. * ##Protocol and protocol plugin -related apis
  2001. *
  2002. * Protocols bind ws protocol names to a custom callback specific to that
  2003. * protocol implementaion.
  2004. *
  2005. * A list of protocols can be passed in at context creation time, but it is
  2006. * also legal to leave that NULL and add the protocols and their callback code
  2007. * using plugins.
  2008. *
  2009. * Plugins are much preferable compared to cut and pasting code into an
  2010. * application each time, since they can be used standalone.
  2011. */
  2012. ///@{
  2013. /** struct lws_protocols - List of protocols and handlers client or server
  2014. * supports. */
  2015. struct lws_protocols {
  2016. const char *name;
  2017. /**< Protocol name that must match the one given in the client
  2018. * Javascript new WebSocket(url, 'protocol') name. */
  2019. lws_callback_function *callback;
  2020. /**< The service callback used for this protocol. It allows the
  2021. * service action for an entire protocol to be encapsulated in
  2022. * the protocol-specific callback */
  2023. size_t per_session_data_size;
  2024. /**< Each new connection using this protocol gets
  2025. * this much memory allocated on connection establishment and
  2026. * freed on connection takedown. A pointer to this per-connection
  2027. * allocation is passed into the callback in the 'user' parameter */
  2028. size_t rx_buffer_size;
  2029. /**< lws allocates this much space for rx data and informs callback
  2030. * when something came. Due to rx flow control, the callback may not
  2031. * be able to consume it all without having to return to the event
  2032. * loop. That is supported in lws.
  2033. *
  2034. * If .tx_packet_size is 0, this also controls how much may be sent at
  2035. * once for backwards compatibility.
  2036. */
  2037. unsigned int id;
  2038. /**< ignored by lws, but useful to contain user information bound
  2039. * to the selected protocol. For example if this protocol was
  2040. * called "myprotocol-v2", you might set id to 2, and the user
  2041. * code that acts differently according to the version can do so by
  2042. * switch (wsi->protocol->id), user code might use some bits as
  2043. * capability flags based on selected protocol version, etc. */
  2044. void *user; /**< ignored by lws, but user code can pass a pointer
  2045. here it can later access from the protocol callback */
  2046. size_t tx_packet_size;
  2047. /**< 0 indicates restrict send() size to .rx_buffer_size for backwards-
  2048. * compatibility.
  2049. * If greater than zero, a single send() is restricted to this amount
  2050. * and any remainder is buffered by lws and sent afterwards also in
  2051. * these size chunks. Since that is expensive, it's preferable
  2052. * to restrict one fragment you are trying to send to match this
  2053. * size.
  2054. */
  2055. /* Add new things just above here ---^
  2056. * This is part of the ABI, don't needlessly break compatibility */
  2057. };
  2058. /**
  2059. * lws_vhost_name_to_protocol() - get vhost's protocol object from its name
  2060. *
  2061. * \param vh: vhost to search
  2062. * \param name: protocol name
  2063. *
  2064. * Returns NULL or a pointer to the vhost's protocol of the requested name
  2065. */
  2066. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  2067. lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name);
  2068. /**
  2069. * lws_get_protocol() - Returns a protocol pointer from a websocket
  2070. * connection.
  2071. * \param wsi: pointer to struct websocket you want to know the protocol of
  2072. *
  2073. *
  2074. * Some apis can act on all live connections of a given protocol,
  2075. * this is how you can get a pointer to the active protocol if needed.
  2076. */
  2077. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  2078. lws_get_protocol(struct lws *wsi);
  2079. /** lws_protocol_get() - deprecated: use lws_get_protocol */
  2080. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  2081. lws_protocol_get(struct lws *wsi) LWS_WARN_DEPRECATED;
  2082. /**
  2083. * lws_protocol_vh_priv_zalloc() - Allocate and zero down a protocol's per-vhost
  2084. * storage
  2085. * \param vhost: vhost the instance is related to
  2086. * \param prot: protocol the instance is related to
  2087. * \param size: bytes to allocate
  2088. *
  2089. * Protocols often find it useful to allocate a per-vhost struct, this is a
  2090. * helper to be called in the per-vhost init LWS_CALLBACK_PROTOCOL_INIT
  2091. */
  2092. LWS_VISIBLE LWS_EXTERN void *
  2093. lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
  2094. int size);
  2095. /**
  2096. * lws_protocol_vh_priv_get() - retreive a protocol's per-vhost storage
  2097. *
  2098. * \param vhost: vhost the instance is related to
  2099. * \param prot: protocol the instance is related to
  2100. *
  2101. * Recover a pointer to the allocated per-vhost storage for the protocol created
  2102. * by lws_protocol_vh_priv_zalloc() earlier
  2103. */
  2104. LWS_VISIBLE LWS_EXTERN void *
  2105. lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot);
  2106. /**
  2107. * lws_adjust_protocol_psds - change a vhost protocol's per session data size
  2108. *
  2109. * \param wsi: a connection with the protocol to change
  2110. * \param new_size: the new size of the per session data size for the protocol
  2111. *
  2112. * Returns user_space for the wsi, after allocating
  2113. *
  2114. * This should not be used except to initalize a vhost protocol's per session
  2115. * data size one time, before any connections are accepted.
  2116. *
  2117. * Sometimes the protocol wraps another protocol and needs to discover and set
  2118. * its per session data size at runtime.
  2119. */
  2120. LWS_VISIBLE LWS_EXTERN void *
  2121. lws_adjust_protocol_psds(struct lws *wsi, size_t new_size);
  2122. /**
  2123. * lws_finalize_startup() - drop initial process privileges
  2124. *
  2125. * \param context: lws context
  2126. *
  2127. * This is called after the end of the vhost protocol initializations, but
  2128. * you may choose to call it earlier
  2129. */
  2130. LWS_VISIBLE LWS_EXTERN int
  2131. lws_finalize_startup(struct lws_context *context);
  2132. /**
  2133. * lws_pvo_search() - helper to find a named pvo in a linked-list
  2134. *
  2135. * \param pvo: the first pvo in the linked-list
  2136. * \param name: the name of the pvo to return if found
  2137. *
  2138. * Returns NULL, or a pointer to the name pvo in the linked-list
  2139. */
  2140. LWS_VISIBLE LWS_EXTERN const struct lws_protocol_vhost_options *
  2141. lws_pvo_search(const struct lws_protocol_vhost_options *pvo, const char *name);
  2142. LWS_VISIBLE LWS_EXTERN int
  2143. lws_protocol_init(struct lws_context *context);
  2144. #ifdef LWS_WITH_PLUGINS
  2145. /* PLUGINS implies LIBUV */
  2146. #define LWS_PLUGIN_API_MAGIC 180
  2147. /** struct lws_plugin_capability - how a plugin introduces itself to lws */
  2148. struct lws_plugin_capability {
  2149. unsigned int api_magic; /**< caller fills this in, plugin fills rest */
  2150. const struct lws_protocols *protocols; /**< array of supported protocols provided by plugin */
  2151. int count_protocols; /**< how many protocols */
  2152. const struct lws_extension *extensions; /**< array of extensions provided by plugin */
  2153. int count_extensions; /**< how many extensions */
  2154. };
  2155. typedef int (*lws_plugin_init_func)(struct lws_context *,
  2156. struct lws_plugin_capability *);
  2157. typedef int (*lws_plugin_destroy_func)(struct lws_context *);
  2158. /** struct lws_plugin */
  2159. struct lws_plugin {
  2160. struct lws_plugin *list; /**< linked list */
  2161. #if (UV_VERSION_MAJOR > 0)
  2162. uv_lib_t lib; /**< shared library pointer */
  2163. #else
  2164. void *l; /**< so we can compile on ancient libuv */
  2165. #endif
  2166. char name[64]; /**< name of the plugin */
  2167. struct lws_plugin_capability caps; /**< plugin capabilities */
  2168. };
  2169. #endif
  2170. ///@}
  2171. /*! \defgroup generic-sessions plugin: generic-sessions
  2172. * \ingroup Protocols-and-Plugins
  2173. *
  2174. * ##Plugin Generic-sessions related
  2175. *
  2176. * generic-sessions plugin provides a reusable, generic session and login /
  2177. * register / forgot password framework including email verification.
  2178. */
  2179. ///@{
  2180. #define LWSGS_EMAIL_CONTENT_SIZE 16384
  2181. /**< Maximum size of email we might send */
  2182. /* SHA-1 binary and hexified versions */
  2183. /** typedef struct lwsgw_hash_bin */
  2184. typedef struct { unsigned char bin[20]; /**< binary representation of hash */} lwsgw_hash_bin;
  2185. /** typedef struct lwsgw_hash */
  2186. typedef struct { char id[41]; /**< ascii hex representation of hash */ } lwsgw_hash;
  2187. /** enum lwsgs_auth_bits */
  2188. enum lwsgs_auth_bits {
  2189. LWSGS_AUTH_LOGGED_IN = 1, /**< user is logged in as somebody */
  2190. LWSGS_AUTH_ADMIN = 2, /**< logged in as the admin user */
  2191. LWSGS_AUTH_VERIFIED = 4, /**< user has verified his email */
  2192. LWSGS_AUTH_FORGOT_FLOW = 8, /**< he just completed "forgot password" flow */
  2193. };
  2194. /** struct lws_session_info - information about user session status */
  2195. struct lws_session_info {
  2196. char username[32]; /**< username logged in as, or empty string */
  2197. char email[100]; /**< email address associated with login, or empty string */
  2198. char ip[72]; /**< ip address session was started from */
  2199. unsigned int mask; /**< access rights mask associated with session
  2200. * see enum lwsgs_auth_bits */
  2201. char session[42]; /**< session id string, usable as opaque uid when not logged in */
  2202. };
  2203. /** enum lws_gs_event */
  2204. enum lws_gs_event {
  2205. LWSGSE_CREATED, /**< a new user was created */
  2206. LWSGSE_DELETED /**< an existing user was deleted */
  2207. };
  2208. /** struct lws_gs_event_args */
  2209. struct lws_gs_event_args {
  2210. enum lws_gs_event event; /**< which event happened */
  2211. const char *username; /**< which username the event happened to */
  2212. const char *email; /**< the email address of that user */
  2213. };
  2214. ///@}
  2215. /*! \defgroup context-and-vhost context and vhost related functions
  2216. * ##Context and Vhost releated functions
  2217. * \ingroup lwsapi
  2218. *
  2219. *
  2220. * LWS requires that there is one context, in which you may define multiple
  2221. * vhosts. Each vhost is a virtual host, with either its own listen port
  2222. * or sharing an existing one. Each vhost has its own SSL context that can
  2223. * be set up individually or left disabled.
  2224. *
  2225. * If you don't care about multiple "site" support, you can ignore it and
  2226. * lws will create a single default vhost at context creation time.
  2227. */
  2228. ///@{
  2229. /*
  2230. * NOTE: These public enums are part of the abi. If you want to add one,
  2231. * add it at where specified so existing users are unaffected.
  2232. */
  2233. /** enum lws_context_options - context and vhost options */
  2234. enum lws_context_options {
  2235. LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = (1 << 1) |
  2236. (1 << 12),
  2237. /**< (VH) Don't allow the connection unless the client has a
  2238. * client cert that we recognize; provides
  2239. * LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
  2240. LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = (1 << 2),
  2241. /**< (CTX) Don't try to get the server's hostname */
  2242. LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = (1 << 3) |
  2243. (1 << 12),
  2244. /**< (VH) Allow non-SSL (plaintext) connections on the same
  2245. * port as SSL is listening... undermines the security of SSL;
  2246. * provides LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
  2247. LWS_SERVER_OPTION_LIBEV = (1 << 4),
  2248. /**< (CTX) Use libev event loop */
  2249. LWS_SERVER_OPTION_DISABLE_IPV6 = (1 << 5),
  2250. /**< (VH) Disable IPV6 support */
  2251. LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = (1 << 6),
  2252. /**< (VH) Don't load OS CA certs, you will need to load your
  2253. * own CA cert(s) */
  2254. LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = (1 << 7),
  2255. /**< (VH) Accept connections with no valid Cert (eg, selfsigned) */
  2256. LWS_SERVER_OPTION_VALIDATE_UTF8 = (1 << 8),
  2257. /**< (VH) Check UT-8 correctness */
  2258. LWS_SERVER_OPTION_SSL_ECDH = (1 << 9) |
  2259. (1 << 12),
  2260. /**< (VH) initialize ECDH ciphers */
  2261. LWS_SERVER_OPTION_LIBUV = (1 << 10),
  2262. /**< (CTX) Use libuv event loop */
  2263. LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS = (1 << 11) |
  2264. (1 << 12),
  2265. /**< (VH) Use http redirect to force http to https
  2266. * (deprecated: use mount redirection) */
  2267. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT = (1 << 12),
  2268. /**< (CTX) Initialize the SSL library at all */
  2269. LWS_SERVER_OPTION_EXPLICIT_VHOSTS = (1 << 13),
  2270. /**< (CTX) Only create the context when calling context
  2271. * create api, implies user code will create its own vhosts */
  2272. LWS_SERVER_OPTION_UNIX_SOCK = (1 << 14),
  2273. /**< (VH) Use Unix socket */
  2274. LWS_SERVER_OPTION_STS = (1 << 15),
  2275. /**< (VH) Send Strict Transport Security header, making
  2276. * clients subsequently go to https even if user asked for http */
  2277. LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY = (1 << 16),
  2278. /**< (VH) Enable LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE to take effect */
  2279. LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE = (1 << 17),
  2280. /**< (VH) if set, only ipv6 allowed on the vhost */
  2281. LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN = (1 << 18),
  2282. /**< (CTX) Libuv only: Do not spin on SIGSEGV / SIGFPE. A segfault
  2283. * normally makes the lib spin so you can attach a debugger to it
  2284. * even if it happened without a debugger in place. You can disable
  2285. * that by giving this option.
  2286. */
  2287. LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN = (1 << 19),
  2288. /**< For backwards-compatibility reasons, by default
  2289. * lws prepends "http://" to the origin you give in the client
  2290. * connection info struct. If you give this flag when you create
  2291. * the context, only the string you give in the client connect
  2292. * info for .origin (if any) will be used directly.
  2293. */
  2294. LWS_SERVER_OPTION_FALLBACK_TO_RAW = (1 << 20),
  2295. /**< (VH) if invalid http is coming in the first line, */
  2296. LWS_SERVER_OPTION_LIBEVENT = (1 << 21),
  2297. /**< (CTX) Use libevent event loop */
  2298. LWS_SERVER_OPTION_ONLY_RAW = (1 << 22),
  2299. /**< (VH) All connections to this vhost / port are RAW as soon as
  2300. * the connection is accepted, no HTTP is going to be coming.
  2301. */
  2302. LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE = (1 << 23),
  2303. /**< (VH) Set to allow multiple listen sockets on one interface +
  2304. * address + port. The default is to strictly allow only one
  2305. * listen socket at a time. This is automatically selected if you
  2306. * have multiple service threads.
  2307. */
  2308. LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX = (1 << 24),
  2309. /**< (VH) Force setting up the vhost SSL_CTX, even though the user
  2310. * code doesn't explicitly provide a cert in the info struct. It
  2311. * implies the user code is going to provide a cert at the
  2312. * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS callback, which
  2313. * provides the vhost SSL_CTX * in the user parameter.
  2314. */
  2315. LWS_SERVER_OPTION_SKIP_PROTOCOL_INIT = (1 << 25),
  2316. /**< (VH) You probably don't want this. It forces this vhost to not
  2317. * call LWS_CALLBACK_PROTOCOL_INIT on its protocols. It's used in the
  2318. * special case of a temporary vhost bound to a single protocol.
  2319. */
  2320. LWS_SERVER_OPTION_IGNORE_MISSING_CERT = (1 << 26),
  2321. /**< (VH) Don't fail if the vhost TLS cert or key are missing, just
  2322. * continue. The vhost won't be able to serve anything, but if for
  2323. * example the ACME plugin was configured to fetch a cert, this lets
  2324. * you bootstrap your vhost from having no cert to start with.
  2325. */
  2326. /****** add new things just above ---^ ******/
  2327. };
  2328. #define lws_check_opt(c, f) (((c) & (f)) == (f))
  2329. struct lws_plat_file_ops;
  2330. /** struct lws_context_creation_info - parameters to create context and /or vhost with
  2331. *
  2332. * This is also used to create vhosts.... if LWS_SERVER_OPTION_EXPLICIT_VHOSTS
  2333. * is not given, then for backwards compatibility one vhost is created at
  2334. * context-creation time using the info from this struct.
  2335. *
  2336. * If LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, then no vhosts are created
  2337. * at the same time as the context, they are expected to be created afterwards.
  2338. */
  2339. struct lws_context_creation_info {
  2340. int port;
  2341. /**< VHOST: Port to listen on. Use CONTEXT_PORT_NO_LISTEN to suppress
  2342. * listening for a client. Use CONTEXT_PORT_NO_LISTEN_SERVER if you are
  2343. * writing a server but you are using \ref sock-adopt instead of the
  2344. * built-in listener.
  2345. *
  2346. * You can also set port to 0, in which case the kernel will pick
  2347. * a random port that is not already in use. You can find out what
  2348. * port the vhost is listening on using lws_get_vhost_listen_port() */
  2349. const char *iface;
  2350. /**< VHOST: NULL to bind the listen socket to all interfaces, or the
  2351. * interface name, eg, "eth2"
  2352. * If options specifies LWS_SERVER_OPTION_UNIX_SOCK, this member is
  2353. * the pathname of a UNIX domain socket. you can use the UNIX domain
  2354. * sockets in abstract namespace, by prepending an at symbol to the
  2355. * socket name. */
  2356. const struct lws_protocols *protocols;
  2357. /**< VHOST: Array of structures listing supported protocols and a protocol-
  2358. * specific callback for each one. The list is ended with an
  2359. * entry that has a NULL callback pointer. */
  2360. const struct lws_extension *extensions;
  2361. /**< VHOST: NULL or array of lws_extension structs listing the
  2362. * extensions this context supports. */
  2363. const struct lws_token_limits *token_limits;
  2364. /**< CONTEXT: NULL or struct lws_token_limits pointer which is initialized
  2365. * with a token length limit for each possible WSI_TOKEN_ */
  2366. const char *ssl_private_key_password;
  2367. /**< VHOST: NULL or the passphrase needed for the private key. (For
  2368. * backwards compatibility, this can also be used to pass the client
  2369. * cert passphrase when setting up a vhost client SSL context, but it is
  2370. * preferred to use .client_ssl_private_key_password for that.) */
  2371. const char *ssl_cert_filepath;
  2372. /**< VHOST: If libwebsockets was compiled to use ssl, and you want
  2373. * to listen using SSL, set to the filepath to fetch the
  2374. * server cert from, otherwise NULL for unencrypted. (For backwards
  2375. * compatibility, this can also be used to pass the client certificate
  2376. * when setting up a vhost client SSL context, but it is preferred to
  2377. * use .client_ssl_cert_filepath for that.) */
  2378. const char *ssl_private_key_filepath;
  2379. /**< VHOST: filepath to private key if wanting SSL mode;
  2380. * if this is set to NULL but ssl_cert_filepath is set, the
  2381. * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
  2382. * to allow setting of the private key directly via openSSL
  2383. * library calls. (For backwards compatibility, this can also be used
  2384. * to pass the client cert private key filepath when setting up a
  2385. * vhost client SSL context, but it is preferred to use
  2386. * .client_ssl_private_key_filepath for that.) */
  2387. const char *ssl_ca_filepath;
  2388. /**< VHOST: CA certificate filepath or NULL. (For backwards
  2389. * compatibility, this can also be used to pass the client CA
  2390. * filepath when setting up a vhost client SSL context,
  2391. * but it is preferred to use .client_ssl_ca_filepath for that.) */
  2392. const char *ssl_cipher_list;
  2393. /**< VHOST: List of valid ciphers to use (eg,
  2394. * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
  2395. * or you can leave it as NULL to get "DEFAULT" (For backwards
  2396. * compatibility, this can also be used to pass the client cipher
  2397. * list when setting up a vhost client SSL context,
  2398. * but it is preferred to use .client_ssl_cipher_list for that.)*/
  2399. const char *http_proxy_address;
  2400. /**< VHOST: If non-NULL, attempts to proxy via the given address.
  2401. * If proxy auth is required, use format "username:password\@server:port" */
  2402. unsigned int http_proxy_port;
  2403. /**< VHOST: If http_proxy_address was non-NULL, uses this port */
  2404. int gid;
  2405. /**< CONTEXT: group id to change to after setting listen socket, or -1. */
  2406. int uid;
  2407. /**< CONTEXT: user id to change to after setting listen socket, or -1. */
  2408. unsigned int options;
  2409. /**< VHOST + CONTEXT: 0, or LWS_SERVER_OPTION_... bitfields */
  2410. void *user;
  2411. /**< VHOST + CONTEXT: optional user pointer that will be associated
  2412. * with the context when creating the context (and can be retrieved by
  2413. * lws_context_user(context), or with the vhost when creating the vhost
  2414. * (and can be retrieved by lws_vhost_user(vhost)). You will need to
  2415. * use LWS_SERVER_OPTION_EXPLICIT_VHOSTS and create the vhost separately
  2416. * if you care about giving the context and vhost different user pointer
  2417. * values.
  2418. */
  2419. int ka_time;
  2420. /**< CONTEXT: 0 for no TCP keepalive, otherwise apply this keepalive
  2421. * timeout to all libwebsocket sockets, client or server */
  2422. int ka_probes;
  2423. /**< CONTEXT: if ka_time was nonzero, after the timeout expires how many
  2424. * times to try to get a response from the peer before giving up
  2425. * and killing the connection */
  2426. int ka_interval;
  2427. /**< CONTEXT: if ka_time was nonzero, how long to wait before each ka_probes
  2428. * attempt */
  2429. #if defined(LWS_WITH_TLS) && !defined(LWS_WITH_MBEDTLS)
  2430. SSL_CTX *provided_client_ssl_ctx;
  2431. /**< CONTEXT: If non-null, swap out libwebsockets ssl
  2432. * implementation for the one provided by provided_ssl_ctx.
  2433. * Libwebsockets no longer is responsible for freeing the context
  2434. * if this option is selected. */
  2435. #else /* maintain structure layout either way */
  2436. void *provided_client_ssl_ctx; /**< dummy if ssl disabled */
  2437. #endif
  2438. short max_http_header_data;
  2439. /**< CONTEXT: The max amount of header payload that can be handled
  2440. * in an http request (unrecognized header payload is dropped) */
  2441. short max_http_header_pool;
  2442. /**< CONTEXT: The max number of connections with http headers that
  2443. * can be processed simultaneously (the corresponding memory is
  2444. * allocated and deallocated dynamically as needed). If the pool is
  2445. * fully busy new incoming connections must wait for accept until one
  2446. * becomes free. 0 = allow as many ah as number of availble fds for
  2447. * the process */
  2448. unsigned int count_threads;
  2449. /**< CONTEXT: how many contexts to create in an array, 0 = 1 */
  2450. unsigned int fd_limit_per_thread;
  2451. /**< CONTEXT: nonzero means restrict each service thread to this
  2452. * many fds, 0 means the default which is divide the process fd
  2453. * limit by the number of threads. */
  2454. unsigned int timeout_secs;
  2455. /**< VHOST: various processes involving network roundtrips in the
  2456. * library are protected from hanging forever by timeouts. If
  2457. * nonzero, this member lets you set the timeout used in seconds.
  2458. * Otherwise a default timeout is used. */
  2459. const char *ecdh_curve;
  2460. /**< VHOST: if NULL, defaults to initializing server with "prime256v1" */
  2461. const char *vhost_name;
  2462. /**< VHOST: name of vhost, must match external DNS name used to
  2463. * access the site, like "warmcat.com" as it's used to match
  2464. * Host: header and / or SNI name for SSL. */
  2465. const char * const *plugin_dirs;
  2466. /**< CONTEXT: NULL, or NULL-terminated array of directories to
  2467. * scan for lws protocol plugins at context creation time */
  2468. const struct lws_protocol_vhost_options *pvo;
  2469. /**< VHOST: pointer to optional linked list of per-vhost
  2470. * options made accessible to protocols */
  2471. int keepalive_timeout;
  2472. /**< VHOST: (default = 0 = 5s) seconds to allow remote
  2473. * client to hold on to an idle HTTP/1.1 connection */
  2474. const char *log_filepath;
  2475. /**< VHOST: filepath to append logs to... this is opened before
  2476. * any dropping of initial privileges */
  2477. const struct lws_http_mount *mounts;
  2478. /**< VHOST: optional linked list of mounts for this vhost */
  2479. const char *server_string;
  2480. /**< CONTEXT: string used in HTTP headers to identify server
  2481. * software, if NULL, "libwebsockets". */
  2482. unsigned int pt_serv_buf_size;
  2483. /**< CONTEXT: 0 = default of 4096. This buffer is used by
  2484. * various service related features including file serving, it
  2485. * defines the max chunk of file that can be sent at once.
  2486. * At the risk of lws having to buffer failed large sends, it
  2487. * can be increased to, eg, 128KiB to improve throughput. */
  2488. unsigned int max_http_header_data2;
  2489. /**< CONTEXT: if max_http_header_data is 0 and this
  2490. * is nonzero, this will be used in place of the default. It's
  2491. * like this for compatibility with the original short version,
  2492. * this is unsigned int length. */
  2493. long ssl_options_set;
  2494. /**< VHOST: Any bits set here will be set as SSL options */
  2495. long ssl_options_clear;
  2496. /**< VHOST: Any bits set here will be cleared as SSL options */
  2497. unsigned short ws_ping_pong_interval;
  2498. /**< CONTEXT: 0 for none, else interval in seconds between sending
  2499. * PINGs on idle websocket connections. When the PING is sent,
  2500. * the PONG must come within the normal timeout_secs timeout period
  2501. * or the connection will be dropped.
  2502. * Any RX or TX traffic on the connection restarts the interval timer,
  2503. * so a connection which always sends or receives something at intervals
  2504. * less than the interval given here will never send PINGs / expect
  2505. * PONGs. Conversely as soon as the ws connection is established, an
  2506. * idle connection will do the PING / PONG roundtrip as soon as
  2507. * ws_ping_pong_interval seconds has passed without traffic
  2508. */
  2509. const struct lws_protocol_vhost_options *headers;
  2510. /**< VHOST: pointer to optional linked list of per-vhost
  2511. * canned headers that are added to server responses */
  2512. const struct lws_protocol_vhost_options *reject_service_keywords;
  2513. /**< CONTEXT: Optional list of keywords and rejection codes + text.
  2514. *
  2515. * The keywords are checked for existing in the user agent string.
  2516. *
  2517. * Eg, "badrobot" "404 Not Found"
  2518. */
  2519. void *external_baggage_free_on_destroy;
  2520. /**< CONTEXT: NULL, or pointer to something externally malloc'd, that
  2521. * should be freed when the context is destroyed. This allows you to
  2522. * automatically sync the freeing action to the context destruction
  2523. * action, so there is no need for an external free() if the context
  2524. * succeeded to create.
  2525. */
  2526. const char *client_ssl_private_key_password;
  2527. /**< VHOST: Client SSL context init: NULL or the passphrase needed
  2528. * for the private key */
  2529. const char *client_ssl_cert_filepath;
  2530. /**< VHOST: Client SSL context init:T he certificate the client
  2531. * should present to the peer on connection */
  2532. const char *client_ssl_private_key_filepath;
  2533. /**< VHOST: Client SSL context init: filepath to client private key
  2534. * if this is set to NULL but client_ssl_cert_filepath is set, you
  2535. * can handle the LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS
  2536. * callback of protocols[0] to allow setting of the private key directly
  2537. * via openSSL library calls */
  2538. const char *client_ssl_ca_filepath;
  2539. /**< VHOST: Client SSL context init: CA certificate filepath or NULL */
  2540. const char *client_ssl_cipher_list;
  2541. /**< VHOST: Client SSL context init: List of valid ciphers to use (eg,
  2542. * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
  2543. * or you can leave it as NULL to get "DEFAULT" */
  2544. const struct lws_plat_file_ops *fops;
  2545. /**< CONTEXT: NULL, or pointer to an array of fops structs, terminated
  2546. * by a sentinel with NULL .open.
  2547. *
  2548. * If NULL, lws provides just the platform file operations struct for
  2549. * backwards compatibility.
  2550. */
  2551. int simultaneous_ssl_restriction;
  2552. /**< CONTEXT: 0 (no limit) or limit of simultaneous SSL sessions possible.*/
  2553. const char *socks_proxy_address;
  2554. /**< VHOST: If non-NULL, attempts to proxy via the given address.
  2555. * If proxy auth is required, use format "username:password\@server:port" */
  2556. unsigned int socks_proxy_port;
  2557. /**< VHOST: If socks_proxy_address was non-NULL, uses this port */
  2558. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  2559. cap_value_t caps[4];
  2560. /**< CONTEXT: array holding Linux capabilities you want to
  2561. * continue to be available to the server after it transitions
  2562. * to a noprivileged user. Usually none are needed but for, eg,
  2563. * .bind_iface, CAP_NET_RAW is required. This gives you a way
  2564. * to still have the capability but drop root.
  2565. */
  2566. char count_caps;
  2567. /**< CONTEXT: count of Linux capabilities in .caps[]. 0 means
  2568. * no capabilities will be inherited from root (the default) */
  2569. #endif
  2570. int bind_iface;
  2571. /**< VHOST: nonzero to strictly bind sockets to the interface name in
  2572. * .iface (eg, "eth2"), using SO_BIND_TO_DEVICE.
  2573. *
  2574. * Requires SO_BINDTODEVICE support from your OS and CAP_NET_RAW
  2575. * capability.
  2576. *
  2577. * Notice that common things like access network interface IP from
  2578. * your local machine use your lo / loopback interface and will be
  2579. * disallowed by this.
  2580. */
  2581. int ssl_info_event_mask;
  2582. /**< VHOST: mask of ssl events to be reported on LWS_CALLBACK_SSL_INFO
  2583. * callback for connections on this vhost. The mask values are of
  2584. * the form SSL_CB_ALERT, defined in openssl/ssl.h. The default of
  2585. * 0 means no info events will be reported.
  2586. */
  2587. unsigned int timeout_secs_ah_idle;
  2588. /**< VHOST: seconds to allow a client to hold an ah without using it.
  2589. * 0 defaults to 10s. */
  2590. unsigned short ip_limit_ah;
  2591. /**< CONTEXT: max number of ah a single IP may use simultaneously
  2592. * 0 is no limit. This is a soft limit: if the limit is
  2593. * reached, connections from that IP will wait in the ah
  2594. * waiting list and not be able to acquire an ah until
  2595. * a connection belonging to the IP relinquishes one it
  2596. * already has.
  2597. */
  2598. unsigned short ip_limit_wsi;
  2599. /**< CONTEXT: max number of wsi a single IP may use simultaneously.
  2600. * 0 is no limit. This is a hard limit, connections from
  2601. * the same IP will simply be dropped once it acquires the
  2602. * amount of simultaneous wsi / accepted connections
  2603. * given here.
  2604. */
  2605. uint32_t http2_settings[7];
  2606. /**< VHOST: if http2_settings[0] is nonzero, the values given in
  2607. * http2_settings[1]..[6] are used instead of the lws
  2608. * platform default values.
  2609. * Just leave all at 0 if you don't care.
  2610. */
  2611. const char *error_document_404;
  2612. /**< VHOST: If non-NULL, when asked to serve a non-existent file,
  2613. * lws attempts to server this url path instead. Eg,
  2614. * "/404.html" */
  2615. const char *alpn;
  2616. /**< CONTEXT: If non-NULL, default list of advertised alpn, comma-
  2617. * separated
  2618. *
  2619. * VHOST: If non-NULL, per-vhost list of advertised alpn, comma-
  2620. * separated
  2621. */
  2622. void **foreign_loops;
  2623. /**< CONTEXT: This is ignored if the context is not being started with
  2624. * an event loop, ie, .options has a flag like
  2625. * LWS_SERVER_OPTION_LIBUV.
  2626. *
  2627. * NULL indicates lws should start its own even loop for
  2628. * each service thread, and deal with closing the loops
  2629. * when the context is destroyed.
  2630. *
  2631. * Non-NULL means it points to an array of external
  2632. * ("foreign") event loops that are to be used in turn for
  2633. * each service thread. In the default case of 1 service
  2634. * thread, it can just point to one foreign event loop.
  2635. */
  2636. void (*signal_cb)(void *event_lib_handle, int signum);
  2637. /**< CONTEXT: NULL: default signal handling. Otherwise this receives
  2638. * the signal handler callback. event_lib_handle is the
  2639. * native event library signal handle, eg uv_signal_t *
  2640. * for libuv.
  2641. */
  2642. /* Add new things just above here ---^
  2643. * This is part of the ABI, don't needlessly break compatibility
  2644. *
  2645. * The below is to ensure later library versions with new
  2646. * members added above will see 0 (default) even if the app
  2647. * was not built against the newer headers.
  2648. */
  2649. struct lws_context **pcontext;
  2650. /**< CONTEXT: if non-NULL, at the end of context destroy processing,
  2651. * the pointer pointed to by pcontext is written with NULL. You can
  2652. * use this to let foreign event loops know that lws context destruction
  2653. * is fully completed.
  2654. */
  2655. void *_unused[4]; /**< dummy */
  2656. };
  2657. /**
  2658. * lws_create_context() - Create the websocket handler
  2659. * \param info: pointer to struct with parameters
  2660. *
  2661. * This function creates the listening socket (if serving) and takes care
  2662. * of all initialization in one step.
  2663. *
  2664. * If option LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, no vhost is
  2665. * created; you're expected to create your own vhosts afterwards using
  2666. * lws_create_vhost(). Otherwise a vhost named "default" is also created
  2667. * using the information in the vhost-related members, for compatibility.
  2668. *
  2669. * After initialization, it returns a struct lws_context * that
  2670. * represents this server. After calling, user code needs to take care
  2671. * of calling lws_service() with the context pointer to get the
  2672. * server's sockets serviced. This must be done in the same process
  2673. * context as the initialization call.
  2674. *
  2675. * The protocol callback functions are called for a handful of events
  2676. * including http requests coming in, websocket connections becoming
  2677. * established, and data arriving; it's also called periodically to allow
  2678. * async transmission.
  2679. *
  2680. * HTTP requests are sent always to the FIRST protocol in protocol, since
  2681. * at that time websocket protocol has not been negotiated. Other
  2682. * protocols after the first one never see any HTTP callback activity.
  2683. *
  2684. * The server created is a simple http server by default; part of the
  2685. * websocket standard is upgrading this http connection to a websocket one.
  2686. *
  2687. * This allows the same server to provide files like scripts and favicon /
  2688. * images or whatever over http and dynamic data over websockets all in
  2689. * one place; they're all handled in the user callback.
  2690. */
  2691. LWS_VISIBLE LWS_EXTERN struct lws_context *
  2692. lws_create_context(const struct lws_context_creation_info *info);
  2693. /**
  2694. * lws_context_destroy() - Destroy the websocket context
  2695. * \param context: Websocket context
  2696. *
  2697. * This function closes any active connections and then frees the
  2698. * context. After calling this, any further use of the context is
  2699. * undefined.
  2700. */
  2701. LWS_VISIBLE LWS_EXTERN void
  2702. lws_context_destroy(struct lws_context *context);
  2703. typedef int (*lws_reload_func)(void);
  2704. /**
  2705. * lws_context_deprecate() - Deprecate the websocket context
  2706. *
  2707. * \param context: Websocket context
  2708. * \param cb: Callback notified when old context listen sockets are closed
  2709. *
  2710. * This function is used on an existing context before superceding it
  2711. * with a new context.
  2712. *
  2713. * It closes any listen sockets in the context, so new connections are
  2714. * not possible.
  2715. *
  2716. * And it marks the context to be deleted when the number of active
  2717. * connections into it falls to zero.
  2718. *
  2719. * Otherwise if you attach the deprecated context to the replacement
  2720. * context when it has been created using lws_context_attach_deprecated()
  2721. * both any deprecated and the new context will service their connections.
  2722. *
  2723. * This is aimed at allowing seamless configuration reloads.
  2724. *
  2725. * The callback cb will be called after the listen sockets are actually
  2726. * closed and may be reopened. In the callback the new context should be
  2727. * configured and created. (With libuv, socket close happens async after
  2728. * more loop events).
  2729. */
  2730. LWS_VISIBLE LWS_EXTERN void
  2731. lws_context_deprecate(struct lws_context *context, lws_reload_func cb);
  2732. LWS_VISIBLE LWS_EXTERN int
  2733. lws_context_is_deprecated(struct lws_context *context);
  2734. /**
  2735. * lws_set_proxy() - Setups proxy to lws_context.
  2736. * \param vhost: pointer to struct lws_vhost you want set proxy for
  2737. * \param proxy: pointer to c string containing proxy in format address:port
  2738. *
  2739. * Returns 0 if proxy string was parsed and proxy was setup.
  2740. * Returns -1 if proxy is NULL or has incorrect format.
  2741. *
  2742. * This is only required if your OS does not provide the http_proxy
  2743. * environment variable (eg, OSX)
  2744. *
  2745. * IMPORTANT! You should call this function right after creation of the
  2746. * lws_context and before call to connect. If you call this
  2747. * function after connect behavior is undefined.
  2748. * This function will override proxy settings made on lws_context
  2749. * creation with genenv() call.
  2750. */
  2751. LWS_VISIBLE LWS_EXTERN int
  2752. lws_set_proxy(struct lws_vhost *vhost, const char *proxy);
  2753. /**
  2754. * lws_set_socks() - Setup socks to lws_context.
  2755. * \param vhost: pointer to struct lws_vhost you want set socks for
  2756. * \param socks: pointer to c string containing socks in format address:port
  2757. *
  2758. * Returns 0 if socks string was parsed and socks was setup.
  2759. * Returns -1 if socks is NULL or has incorrect format.
  2760. *
  2761. * This is only required if your OS does not provide the socks_proxy
  2762. * environment variable (eg, OSX)
  2763. *
  2764. * IMPORTANT! You should call this function right after creation of the
  2765. * lws_context and before call to connect. If you call this
  2766. * function after connect behavior is undefined.
  2767. * This function will override proxy settings made on lws_context
  2768. * creation with genenv() call.
  2769. */
  2770. LWS_VISIBLE LWS_EXTERN int
  2771. lws_set_socks(struct lws_vhost *vhost, const char *socks);
  2772. struct lws_vhost;
  2773. /**
  2774. * lws_create_vhost() - Create a vhost (virtual server context)
  2775. * \param context: pointer to result of lws_create_context()
  2776. * \param info: pointer to struct with parameters
  2777. *
  2778. * This function creates a virtual server (vhost) using the vhost-related
  2779. * members of the info struct. You can create many vhosts inside one context
  2780. * if you created the context with the option LWS_SERVER_OPTION_EXPLICIT_VHOSTS
  2781. */
  2782. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  2783. lws_create_vhost(struct lws_context *context,
  2784. const struct lws_context_creation_info *info);
  2785. /**
  2786. * lws_vhost_destroy() - Destroy a vhost (virtual server context)
  2787. *
  2788. * \param vh: pointer to result of lws_create_vhost()
  2789. *
  2790. * This function destroys a vhost. Normally, if you just want to exit,
  2791. * then lws_destroy_context() will take care of everything. If you want
  2792. * to destroy an individual vhost and all connections and allocations, you
  2793. * can do it with this.
  2794. *
  2795. * If the vhost has a listen sockets shared by other vhosts, it will be given
  2796. * to one of the vhosts sharing it rather than closed.
  2797. */
  2798. LWS_VISIBLE LWS_EXTERN void
  2799. lws_vhost_destroy(struct lws_vhost *vh);
  2800. /**
  2801. * lwsws_get_config_globals() - Parse a JSON server config file
  2802. * \param info: pointer to struct with parameters
  2803. * \param d: filepath of the config file
  2804. * \param config_strings: storage for the config strings extracted from JSON,
  2805. * the pointer is incremented as strings are stored
  2806. * \param len: pointer to the remaining length left in config_strings
  2807. * the value is decremented as strings are stored
  2808. *
  2809. * This function prepares a n lws_context_creation_info struct with global
  2810. * settings from a file d.
  2811. *
  2812. * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
  2813. */
  2814. LWS_VISIBLE LWS_EXTERN int
  2815. lwsws_get_config_globals(struct lws_context_creation_info *info, const char *d,
  2816. char **config_strings, int *len);
  2817. /**
  2818. * lwsws_get_config_vhosts() - Create vhosts from a JSON server config file
  2819. * \param context: pointer to result of lws_create_context()
  2820. * \param info: pointer to struct with parameters
  2821. * \param d: filepath of the config file
  2822. * \param config_strings: storage for the config strings extracted from JSON,
  2823. * the pointer is incremented as strings are stored
  2824. * \param len: pointer to the remaining length left in config_strings
  2825. * the value is decremented as strings are stored
  2826. *
  2827. * This function creates vhosts into a context according to the settings in
  2828. *JSON files found in directory d.
  2829. *
  2830. * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
  2831. */
  2832. LWS_VISIBLE LWS_EXTERN int
  2833. lwsws_get_config_vhosts(struct lws_context *context,
  2834. struct lws_context_creation_info *info, const char *d,
  2835. char **config_strings, int *len);
  2836. /** lws_vhost_get() - \deprecated deprecated: use lws_get_vhost() */
  2837. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  2838. lws_vhost_get(struct lws *wsi) LWS_WARN_DEPRECATED;
  2839. /**
  2840. * lws_get_vhost() - return the vhost a wsi belongs to
  2841. *
  2842. * \param wsi: which connection
  2843. */
  2844. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  2845. lws_get_vhost(struct lws *wsi);
  2846. /**
  2847. * lws_get_vhost_name() - returns the name of a vhost
  2848. *
  2849. * \param vhost: which vhost
  2850. */
  2851. LWS_VISIBLE LWS_EXTERN const char *
  2852. lws_get_vhost_name(struct lws_vhost *vhost);
  2853. /**
  2854. * lws_get_vhost_port() - returns the port a vhost listens on, or -1
  2855. *
  2856. * \param vhost: which vhost
  2857. */
  2858. LWS_VISIBLE LWS_EXTERN int
  2859. lws_get_vhost_port(struct lws_vhost *vhost);
  2860. /**
  2861. * lws_get_vhost_user() - returns the user pointer for the vhost
  2862. *
  2863. * \param vhost: which vhost
  2864. */
  2865. LWS_VISIBLE LWS_EXTERN void *
  2866. lws_get_vhost_user(struct lws_vhost *vhost);
  2867. /**
  2868. * lws_get_vhost_iface() - returns the binding for the vhost listen socket
  2869. *
  2870. * \param vhost: which vhost
  2871. */
  2872. LWS_VISIBLE LWS_EXTERN const char *
  2873. lws_get_vhost_iface(struct lws_vhost *vhost);
  2874. /**
  2875. * lws_json_dump_vhost() - describe vhost state and stats in JSON
  2876. *
  2877. * \param vh: the vhost
  2878. * \param buf: buffer to fill with JSON
  2879. * \param len: max length of buf
  2880. */
  2881. LWS_VISIBLE LWS_EXTERN int
  2882. lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len);
  2883. /**
  2884. * lws_json_dump_context() - describe context state and stats in JSON
  2885. *
  2886. * \param context: the context
  2887. * \param buf: buffer to fill with JSON
  2888. * \param len: max length of buf
  2889. * \param hide_vhosts: nonzero to not provide per-vhost mount etc information
  2890. *
  2891. * Generates a JSON description of vhost state into buf
  2892. */
  2893. LWS_VISIBLE LWS_EXTERN int
  2894. lws_json_dump_context(const struct lws_context *context, char *buf, int len,
  2895. int hide_vhosts);
  2896. /**
  2897. * lws_vhost_user() - get the user data associated with the vhost
  2898. * \param vhost: Websocket vhost
  2899. *
  2900. * This returns the optional user pointer that can be attached to
  2901. * a vhost when it was created. Lws never dereferences this pointer, it only
  2902. * sets it when the vhost is created, and returns it using this api.
  2903. */
  2904. LWS_VISIBLE LWS_EXTERN void *
  2905. lws_vhost_user(struct lws_vhost *vhost);
  2906. /**
  2907. * lws_context_user() - get the user data associated with the context
  2908. * \param context: Websocket context
  2909. *
  2910. * This returns the optional user allocation that can be attached to
  2911. * the context the sockets live in at context_create time. It's a way
  2912. * to let all sockets serviced in the same context share data without
  2913. * using globals statics in the user code.
  2914. */
  2915. LWS_VISIBLE LWS_EXTERN void *
  2916. lws_context_user(struct lws_context *context);
  2917. /*! \defgroup vhost-mounts Vhost mounts and options
  2918. * \ingroup context-and-vhost-creation
  2919. *
  2920. * ##Vhost mounts and options
  2921. */
  2922. ///@{
  2923. /** struct lws_protocol_vhost_options - linked list of per-vhost protocol
  2924. * name=value options
  2925. *
  2926. * This provides a general way to attach a linked-list of name=value pairs,
  2927. * which can also have an optional child link-list using the options member.
  2928. */
  2929. struct lws_protocol_vhost_options {
  2930. const struct lws_protocol_vhost_options *next; /**< linked list */
  2931. const struct lws_protocol_vhost_options *options; /**< child linked-list of more options for this node */
  2932. const char *name; /**< name of name=value pair */
  2933. const char *value; /**< value of name=value pair */
  2934. };
  2935. /** enum lws_mount_protocols
  2936. * This specifies the mount protocol for a mountpoint, whether it is to be
  2937. * served from a filesystem, or it is a cgi etc.
  2938. */
  2939. enum lws_mount_protocols {
  2940. LWSMPRO_HTTP = 0, /**< http reverse proxy */
  2941. LWSMPRO_HTTPS = 1, /**< https reverse proxy */
  2942. LWSMPRO_FILE = 2, /**< serve from filesystem directory */
  2943. LWSMPRO_CGI = 3, /**< pass to CGI to handle */
  2944. LWSMPRO_REDIR_HTTP = 4, /**< redirect to http:// url */
  2945. LWSMPRO_REDIR_HTTPS = 5, /**< redirect to https:// url */
  2946. LWSMPRO_CALLBACK = 6, /**< hand by named protocol's callback */
  2947. };
  2948. /** struct lws_http_mount
  2949. *
  2950. * arguments for mounting something in a vhost's url namespace
  2951. */
  2952. struct lws_http_mount {
  2953. const struct lws_http_mount *mount_next;
  2954. /**< pointer to next struct lws_http_mount */
  2955. const char *mountpoint;
  2956. /**< mountpoint in http pathspace, eg, "/" */
  2957. const char *origin;
  2958. /**< path to be mounted, eg, "/var/www/warmcat.com" */
  2959. const char *def;
  2960. /**< default target, eg, "index.html" */
  2961. const char *protocol;
  2962. /**<"protocol-name" to handle mount */
  2963. const struct lws_protocol_vhost_options *cgienv;
  2964. /**< optional linked-list of cgi options. These are created
  2965. * as environment variables for the cgi process
  2966. */
  2967. const struct lws_protocol_vhost_options *extra_mimetypes;
  2968. /**< optional linked-list of mimetype mappings */
  2969. const struct lws_protocol_vhost_options *interpret;
  2970. /**< optional linked-list of files to be interpreted */
  2971. int cgi_timeout;
  2972. /**< seconds cgi is allowed to live, if cgi://mount type */
  2973. int cache_max_age;
  2974. /**< max-age for reuse of client cache of files, seconds */
  2975. unsigned int auth_mask;
  2976. /**< bits set here must be set for authorized client session */
  2977. unsigned int cache_reusable:1; /**< set if client cache may reuse this */
  2978. unsigned int cache_revalidate:1; /**< set if client cache should revalidate on use */
  2979. unsigned int cache_intermediaries:1; /**< set if intermediaries are allowed to cache */
  2980. unsigned char origin_protocol; /**< one of enum lws_mount_protocols */
  2981. unsigned char mountpoint_len; /**< length of mountpoint string */
  2982. const char *basic_auth_login_file;
  2983. /**<NULL, or filepath to use to check basic auth logins against */
  2984. /* Add new things just above here ---^
  2985. * This is part of the ABI, don't needlessly break compatibility
  2986. *
  2987. * The below is to ensure later library versions with new
  2988. * members added above will see 0 (default) even if the app
  2989. * was not built against the newer headers.
  2990. */
  2991. void *_unused[2]; /**< dummy */
  2992. };
  2993. ///@}
  2994. ///@}
  2995. /*! \defgroup client Client related functions
  2996. * ##Client releated functions
  2997. * \ingroup lwsapi
  2998. *
  2999. * */
  3000. ///@{
  3001. /** enum lws_client_connect_ssl_connection_flags - flags that may be used
  3002. * with struct lws_client_connect_info ssl_connection member to control if
  3003. * and how SSL checks apply to the client connection being created
  3004. */
  3005. enum lws_client_connect_ssl_connection_flags {
  3006. LCCSCF_USE_SSL = (1 << 0),
  3007. LCCSCF_ALLOW_SELFSIGNED = (1 << 1),
  3008. LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK = (1 << 2),
  3009. LCCSCF_ALLOW_EXPIRED = (1 << 3),
  3010. LCCSCF_PIPELINE = (1 << 16),
  3011. /**< Serialize / pipeline multiple client connections
  3012. * on a single connection where possible.
  3013. *
  3014. * HTTP/1.0: possible if Keep-Alive: yes sent by server
  3015. * HTTP/1.1: always possible... uses pipelining
  3016. * HTTP/2: always possible... uses parallel streams
  3017. * */
  3018. };
  3019. /** struct lws_client_connect_info - parameters to connect with when using
  3020. * lws_client_connect_via_info() */
  3021. struct lws_client_connect_info {
  3022. struct lws_context *context;
  3023. /**< lws context to create connection in */
  3024. const char *address;
  3025. /**< remote address to connect to */
  3026. int port;
  3027. /**< remote port to connect to */
  3028. int ssl_connection;
  3029. /**< 0, or a combination of LCCSCF_ flags */
  3030. const char *path;
  3031. /**< uri path */
  3032. const char *host;
  3033. /**< content of host header */
  3034. const char *origin;
  3035. /**< content of origin header */
  3036. const char *protocol;
  3037. /**< list of ws protocols we could accept */
  3038. int ietf_version_or_minus_one;
  3039. /**< deprecated: currently leave at 0 or -1 */
  3040. void *userdata;
  3041. /**< if non-NULL, use this as wsi user_data instead of malloc it */
  3042. const void *client_exts;
  3043. /**< UNUSED... provide in info.extensions at context creation time */
  3044. const char *method;
  3045. /**< if non-NULL, do this http method instead of ws[s] upgrade.
  3046. * use "GET" to be a simple http client connection. "RAW" gets
  3047. * you a connected socket that lws itself will leave alone once
  3048. * connected. */
  3049. struct lws *parent_wsi;
  3050. /**< if another wsi is responsible for this connection, give it here.
  3051. * this is used to make sure if the parent closes so do any
  3052. * child connections first. */
  3053. const char *uri_replace_from;
  3054. /**< if non-NULL, when this string is found in URIs in
  3055. * text/html content-encoding, it's replaced with uri_replace_to */
  3056. const char *uri_replace_to;
  3057. /**< see uri_replace_from */
  3058. struct lws_vhost *vhost;
  3059. /**< vhost to bind to (used to determine related SSL_CTX) */
  3060. struct lws **pwsi;
  3061. /**< if not NULL, store the new wsi here early in the connection
  3062. * process. Although we return the new wsi, the call to create the
  3063. * client connection does progress the connection somewhat and may
  3064. * meet an error that will result in the connection being scrubbed and
  3065. * NULL returned. While the wsi exists though, he may process a
  3066. * callback like CLIENT_CONNECTION_ERROR with his wsi: this gives the
  3067. * user callback a way to identify which wsi it is that faced the error
  3068. * even before the new wsi is returned and even if ultimately no wsi
  3069. * is returned.
  3070. */
  3071. const char *iface;
  3072. /**< NULL to allow routing on any interface, or interface name or IP
  3073. * to bind the socket to */
  3074. const char *local_protocol_name;
  3075. /**< NULL: .protocol is used both to select the local protocol handler
  3076. * to bind to and as the list of remote ws protocols we could
  3077. * accept.
  3078. * non-NULL: this protocol name is used to bind the connection to
  3079. * the local protocol handler. .protocol is used for the
  3080. * list of remote ws protocols we could accept */
  3081. /* Add new things just above here ---^
  3082. * This is part of the ABI, don't needlessly break compatibility
  3083. *
  3084. * The below is to ensure later library versions with new
  3085. * members added above will see 0 (default) even if the app
  3086. * was not built against the newer headers.
  3087. */
  3088. const char *alpn;
  3089. /* NULL: allow lws default ALPN list, from vhost if present or from
  3090. * list of roles built into lws
  3091. * non-NULL: require one from provided comma-separated list of alpn
  3092. * tokens
  3093. */
  3094. void *_unused[4]; /**< dummy */
  3095. };
  3096. /**
  3097. * lws_client_connect_via_info() - Connect to another websocket server
  3098. * \param ccinfo: pointer to lws_client_connect_info struct
  3099. *
  3100. * This function creates a connection to a remote server using the
  3101. * information provided in ccinfo.
  3102. */
  3103. LWS_VISIBLE LWS_EXTERN struct lws *
  3104. lws_client_connect_via_info(struct lws_client_connect_info * ccinfo);
  3105. /**
  3106. * lws_client_connect() - Connect to another websocket server
  3107. * \deprecated DEPRECATED use lws_client_connect_via_info
  3108. * \param clients: Websocket context
  3109. * \param address: Remote server address, eg, "myserver.com"
  3110. * \param port: Port to connect to on the remote server, eg, 80
  3111. * \param ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
  3112. * signed certs
  3113. * \param path: Websocket path on server
  3114. * \param host: Hostname on server
  3115. * \param origin: Socket origin name
  3116. * \param protocol: Comma-separated list of protocols being asked for from
  3117. * the server, or just one. The server will pick the one it
  3118. * likes best. If you don't want to specify a protocol, which is
  3119. * legal, use NULL here.
  3120. * \param ietf_version_or_minus_one: -1 to ask to connect using the default, latest
  3121. * protocol supported, or the specific protocol ordinal
  3122. *
  3123. * This function creates a connection to a remote server
  3124. */
  3125. /* deprecated, use lws_client_connect_via_info() */
  3126. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  3127. lws_client_connect(struct lws_context *clients, const char *address,
  3128. int port, int ssl_connection, const char *path,
  3129. const char *host, const char *origin, const char *protocol,
  3130. int ietf_version_or_minus_one) LWS_WARN_DEPRECATED;
  3131. /* deprecated, use lws_client_connect_via_info() */
  3132. /**
  3133. * lws_client_connect_extended() - Connect to another websocket server
  3134. * \deprecated DEPRECATED use lws_client_connect_via_info
  3135. * \param clients: Websocket context
  3136. * \param address: Remote server address, eg, "myserver.com"
  3137. * \param port: Port to connect to on the remote server, eg, 80
  3138. * \param ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
  3139. * signed certs
  3140. * \param path: Websocket path on server
  3141. * \param host: Hostname on server
  3142. * \param origin: Socket origin name
  3143. * \param protocol: Comma-separated list of protocols being asked for from
  3144. * the server, or just one. The server will pick the one it
  3145. * likes best.
  3146. * \param ietf_version_or_minus_one: -1 to ask to connect using the default, latest
  3147. * protocol supported, or the specific protocol ordinal
  3148. * \param userdata: Pre-allocated user data
  3149. *
  3150. * This function creates a connection to a remote server
  3151. */
  3152. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  3153. lws_client_connect_extended(struct lws_context *clients, const char *address,
  3154. int port, int ssl_connection, const char *path,
  3155. const char *host, const char *origin,
  3156. const char *protocol, int ietf_version_or_minus_one,
  3157. void *userdata) LWS_WARN_DEPRECATED;
  3158. /**
  3159. * lws_init_vhost_client_ssl() - also enable client SSL on an existing vhost
  3160. *
  3161. * \param info: client ssl related info
  3162. * \param vhost: which vhost to initialize client ssl operations on
  3163. *
  3164. * You only need to call this if you plan on using SSL client connections on
  3165. * the vhost. For non-SSL client connections, it's not necessary to call this.
  3166. *
  3167. * The following members of info are used during the call
  3168. *
  3169. * - options must have LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT set,
  3170. * otherwise the call does nothing
  3171. * - provided_client_ssl_ctx must be NULL to get a generated client
  3172. * ssl context, otherwise you can pass a prepared one in by setting it
  3173. * - ssl_cipher_list may be NULL or set to the client valid cipher list
  3174. * - ssl_ca_filepath may be NULL or client cert filepath
  3175. * - ssl_cert_filepath may be NULL or client cert filepath
  3176. * - ssl_private_key_filepath may be NULL or client cert private key
  3177. *
  3178. * You must create your vhost explicitly if you want to use this, so you have
  3179. * a pointer to the vhost. Create the context first with the option flag
  3180. * LWS_SERVER_OPTION_EXPLICIT_VHOSTS and then call lws_create_vhost() with
  3181. * the same info struct.
  3182. */
  3183. LWS_VISIBLE LWS_EXTERN int
  3184. lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
  3185. struct lws_vhost *vhost);
  3186. /**
  3187. * lws_http_client_read() - consume waiting received http client data
  3188. *
  3189. * \param wsi: client connection
  3190. * \param buf: pointer to buffer pointer - fill with pointer to your buffer
  3191. * \param len: pointer to chunk length - fill with max length of buffer
  3192. *
  3193. * This is called when the user code is notified client http data has arrived.
  3194. * The user code may choose to delay calling it to consume the data, for example
  3195. * waiting until an onward connection is writeable.
  3196. *
  3197. * For non-chunked connections, up to len bytes of buf are filled with the
  3198. * received content. len is set to the actual amount filled before return.
  3199. *
  3200. * For chunked connections, the linear buffer content contains the chunking
  3201. * headers and it cannot be passed in one lump. Instead, this function will
  3202. * call back LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ with in pointing to the
  3203. * chunk start and len set to the chunk length. There will be as many calls
  3204. * as there are chunks or partial chunks in the buffer.
  3205. */
  3206. LWS_VISIBLE LWS_EXTERN int
  3207. lws_http_client_read(struct lws *wsi, char **buf, int *len);
  3208. /**
  3209. * lws_http_client_http_response() - get last HTTP response code
  3210. *
  3211. * \param wsi: client connection
  3212. *
  3213. * Returns the last server response code, eg, 200 for client http connections.
  3214. *
  3215. * You should capture this during the LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP
  3216. * callback, because after that the memory reserved for storing the related
  3217. * headers is freed and this value is lost.
  3218. */
  3219. LWS_VISIBLE LWS_EXTERN unsigned int
  3220. lws_http_client_http_response(struct lws *wsi);
  3221. LWS_VISIBLE LWS_EXTERN void
  3222. lws_client_http_body_pending(struct lws *wsi, int something_left_to_send);
  3223. /**
  3224. * lws_client_http_body_pending() - control if client connection neeeds to send body
  3225. *
  3226. * \param wsi: client connection
  3227. * \param something_left_to_send: nonzero if need to send more body, 0 (default)
  3228. * if nothing more to send
  3229. *
  3230. * If you will send payload data with your HTTP client connection, eg, for POST,
  3231. * when you set the related http headers in
  3232. * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER callback you should also call
  3233. * this API with something_left_to_send nonzero, and call
  3234. * lws_callback_on_writable(wsi);
  3235. *
  3236. * After sending the headers, lws will call your callback with
  3237. * LWS_CALLBACK_CLIENT_HTTP_WRITEABLE reason when writable. You can send the
  3238. * next part of the http body payload, calling lws_callback_on_writable(wsi);
  3239. * if there is more to come, or lws_client_http_body_pending(wsi, 0); to
  3240. * let lws know the last part is sent and the connection can move on.
  3241. */
  3242. ///@}
  3243. /** \defgroup service Built-in service loop entry
  3244. *
  3245. * ##Built-in service loop entry
  3246. *
  3247. * If you're not using libev / libuv, these apis are needed to enter the poll()
  3248. * wait in lws and service any connections with pending events.
  3249. */
  3250. ///@{
  3251. /**
  3252. * lws_service() - Service any pending websocket activity
  3253. * \param context: Websocket context
  3254. * \param timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
  3255. * service otherwise block and service immediately, returning
  3256. * after the timeout if nothing needed service.
  3257. *
  3258. * This function deals with any pending websocket traffic, for three
  3259. * kinds of event. It handles these events on both server and client
  3260. * types of connection the same.
  3261. *
  3262. * 1) Accept new connections to our context's server
  3263. *
  3264. * 2) Call the receive callback for incoming frame data received by
  3265. * server or client connections.
  3266. *
  3267. * You need to call this service function periodically to all the above
  3268. * functions to happen; if your application is single-threaded you can
  3269. * just call it in your main event loop.
  3270. *
  3271. * Alternatively you can fork a new process that asynchronously handles
  3272. * calling this service in a loop. In that case you are happy if this
  3273. * call blocks your thread until it needs to take care of something and
  3274. * would call it with a large nonzero timeout. Your loop then takes no
  3275. * CPU while there is nothing happening.
  3276. *
  3277. * If you are calling it in a single-threaded app, you don't want it to
  3278. * wait around blocking other things in your loop from happening, so you
  3279. * would call it with a timeout_ms of 0, so it returns immediately if
  3280. * nothing is pending, or as soon as it services whatever was pending.
  3281. */
  3282. LWS_VISIBLE LWS_EXTERN int
  3283. lws_service(struct lws_context *context, int timeout_ms);
  3284. /**
  3285. * lws_service_tsi() - Service any pending websocket activity
  3286. *
  3287. * \param context: Websocket context
  3288. * \param timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
  3289. * service otherwise block and service immediately, returning
  3290. * after the timeout if nothing needed service.
  3291. * \param tsi: Thread service index, starting at 0
  3292. *
  3293. * Same as lws_service(), but for a specific thread service index. Only needed
  3294. * if you are spawning multiple service threads.
  3295. */
  3296. LWS_VISIBLE LWS_EXTERN int
  3297. lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
  3298. /**
  3299. * lws_cancel_service_pt() - Cancel servicing of pending socket activity
  3300. * on one thread
  3301. * \param wsi: Cancel service on the thread this wsi is serviced by
  3302. *
  3303. * Same as lws_cancel_service(), but targets a single service thread, the one
  3304. * the wsi belongs to. You probably want to use lws_cancel_service() instead.
  3305. */
  3306. LWS_VISIBLE LWS_EXTERN void
  3307. lws_cancel_service_pt(struct lws *wsi);
  3308. /**
  3309. * lws_cancel_service() - Cancel wait for new pending socket activity
  3310. * \param context: Websocket context
  3311. *
  3312. * This function creates an immediate "synchronous interrupt" to the lws poll()
  3313. * wait or event loop. As soon as possible in the serialzed service sequencing,
  3314. * a LWS_CALLBACK_EVENT_WAIT_CANCELLED callback is sent to every protocol on
  3315. * every vhost.
  3316. *
  3317. * lws_cancel_service() may be called from another thread while the context
  3318. * exists, and its effect will be immediately serialized.
  3319. */
  3320. LWS_VISIBLE LWS_EXTERN void
  3321. lws_cancel_service(struct lws_context *context);
  3322. /**
  3323. * lws_service_fd() - Service polled socket with something waiting
  3324. * \param context: Websocket context
  3325. * \param pollfd: The pollfd entry describing the socket fd and which events
  3326. * happened, or NULL to tell lws to do only timeout servicing.
  3327. *
  3328. * This function takes a pollfd that has POLLIN or POLLOUT activity and
  3329. * services it according to the state of the associated
  3330. * struct lws.
  3331. *
  3332. * The one call deals with all "service" that might happen on a socket
  3333. * including listen accepts, http files as well as websocket protocol.
  3334. *
  3335. * If a pollfd says it has something, you can just pass it to
  3336. * lws_service_fd() whether it is a socket handled by lws or not.
  3337. * If it sees it is a lws socket, the traffic will be handled and
  3338. * pollfd->revents will be zeroed now.
  3339. *
  3340. * If the socket is foreign to lws, it leaves revents alone. So you can
  3341. * see if you should service yourself by checking the pollfd revents
  3342. * after letting lws try to service it.
  3343. *
  3344. * You should also call this with pollfd = NULL to just allow the
  3345. * once-per-second global timeout checks; if less than a second since the last
  3346. * check it returns immediately then.
  3347. */
  3348. LWS_VISIBLE LWS_EXTERN int
  3349. lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd);
  3350. /**
  3351. * lws_service_fd_tsi() - Service polled socket in specific service thread
  3352. * \param context: Websocket context
  3353. * \param pollfd: The pollfd entry describing the socket fd and which events
  3354. * happened.
  3355. * \param tsi: thread service index
  3356. *
  3357. * Same as lws_service_fd() but used with multiple service threads
  3358. */
  3359. LWS_VISIBLE LWS_EXTERN int
  3360. lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd,
  3361. int tsi);
  3362. /**
  3363. * lws_service_adjust_timeout() - Check for any connection needing forced service
  3364. * \param context: Websocket context
  3365. * \param timeout_ms: The original poll timeout value. You can just set this
  3366. * to 1 if you don't really have a poll timeout.
  3367. * \param tsi: thread service index
  3368. *
  3369. * Under some conditions connections may need service even though there is no
  3370. * pending network action on them, this is "forced service". For default
  3371. * poll() and libuv / libev, the library takes care of calling this and
  3372. * dealing with it for you. But for external poll() integration, you need
  3373. * access to the apis.
  3374. *
  3375. * If anybody needs "forced service", returned timeout is zero. In that case,
  3376. * you can call lws_service_tsi() with a timeout of -1 to only service
  3377. * guys who need forced service.
  3378. */
  3379. LWS_VISIBLE LWS_EXTERN int
  3380. lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
  3381. /* Backwards compatibility */
  3382. #define lws_plat_service_tsi lws_service_tsi
  3383. LWS_VISIBLE LWS_EXTERN int
  3384. lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
  3385. ///@}
  3386. /*! \defgroup http HTTP
  3387. Modules related to handling HTTP
  3388. */
  3389. //@{
  3390. /*! \defgroup httpft HTTP File transfer
  3391. * \ingroup http
  3392. APIs for sending local files in response to HTTP requests
  3393. */
  3394. //@{
  3395. /**
  3396. * lws_get_mimetype() - Determine mimetype to use from filename
  3397. *
  3398. * \param file: filename
  3399. * \param m: NULL, or mount context
  3400. *
  3401. * This uses a canned list of known filetypes first, if no match and m is
  3402. * non-NULL, then tries a list of per-mount file suffix to mimtype mappings.
  3403. *
  3404. * Returns either NULL or a pointer to the mimetype matching the file.
  3405. */
  3406. LWS_VISIBLE LWS_EXTERN const char *
  3407. lws_get_mimetype(const char *file, const struct lws_http_mount *m);
  3408. /**
  3409. * lws_serve_http_file() - Send a file back to the client using http
  3410. * \param wsi: Websocket instance (available from user callback)
  3411. * \param file: The file to issue over http
  3412. * \param content_type: The http content type, eg, text/html
  3413. * \param other_headers: NULL or pointer to header string
  3414. * \param other_headers_len: length of the other headers if non-NULL
  3415. *
  3416. * This function is intended to be called from the callback in response
  3417. * to http requests from the client. It allows the callback to issue
  3418. * local files down the http link in a single step.
  3419. *
  3420. * Returning <0 indicates error and the wsi should be closed. Returning
  3421. * >0 indicates the file was completely sent and
  3422. * lws_http_transaction_completed() called on the wsi (and close if != 0)
  3423. * ==0 indicates the file transfer is started and needs more service later,
  3424. * the wsi should be left alone.
  3425. */
  3426. LWS_VISIBLE LWS_EXTERN int
  3427. lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
  3428. const char *other_headers, int other_headers_len);
  3429. LWS_VISIBLE LWS_EXTERN int
  3430. lws_serve_http_file_fragment(struct lws *wsi);
  3431. //@}
  3432. enum http_status {
  3433. HTTP_STATUS_CONTINUE = 100,
  3434. HTTP_STATUS_OK = 200,
  3435. HTTP_STATUS_NO_CONTENT = 204,
  3436. HTTP_STATUS_PARTIAL_CONTENT = 206,
  3437. HTTP_STATUS_MOVED_PERMANENTLY = 301,
  3438. HTTP_STATUS_FOUND = 302,
  3439. HTTP_STATUS_SEE_OTHER = 303,
  3440. HTTP_STATUS_NOT_MODIFIED = 304,
  3441. HTTP_STATUS_BAD_REQUEST = 400,
  3442. HTTP_STATUS_UNAUTHORIZED,
  3443. HTTP_STATUS_PAYMENT_REQUIRED,
  3444. HTTP_STATUS_FORBIDDEN,
  3445. HTTP_STATUS_NOT_FOUND,
  3446. HTTP_STATUS_METHOD_NOT_ALLOWED,
  3447. HTTP_STATUS_NOT_ACCEPTABLE,
  3448. HTTP_STATUS_PROXY_AUTH_REQUIRED,
  3449. HTTP_STATUS_REQUEST_TIMEOUT,
  3450. HTTP_STATUS_CONFLICT,
  3451. HTTP_STATUS_GONE,
  3452. HTTP_STATUS_LENGTH_REQUIRED,
  3453. HTTP_STATUS_PRECONDITION_FAILED,
  3454. HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
  3455. HTTP_STATUS_REQ_URI_TOO_LONG,
  3456. HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
  3457. HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
  3458. HTTP_STATUS_EXPECTATION_FAILED,
  3459. HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
  3460. HTTP_STATUS_NOT_IMPLEMENTED,
  3461. HTTP_STATUS_BAD_GATEWAY,
  3462. HTTP_STATUS_SERVICE_UNAVAILABLE,
  3463. HTTP_STATUS_GATEWAY_TIMEOUT,
  3464. HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
  3465. };
  3466. /*! \defgroup html-chunked-substitution HTML Chunked Substitution
  3467. * \ingroup http
  3468. *
  3469. * ##HTML chunked Substitution
  3470. *
  3471. * APIs for receiving chunks of text, replacing a set of variable names via
  3472. * a callback, and then prepending and appending HTML chunked encoding
  3473. * headers.
  3474. */
  3475. //@{
  3476. struct lws_process_html_args {
  3477. char *p; /**< pointer to the buffer containing the data */
  3478. int len; /**< length of the original data at p */
  3479. int max_len; /**< maximum length we can grow the data to */
  3480. int final; /**< set if this is the last chunk of the file */
  3481. int chunked; /**< 0 == unchunked, 1 == produce chunk headers (incompatible with HTTP/2) */
  3482. };
  3483. typedef const char *(*lws_process_html_state_cb)(void *data, int index);
  3484. struct lws_process_html_state {
  3485. char *start; /**< pointer to start of match */
  3486. char swallow[16]; /**< matched character buffer */
  3487. int pos; /**< position in match */
  3488. void *data; /**< opaque pointer */
  3489. const char * const *vars; /**< list of variable names */
  3490. int count_vars; /**< count of variable names */
  3491. lws_process_html_state_cb replace; /**< called on match to perform substitution */
  3492. };
  3493. /*! lws_chunked_html_process() - generic chunked substitution
  3494. * \param args: buffer to process using chunked encoding
  3495. * \param s: current processing state
  3496. */
  3497. LWS_VISIBLE LWS_EXTERN int
  3498. lws_chunked_html_process(struct lws_process_html_args *args,
  3499. struct lws_process_html_state *s);
  3500. //@}
  3501. /** \defgroup HTTP-headers-read HTTP headers: read
  3502. * \ingroup http
  3503. *
  3504. * ##HTTP header releated functions
  3505. *
  3506. * In lws the client http headers are temporarily stored in a pool, only for the
  3507. * duration of the http part of the handshake. It's because in most cases,
  3508. * the header content is ignored for the whole rest of the connection lifetime
  3509. * and would then just be taking up space needlessly.
  3510. *
  3511. * During LWS_CALLBACK_HTTP when the URI path is delivered is the last time
  3512. * the http headers are still allocated, you can use these apis then to
  3513. * look at and copy out interesting header content (cookies, etc)
  3514. *
  3515. * Notice that the header total length reported does not include a terminating
  3516. * '\0', however you must allocate for it when using the _copy apis. So the
  3517. * length reported for a header containing "123" is 3, but you must provide
  3518. * a buffer of length 4 so that "123\0" may be copied into it, or the copy
  3519. * will fail with a nonzero return code.
  3520. *
  3521. * In the special case of URL arguments, like ?x=1&y=2, the arguments are
  3522. * stored in a token named for the method, eg, WSI_TOKEN_GET_URI if it
  3523. * was a GET or WSI_TOKEN_POST_URI if POST. You can check the total
  3524. * length to confirm the method.
  3525. *
  3526. * For URL arguments, each argument is stored urldecoded in a "fragment", so
  3527. * you can use the fragment-aware api lws_hdr_copy_fragment() to access each
  3528. * argument in turn: the fragments contain urldecoded strings like x=1 or y=2.
  3529. *
  3530. * As a convenience, lws has an api that will find the fragment with a
  3531. * given name= part, lws_get_urlarg_by_name().
  3532. */
  3533. ///@{
  3534. /** struct lws_tokens
  3535. * you need these to look at headers that have been parsed if using the
  3536. * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
  3537. * list below is absent, .token = NULL and len = 0. Otherwise .token
  3538. * points to .len chars containing that header content.
  3539. */
  3540. struct lws_tokens {
  3541. char *token; /**< pointer to start of the token */
  3542. int len; /**< length of the token's value */
  3543. };
  3544. /* enum lws_token_indexes
  3545. * these have to be kept in sync with lextable.h / minilex.c
  3546. *
  3547. * NOTE: These public enums are part of the abi. If you want to add one,
  3548. * add it at where specified so existing users are unaffected.
  3549. */
  3550. enum lws_token_indexes {
  3551. WSI_TOKEN_GET_URI = 0,
  3552. WSI_TOKEN_POST_URI = 1,
  3553. WSI_TOKEN_OPTIONS_URI = 2,
  3554. WSI_TOKEN_HOST = 3,
  3555. WSI_TOKEN_CONNECTION = 4,
  3556. WSI_TOKEN_UPGRADE = 5,
  3557. WSI_TOKEN_ORIGIN = 6,
  3558. WSI_TOKEN_DRAFT = 7,
  3559. WSI_TOKEN_CHALLENGE = 8,
  3560. WSI_TOKEN_EXTENSIONS = 9,
  3561. WSI_TOKEN_KEY1 = 10,
  3562. WSI_TOKEN_KEY2 = 11,
  3563. WSI_TOKEN_PROTOCOL = 12,
  3564. WSI_TOKEN_ACCEPT = 13,
  3565. WSI_TOKEN_NONCE = 14,
  3566. WSI_TOKEN_HTTP = 15,
  3567. WSI_TOKEN_HTTP2_SETTINGS = 16,
  3568. WSI_TOKEN_HTTP_ACCEPT = 17,
  3569. WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
  3570. WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
  3571. WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
  3572. WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
  3573. WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
  3574. WSI_TOKEN_HTTP_PRAGMA = 23,
  3575. WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
  3576. WSI_TOKEN_HTTP_AUTHORIZATION = 25,
  3577. WSI_TOKEN_HTTP_COOKIE = 26,
  3578. WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
  3579. WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
  3580. WSI_TOKEN_HTTP_DATE = 29,
  3581. WSI_TOKEN_HTTP_RANGE = 30,
  3582. WSI_TOKEN_HTTP_REFERER = 31,
  3583. WSI_TOKEN_KEY = 32,
  3584. WSI_TOKEN_VERSION = 33,
  3585. WSI_TOKEN_SWORIGIN = 34,
  3586. WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
  3587. WSI_TOKEN_HTTP_COLON_METHOD = 36,
  3588. WSI_TOKEN_HTTP_COLON_PATH = 37,
  3589. WSI_TOKEN_HTTP_COLON_SCHEME = 38,
  3590. WSI_TOKEN_HTTP_COLON_STATUS = 39,
  3591. WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
  3592. WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
  3593. WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
  3594. WSI_TOKEN_HTTP_AGE = 43,
  3595. WSI_TOKEN_HTTP_ALLOW = 44,
  3596. WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
  3597. WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
  3598. WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
  3599. WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
  3600. WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
  3601. WSI_TOKEN_HTTP_ETAG = 50,
  3602. WSI_TOKEN_HTTP_EXPECT = 51,
  3603. WSI_TOKEN_HTTP_EXPIRES = 52,
  3604. WSI_TOKEN_HTTP_FROM = 53,
  3605. WSI_TOKEN_HTTP_IF_MATCH = 54,
  3606. WSI_TOKEN_HTTP_IF_RANGE = 55,
  3607. WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
  3608. WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
  3609. WSI_TOKEN_HTTP_LINK = 58,
  3610. WSI_TOKEN_HTTP_LOCATION = 59,
  3611. WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
  3612. WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
  3613. WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
  3614. WSI_TOKEN_HTTP_REFRESH = 63,
  3615. WSI_TOKEN_HTTP_RETRY_AFTER = 64,
  3616. WSI_TOKEN_HTTP_SERVER = 65,
  3617. WSI_TOKEN_HTTP_SET_COOKIE = 66,
  3618. WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
  3619. WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
  3620. WSI_TOKEN_HTTP_USER_AGENT = 69,
  3621. WSI_TOKEN_HTTP_VARY = 70,
  3622. WSI_TOKEN_HTTP_VIA = 71,
  3623. WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
  3624. WSI_TOKEN_PATCH_URI = 73,
  3625. WSI_TOKEN_PUT_URI = 74,
  3626. WSI_TOKEN_DELETE_URI = 75,
  3627. WSI_TOKEN_HTTP_URI_ARGS = 76,
  3628. WSI_TOKEN_PROXY = 77,
  3629. WSI_TOKEN_HTTP_X_REAL_IP = 78,
  3630. WSI_TOKEN_HTTP1_0 = 79,
  3631. WSI_TOKEN_X_FORWARDED_FOR = 80,
  3632. WSI_TOKEN_CONNECT = 81,
  3633. WSI_TOKEN_HEAD_URI = 82,
  3634. WSI_TOKEN_TE = 83,
  3635. WSI_TOKEN_REPLAY_NONCE = 84,
  3636. WSI_TOKEN_COLON_PROTOCOL = 85,
  3637. WSI_TOKEN_X_AUTH_TOKEN = 86,
  3638. /****** add new things just above ---^ ******/
  3639. /* use token storage to stash these internally, not for
  3640. * user use */
  3641. _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
  3642. _WSI_TOKEN_CLIENT_PEER_ADDRESS,
  3643. _WSI_TOKEN_CLIENT_URI,
  3644. _WSI_TOKEN_CLIENT_HOST,
  3645. _WSI_TOKEN_CLIENT_ORIGIN,
  3646. _WSI_TOKEN_CLIENT_METHOD,
  3647. _WSI_TOKEN_CLIENT_IFACE,
  3648. _WSI_TOKEN_CLIENT_ALPN,
  3649. /* always last real token index*/
  3650. WSI_TOKEN_COUNT,
  3651. /* parser state additions, no storage associated */
  3652. WSI_TOKEN_NAME_PART,
  3653. WSI_TOKEN_SKIPPING,
  3654. WSI_TOKEN_SKIPPING_SAW_CR,
  3655. WSI_PARSING_COMPLETE,
  3656. WSI_INIT_TOKEN_MUXURL,
  3657. };
  3658. struct lws_token_limits {
  3659. unsigned short token_limit[WSI_TOKEN_COUNT]; /**< max chars for this token */
  3660. };
  3661. /**
  3662. * lws_token_to_string() - returns a textual representation of a hdr token index
  3663. *
  3664. * \param token: token index
  3665. */
  3666. LWS_VISIBLE LWS_EXTERN const unsigned char *
  3667. lws_token_to_string(enum lws_token_indexes token);
  3668. /**
  3669. * lws_hdr_total_length: report length of all fragments of a header totalled up
  3670. * The returned length does not include the space for a
  3671. * terminating '\0'
  3672. *
  3673. * \param wsi: websocket connection
  3674. * \param h: which header index we are interested in
  3675. */
  3676. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3677. lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
  3678. /**
  3679. * lws_hdr_fragment_length: report length of a single fragment of a header
  3680. * The returned length does not include the space for a
  3681. * terminating '\0'
  3682. *
  3683. * \param wsi: websocket connection
  3684. * \param h: which header index we are interested in
  3685. * \param frag_idx: which fragment of h we want to get the length of
  3686. */
  3687. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3688. lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx);
  3689. /**
  3690. * lws_hdr_copy() - copy a single fragment of the given header to a buffer
  3691. * The buffer length len must include space for an additional
  3692. * terminating '\0', or it will fail returning -1.
  3693. *
  3694. * \param wsi: websocket connection
  3695. * \param dest: destination buffer
  3696. * \param len: length of destination buffer
  3697. * \param h: which header index we are interested in
  3698. *
  3699. * copies the whole, aggregated header, even if it was delivered in
  3700. * several actual headers piece by piece
  3701. */
  3702. LWS_VISIBLE LWS_EXTERN int
  3703. lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
  3704. /**
  3705. * lws_hdr_copy_fragment() - copy a single fragment of the given header to a buffer
  3706. * The buffer length len must include space for an additional
  3707. * terminating '\0', or it will fail returning -1.
  3708. * If the requested fragment index is not present, it fails
  3709. * returning -1.
  3710. *
  3711. * \param wsi: websocket connection
  3712. * \param dest: destination buffer
  3713. * \param len: length of destination buffer
  3714. * \param h: which header index we are interested in
  3715. * \param frag_idx: which fragment of h we want to copy
  3716. *
  3717. * Normally this is only useful
  3718. * to parse URI arguments like ?x=1&y=2, token index WSI_TOKEN_HTTP_URI_ARGS
  3719. * fragment 0 will contain "x=1" and fragment 1 "y=2"
  3720. */
  3721. LWS_VISIBLE LWS_EXTERN int
  3722. lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
  3723. enum lws_token_indexes h, int frag_idx);
  3724. /**
  3725. * lws_get_urlarg_by_name() - return pointer to arg value if present
  3726. * \param wsi: the connection to check
  3727. * \param name: the arg name, like "token="
  3728. * \param buf: the buffer to receive the urlarg (including the name= part)
  3729. * \param len: the length of the buffer to receive the urlarg
  3730. *
  3731. * Returns NULL if not found or a pointer inside buf to just after the
  3732. * name= part.
  3733. */
  3734. LWS_VISIBLE LWS_EXTERN const char *
  3735. lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
  3736. ///@}
  3737. /*! \defgroup HTTP-headers-create HTTP headers: create
  3738. *
  3739. * ## HTTP headers: Create
  3740. *
  3741. * These apis allow you to create HTTP response headers in a way compatible with
  3742. * both HTTP/1.x and HTTP/2.
  3743. *
  3744. * They each append to a buffer taking care about the buffer end, which is
  3745. * passed in as a pointer. When data is written to the buffer, the current
  3746. * position p is updated accordingly.
  3747. *
  3748. * All of these apis are LWS_WARN_UNUSED_RESULT as they can run out of space
  3749. * and fail with nonzero return.
  3750. */
  3751. ///@{
  3752. #define LWSAHH_CODE_MASK ((1 << 16) - 1)
  3753. #define LWSAHH_FLAG_NO_SERVER_NAME (1 << 30)
  3754. /**
  3755. * lws_add_http_header_status() - add the HTTP response status code
  3756. *
  3757. * \param wsi: the connection to check
  3758. * \param code: an HTTP code like 200, 404 etc (see enum http_status)
  3759. * \param p: pointer to current position in buffer pointer
  3760. * \param end: pointer to end of buffer
  3761. *
  3762. * Adds the initial response code, so should be called first.
  3763. *
  3764. * Code may additionally take OR'd flags:
  3765. *
  3766. * LWSAHH_FLAG_NO_SERVER_NAME: don't apply server name header this time
  3767. */
  3768. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3769. lws_add_http_header_status(struct lws *wsi,
  3770. unsigned int code, unsigned char **p,
  3771. unsigned char *end);
  3772. /**
  3773. * lws_add_http_header_by_name() - append named header and value
  3774. *
  3775. * \param wsi: the connection to check
  3776. * \param name: the hdr name, like "my-header"
  3777. * \param value: the value after the = for this header
  3778. * \param length: the length of the value
  3779. * \param p: pointer to current position in buffer pointer
  3780. * \param end: pointer to end of buffer
  3781. *
  3782. * Appends name: value to the headers
  3783. */
  3784. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3785. lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
  3786. const unsigned char *value, int length,
  3787. unsigned char **p, unsigned char *end);
  3788. /**
  3789. * lws_add_http_header_by_token() - append given header and value
  3790. *
  3791. * \param wsi: the connection to check
  3792. * \param token: the token index for the hdr
  3793. * \param value: the value after the = for this header
  3794. * \param length: the length of the value
  3795. * \param p: pointer to current position in buffer pointer
  3796. * \param end: pointer to end of buffer
  3797. *
  3798. * Appends name=value to the headers, but is able to take advantage of better
  3799. * HTTP/2 coding mechanisms where possible.
  3800. */
  3801. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3802. lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
  3803. const unsigned char *value, int length,
  3804. unsigned char **p, unsigned char *end);
  3805. /**
  3806. * lws_add_http_header_content_length() - append content-length helper
  3807. *
  3808. * \param wsi: the connection to check
  3809. * \param content_length: the content length to use
  3810. * \param p: pointer to current position in buffer pointer
  3811. * \param end: pointer to end of buffer
  3812. *
  3813. * Appends content-length: content_length to the headers
  3814. */
  3815. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3816. lws_add_http_header_content_length(struct lws *wsi,
  3817. lws_filepos_t content_length,
  3818. unsigned char **p, unsigned char *end);
  3819. /**
  3820. * lws_finalize_http_header() - terminate header block
  3821. *
  3822. * \param wsi: the connection to check
  3823. * \param p: pointer to current position in buffer pointer
  3824. * \param end: pointer to end of buffer
  3825. *
  3826. * Indicates no more headers will be added
  3827. */
  3828. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3829. lws_finalize_http_header(struct lws *wsi, unsigned char **p,
  3830. unsigned char *end);
  3831. /**
  3832. * lws_finalize_write_http_header() - Helper finializing and writing http headers
  3833. *
  3834. * \param wsi: the connection to check
  3835. * \param start: pointer to the start of headers in the buffer, eg &buf[LWS_PRE]
  3836. * \param p: pointer to current position in buffer pointer
  3837. * \param end: pointer to end of buffer
  3838. *
  3839. * Terminates the headers correctly accoring to the protocol in use (h1 / h2)
  3840. * and writes the headers. Returns nonzero for error.
  3841. */
  3842. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3843. lws_finalize_write_http_header(struct lws *wsi, unsigned char *start,
  3844. unsigned char **p, unsigned char *end);
  3845. #define LWS_ILLEGAL_HTTP_CONTENT_LEN ((lws_filepos_t)-1ll)
  3846. /**
  3847. * lws_add_http_common_headers() - Helper preparing common http headers
  3848. *
  3849. * \param wsi: the connection to check
  3850. * \param code: an HTTP code like 200, 404 etc (see enum http_status)
  3851. * \param content_type: the content type, like "text/html"
  3852. * \param content_len: the content length, in bytes
  3853. * \param p: pointer to current position in buffer pointer
  3854. * \param end: pointer to end of buffer
  3855. *
  3856. * Adds the initial response code, so should be called first.
  3857. *
  3858. * Code may additionally take OR'd flags:
  3859. *
  3860. * LWSAHH_FLAG_NO_SERVER_NAME: don't apply server name header this time
  3861. *
  3862. * This helper just calls public apis to simplify adding headers that are
  3863. * commonly needed. If it doesn't fit your case, or you want to add additional
  3864. * headers just call the public apis directly yourself for what you want.
  3865. *
  3866. * You can miss out the content length header by providing the constant
  3867. * LWS_ILLEGAL_HTTP_CONTENT_LEN for the content_len.
  3868. *
  3869. * It does not call lws_finalize_http_header(), to allow you to add further
  3870. * headers after calling this. You will need to call that yourself at the end.
  3871. */
  3872. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3873. lws_add_http_common_headers(struct lws *wsi, unsigned int code,
  3874. const char *content_type, lws_filepos_t content_len,
  3875. unsigned char **p, unsigned char *end);
  3876. ///@}
  3877. /** \defgroup form-parsing Form Parsing
  3878. * \ingroup http
  3879. * ##POSTed form parsing functions
  3880. *
  3881. * These lws_spa (stateful post arguments) apis let you parse and urldecode
  3882. * POSTed form arguments, both using simple urlencoded and multipart transfer
  3883. * encoding.
  3884. *
  3885. * It's capable of handling file uploads as well a named input parsing,
  3886. * and the apis are the same for both form upload styles.
  3887. *
  3888. * You feed it a list of parameter names and it creates pointers to the
  3889. * urldecoded arguments: file upload parameters pass the file data in chunks to
  3890. * a user-supplied callback as they come.
  3891. *
  3892. * Since it's stateful, it handles the incoming data needing more than one
  3893. * POST_BODY callback and has no limit on uploaded file size.
  3894. */
  3895. ///@{
  3896. /** enum lws_spa_fileupload_states */
  3897. enum lws_spa_fileupload_states {
  3898. LWS_UFS_CONTENT,
  3899. /**< a chunk of file content has arrived */
  3900. LWS_UFS_FINAL_CONTENT,
  3901. /**< the last chunk (possibly zero length) of file content has arrived */
  3902. LWS_UFS_OPEN
  3903. /**< a new file is starting to arrive */
  3904. };
  3905. /**
  3906. * lws_spa_fileupload_cb() - callback to receive file upload data
  3907. *
  3908. * \param data: opt_data pointer set in lws_spa_create
  3909. * \param name: name of the form field being uploaded
  3910. * \param filename: original filename from client
  3911. * \param buf: start of data to receive
  3912. * \param len: length of data to receive
  3913. * \param state: information about how this call relates to file
  3914. *
  3915. * Notice name and filename shouldn't be trusted, as they are passed from
  3916. * HTTP provided by the client.
  3917. */
  3918. typedef int (*lws_spa_fileupload_cb)(void *data, const char *name,
  3919. const char *filename, char *buf, int len,
  3920. enum lws_spa_fileupload_states state);
  3921. /** struct lws_spa - opaque urldecode parser capable of handling multipart
  3922. * and file uploads */
  3923. struct lws_spa;
  3924. /**
  3925. * lws_spa_create() - create urldecode parser
  3926. *
  3927. * \param wsi: lws connection (used to find Content Type)
  3928. * \param param_names: array of form parameter names, like "username"
  3929. * \param count_params: count of param_names
  3930. * \param max_storage: total amount of form parameter values we can store
  3931. * \param opt_cb: NULL, or callback to receive file upload data.
  3932. * \param opt_data: NULL, or user pointer provided to opt_cb.
  3933. *
  3934. * Creates a urldecode parser and initializes it.
  3935. *
  3936. * opt_cb can be NULL if you just want normal name=value parsing, however
  3937. * if one or more entries in your form are bulk data (file transfer), you
  3938. * can provide this callback and filter on the name callback parameter to
  3939. * treat that urldecoded data separately. The callback should return -1
  3940. * in case of fatal error, and 0 if OK.
  3941. */
  3942. LWS_VISIBLE LWS_EXTERN struct lws_spa *
  3943. lws_spa_create(struct lws *wsi, const char * const *param_names,
  3944. int count_params, int max_storage, lws_spa_fileupload_cb opt_cb,
  3945. void *opt_data);
  3946. /**
  3947. * lws_spa_process() - parses a chunk of input data
  3948. *
  3949. * \param spa: the parser object previously created
  3950. * \param in: incoming, urlencoded data
  3951. * \param len: count of bytes valid at \param in
  3952. */
  3953. LWS_VISIBLE LWS_EXTERN int
  3954. lws_spa_process(struct lws_spa *spa, const char *in, int len);
  3955. /**
  3956. * lws_spa_finalize() - indicate incoming data completed
  3957. *
  3958. * \param spa: the parser object previously created
  3959. */
  3960. LWS_VISIBLE LWS_EXTERN int
  3961. lws_spa_finalize(struct lws_spa *spa);
  3962. /**
  3963. * lws_spa_get_length() - return length of parameter value
  3964. *
  3965. * \param spa: the parser object previously created
  3966. * \param n: parameter ordinal to return length of value for
  3967. */
  3968. LWS_VISIBLE LWS_EXTERN int
  3969. lws_spa_get_length(struct lws_spa *spa, int n);
  3970. /**
  3971. * lws_spa_get_string() - return pointer to parameter value
  3972. * \param spa: the parser object previously created
  3973. * \param n: parameter ordinal to return pointer to value for
  3974. */
  3975. LWS_VISIBLE LWS_EXTERN const char *
  3976. lws_spa_get_string(struct lws_spa *spa, int n);
  3977. /**
  3978. * lws_spa_destroy() - destroy parser object
  3979. *
  3980. * \param spa: the parser object previously created
  3981. */
  3982. LWS_VISIBLE LWS_EXTERN int
  3983. lws_spa_destroy(struct lws_spa *spa);
  3984. ///@}
  3985. /*! \defgroup urlendec Urlencode and Urldecode
  3986. * \ingroup http
  3987. *
  3988. * ##HTML chunked Substitution
  3989. *
  3990. * APIs for receiving chunks of text, replacing a set of variable names via
  3991. * a callback, and then prepending and appending HTML chunked encoding
  3992. * headers.
  3993. */
  3994. //@{
  3995. /**
  3996. * lws_urlencode() - like strncpy but with urlencoding
  3997. *
  3998. * \param escaped: output buffer
  3999. * \param string: input buffer ('/0' terminated)
  4000. * \param len: output buffer max length
  4001. *
  4002. * Because urlencoding expands the output string, it's not
  4003. * possible to do it in-place, ie, with escaped == string
  4004. */
  4005. LWS_VISIBLE LWS_EXTERN const char *
  4006. lws_urlencode(char *escaped, const char *string, int len);
  4007. /*
  4008. * URLDECODE 1 / 2
  4009. *
  4010. * This simple urldecode only operates until the first '\0' and requires the
  4011. * data to exist all at once
  4012. */
  4013. /**
  4014. * lws_urldecode() - like strncpy but with urldecoding
  4015. *
  4016. * \param string: output buffer
  4017. * \param escaped: input buffer ('\0' terminated)
  4018. * \param len: output buffer max length
  4019. *
  4020. * This is only useful for '\0' terminated strings
  4021. *
  4022. * Since urldecoding only shrinks the output string, it is possible to
  4023. * do it in-place, ie, string == escaped
  4024. *
  4025. * Returns 0 if completed OK or nonzero for urldecode violation (non-hex chars
  4026. * where hex required, etc)
  4027. */
  4028. LWS_VISIBLE LWS_EXTERN int
  4029. lws_urldecode(char *string, const char *escaped, int len);
  4030. ///@}
  4031. /**
  4032. * lws_return_http_status() - Return simple http status
  4033. * \param wsi: Websocket instance (available from user callback)
  4034. * \param code: Status index, eg, 404
  4035. * \param html_body: User-readable HTML description < 1KB, or NULL
  4036. *
  4037. * Helper to report HTTP errors back to the client cleanly and
  4038. * consistently
  4039. */
  4040. LWS_VISIBLE LWS_EXTERN int
  4041. lws_return_http_status(struct lws *wsi, unsigned int code,
  4042. const char *html_body);
  4043. /**
  4044. * lws_http_redirect() - write http redirect out on wsi
  4045. *
  4046. * \param wsi: websocket connection
  4047. * \param code: HTTP response code (eg, 301)
  4048. * \param loc: where to redirect to
  4049. * \param len: length of loc
  4050. * \param p: pointer current position in buffer (updated as we write)
  4051. * \param end: pointer to end of buffer
  4052. *
  4053. * Returns amount written, or < 0 indicating fatal write failure.
  4054. */
  4055. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4056. lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
  4057. unsigned char **p, unsigned char *end);
  4058. /**
  4059. * lws_http_transaction_completed() - wait for new http transaction or close
  4060. * \param wsi: websocket connection
  4061. *
  4062. * Returns 1 if the HTTP connection must close now
  4063. * Returns 0 and resets connection to wait for new HTTP header /
  4064. * transaction if possible
  4065. */
  4066. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  4067. lws_http_transaction_completed(struct lws *wsi);
  4068. ///@}
  4069. /*! \defgroup pur Sanitize / purify SQL and JSON helpers
  4070. *
  4071. * ##Sanitize / purify SQL and JSON helpers
  4072. *
  4073. * APIs for escaping untrusted JSON and SQL safely before use
  4074. */
  4075. //@{
  4076. /**
  4077. * lws_sql_purify() - like strncpy but with escaping for sql quotes
  4078. *
  4079. * \param escaped: output buffer
  4080. * \param string: input buffer ('/0' terminated)
  4081. * \param len: output buffer max length
  4082. *
  4083. * Because escaping expands the output string, it's not
  4084. * possible to do it in-place, ie, with escaped == string
  4085. */
  4086. LWS_VISIBLE LWS_EXTERN const char *
  4087. lws_sql_purify(char *escaped, const char *string, int len);
  4088. /**
  4089. * lws_json_purify() - like strncpy but with escaping for json chars
  4090. *
  4091. * \param escaped: output buffer
  4092. * \param string: input buffer ('/0' terminated)
  4093. * \param len: output buffer max length
  4094. *
  4095. * Because escaping expands the output string, it's not
  4096. * possible to do it in-place, ie, with escaped == string
  4097. */
  4098. LWS_VISIBLE LWS_EXTERN const char *
  4099. lws_json_purify(char *escaped, const char *string, int len);
  4100. /**
  4101. * lws_filename_purify_inplace() - replace scary filename chars with underscore
  4102. *
  4103. * \param filename: filename to be purified
  4104. *
  4105. * Replace scary characters in the filename (it should not be a path)
  4106. * with underscore, so it's safe to use.
  4107. */
  4108. LWS_VISIBLE LWS_EXTERN void
  4109. lws_filename_purify_inplace(char *filename);
  4110. LWS_VISIBLE LWS_EXTERN int
  4111. lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
  4112. int len);
  4113. LWS_VISIBLE LWS_EXTERN int
  4114. lws_plat_write_file(const char *filename, void *buf, int len);
  4115. LWS_VISIBLE LWS_EXTERN int
  4116. lws_plat_read_file(const char *filename, void *buf, int len);
  4117. LWS_VISIBLE LWS_EXTERN int
  4118. lws_plat_recommended_rsa_bits(void);
  4119. ///@}
  4120. /*! \defgroup uv libuv helpers
  4121. *
  4122. * ##libuv helpers
  4123. *
  4124. * APIs specific to libuv event loop itegration
  4125. */
  4126. ///@{
  4127. #ifdef LWS_WITH_LIBUV
  4128. /*
  4129. * Any direct libuv allocations in lws protocol handlers must participate in the
  4130. * lws reference counting scheme. Two apis are provided:
  4131. *
  4132. * - lws_libuv_static_refcount_add(handle, context) to mark the handle with
  4133. * a pointer to the context and increment the global uv object counter
  4134. *
  4135. * - lws_libuv_static_refcount_del() which should be used as the close callback
  4136. * for your own libuv objects declared in the protocol scope.
  4137. *
  4138. * Using the apis allows lws to detach itself from a libuv loop completely
  4139. * cleanly and at the moment all of its libuv objects have completed close.
  4140. */
  4141. LWS_VISIBLE LWS_EXTERN uv_loop_t *
  4142. lws_uv_getloop(struct lws_context *context, int tsi);
  4143. LWS_VISIBLE LWS_EXTERN void
  4144. lws_libuv_static_refcount_add(uv_handle_t *, struct lws_context *context);
  4145. LWS_VISIBLE LWS_EXTERN void
  4146. lws_libuv_static_refcount_del(uv_handle_t *);
  4147. #endif /* LWS_WITH_LIBUV */
  4148. #if defined(LWS_WITH_ESP32)
  4149. #define lws_libuv_static_refcount_add(_a, _b)
  4150. #define lws_libuv_static_refcount_del NULL
  4151. #endif
  4152. ///@}
  4153. /*! \defgroup timeout Connection timeouts
  4154. APIs related to setting connection timeouts
  4155. */
  4156. //@{
  4157. /*
  4158. * NOTE: These public enums are part of the abi. If you want to add one,
  4159. * add it at where specified so existing users are unaffected.
  4160. */
  4161. enum pending_timeout {
  4162. NO_PENDING_TIMEOUT = 0,
  4163. PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE = 1,
  4164. PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE = 2,
  4165. PENDING_TIMEOUT_ESTABLISH_WITH_SERVER = 3,
  4166. PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE = 4,
  4167. PENDING_TIMEOUT_AWAITING_PING = 5,
  4168. PENDING_TIMEOUT_CLOSE_ACK = 6,
  4169. PENDING_TIMEOUT_UNUSED1 = 7,
  4170. PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE = 8,
  4171. PENDING_TIMEOUT_SSL_ACCEPT = 9,
  4172. PENDING_TIMEOUT_HTTP_CONTENT = 10,
  4173. PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND = 11,
  4174. PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE = 12,
  4175. PENDING_TIMEOUT_SHUTDOWN_FLUSH = 13,
  4176. PENDING_TIMEOUT_CGI = 14,
  4177. PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE = 15,
  4178. PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING = 16,
  4179. PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG = 17,
  4180. PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD = 18,
  4181. PENDING_TIMEOUT_AWAITING_SOCKS_GREETING_REPLY = 19,
  4182. PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY = 20,
  4183. PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY = 21,
  4184. PENDING_TIMEOUT_KILLED_BY_SSL_INFO = 22,
  4185. PENDING_TIMEOUT_KILLED_BY_PARENT = 23,
  4186. PENDING_TIMEOUT_CLOSE_SEND = 24,
  4187. PENDING_TIMEOUT_HOLDING_AH = 25,
  4188. PENDING_TIMEOUT_UDP_IDLE = 26,
  4189. PENDING_TIMEOUT_CLIENT_CONN_IDLE = 27,
  4190. PENDING_TIMEOUT_LAGGING = 28,
  4191. /****** add new things just above ---^ ******/
  4192. PENDING_TIMEOUT_USER_REASON_BASE = 1000
  4193. };
  4194. #define LWS_TO_KILL_ASYNC -1
  4195. /**< If LWS_TO_KILL_ASYNC is given as the timeout sec in a lws_set_timeout()
  4196. * call, then the connection is marked to be killed at the next timeout
  4197. * check. This is how you should force-close the wsi being serviced if
  4198. * you are doing it outside the callback (where you should close by nonzero
  4199. * return).
  4200. */
  4201. #define LWS_TO_KILL_SYNC -2
  4202. /**< If LWS_TO_KILL_SYNC is given as the timeout sec in a lws_set_timeout()
  4203. * call, then the connection is closed before returning (which may delete
  4204. * the wsi). This should only be used where the wsi being closed is not the
  4205. * wsi currently being serviced.
  4206. */
  4207. /**
  4208. * lws_set_timeout() - marks the wsi as subject to a timeout
  4209. *
  4210. * You will not need this unless you are doing something special
  4211. *
  4212. * \param wsi: Websocket connection instance
  4213. * \param reason: timeout reason
  4214. * \param secs: how many seconds. You may set to LWS_TO_KILL_ASYNC to
  4215. * force the connection to timeout at the next opportunity, or
  4216. * LWS_TO_KILL_SYNC to close it synchronously if you know the
  4217. * wsi is not the one currently being serviced.
  4218. */
  4219. LWS_VISIBLE LWS_EXTERN void
  4220. lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
  4221. #define LWS_SET_TIMER_USEC_CANCEL ((lws_usec_t)-1ll)
  4222. #define LWS_USEC_PER_SEC (1000000ll)
  4223. /**
  4224. * lws_set_timer_usecs() - schedules a callback on the wsi in the future
  4225. *
  4226. * \param wsi: Websocket connection instance
  4227. * \param usecs: LWS_SET_TIMER_USEC_CANCEL removes any existing scheduled
  4228. * callback, otherwise number of microseconds in the future
  4229. * the callback will occur at.
  4230. *
  4231. * NOTE: event loop support for this:
  4232. *
  4233. * default poll() loop: yes
  4234. * libuv event loop: yes
  4235. * libev: not implemented (patch welcome)
  4236. * libevent: not implemented (patch welcome)
  4237. *
  4238. * After the deadline expires, the wsi will get a callback of type
  4239. * LWS_CALLBACK_TIMER and the timer is exhausted. The deadline may be
  4240. * continuously deferred by further calls to lws_set_timer_usecs() with a later
  4241. * deadline, or cancelled by lws_set_timer_usecs(wsi, -1).
  4242. *
  4243. * If the timer should repeat, lws_set_timer_usecs() must be called again from
  4244. * LWS_CALLBACK_TIMER.
  4245. *
  4246. * Accuracy depends on the platform and the load on the event loop or system...
  4247. * all that's guaranteed is the callback will come after the requested wait
  4248. * period.
  4249. */
  4250. LWS_VISIBLE LWS_EXTERN void
  4251. lws_set_timer_usecs(struct lws *wsi, lws_usec_t usecs);
  4252. /*
  4253. * lws_timed_callback_vh_protocol() - calls back a protocol on a vhost after
  4254. * the specified delay
  4255. *
  4256. * \param vh: the vhost to call back
  4257. * \param protocol: the protocol to call back
  4258. * \param reason: callback reason
  4259. * \param secs: how many seconds in the future to do the callback. Set to
  4260. * -1 to cancel the timer callback.
  4261. *
  4262. * Callback the specified protocol with a fake wsi pointing to the specified
  4263. * vhost and protocol, with the specified reason, at the specified time in the
  4264. * future.
  4265. *
  4266. * Returns 0 if OK.
  4267. */
  4268. LWS_VISIBLE LWS_EXTERN int
  4269. lws_timed_callback_vh_protocol(struct lws_vhost *vh,
  4270. const struct lws_protocols *prot,
  4271. int reason, int secs);
  4272. ///@}
  4273. /*! \defgroup sending-data Sending data
  4274. APIs related to writing data on a connection
  4275. */
  4276. //@{
  4277. #if !defined(LWS_SIZEOFPTR)
  4278. #define LWS_SIZEOFPTR ((int)sizeof (void *))
  4279. #endif
  4280. #if defined(__x86_64__)
  4281. #define _LWS_PAD_SIZE 16 /* Intel recommended for best performance */
  4282. #else
  4283. #define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target arch */
  4284. #endif
  4285. #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
  4286. ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
  4287. /* last 2 is for lws-meta */
  4288. #define LWS_PRE _LWS_PAD(4 + 10 + 2)
  4289. /* used prior to 1.7 and retained for backward compatibility */
  4290. #define LWS_SEND_BUFFER_PRE_PADDING LWS_PRE
  4291. #define LWS_SEND_BUFFER_POST_PADDING 0
  4292. #define LWS_WRITE_RAW LWS_WRITE_HTTP
  4293. /*
  4294. * NOTE: These public enums are part of the abi. If you want to add one,
  4295. * add it at where specified so existing users are unaffected.
  4296. */
  4297. enum lws_write_protocol {
  4298. LWS_WRITE_TEXT = 0,
  4299. /**< Send a ws TEXT message,the pointer must have LWS_PRE valid
  4300. * memory behind it. The receiver expects only valid utf-8 in the
  4301. * payload */
  4302. LWS_WRITE_BINARY = 1,
  4303. /**< Send a ws BINARY message, the pointer must have LWS_PRE valid
  4304. * memory behind it. Any sequence of bytes is valid */
  4305. LWS_WRITE_CONTINUATION = 2,
  4306. /**< Continue a previous ws message, the pointer must have LWS_PRE valid
  4307. * memory behind it */
  4308. LWS_WRITE_HTTP = 3,
  4309. /**< Send HTTP content */
  4310. /* LWS_WRITE_CLOSE is handled by lws_close_reason() */
  4311. LWS_WRITE_PING = 5,
  4312. LWS_WRITE_PONG = 6,
  4313. /* Same as write_http but we know this write ends the transaction */
  4314. LWS_WRITE_HTTP_FINAL = 7,
  4315. /* HTTP2 */
  4316. LWS_WRITE_HTTP_HEADERS = 8,
  4317. /**< Send http headers (http2 encodes this payload and LWS_WRITE_HTTP
  4318. * payload differently, http 1.x links also handle this correctly. so
  4319. * to be compatible with both in the future,header response part should
  4320. * be sent using this regardless of http version expected)
  4321. */
  4322. LWS_WRITE_HTTP_HEADERS_CONTINUATION = 9,
  4323. /**< Continuation of http/2 headers
  4324. */
  4325. /****** add new things just above ---^ ******/
  4326. /* flags */
  4327. LWS_WRITE_NO_FIN = 0x40,
  4328. /**< This part of the message is not the end of the message */
  4329. LWS_WRITE_H2_STREAM_END = 0x80,
  4330. /**< Flag indicates this packet should go out with STREAM_END if h2
  4331. * STREAM_END is allowed on DATA or HEADERS.
  4332. */
  4333. LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
  4334. /**< client packet payload goes out on wire unmunged
  4335. * only useful for security tests since normal servers cannot
  4336. * decode the content if used */
  4337. };
  4338. /* used with LWS_CALLBACK_CHILD_WRITE_VIA_PARENT */
  4339. struct lws_write_passthru {
  4340. struct lws *wsi;
  4341. unsigned char *buf;
  4342. size_t len;
  4343. enum lws_write_protocol wp;
  4344. };
  4345. /**
  4346. * lws_write() - Apply protocol then write data to client
  4347. * \param wsi: Websocket instance (available from user callback)
  4348. * \param buf: The data to send. For data being sent on a websocket
  4349. * connection (ie, not default http), this buffer MUST have
  4350. * LWS_PRE bytes valid BEFORE the pointer.
  4351. * This is so the protocol header data can be added in-situ.
  4352. * \param len: Count of the data bytes in the payload starting from buf
  4353. * \param protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
  4354. * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
  4355. * data on a websockets connection. Remember to allow the extra
  4356. * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
  4357. * are used.
  4358. *
  4359. * This function provides the way to issue data back to the client
  4360. * for both http and websocket protocols.
  4361. *
  4362. * IMPORTANT NOTICE!
  4363. *
  4364. * When sending with websocket protocol
  4365. *
  4366. * LWS_WRITE_TEXT,
  4367. * LWS_WRITE_BINARY,
  4368. * LWS_WRITE_CONTINUATION,
  4369. * LWS_WRITE_PING,
  4370. * LWS_WRITE_PONG
  4371. *
  4372. * the send buffer has to have LWS_PRE bytes valid BEFORE
  4373. * the buffer pointer you pass to lws_write().
  4374. *
  4375. * This allows us to add protocol info before and after the data, and send as
  4376. * one packet on the network without payload copying, for maximum efficiency.
  4377. *
  4378. * So for example you need this kind of code to use lws_write with a
  4379. * 128-byte payload
  4380. *
  4381. * char buf[LWS_PRE + 128];
  4382. *
  4383. * // fill your part of the buffer... for example here it's all zeros
  4384. * memset(&buf[LWS_PRE], 0, 128);
  4385. *
  4386. * lws_write(wsi, &buf[LWS_PRE], 128, LWS_WRITE_TEXT);
  4387. *
  4388. * When sending HTTP, with
  4389. *
  4390. * LWS_WRITE_HTTP,
  4391. * LWS_WRITE_HTTP_HEADERS
  4392. * LWS_WRITE_HTTP_FINAL
  4393. *
  4394. * there is no protocol data prepended, and don't need to take care about the
  4395. * LWS_PRE bytes valid before the buffer pointer.
  4396. *
  4397. * LWS_PRE is at least the frame nonce + 2 header + 8 length
  4398. * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off.
  4399. * The example apps no longer use it.
  4400. *
  4401. * Pad LWS_PRE to the CPU word size, so that word references
  4402. * to the address immediately after the padding won't cause an unaligned access
  4403. * error. Sometimes for performance reasons the recommended padding is even
  4404. * larger than sizeof(void *).
  4405. *
  4406. * In the case of sending using websocket protocol, be sure to allocate
  4407. * valid storage before and after buf as explained above. This scheme
  4408. * allows maximum efficiency of sending data and protocol in a single
  4409. * packet while not burdening the user code with any protocol knowledge.
  4410. *
  4411. * Return may be -1 for a fatal error needing connection close, or the
  4412. * number of bytes sent.
  4413. *
  4414. * Truncated Writes
  4415. * ================
  4416. *
  4417. * The OS may not accept everything you asked to write on the connection.
  4418. *
  4419. * Posix defines POLLOUT indication from poll() to show that the connection
  4420. * will accept more write data, but it doesn't specifiy how much. It may just
  4421. * accept one byte of whatever you wanted to send.
  4422. *
  4423. * LWS will buffer the remainder automatically, and send it out autonomously.
  4424. *
  4425. * During that time, WRITABLE callbacks will be suppressed.
  4426. *
  4427. * This is to handle corner cases where unexpectedly the OS refuses what we
  4428. * usually expect it to accept. You should try to send in chunks that are
  4429. * almost always accepted in order to avoid the inefficiency of the buffering.
  4430. */
  4431. LWS_VISIBLE LWS_EXTERN int
  4432. lws_write(struct lws *wsi, unsigned char *buf, size_t len,
  4433. enum lws_write_protocol protocol);
  4434. /* helper for case where buffer may be const */
  4435. #define lws_write_http(wsi, buf, len) \
  4436. lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
  4437. /* helper for multi-frame ws message flags */
  4438. static LWS_INLINE int
  4439. lws_write_ws_flags(int initial, int is_start, int is_end)
  4440. {
  4441. int r;
  4442. if (is_start)
  4443. r = initial;
  4444. else
  4445. r = LWS_WRITE_CONTINUATION;
  4446. if (!is_end)
  4447. r |= LWS_WRITE_NO_FIN;
  4448. return r;
  4449. }
  4450. ///@}
  4451. /** \defgroup callback-when-writeable Callback when writeable
  4452. *
  4453. * ##Callback When Writeable
  4454. *
  4455. * lws can only write data on a connection when it is able to accept more
  4456. * data without blocking.
  4457. *
  4458. * So a basic requirement is we should only use the lws_write() apis when the
  4459. * connection we want to write on says that he can accept more data.
  4460. *
  4461. * When lws cannot complete your send at the time, it will buffer the data
  4462. * and send it in the background, suppressing any further WRITEABLE callbacks
  4463. * on that connection until it completes. So it is important to write new
  4464. * things in a new writeable callback.
  4465. *
  4466. * These apis reflect the various ways we can indicate we would like to be
  4467. * called back when one or more connections is writeable.
  4468. */
  4469. ///@{
  4470. /**
  4471. * lws_callback_on_writable() - Request a callback when this socket
  4472. * becomes able to be written to without
  4473. * blocking
  4474. *
  4475. * \param wsi: Websocket connection instance to get callback for
  4476. *
  4477. * - Which: only this wsi
  4478. * - When: when the individual connection becomes writeable
  4479. * - What: LWS_CALLBACK_*_WRITEABLE
  4480. */
  4481. LWS_VISIBLE LWS_EXTERN int
  4482. lws_callback_on_writable(struct lws *wsi);
  4483. /**
  4484. * lws_callback_on_writable_all_protocol() - Request a callback for all
  4485. * connections using the given protocol when it
  4486. * becomes possible to write to each socket without
  4487. * blocking in turn.
  4488. *
  4489. * \param context: lws_context
  4490. * \param protocol: Protocol whose connections will get callbacks
  4491. *
  4492. * - Which: connections using this protocol on ANY VHOST
  4493. * - When: when the individual connection becomes writeable
  4494. * - What: LWS_CALLBACK_*_WRITEABLE
  4495. */
  4496. LWS_VISIBLE LWS_EXTERN int
  4497. lws_callback_on_writable_all_protocol(const struct lws_context *context,
  4498. const struct lws_protocols *protocol);
  4499. /**
  4500. * lws_callback_on_writable_all_protocol_vhost() - Request a callback for
  4501. * all connections on same vhost using the given protocol
  4502. * when it becomes possible to write to each socket without
  4503. * blocking in turn.
  4504. *
  4505. * \param vhost: Only consider connections on this lws_vhost
  4506. * \param protocol: Protocol whose connections will get callbacks
  4507. *
  4508. * - Which: connections using this protocol on GIVEN VHOST ONLY
  4509. * - When: when the individual connection becomes writeable
  4510. * - What: LWS_CALLBACK_*_WRITEABLE
  4511. */
  4512. LWS_VISIBLE LWS_EXTERN int
  4513. lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
  4514. const struct lws_protocols *protocol);
  4515. /**
  4516. * lws_callback_all_protocol() - Callback all connections using
  4517. * the given protocol with the given reason
  4518. *
  4519. * \param context: lws_context
  4520. * \param protocol: Protocol whose connections will get callbacks
  4521. * \param reason: Callback reason index
  4522. *
  4523. * - Which: connections using this protocol on ALL VHOSTS
  4524. * - When: before returning
  4525. * - What: reason
  4526. *
  4527. * This isn't normally what you want... normally any update of connection-
  4528. * specific information can wait until a network-related callback like rx,
  4529. * writable, or close.
  4530. */
  4531. LWS_VISIBLE LWS_EXTERN int
  4532. lws_callback_all_protocol(struct lws_context *context,
  4533. const struct lws_protocols *protocol, int reason);
  4534. /**
  4535. * lws_callback_all_protocol_vhost() - Callback all connections using
  4536. * the given protocol with the given reason. This is
  4537. * deprecated since v2.4: use lws_callback_all_protocol_vhost_args
  4538. *
  4539. * \param vh: Vhost whose connections will get callbacks
  4540. * \param protocol: Which protocol to match. NULL means all.
  4541. * \param reason: Callback reason index
  4542. *
  4543. * - Which: connections using this protocol on GIVEN VHOST ONLY
  4544. * - When: now
  4545. * - What: reason
  4546. */
  4547. LWS_VISIBLE LWS_EXTERN int
  4548. lws_callback_all_protocol_vhost(struct lws_vhost *vh,
  4549. const struct lws_protocols *protocol, int reason)
  4550. LWS_WARN_DEPRECATED;
  4551. /**
  4552. * lws_callback_all_protocol_vhost_args() - Callback all connections using
  4553. * the given protocol with the given reason and args
  4554. *
  4555. * \param vh: Vhost whose connections will get callbacks
  4556. * \param protocol: Which protocol to match. NULL means all.
  4557. * \param reason: Callback reason index
  4558. * \param argp: Callback "in" parameter
  4559. * \param len: Callback "len" parameter
  4560. *
  4561. * - Which: connections using this protocol on GIVEN VHOST ONLY
  4562. * - When: now
  4563. * - What: reason
  4564. */
  4565. LWS_VISIBLE int
  4566. lws_callback_all_protocol_vhost_args(struct lws_vhost *vh,
  4567. const struct lws_protocols *protocol, int reason,
  4568. void *argp, size_t len);
  4569. /**
  4570. * lws_callback_vhost_protocols() - Callback all protocols enabled on a vhost
  4571. * with the given reason
  4572. *
  4573. * \param wsi: wsi whose vhost will get callbacks
  4574. * \param reason: Callback reason index
  4575. * \param in: in argument to callback
  4576. * \param len: len argument to callback
  4577. *
  4578. * - Which: connections using this protocol on same VHOST as wsi ONLY
  4579. * - When: now
  4580. * - What: reason
  4581. *
  4582. * This is deprecated since v2.5, use lws_callback_vhost_protocols_vhost()
  4583. * which takes the pointer to the vhost directly without using or needing the
  4584. * wsi.
  4585. */
  4586. LWS_VISIBLE LWS_EXTERN int
  4587. lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
  4588. LWS_WARN_DEPRECATED;
  4589. /**
  4590. * lws_callback_vhost_protocols_vhost() - Callback all protocols enabled on a vhost
  4591. * with the given reason
  4592. *
  4593. * \param vh: vhost that will get callbacks
  4594. * \param reason: Callback reason index
  4595. * \param in: in argument to callback
  4596. * \param len: len argument to callback
  4597. *
  4598. * - Which: connections using this protocol on same VHOST as wsi ONLY
  4599. * - When: now
  4600. * - What: reason
  4601. */
  4602. LWS_VISIBLE LWS_EXTERN int
  4603. lws_callback_vhost_protocols_vhost(struct lws_vhost *vh, int reason, void *in,
  4604. size_t len);
  4605. LWS_VISIBLE LWS_EXTERN int
  4606. lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
  4607. void *user, void *in, size_t len);
  4608. /**
  4609. * lws_get_socket_fd() - returns the socket file descriptor
  4610. *
  4611. * This is needed to use sendto() on UDP raw sockets
  4612. *
  4613. * \param wsi: Websocket connection instance
  4614. */
  4615. LWS_VISIBLE LWS_EXTERN lws_sockfd_type
  4616. lws_get_socket_fd(struct lws *wsi);
  4617. /**
  4618. * lws_get_peer_write_allowance() - get the amount of data writeable to peer
  4619. * if known
  4620. *
  4621. * \param wsi: Websocket connection instance
  4622. *
  4623. * if the protocol does not have any guidance, returns -1. Currently only
  4624. * http2 connections get send window information from this API. But your code
  4625. * should use it so it can work properly with any protocol.
  4626. *
  4627. * If nonzero return is the amount of payload data the peer or intermediary has
  4628. * reported it has buffer space for. That has NO relationship with the amount
  4629. * of buffer space your OS can accept on this connection for a write action.
  4630. *
  4631. * This number represents the maximum you could send to the peer or intermediary
  4632. * on this connection right now without the protocol complaining.
  4633. *
  4634. * lws manages accounting for send window updates and payload writes
  4635. * automatically, so this number reflects the situation at the peer or
  4636. * intermediary dynamically.
  4637. */
  4638. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  4639. lws_get_peer_write_allowance(struct lws *wsi);
  4640. ///@}
  4641. enum {
  4642. /*
  4643. * Flags for enable and disable rxflow with reason bitmap and with
  4644. * backwards-compatible single bool
  4645. */
  4646. LWS_RXFLOW_REASON_USER_BOOL = (1 << 0),
  4647. LWS_RXFLOW_REASON_HTTP_RXBUFFER = (1 << 6),
  4648. LWS_RXFLOW_REASON_H2_PPS_PENDING = (1 << 7),
  4649. LWS_RXFLOW_REASON_APPLIES = (1 << 14),
  4650. LWS_RXFLOW_REASON_APPLIES_ENABLE_BIT = (1 << 13),
  4651. LWS_RXFLOW_REASON_APPLIES_ENABLE = LWS_RXFLOW_REASON_APPLIES |
  4652. LWS_RXFLOW_REASON_APPLIES_ENABLE_BIT,
  4653. LWS_RXFLOW_REASON_APPLIES_DISABLE = LWS_RXFLOW_REASON_APPLIES,
  4654. LWS_RXFLOW_REASON_FLAG_PROCESS_NOW = (1 << 12),
  4655. };
  4656. /**
  4657. * lws_rx_flow_control() - Enable and disable socket servicing for
  4658. * received packets.
  4659. *
  4660. * If the output side of a server process becomes choked, this allows flow
  4661. * control for the input side.
  4662. *
  4663. * \param wsi: Websocket connection instance to get callback for
  4664. * \param enable: 0 = disable read servicing for this connection, 1 = enable
  4665. *
  4666. * If you need more than one additive reason for rxflow control, you can give
  4667. * iLWS_RXFLOW_REASON_APPLIES_ENABLE or _DISABLE together with one or more of
  4668. * b5..b0 set to idicate which bits to enable or disable. If any bits are
  4669. * enabled, rx on the connection is suppressed.
  4670. *
  4671. * LWS_RXFLOW_REASON_FLAG_PROCESS_NOW flag may also be given to force any change
  4672. * in rxflowbstatus to benapplied immediately, this should be used when you are
  4673. * changing a wsi flow control state from outside a callback on that wsi.
  4674. */
  4675. LWS_VISIBLE LWS_EXTERN int
  4676. lws_rx_flow_control(struct lws *wsi, int enable);
  4677. /**
  4678. * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
  4679. *
  4680. * When the user server code realizes it can accept more input, it can
  4681. * call this to have the RX flow restriction removed from all connections using
  4682. * the given protocol.
  4683. * \param context: lws_context
  4684. * \param protocol: all connections using this protocol will be allowed to receive
  4685. */
  4686. LWS_VISIBLE LWS_EXTERN void
  4687. lws_rx_flow_allow_all_protocol(const struct lws_context *context,
  4688. const struct lws_protocols *protocol);
  4689. /**
  4690. * lws_remaining_packet_payload() - Bytes to come before "overall"
  4691. * rx fragment is complete
  4692. * \param wsi: Websocket instance (available from user callback)
  4693. *
  4694. * This tracks how many bytes are left in the current ws fragment, according
  4695. * to the ws length given in the fragment header.
  4696. *
  4697. * If the message was in a single fragment, and there is no compression, this
  4698. * is the same as "how much data is left to read for this message".
  4699. *
  4700. * However, if the message is being sent in multiple fragments, this will
  4701. * reflect the unread amount of the current **fragment**, not the message. With
  4702. * ws, it is legal to not know the length of the message before it completes.
  4703. *
  4704. * Additionally if the message is sent via the negotiated permessage-deflate
  4705. * extension, this number only tells the amount of **compressed** data left to
  4706. * be read, since that is the only information available at the ws layer.
  4707. */
  4708. LWS_VISIBLE LWS_EXTERN size_t
  4709. lws_remaining_packet_payload(struct lws *wsi);
  4710. /** \defgroup sock-adopt Socket adoption helpers
  4711. * ##Socket adoption helpers
  4712. *
  4713. * When integrating with an external app with its own event loop, these can
  4714. * be used to accept connections from someone else's listening socket.
  4715. *
  4716. * When using lws own event loop, these are not needed.
  4717. */
  4718. ///@{
  4719. /**
  4720. * lws_adopt_socket() - adopt foreign socket as if listen socket accepted it
  4721. * for the default vhost of context.
  4722. *
  4723. * \param context: lws context
  4724. * \param accept_fd: fd of already-accepted socket to adopt
  4725. *
  4726. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4727. * returns NULL, having cleaned up any new wsi pieces.
  4728. *
  4729. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4730. * to ws or just serve http.
  4731. */
  4732. LWS_VISIBLE LWS_EXTERN struct lws *
  4733. lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd);
  4734. /**
  4735. * lws_adopt_socket_vhost() - adopt foreign socket as if listen socket accepted it
  4736. * for vhost
  4737. *
  4738. * \param vh: lws vhost
  4739. * \param accept_fd: fd of already-accepted socket to adopt
  4740. *
  4741. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4742. * returns NULL, having cleaned up any new wsi pieces.
  4743. *
  4744. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4745. * to ws or just serve http.
  4746. */
  4747. LWS_VISIBLE LWS_EXTERN struct lws *
  4748. lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
  4749. typedef enum {
  4750. LWS_ADOPT_RAW_FILE_DESC = 0, /* convenience constant */
  4751. LWS_ADOPT_HTTP = 1, /* flag: absent implies RAW */
  4752. LWS_ADOPT_SOCKET = 2, /* flag: absent implies file descr */
  4753. LWS_ADOPT_ALLOW_SSL = 4, /* flag: if set requires LWS_ADOPT_SOCKET */
  4754. LWS_ADOPT_WS_PARENTIO = 8, /* flag: ws mode parent handles IO
  4755. * if given must be only flag
  4756. * wsi put directly into ws mode */
  4757. LWS_ADOPT_FLAG_UDP = 16, /* flag: socket is UDP */
  4758. LWS_ADOPT_RAW_SOCKET_UDP = LWS_ADOPT_SOCKET | LWS_ADOPT_FLAG_UDP,
  4759. } lws_adoption_type;
  4760. typedef union {
  4761. lws_sockfd_type sockfd;
  4762. lws_filefd_type filefd;
  4763. } lws_sock_file_fd_type;
  4764. #if !defined(LWS_WITH_ESP32)
  4765. struct lws_udp {
  4766. struct sockaddr sa;
  4767. socklen_t salen;
  4768. struct sockaddr sa_pending;
  4769. socklen_t salen_pending;
  4770. };
  4771. #endif
  4772. /*
  4773. * lws_adopt_descriptor_vhost() - adopt foreign socket or file descriptor
  4774. * if socket descriptor, should already have been accepted from listen socket
  4775. *
  4776. * \param vhost: lws vhost
  4777. * \param type: OR-ed combinations of lws_adoption_type flags
  4778. * \param fd: union with either .sockfd or .filefd set
  4779. * \param vh_prot_name: NULL or vh protocol name to bind raw connection to
  4780. * \param parent: NULL or struct lws to attach new_wsi to as a child
  4781. *
  4782. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4783. * returns NULL, having cleaned up any new wsi pieces.
  4784. *
  4785. * If LWS_ADOPT_SOCKET is set, LWS adopts the socket in http serving mode, it's
  4786. * ready to accept an upgrade to ws or just serve http.
  4787. *
  4788. * parent may be NULL, if given it should be an existing wsi that will become the
  4789. * parent of the new wsi created by this call.
  4790. */
  4791. LWS_VISIBLE LWS_EXTERN struct lws *
  4792. lws_adopt_descriptor_vhost(struct lws_vhost *vh, lws_adoption_type type,
  4793. lws_sock_file_fd_type fd, const char *vh_prot_name,
  4794. struct lws *parent);
  4795. /**
  4796. * lws_adopt_socket_readbuf() - adopt foreign socket and first rx as if listen socket accepted it
  4797. * for the default vhost of context.
  4798. * \param context: lws context
  4799. * \param accept_fd: fd of already-accepted socket to adopt
  4800. * \param readbuf: NULL or pointer to data that must be drained before reading from
  4801. * accept_fd
  4802. * \param len: The length of the data held at \param readbuf
  4803. *
  4804. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4805. * returns NULL, having cleaned up any new wsi pieces.
  4806. *
  4807. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4808. * to ws or just serve http.
  4809. *
  4810. * If your external code did not already read from the socket, you can use
  4811. * lws_adopt_socket() instead.
  4812. *
  4813. * This api is guaranteed to use the data at \param readbuf first, before reading from
  4814. * the socket.
  4815. *
  4816. * readbuf is limited to the size of the ah rx buf, currently 2048 bytes.
  4817. */
  4818. LWS_VISIBLE LWS_EXTERN struct lws *
  4819. lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
  4820. const char *readbuf, size_t len);
  4821. /**
  4822. * lws_adopt_socket_vhost_readbuf() - adopt foreign socket and first rx as if listen socket
  4823. * accepted it for vhost.
  4824. * \param vhost: lws vhost
  4825. * \param accept_fd: fd of already-accepted socket to adopt
  4826. * \param readbuf: NULL or pointer to data that must be drained before reading from
  4827. * accept_fd
  4828. * \param len: The length of the data held at \param readbuf
  4829. *
  4830. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4831. * returns NULL, having cleaned up any new wsi pieces.
  4832. *
  4833. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  4834. * to ws or just serve http.
  4835. *
  4836. * If your external code did not already read from the socket, you can use
  4837. * lws_adopt_socket() instead.
  4838. *
  4839. * This api is guaranteed to use the data at \param readbuf first, before reading from
  4840. * the socket.
  4841. *
  4842. * readbuf is limited to the size of the ah rx buf, currently 2048 bytes.
  4843. */
  4844. LWS_VISIBLE LWS_EXTERN struct lws *
  4845. lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, lws_sockfd_type accept_fd,
  4846. const char *readbuf, size_t len);
  4847. #define LWS_CAUDP_BIND 1
  4848. /**
  4849. * lws_create_adopt_udp() - create, bind and adopt a UDP socket
  4850. *
  4851. * \param vhost: lws vhost
  4852. * \param port: UDP port to bind to, -1 means unbound
  4853. * \param flags: 0 or LWS_CAUDP_NO_BIND
  4854. * \param protocol_name: Name of protocol on vhost to bind wsi to
  4855. * \param parent_wsi: NULL or parent wsi new wsi will be a child of
  4856. *
  4857. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  4858. * returns NULL, having cleaned up any new wsi pieces.
  4859. * */
  4860. LWS_VISIBLE LWS_EXTERN struct lws *
  4861. lws_create_adopt_udp(struct lws_vhost *vhost, int port, int flags,
  4862. const char *protocol_name, struct lws *parent_wsi);
  4863. ///@}
  4864. /** \defgroup net Network related helper APIs
  4865. * ##Network related helper APIs
  4866. *
  4867. * These wrap miscellaneous useful network-related functions
  4868. */
  4869. ///@{
  4870. /**
  4871. * lws_canonical_hostname() - returns this host's hostname
  4872. *
  4873. * This is typically used by client code to fill in the host parameter
  4874. * when making a client connection. You can only call it after the context
  4875. * has been created.
  4876. *
  4877. * \param context: Websocket context
  4878. */
  4879. LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  4880. lws_canonical_hostname(struct lws_context *context);
  4881. /**
  4882. * lws_get_peer_addresses() - Get client address information
  4883. * \param wsi: Local struct lws associated with
  4884. * \param fd: Connection socket descriptor
  4885. * \param name: Buffer to take client address name
  4886. * \param name_len: Length of client address name buffer
  4887. * \param rip: Buffer to take client address IP dotted quad
  4888. * \param rip_len: Length of client address IP buffer
  4889. *
  4890. * This function fills in name and rip with the name and IP of
  4891. * the client connected with socket descriptor fd. Names may be
  4892. * truncated if there is not enough room. If either cannot be
  4893. * determined, they will be returned as valid zero-length strings.
  4894. */
  4895. LWS_VISIBLE LWS_EXTERN void
  4896. lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
  4897. int name_len, char *rip, int rip_len);
  4898. /**
  4899. * lws_get_peer_simple() - Get client address information without RDNS
  4900. *
  4901. * \param wsi: Local struct lws associated with
  4902. * \param name: Buffer to take client address name
  4903. * \param namelen: Length of client address name buffer
  4904. *
  4905. * This provides a 123.123.123.123 type IP address in name from the
  4906. * peer that has connected to wsi
  4907. */
  4908. LWS_VISIBLE LWS_EXTERN const char *
  4909. lws_get_peer_simple(struct lws *wsi, char *name, int namelen);
  4910. #define LWS_ITOSA_NOT_EXIST -1
  4911. #define LWS_ITOSA_NOT_USABLE -2
  4912. #define LWS_ITOSA_USABLE 0
  4913. #if !defined(LWS_WITH_ESP32)
  4914. /**
  4915. * lws_interface_to_sa() - Convert interface name or IP to sockaddr struct
  4916. *
  4917. * \param ipv6: Allow IPV6 addresses
  4918. * \param ifname: Interface name or IP
  4919. * \param addr: struct sockaddr_in * to be written
  4920. * \param addrlen: Length of addr
  4921. *
  4922. * This converts a textual network interface name to a sockaddr usable by
  4923. * other network functions.
  4924. *
  4925. * If the network interface doesn't exist, it will return LWS_ITOSA_NOT_EXIST.
  4926. *
  4927. * If the network interface is not usable, eg ethernet cable is removed, it
  4928. * may logically exist but not have any IP address. As such it will return
  4929. * LWS_ITOSA_NOT_USABLE.
  4930. *
  4931. * If the network interface exists and is usable, it will return
  4932. * LWS_ITOSA_USABLE.
  4933. */
  4934. LWS_VISIBLE LWS_EXTERN int
  4935. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  4936. size_t addrlen);
  4937. ///@}
  4938. #endif
  4939. /** \defgroup misc Miscellaneous APIs
  4940. * ##Miscellaneous APIs
  4941. *
  4942. * Various APIs outside of other categories
  4943. */
  4944. ///@{
  4945. /**
  4946. * lws_start_foreach_ll(): linkedlist iterator helper start
  4947. *
  4948. * \param type: type of iteration, eg, struct xyz *
  4949. * \param it: iterator var name to create
  4950. * \param start: start of list
  4951. *
  4952. * This helper creates an iterator and starts a while (it) {
  4953. * loop. The iterator runs through the linked list starting at start and
  4954. * ends when it gets a NULL.
  4955. * The while loop should be terminated using lws_start_foreach_ll().
  4956. */
  4957. #define lws_start_foreach_ll(type, it, start)\
  4958. { \
  4959. type it = start; \
  4960. while (it) {
  4961. /**
  4962. * lws_end_foreach_ll(): linkedlist iterator helper end
  4963. *
  4964. * \param it: same iterator var name given when starting
  4965. * \param nxt: member name in the iterator pointing to next list element
  4966. *
  4967. * This helper is the partner for lws_start_foreach_ll() that ends the
  4968. * while loop.
  4969. */
  4970. #define lws_end_foreach_ll(it, nxt) \
  4971. it = it->nxt; \
  4972. } \
  4973. }
  4974. /**
  4975. * lws_start_foreach_llp(): linkedlist pointer iterator helper start
  4976. *
  4977. * \param type: type of iteration, eg, struct xyz **
  4978. * \param it: iterator var name to create
  4979. * \param start: start of list
  4980. *
  4981. * This helper creates an iterator and starts a while (it) {
  4982. * loop. The iterator runs through the linked list starting at the
  4983. * address of start and ends when it gets a NULL.
  4984. * The while loop should be terminated using lws_start_foreach_llp().
  4985. *
  4986. * This helper variant iterates using a pointer to the previous linked-list
  4987. * element. That allows you to easily delete list members by rewriting the
  4988. * previous pointer to the element's next pointer.
  4989. */
  4990. #define lws_start_foreach_llp(type, it, start)\
  4991. { \
  4992. type it = &(start); \
  4993. while (*(it)) {
  4994. #define lws_start_foreach_llp_safe(type, it, start, nxt)\
  4995. { \
  4996. type it = &(start); \
  4997. type next; \
  4998. while (*(it)) { \
  4999. next = &((*(it))->nxt); \
  5000. /**
  5001. * lws_end_foreach_llp(): linkedlist pointer iterator helper end
  5002. *
  5003. * \param it: same iterator var name given when starting
  5004. * \param nxt: member name in the iterator pointing to next list element
  5005. *
  5006. * This helper is the partner for lws_start_foreach_llp() that ends the
  5007. * while loop.
  5008. */
  5009. #define lws_end_foreach_llp(it, nxt) \
  5010. it = &(*(it))->nxt; \
  5011. } \
  5012. }
  5013. #define lws_end_foreach_llp_safe(it) \
  5014. it = next; \
  5015. } \
  5016. }
  5017. #define lws_ll_fwd_insert(\
  5018. ___new_object, /* pointer to new object */ \
  5019. ___m_list, /* member for next list object ptr */ \
  5020. ___list_head /* list head */ \
  5021. ) {\
  5022. ___new_object->___m_list = ___list_head; \
  5023. ___list_head = ___new_object; \
  5024. }
  5025. #define lws_ll_fwd_remove(\
  5026. ___type, /* type of listed object */ \
  5027. ___m_list, /* member for next list object ptr */ \
  5028. ___target, /* object to remove from list */ \
  5029. ___list_head /* list head */ \
  5030. ) { \
  5031. lws_start_foreach_llp(___type **, ___ppss, ___list_head) { \
  5032. if (*___ppss == ___target) { \
  5033. *___ppss = ___target->___m_list; \
  5034. break; \
  5035. } \
  5036. } lws_end_foreach_llp(___ppss, ___m_list); \
  5037. }
  5038. /*
  5039. * doubly linked-list
  5040. */
  5041. struct lws_dll { /* abstract */
  5042. struct lws_dll *prev;
  5043. struct lws_dll *next;
  5044. };
  5045. /*
  5046. * these all point to the composed list objects... you have to use the
  5047. * lws_container_of() helper to recover the start of the containing struct
  5048. */
  5049. LWS_VISIBLE LWS_EXTERN void
  5050. lws_dll_add_front(struct lws_dll *d, struct lws_dll *phead);
  5051. LWS_VISIBLE LWS_EXTERN void
  5052. lws_dll_remove(struct lws_dll *d);
  5053. struct lws_dll_lws { /* typed as struct lws * */
  5054. struct lws_dll_lws *prev;
  5055. struct lws_dll_lws *next;
  5056. };
  5057. #define lws_dll_is_null(___dll) (!(___dll)->prev && !(___dll)->next)
  5058. static LWS_INLINE void
  5059. lws_dll_lws_add_front(struct lws_dll_lws *_a, struct lws_dll_lws *_head)
  5060. {
  5061. lws_dll_add_front((struct lws_dll *)_a, (struct lws_dll *)_head);
  5062. }
  5063. static LWS_INLINE void
  5064. lws_dll_lws_remove(struct lws_dll_lws *_a)
  5065. {
  5066. lws_dll_remove((struct lws_dll *)_a);
  5067. }
  5068. /*
  5069. * these are safe against the current container object getting deleted,
  5070. * since the hold his next in a temp and go to that next. ___tmp is
  5071. * the temp.
  5072. */
  5073. #define lws_start_foreach_dll_safe(___type, ___it, ___tmp, ___start) \
  5074. { \
  5075. ___type ___it = ___start; \
  5076. while (___it) { \
  5077. ___type ___tmp = (___it)->next;
  5078. #define lws_end_foreach_dll_safe(___it, ___tmp) \
  5079. ___it = ___tmp; \
  5080. } \
  5081. }
  5082. #define lws_start_foreach_dll(___type, ___it, ___start) \
  5083. { \
  5084. ___type ___it = ___start; \
  5085. while (___it) {
  5086. #define lws_end_foreach_dll(___it) \
  5087. ___it = (___it)->next; \
  5088. } \
  5089. }
  5090. struct lws_buflist;
  5091. /**
  5092. * lws_buflist_append_segment(): add buffer to buflist at head
  5093. *
  5094. * \param head: list head
  5095. * \param buf: buffer to stash
  5096. * \param len: length of buffer to stash
  5097. *
  5098. * Returns -1 on OOM, 1 if this was the first segment on the list, and 0 if
  5099. * it was a subsequent segment.
  5100. */
  5101. LWS_VISIBLE LWS_EXTERN int
  5102. lws_buflist_append_segment(struct lws_buflist **head, const uint8_t *buf,
  5103. size_t len);
  5104. /**
  5105. * lws_buflist_next_segment_len(): number of bytes left in current segment
  5106. *
  5107. * \param head: list head
  5108. * \param buf: if non-NULL, *buf is written with the address of the start of
  5109. * the remaining data in the segment
  5110. *
  5111. * Returns the number of bytes left in the current segment. 0 indicates
  5112. * that the buflist is empty (there are no segments on the buflist).
  5113. */
  5114. LWS_VISIBLE LWS_EXTERN size_t
  5115. lws_buflist_next_segment_len(struct lws_buflist **head, uint8_t **buf);
  5116. /**
  5117. * lws_buflist_use_segment(): remove len bytes from the current segment
  5118. *
  5119. * \param head: list head
  5120. * \param len: number of bytes to mark as used
  5121. *
  5122. * If len is less than the remaining length of the current segment, the position
  5123. * in the current segment is simply advanced and it returns.
  5124. *
  5125. * If len uses up the remaining length of the current segment, then the segment
  5126. * is deleted and the list head moves to the next segment if any.
  5127. *
  5128. * Returns the number of bytes left in the current segment. 0 indicates
  5129. * that the buflist is empty (there are no segments on the buflist).
  5130. */
  5131. LWS_VISIBLE LWS_EXTERN int
  5132. lws_buflist_use_segment(struct lws_buflist **head, size_t len);
  5133. /**
  5134. * lws_buflist_destroy_all_segments(): free all segments on the list
  5135. *
  5136. * \param head: list head
  5137. *
  5138. * This frees everything on the list unconditionally. *head is always
  5139. * NULL after this.
  5140. */
  5141. LWS_VISIBLE LWS_EXTERN void
  5142. lws_buflist_destroy_all_segments(struct lws_buflist **head);
  5143. void
  5144. lws_buflist_describe(struct lws_buflist **head, void *id);
  5145. /**
  5146. * lws_ptr_diff(): helper to report distance between pointers as an int
  5147. *
  5148. * \param head: the pointer with the larger address
  5149. * \param tail: the pointer with the smaller address
  5150. *
  5151. * This helper gives you an int representing the number of bytes further
  5152. * forward the first pointer is compared to the second pointer.
  5153. */
  5154. #define lws_ptr_diff(head, tail) \
  5155. ((int)((char *)(head) - (char *)(tail)))
  5156. /**
  5157. * lws_snprintf(): snprintf that truncates the returned length too
  5158. *
  5159. * \param str: destination buffer
  5160. * \param size: bytes left in destination buffer
  5161. * \param format: format string
  5162. * \param ...: args for format
  5163. *
  5164. * This lets you correctly truncate buffers by concatenating lengths, if you
  5165. * reach the limit the reported length doesn't exceed the limit.
  5166. */
  5167. LWS_VISIBLE LWS_EXTERN int
  5168. lws_snprintf(char *str, size_t size, const char *format, ...) LWS_FORMAT(3);
  5169. /**
  5170. * lws_strncpy(): strncpy that guarantees NUL on truncated copy
  5171. *
  5172. * \param dest: destination buffer
  5173. * \param src: source buffer
  5174. * \param size: bytes left in destination buffer
  5175. *
  5176. * This lets you correctly truncate buffers by concatenating lengths, if you
  5177. * reach the limit the reported length doesn't exceed the limit.
  5178. */
  5179. LWS_VISIBLE LWS_EXTERN char *
  5180. lws_strncpy(char *dest, const char *src, size_t size);
  5181. /**
  5182. * lws_get_random(): fill a buffer with platform random data
  5183. *
  5184. * \param context: the lws context
  5185. * \param buf: buffer to fill
  5186. * \param len: how much to fill
  5187. *
  5188. * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
  5189. * it's interested to see if the frame it's dealing with was sent in binary
  5190. * mode.
  5191. */
  5192. LWS_VISIBLE LWS_EXTERN int
  5193. lws_get_random(struct lws_context *context, void *buf, int len);
  5194. /**
  5195. * lws_daemonize(): make current process run in the background
  5196. *
  5197. * \param _lock_path: the filepath to write the lock file
  5198. *
  5199. * Spawn lws as a background process, taking care of various things
  5200. */
  5201. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5202. lws_daemonize(const char *_lock_path);
  5203. /**
  5204. * lws_get_library_version(): return string describing the version of lws
  5205. *
  5206. * On unix, also includes the git describe
  5207. */
  5208. LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  5209. lws_get_library_version(void);
  5210. /**
  5211. * lws_wsi_user() - get the user data associated with the connection
  5212. * \param wsi: lws connection
  5213. *
  5214. * Not normally needed since it's passed into the callback
  5215. */
  5216. LWS_VISIBLE LWS_EXTERN void *
  5217. lws_wsi_user(struct lws *wsi);
  5218. /**
  5219. * lws_wsi_set_user() - set the user data associated with the client connection
  5220. * \param wsi: lws connection
  5221. * \param user: user data
  5222. *
  5223. * By default lws allocates this and it's not legal to externally set it
  5224. * yourself. However client connections may have it set externally when the
  5225. * connection is created... if so, this api can be used to modify it at
  5226. * runtime additionally.
  5227. */
  5228. LWS_VISIBLE LWS_EXTERN void
  5229. lws_set_wsi_user(struct lws *wsi, void *user);
  5230. /**
  5231. * lws_parse_uri: cut up prot:/ads:port/path into pieces
  5232. * Notice it does so by dropping '\0' into input string
  5233. * and the leading / on the path is consequently lost
  5234. *
  5235. * \param p: incoming uri string.. will get written to
  5236. * \param prot: result pointer for protocol part (https://)
  5237. * \param ads: result pointer for address part
  5238. * \param port: result pointer for port part
  5239. * \param path: result pointer for path part
  5240. */
  5241. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5242. lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
  5243. const char **path);
  5244. /**
  5245. * lws_cmdline_option(): simple commandline parser
  5246. *
  5247. * \param argc: count of argument strings
  5248. * \param argv: argument strings
  5249. * \param val: string to find
  5250. *
  5251. * Returns NULL if the string \p val is not found in the arguments.
  5252. *
  5253. * If it is found, then it returns a pointer to the next character after \p val.
  5254. * So if \p val is "-d", then for the commandlines "myapp -d15" and
  5255. * "myapp -d 15", in both cases the return will point to the "15".
  5256. *
  5257. * In the case there is no argument, like "myapp -d", the return will
  5258. * either point to the '\\0' at the end of -d, or to the start of the
  5259. * next argument, ie, will be non-NULL.
  5260. */
  5261. LWS_VISIBLE LWS_EXTERN const char *
  5262. lws_cmdline_option(int argc, const char **argv, const char *val);
  5263. /**
  5264. * lws_now_secs(): return seconds since 1970-1-1
  5265. */
  5266. LWS_VISIBLE LWS_EXTERN unsigned long
  5267. lws_now_secs(void);
  5268. /**
  5269. * lws_compare_time_t(): return relationship between two time_t
  5270. *
  5271. * \param context: struct lws_context
  5272. * \param t1: time_t 1
  5273. * \param t2: time_t 2
  5274. *
  5275. * returns <0 if t2 > t1; >0 if t1 > t2; or == 0 if t1 == t2.
  5276. *
  5277. * This is aware of clock discontiguities that may have affected either t1 or
  5278. * t2 and adapts the comparison for them.
  5279. *
  5280. * For the discontiguity detection to work, you must avoid any arithmetic on
  5281. * the times being compared. For example to have a timeout that triggers
  5282. * 15s from when it was set, store the time it was set and compare like
  5283. * `if (lws_compare_time_t(context, now, set_time) > 15)`
  5284. */
  5285. LWS_VISIBLE LWS_EXTERN int
  5286. lws_compare_time_t(struct lws_context *context, time_t t1, time_t t2);
  5287. /**
  5288. * lws_get_context - Allow getting lws_context from a Websocket connection
  5289. * instance
  5290. *
  5291. * With this function, users can access context in the callback function.
  5292. * Otherwise users may have to declare context as a global variable.
  5293. *
  5294. * \param wsi: Websocket connection instance
  5295. */
  5296. LWS_VISIBLE LWS_EXTERN struct lws_context * LWS_WARN_UNUSED_RESULT
  5297. lws_get_context(const struct lws *wsi);
  5298. /**
  5299. * lws_get_vhost_listen_port - Find out the port number a vhost is listening on
  5300. *
  5301. * In the case you passed 0 for the port number at context creation time, you
  5302. * can discover the port number that was actually chosen for the vhost using
  5303. * this api.
  5304. *
  5305. * \param vhost: Vhost to get listen port from
  5306. */
  5307. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5308. lws_get_vhost_listen_port(struct lws_vhost *vhost);
  5309. /**
  5310. * lws_get_count_threads(): how many service threads the context uses
  5311. *
  5312. * \param context: the lws context
  5313. *
  5314. * By default this is always 1, if you asked for more than lws can handle it
  5315. * will clip the number of threads. So you can use this to find out how many
  5316. * threads are actually in use.
  5317. */
  5318. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5319. lws_get_count_threads(struct lws_context *context);
  5320. /**
  5321. * lws_get_parent() - get parent wsi or NULL
  5322. * \param wsi: lws connection
  5323. *
  5324. * Specialized wsi like cgi stdin/out/err are associated to a parent wsi,
  5325. * this allows you to get their parent.
  5326. */
  5327. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  5328. lws_get_parent(const struct lws *wsi);
  5329. /**
  5330. * lws_get_child() - get child wsi or NULL
  5331. * \param wsi: lws connection
  5332. *
  5333. * Allows you to find a related wsi from the parent wsi.
  5334. */
  5335. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  5336. lws_get_child(const struct lws *wsi);
  5337. /**
  5338. * lws_get_udp() - get wsi's udp struct
  5339. *
  5340. * \param wsi: lws connection
  5341. *
  5342. * Returns NULL or pointer to the wsi's UDP-specific information
  5343. */
  5344. LWS_VISIBLE LWS_EXTERN const struct lws_udp * LWS_WARN_UNUSED_RESULT
  5345. lws_get_udp(const struct lws *wsi);
  5346. /**
  5347. * lws_parent_carries_io() - mark wsi as needing to send messages via parent
  5348. *
  5349. * \param wsi: child lws connection
  5350. */
  5351. LWS_VISIBLE LWS_EXTERN void
  5352. lws_set_parent_carries_io(struct lws *wsi);
  5353. LWS_VISIBLE LWS_EXTERN void *
  5354. lws_get_opaque_parent_data(const struct lws *wsi);
  5355. LWS_VISIBLE LWS_EXTERN void
  5356. lws_set_opaque_parent_data(struct lws *wsi, void *data);
  5357. LWS_VISIBLE LWS_EXTERN int
  5358. lws_get_child_pending_on_writable(const struct lws *wsi);
  5359. LWS_VISIBLE LWS_EXTERN void
  5360. lws_clear_child_pending_on_writable(struct lws *wsi);
  5361. LWS_VISIBLE LWS_EXTERN int
  5362. lws_get_close_length(struct lws *wsi);
  5363. LWS_VISIBLE LWS_EXTERN unsigned char *
  5364. lws_get_close_payload(struct lws *wsi);
  5365. /**
  5366. * lws_get_network_wsi() - Returns wsi that has the tcp connection for this wsi
  5367. *
  5368. * \param wsi: wsi you have
  5369. *
  5370. * Returns wsi that has the tcp connection (which may be the incoming wsi)
  5371. *
  5372. * HTTP/1 connections will always return the incoming wsi
  5373. * HTTP/2 connections may return a different wsi that has the tcp connection
  5374. */
  5375. LWS_VISIBLE LWS_EXTERN
  5376. struct lws *lws_get_network_wsi(struct lws *wsi);
  5377. /**
  5378. * lws_set_allocator() - custom allocator support
  5379. *
  5380. * \param realloc
  5381. *
  5382. * Allows you to replace the allocator (and deallocator) used by lws
  5383. */
  5384. LWS_VISIBLE LWS_EXTERN void
  5385. lws_set_allocator(void *(*realloc)(void *ptr, size_t size, const char *reason));
  5386. ///@}
  5387. /** \defgroup wsstatus Websocket status APIs
  5388. * ##Websocket connection status APIs
  5389. *
  5390. * These provide information about ws connection or message status
  5391. */
  5392. ///@{
  5393. /**
  5394. * lws_send_pipe_choked() - tests if socket is writable or not
  5395. * \param wsi: lws connection
  5396. *
  5397. * Allows you to check if you can write more on the socket
  5398. */
  5399. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5400. lws_send_pipe_choked(struct lws *wsi);
  5401. /**
  5402. * lws_is_final_fragment() - tests if last part of ws message
  5403. *
  5404. * \param wsi: lws connection
  5405. */
  5406. LWS_VISIBLE LWS_EXTERN int
  5407. lws_is_final_fragment(struct lws *wsi);
  5408. /**
  5409. * lws_is_first_fragment() - tests if first part of ws message
  5410. *
  5411. * \param wsi: lws connection
  5412. */
  5413. LWS_VISIBLE LWS_EXTERN int
  5414. lws_is_first_fragment(struct lws *wsi);
  5415. /**
  5416. * lws_get_reserved_bits() - access reserved bits of ws frame
  5417. * \param wsi: lws connection
  5418. */
  5419. LWS_VISIBLE LWS_EXTERN unsigned char
  5420. lws_get_reserved_bits(struct lws *wsi);
  5421. /**
  5422. * lws_partial_buffered() - find out if lws buffered the last write
  5423. * \param wsi: websocket connection to check
  5424. *
  5425. * Returns 1 if you cannot use lws_write because the last
  5426. * write on this connection is still buffered, and can't be cleared without
  5427. * returning to the service loop and waiting for the connection to be
  5428. * writeable again.
  5429. *
  5430. * If you will try to do >1 lws_write call inside a single
  5431. * WRITEABLE callback, you must check this after every write and bail if
  5432. * set, ask for a new writeable callback and continue writing from there.
  5433. *
  5434. * This is never set at the start of a writeable callback, but any write
  5435. * may set it.
  5436. */
  5437. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5438. lws_partial_buffered(struct lws *wsi);
  5439. /**
  5440. * lws_frame_is_binary(): true if the current frame was sent in binary mode
  5441. *
  5442. * \param wsi: the connection we are inquiring about
  5443. *
  5444. * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
  5445. * it's interested to see if the frame it's dealing with was sent in binary
  5446. * mode.
  5447. */
  5448. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  5449. lws_frame_is_binary(struct lws *wsi);
  5450. /**
  5451. * lws_is_ssl() - Find out if connection is using SSL
  5452. * \param wsi: websocket connection to check
  5453. *
  5454. * Returns 0 if the connection is not using SSL, 1 if using SSL and
  5455. * using verified cert, and 2 if using SSL but the cert was not
  5456. * checked (appears for client wsi told to skip check on connection)
  5457. */
  5458. LWS_VISIBLE LWS_EXTERN int
  5459. lws_is_ssl(struct lws *wsi);
  5460. /**
  5461. * lws_is_cgi() - find out if this wsi is running a cgi process
  5462. * \param wsi: lws connection
  5463. */
  5464. LWS_VISIBLE LWS_EXTERN int
  5465. lws_is_cgi(struct lws *wsi);
  5466. struct lws_wifi_scan { /* generic wlan scan item */
  5467. struct lws_wifi_scan *next;
  5468. char ssid[32];
  5469. int32_t rssi; /* divide by .count to get db */
  5470. uint8_t bssid[6];
  5471. uint8_t count;
  5472. uint8_t channel;
  5473. uint8_t authmode;
  5474. };
  5475. #if defined(LWS_WITH_TLS) && !defined(LWS_WITH_MBEDTLS)
  5476. /**
  5477. * lws_get_ssl() - Return wsi's SSL context structure
  5478. * \param wsi: websocket connection
  5479. *
  5480. * Returns pointer to the SSL library's context structure
  5481. */
  5482. LWS_VISIBLE LWS_EXTERN SSL*
  5483. lws_get_ssl(struct lws *wsi);
  5484. #endif
  5485. enum lws_tls_cert_info {
  5486. LWS_TLS_CERT_INFO_VALIDITY_FROM,
  5487. /**< fills .time with the time_t the cert validity started from */
  5488. LWS_TLS_CERT_INFO_VALIDITY_TO,
  5489. /**< fills .time with the time_t the cert validity ends at */
  5490. LWS_TLS_CERT_INFO_COMMON_NAME,
  5491. /**< fills up to len bytes of .ns.name with the cert common name */
  5492. LWS_TLS_CERT_INFO_ISSUER_NAME,
  5493. /**< fills up to len bytes of .ns.name with the cert issuer name */
  5494. LWS_TLS_CERT_INFO_USAGE,
  5495. /**< fills verified with a bitfield asserting the valid uses */
  5496. LWS_TLS_CERT_INFO_VERIFIED,
  5497. /**< fills .verified with a bool representing peer cert validity,
  5498. * call returns -1 if no cert */
  5499. LWS_TLS_CERT_INFO_OPAQUE_PUBLIC_KEY,
  5500. /**< the certificate's public key, as an opaque bytestream. These
  5501. * opaque bytestreams can only be compared with each other using the
  5502. * same tls backend, ie, OpenSSL or mbedTLS. The different backends
  5503. * produce different, incompatible representations for the same cert.
  5504. */
  5505. };
  5506. union lws_tls_cert_info_results {
  5507. unsigned int verified;
  5508. time_t time;
  5509. unsigned int usage;
  5510. struct {
  5511. int len;
  5512. /* KEEP LAST... notice the [64] is only there because
  5513. * name[] is not allowed in a union. The actual length of
  5514. * name[] is arbitrary and is passed into the api using the
  5515. * len parameter. Eg
  5516. *
  5517. * char big[1024];
  5518. * union lws_tls_cert_info_results *buf =
  5519. * (union lws_tls_cert_info_results *)big;
  5520. *
  5521. * lws_tls_peer_cert_info(wsi, type, buf, sizeof(big) -
  5522. * sizeof(*buf) + sizeof(buf->ns.name));
  5523. */
  5524. char name[64];
  5525. } ns;
  5526. };
  5527. /**
  5528. * lws_tls_peer_cert_info() - get information from the peer's TLS cert
  5529. *
  5530. * \param wsi: the connection to query
  5531. * \param type: one of LWS_TLS_CERT_INFO_
  5532. * \param buf: pointer to union to take result
  5533. * \param len: when result is a string, the true length of buf->ns.name[]
  5534. *
  5535. * lws_tls_peer_cert_info() lets you get hold of information from the peer
  5536. * certificate.
  5537. *
  5538. * Return 0 if there is a result in \p buf, or -1 indicating there was no cert
  5539. * or another problem.
  5540. *
  5541. * This function works the same no matter if the TLS backend is OpenSSL or
  5542. * mbedTLS.
  5543. */
  5544. LWS_VISIBLE LWS_EXTERN int
  5545. lws_tls_peer_cert_info(struct lws *wsi, enum lws_tls_cert_info type,
  5546. union lws_tls_cert_info_results *buf, size_t len);
  5547. /**
  5548. * lws_tls_vhost_cert_info() - get information from the vhost's own TLS cert
  5549. *
  5550. * \param vhost: the vhost to query
  5551. * \param type: one of LWS_TLS_CERT_INFO_
  5552. * \param buf: pointer to union to take result
  5553. * \param len: when result is a string, the true length of buf->ns.name[]
  5554. *
  5555. * lws_tls_vhost_cert_info() lets you get hold of information from the vhost
  5556. * certificate.
  5557. *
  5558. * Return 0 if there is a result in \p buf, or -1 indicating there was no cert
  5559. * or another problem.
  5560. *
  5561. * This function works the same no matter if the TLS backend is OpenSSL or
  5562. * mbedTLS.
  5563. */
  5564. LWS_VISIBLE LWS_EXTERN int
  5565. lws_tls_vhost_cert_info(struct lws_vhost *vhost, enum lws_tls_cert_info type,
  5566. union lws_tls_cert_info_results *buf, size_t len);
  5567. /**
  5568. * lws_tls_acme_sni_cert_create() - creates a temp selfsigned cert
  5569. * and attaches to a vhost
  5570. *
  5571. * \param vhost: the vhost to acquire the selfsigned cert
  5572. * \param san_a: SAN written into the certificate
  5573. * \param san_b: second SAN written into the certificate
  5574. *
  5575. *
  5576. * Returns 0 if created and attached to the vhost. Returns -1 if problems and
  5577. * frees all allocations before returning.
  5578. *
  5579. * On success, any allocations are destroyed at vhost destruction automatically.
  5580. */
  5581. LWS_VISIBLE LWS_EXTERN int
  5582. lws_tls_acme_sni_cert_create(struct lws_vhost *vhost, const char *san_a,
  5583. const char *san_b);
  5584. /**
  5585. * lws_tls_acme_sni_csr_create() - creates a CSR and related private key PEM
  5586. *
  5587. * \param context: lws_context used for random
  5588. * \param elements: array of LWS_TLS_REQ_ELEMENT_COUNT const char *
  5589. * \param csr: buffer that will get the b64URL(ASN-1 CSR)
  5590. * \param csr_len: max length of the csr buffer
  5591. * \param privkey_pem: pointer to pointer allocated to hold the privkey_pem
  5592. * \param privkey_len: pointer to size_t set to the length of the privkey_pem
  5593. *
  5594. * Creates a CSR according to the information in \p elements, and a private
  5595. * RSA key used to sign the CSR.
  5596. *
  5597. * The outputs are the b64URL(ASN-1 CSR) into csr, and the PEM private key into
  5598. * privkey_pem.
  5599. *
  5600. * Notice that \p elements points to an array of const char *s pointing to the
  5601. * information listed in the enum above. If an entry is NULL or an empty
  5602. * string, the element is set to "none" in the CSR.
  5603. *
  5604. * Returns 0 on success or nonzero for failure.
  5605. */
  5606. LWS_VISIBLE LWS_EXTERN int
  5607. lws_tls_acme_sni_csr_create(struct lws_context *context, const char *elements[],
  5608. uint8_t *csr, size_t csr_len, char **privkey_pem,
  5609. size_t *privkey_len);
  5610. /**
  5611. * lws_tls_cert_updated() - update every vhost using the given cert path
  5612. *
  5613. * \param context: our lws_context
  5614. * \param certpath: the filepath to the certificate
  5615. * \param keypath: the filepath to the private key of the certificate
  5616. * \param mem_cert: copy of the cert in memory
  5617. * \param len_mem_cert: length of the copy of the cert in memory
  5618. * \param mem_privkey: copy of the private key in memory
  5619. * \param len_mem_privkey: length of the copy of the private key in memory
  5620. *
  5621. * Checks every vhost to see if it is the using certificate described by the
  5622. * the given filepaths. If so, it attempts to update the vhost ssl_ctx to use
  5623. * the new certificate.
  5624. *
  5625. * Returns 0 on success or nonzero for failure.
  5626. */
  5627. LWS_VISIBLE LWS_EXTERN int
  5628. lws_tls_cert_updated(struct lws_context *context, const char *certpath,
  5629. const char *keypath,
  5630. const char *mem_cert, size_t len_mem_cert,
  5631. const char *mem_privkey, size_t len_mem_privkey);
  5632. ///@}
  5633. /** \defgroup lws_ring LWS Ringbuffer APIs
  5634. * ##lws_ring: generic ringbuffer struct
  5635. *
  5636. * Provides an abstract ringbuffer api supporting one head and one or an
  5637. * unlimited number of tails.
  5638. *
  5639. * All of the members are opaque and manipulated by lws_ring_...() apis.
  5640. *
  5641. * The lws_ring and its buffer is allocated at runtime on the heap, using
  5642. *
  5643. * - lws_ring_create()
  5644. * - lws_ring_destroy()
  5645. *
  5646. * It may contain any type, the size of the "element" stored in the ring
  5647. * buffer and the number of elements is given at creation time.
  5648. *
  5649. * When you create the ringbuffer, you can optionally provide an element
  5650. * destroy callback that frees any allocations inside the element. This is then
  5651. * automatically called for elements with no tail behind them, ie, elements
  5652. * which don't have any pending consumer are auto-freed.
  5653. *
  5654. * Whole elements may be inserted into the ringbuffer and removed from it, using
  5655. *
  5656. * - lws_ring_insert()
  5657. * - lws_ring_consume()
  5658. *
  5659. * You can find out how many whole elements are free or waiting using
  5660. *
  5661. * - lws_ring_get_count_free_elements()
  5662. * - lws_ring_get_count_waiting_elements()
  5663. *
  5664. * In addition there are special purpose optional byte-centric apis
  5665. *
  5666. * - lws_ring_next_linear_insert_range()
  5667. * - lws_ring_bump_head()
  5668. *
  5669. * which let you, eg, read() directly into the ringbuffer without needing
  5670. * an intermediate bounce buffer.
  5671. *
  5672. * The accessors understand that the ring wraps, and optimizes insertion and
  5673. * consumption into one or two memcpy()s depending on if the head or tail
  5674. * wraps.
  5675. *
  5676. * lws_ring only supports a single head, but optionally multiple tails with
  5677. * an API to inform it when the "oldest" tail has moved on. You can give
  5678. * NULL where-ever an api asks for a tail pointer, and it will use an internal
  5679. * single tail pointer for convenience.
  5680. *
  5681. * The "oldest tail", which is the only tail if you give it NULL instead of
  5682. * some other tail, is used to track which elements in the ringbuffer are
  5683. * still unread by anyone.
  5684. *
  5685. * - lws_ring_update_oldest_tail()
  5686. */
  5687. ///@{
  5688. struct lws_ring;
  5689. /**
  5690. * lws_ring_create(): create a new ringbuffer
  5691. *
  5692. * \param element_len: the size in bytes of one element in the ringbuffer
  5693. * \param count: the number of elements the ringbuffer can contain
  5694. * \param destroy_element: NULL, or callback to be called for each element
  5695. * that is removed from the ringbuffer due to the
  5696. * oldest tail moving beyond it
  5697. *
  5698. * Creates the ringbuffer and allocates the storage. Returns the new
  5699. * lws_ring *, or NULL if the allocation failed.
  5700. *
  5701. * If non-NULL, destroy_element will get called back for every element that is
  5702. * retired from the ringbuffer after the oldest tail has gone past it, and for
  5703. * any element still left in the ringbuffer when it is destroyed. It replaces
  5704. * all other element destruction code in your user code.
  5705. */
  5706. LWS_VISIBLE LWS_EXTERN struct lws_ring *
  5707. lws_ring_create(size_t element_len, size_t count,
  5708. void (*destroy_element)(void *element));
  5709. /**
  5710. * lws_ring_destroy(): destroy a previously created ringbuffer
  5711. *
  5712. * \param ring: the struct lws_ring to destroy
  5713. *
  5714. * Destroys the ringbuffer allocation and the struct lws_ring itself.
  5715. */
  5716. LWS_VISIBLE LWS_EXTERN void
  5717. lws_ring_destroy(struct lws_ring *ring);
  5718. /**
  5719. * lws_ring_get_count_free_elements(): return how many elements can fit
  5720. * in the free space
  5721. *
  5722. * \param ring: the struct lws_ring to report on
  5723. *
  5724. * Returns how much room is left in the ringbuffer for whole element insertion.
  5725. */
  5726. LWS_VISIBLE LWS_EXTERN size_t
  5727. lws_ring_get_count_free_elements(struct lws_ring *ring);
  5728. /**
  5729. * lws_ring_get_count_waiting_elements(): return how many elements can be consumed
  5730. *
  5731. * \param ring: the struct lws_ring to report on
  5732. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  5733. *
  5734. * Returns how many elements are waiting to be consumed from the perspective
  5735. * of the tail pointer given.
  5736. */
  5737. LWS_VISIBLE LWS_EXTERN size_t
  5738. lws_ring_get_count_waiting_elements(struct lws_ring *ring, uint32_t *tail);
  5739. /**
  5740. * lws_ring_insert(): attempt to insert up to max_count elements from src
  5741. *
  5742. * \param ring: the struct lws_ring to report on
  5743. * \param src: the array of elements to be inserted
  5744. * \param max_count: the number of available elements at src
  5745. *
  5746. * Attempts to insert as many of the elements at src as possible, up to the
  5747. * maximum max_count. Returns the number of elements actually inserted.
  5748. */
  5749. LWS_VISIBLE LWS_EXTERN size_t
  5750. lws_ring_insert(struct lws_ring *ring, const void *src, size_t max_count);
  5751. /**
  5752. * lws_ring_consume(): attempt to copy out and remove up to max_count elements
  5753. * to src
  5754. *
  5755. * \param ring: the struct lws_ring to report on
  5756. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  5757. * \param dest: the array of elements to be inserted. or NULL for no copy
  5758. * \param max_count: the number of available elements at src
  5759. *
  5760. * Attempts to copy out as many waiting elements as possible into dest, from
  5761. * the perspective of the given tail, up to max_count. If dest is NULL, the
  5762. * copying out is not done but the elements are logically consumed as usual.
  5763. * NULL dest is useful in combination with lws_ring_get_element(), where you
  5764. * can use the element direct from the ringbuffer and then call this with NULL
  5765. * dest to logically consume it.
  5766. *
  5767. * Increments the tail position according to how many elements could be
  5768. * consumed.
  5769. *
  5770. * Returns the number of elements consumed.
  5771. */
  5772. LWS_VISIBLE LWS_EXTERN size_t
  5773. lws_ring_consume(struct lws_ring *ring, uint32_t *tail, void *dest,
  5774. size_t max_count);
  5775. /**
  5776. * lws_ring_get_element(): get a pointer to the next waiting element for tail
  5777. *
  5778. * \param ring: the struct lws_ring to report on
  5779. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  5780. *
  5781. * Points to the next element that tail would consume, directly in the
  5782. * ringbuffer. This lets you write() or otherwise use the element without
  5783. * having to copy it out somewhere first.
  5784. *
  5785. * After calling this, you must call lws_ring_consume(ring, &tail, NULL, 1)
  5786. * which will logically consume the element you used up and increment your
  5787. * tail (tail may also be NULL there if you use a single tail).
  5788. *
  5789. * Returns NULL if no waiting element, or a const void * pointing to it.
  5790. */
  5791. LWS_VISIBLE LWS_EXTERN const void *
  5792. lws_ring_get_element(struct lws_ring *ring, uint32_t *tail);
  5793. /**
  5794. * lws_ring_update_oldest_tail(): free up elements older than tail for reuse
  5795. *
  5796. * \param ring: the struct lws_ring to report on
  5797. * \param tail: a pointer to the tail struct to use, or NULL for single tail
  5798. *
  5799. * If you are using multiple tails, you must use this API to inform the
  5800. * lws_ring when none of the tails still need elements in the fifo any more,
  5801. * by updating it when the "oldest" tail has moved on.
  5802. */
  5803. LWS_VISIBLE LWS_EXTERN void
  5804. lws_ring_update_oldest_tail(struct lws_ring *ring, uint32_t tail);
  5805. /**
  5806. * lws_ring_get_oldest_tail(): get current oldest available data index
  5807. *
  5808. * \param ring: the struct lws_ring to report on
  5809. *
  5810. * If you are initializing a new ringbuffer consumer, you can set its tail to
  5811. * this to start it from the oldest ringbuffer entry still available.
  5812. */
  5813. LWS_VISIBLE LWS_EXTERN uint32_t
  5814. lws_ring_get_oldest_tail(struct lws_ring *ring);
  5815. /**
  5816. * lws_ring_next_linear_insert_range(): used to write directly into the ring
  5817. *
  5818. * \param ring: the struct lws_ring to report on
  5819. * \param start: pointer to a void * set to the start of the next ringbuffer area
  5820. * \param bytes: pointer to a size_t set to the max length you may use from *start
  5821. *
  5822. * This provides a low-level, bytewise access directly into the ringbuffer
  5823. * allowing direct insertion of data without having to use a bounce buffer.
  5824. *
  5825. * The api reports the position and length of the next linear range that can
  5826. * be written in the ringbuffer, ie, up to the point it would wrap, and sets
  5827. * *start and *bytes accordingly. You can then, eg, directly read() into
  5828. * *start for up to *bytes, and use lws_ring_bump_head() to update the lws_ring
  5829. * with what you have done.
  5830. *
  5831. * Returns nonzero if no insertion is currently possible.
  5832. */
  5833. LWS_VISIBLE LWS_EXTERN int
  5834. lws_ring_next_linear_insert_range(struct lws_ring *ring, void **start,
  5835. size_t *bytes);
  5836. /**
  5837. * lws_ring_bump_head(): used to write directly into the ring
  5838. *
  5839. * \param ring: the struct lws_ring to operate on
  5840. * \param bytes: the number of bytes you inserted at the current head
  5841. */
  5842. LWS_VISIBLE LWS_EXTERN void
  5843. lws_ring_bump_head(struct lws_ring *ring, size_t bytes);
  5844. LWS_VISIBLE LWS_EXTERN void
  5845. lws_ring_dump(struct lws_ring *ring, uint32_t *tail);
  5846. /*
  5847. * This is a helper that combines the common pattern of needing to consume
  5848. * some ringbuffer elements, move the consumer tail on, and check if that
  5849. * has moved any ringbuffer elements out of scope, because it was the last
  5850. * consumer that had not already consumed them.
  5851. *
  5852. * Elements that go out of scope because the oldest tail is now after them
  5853. * get garbage-collected by calling the destroy_element callback on them
  5854. * defined when the ringbuffer was created.
  5855. */
  5856. #define lws_ring_consume_and_update_oldest_tail(\
  5857. ___ring, /* the lws_ring object */ \
  5858. ___type, /* type of objects with tails */ \
  5859. ___ptail, /* ptr to tail of obj with tail doing consuming */ \
  5860. ___count, /* count of payload objects being consumed */ \
  5861. ___list_head, /* head of list of objects with tails */ \
  5862. ___mtail, /* member name of tail in ___type */ \
  5863. ___mlist /* member name of next list member ptr in ___type */ \
  5864. ) { \
  5865. int ___n, ___m; \
  5866. \
  5867. ___n = lws_ring_get_oldest_tail(___ring) == *(___ptail); \
  5868. lws_ring_consume(___ring, ___ptail, NULL, ___count); \
  5869. if (___n) { \
  5870. uint32_t ___oldest; \
  5871. ___n = 0; \
  5872. ___oldest = *(___ptail); \
  5873. lws_start_foreach_llp(___type **, ___ppss, ___list_head) { \
  5874. ___m = lws_ring_get_count_waiting_elements( \
  5875. ___ring, &(*___ppss)->tail); \
  5876. if (___m >= ___n) { \
  5877. ___n = ___m; \
  5878. ___oldest = (*___ppss)->tail; \
  5879. } \
  5880. } lws_end_foreach_llp(___ppss, ___mlist); \
  5881. \
  5882. lws_ring_update_oldest_tail(___ring, ___oldest); \
  5883. } \
  5884. }
  5885. /*
  5886. * This does the same as the lws_ring_consume_and_update_oldest_tail()
  5887. * helper, but for the simpler case there is only one consumer, so one
  5888. * tail, and that tail is always the oldest tail.
  5889. */
  5890. #define lws_ring_consume_single_tail(\
  5891. ___ring, /* the lws_ring object */ \
  5892. ___ptail, /* ptr to tail of obj with tail doing consuming */ \
  5893. ___count /* count of payload objects being consumed */ \
  5894. ) { \
  5895. lws_ring_consume(___ring, ___ptail, NULL, ___count); \
  5896. lws_ring_update_oldest_tail(___ring, *(___ptail)); \
  5897. }
  5898. ///@}
  5899. /** \defgroup sha SHA and B64 helpers
  5900. * ##SHA and B64 helpers
  5901. *
  5902. * These provide SHA-1 and B64 helper apis
  5903. */
  5904. ///@{
  5905. #ifdef LWS_SHA1_USE_OPENSSL_NAME
  5906. #define lws_SHA1 SHA1
  5907. #else
  5908. /**
  5909. * lws_SHA1(): make a SHA-1 digest of a buffer
  5910. *
  5911. * \param d: incoming buffer
  5912. * \param n: length of incoming buffer
  5913. * \param md: buffer for message digest (must be >= 20 bytes)
  5914. *
  5915. * Reduces any size buffer into a 20-byte SHA-1 hash.
  5916. */
  5917. LWS_VISIBLE LWS_EXTERN unsigned char *
  5918. lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
  5919. #endif
  5920. /**
  5921. * lws_b64_encode_string(): encode a string into base 64
  5922. *
  5923. * \param in: incoming buffer
  5924. * \param in_len: length of incoming buffer
  5925. * \param out: result buffer
  5926. * \param out_size: length of result buffer
  5927. *
  5928. * Encodes a string using b64
  5929. */
  5930. LWS_VISIBLE LWS_EXTERN int
  5931. lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
  5932. /**
  5933. * lws_b64_encode_string_url(): encode a string into base 64
  5934. *
  5935. * \param in: incoming buffer
  5936. * \param in_len: length of incoming buffer
  5937. * \param out: result buffer
  5938. * \param out_size: length of result buffer
  5939. *
  5940. * Encodes a string using b64 with the "URL" variant (+ -> -, and / -> _)
  5941. */
  5942. LWS_VISIBLE LWS_EXTERN int
  5943. lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size);
  5944. /**
  5945. * lws_b64_decode_string(): decode a string from base 64
  5946. *
  5947. * \param in: incoming buffer
  5948. * \param out: result buffer
  5949. * \param out_size: length of result buffer
  5950. *
  5951. * Decodes a NUL-terminated string using b64
  5952. */
  5953. LWS_VISIBLE LWS_EXTERN int
  5954. lws_b64_decode_string(const char *in, char *out, int out_size);
  5955. /**
  5956. * lws_b64_decode_string_len(): decode a string from base 64
  5957. *
  5958. * \param in: incoming buffer
  5959. * \param in_len: length of incoming buffer
  5960. * \param out: result buffer
  5961. * \param out_size: length of result buffer
  5962. *
  5963. * Decodes a range of chars using b64
  5964. */
  5965. LWS_VISIBLE LWS_EXTERN int
  5966. lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size);
  5967. ///@}
  5968. /*! \defgroup cgi cgi handling
  5969. *
  5970. * ##CGI handling
  5971. *
  5972. * These functions allow low-level control over stdin/out/err of the cgi.
  5973. *
  5974. * However for most cases, binding the cgi to http in and out, the default
  5975. * lws implementation already does the right thing.
  5976. */
  5977. enum lws_enum_stdinouterr {
  5978. LWS_STDIN = 0,
  5979. LWS_STDOUT = 1,
  5980. LWS_STDERR = 2,
  5981. };
  5982. enum lws_cgi_hdr_state {
  5983. LCHS_HEADER,
  5984. LCHS_CR1,
  5985. LCHS_LF1,
  5986. LCHS_CR2,
  5987. LCHS_LF2,
  5988. LHCS_RESPONSE,
  5989. LHCS_DUMP_HEADERS,
  5990. LHCS_PAYLOAD,
  5991. LCHS_SINGLE_0A,
  5992. };
  5993. struct lws_cgi_args {
  5994. struct lws **stdwsi; /**< get fd with lws_get_socket_fd() */
  5995. enum lws_enum_stdinouterr ch; /**< channel index */
  5996. unsigned char *data; /**< for messages with payload */
  5997. enum lws_cgi_hdr_state hdr_state; /**< track where we are in cgi headers */
  5998. int len; /**< length */
  5999. };
  6000. #ifdef LWS_WITH_CGI
  6001. /**
  6002. * lws_cgi: spawn network-connected cgi process
  6003. *
  6004. * \param wsi: connection to own the process
  6005. * \param exec_array: array of "exec-name" "arg1" ... "argn" NULL
  6006. * \param script_uri_path_len: how many chars on the left of the uri are the
  6007. * path to the cgi, or -1 to spawn without URL-related env vars
  6008. * \param timeout_secs: seconds script should be allowed to run
  6009. * \param mp_cgienv: pvo list with per-vhost cgi options to put in env
  6010. */
  6011. LWS_VISIBLE LWS_EXTERN int
  6012. lws_cgi(struct lws *wsi, const char * const *exec_array,
  6013. int script_uri_path_len, int timeout_secs,
  6014. const struct lws_protocol_vhost_options *mp_cgienv);
  6015. /**
  6016. * lws_cgi_write_split_stdout_headers: write cgi output accounting for header part
  6017. *
  6018. * \param wsi: connection to own the process
  6019. */
  6020. LWS_VISIBLE LWS_EXTERN int
  6021. lws_cgi_write_split_stdout_headers(struct lws *wsi);
  6022. /**
  6023. * lws_cgi_kill: terminate cgi process associated with wsi
  6024. *
  6025. * \param wsi: connection to own the process
  6026. */
  6027. LWS_VISIBLE LWS_EXTERN int
  6028. lws_cgi_kill(struct lws *wsi);
  6029. /**
  6030. * lws_cgi_get_stdwsi: get wsi for stdin, stdout, or stderr
  6031. *
  6032. * \param wsi: parent wsi that has cgi
  6033. * \param ch: which of LWS_STDIN, LWS_STDOUT or LWS_STDERR
  6034. */
  6035. LWS_VISIBLE LWS_EXTERN struct lws *
  6036. lws_cgi_get_stdwsi(struct lws *wsi, enum lws_enum_stdinouterr ch);
  6037. #endif
  6038. ///@}
  6039. /*! \defgroup fops file operation wrapping
  6040. *
  6041. * ##File operation wrapping
  6042. *
  6043. * Use these helper functions if you want to access a file from the perspective
  6044. * of a specific wsi, which is usually the case. If you just want contextless
  6045. * file access, use the fops callbacks directly with NULL wsi instead of these
  6046. * helpers.
  6047. *
  6048. * If so, then it calls the platform handler or user overrides where present
  6049. * (as defined in info->fops)
  6050. *
  6051. * The advantage from all this is user code can be portable for file operations
  6052. * without having to deal with differences between platforms.
  6053. */
  6054. //@{
  6055. /** struct lws_plat_file_ops - Platform-specific file operations
  6056. *
  6057. * These provide platform-agnostic ways to deal with filesystem access in the
  6058. * library and in the user code.
  6059. */
  6060. #if defined(LWS_WITH_ESP32)
  6061. /* sdk preprocessor defs? compiler issue? gets confused with member names */
  6062. #define LWS_FOP_OPEN _open
  6063. #define LWS_FOP_CLOSE _close
  6064. #define LWS_FOP_SEEK_CUR _seek_cur
  6065. #define LWS_FOP_READ _read
  6066. #define LWS_FOP_WRITE _write
  6067. #else
  6068. #define LWS_FOP_OPEN open
  6069. #define LWS_FOP_CLOSE close
  6070. #define LWS_FOP_SEEK_CUR seek_cur
  6071. #define LWS_FOP_READ read
  6072. #define LWS_FOP_WRITE write
  6073. #endif
  6074. #define LWS_FOP_FLAGS_MASK ((1 << 23) - 1)
  6075. #define LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP (1 << 24)
  6076. #define LWS_FOP_FLAG_COMPR_IS_GZIP (1 << 25)
  6077. #define LWS_FOP_FLAG_MOD_TIME_VALID (1 << 26)
  6078. #define LWS_FOP_FLAG_VIRTUAL (1 << 27)
  6079. struct lws_plat_file_ops;
  6080. struct lws_fop_fd {
  6081. lws_filefd_type fd;
  6082. /**< real file descriptor related to the file... */
  6083. const struct lws_plat_file_ops *fops;
  6084. /**< fops that apply to this fop_fd */
  6085. void *filesystem_priv;
  6086. /**< ignored by lws; owned by the fops handlers */
  6087. lws_filepos_t pos;
  6088. /**< generic "position in file" */
  6089. lws_filepos_t len;
  6090. /**< generic "length of file" */
  6091. lws_fop_flags_t flags;
  6092. /**< copy of the returned flags */
  6093. uint32_t mod_time;
  6094. /**< optional "modification time of file", only valid if .open()
  6095. * set the LWS_FOP_FLAG_MOD_TIME_VALID flag */
  6096. };
  6097. typedef struct lws_fop_fd *lws_fop_fd_t;
  6098. struct lws_fops_index {
  6099. const char *sig; /* NULL or vfs signature, eg, ".zip/" */
  6100. uint8_t len; /* length of above string */
  6101. };
  6102. struct lws_plat_file_ops {
  6103. lws_fop_fd_t (*LWS_FOP_OPEN)(const struct lws_plat_file_ops *fops,
  6104. const char *filename, const char *vpath,
  6105. lws_fop_flags_t *flags);
  6106. /**< Open file (always binary access if plat supports it)
  6107. * vpath may be NULL, or if the fops understands it, the point at which
  6108. * the filename's virtual part starts.
  6109. * *flags & LWS_FOP_FLAGS_MASK should be set to O_RDONLY or O_RDWR.
  6110. * If the file may be gzip-compressed,
  6111. * LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP is set. If it actually is
  6112. * gzip-compressed, then the open handler should OR
  6113. * LWS_FOP_FLAG_COMPR_IS_GZIP on to *flags before returning.
  6114. */
  6115. int (*LWS_FOP_CLOSE)(lws_fop_fd_t *fop_fd);
  6116. /**< close file AND set the pointer to NULL */
  6117. lws_fileofs_t (*LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd,
  6118. lws_fileofs_t offset_from_cur_pos);
  6119. /**< seek from current position */
  6120. int (*LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  6121. uint8_t *buf, lws_filepos_t len);
  6122. /**< Read from file, on exit *amount is set to amount actually read */
  6123. int (*LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  6124. uint8_t *buf, lws_filepos_t len);
  6125. /**< Write to file, on exit *amount is set to amount actually written */
  6126. struct lws_fops_index fi[3];
  6127. /**< vfs path signatures implying use of this fops */
  6128. const struct lws_plat_file_ops *next;
  6129. /**< NULL or next fops in list */
  6130. /* Add new things just above here ---^
  6131. * This is part of the ABI, don't needlessly break compatibility */
  6132. };
  6133. /**
  6134. * lws_get_fops() - get current file ops
  6135. *
  6136. * \param context: context
  6137. */
  6138. LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * LWS_WARN_UNUSED_RESULT
  6139. lws_get_fops(struct lws_context *context);
  6140. LWS_VISIBLE LWS_EXTERN void
  6141. lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops);
  6142. /**
  6143. * lws_vfs_tell() - get current file position
  6144. *
  6145. * \param fop_fd: fop_fd we are asking about
  6146. */
  6147. LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
  6148. lws_vfs_tell(lws_fop_fd_t fop_fd);
  6149. /**
  6150. * lws_vfs_get_length() - get current file total length in bytes
  6151. *
  6152. * \param fop_fd: fop_fd we are asking about
  6153. */
  6154. LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
  6155. lws_vfs_get_length(lws_fop_fd_t fop_fd);
  6156. /**
  6157. * lws_vfs_get_mod_time() - get time file last modified
  6158. *
  6159. * \param fop_fd: fop_fd we are asking about
  6160. */
  6161. LWS_VISIBLE LWS_EXTERN uint32_t LWS_WARN_UNUSED_RESULT
  6162. lws_vfs_get_mod_time(lws_fop_fd_t fop_fd);
  6163. /**
  6164. * lws_vfs_file_seek_set() - seek relative to start of file
  6165. *
  6166. * \param fop_fd: fop_fd we are seeking in
  6167. * \param offset: offset from start of file
  6168. */
  6169. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  6170. lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
  6171. /**
  6172. * lws_vfs_file_seek_end() - seek relative to end of file
  6173. *
  6174. * \param fop_fd: fop_fd we are seeking in
  6175. * \param offset: offset from start of file
  6176. */
  6177. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  6178. lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
  6179. extern struct lws_plat_file_ops fops_zip;
  6180. /**
  6181. * lws_plat_file_open() - open vfs filepath
  6182. *
  6183. * \param fops: file ops struct that applies to this descriptor
  6184. * \param vfs_path: filename to open
  6185. * \param flags: pointer to open flags
  6186. *
  6187. * The vfs_path is scanned for known fops signatures, and the open directed
  6188. * to any matching fops open.
  6189. *
  6190. * User code should use this api to perform vfs opens.
  6191. *
  6192. * returns semi-opaque handle
  6193. */
  6194. LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
  6195. lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
  6196. lws_fop_flags_t *flags);
  6197. /**
  6198. * lws_plat_file_close() - close file
  6199. *
  6200. * \param fop_fd: file handle to close
  6201. */
  6202. static LWS_INLINE int
  6203. lws_vfs_file_close(lws_fop_fd_t *fop_fd)
  6204. {
  6205. return (*fop_fd)->fops->LWS_FOP_CLOSE(fop_fd);
  6206. }
  6207. /**
  6208. * lws_plat_file_seek_cur() - close file
  6209. *
  6210. *
  6211. * \param fop_fd: file handle
  6212. * \param offset: position to seek to
  6213. */
  6214. static LWS_INLINE lws_fileofs_t
  6215. lws_vfs_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  6216. {
  6217. return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset);
  6218. }
  6219. /**
  6220. * lws_plat_file_read() - read from file
  6221. *
  6222. * \param fop_fd: file handle
  6223. * \param amount: how much to read (rewritten by call)
  6224. * \param buf: buffer to write to
  6225. * \param len: max length
  6226. */
  6227. static LWS_INLINE int LWS_WARN_UNUSED_RESULT
  6228. lws_vfs_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  6229. uint8_t *buf, lws_filepos_t len)
  6230. {
  6231. return fop_fd->fops->LWS_FOP_READ(fop_fd, amount, buf, len);
  6232. }
  6233. /**
  6234. * lws_plat_file_write() - write from file
  6235. *
  6236. * \param fop_fd: file handle
  6237. * \param amount: how much to write (rewritten by call)
  6238. * \param buf: buffer to read from
  6239. * \param len: max length
  6240. */
  6241. static LWS_INLINE int LWS_WARN_UNUSED_RESULT
  6242. lws_vfs_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  6243. uint8_t *buf, lws_filepos_t len)
  6244. {
  6245. return fop_fd->fops->LWS_FOP_WRITE(fop_fd, amount, buf, len);
  6246. }
  6247. /* these are the platform file operations implementations... they can
  6248. * be called directly and used in fops arrays
  6249. */
  6250. LWS_VISIBLE LWS_EXTERN lws_fop_fd_t
  6251. _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
  6252. const char *vpath, lws_fop_flags_t *flags);
  6253. LWS_VISIBLE LWS_EXTERN int
  6254. _lws_plat_file_close(lws_fop_fd_t *fop_fd);
  6255. LWS_VISIBLE LWS_EXTERN lws_fileofs_t
  6256. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
  6257. LWS_VISIBLE LWS_EXTERN int
  6258. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  6259. uint8_t *buf, lws_filepos_t len);
  6260. LWS_VISIBLE LWS_EXTERN int
  6261. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  6262. uint8_t *buf, lws_filepos_t len);
  6263. LWS_VISIBLE LWS_EXTERN int
  6264. lws_alloc_vfs_file(struct lws_context *context, const char *filename,
  6265. uint8_t **buf, lws_filepos_t *amount);
  6266. //@}
  6267. /** \defgroup smtp SMTP related functions
  6268. * ##SMTP related functions
  6269. * \ingroup lwsapi
  6270. *
  6271. * These apis let you communicate with a local SMTP server to send email from
  6272. * lws. It handles all the SMTP sequencing and protocol actions.
  6273. *
  6274. * Your system should have postfix, sendmail or another MTA listening on port
  6275. * 25 and able to send email using the "mail" commandline app. Usually distro
  6276. * MTAs are configured for this by default.
  6277. *
  6278. * It runs via its own libuv events if initialized (which requires giving it
  6279. * a libuv loop to attach to).
  6280. *
  6281. * It operates using three callbacks, on_next() queries if there is a new email
  6282. * to send, on_get_body() asks for the body of the email, and on_sent() is
  6283. * called after the email is successfully sent.
  6284. *
  6285. * To use it
  6286. *
  6287. * - create an lws_email struct
  6288. *
  6289. * - initialize data, loop, the email_* strings, max_content_size and
  6290. * the callbacks
  6291. *
  6292. * - call lws_email_init()
  6293. *
  6294. * When you have at least one email to send, call lws_email_check() to
  6295. * schedule starting to send it.
  6296. */
  6297. //@{
  6298. #ifdef LWS_WITH_SMTP
  6299. /** enum lwsgs_smtp_states - where we are in SMTP protocol sequence */
  6300. enum lwsgs_smtp_states {
  6301. LGSSMTP_IDLE, /**< awaiting new email */
  6302. LGSSMTP_CONNECTING, /**< opening tcp connection to MTA */
  6303. LGSSMTP_CONNECTED, /**< tcp connection to MTA is connected */
  6304. LGSSMTP_SENT_HELO, /**< sent the HELO */
  6305. LGSSMTP_SENT_FROM, /**< sent FROM */
  6306. LGSSMTP_SENT_TO, /**< sent TO */
  6307. LGSSMTP_SENT_DATA, /**< sent DATA request */
  6308. LGSSMTP_SENT_BODY, /**< sent the email body */
  6309. LGSSMTP_SENT_QUIT, /**< sent the session quit */
  6310. };
  6311. /** struct lws_email - abstract context for performing SMTP operations */
  6312. struct lws_email {
  6313. void *data;
  6314. /**< opaque pointer set by user code and available to the callbacks */
  6315. uv_loop_t *loop;
  6316. /**< the libuv loop we will work on */
  6317. char email_smtp_ip[32]; /**< Fill before init, eg, "127.0.0.1" */
  6318. char email_helo[32]; /**< Fill before init, eg, "myserver.com" */
  6319. char email_from[100]; /**< Fill before init or on_next */
  6320. char email_to[100]; /**< Fill before init or on_next */
  6321. unsigned int max_content_size;
  6322. /**< largest possible email body size */
  6323. /* Fill all the callbacks before init */
  6324. int (*on_next)(struct lws_email *email);
  6325. /**< (Fill in before calling lws_email_init)
  6326. * called when idle, 0 = another email to send, nonzero is idle.
  6327. * If you return 0, all of the email_* char arrays must be set
  6328. * to something useful. */
  6329. int (*on_sent)(struct lws_email *email);
  6330. /**< (Fill in before calling lws_email_init)
  6331. * called when transfer of the email to the SMTP server was
  6332. * successful, your callback would remove the current email
  6333. * from its queue */
  6334. int (*on_get_body)(struct lws_email *email, char *buf, int len);
  6335. /**< (Fill in before calling lws_email_init)
  6336. * called when the body part of the queued email is about to be
  6337. * sent to the SMTP server. */
  6338. /* private things */
  6339. uv_timer_t timeout_email; /**< private */
  6340. enum lwsgs_smtp_states estate; /**< private */
  6341. uv_connect_t email_connect_req; /**< private */
  6342. uv_tcp_t email_client; /**< private */
  6343. time_t email_connect_started; /**< private */
  6344. char email_buf[256]; /**< private */
  6345. char *content; /**< private */
  6346. };
  6347. /**
  6348. * lws_email_init() - Initialize a struct lws_email
  6349. *
  6350. * \param email: struct lws_email to init
  6351. * \param loop: libuv loop to use
  6352. * \param max_content: max email content size
  6353. *
  6354. * Prepares a struct lws_email for use ending SMTP
  6355. */
  6356. LWS_VISIBLE LWS_EXTERN int
  6357. lws_email_init(struct lws_email *email, uv_loop_t *loop, int max_content);
  6358. /**
  6359. * lws_email_check() - Request check for new email
  6360. *
  6361. * \param email: struct lws_email context to check
  6362. *
  6363. * Schedules a check for new emails in 1s... call this when you have queued an
  6364. * email for send.
  6365. */
  6366. LWS_VISIBLE LWS_EXTERN void
  6367. lws_email_check(struct lws_email *email);
  6368. /**
  6369. * lws_email_destroy() - stop using the struct lws_email
  6370. *
  6371. * \param email: the struct lws_email context
  6372. *
  6373. * Stop sending email using email and free allocations
  6374. */
  6375. LWS_VISIBLE LWS_EXTERN void
  6376. lws_email_destroy(struct lws_email *email);
  6377. #endif
  6378. //@}
  6379. /** \defgroup lejp JSON parser
  6380. * ##JSON parsing related functions
  6381. * \ingroup lwsapi
  6382. *
  6383. * LEJP is an extremely lightweight JSON stream parser included in lws.
  6384. */
  6385. //@{
  6386. struct lejp_ctx;
  6387. #define LWS_ARRAY_SIZE(_x) (sizeof(_x) / sizeof(_x[0]))
  6388. #define LEJP_FLAG_WS_KEEP 64
  6389. #define LEJP_FLAG_WS_COMMENTLINE 32
  6390. enum lejp_states {
  6391. LEJP_IDLE = 0,
  6392. LEJP_MEMBERS = 1,
  6393. LEJP_M_P = 2,
  6394. LEJP_MP_STRING = LEJP_FLAG_WS_KEEP | 3,
  6395. LEJP_MP_STRING_ESC = LEJP_FLAG_WS_KEEP | 4,
  6396. LEJP_MP_STRING_ESC_U1 = LEJP_FLAG_WS_KEEP | 5,
  6397. LEJP_MP_STRING_ESC_U2 = LEJP_FLAG_WS_KEEP | 6,
  6398. LEJP_MP_STRING_ESC_U3 = LEJP_FLAG_WS_KEEP | 7,
  6399. LEJP_MP_STRING_ESC_U4 = LEJP_FLAG_WS_KEEP | 8,
  6400. LEJP_MP_DELIM = 9,
  6401. LEJP_MP_VALUE = 10,
  6402. LEJP_MP_VALUE_NUM_INT = LEJP_FLAG_WS_KEEP | 11,
  6403. LEJP_MP_VALUE_NUM_EXP = LEJP_FLAG_WS_KEEP | 12,
  6404. LEJP_MP_VALUE_TOK = LEJP_FLAG_WS_KEEP | 13,
  6405. LEJP_MP_COMMA_OR_END = 14,
  6406. LEJP_MP_ARRAY_END = 15,
  6407. };
  6408. enum lejp_reasons {
  6409. LEJP_CONTINUE = -1,
  6410. LEJP_REJECT_IDLE_NO_BRACE = -2,
  6411. LEJP_REJECT_MEMBERS_NO_CLOSE = -3,
  6412. LEJP_REJECT_MP_NO_OPEN_QUOTE = -4,
  6413. LEJP_REJECT_MP_STRING_UNDERRUN = -5,
  6414. LEJP_REJECT_MP_ILLEGAL_CTRL = -6,
  6415. LEJP_REJECT_MP_STRING_ESC_ILLEGAL_ESC = -7,
  6416. LEJP_REJECT_ILLEGAL_HEX = -8,
  6417. LEJP_REJECT_MP_DELIM_MISSING_COLON = -9,
  6418. LEJP_REJECT_MP_DELIM_BAD_VALUE_START = -10,
  6419. LEJP_REJECT_MP_VAL_NUM_INT_NO_FRAC = -11,
  6420. LEJP_REJECT_MP_VAL_NUM_FORMAT = -12,
  6421. LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP = -13,
  6422. LEJP_REJECT_MP_VAL_TOK_UNKNOWN = -14,
  6423. LEJP_REJECT_MP_C_OR_E_UNDERF = -15,
  6424. LEJP_REJECT_MP_C_OR_E_NOTARRAY = -16,
  6425. LEJP_REJECT_MP_ARRAY_END_MISSING = -17,
  6426. LEJP_REJECT_STACK_OVERFLOW = -18,
  6427. LEJP_REJECT_MP_DELIM_ISTACK = -19,
  6428. LEJP_REJECT_NUM_TOO_LONG = -20,
  6429. LEJP_REJECT_MP_C_OR_E_NEITHER = -21,
  6430. LEJP_REJECT_UNKNOWN = -22,
  6431. LEJP_REJECT_CALLBACK = -23
  6432. };
  6433. #define LEJP_FLAG_CB_IS_VALUE 64
  6434. enum lejp_callbacks {
  6435. LEJPCB_CONSTRUCTED = 0,
  6436. LEJPCB_DESTRUCTED = 1,
  6437. LEJPCB_START = 2,
  6438. LEJPCB_COMPLETE = 3,
  6439. LEJPCB_FAILED = 4,
  6440. LEJPCB_PAIR_NAME = 5,
  6441. LEJPCB_VAL_TRUE = LEJP_FLAG_CB_IS_VALUE | 6,
  6442. LEJPCB_VAL_FALSE = LEJP_FLAG_CB_IS_VALUE | 7,
  6443. LEJPCB_VAL_NULL = LEJP_FLAG_CB_IS_VALUE | 8,
  6444. LEJPCB_VAL_NUM_INT = LEJP_FLAG_CB_IS_VALUE | 9,
  6445. LEJPCB_VAL_NUM_FLOAT = LEJP_FLAG_CB_IS_VALUE | 10,
  6446. LEJPCB_VAL_STR_START = 11, /* notice handle separately */
  6447. LEJPCB_VAL_STR_CHUNK = LEJP_FLAG_CB_IS_VALUE | 12,
  6448. LEJPCB_VAL_STR_END = LEJP_FLAG_CB_IS_VALUE | 13,
  6449. LEJPCB_ARRAY_START = 14,
  6450. LEJPCB_ARRAY_END = 15,
  6451. LEJPCB_OBJECT_START = 16,
  6452. LEJPCB_OBJECT_END = 17
  6453. };
  6454. /**
  6455. * _lejp_callback() - User parser actions
  6456. * \param ctx: LEJP context
  6457. * \param reason: Callback reason
  6458. *
  6459. * Your user callback is associated with the context at construction time,
  6460. * and receives calls as the parsing progresses.
  6461. *
  6462. * All of the callbacks may be ignored and just return 0.
  6463. *
  6464. * The reasons it might get called, found in @reason, are:
  6465. *
  6466. * LEJPCB_CONSTRUCTED: The context was just constructed... you might want to
  6467. * perform one-time allocation for the life of the context.
  6468. *
  6469. * LEJPCB_DESTRUCTED: The context is being destructed... if you made any
  6470. * allocations at construction-time, you can free them now
  6471. *
  6472. * LEJPCB_START: Parsing is beginning at the first byte of input
  6473. *
  6474. * LEJPCB_COMPLETE: Parsing has completed successfully. You'll get a 0 or
  6475. * positive return code from lejp_parse indicating the
  6476. * amount of unused bytes left in the input buffer
  6477. *
  6478. * LEJPCB_FAILED: Parsing failed. You'll get a negative error code
  6479. * returned from lejp_parse
  6480. *
  6481. * LEJPCB_PAIR_NAME: When a "name":"value" pair has had the name parsed,
  6482. * this callback occurs. You can find the new name at
  6483. * the end of ctx->path[]
  6484. *
  6485. * LEJPCB_VAL_TRUE: The "true" value appeared
  6486. *
  6487. * LEJPCB_VAL_FALSE: The "false" value appeared
  6488. *
  6489. * LEJPCB_VAL_NULL: The "null" value appeared
  6490. *
  6491. * LEJPCB_VAL_NUM_INT: A string representing an integer is in ctx->buf
  6492. *
  6493. * LEJPCB_VAL_NUM_FLOAT: A string representing a float is in ctx->buf
  6494. *
  6495. * LEJPCB_VAL_STR_START: We are starting to parse a string, no data yet
  6496. *
  6497. * LEJPCB_VAL_STR_CHUNK: We parsed LEJP_STRING_CHUNK -1 bytes of string data in
  6498. * ctx->buf, which is as much as we can buffer, so we are
  6499. * spilling it. If all your strings are less than
  6500. * LEJP_STRING_CHUNK - 1 bytes, you will never see this
  6501. * callback.
  6502. *
  6503. * LEJPCB_VAL_STR_END: String parsing has completed, the last chunk of the
  6504. * string is in ctx->buf.
  6505. *
  6506. * LEJPCB_ARRAY_START: An array started
  6507. *
  6508. * LEJPCB_ARRAY_END: An array ended
  6509. *
  6510. * LEJPCB_OBJECT_START: An object started
  6511. *
  6512. * LEJPCB_OBJECT_END: An object ended
  6513. */
  6514. LWS_EXTERN signed char _lejp_callback(struct lejp_ctx *ctx, char reason);
  6515. typedef signed char (*lejp_callback)(struct lejp_ctx *ctx, char reason);
  6516. #ifndef LEJP_MAX_DEPTH
  6517. #define LEJP_MAX_DEPTH 12
  6518. #endif
  6519. #ifndef LEJP_MAX_INDEX_DEPTH
  6520. #define LEJP_MAX_INDEX_DEPTH 5
  6521. #endif
  6522. #ifndef LEJP_MAX_PATH
  6523. #define LEJP_MAX_PATH 128
  6524. #endif
  6525. #ifndef LEJP_STRING_CHUNK
  6526. /* must be >= 30 to assemble floats */
  6527. #define LEJP_STRING_CHUNK 254
  6528. #endif
  6529. enum num_flags {
  6530. LEJP_SEEN_MINUS = (1 << 0),
  6531. LEJP_SEEN_POINT = (1 << 1),
  6532. LEJP_SEEN_POST_POINT = (1 << 2),
  6533. LEJP_SEEN_EXP = (1 << 3)
  6534. };
  6535. struct _lejp_stack {
  6536. char s; /* lejp_state stack*/
  6537. char p; /* path length */
  6538. char i; /* index array length */
  6539. char b; /* user bitfield */
  6540. };
  6541. struct lejp_ctx {
  6542. /* sorted by type for most compact alignment
  6543. *
  6544. * pointers
  6545. */
  6546. signed char (*callback)(struct lejp_ctx *ctx, char reason);
  6547. void *user;
  6548. const char * const *paths;
  6549. /* arrays */
  6550. struct _lejp_stack st[LEJP_MAX_DEPTH];
  6551. uint16_t i[LEJP_MAX_INDEX_DEPTH]; /* index array */
  6552. uint16_t wild[LEJP_MAX_INDEX_DEPTH]; /* index array */
  6553. char path[LEJP_MAX_PATH];
  6554. char buf[LEJP_STRING_CHUNK + 1];
  6555. /* int */
  6556. uint32_t line;
  6557. /* short */
  6558. uint16_t uni;
  6559. /* char */
  6560. uint8_t npos;
  6561. uint8_t dcount;
  6562. uint8_t f;
  6563. uint8_t sp; /* stack head */
  6564. uint8_t ipos; /* index stack depth */
  6565. uint8_t ppos;
  6566. uint8_t count_paths;
  6567. uint8_t path_match;
  6568. uint8_t path_match_len;
  6569. uint8_t wildcount;
  6570. };
  6571. LWS_VISIBLE LWS_EXTERN void
  6572. lejp_construct(struct lejp_ctx *ctx,
  6573. signed char (*callback)(struct lejp_ctx *ctx, char reason),
  6574. void *user, const char * const *paths, unsigned char paths_count);
  6575. LWS_VISIBLE LWS_EXTERN void
  6576. lejp_destruct(struct lejp_ctx *ctx);
  6577. LWS_VISIBLE LWS_EXTERN int
  6578. lejp_parse(struct lejp_ctx *ctx, const unsigned char *json, int len);
  6579. LWS_VISIBLE LWS_EXTERN void
  6580. lejp_change_callback(struct lejp_ctx *ctx,
  6581. signed char (*callback)(struct lejp_ctx *ctx, char reason));
  6582. LWS_VISIBLE LWS_EXTERN int
  6583. lejp_get_wildcard(struct lejp_ctx *ctx, int wildcard, char *dest, int len);
  6584. //@}
  6585. /*
  6586. * Stats are all uint64_t numbers that start at 0.
  6587. * Index names here have the convention
  6588. *
  6589. * _C_ counter
  6590. * _B_ byte count
  6591. * _MS_ millisecond count
  6592. */
  6593. enum {
  6594. LWSSTATS_C_CONNECTIONS, /**< count incoming connections */
  6595. LWSSTATS_C_API_CLOSE, /**< count calls to close api */
  6596. LWSSTATS_C_API_READ, /**< count calls to read from socket api */
  6597. LWSSTATS_C_API_LWS_WRITE, /**< count calls to lws_write API */
  6598. LWSSTATS_C_API_WRITE, /**< count calls to write API */
  6599. LWSSTATS_C_WRITE_PARTIALS, /**< count of partial writes */
  6600. LWSSTATS_C_WRITEABLE_CB_REQ, /**< count of writable callback requests */
  6601. LWSSTATS_C_WRITEABLE_CB_EFF_REQ, /**< count of effective writable callback requests */
  6602. LWSSTATS_C_WRITEABLE_CB, /**< count of writable callbacks */
  6603. LWSSTATS_C_SSL_CONNECTIONS_FAILED, /**< count of failed SSL connections */
  6604. LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED, /**< count of accepted SSL connections */
  6605. LWSSTATS_C_SSL_CONNECTIONS_ACCEPT_SPIN, /**< count of SSL_accept() attempts */
  6606. LWSSTATS_C_SSL_CONNS_HAD_RX, /**< count of accepted SSL conns that have had some RX */
  6607. LWSSTATS_C_TIMEOUTS, /**< count of timed-out connections */
  6608. LWSSTATS_C_SERVICE_ENTRY, /**< count of entries to lws service loop */
  6609. LWSSTATS_B_READ, /**< aggregate bytes read */
  6610. LWSSTATS_B_WRITE, /**< aggregate bytes written */
  6611. LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, /**< aggreate of size of accepted write data from new partials */
  6612. LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY, /**< aggregate delay in accepting connection */
  6613. LWSSTATS_MS_WRITABLE_DELAY, /**< aggregate delay between asking for writable and getting cb */
  6614. LWSSTATS_MS_WORST_WRITABLE_DELAY, /**< single worst delay between asking for writable and getting cb */
  6615. LWSSTATS_MS_SSL_RX_DELAY, /**< aggregate delay between ssl accept complete and first RX */
  6616. LWSSTATS_C_PEER_LIMIT_AH_DENIED, /**< number of times we would have given an ah but for the peer limit */
  6617. LWSSTATS_C_PEER_LIMIT_WSI_DENIED, /**< number of times we would have given a wsi but for the peer limit */
  6618. /* Add new things just above here ---^
  6619. * This is part of the ABI, don't needlessly break compatibility */
  6620. LWSSTATS_SIZE
  6621. };
  6622. #if defined(LWS_WITH_STATS)
  6623. LWS_VISIBLE LWS_EXTERN uint64_t
  6624. lws_stats_get(struct lws_context *context, int index);
  6625. LWS_VISIBLE LWS_EXTERN void
  6626. lws_stats_log_dump(struct lws_context *context);
  6627. #else
  6628. static LWS_INLINE uint64_t
  6629. lws_stats_get(struct lws_context *context, int index) { (void)context; (void)index; return 0; }
  6630. static LWS_INLINE void
  6631. lws_stats_log_dump(struct lws_context *context) { (void)context; }
  6632. #endif
  6633. #ifdef __cplusplus
  6634. }
  6635. #endif
  6636. #endif