mongoose.h 168 KB

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