mongoose.h 200 KB

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