mdns.c 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "mdns.h"
  7. #include "mdns_private.h"
  8. #include "mdns_networking.h"
  9. #include "esp_log.h"
  10. #include <string.h>
  11. #include <sys/param.h>
  12. #ifdef MDNS_ENABLE_DEBUG
  13. void mdns_debug_packet(const uint8_t * data, size_t len);
  14. #endif
  15. // Internal size of IPv6 address is defined here as size of AAAA record in mdns packet
  16. // since the ip6_addr_t is defined in lwip and depends on using IPv6 zones
  17. #define _MDNS_SIZEOF_IP6_ADDR (MDNS_ANSWER_AAAA_SIZE)
  18. static const char * MDNS_DEFAULT_DOMAIN = "local";
  19. static const char * MDNS_SUB_STR = "_sub";
  20. mdns_server_t * _mdns_server = NULL;
  21. static mdns_host_item_t * _mdns_host_list = NULL;
  22. static mdns_host_item_t _mdns_self_host;
  23. static const char *TAG = "MDNS";
  24. static volatile TaskHandle_t _mdns_service_task_handle = NULL;
  25. static SemaphoreHandle_t _mdns_service_semaphore = NULL;
  26. static void _mdns_search_finish_done(void);
  27. 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);
  28. static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip,
  29. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
  30. static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
  31. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
  32. static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
  33. size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
  34. uint32_t ttl);
  35. static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance,
  36. const char * service_type, const char * proto, mdns_if_t tcpip_if,
  37. mdns_ip_protocol_t ip_protocol, uint32_t ttl);
  38. 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);
  39. static bool _mdns_append_host_list(mdns_out_answer_t ** destination, bool flush, bool bye);
  40. static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname);
  41. /*
  42. * @brief Internal collection of mdns supported interfaces
  43. *
  44. */
  45. static esp_netif_t * s_esp_netifs[MDNS_IF_MAX] = {};
  46. /*
  47. * @brief Convert mdns if to esp-netif handle
  48. */
  49. esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if)
  50. {
  51. if (tcpip_if < MDNS_IF_MAX) {
  52. if (s_esp_netifs[tcpip_if] == NULL) {
  53. // if local netif copy is NULL, try to search for the default interface key
  54. if (tcpip_if == MDNS_IF_STA) {
  55. s_esp_netifs[MDNS_IF_STA] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  56. } else if (tcpip_if == MDNS_IF_AP) {
  57. s_esp_netifs[MDNS_IF_AP] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
  58. #if CONFIG_ETH_ENABLED
  59. } else if (tcpip_if == MDNS_IF_ETH) {
  60. s_esp_netifs[MDNS_IF_ETH] = esp_netif_get_handle_from_ifkey("ETH_DEF");
  61. #endif
  62. }
  63. }
  64. return s_esp_netifs[tcpip_if];
  65. }
  66. return NULL;
  67. }
  68. /*
  69. * @brief Clean internal mdns interface's pointer
  70. */
  71. static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if) {
  72. if (tcpip_if < MDNS_IF_MAX) {
  73. s_esp_netifs[tcpip_if] = NULL;
  74. }
  75. }
  76. /*
  77. * @brief Convert esp-netif handle to mdns if
  78. */
  79. static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *interface)
  80. {
  81. for (int i=0; i<MDNS_IF_MAX; ++i) {
  82. if (interface == s_esp_netifs[i])
  83. return i;
  84. }
  85. return MDNS_IF_MAX;
  86. }
  87. static inline bool _str_null_or_empty(const char * str){
  88. return (str == NULL || *str == 0);
  89. }
  90. /*
  91. * @brief Appends/increments a number to name/instance in case of collision
  92. * */
  93. static char * _mdns_mangle_name(char* in) {
  94. char *p = strrchr(in, '-');
  95. int suffix = 0;
  96. if (p == NULL) {
  97. //No - in ``in``
  98. suffix = 2;
  99. } else {
  100. char *endp = NULL;
  101. suffix = strtol(p + 1, &endp, 10);
  102. if (*endp != 0) {
  103. //suffix is not numerical
  104. suffix = 2;
  105. p = NULL; //so we append -suffix to the entire string
  106. }
  107. }
  108. char *ret;
  109. if (p == NULL) {
  110. //need to add -2 to string
  111. ret = malloc(strlen(in) + 3);
  112. if (ret == NULL) {
  113. HOOK_MALLOC_FAILED;
  114. return NULL;
  115. }
  116. sprintf(ret, "%s-2", in);
  117. } else {
  118. ret = malloc(strlen(in) + 2); //one extra byte in case 9-10 or 99-100 etc
  119. if (ret == NULL) {
  120. HOOK_MALLOC_FAILED;
  121. return NULL;
  122. }
  123. strcpy(ret, in);
  124. int baseLen = p - in; //length of 'bla' in 'bla-123'
  125. //overwrite suffix with new suffix
  126. sprintf(ret + baseLen, "-%d", suffix + 1);
  127. }
  128. return ret;
  129. }
  130. static bool _mdns_service_match(const mdns_service_t * srv, const char * service, const char * proto,
  131. const char * hostname)
  132. {
  133. return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
  134. (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
  135. }
  136. /**
  137. * @brief finds service from given service type
  138. * @param server the server
  139. * @param service service type to match
  140. * @param proto proto to match
  141. * @param hostname hostname of the service (if non-null)
  142. *
  143. * @return the service item if found or NULL on error
  144. */
  145. static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char * proto, const char * hostname)
  146. {
  147. mdns_srv_item_t * s = _mdns_server->services;
  148. while (s) {
  149. if (_mdns_service_match(s->service, service, proto, hostname)) {
  150. return s;
  151. }
  152. s = s->next;
  153. }
  154. return NULL;
  155. }
  156. static mdns_host_item_t * mdns_get_host_item(const char * hostname)
  157. {
  158. if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) {
  159. return &_mdns_self_host;
  160. }
  161. mdns_host_item_t * host = _mdns_host_list;
  162. while (host != NULL) {
  163. if (strcasecmp(host->hostname, hostname) == 0) {
  164. return host;
  165. }
  166. host = host->next;
  167. }
  168. return NULL;
  169. }
  170. static bool _mdns_can_add_more_services(void)
  171. {
  172. mdns_srv_item_t * s = _mdns_server->services;
  173. uint16_t service_num = 0;
  174. while (s) {
  175. service_num ++;
  176. s = s->next;
  177. if (service_num >= MDNS_MAX_SERVICES) {
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet)
  184. {
  185. mdns_action_t * action = NULL;
  186. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  187. if (!action) {
  188. HOOK_MALLOC_FAILED;
  189. return ESP_ERR_NO_MEM;
  190. }
  191. action->type = ACTION_RX_HANDLE;
  192. action->data.rx_handle.packet = packet;
  193. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  194. free(action);
  195. return ESP_ERR_NO_MEM;
  196. }
  197. return ESP_OK;
  198. }
  199. static const char *_mdns_get_default_instance_name(void)
  200. {
  201. if (_mdns_server && !_str_null_or_empty(_mdns_server->instance)) {
  202. return _mdns_server->instance;
  203. }
  204. if (_mdns_server && !_str_null_or_empty(_mdns_server->hostname)) {
  205. return _mdns_server->hostname;
  206. }
  207. return NULL;
  208. }
  209. /**
  210. * @brief Get the service name of a service
  211. */
  212. static const char * _mdns_get_service_instance_name(const mdns_service_t * service)
  213. {
  214. if (service && !_str_null_or_empty(service->instance)) {
  215. return service->instance;
  216. }
  217. return _mdns_get_default_instance_name();
  218. }
  219. static bool _mdns_instance_name_match(const char *lhs, const char *rhs)
  220. {
  221. if (lhs == NULL) {
  222. lhs = _mdns_get_default_instance_name();
  223. }
  224. if (rhs == NULL) {
  225. rhs = _mdns_get_default_instance_name();
  226. }
  227. return !strcasecmp(lhs, rhs);
  228. }
  229. static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service,
  230. const char *proto, const char *hostname)
  231. {
  232. return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
  233. !strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
  234. }
  235. static mdns_srv_item_t *_mdns_get_service_item_instance(const char *instance, const char *service, const char *proto,
  236. const char *hostname)
  237. {
  238. mdns_srv_item_t *s = _mdns_server->services;
  239. while (s) {
  240. if (_mdns_service_match_instance(s->service, instance, service, proto, hostname)) {
  241. return s;
  242. }
  243. s = s->next;
  244. }
  245. return NULL;
  246. }
  247. /**
  248. * @brief reads MDNS FQDN into mdns_name_t structure
  249. * FQDN is in format: [hostname.|[instance.]_service._proto.]local.
  250. *
  251. * @param packet MDNS packet
  252. * @param start Starting point of FQDN
  253. * @param name mdns_name_t structure to populate
  254. * @param buf temporary char buffer
  255. *
  256. * @return the address after the parsed FQDN in the packet or NULL on error
  257. */
  258. static const uint8_t * _mdns_read_fqdn(const uint8_t * packet, const uint8_t * start, mdns_name_t * name, char * buf)
  259. {
  260. size_t index = 0;
  261. while (start[index]) {
  262. if (name->parts == 4) {
  263. name->invalid = true;
  264. }
  265. uint8_t len = start[index++];
  266. if (len < 0xC0) {
  267. if (len > 63) {
  268. //length can not be more than 63
  269. return NULL;
  270. }
  271. uint8_t i;
  272. for (i=0; i<len; i++) {
  273. buf[i] = start[index++];
  274. }
  275. buf[len] = '\0';
  276. if (name->parts == 1 && buf[0] != '_'
  277. && (strcasecmp(buf, MDNS_DEFAULT_DOMAIN) != 0)
  278. && (strcasecmp(buf, "arpa") != 0)
  279. && (strcasecmp(buf, "ip6") != 0)
  280. && (strcasecmp(buf, "in-addr") != 0)) {
  281. strlcat(name->host, ".", sizeof(name->host));
  282. strlcat(name->host, buf, sizeof(name->host));
  283. } else if (strcasecmp(buf, MDNS_SUB_STR) == 0) {
  284. name->sub = 1;
  285. } else if (!name->invalid) {
  286. char* mdns_name_ptrs[]={name->host, name->service, name->proto, name->domain};
  287. memcpy(mdns_name_ptrs[name->parts++], buf, len+1);
  288. }
  289. } else {
  290. size_t address = (((uint16_t)len & 0x3F) << 8) | start[index++];
  291. if ((packet + address) >= start) {
  292. //reference address can not be after where we are
  293. return NULL;
  294. }
  295. if (_mdns_read_fqdn(packet, packet + address, name, buf)) {
  296. return start + index;
  297. }
  298. return NULL;
  299. }
  300. }
  301. return start + index + 1;
  302. }
  303. /**
  304. * @brief sets uint16_t value in a packet
  305. *
  306. * @param packet MDNS packet
  307. * @param index offset of uint16_t value
  308. * @param value the value to set
  309. */
  310. static inline void _mdns_set_u16(uint8_t * packet, uint16_t index, uint16_t value)
  311. {
  312. if ((index + 1) >= MDNS_MAX_PACKET_SIZE) {
  313. return;
  314. }
  315. packet[index] = (value >> 8) & 0xFF;
  316. packet[index+1] = value & 0xFF;
  317. }
  318. /**
  319. * @brief appends byte in a packet, incrementing the index
  320. *
  321. * @param packet MDNS packet
  322. * @param index offset in the packet
  323. * @param value the value to set
  324. *
  325. * @return length of added data: 0 on error or 1 on success
  326. */
  327. static inline uint8_t _mdns_append_u8(uint8_t * packet, uint16_t * index, uint8_t value)
  328. {
  329. if (*index >= MDNS_MAX_PACKET_SIZE) {
  330. return 0;
  331. }
  332. packet[*index] = value;
  333. *index += 1;
  334. return 1;
  335. }
  336. /**
  337. * @brief appends uint16_t in a packet, incrementing the index
  338. *
  339. * @param packet MDNS packet
  340. * @param index offset in the packet
  341. * @param value the value to set
  342. *
  343. * @return length of added data: 0 on error or 2 on success
  344. */
  345. static inline uint8_t _mdns_append_u16(uint8_t * packet, uint16_t * index, uint16_t value)
  346. {
  347. if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) {
  348. return 0;
  349. }
  350. _mdns_append_u8(packet, index, (value >> 8) & 0xFF);
  351. _mdns_append_u8(packet, index, value & 0xFF);
  352. return 2;
  353. }
  354. /**
  355. * @brief appends uint32_t in a packet, incrementing the index
  356. *
  357. * @param packet MDNS packet
  358. * @param index offset in the packet
  359. * @param value the value to set
  360. *
  361. * @return length of added data: 0 on error or 4 on success
  362. */
  363. static inline uint8_t _mdns_append_u32(uint8_t * packet, uint16_t * index, uint32_t value)
  364. {
  365. if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
  366. return 0;
  367. }
  368. _mdns_append_u8(packet, index, (value >> 24) & 0xFF);
  369. _mdns_append_u8(packet, index, (value >> 16) & 0xFF);
  370. _mdns_append_u8(packet, index, (value >> 8) & 0xFF);
  371. _mdns_append_u8(packet, index, value & 0xFF);
  372. return 4;
  373. }
  374. /**
  375. * @brief appends answer type, class, ttl and data length to a packet, incrementing the index
  376. *
  377. * @param packet MDNS packet
  378. * @param index offset in the packet
  379. * @param type answer type
  380. * @param ttl answer ttl
  381. *
  382. * @return length of added data: 0 on error or 10 on success
  383. */
  384. static inline uint8_t _mdns_append_type(uint8_t * packet, uint16_t * index, uint8_t type, bool flush, uint32_t ttl)
  385. {
  386. if ((*index + 10) >= MDNS_MAX_PACKET_SIZE) {
  387. return 0;
  388. }
  389. uint16_t mdns_class = MDNS_CLASS_IN;
  390. if (flush) {
  391. mdns_class = MDNS_CLASS_IN_FLUSH_CACHE;
  392. }
  393. if (type == MDNS_ANSWER_PTR) {
  394. _mdns_append_u16(packet, index, MDNS_TYPE_PTR);
  395. _mdns_append_u16(packet, index, mdns_class);
  396. } else if (type == MDNS_ANSWER_TXT) {
  397. _mdns_append_u16(packet, index, MDNS_TYPE_TXT);
  398. _mdns_append_u16(packet, index, mdns_class);
  399. } else if (type == MDNS_ANSWER_SRV) {
  400. _mdns_append_u16(packet, index, MDNS_TYPE_SRV);
  401. _mdns_append_u16(packet, index, mdns_class);
  402. } else if (type == MDNS_ANSWER_A) {
  403. _mdns_append_u16(packet, index, MDNS_TYPE_A);
  404. _mdns_append_u16(packet, index, mdns_class);
  405. } else if (type == MDNS_ANSWER_AAAA) {
  406. _mdns_append_u16(packet, index, MDNS_TYPE_AAAA);
  407. _mdns_append_u16(packet, index, mdns_class);
  408. } else {
  409. return 0;
  410. }
  411. _mdns_append_u32(packet, index, ttl);
  412. _mdns_append_u16(packet, index, 0);
  413. return 10;
  414. }
  415. static inline uint8_t _mdns_append_string_with_len(uint8_t * packet, uint16_t * index, const char * string, uint8_t len)
  416. {
  417. if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
  418. return 0;
  419. }
  420. _mdns_append_u8(packet, index, len);
  421. memcpy(packet + *index, string, len);
  422. *index += len;
  423. return len + 1;
  424. }
  425. /**
  426. * @brief appends single string to a packet, incrementing the index
  427. *
  428. * @param packet MDNS packet
  429. * @param index offset in the packet
  430. * @param string the string to append
  431. *
  432. * @return length of added data: 0 on error or length of the string + 1 on success
  433. */
  434. static inline uint8_t _mdns_append_string(uint8_t * packet, uint16_t * index, const char * string)
  435. {
  436. uint8_t len = strlen(string);
  437. if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
  438. return 0;
  439. }
  440. _mdns_append_u8(packet, index, len);
  441. memcpy(packet + *index, string, len);
  442. *index += len;
  443. return len + 1;
  444. }
  445. /**
  446. * @brief appends FQDN to a packet, incrementing the index and
  447. * compressing the output if previous occurrence of the string (or part of it) has been found
  448. *
  449. * @param packet MDNS packet
  450. * @param index offset in the packet
  451. * @param strings string array containing the parts of the FQDN
  452. * @param count number of strings in the array
  453. *
  454. * @return length of added data: 0 on error or length on success
  455. */
  456. static uint16_t _mdns_append_fqdn(uint8_t * packet, uint16_t * index, const char * strings[], uint8_t count)
  457. {
  458. if (!count) {
  459. //empty string so terminate
  460. return _mdns_append_u8(packet, index, 0);
  461. }
  462. mdns_name_t name;
  463. static char buf[MDNS_NAME_BUF_LEN];
  464. uint8_t len = strlen(strings[0]);
  465. //try to find first the string length in the packet (if it exists)
  466. uint8_t * len_location = (uint8_t *)memchr(packet, (char)len, *index);
  467. while (len_location) {
  468. //check if the string after len_location is the string that we are looking for
  469. if (memcmp(len_location+1, strings[0], len)) { //not continuing with our string
  470. search_next:
  471. //try and find the length byte further in the packet
  472. len_location = (uint8_t *)memchr(len_location+1, (char)len, *index - (len_location+1 - packet));
  473. continue;
  474. }
  475. //seems that we might have found the string that we are looking for
  476. //read the destination into name and compare
  477. name.parts = 0;
  478. name.sub = 0;
  479. name.host[0] = 0;
  480. name.service[0] = 0;
  481. name.proto[0] = 0;
  482. name.domain[0] = 0;
  483. const uint8_t * content = _mdns_read_fqdn(packet, len_location, &name, buf);
  484. if (!content) {
  485. //not a readable fqdn?
  486. return 0;
  487. }
  488. if (name.parts == count) {
  489. uint8_t i;
  490. for (i=0; i<count; i++) {
  491. if (strcasecmp(strings[i], (const char *)&name + (i * (MDNS_NAME_BUF_LEN)))) {
  492. //not our string! let's search more
  493. goto search_next;
  494. }
  495. }
  496. //we actually have found the string
  497. break;
  498. } else {
  499. goto search_next;
  500. }
  501. }
  502. //string is not yet in the packet, so let's add it
  503. if (!len_location) {
  504. uint8_t written = _mdns_append_string(packet, index, strings[0]);
  505. if (!written) {
  506. return 0;
  507. }
  508. //run the same for the other strings in the name
  509. return written + _mdns_append_fqdn(packet, index, &strings[1], count - 1);
  510. }
  511. //we have found the string so let's insert a pointer to it instead
  512. uint16_t offset = len_location - packet;
  513. offset |= MDNS_NAME_REF;
  514. return _mdns_append_u16(packet, index, offset);
  515. }
  516. /**
  517. * @brief appends PTR record for service to a packet, incrementing the index
  518. *
  519. * @param packet MDNS packet
  520. * @param index offset in the packet
  521. * @param server the server that is hosting the service
  522. * @param service the service to add record for
  523. *
  524. * @return length of added data: 0 on error or length on success
  525. */
  526. 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)
  527. {
  528. const char * str[4];
  529. uint16_t record_length = 0;
  530. uint8_t part_length;
  531. if (service == NULL) {
  532. return 0;
  533. }
  534. str[0] = instance;
  535. str[1] = service;
  536. str[2] = proto;
  537. str[3] = MDNS_DEFAULT_DOMAIN;
  538. part_length = _mdns_append_fqdn(packet, index, str + 1, 3);
  539. if (!part_length) {
  540. return 0;
  541. }
  542. record_length += part_length;
  543. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye?0:MDNS_ANSWER_PTR_TTL);
  544. if (!part_length) {
  545. return 0;
  546. }
  547. record_length += part_length;
  548. uint16_t data_len_location = *index - 2;
  549. part_length = _mdns_append_fqdn(packet, index, str, 4);
  550. if (!part_length) {
  551. return 0;
  552. }
  553. _mdns_set_u16(packet, data_len_location, part_length);
  554. record_length += part_length;
  555. return record_length;
  556. }
  557. /**
  558. * @brief appends DNS-SD PTR record for service to a packet, incrementing the index
  559. *
  560. * @param packet MDNS packet
  561. * @param index offset in the packet
  562. * @param server the server that is hosting the service
  563. * @param service the service to add record for
  564. *
  565. * @return length of added data: 0 on error or length on success
  566. */
  567. static uint16_t _mdns_append_sdptr_record(uint8_t * packet, uint16_t * index, mdns_service_t * service, bool flush, bool bye)
  568. {
  569. const char * str[3];
  570. const char * sd_str[4];
  571. uint16_t record_length = 0;
  572. uint8_t part_length;
  573. if (service == NULL) {
  574. return 0;
  575. }
  576. sd_str[0] = (char*)"_services";
  577. sd_str[1] = (char*)"_dns-sd";
  578. sd_str[2] = (char*)"_udp";
  579. sd_str[3] = MDNS_DEFAULT_DOMAIN;
  580. str[0] = service->service;
  581. str[1] = service->proto;
  582. str[2] = MDNS_DEFAULT_DOMAIN;
  583. part_length = _mdns_append_fqdn(packet, index, sd_str, 4);
  584. record_length += part_length;
  585. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, flush, MDNS_ANSWER_PTR_TTL);
  586. if (!part_length) {
  587. return 0;
  588. }
  589. record_length += part_length;
  590. uint16_t data_len_location = *index - 2;
  591. part_length = _mdns_append_fqdn(packet, index, str, 3);
  592. if (!part_length) {
  593. return 0;
  594. }
  595. _mdns_set_u16(packet, data_len_location, part_length);
  596. record_length += part_length;
  597. return record_length;
  598. }
  599. /**
  600. * @brief appends TXT record for service to a packet, incrementing the index
  601. *
  602. * @param packet MDNS packet
  603. * @param index offset in the packet
  604. * @param server the server that is hosting the service
  605. * @param service the service to add record for
  606. *
  607. * @return length of added data: 0 on error or length on success
  608. */
  609. static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns_service_t * service, bool flush, bool bye)
  610. {
  611. const char * str[4];
  612. uint16_t record_length = 0;
  613. uint8_t part_length;
  614. if (service == NULL) {
  615. return 0;
  616. }
  617. str[0] = _mdns_get_service_instance_name(service);
  618. str[1] = service->service;
  619. str[2] = service->proto;
  620. str[3] = MDNS_DEFAULT_DOMAIN;
  621. if (!str[0]) {
  622. return 0;
  623. }
  624. part_length = _mdns_append_fqdn(packet, index, str, 4);
  625. if (!part_length) {
  626. return 0;
  627. }
  628. record_length += part_length;
  629. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_TXT, flush, bye?0:MDNS_ANSWER_TXT_TTL);
  630. if (!part_length) {
  631. return 0;
  632. }
  633. record_length += part_length;
  634. uint16_t data_len_location = *index - 2;
  635. uint16_t data_len = 0;
  636. char * tmp;
  637. mdns_txt_linked_item_t * txt = service->txt;
  638. while (txt) {
  639. uint8_t txt_data_len = strlen(txt->key) + txt->value_len + 1;
  640. tmp = (char *)malloc(txt_data_len);
  641. if (tmp) {
  642. int offset = sprintf(tmp, "%s=", txt->key);
  643. memcpy(tmp + offset, txt->value, txt->value_len);
  644. uint8_t l = _mdns_append_string_with_len(packet, index, tmp, txt_data_len);
  645. free(tmp);
  646. if (!l) {
  647. return 0;
  648. }
  649. data_len += l;
  650. } else {
  651. HOOK_MALLOC_FAILED;
  652. // continue
  653. }
  654. txt = txt->next;
  655. }
  656. if (!data_len) {
  657. data_len = 1;
  658. packet[*index] = 0;
  659. *index = *index + 1;
  660. }
  661. _mdns_set_u16(packet, data_len_location, data_len);
  662. record_length += data_len;
  663. return record_length;
  664. }
  665. /**
  666. * @brief appends SRV record for service to a packet, incrementing the index
  667. *
  668. * @param packet MDNS packet
  669. * @param index offset in the packet
  670. * @param server the server that is hosting the service
  671. * @param service the service to add record for
  672. *
  673. * @return length of added data: 0 on error or length on success
  674. */
  675. static uint16_t _mdns_append_srv_record(uint8_t * packet, uint16_t * index, mdns_service_t * service, bool flush, bool bye)
  676. {
  677. const char * str[4];
  678. uint16_t record_length = 0;
  679. uint8_t part_length;
  680. if (service == NULL) {
  681. return 0;
  682. }
  683. str[0] = _mdns_get_service_instance_name(service);
  684. str[1] = service->service;
  685. str[2] = service->proto;
  686. str[3] = MDNS_DEFAULT_DOMAIN;
  687. if (!str[0]) {
  688. return 0;
  689. }
  690. part_length = _mdns_append_fqdn(packet, index, str, 4);
  691. if (!part_length) {
  692. return 0;
  693. }
  694. record_length += part_length;
  695. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_SRV, flush, bye?0:MDNS_ANSWER_SRV_TTL);
  696. if (!part_length) {
  697. return 0;
  698. }
  699. record_length += part_length;
  700. uint16_t data_len_location = *index - 2;
  701. part_length = 0;
  702. part_length += _mdns_append_u16(packet, index, service->priority);
  703. part_length += _mdns_append_u16(packet, index, service->weight);
  704. part_length += _mdns_append_u16(packet, index, service->port);
  705. if (part_length != 6) {
  706. return 0;
  707. }
  708. if (service->hostname) {
  709. str[0] = service->hostname;
  710. } else {
  711. str[0] = _mdns_server->hostname;
  712. }
  713. str[1] = MDNS_DEFAULT_DOMAIN;
  714. if (_str_null_or_empty(str[0])) {
  715. return 0;
  716. }
  717. part_length = _mdns_append_fqdn(packet, index, str, 2);
  718. if (!part_length) {
  719. return 0;
  720. }
  721. _mdns_set_u16(packet, data_len_location, part_length + 6);
  722. record_length += part_length + 6;
  723. return record_length;
  724. }
  725. /**
  726. * @brief appends A record to a packet, incrementing the index
  727. *
  728. * @param packet MDNS packet
  729. * @param index offset in the packet
  730. * @param hostname the hostname address to add
  731. * @param ip the IP address to add
  732. *
  733. * @return length of added data: 0 on error or length on success
  734. */
  735. static uint16_t _mdns_append_a_record(uint8_t * packet, uint16_t * index, const char * hostname, uint32_t ip, bool flush, bool bye)
  736. {
  737. const char * str[2];
  738. uint16_t record_length = 0;
  739. uint8_t part_length;
  740. str[0] = hostname;
  741. str[1] = MDNS_DEFAULT_DOMAIN;
  742. if (_str_null_or_empty(str[0])) {
  743. return 0;
  744. }
  745. part_length = _mdns_append_fqdn(packet, index, str, 2);
  746. if (!part_length) {
  747. return 0;
  748. }
  749. record_length += part_length;
  750. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_A, flush, bye?0:MDNS_ANSWER_A_TTL);
  751. if (!part_length) {
  752. return 0;
  753. }
  754. record_length += part_length;
  755. uint16_t data_len_location = *index - 2;
  756. if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
  757. return 0;
  758. }
  759. _mdns_append_u8(packet, index, ip & 0xFF);
  760. _mdns_append_u8(packet, index, (ip >> 8) & 0xFF);
  761. _mdns_append_u8(packet, index, (ip >> 16) & 0xFF);
  762. _mdns_append_u8(packet, index, (ip >> 24) & 0xFF);
  763. _mdns_set_u16(packet, data_len_location, 4);
  764. record_length += 4;
  765. return record_length;
  766. }
  767. #if CONFIG_LWIP_IPV6
  768. /**
  769. * @brief appends AAAA record to a packet, incrementing the index
  770. *
  771. * @param packet MDNS packet
  772. * @param index offset in the packet
  773. * @param hostname the hostname address to add
  774. * @param ipv6 the IPv6 address to add
  775. *
  776. * @return length of added data: 0 on error or length on success
  777. */
  778. static uint16_t _mdns_append_aaaa_record(uint8_t * packet, uint16_t * index, const char * hostname, uint8_t * ipv6, bool flush, bool bye)
  779. {
  780. const char * str[2];
  781. uint16_t record_length = 0;
  782. uint8_t part_length;
  783. str[0] = hostname;
  784. str[1] = MDNS_DEFAULT_DOMAIN;
  785. if (_str_null_or_empty(str[0])) {
  786. return 0;
  787. }
  788. part_length = _mdns_append_fqdn(packet, index, str, 2);
  789. if (!part_length) {
  790. return 0;
  791. }
  792. record_length += part_length;
  793. part_length = _mdns_append_type(packet, index, MDNS_ANSWER_AAAA, flush, bye?0:MDNS_ANSWER_AAAA_TTL);
  794. if (!part_length) {
  795. return 0;
  796. }
  797. record_length += part_length;
  798. uint16_t data_len_location = *index - 2;
  799. if ((*index + MDNS_ANSWER_AAAA_SIZE) > MDNS_MAX_PACKET_SIZE) {
  800. return 0;
  801. }
  802. part_length = MDNS_ANSWER_AAAA_SIZE;
  803. memcpy(packet + *index, ipv6, part_length);
  804. *index += part_length;
  805. _mdns_set_u16(packet, data_len_location, part_length);
  806. record_length += part_length;
  807. return record_length;
  808. }
  809. #endif
  810. /**
  811. * @brief Append question to packet
  812. */
  813. static uint16_t _mdns_append_question(uint8_t * packet, uint16_t * index, mdns_out_question_t * q)
  814. {
  815. const char * str[4];
  816. uint8_t str_index = 0;
  817. uint8_t part_length;
  818. if (q->host) {
  819. str[str_index++] = q->host;
  820. }
  821. if (q->service) {
  822. str[str_index++] = q->service;
  823. }
  824. if (q->proto) {
  825. str[str_index++] = q->proto;
  826. }
  827. if (q->domain) {
  828. str[str_index++] = q->domain;
  829. }
  830. part_length = _mdns_append_fqdn(packet, index, str, str_index);
  831. if (!part_length) {
  832. return 0;
  833. }
  834. part_length += _mdns_append_u16(packet, index, q->type);
  835. part_length += _mdns_append_u16(packet, index, q->unicast?0x8001:0x0001);
  836. return part_length;
  837. }
  838. /**
  839. * @brief Helper to get either ETH or STA if the other is provided
  840. * Used when two interfaces are on the same subnet
  841. */
  842. static mdns_if_t _mdns_get_other_if (mdns_if_t tcpip_if)
  843. {
  844. if (tcpip_if == MDNS_IF_STA) {
  845. return MDNS_IF_ETH;
  846. } else if (tcpip_if == MDNS_IF_ETH) {
  847. return MDNS_IF_STA;
  848. }
  849. return MDNS_IF_MAX;
  850. }
  851. /**
  852. * @brief Check if interface is duplicate (two interfaces on the same subnet)
  853. */
  854. static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
  855. {
  856. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  857. if (other_if == MDNS_IF_MAX) {
  858. return false;
  859. }
  860. if (_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP
  861. || _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP
  862. || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP
  863. || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP
  864. ) {
  865. return true;
  866. }
  867. return false;
  868. }
  869. #if CONFIG_LWIP_IPV6
  870. /**
  871. * @brief Check if IPv6 address is NULL
  872. */
  873. static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
  874. {
  875. uint8_t i;
  876. uint8_t * data = (uint8_t *)ip6.addr;
  877. for (i=0; i<_MDNS_SIZEOF_IP6_ADDR; i++) {
  878. if (data[i]) {
  879. return false;
  880. }
  881. }
  882. return true;
  883. }
  884. #endif
  885. static uint8_t _mdns_append_host_answer(uint8_t * packet, uint16_t * index, mdns_host_item_t * host,
  886. uint8_t address_type, bool flush, bool bye)
  887. {
  888. mdns_ip_addr_t * addr = host->address_list;
  889. uint8_t num_records = 0;
  890. while (addr != NULL) {
  891. if (addr->addr.type == address_type) {
  892. if (address_type == ESP_IPADDR_TYPE_V4 &&
  893. _mdns_append_a_record(packet, index, host->hostname, addr->addr.u_addr.ip4.addr, flush, bye) <= 0) {
  894. break;
  895. }
  896. #if CONFIG_LWIP_IPV6
  897. if (address_type == ESP_IPADDR_TYPE_V6 &&
  898. _mdns_append_aaaa_record(packet, index, host->hostname, (uint8_t *)addr->addr.u_addr.ip6.addr, flush,
  899. bye) <= 0) {
  900. break;
  901. }
  902. #endif // CONFIG_LWIP_IPV6
  903. num_records++;
  904. }
  905. addr = addr->next;
  906. }
  907. return num_records;
  908. }
  909. /**
  910. * @brief Append answer to packet
  911. *
  912. * @return number of answers added to the packet
  913. */
  914. static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_answer_t * answer, mdns_if_t tcpip_if)
  915. {
  916. if (answer->type == MDNS_TYPE_PTR) {
  917. if (answer->service) {
  918. return _mdns_append_ptr_record(packet, index,
  919. _mdns_get_service_instance_name(answer->service),
  920. answer->service->service, answer->service->proto,
  921. answer->flush, answer->bye) > 0;
  922. } else {
  923. return _mdns_append_ptr_record(packet, index,
  924. answer->custom_instance, answer->custom_service, answer->custom_proto,
  925. answer->flush, answer->bye) > 0;
  926. }
  927. } else if (answer->type == MDNS_TYPE_SRV) {
  928. return _mdns_append_srv_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
  929. } else if (answer->type == MDNS_TYPE_TXT) {
  930. return _mdns_append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
  931. } else if (answer->type == MDNS_TYPE_SDPTR) {
  932. return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
  933. } else if (answer->type == MDNS_TYPE_A) {
  934. if (answer->host == &_mdns_self_host) {
  935. esp_netif_ip_info_t if_ip_info;
  936. if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) {
  937. return 0;
  938. }
  939. if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
  940. return 0;
  941. }
  942. if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) <= 0) {
  943. return 0;
  944. }
  945. if (!_mdns_if_is_dup(tcpip_if)) {
  946. return 1;
  947. }
  948. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  949. if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &if_ip_info)) {
  950. return 1;
  951. }
  952. if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) > 0) {
  953. return 2;
  954. }
  955. return 1;
  956. } else if (answer->host != NULL) {
  957. return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye);
  958. }
  959. }
  960. #if CONFIG_LWIP_IPV6
  961. else if (answer->type == MDNS_TYPE_AAAA) {
  962. if (answer->host == &_mdns_self_host) {
  963. struct esp_ip6_addr if_ip6;
  964. if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) {
  965. return 0;
  966. }
  967. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) {
  968. return 0;
  969. }
  970. if (_ipv6_address_is_zero(if_ip6)) {
  971. return 0;
  972. }
  973. if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t*)if_ip6.addr, answer->flush, answer->bye) <= 0) {
  974. return 0;
  975. }
  976. if (!_mdns_if_is_dup(tcpip_if)) {
  977. return 1;
  978. }
  979. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  980. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &if_ip6)) {
  981. return 1;
  982. }
  983. if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t*)if_ip6.addr, answer->flush, answer->bye) > 0) {
  984. return 2;
  985. }
  986. return 1;
  987. } else if (answer->host != NULL) {
  988. return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush, answer->bye);
  989. }
  990. }
  991. #endif
  992. return 0;
  993. }
  994. /**
  995. * @brief sends a packet
  996. *
  997. * @param p the packet
  998. */
  999. static void _mdns_dispatch_tx_packet(mdns_tx_packet_t * p)
  1000. {
  1001. static uint8_t packet[MDNS_MAX_PACKET_SIZE];
  1002. uint16_t index = MDNS_HEAD_LEN;
  1003. memset(packet, 0, MDNS_HEAD_LEN);
  1004. mdns_out_question_t * q;
  1005. mdns_out_answer_t * a;
  1006. uint8_t count;
  1007. _mdns_set_u16(packet, MDNS_HEAD_FLAGS_OFFSET, p->flags);
  1008. _mdns_set_u16(packet, MDNS_HEAD_ID_OFFSET, p->id);
  1009. count = 0;
  1010. q = p->questions;
  1011. while (q) {
  1012. if (_mdns_append_question(packet, &index, q)) {
  1013. count++;
  1014. }
  1015. q = q->next;
  1016. }
  1017. _mdns_set_u16(packet, MDNS_HEAD_QUESTIONS_OFFSET, count);
  1018. count = 0;
  1019. a = p->answers;
  1020. while (a) {
  1021. count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
  1022. a = a->next;
  1023. }
  1024. _mdns_set_u16(packet, MDNS_HEAD_ANSWERS_OFFSET, count);
  1025. count = 0;
  1026. a = p->servers;
  1027. while (a) {
  1028. count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
  1029. a = a->next;
  1030. }
  1031. _mdns_set_u16(packet, MDNS_HEAD_SERVERS_OFFSET, count);
  1032. count = 0;
  1033. a = p->additional;
  1034. while (a) {
  1035. count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
  1036. a = a->next;
  1037. }
  1038. _mdns_set_u16(packet, MDNS_HEAD_ADDITIONAL_OFFSET, count);
  1039. #ifdef MDNS_ENABLE_DEBUG
  1040. _mdns_dbg_printf("\nTX[%u][%u]: ", p->tcpip_if, p->ip_protocol);
  1041. if (p->dst.type == ESP_IPADDR_TYPE_V4) {
  1042. _mdns_dbg_printf("To: " IPSTR ":%u, ", IP2STR(&p->dst.u_addr.ip4), p->port);
  1043. } else {
  1044. _mdns_dbg_printf("To: " IPV6STR ":%u, ", IPV62STR(p->dst.u_addr.ip6), p->port);
  1045. }
  1046. mdns_debug_packet(packet, index);
  1047. #endif
  1048. _mdns_udp_pcb_write(p->tcpip_if, p->ip_protocol, &p->dst, p->port, packet, index);
  1049. }
  1050. /**
  1051. * @brief frees a packet
  1052. *
  1053. * @param packet the packet
  1054. */
  1055. static void _mdns_free_tx_packet(mdns_tx_packet_t * packet)
  1056. {
  1057. if (!packet) {
  1058. return;
  1059. }
  1060. mdns_out_question_t * q = packet->questions;
  1061. while (q) {
  1062. mdns_out_question_t * next = q->next;
  1063. if (q->own_dynamic_memory) {
  1064. free((char *)q->host);
  1065. free((char *)q->service);
  1066. free((char *)q->proto);
  1067. free((char *)q->domain);
  1068. }
  1069. free(q);
  1070. q = next;
  1071. }
  1072. queueFree(mdns_out_answer_t, packet->answers);
  1073. queueFree(mdns_out_answer_t, packet->servers);
  1074. queueFree(mdns_out_answer_t, packet->additional);
  1075. free(packet);
  1076. }
  1077. /**
  1078. * @brief schedules a packet to be sent after given milliseconds
  1079. *
  1080. * @param packet the packet
  1081. * @param ms_after number of milliseconds after which the packet should be dispatched
  1082. */
  1083. static void _mdns_schedule_tx_packet(mdns_tx_packet_t * packet, uint32_t ms_after)
  1084. {
  1085. if (!packet) {
  1086. return;
  1087. }
  1088. packet->send_at = (xTaskGetTickCount() * portTICK_PERIOD_MS) + ms_after;
  1089. packet->next = NULL;
  1090. if (!_mdns_server->tx_queue_head || _mdns_server->tx_queue_head->send_at > packet->send_at) {
  1091. packet->next = _mdns_server->tx_queue_head;
  1092. _mdns_server->tx_queue_head = packet;
  1093. return;
  1094. }
  1095. mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
  1096. while (q->next && q->next->send_at <= packet->send_at) {
  1097. q = q->next;
  1098. }
  1099. packet->next = q->next;
  1100. q->next = packet;
  1101. }
  1102. /**
  1103. * @brief free all packets scheduled for sending
  1104. */
  1105. static void _mdns_clear_tx_queue_head(void)
  1106. {
  1107. mdns_tx_packet_t * q;
  1108. while (_mdns_server->tx_queue_head) {
  1109. q = _mdns_server->tx_queue_head;
  1110. _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next;
  1111. _mdns_free_tx_packet(q);
  1112. }
  1113. }
  1114. /**
  1115. * @brief clear packets scheduled for sending on a specific interface
  1116. *
  1117. * @param tcpip_if the interface
  1118. * @param ip_protocol pcb type V4/V6
  1119. */
  1120. static void _mdns_clear_pcb_tx_queue_head(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1121. {
  1122. mdns_tx_packet_t * q, * p;
  1123. while (_mdns_server->tx_queue_head && _mdns_server->tx_queue_head->tcpip_if == tcpip_if && _mdns_server->tx_queue_head->ip_protocol == ip_protocol) {
  1124. q = _mdns_server->tx_queue_head;
  1125. _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next;
  1126. _mdns_free_tx_packet(q);
  1127. }
  1128. if (_mdns_server->tx_queue_head) {
  1129. q = _mdns_server->tx_queue_head;
  1130. while (q->next) {
  1131. if (q->next->tcpip_if == tcpip_if && q->next->ip_protocol == ip_protocol) {
  1132. p = q->next;
  1133. q->next = p->next;
  1134. _mdns_free_tx_packet(p);
  1135. } else {
  1136. q = q->next;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. /**
  1142. * @brief get the next packet scheduled for sending on a specific interface
  1143. *
  1144. * @param tcpip_if the interface
  1145. * @param ip_protocol pcb type V4/V6
  1146. */
  1147. static mdns_tx_packet_t * _mdns_get_next_pcb_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1148. {
  1149. mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
  1150. while (q) {
  1151. if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol) {
  1152. return q;
  1153. }
  1154. q = q->next;
  1155. }
  1156. return NULL;
  1157. }
  1158. /**
  1159. * @brief Find, remove and free answer from the scheduled packets
  1160. */
  1161. 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)
  1162. {
  1163. mdns_srv_item_t s = {NULL, NULL};
  1164. if (!service) {
  1165. service = &s;
  1166. }
  1167. mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
  1168. while (q) {
  1169. if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol && q->distributed) {
  1170. mdns_out_answer_t * a = q->answers;
  1171. if (a->type == type && a->service == service->service) {
  1172. q->answers = q->answers->next;
  1173. free(a);
  1174. } else {
  1175. while (a->next) {
  1176. if (a->next->type == type && a->next->service == service->service) {
  1177. mdns_out_answer_t * b = a->next;
  1178. a->next = b->next;
  1179. free(b);
  1180. break;
  1181. }
  1182. a = a->next;
  1183. }
  1184. }
  1185. }
  1186. q = q->next;
  1187. }
  1188. }
  1189. /**
  1190. * @brief Remove and free answer from answer list (destination)
  1191. */
  1192. static void _mdns_dealloc_answer(mdns_out_answer_t ** destination, uint16_t type, mdns_srv_item_t * service)
  1193. {
  1194. mdns_out_answer_t * d = *destination;
  1195. if (!d) {
  1196. return;
  1197. }
  1198. mdns_srv_item_t s = {NULL, NULL};
  1199. if (!service) {
  1200. service = &s;
  1201. }
  1202. if (d->type == type && d->service == service->service) {
  1203. *destination = d->next;
  1204. free(d);
  1205. return;
  1206. }
  1207. while (d->next) {
  1208. mdns_out_answer_t * a = d->next;
  1209. if (a->type == type && a->service == service->service) {
  1210. d->next = a->next;
  1211. free(a);
  1212. return;
  1213. }
  1214. d = d->next;
  1215. }
  1216. }
  1217. /**
  1218. * @brief Allocate new answer and add it to answer list (destination)
  1219. */
  1220. static bool _mdns_alloc_answer(mdns_out_answer_t ** destination, uint16_t type, mdns_service_t * service,
  1221. mdns_host_item_t * host, bool flush, bool bye)
  1222. {
  1223. mdns_out_answer_t * d = *destination;
  1224. while (d) {
  1225. if (d->type == type && d->service == service && d->host == host) {
  1226. return true;
  1227. }
  1228. d = d->next;
  1229. }
  1230. mdns_out_answer_t * a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
  1231. if (!a) {
  1232. HOOK_MALLOC_FAILED;
  1233. return false;
  1234. }
  1235. a->type = type;
  1236. a->service = service;
  1237. a->host = host;
  1238. a->custom_service = NULL;
  1239. a->bye = bye;
  1240. a->flush = flush;
  1241. a->next = NULL;
  1242. queueToEnd(mdns_out_answer_t, *destination, a);
  1243. return true;
  1244. }
  1245. /**
  1246. * @brief Allocate new packet for sending
  1247. */
  1248. static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1249. {
  1250. mdns_tx_packet_t * packet = (mdns_tx_packet_t*)malloc(sizeof(mdns_tx_packet_t));
  1251. if (!packet) {
  1252. HOOK_MALLOC_FAILED;
  1253. return NULL;
  1254. }
  1255. memset((uint8_t*)packet, 0, sizeof(mdns_tx_packet_t));
  1256. packet->tcpip_if = tcpip_if;
  1257. packet->ip_protocol = ip_protocol;
  1258. packet->port = MDNS_SERVICE_PORT;
  1259. if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
  1260. esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251);
  1261. memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
  1262. }
  1263. #if CONFIG_LWIP_IPV6
  1264. else {
  1265. esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000);
  1266. memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
  1267. }
  1268. #endif
  1269. return packet;
  1270. }
  1271. static bool _mdns_create_answer_from_service(mdns_tx_packet_t * packet, mdns_service_t * service,
  1272. mdns_parsed_question_t * question, bool shared, bool send_flush)
  1273. {
  1274. mdns_host_item_t * host = mdns_get_host_item(service->hostname);
  1275. if (question->type == MDNS_TYPE_PTR || question->type == MDNS_TYPE_ANY) {
  1276. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) ||
  1277. !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
  1278. !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false) ||
  1279. !_mdns_alloc_answer(shared ? &packet->additional : &packet->answers, MDNS_TYPE_A, service, host, send_flush,
  1280. false) ||
  1281. !_mdns_alloc_answer(shared ? &packet->additional : &packet->answers, MDNS_TYPE_AAAA, service, host,
  1282. send_flush, false)) {
  1283. return false;
  1284. }
  1285. } else if (question->type == MDNS_TYPE_SRV) {
  1286. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
  1287. !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_A, service, host, send_flush, false) ||
  1288. !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_AAAA, service, host, send_flush, false)) {
  1289. return false;
  1290. }
  1291. } else if (question->type == MDNS_TYPE_TXT) {
  1292. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false)) {
  1293. return false;
  1294. }
  1295. } else if (question->type == MDNS_TYPE_SDPTR) {
  1296. shared = true;
  1297. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, service, NULL, false, false)) {
  1298. return false;
  1299. }
  1300. }
  1301. return true;
  1302. }
  1303. static bool _mdns_create_answer_from_hostname(mdns_tx_packet_t * packet, const char * hostname, bool send_flush)
  1304. {
  1305. mdns_host_item_t * host = mdns_get_host_item(hostname);
  1306. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_A, NULL, host, send_flush, false) ||
  1307. !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, send_flush, false)) {
  1308. return false;
  1309. }
  1310. return true;
  1311. }
  1312. /**
  1313. * @brief Create answer packet to questions from parsed packet
  1314. */
  1315. static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_packet)
  1316. {
  1317. if (!parsed_packet->questions) {
  1318. return;
  1319. }
  1320. bool send_flush = parsed_packet->src_port == MDNS_SERVICE_PORT;
  1321. bool unicast = false;
  1322. bool shared = false;
  1323. mdns_tx_packet_t *packet = _mdns_alloc_packet_default(parsed_packet->tcpip_if, parsed_packet->ip_protocol);
  1324. if (!packet) {
  1325. return;
  1326. }
  1327. packet->flags = MDNS_FLAGS_AUTHORITATIVE;
  1328. packet->distributed = parsed_packet->distributed;
  1329. packet->id = parsed_packet->id;
  1330. mdns_parsed_question_t *q = parsed_packet->questions;
  1331. while (q) {
  1332. shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe;
  1333. if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) {
  1334. mdns_srv_item_t *service = _mdns_get_service_item_instance(q->host, q->service, q->proto, NULL);
  1335. if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
  1336. _mdns_free_tx_packet(packet);
  1337. return;
  1338. }
  1339. } else if (q->service && q->proto) {
  1340. mdns_srv_item_t *service = _mdns_server->services;
  1341. while (service) {
  1342. if (_mdns_service_match(service->service, q->service, q->proto, NULL)) {
  1343. if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
  1344. _mdns_free_tx_packet(packet);
  1345. return;
  1346. }
  1347. }
  1348. service = service->next;
  1349. }
  1350. } else if (q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA) {
  1351. if (!_mdns_create_answer_from_hostname(packet, q->host, send_flush)) {
  1352. _mdns_free_tx_packet(packet);
  1353. return;
  1354. }
  1355. } else if (q->type == MDNS_TYPE_ANY) {
  1356. if (!_mdns_append_host_list(&packet->answers, send_flush, false)) {
  1357. _mdns_free_tx_packet(packet);
  1358. return;
  1359. }
  1360. } else if (!_mdns_alloc_answer(&packet->answers, q->type, NULL, NULL, send_flush, false)) {
  1361. _mdns_free_tx_packet(packet);
  1362. return;
  1363. }
  1364. #ifdef MDNS_REPEAT_QUERY_IN_RESPONSE
  1365. if (parsed_packet->src_port != MDNS_SERVICE_PORT && // Repeat the queries only for "One-Shot mDNS queries"
  1366. (q->type == MDNS_TYPE_ANY || q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA)) {
  1367. mdns_out_question_t * out_question = malloc(sizeof(mdns_out_question_t));
  1368. if (out_question == NULL) {
  1369. HOOK_MALLOC_FAILED;
  1370. _mdns_free_tx_packet(packet);
  1371. return;
  1372. }
  1373. out_question->type = q->type;
  1374. out_question->unicast = q->unicast;
  1375. out_question->host = q->host;
  1376. q->host = NULL;
  1377. out_question->service = q->service;
  1378. q->service = NULL;
  1379. out_question->proto = q->proto;
  1380. q->proto = NULL;
  1381. out_question->domain = q->domain;
  1382. q->domain = NULL;
  1383. out_question->next = NULL;
  1384. out_question->own_dynamic_memory = true;
  1385. queueToEnd(mdns_out_question_t, packet->questions, out_question);
  1386. }
  1387. #endif // MDNS_REPEAT_QUERY_IN_RESPONSE
  1388. if (q->unicast) {
  1389. unicast = true;
  1390. }
  1391. q = q->next;
  1392. }
  1393. if (unicast || !send_flush) {
  1394. memcpy(&packet->dst, &parsed_packet->src, sizeof(esp_ip_addr_t));
  1395. packet->port = parsed_packet->src_port;
  1396. }
  1397. static uint8_t share_step = 0;
  1398. if (shared) {
  1399. _mdns_schedule_tx_packet(packet, 25 + (share_step * 25));
  1400. share_step = (share_step + 1) & 0x03;
  1401. } else {
  1402. _mdns_dispatch_tx_packet(packet);
  1403. _mdns_free_tx_packet(packet);
  1404. }
  1405. }
  1406. /**
  1407. * @brief Check if question is already in the list
  1408. */
  1409. static bool _mdns_question_exists(mdns_out_question_t * needle, mdns_out_question_t * haystack)
  1410. {
  1411. while (haystack) {
  1412. if (haystack->type == needle->type
  1413. && haystack->host == needle->host
  1414. && haystack->service == needle->service
  1415. && haystack->proto == needle->proto) {
  1416. return true;
  1417. }
  1418. haystack = haystack->next;
  1419. }
  1420. return false;
  1421. }
  1422. static bool _mdns_append_host(mdns_out_answer_t ** destination, mdns_host_item_t * host, bool flush, bool bye)
  1423. {
  1424. if (!_mdns_alloc_answer(destination, MDNS_TYPE_A, NULL, host, flush, bye)) {
  1425. return false;
  1426. }
  1427. if (!_mdns_alloc_answer(destination, MDNS_TYPE_AAAA, NULL, host, flush, bye)) {
  1428. return false;
  1429. }
  1430. return true;
  1431. }
  1432. static bool _mdns_append_host_list_in_services(mdns_out_answer_t ** destination, mdns_srv_item_t * services[],
  1433. size_t services_len, bool flush, bool bye)
  1434. {
  1435. if (services == NULL) {
  1436. mdns_host_item_t * host = mdns_get_host_item(_mdns_server->hostname);
  1437. if (host != NULL) {
  1438. return _mdns_append_host(destination, host, flush, bye);
  1439. }
  1440. return true;
  1441. }
  1442. for (size_t i = 0; i < services_len; i++) {
  1443. mdns_host_item_t *host = mdns_get_host_item(services[i]->service->hostname);
  1444. if (!_mdns_append_host(destination, host, flush, bye)) {
  1445. return false;
  1446. }
  1447. }
  1448. return true;
  1449. }
  1450. static bool _mdns_append_host_list(mdns_out_answer_t ** destination, bool flush, bool bye)
  1451. {
  1452. if (!_str_null_or_empty(_mdns_server->hostname)) {
  1453. mdns_host_item_t * self_host = mdns_get_host_item(_mdns_server->hostname);
  1454. if (!_mdns_append_host(destination, self_host, flush, bye)) {
  1455. return false;
  1456. }
  1457. }
  1458. mdns_host_item_t * host = _mdns_host_list;
  1459. while (host != NULL) {
  1460. host = host->next;
  1461. if (!_mdns_append_host(destination, host, flush, bye)) {
  1462. return false;
  1463. }
  1464. }
  1465. return true;
  1466. }
  1467. static bool _mdns_append_host_question(mdns_out_question_t **questions, const char *hostname, bool unicast)
  1468. {
  1469. mdns_out_question_t *q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
  1470. if (!q) {
  1471. HOOK_MALLOC_FAILED;
  1472. return false;
  1473. }
  1474. q->next = NULL;
  1475. q->unicast = unicast;
  1476. q->type = MDNS_TYPE_ANY;
  1477. q->host = hostname;
  1478. q->service = NULL;
  1479. q->proto = NULL;
  1480. q->domain = MDNS_DEFAULT_DOMAIN;
  1481. q->own_dynamic_memory = false;
  1482. if (_mdns_question_exists(q, *questions)) {
  1483. free(q);
  1484. } else {
  1485. queueToEnd(mdns_out_question_t, *questions, q);
  1486. }
  1487. return true;
  1488. }
  1489. static bool _mdns_append_host_questions_for_services(mdns_out_question_t **questions, mdns_srv_item_t *services[],
  1490. size_t len, bool unicast)
  1491. {
  1492. if (!_str_null_or_empty(_mdns_server->hostname) &&
  1493. !_mdns_append_host_question(questions, _mdns_server->hostname, unicast)) {
  1494. return false;
  1495. }
  1496. for (size_t i = 0; i < len; i++) {
  1497. if (!_mdns_append_host_question(questions, services[i]->service->hostname, unicast)) {
  1498. return false;
  1499. }
  1500. }
  1501. return true;
  1502. }
  1503. /**
  1504. * @brief Create probe packet for particular services on particular PCB
  1505. */
  1506. 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)
  1507. {
  1508. mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  1509. if (!packet) {
  1510. return NULL;
  1511. }
  1512. size_t i;
  1513. for (i=0; i<len; i++) {
  1514. mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
  1515. if (!q) {
  1516. HOOK_MALLOC_FAILED;
  1517. _mdns_free_tx_packet(packet);
  1518. return NULL;
  1519. }
  1520. q->next = NULL;
  1521. q->unicast = first;
  1522. q->type = MDNS_TYPE_ANY;
  1523. q->host = _mdns_get_service_instance_name(services[i]->service);
  1524. q->service = services[i]->service->service;
  1525. q->proto = services[i]->service->proto;
  1526. q->domain = MDNS_DEFAULT_DOMAIN;
  1527. q->own_dynamic_memory = false;
  1528. if (!q->host || _mdns_question_exists(q, packet->questions)) {
  1529. free(q);
  1530. continue;
  1531. } else {
  1532. queueToEnd(mdns_out_question_t, packet->questions, q);
  1533. }
  1534. if (!q->host || !_mdns_alloc_answer(&packet->servers, MDNS_TYPE_SRV, services[i]->service, NULL, false, false)) {
  1535. _mdns_free_tx_packet(packet);
  1536. return NULL;
  1537. }
  1538. }
  1539. if (include_ip) {
  1540. if (!_mdns_append_host_questions_for_services(&packet->questions, services, len, first)) {
  1541. _mdns_free_tx_packet(packet);
  1542. return NULL;
  1543. }
  1544. if (!_mdns_append_host_list_in_services(&packet->servers, services, len, false, false)) {
  1545. _mdns_free_tx_packet(packet);
  1546. return NULL;
  1547. }
  1548. }
  1549. return packet;
  1550. }
  1551. /**
  1552. * @brief Create announce packet for particular services on particular PCB
  1553. */
  1554. 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)
  1555. {
  1556. mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  1557. if (!packet) {
  1558. return NULL;
  1559. }
  1560. packet->flags = MDNS_FLAGS_AUTHORITATIVE;
  1561. uint8_t i;
  1562. for (i=0; i<len; i++) {
  1563. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false)
  1564. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false)
  1565. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false)
  1566. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) {
  1567. _mdns_free_tx_packet(packet);
  1568. return NULL;
  1569. }
  1570. }
  1571. if (include_ip) {
  1572. if (!_mdns_append_host_list_in_services(&packet->servers, services, len, true, false)) {
  1573. _mdns_free_tx_packet(packet);
  1574. return NULL;
  1575. }
  1576. }
  1577. return packet;
  1578. }
  1579. /**
  1580. * @brief Convert probe packet to announce
  1581. */
  1582. static mdns_tx_packet_t * _mdns_create_announce_from_probe(mdns_tx_packet_t * probe)
  1583. {
  1584. mdns_tx_packet_t * packet = _mdns_alloc_packet_default(probe->tcpip_if, probe->ip_protocol);
  1585. if (!packet) {
  1586. return NULL;
  1587. }
  1588. packet->flags = MDNS_FLAGS_AUTHORITATIVE;
  1589. mdns_out_answer_t * s = probe->servers;
  1590. while (s) {
  1591. if (s->type == MDNS_TYPE_SRV) {
  1592. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, s->service, NULL, false, false)
  1593. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, s->service, NULL, false, false)
  1594. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, s->service, NULL, true, false)
  1595. || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, s->service, NULL, true, false)) {
  1596. _mdns_free_tx_packet(packet);
  1597. return NULL;
  1598. }
  1599. } else if (s->type == MDNS_TYPE_A || s->type == MDNS_TYPE_AAAA) {
  1600. if (!_mdns_alloc_answer(&packet->answers, s->type, NULL, s->host, true, false)) {
  1601. _mdns_free_tx_packet(packet);
  1602. return NULL;
  1603. }
  1604. }
  1605. s = s->next;
  1606. }
  1607. return packet;
  1608. }
  1609. /**
  1610. * @brief Send by for particular services on particular PCB
  1611. */
  1612. 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)
  1613. {
  1614. mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  1615. if (!packet) {
  1616. return;
  1617. }
  1618. packet->flags = MDNS_FLAGS_AUTHORITATIVE;
  1619. size_t i;
  1620. for (i=0; i<len; i++) {
  1621. if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, true, true)) {
  1622. _mdns_free_tx_packet(packet);
  1623. return;
  1624. }
  1625. }
  1626. if (include_ip) {
  1627. _mdns_append_host_list_in_services(&packet->answers, services, len, true, true);
  1628. }
  1629. _mdns_dispatch_tx_packet(packet);
  1630. _mdns_free_tx_packet(packet);
  1631. }
  1632. /**
  1633. * @brief Send probe for additional services on particular PCB
  1634. */
  1635. 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)
  1636. {
  1637. mdns_pcb_t * pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
  1638. size_t services_final_len = len;
  1639. if (PCB_STATE_IS_PROBING(pcb)) {
  1640. services_final_len += pcb->probe_services_len;
  1641. }
  1642. mdns_srv_item_t ** _services = NULL;
  1643. if (services_final_len) {
  1644. _services = (mdns_srv_item_t **)malloc(sizeof(mdns_srv_item_t *) * services_final_len);
  1645. if (!_services) {
  1646. HOOK_MALLOC_FAILED;
  1647. return;
  1648. }
  1649. size_t i;
  1650. for (i=0; i<len; i++) {
  1651. _services[i] = services[i];
  1652. }
  1653. if (pcb->probe_services) {
  1654. for (i=0; i<pcb->probe_services_len; i++) {
  1655. _services[len+i] = pcb->probe_services[i];
  1656. }
  1657. free(pcb->probe_services);
  1658. }
  1659. }
  1660. probe_ip = pcb->probe_ip || probe_ip;
  1661. pcb->probe_ip = false;
  1662. pcb->probe_services = NULL;
  1663. pcb->probe_services_len = 0;
  1664. pcb->probe_running = false;
  1665. mdns_tx_packet_t * packet = _mdns_create_probe_packet(tcpip_if, ip_protocol, _services, services_final_len, true, probe_ip);
  1666. if (!packet) {
  1667. free(_services);
  1668. return;
  1669. }
  1670. pcb->probe_ip = probe_ip;
  1671. pcb->probe_services = _services;
  1672. pcb->probe_services_len = services_final_len;
  1673. pcb->probe_running = true;
  1674. _mdns_schedule_tx_packet(packet, ((pcb->failed_probes > 5)?1000:120) + (esp_random() & 0x7F));
  1675. pcb->state = PCB_PROBE_1;
  1676. }
  1677. /**
  1678. * @brief Send probe for particular services on particular PCB
  1679. *
  1680. * Tests possible duplication on probing service structure and probes only for new entries.
  1681. * - If pcb probing then add only non-probing services and restarts probing
  1682. * - If pcb not probing, run probing for all specified services
  1683. */
  1684. 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)
  1685. {
  1686. mdns_pcb_t * pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
  1687. _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
  1688. if (_str_null_or_empty(_mdns_server->hostname)) {
  1689. pcb->state = PCB_RUNNING;
  1690. return;
  1691. }
  1692. if (PCB_STATE_IS_PROBING(pcb)) {
  1693. // Looking for already probing services to resolve duplications
  1694. mdns_srv_item_t * new_probe_services[len];
  1695. int new_probe_service_len = 0;
  1696. bool found;
  1697. for (size_t j=0; j < len; ++j) {
  1698. found = false;
  1699. for (int i=0; i < pcb->probe_services_len; ++i) {
  1700. if (pcb->probe_services[i] == services[j]) {
  1701. found = true;
  1702. break;
  1703. }
  1704. }
  1705. if (!found) {
  1706. new_probe_services[new_probe_service_len++] = services[j];
  1707. }
  1708. }
  1709. // init probing for newly added services
  1710. _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol,
  1711. new_probe_service_len?new_probe_services:NULL, new_probe_service_len, probe_ip);
  1712. } else {
  1713. // not probing, so init for all services
  1714. _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol, services, len, probe_ip);
  1715. }
  1716. }
  1717. /**
  1718. * @brief Restart the responder on particular PCB
  1719. */
  1720. static void _mdns_restart_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  1721. {
  1722. size_t srv_count = 0;
  1723. mdns_srv_item_t * a = _mdns_server->services;
  1724. while (a) {
  1725. srv_count++;
  1726. a = a->next;
  1727. }
  1728. mdns_srv_item_t * services[srv_count];
  1729. size_t i = 0;
  1730. a = _mdns_server->services;
  1731. while (a) {
  1732. services[i++] = a;
  1733. a = a->next;
  1734. }
  1735. _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, srv_count, true);
  1736. }
  1737. /**
  1738. * @brief Send by for particular services
  1739. */
  1740. static void _mdns_send_bye(mdns_srv_item_t ** services, size_t len, bool include_ip)
  1741. {
  1742. uint8_t i, j;
  1743. if (_str_null_or_empty(_mdns_server->hostname)) {
  1744. return;
  1745. }
  1746. for (i=0; i<MDNS_IF_MAX; i++) {
  1747. for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
  1748. if (_mdns_server->interfaces[i].pcbs[j].pcb && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) {
  1749. _mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
  1750. }
  1751. }
  1752. }
  1753. }
  1754. /**
  1755. * @brief Send announcement on particular PCB
  1756. */
  1757. 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)
  1758. {
  1759. mdns_pcb_t * _pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
  1760. size_t i;
  1761. if (_pcb->pcb) {
  1762. if (PCB_STATE_IS_PROBING(_pcb)) {
  1763. _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, len, include_ip);
  1764. } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
  1765. mdns_tx_packet_t * p = _mdns_get_next_pcb_packet(tcpip_if, ip_protocol);
  1766. if (p) {
  1767. for (i=0; i<len; i++) {
  1768. if (!_mdns_alloc_answer(&p->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false)
  1769. || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false)
  1770. || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false)
  1771. || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) {
  1772. break;
  1773. }
  1774. }
  1775. if (include_ip) {
  1776. _mdns_dealloc_answer(&p->additional, MDNS_TYPE_A, NULL);
  1777. _mdns_dealloc_answer(&p->additional, MDNS_TYPE_AAAA, NULL);
  1778. _mdns_append_host_list_in_services(&p->answers, services, len, true, false);
  1779. }
  1780. _pcb->state = PCB_ANNOUNCE_1;
  1781. }
  1782. } else if (_pcb->state == PCB_RUNNING) {
  1783. if (_str_null_or_empty(_mdns_server->hostname)) {
  1784. return;
  1785. }
  1786. _pcb->state = PCB_ANNOUNCE_1;
  1787. mdns_tx_packet_t * p = _mdns_create_announce_packet(tcpip_if, ip_protocol, services, len, include_ip);
  1788. if (p) {
  1789. _mdns_schedule_tx_packet(p, 0);
  1790. }
  1791. }
  1792. }
  1793. }
  1794. /**
  1795. * @brief Send probe on all active PCBs
  1796. */
  1797. static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool probe_ip, bool clear_old_probe)
  1798. {
  1799. uint8_t i, j;
  1800. for (i=0; i<MDNS_IF_MAX; i++) {
  1801. for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
  1802. if (_mdns_server->interfaces[i].pcbs[j].pcb) {
  1803. mdns_pcb_t * _pcb = &_mdns_server->interfaces[i].pcbs[j];
  1804. if (clear_old_probe) {
  1805. free(_pcb->probe_services);
  1806. _pcb->probe_services = NULL;
  1807. _pcb->probe_services_len = 0;
  1808. _pcb->probe_running = false;
  1809. }
  1810. _mdns_init_pcb_probe((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, probe_ip);
  1811. }
  1812. }
  1813. }
  1814. }
  1815. /**
  1816. * @brief Send announcement on all active PCBs
  1817. */
  1818. static void _mdns_announce_all_pcbs(mdns_srv_item_t ** services, size_t len, bool include_ip)
  1819. {
  1820. uint8_t i, j;
  1821. for (i=0; i<MDNS_IF_MAX; i++) {
  1822. for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
  1823. _mdns_announce_pcb((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
  1824. }
  1825. }
  1826. }
  1827. /**
  1828. * @brief Restart the responder on all active PCBs
  1829. */
  1830. static void _mdns_send_final_bye(bool include_ip)
  1831. {
  1832. //collect all services and start probe
  1833. size_t srv_count = 0;
  1834. mdns_srv_item_t * a = _mdns_server->services;
  1835. while (a) {
  1836. srv_count++;
  1837. a = a->next;
  1838. }
  1839. if (!srv_count) {
  1840. return;
  1841. }
  1842. mdns_srv_item_t * services[srv_count];
  1843. size_t i = 0;
  1844. a = _mdns_server->services;
  1845. while (a) {
  1846. services[i++] = a;
  1847. a = a->next;
  1848. }
  1849. _mdns_send_bye(services, srv_count, include_ip);
  1850. }
  1851. /**
  1852. * @brief Stop the responder on all services without instance
  1853. */
  1854. static void _mdns_send_bye_all_pcbs_no_instance(bool include_ip)
  1855. {
  1856. size_t srv_count = 0;
  1857. mdns_srv_item_t * a = _mdns_server->services;
  1858. while (a) {
  1859. if (!a->service->instance) {
  1860. srv_count++;
  1861. }
  1862. a = a->next;
  1863. }
  1864. if (!srv_count) {
  1865. return;
  1866. }
  1867. mdns_srv_item_t * services[srv_count];
  1868. size_t i = 0;
  1869. a = _mdns_server->services;
  1870. while (a) {
  1871. if (!a->service->instance) {
  1872. services[i++] = a;
  1873. }
  1874. a = a->next;
  1875. }
  1876. _mdns_send_bye(services, srv_count, include_ip);
  1877. }
  1878. /**
  1879. * @brief Restart the responder on all services without instance
  1880. */
  1881. static void _mdns_restart_all_pcbs_no_instance(void)
  1882. {
  1883. size_t srv_count = 0;
  1884. mdns_srv_item_t * a = _mdns_server->services;
  1885. while (a) {
  1886. if (!a->service->instance) {
  1887. srv_count++;
  1888. }
  1889. a = a->next;
  1890. }
  1891. if (!srv_count) {
  1892. return;
  1893. }
  1894. mdns_srv_item_t * services[srv_count];
  1895. size_t i = 0;
  1896. a = _mdns_server->services;
  1897. while (a) {
  1898. if (!a->service->instance) {
  1899. services[i++] = a;
  1900. }
  1901. a = a->next;
  1902. }
  1903. _mdns_probe_all_pcbs(services, srv_count, false, true);
  1904. }
  1905. /**
  1906. * @brief Restart the responder on all active PCBs
  1907. */
  1908. static void _mdns_restart_all_pcbs(void)
  1909. {
  1910. _mdns_clear_tx_queue_head();
  1911. size_t srv_count = 0;
  1912. mdns_srv_item_t * a = _mdns_server->services;
  1913. while (a) {
  1914. srv_count++;
  1915. a = a->next;
  1916. }
  1917. mdns_srv_item_t * services[srv_count];
  1918. size_t l = 0;
  1919. a = _mdns_server->services;
  1920. while (a) {
  1921. services[l++] = a;
  1922. a = a->next;
  1923. }
  1924. _mdns_probe_all_pcbs(services, srv_count, true, true);
  1925. }
  1926. /**
  1927. * @brief creates/allocates new text item list
  1928. * @param num_items service number of txt items or 0
  1929. * @param txt service txt items array or NULL
  1930. *
  1931. * @return pointer to the linked txt item list or NULL
  1932. */
  1933. static mdns_txt_linked_item_t * _mdns_allocate_txt(size_t num_items, mdns_txt_item_t txt[])
  1934. {
  1935. mdns_txt_linked_item_t * new_txt = NULL;
  1936. size_t i = 0;
  1937. if (num_items) {
  1938. for (i=0; i<num_items; i++) {
  1939. mdns_txt_linked_item_t * new_item = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
  1940. if (!new_item) {
  1941. HOOK_MALLOC_FAILED;
  1942. break;
  1943. }
  1944. new_item->key = strdup(txt[i].key);
  1945. if (!new_item->key) {
  1946. free(new_item);
  1947. break;
  1948. }
  1949. new_item->value = strdup(txt[i].value);
  1950. if (!new_item->value) {
  1951. free((char *)new_item->key);
  1952. free(new_item);
  1953. break;
  1954. }
  1955. new_item->value_len = strlen(new_item->value);
  1956. new_item->next = new_txt;
  1957. new_txt = new_item;
  1958. }
  1959. }
  1960. return new_txt;
  1961. }
  1962. static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt)
  1963. {
  1964. mdns_txt_linked_item_t *t;
  1965. while (txt) {
  1966. t = txt;
  1967. txt = txt->next;
  1968. free((char *)t->value);
  1969. free((char *)t->key);
  1970. free(t);
  1971. }
  1972. }
  1973. /**
  1974. * @brief creates/allocates new service
  1975. * @param service service type
  1976. * @param proto service proto
  1977. * @param port service port
  1978. * @param instance service instance
  1979. * @param num_items service number of txt items or 0
  1980. * @param txt service txt items array or NULL
  1981. *
  1982. * @return pointer to the service or NULL on error
  1983. */
  1984. static mdns_service_t * _mdns_create_service(const char * service, const char * proto, const char * hostname,
  1985. uint16_t port, const char * instance, size_t num_items,
  1986. mdns_txt_item_t txt[])
  1987. {
  1988. mdns_service_t * s = (mdns_service_t *)malloc(sizeof(mdns_service_t));
  1989. if (!s) {
  1990. HOOK_MALLOC_FAILED;
  1991. return NULL;
  1992. }
  1993. mdns_txt_linked_item_t * new_txt = _mdns_allocate_txt(num_items, txt);
  1994. if (num_items && new_txt == NULL) {
  1995. free(s);
  1996. return NULL;
  1997. }
  1998. s->priority = 0;
  1999. s->weight = 0;
  2000. s->instance = instance?strndup(instance, MDNS_NAME_BUF_LEN - 1):NULL;
  2001. s->txt = new_txt;
  2002. s->port = port;
  2003. if (hostname) {
  2004. s->hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  2005. if (!s->hostname) {
  2006. free(s);
  2007. return NULL;
  2008. }
  2009. } else {
  2010. s->hostname = NULL;
  2011. }
  2012. s->service = strndup(service, MDNS_NAME_BUF_LEN - 1);
  2013. if (!s->service) {
  2014. free(s);
  2015. return NULL;
  2016. }
  2017. s->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
  2018. if (!s->proto) {
  2019. free((char *)s->service);
  2020. free(s);
  2021. return NULL;
  2022. }
  2023. return s;
  2024. }
  2025. /**
  2026. * @brief Remove and free service answer from answer list (destination)
  2027. */
  2028. static void _mdns_dealloc_scheduled_service_answers(mdns_out_answer_t ** destination, mdns_service_t * service)
  2029. {
  2030. mdns_out_answer_t * d = *destination;
  2031. if (!d) {
  2032. return;
  2033. }
  2034. while (d && d->service == service) {
  2035. *destination = d->next;
  2036. free(d);
  2037. d = *destination;
  2038. }
  2039. while (d && d->next) {
  2040. mdns_out_answer_t * a = d->next;
  2041. if (a->service == service) {
  2042. d->next = a->next;
  2043. free(a);
  2044. } else {
  2045. d = d->next;
  2046. }
  2047. }
  2048. }
  2049. /**
  2050. * @brief Find, remove and free answers and scheduled packets for service
  2051. */
  2052. static void _mdns_remove_scheduled_service_packets(mdns_service_t * service)
  2053. {
  2054. if (!service) {
  2055. return;
  2056. }
  2057. mdns_tx_packet_t * p = NULL;
  2058. mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
  2059. while (q) {
  2060. bool had_answers = (q->answers != NULL);
  2061. _mdns_dealloc_scheduled_service_answers(&(q->answers), service);
  2062. _mdns_dealloc_scheduled_service_answers(&(q->additional), service);
  2063. _mdns_dealloc_scheduled_service_answers(&(q->servers), service);
  2064. mdns_pcb_t * _pcb = &_mdns_server->interfaces[q->tcpip_if].pcbs[q->ip_protocol];
  2065. if(_pcb->pcb) {
  2066. if (PCB_STATE_IS_PROBING(_pcb)) {
  2067. uint8_t i;
  2068. //check if we are probing this service
  2069. for (i=0; i<_pcb->probe_services_len; i++) {
  2070. mdns_srv_item_t * s = _pcb->probe_services[i];
  2071. if (s->service == service){
  2072. break;
  2073. }
  2074. }
  2075. if (i < _pcb->probe_services_len) {
  2076. if (_pcb->probe_services_len > 1) {
  2077. uint8_t n;
  2078. for (n=(i+1); n<_pcb->probe_services_len; n++) {
  2079. _pcb->probe_services[n-1] = _pcb->probe_services[n];
  2080. }
  2081. _pcb->probe_services_len--;
  2082. } else {
  2083. _pcb->probe_services_len = 0;
  2084. free(_pcb->probe_services);
  2085. _pcb->probe_services = NULL;
  2086. if (!_pcb->probe_ip) {
  2087. _pcb->probe_running = false;
  2088. _pcb->state = PCB_RUNNING;
  2089. }
  2090. }
  2091. if (q->questions) {
  2092. mdns_out_question_t * qsn = NULL;
  2093. mdns_out_question_t * qs = q->questions;
  2094. if (qs->type == MDNS_TYPE_ANY
  2095. && qs->service && strcmp(qs->service, service->service) == 0
  2096. && qs->proto && strcmp(qs->proto, service->proto) == 0)
  2097. {
  2098. q->questions = q->questions->next;
  2099. free(qs);
  2100. } else while (qs->next) {
  2101. qsn = qs->next;
  2102. if (qsn->type == MDNS_TYPE_ANY
  2103. && qsn->service && strcmp(qsn->service, service->service) == 0
  2104. && qsn->proto && strcmp(qsn->proto, service->proto) == 0)
  2105. {
  2106. qs->next = qsn->next;
  2107. free(qsn);
  2108. break;
  2109. }
  2110. qs = qs->next;
  2111. }
  2112. }
  2113. }
  2114. } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
  2115. //if answers were cleared, set to running
  2116. if (had_answers && q->answers == NULL) {
  2117. _pcb->state = PCB_RUNNING;
  2118. }
  2119. }
  2120. }
  2121. p = q;
  2122. q = q->next;
  2123. if(!p->questions && !p->answers && !p->additional && !p->servers){
  2124. queueDetach(mdns_tx_packet_t, _mdns_server->tx_queue_head, p);
  2125. _mdns_free_tx_packet(p);
  2126. }
  2127. }
  2128. }
  2129. /**
  2130. * @brief free service memory
  2131. *
  2132. * @param service the service
  2133. */
  2134. static void _mdns_free_service(mdns_service_t * service)
  2135. {
  2136. if (!service) {
  2137. return;
  2138. }
  2139. free((char *)service->instance);
  2140. free((char *)service->service);
  2141. free((char *)service->proto);
  2142. free((char *)service->hostname);
  2143. while (service->txt) {
  2144. mdns_txt_linked_item_t * s = service->txt;
  2145. service->txt = service->txt->next;
  2146. free((char *)s->key);
  2147. free((char *)s->value);
  2148. free(s);
  2149. }
  2150. free(service->txt);
  2151. free(service);
  2152. }
  2153. /*
  2154. * Received Packet Handling
  2155. * */
  2156. /**
  2157. * @brief Detect SRV collision
  2158. */
  2159. 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)
  2160. {
  2161. if (_str_null_or_empty(_mdns_server->hostname)) {
  2162. return 0;
  2163. }
  2164. size_t our_host_len = strlen(_mdns_server->hostname);
  2165. size_t our_len = 14 + our_host_len;
  2166. size_t their_host_len = strlen(host);
  2167. size_t their_domain_len = strlen(domain);
  2168. size_t their_len = 9 + their_host_len + their_domain_len;
  2169. if (their_len > our_len) {
  2170. return 1;//they win
  2171. } else if (their_len < our_len) {
  2172. return -1;//we win
  2173. }
  2174. uint16_t our_index = 0;
  2175. uint8_t our_data[our_len];
  2176. _mdns_append_u16(our_data, &our_index, service->priority);
  2177. _mdns_append_u16(our_data, &our_index, service->weight);
  2178. _mdns_append_u16(our_data, &our_index, service->port);
  2179. our_data[our_index++] = our_host_len;
  2180. memcpy(our_data + our_index, _mdns_server->hostname, our_host_len);
  2181. our_index += our_host_len;
  2182. our_data[our_index++] = 5;
  2183. memcpy(our_data + our_index, MDNS_DEFAULT_DOMAIN, 5);
  2184. our_index += 5;
  2185. our_data[our_index++] = 0;
  2186. uint16_t their_index = 0;
  2187. uint8_t their_data[their_len];
  2188. _mdns_append_u16(their_data, &their_index, priority);
  2189. _mdns_append_u16(their_data, &their_index, weight);
  2190. _mdns_append_u16(their_data, &their_index, port);
  2191. their_data[their_index++] = their_host_len;
  2192. memcpy(their_data + their_index, host, their_host_len);
  2193. their_index += their_host_len;
  2194. their_data[their_index++] = their_domain_len;
  2195. memcpy(their_data + their_index, domain, their_domain_len);
  2196. their_index += their_domain_len;
  2197. their_data[their_index++] = 0;
  2198. int ret = memcmp(our_data, their_data, our_len);
  2199. if (ret > 0) {
  2200. return -1;//we win
  2201. } else if (ret < 0) {
  2202. return 1;//they win
  2203. }
  2204. return 0;//same
  2205. }
  2206. /**
  2207. * @brief Detect TXT collision
  2208. */
  2209. static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * data, size_t len)
  2210. {
  2211. size_t data_len = 0;
  2212. if (len == 1 && service->txt) {
  2213. return -1;//we win
  2214. } else if (len > 1 && !service->txt) {
  2215. return 1;//they win
  2216. } else if (len == 1 && !service->txt) {
  2217. return 0;//same
  2218. }
  2219. mdns_txt_linked_item_t * txt = service->txt;
  2220. while (txt) {
  2221. data_len += 2 + strlen(txt->key) + strlen(txt->value);
  2222. txt = txt->next;
  2223. }
  2224. if (len > data_len) {
  2225. return 1;//they win
  2226. } else if (len < data_len) {
  2227. return -1;//we win
  2228. }
  2229. uint8_t ours[len];
  2230. uint16_t index = 0;
  2231. char * tmp;
  2232. txt = service->txt;
  2233. while (txt) {
  2234. tmp = (char *)malloc(2 + strlen(txt->key) + strlen(txt->value));
  2235. if (tmp) {
  2236. sprintf(tmp, "%s=%s", txt->key, txt->value);
  2237. _mdns_append_string(ours, &index, tmp);
  2238. free(tmp);
  2239. } else {
  2240. HOOK_MALLOC_FAILED;
  2241. // continue
  2242. }
  2243. txt = txt->next;
  2244. }
  2245. int ret = memcmp(ours, data, len);
  2246. if (ret > 0) {
  2247. return -1;//we win
  2248. } else if (ret < 0) {
  2249. return 1;//they win
  2250. }
  2251. return 0;//same
  2252. }
  2253. /**
  2254. * @brief Set interface as duplicate if another is found on the same subnet
  2255. */
  2256. static void _mdns_dup_interface(mdns_if_t tcpip_if)
  2257. {
  2258. uint8_t i;
  2259. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  2260. if (other_if == MDNS_IF_MAX) {
  2261. return; // no other interface found
  2262. }
  2263. for (i=0; i<MDNS_IP_PROTOCOL_MAX; i++) {
  2264. if (_mdns_server->interfaces[other_if].pcbs[i].pcb) {
  2265. //stop this interface and mark as dup
  2266. if (_mdns_server->interfaces[tcpip_if].pcbs[i].pcb) {
  2267. _mdns_clear_pcb_tx_queue_head(tcpip_if, i);
  2268. _mdns_pcb_deinit(tcpip_if, i);
  2269. }
  2270. _mdns_server->interfaces[tcpip_if].pcbs[i].state = PCB_DUP;
  2271. _mdns_announce_pcb(other_if, i, NULL, 0, true);
  2272. }
  2273. }
  2274. }
  2275. /**
  2276. * @brief Detect IPv4 address collision
  2277. */
  2278. static int _mdns_check_a_collision(esp_ip4_addr_t * ip, mdns_if_t tcpip_if)
  2279. {
  2280. esp_netif_ip_info_t if_ip_info;
  2281. esp_netif_ip_info_t other_ip_info;
  2282. if (!ip->addr) {
  2283. return 1;//denial! they win
  2284. }
  2285. if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
  2286. return 1;//they win
  2287. }
  2288. int ret = memcmp((uint8_t*)&if_ip_info.ip.addr, (uint8_t*)&ip->addr, sizeof(esp_ip4_addr_t));
  2289. if (ret > 0) {
  2290. return -1;//we win
  2291. } else if (ret < 0) {
  2292. //is it the other interface?
  2293. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  2294. if (other_if == MDNS_IF_MAX) {
  2295. return 1;//AP interface! They win
  2296. }
  2297. if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &other_ip_info)) {
  2298. return 1;//IPv4 not active! They win
  2299. }
  2300. if (ip->addr != other_ip_info.ip.addr) {
  2301. return 1;//IPv4 not ours! They win
  2302. }
  2303. _mdns_dup_interface(tcpip_if);
  2304. return 2;//they win
  2305. }
  2306. return 0;//same
  2307. }
  2308. #if CONFIG_LWIP_IPV6
  2309. /**
  2310. * @brief Detect IPv6 address collision
  2311. */
  2312. static int _mdns_check_aaaa_collision(esp_ip6_addr_t * ip, mdns_if_t tcpip_if)
  2313. {
  2314. struct esp_ip6_addr if_ip6;
  2315. struct esp_ip6_addr other_ip6;
  2316. if (_ipv6_address_is_zero(*ip)) {
  2317. return 1;//denial! they win
  2318. }
  2319. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) {
  2320. return 1;//they win
  2321. }
  2322. int ret = memcmp((uint8_t*)&if_ip6.addr, (uint8_t*)ip->addr, _MDNS_SIZEOF_IP6_ADDR);
  2323. if (ret > 0) {
  2324. return -1;//we win
  2325. } else if (ret < 0) {
  2326. //is it the other interface?
  2327. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  2328. if (other_if == MDNS_IF_MAX) {
  2329. return 1;//AP interface! They win
  2330. }
  2331. if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) {
  2332. return 1;//IPv6 not active! They win
  2333. }
  2334. if (memcmp((uint8_t*)&other_ip6.addr, (uint8_t*)ip->addr, _MDNS_SIZEOF_IP6_ADDR)) {
  2335. return 1;//IPv6 not ours! They win
  2336. }
  2337. _mdns_dup_interface(tcpip_if);
  2338. return 2;//they win
  2339. }
  2340. return 0;//same
  2341. }
  2342. #endif
  2343. static bool _hostname_is_ours(const char * hostname)
  2344. {
  2345. if (strcasecmp(hostname, _mdns_server->hostname) == 0) {
  2346. return true;
  2347. }
  2348. mdns_host_item_t * host = _mdns_host_list;
  2349. while (host != NULL) {
  2350. if (strcasecmp(hostname, host->hostname) == 0) {
  2351. return true;
  2352. }
  2353. host = host->next;
  2354. }
  2355. return false;
  2356. }
  2357. static bool _mdns_delegate_hostname_add(const char * hostname, mdns_ip_addr_t * address_list)
  2358. {
  2359. if (_hostname_is_ours(hostname)) {
  2360. return true;
  2361. }
  2362. mdns_host_item_t * host = (mdns_host_item_t *)malloc(sizeof(mdns_host_item_t));
  2363. if (host == NULL) {
  2364. return false;
  2365. }
  2366. host->address_list = address_list;
  2367. host->hostname = hostname;
  2368. host->next = _mdns_host_list;
  2369. _mdns_host_list = host;
  2370. return true;
  2371. }
  2372. static void free_address_list(mdns_ip_addr_t * address_list)
  2373. {
  2374. while (address_list != NULL) {
  2375. mdns_ip_addr_t * next = address_list->next;
  2376. free(address_list);
  2377. address_list = next;
  2378. }
  2379. }
  2380. static mdns_ip_addr_t * copy_address_list(const mdns_ip_addr_t * address_list)
  2381. {
  2382. mdns_ip_addr_t * head = NULL;
  2383. mdns_ip_addr_t * tail = NULL;
  2384. while (address_list != NULL) {
  2385. mdns_ip_addr_t * addr = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
  2386. if (addr == NULL) {
  2387. free_address_list(head);
  2388. return NULL;
  2389. }
  2390. addr->addr = address_list->addr;
  2391. addr->next = NULL;
  2392. if (head == NULL) {
  2393. head = addr;
  2394. tail = addr;
  2395. } else {
  2396. tail->next = addr;
  2397. tail = tail->next;
  2398. }
  2399. address_list = address_list->next;
  2400. }
  2401. return head;
  2402. }
  2403. static bool _mdns_delegate_hostname_remove(const char * hostname)
  2404. {
  2405. mdns_srv_item_t * srv = _mdns_server->services;
  2406. mdns_srv_item_t * prev_srv = NULL;
  2407. while (srv) {
  2408. if (strcasecmp(srv->service->hostname, hostname) == 0) {
  2409. mdns_srv_item_t * to_free = srv;
  2410. _mdns_send_bye(&srv, 1, false);
  2411. _mdns_remove_scheduled_service_packets(srv->service);
  2412. if (prev_srv == NULL) {
  2413. _mdns_server->services = srv->next;
  2414. srv = srv->next;
  2415. } else {
  2416. prev_srv->next = srv->next;
  2417. srv = srv->next;
  2418. }
  2419. _mdns_free_service(to_free->service);
  2420. free(to_free);
  2421. } else {
  2422. prev_srv = srv;
  2423. srv = srv->next;
  2424. }
  2425. }
  2426. mdns_host_item_t * host = _mdns_host_list;
  2427. mdns_host_item_t * prev_host = NULL;
  2428. while (host != NULL) {
  2429. if (strcasecmp(hostname, host->hostname) == 0) {
  2430. if (prev_host == NULL) {
  2431. _mdns_host_list = host->next;
  2432. } else {
  2433. prev_host->next = host->next;
  2434. }
  2435. free_address_list(host->address_list);
  2436. free((char *)host->hostname);
  2437. free(host);
  2438. break;
  2439. } else {
  2440. prev_host = host;
  2441. host = host->next;
  2442. }
  2443. }
  2444. return true;
  2445. }
  2446. /**
  2447. * @brief Check if parsed name is discovery
  2448. */
  2449. static bool _mdns_name_is_discovery(mdns_name_t * name, uint16_t type)
  2450. {
  2451. return (
  2452. (name->host && name->host[0] && !strcasecmp(name->host, "_services"))
  2453. && (name->service && name->service[0] && !strcasecmp(name->service, "_dns-sd"))
  2454. && (name->proto && name->proto[0] && !strcasecmp(name->proto, "_udp"))
  2455. && (name->domain && name->domain[0] && !strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN))
  2456. && type == MDNS_TYPE_PTR
  2457. );
  2458. }
  2459. /**
  2460. * @brief Check if the parsed name is ours (matches service or host name)
  2461. */
  2462. static bool _mdns_name_is_ours(mdns_name_t * name)
  2463. {
  2464. //domain have to be "local"
  2465. if (_str_null_or_empty(name->domain) || strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN)) {
  2466. return false;
  2467. }
  2468. //if service and proto are empty, host must match out hostname
  2469. if (_str_null_or_empty(name->service) && _str_null_or_empty(name->proto)) {
  2470. if (!_str_null_or_empty(name->host)
  2471. && !_str_null_or_empty(_mdns_server->hostname)
  2472. && _hostname_is_ours(name->host))
  2473. {
  2474. return true;
  2475. }
  2476. return false;
  2477. }
  2478. //if service or proto is empty, name is invalid
  2479. if (_str_null_or_empty(name->service) || _str_null_or_empty(name->proto)) {
  2480. return false;
  2481. }
  2482. //find the service
  2483. mdns_srv_item_t * service;
  2484. if (_str_null_or_empty(name->host)) {
  2485. service = _mdns_get_service_item(name->service, name->proto, NULL);
  2486. } else {
  2487. service = _mdns_get_service_item_instance(name->host, name->service, name->proto, NULL);
  2488. }
  2489. if (!service) {
  2490. return false;
  2491. }
  2492. //if host is empty and we have service, we have success
  2493. if (_str_null_or_empty(name->host)) {
  2494. return true;
  2495. }
  2496. //OK we have host in the name. find what is the instance of the service
  2497. const char * instance = _mdns_get_service_instance_name(service->service);
  2498. if (instance == NULL) {
  2499. return false;
  2500. }
  2501. //compare the instance against the name
  2502. if (strcasecmp(name->host, instance) == 0) {
  2503. return true;
  2504. }
  2505. return false;
  2506. }
  2507. /**
  2508. * @brief read uint16_t from a packet
  2509. * @param packet the packet
  2510. * @param index index in the packet where the value starts
  2511. *
  2512. * @return the value
  2513. */
  2514. static inline uint16_t _mdns_read_u16(const uint8_t * packet, uint16_t index)
  2515. {
  2516. return (uint16_t)(packet[index]) << 8 | packet[index+1];
  2517. }
  2518. /**
  2519. * @brief read uint32_t from a packet
  2520. * @param packet the packet
  2521. * @param index index in the packet where the value starts
  2522. *
  2523. * @return the value
  2524. */
  2525. static inline uint32_t _mdns_read_u32(const uint8_t * packet, uint16_t index)
  2526. {
  2527. return (uint32_t)(packet[index]) << 24 | (uint32_t)(packet[index+1]) << 16 | (uint32_t)(packet[index+2]) << 8 | packet[index+3];
  2528. }
  2529. /**
  2530. * @brief reads and formats MDNS FQDN into mdns_name_t structure
  2531. *
  2532. * @param packet MDNS packet
  2533. * @param start Starting point of FQDN
  2534. * @param name mdns_name_t structure to populate
  2535. *
  2536. * @return the address after the parsed FQDN in the packet or NULL on error
  2537. */
  2538. static const uint8_t * _mdns_parse_fqdn(const uint8_t * packet, const uint8_t * start, mdns_name_t * name)
  2539. {
  2540. name->parts = 0;
  2541. name->sub = 0;
  2542. name->host[0] = 0;
  2543. name->service[0] = 0;
  2544. name->proto[0] = 0;
  2545. name->domain[0] = 0;
  2546. name->invalid = false;
  2547. static char buf[MDNS_NAME_BUF_LEN];
  2548. const uint8_t * next_data = (uint8_t*)_mdns_read_fqdn(packet, start, name, buf);
  2549. if (!next_data) {
  2550. return 0;
  2551. }
  2552. if (!name->parts || name->invalid) {
  2553. return next_data;
  2554. }
  2555. if (name->parts == 3) {
  2556. memmove((uint8_t*)name + (MDNS_NAME_BUF_LEN), (uint8_t*)name, 3*(MDNS_NAME_BUF_LEN));
  2557. name->host[0] = 0;
  2558. } else if (name->parts == 2) {
  2559. memmove((uint8_t*)(name->domain), (uint8_t*)(name->service), (MDNS_NAME_BUF_LEN));
  2560. name->service[0] = 0;
  2561. name->proto[0] = 0;
  2562. }
  2563. if (strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN) == 0 || strcasecmp(name->domain, "arpa") == 0) {
  2564. return next_data;
  2565. }
  2566. name->invalid = true; // mark the current name invalid, but continue with other question
  2567. return next_data;
  2568. }
  2569. /**
  2570. * @brief Called from parser to check if question matches particular service
  2571. */
  2572. static bool _mdns_question_matches(mdns_parsed_question_t * question, uint16_t type, mdns_srv_item_t * service)
  2573. {
  2574. if (question->type != type) {
  2575. return false;
  2576. }
  2577. if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
  2578. return true;
  2579. } else if (type == MDNS_TYPE_PTR || type == MDNS_TYPE_SDPTR) {
  2580. if (!strcasecmp(service->service->service, question->service)
  2581. && !strcasecmp(service->service->proto, question->proto)
  2582. && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) {
  2583. return true;
  2584. }
  2585. } else if (service && (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT)) {
  2586. const char * name = _mdns_get_service_instance_name(service->service);
  2587. if (name && question->host && question->service && question->proto && question->domain
  2588. && !strcasecmp(name, question->host)
  2589. && !strcasecmp(service->service->service, question->service)
  2590. && !strcasecmp(service->service->proto, question->proto)
  2591. && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) {
  2592. return true;
  2593. }
  2594. }
  2595. return false;
  2596. }
  2597. /**
  2598. * @brief Removes saved question from parsed data
  2599. */
  2600. static void _mdns_remove_parsed_question(mdns_parsed_packet_t * parsed_packet, uint16_t type, mdns_srv_item_t * service)
  2601. {
  2602. mdns_parsed_question_t * q = parsed_packet->questions;
  2603. if (_mdns_question_matches(q, type, service)) {
  2604. parsed_packet->questions = q->next;
  2605. free(q->host);
  2606. free(q->service);
  2607. free(q->proto);
  2608. free(q->domain);
  2609. free(q);
  2610. return;
  2611. }
  2612. while (q->next) {
  2613. mdns_parsed_question_t * p = q->next;
  2614. if (_mdns_question_matches(p, type, service)) {
  2615. q->next = p->next;
  2616. free(p->host);
  2617. free(p->service);
  2618. free(p->proto);
  2619. free(p->domain);
  2620. free(p);
  2621. return;
  2622. }
  2623. q = q->next;
  2624. }
  2625. }
  2626. /**
  2627. * @brief Get number of items in TXT parsed data
  2628. */
  2629. static int _mdns_txt_items_count_get(const uint8_t * data, size_t len)
  2630. {
  2631. if (len == 1) {
  2632. return 0;
  2633. }
  2634. int num_items = 0;
  2635. uint16_t i=0;
  2636. size_t partLen = 0;
  2637. while (i < len) {
  2638. partLen = data[i++];
  2639. if (!partLen) {
  2640. break;
  2641. }
  2642. if ((i+partLen) > len) {
  2643. return -1;//error
  2644. }
  2645. i+=partLen;
  2646. num_items++;
  2647. }
  2648. return num_items;
  2649. }
  2650. /**
  2651. * @brief Get the length of TXT item's key name
  2652. */
  2653. static int _mdns_txt_item_name_get_len(const uint8_t * data, size_t len)
  2654. {
  2655. if (*data == '=') {
  2656. return -1;
  2657. }
  2658. for (size_t i = 0; i < len; i++) {
  2659. if (data[i] == '=') {
  2660. return i;
  2661. }
  2662. }
  2663. return len;
  2664. }
  2665. /**
  2666. * @brief Create TXT result array from parsed TXT data
  2667. */
  2668. static void _mdns_result_txt_create(const uint8_t *data, size_t len, mdns_txt_item_t **out_txt, uint8_t **out_value_len,
  2669. size_t *out_count)
  2670. {
  2671. *out_txt = NULL;
  2672. *out_count = 0;
  2673. uint16_t i=0, y;
  2674. size_t partLen = 0;
  2675. int num_items = _mdns_txt_items_count_get(data, len);
  2676. if (num_items < 0) {
  2677. return;//error
  2678. }
  2679. if (!num_items) {
  2680. return;
  2681. }
  2682. mdns_txt_item_t * txt = (mdns_txt_item_t *)malloc(sizeof(mdns_txt_item_t) * num_items);
  2683. if (!txt) {
  2684. HOOK_MALLOC_FAILED;
  2685. return;
  2686. }
  2687. uint8_t * txt_value_len = (uint8_t *)malloc(num_items);
  2688. if (!txt_value_len) {
  2689. free(txt);
  2690. HOOK_MALLOC_FAILED;
  2691. return;
  2692. }
  2693. memset(txt, 0, sizeof(mdns_txt_item_t) * num_items);
  2694. memset(txt_value_len, 0, num_items);
  2695. size_t txt_num = 0;
  2696. while (i < len) {
  2697. partLen = data[i++];
  2698. if (!partLen) {
  2699. break;
  2700. }
  2701. if ((i+partLen) > len) {
  2702. goto handle_error;//error
  2703. }
  2704. int name_len = _mdns_txt_item_name_get_len(data+i, partLen);
  2705. if (name_len < 0) {//invalid item (no name)
  2706. i += partLen;
  2707. continue;
  2708. }
  2709. char * key = (char *)malloc(name_len + 1);
  2710. if (!key) {
  2711. HOOK_MALLOC_FAILED;
  2712. goto handle_error;//error
  2713. }
  2714. mdns_txt_item_t * t = &txt[txt_num];
  2715. uint8_t * value_len = &txt_value_len[txt_num];
  2716. txt_num++;
  2717. memcpy(key, data + i, name_len);
  2718. key[name_len] = 0;
  2719. i += name_len + 1;
  2720. t->key = key;
  2721. int new_value_len = partLen - name_len - 1;
  2722. if (new_value_len > 0) {
  2723. char * value = (char *)malloc(new_value_len + 1);
  2724. if (!value) {
  2725. HOOK_MALLOC_FAILED;
  2726. goto handle_error;//error
  2727. }
  2728. memcpy(value, data + i, new_value_len);
  2729. value[new_value_len] = 0;
  2730. *value_len = new_value_len;
  2731. i += new_value_len;
  2732. t->value = value;
  2733. }
  2734. }
  2735. *out_txt = txt;
  2736. *out_count = txt_num;
  2737. *out_value_len = txt_value_len;
  2738. return;
  2739. handle_error :
  2740. for (y=0; y<txt_num; y++) {
  2741. mdns_txt_item_t * t = &txt[y];
  2742. free((char *)t->key);
  2743. free((char *)t->value);
  2744. }
  2745. free(txt_value_len);
  2746. free(txt);
  2747. }
  2748. /**
  2749. * @brief Duplicate string or return error
  2750. */
  2751. static esp_err_t _mdns_strdup_check(char ** out, char * in)
  2752. {
  2753. if (in && in[0]) {
  2754. *out = strdup(in);
  2755. if (!*out) {
  2756. return ESP_FAIL;
  2757. }
  2758. return ESP_OK;
  2759. }
  2760. *out = NULL;
  2761. return ESP_OK;
  2762. }
  2763. /**
  2764. * @brief main packet parser
  2765. *
  2766. * @param packet the packet
  2767. */
  2768. void mdns_parse_packet(mdns_rx_packet_t * packet)
  2769. {
  2770. static mdns_name_t n;
  2771. mdns_header_t header;
  2772. const uint8_t * data = _mdns_get_packet_data(packet);
  2773. size_t len = _mdns_get_packet_len(packet);
  2774. const uint8_t * content = data + MDNS_HEAD_LEN;
  2775. bool do_not_reply = false;
  2776. mdns_search_once_t * search_result = NULL;
  2777. #ifdef MDNS_ENABLE_DEBUG
  2778. _mdns_dbg_printf("\nRX[%u][%u]: ", packet->tcpip_if, (uint32_t)packet->ip_protocol);
  2779. if (packet->src.type == ESP_IPADDR_TYPE_V4) {
  2780. _mdns_dbg_printf("From: " IPSTR ":%u, To: " IPSTR ", ", IP2STR(&packet->src.u_addr.ip4), packet->src_port, IP2STR(&packet->dest.u_addr.ip4));
  2781. } else {
  2782. _mdns_dbg_printf("From: " IPV6STR ":%u, To: " IPV6STR ", ", IPV62STR(packet->src.u_addr.ip6), packet->src_port, IPV62STR(packet->dest.u_addr.ip6));
  2783. }
  2784. mdns_debug_packet(data, len);
  2785. #endif
  2786. mdns_parsed_packet_t * parsed_packet = (mdns_parsed_packet_t *)malloc(sizeof(mdns_parsed_packet_t));
  2787. if (!parsed_packet) {
  2788. HOOK_MALLOC_FAILED;
  2789. return;
  2790. }
  2791. memset(parsed_packet, 0, sizeof(mdns_parsed_packet_t));
  2792. mdns_name_t * name = &n;
  2793. memset(name, 0, sizeof(mdns_name_t));
  2794. header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
  2795. header.flags.value = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
  2796. header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
  2797. header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
  2798. header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
  2799. header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
  2800. if (header.flags.value == MDNS_FLAGS_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) {
  2801. free(parsed_packet);
  2802. return;
  2803. }
  2804. //if we have not set the hostname, we can not answer questions
  2805. if (header.questions && !header.answers && _str_null_or_empty(_mdns_server->hostname)) {
  2806. free(parsed_packet);
  2807. return;
  2808. }
  2809. parsed_packet->tcpip_if = packet->tcpip_if;
  2810. parsed_packet->ip_protocol = packet->ip_protocol;
  2811. parsed_packet->multicast = packet->multicast;
  2812. parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
  2813. parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
  2814. parsed_packet->id = header.id;
  2815. esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src);
  2816. parsed_packet->src_port = packet->src_port;
  2817. if (header.questions) {
  2818. uint8_t qs = header.questions;
  2819. while (qs--) {
  2820. content = _mdns_parse_fqdn(data, content, name);
  2821. if (!content) {
  2822. header.answers = 0;
  2823. header.additional = 0;
  2824. header.servers = 0;
  2825. goto clear_rx_packet;//error
  2826. }
  2827. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  2828. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  2829. bool unicast = !!(mdns_class & 0x8000);
  2830. mdns_class &= 0x7FFF;
  2831. content = content + 4;
  2832. if (mdns_class != 0x0001 || name->invalid) {//bad class or invalid name for this question entry
  2833. continue;
  2834. }
  2835. if (_mdns_name_is_discovery(name, type)) {
  2836. //service discovery
  2837. parsed_packet->discovery = true;
  2838. mdns_srv_item_t * a = _mdns_server->services;
  2839. while (a) {
  2840. mdns_parsed_question_t * question = (mdns_parsed_question_t *)calloc(1, sizeof(mdns_parsed_question_t));
  2841. if (!question) {
  2842. HOOK_MALLOC_FAILED;
  2843. goto clear_rx_packet;
  2844. }
  2845. question->next = parsed_packet->questions;
  2846. parsed_packet->questions = question;
  2847. question->unicast = unicast;
  2848. question->type = MDNS_TYPE_SDPTR;
  2849. question->host = NULL;
  2850. question->service = strdup(a->service->service);
  2851. question->proto = strdup(a->service->proto);
  2852. question->domain = strdup(MDNS_DEFAULT_DOMAIN);
  2853. if (!question->service || !question->proto || !question->domain) {
  2854. goto clear_rx_packet;
  2855. }
  2856. a = a->next;
  2857. }
  2858. continue;
  2859. } else if (name->sub || !_mdns_name_is_ours(name)) {
  2860. continue;
  2861. }
  2862. if (type == MDNS_TYPE_ANY && !_str_null_or_empty(name->host)) {
  2863. parsed_packet->probe = true;
  2864. }
  2865. mdns_parsed_question_t * question = (mdns_parsed_question_t *)calloc(1, sizeof(mdns_parsed_question_t));
  2866. if (!question) {
  2867. HOOK_MALLOC_FAILED;
  2868. goto clear_rx_packet;
  2869. }
  2870. question->next = parsed_packet->questions;
  2871. parsed_packet->questions = question;
  2872. question->unicast = unicast;
  2873. question->type = type;
  2874. if (_mdns_strdup_check(&(question->host), name->host)
  2875. || _mdns_strdup_check(&(question->service), name->service)
  2876. || _mdns_strdup_check(&(question->proto), name->proto)
  2877. || _mdns_strdup_check(&(question->domain), name->domain)) {
  2878. goto clear_rx_packet;
  2879. }
  2880. }
  2881. }
  2882. if (header.questions && !parsed_packet->questions && !parsed_packet->discovery && !header.answers) {
  2883. goto clear_rx_packet;
  2884. } else if (header.answers || header.servers || header.additional) {
  2885. uint16_t recordIndex = 0;
  2886. while (content < (data + len)) {
  2887. content = _mdns_parse_fqdn(data, content, name);
  2888. if (!content) {
  2889. goto clear_rx_packet;//error
  2890. }
  2891. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  2892. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  2893. uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET);
  2894. uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET);
  2895. const uint8_t * data_ptr = content + MDNS_DATA_OFFSET;
  2896. mdns_class &= 0x7FFF;
  2897. content = data_ptr + data_len;
  2898. if (content > (data + len)) {
  2899. goto clear_rx_packet;
  2900. }
  2901. bool discovery = false;
  2902. bool ours = false;
  2903. mdns_srv_item_t * service = NULL;
  2904. mdns_parsed_record_type_t record_type = MDNS_ANSWER;
  2905. if (recordIndex >= (header.answers + header.servers)) {
  2906. record_type = MDNS_EXTRA;
  2907. } else if (recordIndex >= (header.answers)) {
  2908. record_type = MDNS_NS;
  2909. }
  2910. recordIndex++;
  2911. if (type == MDNS_TYPE_NSEC || type == MDNS_TYPE_OPT) {
  2912. //skip NSEC and OPT
  2913. continue;
  2914. }
  2915. if (parsed_packet->discovery && _mdns_name_is_discovery(name, type)) {
  2916. discovery = true;
  2917. } else if (!name->sub && _mdns_name_is_ours(name)) {
  2918. ours = true;
  2919. if (name->service && name->service[0] && name->proto && name->proto[0]) {
  2920. service = _mdns_get_service_item(name->service, name->proto, NULL);
  2921. }
  2922. } else {
  2923. if (!parsed_packet->authoritative || record_type == MDNS_NS) {
  2924. //skip this record
  2925. continue;
  2926. }
  2927. search_result = _mdns_search_find_from(_mdns_server->search_once, name, type, packet->tcpip_if, packet->ip_protocol);
  2928. }
  2929. if (type == MDNS_TYPE_PTR) {
  2930. if (!_mdns_parse_fqdn(data, data_ptr, name)) {
  2931. continue;//error
  2932. }
  2933. if (search_result) {
  2934. _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
  2935. packet->tcpip_if, packet->ip_protocol, ttl);
  2936. } else if ((discovery || ours) && !name->sub && _mdns_name_is_ours(name)) {
  2937. if (discovery) {
  2938. service = _mdns_get_service_item(name->service, name->proto, NULL);
  2939. _mdns_remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service);
  2940. } else if (service && parsed_packet->questions && !parsed_packet->probe) {
  2941. _mdns_remove_parsed_question(parsed_packet, type, service);
  2942. } else if (service) {
  2943. //check if TTL is more than half of the full TTL value (4500)
  2944. if (ttl > 2250) {
  2945. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  2946. }
  2947. }
  2948. }
  2949. } else if (type == MDNS_TYPE_SRV) {
  2950. mdns_result_t * result = NULL;
  2951. if (search_result && search_result->type == MDNS_TYPE_PTR) {
  2952. result = search_result->result;
  2953. while (result) {
  2954. if (packet->tcpip_if == result->tcpip_if
  2955. && packet->ip_protocol == result->ip_protocol
  2956. && result->instance_name && !strcmp(name->host, result->instance_name)) {
  2957. break;
  2958. }
  2959. result = result->next;
  2960. }
  2961. if (!result) {
  2962. result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
  2963. packet->tcpip_if, packet->ip_protocol, ttl);
  2964. if (!result) {
  2965. continue;//error
  2966. }
  2967. }
  2968. }
  2969. if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name)) {
  2970. continue;//error
  2971. }
  2972. uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
  2973. uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET);
  2974. uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET);
  2975. if (search_result) {
  2976. if (search_result->type == MDNS_TYPE_PTR) {
  2977. if (!result->hostname) { // assign host/port for this entry only if not previously set
  2978. result->port = port;
  2979. result->hostname = strdup(name->host);
  2980. }
  2981. } else {
  2982. _mdns_search_result_add_srv(search_result, name->host, port, packet->tcpip_if, packet->ip_protocol, ttl);
  2983. }
  2984. } else if (ours) {
  2985. if (parsed_packet->questions && !parsed_packet->probe) {
  2986. _mdns_remove_parsed_question(parsed_packet, type, service);
  2987. continue;
  2988. } else if (parsed_packet->distributed) {
  2989. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  2990. continue;
  2991. }
  2992. //detect collision (-1=won, 0=none, 1=lost)
  2993. int col = 0;
  2994. if (mdns_class > 1) {
  2995. col = 1;
  2996. } else if (!mdns_class) {
  2997. col = -1;
  2998. } else if (service) { // only detect srv collision if service existed
  2999. col = _mdns_check_srv_collision(service->service, priority, weight, port, name->host, name->domain);
  3000. }
  3001. if (service && col && (parsed_packet->probe || parsed_packet->authoritative)) {
  3002. if (col > 0 || !port) {
  3003. do_not_reply = true;
  3004. if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3005. _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
  3006. if (!_str_null_or_empty(service->service->instance)) {
  3007. char * new_instance = _mdns_mangle_name((char *)service->service->instance);
  3008. if (new_instance) {
  3009. free((char *)service->service->instance);
  3010. service->service->instance = new_instance;
  3011. }
  3012. _mdns_probe_all_pcbs(&service, 1, false, false);
  3013. } else if (!_str_null_or_empty(_mdns_server->instance)) {
  3014. char * new_instance = _mdns_mangle_name((char *)_mdns_server->instance);
  3015. if (new_instance) {
  3016. free((char *)_mdns_server->instance);
  3017. _mdns_server->instance = new_instance;
  3018. }
  3019. _mdns_restart_all_pcbs_no_instance();
  3020. } else {
  3021. char * new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
  3022. if (new_host) {
  3023. _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
  3024. free((char *)_mdns_server->hostname);
  3025. _mdns_server->hostname = new_host;
  3026. _mdns_self_host.hostname = new_host;
  3027. }
  3028. _mdns_restart_all_pcbs();
  3029. }
  3030. } else if (service) {
  3031. _mdns_pcb_send_bye(packet->tcpip_if, packet->ip_protocol, &service, 1, false);
  3032. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, false);
  3033. }
  3034. }
  3035. } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions) {
  3036. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  3037. }
  3038. }
  3039. } else if (type == MDNS_TYPE_TXT) {
  3040. if (search_result) {
  3041. mdns_txt_item_t * txt = NULL;
  3042. uint8_t *txt_value_len = NULL;
  3043. size_t txt_count = 0;
  3044. mdns_result_t * result = NULL;
  3045. if (search_result->type == MDNS_TYPE_PTR) {
  3046. result = search_result->result;
  3047. while (result) {
  3048. if (packet->tcpip_if == result->tcpip_if
  3049. && packet->ip_protocol == result->ip_protocol
  3050. && result->instance_name && !strcmp(name->host, result->instance_name)) {
  3051. break;
  3052. }
  3053. result = result->next;
  3054. }
  3055. if (!result) {
  3056. result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
  3057. packet->tcpip_if, packet->ip_protocol, ttl);
  3058. if (!result) {
  3059. continue;//error
  3060. }
  3061. }
  3062. if (!result->txt) {
  3063. _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
  3064. if (txt_count) {
  3065. result->txt = txt;
  3066. result->txt_count = txt_count;
  3067. result->txt_value_len = txt_value_len;
  3068. }
  3069. }
  3070. } else {
  3071. _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
  3072. if (txt_count) {
  3073. _mdns_search_result_add_txt(search_result, txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl);
  3074. }
  3075. }
  3076. } else if (ours) {
  3077. if (parsed_packet->questions && !parsed_packet->probe) {
  3078. _mdns_remove_parsed_question(parsed_packet, type, service);
  3079. continue;
  3080. }
  3081. //detect collision (-1=won, 0=none, 1=lost)
  3082. int col = 0;
  3083. if (mdns_class > 1) {
  3084. col = 1;
  3085. } else if (!mdns_class) {
  3086. col = -1;
  3087. } else if (service) { // only detect txt collision if service existed
  3088. col = _mdns_check_txt_collision(service->service, data_ptr, data_len);
  3089. }
  3090. if (col && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running && service) {
  3091. do_not_reply = true;
  3092. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, true);
  3093. } else if (ttl > 2250 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3094. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
  3095. }
  3096. }
  3097. }
  3098. #if CONFIG_LWIP_IPV6
  3099. else if (type == MDNS_TYPE_AAAA) {//ipv6
  3100. esp_ip_addr_t ip6;
  3101. ip6.type = ESP_IPADDR_TYPE_V6;
  3102. memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
  3103. if (search_result) {
  3104. //check for more applicable searches (PTR & A/AAAA at the same time)
  3105. while (search_result) {
  3106. _mdns_search_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl);
  3107. search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
  3108. }
  3109. } else if (ours) {
  3110. if (parsed_packet->questions && !parsed_packet->probe) {
  3111. _mdns_remove_parsed_question(parsed_packet, type, NULL);
  3112. continue;
  3113. }
  3114. //detect collision (-1=won, 0=none, 1=lost)
  3115. int col = 0;
  3116. if (mdns_class > 1) {
  3117. col = 1;
  3118. } else if (!mdns_class) {
  3119. col = -1;
  3120. } else {
  3121. col = _mdns_check_aaaa_collision(&(ip6.u_addr.ip6), packet->tcpip_if);
  3122. }
  3123. if (col == 2) {
  3124. goto clear_rx_packet;
  3125. } else if (col == 1) {
  3126. do_not_reply = true;
  3127. if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3128. if (col && (parsed_packet->probe || parsed_packet->authoritative)) {
  3129. _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
  3130. char * new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
  3131. if (new_host) {
  3132. _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
  3133. free((char *)_mdns_server->hostname);
  3134. _mdns_server->hostname = new_host;
  3135. _mdns_self_host.hostname = new_host;
  3136. }
  3137. _mdns_restart_all_pcbs();
  3138. }
  3139. } else {
  3140. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true);
  3141. }
  3142. } 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) {
  3143. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL);
  3144. }
  3145. }
  3146. }
  3147. #endif
  3148. else if (type == MDNS_TYPE_A) {
  3149. esp_ip_addr_t ip;
  3150. ip.type = ESP_IPADDR_TYPE_V4;
  3151. memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
  3152. if (search_result) {
  3153. //check for more applicable searches (PTR & A/AAAA at the same time)
  3154. while (search_result) {
  3155. _mdns_search_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl);
  3156. search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
  3157. }
  3158. } else if (ours) {
  3159. if (parsed_packet->questions && !parsed_packet->probe) {
  3160. _mdns_remove_parsed_question(parsed_packet, type, NULL);
  3161. continue;
  3162. }
  3163. //detect collision (-1=won, 0=none, 1=lost)
  3164. int col = 0;
  3165. if (mdns_class > 1) {
  3166. col = 1;
  3167. } else if (!mdns_class) {
  3168. col = -1;
  3169. } else {
  3170. col = _mdns_check_a_collision(&(ip.u_addr.ip4), packet->tcpip_if);
  3171. }
  3172. if (col == 2) {
  3173. goto clear_rx_packet;
  3174. } else if (col == 1) {
  3175. do_not_reply = true;
  3176. if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
  3177. if (col && (parsed_packet->probe || parsed_packet->authoritative)) {
  3178. _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
  3179. char * new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
  3180. if (new_host) {
  3181. _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
  3182. free((char *)_mdns_server->hostname);
  3183. _mdns_server->hostname = new_host;
  3184. _mdns_self_host.hostname = new_host;
  3185. }
  3186. _mdns_restart_all_pcbs();
  3187. }
  3188. } else {
  3189. _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true);
  3190. }
  3191. } 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) {
  3192. _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL);
  3193. }
  3194. }
  3195. }
  3196. }
  3197. //end while
  3198. if (parsed_packet->authoritative) {
  3199. _mdns_search_finish_done();
  3200. }
  3201. }
  3202. if (!do_not_reply && _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].state > PCB_PROBE_3 && (parsed_packet->questions || parsed_packet->discovery)) {
  3203. _mdns_create_answer_from_parsed_packet(parsed_packet);
  3204. }
  3205. clear_rx_packet:
  3206. while (parsed_packet->questions) {
  3207. mdns_parsed_question_t * question = parsed_packet->questions;
  3208. parsed_packet->questions = parsed_packet->questions->next;
  3209. if (question->host) {
  3210. free(question->host);
  3211. }
  3212. if (question->service) {
  3213. free(question->service);
  3214. }
  3215. if (question->proto) {
  3216. free(question->proto);
  3217. }
  3218. if (question->domain) {
  3219. free(question->domain);
  3220. }
  3221. free(question);
  3222. }
  3223. free(parsed_packet);
  3224. }
  3225. /**
  3226. * @brief Enable mDNS interface
  3227. */
  3228. void _mdns_enable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  3229. {
  3230. if (!_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
  3231. if (_mdns_pcb_init(tcpip_if, ip_protocol)) {
  3232. return;
  3233. }
  3234. }
  3235. _mdns_restart_pcb(tcpip_if, ip_protocol);
  3236. }
  3237. /**
  3238. * @brief Disable mDNS interface
  3239. */
  3240. void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  3241. {
  3242. _mdns_clean_netif_ptr(tcpip_if);
  3243. if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
  3244. _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
  3245. _mdns_pcb_deinit(tcpip_if, ip_protocol);
  3246. mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
  3247. if (other_if != MDNS_IF_MAX && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) {
  3248. _mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF;
  3249. _mdns_enable_pcb(other_if, ip_protocol);
  3250. }
  3251. }
  3252. _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state = PCB_OFF;
  3253. }
  3254. /**
  3255. * @brief Dispatch interface changes based on system events
  3256. */
  3257. static void _mdns_handle_system_event(esp_event_base_t event_base,
  3258. int32_t event_id, esp_netif_t* interface)
  3259. {
  3260. if (!_mdns_server) {
  3261. return;
  3262. }
  3263. esp_netif_dhcp_status_t dcst;
  3264. if (event_base == WIFI_EVENT) {
  3265. switch(event_id) {
  3266. case WIFI_EVENT_STA_CONNECTED:
  3267. if (!esp_netif_dhcpc_get_status(_mdns_get_esp_netif(MDNS_IF_STA), &dcst)) {
  3268. if (dcst == ESP_NETIF_DHCP_STOPPED) {
  3269. _mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
  3270. }
  3271. }
  3272. break;
  3273. case WIFI_EVENT_STA_DISCONNECTED:
  3274. _mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
  3275. _mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6);
  3276. break;
  3277. case WIFI_EVENT_AP_START:
  3278. _mdns_enable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4);
  3279. break;
  3280. case WIFI_EVENT_AP_STOP:
  3281. _mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4);
  3282. _mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V6);
  3283. break;
  3284. default:
  3285. break;
  3286. }
  3287. }
  3288. #if CONFIG_ETH_ENABLED
  3289. else if (event_base == ETH_EVENT) {
  3290. switch (event_id) {
  3291. case ETHERNET_EVENT_CONNECTED:
  3292. if (!esp_netif_dhcpc_get_status(_mdns_get_esp_netif(MDNS_IF_ETH), &dcst)) {
  3293. if (dcst == ESP_NETIF_DHCP_STOPPED) {
  3294. _mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
  3295. }
  3296. }
  3297. break;
  3298. case ETHERNET_EVENT_DISCONNECTED:
  3299. _mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
  3300. _mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V6);
  3301. break;
  3302. default:
  3303. break;
  3304. }
  3305. }
  3306. #endif
  3307. else if (event_base == IP_EVENT) {
  3308. switch (event_id) {
  3309. case IP_EVENT_STA_GOT_IP:
  3310. _mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
  3311. _mdns_announce_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6, NULL, 0, true);
  3312. break;
  3313. #if CONFIG_ETH_ENABLED
  3314. case IP_EVENT_ETH_GOT_IP:
  3315. _mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
  3316. break;
  3317. #endif
  3318. case IP_EVENT_GOT_IP6:
  3319. {
  3320. mdns_if_t mdns_if = _mdns_get_if_from_esp_netif(interface);
  3321. if (mdns_if != MDNS_IF_MAX) {
  3322. _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
  3323. _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4, NULL, 0, true);
  3324. }
  3325. }
  3326. break;
  3327. default:
  3328. break;
  3329. }
  3330. }
  3331. }
  3332. /*
  3333. * MDNS Search
  3334. * */
  3335. /**
  3336. * @brief Free search structure (except the results)
  3337. */
  3338. static void _mdns_search_free(mdns_search_once_t * search)
  3339. {
  3340. free(search->instance);
  3341. free(search->service);
  3342. free(search->proto);
  3343. vSemaphoreDelete(search->done_semaphore);
  3344. free(search);
  3345. }
  3346. /**
  3347. * @brief Allocate new search structure
  3348. */
  3349. static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type,
  3350. uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier)
  3351. {
  3352. mdns_search_once_t * search = (mdns_search_once_t *)malloc(sizeof(mdns_search_once_t));
  3353. if (!search) {
  3354. HOOK_MALLOC_FAILED;
  3355. return NULL;
  3356. }
  3357. memset(search, 0, sizeof(mdns_search_once_t));
  3358. search->done_semaphore = xSemaphoreCreateBinary();
  3359. if (!search->done_semaphore) {
  3360. free(search);
  3361. return NULL;
  3362. }
  3363. if (!_str_null_or_empty(name)) {
  3364. search->instance = strndup(name, MDNS_NAME_BUF_LEN-1);
  3365. if (!search->instance) {
  3366. _mdns_search_free(search);
  3367. return NULL;
  3368. }
  3369. }
  3370. if (!_str_null_or_empty(service)) {
  3371. search->service = strndup(service, MDNS_NAME_BUF_LEN-1);
  3372. if (!search->service) {
  3373. _mdns_search_free(search);
  3374. return NULL;
  3375. }
  3376. }
  3377. if (!_str_null_or_empty(proto)) {
  3378. search->proto = strndup(proto, MDNS_NAME_BUF_LEN-1);
  3379. if (!search->proto) {
  3380. _mdns_search_free(search);
  3381. return NULL;
  3382. }
  3383. }
  3384. search->type = type;
  3385. search->timeout = timeout;
  3386. search->num_results = 0;
  3387. search->max_results = max_results;
  3388. search->result = NULL;
  3389. search->state = SEARCH_INIT;
  3390. search->sent_at = 0;
  3391. search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS;
  3392. search->notifier = notifier;
  3393. search->next = NULL;
  3394. return search;
  3395. }
  3396. /**
  3397. * @brief Mark search as finished and remove it from search chain
  3398. */
  3399. static void _mdns_search_finish(mdns_search_once_t * search)
  3400. {
  3401. search->state = SEARCH_OFF;
  3402. queueDetach(mdns_search_once_t, _mdns_server->search_once, search);
  3403. if (search->notifier) {
  3404. search->notifier(search);
  3405. }
  3406. xSemaphoreGive(search->done_semaphore);
  3407. }
  3408. /**
  3409. * @brief Add new search to the search chain
  3410. */
  3411. static void _mdns_search_add(mdns_search_once_t * search)
  3412. {
  3413. search->next = _mdns_server->search_once;
  3414. _mdns_server->search_once = search;
  3415. }
  3416. /**
  3417. * @brief Called from parser to finish any searches that have reached maximum results
  3418. */
  3419. static void _mdns_search_finish_done(void)
  3420. {
  3421. mdns_search_once_t * search = _mdns_server->search_once;
  3422. mdns_search_once_t * s = NULL;
  3423. while (search) {
  3424. s = search;
  3425. search = search->next;
  3426. if (s->max_results && s->num_results >= s->max_results) {
  3427. _mdns_search_finish(s);
  3428. }
  3429. }
  3430. }
  3431. /**
  3432. * @brief Create linked IP (copy) from parsed one
  3433. */
  3434. static mdns_ip_addr_t * _mdns_result_addr_create_ip(esp_ip_addr_t * ip)
  3435. {
  3436. mdns_ip_addr_t * a = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
  3437. if (!a) {
  3438. HOOK_MALLOC_FAILED;
  3439. return NULL;
  3440. }
  3441. memset(a, 0 , sizeof(mdns_ip_addr_t));
  3442. a->addr.type = ip->type;
  3443. if (ip->type == ESP_IPADDR_TYPE_V6) {
  3444. memcpy(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16);
  3445. } else {
  3446. a->addr.u_addr.ip4.addr = ip->u_addr.ip4.addr;
  3447. }
  3448. return a;
  3449. }
  3450. static inline void _mdns_result_update_ttl(mdns_result_t * r, uint32_t ttl)
  3451. {
  3452. r->ttl = r->ttl < ttl ? r->ttl : ttl;
  3453. }
  3454. /**
  3455. * @brief Chain new IP to search result
  3456. */
  3457. static void _mdns_result_add_ip(mdns_result_t * r, esp_ip_addr_t * ip)
  3458. {
  3459. mdns_ip_addr_t * a = r->addr;
  3460. while (a) {
  3461. if (a->addr.type == ip->type) {
  3462. if (a->addr.type == ESP_IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) {
  3463. return;
  3464. }
  3465. if (a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) {
  3466. return;
  3467. }
  3468. }
  3469. a = a->next;
  3470. }
  3471. a = _mdns_result_addr_create_ip(ip);
  3472. if (!a) {
  3473. return;
  3474. }
  3475. a->next = r->addr;
  3476. r->addr = a;
  3477. }
  3478. /**
  3479. * @brief Called from parser to add A/AAAA data to search result
  3480. */
  3481. static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip,
  3482. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
  3483. {
  3484. mdns_result_t * r = NULL;
  3485. mdns_ip_addr_t * a = NULL;
  3486. if ((search->type == MDNS_TYPE_A && ip->type == ESP_IPADDR_TYPE_V4)
  3487. || (search->type == MDNS_TYPE_AAAA && ip->type == ESP_IPADDR_TYPE_V6)
  3488. || search->type == MDNS_TYPE_ANY) {
  3489. r = search->result;
  3490. while (r) {
  3491. if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
  3492. _mdns_result_add_ip(r, ip);
  3493. _mdns_result_update_ttl(r, ttl);
  3494. return;
  3495. }
  3496. r = r->next;
  3497. }
  3498. if (!search->max_results || search->num_results < search->max_results) {
  3499. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  3500. if (!r) {
  3501. HOOK_MALLOC_FAILED;
  3502. return;
  3503. }
  3504. memset(r, 0 , sizeof(mdns_result_t));
  3505. a = _mdns_result_addr_create_ip(ip);
  3506. if (!a) {
  3507. free(r);
  3508. return;
  3509. }
  3510. a->next = r->addr;
  3511. r->hostname = strdup(hostname);
  3512. r->addr = a;
  3513. r->tcpip_if = tcpip_if;
  3514. r->ip_protocol = ip_protocol;
  3515. r->next = search->result;
  3516. r->ttl = ttl;
  3517. search->result = r;
  3518. search->num_results++;
  3519. }
  3520. } else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) {
  3521. r = search->result;
  3522. while (r) {
  3523. if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
  3524. _mdns_result_add_ip(r, ip);
  3525. _mdns_result_update_ttl(r, ttl);
  3526. break;
  3527. }
  3528. r = r->next;
  3529. }
  3530. }
  3531. }
  3532. /**
  3533. * @brief Called from parser to add PTR data to search result
  3534. */
  3535. static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance,
  3536. const char * service_type, const char * proto, mdns_if_t tcpip_if,
  3537. mdns_ip_protocol_t ip_protocol, uint32_t ttl)
  3538. {
  3539. mdns_result_t * r = search->result;
  3540. while (r) {
  3541. if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) {
  3542. _mdns_result_update_ttl(r, ttl);
  3543. return r;
  3544. }
  3545. r = r->next;
  3546. }
  3547. if (!search->max_results || search->num_results < search->max_results) {
  3548. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  3549. if (!r) {
  3550. HOOK_MALLOC_FAILED;
  3551. return NULL;
  3552. }
  3553. memset(r, 0 , sizeof(mdns_result_t));
  3554. r->instance_name = strdup(instance);
  3555. r->service_type = strdup(service_type);
  3556. r->proto = strdup(proto);
  3557. if (!r->instance_name) {
  3558. free(r);
  3559. return NULL;
  3560. }
  3561. r->tcpip_if = tcpip_if;
  3562. r->ip_protocol = ip_protocol;
  3563. r->ttl = ttl;
  3564. r->next = search->result;
  3565. search->result = r;
  3566. search->num_results++;
  3567. return r;
  3568. }
  3569. return NULL;
  3570. }
  3571. /**
  3572. * @brief Called from parser to add SRV data to search result
  3573. */
  3574. static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
  3575. mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
  3576. {
  3577. mdns_result_t * r = search->result;
  3578. while (r) {
  3579. if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
  3580. _mdns_result_update_ttl(r, ttl);
  3581. return;
  3582. }
  3583. r = r->next;
  3584. }
  3585. if (!search->max_results || search->num_results < search->max_results) {
  3586. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  3587. if (!r) {
  3588. HOOK_MALLOC_FAILED;
  3589. return;
  3590. }
  3591. memset(r, 0 , sizeof(mdns_result_t));
  3592. r->hostname = strdup(hostname);
  3593. if (!r->hostname) {
  3594. free(r);
  3595. return;
  3596. }
  3597. if (search->instance) {
  3598. r->instance_name = strdup(search->instance);
  3599. }
  3600. r->service_type = strdup(search->service);
  3601. r->proto = strdup(search->proto);
  3602. r->port = port;
  3603. r->tcpip_if = tcpip_if;
  3604. r->ip_protocol = ip_protocol;
  3605. r->ttl = ttl;
  3606. r->next = search->result;
  3607. search->result = r;
  3608. search->num_results++;
  3609. }
  3610. }
  3611. /**
  3612. * @brief Called from parser to add TXT data to search result
  3613. */
  3614. static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
  3615. size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
  3616. uint32_t ttl)
  3617. {
  3618. mdns_result_t * r = search->result;
  3619. while (r) {
  3620. if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
  3621. if (r->txt) {
  3622. goto free_txt;
  3623. }
  3624. r->txt = txt;
  3625. r->txt_value_len = txt_value_len;
  3626. r->txt_count = txt_count;
  3627. _mdns_result_update_ttl(r, ttl);
  3628. return;
  3629. }
  3630. r = r->next;
  3631. }
  3632. if (!search->max_results || search->num_results < search->max_results) {
  3633. r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
  3634. if (!r) {
  3635. HOOK_MALLOC_FAILED;
  3636. goto free_txt;
  3637. }
  3638. memset(r, 0 , sizeof(mdns_result_t));
  3639. r->txt = txt;
  3640. r->txt_value_len = txt_value_len;
  3641. r->txt_count = txt_count;
  3642. r->tcpip_if = tcpip_if;
  3643. r->ip_protocol = ip_protocol;
  3644. r->ttl = ttl;
  3645. r->next = search->result;
  3646. search->result = r;
  3647. search->num_results++;
  3648. }
  3649. return;
  3650. free_txt:
  3651. for (size_t i=0; i<txt_count; i++) {
  3652. free((char *)(txt[i].key));
  3653. free((char *)(txt[i].value));
  3654. }
  3655. free(txt);
  3656. }
  3657. /**
  3658. * @brief Called from packet parser to find matching running search
  3659. */
  3660. 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)
  3661. {
  3662. mdns_result_t * r = NULL;
  3663. while (s) {
  3664. if (s->state == SEARCH_OFF) {
  3665. s = s->next;
  3666. continue;
  3667. }
  3668. if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
  3669. if ((s->type == MDNS_TYPE_ANY && s->service != NULL)
  3670. || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV))
  3671. {
  3672. s = s->next;
  3673. continue;
  3674. }
  3675. if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) {
  3676. if (!strcasecmp(name->host, s->instance)) {
  3677. return s;
  3678. }
  3679. s = s->next;
  3680. continue;
  3681. }
  3682. r = s->result;
  3683. while (r) {
  3684. if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) {
  3685. return s;
  3686. }
  3687. r = r->next;
  3688. }
  3689. s = s->next;
  3690. continue;
  3691. }
  3692. if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) {
  3693. if ((s->type == MDNS_TYPE_ANY && s->service == NULL)
  3694. || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR))
  3695. {
  3696. s = s->next;
  3697. continue;
  3698. }
  3699. if (strcasecmp(name->service, s->service)
  3700. || strcasecmp(name->proto, s->proto))
  3701. {
  3702. s = s->next;
  3703. continue;
  3704. }
  3705. if (s->type != MDNS_TYPE_PTR) {
  3706. if (!strcasecmp(name->host, s->instance)) {
  3707. return s;
  3708. }
  3709. s = s->next;
  3710. continue;
  3711. }
  3712. return s;
  3713. }
  3714. if (type == MDNS_TYPE_PTR && type == s->type && !strcasecmp(name->service, s->service) && !strcasecmp(name->proto, s->proto)) {
  3715. return s;
  3716. }
  3717. s = s->next;
  3718. }
  3719. return NULL;
  3720. }
  3721. /**
  3722. * @brief Create search packet for particular interface
  3723. */
  3724. 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)
  3725. {
  3726. mdns_result_t * r = NULL;
  3727. mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
  3728. if (!packet) {
  3729. return NULL;
  3730. }
  3731. mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
  3732. if (!q) {
  3733. HOOK_MALLOC_FAILED;
  3734. _mdns_free_tx_packet(packet);
  3735. return NULL;
  3736. }
  3737. q->next = NULL;
  3738. q->unicast = search->type != MDNS_TYPE_PTR;
  3739. q->type = search->type;
  3740. q->host = search->instance;
  3741. q->service = search->service;
  3742. q->proto = search->proto;
  3743. q->domain = MDNS_DEFAULT_DOMAIN;
  3744. q->own_dynamic_memory = false;
  3745. queueToEnd(mdns_out_question_t, packet->questions, q);
  3746. if (search->type == MDNS_TYPE_PTR) {
  3747. r = search->result;
  3748. while (r) {
  3749. //full record on the same interface is available
  3750. if (r->tcpip_if != tcpip_if || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) {
  3751. r = r->next;
  3752. continue;
  3753. }
  3754. mdns_out_answer_t * a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
  3755. if (!a) {
  3756. HOOK_MALLOC_FAILED;
  3757. _mdns_free_tx_packet(packet);
  3758. return NULL;
  3759. }
  3760. a->type = MDNS_TYPE_PTR;
  3761. a->service = NULL;
  3762. a->custom_instance = r->instance_name;
  3763. a->custom_service = search->service;
  3764. a->custom_proto = search->proto;
  3765. a->bye = false;
  3766. a->flush = false;
  3767. a->next = NULL;
  3768. queueToEnd(mdns_out_answer_t, packet->answers, a);
  3769. r = r->next;
  3770. }
  3771. }
  3772. return packet;
  3773. }
  3774. /**
  3775. * @brief Send search packet to particular interface
  3776. */
  3777. static void _mdns_search_send_pcb(mdns_search_once_t * search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  3778. {
  3779. mdns_tx_packet_t * packet = NULL;
  3780. if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) {
  3781. packet = _mdns_create_search_packet(search, tcpip_if, ip_protocol);
  3782. if (!packet) {
  3783. return;
  3784. }
  3785. _mdns_dispatch_tx_packet(packet);
  3786. _mdns_free_tx_packet(packet);
  3787. }
  3788. }
  3789. /**
  3790. * @brief Send search packet to all available interfaces
  3791. */
  3792. static void _mdns_search_send(mdns_search_once_t * search)
  3793. {
  3794. mdns_search_once_t* queue = _mdns_server->search_once;
  3795. bool found = false;
  3796. // looking for this search in active searches
  3797. while (queue) {
  3798. if (queue == search) {
  3799. found = true;
  3800. break;
  3801. }
  3802. queue = queue->next;
  3803. }
  3804. if (!found) {
  3805. // no longer active -> skip sending this search
  3806. return;
  3807. }
  3808. uint8_t i, j;
  3809. for (i=0; i<MDNS_IF_MAX; i++) {
  3810. for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
  3811. _mdns_search_send_pcb(search, (mdns_if_t)i, (mdns_ip_protocol_t)j);
  3812. }
  3813. }
  3814. }
  3815. static void _mdns_tx_handle_packet(mdns_tx_packet_t * p)
  3816. {
  3817. mdns_tx_packet_t * a = NULL;
  3818. mdns_out_question_t * q = NULL;
  3819. mdns_pcb_t * pcb = &_mdns_server->interfaces[p->tcpip_if].pcbs[p->ip_protocol];
  3820. uint32_t send_after = 1000;
  3821. if (pcb->state == PCB_OFF) {
  3822. _mdns_free_tx_packet(p);
  3823. return;
  3824. }
  3825. _mdns_dispatch_tx_packet(p);
  3826. switch(pcb->state) {
  3827. case PCB_PROBE_1:
  3828. q = p->questions;
  3829. while (q) {
  3830. q->unicast = false;
  3831. q = q->next;
  3832. }
  3833. //fallthrough
  3834. case PCB_PROBE_2:
  3835. _mdns_schedule_tx_packet(p, 250);
  3836. pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1);
  3837. break;
  3838. case PCB_PROBE_3:
  3839. a = _mdns_create_announce_from_probe(p);
  3840. if (!a) {
  3841. _mdns_schedule_tx_packet(p, 250);
  3842. break;
  3843. }
  3844. pcb->probe_running = false;
  3845. pcb->probe_ip = false;
  3846. pcb->probe_services_len = 0;
  3847. pcb->failed_probes = 0;
  3848. free(pcb->probe_services);
  3849. pcb->probe_services = NULL;
  3850. _mdns_free_tx_packet(p);
  3851. p = a;
  3852. send_after = 250;
  3853. //fallthrough
  3854. case PCB_ANNOUNCE_1:
  3855. //fallthrough
  3856. case PCB_ANNOUNCE_2:
  3857. _mdns_schedule_tx_packet(p, send_after);
  3858. pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1);
  3859. break;
  3860. case PCB_ANNOUNCE_3:
  3861. pcb->state = PCB_RUNNING;
  3862. _mdns_free_tx_packet(p);
  3863. break;
  3864. default:
  3865. _mdns_free_tx_packet(p);
  3866. break;
  3867. }
  3868. }
  3869. static void _mdns_remap_self_service_hostname(const char * old_hostname, const char * new_hostname)
  3870. {
  3871. mdns_srv_item_t * service = _mdns_server->services;
  3872. while (service) {
  3873. if (strcmp(service->service->hostname, old_hostname) == 0) {
  3874. free((char *)service->service->hostname);
  3875. service->service->hostname = strdup(new_hostname);
  3876. }
  3877. service = service->next;
  3878. }
  3879. }
  3880. /**
  3881. * @brief Free action data
  3882. */
  3883. static void _mdns_free_action(mdns_action_t * action)
  3884. {
  3885. switch(action->type) {
  3886. case ACTION_HOSTNAME_SET:
  3887. free(action->data.hostname_set.hostname);
  3888. break;
  3889. case ACTION_INSTANCE_SET:
  3890. free(action->data.instance);
  3891. break;
  3892. case ACTION_SERVICE_ADD:
  3893. _mdns_free_service(action->data.srv_add.service->service);
  3894. free(action->data.srv_add.service);
  3895. break;
  3896. case ACTION_SERVICE_INSTANCE_SET:
  3897. free(action->data.srv_instance.instance);
  3898. break;
  3899. case ACTION_SERVICE_TXT_REPLACE:
  3900. _mdns_free_linked_txt(action->data.srv_txt_replace.txt);
  3901. break;
  3902. case ACTION_SERVICE_TXT_SET:
  3903. free(action->data.srv_txt_set.key);
  3904. free(action->data.srv_txt_set.value);
  3905. break;
  3906. case ACTION_SERVICE_TXT_DEL:
  3907. free(action->data.srv_txt_del.key);
  3908. break;
  3909. case ACTION_SEARCH_ADD:
  3910. //fallthrough
  3911. case ACTION_SEARCH_SEND:
  3912. //fallthrough
  3913. case ACTION_SEARCH_END:
  3914. _mdns_search_free(action->data.search_add.search);
  3915. break;
  3916. case ACTION_TX_HANDLE:
  3917. _mdns_free_tx_packet(action->data.tx_handle.packet);
  3918. break;
  3919. case ACTION_RX_HANDLE:
  3920. _mdns_packet_free(action->data.rx_handle.packet);
  3921. break;
  3922. case ACTION_DELEGATE_HOSTNAME_ADD:
  3923. free((char *)action->data.delegate_hostname.hostname);
  3924. free_address_list(action->data.delegate_hostname.address_list);
  3925. break;
  3926. case ACTION_DELEGATE_HOSTNAME_REMOVE:
  3927. free((char *)action->data.delegate_hostname.hostname);
  3928. break;
  3929. default:
  3930. break;
  3931. }
  3932. free(action);
  3933. }
  3934. /**
  3935. * @brief Called from service thread to execute given action
  3936. */
  3937. static void _mdns_execute_action(mdns_action_t * action)
  3938. {
  3939. mdns_srv_item_t * a = NULL;
  3940. mdns_service_t * service;
  3941. char * key;
  3942. char * value;
  3943. mdns_txt_linked_item_t * txt, * t;
  3944. switch(action->type) {
  3945. case ACTION_SYSTEM_EVENT:
  3946. _mdns_handle_system_event(action->data.sys_event.event_base,
  3947. action->data.sys_event.event_id, action->data.sys_event.interface);
  3948. break;
  3949. case ACTION_HOSTNAME_SET:
  3950. _mdns_send_bye_all_pcbs_no_instance(true);
  3951. _mdns_remap_self_service_hostname(_mdns_server->hostname, action->data.hostname_set.hostname);
  3952. free((char*)_mdns_server->hostname);
  3953. _mdns_server->hostname = action->data.hostname_set.hostname;
  3954. _mdns_self_host.hostname = action->data.hostname_set.hostname;
  3955. _mdns_restart_all_pcbs();
  3956. xTaskNotifyGive(action->data.hostname_set.calling_task);
  3957. break;
  3958. case ACTION_INSTANCE_SET:
  3959. _mdns_send_bye_all_pcbs_no_instance(false);
  3960. free((char*)_mdns_server->instance);
  3961. _mdns_server->instance = action->data.instance;
  3962. _mdns_restart_all_pcbs_no_instance();
  3963. break;
  3964. case ACTION_SERVICE_ADD:
  3965. action->data.srv_add.service->next = _mdns_server->services;
  3966. _mdns_server->services = action->data.srv_add.service;
  3967. _mdns_probe_all_pcbs(&action->data.srv_add.service, 1, false, false);
  3968. break;
  3969. case ACTION_SERVICE_INSTANCE_SET:
  3970. if (action->data.srv_instance.service->service->instance) {
  3971. _mdns_send_bye(&action->data.srv_instance.service, 1, false);
  3972. free((char*)action->data.srv_instance.service->service->instance);
  3973. }
  3974. action->data.srv_instance.service->service->instance = action->data.srv_instance.instance;
  3975. _mdns_probe_all_pcbs(&action->data.srv_instance.service, 1, false, false);
  3976. break;
  3977. case ACTION_SERVICE_PORT_SET:
  3978. action->data.srv_port.service->service->port = action->data.srv_port.port;
  3979. _mdns_announce_all_pcbs(&action->data.srv_port.service, 1, true);
  3980. break;
  3981. case ACTION_SERVICE_TXT_REPLACE:
  3982. service = action->data.srv_txt_replace.service->service;
  3983. txt = service->txt;
  3984. service->txt = NULL;
  3985. _mdns_free_linked_txt(txt);
  3986. service->txt = action->data.srv_txt_replace.txt;
  3987. _mdns_announce_all_pcbs(&action->data.srv_txt_replace.service, 1, false);
  3988. break;
  3989. case ACTION_SERVICE_TXT_SET:
  3990. service = action->data.srv_txt_set.service->service;
  3991. key = action->data.srv_txt_set.key;
  3992. value = action->data.srv_txt_set.value;
  3993. txt = service->txt;
  3994. while (txt) {
  3995. if (strcmp(txt->key, key) == 0) {
  3996. free((char *)txt->value);
  3997. free(key);
  3998. txt->value = value;
  3999. txt->value_len = action->data.srv_txt_set.value_len;
  4000. break;
  4001. }
  4002. txt = txt->next;
  4003. }
  4004. if (!txt) {
  4005. txt = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
  4006. if (!txt) {
  4007. HOOK_MALLOC_FAILED;
  4008. _mdns_free_action(action);
  4009. return;
  4010. }
  4011. txt->key = key;
  4012. txt->value = value;
  4013. txt->value_len = action->data.srv_txt_set.value_len;
  4014. txt->next = service->txt;
  4015. service->txt = txt;
  4016. }
  4017. _mdns_announce_all_pcbs(&action->data.srv_txt_set.service, 1, false);
  4018. break;
  4019. case ACTION_SERVICE_TXT_DEL:
  4020. service = action->data.srv_txt_del.service->service;
  4021. key = action->data.srv_txt_del.key;
  4022. txt = service->txt;
  4023. if (!txt) {
  4024. break;
  4025. }
  4026. if (strcmp(txt->key, key) == 0) {
  4027. service->txt = txt->next;
  4028. free((char *)txt->key);
  4029. free((char *)txt->value);
  4030. free(txt);
  4031. } else {
  4032. while (txt->next) {
  4033. if (strcmp(txt->next->key, key) == 0) {
  4034. t = txt->next;
  4035. txt->next = t->next;
  4036. free((char *)t->key);
  4037. free((char *)t->value);
  4038. free(t);
  4039. break;
  4040. } else {
  4041. txt = txt->next;
  4042. }
  4043. }
  4044. }
  4045. free(key);
  4046. _mdns_announce_all_pcbs(&action->data.srv_txt_set.service, 1, false);
  4047. break;
  4048. case ACTION_SERVICE_DEL:
  4049. a = _mdns_server->services;
  4050. if (action->data.srv_del.service) {
  4051. if (_mdns_server->services == action->data.srv_del.service) {
  4052. _mdns_server->services = a->next;
  4053. _mdns_send_bye(&a, 1, false);
  4054. _mdns_remove_scheduled_service_packets(a->service);
  4055. _mdns_free_service(a->service);
  4056. free(a);
  4057. } else {
  4058. while (a->next && a->next != action->data.srv_del.service) {
  4059. a = a->next;
  4060. }
  4061. if (a->next == action->data.srv_del.service) {
  4062. mdns_srv_item_t * b = a->next;
  4063. a->next = a->next->next;
  4064. _mdns_send_bye(&b, 1, false);
  4065. _mdns_remove_scheduled_service_packets(b->service);
  4066. _mdns_free_service(b->service);
  4067. free(b);
  4068. }
  4069. }
  4070. }
  4071. break;
  4072. case ACTION_SERVICES_CLEAR:
  4073. _mdns_send_final_bye(false);
  4074. a = _mdns_server->services;
  4075. _mdns_server->services = NULL;
  4076. while (a) {
  4077. mdns_srv_item_t * s = a;
  4078. a = a->next;
  4079. _mdns_remove_scheduled_service_packets(s->service);
  4080. _mdns_free_service(s->service);
  4081. free(s);
  4082. }
  4083. break;
  4084. case ACTION_SEARCH_ADD:
  4085. _mdns_search_add(action->data.search_add.search);
  4086. break;
  4087. case ACTION_SEARCH_SEND:
  4088. _mdns_search_send(action->data.search_add.search);
  4089. break;
  4090. case ACTION_SEARCH_END:
  4091. _mdns_search_finish(action->data.search_add.search);
  4092. break;
  4093. case ACTION_TX_HANDLE:
  4094. {
  4095. mdns_tx_packet_t * p = _mdns_server->tx_queue_head;
  4096. // packet to be handled should be at tx head, but must be consistent with the one pushed to action queue
  4097. if (p && p==action->data.tx_handle.packet && p->queued) {
  4098. p->queued = false; // clearing, as the packet might be reused (pushed and transmitted again)
  4099. _mdns_server->tx_queue_head = p->next;
  4100. _mdns_tx_handle_packet(p);
  4101. } else {
  4102. ESP_LOGD(TAG, "Skipping transmit of an unexpected packet!");
  4103. }
  4104. }
  4105. break;
  4106. case ACTION_RX_HANDLE:
  4107. mdns_parse_packet(action->data.rx_handle.packet);
  4108. _mdns_packet_free(action->data.rx_handle.packet);
  4109. break;
  4110. case ACTION_DELEGATE_HOSTNAME_ADD:
  4111. _mdns_delegate_hostname_add(action->data.delegate_hostname.hostname,
  4112. action->data.delegate_hostname.address_list);
  4113. break;
  4114. case ACTION_DELEGATE_HOSTNAME_REMOVE:
  4115. _mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname);
  4116. free((char *)action->data.delegate_hostname.hostname);
  4117. break;
  4118. default:
  4119. break;
  4120. }
  4121. free(action);
  4122. }
  4123. /**
  4124. * @brief Queue search action
  4125. */
  4126. static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t * search)
  4127. {
  4128. mdns_action_t * action = NULL;
  4129. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4130. if (!action) {
  4131. HOOK_MALLOC_FAILED;
  4132. return ESP_ERR_NO_MEM;
  4133. }
  4134. action->type = type;
  4135. action->data.search_add.search = search;
  4136. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4137. free(action);
  4138. return ESP_ERR_NO_MEM;
  4139. }
  4140. return ESP_OK;
  4141. }
  4142. /**
  4143. * @brief Called from timer task to run mDNS responder
  4144. *
  4145. * periodically checks first unqueued packet (from tx head).
  4146. * if it is scheduled to be transmitted, then pushes the packet to action queue to be handled.
  4147. *
  4148. */
  4149. static void _mdns_scheduler_run(void)
  4150. {
  4151. MDNS_SERVICE_LOCK();
  4152. mdns_tx_packet_t * p = _mdns_server->tx_queue_head;
  4153. mdns_action_t * action = NULL;
  4154. // find first unqueued packet
  4155. while (p && p->queued) {
  4156. p = p->next;
  4157. }
  4158. if (!p) {
  4159. MDNS_SERVICE_UNLOCK();
  4160. return;
  4161. }
  4162. if ((int32_t)(p->send_at - (xTaskGetTickCount() * portTICK_PERIOD_MS)) < 0) {
  4163. action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4164. if (action) {
  4165. action->type = ACTION_TX_HANDLE;
  4166. action->data.tx_handle.packet = p;
  4167. p->queued = true;
  4168. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4169. free(action);
  4170. p->queued = false;
  4171. }
  4172. } else {
  4173. HOOK_MALLOC_FAILED;
  4174. // continue
  4175. }
  4176. }
  4177. MDNS_SERVICE_UNLOCK();
  4178. }
  4179. /**
  4180. * @brief Called from timer task to run active searches
  4181. */
  4182. static void _mdns_search_run(void)
  4183. {
  4184. MDNS_SERVICE_LOCK();
  4185. mdns_search_once_t * s = _mdns_server->search_once;
  4186. uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS;
  4187. if (!s) {
  4188. MDNS_SERVICE_UNLOCK();
  4189. return;
  4190. }
  4191. while (s) {
  4192. if (s->state != SEARCH_OFF) {
  4193. if (now > (s->started_at + s->timeout)) {
  4194. s->state = SEARCH_OFF;
  4195. if (_mdns_send_search_action(ACTION_SEARCH_END, s) != ESP_OK) {
  4196. s->state = SEARCH_RUNNING;
  4197. }
  4198. } else if (s->state == SEARCH_INIT || (now - s->sent_at) > 1000) {
  4199. s->state = SEARCH_RUNNING;
  4200. s->sent_at = now;
  4201. if (_mdns_send_search_action(ACTION_SEARCH_SEND, s) != ESP_OK) {
  4202. s->sent_at -= 1000;
  4203. }
  4204. }
  4205. }
  4206. s = s->next;
  4207. }
  4208. MDNS_SERVICE_UNLOCK();
  4209. }
  4210. /**
  4211. * @brief the main MDNS service task. Packets are received and parsed here
  4212. */
  4213. static void _mdns_service_task(void *pvParameters)
  4214. {
  4215. mdns_action_t * a = NULL;
  4216. for (;;) {
  4217. if (_mdns_server && _mdns_server->action_queue) {
  4218. if (xQueueReceive(_mdns_server->action_queue, &a, portMAX_DELAY) == pdTRUE) {
  4219. if (a->type == ACTION_TASK_STOP) {
  4220. break;
  4221. }
  4222. MDNS_SERVICE_LOCK();
  4223. _mdns_execute_action(a);
  4224. MDNS_SERVICE_UNLOCK();
  4225. }
  4226. } else {
  4227. vTaskDelay(500 * portTICK_PERIOD_MS);
  4228. }
  4229. }
  4230. _mdns_service_task_handle = NULL;
  4231. vTaskDelete(NULL);
  4232. }
  4233. static void _mdns_timer_cb(void * arg)
  4234. {
  4235. _mdns_scheduler_run();
  4236. _mdns_search_run();
  4237. }
  4238. static esp_err_t _mdns_start_timer(void){
  4239. esp_timer_create_args_t timer_conf = {
  4240. .callback = _mdns_timer_cb,
  4241. .arg = NULL,
  4242. .dispatch_method = ESP_TIMER_TASK,
  4243. .name = "mdns_timer"
  4244. };
  4245. esp_err_t err = esp_timer_create(&timer_conf, &(_mdns_server->timer_handle));
  4246. if (err) {
  4247. return err;
  4248. }
  4249. return esp_timer_start_periodic(_mdns_server->timer_handle, MDNS_TIMER_PERIOD_US);
  4250. }
  4251. static esp_err_t _mdns_stop_timer(void){
  4252. esp_err_t err = ESP_OK;
  4253. if (_mdns_server->timer_handle) {
  4254. err = esp_timer_stop(_mdns_server->timer_handle);
  4255. if (err) {
  4256. return err;
  4257. }
  4258. err = esp_timer_delete(_mdns_server->timer_handle);
  4259. }
  4260. return err;
  4261. }
  4262. /**
  4263. * @brief Start the service thread if not running
  4264. *
  4265. * @return
  4266. * - ESP_OK on success
  4267. * - ESP_FAIL on error
  4268. */
  4269. static esp_err_t _mdns_service_task_start(void)
  4270. {
  4271. if (!_mdns_service_semaphore) {
  4272. _mdns_service_semaphore = xSemaphoreCreateMutex();
  4273. if (!_mdns_service_semaphore) {
  4274. return ESP_FAIL;
  4275. }
  4276. }
  4277. MDNS_SERVICE_LOCK();
  4278. if (_mdns_start_timer()) {
  4279. MDNS_SERVICE_UNLOCK();
  4280. return ESP_FAIL;
  4281. }
  4282. if (!_mdns_service_task_handle) {
  4283. xTaskCreatePinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY,
  4284. (TaskHandle_t * const)(&_mdns_service_task_handle), MDNS_TASK_AFFINITY);
  4285. if (!_mdns_service_task_handle) {
  4286. _mdns_stop_timer();
  4287. MDNS_SERVICE_UNLOCK();
  4288. vSemaphoreDelete(_mdns_service_semaphore);
  4289. _mdns_service_semaphore = NULL;
  4290. return ESP_FAIL;
  4291. }
  4292. }
  4293. MDNS_SERVICE_UNLOCK();
  4294. return ESP_OK;
  4295. }
  4296. /**
  4297. * @brief Stop the service thread
  4298. *
  4299. * @return
  4300. * - ESP_OK
  4301. */
  4302. static esp_err_t _mdns_service_task_stop(void)
  4303. {
  4304. _mdns_stop_timer();
  4305. if (_mdns_service_task_handle) {
  4306. mdns_action_t action;
  4307. mdns_action_t * a = &action;
  4308. action.type = ACTION_TASK_STOP;
  4309. if (xQueueSend(_mdns_server->action_queue, &a, (portTickType)0) != pdPASS) {
  4310. vTaskDelete(_mdns_service_task_handle);
  4311. _mdns_service_task_handle = NULL;
  4312. }
  4313. while (_mdns_service_task_handle) {
  4314. vTaskDelay(10 / portTICK_PERIOD_MS);
  4315. }
  4316. }
  4317. vSemaphoreDelete(_mdns_service_semaphore);
  4318. _mdns_service_semaphore = NULL;
  4319. return ESP_OK;
  4320. }
  4321. /*
  4322. * Public Methods
  4323. * */
  4324. esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event)
  4325. {
  4326. /* no-op, kept for compatibility */
  4327. return ESP_OK;
  4328. }
  4329. static void event_handler(void* arg, esp_event_base_t event_base,
  4330. int32_t event_id, void* event_data)
  4331. {
  4332. if (!_mdns_server) {
  4333. return;
  4334. }
  4335. mdns_action_t * action = (mdns_action_t *)calloc(1, sizeof(mdns_action_t));
  4336. if (!action) {
  4337. HOOK_MALLOC_FAILED;
  4338. return;
  4339. }
  4340. action->type = ACTION_SYSTEM_EVENT;
  4341. action->data.sys_event.event_base = event_base;
  4342. action->data.sys_event.event_id = event_id;
  4343. if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
  4344. ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data;
  4345. action->data.sys_event.interface = event->esp_netif;
  4346. }
  4347. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4348. free(action);
  4349. }
  4350. }
  4351. esp_err_t mdns_init(void)
  4352. {
  4353. esp_err_t err = ESP_OK;
  4354. if (_mdns_server) {
  4355. return err;
  4356. }
  4357. _mdns_server = (mdns_server_t *)malloc(sizeof(mdns_server_t));
  4358. if (!_mdns_server) {
  4359. HOOK_MALLOC_FAILED;
  4360. return ESP_ERR_NO_MEM;
  4361. }
  4362. memset((uint8_t*)_mdns_server, 0, sizeof(mdns_server_t));
  4363. // zero-out local copy of netifs to initiate a fresh search by interface key whenever a netif ptr is needed
  4364. memset(s_esp_netifs, 0, sizeof(s_esp_netifs));
  4365. _mdns_server->lock = xSemaphoreCreateMutex();
  4366. if (!_mdns_server->lock) {
  4367. err = ESP_ERR_NO_MEM;
  4368. goto free_server;
  4369. }
  4370. _mdns_server->action_queue = xQueueCreate(MDNS_ACTION_QUEUE_LEN, sizeof(mdns_action_t *));
  4371. if (!_mdns_server->action_queue) {
  4372. err = ESP_ERR_NO_MEM;
  4373. goto free_lock;
  4374. }
  4375. if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) {
  4376. goto free_event_handlers;
  4377. }
  4378. if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) {
  4379. goto free_event_handlers;
  4380. }
  4381. #if CONFIG_ETH_ENABLED
  4382. if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) {
  4383. goto free_event_handlers;
  4384. }
  4385. #endif
  4386. uint8_t i;
  4387. #if CONFIG_LWIP_IPV6
  4388. esp_ip6_addr_t tmp_addr6;
  4389. #endif
  4390. esp_netif_ip_info_t if_ip_info;
  4391. for (i=0; i<MDNS_IF_MAX; i++) {
  4392. #if CONFIG_LWIP_IPV6
  4393. if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
  4394. _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
  4395. }
  4396. #endif
  4397. if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
  4398. _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
  4399. }
  4400. }
  4401. if (_mdns_service_task_start()) {
  4402. //service start failed!
  4403. err = ESP_FAIL;
  4404. goto free_all_and_disable_pcbs;
  4405. }
  4406. return ESP_OK;
  4407. free_all_and_disable_pcbs:
  4408. for (i=0; i<MDNS_IF_MAX; i++) {
  4409. _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V6);
  4410. _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4);
  4411. }
  4412. free_event_handlers:
  4413. esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler);
  4414. esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler);
  4415. #if CONFIG_ETH_ENABLED
  4416. esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler);
  4417. #endif
  4418. vQueueDelete(_mdns_server->action_queue);
  4419. free_lock:
  4420. vSemaphoreDelete(_mdns_server->lock);
  4421. free_server:
  4422. free(_mdns_server);
  4423. _mdns_server = NULL;
  4424. return err;
  4425. }
  4426. void mdns_free(void)
  4427. {
  4428. uint8_t i, j;
  4429. if (!_mdns_server) {
  4430. return;
  4431. }
  4432. // Unregister handlers before destroying the mdns internals to avoid receiving async events while deinit
  4433. esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler);
  4434. esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler);
  4435. #if CONFIG_ETH_ENABLED
  4436. esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler);
  4437. #endif
  4438. mdns_service_remove_all();
  4439. _mdns_service_task_stop();
  4440. for (i=0; i<MDNS_IF_MAX; i++) {
  4441. for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
  4442. _mdns_pcb_deinit(i, j);
  4443. }
  4444. }
  4445. free((char*)_mdns_server->hostname);
  4446. free((char*)_mdns_server->instance);
  4447. if (_mdns_server->action_queue) {
  4448. mdns_action_t * c;
  4449. while (xQueueReceive(_mdns_server->action_queue, &c, 0) == pdTRUE) {
  4450. _mdns_free_action(c);
  4451. }
  4452. vQueueDelete(_mdns_server->action_queue);
  4453. }
  4454. _mdns_clear_tx_queue_head();
  4455. while (_mdns_server->search_once) {
  4456. mdns_search_once_t * h = _mdns_server->search_once;
  4457. _mdns_server->search_once = h->next;
  4458. free(h->instance);
  4459. free(h->service);
  4460. free(h->proto);
  4461. vSemaphoreDelete(h->done_semaphore);
  4462. if (h->result) {
  4463. mdns_query_results_free(h->result);
  4464. }
  4465. free(h);
  4466. }
  4467. vSemaphoreDelete(_mdns_server->lock);
  4468. free(_mdns_server);
  4469. _mdns_server = NULL;
  4470. }
  4471. esp_err_t mdns_hostname_set(const char * hostname)
  4472. {
  4473. if (!_mdns_server) {
  4474. return ESP_ERR_INVALID_ARG;
  4475. }
  4476. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
  4477. return ESP_ERR_INVALID_ARG;
  4478. }
  4479. char * new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  4480. if (!new_hostname) {
  4481. return ESP_ERR_NO_MEM;
  4482. }
  4483. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4484. if (!action) {
  4485. HOOK_MALLOC_FAILED;
  4486. free(new_hostname);
  4487. return ESP_ERR_NO_MEM;
  4488. }
  4489. action->type = ACTION_HOSTNAME_SET;
  4490. action->data.hostname_set.hostname = new_hostname;
  4491. action->data.hostname_set.calling_task = xTaskGetCurrentTaskHandle();
  4492. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4493. free(new_hostname);
  4494. free(action);
  4495. return ESP_ERR_NO_MEM;
  4496. }
  4497. xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
  4498. return ESP_OK;
  4499. }
  4500. esp_err_t mdns_delegate_hostname_add(const char * hostname, const mdns_ip_addr_t * address_list)
  4501. {
  4502. if (!_mdns_server) {
  4503. return ESP_ERR_INVALID_STATE;
  4504. }
  4505. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1) || address_list == NULL) {
  4506. return ESP_ERR_INVALID_ARG;
  4507. }
  4508. char * new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  4509. if (!new_hostname) {
  4510. return ESP_ERR_NO_MEM;
  4511. }
  4512. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4513. if (!action) {
  4514. HOOK_MALLOC_FAILED;
  4515. free(new_hostname);
  4516. return ESP_ERR_NO_MEM;
  4517. }
  4518. action->type = ACTION_DELEGATE_HOSTNAME_ADD;
  4519. action->data.delegate_hostname.hostname = new_hostname;
  4520. action->data.delegate_hostname.address_list = copy_address_list(address_list);
  4521. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4522. free(new_hostname);
  4523. free(action);
  4524. return ESP_ERR_NO_MEM;
  4525. }
  4526. return ESP_OK;
  4527. }
  4528. esp_err_t mdns_delegate_hostname_remove(const char * hostname)
  4529. {
  4530. if (!_mdns_server) {
  4531. return ESP_ERR_INVALID_STATE;
  4532. }
  4533. if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
  4534. return ESP_ERR_INVALID_ARG;
  4535. }
  4536. char * new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
  4537. if (!new_hostname) {
  4538. return ESP_ERR_NO_MEM;
  4539. }
  4540. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4541. if (!action) {
  4542. HOOK_MALLOC_FAILED;
  4543. free(new_hostname);
  4544. return ESP_ERR_NO_MEM;
  4545. }
  4546. action->type = ACTION_DELEGATE_HOSTNAME_REMOVE;
  4547. action->data.delegate_hostname.hostname = new_hostname;
  4548. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4549. free(new_hostname);
  4550. free(action);
  4551. return ESP_ERR_NO_MEM;
  4552. }
  4553. return ESP_OK;
  4554. }
  4555. bool mdns_hostname_exists(const char * hostname)
  4556. {
  4557. return _hostname_is_ours(hostname);
  4558. }
  4559. esp_err_t mdns_instance_name_set(const char * instance)
  4560. {
  4561. if (!_mdns_server) {
  4562. return ESP_ERR_INVALID_STATE;
  4563. }
  4564. if (_str_null_or_empty(instance) || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
  4565. return ESP_ERR_INVALID_ARG;
  4566. }
  4567. char * new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
  4568. if (!new_instance) {
  4569. return ESP_ERR_NO_MEM;
  4570. }
  4571. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4572. if (!action) {
  4573. HOOK_MALLOC_FAILED;
  4574. free(new_instance);
  4575. return ESP_ERR_NO_MEM;
  4576. }
  4577. action->type = ACTION_INSTANCE_SET;
  4578. action->data.instance = new_instance;
  4579. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4580. free(new_instance);
  4581. free(action);
  4582. return ESP_ERR_NO_MEM;
  4583. }
  4584. return ESP_OK;
  4585. }
  4586. /*
  4587. * MDNS SERVICES
  4588. * */
  4589. esp_err_t mdns_service_add_for_host(const char * instance, const char * service, const char * proto, const char * hostname,
  4590. uint16_t port, mdns_txt_item_t txt[], size_t num_items)
  4591. {
  4592. if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
  4593. return ESP_ERR_INVALID_ARG;
  4594. }
  4595. if (!_mdns_can_add_more_services()) {
  4596. return ESP_ERR_NO_MEM;
  4597. }
  4598. #if CONFIG_MDNS_MULTIPLE_INSTANCE
  4599. mdns_srv_item_t * item = _mdns_get_service_item_instance(instance, service, proto, hostname);
  4600. #else
  4601. mdns_srv_item_t * item = _mdns_get_service_item(service, proto, hostname);
  4602. #endif // CONFIG_MDNS_MULTIPLE_INSTANCE
  4603. if (item) {
  4604. return ESP_ERR_INVALID_ARG;
  4605. }
  4606. mdns_service_t * s = _mdns_create_service(service, proto, hostname, port, instance, num_items, txt);
  4607. if (!s) {
  4608. return ESP_ERR_NO_MEM;
  4609. }
  4610. item = (mdns_srv_item_t *)malloc(sizeof(mdns_srv_item_t));
  4611. if (!item) {
  4612. HOOK_MALLOC_FAILED;
  4613. _mdns_free_service(s);
  4614. return ESP_ERR_NO_MEM;
  4615. }
  4616. item->service = s;
  4617. item->next = NULL;
  4618. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4619. if (!action) {
  4620. HOOK_MALLOC_FAILED;
  4621. _mdns_free_service(s);
  4622. free(item);
  4623. return ESP_ERR_NO_MEM;
  4624. }
  4625. action->type = ACTION_SERVICE_ADD;
  4626. action->data.srv_add.service = item;
  4627. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4628. _mdns_free_service(s);
  4629. free(item);
  4630. free(action);
  4631. return ESP_ERR_NO_MEM;
  4632. }
  4633. size_t start = xTaskGetTickCount();
  4634. size_t timeout_ticks = pdMS_TO_TICKS(MDNS_SERVICE_ADD_TIMEOUT_MS);
  4635. while (_mdns_get_service_item(service, proto, hostname) == NULL) {
  4636. uint32_t expired = xTaskGetTickCount() - start;
  4637. if (expired >= timeout_ticks) {
  4638. return ESP_FAIL; // Timeout
  4639. }
  4640. vTaskDelay(MIN(10 / portTICK_RATE_MS, timeout_ticks - expired));
  4641. }
  4642. return ESP_OK;
  4643. }
  4644. esp_err_t mdns_service_add(const char * instance, const char * service, const char * proto, uint16_t port,
  4645. mdns_txt_item_t txt[], size_t num_items)
  4646. {
  4647. if (!_mdns_server) {
  4648. return ESP_ERR_INVALID_STATE;
  4649. }
  4650. return mdns_service_add_for_host(instance, service, proto, _mdns_server->hostname, port, txt, num_items);
  4651. }
  4652. bool mdns_service_exists(const char * service_type, const char * proto, const char * hostname)
  4653. {
  4654. return _mdns_get_service_item(service_type, proto, hostname) != NULL;
  4655. }
  4656. bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto,
  4657. const char *hostname)
  4658. {
  4659. return _mdns_get_service_item_instance(instance, service_type, proto, hostname) != NULL;
  4660. }
  4661. esp_err_t mdns_service_port_set_for_host(const char * service, const char * proto, const char * hostname, uint16_t port)
  4662. {
  4663. if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
  4664. return ESP_ERR_INVALID_ARG;
  4665. }
  4666. mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
  4667. if (!s) {
  4668. return ESP_ERR_NOT_FOUND;
  4669. }
  4670. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4671. if (!action) {
  4672. HOOK_MALLOC_FAILED;
  4673. return ESP_ERR_NO_MEM;
  4674. }
  4675. action->type = ACTION_SERVICE_PORT_SET;
  4676. action->data.srv_port.service = s;
  4677. action->data.srv_port.port = port;
  4678. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4679. free(action);
  4680. return ESP_ERR_NO_MEM;
  4681. }
  4682. return ESP_OK;
  4683. }
  4684. esp_err_t mdns_service_port_set(const char * service, const char * proto, uint16_t port)
  4685. {
  4686. if (!_mdns_server) {
  4687. return ESP_ERR_INVALID_STATE;
  4688. }
  4689. return mdns_service_port_set_for_host(service, proto, _mdns_server->hostname, port);
  4690. }
  4691. esp_err_t mdns_service_txt_set_for_host(const char * service, const char * proto, const char * hostname,
  4692. mdns_txt_item_t txt[], uint8_t num_items)
  4693. {
  4694. if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || (num_items && txt == NULL)) {
  4695. return ESP_ERR_INVALID_ARG;
  4696. }
  4697. mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
  4698. if (!s) {
  4699. return ESP_ERR_NOT_FOUND;
  4700. }
  4701. mdns_txt_linked_item_t * new_txt = NULL;
  4702. if (num_items){
  4703. new_txt = _mdns_allocate_txt(num_items, txt);
  4704. if (!new_txt) {
  4705. return ESP_ERR_NO_MEM;
  4706. }
  4707. }
  4708. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4709. if (!action) {
  4710. HOOK_MALLOC_FAILED;
  4711. _mdns_free_linked_txt(new_txt);
  4712. return ESP_ERR_NO_MEM;
  4713. }
  4714. action->type = ACTION_SERVICE_TXT_REPLACE;
  4715. action->data.srv_txt_replace.service = s;
  4716. action->data.srv_txt_replace.txt = new_txt;
  4717. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4718. _mdns_free_linked_txt(new_txt);
  4719. free(action);
  4720. return ESP_ERR_NO_MEM;
  4721. }
  4722. return ESP_OK;
  4723. }
  4724. esp_err_t mdns_service_txt_set(const char * service, const char * proto, mdns_txt_item_t txt[], uint8_t num_items)
  4725. {
  4726. if (!_mdns_server) {
  4727. return ESP_ERR_INVALID_STATE;
  4728. }
  4729. return mdns_service_txt_set_for_host(service, proto, _mdns_server->hostname, txt, num_items);
  4730. }
  4731. esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *service, const char *proto,
  4732. const char *hostname, const char *key,
  4733. const char *value, uint8_t value_len)
  4734. {
  4735. if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) ||
  4736. _str_null_or_empty(key) || !value) {
  4737. return ESP_ERR_INVALID_ARG;
  4738. }
  4739. mdns_srv_item_t *s = _mdns_get_service_item(service, proto, hostname);
  4740. if (!s) {
  4741. return ESP_ERR_NOT_FOUND;
  4742. }
  4743. mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4744. if (!action) {
  4745. HOOK_MALLOC_FAILED;
  4746. return ESP_ERR_NO_MEM;
  4747. }
  4748. action->type = ACTION_SERVICE_TXT_SET;
  4749. action->data.srv_txt_set.service = s;
  4750. action->data.srv_txt_set.key = strdup(key);
  4751. if (!action->data.srv_txt_set.key) {
  4752. free(action);
  4753. return ESP_ERR_NO_MEM;
  4754. }
  4755. action->data.srv_txt_set.value = (char *)malloc(value_len);
  4756. if (!action->data.srv_txt_set.value) {
  4757. free(action->data.srv_txt_set.key);
  4758. free(action);
  4759. return ESP_ERR_NO_MEM;
  4760. }
  4761. memcpy(action->data.srv_txt_set.value, value, value_len);
  4762. action->data.srv_txt_set.value_len = value_len;
  4763. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4764. free(action->data.srv_txt_set.key);
  4765. free(action->data.srv_txt_set.value);
  4766. free(action);
  4767. return ESP_ERR_NO_MEM;
  4768. }
  4769. return ESP_OK;
  4770. }
  4771. esp_err_t mdns_service_txt_item_set_for_host(const char *service, const char *proto, const char *hostname,
  4772. const char *key, const char *value)
  4773. {
  4774. return mdns_service_txt_item_set_for_host_with_explicit_value_len(service, proto, hostname, key, value,
  4775. strlen(value));
  4776. }
  4777. esp_err_t mdns_service_txt_item_set(const char *service, const char *proto, const char *key, const char *value)
  4778. {
  4779. if (!_mdns_server) {
  4780. return ESP_ERR_INVALID_STATE;
  4781. }
  4782. return mdns_service_txt_item_set_for_host_with_explicit_value_len(service, proto, _mdns_server->hostname, key,
  4783. value, strlen(value));
  4784. }
  4785. esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service, const char *proto, const char *key,
  4786. const char *value, uint8_t value_len)
  4787. {
  4788. if (!_mdns_server) {
  4789. return ESP_ERR_INVALID_STATE;
  4790. }
  4791. return mdns_service_txt_item_set_for_host_with_explicit_value_len(service, proto, _mdns_server->hostname, key,
  4792. value, value_len);
  4793. }
  4794. esp_err_t mdns_service_txt_item_remove_for_host(const char * service, const char * proto, const char * hostname,
  4795. const char * key)
  4796. {
  4797. if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || _str_null_or_empty(key)) {
  4798. return ESP_ERR_INVALID_ARG;
  4799. }
  4800. mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
  4801. if (!s) {
  4802. return ESP_ERR_NOT_FOUND;
  4803. }
  4804. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4805. if (!action) {
  4806. HOOK_MALLOC_FAILED;
  4807. return ESP_ERR_NO_MEM;
  4808. }
  4809. action->type = ACTION_SERVICE_TXT_DEL;
  4810. action->data.srv_txt_del.service = s;
  4811. action->data.srv_txt_del.key = strdup(key);
  4812. if (!action->data.srv_txt_del.key) {
  4813. free(action);
  4814. return ESP_ERR_NO_MEM;
  4815. }
  4816. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4817. free(action->data.srv_txt_del.key);
  4818. free(action);
  4819. return ESP_ERR_NO_MEM;
  4820. }
  4821. return ESP_OK;
  4822. }
  4823. esp_err_t mdns_service_txt_item_remove(const char * service, const char * proto, const char * key)
  4824. {
  4825. if (!_mdns_server) {
  4826. return ESP_ERR_INVALID_STATE;
  4827. }
  4828. return mdns_service_txt_item_remove_for_host(service, proto, _mdns_server->hostname, key);
  4829. }
  4830. esp_err_t mdns_service_instance_name_set_for_host(const char * service, const char * proto, const char * hostname,
  4831. const char * instance)
  4832. {
  4833. if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  4834. return ESP_ERR_INVALID_ARG;
  4835. }
  4836. if (_str_null_or_empty(instance) || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
  4837. return ESP_ERR_INVALID_ARG;
  4838. }
  4839. mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
  4840. if (!s) {
  4841. return ESP_ERR_NOT_FOUND;
  4842. }
  4843. char * new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
  4844. if (!new_instance) {
  4845. return ESP_ERR_NO_MEM;
  4846. }
  4847. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4848. if (!action) {
  4849. HOOK_MALLOC_FAILED;
  4850. free(new_instance);
  4851. return ESP_ERR_NO_MEM;
  4852. }
  4853. action->type = ACTION_SERVICE_INSTANCE_SET;
  4854. action->data.srv_instance.service = s;
  4855. action->data.srv_instance.instance = new_instance;
  4856. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4857. free(new_instance);
  4858. free(action);
  4859. return ESP_ERR_NO_MEM;
  4860. }
  4861. return ESP_OK;
  4862. }
  4863. esp_err_t mdns_service_instance_name_set(const char * service, const char * proto, const char * instance)
  4864. {
  4865. if (!_mdns_server) {
  4866. return ESP_ERR_INVALID_STATE;
  4867. }
  4868. return mdns_service_instance_name_set_for_host(service, proto, _mdns_server->hostname, instance);
  4869. }
  4870. esp_err_t mdns_service_remove_for_host(const char * service, const char * proto, const char * hostname)
  4871. {
  4872. if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  4873. return ESP_ERR_INVALID_ARG;
  4874. }
  4875. mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
  4876. if (!s) {
  4877. return ESP_ERR_NOT_FOUND;
  4878. }
  4879. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4880. if (!action) {
  4881. HOOK_MALLOC_FAILED;
  4882. return ESP_ERR_NO_MEM;
  4883. }
  4884. action->type = ACTION_SERVICE_DEL;
  4885. action->data.srv_del.service = s;
  4886. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4887. free(action);
  4888. return ESP_ERR_NO_MEM;
  4889. }
  4890. return ESP_OK;
  4891. }
  4892. esp_err_t mdns_service_remove(const char * service_type, const char * proto)
  4893. {
  4894. if (!_mdns_server) {
  4895. return ESP_ERR_INVALID_STATE;
  4896. }
  4897. return mdns_service_remove_for_host(service_type, proto, _mdns_server->hostname);
  4898. }
  4899. esp_err_t mdns_service_remove_all(void)
  4900. {
  4901. if (!_mdns_server) {
  4902. return ESP_ERR_INVALID_ARG;
  4903. }
  4904. if (!_mdns_server->services) {
  4905. return ESP_OK;
  4906. }
  4907. mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
  4908. if (!action) {
  4909. HOOK_MALLOC_FAILED;
  4910. return ESP_ERR_NO_MEM;
  4911. }
  4912. action->type = ACTION_SERVICES_CLEAR;
  4913. if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
  4914. free(action);
  4915. return ESP_ERR_NO_MEM;
  4916. }
  4917. return ESP_OK;
  4918. }
  4919. /*
  4920. * MDNS QUERY
  4921. * */
  4922. void mdns_query_results_free(mdns_result_t * results)
  4923. {
  4924. mdns_result_t * r;
  4925. mdns_ip_addr_t * a;
  4926. while (results) {
  4927. r = results;
  4928. free((char *)(r->hostname));
  4929. free((char *)(r->instance_name));
  4930. free((char *)(r->service_type));
  4931. free((char *)(r->proto));
  4932. for (size_t i=0; i<r->txt_count; i++) {
  4933. free((char *)(r->txt[i].key));
  4934. free((char *)(r->txt[i].value));
  4935. }
  4936. free(r->txt);
  4937. free(r->txt_value_len);
  4938. while (r->addr) {
  4939. a = r->addr;
  4940. r->addr = r->addr->next;
  4941. free(a);
  4942. }
  4943. results = results->next;
  4944. free(r);
  4945. }
  4946. }
  4947. esp_err_t mdns_query_async_delete(mdns_search_once_t* search)
  4948. {
  4949. if (!search) {
  4950. return ESP_ERR_INVALID_ARG;
  4951. }
  4952. if (search->state != SEARCH_OFF) {
  4953. return ESP_ERR_INVALID_STATE;
  4954. }
  4955. MDNS_SERVICE_LOCK();
  4956. _mdns_search_free(search);
  4957. MDNS_SERVICE_UNLOCK();
  4958. return ESP_OK;
  4959. }
  4960. bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout, mdns_result_t ** results)
  4961. {
  4962. if (xSemaphoreTake(search->done_semaphore, pdMS_TO_TICKS(timeout)) == pdTRUE) {
  4963. *results = search->result;
  4964. return true;
  4965. }
  4966. return false;
  4967. }
  4968. mdns_search_once_t *mdns_query_async_new(const char *name, const char *service, const char *proto, uint16_t type,
  4969. uint32_t timeout, size_t max_results, mdns_query_notify_t notifier)
  4970. {
  4971. mdns_search_once_t *search = NULL;
  4972. if (!_mdns_server || !timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
  4973. return NULL;
  4974. }
  4975. search = _mdns_search_init(name, service, proto, type, timeout, max_results, notifier);
  4976. if (!search) {
  4977. return NULL;
  4978. }
  4979. if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) {
  4980. _mdns_search_free(search);
  4981. return NULL;
  4982. }
  4983. return search;
  4984. }
  4985. esp_err_t mdns_query(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t ** results)
  4986. {
  4987. mdns_search_once_t * search = NULL;
  4988. *results = NULL;
  4989. if (!_mdns_server) {
  4990. return ESP_ERR_INVALID_STATE;
  4991. }
  4992. if (!timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
  4993. return ESP_ERR_INVALID_ARG;
  4994. }
  4995. search = _mdns_search_init(name, service, proto, type, timeout, max_results, NULL);
  4996. if (!search) {
  4997. return ESP_ERR_NO_MEM;
  4998. }
  4999. if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) {
  5000. _mdns_search_free(search);
  5001. return ESP_ERR_NO_MEM;
  5002. }
  5003. xSemaphoreTake(search->done_semaphore, portMAX_DELAY);
  5004. *results = search->result;
  5005. _mdns_search_free(search);
  5006. return ESP_OK;
  5007. }
  5008. esp_err_t mdns_query_ptr(const char * service, const char * proto, uint32_t timeout, size_t max_results, mdns_result_t ** results)
  5009. {
  5010. if (_str_null_or_empty(service) || _str_null_or_empty(proto)) {
  5011. return ESP_ERR_INVALID_ARG;
  5012. }
  5013. return mdns_query(NULL, service, proto, MDNS_TYPE_PTR, timeout, max_results, results);
  5014. }
  5015. esp_err_t mdns_query_srv(const char * instance, const char * service, const char * proto, uint32_t timeout, mdns_result_t ** result)
  5016. {
  5017. if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  5018. return ESP_ERR_INVALID_ARG;
  5019. }
  5020. return mdns_query(instance, service, proto, MDNS_TYPE_SRV, timeout, 1, result);
  5021. }
  5022. esp_err_t mdns_query_txt(const char * instance, const char * service, const char * proto, uint32_t timeout, mdns_result_t ** result)
  5023. {
  5024. if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
  5025. return ESP_ERR_INVALID_ARG;
  5026. }
  5027. return mdns_query(instance, service, proto, MDNS_TYPE_TXT, timeout, 1, result);
  5028. }
  5029. esp_err_t mdns_query_a(const char * name, uint32_t timeout, esp_ip4_addr_t * addr)
  5030. {
  5031. mdns_result_t * result = NULL;
  5032. esp_err_t err;
  5033. if (_str_null_or_empty(name)) {
  5034. return ESP_ERR_INVALID_ARG;
  5035. }
  5036. if (strstr(name, ".local")) {
  5037. ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain");
  5038. }
  5039. err = mdns_query(name, NULL, NULL, MDNS_TYPE_A, timeout, 1, &result);
  5040. if (err) {
  5041. return err;
  5042. }
  5043. if (!result) {
  5044. return ESP_ERR_NOT_FOUND;
  5045. }
  5046. mdns_ip_addr_t * a = result->addr;
  5047. while (a) {
  5048. if (a->addr.type == ESP_IPADDR_TYPE_V4) {
  5049. addr->addr = a->addr.u_addr.ip4.addr;
  5050. mdns_query_results_free(result);
  5051. return ESP_OK;
  5052. }
  5053. a = a->next;
  5054. }
  5055. mdns_query_results_free(result);
  5056. return ESP_ERR_NOT_FOUND;
  5057. }
  5058. #if CONFIG_LWIP_IPV6
  5059. esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t * addr)
  5060. {
  5061. mdns_result_t * result = NULL;
  5062. esp_err_t err;
  5063. if (_str_null_or_empty(name)) {
  5064. return ESP_ERR_INVALID_ARG;
  5065. }
  5066. if (strstr(name, ".local")) {
  5067. ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain");
  5068. }
  5069. err = mdns_query(name, NULL, NULL, MDNS_TYPE_AAAA, timeout, 1, &result);
  5070. if (err) {
  5071. return err;
  5072. }
  5073. if (!result) {
  5074. return ESP_ERR_NOT_FOUND;
  5075. }
  5076. mdns_ip_addr_t * a = result->addr;
  5077. while (a) {
  5078. if (a->addr.type == ESP_IPADDR_TYPE_V6) {
  5079. memcpy(addr->addr, a->addr.u_addr.ip6.addr, 16);
  5080. mdns_query_results_free(result);
  5081. return ESP_OK;
  5082. }
  5083. a = a->next;
  5084. }
  5085. mdns_query_results_free(result);
  5086. return ESP_ERR_NOT_FOUND;
  5087. }
  5088. #endif
  5089. #ifdef MDNS_ENABLE_DEBUG
  5090. void mdns_debug_packet(const uint8_t * data, size_t len)
  5091. {
  5092. static mdns_name_t n;
  5093. mdns_header_t header;
  5094. const uint8_t * content = data + MDNS_HEAD_LEN;
  5095. uint32_t t = xTaskGetTickCount() * portTICK_PERIOD_MS;
  5096. mdns_name_t * name = &n;
  5097. memset(name, 0, sizeof(mdns_name_t));
  5098. _mdns_dbg_printf("Packet[%u]: ", t);
  5099. header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
  5100. header.flags.value = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
  5101. header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
  5102. header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
  5103. header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
  5104. header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
  5105. _mdns_dbg_printf("%s",
  5106. (header.flags.value == MDNS_FLAGS_AUTHORITATIVE)?"AUTHORITATIVE\n":
  5107. (header.flags.value == MDNS_FLAGS_DISTRIBUTED)?"DISTRIBUTED\n":
  5108. (header.flags.value == 0)?"\n":" "
  5109. );
  5110. if (header.flags.value && header.flags.value != MDNS_FLAGS_AUTHORITATIVE) {
  5111. _mdns_dbg_printf("0x%04X\n", header.flags.value);
  5112. }
  5113. if (header.questions) {
  5114. uint8_t qs = header.questions;
  5115. while (qs--) {
  5116. content = _mdns_parse_fqdn(data, content, name);
  5117. if (!content) {
  5118. header.answers = 0;
  5119. header.additional = 0;
  5120. header.servers = 0;
  5121. _mdns_dbg_printf("ERROR: parse header questions\n");
  5122. break;
  5123. }
  5124. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  5125. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  5126. bool unicast = !!(mdns_class & 0x8000);
  5127. mdns_class &= 0x7FFF;
  5128. content = content + 4;
  5129. _mdns_dbg_printf(" Q: ");
  5130. if (unicast) {
  5131. _mdns_dbg_printf("*U* ");
  5132. }
  5133. if (type == MDNS_TYPE_PTR) {
  5134. _mdns_dbg_printf("%s.%s%s.%s.%s. PTR ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
  5135. } else if (type == MDNS_TYPE_SRV) {
  5136. _mdns_dbg_printf("%s.%s%s.%s.%s. SRV ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
  5137. } else if (type == MDNS_TYPE_TXT) {
  5138. _mdns_dbg_printf("%s.%s%s.%s.%s. TXT ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
  5139. } else if (type == MDNS_TYPE_A) {
  5140. _mdns_dbg_printf("%s.%s. A ", name->host, name->domain);
  5141. } else if (type == MDNS_TYPE_AAAA) {
  5142. _mdns_dbg_printf("%s.%s. AAAA ", name->host, name->domain);
  5143. } else if (type == MDNS_TYPE_NSEC) {
  5144. _mdns_dbg_printf("%s.%s%s.%s.%s. NSEC ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
  5145. } else if (type == MDNS_TYPE_ANY) {
  5146. _mdns_dbg_printf("%s.%s%s.%s.%s. ANY ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
  5147. } else {
  5148. _mdns_dbg_printf("%s.%s%s.%s.%s. %04X ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain, type);
  5149. }
  5150. if (mdns_class == 0x0001) {
  5151. _mdns_dbg_printf("IN");
  5152. } else {
  5153. _mdns_dbg_printf("%04X", mdns_class);
  5154. }
  5155. _mdns_dbg_printf("\n");
  5156. }
  5157. }
  5158. if (header.answers || header.servers || header.additional) {
  5159. uint16_t recordIndex = 0;
  5160. while (content < (data + len)) {
  5161. content = _mdns_parse_fqdn(data, content, name);
  5162. if (!content) {
  5163. _mdns_dbg_printf("ERROR: parse mdns records\n");
  5164. break;
  5165. }
  5166. uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
  5167. uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
  5168. uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET);
  5169. uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET);
  5170. const uint8_t * data_ptr = content + MDNS_DATA_OFFSET;
  5171. bool flush = !!(mdns_class & 0x8000);
  5172. mdns_class &= 0x7FFF;
  5173. content = data_ptr + data_len;
  5174. if (content > (data + len)) {
  5175. _mdns_dbg_printf("ERROR: content length overflow\n");
  5176. break;
  5177. }
  5178. mdns_parsed_record_type_t record_type = MDNS_ANSWER;
  5179. if (recordIndex >= (header.answers + header.servers)) {
  5180. record_type = MDNS_EXTRA;
  5181. } else if (recordIndex >= (header.answers)) {
  5182. record_type = MDNS_NS;
  5183. }
  5184. recordIndex++;
  5185. if (record_type == MDNS_EXTRA) {
  5186. _mdns_dbg_printf(" X");
  5187. } else if (record_type == MDNS_NS) {
  5188. _mdns_dbg_printf(" S");
  5189. } else {
  5190. _mdns_dbg_printf(" A");
  5191. }
  5192. if (type == MDNS_TYPE_PTR) {
  5193. _mdns_dbg_printf(": %s%s%s.%s.%s. PTR ", name->host, name->host[0]?".":"", name->service, name->proto, name->domain);
  5194. } else if (type == MDNS_TYPE_SRV) {
  5195. _mdns_dbg_printf(": %s.%s.%s.%s. SRV ", name->host, name->service, name->proto, name->domain);
  5196. } else if (type == MDNS_TYPE_TXT) {
  5197. _mdns_dbg_printf(": %s.%s.%s.%s. TXT ", name->host, name->service, name->proto, name->domain);
  5198. } else if (type == MDNS_TYPE_A) {
  5199. _mdns_dbg_printf(": %s.%s. A ", name->host, name->domain);
  5200. } else if (type == MDNS_TYPE_AAAA) {
  5201. _mdns_dbg_printf(": %s.%s. AAAA ", name->host, name->domain);
  5202. } else if (type == MDNS_TYPE_NSEC) {
  5203. _mdns_dbg_printf(": %s.%s.%s.%s. NSEC ", name->host, name->service, name->proto, name->domain);
  5204. } else if (type == MDNS_TYPE_ANY) {
  5205. _mdns_dbg_printf(": %s.%s.%s.%s. ANY ", name->host, name->service, name->proto, name->domain);
  5206. } else if (type == MDNS_TYPE_OPT) {
  5207. _mdns_dbg_printf(": . OPT ");
  5208. } else {
  5209. _mdns_dbg_printf(": %s.%s.%s.%s. %04X ", name->host, name->service, name->proto, name->domain, type);
  5210. }
  5211. if (mdns_class == 0x0001) {
  5212. _mdns_dbg_printf("IN ");
  5213. } else {
  5214. _mdns_dbg_printf("%04X ", mdns_class);
  5215. }
  5216. if (flush) {
  5217. _mdns_dbg_printf("FLUSH ");
  5218. }
  5219. _mdns_dbg_printf("%u ", ttl);
  5220. _mdns_dbg_printf("[%u] ", data_len);
  5221. if (type == MDNS_TYPE_PTR) {
  5222. if (!_mdns_parse_fqdn(data, data_ptr, name)) {
  5223. _mdns_dbg_printf("ERROR: parse PTR\n");
  5224. continue;
  5225. }
  5226. _mdns_dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain);
  5227. } else if (type == MDNS_TYPE_SRV) {
  5228. if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name)) {
  5229. _mdns_dbg_printf("ERROR: parse SRV\n");
  5230. continue;
  5231. }
  5232. uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
  5233. uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET);
  5234. uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET);
  5235. _mdns_dbg_printf("%u %u %u %s.%s.\n", priority, weight, port, name->host, name->domain);
  5236. } else if (type == MDNS_TYPE_TXT) {
  5237. uint16_t i=0, y;
  5238. while (i < data_len) {
  5239. uint8_t partLen = data_ptr[i++];
  5240. if ((i+partLen) > data_len) {
  5241. _mdns_dbg_printf("ERROR: parse TXT\n");
  5242. break;
  5243. }
  5244. char txt[partLen+1];
  5245. for (y=0; y<partLen; y++) {
  5246. char d = data_ptr[i++];
  5247. txt[y] = d;
  5248. }
  5249. txt[partLen] = 0;
  5250. _mdns_dbg_printf("%s", txt);
  5251. if (i<data_len) {
  5252. _mdns_dbg_printf("; ");
  5253. }
  5254. }
  5255. _mdns_dbg_printf("\n");
  5256. } else if (type == MDNS_TYPE_AAAA) {
  5257. esp_ip6_addr_t ip6;
  5258. memcpy(&ip6, data_ptr, sizeof(esp_ip6_addr_t));
  5259. _mdns_dbg_printf(IPV6STR "\n", IPV62STR(ip6));
  5260. } else if (type == MDNS_TYPE_A) {
  5261. esp_ip4_addr_t ip;
  5262. memcpy(&ip, data_ptr, sizeof(esp_ip4_addr_t));
  5263. _mdns_dbg_printf(IPSTR "\n", IP2STR(&ip));
  5264. } else if (type == MDNS_TYPE_NSEC) {
  5265. const uint8_t * old_ptr = data_ptr;
  5266. const uint8_t * new_ptr = _mdns_parse_fqdn(data, data_ptr, name);
  5267. if (new_ptr) {
  5268. _mdns_dbg_printf("%s.%s.%s.%s. ", name->host, name->service, name->proto, name->domain);
  5269. size_t diff = new_ptr - old_ptr;
  5270. data_len -= diff;
  5271. data_ptr = new_ptr;
  5272. }
  5273. size_t i;
  5274. for (i=0; i<data_len; i++) {
  5275. _mdns_dbg_printf(" %02x", data_ptr[i]);
  5276. }
  5277. _mdns_dbg_printf("\n");
  5278. } else if (type == MDNS_TYPE_OPT) {
  5279. uint16_t opCode = _mdns_read_u16(data_ptr, 0);
  5280. uint16_t opLen = _mdns_read_u16(data_ptr, 2);
  5281. _mdns_dbg_printf(" Code: %04x Data[%u]:", opCode, opLen);
  5282. size_t i;
  5283. for (i=4; i<data_len; i++) {
  5284. _mdns_dbg_printf(" %02x", data_ptr[i]);
  5285. }
  5286. _mdns_dbg_printf("\n");
  5287. } else {
  5288. size_t i;
  5289. for (i=0; i<data_len; i++) {
  5290. _mdns_dbg_printf(" %02x", data_ptr[i]);
  5291. }
  5292. _mdns_dbg_printf("\n");
  5293. }
  5294. }
  5295. }
  5296. }
  5297. #endif