mongoose.h 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883
  1. #ifdef MG_MODULE_LINES
  2. #line 1 "mongoose/src/common.h"
  3. #endif
  4. /*
  5. * Copyright (c) 2004-2013 Sergey Lyubka
  6. * Copyright (c) 2013-2015 Cesanta Software Limited
  7. * All rights reserved
  8. *
  9. * This software is dual-licensed: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation. For the terms of this
  12. * license, see <http://www.gnu.org/licenses/>.
  13. *
  14. * You are free to use this software under the terms of the GNU General
  15. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  16. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. *
  19. * Alternatively, you can license this software under a commercial
  20. * license, as set out in <https://www.cesanta.com/license>.
  21. */
  22. #ifndef CS_MONGOOSE_SRC_COMMON_H_
  23. #define CS_MONGOOSE_SRC_COMMON_H_
  24. #define MG_VERSION "6.7"
  25. /* Local tweaks, applied before any of Mongoose's own headers. */
  26. #if 1//def MG_LOCALS
  27. #include <mg_locals.h>
  28. #endif
  29. #endif /* CS_MONGOOSE_SRC_COMMON_H_ */
  30. #ifdef MG_MODULE_LINES
  31. #line 1 "common/platform.h"
  32. #endif
  33. #ifndef CS_COMMON_PLATFORM_H_
  34. #define CS_COMMON_PLATFORM_H_
  35. #define MG_ARCH_CUSTOM 0 // User creates its own mongoose_config.h
  36. #define MG_ARCH_UNIX 1 // Linux, BSD, Mac, ...
  37. #define MG_ARCH_WIN32 2 // Windows
  38. #define MG_ARCH_ESP32 3 // ESP32
  39. #define MG_ARCH_ESP8266 4 // ESP8266
  40. #define MG_ARCH_FREERTOS 5 // FreeRTOS
  41. #define MG_ARCH_AZURERTOS 6 // MS Azure RTOS
  42. #define MG_ARCH_ZEPHYR 7 // Zephyr RTOS
  43. #define MG_ARCH_NEWLIB 8 // Bare metal ARM
  44. #define MG_ARCH_CMSIS_RTOS1 9 // CMSIS-RTOS API v1 (Keil RTX)
  45. #define MG_ARCH_TIRTOS 10 // Texas Semi TI-RTOS
  46. #define MG_ARCH_RP2040 11 // Raspberry Pi RP2040
  47. #define MG_ARCH_ARMCC 12 // Keil MDK-Core with Configuration Wizard
  48. #define MG_ARCH_CMSIS_RTOS2 13 // CMSIS-RTOS API v2 (Keil RTX5, FreeRTOS)
  49. #define MG_ARCH_RTTHREAD 14 // RT-Thread RTOS
  50. /*
  51. * For the "custom" platform, includes and dependencies can be
  52. * provided through mg_locals.h.
  53. */
  54. #define CS_P_CUSTOM 0
  55. #define CS_P_UNIX 1
  56. #define CS_P_WINDOWS 2
  57. #define CS_P_ESP32 15
  58. #define CS_P_ESP8266 3
  59. #define CS_P_CC3200 4
  60. #define CS_P_MSP432 5
  61. #define CS_P_CC3100 6
  62. #define CS_P_TM4C129 14
  63. #define CS_P_MBED 7
  64. #define CS_P_WINCE 8
  65. #define CS_P_NXP_LPC 13
  66. #define CS_P_NXP_KINETIS 9
  67. #define CS_P_NRF51 12
  68. #define CS_P_NRF52 10
  69. #define CS_P_PIC32 11
  70. #define CS_P_STM32 16
  71. /* Next id: 17 */
  72. /* If not specified explicitly, we guess platform by defines. */
  73. #ifndef CS_PLATFORM
  74. #if defined(TARGET_IS_MSP432P4XX) || defined(__MSP432P401R__)
  75. #define CS_PLATFORM CS_P_MSP432
  76. #elif defined(cc3200)
  77. #define CS_PLATFORM CS_P_CC3200
  78. #elif defined(__unix__) || defined(__APPLE__)
  79. #define CS_PLATFORM CS_P_UNIX
  80. #elif defined(WINCE)
  81. #define CS_PLATFORM CS_P_WINCE
  82. #elif defined(_WIN32)
  83. #define CS_PLATFORM CS_P_WINDOWS
  84. #elif defined(__MBED__)
  85. #define CS_PLATFORM CS_P_MBED
  86. #elif defined(__USE_LPCOPEN)
  87. #define CS_PLATFORM CS_P_NXP_LPC
  88. #elif defined(FRDM_K64F) || defined(FREEDOM)
  89. #define CS_PLATFORM CS_P_NXP_KINETIS
  90. #elif defined(PIC32)
  91. #define CS_PLATFORM CS_P_PIC32
  92. #elif defined(ESP_PLATFORM)
  93. #define CS_PLATFORM CS_P_ESP32
  94. #elif defined(ICACHE_FLASH)
  95. #define CS_PLATFORM CS_P_ESP8266
  96. #elif defined(TARGET_IS_TM4C129_RA0) || defined(TARGET_IS_TM4C129_RA1) || \
  97. defined(TARGET_IS_TM4C129_RA2)
  98. #define CS_PLATFORM CS_P_TM4C129
  99. #elif defined(STM32)
  100. #define CS_PLATFORM CS_P_STM32
  101. #endif
  102. #ifndef CS_PLATFORM
  103. #error "CS_PLATFORM is not specified and we couldn't guess it."
  104. #endif
  105. #endif /* !defined(CS_PLATFORM) */
  106. #define MG_NET_IF_SOCKET 1
  107. #define MG_NET_IF_SIMPLELINK 2
  108. #define MG_NET_IF_LWIP_LOW_LEVEL 3
  109. #define MG_NET_IF_PIC32 4
  110. #define MG_SSL_IF_OPENSSL 1
  111. #define MG_SSL_IF_MBEDTLS 2
  112. #define MG_SSL_IF_SIMPLELINK 3
  113. /* Amalgamated: #include "common/platforms/platform_unix.h" */
  114. /* Amalgamated: #include "common/platforms/platform_windows.h" */
  115. /* Amalgamated: #include "common/platforms/platform_esp32.h" */
  116. /* Amalgamated: #include "common/platforms/platform_esp8266.h" */
  117. /* Amalgamated: #include "common/platforms/platform_cc3200.h" */
  118. /* Amalgamated: #include "common/platforms/platform_cc3100.h" */
  119. /* Amalgamated: #include "common/platforms/platform_mbed.h" */
  120. /* Amalgamated: #include "common/platforms/platform_nrf51.h" */
  121. /* Amalgamated: #include "common/platforms/platform_nrf52.h" */
  122. /* Amalgamated: #include "common/platforms/platform_wince.h" */
  123. /* Amalgamated: #include "common/platforms/platform_nxp_lpc.h" */
  124. /* Amalgamated: #include "common/platforms/platform_nxp_kinetis.h" */
  125. /* Amalgamated: #include "common/platforms/platform_pic32.h" */
  126. /* Amalgamated: #include "common/platforms/platform_stm32.h" */
  127. /* Common stuff */
  128. #if !defined(WEAK)
  129. #if (defined(__GNUC__) || defined(__TI_COMPILER_VERSION__)) && !defined(_WIN32)
  130. #define WEAK __attribute__((weak))
  131. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  132. #define WEAK __weak
  133. #else
  134. #define WEAK
  135. #endif
  136. #endif
  137. #ifdef __GNUC__
  138. #define NORETURN __attribute__((noreturn))
  139. #define NOINLINE __attribute__((noinline))
  140. #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  141. #define NOINSTR __attribute__((no_instrument_function))
  142. #define DO_NOT_WARN_UNUSED __attribute__((unused))
  143. #else
  144. #define NORETURN
  145. #define NOINLINE
  146. #define WARN_UNUSED_RESULT
  147. #define NOINSTR
  148. #define DO_NOT_WARN_UNUSED
  149. #endif /* __GNUC__ */
  150. #ifndef ARRAY_SIZE
  151. #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
  152. #endif
  153. #endif /* CS_COMMON_PLATFORM_H_ */
  154. #ifdef MG_MODULE_LINES
  155. #line 1 "common/platforms/platform_windows.h"
  156. #endif
  157. #ifndef CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_
  158. #define CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_
  159. #if CS_PLATFORM == CS_P_WINDOWS
  160. /*
  161. * MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
  162. * MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
  163. * MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
  164. * MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
  165. * MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
  166. * MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
  167. * MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio 2003)
  168. * MSVC++ 7.0 _MSC_VER == 1300
  169. * MSVC++ 6.0 _MSC_VER == 1200
  170. * MSVC++ 5.0 _MSC_VER == 1100
  171. */
  172. #ifdef _MSC_VER
  173. #pragma warning(disable : 4127) /* FD_SET() emits warning, disable it */
  174. #pragma warning(disable : 4204) /* missing c99 support */
  175. #endif
  176. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  177. #define _WINSOCK_DEPRECATED_NO_WARNINGS 1
  178. #endif
  179. #ifndef _CRT_SECURE_NO_WARNINGS
  180. #define _CRT_SECURE_NO_WARNINGS
  181. #endif
  182. #include <assert.h>
  183. #include <direct.h>
  184. #include <errno.h>
  185. #include <fcntl.h>
  186. #include <io.h>
  187. #include <limits.h>
  188. #include <signal.h>
  189. #include <stddef.h>
  190. #include <stdio.h>
  191. #include <stdlib.h>
  192. #include <sys/stat.h>
  193. #include <time.h>
  194. #ifdef _MSC_VER
  195. #pragma comment(lib, "ws2_32.lib") /* Linking with winsock library */
  196. #endif
  197. #include <winsock2.h>
  198. #include <ws2tcpip.h>
  199. #include <windows.h>
  200. #include <process.h>
  201. #if defined(_MSC_VER) && _MSC_VER >= 1800
  202. #define strdup _strdup
  203. #endif
  204. #ifndef EINPROGRESS
  205. #define EINPROGRESS WSAEINPROGRESS
  206. #endif
  207. #ifndef EWOULDBLOCK
  208. #define EWOULDBLOCK WSAEWOULDBLOCK
  209. #endif
  210. #ifndef __func__
  211. #define STRX(x) #x
  212. #define STR(x) STRX(x)
  213. #define __func__ __FILE__ ":" STR(__LINE__)
  214. #endif
  215. #define snprintf _snprintf
  216. #define fileno _fileno
  217. #define vsnprintf _vsnprintf
  218. #define sleep(x) Sleep((x) *1000)
  219. #define to64(x) _atoi64(x)
  220. #if !defined(__MINGW32__) && !defined(__MINGW64__)
  221. #define popen(x, y) _popen((x), (y))
  222. #define pclose(x) _pclose(x)
  223. #endif
  224. #define rmdir _rmdir
  225. #if defined(_MSC_VER) && _MSC_VER >= 1400
  226. #define fseeko(x, y, z) _fseeki64((x), (y), (z))
  227. #else
  228. #define fseeko(x, y, z) fseek((x), (y), (z))
  229. #endif
  230. #if defined(_MSC_VER) && _MSC_VER <= 1200
  231. typedef unsigned long uintptr_t;
  232. typedef long intptr_t;
  233. #endif
  234. typedef int socklen_t;
  235. #if _MSC_VER >= 1700
  236. #include <stdint.h>
  237. #else
  238. typedef signed char int8_t;
  239. typedef unsigned char uint8_t;
  240. typedef int int32_t;
  241. typedef unsigned int uint32_t;
  242. typedef short int16_t;
  243. typedef unsigned short uint16_t;
  244. typedef __int64 int64_t;
  245. typedef unsigned __int64 uint64_t;
  246. #endif
  247. typedef SOCKET sock_t;
  248. typedef uint32_t in_addr_t;
  249. #ifndef UINT16_MAX
  250. #define UINT16_MAX 65535
  251. #endif
  252. #ifndef UINT32_MAX
  253. #define UINT32_MAX 4294967295
  254. #endif
  255. #ifndef pid_t
  256. #define pid_t HANDLE
  257. #endif
  258. #define INT64_FMT "I64d"
  259. #define INT64_X_FMT "I64x"
  260. #define SIZE_T_FMT "Iu"
  261. typedef struct _stati64 cs_stat_t;
  262. #ifndef S_ISDIR
  263. #define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
  264. #endif
  265. #ifndef S_ISREG
  266. #define S_ISREG(x) (((x) &_S_IFMT) == _S_IFREG)
  267. #endif
  268. #define DIRSEP '\\'
  269. #define CS_DEFINE_DIRENT
  270. #ifndef va_copy
  271. #ifdef __va_copy
  272. #define va_copy __va_copy
  273. #else
  274. #define va_copy(x, y) (x) = (y)
  275. #endif
  276. #endif
  277. #ifndef MG_MAX_HTTP_REQUEST_SIZE
  278. #define MG_MAX_HTTP_REQUEST_SIZE 8192
  279. #endif
  280. #ifndef MG_MAX_HTTP_SEND_MBUF
  281. #define MG_MAX_HTTP_SEND_MBUF 4096
  282. #endif
  283. #ifndef MG_MAX_HTTP_HEADERS
  284. #define MG_MAX_HTTP_HEADERS 40
  285. #endif
  286. #ifndef CS_ENABLE_STDIO
  287. #define CS_ENABLE_STDIO 1
  288. #endif
  289. #ifndef MG_ENABLE_BROADCAST
  290. #define MG_ENABLE_BROADCAST 1
  291. #endif
  292. #ifndef MG_ENABLE_DIRECTORY_LISTING
  293. #define MG_ENABLE_DIRECTORY_LISTING 1
  294. #endif
  295. #ifndef MG_ENABLE_FILESYSTEM
  296. #define MG_ENABLE_FILESYSTEM 1
  297. #endif
  298. #ifndef MG_ENABLE_HTTP_CGI
  299. #define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM
  300. #endif
  301. #ifndef MG_NET_IF
  302. #define MG_NET_IF MG_NET_IF_SOCKET
  303. #endif
  304. #endif /* CS_PLATFORM == CS_P_WINDOWS */
  305. #endif /* CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_ */
  306. #ifdef MG_MODULE_LINES
  307. #line 1 "common/platforms/platform_unix.h"
  308. #endif
  309. #ifndef CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_
  310. #define CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_
  311. #if CS_PLATFORM == CS_P_UNIX
  312. #ifndef _XOPEN_SOURCE
  313. #define _XOPEN_SOURCE 600
  314. #endif
  315. /* <inttypes.h> wants this for C++ */
  316. #ifndef __STDC_FORMAT_MACROS
  317. #define __STDC_FORMAT_MACROS
  318. #endif
  319. /* C++ wants that for INT64_MAX */
  320. #ifndef __STDC_LIMIT_MACROS
  321. #define __STDC_LIMIT_MACROS
  322. #endif
  323. /* Enable fseeko() and ftello() functions */
  324. #ifndef _LARGEFILE_SOURCE
  325. #define _LARGEFILE_SOURCE
  326. #endif
  327. /* Enable 64-bit file offsets */
  328. #ifndef _FILE_OFFSET_BITS
  329. #define _FILE_OFFSET_BITS 64
  330. #endif
  331. #include <arpa/inet.h>
  332. #include <assert.h>
  333. #include <ctype.h>
  334. #include <dirent.h>
  335. #include <errno.h>
  336. #include <fcntl.h>
  337. #include <inttypes.h>
  338. #include <stdint.h>
  339. #include <limits.h>
  340. #include <math.h>
  341. #include <netdb.h>
  342. #include <netinet/in.h>
  343. #include <pthread.h>
  344. #include <signal.h>
  345. #include <stdarg.h>
  346. #include <stdio.h>
  347. #include <stdlib.h>
  348. #include <string.h>
  349. #include <sys/param.h>
  350. #include <sys/socket.h>
  351. #include <sys/select.h>
  352. #include <sys/stat.h>
  353. #include <sys/time.h>
  354. #include <sys/types.h>
  355. #include <unistd.h>
  356. #ifdef __APPLE__
  357. #include <machine/endian.h>
  358. #ifndef BYTE_ORDER
  359. #define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  360. #define BIG_ENDIAN __DARWIN_BIG_ENDIAN
  361. #define PDP_ENDIAN __DARWIN_PDP_ENDIAN
  362. #define BYTE_ORDER __DARWIN_BYTE_ORDER
  363. #endif
  364. #endif
  365. /*
  366. * osx correctly avoids defining strtoll when compiling in strict ansi mode.
  367. * c++ 11 standard defines strtoll as well.
  368. * We require strtoll, and if your embedded pre-c99 compiler lacks one, please
  369. * implement a shim.
  370. */
  371. #if !(defined(__cplusplus) && __cplusplus >= 201103L) && \
  372. !(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L)
  373. long long strtoll(const char *, char **, int);
  374. #endif
  375. typedef int sock_t;
  376. #define INVALID_SOCKET (-1)
  377. #define SIZE_T_FMT "zu"
  378. typedef struct stat cs_stat_t;
  379. #define DIRSEP '/'
  380. #define to64(x) strtoll(x, NULL, 10)
  381. #define INT64_FMT PRId64
  382. #define INT64_X_FMT PRIx64
  383. #ifndef __cdecl
  384. #define __cdecl
  385. #endif
  386. #ifndef va_copy
  387. #ifdef __va_copy
  388. #define va_copy __va_copy
  389. #else
  390. #define va_copy(x, y) (x) = (y)
  391. #endif
  392. #endif
  393. #define closesocket(x) close(x)
  394. #ifndef MG_MAX_HTTP_REQUEST_SIZE
  395. #define MG_MAX_HTTP_REQUEST_SIZE 8192
  396. #endif
  397. #ifndef MG_MAX_HTTP_SEND_MBUF
  398. #define MG_MAX_HTTP_SEND_MBUF 4096
  399. #endif
  400. #ifndef MG_MAX_HTTP_HEADERS
  401. #define MG_MAX_HTTP_HEADERS 40
  402. #endif
  403. #ifndef CS_ENABLE_STDIO
  404. #define CS_ENABLE_STDIO 1
  405. #endif
  406. #ifndef MG_ENABLE_BROADCAST
  407. #define MG_ENABLE_BROADCAST 1
  408. #endif
  409. #ifndef MG_ENABLE_DIRECTORY_LISTING
  410. #define MG_ENABLE_DIRECTORY_LISTING 1
  411. #endif
  412. #ifndef MG_ENABLE_FILESYSTEM
  413. #define MG_ENABLE_FILESYSTEM 1
  414. #endif
  415. #ifndef MG_ENABLE_HTTP_CGI
  416. #define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM
  417. #endif
  418. #ifndef MG_NET_IF
  419. #define MG_NET_IF MG_NET_IF_SOCKET
  420. #endif
  421. #endif /* CS_PLATFORM == CS_P_UNIX */
  422. #endif /* CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_ */
  423. #ifdef MG_MODULE_LINES
  424. #line 1 "common/platforms/platform_esp32.h"
  425. #endif
  426. /*
  427. * Copyright (c) 2014-2016 Cesanta Software Limited
  428. * All rights reserved
  429. */
  430. #ifndef CS_COMMON_PLATFORMS_PLATFORM_ESP32_H_
  431. #define CS_COMMON_PLATFORMS_PLATFORM_ESP32_H_
  432. #if CS_PLATFORM == CS_P_ESP32
  433. #include <assert.h>
  434. #include <ctype.h>
  435. #include <dirent.h>
  436. #include <fcntl.h>
  437. #include <inttypes.h>
  438. #include <machine/endian.h>
  439. #include <stdint.h>
  440. #include <string.h>
  441. #include <sys/stat.h>
  442. #include <sys/time.h>
  443. #define SIZE_T_FMT "u"
  444. typedef struct stat cs_stat_t;
  445. #define DIRSEP '/'
  446. #define to64(x) strtoll(x, NULL, 10)
  447. #define INT64_FMT PRId64
  448. #define INT64_X_FMT PRIx64
  449. #define __cdecl
  450. #define _FILE_OFFSET_BITS 32
  451. #define MG_LWIP 1
  452. #ifndef MG_NET_IF
  453. #define MG_NET_IF MG_NET_IF_SOCKET
  454. #endif
  455. #ifndef CS_ENABLE_STDIO
  456. #define CS_ENABLE_STDIO 1
  457. #endif
  458. #endif /* CS_PLATFORM == CS_P_ESP32 */
  459. #endif /* CS_COMMON_PLATFORMS_PLATFORM_ESP32_H_ */
  460. #ifdef MG_MODULE_LINES
  461. #line 1 "common/platforms/platform_esp8266.h"
  462. #endif
  463. /*
  464. * Copyright (c) 2014-2016 Cesanta Software Limited
  465. * All rights reserved
  466. */
  467. #ifndef CS_COMMON_PLATFORMS_PLATFORM_ESP8266_H_
  468. #define CS_COMMON_PLATFORMS_PLATFORM_ESP8266_H_
  469. #if CS_PLATFORM == CS_P_ESP8266
  470. #include <assert.h>
  471. #include <ctype.h>
  472. #include <fcntl.h>
  473. #include <inttypes.h>
  474. #include <machine/endian.h>
  475. #include <string.h>
  476. #include <sys/stat.h>
  477. #include <sys/time.h>
  478. #define SIZE_T_FMT "u"
  479. typedef struct stat cs_stat_t;
  480. #define DIRSEP '/'
  481. #define CS_DEFINE_DIRENT
  482. #define to64(x) strtoll(x, NULL, 10)
  483. #define INT64_FMT PRId64
  484. #define INT64_X_FMT PRIx64
  485. #define __cdecl
  486. #define _FILE_OFFSET_BITS 32
  487. #ifndef RTOS_SDK
  488. #define fileno(x) -1
  489. #endif
  490. #define MG_LWIP 1
  491. /* struct timeval is defined in sys/time.h. */
  492. #define LWIP_TIMEVAL_PRIVATE 0
  493. #ifndef MG_NET_IF
  494. #include <lwip/opt.h>
  495. #if LWIP_SOCKET /* RTOS SDK has LWIP sockets */
  496. # define MG_NET_IF MG_NET_IF_SOCKET
  497. #else
  498. # define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
  499. #endif
  500. #endif
  501. #ifndef CS_ENABLE_STDIO
  502. #define CS_ENABLE_STDIO 1
  503. #endif
  504. #endif /* CS_PLATFORM == CS_P_ESP8266 */
  505. #endif /* CS_COMMON_PLATFORMS_PLATFORM_ESP8266_H_ */
  506. #ifdef MG_MODULE_LINES
  507. #line 1 "common/platforms/platform_cc3100.h"
  508. #endif
  509. /*
  510. * Copyright (c) 2014-2016 Cesanta Software Limited
  511. * All rights reserved
  512. */
  513. #ifndef CS_COMMON_PLATFORMS_PLATFORM_CC3100_H_
  514. #define CS_COMMON_PLATFORMS_PLATFORM_CC3100_H_
  515. #if CS_PLATFORM == CS_P_CC3100
  516. #include <assert.h>
  517. #include <ctype.h>
  518. #include <errno.h>
  519. #include <inttypes.h>
  520. #include <stdint.h>
  521. #include <string.h>
  522. #include <time.h>
  523. #define MG_NET_IF MG_NET_IF_SIMPLELINK
  524. #define MG_SSL_IF MG_SSL_IF_SIMPLELINK
  525. /*
  526. * CC3100 SDK and STM32 SDK include headers w/out path, just like
  527. * #include "simplelink.h". As result, we have to add all required directories
  528. * into Makefile IPATH and do the same thing (include w/out path)
  529. */
  530. #include <simplelink.h>
  531. #include <netapp.h>
  532. #undef timeval
  533. typedef int sock_t;
  534. #define INVALID_SOCKET (-1)
  535. #define to64(x) strtoll(x, NULL, 10)
  536. #define INT64_FMT PRId64
  537. #define INT64_X_FMT PRIx64
  538. #define SIZE_T_FMT "u"
  539. #define SOMAXCONN 8
  540. const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  541. char *inet_ntoa(struct in_addr in);
  542. int inet_pton(int af, const char *src, void *dst);
  543. #endif /* CS_PLATFORM == CS_P_CC3100 */
  544. #endif /* CS_COMMON_PLATFORMS_PLATFORM_CC3100_H_ */
  545. #ifdef MG_MODULE_LINES
  546. #line 1 "common/platforms/platform_cc3200.h"
  547. #endif
  548. /*
  549. * Copyright (c) 2014-2016 Cesanta Software Limited
  550. * All rights reserved
  551. */
  552. #ifndef CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_
  553. #define CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_
  554. #if CS_PLATFORM == CS_P_CC3200
  555. #include <assert.h>
  556. #include <ctype.h>
  557. #include <errno.h>
  558. #include <inttypes.h>
  559. #include <stdint.h>
  560. #include <string.h>
  561. #include <time.h>
  562. #ifndef __TI_COMPILER_VERSION__
  563. #include <fcntl.h>
  564. #include <sys/time.h>
  565. #endif
  566. #define MG_NET_IF MG_NET_IF_SIMPLELINK
  567. #define MG_SSL_IF MG_SSL_IF_SIMPLELINK
  568. /* Only SPIFFS supports directories, SLFS does not. */
  569. #if defined(CC3200_FS_SPIFFS) && !defined(MG_ENABLE_DIRECTORY_LISTING)
  570. #define MG_ENABLE_DIRECTORY_LISTING 1
  571. #endif
  572. /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */
  573. typedef int sock_t;
  574. #define INVALID_SOCKET (-1)
  575. #define SIZE_T_FMT "u"
  576. typedef struct stat cs_stat_t;
  577. #define DIRSEP '/'
  578. #define to64(x) strtoll(x, NULL, 10)
  579. #define INT64_FMT PRId64
  580. #define INT64_X_FMT PRIx64
  581. #define __cdecl
  582. #define fileno(x) -1
  583. /* Some functions we implement for Mongoose. */
  584. #ifdef __cplusplus
  585. extern "C" {
  586. #endif
  587. #ifdef __TI_COMPILER_VERSION__
  588. struct SlTimeval_t;
  589. #define timeval SlTimeval_t
  590. int gettimeofday(struct timeval *t, void *tz);
  591. int asprintf(char **strp, const char *fmt, ...);
  592. #endif
  593. /* TI's libc does not have stat & friends, add them. */
  594. #ifdef __TI_COMPILER_VERSION__
  595. #include <file.h>
  596. typedef unsigned int mode_t;
  597. typedef size_t _off_t;
  598. typedef long ssize_t;
  599. struct stat {
  600. int st_ino;
  601. mode_t st_mode;
  602. int st_nlink;
  603. time_t st_mtime;
  604. off_t st_size;
  605. };
  606. int _stat(const char *pathname, struct stat *st);
  607. #define stat(a, b) _stat(a, b)
  608. #define __S_IFMT 0170000
  609. #define __S_IFDIR 0040000
  610. #define __S_IFCHR 0020000
  611. #define __S_IFREG 0100000
  612. #define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask))
  613. #define S_IFDIR __S_IFDIR
  614. #define S_IFCHR __S_IFCHR
  615. #define S_IFREG __S_IFREG
  616. #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
  617. #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
  618. /* As of 5.2.7, TI compiler does not support va_copy() yet. */
  619. #define va_copy(apc, ap) ((apc) = (ap))
  620. #endif /* __TI_COMPILER_VERSION__ */
  621. #ifdef CC3200_FS_SLFS
  622. #define MG_FS_SLFS
  623. #endif
  624. #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && \
  625. !defined(MG_ENABLE_FILESYSTEM)
  626. #define MG_ENABLE_FILESYSTEM 1
  627. #define CS_DEFINE_DIRENT
  628. #endif
  629. #ifndef CS_ENABLE_STDIO
  630. #define CS_ENABLE_STDIO 1
  631. #endif
  632. #ifdef __cplusplus
  633. }
  634. #endif
  635. #endif /* CS_PLATFORM == CS_P_CC3200 */
  636. #endif /* CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_ */
  637. #ifdef MG_MODULE_LINES
  638. #line 1 "common/platforms/platform_msp432.h"
  639. #endif
  640. /*
  641. * Copyright (c) 2014-2016 Cesanta Software Limited
  642. * All rights reserved
  643. */
  644. #ifndef CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_
  645. #define CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_
  646. #if CS_PLATFORM == CS_P_MSP432
  647. #include <assert.h>
  648. #include <ctype.h>
  649. #include <errno.h>
  650. #include <inttypes.h>
  651. #include <stdint.h>
  652. #include <string.h>
  653. #include <time.h>
  654. #ifndef __TI_COMPILER_VERSION__
  655. #include <fcntl.h>
  656. #include <sys/time.h>
  657. #endif
  658. #define MG_NET_IF MG_NET_IF_SIMPLELINK
  659. #define MG_SSL_IF MG_SSL_IF_SIMPLELINK
  660. /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */
  661. typedef int sock_t;
  662. #define INVALID_SOCKET (-1)
  663. #define SIZE_T_FMT "u"
  664. typedef struct stat cs_stat_t;
  665. #define DIRSEP '/'
  666. #define to64(x) strtoll(x, NULL, 10)
  667. #define INT64_FMT PRId64
  668. #define INT64_X_FMT PRIx64
  669. #define __cdecl
  670. #define fileno(x) -1
  671. /* Some functions we implement for Mongoose. */
  672. #ifdef __cplusplus
  673. extern "C" {
  674. #endif
  675. #ifdef __TI_COMPILER_VERSION__
  676. struct SlTimeval_t;
  677. #define timeval SlTimeval_t
  678. int gettimeofday(struct timeval *t, void *tz);
  679. #endif
  680. /* TI's libc does not have stat & friends, add them. */
  681. #ifdef __TI_COMPILER_VERSION__
  682. #include <file.h>
  683. typedef unsigned int mode_t;
  684. typedef size_t _off_t;
  685. typedef long ssize_t;
  686. struct stat {
  687. int st_ino;
  688. mode_t st_mode;
  689. int st_nlink;
  690. time_t st_mtime;
  691. off_t st_size;
  692. };
  693. int _stat(const char *pathname, struct stat *st);
  694. #define stat(a, b) _stat(a, b)
  695. #define __S_IFMT 0170000
  696. #define __S_IFDIR 0040000
  697. #define __S_IFCHR 0020000
  698. #define __S_IFREG 0100000
  699. #define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask))
  700. #define S_IFDIR __S_IFDIR
  701. #define S_IFCHR __S_IFCHR
  702. #define S_IFREG __S_IFREG
  703. #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
  704. #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
  705. /* As of 5.2.7, TI compiler does not support va_copy() yet. */
  706. #define va_copy(apc, ap) ((apc) = (ap))
  707. #endif /* __TI_COMPILER_VERSION__ */
  708. #ifndef CS_ENABLE_STDIO
  709. #define CS_ENABLE_STDIO 1
  710. #endif
  711. #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && !defined(MG_ENABLE_FILESYSTEM)
  712. #define MG_ENABLE_FILESYSTEM 1
  713. #endif
  714. #ifdef __cplusplus
  715. }
  716. #endif
  717. #endif /* CS_PLATFORM == CS_P_MSP432 */
  718. #endif /* CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_ */
  719. #ifdef MG_MODULE_LINES
  720. #line 1 "common/platforms/platform_tm4c129.h"
  721. #endif
  722. /*
  723. * Copyright (c) 2014-2016 Cesanta Software Limited
  724. * All rights reserved
  725. */
  726. #ifndef CS_COMMON_PLATFORMS_PLATFORM_TM4C129_H_
  727. #define CS_COMMON_PLATFORMS_PLATFORM_TM4C129_H_
  728. #if CS_PLATFORM == CS_P_TM4C129
  729. #include <assert.h>
  730. #include <ctype.h>
  731. #include <errno.h>
  732. #include <inttypes.h>
  733. #include <stdint.h>
  734. #include <string.h>
  735. #include <time.h>
  736. #ifndef __TI_COMPILER_VERSION__
  737. #include <fcntl.h>
  738. #include <sys/time.h>
  739. #endif
  740. #define SIZE_T_FMT "u"
  741. typedef struct stat cs_stat_t;
  742. #define DIRSEP '/'
  743. #define to64(x) strtoll(x, NULL, 10)
  744. #define INT64_FMT PRId64
  745. #define INT64_X_FMT PRIx64
  746. #define __cdecl
  747. #ifndef MG_NET_IF
  748. # include <lwip/opt.h>
  749. # if LWIP_SOCKET
  750. # define MG_NET_IF MG_NET_IF_SOCKET
  751. # else
  752. # define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
  753. # endif
  754. # define MG_LWIP 1
  755. #elif MG_NET_IF == MG_NET_IF_SIMPLELINK
  756. # include "common/platforms/simplelink/cs_simplelink.h"
  757. #endif
  758. #ifndef CS_ENABLE_STDIO
  759. #define CS_ENABLE_STDIO 1
  760. #endif
  761. #ifdef __TI_COMPILER_VERSION__
  762. /* As of 5.2.8, TI compiler does not support va_copy() yet. */
  763. #define va_copy(apc, ap) ((apc) = (ap))
  764. #endif /* __TI_COMPILER_VERSION__ */
  765. #ifdef __cplusplus
  766. }
  767. #endif
  768. #endif /* CS_PLATFORM == CS_P_TM4C129 */
  769. #endif /* CS_COMMON_PLATFORMS_PLATFORM_TM4C129_H_ */
  770. #ifdef MG_MODULE_LINES
  771. #line 1 "common/platforms/platform_mbed.h"
  772. #endif
  773. /*
  774. * Copyright (c) 2014-2016 Cesanta Software Limited
  775. * All rights reserved
  776. */
  777. #ifndef CS_COMMON_PLATFORMS_PLATFORM_MBED_H_
  778. #define CS_COMMON_PLATFORMS_PLATFORM_MBED_H_
  779. #if CS_PLATFORM == CS_P_MBED
  780. /*
  781. * mbed.h contains C++ code (e.g. templates), thus, it should be processed
  782. * only if included directly to startup file (ex: main.cpp)
  783. */
  784. #ifdef __cplusplus
  785. /* Amalgamated: #include "mbed.h" */
  786. #endif /* __cplusplus */
  787. #include <assert.h>
  788. #include <ctype.h>
  789. #include <errno.h>
  790. #include <inttypes.h>
  791. #include <stdint.h>
  792. #include <string.h>
  793. #include <time.h>
  794. #include <sys/stat.h>
  795. #include <sys/types.h>
  796. #include <fcntl.h>
  797. #include <stdio.h>
  798. typedef struct stat cs_stat_t;
  799. #define DIRSEP '/'
  800. #ifndef CS_ENABLE_STDIO
  801. #define CS_ENABLE_STDIO 1
  802. #endif
  803. /*
  804. * mbed can be compiled with the ARM compiler which
  805. * just doesn't come with a gettimeofday shim
  806. * because it's a BSD API and ARM targets embedded
  807. * non-unix platforms.
  808. */
  809. #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
  810. #define _TIMEVAL_DEFINED
  811. #define gettimeofday _gettimeofday
  812. /* copied from GCC on ARM; for some reason useconds are signed */
  813. typedef long suseconds_t; /* microseconds (signed) */
  814. struct timeval {
  815. time_t tv_sec; /* seconds */
  816. suseconds_t tv_usec; /* and microseconds */
  817. };
  818. #endif
  819. #if MG_NET_IF == MG_NET_IF_SIMPLELINK
  820. #define MG_SIMPLELINK_NO_OSI 1
  821. #include <simplelink.h>
  822. typedef int sock_t;
  823. #define INVALID_SOCKET (-1)
  824. #define to64(x) strtoll(x, NULL, 10)
  825. #define INT64_FMT PRId64
  826. #define INT64_X_FMT PRIx64
  827. #define SIZE_T_FMT "u"
  828. #define SOMAXCONN 8
  829. const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  830. char *inet_ntoa(struct in_addr in);
  831. int inet_pton(int af, const char *src, void *dst);
  832. int inet_aton(const char *cp, struct in_addr *inp);
  833. in_addr_t inet_addr(const char *cp);
  834. #endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK */
  835. #endif /* CS_PLATFORM == CS_P_MBED */
  836. #endif /* CS_COMMON_PLATFORMS_PLATFORM_MBED_H_ */
  837. #ifdef MG_MODULE_LINES
  838. #line 1 "common/platforms/platform_nrf51.h"
  839. #endif
  840. /*
  841. * Copyright (c) 2014-2016 Cesanta Software Limited
  842. * All rights reserved
  843. */
  844. #ifndef CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_
  845. #define CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_
  846. #if CS_PLATFORM == CS_P_NRF51
  847. #include <assert.h>
  848. #include <ctype.h>
  849. #include <inttypes.h>
  850. #include <stdint.h>
  851. #include <string.h>
  852. #include <time.h>
  853. #define to64(x) strtoll(x, NULL, 10)
  854. #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
  855. #define MG_LWIP 1
  856. #define MG_ENABLE_IPV6 1
  857. /*
  858. * For ARM C Compiler, make lwip to export `struct timeval`; for other
  859. * compilers, suppress it.
  860. */
  861. #if !defined(__ARMCC_VERSION)
  862. # define LWIP_TIMEVAL_PRIVATE 0
  863. #else
  864. struct timeval;
  865. int gettimeofday(struct timeval *tp, void *tzp);
  866. #endif
  867. #define INT64_FMT PRId64
  868. #define SIZE_T_FMT "u"
  869. /*
  870. * ARM C Compiler doesn't have strdup, so we provide it
  871. */
  872. #define CS_ENABLE_STRDUP defined(__ARMCC_VERSION)
  873. #endif /* CS_PLATFORM == CS_P_NRF51 */
  874. #endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_ */
  875. #ifdef MG_MODULE_LINES
  876. #line 1 "common/platforms/platform_nrf52.h"
  877. #endif
  878. /*
  879. * Copyright (c) 2014-2016 Cesanta Software Limited
  880. * All rights reserved
  881. */
  882. #ifndef CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_
  883. #define CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_
  884. #if CS_PLATFORM == CS_P_NRF52
  885. #include <assert.h>
  886. #include <ctype.h>
  887. #include <errno.h>
  888. #include <inttypes.h>
  889. #include <stdint.h>
  890. #include <string.h>
  891. #include <time.h>
  892. #define to64(x) strtoll(x, NULL, 10)
  893. #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
  894. #define MG_LWIP 1
  895. #define MG_ENABLE_IPV6 1
  896. #if !defined(ENOSPC)
  897. # define ENOSPC 28 /* No space left on device */
  898. #endif
  899. /*
  900. * For ARM C Compiler, make lwip to export `struct timeval`; for other
  901. * compilers, suppress it.
  902. */
  903. #if !defined(__ARMCC_VERSION)
  904. # define LWIP_TIMEVAL_PRIVATE 0
  905. #endif
  906. #define INT64_FMT PRId64
  907. #define SIZE_T_FMT "u"
  908. /*
  909. * ARM C Compiler doesn't have strdup, so we provide it
  910. */
  911. #define CS_ENABLE_STRDUP defined(__ARMCC_VERSION)
  912. #endif /* CS_PLATFORM == CS_P_NRF52 */
  913. #endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_ */
  914. #ifdef MG_MODULE_LINES
  915. #line 1 "common/platforms/simplelink/cs_simplelink.h"
  916. #endif
  917. /*
  918. * Copyright (c) 2014-2016 Cesanta Software Limited
  919. * All rights reserved
  920. */
  921. #ifndef CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
  922. #define CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
  923. /* If simplelink.h is already included, all bets are off. */
  924. #if defined(MG_NET_IF) && MG_NET_IF == MG_NET_IF_SIMPLELINK && \
  925. !defined(__SIMPLELINK_H__)
  926. #include <stdbool.h>
  927. #ifndef __TI_COMPILER_VERSION__
  928. #undef __CONCAT
  929. #undef FD_CLR
  930. #undef FD_ISSET
  931. #undef FD_SET
  932. #undef FD_SETSIZE
  933. #undef FD_ZERO
  934. #undef fd_set
  935. #endif
  936. /* We want to disable SL_INC_STD_BSD_API_NAMING, so we include user.h ourselves
  937. * and undef it. */
  938. #define PROVISIONING_API_H_
  939. #include <simplelink/user.h>
  940. #undef PROVISIONING_API_H_
  941. #undef SL_INC_STD_BSD_API_NAMING
  942. #include <simplelink/include/simplelink.h>
  943. #include <simplelink/include/netapp.h>
  944. /* Now define only the subset of the BSD API that we use.
  945. * Notably, close(), read() and write() are not defined. */
  946. #define AF_INET SL_AF_INET
  947. #define socklen_t SlSocklen_t
  948. #define sockaddr SlSockAddr_t
  949. #define sockaddr_in SlSockAddrIn_t
  950. #define in_addr SlInAddr_t
  951. #define SOCK_STREAM SL_SOCK_STREAM
  952. #define SOCK_DGRAM SL_SOCK_DGRAM
  953. #define htonl sl_Htonl
  954. #define ntohl sl_Ntohl
  955. #define htons sl_Htons
  956. #define ntohs sl_Ntohs
  957. #ifndef EACCES
  958. #define EACCES SL_EACCES
  959. #endif
  960. #ifndef EAFNOSUPPORT
  961. #define EAFNOSUPPORT SL_EAFNOSUPPORT
  962. #endif
  963. #ifndef EAGAIN
  964. #define EAGAIN SL_EAGAIN
  965. #endif
  966. #ifndef EBADF
  967. #define EBADF SL_EBADF
  968. #endif
  969. #ifndef EINVAL
  970. #define EINVAL SL_EINVAL
  971. #endif
  972. #ifndef ENOMEM
  973. #define ENOMEM SL_ENOMEM
  974. #endif
  975. #ifndef EWOULDBLOCK
  976. #define EWOULDBLOCK SL_EWOULDBLOCK
  977. #endif
  978. #define SOMAXCONN 8
  979. #ifdef __cplusplus
  980. extern "C" {
  981. #endif
  982. const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  983. char *inet_ntoa(struct in_addr in);
  984. int inet_pton(int af, const char *src, void *dst);
  985. struct mg_mgr;
  986. struct mg_connection;
  987. typedef void (*mg_init_cb)(struct mg_mgr *mgr);
  988. bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init);
  989. void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg);
  990. int sl_fs_init(void);
  991. void sl_restart_cb(struct mg_mgr *mgr);
  992. int sl_set_ssl_opts(struct mg_connection *nc);
  993. #ifdef __cplusplus
  994. }
  995. #endif
  996. #endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(__SIMPLELINK_H__) */
  997. #endif /* CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_ */
  998. #ifdef MG_MODULE_LINES
  999. #line 1 "common/platforms/platform_wince.h"
  1000. #endif
  1001. #ifndef CS_COMMON_PLATFORMS_PLATFORM_WINCE_H_
  1002. #define CS_COMMON_PLATFORMS_PLATFORM_WINCE_H_
  1003. #if CS_PLATFORM == CS_P_WINCE
  1004. /*
  1005. * MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
  1006. * MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
  1007. * MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
  1008. * MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
  1009. * MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
  1010. * MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
  1011. * MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio 2003)
  1012. * MSVC++ 7.0 _MSC_VER == 1300
  1013. * MSVC++ 6.0 _MSC_VER == 1200
  1014. * MSVC++ 5.0 _MSC_VER == 1100
  1015. */
  1016. #pragma warning(disable : 4127) /* FD_SET() emits warning, disable it */
  1017. #pragma warning(disable : 4204) /* missing c99 support */
  1018. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  1019. #define _WINSOCK_DEPRECATED_NO_WARNINGS 1
  1020. #endif
  1021. #ifndef _CRT_SECURE_NO_WARNINGS
  1022. #define _CRT_SECURE_NO_WARNINGS
  1023. #endif
  1024. #include <assert.h>
  1025. #include <limits.h>
  1026. #include <stddef.h>
  1027. #include <stdio.h>
  1028. #include <stdlib.h>
  1029. #include <time.h>
  1030. #pragma comment(lib, "ws2.lib") /* Linking with WinCE winsock library */
  1031. #include <winsock2.h>
  1032. #include <ws2tcpip.h>
  1033. #include <windows.h>
  1034. #define strdup _strdup
  1035. #ifndef EINPROGRESS
  1036. #define EINPROGRESS WSAEINPROGRESS
  1037. #endif
  1038. #ifndef EWOULDBLOCK
  1039. #define EWOULDBLOCK WSAEWOULDBLOCK
  1040. #endif
  1041. #ifndef __func__
  1042. #define STRX(x) #x
  1043. #define STR(x) STRX(x)
  1044. #define __func__ __FILE__ ":" STR(__LINE__)
  1045. #endif
  1046. #define snprintf _snprintf
  1047. #define fileno _fileno
  1048. #define vsnprintf _vsnprintf
  1049. #define sleep(x) Sleep((x) *1000)
  1050. #define to64(x) _atoi64(x)
  1051. #define rmdir _rmdir
  1052. #if defined(_MSC_VER) && _MSC_VER >= 1400
  1053. #define fseeko(x, y, z) _fseeki64((x), (y), (z))
  1054. #else
  1055. #define fseeko(x, y, z) fseek((x), (y), (z))
  1056. #endif
  1057. typedef int socklen_t;
  1058. #if _MSC_VER >= 1700
  1059. #include <stdint.h>
  1060. #else
  1061. typedef signed char int8_t;
  1062. typedef unsigned char uint8_t;
  1063. typedef int int32_t;
  1064. typedef unsigned int uint32_t;
  1065. typedef short int16_t;
  1066. typedef unsigned short uint16_t;
  1067. typedef __int64 int64_t;
  1068. typedef unsigned __int64 uint64_t;
  1069. #endif
  1070. typedef SOCKET sock_t;
  1071. typedef uint32_t in_addr_t;
  1072. #ifndef UINT16_MAX
  1073. #define UINT16_MAX 65535
  1074. #endif
  1075. #ifndef UINT32_MAX
  1076. #define UINT32_MAX 4294967295
  1077. #endif
  1078. #ifndef pid_t
  1079. #define pid_t HANDLE
  1080. #endif
  1081. #define INT64_FMT "I64d"
  1082. #define INT64_X_FMT "I64x"
  1083. /* TODO(alashkin): check if this is correct */
  1084. #define SIZE_T_FMT "u"
  1085. #define DIRSEP '\\'
  1086. #define CS_DEFINE_DIRENT
  1087. #ifndef va_copy
  1088. #ifdef __va_copy
  1089. #define va_copy __va_copy
  1090. #else
  1091. #define va_copy(x, y) (x) = (y)
  1092. #endif
  1093. #endif
  1094. #ifndef MG_MAX_HTTP_REQUEST_SIZE
  1095. #define MG_MAX_HTTP_REQUEST_SIZE 8192
  1096. #endif
  1097. #ifndef MG_MAX_HTTP_SEND_MBUF
  1098. #define MG_MAX_HTTP_SEND_MBUF 4096
  1099. #endif
  1100. #ifndef MG_MAX_HTTP_HEADERS
  1101. #define MG_MAX_HTTP_HEADERS 40
  1102. #endif
  1103. #ifndef CS_ENABLE_STDIO
  1104. #define CS_ENABLE_STDIO 1
  1105. #endif
  1106. #define abort() DebugBreak();
  1107. #ifndef BUFSIZ
  1108. #define BUFSIZ 4096
  1109. #endif
  1110. /*
  1111. * Explicitly disabling MG_ENABLE_THREADS for WinCE
  1112. * because they are enabled for _WIN32 by default
  1113. */
  1114. #ifndef MG_ENABLE_THREADS
  1115. #define MG_ENABLE_THREADS 0
  1116. #endif
  1117. #ifndef MG_ENABLE_FILESYSTEM
  1118. #define MG_ENABLE_FILESYSTEM 1
  1119. #endif
  1120. #ifndef MG_NET_IF
  1121. #define MG_NET_IF MG_NET_IF_SOCKET
  1122. #endif
  1123. typedef struct _stati64 {
  1124. uint32_t st_mtime;
  1125. uint32_t st_size;
  1126. uint32_t st_mode;
  1127. } cs_stat_t;
  1128. /*
  1129. * WinCE 6.0 has a lot of useful definitions in ATL (not windows.h) headers
  1130. * use #ifdefs to avoid conflicts
  1131. */
  1132. #ifndef ENOENT
  1133. #define ENOENT ERROR_PATH_NOT_FOUND
  1134. #endif
  1135. #ifndef EACCES
  1136. #define EACCES ERROR_ACCESS_DENIED
  1137. #endif
  1138. #ifndef ENOMEM
  1139. #define ENOMEM ERROR_NOT_ENOUGH_MEMORY
  1140. #endif
  1141. #ifndef _UINTPTR_T_DEFINED
  1142. typedef unsigned int* uintptr_t;
  1143. #endif
  1144. #define _S_IFREG 2
  1145. #define _S_IFDIR 4
  1146. #ifndef S_ISDIR
  1147. #define S_ISDIR(x) (((x) & _S_IFDIR) != 0)
  1148. #endif
  1149. #ifndef S_ISREG
  1150. #define S_ISREG(x) (((x) & _S_IFREG) != 0)
  1151. #endif
  1152. int open(const char *filename, int oflag, int pmode);
  1153. int _wstati64(const wchar_t *path, cs_stat_t *st);
  1154. const char *strerror();
  1155. #endif /* CS_PLATFORM == CS_P_WINCE */
  1156. #endif /* CS_COMMON_PLATFORMS_PLATFORM_WINCE_H_ */
  1157. #ifdef MG_MODULE_LINES
  1158. #line 1 "common/platforms/platform_nxp_lpc.h"
  1159. #endif
  1160. /*
  1161. * Copyright (c) 2014-2016 Cesanta Software Limited
  1162. * All rights reserved
  1163. */
  1164. #ifndef CS_COMMON_PLATFORMS_PLATFORM_NXP_LPC_H_
  1165. #define CS_COMMON_PLATFORMS_PLATFORM_NXP_LPC_H_
  1166. #if CS_PLATFORM == CS_P_NXP_LPC
  1167. #include <ctype.h>
  1168. #include <stdint.h>
  1169. #include <string.h>
  1170. #define SIZE_T_FMT "u"
  1171. typedef struct stat cs_stat_t;
  1172. #define INT64_FMT "lld"
  1173. #define INT64_X_FMT "llx"
  1174. #define __cdecl
  1175. #define MG_LWIP 1
  1176. #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
  1177. /*
  1178. * LPCXpress comes with 3 C library implementations: Newlib, NewlibNano and Redlib.
  1179. * See https://community.nxp.com/message/630860 for more details.
  1180. *
  1181. * Redlib is the default and lacks certain things, so we provide them.
  1182. */
  1183. #ifdef __REDLIB_INTERFACE_VERSION__
  1184. /* Let LWIP define timeval for us. */
  1185. #define LWIP_TIMEVAL_PRIVATE 1
  1186. #define va_copy(d, s) __builtin_va_copy(d, s)
  1187. #define CS_ENABLE_TO64 1
  1188. #define to64(x) cs_to64(x)
  1189. #define CS_ENABLE_STRDUP 1
  1190. #else
  1191. #include <sys/time.h>
  1192. #define LWIP_TIMEVAL_PRIVATE 0
  1193. #define to64(x) strtoll(x, NULL, 10)
  1194. #endif
  1195. #endif /* CS_PLATFORM == CS_P_NXP_LPC */
  1196. #endif /* CS_COMMON_PLATFORMS_PLATFORM_NXP_LPC_H_ */
  1197. #ifdef MG_MODULE_LINES
  1198. #line 1 "common/platforms/platform_nxp_kinetis.h"
  1199. #endif
  1200. /*
  1201. * Copyright (c) 2014-2016 Cesanta Software Limited
  1202. * All rights reserved
  1203. */
  1204. #ifndef CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_
  1205. #define CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_
  1206. #if CS_PLATFORM == CS_P_NXP_KINETIS
  1207. #include <ctype.h>
  1208. #include <inttypes.h>
  1209. #include <string.h>
  1210. #include <sys/time.h>
  1211. #define SIZE_T_FMT "u"
  1212. typedef struct stat cs_stat_t;
  1213. #define to64(x) strtoll(x, NULL, 10)
  1214. #define INT64_FMT "lld"
  1215. #define INT64_X_FMT "llx"
  1216. #define __cdecl
  1217. #define MG_LWIP 1
  1218. #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
  1219. /* struct timeval is defined in sys/time.h. */
  1220. #define LWIP_TIMEVAL_PRIVATE 0
  1221. #endif /* CS_PLATFORM == CS_P_NXP_KINETIS */
  1222. #endif /* CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_ */
  1223. #ifdef MG_MODULE_LINES
  1224. #line 1 "common/platforms/platform_pic32.h"
  1225. #endif
  1226. /*
  1227. * Copyright (c) 2014-2016 Cesanta Software Limited
  1228. * All rights reserved
  1229. */
  1230. #ifndef CS_COMMON_PLATFORMS_PLATFORM_PIC32_H_
  1231. #define CS_COMMON_PLATFORMS_PLATFORM_PIC32_H_
  1232. #if CS_PLATFORM == CS_P_PIC32
  1233. #define MG_NET_IF MG_NET_IF_PIC32
  1234. #include <stdint.h>
  1235. #include <time.h>
  1236. #include <ctype.h>
  1237. #include <stdlib.h>
  1238. #include <system_config.h>
  1239. #include <system_definitions.h>
  1240. #include <sys/types.h>
  1241. typedef TCP_SOCKET sock_t;
  1242. #define to64(x) strtoll(x, NULL, 10)
  1243. #define SIZE_T_FMT "lu"
  1244. #define INT64_FMT "lld"
  1245. #ifndef CS_ENABLE_STDIO
  1246. #define CS_ENABLE_STDIO 1
  1247. #endif
  1248. char* inet_ntoa(struct in_addr in);
  1249. #endif /* CS_PLATFORM == CS_P_PIC32 */
  1250. #endif /* CS_COMMON_PLATFORMS_PLATFORM_PIC32_H_ */
  1251. #ifdef MG_MODULE_LINES
  1252. #line 1 "common/platforms/platform_stm32.h"
  1253. #endif
  1254. /*
  1255. * Copyright (c) 2014-2016 Cesanta Software Limited
  1256. * All rights reserved
  1257. */
  1258. #ifndef CS_COMMON_PLATFORMS_PLATFORM_STM32_H_
  1259. #define CS_COMMON_PLATFORMS_PLATFORM_STM32_H_
  1260. #if CS_PLATFORM == CS_P_STM32
  1261. //#include <sys/types.h>
  1262. //#include <sys/stat.h>
  1263. #include <stdint.h>
  1264. #include <inttypes.h>
  1265. #include <stdio.h>
  1266. #include <ctype.h>
  1267. #include <errno.h>
  1268. //#include <memory.h>
  1269. //#include <fcntl.h>
  1270. //#include <stm32_sdk_hal.h>
  1271. #define to64(x) strtoll(x, NULL, 10)
  1272. #define INT64_FMT PRId64
  1273. #define SIZE_T_FMT "u"
  1274. typedef struct stat cs_stat_t;
  1275. #define DIRSEP '/'
  1276. #ifndef CS_ENABLE_STDIO
  1277. #define CS_ENABLE_STDIO 1
  1278. #endif
  1279. #ifndef MG_ENABLE_FILESYSTEM
  1280. #define MG_ENABLE_FILESYSTEM 1
  1281. #endif
  1282. #ifndef CS_DEFINE_DIRENT
  1283. #define CS_DEFINE_DIRENT 1
  1284. #endif
  1285. #endif /* CS_PLATFORM == CS_P_STM32 */
  1286. #endif /* CS_COMMON_PLATFORMS_PLATFORM_STM32_H_ */
  1287. #ifdef MG_MODULE_LINES
  1288. #line 1 "common/platforms/lwip/mg_lwip.h"
  1289. #endif
  1290. /*
  1291. * Copyright (c) 2014-2016 Cesanta Software Limited
  1292. * All rights reserved
  1293. */
  1294. #ifndef CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_
  1295. #define CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_
  1296. #ifndef MG_LWIP
  1297. #define MG_LWIP 0
  1298. #endif
  1299. #if MG_LWIP
  1300. /*
  1301. * When compiling for nRF5x chips with arm-none-eabi-gcc, it has BYTE_ORDER
  1302. * already defined, so in order to avoid warnings in lwip, we have to undefine
  1303. * it.
  1304. *
  1305. * TODO: Check if in the future versions of nRF5 SDK that changes.
  1306. * Current version of nRF51 SDK: 0.8.0
  1307. * nRF5 SDK: 0.9.0
  1308. */
  1309. #if CS_PLATFORM == CS_P_NRF51 || CS_PLATFORM == CS_P_NRF52
  1310. #undef BYTE_ORDER
  1311. #endif
  1312. #include <lwip/opt.h>
  1313. #include <lwip/err.h>
  1314. #include <lwip/ip_addr.h>
  1315. #include <lwip/inet.h>
  1316. #include <lwip/netdb.h>
  1317. #include <lwip/dns.h>
  1318. #ifndef LWIP_PROVIDE_ERRNO
  1319. #include <errno.h>
  1320. #endif
  1321. #if LWIP_SOCKET
  1322. #include <lwip/sockets.h>
  1323. #else
  1324. /* We really need the definitions from sockets.h. */
  1325. #undef LWIP_SOCKET
  1326. #define LWIP_SOCKET 1
  1327. #include <lwip/sockets.h>
  1328. #undef LWIP_SOCKET
  1329. #define LWIP_SOCKET 0
  1330. #endif
  1331. #define INVALID_SOCKET (-1)
  1332. #define SOMAXCONN 10
  1333. typedef int sock_t;
  1334. #if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
  1335. struct mg_mgr;
  1336. struct mg_connection;
  1337. uint32_t mg_lwip_get_poll_delay_ms(struct mg_mgr *mgr);
  1338. void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle,
  1339. int interval, int count);
  1340. #endif
  1341. /* For older version of LWIP */
  1342. #ifndef ipX_2_ip
  1343. #define ipX_2_ip(x) (x)
  1344. #endif
  1345. #endif /* MG_LWIP */
  1346. #endif /* CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_ */
  1347. #ifdef MG_MODULE_LINES
  1348. #line 1 "common/cs_time.h"
  1349. #endif
  1350. /*
  1351. * Copyright (c) 2014-2016 Cesanta Software Limited
  1352. * All rights reserved
  1353. */
  1354. #ifndef CS_COMMON_CS_TIME_H_
  1355. #define CS_COMMON_CS_TIME_H_
  1356. /* Amalgamated: #include "common/platform.h" */
  1357. #ifdef __cplusplus
  1358. extern "C" {
  1359. #endif /* __cplusplus */
  1360. /* Sub-second granularity time(). */
  1361. double cs_time(void);
  1362. #ifdef __cplusplus
  1363. }
  1364. #endif /* __cplusplus */
  1365. #endif /* CS_COMMON_CS_TIME_H_ */
  1366. #ifdef MG_MODULE_LINES
  1367. #line 1 "common/mg_str.h"
  1368. #endif
  1369. /*
  1370. * Copyright (c) 2014-2016 Cesanta Software Limited
  1371. * All rights reserved
  1372. */
  1373. #ifndef CS_COMMON_MG_STR_H_
  1374. #define CS_COMMON_MG_STR_H_
  1375. #include <stddef.h>
  1376. /* Amalgamated: #include "common/platform.h" */
  1377. #ifdef __cplusplus
  1378. extern "C" {
  1379. #endif /* __cplusplus */
  1380. /* Describes chunk of memory */
  1381. struct mg_str {
  1382. const char *p; /* Memory chunk pointer */
  1383. size_t len; /* Memory chunk length */
  1384. };
  1385. /*
  1386. * Helper functions for creating mg_str struct from plain C string.
  1387. * `NULL` is allowed and becomes `{NULL, 0}`.
  1388. */
  1389. struct mg_str mg_mk_str(const char *s);
  1390. struct mg_str mg_mk_str_n(const char *s, size_t len);
  1391. /* Macro for initializing mg_str. */
  1392. #define MG_MK_STR(str_literal) \
  1393. { str_literal, sizeof(str_literal) - 1 }
  1394. /*
  1395. * Cross-platform version of `strcmp()` where where first string is
  1396. * specified by `struct mg_str`.
  1397. */
  1398. int mg_vcmp(const struct mg_str *str2, const char *str1);
  1399. /*
  1400. * Cross-platform version of `strncasecmp()` where first string is
  1401. * specified by `struct mg_str`.
  1402. */
  1403. int mg_vcasecmp(const struct mg_str *str2, const char *str1);
  1404. struct mg_str mg_strdup(const struct mg_str s);
  1405. int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
  1406. int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n);
  1407. #ifdef __cplusplus
  1408. }
  1409. #endif /* __cplusplus */
  1410. #endif /* CS_COMMON_MG_STR_H_ */
  1411. #ifdef MG_MODULE_LINES
  1412. #line 1 "common/mbuf.h"
  1413. #endif
  1414. /*
  1415. * Copyright (c) 2015 Cesanta Software Limited
  1416. * All rights reserved
  1417. */
  1418. /*
  1419. * === Memory Buffers
  1420. *
  1421. * Mbufs are mutable/growing memory buffers, like C++ strings.
  1422. * Mbuf can append data to the end of a buffer or insert data into arbitrary
  1423. * position in the middle of a buffer. The buffer grows automatically when
  1424. * needed.
  1425. */
  1426. #ifndef CS_COMMON_MBUF_H_
  1427. #define CS_COMMON_MBUF_H_
  1428. #include <stdlib.h>
  1429. /* Amalgamated: #include "common/platform.h" */
  1430. #if defined(__cplusplus)
  1431. extern "C" {
  1432. #endif
  1433. #ifndef MBUF_SIZE_MULTIPLIER
  1434. #define MBUF_SIZE_MULTIPLIER 1.5
  1435. #endif
  1436. /* Memory buffer descriptor */
  1437. struct mbuf {
  1438. char *buf; /* Buffer pointer */
  1439. size_t len; /* Data length. Data is located between offset 0 and len. */
  1440. size_t size; /* Buffer size allocated by realloc(1). Must be >= len */
  1441. };
  1442. /*
  1443. * Initialises an Mbuf.
  1444. * `initial_capacity` specifies the initial capacity of the mbuf.
  1445. */
  1446. void mbuf_init(struct mbuf *, size_t initial_capacity);
  1447. /* Frees the space allocated for the mbuffer and resets the mbuf structure. */
  1448. void mbuf_free(struct mbuf *);
  1449. /*
  1450. * Appends data to the Mbuf.
  1451. *
  1452. * Returns the number of bytes appended or 0 if out of memory.
  1453. */
  1454. size_t mbuf_append(struct mbuf *, const void *data, size_t data_size);
  1455. /*
  1456. * Inserts data at a specified offset in the Mbuf.
  1457. *
  1458. * Existing data will be shifted forwards and the buffer will
  1459. * be grown if necessary.
  1460. * Returns the number of bytes inserted.
  1461. */
  1462. size_t mbuf_insert(struct mbuf *, size_t, const void *, size_t);
  1463. /* Removes `data_size` bytes from the beginning of the buffer. */
  1464. void mbuf_remove(struct mbuf *, size_t data_size);
  1465. /*
  1466. * Resizes an Mbuf.
  1467. *
  1468. * If `new_size` is smaller than buffer's `len`, the
  1469. * resize is not performed.
  1470. */
  1471. void mbuf_resize(struct mbuf *, size_t new_size);
  1472. /* Shrinks an Mbuf by resizing its `size` to `len`. */
  1473. void mbuf_trim(struct mbuf *);
  1474. #if defined(__cplusplus)
  1475. }
  1476. #endif /* __cplusplus */
  1477. #endif /* CS_COMMON_MBUF_H_ */
  1478. #ifdef MG_MODULE_LINES
  1479. #line 1 "common/sha1.h"
  1480. #endif
  1481. /*
  1482. * Copyright (c) 2014 Cesanta Software Limited
  1483. * All rights reserved
  1484. */
  1485. #ifndef CS_COMMON_SHA1_H_
  1486. #define CS_COMMON_SHA1_H_
  1487. #ifndef DISABLE_SHA1
  1488. #define DISABLE_SHA1 0
  1489. #endif
  1490. #if !DISABLE_SHA1
  1491. /* Amalgamated: #include "common/platform.h" */
  1492. #ifdef __cplusplus
  1493. extern "C" {
  1494. #endif /* __cplusplus */
  1495. typedef struct {
  1496. uint32_t state[5];
  1497. uint32_t count[2];
  1498. unsigned char buffer[64];
  1499. } cs_sha1_ctx;
  1500. void cs_sha1_init(cs_sha1_ctx *);
  1501. void cs_sha1_update(cs_sha1_ctx *, const unsigned char *data, uint32_t len);
  1502. void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *);
  1503. void cs_hmac_sha1(const unsigned char *key, size_t key_len,
  1504. const unsigned char *text, size_t text_len,
  1505. unsigned char out[20]);
  1506. #ifdef __cplusplus
  1507. }
  1508. #endif /* __cplusplus */
  1509. #endif /* DISABLE_SHA1 */
  1510. #endif /* CS_COMMON_SHA1_H_ */
  1511. #ifdef MG_MODULE_LINES
  1512. #line 1 "common/md5.h"
  1513. #endif
  1514. /*
  1515. * Copyright (c) 2014 Cesanta Software Limited
  1516. * All rights reserved
  1517. */
  1518. #ifndef CS_COMMON_MD5_H_
  1519. #define CS_COMMON_MD5_H_
  1520. /* Amalgamated: #include "common/platform.h" */
  1521. #ifndef DISABLE_MD5
  1522. #define DISABLE_MD5 0
  1523. #endif
  1524. #ifdef __cplusplus
  1525. extern "C" {
  1526. #endif /* __cplusplus */
  1527. typedef struct MD5Context {
  1528. uint32_t buf[4];
  1529. uint32_t bits[2];
  1530. unsigned char in[64];
  1531. } MD5_CTX;
  1532. void MD5_Init(MD5_CTX *c);
  1533. void MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len);
  1534. void MD5_Final(unsigned char *md, MD5_CTX *c);
  1535. /*
  1536. * Return stringified MD5 hash for NULL terminated list of pointer/length pairs.
  1537. * A length should be specified as size_t variable.
  1538. * Example:
  1539. *
  1540. * char buf[33];
  1541. * cs_md5(buf, "foo", (size_t) 3, "bar", (size_t) 3, NULL);
  1542. */
  1543. char *cs_md5(char buf[33], ...);
  1544. #ifdef __cplusplus
  1545. }
  1546. #endif /* __cplusplus */
  1547. #endif /* CS_COMMON_MD5_H_ */
  1548. #ifdef MG_MODULE_LINES
  1549. #line 1 "common/base64.h"
  1550. #endif
  1551. /*
  1552. * Copyright (c) 2014 Cesanta Software Limited
  1553. * All rights reserved
  1554. */
  1555. #ifndef CS_COMMON_BASE64_H_
  1556. #define CS_COMMON_BASE64_H_
  1557. #ifndef DISABLE_BASE64
  1558. #define DISABLE_BASE64 0
  1559. #endif
  1560. #if !DISABLE_BASE64
  1561. #include <stdio.h>
  1562. #ifdef __cplusplus
  1563. extern "C" {
  1564. #endif
  1565. typedef void (*cs_base64_putc_t)(char, void *);
  1566. struct cs_base64_ctx {
  1567. /* cannot call it putc because it's a macro on some environments */
  1568. cs_base64_putc_t b64_putc;
  1569. unsigned char chunk[3];
  1570. int chunk_size;
  1571. void *user_data;
  1572. };
  1573. void cs_base64_init(struct cs_base64_ctx *ctx, cs_base64_putc_t putc,
  1574. void *user_data);
  1575. void cs_base64_update(struct cs_base64_ctx *ctx, const char *str, size_t len);
  1576. void cs_base64_finish(struct cs_base64_ctx *ctx);
  1577. void cs_base64_encode(const unsigned char *src, int src_len, char *dst);
  1578. void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len);
  1579. int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len);
  1580. #ifdef __cplusplus
  1581. }
  1582. #endif
  1583. #endif /* DISABLE_BASE64 */
  1584. #endif /* CS_COMMON_BASE64_H_ */
  1585. #ifdef MG_MODULE_LINES
  1586. #line 1 "common/str_util.h"
  1587. #endif
  1588. /*
  1589. * Copyright (c) 2015 Cesanta Software Limited
  1590. * All rights reserved
  1591. */
  1592. #ifndef CS_COMMON_STR_UTIL_H_
  1593. #define CS_COMMON_STR_UTIL_H_
  1594. #include <stdarg.h>
  1595. #include <stdlib.h>
  1596. /* Amalgamated: #include "common/platform.h" */
  1597. #ifndef CS_ENABLE_STRDUP
  1598. #define CS_ENABLE_STRDUP 0
  1599. #endif
  1600. #ifndef CS_ENABLE_TO64
  1601. #define CS_ENABLE_TO64 0
  1602. #endif
  1603. /*
  1604. * Expands to a string representation of its argument: e.g.
  1605. * `CS_STRINGIFY_LIT(5) expands to "5"`
  1606. */
  1607. #define CS_STRINGIFY_LIT(x) #x
  1608. /*
  1609. * Expands to a string representation of its argument, which is allowed
  1610. * to be a macro: e.g.
  1611. *
  1612. * #define FOO 123
  1613. * CS_STRINGIFY_MACRO(FOO)
  1614. *
  1615. * expands to 123.
  1616. */
  1617. #define CS_STRINGIFY_MACRO(x) CS_STRINGIFY_LIT(x)
  1618. #ifdef __cplusplus
  1619. extern "C" {
  1620. #endif
  1621. size_t c_strnlen(const char *s, size_t maxlen);
  1622. int c_snprintf(char *buf, size_t buf_size, const char *format, ...);
  1623. int c_vsnprintf(char *buf, size_t buf_size, const char *format, va_list ap);
  1624. /*
  1625. * Find the first occurrence of find in s, where the search is limited to the
  1626. * first slen characters of s.
  1627. */
  1628. const char *c_strnstr(const char *s, const char *find, size_t slen);
  1629. /*
  1630. * Stringify binary data. Output buffer size must be 2 * size_of_input + 1
  1631. * because each byte of input takes 2 bytes in string representation
  1632. * plus 1 byte for the terminating \0 character.
  1633. */
  1634. void cs_to_hex(char *to, const unsigned char *p, size_t len);
  1635. /*
  1636. * Convert stringified binary data back to binary.
  1637. * Does the reverse of `cs_to_hex()`.
  1638. */
  1639. void cs_from_hex(char *to, const char *p, size_t len);
  1640. #if CS_ENABLE_STRDUP
  1641. char *strdup(const char *src);
  1642. #endif
  1643. #if CS_ENABLE_TO64
  1644. #include <stdint.h>
  1645. /*
  1646. * Simple string -> int64 conversion routine.
  1647. */
  1648. int64_t cs_to64(const char *s);
  1649. #endif
  1650. /*
  1651. * Cross-platform version of `strncasecmp()`.
  1652. */
  1653. int mg_ncasecmp(const char *s1, const char *s2, size_t len);
  1654. /*
  1655. * Cross-platform version of `strcasecmp()`.
  1656. */
  1657. int mg_casecmp(const char *s1, const char *s2);
  1658. /*
  1659. * Prints message to the buffer. If the buffer is large enough to hold the
  1660. * message, it returns buffer. If buffer is to small, it allocates a large
  1661. * enough buffer on heap and returns allocated buffer.
  1662. * This is a supposed use case:
  1663. *
  1664. * char buf[5], *p = buf;
  1665. * mg_avprintf(&p, sizeof(buf), "%s", "hi there");
  1666. * use_p_somehow(p);
  1667. * if (p != buf) {
  1668. * free(p);
  1669. * }
  1670. *
  1671. * The purpose of this is to avoid malloc-ing if generated strings are small.
  1672. */
  1673. int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
  1674. /* Same as mg_asprintf, but takes varargs list. */
  1675. int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap);
  1676. #ifdef __cplusplus
  1677. }
  1678. #endif
  1679. #endif /* CS_COMMON_STR_UTIL_H_ */
  1680. #ifdef MG_MODULE_LINES
  1681. #line 1 "common/queue.h"
  1682. #endif
  1683. /* clang-format off */
  1684. /*-
  1685. * Copyright (c) 1991, 1993
  1686. * The Regents of the University of California. All rights reserved.
  1687. *
  1688. * Redistribution and use in source and binary forms, with or without
  1689. * modification, are permitted provided that the following conditions
  1690. * are met:
  1691. * 1. Redistributions of source code must retain the above copyright
  1692. * notice, this list of conditions and the following disclaimer.
  1693. * 2. Redistributions in binary form must reproduce the above copyright
  1694. * notice, this list of conditions and the following disclaimer in the
  1695. * documentation and/or other materials provided with the distribution.
  1696. * 4. Neither the name of the University nor the names of its contributors
  1697. * may be used to endorse or promote products derived from this software
  1698. * without specific prior written permission.
  1699. *
  1700. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1701. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1702. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1703. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1704. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1705. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1706. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1707. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1708. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1709. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1710. * SUCH DAMAGE.
  1711. *
  1712. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  1713. * $FreeBSD$
  1714. */
  1715. #ifndef _SYS_QUEUE_H_
  1716. #define _SYS_QUEUE_H_
  1717. /*
  1718. * This file defines four types of data structures: singly-linked lists,
  1719. * singly-linked tail queues, lists and tail queues.
  1720. *
  1721. * A singly-linked list is headed by a single forward pointer. The elements
  1722. * are singly linked for minimum space and pointer manipulation overhead at
  1723. * the expense of O(n) removal for arbitrary elements. New elements can be
  1724. * added to the list after an existing element or at the head of the list.
  1725. * Elements being removed from the head of the list should use the explicit
  1726. * macro for this purpose for optimum efficiency. A singly-linked list may
  1727. * only be traversed in the forward direction. Singly-linked lists are ideal
  1728. * for applications with large datasets and few or no removals or for
  1729. * implementing a LIFO queue.
  1730. *
  1731. * A singly-linked tail queue is headed by a pair of pointers, one to the
  1732. * head of the list and the other to the tail of the list. The elements are
  1733. * singly linked for minimum space and pointer manipulation overhead at the
  1734. * expense of O(n) removal for arbitrary elements. New elements can be added
  1735. * to the list after an existing element, at the head of the list, or at the
  1736. * end of the list. Elements being removed from the head of the tail queue
  1737. * should use the explicit macro for this purpose for optimum efficiency.
  1738. * A singly-linked tail queue may only be traversed in the forward direction.
  1739. * Singly-linked tail queues are ideal for applications with large datasets
  1740. * and few or no removals or for implementing a FIFO queue.
  1741. *
  1742. * A list is headed by a single forward pointer (or an array of forward
  1743. * pointers for a hash table header). The elements are doubly linked
  1744. * so that an arbitrary element can be removed without a need to
  1745. * traverse the list. New elements can be added to the list before
  1746. * or after an existing element or at the head of the list. A list
  1747. * may be traversed in either direction.
  1748. *
  1749. * A tail queue is headed by a pair of pointers, one to the head of the
  1750. * list and the other to the tail of the list. The elements are doubly
  1751. * linked so that an arbitrary element can be removed without a need to
  1752. * traverse the list. New elements can be added to the list before or
  1753. * after an existing element, at the head of the list, or at the end of
  1754. * the list. A tail queue may be traversed in either direction.
  1755. *
  1756. * For details on the use of these macros, see the queue(3) manual page.
  1757. *
  1758. *
  1759. * SLIST LIST STAILQ TAILQ
  1760. * _HEAD + + + +
  1761. * _CLASS_HEAD + + + +
  1762. * _HEAD_INITIALIZER + + + +
  1763. * _ENTRY + + + +
  1764. * _CLASS_ENTRY + + + +
  1765. * _INIT + + + +
  1766. * _EMPTY + + + +
  1767. * _FIRST + + + +
  1768. * _NEXT + + + +
  1769. * _PREV - + - +
  1770. * _LAST - - + +
  1771. * _FOREACH + + + +
  1772. * _FOREACH_FROM + + + +
  1773. * _FOREACH_SAFE + + + +
  1774. * _FOREACH_FROM_SAFE + + + +
  1775. * _FOREACH_REVERSE - - - +
  1776. * _FOREACH_REVERSE_FROM - - - +
  1777. * _FOREACH_REVERSE_SAFE - - - +
  1778. * _FOREACH_REVERSE_FROM_SAFE - - - +
  1779. * _INSERT_HEAD + + + +
  1780. * _INSERT_BEFORE - + - +
  1781. * _INSERT_AFTER + + + +
  1782. * _INSERT_TAIL - - + +
  1783. * _CONCAT - - + +
  1784. * _REMOVE_AFTER + - + -
  1785. * _REMOVE_HEAD + - + -
  1786. * _REMOVE + + + +
  1787. * _SWAP + + + +
  1788. *
  1789. */
  1790. #ifdef QUEUE_MACRO_DEBUG
  1791. /* Store the last 2 places the queue element or head was altered */
  1792. struct qm_trace {
  1793. unsigned long lastline;
  1794. unsigned long prevline;
  1795. const char *lastfile;
  1796. const char *prevfile;
  1797. };
  1798. #define TRACEBUF struct qm_trace trace;
  1799. #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
  1800. #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
  1801. #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
  1802. #define QMD_TRACE_HEAD(head) do { \
  1803. (head)->trace.prevline = (head)->trace.lastline; \
  1804. (head)->trace.prevfile = (head)->trace.lastfile; \
  1805. (head)->trace.lastline = __LINE__; \
  1806. (head)->trace.lastfile = __FILE__; \
  1807. } while (0)
  1808. #define QMD_TRACE_ELEM(elem) do { \
  1809. (elem)->trace.prevline = (elem)->trace.lastline; \
  1810. (elem)->trace.prevfile = (elem)->trace.lastfile; \
  1811. (elem)->trace.lastline = __LINE__; \
  1812. (elem)->trace.lastfile = __FILE__; \
  1813. } while (0)
  1814. #else
  1815. #define QMD_TRACE_ELEM(elem)
  1816. #define QMD_TRACE_HEAD(head)
  1817. #define QMD_SAVELINK(name, link)
  1818. #define TRACEBUF
  1819. #define TRACEBUF_INITIALIZER
  1820. #define TRASHIT(x)
  1821. #endif /* QUEUE_MACRO_DEBUG */
  1822. #ifdef __cplusplus
  1823. /*
  1824. * In C++ there can be structure lists and class lists:
  1825. */
  1826. #define QUEUE_TYPEOF(type) type
  1827. #else
  1828. #define QUEUE_TYPEOF(type) struct type
  1829. #endif
  1830. /*
  1831. * Singly-linked List declarations.
  1832. */
  1833. #define SLIST_HEAD(name, type) \
  1834. struct name { \
  1835. struct type *slh_first; /* first element */ \
  1836. }
  1837. #define SLIST_CLASS_HEAD(name, type) \
  1838. struct name { \
  1839. class type *slh_first; /* first element */ \
  1840. }
  1841. #define SLIST_HEAD_INITIALIZER(head) \
  1842. { NULL }
  1843. #define SLIST_ENTRY(type) \
  1844. struct { \
  1845. struct type *sle_next; /* next element */ \
  1846. }
  1847. #define SLIST_CLASS_ENTRY(type) \
  1848. struct { \
  1849. class type *sle_next; /* next element */ \
  1850. }
  1851. /*
  1852. * Singly-linked List functions.
  1853. */
  1854. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  1855. #define SLIST_FIRST(head) ((head)->slh_first)
  1856. #define SLIST_FOREACH(var, head, field) \
  1857. for ((var) = SLIST_FIRST((head)); \
  1858. (var); \
  1859. (var) = SLIST_NEXT((var), field))
  1860. #define SLIST_FOREACH_FROM(var, head, field) \
  1861. for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
  1862. (var); \
  1863. (var) = SLIST_NEXT((var), field))
  1864. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  1865. for ((var) = SLIST_FIRST((head)); \
  1866. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  1867. (var) = (tvar))
  1868. #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
  1869. for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
  1870. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  1871. (var) = (tvar))
  1872. #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  1873. for ((varp) = &SLIST_FIRST((head)); \
  1874. ((var) = *(varp)) != NULL; \
  1875. (varp) = &SLIST_NEXT((var), field))
  1876. #define SLIST_INIT(head) do { \
  1877. SLIST_FIRST((head)) = NULL; \
  1878. } while (0)
  1879. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  1880. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  1881. SLIST_NEXT((slistelm), field) = (elm); \
  1882. } while (0)
  1883. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  1884. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  1885. SLIST_FIRST((head)) = (elm); \
  1886. } while (0)
  1887. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  1888. #define SLIST_REMOVE(head, elm, type, field) do { \
  1889. QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
  1890. if (SLIST_FIRST((head)) == (elm)) { \
  1891. SLIST_REMOVE_HEAD((head), field); \
  1892. } \
  1893. else { \
  1894. QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
  1895. while (SLIST_NEXT(curelm, field) != (elm)) \
  1896. curelm = SLIST_NEXT(curelm, field); \
  1897. SLIST_REMOVE_AFTER(curelm, field); \
  1898. } \
  1899. TRASHIT(*oldnext); \
  1900. } while (0)
  1901. #define SLIST_REMOVE_AFTER(elm, field) do { \
  1902. SLIST_NEXT(elm, field) = \
  1903. SLIST_NEXT(SLIST_NEXT(elm, field), field); \
  1904. } while (0)
  1905. #define SLIST_REMOVE_HEAD(head, field) do { \
  1906. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  1907. } while (0)
  1908. #define SLIST_SWAP(head1, head2, type) do { \
  1909. QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
  1910. SLIST_FIRST(head1) = SLIST_FIRST(head2); \
  1911. SLIST_FIRST(head2) = swap_first; \
  1912. } while (0)
  1913. /*
  1914. * Singly-linked Tail queue declarations.
  1915. */
  1916. #define STAILQ_HEAD(name, type) \
  1917. struct name { \
  1918. struct type *stqh_first;/* first element */ \
  1919. struct type **stqh_last;/* addr of last next element */ \
  1920. }
  1921. #define STAILQ_CLASS_HEAD(name, type) \
  1922. struct name { \
  1923. class type *stqh_first; /* first element */ \
  1924. class type **stqh_last; /* addr of last next element */ \
  1925. }
  1926. #define STAILQ_HEAD_INITIALIZER(head) \
  1927. { NULL, &(head).stqh_first }
  1928. #define STAILQ_ENTRY(type) \
  1929. struct { \
  1930. struct type *stqe_next; /* next element */ \
  1931. }
  1932. #define STAILQ_CLASS_ENTRY(type) \
  1933. struct { \
  1934. class type *stqe_next; /* next element */ \
  1935. }
  1936. /*
  1937. * Singly-linked Tail queue functions.
  1938. */
  1939. #define STAILQ_CONCAT(head1, head2) do { \
  1940. if (!STAILQ_EMPTY((head2))) { \
  1941. *(head1)->stqh_last = (head2)->stqh_first; \
  1942. (head1)->stqh_last = (head2)->stqh_last; \
  1943. STAILQ_INIT((head2)); \
  1944. } \
  1945. } while (0)
  1946. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  1947. #define STAILQ_FIRST(head) ((head)->stqh_first)
  1948. #define STAILQ_FOREACH(var, head, field) \
  1949. for((var) = STAILQ_FIRST((head)); \
  1950. (var); \
  1951. (var) = STAILQ_NEXT((var), field))
  1952. #define STAILQ_FOREACH_FROM(var, head, field) \
  1953. for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
  1954. (var); \
  1955. (var) = STAILQ_NEXT((var), field))
  1956. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  1957. for ((var) = STAILQ_FIRST((head)); \
  1958. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  1959. (var) = (tvar))
  1960. #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
  1961. for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
  1962. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  1963. (var) = (tvar))
  1964. #define STAILQ_INIT(head) do { \
  1965. STAILQ_FIRST((head)) = NULL; \
  1966. (head)->stqh_last = &STAILQ_FIRST((head)); \
  1967. } while (0)
  1968. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  1969. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  1970. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  1971. STAILQ_NEXT((tqelm), field) = (elm); \
  1972. } while (0)
  1973. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  1974. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  1975. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  1976. STAILQ_FIRST((head)) = (elm); \
  1977. } while (0)
  1978. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  1979. STAILQ_NEXT((elm), field) = NULL; \
  1980. *(head)->stqh_last = (elm); \
  1981. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  1982. } while (0)
  1983. #define STAILQ_LAST(head, type, field) \
  1984. (STAILQ_EMPTY((head)) ? NULL : \
  1985. __containerof((head)->stqh_last, \
  1986. QUEUE_TYPEOF(type), field.stqe_next))
  1987. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  1988. #define STAILQ_REMOVE(head, elm, type, field) do { \
  1989. QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
  1990. if (STAILQ_FIRST((head)) == (elm)) { \
  1991. STAILQ_REMOVE_HEAD((head), field); \
  1992. } \
  1993. else { \
  1994. QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
  1995. while (STAILQ_NEXT(curelm, field) != (elm)) \
  1996. curelm = STAILQ_NEXT(curelm, field); \
  1997. STAILQ_REMOVE_AFTER(head, curelm, field); \
  1998. } \
  1999. TRASHIT(*oldnext); \
  2000. } while (0)
  2001. #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
  2002. if ((STAILQ_NEXT(elm, field) = \
  2003. STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
  2004. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  2005. } while (0)
  2006. #define STAILQ_REMOVE_HEAD(head, field) do { \
  2007. if ((STAILQ_FIRST((head)) = \
  2008. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  2009. (head)->stqh_last = &STAILQ_FIRST((head)); \
  2010. } while (0)
  2011. #define STAILQ_SWAP(head1, head2, type) do { \
  2012. QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
  2013. QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
  2014. STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
  2015. (head1)->stqh_last = (head2)->stqh_last; \
  2016. STAILQ_FIRST(head2) = swap_first; \
  2017. (head2)->stqh_last = swap_last; \
  2018. if (STAILQ_EMPTY(head1)) \
  2019. (head1)->stqh_last = &STAILQ_FIRST(head1); \
  2020. if (STAILQ_EMPTY(head2)) \
  2021. (head2)->stqh_last = &STAILQ_FIRST(head2); \
  2022. } while (0)
  2023. /*
  2024. * List declarations.
  2025. */
  2026. #define LIST_HEAD(name, type) \
  2027. struct name { \
  2028. struct type *lh_first; /* first element */ \
  2029. }
  2030. #define LIST_CLASS_HEAD(name, type) \
  2031. struct name { \
  2032. class type *lh_first; /* first element */ \
  2033. }
  2034. #define LIST_HEAD_INITIALIZER(head) \
  2035. { NULL }
  2036. #define LIST_ENTRY(type) \
  2037. struct { \
  2038. struct type *le_next; /* next element */ \
  2039. struct type **le_prev; /* address of previous next element */ \
  2040. }
  2041. #define LIST_CLASS_ENTRY(type) \
  2042. struct { \
  2043. class type *le_next; /* next element */ \
  2044. class type **le_prev; /* address of previous next element */ \
  2045. }
  2046. /*
  2047. * List functions.
  2048. */
  2049. #if (defined(_KERNEL) && defined(INVARIANTS))
  2050. #define QMD_LIST_CHECK_HEAD(head, field) do { \
  2051. if (LIST_FIRST((head)) != NULL && \
  2052. LIST_FIRST((head))->field.le_prev != \
  2053. &LIST_FIRST((head))) \
  2054. panic("Bad list head %p first->prev != head", (head)); \
  2055. } while (0)
  2056. #define QMD_LIST_CHECK_NEXT(elm, field) do { \
  2057. if (LIST_NEXT((elm), field) != NULL && \
  2058. LIST_NEXT((elm), field)->field.le_prev != \
  2059. &((elm)->field.le_next)) \
  2060. panic("Bad link elm %p next->prev != elm", (elm)); \
  2061. } while (0)
  2062. #define QMD_LIST_CHECK_PREV(elm, field) do { \
  2063. if (*(elm)->field.le_prev != (elm)) \
  2064. panic("Bad link elm %p prev->next != elm", (elm)); \
  2065. } while (0)
  2066. #else
  2067. #define QMD_LIST_CHECK_HEAD(head, field)
  2068. #define QMD_LIST_CHECK_NEXT(elm, field)
  2069. #define QMD_LIST_CHECK_PREV(elm, field)
  2070. #endif /* (_KERNEL && INVARIANTS) */
  2071. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  2072. #define LIST_FIRST(head) ((head)->lh_first)
  2073. #define LIST_FOREACH(var, head, field) \
  2074. for ((var) = LIST_FIRST((head)); \
  2075. (var); \
  2076. (var) = LIST_NEXT((var), field))
  2077. #define LIST_FOREACH_FROM(var, head, field) \
  2078. for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
  2079. (var); \
  2080. (var) = LIST_NEXT((var), field))
  2081. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  2082. for ((var) = LIST_FIRST((head)); \
  2083. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  2084. (var) = (tvar))
  2085. #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
  2086. for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
  2087. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  2088. (var) = (tvar))
  2089. #define LIST_INIT(head) do { \
  2090. LIST_FIRST((head)) = NULL; \
  2091. } while (0)
  2092. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  2093. QMD_LIST_CHECK_NEXT(listelm, field); \
  2094. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  2095. LIST_NEXT((listelm), field)->field.le_prev = \
  2096. &LIST_NEXT((elm), field); \
  2097. LIST_NEXT((listelm), field) = (elm); \
  2098. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  2099. } while (0)
  2100. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  2101. QMD_LIST_CHECK_PREV(listelm, field); \
  2102. (elm)->field.le_prev = (listelm)->field.le_prev; \
  2103. LIST_NEXT((elm), field) = (listelm); \
  2104. *(listelm)->field.le_prev = (elm); \
  2105. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  2106. } while (0)
  2107. #define LIST_INSERT_HEAD(head, elm, field) do { \
  2108. QMD_LIST_CHECK_HEAD((head), field); \
  2109. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  2110. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  2111. LIST_FIRST((head)) = (elm); \
  2112. (elm)->field.le_prev = &LIST_FIRST((head)); \
  2113. } while (0)
  2114. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  2115. #define LIST_PREV(elm, head, type, field) \
  2116. ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
  2117. __containerof((elm)->field.le_prev, \
  2118. QUEUE_TYPEOF(type), field.le_next))
  2119. #define LIST_REMOVE(elm, field) do { \
  2120. QMD_SAVELINK(oldnext, (elm)->field.le_next); \
  2121. QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
  2122. QMD_LIST_CHECK_NEXT(elm, field); \
  2123. QMD_LIST_CHECK_PREV(elm, field); \
  2124. if (LIST_NEXT((elm), field) != NULL) \
  2125. LIST_NEXT((elm), field)->field.le_prev = \
  2126. (elm)->field.le_prev; \
  2127. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  2128. TRASHIT(*oldnext); \
  2129. TRASHIT(*oldprev); \
  2130. } while (0)
  2131. #define LIST_SWAP(head1, head2, type, field) do { \
  2132. QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
  2133. LIST_FIRST((head1)) = LIST_FIRST((head2)); \
  2134. LIST_FIRST((head2)) = swap_tmp; \
  2135. if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
  2136. swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
  2137. if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
  2138. swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
  2139. } while (0)
  2140. /*
  2141. * Tail queue declarations.
  2142. */
  2143. #define TAILQ_HEAD(name, type) \
  2144. struct name { \
  2145. struct type *tqh_first; /* first element */ \
  2146. struct type **tqh_last; /* addr of last next element */ \
  2147. TRACEBUF \
  2148. }
  2149. #define TAILQ_CLASS_HEAD(name, type) \
  2150. struct name { \
  2151. class type *tqh_first; /* first element */ \
  2152. class type **tqh_last; /* addr of last next element */ \
  2153. TRACEBUF \
  2154. }
  2155. #define TAILQ_HEAD_INITIALIZER(head) \
  2156. { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
  2157. #define TAILQ_ENTRY(type) \
  2158. struct { \
  2159. struct type *tqe_next; /* next element */ \
  2160. struct type **tqe_prev; /* address of previous next element */ \
  2161. TRACEBUF \
  2162. }
  2163. #define TAILQ_CLASS_ENTRY(type) \
  2164. struct { \
  2165. class type *tqe_next; /* next element */ \
  2166. class type **tqe_prev; /* address of previous next element */ \
  2167. TRACEBUF \
  2168. }
  2169. /*
  2170. * Tail queue functions.
  2171. */
  2172. #if (defined(_KERNEL) && defined(INVARIANTS))
  2173. #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
  2174. if (!TAILQ_EMPTY(head) && \
  2175. TAILQ_FIRST((head))->field.tqe_prev != \
  2176. &TAILQ_FIRST((head))) \
  2177. panic("Bad tailq head %p first->prev != head", (head)); \
  2178. } while (0)
  2179. #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
  2180. if (*(head)->tqh_last != NULL) \
  2181. panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
  2182. } while (0)
  2183. #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
  2184. if (TAILQ_NEXT((elm), field) != NULL && \
  2185. TAILQ_NEXT((elm), field)->field.tqe_prev != \
  2186. &((elm)->field.tqe_next)) \
  2187. panic("Bad link elm %p next->prev != elm", (elm)); \
  2188. } while (0)
  2189. #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
  2190. if (*(elm)->field.tqe_prev != (elm)) \
  2191. panic("Bad link elm %p prev->next != elm", (elm)); \
  2192. } while (0)
  2193. #else
  2194. #define QMD_TAILQ_CHECK_HEAD(head, field)
  2195. #define QMD_TAILQ_CHECK_TAIL(head, headname)
  2196. #define QMD_TAILQ_CHECK_NEXT(elm, field)
  2197. #define QMD_TAILQ_CHECK_PREV(elm, field)
  2198. #endif /* (_KERNEL && INVARIANTS) */
  2199. #define TAILQ_CONCAT(head1, head2, field) do { \
  2200. if (!TAILQ_EMPTY(head2)) { \
  2201. *(head1)->tqh_last = (head2)->tqh_first; \
  2202. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  2203. (head1)->tqh_last = (head2)->tqh_last; \
  2204. TAILQ_INIT((head2)); \
  2205. QMD_TRACE_HEAD(head1); \
  2206. QMD_TRACE_HEAD(head2); \
  2207. } \
  2208. } while (0)
  2209. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  2210. #define TAILQ_FIRST(head) ((head)->tqh_first)
  2211. #define TAILQ_FOREACH(var, head, field) \
  2212. for ((var) = TAILQ_FIRST((head)); \
  2213. (var); \
  2214. (var) = TAILQ_NEXT((var), field))
  2215. #define TAILQ_FOREACH_FROM(var, head, field) \
  2216. for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
  2217. (var); \
  2218. (var) = TAILQ_NEXT((var), field))
  2219. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  2220. for ((var) = TAILQ_FIRST((head)); \
  2221. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  2222. (var) = (tvar))
  2223. #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
  2224. for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
  2225. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  2226. (var) = (tvar))
  2227. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  2228. for ((var) = TAILQ_LAST((head), headname); \
  2229. (var); \
  2230. (var) = TAILQ_PREV((var), headname, field))
  2231. #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
  2232. for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
  2233. (var); \
  2234. (var) = TAILQ_PREV((var), headname, field))
  2235. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  2236. for ((var) = TAILQ_LAST((head), headname); \
  2237. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  2238. (var) = (tvar))
  2239. #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
  2240. for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
  2241. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  2242. (var) = (tvar))
  2243. #define TAILQ_INIT(head) do { \
  2244. TAILQ_FIRST((head)) = NULL; \
  2245. (head)->tqh_last = &TAILQ_FIRST((head)); \
  2246. QMD_TRACE_HEAD(head); \
  2247. } while (0)
  2248. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  2249. QMD_TAILQ_CHECK_NEXT(listelm, field); \
  2250. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  2251. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  2252. &TAILQ_NEXT((elm), field); \
  2253. else { \
  2254. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  2255. QMD_TRACE_HEAD(head); \
  2256. } \
  2257. TAILQ_NEXT((listelm), field) = (elm); \
  2258. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  2259. QMD_TRACE_ELEM(&(elm)->field); \
  2260. QMD_TRACE_ELEM(&(listelm)->field); \
  2261. } while (0)
  2262. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  2263. QMD_TAILQ_CHECK_PREV(listelm, field); \
  2264. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  2265. TAILQ_NEXT((elm), field) = (listelm); \
  2266. *(listelm)->field.tqe_prev = (elm); \
  2267. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  2268. QMD_TRACE_ELEM(&(elm)->field); \
  2269. QMD_TRACE_ELEM(&(listelm)->field); \
  2270. } while (0)
  2271. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  2272. QMD_TAILQ_CHECK_HEAD(head, field); \
  2273. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  2274. TAILQ_FIRST((head))->field.tqe_prev = \
  2275. &TAILQ_NEXT((elm), field); \
  2276. else \
  2277. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  2278. TAILQ_FIRST((head)) = (elm); \
  2279. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  2280. QMD_TRACE_HEAD(head); \
  2281. QMD_TRACE_ELEM(&(elm)->field); \
  2282. } while (0)
  2283. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  2284. QMD_TAILQ_CHECK_TAIL(head, field); \
  2285. TAILQ_NEXT((elm), field) = NULL; \
  2286. (elm)->field.tqe_prev = (head)->tqh_last; \
  2287. *(head)->tqh_last = (elm); \
  2288. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  2289. QMD_TRACE_HEAD(head); \
  2290. QMD_TRACE_ELEM(&(elm)->field); \
  2291. } while (0)
  2292. #define TAILQ_LAST(head, headname) \
  2293. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  2294. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  2295. #define TAILQ_PREV(elm, headname, field) \
  2296. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  2297. #define TAILQ_REMOVE(head, elm, field) do { \
  2298. QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
  2299. QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
  2300. QMD_TAILQ_CHECK_NEXT(elm, field); \
  2301. QMD_TAILQ_CHECK_PREV(elm, field); \
  2302. if ((TAILQ_NEXT((elm), field)) != NULL) \
  2303. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  2304. (elm)->field.tqe_prev; \
  2305. else { \
  2306. (head)->tqh_last = (elm)->field.tqe_prev; \
  2307. QMD_TRACE_HEAD(head); \
  2308. } \
  2309. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  2310. TRASHIT(*oldnext); \
  2311. TRASHIT(*oldprev); \
  2312. QMD_TRACE_ELEM(&(elm)->field); \
  2313. } while (0)
  2314. #define TAILQ_SWAP(head1, head2, type, field) do { \
  2315. QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
  2316. QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
  2317. (head1)->tqh_first = (head2)->tqh_first; \
  2318. (head1)->tqh_last = (head2)->tqh_last; \
  2319. (head2)->tqh_first = swap_first; \
  2320. (head2)->tqh_last = swap_last; \
  2321. if ((swap_first = (head1)->tqh_first) != NULL) \
  2322. swap_first->field.tqe_prev = &(head1)->tqh_first; \
  2323. else \
  2324. (head1)->tqh_last = &(head1)->tqh_first; \
  2325. if ((swap_first = (head2)->tqh_first) != NULL) \
  2326. swap_first->field.tqe_prev = &(head2)->tqh_first; \
  2327. else \
  2328. (head2)->tqh_last = &(head2)->tqh_first; \
  2329. } while (0)
  2330. #endif /* !_SYS_QUEUE_H_ */
  2331. #ifdef MG_MODULE_LINES
  2332. #line 1 "mongoose/src/features.h"
  2333. #endif
  2334. /*
  2335. * Copyright (c) 2014-2016 Cesanta Software Limited
  2336. * All rights reserved
  2337. */
  2338. #ifndef CS_MONGOOSE_SRC_FEATURES_H_
  2339. #define CS_MONGOOSE_SRC_FEATURES_H_
  2340. #ifndef MG_DISABLE_HTTP_DIGEST_AUTH
  2341. #define MG_DISABLE_HTTP_DIGEST_AUTH 0
  2342. #endif
  2343. #ifndef MG_DISABLE_HTTP_KEEP_ALIVE
  2344. #define MG_DISABLE_HTTP_KEEP_ALIVE 0
  2345. #endif
  2346. #ifndef MG_DISABLE_PFS
  2347. #define MG_DISABLE_PFS 0
  2348. #endif
  2349. #ifndef MG_DISABLE_WS_RANDOM_MASK
  2350. #define MG_DISABLE_WS_RANDOM_MASK 0
  2351. #endif
  2352. #ifndef MG_ENABLE_ASYNC_RESOLVER
  2353. #define MG_ENABLE_ASYNC_RESOLVER 1
  2354. #endif
  2355. #ifndef MG_ENABLE_BROADCAST
  2356. #define MG_ENABLE_BROADCAST 0
  2357. #endif
  2358. #ifndef MG_ENABLE_COAP
  2359. #define MG_ENABLE_COAP 0
  2360. #endif
  2361. #ifndef MG_ENABLE_DEBUG
  2362. #define MG_ENABLE_DEBUG 0
  2363. #endif
  2364. #ifndef MG_ENABLE_DIRECTORY_LISTING
  2365. #define MG_ENABLE_DIRECTORY_LISTING 0
  2366. #endif
  2367. #ifndef MG_ENABLE_DNS
  2368. #define MG_ENABLE_DNS 1
  2369. #endif
  2370. #ifndef MG_ENABLE_DNS_SERVER
  2371. #define MG_ENABLE_DNS_SERVER 0
  2372. #endif
  2373. #ifndef MG_ENABLE_FAKE_DAVLOCK
  2374. #define MG_ENABLE_FAKE_DAVLOCK 0
  2375. #endif
  2376. #ifndef MG_ENABLE_FILESYSTEM
  2377. #define MG_ENABLE_FILESYSTEM 0
  2378. #endif
  2379. #ifndef MG_ENABLE_GETADDRINFO
  2380. #define MG_ENABLE_GETADDRINFO 0
  2381. #endif
  2382. #ifndef MG_ENABLE_HEXDUMP
  2383. #define MG_ENABLE_HEXDUMP CS_ENABLE_STDIO
  2384. #endif
  2385. #ifndef MG_ENABLE_HTTP
  2386. #define MG_ENABLE_HTTP 1
  2387. #endif
  2388. #ifndef MG_ENABLE_HTTP_CGI
  2389. #define MG_ENABLE_HTTP_CGI 0
  2390. #endif
  2391. #ifndef MG_ENABLE_HTTP_SSI
  2392. #define MG_ENABLE_HTTP_SSI MG_ENABLE_FILESYSTEM
  2393. #endif
  2394. #ifndef MG_ENABLE_HTTP_SSI_EXEC
  2395. #define MG_ENABLE_HTTP_SSI_EXEC 0
  2396. #endif
  2397. #ifndef MG_ENABLE_HTTP_STREAMING_MULTIPART
  2398. #define MG_ENABLE_HTTP_STREAMING_MULTIPART 0
  2399. #endif
  2400. #ifndef MG_ENABLE_HTTP_WEBDAV
  2401. #define MG_ENABLE_HTTP_WEBDAV 0
  2402. #endif
  2403. #ifndef MG_ENABLE_HTTP_WEBSOCKET
  2404. #define MG_ENABLE_HTTP_WEBSOCKET MG_ENABLE_HTTP
  2405. #endif
  2406. #ifndef MG_ENABLE_IPV6
  2407. #define MG_ENABLE_IPV6 0
  2408. #endif
  2409. #ifndef MG_ENABLE_JAVASCRIPT
  2410. #define MG_ENABLE_JAVASCRIPT 0
  2411. #endif
  2412. #ifndef MG_ENABLE_MQTT
  2413. #define MG_ENABLE_MQTT 1
  2414. #endif
  2415. #ifndef MG_ENABLE_MQTT_BROKER
  2416. #define MG_ENABLE_MQTT_BROKER 0
  2417. #endif
  2418. #ifndef MG_ENABLE_SSL
  2419. #define MG_ENABLE_SSL 0
  2420. #endif
  2421. #ifndef MG_ENABLE_SYNC_RESOLVER
  2422. #define MG_ENABLE_SYNC_RESOLVER 0
  2423. #endif
  2424. #ifndef MG_ENABLE_STDIO
  2425. #define MG_ENABLE_STDIO CS_ENABLE_STDIO
  2426. #endif
  2427. #ifndef MG_NET_IF
  2428. #define MG_NET_IF MG_NET_IF_SOCKET
  2429. #endif
  2430. #ifndef MG_SSL_IF
  2431. #define MG_SSL_IF MG_SSL_IF_OPENSSL
  2432. #endif
  2433. #ifndef MG_ENABLE_THREADS /* ifdef-ok */
  2434. #ifdef _WIN32
  2435. #define MG_ENABLE_THREADS 1
  2436. #else
  2437. #define MG_ENABLE_THREADS 0
  2438. #endif
  2439. #endif
  2440. #if MG_ENABLE_DEBUG && !defined(CS_ENABLE_DEBUG)
  2441. #define CS_ENABLE_DEBUG 1
  2442. #endif
  2443. /* MQTT broker requires MQTT */
  2444. #if MG_ENABLE_MQTT_BROKER && !MG_ENABLE_MQTT
  2445. #undef MG_ENABLE_MQTT
  2446. #define MG_ENABLE_MQTT 1
  2447. #endif
  2448. #ifndef MG_ENABLE_HTTP_URL_REWRITES
  2449. #define MG_ENABLE_HTTP_URL_REWRITES \
  2450. (CS_PLATFORM == CS_P_WINDOWS || CS_PLATFORM == CS_P_UNIX)
  2451. #endif
  2452. #ifndef MG_ENABLE_TUN
  2453. #define MG_ENABLE_TUN MG_ENABLE_HTTP_WEBSOCKET
  2454. #endif
  2455. #ifndef MG_ENABLE_SNTP
  2456. #define MG_ENABLE_SNTP 0
  2457. #endif
  2458. #ifndef MG_ENABLE_EXTRA_ERRORS_DESC
  2459. #define MG_ENABLE_EXTRA_ERRORS_DESC 0
  2460. #endif
  2461. #endif /* CS_MONGOOSE_SRC_FEATURES_H_ */
  2462. #ifdef MG_MODULE_LINES
  2463. #line 1 "mongoose/src/net_if.h"
  2464. #endif
  2465. /*
  2466. * Copyright (c) 2014-2016 Cesanta Software Limited
  2467. * All rights reserved
  2468. */
  2469. #ifndef CS_MONGOOSE_SRC_NET_IF_H_
  2470. #define CS_MONGOOSE_SRC_NET_IF_H_
  2471. /* Amalgamated: #include "common/platform.h" */
  2472. /*
  2473. * Internal async networking core interface.
  2474. * Consists of calls made by the core, which should not block,
  2475. * and callbacks back into the core ("..._cb").
  2476. * Callbacks may (will) cause methods to be invoked from within,
  2477. * but methods are not allowed to invoke callbacks inline.
  2478. *
  2479. * Implementation must ensure that only one callback is invoked at any time.
  2480. */
  2481. #ifdef __cplusplus
  2482. extern "C" {
  2483. #endif /* __cplusplus */
  2484. #define MG_MAIN_IFACE 0
  2485. struct mg_mgr;
  2486. struct mg_connection;
  2487. union socket_address;
  2488. struct mg_iface_vtable;
  2489. struct mg_iface {
  2490. struct mg_mgr *mgr;
  2491. void *data; /* Implementation-specific data */
  2492. struct mg_iface_vtable *vtable;
  2493. };
  2494. struct mg_iface_vtable {
  2495. void (*init)(struct mg_iface *iface);
  2496. void (*free)(struct mg_iface *iface);
  2497. void (*add_conn)(struct mg_connection *nc);
  2498. void (*remove_conn)(struct mg_connection *nc);
  2499. time_t (*poll)(struct mg_iface *iface, int timeout_ms);
  2500. /* Set up a listening TCP socket on a given address. rv = 0 -> ok. */
  2501. int (*listen_tcp)(struct mg_connection *nc, union socket_address *sa);
  2502. /* Request that a "listening" UDP socket be created. */
  2503. int (*listen_udp)(struct mg_connection *nc, union socket_address *sa);
  2504. /* Request that a TCP connection is made to the specified address. */
  2505. void (*connect_tcp)(struct mg_connection *nc, const union socket_address *sa);
  2506. /* Open a UDP socket. Doesn't actually connect anything. */
  2507. void (*connect_udp)(struct mg_connection *nc);
  2508. /* Send functions for TCP and UDP. Sent data is copied before return. */
  2509. void (*tcp_send)(struct mg_connection *nc, const void *buf, size_t len);
  2510. void (*udp_send)(struct mg_connection *nc, const void *buf, size_t len);
  2511. void (*recved)(struct mg_connection *nc, size_t len);
  2512. /* Perform interface-related connection initialization. Return 1 on ok. */
  2513. int (*create_conn)(struct mg_connection *nc);
  2514. /* Perform interface-related cleanup on connection before destruction. */
  2515. void (*destroy_conn)(struct mg_connection *nc);
  2516. /* Associate a socket to a connection. */
  2517. void (*sock_set)(struct mg_connection *nc, sock_t sock);
  2518. /* Put connection's address into *sa, local (remote = 0) or remote. */
  2519. void (*get_conn_addr)(struct mg_connection *nc, int remote,
  2520. union socket_address *sa);
  2521. };
  2522. extern struct mg_iface_vtable *mg_ifaces[];
  2523. extern int mg_num_ifaces;
  2524. /* Creates a new interface instance. */
  2525. struct mg_iface *mg_if_create_iface(struct mg_iface_vtable *vtable,
  2526. struct mg_mgr *mgr);
  2527. /*
  2528. * Find an interface with a given implementation. The search is started from
  2529. * interface `from`, exclusive. Returns NULL if none is found.
  2530. */
  2531. struct mg_iface *mg_find_iface(struct mg_mgr *mgr,
  2532. struct mg_iface_vtable *vtable,
  2533. struct mg_iface *from);
  2534. /*
  2535. * Deliver a new TCP connection. Returns NULL in case on error (unable to
  2536. * create connection, in which case interface state should be discarded.
  2537. * This is phase 1 of the two-phase process - MG_EV_ACCEPT will be delivered
  2538. * when mg_if_accept_tcp_cb is invoked.
  2539. */
  2540. struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc);
  2541. void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa,
  2542. size_t sa_len);
  2543. /* Callback invoked by connect methods. err = 0 -> ok, != 0 -> error. */
  2544. void mg_if_connect_cb(struct mg_connection *nc, int err);
  2545. /* Callback that reports that data has been put on the wire. */
  2546. void mg_if_sent_cb(struct mg_connection *nc, int num_sent);
  2547. /*
  2548. * Receive callback.
  2549. * if `own` is true, buf must be heap-allocated and ownership is transferred
  2550. * to the core.
  2551. * Core will acknowledge consumption by calling iface::recved.
  2552. */
  2553. void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own);
  2554. /*
  2555. * Receive callback.
  2556. * buf must be heap-allocated and ownership is transferred to the core.
  2557. * Core will acknowledge consumption by calling iface::recved.
  2558. */
  2559. void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
  2560. union socket_address *sa, size_t sa_len);
  2561. /* void mg_if_close_conn(struct mg_connection *nc); */
  2562. /* Deliver a POLL event to the connection. */
  2563. void mg_if_poll(struct mg_connection *nc, time_t now);
  2564. /* Deliver a TIMER event to the connection. */
  2565. void mg_if_timer(struct mg_connection *c, double now);
  2566. #ifdef __cplusplus
  2567. }
  2568. #endif /* __cplusplus */
  2569. #endif /* CS_MONGOOSE_SRC_NET_IF_H_ */
  2570. #ifdef MG_MODULE_LINES
  2571. #line 1 "mongoose/src/ssl_if.h"
  2572. #endif
  2573. /*
  2574. * Copyright (c) 2014-2016 Cesanta Software Limited
  2575. * All rights reserved
  2576. */
  2577. #ifndef CS_MONGOOSE_SRC_SSL_IF_H_
  2578. #define CS_MONGOOSE_SRC_SSL_IF_H_
  2579. #if MG_ENABLE_SSL
  2580. #ifdef __cplusplus
  2581. extern "C" {
  2582. #endif /* __cplusplus */
  2583. struct mg_ssl_if_ctx;
  2584. struct mg_connection;
  2585. void mg_ssl_if_init();
  2586. enum mg_ssl_if_result {
  2587. MG_SSL_OK = 0,
  2588. MG_SSL_WANT_READ = -1,
  2589. MG_SSL_WANT_WRITE = -2,
  2590. MG_SSL_ERROR = -3,
  2591. };
  2592. struct mg_ssl_if_conn_params {
  2593. const char *cert;
  2594. const char *key;
  2595. const char *ca_cert;
  2596. const char *server_name;
  2597. const char *cipher_suites;
  2598. const char *psk_identity;
  2599. const char *psk_key;
  2600. };
  2601. enum mg_ssl_if_result mg_ssl_if_conn_init(
  2602. struct mg_connection *nc, const struct mg_ssl_if_conn_params *params,
  2603. const char **err_msg);
  2604. enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc,
  2605. struct mg_connection *lc);
  2606. void mg_ssl_if_conn_free(struct mg_connection *nc);
  2607. enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc);
  2608. int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size);
  2609. int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len);
  2610. #ifdef __cplusplus
  2611. }
  2612. #endif /* __cplusplus */
  2613. #endif /* MG_ENABLE_SSL */
  2614. #endif /* CS_MONGOOSE_SRC_SSL_IF_H_ */
  2615. #ifdef MG_MODULE_LINES
  2616. #line 1 "mongoose/src/net.h"
  2617. #endif
  2618. /*
  2619. * Copyright (c) 2014 Cesanta Software Limited
  2620. * All rights reserved
  2621. * This software is dual-licensed: you can redistribute it and/or modify
  2622. * it under the terms of the GNU General Public License version 2 as
  2623. * published by the Free Software Foundation. For the terms of this
  2624. * license, see <http://www.gnu.org/licenses/>.
  2625. *
  2626. * You are free to use this software under the terms of the GNU General
  2627. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  2628. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  2629. * See the GNU General Public License for more details.
  2630. *
  2631. * Alternatively, you can license this software under a commercial
  2632. * license, as set out in <https://www.cesanta.com/license>.
  2633. */
  2634. /*
  2635. * === Core API: TCP/UDP/SSL
  2636. *
  2637. * NOTE: Mongoose manager is single threaded. It does not protect
  2638. * its data structures by mutexes, therefore all functions that are dealing
  2639. * with a particular event manager should be called from the same thread,
  2640. * with exception of the `mg_broadcast()` function. It is fine to have different
  2641. * event managers handled by different threads.
  2642. */
  2643. #ifndef CS_MONGOOSE_SRC_NET_H_
  2644. #define CS_MONGOOSE_SRC_NET_H_
  2645. #if MG_ENABLE_JAVASCRIPT
  2646. #define EXCLUDE_COMMON
  2647. #include <v7.h>
  2648. #endif
  2649. /* Amalgamated: #include "mongoose/src/common.h" */
  2650. /* Amalgamated: #include "mongoose/src/net_if.h" */
  2651. /* Amalgamated: #include "common/mbuf.h" */
  2652. #ifndef MG_VPRINTF_BUFFER_SIZE
  2653. #define MG_VPRINTF_BUFFER_SIZE 100
  2654. #endif
  2655. #ifdef MG_USE_READ_WRITE
  2656. #define MG_RECV_FUNC(s, b, l, f) read(s, b, l)
  2657. #define MG_SEND_FUNC(s, b, l, f) write(s, b, l)
  2658. #else
  2659. #define MG_RECV_FUNC(s, b, l, f) recv(s, b, l, f)
  2660. #define MG_SEND_FUNC(s, b, l, f) send(s, b, l, f)
  2661. #endif
  2662. #ifdef __cplusplus
  2663. extern "C" {
  2664. #endif /* __cplusplus */
  2665. union socket_address {
  2666. struct sockaddr sa;
  2667. struct sockaddr_in sin;
  2668. #if MG_ENABLE_IPV6
  2669. struct sockaddr_in6 sin6;
  2670. #else
  2671. struct sockaddr sin6;
  2672. #endif
  2673. };
  2674. struct mg_connection;
  2675. /*
  2676. * Callback function (event handler) prototype. Must be defined by the user.
  2677. * Mongoose calls the event handler, passing the events defined below.
  2678. */
  2679. typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev,
  2680. void *ev_data);
  2681. /* Events. Meaning of event parameter (evp) is given in the comment. */
  2682. #define MG_EV_POLL 0 /* Sent to each connection on each mg_mgr_poll() call */
  2683. #define MG_EV_ACCEPT 1 /* New connection accepted. union socket_address * */
  2684. #define MG_EV_CONNECT 2 /* connect() succeeded or failed. int * */
  2685. #define MG_EV_RECV 3 /* Data has benn received. int *num_bytes */
  2686. #define MG_EV_SEND 4 /* Data has been written to a socket. int *num_bytes */
  2687. #define MG_EV_CLOSE 5 /* Connection is closed. NULL */
  2688. #define MG_EV_TIMER 6 /* now >= conn->ev_timer_time. double * */
  2689. /*
  2690. * Mongoose event manager.
  2691. */
  2692. struct mg_mgr {
  2693. struct mg_connection *active_connections;
  2694. #if MG_ENABLE_HEXDUMP
  2695. const char *hexdump_file; /* Debug hexdump file path */
  2696. #endif
  2697. #if MG_ENABLE_BROADCAST
  2698. sock_t ctl[2]; /* Socketpair for mg_broadcast() */
  2699. #endif
  2700. void *user_data; /* User data */
  2701. int num_ifaces;
  2702. struct mg_iface **ifaces; /* network interfaces */
  2703. #if MG_ENABLE_JAVASCRIPT
  2704. struct v7 *v7;
  2705. #endif
  2706. struct mg_timer *timers;
  2707. unsigned long timerid; // Next timer ID
  2708. };
  2709. /*
  2710. * Mongoose connection.
  2711. */
  2712. struct mg_connection {
  2713. struct mg_connection *next, *prev; /* mg_mgr::active_connections linkage */
  2714. struct mg_connection *listener; /* Set only for accept()-ed connections */
  2715. struct mg_mgr *mgr; /* Pointer to containing manager */
  2716. sock_t sock; /* Socket to the remote peer */
  2717. int err;
  2718. union socket_address sa; /* Remote peer address */
  2719. size_t recv_mbuf_limit; /* Max size of recv buffer */
  2720. struct mbuf recv_mbuf; /* Received data */
  2721. struct mbuf send_mbuf; /* Data scheduled for sending */
  2722. time_t last_io_time; /* Timestamp of the last socket IO */
  2723. double ev_timer_time; /* Timestamp of the future MG_EV_TIMER */
  2724. #if MG_ENABLE_SSL
  2725. void *ssl_if_data; /* SSL library data. */
  2726. #endif
  2727. mg_event_handler_t proto_handler; /* Protocol-specific event handler */
  2728. void *proto_data; /* Protocol-specific data */
  2729. void (*proto_data_destructor)(void *proto_data);
  2730. mg_event_handler_t handler; /* Event handler function */
  2731. void *user_data; /* User-specific data */
  2732. union {
  2733. void *v;
  2734. /*
  2735. * the C standard is fussy about fitting function pointers into
  2736. * void pointers, since some archs might have fat pointers for functions.
  2737. */
  2738. mg_event_handler_t f;
  2739. } priv_1;
  2740. void *priv_2;
  2741. void *mgr_data; /* Implementation-specific event manager's data. */
  2742. struct mg_iface *iface;
  2743. unsigned long flags;
  2744. /* Flags set by Mongoose */
  2745. #define MG_F_LISTENING (1 << 0) /* This connection is listening */
  2746. #define MG_F_UDP (1 << 1) /* This connection is UDP */
  2747. #define MG_F_RESOLVING (1 << 2) /* Waiting for async resolver */
  2748. #define MG_F_CONNECTING (1 << 3) /* connect() call in progress */
  2749. #define MG_F_SSL (1 << 4) /* SSL is enabled on the connection */
  2750. #define MG_F_SSL_HANDSHAKE_DONE (1 << 5) /* SSL hanshake has completed */
  2751. #define MG_F_WANT_READ (1 << 6) /* SSL specific */
  2752. #define MG_F_WANT_WRITE (1 << 7) /* SSL specific */
  2753. #define MG_F_IS_WEBSOCKET (1 << 8) /* Websocket specific */
  2754. /* Flags that are settable by user */
  2755. #define MG_F_SEND_AND_CLOSE (1 << 10) /* Push remaining data and close */
  2756. #define MG_F_CLOSE_IMMEDIATELY (1 << 11) /* Disconnect */
  2757. #define MG_F_WEBSOCKET_NO_DEFRAG (1 << 12) /* Websocket specific */
  2758. #define MG_F_DELETE_CHUNK (1 << 13) /* HTTP specific */
  2759. #define MG_F_ENABLE_BROADCAST (1 << 14) /* Allow broadcast address usage */
  2760. #define MG_F_TUN_DO_NOT_RECONNECT (1 << 15) /* Don't reconnect tunnel */
  2761. #define MG_F_USER_1 (1 << 20) /* Flags left for application */
  2762. #define MG_F_USER_2 (1 << 21)
  2763. #define MG_F_USER_3 (1 << 22)
  2764. #define MG_F_USER_4 (1 << 23)
  2765. #define MG_F_USER_5 (1 << 24)
  2766. #define MG_F_USER_6 (1 << 25)
  2767. };
  2768. /*
  2769. * Initialise Mongoose manager. Side effect: ignores SIGPIPE signal.
  2770. * `mgr->user_data` field will be initialised with a `user_data` parameter.
  2771. * That is an arbitrary pointer, where the user code can associate some data
  2772. * with the particular Mongoose manager. For example, a C++ wrapper class
  2773. * could be written in which case `user_data` can hold a pointer to the
  2774. * class instance.
  2775. */
  2776. void mg_mgr_init(struct mg_mgr *mgr, void *user_data);
  2777. /*
  2778. * Optional parameters to `mg_mgr_init_opt()`.
  2779. *
  2780. * If `main_iface` is not NULL, it will be used as the main interface in the
  2781. * default interface set. The pointer will be free'd by `mg_mgr_free`.
  2782. * Otherwise, the main interface will be autodetected based on the current
  2783. * platform.
  2784. *
  2785. * If `num_ifaces` is 0 and `ifaces` is NULL, the default interface set will be
  2786. * used.
  2787. * This is an advanced option, as it requires you to construct a full interface
  2788. * set, including special networking interfaces required by some optional
  2789. * features such as TCP tunneling. Memory backing `ifaces` and each of the
  2790. * `num_ifaces` pointers it contains will be reclaimed by `mg_mgr_free`.
  2791. */
  2792. struct mg_mgr_init_opts {
  2793. struct mg_iface_vtable *main_iface;
  2794. int num_ifaces;
  2795. struct mg_iface_vtable **ifaces;
  2796. };
  2797. /*
  2798. * Like `mg_mgr_init` but with more options.
  2799. *
  2800. * Notably, this allows you to create a manger and choose
  2801. * dynamically which networking interface implementation to use.
  2802. */
  2803. void mg_mgr_init_opt(struct mg_mgr *mgr, void *user_data,
  2804. struct mg_mgr_init_opts opts);
  2805. /*
  2806. * De-initialises Mongoose manager.
  2807. *
  2808. * Closes and deallocates all active connections.
  2809. */
  2810. void mg_mgr_free(struct mg_mgr *);
  2811. /*
  2812. * This function performs the actual IO and must be called in a loop
  2813. * (an event loop). It returns the current timestamp.
  2814. * `milli` is the maximum number of milliseconds to sleep.
  2815. * `mg_mgr_poll()` checks all connections for IO readiness. If at least one
  2816. * of the connections is IO-ready, `mg_mgr_poll()` triggers the respective
  2817. * event handlers and returns.
  2818. */
  2819. time_t mg_mgr_poll(struct mg_mgr *, int milli);
  2820. #if MG_ENABLE_BROADCAST
  2821. /*
  2822. * Passes a message of a given length to all connections.
  2823. *
  2824. * Must be called from a thread that does NOT call `mg_mgr_poll()`.
  2825. * Note that `mg_broadcast()` is the only function
  2826. * that can be, and must be, called from a different (non-IO) thread.
  2827. *
  2828. * `func` callback function will be called by the IO thread for each
  2829. * connection. When called, the event will be `MG_EV_POLL`, and a message will
  2830. * be passed as the `ev_data` pointer. Maximum message size is capped
  2831. * by `MG_CTL_MSG_MESSAGE_SIZE` which is set to 8192 bytes.
  2832. */
  2833. void mg_broadcast(struct mg_mgr *, mg_event_handler_t func, void *, size_t);
  2834. #endif
  2835. /*
  2836. * Iterates over all active connections.
  2837. *
  2838. * Returns the next connection from the list
  2839. * of active connections or `NULL` if there are no more connections. Below
  2840. * is the iteration idiom:
  2841. *
  2842. * ```c
  2843. * for (c = mg_next(srv, NULL); c != NULL; c = mg_next(srv, c)) {
  2844. * // Do something with connection `c`
  2845. * }
  2846. * ```
  2847. */
  2848. struct mg_connection *mg_next(struct mg_mgr *, struct mg_connection *);
  2849. /*
  2850. * Optional parameters to `mg_add_sock_opt()`.
  2851. *
  2852. * `flags` is an initial `struct mg_connection::flags` bitmask to set,
  2853. * see `MG_F_*` flags definitions.
  2854. */
  2855. struct mg_add_sock_opts {
  2856. void *user_data; /* Initial value for connection's user_data */
  2857. unsigned int flags; /* Initial connection flags */
  2858. const char **error_string; /* Placeholder for the error string */
  2859. struct mg_iface *iface; /* Interface instance */
  2860. };
  2861. /*
  2862. * Creates a connection, associates it with the given socket and event handler
  2863. * and adds it to the manager.
  2864. *
  2865. * For more options see the `mg_add_sock_opt` variant.
  2866. */
  2867. struct mg_connection *mg_add_sock(struct mg_mgr *, sock_t, mg_event_handler_t);
  2868. /*
  2869. * Creates a connection, associates it with the given socket and event handler
  2870. * and adds to the manager.
  2871. *
  2872. * See the `mg_add_sock_opts` structure for a description of the options.
  2873. */
  2874. struct mg_connection *mg_add_sock_opt(struct mg_mgr *, sock_t,
  2875. mg_event_handler_t,
  2876. struct mg_add_sock_opts);
  2877. /*
  2878. * Optional parameters to `mg_bind_opt()`.
  2879. *
  2880. * `flags` is an initial `struct mg_connection::flags` bitmask to set,
  2881. * see `MG_F_*` flags definitions.
  2882. */
  2883. struct mg_bind_opts {
  2884. void *user_data; /* Initial value for connection's user_data */
  2885. unsigned int flags; /* Extra connection flags */
  2886. const char **error_string; /* Placeholder for the error string */
  2887. struct mg_iface *iface; /* Interface instance */
  2888. #if MG_ENABLE_SSL
  2889. /*
  2890. * SSL settings.
  2891. *
  2892. * Server certificate to present to clients or client certificate to
  2893. * present to tunnel dispatcher (for tunneled connections).
  2894. */
  2895. const char *ssl_cert;
  2896. /* Private key corresponding to the certificate. If ssl_cert is set but
  2897. * ssl_key is not, ssl_cert is used. */
  2898. const char *ssl_key;
  2899. /* CA bundle used to verify client certificates or tunnel dispatchers. */
  2900. const char *ssl_ca_cert;
  2901. /* Colon-delimited list of acceptable cipher suites.
  2902. * Names depend on the library used, for example:
  2903. *
  2904. * ECDH-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256 (OpenSSL)
  2905. * TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256
  2906. * (mbedTLS)
  2907. *
  2908. * For OpenSSL the list can be obtained by running "openssl ciphers".
  2909. * For mbedTLS, names can be found in library/ssl_ciphersuites.c
  2910. * If NULL, a reasonable default is used.
  2911. */
  2912. const char *ssl_cipher_suites;
  2913. #endif
  2914. };
  2915. /*
  2916. * Creates a listening connection.
  2917. *
  2918. * See `mg_bind_opt` for full documentation.
  2919. */
  2920. struct mg_connection *mg_bind(struct mg_mgr *, const char *,
  2921. mg_event_handler_t);
  2922. /*
  2923. * Creates a listening connection.
  2924. *
  2925. * The `address` parameter specifies which address to bind to. It's format is
  2926. * the same as for the `mg_connect()` call, where `HOST` part is optional.
  2927. * `address` can be just a port number, e.g. `:8000`. To bind to a specific
  2928. * interface, an IP address can be specified, e.g. `1.2.3.4:8000`. By default,
  2929. * a TCP connection is created. To create UDP connection, prepend `udp://`
  2930. * prefix, e.g. `udp://:8000`. To summarize, `address` paramer has following
  2931. * format: `[PROTO://][IP_ADDRESS]:PORT`, where `PROTO` could be `tcp` or
  2932. * `udp`.
  2933. *
  2934. * See the `mg_bind_opts` structure for a description of the optional
  2935. * parameters.
  2936. *
  2937. * Returns a new listening connection or `NULL` on error.
  2938. * NOTE: The connection remains owned by the manager, do not free().
  2939. */
  2940. struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address,
  2941. mg_event_handler_t handler,
  2942. struct mg_bind_opts opts);
  2943. /* Optional parameters to `mg_connect_opt()` */
  2944. struct mg_connect_opts {
  2945. void *user_data; /* Initial value for connection's user_data */
  2946. unsigned int flags; /* Extra connection flags */
  2947. const char **error_string; /* Placeholder for the error string */
  2948. struct mg_iface *iface; /* Interface instance */
  2949. #if MG_ENABLE_SSL
  2950. /*
  2951. * SSL settings.
  2952. * Client certificate to present to the server.
  2953. */
  2954. const char *ssl_cert;
  2955. /*
  2956. * Private key corresponding to the certificate.
  2957. * If ssl_cert is set but ssl_key is not, ssl_cert is used.
  2958. */
  2959. const char *ssl_key;
  2960. /*
  2961. * Verify server certificate using this CA bundle. If set to "*", then SSL
  2962. * is enabled but no cert verification is performed.
  2963. */
  2964. const char *ssl_ca_cert;
  2965. /* Colon-delimited list of acceptable cipher suites.
  2966. * Names depend on the library used, for example:
  2967. *
  2968. * ECDH-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256 (OpenSSL)
  2969. * TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256
  2970. * (mbedTLS)
  2971. *
  2972. * For OpenSSL the list can be obtained by running "openssl ciphers".
  2973. * For mbedTLS, names can be found in library/ssl_ciphersuites.c
  2974. * If NULL, a reasonable default is used.
  2975. */
  2976. const char *ssl_cipher_suites;
  2977. /*
  2978. * Server name verification. If ssl_ca_cert is set and the certificate has
  2979. * passed verification, its subject will be verified against this string.
  2980. * By default (if ssl_server_name is NULL) hostname part of the address will
  2981. * be used. Wildcard matching is supported. A special value of "*" disables
  2982. * name verification.
  2983. */
  2984. const char *ssl_server_name;
  2985. /*
  2986. * PSK identity and key. Identity is a NUL-terminated string and key is a hex
  2987. * string. Key must be either 16 or 32 bytes (32 or 64 hex digits) for AES-128
  2988. * or AES-256 respectively.
  2989. * Note: Default list of cipher suites does not include PSK suites, if you
  2990. * want to use PSK you will need to set ssl_cipher_suites as well.
  2991. */
  2992. const char *ssl_psk_identity;
  2993. const char *ssl_psk_key;
  2994. #endif
  2995. };
  2996. /*
  2997. * Connects to a remote host.
  2998. *
  2999. * See `mg_connect_opt()` for full documentation.
  3000. */
  3001. struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address,
  3002. mg_event_handler_t handler);
  3003. /*
  3004. * Connects to a remote host.
  3005. *
  3006. * The `address` format is `[PROTO://]HOST:PORT`. `PROTO` could be `tcp` or
  3007. * `udp`. `HOST` could be an IP address,
  3008. * IPv6 address (if Mongoose is compiled with `-DMG_ENABLE_IPV6`) or a host
  3009. * name. If `HOST` is a name, Mongoose will resolve it asynchronously. Examples
  3010. * of valid addresses: `google.com:80`, `udp://1.2.3.4:53`, `10.0.0.1:443`,
  3011. * `[::1]:80`
  3012. *
  3013. * See the `mg_connect_opts` structure for a description of the optional
  3014. * parameters.
  3015. *
  3016. * Returns a new outbound connection or `NULL` on error.
  3017. *
  3018. * NOTE: The connection remains owned by the manager, do not free().
  3019. *
  3020. * NOTE: To enable IPv6 addresses `-DMG_ENABLE_IPV6` should be specified
  3021. * in the compilation flags.
  3022. *
  3023. * NOTE: The new connection will receive `MG_EV_CONNECT` as its first event
  3024. * which will report the connect success status.
  3025. * If the asynchronous resolution fails or the `connect()` syscall fails for
  3026. * whatever reason (e.g. with `ECONNREFUSED` or `ENETUNREACH`), then
  3027. * `MG_EV_CONNECT` event will report failure. Code example below:
  3028. *
  3029. * ```c
  3030. * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  3031. * int connect_status;
  3032. *
  3033. * switch (ev) {
  3034. * case MG_EV_CONNECT:
  3035. * connect_status = * (int *) ev_data;
  3036. * if (connect_status == 0) {
  3037. * // Success
  3038. * } else {
  3039. * // Error
  3040. * printf("connect() error: %s\n", strerror(connect_status));
  3041. * }
  3042. * break;
  3043. * ...
  3044. * }
  3045. * }
  3046. *
  3047. * ...
  3048. * mg_connect(mgr, "my_site.com:80", ev_handler);
  3049. * ```
  3050. */
  3051. struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address,
  3052. mg_event_handler_t handler,
  3053. struct mg_connect_opts opts);
  3054. #if MG_ENABLE_SSL && MG_NET_IF != MG_NET_IF_SIMPLELINK
  3055. /*
  3056. * Note: This function is deprecated. Please, use SSL options in
  3057. * mg_connect_opt.
  3058. *
  3059. * Enables SSL for a given connection.
  3060. * `cert` is a server certificate file name for a listening connection
  3061. * or a client certificate file name for an outgoing connection.
  3062. * The certificate files must be in PEM format. The server certificate file
  3063. * must contain a certificate, concatenated with a private key, optionally
  3064. * concatenated with DH parameters.
  3065. * `ca_cert` is a CA certificate or NULL if peer verification is not
  3066. * required.
  3067. * Return: NULL on success or error message on error.
  3068. */
  3069. const char *mg_set_ssl(struct mg_connection *nc, const char *cert,
  3070. const char *ca_cert);
  3071. #endif
  3072. /*
  3073. * Sends data to the connection.
  3074. *
  3075. * Note that sending functions do not actually push data to the socket.
  3076. * They just append data to the output buffer. MG_EV_SEND will be delivered when
  3077. * the data has actually been pushed out.
  3078. */
  3079. void mg_send(struct mg_connection *, const void *buf, int len);
  3080. /* Enables format string warnings for mg_printf */
  3081. #if defined(__GNUC__)
  3082. __attribute__((format(printf, 2, 3)))
  3083. #endif
  3084. /* don't separate from mg_printf declaration */
  3085. /*
  3086. * Sends `printf`-style formatted data to the connection.
  3087. *
  3088. * See `mg_send` for more details on send semantics.
  3089. */
  3090. int mg_printf(struct mg_connection *, const char *fmt, ...);
  3091. /* Same as `mg_printf()`, but takes `va_list ap` as an argument. */
  3092. int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap);
  3093. /*
  3094. * Creates a socket pair.
  3095. * `sock_type` can be either `SOCK_STREAM` or `SOCK_DGRAM`.
  3096. * Returns 0 on failure and 1 on success.
  3097. */
  3098. int mg_socketpair(sock_t[2], int sock_type);
  3099. #if MG_ENABLE_SYNC_RESOLVER
  3100. /*
  3101. * Convert domain name into IP address.
  3102. *
  3103. * This is a utility function. If compilation flags have
  3104. * `-DMG_ENABLE_GETADDRINFO`, then `getaddrinfo()` call is used for name
  3105. * resolution. Otherwise, `gethostbyname()` is used.
  3106. *
  3107. * CAUTION: this function can block.
  3108. * Return 1 on success, 0 on failure.
  3109. */
  3110. int mg_resolve(const char *domain_name, char *ip_addr_buf, size_t buf_len);
  3111. #endif
  3112. /*
  3113. * Verify given IP address against the ACL.
  3114. *
  3115. * `remote_ip` - an IPv4 address to check, in host byte order
  3116. * `acl` - a comma separated list of IP subnets: `x.x.x.x/x` or `x.x.x.x`.
  3117. * Each subnet is
  3118. * prepended by either a - or a + sign. A plus sign means allow, where a
  3119. * minus sign means deny. If a subnet mask is omitted, such as `-1.2.3.4`,
  3120. * it means that only that single IP address is denied.
  3121. * Subnet masks may vary from 0 to 32, inclusive. The default setting
  3122. * is to allow all access. On each request the full list is traversed,
  3123. * and the last match wins. Example:
  3124. *
  3125. * `-0.0.0.0/0,+192.168/16` - deny all acccesses, only allow 192.168/16 subnet
  3126. *
  3127. * To learn more about subnet masks, see this
  3128. * link:https://en.wikipedia.org/wiki/Subnetwork[Wikipedia page on Subnetwork].
  3129. *
  3130. * Returns -1 if ACL is malformed, 0 if address is disallowed, 1 if allowed.
  3131. */
  3132. int mg_check_ip_acl(const char *acl, uint32_t remote_ip);
  3133. #if MG_ENABLE_JAVASCRIPT
  3134. /*
  3135. * Enables server-side JavaScript scripting.
  3136. * Requires a `-DMG_ENABLE_JAVASCRIPT` compilation flag and V7 engine sources.
  3137. * V7 instance must not be destroyed during manager's lifetime.
  3138. * Returns a V7 error.
  3139. */
  3140. enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7,
  3141. const char *init_js_file_name);
  3142. #endif
  3143. /*
  3144. * Schedules an MG_EV_TIMER event to be delivered at `timestamp` time.
  3145. * `timestamp` is UNIX time (the number of seconds since Epoch). It is
  3146. * `double` instead of `time_t` to allow for sub-second precision.
  3147. * Returns the old timer value.
  3148. *
  3149. * Example: set the connect timeout to 1.5 seconds:
  3150. *
  3151. * ```
  3152. * c = mg_connect(&mgr, "cesanta.com", ev_handler);
  3153. * mg_set_timer(c, mg_time() + 1.5);
  3154. * ...
  3155. *
  3156. * void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
  3157. * switch (ev) {
  3158. * case MG_EV_CONNECT:
  3159. * mg_set_timer(c, 0); // Clear connect timer
  3160. * break;
  3161. * case MG_EV_TIMER:
  3162. * log("Connect timeout");
  3163. * c->flags |= MG_F_CLOSE_IMMEDIATELY;
  3164. * break;
  3165. * ```
  3166. */
  3167. double mg_set_timer(struct mg_connection *c, double timestamp);
  3168. /*
  3169. * A sub-second precision version of time().
  3170. */
  3171. double mg_time(void);
  3172. #ifdef __cplusplus
  3173. }
  3174. #endif /* __cplusplus */
  3175. #endif /* CS_MONGOOSE_SRC_NET_H_ */
  3176. #ifdef MG_MODULE_LINES
  3177. #line 1 "mongoose/src/uri.h"
  3178. #endif
  3179. /*
  3180. * Copyright (c) 2014 Cesanta Software Limited
  3181. * All rights reserved
  3182. */
  3183. /*
  3184. * === URI
  3185. */
  3186. #ifndef CS_MONGOOSE_SRC_URI_H_
  3187. #define CS_MONGOOSE_SRC_URI_H_
  3188. /* Amalgamated: #include "mongoose/src/net.h" */
  3189. #ifdef __cplusplus
  3190. extern "C" {
  3191. #endif /* __cplusplus */
  3192. /*
  3193. * Parses an URI and fills string chunks with locations of the respective
  3194. * uri components within the input uri string. NULL pointers will be
  3195. * ignored.
  3196. *
  3197. * General syntax:
  3198. *
  3199. * [scheme://[user_info@]]host[:port][/path][?query][#fragment]
  3200. *
  3201. * Example:
  3202. *
  3203. * foo.com:80
  3204. * tcp://foo.com:1234
  3205. * http://foo.com:80/bar?baz=1
  3206. * https://user:pw@foo.com:443/blah
  3207. *
  3208. * `path` will include the leading slash. `query` won't include the leading `?`.
  3209. * `host` can contain embedded colons if surrounded by square brackets in order
  3210. * to support IPv6 literal addresses.
  3211. *
  3212. *
  3213. * Returns 0 on success, -1 on error.
  3214. */
  3215. int mg_parse_uri(struct mg_str uri, struct mg_str *scheme,
  3216. struct mg_str *user_info, struct mg_str *host,
  3217. unsigned int *port, struct mg_str *path, struct mg_str *query,
  3218. struct mg_str *fragment);
  3219. int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out);
  3220. #ifdef __cplusplus
  3221. }
  3222. #endif /* __cplusplus */
  3223. #endif /* CS_MONGOOSE_SRC_URI_H_ */
  3224. #ifdef MG_MODULE_LINES
  3225. #line 1 "mongoose/src/util.h"
  3226. #endif
  3227. /*
  3228. * Copyright (c) 2014 Cesanta Software Limited
  3229. * All rights reserved
  3230. */
  3231. /*
  3232. * === Utility API
  3233. */
  3234. #ifndef CS_MONGOOSE_SRC_UTIL_H_
  3235. #define CS_MONGOOSE_SRC_UTIL_H_
  3236. #include <stdio.h>
  3237. /* Amalgamated: #include "mongoose/src/common.h" */
  3238. /* Amalgamated: #include "mongoose/src/net_if.h" */
  3239. #ifdef __cplusplus
  3240. extern "C" {
  3241. #endif /* __cplusplus */
  3242. #ifndef MAX_PATH_SIZE
  3243. #define MAX_PATH_SIZE 500
  3244. #endif
  3245. /*
  3246. * Fetches substring from input string `s`, `end` into `v`.
  3247. * Skips initial delimiter characters. Records first non-delimiter character
  3248. * at the beginning of substring `v`. Then scans the rest of the string
  3249. * until a delimiter character or end-of-string is found.
  3250. * `delimiters` is a 0-terminated string containing delimiter characters.
  3251. * Either one of `delimiters` or `end_string` terminates the search.
  3252. * Returns an `s` pointer, advanced forward where parsing has stopped.
  3253. */
  3254. const char *mg_skip(const char *s, const char *end_string,
  3255. const char *delimiters, struct mg_str *v);
  3256. /*
  3257. * Decodes base64-encoded string `s`, `len` into the destination `dst`.
  3258. * The destination has to have enough space to hold the decoded buffer.
  3259. * Decoding stops either when all strings have been decoded or invalid an
  3260. * character appeared.
  3261. * Destination is '\0'-terminated.
  3262. * Returns the number of decoded characters. On success, that should be equal
  3263. * to `len`. On error (invalid character) the return value is smaller then
  3264. * `len`.
  3265. */
  3266. int mg_base64_decode(const unsigned char *s, int len, char *dst);
  3267. /*
  3268. * Base64-encode chunk of memory `src`, `src_len` into the destination `dst`.
  3269. * Destination has to have enough space to hold encoded buffer.
  3270. * Destination is '\0'-terminated.
  3271. */
  3272. void mg_base64_encode(const unsigned char *src, int src_len, char *dst);
  3273. #if MG_ENABLE_FILESYSTEM
  3274. /*
  3275. * Performs a 64-bit `stat()` call against a given file.
  3276. *
  3277. * `path` should be UTF8 encoded.
  3278. *
  3279. * Return value is the same as for `stat()` syscall.
  3280. */
  3281. int mg_stat(const char *path, cs_stat_t *st);
  3282. /*
  3283. * Opens the given file and returns a file stream.
  3284. *
  3285. * `path` and `mode` should be UTF8 encoded.
  3286. *
  3287. * Return value is the same as for the `fopen()` call.
  3288. */
  3289. FILE *mg_fopen(const char *path, const char *mode);
  3290. /*
  3291. * Opens the given file and returns a file stream.
  3292. *
  3293. * `path` should be UTF8 encoded.
  3294. *
  3295. * Return value is the same as for the `open()` syscall.
  3296. */
  3297. int mg_open(const char *path, int flag, int mode);
  3298. #endif /* MG_ENABLE_FILESYSTEM */
  3299. #if MG_ENABLE_THREADS
  3300. /*
  3301. * Starts a new detached thread.
  3302. * Arguments and semantics are the same as pthead's `pthread_create()`.
  3303. * `thread_func` is a thread function, `thread_func_param` is a parameter
  3304. * that is passed to the thread function.
  3305. */
  3306. void *mg_start_thread(void *(*thread_func)(void *), void *thread_func_param);
  3307. #endif
  3308. void mg_set_close_on_exec(sock_t);
  3309. #define MG_SOCK_STRINGIFY_IP 1
  3310. #define MG_SOCK_STRINGIFY_PORT 2
  3311. #define MG_SOCK_STRINGIFY_REMOTE 4
  3312. /*
  3313. * Converts a connection's local or remote address into string.
  3314. *
  3315. * The `flags` parameter is a bit mask that controls the behaviour,
  3316. * see `MG_SOCK_STRINGIFY_*` definitions.
  3317. *
  3318. * - MG_SOCK_STRINGIFY_IP - print IP address
  3319. * - MG_SOCK_STRINGIFY_PORT - print port number
  3320. * - MG_SOCK_STRINGIFY_REMOTE - print remote peer's IP/port, not local address
  3321. *
  3322. * If both port number and IP address are printed, they are separated by `:`.
  3323. * If compiled with `-DMG_ENABLE_IPV6`, IPv6 addresses are supported.
  3324. */
  3325. void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
  3326. int flags);
  3327. #if MG_NET_IF == MG_NET_IF_SOCKET
  3328. /* Legacy interface. */
  3329. void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags);
  3330. #endif
  3331. /*
  3332. * Convert the socket's address into string.
  3333. *
  3334. * `flags` is MG_SOCK_STRINGIFY_IP and/or MG_SOCK_STRINGIFY_PORT.
  3335. */
  3336. void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len,
  3337. int flags);
  3338. #if MG_ENABLE_HEXDUMP
  3339. /*
  3340. * Generates a human-readable hexdump of memory chunk.
  3341. *
  3342. * Takes a memory buffer `buf` of length `len` and creates a hex dump of that
  3343. * buffer in `dst`. The generated output is a-la hexdump(1).
  3344. * Returns the length of generated string, excluding terminating `\0`. If
  3345. * returned length is bigger than `dst_len`, the overflow bytes are discarded.
  3346. */
  3347. int mg_hexdump(const void *buf, int len, char *dst, int dst_len);
  3348. /* Same as mg_hexdump, but with output going to file instead of a buffer. */
  3349. void mg_hexdumpf(FILE *fp, const void *buf, int len);
  3350. /*
  3351. * Generates human-readable hexdump of the data sent or received by the
  3352. * connection. `path` is a file name where hexdump should be written.
  3353. * `num_bytes` is a number of bytes sent/received. `ev` is one of the `MG_*`
  3354. * events sent to an event handler. This function is supposed to be called from
  3355. * the event handler.
  3356. */
  3357. void mg_hexdump_connection(struct mg_connection *nc, const char *path,
  3358. const void *buf, int num_bytes, int ev);
  3359. #endif
  3360. /*
  3361. * Returns true if target platform is big endian.
  3362. */
  3363. int mg_is_big_endian(void);
  3364. /*
  3365. * A helper function for traversing a comma separated list of values.
  3366. * It returns a list pointer shifted to the next value or NULL if the end
  3367. * of the list found.
  3368. * The value is stored in a val vector. If the value has a form "x=y", then
  3369. * eq_val vector is initialised to point to the "y" part, and val vector length
  3370. * is adjusted to point only to "x".
  3371. * If the list is just a comma separated list of entries, like "aa,bb,cc" then
  3372. * `eq_val` will contain zero-length string.
  3373. *
  3374. * The purpose of this function is to parse comma separated string without
  3375. * any copying/memory allocation.
  3376. */
  3377. const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
  3378. struct mg_str *eq_val);
  3379. /*
  3380. * Matches 0-terminated string (mg_match_prefix) or string with given length
  3381. * mg_match_prefix_n against a glob pattern.
  3382. *
  3383. * Match is case-insensitive. Returns number of bytes matched, or -1 if no
  3384. * match.
  3385. */
  3386. int mg_match_prefix(const char *pattern, int pattern_len, const char *str);
  3387. int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str);
  3388. /*
  3389. * Use with cs_base64_init/update/finish in order to write out base64 in chunks.
  3390. */
  3391. void mg_mbuf_append_base64_putc(char ch, void *user_data);
  3392. /*
  3393. * Encode `len` bytes starting at `data` as base64 and append them to an mbuf.
  3394. */
  3395. void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len);
  3396. /*
  3397. * Generate a Basic Auth header and appends it to buf.
  3398. * If pass is NULL, then user is expected to contain the credentials pair
  3399. * already encoded as `user:pass`.
  3400. */
  3401. void mg_basic_auth_header(const char *user, const char *pass, struct mbuf *buf);
  3402. #ifdef __cplusplus
  3403. }
  3404. #endif /* __cplusplus */
  3405. #endif /* CS_MONGOOSE_SRC_UTIL_H_ */
  3406. #ifdef MG_MODULE_LINES
  3407. #line 1 "mongoose/src/http.h"
  3408. #endif
  3409. /*
  3410. * Copyright (c) 2014 Cesanta Software Limited
  3411. * All rights reserved
  3412. */
  3413. /*
  3414. * === Common API reference
  3415. */
  3416. #ifndef CS_MONGOOSE_SRC_HTTP_H_
  3417. #define CS_MONGOOSE_SRC_HTTP_H_
  3418. #if MG_ENABLE_HTTP
  3419. /* Amalgamated: #include "mongoose/src/net.h" */
  3420. /* Amalgamated: #include "common/mg_str.h" */
  3421. #ifdef __cplusplus
  3422. extern "C" {
  3423. #endif /* __cplusplus */
  3424. #ifndef MG_MAX_HTTP_HEADERS
  3425. #define MG_MAX_HTTP_HEADERS 20
  3426. #endif
  3427. #ifndef MG_MAX_HTTP_REQUEST_SIZE
  3428. #define MG_MAX_HTTP_REQUEST_SIZE 1024
  3429. #endif
  3430. #ifndef MG_MAX_PATH
  3431. #ifdef PATH_MAX
  3432. #define MG_MAX_PATH PATH_MAX
  3433. #else
  3434. #define MG_MAX_PATH 256
  3435. #endif
  3436. #endif
  3437. #ifndef MG_MAX_HTTP_SEND_MBUF
  3438. #define MG_MAX_HTTP_SEND_MBUF 1024
  3439. #endif
  3440. #ifndef MG_CGI_ENVIRONMENT_SIZE
  3441. #define MG_CGI_ENVIRONMENT_SIZE 8192
  3442. #endif
  3443. /* HTTP message */
  3444. struct http_message {
  3445. struct mg_str message; /* Whole message: request line + headers + body */
  3446. /* HTTP Request line (or HTTP response line) */
  3447. struct mg_str method; /* "GET" */
  3448. struct mg_str uri; /* "/my_file.html" */
  3449. struct mg_str proto; /* "HTTP/1.1" -- for both request and response */
  3450. /* For responses, code and response status message are set */
  3451. int resp_code;
  3452. struct mg_str resp_status_msg;
  3453. /*
  3454. * Query-string part of the URI. For example, for HTTP request
  3455. * GET /foo/bar?param1=val1&param2=val2
  3456. * | uri | query_string |
  3457. *
  3458. * Note that question mark character doesn't belong neither to the uri,
  3459. * nor to the query_string
  3460. */
  3461. struct mg_str query_string;
  3462. /* Headers */
  3463. struct mg_str header_names[MG_MAX_HTTP_HEADERS];
  3464. struct mg_str header_values[MG_MAX_HTTP_HEADERS];
  3465. /* Message body */
  3466. struct mg_str body; /* Zero-length for requests with no body */
  3467. };
  3468. #if MG_ENABLE_HTTP_WEBSOCKET
  3469. /* WebSocket message */
  3470. struct websocket_message {
  3471. unsigned char *data;
  3472. size_t size;
  3473. unsigned char flags;
  3474. };
  3475. #endif
  3476. /* HTTP multipart part */
  3477. struct mg_http_multipart_part {
  3478. const char *file_name;
  3479. const char *var_name;
  3480. struct mg_str data;
  3481. int status; /* <0 on error */
  3482. void *user_data;
  3483. };
  3484. /* SSI call context */
  3485. struct mg_ssi_call_ctx {
  3486. struct http_message *req; /* The request being processed. */
  3487. struct mg_str file; /* Filesystem path of the file being processed. */
  3488. struct mg_str arg; /* The argument passed to the tag: <!-- call arg -->. */
  3489. };
  3490. /* HTTP and websocket events. void *ev_data is described in a comment. */
  3491. #define MG_EV_HTTP_REQUEST 100 /* struct http_message * */
  3492. #define MG_EV_HTTP_REPLY 101 /* struct http_message * */
  3493. #define MG_EV_HTTP_CHUNK 102 /* struct http_message * */
  3494. #define MG_EV_SSI_CALL 105 /* char * */
  3495. #define MG_EV_SSI_CALL_CTX 106 /* struct mg_ssi_call_ctx * */
  3496. #if MG_ENABLE_HTTP_WEBSOCKET
  3497. #define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST 111 /* struct http_message * */
  3498. #define MG_EV_WEBSOCKET_HANDSHAKE_DONE 112 /* NULL */
  3499. #define MG_EV_WEBSOCKET_FRAME 113 /* struct websocket_message * */
  3500. #define MG_EV_WEBSOCKET_CONTROL_FRAME 114 /* struct websocket_message * */
  3501. #endif
  3502. #if MG_ENABLE_HTTP_STREAMING_MULTIPART
  3503. #define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */
  3504. #define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */
  3505. #define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */
  3506. #define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */
  3507. /* struct mg_http_multipart_part */
  3508. #define MG_EV_HTTP_MULTIPART_REQUEST_END 125
  3509. #endif
  3510. /*
  3511. * Attaches a built-in HTTP event handler to the given connection.
  3512. * The user-defined event handler will receive following extra events:
  3513. *
  3514. * - MG_EV_HTTP_REQUEST: HTTP request has arrived. Parsed HTTP request
  3515. * is passed as
  3516. * `struct http_message` through the handler's `void *ev_data` pointer.
  3517. * - MG_EV_HTTP_REPLY: The HTTP reply has arrived. The parsed HTTP reply is
  3518. * passed as `struct http_message` through the handler's `void *ev_data`
  3519. * pointer.
  3520. * - MG_EV_HTTP_CHUNK: The HTTP chunked-encoding chunk has arrived.
  3521. * The parsed HTTP reply is passed as `struct http_message` through the
  3522. * handler's `void *ev_data` pointer. `http_message::body` would contain
  3523. * incomplete, reassembled HTTP body.
  3524. * It will grow with every new chunk that arrives, and it can
  3525. * potentially consume a lot of memory. An event handler may process
  3526. * the body as chunks are coming, and signal Mongoose to delete processed
  3527. * body by setting `MG_F_DELETE_CHUNK` in `mg_connection::flags`. When
  3528. * the last zero chunk is received,
  3529. * Mongoose sends `MG_EV_HTTP_REPLY` event with
  3530. * full reassembled body (if handler did not signal to delete chunks) or
  3531. * with empty body (if handler did signal to delete chunks).
  3532. * - MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: server has received the WebSocket
  3533. * handshake request. `ev_data` contains parsed HTTP request.
  3534. * - MG_EV_WEBSOCKET_HANDSHAKE_DONE: server has completed the WebSocket
  3535. * handshake. `ev_data` is `NULL`.
  3536. * - MG_EV_WEBSOCKET_FRAME: new WebSocket frame has arrived. `ev_data` is
  3537. * `struct websocket_message *`
  3538. *
  3539. * When compiled with MG_ENABLE_HTTP_STREAMING_MULTIPART, Mongoose parses
  3540. * multipart requests and splits them into separate events:
  3541. * - MG_EV_HTTP_MULTIPART_REQUEST: Start of the request.
  3542. * This event is sent before body is parsed. After this, the user
  3543. * should expect a sequence of PART_BEGIN/DATA/END requests.
  3544. * This is also the last time when headers and other request fields are
  3545. * accessible.
  3546. * - MG_EV_HTTP_PART_BEGIN: Start of a part of a multipart message.
  3547. * Argument: mg_http_multipart_part with var_name and file_name set
  3548. * (if present). No data is passed in this message.
  3549. * - MG_EV_HTTP_PART_DATA: new portion of data from the multipart message.
  3550. * Argument: mg_http_multipart_part. var_name and file_name are preserved,
  3551. * data is available in mg_http_multipart_part.data.
  3552. * - MG_EV_HTTP_PART_END: End of the current part. var_name, file_name are
  3553. * the same, no data in the message. If status is 0, then the part is
  3554. * properly terminated with a boundary, status < 0 means that connection
  3555. * was terminated.
  3556. * - MG_EV_HTTP_MULTIPART_REQUEST_END: End of the multipart request.
  3557. * Argument: mg_http_multipart_part, var_name and file_name are NULL,
  3558. * status = 0 means request was properly closed, < 0 means connection
  3559. * was terminated (note: in this case both PART_END and REQUEST_END are
  3560. * delivered).
  3561. */
  3562. void mg_set_protocol_http_websocket(struct mg_connection *nc);
  3563. #if MG_ENABLE_HTTP_WEBSOCKET
  3564. /*
  3565. * Send websocket handshake to the server.
  3566. *
  3567. * `nc` must be a valid connection, connected to a server. `uri` is an URI
  3568. * to fetch, extra_headers` is extra HTTP headers to send or `NULL`.
  3569. *
  3570. * This function is intended to be used by websocket client.
  3571. *
  3572. * Note that the Host header is mandatory in HTTP/1.1 and must be
  3573. * included in `extra_headers`. `mg_send_websocket_handshake2` offers
  3574. * a better API for that.
  3575. *
  3576. * Deprecated in favour of `mg_send_websocket_handshake2`
  3577. */
  3578. void mg_send_websocket_handshake(struct mg_connection *nc, const char *uri,
  3579. const char *extra_headers);
  3580. /*
  3581. * Send websocket handshake to the server.
  3582. *
  3583. * `nc` must be a valid connection, connected to a server. `uri` is an URI
  3584. * to fetch, `host` goes into the `Host` header, `protocol` goes into the
  3585. * `Sec-WebSocket-Proto` header (NULL to omit), extra_headers` is extra HTTP
  3586. * headers to send or `NULL`.
  3587. *
  3588. * This function is intended to be used by websocket client.
  3589. */
  3590. void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path,
  3591. const char *host, const char *protocol,
  3592. const char *extra_headers);
  3593. /* Like mg_send_websocket_handshake2 but also passes basic auth header */
  3594. void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path,
  3595. const char *host, const char *protocol,
  3596. const char *extra_headers, const char *user,
  3597. const char *pass);
  3598. /*
  3599. * Helper function that creates an outbound WebSocket connection.
  3600. *
  3601. * `url` is a URL to connect to. It must be properly URL-encoded, e.g. have
  3602. * no spaces, etc. By default, `mg_connect_ws()` sends Connection and
  3603. * Host headers. `extra_headers` is an extra HTTP header to send, e.g.
  3604. * `"User-Agent: my-app\r\n"`.
  3605. * If `protocol` is not NULL, then a `Sec-WebSocket-Protocol` header is sent.
  3606. *
  3607. * Examples:
  3608. *
  3609. * ```c
  3610. * nc1 = mg_connect_ws(mgr, ev_handler_1, "ws://echo.websocket.org", NULL,
  3611. * NULL);
  3612. * nc2 = mg_connect_ws(mgr, ev_handler_1, "wss://echo.websocket.org", NULL,
  3613. * NULL);
  3614. * nc3 = mg_connect_ws(mgr, ev_handler_1, "ws://api.cesanta.com",
  3615. * "clubby.cesanta.com", NULL);
  3616. * ```
  3617. */
  3618. struct mg_connection *mg_connect_ws(struct mg_mgr *mgr,
  3619. mg_event_handler_t event_handler,
  3620. const char *url, const char *protocol,
  3621. const char *extra_headers);
  3622. /*
  3623. * Helper function that creates an outbound WebSocket connection
  3624. *
  3625. * Mostly identical to `mg_connect_ws`, but allows to provide extra parameters
  3626. * (for example, SSL parameters)
  3627. */
  3628. struct mg_connection *mg_connect_ws_opt(struct mg_mgr *mgr,
  3629. mg_event_handler_t ev_handler,
  3630. struct mg_connect_opts opts,
  3631. const char *url, const char *protocol,
  3632. const char *extra_headers);
  3633. /*
  3634. * Send WebSocket frame to the remote end.
  3635. *
  3636. * `op_and_flags` specifies the frame's type. It's one of:
  3637. *
  3638. * - WEBSOCKET_OP_CONTINUE
  3639. * - WEBSOCKET_OP_TEXT
  3640. * - WEBSOCKET_OP_BINARY
  3641. * - WEBSOCKET_OP_CLOSE
  3642. * - WEBSOCKET_OP_PING
  3643. * - WEBSOCKET_OP_PONG
  3644. *
  3645. * Orred with one of the flags:
  3646. *
  3647. * - WEBSOCKET_DONT_FIN: Don't set the FIN flag on the frame to be sent.
  3648. *
  3649. * `data` and `data_len` contain frame data.
  3650. */
  3651. void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags,
  3652. const void *data, size_t data_len);
  3653. /*
  3654. * Sends multiple websocket frames.
  3655. *
  3656. * Like `mg_send_websocket_frame()`, but composes a frame from multiple buffers.
  3657. */
  3658. void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags,
  3659. const struct mg_str *strings, int num_strings);
  3660. /*
  3661. * Sends WebSocket frame to the remote end.
  3662. *
  3663. * Like `mg_send_websocket_frame()`, but allows to create formatted messages
  3664. * with `printf()`-like semantics.
  3665. */
  3666. void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags,
  3667. const char *fmt, ...);
  3668. /* Websocket opcodes, from http://tools.ietf.org/html/rfc6455 */
  3669. #define WEBSOCKET_OP_CONTINUE 0
  3670. #define WEBSOCKET_OP_TEXT 1
  3671. #define WEBSOCKET_OP_BINARY 2
  3672. #define WEBSOCKET_OP_CLOSE 8
  3673. #define WEBSOCKET_OP_PING 9
  3674. #define WEBSOCKET_OP_PONG 10
  3675. /*
  3676. * If set causes the FIN flag to not be set on outbound
  3677. * frames. This enables sending multiple fragments of a single
  3678. * logical message.
  3679. *
  3680. * The WebSocket protocol mandates that if the FIN flag of a data
  3681. * frame is not set, the next frame must be a WEBSOCKET_OP_CONTINUE.
  3682. * The last frame must have the FIN bit set.
  3683. *
  3684. * Note that mongoose will automatically defragment incoming messages,
  3685. * so this flag is used only on outbound messages.
  3686. */
  3687. #define WEBSOCKET_DONT_FIN 0x100
  3688. #endif /* MG_ENABLE_HTTP_WEBSOCKET */
  3689. /*
  3690. * Decodes a URL-encoded string.
  3691. *
  3692. * Source string is specified by (`src`, `src_len`), and destination is
  3693. * (`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then
  3694. * `+` character is decoded as a blank space character. This function
  3695. * guarantees to NUL-terminate the destination. If destination is too small,
  3696. * then the source string is partially decoded and `-1` is returned. Otherwise,
  3697. * a length of the decoded string is returned, not counting final NUL.
  3698. */
  3699. int mg_url_decode(const char *src, int src_len, char *dst, int dst_len,
  3700. int is_form_url_encoded);
  3701. #ifdef __cplusplus
  3702. }
  3703. #endif /* __cplusplus */
  3704. #endif /* MG_ENABLE_HTTP */
  3705. #endif /* CS_MONGOOSE_SRC_HTTP_H_ */
  3706. #ifdef MG_MODULE_LINES
  3707. #line 1 "mongoose/src/http_server.h"
  3708. #endif
  3709. /*
  3710. * === Server API reference
  3711. */
  3712. #ifndef CS_MONGOOSE_SRC_HTTP_SERVER_H_
  3713. #define CS_MONGOOSE_SRC_HTTP_SERVER_H_
  3714. #if MG_ENABLE_HTTP
  3715. #ifdef __cplusplus
  3716. extern "C" {
  3717. #endif /* __cplusplus */
  3718. /*
  3719. * Parses a HTTP message.
  3720. *
  3721. * `is_req` should be set to 1 if parsing a request, 0 if reply.
  3722. *
  3723. * Returns the number of bytes parsed. If HTTP message is
  3724. * incomplete `0` is returned. On parse error, a negative number is returned.
  3725. */
  3726. int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req);
  3727. /*
  3728. * Searches and returns the header `name` in parsed HTTP message `hm`.
  3729. * If header is not found, NULL is returned. Example:
  3730. *
  3731. * struct mg_str *host_hdr = mg_get_http_header(hm, "Host");
  3732. */
  3733. struct mg_str *mg_get_http_header(struct http_message *hm, const char *name);
  3734. /*
  3735. * Parses the HTTP header `hdr`. Finds variable `var_name` and stores its value
  3736. * in the buffer `buf`, `buf_size`. Returns 0 if variable not found, non-zero
  3737. * otherwise.
  3738. *
  3739. * This function is supposed to parse cookies, authentication headers, etc.
  3740. * Example (error handling omitted):
  3741. *
  3742. * char user[20];
  3743. * struct mg_str *hdr = mg_get_http_header(hm, "Authorization");
  3744. * mg_http_parse_header(hdr, "username", user, sizeof(user));
  3745. *
  3746. * Returns the length of the variable's value. If buffer is not large enough,
  3747. * or variable not found, 0 is returned.
  3748. */
  3749. int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
  3750. size_t buf_size);
  3751. /*
  3752. * Gets and parses the Authorization: Basic header
  3753. * Returns -1 if no Authorization header is found, or if
  3754. * mg_parse_http_basic_auth
  3755. * fails parsing the resulting header.
  3756. */
  3757. int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len,
  3758. char *pass, size_t pass_len);
  3759. /*
  3760. * Parses the Authorization: Basic header
  3761. * Returns -1 iif the authorization type is not "Basic" or any other error such
  3762. * as incorrectly encoded base64 user password pair.
  3763. */
  3764. int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len,
  3765. char *pass, size_t pass_len);
  3766. /*
  3767. * Parses the buffer `buf`, `buf_len` that contains multipart form data chunks.
  3768. * Stores the chunk name in a `var_name`, `var_name_len` buffer.
  3769. * If a chunk is an uploaded file, then `file_name`, `file_name_len` is
  3770. * filled with an uploaded file name. `chunk`, `chunk_len`
  3771. * points to the chunk data.
  3772. *
  3773. * Return: number of bytes to skip to the next chunk or 0 if there are
  3774. * no more chunks.
  3775. *
  3776. * Usage example:
  3777. *
  3778. * ```c
  3779. * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  3780. * switch(ev) {
  3781. * case MG_EV_HTTP_REQUEST: {
  3782. * struct http_message *hm = (struct http_message *) ev_data;
  3783. * char var_name[100], file_name[100];
  3784. * const char *chunk;
  3785. * size_t chunk_len, n1, n2;
  3786. *
  3787. * n1 = n2 = 0;
  3788. * while ((n2 = mg_parse_multipart(hm->body.p + n1,
  3789. * hm->body.len - n1,
  3790. * var_name, sizeof(var_name),
  3791. * file_name, sizeof(file_name),
  3792. * &chunk, &chunk_len)) > 0) {
  3793. * printf("var: %s, file_name: %s, size: %d, chunk: [%.*s]\n",
  3794. * var_name, file_name, (int) chunk_len,
  3795. * (int) chunk_len, chunk);
  3796. * n1 += n2;
  3797. * }
  3798. * }
  3799. * break;
  3800. * ```
  3801. */
  3802. size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
  3803. size_t var_name_len, char *file_name,
  3804. size_t file_name_len, const char **chunk,
  3805. size_t *chunk_len);
  3806. /*
  3807. * Fetches a HTTP form variable.
  3808. *
  3809. * Fetches a variable `name` from a `buf` into a buffer specified by `dst`,
  3810. * `dst_len`. The destination is always zero-terminated. Returns the length of
  3811. * a fetched variable. If not found, 0 is returned. `buf` must be valid
  3812. * url-encoded buffer. If destination is too small, `-1` is returned.
  3813. */
  3814. int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst,
  3815. size_t dst_len);
  3816. #if MG_ENABLE_FILESYSTEM
  3817. /*
  3818. * This structure defines how `mg_serve_http()` works.
  3819. * Best practice is to set only required settings, and leave the rest as NULL.
  3820. */
  3821. struct mg_serve_http_opts {
  3822. /* Path to web root directory */
  3823. const char *document_root;
  3824. /* List of index files. Default is "" */
  3825. const char *index_files;
  3826. /*
  3827. * Leave as NULL to disable authentication.
  3828. * To enable directory protection with authentication, set this to ".htpasswd"
  3829. * Then, creating ".htpasswd" file in any directory automatically protects
  3830. * it with digest authentication.
  3831. * Use `mongoose` web server binary, or `htdigest` Apache utility to
  3832. * create/manipulate passwords file.
  3833. * Make sure `auth_domain` is set to a valid domain name.
  3834. */
  3835. const char *per_directory_auth_file;
  3836. /* Authorization domain (domain name of this web server) */
  3837. const char *auth_domain;
  3838. /*
  3839. * Leave as NULL to disable authentication.
  3840. * Normally, only selected directories in the document root are protected.
  3841. * If absolutely every access to the web server needs to be authenticated,
  3842. * regardless of the URI, set this option to the path to the passwords file.
  3843. * Format of that file is the same as ".htpasswd" file. Make sure that file
  3844. * is located outside document root to prevent people fetching it.
  3845. */
  3846. const char *global_auth_file;
  3847. /* Set to "no" to disable directory listing. Enabled by default. */
  3848. const char *enable_directory_listing;
  3849. /*
  3850. * SSI files pattern. If not set, "**.shtml$|**.shtm$" is used.
  3851. *
  3852. * All files that match ssi_pattern are treated as SSI.
  3853. *
  3854. * Server Side Includes (SSI) is a simple interpreted server-side scripting
  3855. * language which is most commonly used to include the contents of a file
  3856. * into a web page. It can be useful when it is desirable to include a common
  3857. * piece of code throughout a website, for example, headers and footers.
  3858. *
  3859. * In order for a webpage to recognize an SSI-enabled HTML file, the
  3860. * filename should end with a special extension, by default the extension
  3861. * should be either .shtml or .shtm
  3862. *
  3863. * Unknown SSI directives are silently ignored by Mongoose. Currently,
  3864. * the following SSI directives are supported:
  3865. * &lt;!--#include FILE_TO_INCLUDE --&gt;
  3866. * &lt;!--#exec "COMMAND_TO_EXECUTE" --&gt;
  3867. * &lt;!--#call COMMAND --&gt;
  3868. *
  3869. * Note that &lt;!--#include ...> directive supports three path
  3870. *specifications:
  3871. *
  3872. * &lt;!--#include virtual="path" --&gt; Path is relative to web server root
  3873. * &lt;!--#include abspath="path" --&gt; Path is absolute or relative to the
  3874. * web server working dir
  3875. * &lt;!--#include file="path" --&gt;, Path is relative to current document
  3876. * &lt;!--#include "path" --&gt;
  3877. *
  3878. * The include directive may be used to include the contents of a file or
  3879. * the result of running a CGI script.
  3880. *
  3881. * The exec directive is used to execute
  3882. * a command on a server, and show command's output. Example:
  3883. *
  3884. * &lt;!--#exec "ls -l" --&gt;
  3885. *
  3886. * The call directive is a way to invoke a C handler from the HTML page.
  3887. * On each occurence of &lt;!--#call COMMAND OPTIONAL_PARAMS> directive,
  3888. * Mongoose calls a registered event handler with MG_EV_SSI_CALL event,
  3889. * and event parameter will point to the COMMAND OPTIONAL_PARAMS string.
  3890. * An event handler can output any text, for example by calling
  3891. * `mg_printf()`. This is a flexible way of generating a web page on
  3892. * server side by calling a C event handler. Example:
  3893. *
  3894. * &lt;!--#call foo --&gt; ... &lt;!--#call bar --&gt;
  3895. *
  3896. * In the event handler:
  3897. * case MG_EV_SSI_CALL: {
  3898. * const char *param = (const char *) ev_data;
  3899. * if (strcmp(param, "foo") == 0) {
  3900. * mg_printf(c, "hello from foo");
  3901. * } else if (strcmp(param, "bar") == 0) {
  3902. * mg_printf(c, "hello from bar");
  3903. * }
  3904. * break;
  3905. * }
  3906. */
  3907. const char *ssi_pattern;
  3908. /* IP ACL. By default, NULL, meaning all IPs are allowed to connect */
  3909. const char *ip_acl;
  3910. #if MG_ENABLE_HTTP_URL_REWRITES
  3911. /* URL rewrites.
  3912. *
  3913. * Comma-separated list of `uri_pattern=url_file_or_directory_path` rewrites.
  3914. * When HTTP request is received, Mongoose constructs a file name from the
  3915. * requested URI by combining `document_root` and the URI. However, if the
  3916. * rewrite option is used and `uri_pattern` matches requested URI, then
  3917. * `document_root` is ignored. Instead, `url_file_or_directory_path` is used,
  3918. * which should be a full path name or a path relative to the web server's
  3919. * current working directory. It can also be an URI (http:// or https://)
  3920. * in which case mongoose will behave as a reverse proxy for that destination.
  3921. *
  3922. * Note that `uri_pattern`, as all Mongoose patterns, is a prefix pattern.
  3923. *
  3924. * If uri_pattern starts with `@` symbol, then Mongoose compares it with the
  3925. * HOST header of the request. If they are equal, Mongoose sets document root
  3926. * to `file_or_directory_path`, implementing virtual hosts support.
  3927. * Example: `@foo.com=/document/root/for/foo.com`
  3928. *
  3929. * If `uri_pattern` starts with `%` symbol, then Mongoose compares it with
  3930. * the listening port. If they match, then Mongoose issues a 301 redirect.
  3931. * For example, to redirect all HTTP requests to the
  3932. * HTTPS port, do `%80=https://my.site.com`. Note that the request URI is
  3933. * automatically appended to the redirect location.
  3934. */
  3935. const char *url_rewrites;
  3936. #endif
  3937. /* DAV document root. If NULL, DAV requests are going to fail. */
  3938. const char *dav_document_root;
  3939. /*
  3940. * DAV passwords file. If NULL, DAV requests are going to fail.
  3941. * If passwords file is set to "-", then DAV auth is disabled.
  3942. */
  3943. const char *dav_auth_file;
  3944. /* Glob pattern for the files to hide. */
  3945. const char *hidden_file_pattern;
  3946. /* Set to non-NULL to enable CGI, e.g. **.cgi$|**.php$" */
  3947. const char *cgi_file_pattern;
  3948. /* If not NULL, ignore CGI script hashbang and use this interpreter */
  3949. const char *cgi_interpreter;
  3950. /*
  3951. * Comma-separated list of Content-Type overrides for path suffixes, e.g.
  3952. * ".txt=text/plain; charset=utf-8,.c=text/plain"
  3953. */
  3954. const char *custom_mime_types;
  3955. /*
  3956. * Extra HTTP headers to add to each server response.
  3957. * Example: to enable CORS, set this to "Access-Control-Allow-Origin: *".
  3958. */
  3959. const char *extra_headers;
  3960. };
  3961. /*
  3962. * Serves given HTTP request according to the `options`.
  3963. *
  3964. * Example code snippet:
  3965. *
  3966. * ```c
  3967. * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  3968. * struct http_message *hm = (struct http_message *) ev_data;
  3969. * struct mg_serve_http_opts opts = { .document_root = "/var/www" }; // C99
  3970. *
  3971. * switch (ev) {
  3972. * case MG_EV_HTTP_REQUEST:
  3973. * mg_serve_http(nc, hm, opts);
  3974. * break;
  3975. * default:
  3976. * break;
  3977. * }
  3978. * }
  3979. * ```
  3980. */
  3981. void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
  3982. struct mg_serve_http_opts opts);
  3983. /*
  3984. * Serves a specific file with a given MIME type and optional extra headers.
  3985. *
  3986. * Example code snippet:
  3987. *
  3988. * ```c
  3989. * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  3990. * switch (ev) {
  3991. * case MG_EV_HTTP_REQUEST: {
  3992. * struct http_message *hm = (struct http_message *) ev_data;
  3993. * mg_http_serve_file(nc, hm, "file.txt",
  3994. * mg_mk_str("text/plain"), mg_mk_str(""));
  3995. * break;
  3996. * }
  3997. * ...
  3998. * }
  3999. * }
  4000. * ```
  4001. */
  4002. void mg_http_serve_file(struct mg_connection *nc, struct http_message *hm,
  4003. const char *path, const struct mg_str mime_type,
  4004. const struct mg_str extra_headers);
  4005. #if MG_ENABLE_HTTP_STREAMING_MULTIPART
  4006. /* Callback prototype for `mg_file_upload_handler()`. */
  4007. typedef struct mg_str (*mg_fu_fname_fn)(struct mg_connection *nc,
  4008. struct mg_str fname);
  4009. /*
  4010. * File upload handler.
  4011. * This handler can be used to implement file uploads with minimum code.
  4012. * This handler will process MG_EV_HTTP_PART_* events and store file data into
  4013. * a local file.
  4014. * `local_name_fn` will be invoked with whatever name was provided by the client
  4015. * and will expect the name of the local file to open. A return value of NULL
  4016. * will abort file upload (client will get a "403 Forbidden" response). If
  4017. * non-null, the returned string must be heap-allocated and will be freed by
  4018. * the caller.
  4019. * Exception: it is ok to return the same string verbatim.
  4020. *
  4021. * Example:
  4022. *
  4023. * ```c
  4024. * struct mg_str upload_fname(struct mg_connection *nc, struct mg_str fname) {
  4025. * // Just return the same filename. Do not actually do this except in test!
  4026. * // fname is user-controlled and needs to be sanitized.
  4027. * return fname;
  4028. * }
  4029. * void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  4030. * switch (ev) {
  4031. * ...
  4032. * case MG_EV_HTTP_PART_BEGIN:
  4033. * case MG_EV_HTTP_PART_DATA:
  4034. * case MG_EV_HTTP_PART_END:
  4035. * mg_file_upload_handler(nc, ev, ev_data, upload_fname);
  4036. * break;
  4037. * }
  4038. * }
  4039. * ```
  4040. */
  4041. void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
  4042. mg_fu_fname_fn local_name_fn);
  4043. #endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
  4044. #endif /* MG_ENABLE_FILESYSTEM */
  4045. /*
  4046. * Registers a callback for a specified http endpoint
  4047. * Note: if callback is registered it is called instead of the
  4048. * callback provided in mg_bind
  4049. *
  4050. * Example code snippet:
  4051. *
  4052. * ```c
  4053. * static void handle_hello1(struct mg_connection *nc, int ev, void *ev_data) {
  4054. * (void) ev; (void) ev_data;
  4055. * mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]");
  4056. * nc->flags |= MG_F_SEND_AND_CLOSE;
  4057. * }
  4058. *
  4059. * static void handle_hello1(struct mg_connection *nc, int ev, void *ev_data) {
  4060. * (void) ev; (void) ev_data;
  4061. * mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello2]");
  4062. * nc->flags |= MG_F_SEND_AND_CLOSE;
  4063. * }
  4064. *
  4065. * void init() {
  4066. * nc = mg_bind(&mgr, local_addr, cb1);
  4067. * mg_register_http_endpoint(nc, "/hello1", handle_hello1);
  4068. * mg_register_http_endpoint(nc, "/hello1/hello2", handle_hello2);
  4069. * }
  4070. * ```
  4071. */
  4072. void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path,
  4073. mg_event_handler_t handler);
  4074. /*
  4075. * Authenticates a HTTP request against an opened password file.
  4076. * Returns 1 if authenticated, 0 otherwise.
  4077. */
  4078. int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain,
  4079. FILE *fp);
  4080. /*
  4081. * Sends buffer `buf` of size `len` to the client using chunked HTTP encoding.
  4082. * This function sends the buffer size as hex number + newline first, then
  4083. * the buffer itself, then the newline. For example,
  4084. * `mg_send_http_chunk(nc, "foo", 3)` whill append the `3\r\nfoo\r\n` string
  4085. * to the `nc->send_mbuf` output IO buffer.
  4086. *
  4087. * NOTE: The HTTP header "Transfer-Encoding: chunked" should be sent prior to
  4088. * using this function.
  4089. *
  4090. * NOTE: do not forget to send an empty chunk at the end of the response,
  4091. * to tell the client that everything was sent. Example:
  4092. *
  4093. * ```
  4094. * mg_printf_http_chunk(nc, "%s", "my response!");
  4095. * mg_send_http_chunk(nc, "", 0); // Tell the client we're finished
  4096. * ```
  4097. */
  4098. void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len);
  4099. /*
  4100. * Sends a printf-formatted HTTP chunk.
  4101. * Functionality is similar to `mg_send_http_chunk()`.
  4102. */
  4103. void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...);
  4104. /*
  4105. * Sends the response status line.
  4106. * If `extra_headers` is not NULL, then `extra_headers` are also sent
  4107. * after the reponse line. `extra_headers` must NOT end end with new line.
  4108. * Example:
  4109. *
  4110. * mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *");
  4111. *
  4112. * Will result in:
  4113. *
  4114. * HTTP/1.1 200 OK\r\n
  4115. * Access-Control-Allow-Origin: *\r\n
  4116. */
  4117. void mg_send_response_line(struct mg_connection *nc, int status_code,
  4118. const char *extra_headers);
  4119. /*
  4120. * Sends an error response. If reason is NULL, the message will be inferred
  4121. * from the error code (if supported).
  4122. */
  4123. void mg_http_send_error(struct mg_connection *nc, int code, const char *reason);
  4124. /*
  4125. * Sends a redirect response.
  4126. * `status_code` should be either 301 or 302 and `location` point to the
  4127. * new location.
  4128. * If `extra_headers` is not empty, then `extra_headers` are also sent
  4129. * after the reponse line. `extra_headers` must NOT end end with new line.
  4130. *
  4131. * Example:
  4132. *
  4133. * mg_http_send_redirect(nc, 302, mg_mk_str("/login"), mg_mk_str(NULL));
  4134. */
  4135. void mg_http_send_redirect(struct mg_connection *nc, int status_code,
  4136. const struct mg_str location,
  4137. const struct mg_str extra_headers);
  4138. /*
  4139. * Sends the response line and headers.
  4140. * This function sends the response line with the `status_code`, and
  4141. * automatically
  4142. * sends one header: either "Content-Length" or "Transfer-Encoding".
  4143. * If `content_length` is negative, then "Transfer-Encoding: chunked" header
  4144. * is sent, otherwise, "Content-Length" header is sent.
  4145. *
  4146. * NOTE: If `Transfer-Encoding` is `chunked`, then message body must be sent
  4147. * using `mg_send_http_chunk()` or `mg_printf_http_chunk()` functions.
  4148. * Otherwise, `mg_send()` or `mg_printf()` must be used.
  4149. * Extra headers could be set through `extra_headers`. Note `extra_headers`
  4150. * must NOT be terminated by a new line.
  4151. */
  4152. void mg_send_head(struct mg_connection *n, int status_code,
  4153. int64_t content_length, const char *extra_headers);
  4154. /*
  4155. * Sends a printf-formatted HTTP chunk, escaping HTML tags.
  4156. */
  4157. void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...);
  4158. #if MG_ENABLE_HTTP_URL_REWRITES
  4159. /*
  4160. * Proxies a given request to a given upstream http server. The path prefix
  4161. * in `mount` will be stripped of the path requested to the upstream server,
  4162. * e.g. if mount is /api and upstream is http://localhost:8001/foo
  4163. * then an incoming request to /api/bar will cause a request to
  4164. * http://localhost:8001/foo/bar
  4165. *
  4166. * EXPERIMENTAL API. Please use http_serve_http + url_rewrites if a static
  4167. * mapping is good enough.
  4168. */
  4169. void mg_http_reverse_proxy(struct mg_connection *nc,
  4170. const struct http_message *hm, struct mg_str mount,
  4171. struct mg_str upstream);
  4172. #endif
  4173. #ifdef __cplusplus
  4174. }
  4175. #endif /* __cplusplus */
  4176. #endif /* MG_ENABLE_HTTP */
  4177. #endif /* CS_MONGOOSE_SRC_HTTP_SERVER_H_ */
  4178. #ifdef MG_MODULE_LINES
  4179. #line 1 "mongoose/src/http_client.h"
  4180. #endif
  4181. /*
  4182. * === Client API reference
  4183. */
  4184. #ifndef CS_MONGOOSE_SRC_HTTP_CLIENT_H_
  4185. #define CS_MONGOOSE_SRC_HTTP_CLIENT_H_
  4186. #ifdef __cplusplus
  4187. extern "C" {
  4188. #endif /* __cplusplus */
  4189. /*
  4190. * Helper function that creates an outbound HTTP connection.
  4191. *
  4192. * `url` is the URL to fetch. It must be properly URL-encoded, e.g. have
  4193. * no spaces, etc. By default, `mg_connect_http()` sends the Connection and
  4194. * Host headers. `extra_headers` is an extra HTTP header to send, e.g.
  4195. * `"User-Agent: my-app\r\n"`.
  4196. * If `post_data` is NULL, then a GET request is created. Otherwise, a POST
  4197. * request is created with the specified POST data. Note that if the data being
  4198. * posted is a form submission, the `Content-Type` header should be set
  4199. * accordingly (see example below).
  4200. *
  4201. * Examples:
  4202. *
  4203. * ```c
  4204. * nc1 = mg_connect_http(mgr, ev_handler_1, "http://www.google.com", NULL,
  4205. * NULL);
  4206. * nc2 = mg_connect_http(mgr, ev_handler_1, "https://github.com", NULL, NULL);
  4207. * nc3 = mg_connect_http(
  4208. * mgr, ev_handler_1, "my_server:8000/form_submit/",
  4209. * "Content-Type: application/x-www-form-urlencoded\r\n",
  4210. * "var_1=value_1&var_2=value_2");
  4211. * ```
  4212. */
  4213. struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
  4214. mg_event_handler_t event_handler,
  4215. const char *url,
  4216. const char *extra_headers,
  4217. const char *post_data);
  4218. /*
  4219. * Helper function that creates an outbound HTTP connection.
  4220. *
  4221. * Mostly identical to mg_connect_http, but allows you to provide extra
  4222. *parameters
  4223. * (for example, SSL parameters)
  4224. */
  4225. struct mg_connection *mg_connect_http_opt(struct mg_mgr *mgr,
  4226. mg_event_handler_t ev_handler,
  4227. struct mg_connect_opts opts,
  4228. const char *url,
  4229. const char *extra_headers,
  4230. const char *post_data);
  4231. /* Creates digest authentication header for a client request. */
  4232. int mg_http_create_digest_auth_header(char *buf, size_t buf_len,
  4233. const char *method, const char *uri,
  4234. const char *auth_domain, const char *user,
  4235. const char *passwd);
  4236. #ifdef __cplusplus
  4237. }
  4238. #endif /* __cplusplus */
  4239. #endif /* CS_MONGOOSE_SRC_HTTP_CLIENT_H_ */
  4240. #ifdef MG_MODULE_LINES
  4241. #line 1 "mongoose/src/mqtt.h"
  4242. #endif
  4243. /*
  4244. * Copyright (c) 2014 Cesanta Software Limited
  4245. * All rights reserved
  4246. * This software is dual-licensed: you can redistribute it and/or modify
  4247. * it under the terms of the GNU General Public License version 2 as
  4248. * published by the Free Software Foundation. For the terms of this
  4249. * license, see <http://www.gnu.org/licenses/>.
  4250. *
  4251. * You are free to use this software under the terms of the GNU General
  4252. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  4253. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  4254. * See the GNU General Public License for more details.
  4255. *
  4256. * Alternatively, you can license this software under a commercial
  4257. * license, as set out in <https://www.cesanta.com/license>.
  4258. */
  4259. /*
  4260. * === MQTT API reference
  4261. */
  4262. #ifndef CS_MONGOOSE_SRC_MQTT_H_
  4263. #define CS_MONGOOSE_SRC_MQTT_H_
  4264. /* Amalgamated: #include "mongoose/src/net.h" */
  4265. struct mg_mqtt_message {
  4266. int cmd;
  4267. int qos;
  4268. struct mg_str topic;
  4269. struct mg_str payload;
  4270. uint8_t connack_ret_code; /* connack */
  4271. uint16_t message_id; /* puback */
  4272. /* connect */
  4273. uint8_t protocol_version;
  4274. uint8_t connect_flags;
  4275. uint16_t keep_alive_timer;
  4276. struct mg_str protocol_name;
  4277. struct mg_str client_id;
  4278. struct mg_str will_topic;
  4279. struct mg_str will_message;
  4280. struct mg_str user_name;
  4281. struct mg_str password;
  4282. };
  4283. struct mg_mqtt_topic_expression {
  4284. const char *topic;
  4285. uint8_t qos;
  4286. };
  4287. struct mg_send_mqtt_handshake_opts {
  4288. unsigned char flags; /* connection flags */
  4289. uint16_t keep_alive;
  4290. const char *will_topic;
  4291. const char *will_message;
  4292. const char *user_name;
  4293. const char *password;
  4294. };
  4295. /* mg_mqtt_proto_data should be in header to allow external access to it */
  4296. struct mg_mqtt_proto_data {
  4297. uint16_t keep_alive;
  4298. };
  4299. /* Message types */
  4300. #define MG_MQTT_CMD_CONNECT 1
  4301. #define MG_MQTT_CMD_CONNACK 2
  4302. #define MG_MQTT_CMD_PUBLISH 3
  4303. #define MG_MQTT_CMD_PUBACK 4
  4304. #define MG_MQTT_CMD_PUBREC 5
  4305. #define MG_MQTT_CMD_PUBREL 6
  4306. #define MG_MQTT_CMD_PUBCOMP 7
  4307. #define MG_MQTT_CMD_SUBSCRIBE 8
  4308. #define MG_MQTT_CMD_SUBACK 9
  4309. #define MG_MQTT_CMD_UNSUBSCRIBE 10
  4310. #define MG_MQTT_CMD_UNSUBACK 11
  4311. #define MG_MQTT_CMD_PINGREQ 12
  4312. #define MG_MQTT_CMD_PINGRESP 13
  4313. #define MG_MQTT_CMD_DISCONNECT 14
  4314. /* MQTT event types */
  4315. #define MG_MQTT_EVENT_BASE 200
  4316. #define MG_EV_MQTT_CONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNECT)
  4317. #define MG_EV_MQTT_CONNACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNACK)
  4318. #define MG_EV_MQTT_PUBLISH (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBLISH)
  4319. #define MG_EV_MQTT_PUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBACK)
  4320. #define MG_EV_MQTT_PUBREC (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREC)
  4321. #define MG_EV_MQTT_PUBREL (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREL)
  4322. #define MG_EV_MQTT_PUBCOMP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBCOMP)
  4323. #define MG_EV_MQTT_SUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBSCRIBE)
  4324. #define MG_EV_MQTT_SUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBACK)
  4325. #define MG_EV_MQTT_UNSUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBSCRIBE)
  4326. #define MG_EV_MQTT_UNSUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBACK)
  4327. #define MG_EV_MQTT_PINGREQ (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGREQ)
  4328. #define MG_EV_MQTT_PINGRESP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGRESP)
  4329. #define MG_EV_MQTT_DISCONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_DISCONNECT)
  4330. /* Message flags */
  4331. #define MG_MQTT_RETAIN 0x1
  4332. #define MG_MQTT_DUP 0x4
  4333. #define MG_MQTT_QOS(qos) ((qos) << 1)
  4334. #define MG_MQTT_GET_QOS(flags) (((flags) &0x6) >> 1)
  4335. #define MG_MQTT_SET_QOS(flags, qos) (flags) = ((flags) & ~0x6) | ((qos) << 1)
  4336. /* Connection flags */
  4337. #define MG_MQTT_CLEAN_SESSION 0x02
  4338. #define MG_MQTT_HAS_WILL 0x04
  4339. #define MG_MQTT_WILL_RETAIN 0x20
  4340. #define MG_MQTT_HAS_PASSWORD 0x40
  4341. #define MG_MQTT_HAS_USER_NAME 0x80
  4342. #define MG_MQTT_GET_WILL_QOS(flags) (((flags) &0x18) >> 3)
  4343. #define MG_MQTT_SET_WILL_QOS(flags, qos) \
  4344. (flags) = ((flags) & ~0x18) | ((qos) << 3)
  4345. /* CONNACK return codes */
  4346. #define MG_EV_MQTT_CONNACK_ACCEPTED 0
  4347. #define MG_EV_MQTT_CONNACK_UNACCEPTABLE_VERSION 1
  4348. #define MG_EV_MQTT_CONNACK_IDENTIFIER_REJECTED 2
  4349. #define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE 3
  4350. #define MG_EV_MQTT_CONNACK_BAD_AUTH 4
  4351. #define MG_EV_MQTT_CONNACK_NOT_AUTHORIZED 5
  4352. #ifdef __cplusplus
  4353. extern "C" {
  4354. #endif /* __cplusplus */
  4355. /*
  4356. * Attaches a built-in MQTT event handler to the given connection.
  4357. *
  4358. * The user-defined event handler will receive following extra events:
  4359. *
  4360. * - MG_EV_MQTT_CONNACK
  4361. * - MG_EV_MQTT_PUBLISH
  4362. * - MG_EV_MQTT_PUBACK
  4363. * - MG_EV_MQTT_PUBREC
  4364. * - MG_EV_MQTT_PUBREL
  4365. * - MG_EV_MQTT_PUBCOMP
  4366. * - MG_EV_MQTT_SUBACK
  4367. */
  4368. void mg_set_protocol_mqtt(struct mg_connection *nc);
  4369. /* Sends an MQTT handshake. */
  4370. void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id);
  4371. /* Sends an MQTT handshake with optional parameters. */
  4372. void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
  4373. struct mg_send_mqtt_handshake_opts);
  4374. /* Publishes a message to a given topic. */
  4375. void mg_mqtt_publish(struct mg_connection *nc, const char *topic,
  4376. uint16_t message_id, int flags, const void *data,
  4377. size_t len);
  4378. /* Subscribes to a bunch of topics. */
  4379. void mg_mqtt_subscribe(struct mg_connection *nc,
  4380. const struct mg_mqtt_topic_expression *topics,
  4381. size_t topics_len, uint16_t message_id);
  4382. /* Unsubscribes from a bunch of topics. */
  4383. void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics,
  4384. size_t topics_len, uint16_t message_id);
  4385. /* Sends a DISCONNECT command. */
  4386. void mg_mqtt_disconnect(struct mg_connection *nc);
  4387. /* Sends a CONNACK command with a given `return_code`. */
  4388. void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code);
  4389. /* Sends a PUBACK command with a given `message_id`. */
  4390. void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id);
  4391. /* Sends a PUBREC command with a given `message_id`. */
  4392. void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id);
  4393. /* Sends a PUBREL command with a given `message_id`. */
  4394. void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id);
  4395. /* Sends a PUBCOMP command with a given `message_id`. */
  4396. void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id);
  4397. /*
  4398. * Sends a SUBACK command with a given `message_id`
  4399. * and a sequence of granted QoSs.
  4400. */
  4401. void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len,
  4402. uint16_t message_id);
  4403. /* Sends a UNSUBACK command with a given `message_id`. */
  4404. void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id);
  4405. /* Sends a PINGREQ command. */
  4406. void mg_mqtt_ping(struct mg_connection *nc);
  4407. /* Sends a PINGRESP command. */
  4408. void mg_mqtt_pong(struct mg_connection *nc);
  4409. /*
  4410. * Extracts the next topic expression from a SUBSCRIBE command payload.
  4411. *
  4412. * The topic expression name will point to a string in the payload buffer.
  4413. * Returns the pos of the next topic expression or -1 when the list
  4414. * of topics is exhausted.
  4415. */
  4416. int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg,
  4417. struct mg_str *topic, uint8_t *qos, int pos);
  4418. #ifdef __cplusplus
  4419. }
  4420. #endif /* __cplusplus */
  4421. #endif /* CS_MONGOOSE_SRC_MQTT_H_ */
  4422. #ifdef MG_MODULE_LINES
  4423. #line 1 "mongoose/src/mqtt_server.h"
  4424. #endif
  4425. /*
  4426. * Copyright (c) 2014 Cesanta Software Limited
  4427. * All rights reserved
  4428. * This software is dual-licensed: you can redistribute it and/or modify
  4429. * it under the terms of the GNU General Public License version 2 as
  4430. * published by the Free Software Foundation. For the terms of this
  4431. * license, see <http://www.gnu.org/licenses/>.
  4432. *
  4433. * You are free to use this software under the terms of the GNU General
  4434. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  4435. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  4436. * See the GNU General Public License for more details.
  4437. *
  4438. * Alternatively, you can license this software under a commercial
  4439. * license, as set out in <https://www.cesanta.com/license>.
  4440. */
  4441. /*
  4442. * === MQTT Server API reference
  4443. */
  4444. #ifndef CS_MONGOOSE_SRC_MQTT_BROKER_H_
  4445. #define CS_MONGOOSE_SRC_MQTT_BROKER_H_
  4446. #if MG_ENABLE_MQTT_BROKER
  4447. /* Amalgamated: #include "common/queue.h" */
  4448. /* Amalgamated: #include "mongoose/src/mqtt.h" */
  4449. #ifdef __cplusplus
  4450. extern "C" {
  4451. #endif /* __cplusplus */
  4452. #define MG_MQTT_MAX_SESSION_SUBSCRIPTIONS 512;
  4453. struct mg_mqtt_broker;
  4454. /* MQTT session (Broker side). */
  4455. struct mg_mqtt_session {
  4456. struct mg_mqtt_broker *brk; /* Broker */
  4457. LIST_ENTRY(mg_mqtt_session) link; /* mg_mqtt_broker::sessions linkage */
  4458. struct mg_connection *nc; /* Connection with the client */
  4459. size_t num_subscriptions; /* Size of `subscriptions` array */
  4460. void *user_data; /* User data */
  4461. struct mg_mqtt_topic_expression *subscriptions;
  4462. };
  4463. /* MQTT broker. */
  4464. struct mg_mqtt_broker {
  4465. LIST_HEAD(_mg_sesshead, mg_mqtt_session) sessions; /* Session list */
  4466. void *user_data; /* User data */
  4467. };
  4468. /* Initialises a MQTT broker. */
  4469. void mg_mqtt_broker_init(struct mg_mqtt_broker *brk, void *user_data);
  4470. /*
  4471. * Processes a MQTT broker message.
  4472. *
  4473. * The listening connection expects a pointer to an initialised
  4474. * `mg_mqtt_broker` structure in the `user_data` field.
  4475. *
  4476. * Basic usage:
  4477. *
  4478. * ```c
  4479. * mg_mqtt_broker_init(&brk, NULL);
  4480. *
  4481. * if ((nc = mg_bind(&mgr, address, mg_mqtt_broker)) == NULL) {
  4482. * // fail;
  4483. * }
  4484. * nc->user_data = &brk;
  4485. * ```
  4486. *
  4487. * New incoming connections will receive a `mg_mqtt_session` structure
  4488. * in the connection `user_data`. The original `user_data` will be stored
  4489. * in the `user_data` field of the session structure. This allows the user
  4490. * handler to store user data before `mg_mqtt_broker` creates the session.
  4491. *
  4492. * Since only the MG_EV_ACCEPT message is processed by the listening socket,
  4493. * for most events the `user_data` will thus point to a `mg_mqtt_session`.
  4494. */
  4495. void mg_mqtt_broker(struct mg_connection *brk, int ev, void *data);
  4496. /*
  4497. * Iterates over all MQTT session connections. Example:
  4498. *
  4499. * ```c
  4500. * struct mg_mqtt_session *s;
  4501. * for (s = mg_mqtt_next(brk, NULL); s != NULL; s = mg_mqtt_next(brk, s)) {
  4502. * // Do something
  4503. * }
  4504. * ```
  4505. */
  4506. struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk,
  4507. struct mg_mqtt_session *s);
  4508. #ifdef __cplusplus
  4509. }
  4510. #endif /* __cplusplus */
  4511. #endif /* MG_ENABLE_MQTT_BROKER */
  4512. #endif /* CS_MONGOOSE_SRC_MQTT_BROKER_H_ */
  4513. #ifdef MG_MODULE_LINES
  4514. #line 1 "mongoose/src/dns.h"
  4515. #endif
  4516. /*
  4517. * Copyright (c) 2014 Cesanta Software Limited
  4518. * All rights reserved
  4519. */
  4520. /*
  4521. * === DNS API reference
  4522. */
  4523. #ifndef CS_MONGOOSE_SRC_DNS_H_
  4524. #define CS_MONGOOSE_SRC_DNS_H_
  4525. /* Amalgamated: #include "mongoose/src/net.h" */
  4526. #ifdef __cplusplus
  4527. extern "C" {
  4528. #endif /* __cplusplus */
  4529. #define MG_DNS_A_RECORD 0x01 /* Lookup IP address */
  4530. #define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */
  4531. #define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */
  4532. #define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */
  4533. #define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */
  4534. #define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */
  4535. #define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */
  4536. #define MG_MAX_DNS_QUESTIONS 32
  4537. #define MG_MAX_DNS_ANSWERS 32
  4538. #define MG_DNS_MESSAGE 100 /* High-level DNS message event */
  4539. enum mg_dns_resource_record_kind {
  4540. MG_DNS_INVALID_RECORD = 0,
  4541. MG_DNS_QUESTION,
  4542. MG_DNS_ANSWER
  4543. };
  4544. /* DNS resource record. */
  4545. struct mg_dns_resource_record {
  4546. struct mg_str name; /* buffer with compressed name */
  4547. int rtype;
  4548. int rclass;
  4549. int ttl;
  4550. enum mg_dns_resource_record_kind kind;
  4551. struct mg_str rdata; /* protocol data (can be a compressed name) */
  4552. };
  4553. /* DNS message (request and response). */
  4554. struct mg_dns_message {
  4555. struct mg_str pkt; /* packet body */
  4556. uint16_t flags;
  4557. uint16_t transaction_id;
  4558. int num_questions;
  4559. int num_answers;
  4560. struct mg_dns_resource_record questions[MG_MAX_DNS_QUESTIONS];
  4561. struct mg_dns_resource_record answers[MG_MAX_DNS_ANSWERS];
  4562. };
  4563. struct mg_dns_resource_record *mg_dns_next_record(
  4564. struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev);
  4565. /*
  4566. * Parses the record data from a DNS resource record.
  4567. *
  4568. * - A: struct in_addr *ina
  4569. * - AAAA: struct in6_addr *ina
  4570. * - CNAME: char buffer
  4571. *
  4572. * Returns -1 on error.
  4573. *
  4574. * TODO(mkm): MX
  4575. */
  4576. int mg_dns_parse_record_data(struct mg_dns_message *msg,
  4577. struct mg_dns_resource_record *rr, void *data,
  4578. size_t data_len);
  4579. /*
  4580. * Sends a DNS query to the remote end.
  4581. */
  4582. void mg_send_dns_query(struct mg_connection *nc, const char *name,
  4583. int query_type);
  4584. /*
  4585. * Inserts a DNS header to an IO buffer.
  4586. *
  4587. * Returns the number of bytes inserted.
  4588. */
  4589. int mg_dns_insert_header(struct mbuf *io, size_t pos,
  4590. struct mg_dns_message *msg);
  4591. /*
  4592. * Appends already encoded questions from an existing message.
  4593. *
  4594. * This is useful when generating a DNS reply message which includes
  4595. * all question records.
  4596. *
  4597. * Returns the number of appended bytes.
  4598. */
  4599. int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg);
  4600. /*
  4601. * Encodes and appends a DNS resource record to an IO buffer.
  4602. *
  4603. * The record metadata is taken from the `rr` parameter, while the name and data
  4604. * are taken from the parameters, encoded in the appropriate format depending on
  4605. * record type and stored in the IO buffer. The encoded values might contain
  4606. * offsets within the IO buffer. It's thus important that the IO buffer doesn't
  4607. * get trimmed while a sequence of records are encoded while preparing a DNS
  4608. * reply.
  4609. *
  4610. * This function doesn't update the `name` and `rdata` pointers in the `rr`
  4611. * struct because they might be invalidated as soon as the IO buffer grows
  4612. * again.
  4613. *
  4614. * Returns the number of bytes appened or -1 in case of error.
  4615. */
  4616. int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
  4617. const char *name, size_t nlen, const void *rdata,
  4618. size_t rlen);
  4619. /*
  4620. * Encodes a DNS name.
  4621. */
  4622. int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
  4623. /* Low-level: parses a DNS response. */
  4624. int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);
  4625. /*
  4626. * Uncompresses a DNS compressed name.
  4627. *
  4628. * The containing DNS message is required because of the compressed encoding
  4629. * and reference suffixes present elsewhere in the packet.
  4630. *
  4631. * If the name is less than `dst_len` characters long, the remainder
  4632. * of `dst` is terminated with `\0` characters. Otherwise, `dst` is not
  4633. * terminated.
  4634. *
  4635. * If `dst_len` is 0 `dst` can be NULL.
  4636. * Returns the uncompressed name length.
  4637. */
  4638. size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
  4639. char *dst, int dst_len);
  4640. /*
  4641. * Attaches a built-in DNS event handler to the given listening connection.
  4642. *
  4643. * The DNS event handler parses the incoming UDP packets, treating them as DNS
  4644. * requests. If an incoming packet gets successfully parsed by the DNS event
  4645. * handler, a user event handler will receive an `MG_DNS_REQUEST` event, with
  4646. * `ev_data` pointing to the parsed `struct mg_dns_message`.
  4647. *
  4648. * See
  4649. * [captive_dns_server](https://github.com/cesanta/mongoose/tree/master/examples/captive_dns_server)
  4650. * example on how to handle DNS request and send DNS reply.
  4651. */
  4652. void mg_set_protocol_dns(struct mg_connection *nc);
  4653. #ifdef __cplusplus
  4654. }
  4655. #endif /* __cplusplus */
  4656. #endif /* CS_MONGOOSE_SRC_DNS_H_ */
  4657. #ifdef MG_MODULE_LINES
  4658. #line 1 "mongoose/src/dns_server.h"
  4659. #endif
  4660. /*
  4661. * Copyright (c) 2014 Cesanta Software Limited
  4662. * All rights reserved
  4663. */
  4664. /*
  4665. * === DNS server API reference
  4666. *
  4667. * Disabled by default; enable with `-DMG_ENABLE_DNS_SERVER`.
  4668. */
  4669. #ifndef CS_MONGOOSE_SRC_DNS_SERVER_H_
  4670. #define CS_MONGOOSE_SRC_DNS_SERVER_H_
  4671. #if MG_ENABLE_DNS_SERVER
  4672. /* Amalgamated: #include "mongoose/src/dns.h" */
  4673. #ifdef __cplusplus
  4674. extern "C" {
  4675. #endif /* __cplusplus */
  4676. #define MG_DNS_SERVER_DEFAULT_TTL 3600
  4677. struct mg_dns_reply {
  4678. struct mg_dns_message *msg;
  4679. struct mbuf *io;
  4680. size_t start;
  4681. };
  4682. /*
  4683. * Creates a DNS reply.
  4684. *
  4685. * The reply will be based on an existing query message `msg`.
  4686. * The query body will be appended to the output buffer.
  4687. * "reply + recursion allowed" will be added to the message flags and the
  4688. * message's num_answers will be set to 0.
  4689. *
  4690. * Answer records can be appended with `mg_dns_send_reply` or by lower
  4691. * level function defined in the DNS API.
  4692. *
  4693. * In order to send a reply use `mg_dns_send_reply`.
  4694. * It's possible to use a connection's send buffer as reply buffer,
  4695. * and it will work for both UDP and TCP connections.
  4696. *
  4697. * Example:
  4698. *
  4699. * ```c
  4700. * reply = mg_dns_create_reply(&nc->send_mbuf, msg);
  4701. * for (i = 0; i < msg->num_questions; i++) {
  4702. * rr = &msg->questions[i];
  4703. * if (rr->rtype == MG_DNS_A_RECORD) {
  4704. * mg_dns_reply_record(&reply, rr, 3600, &dummy_ip_addr, 4);
  4705. * }
  4706. * }
  4707. * mg_dns_send_reply(nc, &reply);
  4708. * ```
  4709. */
  4710. struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
  4711. struct mg_dns_message *msg);
  4712. /*
  4713. * Appends a DNS reply record to the IO buffer and to the DNS message.
  4714. *
  4715. * The message's num_answers field will be incremented. It's the caller's duty
  4716. * to ensure num_answers is properly initialised.
  4717. *
  4718. * Returns -1 on error.
  4719. */
  4720. int mg_dns_reply_record(struct mg_dns_reply *reply,
  4721. struct mg_dns_resource_record *question,
  4722. const char *name, int rtype, int ttl, const void *rdata,
  4723. size_t rdata_len);
  4724. /*
  4725. * Sends a DNS reply through a connection.
  4726. *
  4727. * The DNS data is stored in an IO buffer pointed by reply structure in `r`.
  4728. * This function mutates the content of that buffer in order to ensure that
  4729. * the DNS header reflects the size and flags of the message, that might have
  4730. * been updated either with `mg_dns_reply_record` or by direct manipulation of
  4731. * `r->message`.
  4732. *
  4733. * Once sent, the IO buffer will be trimmed unless the reply IO buffer
  4734. * is the connection's send buffer and the connection is not in UDP mode.
  4735. */
  4736. void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r);
  4737. #ifdef __cplusplus
  4738. }
  4739. #endif /* __cplusplus */
  4740. #endif /* MG_ENABLE_DNS_SERVER */
  4741. #endif /* CS_MONGOOSE_SRC_DNS_SERVER_H_ */
  4742. #ifdef MG_MODULE_LINES
  4743. #line 1 "mongoose/src/resolv.h"
  4744. #endif
  4745. /*
  4746. * Copyright (c) 2014 Cesanta Software Limited
  4747. * All rights reserved
  4748. */
  4749. /*
  4750. * === API reference
  4751. */
  4752. #ifndef CS_MONGOOSE_SRC_RESOLV_H_
  4753. #define CS_MONGOOSE_SRC_RESOLV_H_
  4754. /* Amalgamated: #include "mongoose/src/dns.h" */
  4755. #ifdef __cplusplus
  4756. extern "C" {
  4757. #endif /* __cplusplus */
  4758. enum mg_resolve_err {
  4759. MG_RESOLVE_OK = 0,
  4760. MG_RESOLVE_NO_ANSWERS = 1,
  4761. MG_RESOLVE_EXCEEDED_RETRY_COUNT = 2,
  4762. MG_RESOLVE_TIMEOUT = 3
  4763. };
  4764. typedef void (*mg_resolve_callback_t)(struct mg_dns_message *dns_message,
  4765. void *user_data, enum mg_resolve_err);
  4766. /* Options for `mg_resolve_async_opt`. */
  4767. struct mg_resolve_async_opts {
  4768. const char *nameserver_url;
  4769. int max_retries; /* defaults to 2 if zero */
  4770. int timeout; /* in seconds; defaults to 5 if zero */
  4771. int accept_literal; /* pseudo-resolve literal ipv4 and ipv6 addrs */
  4772. int only_literal; /* only resolves literal addrs; sync cb invocation */
  4773. struct mg_connection **dns_conn; /* return DNS connection */
  4774. };
  4775. /* See `mg_resolve_async_opt()` */
  4776. int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query,
  4777. mg_resolve_callback_t cb, void *data);
  4778. /*
  4779. * Resolved a DNS name asynchronously.
  4780. *
  4781. * Upon successful resolution, the user callback will be invoked
  4782. * with the full DNS response message and a pointer to the user's
  4783. * context `data`.
  4784. *
  4785. * In case of timeout while performing the resolution the callback
  4786. * will receive a NULL `msg`.
  4787. *
  4788. * The DNS answers can be extracted with `mg_next_record` and
  4789. * `mg_dns_parse_record_data`:
  4790. *
  4791. * [source,c]
  4792. * ----
  4793. * struct in_addr ina;
  4794. * struct mg_dns_resource_record *rr = mg_next_record(msg, MG_DNS_A_RECORD,
  4795. * NULL);
  4796. * mg_dns_parse_record_data(msg, rr, &ina, sizeof(ina));
  4797. * ----
  4798. */
  4799. int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query,
  4800. mg_resolve_callback_t cb, void *data,
  4801. struct mg_resolve_async_opts opts);
  4802. /*
  4803. * Resolve a name from `/etc/hosts`.
  4804. *
  4805. * Returns 0 on success, -1 on failure.
  4806. */
  4807. int mg_resolve_from_hosts_file(const char *host, union socket_address *usa);
  4808. #ifdef __cplusplus
  4809. }
  4810. #endif /* __cplusplus */
  4811. #endif /* CS_MONGOOSE_SRC_RESOLV_H_ */
  4812. #ifdef MG_MODULE_LINES
  4813. #line 1 "mongoose/src/coap.h"
  4814. #endif
  4815. /*
  4816. * Copyright (c) 2015 Cesanta Software Limited
  4817. * All rights reserved
  4818. * This software is dual-licensed: you can redistribute it and/or modify
  4819. * it under the terms of the GNU General Public License version 2 as
  4820. * published by the Free Software Foundation. For the terms of this
  4821. * license, see <http://www.gnu.org/licenses/>.
  4822. *
  4823. * You are free to use this software under the terms of the GNU General
  4824. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  4825. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  4826. * See the GNU General Public License for more details.
  4827. *
  4828. * Alternatively, you can license this software under a commercial
  4829. * license, as set out in <https://www.cesanta.com/license>.
  4830. */
  4831. /*
  4832. * === CoAP API reference
  4833. *
  4834. * CoAP message format:
  4835. *
  4836. * ```
  4837. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  4838. * |Ver| T | TKL | Code | Message ID | Token (if any, TKL bytes) ...
  4839. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  4840. * | Options (if any) ... |1 1 1 1 1 1 1 1| Payload (if any) ...
  4841. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  4842. * ```
  4843. */
  4844. #ifndef CS_MONGOOSE_SRC_COAP_H_
  4845. #define CS_MONGOOSE_SRC_COAP_H_
  4846. #if MG_ENABLE_COAP
  4847. #define MG_COAP_MSG_TYPE_FIELD 0x2
  4848. #define MG_COAP_CODE_CLASS_FIELD 0x4
  4849. #define MG_COAP_CODE_DETAIL_FIELD 0x8
  4850. #define MG_COAP_MSG_ID_FIELD 0x10
  4851. #define MG_COAP_TOKEN_FIELD 0x20
  4852. #define MG_COAP_OPTIOMG_FIELD 0x40
  4853. #define MG_COAP_PAYLOAD_FIELD 0x80
  4854. #define MG_COAP_ERROR 0x10000
  4855. #define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)
  4856. #define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)
  4857. #define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)
  4858. #define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)
  4859. #define MG_COAP_MSG_CON 0
  4860. #define MG_COAP_MSG_NOC 1
  4861. #define MG_COAP_MSG_ACK 2
  4862. #define MG_COAP_MSG_RST 3
  4863. #define MG_COAP_MSG_MAX 3
  4864. #define MG_COAP_CODECLASS_REQUEST 0
  4865. #define MG_COAP_CODECLASS_RESP_OK 2
  4866. #define MG_COAP_CODECLASS_CLIENT_ERR 4
  4867. #define MG_COAP_CODECLASS_SRV_ERR 5
  4868. #define MG_COAP_EVENT_BASE 300
  4869. #define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON)
  4870. #define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC)
  4871. #define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK)
  4872. #define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST)
  4873. /*
  4874. * CoAP options.
  4875. * Use mg_coap_add_option and mg_coap_free_options
  4876. * for creation and destruction.
  4877. */
  4878. struct mg_coap_option {
  4879. struct mg_coap_option *next;
  4880. uint32_t number;
  4881. struct mg_str value;
  4882. };
  4883. /* CoAP message. See RFC 7252 for details. */
  4884. struct mg_coap_message {
  4885. uint32_t flags;
  4886. uint8_t msg_type;
  4887. uint8_t code_class;
  4888. uint8_t code_detail;
  4889. uint16_t msg_id;
  4890. struct mg_str token;
  4891. struct mg_coap_option *options;
  4892. struct mg_str payload;
  4893. struct mg_coap_option *optiomg_tail;
  4894. };
  4895. #ifdef __cplusplus
  4896. extern "C" {
  4897. #endif /* __cplusplus */
  4898. /* Sets CoAP protocol handler - triggers CoAP specific events. */
  4899. int mg_set_protocol_coap(struct mg_connection *nc);
  4900. /*
  4901. * Adds a new option to mg_coap_message structure.
  4902. * Returns pointer to the newly created option.
  4903. * Note: options must be freed by using mg_coap_free_options
  4904. */
  4905. struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm,
  4906. uint32_t number, char *value,
  4907. size_t len);
  4908. /*
  4909. * Frees the memory allocated for options.
  4910. * If the cm paramater doesn't contain any option it does nothing.
  4911. */
  4912. void mg_coap_free_options(struct mg_coap_message *cm);
  4913. /*
  4914. * Composes a CoAP message from `mg_coap_message`
  4915. * and sends it into `nc` connection.
  4916. * Returns 0 on success. On error, it is a bitmask:
  4917. *
  4918. * - `#define MG_COAP_ERROR 0x10000`
  4919. * - `#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)`
  4920. * - `#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)`
  4921. * - `#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)`
  4922. * - `#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)`
  4923. */
  4924. uint32_t mg_coap_send_message(struct mg_connection *nc,
  4925. struct mg_coap_message *cm);
  4926. /*
  4927. * Composes CoAP acknowledgement from `mg_coap_message`
  4928. * and sends it into `nc` connection.
  4929. * Return value: see `mg_coap_send_message()`
  4930. */
  4931. uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id);
  4932. /*
  4933. * Parses CoAP message and fills mg_coap_message and returns cm->flags.
  4934. * This is a helper function.
  4935. *
  4936. * NOTE: usually CoAP works over UDP, so lack of data means format error.
  4937. * But, in theory, it is possible to use CoAP over TCP (according to RFC)
  4938. *
  4939. * The caller has to check results and treat COAP_NOT_ENOUGH_DATA according to
  4940. * underlying protocol:
  4941. *
  4942. * - in case of UDP COAP_NOT_ENOUGH_DATA means COAP_FORMAT_ERROR,
  4943. * - in case of TCP client can try to receive more data
  4944. *
  4945. * Return value: see `mg_coap_send_message()`
  4946. */
  4947. uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm);
  4948. /*
  4949. * Composes CoAP message from mg_coap_message structure.
  4950. * This is a helper function.
  4951. * Return value: see `mg_coap_send_message()`
  4952. */
  4953. uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io);
  4954. #ifdef __cplusplus
  4955. }
  4956. #endif /* __cplusplus */
  4957. #endif /* MG_ENABLE_COAP */
  4958. #endif /* CS_MONGOOSE_SRC_COAP_H_ */
  4959. #ifdef MG_MODULE_LINES
  4960. #line 1 "mongoose/src/sntp.h"
  4961. #endif
  4962. /*
  4963. * Copyright (c) 2016 Cesanta Software Limited
  4964. * All rights reserved
  4965. */
  4966. #ifndef CS_MONGOOSE_SRC_SNTP_H_
  4967. #define CS_MONGOOSE_SRC_SNTP_H_
  4968. #if MG_ENABLE_SNTP
  4969. #define MG_SNTP_EVENT_BASE 500
  4970. /*
  4971. * Received reply from time server. Event handler parameter contains
  4972. * pointer to mg_sntp_message structure
  4973. */
  4974. #define MG_SNTP_REPLY (MG_SNTP_EVENT_BASE + 1)
  4975. /* Received malformed SNTP packet */
  4976. #define MG_SNTP_MALFORMED_REPLY (MG_SNTP_EVENT_BASE + 2)
  4977. /* Failed to get time from server (timeout etc) */
  4978. #define MG_SNTP_FAILED (MG_SNTP_EVENT_BASE + 3)
  4979. struct mg_sntp_message {
  4980. /* if server sends this flags, user should not send requests to it */
  4981. int kiss_of_death;
  4982. /* usual mg_time */
  4983. double time;
  4984. };
  4985. /* Establishes connection to given sntp server */
  4986. struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr,
  4987. mg_event_handler_t event_handler,
  4988. const char *sntp_server_name);
  4989. /* Sends time request to given connection */
  4990. void mg_sntp_send_request(struct mg_connection *c);
  4991. /*
  4992. * Helper function
  4993. * Establishes connection to time server, tries to send request
  4994. * repeats sending SNTP_ATTEMPTS times every SNTP_TIMEOUT sec
  4995. * (if needed)
  4996. * See sntp_client example
  4997. */
  4998. struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr,
  4999. mg_event_handler_t event_handler,
  5000. const char *sntp_server_name);
  5001. #endif
  5002. /////////////////////////////////////////////////////////////////////////////////////////
  5003. struct mg_timer {
  5004. unsigned long id; // Timer ID
  5005. uint64_t period_ms; // Timer period in milliseconds
  5006. uint64_t expire; // Expiration timestamp in milliseconds
  5007. unsigned flags; // Possible flags values below
  5008. #define MG_TIMER_ONCE 0 // Call function once
  5009. #define MG_TIMER_REPEAT 1 // Call function periodically
  5010. #define MG_TIMER_RUN_NOW 2 // Call immediately when timer is set
  5011. void (*fn)(void *); // Function to call
  5012. void *arg; // Function argument
  5013. struct mg_timer *next; // Linkage
  5014. };
  5015. void mg_timer_init(struct mg_timer **head, struct mg_timer *timer,
  5016. uint64_t milliseconds, unsigned flags, void (*fn)(void *),
  5017. void *arg);
  5018. void mg_timer_free(struct mg_timer **head, struct mg_timer *);
  5019. void mg_timer_poll(struct mg_timer **head, uint64_t new_ms);
  5020. #include "stdbool.h"
  5021. bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
  5022. struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
  5023. unsigned flags, void (*fn)(void *), void *arg);
  5024. //////////////////////////////////////////////////////////////////////////////////////////////
  5025. #endif /* CS_MONGOOSE_SRC_SNTP_H_ */