mdns.c 261 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/queue.h"
  10. #include "freertos/semphr.h"
  11. #include "esp_log.h"
  12. #include "esp_event.h"
  13. #include "esp_random.h"
  14. #include "esp_check.h"
  15. #include "mdns.h"
  16. #include "mdns_private.h"
  17. #include "mdns_networking.h"
  18. static void _mdns_browse_item_free(mdns_browse_t *browse);
  19. static esp_err_t _mdns_send_browse_action(mdns_action_type_t type, mdns_browse_t *browse);
  20. static esp_err_t _mdns_sync_browse_action(mdns_action_type_t type, mdns_browse_sync_t *browse_sync);
  21. static void _mdns_browse_sync(mdns_browse_sync_t *browse_sync);
  22. static void _mdns_browse_finish(mdns_browse_t *browse);
  23. static void _mdns_browse_add(mdns_browse_t *browse);
  24. static void _mdns_browse_send(mdns_browse_t *browse, mdns_if_t interface);
  25. #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
  26. #include "esp_eth.h"
  27. #endif
  28. #if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 0)
  29. #define MDNS_ESP_WIFI_ENABLED CONFIG_SOC_WIFI_SUPPORTED
  30. #else
  31. #define MDNS_ESP_WIFI_ENABLED CONFIG_ESP_WIFI_ENABLED
  32. #endif
  33. #if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP)
  34. #include "esp_wifi.h"
  35. #endif
  36. #ifdef MDNS_ENABLE_DEBUG
  37. void mdns_debug_packet(const uint8_t *data, size_t len);
  38. #endif
  39. #ifndef ARRAY_SIZE
  40. #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  41. #endif
  42. // Internal size of IPv6 address is defined here as size of AAAA record in mdns packet
  43. // since the ip6_addr_t is defined in lwip and depends on using IPv6 zones
  44. #define _MDNS_SIZEOF_IP6_ADDR (MDNS_ANSWER_AAAA_SIZE)
  45. static const char *MDNS_DEFAULT_DOMAIN = "local";
  46. static const char *MDNS_SUB_STR = "_sub";
  47. mdns_server_t *_mdns_server = NULL;
  48. static mdns_host_item_t *_mdns_host_list = NULL;
  49. static mdns_host_item_t _mdns_self_host;
  50. static const char *TAG = "mdns";
  51. static volatile TaskHandle_t _mdns_service_task_handle = NULL;
  52. static SemaphoreHandle_t _mdns_service_semaphore = NULL;
  53. static void _mdns_search_finish_done(void);
  54. static mdns_search_once_t *_mdns_search_find_from(mdns_search_once_t *search, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
  55. static mdns_browse_t *_mdns_browse_find_from(mdns_browse_t *b, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
  56. static void _mdns_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto,
  57. uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse);
  58. static void _mdns_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip,
  59. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse);
  60. static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto,
  61. mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
  62. uint32_t ttl, mdns_browse_sync_t *out_sync_browse);
  63. #ifdef MDNS_ENABLE_DEBUG
  64. static void debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t);
  65. static void debug_printf_browse_result_all(mdns_result_t *r_t);
  66. #endif // MDNS_ENABLE_DEBUG
  67. static void _mdns_search_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip,
  68. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
  69. static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
  70. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
  71. static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
  72. size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
  73. uint32_t ttl);
  74. static mdns_result_t *_mdns_search_result_add_ptr(mdns_search_once_t *search, const char *instance,
  75. const char *service_type, const char *proto, mdns_if_t tcpip_if,
  76. mdns_ip_protocol_t ip_protocol, uint32_t ttl);
  77. static bool _mdns_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[], size_t services_len, bool flush, bool bye);
  78. static bool _mdns_append_host_list(mdns_out_answer_t **destination, bool flush, bool bye);
  79. static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname);
  80. static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action);
  81. static void _mdns_query_results_free(mdns_result_t *results);
  82. typedef enum {
  83. MDNS_IF_STA = 0,
  84. MDNS_IF_AP = 1,
  85. MDNS_IF_ETH = 2,
  86. } mdns_predef_if_t;
  87. typedef struct mdns_interfaces mdns_interfaces_t;
  88. struct mdns_interfaces {
  89. const bool predefined;
  90. esp_netif_t *netif;
  91. const mdns_predef_if_t predef_if;
  92. mdns_if_t duplicate;
  93. };
  94. /*
  95. * @brief Internal collection of mdns supported interfaces
  96. *
  97. */
  98. static mdns_interfaces_t s_esp_netifs[MDNS_MAX_INTERFACES] = {
  99. #if CONFIG_MDNS_PREDEF_NETIF_STA
  100. { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_STA, .duplicate = MDNS_MAX_INTERFACES },
  101. #endif
  102. #if CONFIG_MDNS_PREDEF_NETIF_AP
  103. { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_AP, .duplicate = MDNS_MAX_INTERFACES },
  104. #endif
  105. #if CONFIG_MDNS_PREDEF_NETIF_ETH
  106. { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_ETH, .duplicate = MDNS_MAX_INTERFACES },
  107. #endif
  108. };
  109. /**
  110. * @brief Convert Predefined interface to the netif id from the internal netif list
  111. * @param predef_if Predefined interface enum
  112. * @return Ordinal number of internal list of mdns network interface.
  113. * Returns MDNS_MAX_INTERFACES if the predefined interface wasn't found in the list
  114. */
  115. static mdns_if_t mdns_if_from_preset_if(mdns_predef_if_t predef_if)
  116. {
  117. for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) {
  118. if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == predef_if) {
  119. return i;
  120. }
  121. }
  122. return MDNS_MAX_INTERFACES;
  123. }
  124. /**
  125. * @brief Convert Predefined interface to esp-netif handle
  126. * @param predef_if Predefined interface enum
  127. * @return esp_netif pointer from system list of network interfaces
  128. */
  129. static inline esp_netif_t *esp_netif_from_preset_if(mdns_predef_if_t predef_if)
  130. {
  131. switch (predef_if) {
  132. case MDNS_IF_STA:
  133. return esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  134. case MDNS_IF_AP:
  135. return esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
  136. #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
  137. case MDNS_IF_ETH:
  138. return esp_netif_get_handle_from_ifkey("ETH_DEF");
  139. #endif
  140. default:
  141. return NULL;
  142. }
  143. }
  144. /**
  145. * @brief Gets the actual esp_netif pointer from the internal network interface list
  146. *
  147. * The supplied ordinal number could
  148. * - point to a predef netif -> "STA", "AP", "ETH"
  149. * - if no entry in the list (NULL) -> check if the system added this netif
  150. * - point to a custom netif -> just return the entry in the list
  151. * - users is responsible for the lifetime of this netif (to be valid between mdns-init -> deinit)
  152. *
  153. * @param tcpip_if Ordinal number of the interface
  154. * @return Pointer ot the esp_netif object if the interface is available, NULL otherwise
  155. */
  156. esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if)
  157. {
  158. if (tcpip_if < MDNS_MAX_INTERFACES) {
  159. if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) {
  160. // If the local copy is NULL and this netif is predefined -> we can find it in the global netif list
  161. s_esp_netifs[tcpip_if].netif = esp_netif_from_preset_if(s_esp_netifs[tcpip_if].predef_if);
  162. // failing to find it means that the netif is *not* available -> return NULL
  163. }
  164. return s_esp_netifs[tcpip_if].netif;
  165. }
  166. return NULL;
  167. }
  168. /*
  169. * @brief Clean internal mdns interface's pointer
  170. */
  171. static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if)
  172. {
  173. if (tcpip_if < MDNS_MAX_INTERFACES) {
  174. s_esp_netifs[tcpip_if].netif = NULL;
  175. }
  176. }
  177. /*
  178. * @brief Convert esp-netif handle to mdns if
  179. */
  180. static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *esp_netif)
  181. {
  182. for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) {
  183. // The predefined netifs in the static array are NULL when firstly calling this function
  184. // if IPv4 is disabled. Set these netifs here.
  185. if (s_esp_netifs[i].netif == NULL && s_esp_netifs[i].predefined) {
  186. s_esp_netifs[i].netif = esp_netif_from_preset_if(s_esp_netifs[i].predef_if);
  187. }
  188. if (esp_netif == s_esp_netifs[i].netif) {
  189. return i;
  190. }
  191. }
  192. return MDNS_MAX_INTERFACES;
  193. }
  194. static inline bool _str_null_or_empty(const char *str)
  195. {
  196. return (str == NULL || *str == 0);
  197. }
  198. /*
  199. * @brief Appends/increments a number to name/instance in case of collision
  200. * */
  201. static char *_mdns_mangle_name(char *in)
  202. {
  203. char *p = strrchr(in, '-');
  204. int suffix = 0;
  205. if (p == NULL) {
  206. //No - in ``in``
  207. suffix = 2;
  208. } else {
  209. char *endp = NULL;
  210. suffix = strtol(p + 1, &endp, 10);
  211. if (*endp != 0) {
  212. //suffix is not numerical
  213. suffix = 2;
  214. p = NULL; //so we append -suffix to the entire string
  215. }
  216. }
  217. char *ret;
  218. if (p == NULL) {
  219. //need to add -2 to string
  220. ret = malloc(strlen(in) + 3);
  221. if (ret == NULL) {
  222. HOOK_MALLOC_FAILED;
  223. return NULL;
  224. }
  225. sprintf(ret, "%s-2", in);
  226. } else {
  227. ret = malloc(strlen(in) + 2); //one extra byte in case 9-10 or 99-100 etc
  228. if (ret == NULL) {
  229. HOOK_MALLOC_FAILED;
  230. return NULL;
  231. }
  232. strcpy(ret, in);
  233. int baseLen = p - in; //length of 'bla' in 'bla-123'
  234. //overwrite suffix with new suffix
  235. sprintf(ret + baseLen, "-%d", suffix + 1);
  236. }
  237. return ret;
  238. }
  239. static bool _mdns_service_match(const mdns_service_t *srv, const char *service, const char *proto,
  240. const char *hostname)
  241. {
  242. if (!service || !proto || !srv->hostname) {
  243. return false;
  244. }
  245. return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
  246. (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
  247. }
  248. /**
  249. * @brief finds service from given service type
  250. * @param server the server
  251. * @param service service type to match
  252. * @param proto proto to match
  253. * @param hostname hostname of the service (if non-null)
  254. *
  255. * @return the service item if found or NULL on error
  256. */
  257. static mdns_srv_item_t *_mdns_get_service_item(const char *service, const char *proto, const char *hostname)
  258. {
  259. mdns_srv_item_t *s = _mdns_server->services;
  260. while (s) {
  261. if (_mdns_service_match(s->service, service, proto, hostname)) {
  262. return s;
  263. }
  264. s = s->next;
  265. }
  266. return NULL;
  267. }
  268. static mdns_srv_item_t *_mdns_get_service_item_subtype(const char *subtype, const char *service, const char *proto)
  269. {
  270. mdns_srv_item_t *s = _mdns_server->services;
  271. while (s) {
  272. if (_mdns_service_match(s->service, service, proto, NULL)) {
  273. mdns_subtype_t *subtype_item = s->service->subtype;
  274. while (subtype_item) {
  275. if (!strcasecmp(subtype_item->subtype, subtype)) {
  276. return s;
  277. }
  278. subtype_item = subtype_item->next;
  279. }
  280. }
  281. s = s->next;
  282. }
  283. return NULL;
  284. }
  285. static mdns_host_item_t *mdns_get_host_item(const char *hostname)
  286. {
  287. if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) {
  288. return &_mdns_self_host;
  289. }
  290. mdns_host_item_t *host = _mdns_host_list;
  291. while (host != NULL) {
  292. if (strcasecmp(host->hostname, hostname) == 0) {
  293. return host;
  294. }
  295. host = host->next;
  296. }
  297. return NULL;
  298. }
  299. static bool _mdns_can_add_more_services(void)
  300. {
  301. #if MDNS_MAX_SERVICES == 0
  302. return false;
  303. #else
  304. mdns_srv_item_t *s = _mdns_server->services;
  305. uint16_t service_num = 0;
  306. while (s) {
  307. service_num ++;
  308. s = s->next;
  309. if (service_num >= MDNS_MAX_SERVICES) {
  310. return false;
  311. }
  312. }
  313. return true;
  314. #endif
  315. }
  316. esp_err_t _mdns_send_rx_action(mdns_rx_packet_t *packet)
  317. {
  318. mdns_action_t *action = NULL;
  319. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  320. if (!action) {
  321. HOOK_MALLOC_FAILED;
  322. return ESP_ERR_NO_MEM;
  323. }
  324. action->type = ACTION_RX_HANDLE;
  325. action->data.rx_handle.packet = packet;
  326. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  327. free(action);
  328. return ESP_ERR_NO_MEM;
  329. }
  330. return ESP_OK;
  331. }
  332. static const char *_mdns_get_default_instance_name(void)
  333. {
  334. if (_mdns_server && !_str_null_or_empty(_mdns_server->instance)) {
  335. return _mdns_server->instance;
  336. }
  337. if (_mdns_server && !_str_null_or_empty(_mdns_server->hostname)) {
  338. return _mdns_server->hostname;
  339. }
  340. return NULL;
  341. }
  342. /**
  343. * @brief Get the service name of a service
  344. */
  345. static const char *_mdns_get_service_instance_name(const mdns_service_t *service)
  346. {
  347. if (service && !_str_null_or_empty(service->instance)) {
  348. return service->instance;
  349. }
  350. return _mdns_get_default_instance_name();
  351. }
  352. static bool _mdns_instance_name_match(const char *lhs, const char *rhs)
  353. {
  354. if (lhs == NULL) {
  355. lhs = _mdns_get_default_instance_name();
  356. }
  357. if (rhs == NULL) {
  358. rhs = _mdns_get_default_instance_name();
  359. }
  360. return !strcasecmp(lhs, rhs);
  361. }
  362. static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service,
  363. const char *proto, const char *hostname)
  364. {
  365. // service and proto must be supplied, if not this instance won't match
  366. if (!service || !proto) {
  367. return false;
  368. }
  369. // instance==NULL -> _mdns_instance_name_match() will check the default instance
  370. // hostname==NULL -> matches if instance, service and proto matches
  371. return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
  372. !strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
  373. }
  374. static mdns_srv_item_t *_mdns_get_service_item_instance(const char *instance, const char *service, const char *proto,
  375. const char *hostname)
  376. {
  377. mdns_srv_item_t *s = _mdns_server->services;
  378. while (s) {
  379. if (instance) {
  380. if (_mdns_service_match_instance(s->service, instance, service, proto, hostname)) {
  381. return s;
  382. }
  383. } else {
  384. if (_mdns_service_match(s->service, service, proto, hostname)) {
  385. return s;
  386. }
  387. }
  388. s = s->next;
  389. }
  390. return NULL;
  391. }
  392. /**
  393. * @brief reads MDNS FQDN into mdns_name_t structure
  394. * FQDN is in format: [hostname.|[instance.]_service._proto.]local.
  395. *
  396. * @param packet MDNS packet
  397. * @param start Starting point of FQDN
  398. * @param name mdns_name_t structure to populate
  399. * @param buf temporary char buffer
  400. *
  401. * @return the address after the parsed FQDN in the packet or NULL on error
  402. */
  403. static const uint8_t *_mdns_read_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, char *buf, size_t packet_len)
  404. {
  405. size_t index = 0;
  406. const uint8_t *packet_end = packet + packet_len;
  407. while (start + index < packet_end && start[index]) {
  408. if (name->parts == 4) {
  409. name->invalid = true;
  410. }
  411. uint8_t len = start[index++];
  412. if (len < 0xC0) {
  413. if (len > 63) {
  414. //length can not be more than 63
  415. return NULL;
  416. }
  417. uint8_t i;
  418. for (i = 0; i < len; i++) {
  419. if (start + index >= packet_end) {
  420. return NULL;
  421. }
  422. buf[i] = start[index++];
  423. }
  424. buf[len] = '\0';
  425. if (name->parts == 1 && buf[0] != '_'
  426. && (strcasecmp(buf, MDNS_DEFAULT_DOMAIN) != 0)
  427. && (strcasecmp(buf, "arpa") != 0)
  428. #ifndef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  429. && (strcasecmp(buf, "ip6") != 0)
  430. && (strcasecmp(buf, "in-addr") != 0)
  431. #endif
  432. ) {
  433. strlcat(name->host, ".", sizeof(name->host));
  434. strlcat(name->host, buf, sizeof(name->host));
  435. } else if (strcasecmp(buf, MDNS_SUB_STR) == 0) {
  436. name->sub = 1;
  437. } else if (!name->invalid) {
  438. char *mdns_name_ptrs[] = {name->host, name->service, name->proto, name->domain};
  439. memcpy(mdns_name_ptrs[name->parts++], buf, len + 1);
  440. }
  441. } else {
  442. size_t address = (((uint16_t)len & 0x3F) << 8) | start[index++];
  443. if ((packet + address) >= start) {
  444. //reference address can not be after where we are
  445. return NULL;
  446. }
  447. if (_mdns_read_fqdn(packet, packet + address, name, buf, packet_len)) {
  448. return start + index;
  449. }
  450. return NULL;
  451. }
  452. }
  453. return start + index + 1;
  454. }
  455. /**
  456. * @brief sets uint16_t value in a packet
  457. *
  458. * @param packet MDNS packet
  459. * @param index offset of uint16_t value
  460. * @param value the value to set
  461. */
  462. static inline void _mdns_set_u16(uint8_t *packet, uint16_t index, uint16_t value)
  463. {
  464. if ((index + 1) >= MDNS_MAX_PACKET_SIZE) {
  465. return;
  466. }
  467. packet[index] = (value >> 8) & 0xFF;
  468. packet[index + 1] = value & 0xFF;
  469. }
  470. /**
  471. * @brief appends byte in a packet, incrementing the index
  472. *
  473. * @param packet MDNS packet
  474. * @param index offset in the packet
  475. * @param value the value to set
  476. *
  477. * @return length of added data: 0 on error or 1 on success
  478. */
  479. static inline uint8_t _mdns_append_u8(uint8_t *packet, uint16_t *index, uint8_t value)
  480. {
  481. if (*index >= MDNS_MAX_PACKET_SIZE) {
  482. return 0;
  483. }
  484. packet[*index] = value;
  485. *index += 1;
  486. return 1;
  487. }
  488. /**
  489. * @brief appends uint16_t in a packet, incrementing the index
  490. *
  491. * @param packet MDNS packet
  492. * @param index offset in the packet
  493. * @param value the value to set
  494. *
  495. * @return length of added data: 0 on error or 2 on success
  496. */
  497. static inline uint8_t _mdns_append_u16(uint8_t *packet, uint16_t *index, uint16_t value)
  498. {
  499. if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) {
  500. return 0;
  501. }
  502. _mdns_append_u8(packet, index, (value >> 8) & 0xFF);
  503. _mdns_append_u8(packet, index, value & 0xFF);
  504. return 2;
  505. }
  506. /**
  507. * @brief appends uint32_t in a packet, incrementing the index
  508. *
  509. * @param packet MDNS packet
  510. * @param index offset in the packet
  511. * @param value the value to set
  512. *
  513. * @return length of added data: 0 on error or 4 on success
  514. */
  515. static inline uint8_t _mdns_append_u32(uint8_t *packet, uint16_t *index, uint32_t value)
  516. {
  517. if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
  518. return 0;
  519. }
  520. _mdns_append_u8(packet, index, (value >> 24) & 0xFF);
  521. _mdns_append_u8(packet, index, (value >> 16) & 0xFF);
  522. _mdns_append_u8(packet, index, (value >> 8) & 0xFF);
  523. _mdns_append_u8(packet, index, value & 0xFF);
  524. return 4;
  525. }
  526. /**
  527. * @brief appends answer type, class, ttl and data length to a packet, incrementing the index
  528. *
  529. * @param packet MDNS packet
  530. * @param index offset in the packet
  531. * @param type answer type
  532. * @param ttl answer ttl
  533. *
  534. * @return length of added data: 0 on error or 10 on success
  535. */
  536. static inline uint8_t _mdns_append_type(uint8_t *packet, uint16_t *index, uint8_t type, bool flush, uint32_t ttl)
  537. {
  538. if ((*index + 10) >= MDNS_MAX_PACKET_SIZE) {
  539. return 0;
  540. }
  541. uint16_t mdns_class = MDNS_CLASS_IN;
  542. if (flush) {
  543. mdns_class = MDNS_CLASS_IN_FLUSH_CACHE;
  544. }
  545. if (type == MDNS_ANSWER_PTR) {
  546. _mdns_append_u16(packet, index, MDNS_TYPE_PTR);
  547. _mdns_append_u16(packet, index, mdns_class);
  548. } else if (type == MDNS_ANSWER_TXT) {
  549. _mdns_append_u16(packet, index, MDNS_TYPE_TXT);
  550. _mdns_append_u16(packet, index, mdns_class);
  551. } else if (type == MDNS_ANSWER_SRV) {
  552. _mdns_append_u16(packet, index, MDNS_TYPE_SRV);
  553. _mdns_append_u16(packet, index, mdns_class);
  554. } else if (type == MDNS_ANSWER_A) {
  555. _mdns_append_u16(packet, index, MDNS_TYPE_A);
  556. _mdns_append_u16(packet, index, mdns_class);
  557. } else if (type == MDNS_ANSWER_AAAA) {
  558. _mdns_append_u16(packet, index, MDNS_TYPE_AAAA);
  559. _mdns_append_u16(packet, index, mdns_class);
  560. } else {
  561. return 0;
  562. }
  563. _mdns_append_u32(packet, index, ttl);
  564. _mdns_append_u16(packet, index, 0);
  565. return 10;
  566. }
  567. static inline uint8_t _mdns_append_string_with_len(uint8_t *packet, uint16_t *index, const char *string, uint8_t len)
  568. {
  569. if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
  570. return 0;
  571. }
  572. _mdns_append_u8(packet, index, len);
  573. memcpy(packet + *index, string, len);
  574. *index += len;
  575. return len + 1;
  576. }
  577. /**
  578. * @brief appends single string to a packet, incrementing the index
  579. *
  580. * @param packet MDNS packet
  581. * @param index offset in the packet
  582. * @param string the string to append
  583. *
  584. * @return length of added data: 0 on error or length of the string + 1 on success
  585. */
  586. static inline uint8_t _mdns_append_string(uint8_t *packet, uint16_t *index, const char *string)
  587. {
  588. uint8_t len = strlen(string);
  589. if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
  590. return 0;
  591. }
  592. _mdns_append_u8(packet, index, len);
  593. memcpy(packet + *index, string, len);
  594. *index += len;
  595. return len + 1;
  596. }
  597. /**
  598. * @brief appends one TXT record ("key=value" or "key")
  599. *
  600. * @param packet MDNS packet
  601. * @param index offset in the packet
  602. * @param txt one txt record
  603. *
  604. * @return length of added data: length of the added txt value + 1 on success
  605. * 0 if data won't fit the packet
  606. * -1 if invalid TXT entry
  607. */
  608. static inline int append_one_txt_record_entry(uint8_t *packet, uint16_t *index, mdns_txt_linked_item_t *txt)
  609. {
  610. if (txt == NULL || txt->key == NULL) {
  611. return -1;
  612. }
  613. size_t key_len = strlen(txt->key);
  614. size_t len = key_len + txt->value_len + (txt->value ? 1 : 0);
  615. if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
  616. return 0;
  617. }
  618. _mdns_append_u8(packet, index, len);
  619. memcpy(packet + *index, txt->key, key_len);
  620. if (txt->value) {
  621. packet[*index + key_len] = '=';
  622. memcpy(packet + *index + key_len + 1, txt->value, txt->value_len);
  623. }
  624. *index += len;
  625. return len + 1;
  626. }
  627. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  628. static inline int append_single_str(uint8_t *packet, uint16_t *index, const char *str, int len)
  629. {
  630. if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
  631. return 0;
  632. }
  633. if (!_mdns_append_u8(packet, index, len)) {
  634. return 0;
  635. }
  636. memcpy(packet + *index, str, len);
  637. *index += len;
  638. return *index;
  639. }
  640. /**
  641. * @brief appends FQDN to a packet from hostname separated by dots. This API works the same way as
  642. * _mdns_append_fqdn(), but refrains from DNS compression (as it's mainly used for IP addresses (many short items),
  643. * where we gain very little (or compression even gets counter-productive mainly for IPv6 addresses)
  644. *
  645. * @param packet MDNS packet
  646. * @param index offset in the packet
  647. * @param name name representing FQDN in '.' separated parts
  648. * @param last true if appending the last part (domain, typically "arpa")
  649. *
  650. * @return length of added data: 0 on error or length on success
  651. */
  652. static uint16_t append_fqdn_dots(uint8_t *packet, uint16_t *index, const char *name, bool last)
  653. {
  654. int len = strlen(name);
  655. char *host = (char *)name;
  656. char *end = host;
  657. char *start = host;
  658. do {
  659. end = memchr(start, '.', host + len - start);
  660. end = end ? end : host + len;
  661. int part_len = end - start;
  662. if (!append_single_str(packet, index, start, part_len)) {
  663. return 0;
  664. }
  665. start = ++end;
  666. } while (end < name + len);
  667. if (!append_single_str(packet, index, "arpa", sizeof("arpa") - 1)) {
  668. return 0;
  669. }
  670. //empty string so terminate
  671. if (!_mdns_append_u8(packet, index, 0)) {
  672. return 0;
  673. }
  674. return *index;
  675. }
  676. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  677. /**
  678. * @brief appends FQDN to a packet, incrementing the index and
  679. * compressing the output if previous occurrence of the string (or part of it) has been found
  680. *
  681. * @param packet MDNS packet
  682. * @param index offset in the packet
  683. * @param strings string array containing the parts of the FQDN
  684. * @param count number of strings in the array
  685. *
  686. * @return length of added data: 0 on error or length on success
  687. */
  688. static uint16_t _mdns_append_fqdn(uint8_t *packet, uint16_t *index, const char *strings[], uint8_t count, size_t packet_len)
  689. {
  690. if (!count) {
  691. //empty string so terminate
  692. return _mdns_append_u8(packet, index, 0);
  693. }
  694. mdns_name_t name;
  695. static char buf[MDNS_NAME_BUF_LEN];
  696. uint8_t len = strlen(strings[0]);
  697. //try to find first the string length in the packet (if it exists)
  698. uint8_t *len_location = (uint8_t *)memchr(packet, (char)len, *index);
  699. while (len_location) {
  700. //check if the string after len_location is the string that we are looking for
  701. if (memcmp(len_location + 1, strings[0], len)) { //not continuing with our string
  702. search_next:
  703. //try and find the length byte further in the packet
  704. len_location = (uint8_t *)memchr(len_location + 1, (char)len, *index - (len_location + 1 - packet));
  705. continue;
  706. }
  707. //seems that we might have found the string that we are looking for
  708. //read the destination into name and compare
  709. name.parts = 0;
  710. name.sub = 0;
  711. name.invalid = false;
  712. name.host[0] = 0;
  713. name.service[0] = 0;
  714. name.proto[0] = 0;
  715. name.domain[0] = 0;
  716. const uint8_t *content = _mdns_read_fqdn(packet, len_location, &name, buf, packet_len);
  717. if (!content) {
  718. //not a readable fqdn?
  719. goto search_next; // could be our unfinished fqdn, continue searching
  720. }
  721. if (name.parts == count) {
  722. uint8_t i;
  723. for (i = 0; i < count; i++) {
  724. if (strcasecmp(strings[i], (const char *)&name + (i * (MDNS_NAME_BUF_LEN)))) {
  725. //not our string! let's search more
  726. goto search_next;
  727. }
  728. }
  729. //we actually have found the string
  730. break;
  731. } else {
  732. goto search_next;
  733. }
  734. }
  735. //string is not yet in the packet, so let's add it
  736. if (!len_location) {
  737. uint8_t written = _mdns_append_string(packet, index, strings[0]);
  738. if (!written) {
  739. return 0;
  740. }
  741. //run the same for the other strings in the name
  742. return written + _mdns_append_fqdn(packet, index, &strings[1], count - 1, packet_len);
  743. }
  744. //we have found the string so let's insert a pointer to it instead
  745. uint16_t offset = len_location - packet;
  746. offset |= MDNS_NAME_REF;
  747. return _mdns_append_u16(packet, index, offset);
  748. }
  749. /**
  750. * @brief appends PTR record for service to a packet, incrementing the index
  751. *
  752. * @param packet MDNS packet
  753. * @param index offset in the packet
  754. * @param server the server that is hosting the service
  755. * @param service the service to add record for
  756. *
  757. * @return length of added data: 0 on error or length on success
  758. */
  759. static uint16_t _mdns_append_ptr_record(uint8_t *packet, uint16_t *index, const char *instance, const char *service, const char *proto, bool flush, bool bye)
  760. {
  761. const char *str[4];
  762. uint16_t record_length = 0;
  763. uint8_t part_length;
  764. if (service == NULL) {
  765. return 0;
  766. }
  767. str[0] = instance;
  768. str[1] = service;
  769. str[2] = proto;
  770. str[3] = MDNS_DEFAULT_DOMAIN;
  771. part_length = _mdns_append_fqdn(packet, index, str + 1, 3, MDNS_MAX_PACKET_SIZE);
  772. if (!part_length) {
  773. return 0;
  774. }
  775. record_length += part_length;
  776. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL);
  777. if (!part_length) {
  778. return 0;
  779. }
  780. record_length += part_length;
  781. uint16_t data_len_location = *index - 2;
  782. part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE);
  783. if (!part_length) {
  784. return 0;
  785. }
  786. _mdns_set_u16(packet, data_len_location, part_length);
  787. record_length += part_length;
  788. return record_length;
  789. }
  790. /**
  791. * @brief appends PTR record for a subtype to a packet, incrementing the index
  792. *
  793. * @param packet MDNS packet
  794. * @param index offset in the packet
  795. * @param instance the service instance name
  796. * @param subtype the service subtype
  797. * @param proto the service protocol
  798. * @param flush whether to set the flush flag
  799. * @param bye whether to set the bye flag
  800. *
  801. * @return length of added data: 0 on error or length on success
  802. */
  803. static uint16_t _mdns_append_subtype_ptr_record(uint8_t *packet, uint16_t *index, const char *instance,
  804. const char *subtype, const char *service, const char *proto, bool flush,
  805. bool bye)
  806. {
  807. const char *subtype_str[5] = {subtype, MDNS_SUB_STR, service, proto, MDNS_DEFAULT_DOMAIN};
  808. const char *instance_str[4] = {instance, service, proto, MDNS_DEFAULT_DOMAIN};
  809. uint16_t record_length = 0;
  810. uint8_t part_length;
  811. if (service == NULL) {
  812. return 0;
  813. }
  814. part_length = _mdns_append_fqdn(packet, index, subtype_str, ARRAY_SIZE(subtype_str), MDNS_MAX_PACKET_SIZE);
  815. if (!part_length) {
  816. return 0;
  817. }
  818. record_length += part_length;
  819. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL);
  820. if (!part_length) {
  821. return 0;
  822. }
  823. record_length += part_length;
  824. uint16_t data_len_location = *index - 2;
  825. part_length = _mdns_append_fqdn(packet, index, instance_str, ARRAY_SIZE(instance_str), MDNS_MAX_PACKET_SIZE);
  826. if (!part_length) {
  827. return 0;
  828. }
  829. _mdns_set_u16(packet, data_len_location, part_length);
  830. record_length += part_length;
  831. return record_length;
  832. }
  833. /**
  834. * @brief appends DNS-SD PTR record for service to a packet, incrementing the index
  835. *
  836. * @param packet MDNS packet
  837. * @param index offset in the packet
  838. * @param server the server that is hosting the service
  839. * @param service the service to add record for
  840. *
  841. * @return length of added data: 0 on error or length on success
  842. */
  843. static uint16_t _mdns_append_sdptr_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye)
  844. {
  845. const char *str[3];
  846. const char *sd_str[4];
  847. uint16_t record_length = 0;
  848. uint8_t part_length;
  849. if (service == NULL) {
  850. return 0;
  851. }
  852. sd_str[0] = (char *)"_services";
  853. sd_str[1] = (char *)"_dns-sd";
  854. sd_str[2] = (char *)"_udp";
  855. sd_str[3] = MDNS_DEFAULT_DOMAIN;
  856. str[0] = service->service;
  857. str[1] = service->proto;
  858. str[2] = MDNS_DEFAULT_DOMAIN;
  859. part_length = _mdns_append_fqdn(packet, index, sd_str, 4, MDNS_MAX_PACKET_SIZE);
  860. record_length += part_length;
  861. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, flush, MDNS_ANSWER_PTR_TTL);
  862. if (!part_length) {
  863. return 0;
  864. }
  865. record_length += part_length;
  866. uint16_t data_len_location = *index - 2;
  867. part_length = _mdns_append_fqdn(packet, index, str, 3, MDNS_MAX_PACKET_SIZE);
  868. if (!part_length) {
  869. return 0;
  870. }
  871. _mdns_set_u16(packet, data_len_location, part_length);
  872. record_length += part_length;
  873. return record_length;
  874. }
  875. /**
  876. * @brief appends TXT record for service to a packet, incrementing the index
  877. *
  878. * @param packet MDNS packet
  879. * @param index offset in the packet
  880. * @param server the server that is hosting the service
  881. * @param service the service to add record for
  882. *
  883. * @return length of added data: 0 on error or length on success
  884. */
  885. static uint16_t _mdns_append_txt_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye)
  886. {
  887. const char *str[4];
  888. uint16_t record_length = 0;
  889. uint8_t part_length;
  890. if (service == NULL) {
  891. return 0;
  892. }
  893. str[0] = _mdns_get_service_instance_name(service);
  894. str[1] = service->service;
  895. str[2] = service->proto;
  896. str[3] = MDNS_DEFAULT_DOMAIN;
  897. if (!str[0]) {
  898. return 0;
  899. }
  900. part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE);
  901. if (!part_length) {
  902. return 0;
  903. }
  904. record_length += part_length;
  905. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_TXT, flush, bye ? 0 : MDNS_ANSWER_TXT_TTL);
  906. if (!part_length) {
  907. return 0;
  908. }
  909. record_length += part_length;
  910. uint16_t data_len_location = *index - 2;
  911. uint16_t data_len = 0;
  912. mdns_txt_linked_item_t *txt = service->txt;
  913. while (txt) {
  914. int l = append_one_txt_record_entry(packet, index, txt);
  915. if (l > 0) {
  916. data_len += l;
  917. } else if (l == 0) { // TXT entry won't fit into the mdns packet
  918. return 0;
  919. }
  920. txt = txt->next;
  921. }
  922. if (!data_len) {
  923. data_len = 1;
  924. packet[*index] = 0;
  925. *index = *index + 1;
  926. }
  927. _mdns_set_u16(packet, data_len_location, data_len);
  928. record_length += data_len;
  929. return record_length;
  930. }
  931. /**
  932. * @brief appends SRV record for service to a packet, incrementing the index
  933. *
  934. * @param packet MDNS packet
  935. * @param index offset in the packet
  936. * @param server the server that is hosting the service
  937. * @param service the service to add record for
  938. *
  939. * @return length of added data: 0 on error or length on success
  940. */
  941. static uint16_t _mdns_append_srv_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye)
  942. {
  943. const char *str[4];
  944. uint16_t record_length = 0;
  945. uint8_t part_length;
  946. if (service == NULL) {
  947. return 0;
  948. }
  949. str[0] = _mdns_get_service_instance_name(service);
  950. str[1] = service->service;
  951. str[2] = service->proto;
  952. str[3] = MDNS_DEFAULT_DOMAIN;
  953. if (!str[0]) {
  954. return 0;
  955. }
  956. part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE);
  957. if (!part_length) {
  958. return 0;
  959. }
  960. record_length += part_length;
  961. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_SRV, flush, bye ? 0 : MDNS_ANSWER_SRV_TTL);
  962. if (!part_length) {
  963. return 0;
  964. }
  965. record_length += part_length;
  966. uint16_t data_len_location = *index - 2;
  967. part_length = 0;
  968. part_length += _mdns_append_u16(packet, index, service->priority);
  969. part_length += _mdns_append_u16(packet, index, service->weight);
  970. part_length += _mdns_append_u16(packet, index, service->port);
  971. if (part_length != 6) {
  972. return 0;
  973. }
  974. if (service->hostname) {
  975. str[0] = service->hostname;
  976. } else {
  977. str[0] = _mdns_server->hostname;
  978. }
  979. str[1] = MDNS_DEFAULT_DOMAIN;
  980. if (_str_null_or_empty(str[0])) {
  981. return 0;
  982. }
  983. part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE);
  984. if (!part_length) {
  985. return 0;
  986. }
  987. _mdns_set_u16(packet, data_len_location, part_length + 6);
  988. record_length += part_length + 6;
  989. return record_length;
  990. }
  991. #ifdef CONFIG_LWIP_IPV4
  992. /**
  993. * @brief appends A record to a packet, incrementing the index
  994. *
  995. * @param packet MDNS packet
  996. * @param index offset in the packet
  997. * @param hostname the hostname address to add
  998. * @param ip the IP address to add
  999. *
  1000. * @return length of added data: 0 on error or length on success
  1001. */
  1002. static uint16_t _mdns_append_a_record(uint8_t *packet, uint16_t *index, const char *hostname, uint32_t ip, bool flush, bool bye)
  1003. {
  1004. const char *str[2];
  1005. uint16_t record_length = 0;
  1006. uint8_t part_length;
  1007. str[0] = hostname;
  1008. str[1] = MDNS_DEFAULT_DOMAIN;
  1009. if (_str_null_or_empty(str[0])) {
  1010. return 0;
  1011. }
  1012. part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE);
  1013. if (!part_length) {
  1014. return 0;
  1015. }
  1016. record_length += part_length;
  1017. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_A, flush, bye ? 0 : MDNS_ANSWER_A_TTL);
  1018. if (!part_length) {
  1019. return 0;
  1020. }
  1021. record_length += part_length;
  1022. uint16_t data_len_location = *index - 2;
  1023. if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
  1024. return 0;
  1025. }
  1026. _mdns_append_u8(packet, index, ip & 0xFF);
  1027. _mdns_append_u8(packet, index, (ip >> 8) & 0xFF);
  1028. _mdns_append_u8(packet, index, (ip >> 16) & 0xFF);
  1029. _mdns_append_u8(packet, index, (ip >> 24) & 0xFF);
  1030. _mdns_set_u16(packet, data_len_location, 4);
  1031. record_length += 4;
  1032. return record_length;
  1033. }
  1034. #endif /* CONFIG_LWIP_IPV4 */
  1035. #ifdef CONFIG_LWIP_IPV6
  1036. /**
  1037. * @brief appends AAAA record to a packet, incrementing the index
  1038. *
  1039. * @param packet MDNS packet
  1040. * @param index offset in the packet
  1041. * @param hostname the hostname address to add
  1042. * @param ipv6 the IPv6 address to add
  1043. *
  1044. * @return length of added data: 0 on error or length on success
  1045. */
  1046. static uint16_t _mdns_append_aaaa_record(uint8_t *packet, uint16_t *index, const char *hostname, uint8_t *ipv6, bool flush, bool bye)
  1047. {
  1048. const char *str[2];
  1049. uint16_t record_length = 0;
  1050. uint8_t part_length;
  1051. str[0] = hostname;
  1052. str[1] = MDNS_DEFAULT_DOMAIN;
  1053. if (_str_null_or_empty(str[0])) {
  1054. return 0;
  1055. }
  1056. part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE);
  1057. if (!part_length) {
  1058. return 0;
  1059. }
  1060. record_length += part_length;
  1061. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_AAAA, flush, bye ? 0 : MDNS_ANSWER_AAAA_TTL);
  1062. if (!part_length) {
  1063. return 0;
  1064. }
  1065. record_length += part_length;
  1066. uint16_t data_len_location = *index - 2;
  1067. if ((*index + MDNS_ANSWER_AAAA_SIZE) > MDNS_MAX_PACKET_SIZE) {
  1068. return 0;
  1069. }
  1070. part_length = MDNS_ANSWER_AAAA_SIZE;
  1071. memcpy(packet + *index, ipv6, part_length);
  1072. *index += part_length;
  1073. _mdns_set_u16(packet, data_len_location, part_length);
  1074. record_length += part_length;
  1075. return record_length;
  1076. }
  1077. #endif
  1078. /**
  1079. * @brief Append question to packet
  1080. */
  1081. static uint16_t _mdns_append_question(uint8_t *packet, uint16_t *index, mdns_out_question_t *q)
  1082. {
  1083. uint8_t part_length;
  1084. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  1085. if (q->host && (strstr(q->host, "in-addr") || strstr(q->host, "ip6"))) {
  1086. part_length = append_fqdn_dots(packet, index, q->host, false);
  1087. if (!part_length) {
  1088. return 0;
  1089. }
  1090. } else
  1091. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  1092. {
  1093. const char *str[4];
  1094. uint8_t str_index = 0;
  1095. if (q->host) {
  1096. str[str_index++] = q->host;
  1097. }
  1098. if (q->service) {
  1099. str[str_index++] = q->service;
  1100. }
  1101. if (q->proto) {
  1102. str[str_index++] = q->proto;
  1103. }
  1104. if (q->domain) {
  1105. str[str_index++] = q->domain;
  1106. }
  1107. part_length = _mdns_append_fqdn(packet, index, str, str_index, MDNS_MAX_PACKET_SIZE);
  1108. if (!part_length) {
  1109. return 0;
  1110. }
  1111. }
  1112. part_length += _mdns_append_u16(packet, index, q->type);
  1113. part_length += _mdns_append_u16(packet, index, q->unicast ? 0x8001 : 0x0001);
  1114. return part_length;
  1115. }
  1116. /**
  1117. * @brief Helper to get either ETH or STA if the other is provided
  1118. * Used when two interfaces are on the same subnet
  1119. */
  1120. static mdns_if_t _mdns_get_other_if (mdns_if_t tcpip_if)
  1121. {
  1122. if (tcpip_if < MDNS_MAX_INTERFACES) {
  1123. return s_esp_netifs[tcpip_if].duplicate;
  1124. }
  1125. return MDNS_MAX_INTERFACES;
  1126. }
  1127. /**
  1128. * @brief Check if interface is duplicate (two interfaces on the same subnet)
  1129. */
  1130. static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
  1131. {
  1132. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  1133. if (other_if == MDNS_MAX_INTERFACES) {
  1134. return false;
  1135. }
  1136. if (_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP
  1137. || _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP
  1138. || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP
  1139. || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP
  1140. ) {
  1141. return true;
  1142. }
  1143. return false;
  1144. }
  1145. #ifdef CONFIG_LWIP_IPV6
  1146. /**
  1147. * @brief Check if IPv6 address is NULL
  1148. */
  1149. static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
  1150. {
  1151. uint8_t i;
  1152. uint8_t *data = (uint8_t *)ip6.addr;
  1153. for (i = 0; i < _MDNS_SIZEOF_IP6_ADDR; i++) {
  1154. if (data[i]) {
  1155. return false;
  1156. }
  1157. }
  1158. return true;
  1159. }
  1160. #endif /* CONFIG_LWIP_IPV6 */
  1161. static uint8_t _mdns_append_host_answer(uint8_t *packet, uint16_t *index, mdns_host_item_t *host,
  1162. uint8_t address_type, bool flush, bool bye)
  1163. {
  1164. mdns_ip_addr_t *addr = host->address_list;
  1165. uint8_t num_records = 0;
  1166. while (addr != NULL) {
  1167. if (addr->addr.type == address_type) {
  1168. #ifdef CONFIG_LWIP_IPV4
  1169. if (address_type == ESP_IPADDR_TYPE_V4 &&
  1170. _mdns_append_a_record(packet, index, host->hostname, addr->addr.u_addr.ip4.addr, flush, bye) <= 0) {
  1171. break;
  1172. }
  1173. #endif /* CONFIG_LWIP_IPV4 */
  1174. #ifdef CONFIG_LWIP_IPV6
  1175. if (address_type == ESP_IPADDR_TYPE_V6 &&
  1176. _mdns_append_aaaa_record(packet, index, host->hostname, (uint8_t *)addr->addr.u_addr.ip6.addr, flush,
  1177. bye) <= 0) {
  1178. break;
  1179. }
  1180. #endif /* CONFIG_LWIP_IPV6 */
  1181. num_records++;
  1182. }
  1183. addr = addr->next;
  1184. }
  1185. return num_records;
  1186. }
  1187. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  1188. /**
  1189. * @brief Appends reverse lookup PTR record
  1190. */
  1191. static uint8_t _mdns_append_reverse_ptr_record(uint8_t *packet, uint16_t *index, const char *name)
  1192. {
  1193. if (strstr(name, "in-addr") == NULL && strstr(name, "ip6") == NULL) {
  1194. return 0;
  1195. }
  1196. if (!append_fqdn_dots(packet, index, name, false)) {
  1197. return 0;
  1198. }
  1199. if (!_mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, 10 /* TTL set to 10s*/ )) {
  1200. return 0;
  1201. }
  1202. uint16_t data_len_location = *index - 2; /* store the position of size (2=16bis) of this record */
  1203. const char *str[2] = { _mdns_self_host.hostname, MDNS_DEFAULT_DOMAIN };
  1204. int part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE);
  1205. if (!part_length) {
  1206. return 0;
  1207. }
  1208. _mdns_set_u16(packet, data_len_location, part_length);
  1209. return 1; /* appending only 1 record */
  1210. }
  1211. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  1212. /**
  1213. * @brief Append PTR answers to packet
  1214. *
  1215. * @return number of answers added to the packet
  1216. */
  1217. static uint8_t _mdns_append_service_ptr_answers(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush,
  1218. bool bye)
  1219. {
  1220. uint8_t appended_answers = 0;
  1221. if (_mdns_append_ptr_record(packet, index, _mdns_get_service_instance_name(service), service->service,
  1222. service->proto, flush, bye) <= 0) {
  1223. return appended_answers;
  1224. }
  1225. appended_answers++;
  1226. mdns_subtype_t *subtype = service->subtype;
  1227. while (subtype) {
  1228. appended_answers +=
  1229. (_mdns_append_subtype_ptr_record(packet, index, _mdns_get_service_instance_name(service), subtype->subtype,
  1230. service->service, service->proto, flush, bye) > 0);
  1231. subtype = subtype->next;
  1232. }
  1233. return appended_answers;
  1234. }
  1235. /**
  1236. * @brief Append answer to packet
  1237. *
  1238. * @return number of answers added to the packet
  1239. */
  1240. static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_answer_t *answer, mdns_if_t tcpip_if)
  1241. {
  1242. if (answer->host) {
  1243. bool is_host_valid = (&_mdns_self_host == answer->host);
  1244. mdns_host_item_t *target_host = _mdns_host_list;
  1245. while (target_host && !is_host_valid) {
  1246. if (target_host == answer->host) {
  1247. is_host_valid = true;
  1248. }
  1249. target_host = target_host->next;
  1250. }
  1251. if (!is_host_valid) {
  1252. return 0;
  1253. }
  1254. }
  1255. if (answer->type == MDNS_TYPE_PTR) {
  1256. if (answer->service) {
  1257. return _mdns_append_service_ptr_answers(packet, index, answer->service, answer->flush, answer->bye);
  1258. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  1259. } else if (answer->host && answer->host->hostname &&
  1260. (strstr(answer->host->hostname, "in-addr") || strstr(answer->host->hostname, "ip6"))) {
  1261. return _mdns_append_reverse_ptr_record(packet, index, answer->host->hostname) > 0;
  1262. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  1263. } else {
  1264. return _mdns_append_ptr_record(packet, index,
  1265. answer->custom_instance, answer->custom_service, answer->custom_proto,
  1266. answer->flush, answer->bye) > 0;
  1267. }
  1268. } else if (answer->type == MDNS_TYPE_SRV) {
  1269. return _mdns_append_srv_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
  1270. } else if (answer->type == MDNS_TYPE_TXT) {
  1271. return _mdns_append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
  1272. } else if (answer->type == MDNS_TYPE_SDPTR) {
  1273. return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
  1274. }
  1275. #ifdef CONFIG_LWIP_IPV4
  1276. else if (answer->type == MDNS_TYPE_A) {
  1277. if (answer->host == &_mdns_self_host) {
  1278. esp_netif_ip_info_t if_ip_info;
  1279. if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V4) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) {
  1280. return 0;
  1281. }
  1282. if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
  1283. return 0;
  1284. }
  1285. if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) <= 0) {
  1286. return 0;
  1287. }
  1288. if (!_mdns_if_is_dup(tcpip_if)) {
  1289. return 1;
  1290. }
  1291. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  1292. if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &if_ip_info)) {
  1293. return 1;
  1294. }
  1295. if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) > 0) {
  1296. return 2;
  1297. }
  1298. return 1;
  1299. } else if (answer->host != NULL) {
  1300. return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye);
  1301. }
  1302. }
  1303. #endif /* CONFIG_LWIP_IPV4 */
  1304. #ifdef CONFIG_LWIP_IPV6
  1305. else if (answer->type == MDNS_TYPE_AAAA) {
  1306. if (answer->host == &_mdns_self_host) {
  1307. struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS];
  1308. uint8_t count = 0;
  1309. if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V6) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) {
  1310. return 0;
  1311. }
  1312. count = esp_netif_get_all_ip6(_mdns_get_esp_netif(tcpip_if), if_ip6s);
  1313. assert(count <= NETIF_IPV6_MAX_NUMS);
  1314. for (int i = 0; i < count; i++) {
  1315. if (_ipv6_address_is_zero(if_ip6s[i])) {
  1316. return 0;
  1317. }
  1318. if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)if_ip6s[i].addr,
  1319. answer->flush, answer->bye) <= 0) {
  1320. return 0;
  1321. }
  1322. }
  1323. if (!_mdns_if_is_dup(tcpip_if)) {
  1324. return count;
  1325. }
  1326. mdns_if_t other_if = _mdns_get_other_if(tcpip_if);
  1327. struct esp_ip6_addr other_ip6;
  1328. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) {
  1329. return count;
  1330. }
  1331. if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)other_ip6.addr,
  1332. answer->flush, answer->bye) > 0) {
  1333. return 1 + count;
  1334. }
  1335. return count;
  1336. } else if (answer->host != NULL) {
  1337. return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush,
  1338. answer->bye);
  1339. }
  1340. }
  1341. #endif /* CONFIG_LWIP_IPV6 */
  1342. return 0;
  1343. }
  1344. /**
  1345. * @brief sends a packet
  1346. *
  1347. * @param p the packet
  1348. */
  1349. static void _mdns_dispatch_tx_packet(mdns_tx_packet_t *p)
  1350. {
  1351. static uint8_t packet[MDNS_MAX_PACKET_SIZE];
  1352. uint16_t index = MDNS_HEAD_LEN;
  1353. memset(packet, 0, MDNS_HEAD_LEN);
  1354. mdns_out_question_t *q;
  1355. mdns_out_answer_t *a;
  1356. uint8_t count;
  1357. _mdns_set_u16(packet, MDNS_HEAD_FLAGS_OFFSET, p->flags);
  1358. _mdns_set_u16(packet, MDNS_HEAD_ID_OFFSET, p->id);
  1359. count = 0;
  1360. q = p->questions;
  1361. while (q) {
  1362. if (_mdns_append_question(packet, &index, q)) {
  1363. count++;
  1364. }
  1365. q = q->next;
  1366. }
  1367. _mdns_set_u16(packet, MDNS_HEAD_QUESTIONS_OFFSET, count);
  1368. count = 0;
  1369. a = p->answers;
  1370. while (a) {
  1371. count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
  1372. a = a->next;
  1373. }
  1374. _mdns_set_u16(packet, MDNS_HEAD_ANSWERS_OFFSET, count);
  1375. count = 0;
  1376. a = p->servers;
  1377. while (a) {
  1378. count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
  1379. a = a->next;
  1380. }
  1381. _mdns_set_u16(packet, MDNS_HEAD_SERVERS_OFFSET, count);
  1382. count = 0;
  1383. a = p->additional;
  1384. while (a) {
  1385. count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
  1386. a = a->next;
  1387. }
  1388. _mdns_set_u16(packet, MDNS_HEAD_ADDITIONAL_OFFSET, count);
  1389. #ifdef MDNS_ENABLE_DEBUG
  1390. _mdns_dbg_printf("\nTX[%lu][%lu]: ", (unsigned long)p->tcpip_if, (unsigned long)p->ip_protocol);
  1391. #ifdef CONFIG_LWIP_IPV4
  1392. if (p->dst.type == ESP_IPADDR_TYPE_V4) {
  1393. _mdns_dbg_printf("To: " IPSTR ":%u, ", IP2STR(&p->dst.u_addr.ip4), p->port);
  1394. }
  1395. #endif
  1396. #ifdef CONFIG_LWIP_IPV6
  1397. if (p->dst.type == ESP_IPADDR_TYPE_V6) {
  1398. _mdns_dbg_printf("To: " IPV6STR ":%u, ", IPV62STR(p->dst.u_addr.ip6), p->port);
  1399. }
  1400. #endif
  1401. mdns_debug_packet(packet, index);
  1402. #endif
  1403. _mdns_udp_pcb_write(p->tcpip_if, p->ip_protocol, &p->dst, p->port, packet, index);
  1404. }
  1405. /**
  1406. * @brief frees a packet
  1407. *
  1408. * @param packet the packet
  1409. */
  1410. static void _mdns_free_tx_packet(mdns_tx_packet_t *packet)
  1411. {
  1412. if (!packet) {
  1413. return;
  1414. }
  1415. mdns_out_question_t *q = packet->questions;
  1416. while (q) {
  1417. mdns_out_question_t *next = q->next;
  1418. if (q->own_dynamic_memory) {
  1419. free((char *)q->host);
  1420. free((char *)q->service);
  1421. free((char *)q->proto);
  1422. free((char *)q->domain);
  1423. }
  1424. free(q);
  1425. q = next;
  1426. }
  1427. queueFree(mdns_out_answer_t, packet->answers);
  1428. queueFree(mdns_out_answer_t, packet->servers);
  1429. queueFree(mdns_out_answer_t, packet->additional);
  1430. free(packet);
  1431. }
  1432. /**
  1433. * @brief schedules a packet to be sent after given milliseconds
  1434. *
  1435. * @param packet the packet
  1436. * @param ms_after number of milliseconds after which the packet should be dispatched
  1437. */
  1438. static void _mdns_schedule_tx_packet(mdns_tx_packet_t *packet, uint32_t ms_after)
  1439. {
  1440. if (!packet) {
  1441. return;
  1442. }
  1443. packet->send_at = (xTaskGetTickCount() * portTICK_PERIOD_MS) + ms_after;
  1444. packet->next = NULL;
  1445. if (!_mdns_server->tx_queue_head || _mdns_server->tx_queue_head->send_at > packet->send_at) {
  1446. packet->next = _mdns_server->tx_queue_head;
  1447. _mdns_server->tx_queue_head = packet;
  1448. return;
  1449. }
  1450. mdns_tx_packet_t *q = _mdns_server->tx_queue_head;
  1451. while (q->next && q->next->send_at <= packet->send_at) {
  1452. q = q->next;
  1453. }
  1454. packet->next = q->next;
  1455. q->next = packet;
  1456. }
  1457. /**
  1458. * @brief free all packets scheduled for sending
  1459. */
  1460. static void _mdns_clear_tx_queue_head(void)
  1461. {
  1462. mdns_tx_packet_t *q;
  1463. while (_mdns_server->tx_queue_head) {
  1464. q = _mdns_server->tx_queue_head;
  1465. _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next;
  1466. _mdns_free_tx_packet(q);
  1467. }
  1468. }
  1469. /**
  1470. * @brief clear packets scheduled for sending on a specific interface
  1471. *
  1472. * @param tcpip_if the interface
  1473. * @param ip_protocol pcb type V4/V6
  1474. */
  1475. static void _mdns_clear_pcb_tx_queue_head(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1476. {
  1477. mdns_tx_packet_t *q, * p;
  1478. while (_mdns_server->tx_queue_head && _mdns_server->tx_queue_head->tcpip_if == tcpip_if && _mdns_server->tx_queue_head->ip_protocol == ip_protocol) {
  1479. q = _mdns_server->tx_queue_head;
  1480. _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next;
  1481. _mdns_free_tx_packet(q);
  1482. }
  1483. if (_mdns_server->tx_queue_head) {
  1484. q = _mdns_server->tx_queue_head;
  1485. while (q->next) {
  1486. if (q->next->tcpip_if == tcpip_if && q->next->ip_protocol == ip_protocol) {
  1487. p = q->next;
  1488. q->next = p->next;
  1489. _mdns_free_tx_packet(p);
  1490. } else {
  1491. q = q->next;
  1492. }
  1493. }
  1494. }
  1495. }
  1496. /**
  1497. * @brief get the next packet scheduled for sending on a specific interface
  1498. *
  1499. * @param tcpip_if the interface
  1500. * @param ip_protocol pcb type V4/V6
  1501. */
  1502. static mdns_tx_packet_t *_mdns_get_next_pcb_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1503. {
  1504. mdns_tx_packet_t *q = _mdns_server->tx_queue_head;
  1505. while (q) {
  1506. if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol) {
  1507. return q;
  1508. }
  1509. q = q->next;
  1510. }
  1511. return NULL;
  1512. }
  1513. /**
  1514. * @brief Find, remove and free answer from the scheduled packets
  1515. */
  1516. static void _mdns_remove_scheduled_answer(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint16_t type, mdns_srv_item_t *service)
  1517. {
  1518. mdns_srv_item_t s = {NULL, NULL};
  1519. if (!service) {
  1520. service = &s;
  1521. }
  1522. mdns_tx_packet_t *q = _mdns_server->tx_queue_head;
  1523. while (q) {
  1524. if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol && q->distributed) {
  1525. mdns_out_answer_t *a = q->answers;
  1526. if (a) {
  1527. if (a->type == type && a->service == service->service) {
  1528. q->answers = q->answers->next;
  1529. free(a);
  1530. } else {
  1531. while (a->next) {
  1532. if (a->next->type == type && a->next->service == service->service) {
  1533. mdns_out_answer_t *b = a->next;
  1534. a->next = b->next;
  1535. free(b);
  1536. break;
  1537. }
  1538. a = a->next;
  1539. }
  1540. }
  1541. }
  1542. }
  1543. q = q->next;
  1544. }
  1545. }
  1546. /**
  1547. * @brief Remove and free answer from answer list (destination)
  1548. */
  1549. static void _mdns_dealloc_answer(mdns_out_answer_t **destination, uint16_t type, mdns_srv_item_t *service)
  1550. {
  1551. mdns_out_answer_t *d = *destination;
  1552. if (!d) {
  1553. return;
  1554. }
  1555. mdns_srv_item_t s = {NULL, NULL};
  1556. if (!service) {
  1557. service = &s;
  1558. }
  1559. if (d->type == type && d->service == service->service) {
  1560. *destination = d->next;
  1561. free(d);
  1562. return;
  1563. }
  1564. while (d->next) {
  1565. mdns_out_answer_t *a = d->next;
  1566. if (a->type == type && a->service == service->service) {
  1567. d->next = a->next;
  1568. free(a);
  1569. return;
  1570. }
  1571. d = d->next;
  1572. }
  1573. }
  1574. /**
  1575. * @brief Allocate new answer and add it to answer list (destination)
  1576. */
  1577. static bool _mdns_alloc_answer(mdns_out_answer_t **destination, uint16_t type, mdns_service_t *service,
  1578. mdns_host_item_t *host, bool flush, bool bye)
  1579. {
  1580. mdns_out_answer_t *d = *destination;
  1581. while (d) {
  1582. if (d->type == type && d->service == service && d->host == host) {
  1583. return true;
  1584. }
  1585. d = d->next;
  1586. }
  1587. mdns_out_answer_t *a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
  1588. if (!a) {
  1589. HOOK_MALLOC_FAILED;
  1590. return false;
  1591. }
  1592. a->type = type;
  1593. a->service = service;
  1594. a->host = host;
  1595. a->custom_service = NULL;
  1596. a->bye = bye;
  1597. a->flush = flush;
  1598. a->next = NULL;
  1599. queueToEnd(mdns_out_answer_t, *destination, a);
  1600. return true;
  1601. }
  1602. /**
  1603. * @brief Allocate new packet for sending
  1604. */
  1605. static mdns_tx_packet_t *_mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1606. {
  1607. mdns_tx_packet_t *packet = (mdns_tx_packet_t *)malloc(sizeof(mdns_tx_packet_t));
  1608. if (!packet) {
  1609. HOOK_MALLOC_FAILED;
  1610. return NULL;
  1611. }
  1612. memset((uint8_t *)packet, 0, sizeof(mdns_tx_packet_t));
  1613. packet->tcpip_if = tcpip_if;
  1614. packet->ip_protocol = ip_protocol;
  1615. packet->port = MDNS_SERVICE_PORT;
  1616. #ifdef CONFIG_LWIP_IPV4
  1617. if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
  1618. esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251);
  1619. memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
  1620. }
  1621. #endif
  1622. #ifdef CONFIG_LWIP_IPV6
  1623. if (ip_protocol == MDNS_IP_PROTOCOL_V6) {
  1624. esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000);
  1625. memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
  1626. }
  1627. #endif
  1628. return packet;
  1629. }
  1630. static bool _mdns_create_answer_from_service(mdns_tx_packet_t *packet, mdns_service_t *service,
  1631. mdns_parsed_question_t *question, bool shared, bool send_flush)
  1632. {
  1633. mdns_host_item_t *host = mdns_get_host_item(service->hostname);
  1634. bool is_delegated = (host != &_mdns_self_host);
  1635. if (question->type == MDNS_TYPE_PTR || question->type == MDNS_TYPE_ANY) {
  1636. // According to RFC6763-section12.1, for DNS-SD, SRV, TXT and all address records
  1637. // should be included in additional records.
  1638. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) ||
  1639. !_mdns_alloc_answer(is_delegated ? &packet->additional : &packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
  1640. !_mdns_alloc_answer(is_delegated ? &packet->additional : &packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false) ||
  1641. !_mdns_alloc_answer((shared || is_delegated) ? &packet->additional : &packet->answers, MDNS_TYPE_A, service, host, send_flush,
  1642. false) ||
  1643. !_mdns_alloc_answer((shared || is_delegated) ? &packet->additional : &packet->answers, MDNS_TYPE_AAAA, service, host,
  1644. send_flush, false)) {
  1645. return false;
  1646. }
  1647. } else if (question->type == MDNS_TYPE_SRV) {
  1648. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
  1649. !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_A, service, host, send_flush, false) ||
  1650. !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_AAAA, service, host, send_flush, false)) {
  1651. return false;
  1652. }
  1653. } else if (question->type == MDNS_TYPE_TXT) {
  1654. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false)) {
  1655. return false;
  1656. }
  1657. } else if (question->type == MDNS_TYPE_SDPTR) {
  1658. shared = true;
  1659. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, service, NULL, false, false)) {
  1660. return false;
  1661. }
  1662. }
  1663. return true;
  1664. }
  1665. static bool _mdns_create_answer_from_hostname(mdns_tx_packet_t *packet, const char *hostname, bool send_flush)
  1666. {
  1667. mdns_host_item_t *host = mdns_get_host_item(hostname);
  1668. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_A, NULL, host, send_flush, false) ||
  1669. !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, send_flush, false)) {
  1670. return false;
  1671. }
  1672. return true;
  1673. }
  1674. static bool _mdns_service_match_ptr_question(const mdns_service_t *service, const mdns_parsed_question_t *question)
  1675. {
  1676. if (!_mdns_service_match(service, question->service, question->proto, NULL)) {
  1677. return false;
  1678. }
  1679. // The question parser stores anything before _type._proto in question->host
  1680. // So the question->host can be subtype or instance name based on its content
  1681. if (question->sub) {
  1682. mdns_subtype_t *subtype = service->subtype;
  1683. while (subtype) {
  1684. if (!strcasecmp(subtype->subtype, question->host)) {
  1685. return true;
  1686. }
  1687. subtype = subtype->next;
  1688. }
  1689. return false;
  1690. }
  1691. if (question->host) {
  1692. if (strcasecmp(_mdns_get_service_instance_name(service), question->host) != 0) {
  1693. return false;
  1694. }
  1695. }
  1696. return true;
  1697. }
  1698. /**
  1699. * @brief Create answer packet to questions from parsed packet
  1700. */
  1701. static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_packet)
  1702. {
  1703. if (!parsed_packet->questions) {
  1704. return;
  1705. }
  1706. bool send_flush = parsed_packet->src_port == MDNS_SERVICE_PORT;
  1707. bool unicast = false;
  1708. bool shared = false;
  1709. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(parsed_packet->tcpip_if, parsed_packet->ip_protocol);
  1710. if (!packet) {
  1711. return;
  1712. }
  1713. packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
  1714. packet->distributed = parsed_packet->distributed;
  1715. packet->id = parsed_packet->id;
  1716. mdns_parsed_question_t *q = parsed_packet->questions;
  1717. uint32_t out_record_nums = 0;
  1718. while (q) {
  1719. shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe;
  1720. if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) {
  1721. mdns_srv_item_t *service = _mdns_get_service_item_instance(q->host, q->service, q->proto, NULL);
  1722. if (service == NULL || !_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
  1723. _mdns_free_tx_packet(packet);
  1724. return;
  1725. } else {
  1726. out_record_nums++;
  1727. }
  1728. } else if (q->service && q->proto) {
  1729. mdns_srv_item_t *service = _mdns_server->services;
  1730. while (service) {
  1731. if (_mdns_service_match_ptr_question(service->service, q)) {
  1732. mdns_parsed_record_t *r = parsed_packet->records;
  1733. bool is_record_exist = false;
  1734. while (r) {
  1735. if (service->service->instance && r->host) {
  1736. if (_mdns_service_match_instance(service->service, r->host, r->service, r->proto, NULL) && r->ttl > (MDNS_ANSWER_PTR_TTL / 2)) {
  1737. is_record_exist = true;
  1738. break;
  1739. }
  1740. } else if (!service->service->instance && !r->host) {
  1741. if (_mdns_service_match(service->service, r->service, r->proto, NULL) && r->ttl > (MDNS_ANSWER_PTR_TTL / 2)) {
  1742. is_record_exist = true;
  1743. break;
  1744. }
  1745. }
  1746. r = r->next;
  1747. }
  1748. if (!is_record_exist) {
  1749. if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
  1750. _mdns_free_tx_packet(packet);
  1751. return;
  1752. } else {
  1753. out_record_nums++;
  1754. }
  1755. }
  1756. }
  1757. service = service->next;
  1758. }
  1759. } else if (q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA) {
  1760. if (!_mdns_create_answer_from_hostname(packet, q->host, send_flush)) {
  1761. _mdns_free_tx_packet(packet);
  1762. return;
  1763. } else {
  1764. out_record_nums++;
  1765. }
  1766. } else if (q->type == MDNS_TYPE_ANY) {
  1767. if (!_mdns_append_host_list(&packet->answers, send_flush, false)) {
  1768. _mdns_free_tx_packet(packet);
  1769. return;
  1770. } else {
  1771. out_record_nums++;
  1772. }
  1773. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  1774. } else if (q->type == MDNS_TYPE_PTR) {
  1775. mdns_host_item_t *host = mdns_get_host_item(q->host);
  1776. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, NULL, host, send_flush, false)) {
  1777. _mdns_free_tx_packet(packet);
  1778. return;
  1779. } else {
  1780. out_record_nums++;
  1781. }
  1782. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  1783. } else if (!_mdns_alloc_answer(&packet->answers, q->type, NULL, NULL, send_flush, false)) {
  1784. _mdns_free_tx_packet(packet);
  1785. return;
  1786. } else {
  1787. out_record_nums++;
  1788. }
  1789. if (parsed_packet->src_port != MDNS_SERVICE_PORT && // Repeat the queries only for "One-Shot mDNS queries"
  1790. (q->type == MDNS_TYPE_ANY || q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA
  1791. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  1792. || q->type == MDNS_TYPE_PTR
  1793. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  1794. )) {
  1795. mdns_out_question_t *out_question = malloc(sizeof(mdns_out_question_t));
  1796. if (out_question == NULL) {
  1797. HOOK_MALLOC_FAILED;
  1798. _mdns_free_tx_packet(packet);
  1799. return;
  1800. }
  1801. out_question->type = q->type;
  1802. out_question->unicast = q->unicast;
  1803. out_question->host = q->host;
  1804. q->host = NULL;
  1805. out_question->service = q->service;
  1806. q->service = NULL;
  1807. out_question->proto = q->proto;
  1808. q->proto = NULL;
  1809. out_question->domain = q->domain;
  1810. q->domain = NULL;
  1811. out_question->next = NULL;
  1812. out_question->own_dynamic_memory = true;
  1813. queueToEnd(mdns_out_question_t, packet->questions, out_question);
  1814. }
  1815. if (q->unicast) {
  1816. unicast = true;
  1817. }
  1818. q = q->next;
  1819. }
  1820. if (out_record_nums == 0) {
  1821. _mdns_free_tx_packet(packet);
  1822. return;
  1823. }
  1824. if (unicast || !send_flush) {
  1825. memcpy(&packet->dst, &parsed_packet->src, sizeof(esp_ip_addr_t));
  1826. packet->port = parsed_packet->src_port;
  1827. }
  1828. static uint8_t share_step = 0;
  1829. if (shared) {
  1830. _mdns_schedule_tx_packet(packet, 25 + (share_step * 25));
  1831. share_step = (share_step + 1) & 0x03;
  1832. } else {
  1833. _mdns_dispatch_tx_packet(packet);
  1834. _mdns_free_tx_packet(packet);
  1835. }
  1836. }
  1837. /**
  1838. * @brief Check if question is already in the list
  1839. */
  1840. static bool _mdns_question_exists(mdns_out_question_t *needle, mdns_out_question_t *haystack)
  1841. {
  1842. while (haystack) {
  1843. if (haystack->type == needle->type
  1844. && haystack->host == needle->host
  1845. && haystack->service == needle->service
  1846. && haystack->proto == needle->proto) {
  1847. return true;
  1848. }
  1849. haystack = haystack->next;
  1850. }
  1851. return false;
  1852. }
  1853. static bool _mdns_append_host(mdns_out_answer_t **destination, mdns_host_item_t *host, bool flush, bool bye)
  1854. {
  1855. if (!_mdns_alloc_answer(destination, MDNS_TYPE_A, NULL, host, flush, bye)) {
  1856. return false;
  1857. }
  1858. if (!_mdns_alloc_answer(destination, MDNS_TYPE_AAAA, NULL, host, flush, bye)) {
  1859. return false;
  1860. }
  1861. return true;
  1862. }
  1863. static bool _mdns_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[],
  1864. size_t services_len, bool flush, bool bye)
  1865. {
  1866. if (services == NULL) {
  1867. mdns_host_item_t *host = mdns_get_host_item(_mdns_server->hostname);
  1868. if (host != NULL) {
  1869. return _mdns_append_host(destination, host, flush, bye);
  1870. }
  1871. return true;
  1872. }
  1873. for (size_t i = 0; i < services_len; i++) {
  1874. mdns_host_item_t *host = mdns_get_host_item(services[i]->service->hostname);
  1875. if (!_mdns_append_host(destination, host, flush, bye)) {
  1876. return false;
  1877. }
  1878. }
  1879. return true;
  1880. }
  1881. static bool _mdns_append_host_list(mdns_out_answer_t **destination, bool flush, bool bye)
  1882. {
  1883. if (!_str_null_or_empty(_mdns_server->hostname)) {
  1884. mdns_host_item_t *self_host = mdns_get_host_item(_mdns_server->hostname);
  1885. if (!_mdns_append_host(destination, self_host, flush, bye)) {
  1886. return false;
  1887. }
  1888. }
  1889. mdns_host_item_t *host = _mdns_host_list;
  1890. while (host != NULL) {
  1891. host = host->next;
  1892. if (!_mdns_append_host(destination, host, flush, bye)) {
  1893. return false;
  1894. }
  1895. }
  1896. return true;
  1897. }
  1898. static bool _mdns_append_host_question(mdns_out_question_t **questions, const char *hostname, bool unicast)
  1899. {
  1900. mdns_out_question_t *q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
  1901. if (!q) {
  1902. HOOK_MALLOC_FAILED;
  1903. return false;
  1904. }
  1905. q->next = NULL;
  1906. q->unicast = unicast;
  1907. q->type = MDNS_TYPE_ANY;
  1908. q->host = hostname;
  1909. q->service = NULL;
  1910. q->proto = NULL;
  1911. q->domain = MDNS_DEFAULT_DOMAIN;
  1912. q->own_dynamic_memory = false;
  1913. if (_mdns_question_exists(q, *questions)) {
  1914. free(q);
  1915. } else {
  1916. queueToEnd(mdns_out_question_t, *questions, q);
  1917. }
  1918. return true;
  1919. }
  1920. static bool _mdns_append_host_questions_for_services(mdns_out_question_t **questions, mdns_srv_item_t *services[],
  1921. size_t len, bool unicast)
  1922. {
  1923. if (!_str_null_or_empty(_mdns_server->hostname) &&
  1924. !_mdns_append_host_question(questions, _mdns_server->hostname, unicast)) {
  1925. return false;
  1926. }
  1927. for (size_t i = 0; i < len; i++) {
  1928. if (!_mdns_append_host_question(questions, services[i]->service->hostname, unicast)) {
  1929. return false;
  1930. }
  1931. }
  1932. return true;
  1933. }
  1934. /**
  1935. * @brief Create probe packet for particular services on particular PCB
  1936. */
  1937. static mdns_tx_packet_t *_mdns_create_probe_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool first, bool include_ip)
  1938. {
  1939. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  1940. if (!packet) {
  1941. return NULL;
  1942. }
  1943. size_t i;
  1944. for (i = 0; i < len; i++) {
  1945. mdns_out_question_t *q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
  1946. if (!q) {
  1947. HOOK_MALLOC_FAILED;
  1948. _mdns_free_tx_packet(packet);
  1949. return NULL;
  1950. }
  1951. q->next = NULL;
  1952. q->unicast = first;
  1953. q->type = MDNS_TYPE_ANY;
  1954. q->host = _mdns_get_service_instance_name(services[i]->service);
  1955. q->service = services[i]->service->service;
  1956. q->proto = services[i]->service->proto;
  1957. q->domain = MDNS_DEFAULT_DOMAIN;
  1958. q->own_dynamic_memory = false;
  1959. if (!q->host || _mdns_question_exists(q, packet->questions)) {
  1960. free(q);
  1961. continue;
  1962. } else {
  1963. queueToEnd(mdns_out_question_t, packet->questions, q);
  1964. }
  1965. if (!q->host || !_mdns_alloc_answer(&packet->servers, MDNS_TYPE_SRV, services[i]->service, NULL, false, false)) {
  1966. _mdns_free_tx_packet(packet);
  1967. return NULL;
  1968. }
  1969. }
  1970. if (include_ip) {
  1971. if (!_mdns_append_host_questions_for_services(&packet->questions, services, len, first)) {
  1972. _mdns_free_tx_packet(packet);
  1973. return NULL;
  1974. }
  1975. if (!_mdns_append_host_list_in_services(&packet->servers, services, len, false, false)) {
  1976. _mdns_free_tx_packet(packet);
  1977. return NULL;
  1978. }
  1979. }
  1980. return packet;
  1981. }
  1982. /**
  1983. * @brief Create announce packet for particular services on particular PCB
  1984. */
  1985. static mdns_tx_packet_t *_mdns_create_announce_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool include_ip)
  1986. {
  1987. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  1988. if (!packet) {
  1989. return NULL;
  1990. }
  1991. packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
  1992. uint8_t i;
  1993. for (i = 0; i < len; i++) {
  1994. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false)
  1995. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false)
  1996. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false)
  1997. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) {
  1998. _mdns_free_tx_packet(packet);
  1999. return NULL;
  2000. }
  2001. }
  2002. if (include_ip) {
  2003. if (!_mdns_append_host_list_in_services(&packet->servers, services, len, true, false)) {
  2004. _mdns_free_tx_packet(packet);
  2005. return NULL;
  2006. }
  2007. }
  2008. return packet;
  2009. }
  2010. /**
  2011. * @brief Convert probe packet to announce
  2012. */
  2013. static mdns_tx_packet_t *_mdns_create_announce_from_probe(mdns_tx_packet_t *probe)
  2014. {
  2015. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(probe->tcpip_if, probe->ip_protocol);
  2016. if (!packet) {
  2017. return NULL;
  2018. }
  2019. packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
  2020. mdns_out_answer_t *s = probe->servers;
  2021. while (s) {
  2022. if (s->type == MDNS_TYPE_SRV) {
  2023. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, s->service, NULL, false, false)
  2024. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, s->service, NULL, false, false)
  2025. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, s->service, NULL, true, false)
  2026. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, s->service, NULL, true, false)) {
  2027. _mdns_free_tx_packet(packet);
  2028. return NULL;
  2029. }
  2030. mdns_host_item_t *host = mdns_get_host_item(s->service->hostname);
  2031. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_A, NULL, host, true, false)
  2032. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, true, false)) {
  2033. _mdns_free_tx_packet(packet);
  2034. return NULL;
  2035. }
  2036. } else if (s->type == MDNS_TYPE_A || s->type == MDNS_TYPE_AAAA) {
  2037. if (!_mdns_alloc_answer(&packet->answers, s->type, NULL, s->host, true, false)) {
  2038. _mdns_free_tx_packet(packet);
  2039. return NULL;
  2040. }
  2041. }
  2042. s = s->next;
  2043. }
  2044. return packet;
  2045. }
  2046. /**
  2047. * @brief Send by for particular services on particular PCB
  2048. */
  2049. static void _mdns_pcb_send_bye(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip)
  2050. {
  2051. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  2052. if (!packet) {
  2053. return;
  2054. }
  2055. packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
  2056. size_t i;
  2057. for (i = 0; i < len; i++) {
  2058. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, true, true)) {
  2059. _mdns_free_tx_packet(packet);
  2060. return;
  2061. }
  2062. }
  2063. if (include_ip) {
  2064. _mdns_append_host_list_in_services(&packet->answers, services, len, true, true);
  2065. }
  2066. _mdns_dispatch_tx_packet(packet);
  2067. _mdns_free_tx_packet(packet);
  2068. }
  2069. /**
  2070. * @brief Send probe for additional services on particular PCB
  2071. */
  2072. static void _mdns_init_pcb_probe_new_service(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip)
  2073. {
  2074. mdns_pcb_t *pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
  2075. size_t services_final_len = len;
  2076. if (PCB_STATE_IS_PROBING(pcb)) {
  2077. services_final_len += pcb->probe_services_len;
  2078. }
  2079. mdns_srv_item_t **_services = NULL;
  2080. if (services_final_len) {
  2081. _services = (mdns_srv_item_t **)malloc(sizeof(mdns_srv_item_t *) * services_final_len);
  2082. if (!_services) {
  2083. HOOK_MALLOC_FAILED;
  2084. return;
  2085. }
  2086. size_t i;
  2087. for (i = 0; i < len; i++) {
  2088. _services[i] = services[i];
  2089. }
  2090. if (pcb->probe_services) {
  2091. for (i = 0; i < pcb->probe_services_len; i++) {
  2092. _services[len + i] = pcb->probe_services[i];
  2093. }
  2094. free(pcb->probe_services);
  2095. }
  2096. }
  2097. probe_ip = pcb->probe_ip || probe_ip;
  2098. pcb->probe_ip = false;
  2099. pcb->probe_services = NULL;
  2100. pcb->probe_services_len = 0;
  2101. pcb->probe_running = false;
  2102. mdns_tx_packet_t *packet = _mdns_create_probe_packet(tcpip_if, ip_protocol, _services, services_final_len, true, probe_ip);
  2103. if (!packet) {
  2104. free(_services);
  2105. return;
  2106. }
  2107. pcb->probe_ip = probe_ip;
  2108. pcb->probe_services = _services;
  2109. pcb->probe_services_len = services_final_len;
  2110. pcb->probe_running = true;
  2111. _mdns_schedule_tx_packet(packet, ((pcb->failed_probes > 5) ? 1000 : 120) + (esp_random() & 0x7F));
  2112. pcb->state = PCB_PROBE_1;
  2113. }
  2114. /**
  2115. * @brief Send probe for particular services on particular PCB
  2116. *
  2117. * Tests possible duplication on probing service structure and probes only for new entries.
  2118. * - If pcb probing then add only non-probing services and restarts probing
  2119. * - If pcb not probing, run probing for all specified services
  2120. */
  2121. static void _mdns_init_pcb_probe(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip)
  2122. {
  2123. mdns_pcb_t *pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
  2124. _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
  2125. if (_str_null_or_empty(_mdns_server->hostname)) {
  2126. pcb->state = PCB_RUNNING;
  2127. return;
  2128. }
  2129. if (PCB_STATE_IS_PROBING(pcb)) {
  2130. // Looking for already probing services to resolve duplications
  2131. mdns_srv_item_t *new_probe_services[len];
  2132. int new_probe_service_len = 0;
  2133. bool found;
  2134. for (size_t j = 0; j < len; ++j) {
  2135. found = false;
  2136. for (int i = 0; i < pcb->probe_services_len; ++i) {
  2137. if (pcb->probe_services[i] == services[j]) {
  2138. found = true;
  2139. break;
  2140. }
  2141. }
  2142. if (!found) {
  2143. new_probe_services[new_probe_service_len++] = services[j];
  2144. }
  2145. }
  2146. // init probing for newly added services
  2147. _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol,
  2148. new_probe_service_len ? new_probe_services : NULL, new_probe_service_len, probe_ip);
  2149. } else {
  2150. // not probing, so init for all services
  2151. _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol, services, len, probe_ip);
  2152. }
  2153. }
  2154. /**
  2155. * @brief Restart the responder on particular PCB
  2156. */
  2157. static void _mdns_restart_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  2158. {
  2159. size_t srv_count = 0;
  2160. mdns_srv_item_t *a = _mdns_server->services;
  2161. while (a) {
  2162. srv_count++;
  2163. a = a->next;
  2164. }
  2165. mdns_srv_item_t *services[srv_count];
  2166. size_t i = 0;
  2167. a = _mdns_server->services;
  2168. while (a) {
  2169. services[i++] = a;
  2170. a = a->next;
  2171. }
  2172. _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, srv_count, true);
  2173. }
  2174. /**
  2175. * @brief Send by for particular services
  2176. */
  2177. static void _mdns_send_bye(mdns_srv_item_t **services, size_t len, bool include_ip)
  2178. {
  2179. uint8_t i, j;
  2180. if (_str_null_or_empty(_mdns_server->hostname)) {
  2181. return;
  2182. }
  2183. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  2184. for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
  2185. if (mdns_is_netif_ready(i, j) && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) {
  2186. _mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
  2187. }
  2188. }
  2189. }
  2190. }
  2191. /**
  2192. * @brief Send announcement on particular PCB
  2193. */
  2194. static void _mdns_announce_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip)
  2195. {
  2196. mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
  2197. size_t i;
  2198. if (mdns_is_netif_ready(tcpip_if, ip_protocol)) {
  2199. if (PCB_STATE_IS_PROBING(_pcb)) {
  2200. _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, len, include_ip);
  2201. } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
  2202. mdns_tx_packet_t *p = _mdns_get_next_pcb_packet(tcpip_if, ip_protocol);
  2203. if (p) {
  2204. for (i = 0; i < len; i++) {
  2205. if (!_mdns_alloc_answer(&p->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false)
  2206. || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false)
  2207. || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false)
  2208. || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) {
  2209. break;
  2210. }
  2211. }
  2212. if (include_ip) {
  2213. _mdns_dealloc_answer(&p->additional, MDNS_TYPE_A, NULL);
  2214. _mdns_dealloc_answer(&p->additional, MDNS_TYPE_AAAA, NULL);
  2215. _mdns_append_host_list_in_services(&p->answers, services, len, true, false);
  2216. }
  2217. _pcb->state = PCB_ANNOUNCE_1;
  2218. }
  2219. } else if (_pcb->state == PCB_RUNNING) {
  2220. if (_str_null_or_empty(_mdns_server->hostname)) {
  2221. return;
  2222. }
  2223. _pcb->state = PCB_ANNOUNCE_1;
  2224. mdns_tx_packet_t *p = _mdns_create_announce_packet(tcpip_if, ip_protocol, services, len, include_ip);
  2225. if (p) {
  2226. _mdns_schedule_tx_packet(p, 0);
  2227. }
  2228. }
  2229. }
  2230. }
  2231. /**
  2232. * @brief Send probe on all active PCBs
  2233. */
  2234. static void _mdns_probe_all_pcbs(mdns_srv_item_t **services, size_t len, bool probe_ip, bool clear_old_probe)
  2235. {
  2236. uint8_t i, j;
  2237. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  2238. for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
  2239. if (mdns_is_netif_ready(i, j)) {
  2240. mdns_pcb_t *_pcb = &_mdns_server->interfaces[i].pcbs[j];
  2241. if (clear_old_probe) {
  2242. free(_pcb->probe_services);
  2243. _pcb->probe_services = NULL;
  2244. _pcb->probe_services_len = 0;
  2245. _pcb->probe_running = false;
  2246. }
  2247. _mdns_init_pcb_probe((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, probe_ip);
  2248. }
  2249. }
  2250. }
  2251. }
  2252. /**
  2253. * @brief Send announcement on all active PCBs
  2254. */
  2255. static void _mdns_announce_all_pcbs(mdns_srv_item_t **services, size_t len, bool include_ip)
  2256. {
  2257. uint8_t i, j;
  2258. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  2259. for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
  2260. _mdns_announce_pcb((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
  2261. }
  2262. }
  2263. }
  2264. /**
  2265. * @brief Restart the responder on all active PCBs
  2266. */
  2267. static void _mdns_send_final_bye(bool include_ip)
  2268. {
  2269. //collect all services and start probe
  2270. size_t srv_count = 0;
  2271. mdns_srv_item_t *a = _mdns_server->services;
  2272. while (a) {
  2273. srv_count++;
  2274. a = a->next;
  2275. }
  2276. if (!srv_count) {
  2277. return;
  2278. }
  2279. mdns_srv_item_t *services[srv_count];
  2280. size_t i = 0;
  2281. a = _mdns_server->services;
  2282. while (a) {
  2283. services[i++] = a;
  2284. a = a->next;
  2285. }
  2286. _mdns_send_bye(services, srv_count, include_ip);
  2287. }
  2288. /**
  2289. * @brief Stop the responder on all services without instance
  2290. */
  2291. static void _mdns_send_bye_all_pcbs_no_instance(bool include_ip)
  2292. {
  2293. size_t srv_count = 0;
  2294. mdns_srv_item_t *a = _mdns_server->services;
  2295. while (a) {
  2296. if (!a->service->instance) {
  2297. srv_count++;
  2298. }
  2299. a = a->next;
  2300. }
  2301. if (!srv_count) {
  2302. return;
  2303. }
  2304. mdns_srv_item_t *services[srv_count];
  2305. size_t i = 0;
  2306. a = _mdns_server->services;
  2307. while (a) {
  2308. if (!a->service->instance) {
  2309. services[i++] = a;
  2310. }
  2311. a = a->next;
  2312. }
  2313. _mdns_send_bye(services, srv_count, include_ip);
  2314. }
  2315. /**
  2316. * @brief Restart the responder on all services without instance
  2317. */
  2318. static void _mdns_restart_all_pcbs_no_instance(void)
  2319. {
  2320. size_t srv_count = 0;
  2321. mdns_srv_item_t *a = _mdns_server->services;
  2322. while (a) {
  2323. if (!a->service->instance) {
  2324. srv_count++;
  2325. }
  2326. a = a->next;
  2327. }
  2328. if (!srv_count) {
  2329. return;
  2330. }
  2331. mdns_srv_item_t *services[srv_count];
  2332. size_t i = 0;
  2333. a = _mdns_server->services;
  2334. while (a) {
  2335. if (!a->service->instance) {
  2336. services[i++] = a;
  2337. }
  2338. a = a->next;
  2339. }
  2340. _mdns_probe_all_pcbs(services, srv_count, false, true);
  2341. }
  2342. /**
  2343. * @brief Restart the responder on all active PCBs
  2344. */
  2345. static void _mdns_restart_all_pcbs(void)
  2346. {
  2347. _mdns_clear_tx_queue_head();
  2348. size_t srv_count = 0;
  2349. mdns_srv_item_t *a = _mdns_server->services;
  2350. while (a) {
  2351. srv_count++;
  2352. a = a->next;
  2353. }
  2354. mdns_srv_item_t *services[srv_count];
  2355. size_t l = 0;
  2356. a = _mdns_server->services;
  2357. while (a) {
  2358. services[l++] = a;
  2359. a = a->next;
  2360. }
  2361. _mdns_probe_all_pcbs(services, srv_count, true, true);
  2362. }
  2363. /**
  2364. * @brief creates/allocates new text item list
  2365. * @param num_items service number of txt items or 0
  2366. * @param txt service txt items array or NULL
  2367. *
  2368. * @return pointer to the linked txt item list or NULL
  2369. */
  2370. static mdns_txt_linked_item_t *_mdns_allocate_txt(size_t num_items, mdns_txt_item_t txt[])
  2371. {
  2372. mdns_txt_linked_item_t *new_txt = NULL;
  2373. size_t i = 0;
  2374. if (num_items) {
  2375. for (i = 0; i < num_items; i++) {
  2376. mdns_txt_linked_item_t *new_item = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
  2377. if (!new_item) {
  2378. HOOK_MALLOC_FAILED;
  2379. break;
  2380. }
  2381. new_item->key = strdup(txt[i].key);
  2382. if (!new_item->key) {
  2383. free(new_item);
  2384. break;
  2385. }
  2386. new_item->value = strdup(txt[i].value);
  2387. if (!new_item->value) {
  2388. free((char *)new_item->key);
  2389. free(new_item);
  2390. break;
  2391. }
  2392. new_item->value_len = strlen(new_item->value);
  2393. new_item->next = new_txt;
  2394. new_txt = new_item;
  2395. }
  2396. }
  2397. return new_txt;
  2398. }
  2399. /**
  2400. * @brief Deallocate the txt linked list
  2401. * @param txt pointer to the txt pointer to free, noop if txt==NULL
  2402. */
  2403. static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt)
  2404. {
  2405. mdns_txt_linked_item_t *t;
  2406. while (txt) {
  2407. t = txt;
  2408. txt = txt->next;
  2409. free((char *)t->value);
  2410. free((char *)t->key);
  2411. free(t);
  2412. }
  2413. }
  2414. /**
  2415. * @brief creates/allocates new service
  2416. * @param service service type
  2417. * @param proto service proto
  2418. * @param hostname service hostname
  2419. * @param port service port
  2420. * @param instance service instance
  2421. * @param num_items service number of txt items or 0
  2422. * @param txt service txt items array or NULL
  2423. *
  2424. * @return pointer to the service or NULL on error
  2425. */
  2426. static mdns_service_t *_mdns_create_service(const char *service, const char *proto, const char *hostname,
  2427. uint16_t port, const char *instance, size_t num_items,
  2428. mdns_txt_item_t txt[])
  2429. {
  2430. mdns_service_t *s = (mdns_service_t *)calloc(1, sizeof(mdns_service_t));
  2431. if (!s) {
  2432. HOOK_MALLOC_FAILED;
  2433. return NULL;
  2434. }
  2435. mdns_txt_linked_item_t *new_txt = _mdns_allocate_txt(num_items, txt);
  2436. if (num_items && new_txt == NULL) {
  2437. goto fail;
  2438. }
  2439. s->priority = 0;
  2440. s->weight = 0;
  2441. s->instance = instance ? strndup(instance, MDNS_NAME_BUF_LEN - 1) : NULL;
  2442. s->txt = new_txt;
  2443. s->port = port;
  2444. s->subtype = NULL;
  2445. if (hostname) {
  2446. s->hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  2447. if (!s->hostname) {
  2448. goto fail;
  2449. }
  2450. } else {
  2451. s->hostname = NULL;
  2452. }
  2453. s->service = strndup(service, MDNS_NAME_BUF_LEN - 1);
  2454. if (!s->service) {
  2455. goto fail;
  2456. }
  2457. s->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
  2458. if (!s->proto) {
  2459. goto fail;
  2460. }
  2461. return s;
  2462. fail:
  2463. _mdns_free_linked_txt(s->txt);
  2464. free((char *)s->instance);
  2465. free((char *)s->service);
  2466. free((char *)s->proto);
  2467. free((char *)s->hostname);
  2468. free(s);
  2469. return NULL;
  2470. }
  2471. /**
  2472. * @brief Remove and free service answer from answer list (destination)
  2473. */
  2474. static void _mdns_dealloc_scheduled_service_answers(mdns_out_answer_t **destination, mdns_service_t *service)
  2475. {
  2476. mdns_out_answer_t *d = *destination;
  2477. if (!d) {
  2478. return;
  2479. }
  2480. while (d && d->service == service) {
  2481. *destination = d->next;
  2482. free(d);
  2483. d = *destination;
  2484. }
  2485. while (d && d->next) {
  2486. mdns_out_answer_t *a = d->next;
  2487. if (a->service == service) {
  2488. d->next = a->next;
  2489. free(a);
  2490. } else {
  2491. d = d->next;
  2492. }
  2493. }
  2494. }
  2495. /**
  2496. * @brief Find, remove and free answers and scheduled packets for service
  2497. */
  2498. static void _mdns_remove_scheduled_service_packets(mdns_service_t *service)
  2499. {
  2500. if (!service) {
  2501. return;
  2502. }
  2503. mdns_tx_packet_t *p = NULL;
  2504. mdns_tx_packet_t *q = _mdns_server->tx_queue_head;
  2505. while (q) {
  2506. bool had_answers = (q->answers != NULL);
  2507. _mdns_dealloc_scheduled_service_answers(&(q->answers), service);
  2508. _mdns_dealloc_scheduled_service_answers(&(q->additional), service);
  2509. _mdns_dealloc_scheduled_service_answers(&(q->servers), service);
  2510. mdns_pcb_t *_pcb = &_mdns_server->interfaces[q->tcpip_if].pcbs[q->ip_protocol];
  2511. if (mdns_is_netif_ready(q->tcpip_if, q->ip_protocol)) {
  2512. if (PCB_STATE_IS_PROBING(_pcb)) {
  2513. uint8_t i;
  2514. //check if we are probing this service
  2515. for (i = 0; i < _pcb->probe_services_len; i++) {
  2516. mdns_srv_item_t *s = _pcb->probe_services[i];
  2517. if (s->service == service) {
  2518. break;
  2519. }
  2520. }
  2521. if (i < _pcb->probe_services_len) {
  2522. if (_pcb->probe_services_len > 1) {
  2523. uint8_t n;
  2524. for (n = (i + 1); n < _pcb->probe_services_len; n++) {
  2525. _pcb->probe_services[n - 1] = _pcb->probe_services[n];
  2526. }
  2527. _pcb->probe_services_len--;
  2528. } else {
  2529. _pcb->probe_services_len = 0;
  2530. free(_pcb->probe_services);
  2531. _pcb->probe_services = NULL;
  2532. if (!_pcb->probe_ip) {
  2533. _pcb->probe_running = false;
  2534. _pcb->state = PCB_RUNNING;
  2535. }
  2536. }
  2537. if (q->questions) {
  2538. mdns_out_question_t *qsn = NULL;
  2539. mdns_out_question_t *qs = q->questions;
  2540. if (qs->type == MDNS_TYPE_ANY
  2541. && qs->service && strcmp(qs->service, service->service) == 0
  2542. && qs->proto && strcmp(qs->proto, service->proto) == 0) {
  2543. q->questions = q->questions->next;
  2544. free(qs);
  2545. } else while (qs->next) {
  2546. qsn = qs->next;
  2547. if (qsn->type == MDNS_TYPE_ANY
  2548. && qsn->service && strcmp(qsn->service, service->service) == 0
  2549. && qsn->proto && strcmp(qsn->proto, service->proto) == 0) {
  2550. qs->next = qsn->next;
  2551. free(qsn);
  2552. break;
  2553. }
  2554. qs = qs->next;
  2555. }
  2556. }
  2557. }
  2558. } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
  2559. //if answers were cleared, set to running
  2560. if (had_answers && q->answers == NULL) {
  2561. _pcb->state = PCB_RUNNING;
  2562. }
  2563. }
  2564. }
  2565. p = q;
  2566. q = q->next;
  2567. if (!p->questions && !p->answers && !p->additional && !p->servers) {
  2568. queueDetach(mdns_tx_packet_t, _mdns_server->tx_queue_head, p);
  2569. _mdns_free_tx_packet(p);
  2570. }
  2571. }
  2572. }
  2573. static void _mdns_free_service_subtype(mdns_service_t *service)
  2574. {
  2575. while (service->subtype) {
  2576. mdns_subtype_t *next = service->subtype->next;
  2577. free((char *)service->subtype->subtype);
  2578. free(service->subtype);
  2579. service->subtype = next;
  2580. }
  2581. }
  2582. /**
  2583. * @brief free service memory
  2584. *
  2585. * @param service the service
  2586. */
  2587. static void _mdns_free_service(mdns_service_t *service)
  2588. {
  2589. if (!service) {
  2590. return;
  2591. }
  2592. free((char *)service->instance);
  2593. free((char *)service->service);
  2594. free((char *)service->proto);
  2595. free((char *)service->hostname);
  2596. while (service->txt) {
  2597. mdns_txt_linked_item_t *s = service->txt;
  2598. service->txt = service->txt->next;
  2599. free((char *)s->key);
  2600. free((char *)s->value);
  2601. free(s);
  2602. }
  2603. _mdns_free_service_subtype(service);
  2604. free(service);
  2605. }
  2606. /*
  2607. * Received Packet Handling
  2608. * */
  2609. /**
  2610. * @brief Detect SRV collision
  2611. */
  2612. static int _mdns_check_srv_collision(mdns_service_t *service, uint16_t priority, uint16_t weight, uint16_t port, const char *host, const char *domain)
  2613. {
  2614. if (_str_null_or_empty(_mdns_server->hostname)) {
  2615. return 0;
  2616. }
  2617. size_t our_host_len = strlen(_mdns_server->hostname);
  2618. size_t our_len = 14 + our_host_len;
  2619. size_t their_host_len = strlen(host);
  2620. size_t their_domain_len = strlen(domain);
  2621. size_t their_len = 9 + their_host_len + their_domain_len;
  2622. if (their_len > our_len) {
  2623. return 1;//they win
  2624. } else if (their_len < our_len) {
  2625. return -1;//we win
  2626. }
  2627. uint16_t our_index = 0;
  2628. uint8_t our_data[our_len];
  2629. _mdns_append_u16(our_data, &our_index, service->priority);
  2630. _mdns_append_u16(our_data, &our_index, service->weight);
  2631. _mdns_append_u16(our_data, &our_index, service->port);
  2632. our_data[our_index++] = our_host_len;
  2633. memcpy(our_data + our_index, _mdns_server->hostname, our_host_len);
  2634. our_index += our_host_len;
  2635. our_data[our_index++] = 5;
  2636. memcpy(our_data + our_index, MDNS_DEFAULT_DOMAIN, 5);
  2637. our_index += 5;
  2638. our_data[our_index++] = 0;
  2639. uint16_t their_index = 0;
  2640. uint8_t their_data[their_len];
  2641. _mdns_append_u16(their_data, &their_index, priority);
  2642. _mdns_append_u16(their_data, &their_index, weight);
  2643. _mdns_append_u16(their_data, &their_index, port);
  2644. their_data[their_index++] = their_host_len;
  2645. memcpy(their_data + their_index, host, their_host_len);
  2646. their_index += their_host_len;
  2647. their_data[their_index++] = their_domain_len;
  2648. memcpy(their_data + their_index, domain, their_domain_len);
  2649. their_index += their_domain_len;
  2650. their_data[their_index++] = 0;
  2651. int ret = memcmp(our_data, their_data, our_len);
  2652. if (ret > 0) {
  2653. return -1;//we win
  2654. } else if (ret < 0) {
  2655. return 1;//they win
  2656. }
  2657. return 0;//same
  2658. }
  2659. /**
  2660. * @brief Detect TXT collision
  2661. */
  2662. static int _mdns_check_txt_collision(mdns_service_t *service, const uint8_t *data, size_t len)
  2663. {
  2664. size_t data_len = 0;
  2665. if (len == 1 && service->txt) {
  2666. return -1;//we win
  2667. } else if (len > 1 && !service->txt) {
  2668. return 1;//they win
  2669. } else if (len == 1 && !service->txt) {
  2670. return 0;//same
  2671. }
  2672. mdns_txt_linked_item_t *txt = service->txt;
  2673. while (txt) {
  2674. data_len += 1 /* record-len */ + strlen(txt->key) + txt->value_len + (txt->value ? 1 : 0 /* "=" */);
  2675. txt = txt->next;
  2676. }
  2677. if (len > data_len) {
  2678. return 1;//they win
  2679. } else if (len < data_len) {
  2680. return -1;//we win
  2681. }
  2682. uint8_t ours[len];
  2683. uint16_t index = 0;
  2684. txt = service->txt;
  2685. while (txt) {
  2686. append_one_txt_record_entry(ours, &index, txt);
  2687. txt = txt->next;
  2688. }
  2689. int ret = memcmp(ours, data, len);
  2690. if (ret > 0) {
  2691. return -1;//we win
  2692. } else if (ret < 0) {
  2693. return 1;//they win
  2694. }
  2695. return 0;//same
  2696. }
  2697. static esp_err_t mdns_pcb_deinit_local(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_proto)
  2698. {
  2699. esp_err_t err = _mdns_pcb_deinit(tcpip_if, ip_proto);
  2700. mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_proto];
  2701. if (_pcb == NULL || err != ESP_OK) {
  2702. return err;
  2703. }
  2704. free(_pcb->probe_services);
  2705. _pcb->state = PCB_OFF;
  2706. _pcb->probe_ip = false;
  2707. _pcb->probe_services = NULL;
  2708. _pcb->probe_services_len = 0;
  2709. _pcb->probe_running = false;
  2710. _pcb->failed_probes = 0;
  2711. return ESP_OK;
  2712. }
  2713. /**
  2714. * @brief Set interface as duplicate if another is found on the same subnet
  2715. */
  2716. static void _mdns_dup_interface(mdns_if_t tcpip_if)
  2717. {
  2718. uint8_t i;
  2719. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  2720. if (other_if == MDNS_MAX_INTERFACES) {
  2721. return; // no other interface found
  2722. }
  2723. for (i = 0; i < MDNS_IP_PROTOCOL_MAX; i++) {
  2724. if (mdns_is_netif_ready(other_if, i)) {
  2725. //stop this interface and mark as dup
  2726. if (mdns_is_netif_ready(tcpip_if, i)) {
  2727. _mdns_clear_pcb_tx_queue_head(tcpip_if, i);
  2728. mdns_pcb_deinit_local(tcpip_if, i);
  2729. }
  2730. _mdns_server->interfaces[tcpip_if].pcbs[i].state = PCB_DUP;
  2731. _mdns_announce_pcb(other_if, i, NULL, 0, true);
  2732. }
  2733. }
  2734. }
  2735. #ifdef CONFIG_LWIP_IPV4
  2736. /**
  2737. * @brief Detect IPv4 address collision
  2738. */
  2739. static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if)
  2740. {
  2741. esp_netif_ip_info_t if_ip_info;
  2742. esp_netif_ip_info_t other_ip_info;
  2743. if (!ip->addr) {
  2744. return 1;//denial! they win
  2745. }
  2746. if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
  2747. return 1;//they win
  2748. }
  2749. int ret = memcmp((uint8_t *)&if_ip_info.ip.addr, (uint8_t *)&ip->addr, sizeof(esp_ip4_addr_t));
  2750. if (ret > 0) {
  2751. return -1;//we win
  2752. } else if (ret < 0) {
  2753. //is it the other interface?
  2754. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  2755. if (other_if == MDNS_MAX_INTERFACES) {
  2756. return 1;//AP interface! They win
  2757. }
  2758. if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &other_ip_info)) {
  2759. return 1;//IPv4 not active! They win
  2760. }
  2761. if (ip->addr != other_ip_info.ip.addr) {
  2762. return 1;//IPv4 not ours! They win
  2763. }
  2764. _mdns_dup_interface(tcpip_if);
  2765. return 2;//they win
  2766. }
  2767. return 0;//same
  2768. }
  2769. #endif /* CONFIG_LWIP_IPV4 */
  2770. #ifdef CONFIG_LWIP_IPV6
  2771. /**
  2772. * @brief Detect IPv6 address collision
  2773. */
  2774. static int _mdns_check_aaaa_collision(esp_ip6_addr_t *ip, mdns_if_t tcpip_if)
  2775. {
  2776. struct esp_ip6_addr if_ip6;
  2777. struct esp_ip6_addr other_ip6;
  2778. if (_ipv6_address_is_zero(*ip)) {
  2779. return 1;//denial! they win
  2780. }
  2781. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) {
  2782. return 1;//they win
  2783. }
  2784. int ret = memcmp((uint8_t *)&if_ip6.addr, (uint8_t *)ip->addr, _MDNS_SIZEOF_IP6_ADDR);
  2785. if (ret > 0) {
  2786. return -1;//we win
  2787. } else if (ret < 0) {
  2788. //is it the other interface?
  2789. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  2790. if (other_if == MDNS_MAX_INTERFACES) {
  2791. return 1;//AP interface! They win
  2792. }
  2793. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) {
  2794. return 1;//IPv6 not active! They win
  2795. }
  2796. if (memcmp((uint8_t *)&other_ip6.addr, (uint8_t *)ip->addr, _MDNS_SIZEOF_IP6_ADDR)) {
  2797. return 1;//IPv6 not ours! They win
  2798. }
  2799. _mdns_dup_interface(tcpip_if);
  2800. return 2;//they win
  2801. }
  2802. return 0;//same
  2803. }
  2804. #endif /* CONFIG_LWIP_IPV6 */
  2805. static bool _hostname_is_ours(const char *hostname)
  2806. {
  2807. if (!_str_null_or_empty(_mdns_server->hostname) &&
  2808. strcasecmp(hostname, _mdns_server->hostname) == 0) {
  2809. return true;
  2810. }
  2811. mdns_host_item_t *host = _mdns_host_list;
  2812. while (host != NULL) {
  2813. if (strcasecmp(hostname, host->hostname) == 0) {
  2814. return true;
  2815. }
  2816. host = host->next;
  2817. }
  2818. return false;
  2819. }
  2820. /**
  2821. * @brief Adds a delegated hostname to the linked list
  2822. * @param hostname Host name pointer
  2823. * @param address_list Address list
  2824. * @return true on success
  2825. * false if the host wasn't attached (this is our hostname, or alloc failure) so we have to free the structs
  2826. */
  2827. static bool _mdns_delegate_hostname_add(const char *hostname, mdns_ip_addr_t *address_list)
  2828. {
  2829. if (_hostname_is_ours(hostname)) {
  2830. return false;
  2831. }
  2832. mdns_host_item_t *host = (mdns_host_item_t *)malloc(sizeof(mdns_host_item_t));
  2833. if (host == NULL) {
  2834. return false;
  2835. }
  2836. host->address_list = address_list;
  2837. host->hostname = hostname;
  2838. host->next = _mdns_host_list;
  2839. _mdns_host_list = host;
  2840. return true;
  2841. }
  2842. static void free_address_list(mdns_ip_addr_t *address_list)
  2843. {
  2844. while (address_list != NULL) {
  2845. mdns_ip_addr_t *next = address_list->next;
  2846. free(address_list);
  2847. address_list = next;
  2848. }
  2849. }
  2850. static bool _mdns_delegate_hostname_set_address(const char *hostname, mdns_ip_addr_t *address_list)
  2851. {
  2852. if (!_str_null_or_empty(_mdns_server->hostname) &&
  2853. strcasecmp(hostname, _mdns_server->hostname) == 0) {
  2854. return false;
  2855. }
  2856. mdns_host_item_t *host = _mdns_host_list;
  2857. while (host != NULL) {
  2858. if (strcasecmp(hostname, host->hostname) == 0) {
  2859. // free previous address list
  2860. free_address_list(host->address_list);
  2861. // set current address list to the host
  2862. host->address_list = address_list;
  2863. return true;
  2864. }
  2865. host = host->next;
  2866. }
  2867. return false;
  2868. }
  2869. static mdns_ip_addr_t *copy_address_list(const mdns_ip_addr_t *address_list)
  2870. {
  2871. mdns_ip_addr_t *head = NULL;
  2872. mdns_ip_addr_t *tail = NULL;
  2873. while (address_list != NULL) {
  2874. mdns_ip_addr_t *addr = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
  2875. if (addr == NULL) {
  2876. free_address_list(head);
  2877. return NULL;
  2878. }
  2879. addr->addr = address_list->addr;
  2880. addr->next = NULL;
  2881. if (head == NULL) {
  2882. head = addr;
  2883. tail = addr;
  2884. } else {
  2885. tail->next = addr;
  2886. tail = tail->next;
  2887. }
  2888. address_list = address_list->next;
  2889. }
  2890. return head;
  2891. }
  2892. static void free_delegated_hostnames(void)
  2893. {
  2894. mdns_host_item_t *host = _mdns_host_list;
  2895. while (host != NULL) {
  2896. free_address_list(host->address_list);
  2897. free((char *)host->hostname);
  2898. mdns_host_item_t *item = host;
  2899. host = host->next;
  2900. free(item);
  2901. }
  2902. _mdns_host_list = NULL;
  2903. }
  2904. static bool _mdns_delegate_hostname_remove(const char *hostname)
  2905. {
  2906. mdns_srv_item_t *srv = _mdns_server->services;
  2907. mdns_srv_item_t *prev_srv = NULL;
  2908. while (srv) {
  2909. if (strcasecmp(srv->service->hostname, hostname) == 0) {
  2910. mdns_srv_item_t *to_free = srv;
  2911. _mdns_send_bye(&srv, 1, false);
  2912. _mdns_remove_scheduled_service_packets(srv->service);
  2913. if (prev_srv == NULL) {
  2914. _mdns_server->services = srv->next;
  2915. srv = srv->next;
  2916. } else {
  2917. prev_srv->next = srv->next;
  2918. srv = srv->next;
  2919. }
  2920. _mdns_free_service(to_free->service);
  2921. free(to_free);
  2922. } else {
  2923. prev_srv = srv;
  2924. srv = srv->next;
  2925. }
  2926. }
  2927. mdns_host_item_t *host = _mdns_host_list;
  2928. mdns_host_item_t *prev_host = NULL;
  2929. while (host != NULL) {
  2930. if (strcasecmp(hostname, host->hostname) == 0) {
  2931. if (prev_host == NULL) {
  2932. _mdns_host_list = host->next;
  2933. } else {
  2934. prev_host->next = host->next;
  2935. }
  2936. free_address_list(host->address_list);
  2937. free((char *)host->hostname);
  2938. free(host);
  2939. break;
  2940. } else {
  2941. prev_host = host;
  2942. host = host->next;
  2943. }
  2944. }
  2945. return true;
  2946. }
  2947. /**
  2948. * @brief Check if parsed name is discovery
  2949. */
  2950. static bool _mdns_name_is_discovery(mdns_name_t *name, uint16_t type)
  2951. {
  2952. return (
  2953. (name->host[0] && !strcasecmp(name->host, "_services"))
  2954. && (name->service[0] && !strcasecmp(name->service, "_dns-sd"))
  2955. && (name->proto[0] && !strcasecmp(name->proto, "_udp"))
  2956. && (name->domain[0] && !strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN))
  2957. && type == MDNS_TYPE_PTR
  2958. );
  2959. }
  2960. /**
  2961. * @brief Check if the parsed name is self-hosted, i.e. we should resolve conflicts
  2962. */
  2963. static bool _mdns_name_is_selfhosted(mdns_name_t *name)
  2964. {
  2965. if (_str_null_or_empty(_mdns_server->hostname)) { // self-hostname needs to be defined
  2966. return false;
  2967. }
  2968. // hostname only -- check if selfhosted name
  2969. if (_str_null_or_empty(name->service) && _str_null_or_empty(name->proto) &&
  2970. strcasecmp(name->host, _mdns_server->hostname) == 0 ) {
  2971. return true;
  2972. }
  2973. // service -- check if selfhosted service
  2974. mdns_srv_item_t *srv = _mdns_get_service_item(name->service, name->proto, NULL);
  2975. if (srv && strcasecmp(_mdns_server->hostname, srv->service->hostname) == 0) {
  2976. return true;
  2977. }
  2978. return false;
  2979. }
  2980. /**
  2981. * @brief Check if the parsed name is ours (matches service or host name)
  2982. */
  2983. static bool _mdns_name_is_ours(mdns_name_t *name)
  2984. {
  2985. //domain have to be "local"
  2986. if (_str_null_or_empty(name->domain) || ( strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN)
  2987. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  2988. && strcasecmp(name->domain, "arpa")
  2989. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  2990. ) ) {
  2991. return false;
  2992. }
  2993. //if service and proto are empty, host must match out hostname
  2994. if (_str_null_or_empty(name->service) && _str_null_or_empty(name->proto)) {
  2995. if (!_str_null_or_empty(name->host)
  2996. && !_str_null_or_empty(_mdns_server->hostname)
  2997. && _hostname_is_ours(name->host)) {
  2998. return true;
  2999. }
  3000. return false;
  3001. }
  3002. //if service or proto is empty, name is invalid
  3003. if (_str_null_or_empty(name->service) || _str_null_or_empty(name->proto)) {
  3004. return false;
  3005. }
  3006. //find the service
  3007. mdns_srv_item_t *service;
  3008. if (name->sub) {
  3009. service = _mdns_get_service_item_subtype(name->host, name->service, name->proto);
  3010. } else if (_str_null_or_empty(name->host)) {
  3011. service = _mdns_get_service_item(name->service, name->proto, NULL);
  3012. } else {
  3013. service = _mdns_get_service_item_instance(name->host, name->service, name->proto, NULL);
  3014. }
  3015. if (!service) {
  3016. return false;
  3017. }
  3018. //if query is PTR query and we have service, we have success
  3019. if (name->sub || _str_null_or_empty(name->host)) {
  3020. return true;
  3021. }
  3022. //OK we have host in the name. find what is the instance of the service
  3023. const char *instance = _mdns_get_service_instance_name(service->service);
  3024. if (instance == NULL) {
  3025. return false;
  3026. }
  3027. //compare the instance against the name
  3028. if (strcasecmp(name->host, instance) == 0) {
  3029. return true;
  3030. }
  3031. return false;
  3032. }
  3033. /**
  3034. * @brief read uint16_t from a packet
  3035. * @param packet the packet
  3036. * @param index index in the packet where the value starts
  3037. *
  3038. * @return the value
  3039. */
  3040. static inline uint16_t _mdns_read_u16(const uint8_t *packet, uint16_t index)
  3041. {
  3042. return (uint16_t)(packet[index]) << 8 | packet[index + 1];
  3043. }
  3044. /**
  3045. * @brief read uint32_t from a packet
  3046. * @param packet the packet
  3047. * @param index index in the packet where the value starts
  3048. *
  3049. * @return the value
  3050. */
  3051. static inline uint32_t _mdns_read_u32(const uint8_t *packet, uint16_t index)
  3052. {
  3053. return (uint32_t)(packet[index]) << 24 | (uint32_t)(packet[index + 1]) << 16 | (uint32_t)(packet[index + 2]) << 8 | packet[index + 3];
  3054. }
  3055. /**
  3056. * @brief reads and formats MDNS FQDN into mdns_name_t structure
  3057. *
  3058. * @param packet MDNS packet
  3059. * @param start Starting point of FQDN
  3060. * @param name mdns_name_t structure to populate
  3061. *
  3062. * @return the address after the parsed FQDN in the packet or NULL on error
  3063. */
  3064. static const uint8_t *_mdns_parse_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, size_t packet_len)
  3065. {
  3066. name->parts = 0;
  3067. name->sub = 0;
  3068. name->host[0] = 0;
  3069. name->service[0] = 0;
  3070. name->proto[0] = 0;
  3071. name->domain[0] = 0;
  3072. name->invalid = false;
  3073. static char buf[MDNS_NAME_BUF_LEN];
  3074. const uint8_t *next_data = (uint8_t *)_mdns_read_fqdn(packet, start, name, buf, packet_len);
  3075. if (!next_data) {
  3076. return 0;
  3077. }
  3078. if (!name->parts || name->invalid) {
  3079. return next_data;
  3080. }
  3081. if (name->parts == 3) {
  3082. memmove((uint8_t *)name + (MDNS_NAME_BUF_LEN), (uint8_t *)name, 3 * (MDNS_NAME_BUF_LEN));
  3083. name->host[0] = 0;
  3084. } else if (name->parts == 2) {
  3085. memmove((uint8_t *)(name->domain), (uint8_t *)(name->service), (MDNS_NAME_BUF_LEN));
  3086. name->service[0] = 0;
  3087. name->proto[0] = 0;
  3088. }
  3089. if (strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN) == 0 || strcasecmp(name->domain, "arpa") == 0) {
  3090. return next_data;
  3091. }
  3092. name->invalid = true; // mark the current name invalid, but continue with other question
  3093. return next_data;
  3094. }
  3095. /**
  3096. * @brief Called from parser to check if question matches particular service
  3097. */
  3098. static bool _mdns_question_matches(mdns_parsed_question_t *question, uint16_t type, mdns_srv_item_t *service)
  3099. {
  3100. if (question->type != type) {
  3101. return false;
  3102. }
  3103. if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
  3104. return true;
  3105. } else if (type == MDNS_TYPE_PTR || type == MDNS_TYPE_SDPTR) {
  3106. if (question->service && question->proto && question->domain
  3107. && !strcasecmp(service->service->service, question->service)
  3108. && !strcasecmp(service->service->proto, question->proto)
  3109. && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) {
  3110. if (!service->service->instance) {
  3111. return true;
  3112. } else if (service->service->instance && question->host && !strcasecmp(service->service->instance, question->host)) {
  3113. return true;
  3114. }
  3115. }
  3116. } else if (service && (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT)) {
  3117. const char *name = _mdns_get_service_instance_name(service->service);
  3118. if (name && question->host && question->service && question->proto && question->domain
  3119. && !strcasecmp(name, question->host)
  3120. && !strcasecmp(service->service->service, question->service)
  3121. && !strcasecmp(service->service->proto, question->proto)
  3122. && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) {
  3123. return true;
  3124. }
  3125. }
  3126. return false;
  3127. }
  3128. /**
  3129. * @brief Removes saved question from parsed data
  3130. */
  3131. static void _mdns_remove_parsed_question(mdns_parsed_packet_t *parsed_packet, uint16_t type, mdns_srv_item_t *service)
  3132. {
  3133. mdns_parsed_question_t *q = parsed_packet->questions;
  3134. if (_mdns_question_matches(q, type, service)) {
  3135. parsed_packet->questions = q->next;
  3136. free(q->host);
  3137. free(q->service);
  3138. free(q->proto);
  3139. free(q->domain);
  3140. free(q);
  3141. return;
  3142. }
  3143. while (q->next) {
  3144. mdns_parsed_question_t *p = q->next;
  3145. if (_mdns_question_matches(p, type, service)) {
  3146. q->next = p->next;
  3147. free(p->host);
  3148. free(p->service);
  3149. free(p->proto);
  3150. free(p->domain);
  3151. free(p);
  3152. return;
  3153. }
  3154. q = q->next;
  3155. }
  3156. }
  3157. /**
  3158. * @brief Get number of items in TXT parsed data
  3159. */
  3160. static int _mdns_txt_items_count_get(const uint8_t *data, size_t len)
  3161. {
  3162. if (len == 1) {
  3163. return 0;
  3164. }
  3165. int num_items = 0;
  3166. uint16_t i = 0;
  3167. size_t partLen = 0;
  3168. while (i < len) {
  3169. partLen = data[i++];
  3170. if (!partLen) {
  3171. break;
  3172. }
  3173. if ((i + partLen) > len) {
  3174. return -1;//error
  3175. }
  3176. i += partLen;
  3177. num_items++;
  3178. }
  3179. return num_items;
  3180. }
  3181. /**
  3182. * @brief Get the length of TXT item's key name
  3183. */
  3184. static int _mdns_txt_item_name_get_len(const uint8_t *data, size_t len)
  3185. {
  3186. if (*data == '=') {
  3187. return -1;
  3188. }
  3189. for (size_t i = 0; i < len; i++) {
  3190. if (data[i] == '=') {
  3191. return i;
  3192. }
  3193. }
  3194. return len;
  3195. }
  3196. /**
  3197. * @brief Create TXT result array from parsed TXT data
  3198. */
  3199. static void _mdns_result_txt_create(const uint8_t *data, size_t len, mdns_txt_item_t **out_txt, uint8_t **out_value_len,
  3200. size_t *out_count)
  3201. {
  3202. *out_txt = NULL;
  3203. *out_count = 0;
  3204. uint16_t i = 0, y;
  3205. size_t partLen = 0;
  3206. int num_items = _mdns_txt_items_count_get(data, len);
  3207. if (num_items < 0 || num_items > SIZE_MAX / sizeof(mdns_txt_item_t)) {
  3208. // Error: num_items is incorrect (or too large to allocate)
  3209. return;
  3210. }
  3211. if (!num_items) {
  3212. return;
  3213. }
  3214. mdns_txt_item_t *txt = (mdns_txt_item_t *)malloc(sizeof(mdns_txt_item_t) * num_items);
  3215. if (!txt) {
  3216. HOOK_MALLOC_FAILED;
  3217. return;
  3218. }
  3219. uint8_t *txt_value_len = (uint8_t *)malloc(num_items);
  3220. if (!txt_value_len) {
  3221. free(txt);
  3222. HOOK_MALLOC_FAILED;
  3223. return;
  3224. }
  3225. memset(txt, 0, sizeof(mdns_txt_item_t) * num_items);
  3226. memset(txt_value_len, 0, num_items);
  3227. size_t txt_num = 0;
  3228. while (i < len) {
  3229. partLen = data[i++];
  3230. if (!partLen) {
  3231. break;
  3232. }
  3233. if ((i + partLen) > len) {
  3234. goto handle_error;//error
  3235. }
  3236. int name_len = _mdns_txt_item_name_get_len(data + i, partLen);
  3237. if (name_len < 0) {//invalid item (no name)
  3238. i += partLen;
  3239. continue;
  3240. }
  3241. char *key = (char *)malloc(name_len + 1);
  3242. if (!key) {
  3243. HOOK_MALLOC_FAILED;
  3244. goto handle_error;//error
  3245. }
  3246. mdns_txt_item_t *t = &txt[txt_num];
  3247. uint8_t *value_len = &txt_value_len[txt_num];
  3248. txt_num++;
  3249. memcpy(key, data + i, name_len);
  3250. key[name_len] = 0;
  3251. i += name_len + 1;
  3252. t->key = key;
  3253. int new_value_len = partLen - name_len - 1;
  3254. if (new_value_len > 0) {
  3255. char *value = (char *)malloc(new_value_len + 1);
  3256. if (!value) {
  3257. HOOK_MALLOC_FAILED;
  3258. goto handle_error;//error
  3259. }
  3260. memcpy(value, data + i, new_value_len);
  3261. value[new_value_len] = 0;
  3262. *value_len = new_value_len;
  3263. i += new_value_len;
  3264. t->value = value;
  3265. }
  3266. }
  3267. *out_txt = txt;
  3268. *out_count = txt_num;
  3269. *out_value_len = txt_value_len;
  3270. return;
  3271. handle_error :
  3272. for (y = 0; y < txt_num; y++) {
  3273. mdns_txt_item_t *t = &txt[y];
  3274. free((char *)t->key);
  3275. free((char *)t->value);
  3276. }
  3277. free(txt_value_len);
  3278. free(txt);
  3279. }
  3280. /**
  3281. * @brief Duplicate string or return error
  3282. */
  3283. static esp_err_t _mdns_strdup_check(char **out, char *in)
  3284. {
  3285. if (in && in[0]) {
  3286. *out = strdup(in);
  3287. if (!*out) {
  3288. return ESP_FAIL;
  3289. }
  3290. return ESP_OK;
  3291. }
  3292. *out = NULL;
  3293. return ESP_OK;
  3294. }
  3295. /**
  3296. * @brief main packet parser
  3297. *
  3298. * @param packet the packet
  3299. */
  3300. void mdns_parse_packet(mdns_rx_packet_t *packet)
  3301. {
  3302. static mdns_name_t n;
  3303. mdns_header_t header;
  3304. const uint8_t *data = _mdns_get_packet_data(packet);
  3305. size_t len = _mdns_get_packet_len(packet);
  3306. const uint8_t *content = data + MDNS_HEAD_LEN;
  3307. bool do_not_reply = false;
  3308. mdns_search_once_t *search_result = NULL;
  3309. mdns_browse_t *browse_result = NULL;
  3310. char *browse_result_instance = NULL;
  3311. char *browse_result_service = NULL;
  3312. char *browse_result_proto = NULL;
  3313. mdns_browse_sync_t *out_sync_browse = NULL;
  3314. #ifdef MDNS_ENABLE_DEBUG
  3315. _mdns_dbg_printf("\nRX[%lu][%lu]: ", (unsigned long)packet->tcpip_if, (unsigned long)packet->ip_protocol);
  3316. #ifdef CONFIG_LWIP_IPV4
  3317. if (packet->src.type == ESP_IPADDR_TYPE_V4) {
  3318. _mdns_dbg_printf("From: " IPSTR ":%u, To: " IPSTR ", ", IP2STR(&packet->src.u_addr.ip4), packet->src_port, IP2STR(&packet->dest.u_addr.ip4));
  3319. }
  3320. #endif
  3321. #ifdef CONFIG_LWIP_IPV6
  3322. if (packet->src.type == ESP_IPADDR_TYPE_V6) {
  3323. _mdns_dbg_printf("From: " IPV6STR ":%u, To: " IPV6STR ", ", IPV62STR(packet->src.u_addr.ip6), packet->src_port, IPV62STR(packet->dest.u_addr.ip6));
  3324. }
  3325. #endif
  3326. mdns_debug_packet(data, len);
  3327. #endif
  3328. #ifndef CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES
  3329. // Check if the packet wasn't sent by us
  3330. #ifdef CONFIG_LWIP_IPV4
  3331. if (packet->ip_protocol == MDNS_IP_PROTOCOL_V4) {
  3332. esp_netif_ip_info_t if_ip_info;
  3333. if (esp_netif_get_ip_info(_mdns_get_esp_netif(packet->tcpip_if), &if_ip_info) == ESP_OK &&
  3334. memcmp(&if_ip_info.ip.addr, &packet->src.u_addr.ip4.addr, sizeof(esp_ip4_addr_t)) == 0) {
  3335. return;
  3336. }
  3337. }
  3338. #endif /* CONFIG_LWIP_IPV4 */
  3339. #ifdef CONFIG_LWIP_IPV6
  3340. if (packet->ip_protocol == MDNS_IP_PROTOCOL_V6) {
  3341. struct esp_ip6_addr if_ip6;
  3342. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK &&
  3343. memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) {
  3344. return;
  3345. }
  3346. }
  3347. #endif /* CONFIG_LWIP_IPV6 */
  3348. #endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES
  3349. // Check for the minimum size of mdns packet
  3350. if (len <= MDNS_HEAD_ADDITIONAL_OFFSET) {
  3351. return;
  3352. }
  3353. mdns_parsed_packet_t *parsed_packet = (mdns_parsed_packet_t *)malloc(sizeof(mdns_parsed_packet_t));
  3354. if (!parsed_packet) {
  3355. HOOK_MALLOC_FAILED;
  3356. return;
  3357. }
  3358. memset(parsed_packet, 0, sizeof(mdns_parsed_packet_t));
  3359. mdns_name_t *name = &n;
  3360. memset(name, 0, sizeof(mdns_name_t));
  3361. header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
  3362. header.flags = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
  3363. header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
  3364. header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
  3365. header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
  3366. header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
  3367. if (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) {
  3368. free(parsed_packet);
  3369. return;
  3370. }
  3371. //if we have not set the hostname, we can not answer questions
  3372. if (header.questions && !header.answers && _str_null_or_empty(_mdns_server->hostname)) {
  3373. free(parsed_packet);
  3374. return;
  3375. }
  3376. parsed_packet->tcpip_if = packet->tcpip_if;
  3377. parsed_packet->ip_protocol = packet->ip_protocol;
  3378. parsed_packet->multicast = packet->multicast;
  3379. parsed_packet->authoritative = (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE);
  3380. parsed_packet->distributed = header.flags == MDNS_FLAGS_DISTRIBUTED;
  3381. parsed_packet->id = header.id;
  3382. esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src);
  3383. parsed_packet->src_port = packet->src_port;
  3384. parsed_packet->records = NULL;
  3385. if (header.questions) {
  3386. uint8_t qs = header.questions;
  3387. while (qs--) {
  3388. content = _mdns_parse_fqdn(data, content, name, len);
  3389. if (!content) {
  3390. header.answers = 0;
  3391. header.additional = 0;
  3392. header.servers = 0;
  3393. goto clear_rx_packet;//error
  3394. }
  3395. if (content + MDNS_CLASS_OFFSET + 1 >= data + len) {
  3396. goto clear_rx_packet; // malformed packet, won't read behind it
  3397. }
  3398. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  3399. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  3400. bool unicast = !!(mdns_class & 0x8000);
  3401. mdns_class &= 0x7FFF;
  3402. content = content + 4;
  3403. if (mdns_class != 0x0001 || name->invalid) {//bad class or invalid name for this question entry
  3404. continue;
  3405. }
  3406. if (_mdns_name_is_discovery(name, type)) {
  3407. //service discovery
  3408. parsed_packet->discovery = true;
  3409. mdns_srv_item_t *a = _mdns_server->services;
  3410. while (a) {
  3411. mdns_parsed_question_t *question = (mdns_parsed_question_t *)calloc(1, sizeof(mdns_parsed_question_t));
  3412. if (!question) {
  3413. HOOK_MALLOC_FAILED;
  3414. goto clear_rx_packet;
  3415. }
  3416. question->next = parsed_packet->questions;
  3417. parsed_packet->questions = question;
  3418. question->unicast = unicast;
  3419. question->type = MDNS_TYPE_SDPTR;
  3420. question->host = NULL;
  3421. question->service = strdup(a->service->service);
  3422. question->proto = strdup(a->service->proto);
  3423. question->domain = strdup(MDNS_DEFAULT_DOMAIN);
  3424. if (!question->service || !question->proto || !question->domain) {
  3425. goto clear_rx_packet;
  3426. }
  3427. a = a->next;
  3428. }
  3429. continue;
  3430. }
  3431. if (!_mdns_name_is_ours(name)) {
  3432. continue;
  3433. }
  3434. if (type == MDNS_TYPE_ANY && !_str_null_or_empty(name->host)) {
  3435. parsed_packet->probe = true;
  3436. }
  3437. mdns_parsed_question_t *question = (mdns_parsed_question_t *)calloc(1, sizeof(mdns_parsed_question_t));
  3438. if (!question) {
  3439. HOOK_MALLOC_FAILED;
  3440. goto clear_rx_packet;
  3441. }
  3442. question->next = parsed_packet->questions;
  3443. parsed_packet->questions = question;
  3444. question->unicast = unicast;
  3445. question->type = type;
  3446. question->sub = name->sub;
  3447. if (_mdns_strdup_check(&(question->host), name->host)
  3448. || _mdns_strdup_check(&(question->service), name->service)
  3449. || _mdns_strdup_check(&(question->proto), name->proto)
  3450. || _mdns_strdup_check(&(question->domain), name->domain)) {
  3451. goto clear_rx_packet;
  3452. }
  3453. }
  3454. }
  3455. if (header.questions && !parsed_packet->questions && !parsed_packet->discovery && !header.answers) {
  3456. goto clear_rx_packet;
  3457. } else if (header.answers || header.servers || header.additional) {
  3458. uint16_t recordIndex = 0;
  3459. while (content < (data + len)) {
  3460. content = _mdns_parse_fqdn(data, content, name, len);
  3461. if (!content) {
  3462. goto clear_rx_packet;//error
  3463. }
  3464. if (content + MDNS_LEN_OFFSET + 1 >= data + len) {
  3465. goto clear_rx_packet; // malformed packet, won't read behind it
  3466. }
  3467. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  3468. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  3469. uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET);
  3470. uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET);
  3471. const uint8_t *data_ptr = content + MDNS_DATA_OFFSET;
  3472. mdns_class &= 0x7FFF;
  3473. content = data_ptr + data_len;
  3474. if (content > (data + len)) {
  3475. goto clear_rx_packet;
  3476. }
  3477. bool discovery = false;
  3478. bool ours = false;
  3479. mdns_srv_item_t *service = NULL;
  3480. mdns_parsed_record_type_t record_type = MDNS_ANSWER;
  3481. if (recordIndex >= (header.answers + header.servers)) {
  3482. record_type = MDNS_EXTRA;
  3483. } else if (recordIndex >= (header.answers)) {
  3484. record_type = MDNS_NS;
  3485. }
  3486. recordIndex++;
  3487. if (type == MDNS_TYPE_NSEC || type == MDNS_TYPE_OPT) {
  3488. //skip NSEC and OPT
  3489. continue;
  3490. }
  3491. if (parsed_packet->discovery && _mdns_name_is_discovery(name, type)) {
  3492. discovery = true;
  3493. } else if (!name->sub && _mdns_name_is_ours(name)) {
  3494. ours = true;
  3495. if (name->service[0] && name->proto[0]) {
  3496. service = _mdns_get_service_item(name->service, name->proto, NULL);
  3497. }
  3498. } else {
  3499. if ((header.flags & MDNS_FLAGS_QUERY_REPSONSE) == 0 || record_type == MDNS_NS) {
  3500. //skip this record
  3501. continue;
  3502. }
  3503. search_result = _mdns_search_find_from(_mdns_server->search_once, name, type, packet->tcpip_if, packet->ip_protocol);
  3504. browse_result = _mdns_browse_find_from(_mdns_server->browse, name, type, packet->tcpip_if, packet->ip_protocol);
  3505. if (browse_result) {
  3506. if (!out_sync_browse) {
  3507. // will be freed in function `_mdns_browse_sync`
  3508. out_sync_browse = (mdns_browse_sync_t *)malloc(sizeof(mdns_browse_sync_t));
  3509. if (!out_sync_browse) {
  3510. HOOK_MALLOC_FAILED;
  3511. goto clear_rx_packet;
  3512. }
  3513. out_sync_browse->browse = browse_result;
  3514. out_sync_browse->sync_result = NULL;
  3515. }
  3516. if (!browse_result_service) {
  3517. browse_result_service = (char *)malloc(MDNS_NAME_BUF_LEN);
  3518. if (!browse_result_service) {
  3519. HOOK_MALLOC_FAILED;
  3520. goto clear_rx_packet;
  3521. }
  3522. }
  3523. memcpy(browse_result_service, browse_result->service, MDNS_NAME_BUF_LEN);
  3524. if (!browse_result_proto) {
  3525. browse_result_proto = (char *)malloc(MDNS_NAME_BUF_LEN);
  3526. if (!browse_result_proto) {
  3527. HOOK_MALLOC_FAILED;
  3528. goto clear_rx_packet;
  3529. }
  3530. }
  3531. memcpy(browse_result_proto, browse_result->proto, MDNS_NAME_BUF_LEN);
  3532. if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) {
  3533. if (!browse_result_instance) {
  3534. browse_result_instance = (char *)malloc(MDNS_NAME_BUF_LEN);
  3535. if (!browse_result_instance) {
  3536. HOOK_MALLOC_FAILED;
  3537. goto clear_rx_packet;
  3538. }
  3539. }
  3540. memcpy(browse_result_instance, name->host, MDNS_NAME_BUF_LEN);
  3541. }
  3542. }
  3543. }
  3544. if (type == MDNS_TYPE_PTR) {
  3545. if (!_mdns_parse_fqdn(data, data_ptr, name, len)) {
  3546. continue;//error
  3547. }
  3548. if (search_result) {
  3549. _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
  3550. packet->tcpip_if, packet->ip_protocol, ttl);
  3551. } else if ((discovery || ours) && !name->sub && _mdns_name_is_ours(name)) {
  3552. if (name->host[0]) {
  3553. service = _mdns_get_service_item_instance(name->host, name->service, name->proto, NULL);
  3554. } else {
  3555. service = _mdns_get_service_item(name->service, name->proto, NULL);
  3556. }
  3557. if (discovery && service) {
  3558. _mdns_remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service);
  3559. } else if (service && parsed_packet->questions && !parsed_packet->probe) {
  3560. _mdns_remove_parsed_question(parsed_packet, type, service);
  3561. } else if (service) {
  3562. //check if TTL is more than half of the full TTL value (4500)
  3563. if (ttl > (MDNS_ANSWER_PTR_TTL / 2)) {
  3564. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  3565. }
  3566. }
  3567. if (service) {
  3568. mdns_parsed_record_t *record = malloc(sizeof(mdns_parsed_record_t));
  3569. if (!record) {
  3570. HOOK_MALLOC_FAILED;
  3571. goto clear_rx_packet;
  3572. }
  3573. record->next = parsed_packet->records;
  3574. parsed_packet->records = record;
  3575. record->type = MDNS_TYPE_PTR;
  3576. record->record_type = MDNS_ANSWER;
  3577. record->ttl = ttl;
  3578. record->host = NULL;
  3579. record->service = NULL;
  3580. record->proto = NULL;
  3581. if (name->host[0]) {
  3582. record->host = malloc(MDNS_NAME_BUF_LEN);
  3583. if (!record->host) {
  3584. HOOK_MALLOC_FAILED;
  3585. goto clear_rx_packet;
  3586. }
  3587. memcpy(record->host, name->host, MDNS_NAME_BUF_LEN);
  3588. }
  3589. if (name->service[0]) {
  3590. record->service = malloc(MDNS_NAME_BUF_LEN);
  3591. if (!record->service) {
  3592. HOOK_MALLOC_FAILED;
  3593. goto clear_rx_packet;
  3594. }
  3595. memcpy(record->service, name->service, MDNS_NAME_BUF_LEN);
  3596. }
  3597. if (name->proto[0]) {
  3598. record->proto = malloc(MDNS_NAME_BUF_LEN);
  3599. if (!record->proto) {
  3600. HOOK_MALLOC_FAILED;
  3601. goto clear_rx_packet;
  3602. }
  3603. memcpy(record->proto, name->proto, MDNS_NAME_BUF_LEN);
  3604. }
  3605. }
  3606. }
  3607. } else if (type == MDNS_TYPE_SRV) {
  3608. mdns_result_t *result = NULL;
  3609. if (search_result && search_result->type == MDNS_TYPE_PTR) {
  3610. result = search_result->result;
  3611. while (result) {
  3612. if (_mdns_get_esp_netif(packet->tcpip_if) == result->esp_netif
  3613. && packet->ip_protocol == result->ip_protocol
  3614. && result->instance_name && !strcmp(name->host, result->instance_name)) {
  3615. break;
  3616. }
  3617. result = result->next;
  3618. }
  3619. if (!result) {
  3620. result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
  3621. packet->tcpip_if, packet->ip_protocol, ttl);
  3622. if (!result) {
  3623. continue;//error
  3624. }
  3625. }
  3626. }
  3627. bool is_selfhosted = _mdns_name_is_selfhosted(name);
  3628. if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name, len)) {
  3629. continue;//error
  3630. }
  3631. if (data_ptr + MDNS_SRV_PORT_OFFSET + 1 >= data + len) {
  3632. goto clear_rx_packet; // malformed packet, won't read behind it
  3633. }
  3634. uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
  3635. uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET);
  3636. uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET);
  3637. if (browse_result) {
  3638. _mdns_browse_result_add_srv(browse_result, name->host, browse_result_instance, browse_result_service,
  3639. browse_result_proto, port, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse);
  3640. }
  3641. if (search_result) {
  3642. if (search_result->type == MDNS_TYPE_PTR) {
  3643. if (!result->hostname) { // assign host/port for this entry only if not previously set
  3644. result->port = port;
  3645. result->hostname = strdup(name->host);
  3646. }
  3647. } else {
  3648. _mdns_search_result_add_srv(search_result, name->host, port, packet->tcpip_if, packet->ip_protocol, ttl);
  3649. }
  3650. } else if (ours) {
  3651. if (parsed_packet->questions && !parsed_packet->probe) {
  3652. _mdns_remove_parsed_question(parsed_packet, type, service);
  3653. continue;
  3654. } else if (parsed_packet->distributed) {
  3655. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  3656. continue;
  3657. }
  3658. if (!is_selfhosted) {
  3659. continue;
  3660. }
  3661. //detect collision (-1=won, 0=none, 1=lost)
  3662. int col = 0;
  3663. if (mdns_class > 1) {
  3664. col = 1;
  3665. } else if (!mdns_class) {
  3666. col = -1;
  3667. } else if (service) { // only detect srv collision if service existed
  3668. col = _mdns_check_srv_collision(service->service, priority, weight, port, name->host, name->domain);
  3669. }
  3670. if (service && col && (parsed_packet->probe || parsed_packet->authoritative)) {
  3671. if (col > 0 || !port) {
  3672. do_not_reply = true;
  3673. if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3674. _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
  3675. if (!_str_null_or_empty(service->service->instance)) {
  3676. char *new_instance = _mdns_mangle_name((char *)service->service->instance);
  3677. if (new_instance) {
  3678. free((char *)service->service->instance);
  3679. service->service->instance = new_instance;
  3680. }
  3681. _mdns_probe_all_pcbs(&service, 1, false, false);
  3682. } else if (!_str_null_or_empty(_mdns_server->instance)) {
  3683. char *new_instance = _mdns_mangle_name((char *)_mdns_server->instance);
  3684. if (new_instance) {
  3685. free((char *)_mdns_server->instance);
  3686. _mdns_server->instance = new_instance;
  3687. }
  3688. _mdns_restart_all_pcbs_no_instance();
  3689. } else {
  3690. char *new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
  3691. if (new_host) {
  3692. _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
  3693. free((char *)_mdns_server->hostname);
  3694. _mdns_server->hostname = new_host;
  3695. _mdns_self_host.hostname = new_host;
  3696. }
  3697. _mdns_restart_all_pcbs();
  3698. }
  3699. } else if (service) {
  3700. _mdns_pcb_send_bye(packet->tcpip_if, packet->ip_protocol, &service, 1, false);
  3701. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, false);
  3702. }
  3703. }
  3704. } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions) {
  3705. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  3706. }
  3707. }
  3708. } else if (type == MDNS_TYPE_TXT) {
  3709. mdns_txt_item_t *txt = NULL;
  3710. uint8_t *txt_value_len = NULL;
  3711. size_t txt_count = 0;
  3712. mdns_result_t *result = NULL;
  3713. if (browse_result) {
  3714. _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
  3715. _mdns_browse_result_add_txt(browse_result, browse_result_instance, browse_result_service, browse_result_proto,
  3716. txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse);
  3717. }
  3718. if (search_result) {
  3719. if (search_result->type == MDNS_TYPE_PTR) {
  3720. result = search_result->result;
  3721. while (result) {
  3722. if (_mdns_get_esp_netif(packet->tcpip_if) == result->esp_netif
  3723. && packet->ip_protocol == result->ip_protocol
  3724. && result->instance_name && !strcmp(name->host, result->instance_name)) {
  3725. break;
  3726. }
  3727. result = result->next;
  3728. }
  3729. if (!result) {
  3730. result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
  3731. packet->tcpip_if, packet->ip_protocol, ttl);
  3732. if (!result) {
  3733. continue;//error
  3734. }
  3735. }
  3736. if (!result->txt) {
  3737. _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
  3738. if (txt_count) {
  3739. result->txt = txt;
  3740. result->txt_count = txt_count;
  3741. result->txt_value_len = txt_value_len;
  3742. }
  3743. }
  3744. } else {
  3745. _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
  3746. if (txt_count) {
  3747. _mdns_search_result_add_txt(search_result, txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl);
  3748. }
  3749. }
  3750. } else if (ours) {
  3751. if (parsed_packet->questions && !parsed_packet->probe && service) {
  3752. _mdns_remove_parsed_question(parsed_packet, type, service);
  3753. continue;
  3754. }
  3755. if (!_mdns_name_is_selfhosted(name)) {
  3756. continue;
  3757. }
  3758. //detect collision (-1=won, 0=none, 1=lost)
  3759. int col = 0;
  3760. if (mdns_class > 1) {
  3761. col = 1;
  3762. } else if (!mdns_class) {
  3763. col = -1;
  3764. } else if (service) { // only detect txt collision if service existed
  3765. col = _mdns_check_txt_collision(service->service, data_ptr, data_len);
  3766. }
  3767. if (col && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running && service) {
  3768. do_not_reply = true;
  3769. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, true);
  3770. } else if (ttl > (MDNS_ANSWER_TXT_TTL / 2) && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3771. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  3772. }
  3773. }
  3774. }
  3775. #ifdef CONFIG_LWIP_IPV6
  3776. else if (type == MDNS_TYPE_AAAA) {//ipv6
  3777. esp_ip_addr_t ip6;
  3778. ip6.type = ESP_IPADDR_TYPE_V6;
  3779. memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
  3780. if (browse_result) {
  3781. _mdns_browse_result_add_ip(browse_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse);
  3782. }
  3783. if (search_result) {
  3784. //check for more applicable searches (PTR & A/AAAA at the same time)
  3785. while (search_result) {
  3786. _mdns_search_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl);
  3787. search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
  3788. }
  3789. } else if (ours) {
  3790. if (parsed_packet->questions && !parsed_packet->probe) {
  3791. _mdns_remove_parsed_question(parsed_packet, type, NULL);
  3792. continue;
  3793. }
  3794. if (!_mdns_name_is_selfhosted(name)) {
  3795. continue;
  3796. }
  3797. //detect collision (-1=won, 0=none, 1=lost)
  3798. int col = 0;
  3799. if (mdns_class > 1) {
  3800. col = 1;
  3801. } else if (!mdns_class) {
  3802. col = -1;
  3803. } else {
  3804. col = _mdns_check_aaaa_collision(&(ip6.u_addr.ip6), packet->tcpip_if);
  3805. }
  3806. if (col == 2) {
  3807. goto clear_rx_packet;
  3808. } else if (col == 1) {
  3809. do_not_reply = true;
  3810. if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3811. if (col && (parsed_packet->probe || parsed_packet->authoritative)) {
  3812. _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
  3813. char *new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
  3814. if (new_host) {
  3815. _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
  3816. free((char *)_mdns_server->hostname);
  3817. _mdns_server->hostname = new_host;
  3818. _mdns_self_host.hostname = new_host;
  3819. }
  3820. _mdns_restart_all_pcbs();
  3821. }
  3822. } else {
  3823. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true);
  3824. }
  3825. } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3826. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL);
  3827. }
  3828. }
  3829. }
  3830. #endif /* CONFIG_LWIP_IPV6 */
  3831. #ifdef CONFIG_LWIP_IPV4
  3832. else if (type == MDNS_TYPE_A) {
  3833. esp_ip_addr_t ip;
  3834. ip.type = ESP_IPADDR_TYPE_V4;
  3835. memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
  3836. if (browse_result) {
  3837. _mdns_browse_result_add_ip(browse_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse);
  3838. }
  3839. if (search_result) {
  3840. //check for more applicable searches (PTR & A/AAAA at the same time)
  3841. while (search_result) {
  3842. _mdns_search_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl);
  3843. search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
  3844. }
  3845. } else if (ours) {
  3846. if (parsed_packet->questions && !parsed_packet->probe) {
  3847. _mdns_remove_parsed_question(parsed_packet, type, NULL);
  3848. continue;
  3849. }
  3850. if (!_mdns_name_is_selfhosted(name)) {
  3851. continue;
  3852. }
  3853. //detect collision (-1=won, 0=none, 1=lost)
  3854. int col = 0;
  3855. if (mdns_class > 1) {
  3856. col = 1;
  3857. } else if (!mdns_class) {
  3858. col = -1;
  3859. } else {
  3860. col = _mdns_check_a_collision(&(ip.u_addr.ip4), packet->tcpip_if);
  3861. }
  3862. if (col == 2) {
  3863. goto clear_rx_packet;
  3864. } else if (col == 1) {
  3865. do_not_reply = true;
  3866. if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3867. if (col && (parsed_packet->probe || parsed_packet->authoritative)) {
  3868. _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
  3869. char *new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
  3870. if (new_host) {
  3871. _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
  3872. free((char *)_mdns_server->hostname);
  3873. _mdns_server->hostname = new_host;
  3874. _mdns_self_host.hostname = new_host;
  3875. }
  3876. _mdns_restart_all_pcbs();
  3877. }
  3878. } else {
  3879. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true);
  3880. }
  3881. } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3882. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL);
  3883. }
  3884. }
  3885. }
  3886. #endif /* CONFIG_LWIP_IPV4 */
  3887. }
  3888. //end while
  3889. if (parsed_packet->authoritative) {
  3890. _mdns_search_finish_done();
  3891. }
  3892. }
  3893. if (!do_not_reply && _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].state > PCB_PROBE_3 && (parsed_packet->questions || parsed_packet->discovery)) {
  3894. _mdns_create_answer_from_parsed_packet(parsed_packet);
  3895. }
  3896. if (out_sync_browse) {
  3897. #ifdef MDNS_ENABLE_DEBUG
  3898. _mdns_dbg_printf("Browse %s%s total result:", out_sync_browse->browse->service, out_sync_browse->browse->proto);
  3899. debug_printf_browse_result_all(out_sync_browse->browse->result);
  3900. #endif // MDNS_ENABLE_DEBUG
  3901. if (out_sync_browse->sync_result) {
  3902. #ifdef MDNS_ENABLE_DEBUG
  3903. _mdns_dbg_printf("Changed result:");
  3904. debug_printf_browse_result_all(out_sync_browse->sync_result->result);
  3905. #endif // MDNS_ENABLE_DEBUG
  3906. _mdns_sync_browse_action(ACTION_BROWSE_SYNC, out_sync_browse);
  3907. } else {
  3908. free(out_sync_browse);
  3909. }
  3910. }
  3911. clear_rx_packet:
  3912. while (parsed_packet->questions) {
  3913. mdns_parsed_question_t *question = parsed_packet->questions;
  3914. parsed_packet->questions = parsed_packet->questions->next;
  3915. if (question->host) {
  3916. free(question->host);
  3917. }
  3918. if (question->service) {
  3919. free(question->service);
  3920. }
  3921. if (question->proto) {
  3922. free(question->proto);
  3923. }
  3924. if (question->domain) {
  3925. free(question->domain);
  3926. }
  3927. free(question);
  3928. }
  3929. while (parsed_packet->records) {
  3930. mdns_parsed_record_t *record = parsed_packet->records;
  3931. parsed_packet->records = parsed_packet->records->next;
  3932. if (record->host) {
  3933. free(record->host);
  3934. }
  3935. if (record->service) {
  3936. free(record->service);
  3937. }
  3938. if (record->proto) {
  3939. free(record->proto);
  3940. }
  3941. record->next = NULL;
  3942. free(record);
  3943. }
  3944. free(parsed_packet);
  3945. if (browse_result_instance) {
  3946. free(browse_result_instance);
  3947. }
  3948. if (browse_result_service) {
  3949. free(browse_result_service);
  3950. }
  3951. if (browse_result_proto) {
  3952. free(browse_result_proto);
  3953. }
  3954. }
  3955. /**
  3956. * @brief Enable mDNS interface
  3957. */
  3958. void _mdns_enable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  3959. {
  3960. if (!mdns_is_netif_ready(tcpip_if, ip_protocol)) {
  3961. if (_mdns_pcb_init(tcpip_if, ip_protocol)) {
  3962. _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].failed_probes = 0;
  3963. return;
  3964. }
  3965. }
  3966. _mdns_restart_pcb(tcpip_if, ip_protocol);
  3967. }
  3968. /**
  3969. * @brief Disable mDNS interface
  3970. */
  3971. void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  3972. {
  3973. _mdns_clean_netif_ptr(tcpip_if);
  3974. if (mdns_is_netif_ready(tcpip_if, ip_protocol)) {
  3975. _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
  3976. mdns_pcb_deinit_local(tcpip_if, ip_protocol);
  3977. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  3978. if (other_if != MDNS_MAX_INTERFACES && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) {
  3979. _mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF;
  3980. _mdns_enable_pcb(other_if, ip_protocol);
  3981. }
  3982. }
  3983. _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state = PCB_OFF;
  3984. }
  3985. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  3986. static inline char nibble_to_hex(int var)
  3987. {
  3988. return var > 9 ? var - 10 + 'a' : var + '0';
  3989. }
  3990. #endif
  3991. /**
  3992. * @brief Performs interface changes based on system events or custom commands
  3993. */
  3994. static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
  3995. {
  3996. if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) {
  3997. return;
  3998. }
  3999. if (action & MDNS_EVENT_ENABLE_IP4) {
  4000. _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V4);
  4001. }
  4002. if (action & MDNS_EVENT_ENABLE_IP6) {
  4003. _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
  4004. }
  4005. if (action & MDNS_EVENT_DISABLE_IP4) {
  4006. _mdns_disable_pcb(mdns_if, MDNS_IP_PROTOCOL_V4);
  4007. }
  4008. if (action & MDNS_EVENT_DISABLE_IP6) {
  4009. _mdns_disable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
  4010. }
  4011. if (action & MDNS_EVENT_ANNOUNCE_IP4) {
  4012. _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4, NULL, 0, true);
  4013. }
  4014. if (action & MDNS_EVENT_ANNOUNCE_IP6) {
  4015. _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V6, NULL, 0, true);
  4016. }
  4017. #ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES
  4018. #ifdef CONFIG_LWIP_IPV4
  4019. if (action & MDNS_EVENT_IP4_REVERSE_LOOKUP) {
  4020. esp_netif_ip_info_t if_ip_info;
  4021. if (esp_netif_get_ip_info(_mdns_get_esp_netif(mdns_if), &if_ip_info) == ESP_OK) {
  4022. esp_ip4_addr_t *ip = &if_ip_info.ip;
  4023. char *reverse_query_name = NULL;
  4024. if (asprintf(&reverse_query_name, "%d.%d.%d.%d.in-addr",
  4025. esp_ip4_addr4_16(ip), esp_ip4_addr3_16(ip),
  4026. esp_ip4_addr2_16(ip), esp_ip4_addr1_16(ip)) > 0 && reverse_query_name) {
  4027. ESP_LOGD(TAG, "Registered reverse query: %s.arpa", reverse_query_name);
  4028. _mdns_delegate_hostname_add(reverse_query_name, NULL);
  4029. }
  4030. }
  4031. }
  4032. #endif /* CONFIG_LWIP_IPV4 */
  4033. #ifdef CONFIG_LWIP_IPV6
  4034. if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) {
  4035. esp_ip6_addr_t addr6;
  4036. if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(mdns_if), &addr6) && !_ipv6_address_is_zero(addr6)) {
  4037. uint8_t *paddr = (uint8_t *)&addr6.addr;
  4038. const char sub[] = "ip6";
  4039. const size_t query_name_size = 4 * sizeof(addr6.addr) /* (2 nibbles + 2 dots)/per byte of IP address */ + sizeof(sub);
  4040. char *reverse_query_name = malloc(query_name_size);
  4041. if (reverse_query_name) {
  4042. char *ptr = &reverse_query_name[query_name_size]; // point to the end
  4043. memcpy(ptr - sizeof(sub), sub, sizeof(sub)); // copy the IP sub-domain
  4044. ptr -= sizeof(sub) + 1; // move before the sub-domain
  4045. while (reverse_query_name < ptr) { // continue populating reverse query from the end
  4046. *ptr-- = '.'; // nibble by nibble, until we reach the beginning
  4047. *ptr-- = nibble_to_hex(((*paddr) >> 4) & 0x0F);
  4048. *ptr-- = '.';
  4049. *ptr-- = nibble_to_hex((*paddr) & 0x0F);
  4050. paddr++;
  4051. }
  4052. ESP_LOGD(TAG, "Registered reverse query: %s.arpa", reverse_query_name);
  4053. _mdns_delegate_hostname_add(reverse_query_name, NULL);
  4054. }
  4055. }
  4056. }
  4057. #endif /* CONFIG_LWIP_IPV6 */
  4058. #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
  4059. }
  4060. /**
  4061. * @brief Dispatch interface changes based on system events
  4062. */
  4063. static inline void post_mdns_disable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol)
  4064. {
  4065. mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_DISABLE_IP4 : MDNS_EVENT_DISABLE_IP6);
  4066. }
  4067. static inline void post_mdns_enable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol)
  4068. {
  4069. mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ENABLE_IP4 : MDNS_EVENT_ENABLE_IP6);
  4070. }
  4071. static inline void post_mdns_announce_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol)
  4072. {
  4073. mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ANNOUNCE_IP4 : MDNS_EVENT_ANNOUNCE_IP6);
  4074. }
  4075. #if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
  4076. void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base,
  4077. int32_t event_id, void *event_data)
  4078. {
  4079. if (!_mdns_server) {
  4080. return;
  4081. }
  4082. esp_netif_dhcp_status_t dcst;
  4083. #if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP)
  4084. if (event_base == WIFI_EVENT) {
  4085. switch (event_id) {
  4086. case WIFI_EVENT_STA_CONNECTED:
  4087. if (!esp_netif_dhcpc_get_status(esp_netif_from_preset_if(MDNS_IF_STA), &dcst)) {
  4088. if (dcst == ESP_NETIF_DHCP_STOPPED) {
  4089. post_mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
  4090. }
  4091. }
  4092. break;
  4093. case WIFI_EVENT_STA_DISCONNECTED:
  4094. post_mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
  4095. post_mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6);
  4096. break;
  4097. case WIFI_EVENT_AP_START:
  4098. post_mdns_enable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4);
  4099. break;
  4100. case WIFI_EVENT_AP_STOP:
  4101. post_mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4);
  4102. post_mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V6);
  4103. break;
  4104. default:
  4105. break;
  4106. }
  4107. } else
  4108. #endif
  4109. #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
  4110. if (event_base == ETH_EVENT) {
  4111. switch (event_id) {
  4112. case ETHERNET_EVENT_CONNECTED:
  4113. if (!esp_netif_dhcpc_get_status(esp_netif_from_preset_if(MDNS_IF_ETH), &dcst)) {
  4114. if (dcst == ESP_NETIF_DHCP_STOPPED) {
  4115. post_mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
  4116. }
  4117. }
  4118. break;
  4119. case ETHERNET_EVENT_DISCONNECTED:
  4120. post_mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
  4121. post_mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V6);
  4122. break;
  4123. default:
  4124. break;
  4125. }
  4126. } else
  4127. #endif
  4128. if (event_base == IP_EVENT) {
  4129. switch (event_id) {
  4130. case IP_EVENT_STA_GOT_IP:
  4131. post_mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
  4132. post_mdns_announce_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6);
  4133. break;
  4134. #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
  4135. case IP_EVENT_ETH_GOT_IP:
  4136. post_mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
  4137. break;
  4138. #endif
  4139. case IP_EVENT_GOT_IP6: {
  4140. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
  4141. mdns_if_t mdns_if = _mdns_get_if_from_esp_netif(event->esp_netif);
  4142. if (mdns_if >= MDNS_MAX_INTERFACES) {
  4143. return;
  4144. }
  4145. post_mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
  4146. post_mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4);
  4147. mdns_browse_t *browse = _mdns_server->browse;
  4148. while (browse) {
  4149. _mdns_browse_send(browse, mdns_if);
  4150. browse = browse->next;
  4151. }
  4152. }
  4153. break;
  4154. default:
  4155. break;
  4156. }
  4157. }
  4158. }
  4159. #endif /* CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH */
  4160. /*
  4161. * MDNS Search
  4162. * */
  4163. /**
  4164. * @brief Free search structure (except the results)
  4165. */
  4166. static void _mdns_search_free(mdns_search_once_t *search)
  4167. {
  4168. free(search->instance);
  4169. free(search->service);
  4170. free(search->proto);
  4171. vSemaphoreDelete(search->done_semaphore);
  4172. free(search);
  4173. }
  4174. /**
  4175. * @brief Allocate new search structure
  4176. */
  4177. static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast,
  4178. uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier)
  4179. {
  4180. mdns_search_once_t *search = (mdns_search_once_t *)malloc(sizeof(mdns_search_once_t));
  4181. if (!search) {
  4182. HOOK_MALLOC_FAILED;
  4183. return NULL;
  4184. }
  4185. memset(search, 0, sizeof(mdns_search_once_t));
  4186. search->done_semaphore = xSemaphoreCreateBinary();
  4187. if (!search->done_semaphore) {
  4188. free(search);
  4189. return NULL;
  4190. }
  4191. if (!_str_null_or_empty(name)) {
  4192. search->instance = strndup(name, MDNS_NAME_BUF_LEN - 1);
  4193. if (!search->instance) {
  4194. _mdns_search_free(search);
  4195. return NULL;
  4196. }
  4197. }
  4198. if (!_str_null_or_empty(service)) {
  4199. search->service = strndup(service, MDNS_NAME_BUF_LEN - 1);
  4200. if (!search->service) {
  4201. _mdns_search_free(search);
  4202. return NULL;
  4203. }
  4204. }
  4205. if (!_str_null_or_empty(proto)) {
  4206. search->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
  4207. if (!search->proto) {
  4208. _mdns_search_free(search);
  4209. return NULL;
  4210. }
  4211. }
  4212. search->type = type;
  4213. search->unicast = unicast;
  4214. search->timeout = timeout;
  4215. search->num_results = 0;
  4216. search->max_results = max_results;
  4217. search->result = NULL;
  4218. search->state = SEARCH_INIT;
  4219. search->sent_at = 0;
  4220. search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS;
  4221. search->notifier = notifier;
  4222. search->next = NULL;
  4223. return search;
  4224. }
  4225. /**
  4226. * @brief Mark search as finished and remove it from search chain
  4227. */
  4228. static void _mdns_search_finish(mdns_search_once_t *search)
  4229. {
  4230. search->state = SEARCH_OFF;
  4231. queueDetach(mdns_search_once_t, _mdns_server->search_once, search);
  4232. if (search->notifier) {
  4233. search->notifier(search);
  4234. }
  4235. xSemaphoreGive(search->done_semaphore);
  4236. }
  4237. /**
  4238. * @brief Add new search to the search chain
  4239. */
  4240. static void _mdns_search_add(mdns_search_once_t *search)
  4241. {
  4242. search->next = _mdns_server->search_once;
  4243. _mdns_server->search_once = search;
  4244. }
  4245. /**
  4246. * @brief Called from parser to finish any searches that have reached maximum results
  4247. */
  4248. static void _mdns_search_finish_done(void)
  4249. {
  4250. mdns_search_once_t *search = _mdns_server->search_once;
  4251. mdns_search_once_t *s = NULL;
  4252. while (search) {
  4253. s = search;
  4254. search = search->next;
  4255. if (s->max_results && s->num_results >= s->max_results) {
  4256. _mdns_search_finish(s);
  4257. }
  4258. }
  4259. }
  4260. /**
  4261. * @brief Create linked IP (copy) from parsed one
  4262. */
  4263. static mdns_ip_addr_t *_mdns_result_addr_create_ip(esp_ip_addr_t *ip)
  4264. {
  4265. mdns_ip_addr_t *a = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
  4266. if (!a) {
  4267. HOOK_MALLOC_FAILED;
  4268. return NULL;
  4269. }
  4270. memset(a, 0, sizeof(mdns_ip_addr_t));
  4271. a->addr.type = ip->type;
  4272. if (ip->type == ESP_IPADDR_TYPE_V6) {
  4273. memcpy(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16);
  4274. } else {
  4275. a->addr.u_addr.ip4.addr = ip->u_addr.ip4.addr;
  4276. }
  4277. return a;
  4278. }
  4279. static inline void _mdns_result_update_ttl(mdns_result_t *r, uint32_t ttl)
  4280. {
  4281. r->ttl = r->ttl < ttl ? r->ttl : ttl;
  4282. }
  4283. /**
  4284. * @brief Chain new IP to search result
  4285. */
  4286. static void _mdns_result_add_ip(mdns_result_t *r, esp_ip_addr_t *ip)
  4287. {
  4288. mdns_ip_addr_t *a = r->addr;
  4289. while (a) {
  4290. if (a->addr.type == ip->type) {
  4291. #ifdef CONFIG_LWIP_IPV4
  4292. if (a->addr.type == ESP_IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) {
  4293. return;
  4294. }
  4295. #endif
  4296. #ifdef CONFIG_LWIP_IPV6
  4297. if (a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) {
  4298. return;
  4299. }
  4300. #endif
  4301. }
  4302. a = a->next;
  4303. }
  4304. a = _mdns_result_addr_create_ip(ip);
  4305. if (!a) {
  4306. return;
  4307. }
  4308. a->next = r->addr;
  4309. r->addr = a;
  4310. }
  4311. /**
  4312. * @brief Called from parser to add A/AAAA data to search result
  4313. */
  4314. static void _mdns_search_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip,
  4315. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
  4316. {
  4317. mdns_result_t *r = NULL;
  4318. mdns_ip_addr_t *a = NULL;
  4319. if ((search->type == MDNS_TYPE_A && ip->type == ESP_IPADDR_TYPE_V4)
  4320. || (search->type == MDNS_TYPE_AAAA && ip->type == ESP_IPADDR_TYPE_V6)
  4321. || search->type == MDNS_TYPE_ANY) {
  4322. r = search->result;
  4323. while (r) {
  4324. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) {
  4325. _mdns_result_add_ip(r, ip);
  4326. _mdns_result_update_ttl(r, ttl);
  4327. return;
  4328. }
  4329. r = r->next;
  4330. }
  4331. if (!search->max_results || search->num_results < search->max_results) {
  4332. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  4333. if (!r) {
  4334. HOOK_MALLOC_FAILED;
  4335. return;
  4336. }
  4337. memset(r, 0, sizeof(mdns_result_t));
  4338. a = _mdns_result_addr_create_ip(ip);
  4339. if (!a) {
  4340. free(r);
  4341. return;
  4342. }
  4343. a->next = r->addr;
  4344. r->hostname = strdup(hostname);
  4345. r->addr = a;
  4346. r->esp_netif = _mdns_get_esp_netif(tcpip_if);
  4347. r->ip_protocol = ip_protocol;
  4348. r->next = search->result;
  4349. r->ttl = ttl;
  4350. search->result = r;
  4351. search->num_results++;
  4352. }
  4353. } else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) {
  4354. r = search->result;
  4355. while (r) {
  4356. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
  4357. _mdns_result_add_ip(r, ip);
  4358. _mdns_result_update_ttl(r, ttl);
  4359. break;
  4360. }
  4361. r = r->next;
  4362. }
  4363. }
  4364. }
  4365. /**
  4366. * @brief Called from parser to add PTR data to search result
  4367. */
  4368. static mdns_result_t *_mdns_search_result_add_ptr(mdns_search_once_t *search, const char *instance,
  4369. const char *service_type, const char *proto, mdns_if_t tcpip_if,
  4370. mdns_ip_protocol_t ip_protocol, uint32_t ttl)
  4371. {
  4372. mdns_result_t *r = search->result;
  4373. while (r) {
  4374. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) {
  4375. _mdns_result_update_ttl(r, ttl);
  4376. return r;
  4377. }
  4378. r = r->next;
  4379. }
  4380. if (!search->max_results || search->num_results < search->max_results) {
  4381. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  4382. if (!r) {
  4383. HOOK_MALLOC_FAILED;
  4384. return NULL;
  4385. }
  4386. memset(r, 0, sizeof(mdns_result_t));
  4387. r->instance_name = strdup(instance);
  4388. r->service_type = strdup(service_type);
  4389. r->proto = strdup(proto);
  4390. if (!r->instance_name) {
  4391. free(r);
  4392. return NULL;
  4393. }
  4394. r->esp_netif = _mdns_get_esp_netif(tcpip_if);
  4395. r->ip_protocol = ip_protocol;
  4396. r->ttl = ttl;
  4397. r->next = search->result;
  4398. search->result = r;
  4399. search->num_results++;
  4400. return r;
  4401. }
  4402. return NULL;
  4403. }
  4404. /**
  4405. * @brief Called from parser to add SRV data to search result
  4406. */
  4407. static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
  4408. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
  4409. {
  4410. mdns_result_t *r = search->result;
  4411. while (r) {
  4412. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
  4413. _mdns_result_update_ttl(r, ttl);
  4414. return;
  4415. }
  4416. r = r->next;
  4417. }
  4418. if (!search->max_results || search->num_results < search->max_results) {
  4419. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  4420. if (!r) {
  4421. HOOK_MALLOC_FAILED;
  4422. return;
  4423. }
  4424. memset(r, 0, sizeof(mdns_result_t));
  4425. r->hostname = strdup(hostname);
  4426. if (!r->hostname) {
  4427. free(r);
  4428. return;
  4429. }
  4430. if (search->instance) {
  4431. r->instance_name = strdup(search->instance);
  4432. }
  4433. r->service_type = strdup(search->service);
  4434. r->proto = strdup(search->proto);
  4435. r->port = port;
  4436. r->esp_netif = _mdns_get_esp_netif(tcpip_if);
  4437. r->ip_protocol = ip_protocol;
  4438. r->ttl = ttl;
  4439. r->next = search->result;
  4440. search->result = r;
  4441. search->num_results++;
  4442. }
  4443. }
  4444. /**
  4445. * @brief Called from parser to add TXT data to search result
  4446. */
  4447. static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
  4448. size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
  4449. uint32_t ttl)
  4450. {
  4451. mdns_result_t *r = search->result;
  4452. while (r) {
  4453. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) {
  4454. if (r->txt) {
  4455. goto free_txt;
  4456. }
  4457. r->txt = txt;
  4458. r->txt_value_len = txt_value_len;
  4459. r->txt_count = txt_count;
  4460. _mdns_result_update_ttl(r, ttl);
  4461. return;
  4462. }
  4463. r = r->next;
  4464. }
  4465. if (!search->max_results || search->num_results < search->max_results) {
  4466. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  4467. if (!r) {
  4468. HOOK_MALLOC_FAILED;
  4469. goto free_txt;
  4470. }
  4471. memset(r, 0, sizeof(mdns_result_t));
  4472. r->txt = txt;
  4473. r->txt_value_len = txt_value_len;
  4474. r->txt_count = txt_count;
  4475. r->esp_netif = _mdns_get_esp_netif(tcpip_if);
  4476. r->ip_protocol = ip_protocol;
  4477. r->ttl = ttl;
  4478. r->next = search->result;
  4479. search->result = r;
  4480. search->num_results++;
  4481. }
  4482. return;
  4483. free_txt:
  4484. for (size_t i = 0; i < txt_count; i++) {
  4485. free((char *)(txt[i].key));
  4486. free((char *)(txt[i].value));
  4487. }
  4488. free(txt);
  4489. free(txt_value_len);
  4490. }
  4491. /**
  4492. * @brief Called from packet parser to find matching running search
  4493. */
  4494. static mdns_search_once_t *_mdns_search_find_from(mdns_search_once_t *s, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  4495. {
  4496. mdns_result_t *r = NULL;
  4497. while (s) {
  4498. if (s->state == SEARCH_OFF) {
  4499. s = s->next;
  4500. continue;
  4501. }
  4502. if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
  4503. if ((s->type == MDNS_TYPE_ANY && s->service != NULL)
  4504. || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV)) {
  4505. s = s->next;
  4506. continue;
  4507. }
  4508. if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) {
  4509. if (!strcasecmp(name->host, s->instance)) {
  4510. return s;
  4511. }
  4512. s = s->next;
  4513. continue;
  4514. }
  4515. r = s->result;
  4516. while (r) {
  4517. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) {
  4518. return s;
  4519. }
  4520. r = r->next;
  4521. }
  4522. s = s->next;
  4523. continue;
  4524. }
  4525. if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) {
  4526. if ((s->type == MDNS_TYPE_ANY && s->service == NULL)
  4527. || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR)) {
  4528. s = s->next;
  4529. continue;
  4530. }
  4531. if (strcasecmp(name->service, s->service)
  4532. || strcasecmp(name->proto, s->proto)) {
  4533. s = s->next;
  4534. continue;
  4535. }
  4536. if (s->type != MDNS_TYPE_PTR) {
  4537. if (s->instance && strcasecmp(name->host, s->instance) == 0) {
  4538. return s;
  4539. }
  4540. s = s->next;
  4541. continue;
  4542. }
  4543. return s;
  4544. }
  4545. if (type == MDNS_TYPE_PTR && type == s->type && !strcasecmp(name->service, s->service) && !strcasecmp(name->proto, s->proto)) {
  4546. return s;
  4547. }
  4548. s = s->next;
  4549. }
  4550. return NULL;
  4551. }
  4552. /**
  4553. * @brief Create search packet for particular interface
  4554. */
  4555. static mdns_tx_packet_t *_mdns_create_search_packet(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  4556. {
  4557. mdns_result_t *r = NULL;
  4558. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  4559. if (!packet) {
  4560. return NULL;
  4561. }
  4562. mdns_out_question_t *q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
  4563. if (!q) {
  4564. HOOK_MALLOC_FAILED;
  4565. _mdns_free_tx_packet(packet);
  4566. return NULL;
  4567. }
  4568. q->next = NULL;
  4569. q->unicast = search->unicast;
  4570. q->type = search->type;
  4571. q->host = search->instance;
  4572. q->service = search->service;
  4573. q->proto = search->proto;
  4574. q->domain = MDNS_DEFAULT_DOMAIN;
  4575. q->own_dynamic_memory = false;
  4576. queueToEnd(mdns_out_question_t, packet->questions, q);
  4577. if (search->type == MDNS_TYPE_PTR) {
  4578. r = search->result;
  4579. while (r) {
  4580. //full record on the same interface is available
  4581. if (r->esp_netif != _mdns_get_esp_netif(tcpip_if) || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) {
  4582. r = r->next;
  4583. continue;
  4584. }
  4585. mdns_out_answer_t *a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
  4586. if (!a) {
  4587. HOOK_MALLOC_FAILED;
  4588. _mdns_free_tx_packet(packet);
  4589. return NULL;
  4590. }
  4591. a->type = MDNS_TYPE_PTR;
  4592. a->service = NULL;
  4593. a->custom_instance = r->instance_name;
  4594. a->custom_service = search->service;
  4595. a->custom_proto = search->proto;
  4596. a->bye = false;
  4597. a->flush = false;
  4598. a->next = NULL;
  4599. queueToEnd(mdns_out_answer_t, packet->answers, a);
  4600. r = r->next;
  4601. }
  4602. }
  4603. return packet;
  4604. }
  4605. /**
  4606. * @brief Send search packet to particular interface
  4607. */
  4608. static void _mdns_search_send_pcb(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  4609. {
  4610. mdns_tx_packet_t *packet = NULL;
  4611. if (mdns_is_netif_ready(tcpip_if, ip_protocol) && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) {
  4612. packet = _mdns_create_search_packet(search, tcpip_if, ip_protocol);
  4613. if (!packet) {
  4614. return;
  4615. }
  4616. _mdns_dispatch_tx_packet(packet);
  4617. _mdns_free_tx_packet(packet);
  4618. }
  4619. }
  4620. /**
  4621. * @brief Send search packet to all available interfaces
  4622. */
  4623. static void _mdns_search_send(mdns_search_once_t *search)
  4624. {
  4625. mdns_search_once_t *queue = _mdns_server->search_once;
  4626. bool found = false;
  4627. // looking for this search in active searches
  4628. while (queue) {
  4629. if (queue == search) {
  4630. found = true;
  4631. break;
  4632. }
  4633. queue = queue->next;
  4634. }
  4635. if (!found) {
  4636. // no longer active -> skip sending this search
  4637. return;
  4638. }
  4639. uint8_t i, j;
  4640. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  4641. for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
  4642. _mdns_search_send_pcb(search, (mdns_if_t)i, (mdns_ip_protocol_t)j);
  4643. }
  4644. }
  4645. }
  4646. static void _mdns_tx_handle_packet(mdns_tx_packet_t *p)
  4647. {
  4648. mdns_tx_packet_t *a = NULL;
  4649. mdns_out_question_t *q = NULL;
  4650. mdns_pcb_t *pcb = &_mdns_server->interfaces[p->tcpip_if].pcbs[p->ip_protocol];
  4651. uint32_t send_after = 1000;
  4652. if (pcb->state == PCB_OFF) {
  4653. _mdns_free_tx_packet(p);
  4654. return;
  4655. }
  4656. _mdns_dispatch_tx_packet(p);
  4657. switch (pcb->state) {
  4658. case PCB_PROBE_1:
  4659. q = p->questions;
  4660. while (q) {
  4661. q->unicast = false;
  4662. q = q->next;
  4663. }
  4664. //fallthrough
  4665. case PCB_PROBE_2:
  4666. _mdns_schedule_tx_packet(p, 250);
  4667. pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1);
  4668. break;
  4669. case PCB_PROBE_3:
  4670. a = _mdns_create_announce_from_probe(p);
  4671. if (!a) {
  4672. _mdns_schedule_tx_packet(p, 250);
  4673. break;
  4674. }
  4675. pcb->probe_running = false;
  4676. pcb->probe_ip = false;
  4677. pcb->probe_services_len = 0;
  4678. pcb->failed_probes = 0;
  4679. free(pcb->probe_services);
  4680. pcb->probe_services = NULL;
  4681. _mdns_free_tx_packet(p);
  4682. p = a;
  4683. send_after = 250;
  4684. //fallthrough
  4685. case PCB_ANNOUNCE_1:
  4686. //fallthrough
  4687. case PCB_ANNOUNCE_2:
  4688. _mdns_schedule_tx_packet(p, send_after);
  4689. pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1);
  4690. break;
  4691. case PCB_ANNOUNCE_3:
  4692. pcb->state = PCB_RUNNING;
  4693. _mdns_free_tx_packet(p);
  4694. break;
  4695. default:
  4696. _mdns_free_tx_packet(p);
  4697. break;
  4698. }
  4699. }
  4700. static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname)
  4701. {
  4702. mdns_srv_item_t *service = _mdns_server->services;
  4703. while (service) {
  4704. if (service->service->hostname &&
  4705. strcmp(service->service->hostname, old_hostname) == 0) {
  4706. free((char *)service->service->hostname);
  4707. service->service->hostname = strdup(new_hostname);
  4708. }
  4709. service = service->next;
  4710. }
  4711. }
  4712. static void _mdns_sync_browse_result_link_free(mdns_browse_sync_t *browse_sync)
  4713. {
  4714. mdns_browse_result_sync_t *current = browse_sync->sync_result;
  4715. mdns_browse_result_sync_t *need_free;
  4716. while (current) {
  4717. need_free = current;
  4718. current = current->next;
  4719. free(need_free);
  4720. }
  4721. free(browse_sync);
  4722. }
  4723. /**
  4724. * @brief Free action data
  4725. */
  4726. static void _mdns_free_action(mdns_action_t *action)
  4727. {
  4728. switch (action->type) {
  4729. case ACTION_HOSTNAME_SET:
  4730. free(action->data.hostname_set.hostname);
  4731. break;
  4732. case ACTION_INSTANCE_SET:
  4733. free(action->data.instance);
  4734. break;
  4735. case ACTION_SEARCH_ADD:
  4736. //fallthrough
  4737. case ACTION_SEARCH_SEND:
  4738. //fallthrough
  4739. case ACTION_SEARCH_END:
  4740. _mdns_search_free(action->data.search_add.search);
  4741. break;
  4742. case ACTION_BROWSE_ADD:
  4743. //fallthrough
  4744. case ACTION_BROWSE_END:
  4745. _mdns_browse_item_free(action->data.browse_add.browse);
  4746. break;
  4747. case ACTION_BROWSE_SYNC:
  4748. _mdns_sync_browse_result_link_free(action->data.browse_sync.browse_sync);
  4749. break;
  4750. case ACTION_TX_HANDLE:
  4751. _mdns_free_tx_packet(action->data.tx_handle.packet);
  4752. break;
  4753. case ACTION_RX_HANDLE:
  4754. _mdns_packet_free(action->data.rx_handle.packet);
  4755. break;
  4756. case ACTION_DELEGATE_HOSTNAME_SET_ADDR:
  4757. case ACTION_DELEGATE_HOSTNAME_ADD:
  4758. free((char *)action->data.delegate_hostname.hostname);
  4759. free_address_list(action->data.delegate_hostname.address_list);
  4760. break;
  4761. case ACTION_DELEGATE_HOSTNAME_REMOVE:
  4762. free((char *)action->data.delegate_hostname.hostname);
  4763. break;
  4764. default:
  4765. break;
  4766. }
  4767. free(action);
  4768. }
  4769. /**
  4770. * @brief Called from service thread to execute given action
  4771. */
  4772. static void _mdns_execute_action(mdns_action_t *action)
  4773. {
  4774. switch (action->type) {
  4775. case ACTION_SYSTEM_EVENT:
  4776. perform_event_action(action->data.sys_event.interface, action->data.sys_event.event_action);
  4777. break;
  4778. case ACTION_HOSTNAME_SET:
  4779. _mdns_send_bye_all_pcbs_no_instance(true);
  4780. _mdns_remap_self_service_hostname(_mdns_server->hostname, action->data.hostname_set.hostname);
  4781. free((char *)_mdns_server->hostname);
  4782. _mdns_server->hostname = action->data.hostname_set.hostname;
  4783. _mdns_self_host.hostname = action->data.hostname_set.hostname;
  4784. _mdns_restart_all_pcbs();
  4785. xSemaphoreGive(_mdns_server->action_sema);
  4786. break;
  4787. case ACTION_INSTANCE_SET:
  4788. _mdns_send_bye_all_pcbs_no_instance(false);
  4789. free((char *)_mdns_server->instance);
  4790. _mdns_server->instance = action->data.instance;
  4791. _mdns_restart_all_pcbs_no_instance();
  4792. break;
  4793. case ACTION_SEARCH_ADD:
  4794. _mdns_search_add(action->data.search_add.search);
  4795. break;
  4796. case ACTION_SEARCH_SEND:
  4797. _mdns_search_send(action->data.search_add.search);
  4798. break;
  4799. case ACTION_SEARCH_END:
  4800. _mdns_search_finish(action->data.search_add.search);
  4801. break;
  4802. case ACTION_BROWSE_ADD:
  4803. _mdns_browse_add(action->data.browse_add.browse);
  4804. break;
  4805. case ACTION_BROWSE_SYNC:
  4806. _mdns_browse_sync(action->data.browse_sync.browse_sync);
  4807. _mdns_sync_browse_result_link_free(action->data.browse_sync.browse_sync);
  4808. break;
  4809. case ACTION_BROWSE_END:
  4810. _mdns_browse_finish(action->data.browse_add.browse);
  4811. break;
  4812. case ACTION_TX_HANDLE: {
  4813. mdns_tx_packet_t *p = _mdns_server->tx_queue_head;
  4814. // packet to be handled should be at tx head, but must be consistent with the one pushed to action queue
  4815. if (p && p == action->data.tx_handle.packet && p->queued) {
  4816. p->queued = false; // clearing, as the packet might be reused (pushed and transmitted again)
  4817. _mdns_server->tx_queue_head = p->next;
  4818. _mdns_tx_handle_packet(p);
  4819. } else {
  4820. ESP_LOGD(TAG, "Skipping transmit of an unexpected packet!");
  4821. }
  4822. }
  4823. break;
  4824. case ACTION_RX_HANDLE:
  4825. mdns_parse_packet(action->data.rx_handle.packet);
  4826. _mdns_packet_free(action->data.rx_handle.packet);
  4827. break;
  4828. case ACTION_DELEGATE_HOSTNAME_ADD:
  4829. if (!_mdns_delegate_hostname_add(action->data.delegate_hostname.hostname,
  4830. action->data.delegate_hostname.address_list)) {
  4831. free((char *)action->data.delegate_hostname.hostname);
  4832. free_address_list(action->data.delegate_hostname.address_list);
  4833. }
  4834. xSemaphoreGive(_mdns_server->action_sema);
  4835. break;
  4836. case ACTION_DELEGATE_HOSTNAME_SET_ADDR:
  4837. if (!_mdns_delegate_hostname_set_address(action->data.delegate_hostname.hostname,
  4838. action->data.delegate_hostname.address_list)) {
  4839. free_address_list(action->data.delegate_hostname.address_list);
  4840. }
  4841. free((char *)action->data.delegate_hostname.hostname);
  4842. break;
  4843. case ACTION_DELEGATE_HOSTNAME_REMOVE:
  4844. _mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname);
  4845. free((char *)action->data.delegate_hostname.hostname);
  4846. break;
  4847. default:
  4848. break;
  4849. }
  4850. free(action);
  4851. }
  4852. /**
  4853. * @brief Queue search action
  4854. */
  4855. static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t *search)
  4856. {
  4857. mdns_action_t *action = NULL;
  4858. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4859. if (!action) {
  4860. HOOK_MALLOC_FAILED;
  4861. return ESP_ERR_NO_MEM;
  4862. }
  4863. action->type = type;
  4864. action->data.search_add.search = search;
  4865. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  4866. free(action);
  4867. return ESP_ERR_NO_MEM;
  4868. }
  4869. return ESP_OK;
  4870. }
  4871. /**
  4872. * @brief Called from timer task to run mDNS responder
  4873. *
  4874. * periodically checks first unqueued packet (from tx head).
  4875. * if it is scheduled to be transmitted, then pushes the packet to action queue to be handled.
  4876. *
  4877. */
  4878. static void _mdns_scheduler_run(void)
  4879. {
  4880. MDNS_SERVICE_LOCK();
  4881. mdns_tx_packet_t *p = _mdns_server->tx_queue_head;
  4882. mdns_action_t *action = NULL;
  4883. // find first unqueued packet
  4884. while (p && p->queued) {
  4885. p = p->next;
  4886. }
  4887. if (!p) {
  4888. MDNS_SERVICE_UNLOCK();
  4889. return;
  4890. }
  4891. while (p && (int32_t)(p->send_at - (xTaskGetTickCount() * portTICK_PERIOD_MS)) < 0) {
  4892. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4893. if (action) {
  4894. action->type = ACTION_TX_HANDLE;
  4895. action->data.tx_handle.packet = p;
  4896. p->queued = true;
  4897. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  4898. free(action);
  4899. p->queued = false;
  4900. }
  4901. } else {
  4902. HOOK_MALLOC_FAILED;
  4903. break;
  4904. }
  4905. //Find the next unqued packet
  4906. p = p->next;
  4907. }
  4908. MDNS_SERVICE_UNLOCK();
  4909. }
  4910. /**
  4911. * @brief Called from timer task to run active searches
  4912. */
  4913. static void _mdns_search_run(void)
  4914. {
  4915. MDNS_SERVICE_LOCK();
  4916. mdns_search_once_t *s = _mdns_server->search_once;
  4917. uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS;
  4918. if (!s) {
  4919. MDNS_SERVICE_UNLOCK();
  4920. return;
  4921. }
  4922. while (s) {
  4923. if (s->state != SEARCH_OFF) {
  4924. if (now > (s->started_at + s->timeout)) {
  4925. s->state = SEARCH_OFF;
  4926. if (_mdns_send_search_action(ACTION_SEARCH_END, s) != ESP_OK) {
  4927. s->state = SEARCH_RUNNING;
  4928. }
  4929. } else if (s->state == SEARCH_INIT || (now - s->sent_at) > 1000) {
  4930. s->state = SEARCH_RUNNING;
  4931. s->sent_at = now;
  4932. if (_mdns_send_search_action(ACTION_SEARCH_SEND, s) != ESP_OK) {
  4933. s->sent_at -= 1000;
  4934. }
  4935. }
  4936. }
  4937. s = s->next;
  4938. }
  4939. MDNS_SERVICE_UNLOCK();
  4940. }
  4941. /**
  4942. * @brief the main MDNS service task. Packets are received and parsed here
  4943. */
  4944. static void _mdns_service_task(void *pvParameters)
  4945. {
  4946. mdns_action_t *a = NULL;
  4947. for (;;) {
  4948. if (_mdns_server && _mdns_server->action_queue) {
  4949. if (xQueueReceive(_mdns_server->action_queue, &a, portMAX_DELAY) == pdTRUE) {
  4950. if (a && a->type == ACTION_TASK_STOP) {
  4951. break;
  4952. }
  4953. MDNS_SERVICE_LOCK();
  4954. _mdns_execute_action(a);
  4955. MDNS_SERVICE_UNLOCK();
  4956. }
  4957. } else {
  4958. vTaskDelay(500 * portTICK_PERIOD_MS);
  4959. }
  4960. }
  4961. _mdns_service_task_handle = NULL;
  4962. vTaskDelete(NULL);
  4963. }
  4964. static void _mdns_timer_cb(void *arg)
  4965. {
  4966. _mdns_scheduler_run();
  4967. _mdns_search_run();
  4968. }
  4969. static esp_err_t _mdns_start_timer(void)
  4970. {
  4971. esp_timer_create_args_t timer_conf = {
  4972. .callback = _mdns_timer_cb,
  4973. .arg = NULL,
  4974. .dispatch_method = ESP_TIMER_TASK,
  4975. .name = "mdns_timer"
  4976. };
  4977. esp_err_t err = esp_timer_create(&timer_conf, &(_mdns_server->timer_handle));
  4978. if (err) {
  4979. return err;
  4980. }
  4981. return esp_timer_start_periodic(_mdns_server->timer_handle, MDNS_TIMER_PERIOD_US);
  4982. }
  4983. static esp_err_t _mdns_stop_timer(void)
  4984. {
  4985. esp_err_t err = ESP_OK;
  4986. if (_mdns_server->timer_handle) {
  4987. err = esp_timer_stop(_mdns_server->timer_handle);
  4988. if (err) {
  4989. return err;
  4990. }
  4991. err = esp_timer_delete(_mdns_server->timer_handle);
  4992. }
  4993. return err;
  4994. }
  4995. /**
  4996. * @brief Start the service thread if not running
  4997. *
  4998. * @return
  4999. * - ESP_OK on success
  5000. * - ESP_FAIL on error
  5001. */
  5002. static esp_err_t _mdns_service_task_start(void)
  5003. {
  5004. if (!_mdns_service_semaphore) {
  5005. _mdns_service_semaphore = xSemaphoreCreateMutex();
  5006. if (!_mdns_service_semaphore) {
  5007. return ESP_FAIL;
  5008. }
  5009. }
  5010. MDNS_SERVICE_LOCK();
  5011. if (_mdns_start_timer()) {
  5012. MDNS_SERVICE_UNLOCK();
  5013. return ESP_FAIL;
  5014. }
  5015. if (!_mdns_service_task_handle) {
  5016. xTaskCreatePinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY,
  5017. (TaskHandle_t *const)(&_mdns_service_task_handle), MDNS_TASK_AFFINITY);
  5018. if (!_mdns_service_task_handle) {
  5019. _mdns_stop_timer();
  5020. MDNS_SERVICE_UNLOCK();
  5021. vSemaphoreDelete(_mdns_service_semaphore);
  5022. _mdns_service_semaphore = NULL;
  5023. return ESP_FAIL;
  5024. }
  5025. }
  5026. MDNS_SERVICE_UNLOCK();
  5027. return ESP_OK;
  5028. }
  5029. /**
  5030. * @brief Stop the service thread
  5031. *
  5032. * @return
  5033. * - ESP_OK
  5034. */
  5035. static esp_err_t _mdns_service_task_stop(void)
  5036. {
  5037. _mdns_stop_timer();
  5038. if (_mdns_service_task_handle) {
  5039. mdns_action_t action;
  5040. mdns_action_t *a = &action;
  5041. action.type = ACTION_TASK_STOP;
  5042. if (xQueueSend(_mdns_server->action_queue, &a, (TickType_t)0) != pdPASS) {
  5043. vTaskDelete(_mdns_service_task_handle);
  5044. _mdns_service_task_handle = NULL;
  5045. }
  5046. while (_mdns_service_task_handle) {
  5047. vTaskDelay(10 / portTICK_PERIOD_MS);
  5048. }
  5049. }
  5050. vSemaphoreDelete(_mdns_service_semaphore);
  5051. _mdns_service_semaphore = NULL;
  5052. return ESP_OK;
  5053. }
  5054. static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action)
  5055. {
  5056. if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) {
  5057. return ESP_ERR_INVALID_STATE;
  5058. }
  5059. mdns_action_t *action = (mdns_action_t *)calloc(1, sizeof(mdns_action_t));
  5060. if (!action) {
  5061. HOOK_MALLOC_FAILED;
  5062. return ESP_ERR_NO_MEM;
  5063. }
  5064. action->type = ACTION_SYSTEM_EVENT;
  5065. action->data.sys_event.event_action = event_action;
  5066. action->data.sys_event.interface = mdns_if;
  5067. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  5068. free(action);
  5069. }
  5070. return ESP_OK;
  5071. }
  5072. static inline void set_default_duplicated_interfaces(void)
  5073. {
  5074. mdns_if_t wifi_sta_if = MDNS_MAX_INTERFACES;
  5075. mdns_if_t eth_if = MDNS_MAX_INTERFACES;
  5076. for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; i++) {
  5077. if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == MDNS_IF_STA) {
  5078. wifi_sta_if = i;
  5079. }
  5080. if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == MDNS_IF_ETH) {
  5081. eth_if = i;
  5082. }
  5083. }
  5084. if (wifi_sta_if != MDNS_MAX_INTERFACES && eth_if != MDNS_MAX_INTERFACES) {
  5085. s_esp_netifs[wifi_sta_if].duplicate = eth_if;
  5086. s_esp_netifs[eth_if].duplicate = wifi_sta_if;
  5087. }
  5088. }
  5089. static inline void unregister_predefined_handlers(void)
  5090. {
  5091. #if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP)
  5092. esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event);
  5093. #endif
  5094. #if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
  5095. esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event);
  5096. #endif
  5097. #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
  5098. esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event);
  5099. #endif
  5100. }
  5101. /*
  5102. * Public Methods
  5103. * */
  5104. esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action)
  5105. {
  5106. return mdns_post_custom_action_tcpip_if(_mdns_get_if_from_esp_netif(esp_netif), event_action);
  5107. }
  5108. esp_err_t mdns_register_netif(esp_netif_t *esp_netif)
  5109. {
  5110. if (!_mdns_server) {
  5111. return ESP_ERR_INVALID_STATE;
  5112. }
  5113. esp_err_t err = ESP_ERR_NO_MEM;
  5114. MDNS_SERVICE_LOCK();
  5115. for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) {
  5116. if (s_esp_netifs[i].netif == esp_netif) {
  5117. MDNS_SERVICE_UNLOCK();
  5118. return ESP_ERR_INVALID_STATE;
  5119. }
  5120. }
  5121. for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) {
  5122. if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == NULL) {
  5123. s_esp_netifs[i].netif = esp_netif;
  5124. err = ESP_OK;
  5125. break;
  5126. }
  5127. }
  5128. MDNS_SERVICE_UNLOCK();
  5129. return err;
  5130. }
  5131. esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif)
  5132. {
  5133. if (!_mdns_server) {
  5134. return ESP_ERR_INVALID_STATE;
  5135. }
  5136. esp_err_t err = ESP_ERR_NOT_FOUND;
  5137. MDNS_SERVICE_LOCK();
  5138. for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) {
  5139. if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == esp_netif) {
  5140. s_esp_netifs[i].netif = NULL;
  5141. err = ESP_OK;
  5142. break;
  5143. }
  5144. }
  5145. MDNS_SERVICE_UNLOCK();
  5146. return err;
  5147. }
  5148. esp_err_t mdns_init(void)
  5149. {
  5150. esp_err_t err = ESP_OK;
  5151. if (_mdns_server) {
  5152. return err;
  5153. }
  5154. _mdns_server = (mdns_server_t *)malloc(sizeof(mdns_server_t));
  5155. if (!_mdns_server) {
  5156. HOOK_MALLOC_FAILED;
  5157. return ESP_ERR_NO_MEM;
  5158. }
  5159. memset((uint8_t *)_mdns_server, 0, sizeof(mdns_server_t));
  5160. // zero-out local copy of netifs to initiate a fresh search by interface key whenever a netif ptr is needed
  5161. for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) {
  5162. s_esp_netifs[i].netif = NULL;
  5163. }
  5164. _mdns_server->action_queue = xQueueCreate(MDNS_ACTION_QUEUE_LEN, sizeof(mdns_action_t *));
  5165. if (!_mdns_server->action_queue) {
  5166. err = ESP_ERR_NO_MEM;
  5167. goto free_server;
  5168. }
  5169. _mdns_server->action_sema = xSemaphoreCreateBinary();
  5170. if (!_mdns_server->action_sema) {
  5171. err = ESP_ERR_NO_MEM;
  5172. goto free_queue;
  5173. }
  5174. #if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP)
  5175. if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) {
  5176. goto free_event_handlers;
  5177. }
  5178. #endif
  5179. #if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
  5180. if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) {
  5181. goto free_event_handlers;
  5182. }
  5183. #endif
  5184. #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
  5185. if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) {
  5186. goto free_event_handlers;
  5187. }
  5188. #endif
  5189. #if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
  5190. set_default_duplicated_interfaces();
  5191. #endif
  5192. uint8_t i;
  5193. #ifdef CONFIG_LWIP_IPV6
  5194. esp_ip6_addr_t tmp_addr6;
  5195. #endif
  5196. #ifdef CONFIG_LWIP_IPV4
  5197. esp_netif_ip_info_t if_ip_info;
  5198. #endif
  5199. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  5200. #ifdef CONFIG_LWIP_IPV6
  5201. if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
  5202. _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
  5203. }
  5204. #endif
  5205. #ifdef CONFIG_LWIP_IPV4
  5206. if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
  5207. _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
  5208. }
  5209. #endif
  5210. }
  5211. if (_mdns_service_task_start()) {
  5212. //service start failed!
  5213. err = ESP_FAIL;
  5214. goto free_all_and_disable_pcbs;
  5215. }
  5216. return ESP_OK;
  5217. free_all_and_disable_pcbs:
  5218. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  5219. _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V6);
  5220. _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4);
  5221. s_esp_netifs[i].duplicate = MDNS_MAX_INTERFACES;
  5222. }
  5223. #if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
  5224. free_event_handlers:
  5225. unregister_predefined_handlers();
  5226. #endif
  5227. vSemaphoreDelete(_mdns_server->action_sema);
  5228. free_queue:
  5229. vQueueDelete(_mdns_server->action_queue);
  5230. free_server:
  5231. free(_mdns_server);
  5232. _mdns_server = NULL;
  5233. return err;
  5234. }
  5235. void mdns_free(void)
  5236. {
  5237. uint8_t i, j;
  5238. if (!_mdns_server) {
  5239. return;
  5240. }
  5241. // Unregister handlers before destroying the mdns internals to avoid receiving async events while deinit
  5242. unregister_predefined_handlers();
  5243. mdns_service_remove_all();
  5244. free_delegated_hostnames();
  5245. _mdns_service_task_stop();
  5246. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  5247. for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
  5248. mdns_pcb_deinit_local(i, j);
  5249. }
  5250. }
  5251. free((char *)_mdns_server->hostname);
  5252. free((char *)_mdns_server->instance);
  5253. if (_mdns_server->action_queue) {
  5254. mdns_action_t *c;
  5255. while (xQueueReceive(_mdns_server->action_queue, &c, 0) == pdTRUE) {
  5256. _mdns_free_action(c);
  5257. }
  5258. vQueueDelete(_mdns_server->action_queue);
  5259. }
  5260. _mdns_clear_tx_queue_head();
  5261. while (_mdns_server->search_once) {
  5262. mdns_search_once_t *h = _mdns_server->search_once;
  5263. _mdns_server->search_once = h->next;
  5264. free(h->instance);
  5265. free(h->service);
  5266. free(h->proto);
  5267. vSemaphoreDelete(h->done_semaphore);
  5268. if (h->result) {
  5269. _mdns_query_results_free(h->result);
  5270. }
  5271. free(h);
  5272. }
  5273. while (_mdns_server->browse) {
  5274. mdns_browse_t *b = _mdns_server->browse;
  5275. _mdns_server->browse = _mdns_server->browse->next;
  5276. _mdns_browse_item_free(b);
  5277. }
  5278. vSemaphoreDelete(_mdns_server->action_sema);
  5279. free(_mdns_server);
  5280. _mdns_server = NULL;
  5281. }
  5282. esp_err_t mdns_hostname_set(const char *hostname)
  5283. {
  5284. if (!_mdns_server) {
  5285. return ESP_ERR_INVALID_ARG;
  5286. }
  5287. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
  5288. return ESP_ERR_INVALID_ARG;
  5289. }
  5290. char *new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  5291. if (!new_hostname) {
  5292. return ESP_ERR_NO_MEM;
  5293. }
  5294. mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  5295. if (!action) {
  5296. HOOK_MALLOC_FAILED;
  5297. free(new_hostname);
  5298. return ESP_ERR_NO_MEM;
  5299. }
  5300. action->type = ACTION_HOSTNAME_SET;
  5301. action->data.hostname_set.hostname = new_hostname;
  5302. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  5303. free(new_hostname);
  5304. free(action);
  5305. return ESP_ERR_NO_MEM;
  5306. }
  5307. xSemaphoreTake(_mdns_server->action_sema, portMAX_DELAY);
  5308. return ESP_OK;
  5309. }
  5310. esp_err_t mdns_hostname_get(char *hostname)
  5311. {
  5312. if (!hostname) {
  5313. return ESP_ERR_INVALID_ARG;
  5314. }
  5315. if (!_mdns_server || !_mdns_server->hostname) {
  5316. return ESP_ERR_INVALID_STATE;
  5317. }
  5318. MDNS_SERVICE_LOCK();
  5319. size_t len = strnlen(_mdns_server->hostname, MDNS_NAME_BUF_LEN - 1);
  5320. strncpy(hostname, _mdns_server->hostname, len);
  5321. hostname[len] = 0;
  5322. MDNS_SERVICE_UNLOCK();
  5323. return ESP_OK;
  5324. }
  5325. esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list)
  5326. {
  5327. if (!_mdns_server) {
  5328. return ESP_ERR_INVALID_STATE;
  5329. }
  5330. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
  5331. return ESP_ERR_INVALID_ARG;
  5332. }
  5333. char *new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  5334. if (!new_hostname) {
  5335. return ESP_ERR_NO_MEM;
  5336. }
  5337. mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  5338. if (!action) {
  5339. HOOK_MALLOC_FAILED;
  5340. free(new_hostname);
  5341. return ESP_ERR_NO_MEM;
  5342. }
  5343. action->type = ACTION_DELEGATE_HOSTNAME_ADD;
  5344. action->data.delegate_hostname.hostname = new_hostname;
  5345. action->data.delegate_hostname.address_list = copy_address_list(address_list);
  5346. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  5347. free(new_hostname);
  5348. free(action);
  5349. return ESP_ERR_NO_MEM;
  5350. }
  5351. xSemaphoreTake(_mdns_server->action_sema, portMAX_DELAY);
  5352. return ESP_OK;
  5353. }
  5354. esp_err_t mdns_delegate_hostname_remove(const char *hostname)
  5355. {
  5356. if (!_mdns_server) {
  5357. return ESP_ERR_INVALID_STATE;
  5358. }
  5359. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
  5360. return ESP_ERR_INVALID_ARG;
  5361. }
  5362. char *new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  5363. if (!new_hostname) {
  5364. return ESP_ERR_NO_MEM;
  5365. }
  5366. mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  5367. if (!action) {
  5368. HOOK_MALLOC_FAILED;
  5369. free(new_hostname);
  5370. return ESP_ERR_NO_MEM;
  5371. }
  5372. action->type = ACTION_DELEGATE_HOSTNAME_REMOVE;
  5373. action->data.delegate_hostname.hostname = new_hostname;
  5374. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  5375. free(new_hostname);
  5376. free(action);
  5377. return ESP_ERR_NO_MEM;
  5378. }
  5379. return ESP_OK;
  5380. }
  5381. esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list)
  5382. {
  5383. if (!_mdns_server) {
  5384. return ESP_ERR_INVALID_STATE;
  5385. }
  5386. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
  5387. return ESP_ERR_INVALID_ARG;
  5388. }
  5389. char *new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  5390. if (!new_hostname) {
  5391. return ESP_ERR_NO_MEM;
  5392. }
  5393. mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  5394. if (!action) {
  5395. HOOK_MALLOC_FAILED;
  5396. free(new_hostname);
  5397. return ESP_ERR_NO_MEM;
  5398. }
  5399. action->type = ACTION_DELEGATE_HOSTNAME_SET_ADDR;
  5400. action->data.delegate_hostname.hostname = new_hostname;
  5401. action->data.delegate_hostname.address_list = copy_address_list(address_list);
  5402. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  5403. free(new_hostname);
  5404. free(action);
  5405. return ESP_ERR_NO_MEM;
  5406. }
  5407. return ESP_OK;
  5408. }
  5409. bool mdns_hostname_exists(const char *hostname)
  5410. {
  5411. bool ret = false;
  5412. MDNS_SERVICE_LOCK();
  5413. ret = _hostname_is_ours(hostname);
  5414. MDNS_SERVICE_UNLOCK();
  5415. return ret;
  5416. }
  5417. esp_err_t mdns_instance_name_set(const char *instance)
  5418. {
  5419. if (!_mdns_server) {
  5420. return ESP_ERR_INVALID_STATE;
  5421. }
  5422. if (_str_null_or_empty(instance) || _mdns_server->hostname == NULL || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
  5423. return ESP_ERR_INVALID_ARG;
  5424. }
  5425. char *new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
  5426. if (!new_instance) {
  5427. return ESP_ERR_NO_MEM;
  5428. }
  5429. mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  5430. if (!action) {
  5431. HOOK_MALLOC_FAILED;
  5432. free(new_instance);
  5433. return ESP_ERR_NO_MEM;
  5434. }
  5435. action->type = ACTION_INSTANCE_SET;
  5436. action->data.instance = new_instance;
  5437. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  5438. free(new_instance);
  5439. free(action);
  5440. return ESP_ERR_NO_MEM;
  5441. }
  5442. return ESP_OK;
  5443. }
  5444. /*
  5445. * MDNS SERVICES
  5446. * */
  5447. esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *host,
  5448. uint16_t port, mdns_txt_item_t txt[], size_t num_items)
  5449. {
  5450. if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !_mdns_server->hostname) {
  5451. return ESP_ERR_INVALID_ARG;
  5452. }
  5453. MDNS_SERVICE_LOCK();
  5454. esp_err_t ret = ESP_OK;
  5455. const char *hostname = host ? host : _mdns_server->hostname;
  5456. mdns_service_t *s = NULL;
  5457. ESP_GOTO_ON_FALSE(_mdns_can_add_more_services(), ESP_ERR_NO_MEM, err, TAG,
  5458. "Cannot add more services, please increase CONFIG_MDNS_MAX_SERVICES (%d)", CONFIG_MDNS_MAX_SERVICES);
  5459. mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname);
  5460. ESP_GOTO_ON_FALSE(!item, ESP_ERR_INVALID_ARG, err, TAG, "Service already exists");
  5461. s = _mdns_create_service(service, proto, hostname, port, instance, num_items, txt);
  5462. ESP_GOTO_ON_FALSE(s, ESP_ERR_NO_MEM, err, TAG, "Cannot create service: Out of memory");
  5463. item = (mdns_srv_item_t *)malloc(sizeof(mdns_srv_item_t));
  5464. ESP_GOTO_ON_FALSE(item, ESP_ERR_NO_MEM, err, TAG, "Cannot create service: Out of memory");
  5465. item->service = s;
  5466. item->next = NULL;
  5467. item->next = _mdns_server->services;
  5468. _mdns_server->services = item;
  5469. _mdns_probe_all_pcbs(&item, 1, false, false);
  5470. MDNS_SERVICE_UNLOCK();
  5471. return ESP_OK;
  5472. err:
  5473. MDNS_SERVICE_UNLOCK();
  5474. _mdns_free_service(s);
  5475. if (ret == ESP_ERR_NO_MEM) {
  5476. HOOK_MALLOC_FAILED;
  5477. }
  5478. return ret;
  5479. }
  5480. esp_err_t mdns_service_add(const char *instance, const char *service, const char *proto, uint16_t port,
  5481. mdns_txt_item_t txt[], size_t num_items)
  5482. {
  5483. if (!_mdns_server) {
  5484. return ESP_ERR_INVALID_STATE;
  5485. }
  5486. return mdns_service_add_for_host(instance, service, proto, NULL, port, txt, num_items);
  5487. }
  5488. bool mdns_service_exists(const char *service_type, const char *proto, const char *hostname)
  5489. {
  5490. bool ret = false;
  5491. MDNS_SERVICE_LOCK();
  5492. ret = _mdns_get_service_item(service_type, proto, hostname) != NULL;
  5493. MDNS_SERVICE_UNLOCK();
  5494. return ret;
  5495. }
  5496. bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto,
  5497. const char *hostname)
  5498. {
  5499. bool ret = false;
  5500. MDNS_SERVICE_LOCK();
  5501. ret = _mdns_get_service_item_instance(instance, service_type, proto, hostname) != NULL;
  5502. MDNS_SERVICE_UNLOCK();
  5503. return ret;
  5504. }
  5505. static mdns_txt_item_t *_copy_mdns_txt_items(mdns_txt_linked_item_t *items, uint8_t **txt_value_len, size_t *txt_count)
  5506. {
  5507. mdns_txt_item_t *ret = NULL;
  5508. size_t ret_index = 0;
  5509. for (mdns_txt_linked_item_t *tmp = items; tmp != NULL; tmp = tmp->next) {
  5510. ret_index++;
  5511. }
  5512. *txt_count = ret_index;
  5513. if (ret_index == 0) { // handle empty TXT
  5514. *txt_value_len = NULL;
  5515. return NULL;
  5516. }
  5517. ret = (mdns_txt_item_t *)calloc(ret_index, sizeof(mdns_txt_item_t));
  5518. *txt_value_len = (uint8_t *)calloc(ret_index, sizeof(uint8_t));
  5519. if (!ret || !(*txt_value_len)) {
  5520. HOOK_MALLOC_FAILED;
  5521. goto handle_error;
  5522. }
  5523. ret_index = 0;
  5524. for (mdns_txt_linked_item_t *tmp = items; tmp != NULL; tmp = tmp->next) {
  5525. size_t key_len = strlen(tmp->key);
  5526. char *key = (char *)malloc(key_len + 1);
  5527. if (!key) {
  5528. HOOK_MALLOC_FAILED;
  5529. goto handle_error;
  5530. }
  5531. memcpy(key, tmp->key, key_len);
  5532. key[key_len] = 0;
  5533. ret[ret_index].key = key;
  5534. char *value = (char *)malloc(tmp->value_len + 1);
  5535. if (!value) {
  5536. HOOK_MALLOC_FAILED;
  5537. goto handle_error;
  5538. }
  5539. memcpy(value, tmp->value, tmp->value_len);
  5540. value[tmp->value_len] = 0;
  5541. ret[ret_index].value = value;
  5542. (*txt_value_len)[ret_index] = tmp->value_len;
  5543. ret_index++;
  5544. }
  5545. return ret;
  5546. handle_error:
  5547. for (size_t y = 0; y < ret_index + 1 && ret != NULL; y++) {
  5548. mdns_txt_item_t *t = &ret[y];
  5549. free((char *)t->key);
  5550. free((char *)t->value);
  5551. }
  5552. free(*txt_value_len);
  5553. free(ret);
  5554. return NULL;
  5555. }
  5556. static mdns_ip_addr_t *_copy_delegated_host_address_list(char *hostname)
  5557. {
  5558. mdns_host_item_t *host = _mdns_host_list;
  5559. while (host) {
  5560. if (strcasecmp(host->hostname, hostname) == 0) {
  5561. return copy_address_list(host->address_list);
  5562. }
  5563. host = host->next;
  5564. }
  5565. return NULL;
  5566. }
  5567. static mdns_result_t *_mdns_lookup_service(const char *instance, const char *service, const char *proto, size_t max_results, bool selfhost)
  5568. {
  5569. if (_str_null_or_empty(service) || _str_null_or_empty(proto)) {
  5570. return NULL;
  5571. }
  5572. mdns_result_t *results = NULL;
  5573. size_t num_results = 0;
  5574. mdns_srv_item_t *s = _mdns_server->services;
  5575. while (s) {
  5576. mdns_service_t *srv = s->service;
  5577. if (!srv || !srv->hostname) {
  5578. s = s->next;
  5579. continue;
  5580. }
  5581. bool is_service_selfhosted = !_str_null_or_empty(_mdns_server->hostname) && !strcasecmp(_mdns_server->hostname, srv->hostname);
  5582. bool is_service_delegated = _str_null_or_empty(_mdns_server->hostname) || strcasecmp(_mdns_server->hostname, srv->hostname);
  5583. if ((selfhost && is_service_selfhosted) || (!selfhost && is_service_delegated)) {
  5584. if (!strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
  5585. (_str_null_or_empty(instance) || _mdns_instance_name_match(srv->instance, instance))) {
  5586. mdns_result_t *item = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  5587. if (!item) {
  5588. HOOK_MALLOC_FAILED;
  5589. goto handle_error;
  5590. }
  5591. item->next = results;
  5592. results = item;
  5593. item->esp_netif = NULL;
  5594. item->ttl = _str_null_or_empty(instance) ? MDNS_ANSWER_PTR_TTL : MDNS_ANSWER_SRV_TTL;
  5595. item->ip_protocol = MDNS_IP_PROTOCOL_MAX;
  5596. if (srv->instance) {
  5597. item->instance_name = strndup(srv->instance, MDNS_NAME_BUF_LEN - 1);
  5598. if (!item->instance_name) {
  5599. HOOK_MALLOC_FAILED;
  5600. goto handle_error;
  5601. }
  5602. } else {
  5603. item->instance_name = NULL;
  5604. }
  5605. item->service_type = strndup(srv->service, MDNS_NAME_BUF_LEN - 1);
  5606. if (!item->service_type) {
  5607. HOOK_MALLOC_FAILED;
  5608. goto handle_error;
  5609. }
  5610. item->proto = strndup(srv->proto, MDNS_NAME_BUF_LEN - 1);
  5611. if (!item->proto) {
  5612. HOOK_MALLOC_FAILED;
  5613. goto handle_error;
  5614. }
  5615. item->hostname = strndup(srv->hostname, MDNS_NAME_BUF_LEN - 1);
  5616. if (!item->hostname) {
  5617. HOOK_MALLOC_FAILED;
  5618. goto handle_error;
  5619. }
  5620. item->port = srv->port;
  5621. item->txt = _copy_mdns_txt_items(srv->txt, &(item->txt_value_len), &(item->txt_count));
  5622. // We should not append addresses for selfhost lookup result as we don't know which interface's address to append.
  5623. if (selfhost) {
  5624. item->addr = NULL;
  5625. } else {
  5626. item->addr = _copy_delegated_host_address_list(item->hostname);
  5627. if (!item->addr) {
  5628. goto handle_error;
  5629. }
  5630. }
  5631. if (num_results < max_results) {
  5632. num_results++;
  5633. }
  5634. if (num_results >= max_results) {
  5635. break;
  5636. }
  5637. }
  5638. }
  5639. s = s->next;
  5640. }
  5641. return results;
  5642. handle_error:
  5643. _mdns_query_results_free(results);
  5644. return NULL;
  5645. }
  5646. esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service, const char *proto, const char *host, uint16_t port)
  5647. {
  5648. MDNS_SERVICE_LOCK();
  5649. esp_err_t ret = ESP_OK;
  5650. const char *hostname = host ? host : _mdns_server->hostname;
  5651. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && port,
  5652. ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5653. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
  5654. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5655. s->service->port = port;
  5656. _mdns_announce_all_pcbs(&s, 1, true);
  5657. err:
  5658. MDNS_SERVICE_UNLOCK();
  5659. return ret;
  5660. }
  5661. esp_err_t mdns_service_port_set(const char *service, const char *proto, uint16_t port)
  5662. {
  5663. if (!_mdns_server) {
  5664. return ESP_ERR_INVALID_STATE;
  5665. }
  5666. return mdns_service_port_set_for_host(NULL, service, proto, NULL, port);
  5667. }
  5668. esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service, const char *proto, const char *host,
  5669. mdns_txt_item_t txt_items[], uint8_t num_items)
  5670. {
  5671. MDNS_SERVICE_LOCK();
  5672. esp_err_t ret = ESP_OK;
  5673. const char *hostname = host ? host : _mdns_server->hostname;
  5674. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !(num_items && txt_items == NULL),
  5675. ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5676. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
  5677. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5678. mdns_txt_linked_item_t *new_txt = NULL;
  5679. if (num_items) {
  5680. new_txt = _mdns_allocate_txt(num_items, txt_items);
  5681. if (!new_txt) {
  5682. return ESP_ERR_NO_MEM;
  5683. }
  5684. }
  5685. mdns_service_t *srv = s->service;
  5686. mdns_txt_linked_item_t *txt = srv->txt;
  5687. srv->txt = NULL;
  5688. _mdns_free_linked_txt(txt);
  5689. srv->txt = new_txt;
  5690. _mdns_announce_all_pcbs(&s, 1, false);
  5691. err:
  5692. MDNS_SERVICE_UNLOCK();
  5693. return ret;
  5694. }
  5695. esp_err_t mdns_service_txt_set(const char *service, const char *proto, mdns_txt_item_t txt[], uint8_t num_items)
  5696. {
  5697. if (!_mdns_server) {
  5698. return ESP_ERR_INVALID_STATE;
  5699. }
  5700. return mdns_service_txt_set_for_host(NULL, service, proto, NULL, txt, num_items);
  5701. }
  5702. esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service, const char *proto,
  5703. const char *host, const char *key, const char *value_arg, uint8_t value_len)
  5704. {
  5705. MDNS_SERVICE_LOCK();
  5706. esp_err_t ret = ESP_OK;
  5707. char *value = NULL;
  5708. mdns_txt_linked_item_t *new_txt = NULL;
  5709. const char *hostname = host ? host : _mdns_server->hostname;
  5710. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !_str_null_or_empty(key) &&
  5711. !((!value_arg && value_len)), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5712. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
  5713. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5714. mdns_service_t *srv = s->service;
  5715. if (value_len > 0) {
  5716. value = (char *) malloc(value_len);
  5717. ESP_GOTO_ON_FALSE(value, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
  5718. memcpy(value, value_arg, value_len);
  5719. } else {
  5720. value_len = 0;
  5721. }
  5722. mdns_txt_linked_item_t *txt = srv->txt;
  5723. while (txt) {
  5724. if (strcmp(txt->key, key) == 0) {
  5725. free((char *)txt->value);
  5726. txt->value = value;
  5727. txt->value_len = value_len;
  5728. break;
  5729. }
  5730. txt = txt->next;
  5731. }
  5732. if (!txt) {
  5733. new_txt = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
  5734. ESP_GOTO_ON_FALSE(new_txt, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
  5735. new_txt->key = strdup(key);
  5736. ESP_GOTO_ON_FALSE(new_txt->key, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
  5737. new_txt->value = value;
  5738. new_txt->value_len = value_len;
  5739. new_txt->next = srv->txt;
  5740. srv->txt = new_txt;
  5741. }
  5742. _mdns_announce_all_pcbs(&s, 1, false);
  5743. err:
  5744. MDNS_SERVICE_UNLOCK();
  5745. return ret;
  5746. out_of_mem:
  5747. MDNS_SERVICE_UNLOCK();
  5748. HOOK_MALLOC_FAILED;
  5749. free(value);
  5750. free(new_txt);
  5751. return ret;
  5752. }
  5753. esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *service, const char *proto, const char *hostname,
  5754. const char *key, const char *value)
  5755. {
  5756. return mdns_service_txt_item_set_for_host_with_explicit_value_len(instance, service, proto, hostname, key, value,
  5757. strlen(value));
  5758. }
  5759. esp_err_t mdns_service_txt_item_set(const char *service, const char *proto, const char *key, const char *value)
  5760. {
  5761. if (!_mdns_server) {
  5762. return ESP_ERR_INVALID_STATE;
  5763. }
  5764. return mdns_service_txt_item_set_for_host_with_explicit_value_len(NULL, service, proto, NULL, key,
  5765. value, strlen(value));
  5766. }
  5767. esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service, const char *proto, const char *key,
  5768. const char *value, uint8_t value_len)
  5769. {
  5770. if (!_mdns_server) {
  5771. return ESP_ERR_INVALID_STATE;
  5772. }
  5773. return mdns_service_txt_item_set_for_host_with_explicit_value_len(NULL, service, proto, NULL, key, value, value_len);
  5774. }
  5775. esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char *service, const char *proto, const char *host,
  5776. const char *key)
  5777. {
  5778. MDNS_SERVICE_LOCK();
  5779. esp_err_t ret = ESP_OK;
  5780. const char *hostname = host ? host : _mdns_server->hostname;
  5781. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !_str_null_or_empty(key),
  5782. ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5783. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
  5784. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5785. mdns_service_t *srv = s->service;
  5786. mdns_txt_linked_item_t *txt = srv->txt;
  5787. if (!txt) {
  5788. goto err;
  5789. }
  5790. if (strcmp(txt->key, key) == 0) {
  5791. srv->txt = txt->next;
  5792. free((char *)txt->key);
  5793. free((char *)txt->value);
  5794. free(txt);
  5795. } else {
  5796. while (txt->next) {
  5797. if (strcmp(txt->next->key, key) == 0) {
  5798. mdns_txt_linked_item_t *t = txt->next;
  5799. txt->next = t->next;
  5800. free((char *)t->key);
  5801. free((char *)t->value);
  5802. free(t);
  5803. break;
  5804. } else {
  5805. txt = txt->next;
  5806. }
  5807. }
  5808. }
  5809. _mdns_announce_all_pcbs(&s, 1, false);
  5810. err:
  5811. MDNS_SERVICE_UNLOCK();
  5812. if (ret == ESP_ERR_NO_MEM) {
  5813. HOOK_MALLOC_FAILED;
  5814. }
  5815. return ret;
  5816. }
  5817. esp_err_t mdns_service_txt_item_remove(const char *service, const char *proto, const char *key)
  5818. {
  5819. if (!_mdns_server) {
  5820. return ESP_ERR_INVALID_STATE;
  5821. }
  5822. return mdns_service_txt_item_remove_for_host(NULL, service, proto, NULL, key);
  5823. }
  5824. static esp_err_t _mdns_service_subtype_remove_for_host(mdns_srv_item_t *service, const char *subtype)
  5825. {
  5826. esp_err_t ret = ESP_ERR_NOT_FOUND;
  5827. mdns_subtype_t *srv_subtype = service->service->subtype;
  5828. mdns_subtype_t *pre = service->service->subtype;
  5829. while (srv_subtype) {
  5830. if (strcmp(srv_subtype->subtype, subtype) == 0) {
  5831. // Target subtype is found.
  5832. if (srv_subtype == service->service->subtype) {
  5833. // The first node needs to be removed
  5834. service->service->subtype = service->service->subtype->next;
  5835. } else {
  5836. pre->next = srv_subtype->next;
  5837. }
  5838. free((char *)srv_subtype->subtype);
  5839. free(srv_subtype);
  5840. ret = ESP_OK;
  5841. break;
  5842. }
  5843. pre = srv_subtype;
  5844. srv_subtype = srv_subtype->next;
  5845. }
  5846. if (ret == ESP_ERR_NOT_FOUND) {
  5847. ESP_LOGE(TAG, "Subtype : %s doesn't exist", subtype);
  5848. }
  5849. return ret;
  5850. }
  5851. esp_err_t mdns_service_subtype_remove_for_host(const char *instance_name, const char *service, const char *proto,
  5852. const char *hostname, const char *subtype)
  5853. {
  5854. MDNS_SERVICE_LOCK();
  5855. esp_err_t ret = ESP_OK;
  5856. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) &&
  5857. !_str_null_or_empty(subtype), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5858. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname);
  5859. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5860. ret = _mdns_service_subtype_remove_for_host(s, subtype);
  5861. ESP_GOTO_ON_ERROR(ret, err, TAG, "Failed to remove the subtype: %s", subtype);
  5862. // TODO: Need to transmit a sendbye message for the removed subtype.
  5863. // TODO: Need to remove this subtype answer from the scheduled answer list.
  5864. err:
  5865. MDNS_SERVICE_UNLOCK();
  5866. return ret;
  5867. }
  5868. static esp_err_t _mdns_service_subtype_add_for_host(mdns_srv_item_t *service, const char *subtype)
  5869. {
  5870. esp_err_t ret = ESP_OK;
  5871. mdns_subtype_t *srv_subtype = service->service->subtype;
  5872. while (srv_subtype) {
  5873. ESP_GOTO_ON_FALSE(strcmp(srv_subtype->subtype, subtype) != 0, ESP_ERR_INVALID_ARG, err, TAG, "Subtype: %s has already been added", subtype);
  5874. srv_subtype = srv_subtype->next;
  5875. }
  5876. mdns_subtype_t *subtype_item = (mdns_subtype_t *)malloc(sizeof(mdns_subtype_t));
  5877. ESP_GOTO_ON_FALSE(subtype_item, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
  5878. subtype_item->subtype = strdup(subtype);
  5879. ESP_GOTO_ON_FALSE(subtype_item->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
  5880. subtype_item->next = service->service->subtype;
  5881. service->service->subtype = subtype_item;
  5882. err:
  5883. return ret;
  5884. out_of_mem:
  5885. HOOK_MALLOC_FAILED;
  5886. free(subtype_item);
  5887. return ret;
  5888. }
  5889. esp_err_t mdns_service_subtype_add_multiple_items_for_host(const char *instance_name, const char *service, const char *proto,
  5890. const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items)
  5891. {
  5892. MDNS_SERVICE_LOCK();
  5893. esp_err_t ret = ESP_OK;
  5894. int cur_index = 0;
  5895. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) &&
  5896. (num_items > 0), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5897. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname);
  5898. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5899. for (; cur_index < num_items; cur_index++) {
  5900. ret = _mdns_service_subtype_add_for_host(s, subtype[cur_index].subtype);
  5901. if (ret == ESP_OK) {
  5902. continue;
  5903. } else if (ret == ESP_ERR_NO_MEM) {
  5904. ESP_LOGE(TAG, "Out of memory");
  5905. goto err;
  5906. } else {
  5907. ESP_LOGE(TAG, "Failed to add subtype: %s", subtype[cur_index].subtype);
  5908. goto exit;
  5909. }
  5910. }
  5911. _mdns_announce_all_pcbs(&s, 1, false);
  5912. err:
  5913. if (ret == ESP_ERR_NO_MEM) {
  5914. for (int idx = 0; idx < cur_index; idx++) {
  5915. _mdns_service_subtype_remove_for_host(s, subtype[idx].subtype);
  5916. }
  5917. }
  5918. exit:
  5919. MDNS_SERVICE_UNLOCK();
  5920. return ret;
  5921. }
  5922. esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service_type, const char *proto,
  5923. const char *hostname, const char *subtype)
  5924. {
  5925. mdns_subtype_item_t _subtype[1];
  5926. _subtype[0].subtype = subtype;
  5927. return mdns_service_subtype_add_multiple_items_for_host(instance_name, service_type, proto, hostname, _subtype, 1);
  5928. }
  5929. esp_err_t mdns_service_subtype_update_multiple_items_for_host(const char *instance_name, const char *service_type, const char *proto,
  5930. const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items)
  5931. {
  5932. MDNS_SERVICE_LOCK();
  5933. esp_err_t ret = ESP_OK;
  5934. int cur_index = 0;
  5935. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service_type) && !_str_null_or_empty(proto),
  5936. ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5937. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service_type, proto, hostname);
  5938. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5939. // TODO: find subtype needs to say sendbye
  5940. _mdns_free_service_subtype(s->service);
  5941. for (; cur_index < num_items; cur_index++) {
  5942. ret = _mdns_service_subtype_add_for_host(s, subtype[cur_index].subtype);
  5943. if (ret == ESP_OK) {
  5944. continue;
  5945. } else if (ret == ESP_ERR_NO_MEM) {
  5946. ESP_LOGE(TAG, "Out of memory");
  5947. goto err;
  5948. } else {
  5949. ESP_LOGE(TAG, "Failed to add subtype: %s", subtype[cur_index].subtype);
  5950. goto exit;
  5951. }
  5952. }
  5953. if (num_items) {
  5954. _mdns_announce_all_pcbs(&s, 1, false);
  5955. }
  5956. err:
  5957. if (ret == ESP_ERR_NO_MEM) {
  5958. for (int idx = 0; idx < cur_index; idx++) {
  5959. _mdns_service_subtype_remove_for_host(s, subtype[idx].subtype);
  5960. }
  5961. }
  5962. exit:
  5963. MDNS_SERVICE_UNLOCK();
  5964. return ret;
  5965. }
  5966. esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service, const char *proto, const char *host,
  5967. const char *instance)
  5968. {
  5969. MDNS_SERVICE_LOCK();
  5970. esp_err_t ret = ESP_OK;
  5971. const char *hostname = host ? host : _mdns_server->hostname;
  5972. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) &&
  5973. !_str_null_or_empty(instance) && strlen(instance) <= (MDNS_NAME_BUF_LEN - 1), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  5974. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_old, service, proto, hostname);
  5975. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  5976. if (s->service->instance) {
  5977. _mdns_send_bye(&s, 1, false);
  5978. free((char *)s->service->instance);
  5979. }
  5980. s->service->instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
  5981. ESP_GOTO_ON_FALSE(s->service->instance, ESP_ERR_NO_MEM, err, TAG, "Out of memory");
  5982. _mdns_probe_all_pcbs(&s, 1, false, false);
  5983. err:
  5984. MDNS_SERVICE_UNLOCK();
  5985. return ret;
  5986. }
  5987. esp_err_t mdns_service_instance_name_set(const char *service, const char *proto, const char *instance)
  5988. {
  5989. if (!_mdns_server) {
  5990. return ESP_ERR_INVALID_STATE;
  5991. }
  5992. return mdns_service_instance_name_set_for_host(NULL, service, proto, NULL, instance);
  5993. }
  5994. esp_err_t mdns_service_remove_for_host(const char *instance, const char *service, const char *proto, const char *host)
  5995. {
  5996. MDNS_SERVICE_LOCK();
  5997. esp_err_t ret = ESP_OK;
  5998. const char *hostname = host ? host : _mdns_server->hostname;
  5999. ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto),
  6000. ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
  6001. mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
  6002. ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
  6003. mdns_srv_item_t *a = _mdns_server->services;
  6004. mdns_srv_item_t *b = a;
  6005. if (instance) {
  6006. while (a) {
  6007. if (_mdns_service_match_instance(a->service, instance, service, proto, hostname)) {
  6008. if (_mdns_server->services != a) {
  6009. b->next = a->next;
  6010. } else {
  6011. _mdns_server->services = a->next;
  6012. }
  6013. _mdns_send_bye(&a, 1, false);
  6014. _mdns_remove_scheduled_service_packets(a->service);
  6015. _mdns_free_service(a->service);
  6016. free(a);
  6017. break;
  6018. }
  6019. b = a;
  6020. a = a->next;
  6021. }
  6022. } else {
  6023. while (a) {
  6024. if (_mdns_service_match(a->service, service, proto, hostname)) {
  6025. if (_mdns_server->services != a) {
  6026. b->next = a->next;
  6027. } else {
  6028. _mdns_server->services = a->next;
  6029. }
  6030. _mdns_send_bye(&a, 1, false);
  6031. _mdns_remove_scheduled_service_packets(a->service);
  6032. _mdns_free_service(a->service);
  6033. free(a);
  6034. break;
  6035. }
  6036. b = a;
  6037. a = a->next;
  6038. }
  6039. }
  6040. err:
  6041. MDNS_SERVICE_UNLOCK();
  6042. return ret;
  6043. }
  6044. esp_err_t mdns_service_remove(const char *service_type, const char *proto)
  6045. {
  6046. if (!_mdns_server) {
  6047. return ESP_ERR_INVALID_STATE;
  6048. }
  6049. return mdns_service_remove_for_host(NULL, service_type, proto, NULL);
  6050. }
  6051. esp_err_t mdns_service_remove_all(void)
  6052. {
  6053. MDNS_SERVICE_LOCK();
  6054. esp_err_t ret = ESP_OK;
  6055. ESP_GOTO_ON_FALSE(_mdns_server, ESP_ERR_INVALID_ARG, done, TAG, "Invalid state");
  6056. if (!_mdns_server->services) {
  6057. goto done;
  6058. }
  6059. _mdns_send_final_bye(false);
  6060. mdns_srv_item_t *services = _mdns_server->services;
  6061. _mdns_server->services = NULL;
  6062. while (services) {
  6063. mdns_srv_item_t *s = services;
  6064. services = services->next;
  6065. _mdns_remove_scheduled_service_packets(s->service);
  6066. _mdns_free_service(s->service);
  6067. free(s);
  6068. }
  6069. done:
  6070. MDNS_SERVICE_UNLOCK();
  6071. return ret;
  6072. }
  6073. /*
  6074. * MDNS QUERY
  6075. * */
  6076. void mdns_query_results_free(mdns_result_t *results)
  6077. {
  6078. MDNS_SERVICE_LOCK();
  6079. _mdns_query_results_free(results);
  6080. MDNS_SERVICE_UNLOCK();
  6081. }
  6082. static void _mdns_query_results_free(mdns_result_t *results)
  6083. {
  6084. mdns_result_t *r;
  6085. mdns_ip_addr_t *a;
  6086. while (results) {
  6087. r = results;
  6088. free((char *)(r->hostname));
  6089. free((char *)(r->instance_name));
  6090. free((char *)(r->service_type));
  6091. free((char *)(r->proto));
  6092. for (size_t i = 0; i < r->txt_count; i++) {
  6093. free((char *)(r->txt[i].key));
  6094. free((char *)(r->txt[i].value));
  6095. }
  6096. free(r->txt);
  6097. free(r->txt_value_len);
  6098. while (r->addr) {
  6099. a = r->addr;
  6100. r->addr = r->addr->next;
  6101. free(a);
  6102. }
  6103. results = results->next;
  6104. free(r);
  6105. }
  6106. }
  6107. esp_err_t mdns_query_async_delete(mdns_search_once_t *search)
  6108. {
  6109. if (!search) {
  6110. return ESP_ERR_INVALID_ARG;
  6111. }
  6112. if (search->state != SEARCH_OFF) {
  6113. return ESP_ERR_INVALID_STATE;
  6114. }
  6115. MDNS_SERVICE_LOCK();
  6116. _mdns_search_free(search);
  6117. MDNS_SERVICE_UNLOCK();
  6118. return ESP_OK;
  6119. }
  6120. bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, mdns_result_t **results, uint8_t *num_results)
  6121. {
  6122. if (xSemaphoreTake(search->done_semaphore, pdMS_TO_TICKS(timeout)) == pdTRUE) {
  6123. if (results) {
  6124. *results = search->result;
  6125. }
  6126. if (num_results) {
  6127. *num_results = search->num_results;
  6128. }
  6129. return true;
  6130. }
  6131. return false;
  6132. }
  6133. mdns_search_once_t *mdns_query_async_new(const char *name, const char *service, const char *proto, uint16_t type,
  6134. uint32_t timeout, size_t max_results, mdns_query_notify_t notifier)
  6135. {
  6136. mdns_search_once_t *search = NULL;
  6137. if (!_mdns_server || !timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
  6138. return NULL;
  6139. }
  6140. search = _mdns_search_init(name, service, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, notifier);
  6141. if (!search) {
  6142. return NULL;
  6143. }
  6144. if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) {
  6145. _mdns_search_free(search);
  6146. return NULL;
  6147. }
  6148. return search;
  6149. }
  6150. esp_err_t mdns_query_generic(const char *name, const char *service, const char *proto, uint16_t type, mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t **results)
  6151. {
  6152. mdns_search_once_t *search = NULL;
  6153. *results = NULL;
  6154. if (!_mdns_server) {
  6155. return ESP_ERR_INVALID_STATE;
  6156. }
  6157. if (!timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
  6158. return ESP_ERR_INVALID_ARG;
  6159. }
  6160. search = _mdns_search_init(name, service, proto, type, transmission_type == MDNS_QUERY_UNICAST, timeout, max_results, NULL);
  6161. if (!search) {
  6162. return ESP_ERR_NO_MEM;
  6163. }
  6164. if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) {
  6165. _mdns_search_free(search);
  6166. return ESP_ERR_NO_MEM;
  6167. }
  6168. xSemaphoreTake(search->done_semaphore, portMAX_DELAY);
  6169. *results = search->result;
  6170. _mdns_search_free(search);
  6171. return ESP_OK;
  6172. }
  6173. esp_err_t mdns_query(const char *name, const char *service_type, const char *proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t **results)
  6174. {
  6175. return mdns_query_generic(name, service_type, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, results);
  6176. }
  6177. esp_err_t mdns_query_ptr(const char *service, const char *proto, uint32_t timeout, size_t max_results, mdns_result_t **results)
  6178. {
  6179. if (_str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6180. return ESP_ERR_INVALID_ARG;
  6181. }
  6182. return mdns_query(NULL, service, proto, MDNS_TYPE_PTR, timeout, max_results, results);
  6183. }
  6184. esp_err_t mdns_query_srv(const char *instance, const char *service, const char *proto, uint32_t timeout, mdns_result_t **result)
  6185. {
  6186. if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6187. return ESP_ERR_INVALID_ARG;
  6188. }
  6189. return mdns_query(instance, service, proto, MDNS_TYPE_SRV, timeout, 1, result);
  6190. }
  6191. esp_err_t mdns_query_txt(const char *instance, const char *service, const char *proto, uint32_t timeout, mdns_result_t **result)
  6192. {
  6193. if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6194. return ESP_ERR_INVALID_ARG;
  6195. }
  6196. return mdns_query(instance, service, proto, MDNS_TYPE_TXT, timeout, 1, result);
  6197. }
  6198. esp_err_t mdns_lookup_delegated_service(const char *instance, const char *service, const char *proto, size_t max_results,
  6199. mdns_result_t **result)
  6200. {
  6201. if (!_mdns_server) {
  6202. return ESP_ERR_INVALID_STATE;
  6203. }
  6204. if (!result || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6205. return ESP_ERR_INVALID_ARG;
  6206. }
  6207. MDNS_SERVICE_LOCK();
  6208. *result = _mdns_lookup_service(instance, service, proto, max_results, false);
  6209. MDNS_SERVICE_UNLOCK();
  6210. return ESP_OK;
  6211. }
  6212. esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *service, const char *proto, size_t max_results,
  6213. mdns_result_t **result)
  6214. {
  6215. if (!_mdns_server) {
  6216. return ESP_ERR_INVALID_STATE;
  6217. }
  6218. if (!result || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6219. return ESP_ERR_INVALID_ARG;
  6220. }
  6221. MDNS_SERVICE_LOCK();
  6222. *result = _mdns_lookup_service(instance, service, proto, max_results, true);
  6223. MDNS_SERVICE_UNLOCK();
  6224. return ESP_OK;
  6225. }
  6226. #ifdef CONFIG_LWIP_IPV4
  6227. esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr)
  6228. {
  6229. mdns_result_t *result = NULL;
  6230. esp_err_t err;
  6231. if (_str_null_or_empty(name)) {
  6232. return ESP_ERR_INVALID_ARG;
  6233. }
  6234. if (strstr(name, ".local")) {
  6235. ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain");
  6236. }
  6237. err = mdns_query(name, NULL, NULL, MDNS_TYPE_A, timeout, 1, &result);
  6238. if (err) {
  6239. return err;
  6240. }
  6241. if (!result) {
  6242. return ESP_ERR_NOT_FOUND;
  6243. }
  6244. mdns_ip_addr_t *a = result->addr;
  6245. while (a) {
  6246. if (a->addr.type == ESP_IPADDR_TYPE_V4) {
  6247. addr->addr = a->addr.u_addr.ip4.addr;
  6248. mdns_query_results_free(result);
  6249. return ESP_OK;
  6250. }
  6251. a = a->next;
  6252. }
  6253. mdns_query_results_free(result);
  6254. return ESP_ERR_NOT_FOUND;
  6255. }
  6256. #endif /* CONFIG_LWIP_IPV4 */
  6257. #ifdef CONFIG_LWIP_IPV6
  6258. esp_err_t mdns_query_aaaa(const char *name, uint32_t timeout, esp_ip6_addr_t *addr)
  6259. {
  6260. mdns_result_t *result = NULL;
  6261. esp_err_t err;
  6262. if (_str_null_or_empty(name)) {
  6263. return ESP_ERR_INVALID_ARG;
  6264. }
  6265. if (strstr(name, ".local")) {
  6266. ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain");
  6267. }
  6268. err = mdns_query(name, NULL, NULL, MDNS_TYPE_AAAA, timeout, 1, &result);
  6269. if (err) {
  6270. return err;
  6271. }
  6272. if (!result) {
  6273. return ESP_ERR_NOT_FOUND;
  6274. }
  6275. mdns_ip_addr_t *a = result->addr;
  6276. while (a) {
  6277. if (a->addr.type == ESP_IPADDR_TYPE_V6) {
  6278. memcpy(addr->addr, a->addr.u_addr.ip6.addr, 16);
  6279. mdns_query_results_free(result);
  6280. return ESP_OK;
  6281. }
  6282. a = a->next;
  6283. }
  6284. mdns_query_results_free(result);
  6285. return ESP_ERR_NOT_FOUND;
  6286. }
  6287. #endif /* CONFIG_LWIP_IPV6 */
  6288. #ifdef MDNS_ENABLE_DEBUG
  6289. void mdns_debug_packet(const uint8_t *data, size_t len)
  6290. {
  6291. static mdns_name_t n;
  6292. mdns_header_t header;
  6293. const uint8_t *content = data + MDNS_HEAD_LEN;
  6294. uint32_t t = xTaskGetTickCount() * portTICK_PERIOD_MS;
  6295. mdns_name_t *name = &n;
  6296. memset(name, 0, sizeof(mdns_name_t));
  6297. _mdns_dbg_printf("Packet[%" PRIu32 "]: ", t);
  6298. header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
  6299. header.flags = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
  6300. header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
  6301. header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
  6302. header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
  6303. header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
  6304. _mdns_dbg_printf("%s",
  6305. (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE) ? "AUTHORITATIVE\n" :
  6306. (header.flags == MDNS_FLAGS_DISTRIBUTED) ? "DISTRIBUTED\n" :
  6307. (header.flags == 0) ? "\n" : " "
  6308. );
  6309. if (header.flags && header.flags != MDNS_FLAGS_QR_AUTHORITATIVE) {
  6310. _mdns_dbg_printf("0x%04X\n", header.flags);
  6311. }
  6312. if (header.questions) {
  6313. uint8_t qs = header.questions;
  6314. while (qs--) {
  6315. content = _mdns_parse_fqdn(data, content, name, len);
  6316. if (!content || content + MDNS_CLASS_OFFSET + 1 >= data + len) {
  6317. header.answers = 0;
  6318. header.additional = 0;
  6319. header.servers = 0;
  6320. _mdns_dbg_printf("ERROR: parse header questions\n");
  6321. break;
  6322. }
  6323. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  6324. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  6325. bool unicast = !!(mdns_class & 0x8000);
  6326. mdns_class &= 0x7FFF;
  6327. content = content + 4;
  6328. _mdns_dbg_printf(" Q: ");
  6329. if (unicast) {
  6330. _mdns_dbg_printf("*U* ");
  6331. }
  6332. if (type == MDNS_TYPE_PTR) {
  6333. _mdns_dbg_printf("%s.%s%s.%s.%s. PTR ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain);
  6334. } else if (type == MDNS_TYPE_SRV) {
  6335. _mdns_dbg_printf("%s.%s%s.%s.%s. SRV ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain);
  6336. } else if (type == MDNS_TYPE_TXT) {
  6337. _mdns_dbg_printf("%s.%s%s.%s.%s. TXT ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain);
  6338. } else if (type == MDNS_TYPE_A) {
  6339. _mdns_dbg_printf("%s.%s. A ", name->host, name->domain);
  6340. } else if (type == MDNS_TYPE_AAAA) {
  6341. _mdns_dbg_printf("%s.%s. AAAA ", name->host, name->domain);
  6342. } else if (type == MDNS_TYPE_NSEC) {
  6343. _mdns_dbg_printf("%s.%s%s.%s.%s. NSEC ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain);
  6344. } else if (type == MDNS_TYPE_ANY) {
  6345. _mdns_dbg_printf("%s.%s%s.%s.%s. ANY ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain);
  6346. } else {
  6347. _mdns_dbg_printf("%s.%s%s.%s.%s. %04X ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain, type);
  6348. }
  6349. if (mdns_class == 0x0001) {
  6350. _mdns_dbg_printf("IN");
  6351. } else {
  6352. _mdns_dbg_printf("%04X", mdns_class);
  6353. }
  6354. _mdns_dbg_printf("\n");
  6355. }
  6356. }
  6357. if (header.answers || header.servers || header.additional) {
  6358. uint16_t recordIndex = 0;
  6359. while (content < (data + len)) {
  6360. content = _mdns_parse_fqdn(data, content, name, len);
  6361. if (!content) {
  6362. _mdns_dbg_printf("ERROR: parse mdns records\n");
  6363. break;
  6364. }
  6365. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  6366. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  6367. uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET);
  6368. uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET);
  6369. const uint8_t *data_ptr = content + MDNS_DATA_OFFSET;
  6370. bool flush = !!(mdns_class & 0x8000);
  6371. mdns_class &= 0x7FFF;
  6372. content = data_ptr + data_len;
  6373. if (content > (data + len)) {
  6374. _mdns_dbg_printf("ERROR: content length overflow\n");
  6375. break;
  6376. }
  6377. mdns_parsed_record_type_t record_type = MDNS_ANSWER;
  6378. if (recordIndex >= (header.answers + header.servers)) {
  6379. record_type = MDNS_EXTRA;
  6380. } else if (recordIndex >= (header.answers)) {
  6381. record_type = MDNS_NS;
  6382. }
  6383. recordIndex++;
  6384. if (record_type == MDNS_EXTRA) {
  6385. _mdns_dbg_printf(" X");
  6386. } else if (record_type == MDNS_NS) {
  6387. _mdns_dbg_printf(" S");
  6388. } else {
  6389. _mdns_dbg_printf(" A");
  6390. }
  6391. if (type == MDNS_TYPE_PTR) {
  6392. _mdns_dbg_printf(": %s%s%s.%s.%s. PTR ", name->host, name->host[0] ? "." : "", name->service, name->proto, name->domain);
  6393. } else if (type == MDNS_TYPE_SRV) {
  6394. _mdns_dbg_printf(": %s.%s.%s.%s. SRV ", name->host, name->service, name->proto, name->domain);
  6395. } else if (type == MDNS_TYPE_TXT) {
  6396. _mdns_dbg_printf(": %s.%s.%s.%s. TXT ", name->host, name->service, name->proto, name->domain);
  6397. } else if (type == MDNS_TYPE_A) {
  6398. _mdns_dbg_printf(": %s.%s. A ", name->host, name->domain);
  6399. } else if (type == MDNS_TYPE_AAAA) {
  6400. _mdns_dbg_printf(": %s.%s. AAAA ", name->host, name->domain);
  6401. } else if (type == MDNS_TYPE_NSEC) {
  6402. _mdns_dbg_printf(": %s.%s.%s.%s. NSEC ", name->host, name->service, name->proto, name->domain);
  6403. } else if (type == MDNS_TYPE_ANY) {
  6404. _mdns_dbg_printf(": %s.%s.%s.%s. ANY ", name->host, name->service, name->proto, name->domain);
  6405. } else if (type == MDNS_TYPE_OPT) {
  6406. _mdns_dbg_printf(": . OPT ");
  6407. } else {
  6408. _mdns_dbg_printf(": %s.%s.%s.%s. %04X ", name->host, name->service, name->proto, name->domain, type);
  6409. }
  6410. if (mdns_class == 0x0001) {
  6411. _mdns_dbg_printf("IN ");
  6412. } else {
  6413. _mdns_dbg_printf("%04X ", mdns_class);
  6414. }
  6415. if (flush) {
  6416. _mdns_dbg_printf("FLUSH ");
  6417. }
  6418. _mdns_dbg_printf("%" PRIu32, ttl);
  6419. _mdns_dbg_printf("[%u] ", data_len);
  6420. if (type == MDNS_TYPE_PTR) {
  6421. if (!_mdns_parse_fqdn(data, data_ptr, name, len)) {
  6422. _mdns_dbg_printf("ERROR: parse PTR\n");
  6423. continue;
  6424. }
  6425. _mdns_dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain);
  6426. } else if (type == MDNS_TYPE_SRV) {
  6427. if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name, len)) {
  6428. _mdns_dbg_printf("ERROR: parse SRV\n");
  6429. continue;
  6430. }
  6431. uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
  6432. uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET);
  6433. uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET);
  6434. _mdns_dbg_printf("%u %u %u %s.%s.\n", priority, weight, port, name->host, name->domain);
  6435. } else if (type == MDNS_TYPE_TXT) {
  6436. uint16_t i = 0, y;
  6437. while (i < data_len) {
  6438. uint8_t partLen = data_ptr[i++];
  6439. if ((i + partLen) > data_len) {
  6440. _mdns_dbg_printf("ERROR: parse TXT\n");
  6441. break;
  6442. }
  6443. char txt[partLen + 1];
  6444. for (y = 0; y < partLen; y++) {
  6445. char d = data_ptr[i++];
  6446. txt[y] = d;
  6447. }
  6448. txt[partLen] = 0;
  6449. _mdns_dbg_printf("%s", txt);
  6450. if (i < data_len) {
  6451. _mdns_dbg_printf("; ");
  6452. }
  6453. }
  6454. _mdns_dbg_printf("\n");
  6455. } else if (type == MDNS_TYPE_AAAA) {
  6456. esp_ip6_addr_t ip6;
  6457. memcpy(&ip6, data_ptr, sizeof(esp_ip6_addr_t));
  6458. _mdns_dbg_printf(IPV6STR "\n", IPV62STR(ip6));
  6459. } else if (type == MDNS_TYPE_A) {
  6460. esp_ip4_addr_t ip;
  6461. memcpy(&ip, data_ptr, sizeof(esp_ip4_addr_t));
  6462. _mdns_dbg_printf(IPSTR "\n", IP2STR(&ip));
  6463. } else if (type == MDNS_TYPE_NSEC) {
  6464. const uint8_t *old_ptr = data_ptr;
  6465. const uint8_t *new_ptr = _mdns_parse_fqdn(data, data_ptr, name, len);
  6466. if (new_ptr) {
  6467. _mdns_dbg_printf("%s.%s.%s.%s. ", name->host, name->service, name->proto, name->domain);
  6468. size_t diff = new_ptr - old_ptr;
  6469. data_len -= diff;
  6470. data_ptr = new_ptr;
  6471. }
  6472. size_t i;
  6473. for (i = 0; i < data_len; i++) {
  6474. _mdns_dbg_printf(" %02x", data_ptr[i]);
  6475. }
  6476. _mdns_dbg_printf("\n");
  6477. } else if (type == MDNS_TYPE_OPT) {
  6478. uint16_t opCode = _mdns_read_u16(data_ptr, 0);
  6479. uint16_t opLen = _mdns_read_u16(data_ptr, 2);
  6480. _mdns_dbg_printf(" Code: %04x Data[%u]:", opCode, opLen);
  6481. size_t i;
  6482. for (i = 4; i < data_len; i++) {
  6483. _mdns_dbg_printf(" %02x", data_ptr[i]);
  6484. }
  6485. _mdns_dbg_printf("\n");
  6486. } else {
  6487. size_t i;
  6488. for (i = 0; i < data_len; i++) {
  6489. _mdns_dbg_printf(" %02x", data_ptr[i]);
  6490. }
  6491. _mdns_dbg_printf("\n");
  6492. }
  6493. }
  6494. }
  6495. }
  6496. #endif /* MDNS_ENABLE_DEBUG */
  6497. /**
  6498. * @brief Browse sync result action
  6499. */
  6500. static esp_err_t _mdns_sync_browse_action(mdns_action_type_t type, mdns_browse_sync_t *browse_sync)
  6501. {
  6502. mdns_action_t *action = NULL;
  6503. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  6504. if (!action) {
  6505. HOOK_MALLOC_FAILED;
  6506. return ESP_ERR_NO_MEM;
  6507. }
  6508. action->type = type;
  6509. action->data.browse_sync.browse_sync = browse_sync;
  6510. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  6511. free(action);
  6512. return ESP_ERR_NO_MEM;
  6513. }
  6514. return ESP_OK;
  6515. }
  6516. /**
  6517. * @brief Browse action
  6518. */
  6519. static esp_err_t _mdns_send_browse_action(mdns_action_type_t type, mdns_browse_t *browse)
  6520. {
  6521. mdns_action_t *action = NULL;
  6522. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  6523. if (!action) {
  6524. HOOK_MALLOC_FAILED;
  6525. return ESP_ERR_NO_MEM;
  6526. }
  6527. action->type = type;
  6528. action->data.browse_add.browse = browse;
  6529. if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
  6530. free(action);
  6531. return ESP_ERR_NO_MEM;
  6532. }
  6533. return ESP_OK;
  6534. }
  6535. /**
  6536. * @brief Free a browse item (Not free the list).
  6537. */
  6538. static void _mdns_browse_item_free(mdns_browse_t *browse)
  6539. {
  6540. free(browse->service);
  6541. free(browse->proto);
  6542. if (browse->result) {
  6543. _mdns_query_results_free(browse->result);
  6544. }
  6545. free(browse);
  6546. }
  6547. /**
  6548. * @brief Allocate new browse structure
  6549. */
  6550. static mdns_browse_t *_mdns_browse_init(const char *service, const char *proto, mdns_browse_notify_t notifier)
  6551. {
  6552. mdns_browse_t *browse = (mdns_browse_t *)malloc(sizeof(mdns_browse_t));
  6553. if (!browse) {
  6554. HOOK_MALLOC_FAILED;
  6555. return NULL;
  6556. }
  6557. memset(browse, 0, sizeof(mdns_browse_t));
  6558. browse->state = BROWSE_INIT;
  6559. if (!_str_null_or_empty(service)) {
  6560. browse->service = strndup(service, MDNS_NAME_BUF_LEN - 1);
  6561. if (!browse->service) {
  6562. _mdns_browse_item_free(browse);
  6563. return NULL;
  6564. }
  6565. }
  6566. if (!_str_null_or_empty(proto)) {
  6567. browse->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
  6568. if (!browse->proto) {
  6569. _mdns_browse_item_free(browse);
  6570. return NULL;
  6571. }
  6572. }
  6573. browse->notifier = notifier;
  6574. return browse;
  6575. }
  6576. mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_browse_notify_t notifier)
  6577. {
  6578. mdns_browse_t *browse = NULL;
  6579. if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6580. return NULL;
  6581. }
  6582. browse = _mdns_browse_init(service, proto, notifier);
  6583. if (!browse) {
  6584. return NULL;
  6585. }
  6586. if (_mdns_send_browse_action(ACTION_BROWSE_ADD, browse)) {
  6587. _mdns_browse_item_free(browse);
  6588. return NULL;
  6589. }
  6590. return browse;
  6591. }
  6592. esp_err_t mdns_browse_delete(const char *service, const char *proto)
  6593. {
  6594. mdns_browse_t *browse = NULL;
  6595. if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  6596. return ESP_FAIL;
  6597. }
  6598. browse = _mdns_browse_init(service, proto, NULL);
  6599. if (!browse) {
  6600. return ESP_ERR_NO_MEM;
  6601. }
  6602. if (_mdns_send_browse_action(ACTION_BROWSE_END, browse)) {
  6603. _mdns_browse_item_free(browse);
  6604. return ESP_ERR_NO_MEM;
  6605. }
  6606. return ESP_OK;
  6607. }
  6608. /**
  6609. * @brief Mark browse as finished, remove and free it from browse chain
  6610. */
  6611. static void _mdns_browse_finish(mdns_browse_t *browse)
  6612. {
  6613. browse->state = BROWSE_OFF;
  6614. mdns_browse_t *b = _mdns_server->browse;
  6615. mdns_browse_t *target_free = NULL;
  6616. while (b) {
  6617. if (strlen(b->service) == strlen(browse->service) && memcmp(b->service, browse->service, strlen(b->service)) == 0 &&
  6618. strlen(b->proto) == strlen(browse->proto) && memcmp(b->proto, browse->proto, strlen(b->proto)) == 0) {
  6619. target_free = b;
  6620. b = b->next;
  6621. queueDetach(mdns_browse_t, _mdns_server->browse, target_free);
  6622. _mdns_browse_item_free(target_free);
  6623. } else {
  6624. b = b->next;
  6625. }
  6626. }
  6627. _mdns_browse_item_free(browse);
  6628. }
  6629. /**
  6630. * @brief Add new browse to the browse chain
  6631. */
  6632. static void _mdns_browse_add(mdns_browse_t *browse)
  6633. {
  6634. browse->state = BROWSE_RUNNING;
  6635. mdns_browse_t *queue = _mdns_server->browse;
  6636. bool found = false;
  6637. // looking for this browse in active browses
  6638. while (queue) {
  6639. if (strlen(queue->service) == strlen(browse->service) && memcmp(queue->service, browse->service, strlen(queue->service)) == 0 &&
  6640. strlen(queue->proto) == strlen(browse->proto) && memcmp(queue->proto, browse->proto, strlen(queue->proto)) == 0) {
  6641. found = true;
  6642. break;
  6643. }
  6644. queue = queue->next;
  6645. }
  6646. if (!found) {
  6647. browse->next = _mdns_server->browse;
  6648. _mdns_server->browse = browse;
  6649. }
  6650. for (uint8_t interface_idx = 0; interface_idx < MDNS_MAX_INTERFACES; interface_idx++) {
  6651. _mdns_browse_send(browse, (mdns_if_t)interface_idx);
  6652. }
  6653. if (found) {
  6654. _mdns_browse_item_free(browse);
  6655. }
  6656. }
  6657. /**
  6658. * @brief Send PTR query packet to all available interfaces for browsing.
  6659. */
  6660. static void _mdns_browse_send(mdns_browse_t *browse, mdns_if_t interface)
  6661. {
  6662. // Using search once for sending the PTR query
  6663. mdns_search_once_t search = {0};
  6664. search.instance = NULL;
  6665. search.service = browse->service;
  6666. search.proto = browse->proto;
  6667. search.type = MDNS_TYPE_PTR;
  6668. search.unicast = false;
  6669. search.result = NULL;
  6670. search.next = NULL;
  6671. for (uint8_t protocol_idx = 0; protocol_idx < MDNS_IP_PROTOCOL_MAX; protocol_idx++) {
  6672. _mdns_search_send_pcb(&search, interface, (mdns_ip_protocol_t)protocol_idx);
  6673. }
  6674. }
  6675. /**
  6676. * @brief Add result to browse, only add when the result is a new one.
  6677. */
  6678. static esp_err_t _mdns_add_browse_result(mdns_browse_sync_t *sync_browse, mdns_result_t *r)
  6679. {
  6680. mdns_browse_result_sync_t *sync_r = sync_browse->sync_result;
  6681. while (sync_r) {
  6682. if (sync_r->result == r) {
  6683. break;
  6684. }
  6685. sync_r = sync_r->next;
  6686. }
  6687. if (!sync_r) {
  6688. // Do not find, need to add the result to the list
  6689. mdns_browse_result_sync_t *new = (mdns_browse_result_sync_t *)malloc(sizeof(mdns_browse_result_sync_t));
  6690. if (!new) {
  6691. HOOK_MALLOC_FAILED;
  6692. return ESP_ERR_NO_MEM;
  6693. }
  6694. new->result = r;
  6695. new->next = sync_browse->sync_result;
  6696. sync_browse->sync_result = new;
  6697. }
  6698. return ESP_OK;
  6699. }
  6700. /**
  6701. * @brief Called from parser to add A/AAAA data to search result
  6702. */
  6703. static void _mdns_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip,
  6704. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse)
  6705. {
  6706. if (out_sync_browse->browse == NULL) {
  6707. return;
  6708. } else {
  6709. if (out_sync_browse->browse != browse) {
  6710. return;
  6711. }
  6712. }
  6713. mdns_result_t *r = NULL;
  6714. mdns_ip_addr_t *r_a = NULL;
  6715. if (browse) {
  6716. r = browse->result;
  6717. while (r) {
  6718. if (r->ip_protocol == ip_protocol) {
  6719. // Find the target result in browse result.
  6720. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
  6721. r_a = r->addr;
  6722. // Check if the address has already added in result.
  6723. while (r_a) {
  6724. #ifdef CONFIG_LWIP_IPV4
  6725. if (r_a->addr.type == ip->type && r_a->addr.type == ESP_IPADDR_TYPE_V4 && r_a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) {
  6726. break;
  6727. }
  6728. #endif
  6729. #ifdef CONFIG_LWIP_IPV6
  6730. if (r_a->addr.type == ip->type && r_a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(r_a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) {
  6731. break;
  6732. }
  6733. #endif
  6734. r_a = r_a->next;
  6735. }
  6736. if (!r_a) {
  6737. // The current IP is a new one, add it to the link list.
  6738. mdns_ip_addr_t *a = NULL;
  6739. a = _mdns_result_addr_create_ip(ip);
  6740. if (!a) {
  6741. return;
  6742. }
  6743. a->next = r->addr;
  6744. r->addr = a;
  6745. if (r->ttl != ttl) {
  6746. if (r->ttl == 0) {
  6747. r->ttl = ttl;
  6748. } else {
  6749. _mdns_result_update_ttl(r, ttl);
  6750. }
  6751. }
  6752. if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) {
  6753. return;
  6754. }
  6755. break;
  6756. }
  6757. }
  6758. }
  6759. r = r->next;
  6760. }
  6761. }
  6762. return;
  6763. }
  6764. /**
  6765. * @brief Called from packet parser to find matching running search
  6766. */
  6767. static mdns_browse_t *_mdns_browse_find_from(mdns_browse_t *b, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  6768. {
  6769. // For browse, we only care about the SRV, TXT, A and AAAA
  6770. if (type != MDNS_TYPE_SRV && type != MDNS_TYPE_A && type != MDNS_TYPE_AAAA && type != MDNS_TYPE_TXT) {
  6771. return NULL;
  6772. }
  6773. mdns_result_t *r = NULL;
  6774. while (b) {
  6775. if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) {
  6776. if (strcasecmp(name->service, b->service)
  6777. || strcasecmp(name->proto, b->proto)) {
  6778. b = b->next;
  6779. continue;
  6780. }
  6781. return b;
  6782. } else if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
  6783. r = b->result;
  6784. while (r) {
  6785. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) {
  6786. return b;
  6787. }
  6788. r = r->next;
  6789. }
  6790. b = b->next;
  6791. continue;
  6792. }
  6793. }
  6794. return b;
  6795. }
  6796. static bool is_txt_item_in_list(mdns_txt_item_t txt, uint8_t txt_value_len, mdns_txt_item_t *txt_list, uint8_t *txt_value_len_list, size_t txt_count)
  6797. {
  6798. for (size_t i = 0; i < txt_count; i++) {
  6799. if (strcmp(txt.key, txt_list[i].key) == 0) {
  6800. if (txt_value_len == txt_value_len_list[i] && memcmp(txt.value, txt_list[i].value, txt_value_len) == 0) {
  6801. return true;
  6802. } else {
  6803. // The key value is unique, so there is no need to continue searching.
  6804. return false;
  6805. }
  6806. }
  6807. }
  6808. return false;
  6809. }
  6810. /**
  6811. * @brief Called from parser to add TXT data to search result
  6812. */
  6813. static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto,
  6814. mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
  6815. uint32_t ttl, mdns_browse_sync_t *out_sync_browse)
  6816. {
  6817. if (out_sync_browse->browse == NULL) {
  6818. return;
  6819. } else {
  6820. if (out_sync_browse->browse != browse) {
  6821. return;
  6822. }
  6823. }
  6824. mdns_result_t *r = browse->result;
  6825. while (r) {
  6826. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol &&
  6827. !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) &&
  6828. !_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) &&
  6829. !_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) {
  6830. bool should_update = false;
  6831. if (r->txt) {
  6832. // Check if txt changed
  6833. if (txt_count != r->txt_count) {
  6834. should_update = true;
  6835. } else {
  6836. for (size_t txt_index = 0; txt_index < txt_count; txt_index++) {
  6837. if (!is_txt_item_in_list(txt[txt_index], txt_value_len[txt_index], r->txt, r->txt_value_len, r->txt_count)) {
  6838. should_update = true;
  6839. break;
  6840. }
  6841. }
  6842. }
  6843. // If the result has a previous txt entry, we delete it and re-add.
  6844. for (size_t i = 0; i < r->txt_count; i++) {
  6845. free((char *)(r->txt[i].key));
  6846. free((char *)(r->txt[i].value));
  6847. }
  6848. free(r->txt);
  6849. free(r->txt_value_len);
  6850. }
  6851. r->txt = txt;
  6852. r->txt_value_len = txt_value_len;
  6853. r->txt_count = txt_count;
  6854. if (r->ttl != ttl) {
  6855. uint32_t previous_ttl = r->ttl;
  6856. if (r->ttl == 0) {
  6857. r->ttl = ttl;
  6858. } else {
  6859. _mdns_result_update_ttl(r, ttl);
  6860. }
  6861. if (previous_ttl != r->ttl) {
  6862. should_update = true;
  6863. }
  6864. }
  6865. if (should_update) {
  6866. if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) {
  6867. return;
  6868. }
  6869. }
  6870. return;
  6871. }
  6872. r = r->next;
  6873. }
  6874. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  6875. if (!r) {
  6876. HOOK_MALLOC_FAILED;
  6877. goto free_txt;
  6878. }
  6879. memset(r, 0, sizeof(mdns_result_t));
  6880. r->instance_name = strdup(instance);
  6881. r->service_type = strdup(service);
  6882. r->proto = strdup(proto);
  6883. if (!r->instance_name || !r->service_type || !r->proto) {
  6884. free(r->instance_name);
  6885. free(r->service_type);
  6886. free(r->proto);
  6887. free(r);
  6888. return;
  6889. }
  6890. r->txt = txt;
  6891. r->txt_value_len = txt_value_len;
  6892. r->txt_count = txt_count;
  6893. r->esp_netif = _mdns_get_esp_netif(tcpip_if);
  6894. r->ip_protocol = ip_protocol;
  6895. r->ttl = ttl;
  6896. r->next = browse->result;
  6897. browse->result = r;
  6898. _mdns_add_browse_result(out_sync_browse, r);
  6899. return;
  6900. free_txt:
  6901. for (size_t i = 0; i < txt_count; i++) {
  6902. free((char *)(txt[i].key));
  6903. free((char *)(txt[i].value));
  6904. }
  6905. free(txt);
  6906. free(txt_value_len);
  6907. return;
  6908. }
  6909. static esp_err_t _mdns_copy_address_in_previous_result(mdns_result_t *result_list, mdns_result_t *r)
  6910. {
  6911. while (result_list) {
  6912. if (!_str_null_or_empty(result_list->hostname) && !_str_null_or_empty(r->hostname) && !strcasecmp(result_list->hostname, r->hostname) &&
  6913. result_list->ip_protocol == r->ip_protocol && result_list->addr && !r->addr) {
  6914. // If there is a same hostname in previous result, we need to copy the address here.
  6915. r->addr = copy_address_list(result_list->addr);
  6916. if (!r->addr) {
  6917. return ESP_ERR_NO_MEM;
  6918. }
  6919. break;
  6920. } else {
  6921. result_list = result_list->next;
  6922. }
  6923. }
  6924. return ESP_OK;
  6925. }
  6926. /**
  6927. * @brief Called from parser to add SRV data to search result
  6928. */
  6929. static void _mdns_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto,
  6930. uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse)
  6931. {
  6932. if (out_sync_browse->browse == NULL) {
  6933. return;
  6934. } else {
  6935. if (out_sync_browse->browse != browse) {
  6936. return;
  6937. }
  6938. }
  6939. mdns_result_t *r = browse->result;
  6940. while (r) {
  6941. if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol &&
  6942. !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) &&
  6943. !_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) &&
  6944. !_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) {
  6945. if (_str_null_or_empty(r->hostname) || strcasecmp(hostname, r->hostname)) {
  6946. r->hostname = strdup(hostname);
  6947. r->port = port;
  6948. if (!r->hostname) {
  6949. HOOK_MALLOC_FAILED;
  6950. return;
  6951. }
  6952. if (!r->addr) {
  6953. esp_err_t err = _mdns_copy_address_in_previous_result(browse->result, r);
  6954. if (err == ESP_ERR_NO_MEM) {
  6955. return;
  6956. }
  6957. }
  6958. if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) {
  6959. return;
  6960. }
  6961. }
  6962. if (r->ttl != ttl) {
  6963. uint32_t previous_ttl = r->ttl;
  6964. if (r->ttl == 0) {
  6965. r->ttl = ttl;
  6966. } else {
  6967. _mdns_result_update_ttl(r, ttl);
  6968. }
  6969. if (previous_ttl != r->ttl) {
  6970. if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) {
  6971. return;
  6972. }
  6973. }
  6974. }
  6975. return;
  6976. }
  6977. r = r->next;
  6978. }
  6979. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  6980. if (!r) {
  6981. HOOK_MALLOC_FAILED;
  6982. return;
  6983. }
  6984. memset(r, 0, sizeof(mdns_result_t));
  6985. r->hostname = strdup(hostname);
  6986. r->instance_name = strdup(instance);
  6987. r->service_type = strdup(service);
  6988. r->proto = strdup(proto);
  6989. if (!r->hostname || !r->instance_name || !r->service_type || !r->proto) {
  6990. HOOK_MALLOC_FAILED;
  6991. free(r->hostname);
  6992. free(r->instance_name);
  6993. free(r->service_type);
  6994. free(r->proto);
  6995. free(r);
  6996. return;
  6997. }
  6998. r->port = port;
  6999. r->esp_netif = _mdns_get_esp_netif(tcpip_if);
  7000. r->ip_protocol = ip_protocol;
  7001. r->ttl = ttl;
  7002. r->next = browse->result;
  7003. browse->result = r;
  7004. _mdns_add_browse_result(out_sync_browse, r);
  7005. return;
  7006. }
  7007. static void _mdns_browse_sync(mdns_browse_sync_t *browse_sync)
  7008. {
  7009. mdns_browse_t *browse = browse_sync->browse;
  7010. mdns_browse_result_sync_t *sync_result = browse_sync->sync_result;
  7011. while (sync_result) {
  7012. mdns_result_t *result = sync_result->result;
  7013. #ifdef MDNS_ENABLE_DEBUG
  7014. debug_printf_browse_result(result, browse_sync->browse);
  7015. #endif
  7016. browse->notifier(result);
  7017. if (result->ttl == 0) {
  7018. queueDetach(mdns_result_t, browse->result, result);
  7019. // Just free current result
  7020. result->next = NULL;
  7021. mdns_query_results_free(result);
  7022. }
  7023. sync_result = sync_result->next;
  7024. }
  7025. }
  7026. #ifdef MDNS_ENABLE_DEBUG
  7027. void _debug_printf_result(mdns_result_t *r_t)
  7028. {
  7029. mdns_ip_addr_t *r_a = NULL;
  7030. int addr_count = 0;
  7031. _mdns_dbg_printf("result esp_netif: %p\n", r_t->esp_netif);
  7032. _mdns_dbg_printf("result ip_protocol: %d\n", r_t->ip_protocol);
  7033. _mdns_dbg_printf("result hostname: %s\n", _str_null_or_empty(r_t->hostname) ? "NULL" : r_t->hostname);
  7034. _mdns_dbg_printf("result instance_name: %s\n", _str_null_or_empty(r_t->instance_name) ? "NULL" : r_t->instance_name);
  7035. _mdns_dbg_printf("result service_type: %s\n", _str_null_or_empty(r_t->service_type) ? "NULL" : r_t->service_type);
  7036. _mdns_dbg_printf("result proto: %s\n", _str_null_or_empty(r_t->proto) ? "NULL" : r_t->proto);
  7037. _mdns_dbg_printf("result port: %d\n", r_t->port);
  7038. _mdns_dbg_printf("result ttl: %" PRIu32 "\n", r_t->ttl);
  7039. for (int i = 0; i < r_t->txt_count; i++) {
  7040. _mdns_dbg_printf("result txt item%d, key: %s, value: %s\n", i, r_t->txt[i].key, r_t->txt[i].value);
  7041. }
  7042. r_a = r_t->addr;
  7043. while (r_a) {
  7044. #ifdef CONFIG_LWIP_IPV4
  7045. if (r_a->addr.type == ESP_IPADDR_TYPE_V4) {
  7046. _mdns_dbg_printf("Addr%d: " IPSTR "\n", addr_count++, IP2STR(&r_a->addr.u_addr.ip4));
  7047. }
  7048. #endif
  7049. #ifdef CONFIG_LWIP_IPV6
  7050. if (r_a->addr.type == ESP_IPADDR_TYPE_V6) {
  7051. _mdns_dbg_printf("Addr%d: " IPV6STR "\n", addr_count++, IPV62STR(r_a->addr.u_addr.ip6));
  7052. }
  7053. #endif
  7054. r_a = r_a->next;
  7055. }
  7056. }
  7057. static void debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t)
  7058. {
  7059. _mdns_dbg_printf("----------------sync browse %s.%s result---------------\n", b_t->service, b_t->proto);
  7060. _mdns_dbg_printf("browse pointer: %p\n", b_t);
  7061. _debug_printf_result(r_t);
  7062. }
  7063. static void debug_printf_browse_result_all(mdns_result_t *r_t)
  7064. {
  7065. int count = 0;
  7066. while (r_t) {
  7067. _mdns_dbg_printf("----------------result %d---------------\n", count++);
  7068. _debug_printf_result(r_t);
  7069. r_t = r_t->next;
  7070. }
  7071. }
  7072. #endif // MDNS_ENABLE_DEBUG