| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792 |
- /*
- * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include "mdns.h"
- #include "mdns_private.h"
- #include "mdns_networking.h"
- #include "esp_log.h"
- #include <string.h>
- #include <sys/param.h>
- #ifdef MDNS_ENABLE_DEBUG
- void mdns_debug_packet(const uint8_t * data, size_t len);
- #endif
- // Internal size of IPv6 address is defined here as size of AAAA record in mdns packet
- // since the ip6_addr_t is defined in lwip and depends on using IPv6 zones
- #define _MDNS_SIZEOF_IP6_ADDR (MDNS_ANSWER_AAAA_SIZE)
- static const char * MDNS_DEFAULT_DOMAIN = "local";
- static const char * MDNS_SUB_STR = "_sub";
- mdns_server_t * _mdns_server = NULL;
- static mdns_host_item_t * _mdns_host_list = NULL;
- static mdns_host_item_t _mdns_self_host;
- static const char *TAG = "MDNS";
- static volatile TaskHandle_t _mdns_service_task_handle = NULL;
- static SemaphoreHandle_t _mdns_service_semaphore = NULL;
- static void _mdns_search_finish_done(void);
- 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);
- static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip,
- mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
- static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
- mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
- static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
- size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
- uint32_t ttl);
- static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance,
- const char * service_type, const char * proto, mdns_if_t tcpip_if,
- mdns_ip_protocol_t ip_protocol, uint32_t ttl);
- 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);
- static bool _mdns_append_host_list(mdns_out_answer_t ** destination, bool flush, bool bye);
- static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname);
- /*
- * @brief Internal collection of mdns supported interfaces
- *
- */
- static esp_netif_t * s_esp_netifs[MDNS_IF_MAX] = {};
- /*
- * @brief Convert mdns if to esp-netif handle
- */
- esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if)
- {
- if (tcpip_if < MDNS_IF_MAX) {
- if (s_esp_netifs[tcpip_if] == NULL) {
- // if local netif copy is NULL, try to search for the default interface key
- if (tcpip_if == MDNS_IF_STA) {
- s_esp_netifs[MDNS_IF_STA] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
- } else if (tcpip_if == MDNS_IF_AP) {
- s_esp_netifs[MDNS_IF_AP] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
- #if CONFIG_ETH_ENABLED
- } else if (tcpip_if == MDNS_IF_ETH) {
- s_esp_netifs[MDNS_IF_ETH] = esp_netif_get_handle_from_ifkey("ETH_DEF");
- #endif
- }
- }
- return s_esp_netifs[tcpip_if];
- }
- return NULL;
- }
- /*
- * @brief Clean internal mdns interface's pointer
- */
- static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if) {
- if (tcpip_if < MDNS_IF_MAX) {
- s_esp_netifs[tcpip_if] = NULL;
- }
- }
- /*
- * @brief Convert esp-netif handle to mdns if
- */
- static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *interface)
- {
- for (int i=0; i<MDNS_IF_MAX; ++i) {
- if (interface == s_esp_netifs[i])
- return i;
- }
- return MDNS_IF_MAX;
- }
- static inline bool _str_null_or_empty(const char * str){
- return (str == NULL || *str == 0);
- }
- /*
- * @brief Appends/increments a number to name/instance in case of collision
- * */
- static char * _mdns_mangle_name(char* in) {
- char *p = strrchr(in, '-');
- int suffix = 0;
- if (p == NULL) {
- //No - in ``in``
- suffix = 2;
- } else {
- char *endp = NULL;
- suffix = strtol(p + 1, &endp, 10);
- if (*endp != 0) {
- //suffix is not numerical
- suffix = 2;
- p = NULL; //so we append -suffix to the entire string
- }
- }
- char *ret;
- if (p == NULL) {
- //need to add -2 to string
- ret = malloc(strlen(in) + 3);
- if (ret == NULL) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- sprintf(ret, "%s-2", in);
- } else {
- ret = malloc(strlen(in) + 2); //one extra byte in case 9-10 or 99-100 etc
- if (ret == NULL) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- strcpy(ret, in);
- int baseLen = p - in; //length of 'bla' in 'bla-123'
- //overwrite suffix with new suffix
- sprintf(ret + baseLen, "-%d", suffix + 1);
- }
- return ret;
- }
- static bool _mdns_service_match(const mdns_service_t * srv, const char * service, const char * proto,
- const char * hostname)
- {
- return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
- (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
- }
- /**
- * @brief finds service from given service type
- * @param server the server
- * @param service service type to match
- * @param proto proto to match
- * @param hostname hostname of the service (if non-null)
- *
- * @return the service item if found or NULL on error
- */
- static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char * proto, const char * hostname)
- {
- mdns_srv_item_t * s = _mdns_server->services;
- while (s) {
- if (_mdns_service_match(s->service, service, proto, hostname)) {
- return s;
- }
- s = s->next;
- }
- return NULL;
- }
- static mdns_host_item_t * mdns_get_host_item(const char * hostname)
- {
- if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) {
- return &_mdns_self_host;
- }
- mdns_host_item_t * host = _mdns_host_list;
- while (host != NULL) {
- if (strcasecmp(host->hostname, hostname) == 0) {
- return host;
- }
- host = host->next;
- }
- return NULL;
- }
- static bool _mdns_can_add_more_services(void)
- {
- mdns_srv_item_t * s = _mdns_server->services;
- uint16_t service_num = 0;
- while (s) {
- service_num ++;
- s = s->next;
- if (service_num >= MDNS_MAX_SERVICES) {
- return false;
- }
- }
- return true;
- }
- esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet)
- {
- mdns_action_t * action = NULL;
- action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_RX_HANDLE;
- action->data.rx_handle.packet = packet;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- static const char *_mdns_get_default_instance_name(void)
- {
- if (_mdns_server && !_str_null_or_empty(_mdns_server->instance)) {
- return _mdns_server->instance;
- }
- if (_mdns_server && !_str_null_or_empty(_mdns_server->hostname)) {
- return _mdns_server->hostname;
- }
- return NULL;
- }
- /**
- * @brief Get the service name of a service
- */
- static const char * _mdns_get_service_instance_name(const mdns_service_t * service)
- {
- if (service && !_str_null_or_empty(service->instance)) {
- return service->instance;
- }
- return _mdns_get_default_instance_name();
- }
- static bool _mdns_instance_name_match(const char *lhs, const char *rhs)
- {
- if (lhs == NULL) {
- lhs = _mdns_get_default_instance_name();
- }
- if (rhs == NULL) {
- rhs = _mdns_get_default_instance_name();
- }
- return !strcasecmp(lhs, rhs);
- }
- static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service,
- const char *proto, const char *hostname)
- {
- return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
- !strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
- }
- static mdns_srv_item_t *_mdns_get_service_item_instance(const char *instance, const char *service, const char *proto,
- const char *hostname)
- {
- mdns_srv_item_t *s = _mdns_server->services;
- while (s) {
- if (_mdns_service_match_instance(s->service, instance, service, proto, hostname)) {
- return s;
- }
- s = s->next;
- }
- return NULL;
- }
- /**
- * @brief reads MDNS FQDN into mdns_name_t structure
- * FQDN is in format: [hostname.|[instance.]_service._proto.]local.
- *
- * @param packet MDNS packet
- * @param start Starting point of FQDN
- * @param name mdns_name_t structure to populate
- * @param buf temporary char buffer
- *
- * @return the address after the parsed FQDN in the packet or NULL on error
- */
- static const uint8_t * _mdns_read_fqdn(const uint8_t * packet, const uint8_t * start, mdns_name_t * name, char * buf)
- {
- size_t index = 0;
- while (start[index]) {
- if (name->parts == 4) {
- name->invalid = true;
- }
- uint8_t len = start[index++];
- if (len < 0xC0) {
- if (len > 63) {
- //length can not be more than 63
- return NULL;
- }
- uint8_t i;
- for (i=0; i<len; i++) {
- buf[i] = start[index++];
- }
- buf[len] = '\0';
- if (name->parts == 1 && buf[0] != '_'
- && (strcasecmp(buf, MDNS_DEFAULT_DOMAIN) != 0)
- && (strcasecmp(buf, "arpa") != 0)
- && (strcasecmp(buf, "ip6") != 0)
- && (strcasecmp(buf, "in-addr") != 0)) {
- strlcat(name->host, ".", sizeof(name->host));
- strlcat(name->host, buf, sizeof(name->host));
- } else if (strcasecmp(buf, MDNS_SUB_STR) == 0) {
- name->sub = 1;
- } else if (!name->invalid) {
- char* mdns_name_ptrs[]={name->host, name->service, name->proto, name->domain};
- memcpy(mdns_name_ptrs[name->parts++], buf, len+1);
- }
- } else {
- size_t address = (((uint16_t)len & 0x3F) << 8) | start[index++];
- if ((packet + address) >= start) {
- //reference address can not be after where we are
- return NULL;
- }
- if (_mdns_read_fqdn(packet, packet + address, name, buf)) {
- return start + index;
- }
- return NULL;
- }
- }
- return start + index + 1;
- }
- /**
- * @brief sets uint16_t value in a packet
- *
- * @param packet MDNS packet
- * @param index offset of uint16_t value
- * @param value the value to set
- */
- static inline void _mdns_set_u16(uint8_t * packet, uint16_t index, uint16_t value)
- {
- if ((index + 1) >= MDNS_MAX_PACKET_SIZE) {
- return;
- }
- packet[index] = (value >> 8) & 0xFF;
- packet[index+1] = value & 0xFF;
- }
- /**
- * @brief appends byte in a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param value the value to set
- *
- * @return length of added data: 0 on error or 1 on success
- */
- static inline uint8_t _mdns_append_u8(uint8_t * packet, uint16_t * index, uint8_t value)
- {
- if (*index >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- packet[*index] = value;
- *index += 1;
- return 1;
- }
- /**
- * @brief appends uint16_t in a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param value the value to set
- *
- * @return length of added data: 0 on error or 2 on success
- */
- static inline uint8_t _mdns_append_u16(uint8_t * packet, uint16_t * index, uint16_t value)
- {
- if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- _mdns_append_u8(packet, index, (value >> 8) & 0xFF);
- _mdns_append_u8(packet, index, value & 0xFF);
- return 2;
- }
- /**
- * @brief appends uint32_t in a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param value the value to set
- *
- * @return length of added data: 0 on error or 4 on success
- */
- static inline uint8_t _mdns_append_u32(uint8_t * packet, uint16_t * index, uint32_t value)
- {
- if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- _mdns_append_u8(packet, index, (value >> 24) & 0xFF);
- _mdns_append_u8(packet, index, (value >> 16) & 0xFF);
- _mdns_append_u8(packet, index, (value >> 8) & 0xFF);
- _mdns_append_u8(packet, index, value & 0xFF);
- return 4;
- }
- /**
- * @brief appends answer type, class, ttl and data length to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param type answer type
- * @param ttl answer ttl
- *
- * @return length of added data: 0 on error or 10 on success
- */
- static inline uint8_t _mdns_append_type(uint8_t * packet, uint16_t * index, uint8_t type, bool flush, uint32_t ttl)
- {
- if ((*index + 10) >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- uint16_t mdns_class = MDNS_CLASS_IN;
- if (flush) {
- mdns_class = MDNS_CLASS_IN_FLUSH_CACHE;
- }
- if (type == MDNS_ANSWER_PTR) {
- _mdns_append_u16(packet, index, MDNS_TYPE_PTR);
- _mdns_append_u16(packet, index, mdns_class);
- } else if (type == MDNS_ANSWER_TXT) {
- _mdns_append_u16(packet, index, MDNS_TYPE_TXT);
- _mdns_append_u16(packet, index, mdns_class);
- } else if (type == MDNS_ANSWER_SRV) {
- _mdns_append_u16(packet, index, MDNS_TYPE_SRV);
- _mdns_append_u16(packet, index, mdns_class);
- } else if (type == MDNS_ANSWER_A) {
- _mdns_append_u16(packet, index, MDNS_TYPE_A);
- _mdns_append_u16(packet, index, mdns_class);
- } else if (type == MDNS_ANSWER_AAAA) {
- _mdns_append_u16(packet, index, MDNS_TYPE_AAAA);
- _mdns_append_u16(packet, index, mdns_class);
- } else {
- return 0;
- }
- _mdns_append_u32(packet, index, ttl);
- _mdns_append_u16(packet, index, 0);
- return 10;
- }
- static inline uint8_t _mdns_append_string_with_len(uint8_t * packet, uint16_t * index, const char * string, uint8_t len)
- {
- if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- _mdns_append_u8(packet, index, len);
- memcpy(packet + *index, string, len);
- *index += len;
- return len + 1;
- }
- /**
- * @brief appends single string to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param string the string to append
- *
- * @return length of added data: 0 on error or length of the string + 1 on success
- */
- static inline uint8_t _mdns_append_string(uint8_t * packet, uint16_t * index, const char * string)
- {
- uint8_t len = strlen(string);
- if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- _mdns_append_u8(packet, index, len);
- memcpy(packet + *index, string, len);
- *index += len;
- return len + 1;
- }
- /**
- * @brief appends FQDN to a packet, incrementing the index and
- * compressing the output if previous occurrence of the string (or part of it) has been found
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param strings string array containing the parts of the FQDN
- * @param count number of strings in the array
- *
- * @return length of added data: 0 on error or length on success
- */
- static uint16_t _mdns_append_fqdn(uint8_t * packet, uint16_t * index, const char * strings[], uint8_t count)
- {
- if (!count) {
- //empty string so terminate
- return _mdns_append_u8(packet, index, 0);
- }
- mdns_name_t name;
- static char buf[MDNS_NAME_BUF_LEN];
- uint8_t len = strlen(strings[0]);
- //try to find first the string length in the packet (if it exists)
- uint8_t * len_location = (uint8_t *)memchr(packet, (char)len, *index);
- while (len_location) {
- //check if the string after len_location is the string that we are looking for
- if (memcmp(len_location+1, strings[0], len)) { //not continuing with our string
- search_next:
- //try and find the length byte further in the packet
- len_location = (uint8_t *)memchr(len_location+1, (char)len, *index - (len_location+1 - packet));
- continue;
- }
- //seems that we might have found the string that we are looking for
- //read the destination into name and compare
- name.parts = 0;
- name.sub = 0;
- name.host[0] = 0;
- name.service[0] = 0;
- name.proto[0] = 0;
- name.domain[0] = 0;
- const uint8_t * content = _mdns_read_fqdn(packet, len_location, &name, buf);
- if (!content) {
- //not a readable fqdn?
- return 0;
- }
- if (name.parts == count) {
- uint8_t i;
- for (i=0; i<count; i++) {
- if (strcasecmp(strings[i], (const char *)&name + (i * (MDNS_NAME_BUF_LEN)))) {
- //not our string! let's search more
- goto search_next;
- }
- }
- //we actually have found the string
- break;
- } else {
- goto search_next;
- }
- }
- //string is not yet in the packet, so let's add it
- if (!len_location) {
- uint8_t written = _mdns_append_string(packet, index, strings[0]);
- if (!written) {
- return 0;
- }
- //run the same for the other strings in the name
- return written + _mdns_append_fqdn(packet, index, &strings[1], count - 1);
- }
- //we have found the string so let's insert a pointer to it instead
- uint16_t offset = len_location - packet;
- offset |= MDNS_NAME_REF;
- return _mdns_append_u16(packet, index, offset);
- }
- /**
- * @brief appends PTR record for service to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param server the server that is hosting the service
- * @param service the service to add record for
- *
- * @return length of added data: 0 on error or length on success
- */
- 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)
- {
- const char * str[4];
- uint16_t record_length = 0;
- uint8_t part_length;
- if (service == NULL) {
- return 0;
- }
- str[0] = instance;
- str[1] = service;
- str[2] = proto;
- str[3] = MDNS_DEFAULT_DOMAIN;
- part_length = _mdns_append_fqdn(packet, index, str + 1, 3);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye?0:MDNS_ANSWER_PTR_TTL);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- uint16_t data_len_location = *index - 2;
- part_length = _mdns_append_fqdn(packet, index, str, 4);
- if (!part_length) {
- return 0;
- }
- _mdns_set_u16(packet, data_len_location, part_length);
- record_length += part_length;
- return record_length;
- }
- /**
- * @brief appends DNS-SD PTR record for service to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param server the server that is hosting the service
- * @param service the service to add record for
- *
- * @return length of added data: 0 on error or length on success
- */
- static uint16_t _mdns_append_sdptr_record(uint8_t * packet, uint16_t * index, mdns_service_t * service, bool flush, bool bye)
- {
- const char * str[3];
- const char * sd_str[4];
- uint16_t record_length = 0;
- uint8_t part_length;
- if (service == NULL) {
- return 0;
- }
- sd_str[0] = (char*)"_services";
- sd_str[1] = (char*)"_dns-sd";
- sd_str[2] = (char*)"_udp";
- sd_str[3] = MDNS_DEFAULT_DOMAIN;
- str[0] = service->service;
- str[1] = service->proto;
- str[2] = MDNS_DEFAULT_DOMAIN;
- part_length = _mdns_append_fqdn(packet, index, sd_str, 4);
- record_length += part_length;
- part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, flush, MDNS_ANSWER_PTR_TTL);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- uint16_t data_len_location = *index - 2;
- part_length = _mdns_append_fqdn(packet, index, str, 3);
- if (!part_length) {
- return 0;
- }
- _mdns_set_u16(packet, data_len_location, part_length);
- record_length += part_length;
- return record_length;
- }
- /**
- * @brief appends TXT record for service to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param server the server that is hosting the service
- * @param service the service to add record for
- *
- * @return length of added data: 0 on error or length on success
- */
- static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns_service_t * service, bool flush, bool bye)
- {
- const char * str[4];
- uint16_t record_length = 0;
- uint8_t part_length;
- if (service == NULL) {
- return 0;
- }
- str[0] = _mdns_get_service_instance_name(service);
- str[1] = service->service;
- str[2] = service->proto;
- str[3] = MDNS_DEFAULT_DOMAIN;
- if (!str[0]) {
- return 0;
- }
- part_length = _mdns_append_fqdn(packet, index, str, 4);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- part_length = _mdns_append_type(packet, index, MDNS_ANSWER_TXT, flush, bye?0:MDNS_ANSWER_TXT_TTL);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- uint16_t data_len_location = *index - 2;
- uint16_t data_len = 0;
- char * tmp;
- mdns_txt_linked_item_t * txt = service->txt;
- while (txt) {
- uint8_t txt_data_len = strlen(txt->key) + txt->value_len + 1;
- tmp = (char *)malloc(txt_data_len);
- if (tmp) {
- int offset = sprintf(tmp, "%s=", txt->key);
- memcpy(tmp + offset, txt->value, txt->value_len);
- uint8_t l = _mdns_append_string_with_len(packet, index, tmp, txt_data_len);
- free(tmp);
- if (!l) {
- return 0;
- }
- data_len += l;
- } else {
- HOOK_MALLOC_FAILED;
- // continue
- }
- txt = txt->next;
- }
- if (!data_len) {
- data_len = 1;
- packet[*index] = 0;
- *index = *index + 1;
- }
- _mdns_set_u16(packet, data_len_location, data_len);
- record_length += data_len;
- return record_length;
- }
- /**
- * @brief appends SRV record for service to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param server the server that is hosting the service
- * @param service the service to add record for
- *
- * @return length of added data: 0 on error or length on success
- */
- static uint16_t _mdns_append_srv_record(uint8_t * packet, uint16_t * index, mdns_service_t * service, bool flush, bool bye)
- {
- const char * str[4];
- uint16_t record_length = 0;
- uint8_t part_length;
- if (service == NULL) {
- return 0;
- }
- str[0] = _mdns_get_service_instance_name(service);
- str[1] = service->service;
- str[2] = service->proto;
- str[3] = MDNS_DEFAULT_DOMAIN;
- if (!str[0]) {
- return 0;
- }
- part_length = _mdns_append_fqdn(packet, index, str, 4);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- part_length = _mdns_append_type(packet, index, MDNS_ANSWER_SRV, flush, bye?0:MDNS_ANSWER_SRV_TTL);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- uint16_t data_len_location = *index - 2;
- part_length = 0;
- part_length += _mdns_append_u16(packet, index, service->priority);
- part_length += _mdns_append_u16(packet, index, service->weight);
- part_length += _mdns_append_u16(packet, index, service->port);
- if (part_length != 6) {
- return 0;
- }
- if (service->hostname) {
- str[0] = service->hostname;
- } else {
- str[0] = _mdns_server->hostname;
- }
- str[1] = MDNS_DEFAULT_DOMAIN;
- if (_str_null_or_empty(str[0])) {
- return 0;
- }
- part_length = _mdns_append_fqdn(packet, index, str, 2);
- if (!part_length) {
- return 0;
- }
- _mdns_set_u16(packet, data_len_location, part_length + 6);
- record_length += part_length + 6;
- return record_length;
- }
- /**
- * @brief appends A record to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param hostname the hostname address to add
- * @param ip the IP address to add
- *
- * @return length of added data: 0 on error or length on success
- */
- static uint16_t _mdns_append_a_record(uint8_t * packet, uint16_t * index, const char * hostname, uint32_t ip, bool flush, bool bye)
- {
- const char * str[2];
- uint16_t record_length = 0;
- uint8_t part_length;
- str[0] = hostname;
- str[1] = MDNS_DEFAULT_DOMAIN;
- if (_str_null_or_empty(str[0])) {
- return 0;
- }
- part_length = _mdns_append_fqdn(packet, index, str, 2);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- part_length = _mdns_append_type(packet, index, MDNS_ANSWER_A, flush, bye?0:MDNS_ANSWER_A_TTL);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- uint16_t data_len_location = *index - 2;
- if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- _mdns_append_u8(packet, index, ip & 0xFF);
- _mdns_append_u8(packet, index, (ip >> 8) & 0xFF);
- _mdns_append_u8(packet, index, (ip >> 16) & 0xFF);
- _mdns_append_u8(packet, index, (ip >> 24) & 0xFF);
- _mdns_set_u16(packet, data_len_location, 4);
- record_length += 4;
- return record_length;
- }
- #if CONFIG_LWIP_IPV6
- /**
- * @brief appends AAAA record to a packet, incrementing the index
- *
- * @param packet MDNS packet
- * @param index offset in the packet
- * @param hostname the hostname address to add
- * @param ipv6 the IPv6 address to add
- *
- * @return length of added data: 0 on error or length on success
- */
- static uint16_t _mdns_append_aaaa_record(uint8_t * packet, uint16_t * index, const char * hostname, uint8_t * ipv6, bool flush, bool bye)
- {
- const char * str[2];
- uint16_t record_length = 0;
- uint8_t part_length;
- str[0] = hostname;
- str[1] = MDNS_DEFAULT_DOMAIN;
- if (_str_null_or_empty(str[0])) {
- return 0;
- }
- part_length = _mdns_append_fqdn(packet, index, str, 2);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- part_length = _mdns_append_type(packet, index, MDNS_ANSWER_AAAA, flush, bye?0:MDNS_ANSWER_AAAA_TTL);
- if (!part_length) {
- return 0;
- }
- record_length += part_length;
- uint16_t data_len_location = *index - 2;
- if ((*index + MDNS_ANSWER_AAAA_SIZE) > MDNS_MAX_PACKET_SIZE) {
- return 0;
- }
- part_length = MDNS_ANSWER_AAAA_SIZE;
- memcpy(packet + *index, ipv6, part_length);
- *index += part_length;
- _mdns_set_u16(packet, data_len_location, part_length);
- record_length += part_length;
- return record_length;
- }
- #endif
- /**
- * @brief Append question to packet
- */
- static uint16_t _mdns_append_question(uint8_t * packet, uint16_t * index, mdns_out_question_t * q)
- {
- const char * str[4];
- uint8_t str_index = 0;
- uint8_t part_length;
- if (q->host) {
- str[str_index++] = q->host;
- }
- if (q->service) {
- str[str_index++] = q->service;
- }
- if (q->proto) {
- str[str_index++] = q->proto;
- }
- if (q->domain) {
- str[str_index++] = q->domain;
- }
- part_length = _mdns_append_fqdn(packet, index, str, str_index);
- if (!part_length) {
- return 0;
- }
- part_length += _mdns_append_u16(packet, index, q->type);
- part_length += _mdns_append_u16(packet, index, q->unicast?0x8001:0x0001);
- return part_length;
- }
- /**
- * @brief Helper to get either ETH or STA if the other is provided
- * Used when two interfaces are on the same subnet
- */
- static mdns_if_t _mdns_get_other_if (mdns_if_t tcpip_if)
- {
- if (tcpip_if == MDNS_IF_STA) {
- return MDNS_IF_ETH;
- } else if (tcpip_if == MDNS_IF_ETH) {
- return MDNS_IF_STA;
- }
- return MDNS_IF_MAX;
- }
- /**
- * @brief Check if interface is duplicate (two interfaces on the same subnet)
- */
- static bool _mdns_if_is_dup(mdns_if_t tcpip_if)
- {
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (other_if == MDNS_IF_MAX) {
- return false;
- }
- if (_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP
- || _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP
- || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP
- || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP
- ) {
- return true;
- }
- return false;
- }
- #if CONFIG_LWIP_IPV6
- /**
- * @brief Check if IPv6 address is NULL
- */
- static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6)
- {
- uint8_t i;
- uint8_t * data = (uint8_t *)ip6.addr;
- for (i=0; i<_MDNS_SIZEOF_IP6_ADDR; i++) {
- if (data[i]) {
- return false;
- }
- }
- return true;
- }
- #endif
- static uint8_t _mdns_append_host_answer(uint8_t * packet, uint16_t * index, mdns_host_item_t * host,
- uint8_t address_type, bool flush, bool bye)
- {
- mdns_ip_addr_t * addr = host->address_list;
- uint8_t num_records = 0;
- while (addr != NULL) {
- if (addr->addr.type == address_type) {
- if (address_type == ESP_IPADDR_TYPE_V4 &&
- _mdns_append_a_record(packet, index, host->hostname, addr->addr.u_addr.ip4.addr, flush, bye) <= 0) {
- break;
- }
- #if CONFIG_LWIP_IPV6
- if (address_type == ESP_IPADDR_TYPE_V6 &&
- _mdns_append_aaaa_record(packet, index, host->hostname, (uint8_t *)addr->addr.u_addr.ip6.addr, flush,
- bye) <= 0) {
- break;
- }
- #endif // CONFIG_LWIP_IPV6
- num_records++;
- }
- addr = addr->next;
- }
- return num_records;
- }
- /**
- * @brief Append answer to packet
- *
- * @return number of answers added to the packet
- */
- static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_answer_t * answer, mdns_if_t tcpip_if)
- {
- if (answer->type == MDNS_TYPE_PTR) {
- if (answer->service) {
- return _mdns_append_ptr_record(packet, index,
- _mdns_get_service_instance_name(answer->service),
- answer->service->service, answer->service->proto,
- answer->flush, answer->bye) > 0;
- } else {
- return _mdns_append_ptr_record(packet, index,
- answer->custom_instance, answer->custom_service, answer->custom_proto,
- answer->flush, answer->bye) > 0;
- }
- } else if (answer->type == MDNS_TYPE_SRV) {
- return _mdns_append_srv_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
- } else if (answer->type == MDNS_TYPE_TXT) {
- return _mdns_append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
- } else if (answer->type == MDNS_TYPE_SDPTR) {
- return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0;
- } else if (answer->type == MDNS_TYPE_A) {
- if (answer->host == &_mdns_self_host) {
- esp_netif_ip_info_t if_ip_info;
- 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) {
- return 0;
- }
- if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
- return 0;
- }
- if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) <= 0) {
- return 0;
- }
- if (!_mdns_if_is_dup(tcpip_if)) {
- return 1;
- }
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &if_ip_info)) {
- return 1;
- }
- if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) > 0) {
- return 2;
- }
- return 1;
- } else if (answer->host != NULL) {
- return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye);
- }
- }
- #if CONFIG_LWIP_IPV6
- else if (answer->type == MDNS_TYPE_AAAA) {
- if (answer->host == &_mdns_self_host) {
- struct esp_ip6_addr if_ip6;
- 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) {
- return 0;
- }
- if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) {
- return 0;
- }
- if (_ipv6_address_is_zero(if_ip6)) {
- return 0;
- }
- if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t*)if_ip6.addr, answer->flush, answer->bye) <= 0) {
- return 0;
- }
- if (!_mdns_if_is_dup(tcpip_if)) {
- return 1;
- }
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &if_ip6)) {
- return 1;
- }
- if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t*)if_ip6.addr, answer->flush, answer->bye) > 0) {
- return 2;
- }
- return 1;
- } else if (answer->host != NULL) {
- return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush, answer->bye);
- }
- }
- #endif
- return 0;
- }
- /**
- * @brief sends a packet
- *
- * @param p the packet
- */
- static void _mdns_dispatch_tx_packet(mdns_tx_packet_t * p)
- {
- static uint8_t packet[MDNS_MAX_PACKET_SIZE];
- uint16_t index = MDNS_HEAD_LEN;
- memset(packet, 0, MDNS_HEAD_LEN);
- mdns_out_question_t * q;
- mdns_out_answer_t * a;
- uint8_t count;
- _mdns_set_u16(packet, MDNS_HEAD_FLAGS_OFFSET, p->flags);
- _mdns_set_u16(packet, MDNS_HEAD_ID_OFFSET, p->id);
- count = 0;
- q = p->questions;
- while (q) {
- if (_mdns_append_question(packet, &index, q)) {
- count++;
- }
- q = q->next;
- }
- _mdns_set_u16(packet, MDNS_HEAD_QUESTIONS_OFFSET, count);
- count = 0;
- a = p->answers;
- while (a) {
- count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
- a = a->next;
- }
- _mdns_set_u16(packet, MDNS_HEAD_ANSWERS_OFFSET, count);
- count = 0;
- a = p->servers;
- while (a) {
- count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
- a = a->next;
- }
- _mdns_set_u16(packet, MDNS_HEAD_SERVERS_OFFSET, count);
- count = 0;
- a = p->additional;
- while (a) {
- count += _mdns_append_answer(packet, &index, a, p->tcpip_if);
- a = a->next;
- }
- _mdns_set_u16(packet, MDNS_HEAD_ADDITIONAL_OFFSET, count);
- #ifdef MDNS_ENABLE_DEBUG
- _mdns_dbg_printf("\nTX[%u][%u]: ", p->tcpip_if, p->ip_protocol);
- if (p->dst.type == ESP_IPADDR_TYPE_V4) {
- _mdns_dbg_printf("To: " IPSTR ":%u, ", IP2STR(&p->dst.u_addr.ip4), p->port);
- } else {
- _mdns_dbg_printf("To: " IPV6STR ":%u, ", IPV62STR(p->dst.u_addr.ip6), p->port);
- }
- mdns_debug_packet(packet, index);
- #endif
- _mdns_udp_pcb_write(p->tcpip_if, p->ip_protocol, &p->dst, p->port, packet, index);
- }
- /**
- * @brief frees a packet
- *
- * @param packet the packet
- */
- static void _mdns_free_tx_packet(mdns_tx_packet_t * packet)
- {
- if (!packet) {
- return;
- }
- mdns_out_question_t * q = packet->questions;
- while (q) {
- mdns_out_question_t * next = q->next;
- if (q->own_dynamic_memory) {
- free((char *)q->host);
- free((char *)q->service);
- free((char *)q->proto);
- free((char *)q->domain);
- }
- free(q);
- q = next;
- }
- queueFree(mdns_out_answer_t, packet->answers);
- queueFree(mdns_out_answer_t, packet->servers);
- queueFree(mdns_out_answer_t, packet->additional);
- free(packet);
- }
- /**
- * @brief schedules a packet to be sent after given milliseconds
- *
- * @param packet the packet
- * @param ms_after number of milliseconds after which the packet should be dispatched
- */
- static void _mdns_schedule_tx_packet(mdns_tx_packet_t * packet, uint32_t ms_after)
- {
- if (!packet) {
- return;
- }
- packet->send_at = (xTaskGetTickCount() * portTICK_PERIOD_MS) + ms_after;
- packet->next = NULL;
- if (!_mdns_server->tx_queue_head || _mdns_server->tx_queue_head->send_at > packet->send_at) {
- packet->next = _mdns_server->tx_queue_head;
- _mdns_server->tx_queue_head = packet;
- return;
- }
- mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
- while (q->next && q->next->send_at <= packet->send_at) {
- q = q->next;
- }
- packet->next = q->next;
- q->next = packet;
- }
- /**
- * @brief free all packets scheduled for sending
- */
- static void _mdns_clear_tx_queue_head(void)
- {
- mdns_tx_packet_t * q;
- while (_mdns_server->tx_queue_head) {
- q = _mdns_server->tx_queue_head;
- _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next;
- _mdns_free_tx_packet(q);
- }
- }
- /**
- * @brief clear packets scheduled for sending on a specific interface
- *
- * @param tcpip_if the interface
- * @param ip_protocol pcb type V4/V6
- */
- static void _mdns_clear_pcb_tx_queue_head(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- mdns_tx_packet_t * q, * p;
- while (_mdns_server->tx_queue_head && _mdns_server->tx_queue_head->tcpip_if == tcpip_if && _mdns_server->tx_queue_head->ip_protocol == ip_protocol) {
- q = _mdns_server->tx_queue_head;
- _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next;
- _mdns_free_tx_packet(q);
- }
- if (_mdns_server->tx_queue_head) {
- q = _mdns_server->tx_queue_head;
- while (q->next) {
- if (q->next->tcpip_if == tcpip_if && q->next->ip_protocol == ip_protocol) {
- p = q->next;
- q->next = p->next;
- _mdns_free_tx_packet(p);
- } else {
- q = q->next;
- }
- }
- }
- }
- /**
- * @brief get the next packet scheduled for sending on a specific interface
- *
- * @param tcpip_if the interface
- * @param ip_protocol pcb type V4/V6
- */
- static mdns_tx_packet_t * _mdns_get_next_pcb_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
- while (q) {
- if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol) {
- return q;
- }
- q = q->next;
- }
- return NULL;
- }
- /**
- * @brief Find, remove and free answer from the scheduled packets
- */
- 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)
- {
- mdns_srv_item_t s = {NULL, NULL};
- if (!service) {
- service = &s;
- }
- mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
- while (q) {
- if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol && q->distributed) {
- mdns_out_answer_t * a = q->answers;
- if (a->type == type && a->service == service->service) {
- q->answers = q->answers->next;
- free(a);
- } else {
- while (a->next) {
- if (a->next->type == type && a->next->service == service->service) {
- mdns_out_answer_t * b = a->next;
- a->next = b->next;
- free(b);
- break;
- }
- a = a->next;
- }
- }
- }
- q = q->next;
- }
- }
- /**
- * @brief Remove and free answer from answer list (destination)
- */
- static void _mdns_dealloc_answer(mdns_out_answer_t ** destination, uint16_t type, mdns_srv_item_t * service)
- {
- mdns_out_answer_t * d = *destination;
- if (!d) {
- return;
- }
- mdns_srv_item_t s = {NULL, NULL};
- if (!service) {
- service = &s;
- }
- if (d->type == type && d->service == service->service) {
- *destination = d->next;
- free(d);
- return;
- }
- while (d->next) {
- mdns_out_answer_t * a = d->next;
- if (a->type == type && a->service == service->service) {
- d->next = a->next;
- free(a);
- return;
- }
- d = d->next;
- }
- }
- /**
- * @brief Allocate new answer and add it to answer list (destination)
- */
- static bool _mdns_alloc_answer(mdns_out_answer_t ** destination, uint16_t type, mdns_service_t * service,
- mdns_host_item_t * host, bool flush, bool bye)
- {
- mdns_out_answer_t * d = *destination;
- while (d) {
- if (d->type == type && d->service == service && d->host == host) {
- return true;
- }
- d = d->next;
- }
- mdns_out_answer_t * a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
- if (!a) {
- HOOK_MALLOC_FAILED;
- return false;
- }
- a->type = type;
- a->service = service;
- a->host = host;
- a->custom_service = NULL;
- a->bye = bye;
- a->flush = flush;
- a->next = NULL;
- queueToEnd(mdns_out_answer_t, *destination, a);
- return true;
- }
- /**
- * @brief Allocate new packet for sending
- */
- static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- mdns_tx_packet_t * packet = (mdns_tx_packet_t*)malloc(sizeof(mdns_tx_packet_t));
- if (!packet) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- memset((uint8_t*)packet, 0, sizeof(mdns_tx_packet_t));
- packet->tcpip_if = tcpip_if;
- packet->ip_protocol = ip_protocol;
- packet->port = MDNS_SERVICE_PORT;
- if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
- esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251);
- memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
- }
- #if CONFIG_LWIP_IPV6
- else {
- esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000);
- memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t));
- }
- #endif
- return packet;
- }
- static bool _mdns_create_answer_from_service(mdns_tx_packet_t * packet, mdns_service_t * service,
- mdns_parsed_question_t * question, bool shared, bool send_flush)
- {
- mdns_host_item_t * host = mdns_get_host_item(service->hostname);
- if (question->type == MDNS_TYPE_PTR || question->type == MDNS_TYPE_ANY) {
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) ||
- !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
- !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false) ||
- !_mdns_alloc_answer(shared ? &packet->additional : &packet->answers, MDNS_TYPE_A, service, host, send_flush,
- false) ||
- !_mdns_alloc_answer(shared ? &packet->additional : &packet->answers, MDNS_TYPE_AAAA, service, host,
- send_flush, false)) {
- return false;
- }
- } else if (question->type == MDNS_TYPE_SRV) {
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
- !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_A, service, host, send_flush, false) ||
- !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_AAAA, service, host, send_flush, false)) {
- return false;
- }
- } else if (question->type == MDNS_TYPE_TXT) {
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false)) {
- return false;
- }
- } else if (question->type == MDNS_TYPE_SDPTR) {
- shared = true;
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, service, NULL, false, false)) {
- return false;
- }
- }
- return true;
- }
- static bool _mdns_create_answer_from_hostname(mdns_tx_packet_t * packet, const char * hostname, bool send_flush)
- {
- mdns_host_item_t * host = mdns_get_host_item(hostname);
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_A, NULL, host, send_flush, false) ||
- !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, send_flush, false)) {
- return false;
- }
- return true;
- }
- /**
- * @brief Create answer packet to questions from parsed packet
- */
- static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_packet)
- {
- if (!parsed_packet->questions) {
- return;
- }
- bool send_flush = parsed_packet->src_port == MDNS_SERVICE_PORT;
- bool unicast = false;
- bool shared = false;
- mdns_tx_packet_t *packet = _mdns_alloc_packet_default(parsed_packet->tcpip_if, parsed_packet->ip_protocol);
- if (!packet) {
- return;
- }
- packet->flags = MDNS_FLAGS_AUTHORITATIVE;
- packet->distributed = parsed_packet->distributed;
- packet->id = parsed_packet->id;
- mdns_parsed_question_t *q = parsed_packet->questions;
- while (q) {
- shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe;
- if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) {
- mdns_srv_item_t *service = _mdns_get_service_item_instance(q->host, q->service, q->proto, NULL);
- if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
- _mdns_free_tx_packet(packet);
- return;
- }
- } else if (q->service && q->proto) {
- mdns_srv_item_t *service = _mdns_server->services;
- while (service) {
- if (_mdns_service_match(service->service, q->service, q->proto, NULL)) {
- if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
- _mdns_free_tx_packet(packet);
- return;
- }
- }
- service = service->next;
- }
- } else if (q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA) {
- if (!_mdns_create_answer_from_hostname(packet, q->host, send_flush)) {
- _mdns_free_tx_packet(packet);
- return;
- }
- } else if (q->type == MDNS_TYPE_ANY) {
- if (!_mdns_append_host_list(&packet->answers, send_flush, false)) {
- _mdns_free_tx_packet(packet);
- return;
- }
- } else if (!_mdns_alloc_answer(&packet->answers, q->type, NULL, NULL, send_flush, false)) {
- _mdns_free_tx_packet(packet);
- return;
- }
- #ifdef MDNS_REPEAT_QUERY_IN_RESPONSE
- if (parsed_packet->src_port != MDNS_SERVICE_PORT && // Repeat the queries only for "One-Shot mDNS queries"
- (q->type == MDNS_TYPE_ANY || q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA)) {
- mdns_out_question_t * out_question = malloc(sizeof(mdns_out_question_t));
- if (out_question == NULL) {
- HOOK_MALLOC_FAILED;
- _mdns_free_tx_packet(packet);
- return;
- }
- out_question->type = q->type;
- out_question->unicast = q->unicast;
- out_question->host = q->host;
- q->host = NULL;
- out_question->service = q->service;
- q->service = NULL;
- out_question->proto = q->proto;
- q->proto = NULL;
- out_question->domain = q->domain;
- q->domain = NULL;
- out_question->next = NULL;
- out_question->own_dynamic_memory = true;
- queueToEnd(mdns_out_question_t, packet->questions, out_question);
- }
- #endif // MDNS_REPEAT_QUERY_IN_RESPONSE
- if (q->unicast) {
- unicast = true;
- }
- q = q->next;
- }
- if (unicast || !send_flush) {
- memcpy(&packet->dst, &parsed_packet->src, sizeof(esp_ip_addr_t));
- packet->port = parsed_packet->src_port;
- }
- static uint8_t share_step = 0;
- if (shared) {
- _mdns_schedule_tx_packet(packet, 25 + (share_step * 25));
- share_step = (share_step + 1) & 0x03;
- } else {
- _mdns_dispatch_tx_packet(packet);
- _mdns_free_tx_packet(packet);
- }
- }
- /**
- * @brief Check if question is already in the list
- */
- static bool _mdns_question_exists(mdns_out_question_t * needle, mdns_out_question_t * haystack)
- {
- while (haystack) {
- if (haystack->type == needle->type
- && haystack->host == needle->host
- && haystack->service == needle->service
- && haystack->proto == needle->proto) {
- return true;
- }
- haystack = haystack->next;
- }
- return false;
- }
- static bool _mdns_append_host(mdns_out_answer_t ** destination, mdns_host_item_t * host, bool flush, bool bye)
- {
- if (!_mdns_alloc_answer(destination, MDNS_TYPE_A, NULL, host, flush, bye)) {
- return false;
- }
- if (!_mdns_alloc_answer(destination, MDNS_TYPE_AAAA, NULL, host, flush, bye)) {
- return false;
- }
- return true;
- }
- 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)
- {
- if (services == NULL) {
- mdns_host_item_t * host = mdns_get_host_item(_mdns_server->hostname);
- if (host != NULL) {
- return _mdns_append_host(destination, host, flush, bye);
- }
- return true;
- }
- for (size_t i = 0; i < services_len; i++) {
- mdns_host_item_t *host = mdns_get_host_item(services[i]->service->hostname);
- if (!_mdns_append_host(destination, host, flush, bye)) {
- return false;
- }
- }
- return true;
- }
- static bool _mdns_append_host_list(mdns_out_answer_t ** destination, bool flush, bool bye)
- {
- if (!_str_null_or_empty(_mdns_server->hostname)) {
- mdns_host_item_t * self_host = mdns_get_host_item(_mdns_server->hostname);
- if (!_mdns_append_host(destination, self_host, flush, bye)) {
- return false;
- }
- }
- mdns_host_item_t * host = _mdns_host_list;
- while (host != NULL) {
- host = host->next;
- if (!_mdns_append_host(destination, host, flush, bye)) {
- return false;
- }
- }
- return true;
- }
- static bool _mdns_append_host_question(mdns_out_question_t **questions, const char *hostname, bool unicast)
- {
- mdns_out_question_t *q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
- if (!q) {
- HOOK_MALLOC_FAILED;
- return false;
- }
- q->next = NULL;
- q->unicast = unicast;
- q->type = MDNS_TYPE_ANY;
- q->host = hostname;
- q->service = NULL;
- q->proto = NULL;
- q->domain = MDNS_DEFAULT_DOMAIN;
- q->own_dynamic_memory = false;
- if (_mdns_question_exists(q, *questions)) {
- free(q);
- } else {
- queueToEnd(mdns_out_question_t, *questions, q);
- }
- return true;
- }
- static bool _mdns_append_host_questions_for_services(mdns_out_question_t **questions, mdns_srv_item_t *services[],
- size_t len, bool unicast)
- {
- if (!_str_null_or_empty(_mdns_server->hostname) &&
- !_mdns_append_host_question(questions, _mdns_server->hostname, unicast)) {
- return false;
- }
- for (size_t i = 0; i < len; i++) {
- if (!_mdns_append_host_question(questions, services[i]->service->hostname, unicast)) {
- return false;
- }
- }
- return true;
- }
- /**
- * @brief Create probe packet for particular services on particular PCB
- */
- 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)
- {
- mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
- if (!packet) {
- return NULL;
- }
- size_t i;
- for (i=0; i<len; i++) {
- mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
- if (!q) {
- HOOK_MALLOC_FAILED;
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- q->next = NULL;
- q->unicast = first;
- q->type = MDNS_TYPE_ANY;
- q->host = _mdns_get_service_instance_name(services[i]->service);
- q->service = services[i]->service->service;
- q->proto = services[i]->service->proto;
- q->domain = MDNS_DEFAULT_DOMAIN;
- q->own_dynamic_memory = false;
- if (!q->host || _mdns_question_exists(q, packet->questions)) {
- free(q);
- continue;
- } else {
- queueToEnd(mdns_out_question_t, packet->questions, q);
- }
- if (!q->host || !_mdns_alloc_answer(&packet->servers, MDNS_TYPE_SRV, services[i]->service, NULL, false, false)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- }
- if (include_ip) {
- if (!_mdns_append_host_questions_for_services(&packet->questions, services, len, first)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- if (!_mdns_append_host_list_in_services(&packet->servers, services, len, false, false)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- }
- return packet;
- }
- /**
- * @brief Create announce packet for particular services on particular PCB
- */
- 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)
- {
- mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
- if (!packet) {
- return NULL;
- }
- packet->flags = MDNS_FLAGS_AUTHORITATIVE;
- uint8_t i;
- for (i=0; i<len; i++) {
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false)
- || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false)
- || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false)
- || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- }
- if (include_ip) {
- if (!_mdns_append_host_list_in_services(&packet->servers, services, len, true, false)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- }
- return packet;
- }
- /**
- * @brief Convert probe packet to announce
- */
- static mdns_tx_packet_t * _mdns_create_announce_from_probe(mdns_tx_packet_t * probe)
- {
- mdns_tx_packet_t * packet = _mdns_alloc_packet_default(probe->tcpip_if, probe->ip_protocol);
- if (!packet) {
- return NULL;
- }
- packet->flags = MDNS_FLAGS_AUTHORITATIVE;
- mdns_out_answer_t * s = probe->servers;
- while (s) {
- if (s->type == MDNS_TYPE_SRV) {
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, s->service, NULL, false, false)
- || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, s->service, NULL, false, false)
- || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, s->service, NULL, true, false)
- || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, s->service, NULL, true, false)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- } else if (s->type == MDNS_TYPE_A || s->type == MDNS_TYPE_AAAA) {
- if (!_mdns_alloc_answer(&packet->answers, s->type, NULL, s->host, true, false)) {
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- }
- s = s->next;
- }
- return packet;
- }
- /**
- * @brief Send by for particular services on particular PCB
- */
- 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)
- {
- mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
- if (!packet) {
- return;
- }
- packet->flags = MDNS_FLAGS_AUTHORITATIVE;
- size_t i;
- for (i=0; i<len; i++) {
- if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, true, true)) {
- _mdns_free_tx_packet(packet);
- return;
- }
- }
- if (include_ip) {
- _mdns_append_host_list_in_services(&packet->answers, services, len, true, true);
- }
- _mdns_dispatch_tx_packet(packet);
- _mdns_free_tx_packet(packet);
- }
- /**
- * @brief Send probe for additional services on particular PCB
- */
- 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)
- {
- mdns_pcb_t * pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
- size_t services_final_len = len;
- if (PCB_STATE_IS_PROBING(pcb)) {
- services_final_len += pcb->probe_services_len;
- }
- mdns_srv_item_t ** _services = NULL;
- if (services_final_len) {
- _services = (mdns_srv_item_t **)malloc(sizeof(mdns_srv_item_t *) * services_final_len);
- if (!_services) {
- HOOK_MALLOC_FAILED;
- return;
- }
- size_t i;
- for (i=0; i<len; i++) {
- _services[i] = services[i];
- }
- if (pcb->probe_services) {
- for (i=0; i<pcb->probe_services_len; i++) {
- _services[len+i] = pcb->probe_services[i];
- }
- free(pcb->probe_services);
- }
- }
- probe_ip = pcb->probe_ip || probe_ip;
- pcb->probe_ip = false;
- pcb->probe_services = NULL;
- pcb->probe_services_len = 0;
- pcb->probe_running = false;
- mdns_tx_packet_t * packet = _mdns_create_probe_packet(tcpip_if, ip_protocol, _services, services_final_len, true, probe_ip);
- if (!packet) {
- free(_services);
- return;
- }
- pcb->probe_ip = probe_ip;
- pcb->probe_services = _services;
- pcb->probe_services_len = services_final_len;
- pcb->probe_running = true;
- _mdns_schedule_tx_packet(packet, ((pcb->failed_probes > 5)?1000:120) + (esp_random() & 0x7F));
- pcb->state = PCB_PROBE_1;
- }
- /**
- * @brief Send probe for particular services on particular PCB
- *
- * Tests possible duplication on probing service structure and probes only for new entries.
- * - If pcb probing then add only non-probing services and restarts probing
- * - If pcb not probing, run probing for all specified services
- */
- 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)
- {
- mdns_pcb_t * pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
- _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
- if (_str_null_or_empty(_mdns_server->hostname)) {
- pcb->state = PCB_RUNNING;
- return;
- }
- if (PCB_STATE_IS_PROBING(pcb)) {
- // Looking for already probing services to resolve duplications
- mdns_srv_item_t * new_probe_services[len];
- int new_probe_service_len = 0;
- bool found;
- for (size_t j=0; j < len; ++j) {
- found = false;
- for (int i=0; i < pcb->probe_services_len; ++i) {
- if (pcb->probe_services[i] == services[j]) {
- found = true;
- break;
- }
- }
- if (!found) {
- new_probe_services[new_probe_service_len++] = services[j];
- }
- }
- // init probing for newly added services
- _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol,
- new_probe_service_len?new_probe_services:NULL, new_probe_service_len, probe_ip);
- } else {
- // not probing, so init for all services
- _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol, services, len, probe_ip);
- }
- }
- /**
- * @brief Restart the responder on particular PCB
- */
- static void _mdns_restart_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- size_t srv_count = 0;
- mdns_srv_item_t * a = _mdns_server->services;
- while (a) {
- srv_count++;
- a = a->next;
- }
- mdns_srv_item_t * services[srv_count];
- size_t i = 0;
- a = _mdns_server->services;
- while (a) {
- services[i++] = a;
- a = a->next;
- }
- _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, srv_count, true);
- }
- /**
- * @brief Send by for particular services
- */
- static void _mdns_send_bye(mdns_srv_item_t ** services, size_t len, bool include_ip)
- {
- uint8_t i, j;
- if (_str_null_or_empty(_mdns_server->hostname)) {
- return;
- }
- for (i=0; i<MDNS_IF_MAX; i++) {
- for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
- if (_mdns_server->interfaces[i].pcbs[j].pcb && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) {
- _mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
- }
- }
- }
- }
- /**
- * @brief Send announcement on particular PCB
- */
- 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)
- {
- mdns_pcb_t * _pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
- size_t i;
- if (_pcb->pcb) {
- if (PCB_STATE_IS_PROBING(_pcb)) {
- _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, len, include_ip);
- } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
- mdns_tx_packet_t * p = _mdns_get_next_pcb_packet(tcpip_if, ip_protocol);
- if (p) {
- for (i=0; i<len; i++) {
- if (!_mdns_alloc_answer(&p->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false)
- || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false)
- || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false)
- || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) {
- break;
- }
- }
- if (include_ip) {
- _mdns_dealloc_answer(&p->additional, MDNS_TYPE_A, NULL);
- _mdns_dealloc_answer(&p->additional, MDNS_TYPE_AAAA, NULL);
- _mdns_append_host_list_in_services(&p->answers, services, len, true, false);
- }
- _pcb->state = PCB_ANNOUNCE_1;
- }
- } else if (_pcb->state == PCB_RUNNING) {
- if (_str_null_or_empty(_mdns_server->hostname)) {
- return;
- }
- _pcb->state = PCB_ANNOUNCE_1;
- mdns_tx_packet_t * p = _mdns_create_announce_packet(tcpip_if, ip_protocol, services, len, include_ip);
- if (p) {
- _mdns_schedule_tx_packet(p, 0);
- }
- }
- }
- }
- /**
- * @brief Send probe on all active PCBs
- */
- static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool probe_ip, bool clear_old_probe)
- {
- uint8_t i, j;
- for (i=0; i<MDNS_IF_MAX; i++) {
- for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
- if (_mdns_server->interfaces[i].pcbs[j].pcb) {
- mdns_pcb_t * _pcb = &_mdns_server->interfaces[i].pcbs[j];
- if (clear_old_probe) {
- free(_pcb->probe_services);
- _pcb->probe_services = NULL;
- _pcb->probe_services_len = 0;
- _pcb->probe_running = false;
- }
- _mdns_init_pcb_probe((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, probe_ip);
- }
- }
- }
- }
- /**
- * @brief Send announcement on all active PCBs
- */
- static void _mdns_announce_all_pcbs(mdns_srv_item_t ** services, size_t len, bool include_ip)
- {
- uint8_t i, j;
- for (i=0; i<MDNS_IF_MAX; i++) {
- for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
- _mdns_announce_pcb((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
- }
- }
- }
- /**
- * @brief Restart the responder on all active PCBs
- */
- static void _mdns_send_final_bye(bool include_ip)
- {
- //collect all services and start probe
- size_t srv_count = 0;
- mdns_srv_item_t * a = _mdns_server->services;
- while (a) {
- srv_count++;
- a = a->next;
- }
- if (!srv_count) {
- return;
- }
- mdns_srv_item_t * services[srv_count];
- size_t i = 0;
- a = _mdns_server->services;
- while (a) {
- services[i++] = a;
- a = a->next;
- }
- _mdns_send_bye(services, srv_count, include_ip);
- }
- /**
- * @brief Stop the responder on all services without instance
- */
- static void _mdns_send_bye_all_pcbs_no_instance(bool include_ip)
- {
- size_t srv_count = 0;
- mdns_srv_item_t * a = _mdns_server->services;
- while (a) {
- if (!a->service->instance) {
- srv_count++;
- }
- a = a->next;
- }
- if (!srv_count) {
- return;
- }
- mdns_srv_item_t * services[srv_count];
- size_t i = 0;
- a = _mdns_server->services;
- while (a) {
- if (!a->service->instance) {
- services[i++] = a;
- }
- a = a->next;
- }
- _mdns_send_bye(services, srv_count, include_ip);
- }
- /**
- * @brief Restart the responder on all services without instance
- */
- static void _mdns_restart_all_pcbs_no_instance(void)
- {
- size_t srv_count = 0;
- mdns_srv_item_t * a = _mdns_server->services;
- while (a) {
- if (!a->service->instance) {
- srv_count++;
- }
- a = a->next;
- }
- if (!srv_count) {
- return;
- }
- mdns_srv_item_t * services[srv_count];
- size_t i = 0;
- a = _mdns_server->services;
- while (a) {
- if (!a->service->instance) {
- services[i++] = a;
- }
- a = a->next;
- }
- _mdns_probe_all_pcbs(services, srv_count, false, true);
- }
- /**
- * @brief Restart the responder on all active PCBs
- */
- static void _mdns_restart_all_pcbs(void)
- {
- _mdns_clear_tx_queue_head();
- size_t srv_count = 0;
- mdns_srv_item_t * a = _mdns_server->services;
- while (a) {
- srv_count++;
- a = a->next;
- }
- mdns_srv_item_t * services[srv_count];
- size_t l = 0;
- a = _mdns_server->services;
- while (a) {
- services[l++] = a;
- a = a->next;
- }
- _mdns_probe_all_pcbs(services, srv_count, true, true);
- }
- /**
- * @brief creates/allocates new text item list
- * @param num_items service number of txt items or 0
- * @param txt service txt items array or NULL
- *
- * @return pointer to the linked txt item list or NULL
- */
- static mdns_txt_linked_item_t * _mdns_allocate_txt(size_t num_items, mdns_txt_item_t txt[])
- {
- mdns_txt_linked_item_t * new_txt = NULL;
- size_t i = 0;
- if (num_items) {
- for (i=0; i<num_items; i++) {
- mdns_txt_linked_item_t * new_item = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
- if (!new_item) {
- HOOK_MALLOC_FAILED;
- break;
- }
- new_item->key = strdup(txt[i].key);
- if (!new_item->key) {
- free(new_item);
- break;
- }
- new_item->value = strdup(txt[i].value);
- if (!new_item->value) {
- free((char *)new_item->key);
- free(new_item);
- break;
- }
- new_item->value_len = strlen(new_item->value);
- new_item->next = new_txt;
- new_txt = new_item;
- }
- }
- return new_txt;
- }
- static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt)
- {
- mdns_txt_linked_item_t *t;
- while (txt) {
- t = txt;
- txt = txt->next;
- free((char *)t->value);
- free((char *)t->key);
- free(t);
- }
- }
- /**
- * @brief creates/allocates new service
- * @param service service type
- * @param proto service proto
- * @param port service port
- * @param instance service instance
- * @param num_items service number of txt items or 0
- * @param txt service txt items array or NULL
- *
- * @return pointer to the service or NULL on error
- */
- static mdns_service_t * _mdns_create_service(const char * service, const char * proto, const char * hostname,
- uint16_t port, const char * instance, size_t num_items,
- mdns_txt_item_t txt[])
- {
- mdns_service_t * s = (mdns_service_t *)malloc(sizeof(mdns_service_t));
- if (!s) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- mdns_txt_linked_item_t * new_txt = _mdns_allocate_txt(num_items, txt);
- if (num_items && new_txt == NULL) {
- free(s);
- return NULL;
- }
- s->priority = 0;
- s->weight = 0;
- s->instance = instance?strndup(instance, MDNS_NAME_BUF_LEN - 1):NULL;
- s->txt = new_txt;
- s->port = port;
- if (hostname) {
- s->hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
- if (!s->hostname) {
- free(s);
- return NULL;
- }
- } else {
- s->hostname = NULL;
- }
- s->service = strndup(service, MDNS_NAME_BUF_LEN - 1);
- if (!s->service) {
- free(s);
- return NULL;
- }
- s->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
- if (!s->proto) {
- free((char *)s->service);
- free(s);
- return NULL;
- }
- return s;
- }
- /**
- * @brief Remove and free service answer from answer list (destination)
- */
- static void _mdns_dealloc_scheduled_service_answers(mdns_out_answer_t ** destination, mdns_service_t * service)
- {
- mdns_out_answer_t * d = *destination;
- if (!d) {
- return;
- }
- while (d && d->service == service) {
- *destination = d->next;
- free(d);
- d = *destination;
- }
- while (d && d->next) {
- mdns_out_answer_t * a = d->next;
- if (a->service == service) {
- d->next = a->next;
- free(a);
- } else {
- d = d->next;
- }
- }
- }
- /**
- * @brief Find, remove and free answers and scheduled packets for service
- */
- static void _mdns_remove_scheduled_service_packets(mdns_service_t * service)
- {
- if (!service) {
- return;
- }
- mdns_tx_packet_t * p = NULL;
- mdns_tx_packet_t * q = _mdns_server->tx_queue_head;
- while (q) {
- bool had_answers = (q->answers != NULL);
- _mdns_dealloc_scheduled_service_answers(&(q->answers), service);
- _mdns_dealloc_scheduled_service_answers(&(q->additional), service);
- _mdns_dealloc_scheduled_service_answers(&(q->servers), service);
- mdns_pcb_t * _pcb = &_mdns_server->interfaces[q->tcpip_if].pcbs[q->ip_protocol];
- if(_pcb->pcb) {
- if (PCB_STATE_IS_PROBING(_pcb)) {
- uint8_t i;
- //check if we are probing this service
- for (i=0; i<_pcb->probe_services_len; i++) {
- mdns_srv_item_t * s = _pcb->probe_services[i];
- if (s->service == service){
- break;
- }
- }
- if (i < _pcb->probe_services_len) {
- if (_pcb->probe_services_len > 1) {
- uint8_t n;
- for (n=(i+1); n<_pcb->probe_services_len; n++) {
- _pcb->probe_services[n-1] = _pcb->probe_services[n];
- }
- _pcb->probe_services_len--;
- } else {
- _pcb->probe_services_len = 0;
- free(_pcb->probe_services);
- _pcb->probe_services = NULL;
- if (!_pcb->probe_ip) {
- _pcb->probe_running = false;
- _pcb->state = PCB_RUNNING;
- }
- }
- if (q->questions) {
- mdns_out_question_t * qsn = NULL;
- mdns_out_question_t * qs = q->questions;
- if (qs->type == MDNS_TYPE_ANY
- && qs->service && strcmp(qs->service, service->service) == 0
- && qs->proto && strcmp(qs->proto, service->proto) == 0)
- {
- q->questions = q->questions->next;
- free(qs);
- } else while (qs->next) {
- qsn = qs->next;
- if (qsn->type == MDNS_TYPE_ANY
- && qsn->service && strcmp(qsn->service, service->service) == 0
- && qsn->proto && strcmp(qsn->proto, service->proto) == 0)
- {
- qs->next = qsn->next;
- free(qsn);
- break;
- }
- qs = qs->next;
- }
- }
- }
- } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
- //if answers were cleared, set to running
- if (had_answers && q->answers == NULL) {
- _pcb->state = PCB_RUNNING;
- }
- }
- }
- p = q;
- q = q->next;
- if(!p->questions && !p->answers && !p->additional && !p->servers){
- queueDetach(mdns_tx_packet_t, _mdns_server->tx_queue_head, p);
- _mdns_free_tx_packet(p);
- }
- }
- }
- /**
- * @brief free service memory
- *
- * @param service the service
- */
- static void _mdns_free_service(mdns_service_t * service)
- {
- if (!service) {
- return;
- }
- free((char *)service->instance);
- free((char *)service->service);
- free((char *)service->proto);
- free((char *)service->hostname);
- while (service->txt) {
- mdns_txt_linked_item_t * s = service->txt;
- service->txt = service->txt->next;
- free((char *)s->key);
- free((char *)s->value);
- free(s);
- }
- free(service->txt);
- free(service);
- }
- /*
- * Received Packet Handling
- * */
- /**
- * @brief Detect SRV collision
- */
- 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)
- {
- if (_str_null_or_empty(_mdns_server->hostname)) {
- return 0;
- }
- size_t our_host_len = strlen(_mdns_server->hostname);
- size_t our_len = 14 + our_host_len;
- size_t their_host_len = strlen(host);
- size_t their_domain_len = strlen(domain);
- size_t their_len = 9 + their_host_len + their_domain_len;
- if (their_len > our_len) {
- return 1;//they win
- } else if (their_len < our_len) {
- return -1;//we win
- }
- uint16_t our_index = 0;
- uint8_t our_data[our_len];
- _mdns_append_u16(our_data, &our_index, service->priority);
- _mdns_append_u16(our_data, &our_index, service->weight);
- _mdns_append_u16(our_data, &our_index, service->port);
- our_data[our_index++] = our_host_len;
- memcpy(our_data + our_index, _mdns_server->hostname, our_host_len);
- our_index += our_host_len;
- our_data[our_index++] = 5;
- memcpy(our_data + our_index, MDNS_DEFAULT_DOMAIN, 5);
- our_index += 5;
- our_data[our_index++] = 0;
- uint16_t their_index = 0;
- uint8_t their_data[their_len];
- _mdns_append_u16(their_data, &their_index, priority);
- _mdns_append_u16(their_data, &their_index, weight);
- _mdns_append_u16(their_data, &their_index, port);
- their_data[their_index++] = their_host_len;
- memcpy(their_data + their_index, host, their_host_len);
- their_index += their_host_len;
- their_data[their_index++] = their_domain_len;
- memcpy(their_data + their_index, domain, their_domain_len);
- their_index += their_domain_len;
- their_data[their_index++] = 0;
- int ret = memcmp(our_data, their_data, our_len);
- if (ret > 0) {
- return -1;//we win
- } else if (ret < 0) {
- return 1;//they win
- }
- return 0;//same
- }
- /**
- * @brief Detect TXT collision
- */
- static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * data, size_t len)
- {
- size_t data_len = 0;
- if (len == 1 && service->txt) {
- return -1;//we win
- } else if (len > 1 && !service->txt) {
- return 1;//they win
- } else if (len == 1 && !service->txt) {
- return 0;//same
- }
- mdns_txt_linked_item_t * txt = service->txt;
- while (txt) {
- data_len += 2 + strlen(txt->key) + strlen(txt->value);
- txt = txt->next;
- }
- if (len > data_len) {
- return 1;//they win
- } else if (len < data_len) {
- return -1;//we win
- }
- uint8_t ours[len];
- uint16_t index = 0;
- char * tmp;
- txt = service->txt;
- while (txt) {
- tmp = (char *)malloc(2 + strlen(txt->key) + strlen(txt->value));
- if (tmp) {
- sprintf(tmp, "%s=%s", txt->key, txt->value);
- _mdns_append_string(ours, &index, tmp);
- free(tmp);
- } else {
- HOOK_MALLOC_FAILED;
- // continue
- }
- txt = txt->next;
- }
- int ret = memcmp(ours, data, len);
- if (ret > 0) {
- return -1;//we win
- } else if (ret < 0) {
- return 1;//they win
- }
- return 0;//same
- }
- /**
- * @brief Set interface as duplicate if another is found on the same subnet
- */
- static void _mdns_dup_interface(mdns_if_t tcpip_if)
- {
- uint8_t i;
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (other_if == MDNS_IF_MAX) {
- return; // no other interface found
- }
- for (i=0; i<MDNS_IP_PROTOCOL_MAX; i++) {
- if (_mdns_server->interfaces[other_if].pcbs[i].pcb) {
- //stop this interface and mark as dup
- if (_mdns_server->interfaces[tcpip_if].pcbs[i].pcb) {
- _mdns_clear_pcb_tx_queue_head(tcpip_if, i);
- _mdns_pcb_deinit(tcpip_if, i);
- }
- _mdns_server->interfaces[tcpip_if].pcbs[i].state = PCB_DUP;
- _mdns_announce_pcb(other_if, i, NULL, 0, true);
- }
- }
- }
- /**
- * @brief Detect IPv4 address collision
- */
- static int _mdns_check_a_collision(esp_ip4_addr_t * ip, mdns_if_t tcpip_if)
- {
- esp_netif_ip_info_t if_ip_info;
- esp_netif_ip_info_t other_ip_info;
- if (!ip->addr) {
- return 1;//denial! they win
- }
- if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
- return 1;//they win
- }
- int ret = memcmp((uint8_t*)&if_ip_info.ip.addr, (uint8_t*)&ip->addr, sizeof(esp_ip4_addr_t));
- if (ret > 0) {
- return -1;//we win
- } else if (ret < 0) {
- //is it the other interface?
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (other_if == MDNS_IF_MAX) {
- return 1;//AP interface! They win
- }
- if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &other_ip_info)) {
- return 1;//IPv4 not active! They win
- }
- if (ip->addr != other_ip_info.ip.addr) {
- return 1;//IPv4 not ours! They win
- }
- _mdns_dup_interface(tcpip_if);
- return 2;//they win
- }
- return 0;//same
- }
- #if CONFIG_LWIP_IPV6
- /**
- * @brief Detect IPv6 address collision
- */
- static int _mdns_check_aaaa_collision(esp_ip6_addr_t * ip, mdns_if_t tcpip_if)
- {
- struct esp_ip6_addr if_ip6;
- struct esp_ip6_addr other_ip6;
- if (_ipv6_address_is_zero(*ip)) {
- return 1;//denial! they win
- }
- if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) {
- return 1;//they win
- }
- int ret = memcmp((uint8_t*)&if_ip6.addr, (uint8_t*)ip->addr, _MDNS_SIZEOF_IP6_ADDR);
- if (ret > 0) {
- return -1;//we win
- } else if (ret < 0) {
- //is it the other interface?
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (other_if == MDNS_IF_MAX) {
- return 1;//AP interface! They win
- }
- if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) {
- return 1;//IPv6 not active! They win
- }
- if (memcmp((uint8_t*)&other_ip6.addr, (uint8_t*)ip->addr, _MDNS_SIZEOF_IP6_ADDR)) {
- return 1;//IPv6 not ours! They win
- }
- _mdns_dup_interface(tcpip_if);
- return 2;//they win
- }
- return 0;//same
- }
- #endif
- static bool _hostname_is_ours(const char * hostname)
- {
- if (strcasecmp(hostname, _mdns_server->hostname) == 0) {
- return true;
- }
- mdns_host_item_t * host = _mdns_host_list;
- while (host != NULL) {
- if (strcasecmp(hostname, host->hostname) == 0) {
- return true;
- }
- host = host->next;
- }
- return false;
- }
- static bool _mdns_delegate_hostname_add(const char * hostname, mdns_ip_addr_t * address_list)
- {
- if (_hostname_is_ours(hostname)) {
- return true;
- }
- mdns_host_item_t * host = (mdns_host_item_t *)malloc(sizeof(mdns_host_item_t));
- if (host == NULL) {
- return false;
- }
- host->address_list = address_list;
- host->hostname = hostname;
- host->next = _mdns_host_list;
- _mdns_host_list = host;
- return true;
- }
- static void free_address_list(mdns_ip_addr_t * address_list)
- {
- while (address_list != NULL) {
- mdns_ip_addr_t * next = address_list->next;
- free(address_list);
- address_list = next;
- }
- }
- static mdns_ip_addr_t * copy_address_list(const mdns_ip_addr_t * address_list)
- {
- mdns_ip_addr_t * head = NULL;
- mdns_ip_addr_t * tail = NULL;
- while (address_list != NULL) {
- mdns_ip_addr_t * addr = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
- if (addr == NULL) {
- free_address_list(head);
- return NULL;
- }
- addr->addr = address_list->addr;
- addr->next = NULL;
- if (head == NULL) {
- head = addr;
- tail = addr;
- } else {
- tail->next = addr;
- tail = tail->next;
- }
- address_list = address_list->next;
- }
- return head;
- }
- static bool _mdns_delegate_hostname_remove(const char * hostname)
- {
- mdns_srv_item_t * srv = _mdns_server->services;
- mdns_srv_item_t * prev_srv = NULL;
- while (srv) {
- if (strcasecmp(srv->service->hostname, hostname) == 0) {
- mdns_srv_item_t * to_free = srv;
- _mdns_send_bye(&srv, 1, false);
- _mdns_remove_scheduled_service_packets(srv->service);
- if (prev_srv == NULL) {
- _mdns_server->services = srv->next;
- srv = srv->next;
- } else {
- prev_srv->next = srv->next;
- srv = srv->next;
- }
- _mdns_free_service(to_free->service);
- free(to_free);
- } else {
- prev_srv = srv;
- srv = srv->next;
- }
- }
- mdns_host_item_t * host = _mdns_host_list;
- mdns_host_item_t * prev_host = NULL;
- while (host != NULL) {
- if (strcasecmp(hostname, host->hostname) == 0) {
- if (prev_host == NULL) {
- _mdns_host_list = host->next;
- } else {
- prev_host->next = host->next;
- }
- free_address_list(host->address_list);
- free((char *)host->hostname);
- free(host);
- break;
- } else {
- prev_host = host;
- host = host->next;
- }
- }
- return true;
- }
- /**
- * @brief Check if parsed name is discovery
- */
- static bool _mdns_name_is_discovery(mdns_name_t * name, uint16_t type)
- {
- return (
- (name->host && name->host[0] && !strcasecmp(name->host, "_services"))
- && (name->service && name->service[0] && !strcasecmp(name->service, "_dns-sd"))
- && (name->proto && name->proto[0] && !strcasecmp(name->proto, "_udp"))
- && (name->domain && name->domain[0] && !strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN))
- && type == MDNS_TYPE_PTR
- );
- }
- /**
- * @brief Check if the parsed name is ours (matches service or host name)
- */
- static bool _mdns_name_is_ours(mdns_name_t * name)
- {
- //domain have to be "local"
- if (_str_null_or_empty(name->domain) || strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN)) {
- return false;
- }
- //if service and proto are empty, host must match out hostname
- if (_str_null_or_empty(name->service) && _str_null_or_empty(name->proto)) {
- if (!_str_null_or_empty(name->host)
- && !_str_null_or_empty(_mdns_server->hostname)
- && _hostname_is_ours(name->host))
- {
- return true;
- }
- return false;
- }
- //if service or proto is empty, name is invalid
- if (_str_null_or_empty(name->service) || _str_null_or_empty(name->proto)) {
- return false;
- }
- //find the service
- mdns_srv_item_t * service;
- if (_str_null_or_empty(name->host)) {
- service = _mdns_get_service_item(name->service, name->proto, NULL);
- } else {
- service = _mdns_get_service_item_instance(name->host, name->service, name->proto, NULL);
- }
- if (!service) {
- return false;
- }
- //if host is empty and we have service, we have success
- if (_str_null_or_empty(name->host)) {
- return true;
- }
- //OK we have host in the name. find what is the instance of the service
- const char * instance = _mdns_get_service_instance_name(service->service);
- if (instance == NULL) {
- return false;
- }
- //compare the instance against the name
- if (strcasecmp(name->host, instance) == 0) {
- return true;
- }
- return false;
- }
- /**
- * @brief read uint16_t from a packet
- * @param packet the packet
- * @param index index in the packet where the value starts
- *
- * @return the value
- */
- static inline uint16_t _mdns_read_u16(const uint8_t * packet, uint16_t index)
- {
- return (uint16_t)(packet[index]) << 8 | packet[index+1];
- }
- /**
- * @brief read uint32_t from a packet
- * @param packet the packet
- * @param index index in the packet where the value starts
- *
- * @return the value
- */
- static inline uint32_t _mdns_read_u32(const uint8_t * packet, uint16_t index)
- {
- return (uint32_t)(packet[index]) << 24 | (uint32_t)(packet[index+1]) << 16 | (uint32_t)(packet[index+2]) << 8 | packet[index+3];
- }
- /**
- * @brief reads and formats MDNS FQDN into mdns_name_t structure
- *
- * @param packet MDNS packet
- * @param start Starting point of FQDN
- * @param name mdns_name_t structure to populate
- *
- * @return the address after the parsed FQDN in the packet or NULL on error
- */
- static const uint8_t * _mdns_parse_fqdn(const uint8_t * packet, const uint8_t * start, mdns_name_t * name)
- {
- name->parts = 0;
- name->sub = 0;
- name->host[0] = 0;
- name->service[0] = 0;
- name->proto[0] = 0;
- name->domain[0] = 0;
- name->invalid = false;
- static char buf[MDNS_NAME_BUF_LEN];
- const uint8_t * next_data = (uint8_t*)_mdns_read_fqdn(packet, start, name, buf);
- if (!next_data) {
- return 0;
- }
- if (!name->parts || name->invalid) {
- return next_data;
- }
- if (name->parts == 3) {
- memmove((uint8_t*)name + (MDNS_NAME_BUF_LEN), (uint8_t*)name, 3*(MDNS_NAME_BUF_LEN));
- name->host[0] = 0;
- } else if (name->parts == 2) {
- memmove((uint8_t*)(name->domain), (uint8_t*)(name->service), (MDNS_NAME_BUF_LEN));
- name->service[0] = 0;
- name->proto[0] = 0;
- }
- if (strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN) == 0 || strcasecmp(name->domain, "arpa") == 0) {
- return next_data;
- }
- name->invalid = true; // mark the current name invalid, but continue with other question
- return next_data;
- }
- /**
- * @brief Called from parser to check if question matches particular service
- */
- static bool _mdns_question_matches(mdns_parsed_question_t * question, uint16_t type, mdns_srv_item_t * service)
- {
- if (question->type != type) {
- return false;
- }
- if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
- return true;
- } else if (type == MDNS_TYPE_PTR || type == MDNS_TYPE_SDPTR) {
- if (!strcasecmp(service->service->service, question->service)
- && !strcasecmp(service->service->proto, question->proto)
- && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) {
- return true;
- }
- } else if (service && (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT)) {
- const char * name = _mdns_get_service_instance_name(service->service);
- if (name && question->host && question->service && question->proto && question->domain
- && !strcasecmp(name, question->host)
- && !strcasecmp(service->service->service, question->service)
- && !strcasecmp(service->service->proto, question->proto)
- && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) {
- return true;
- }
- }
- return false;
- }
- /**
- * @brief Removes saved question from parsed data
- */
- static void _mdns_remove_parsed_question(mdns_parsed_packet_t * parsed_packet, uint16_t type, mdns_srv_item_t * service)
- {
- mdns_parsed_question_t * q = parsed_packet->questions;
- if (_mdns_question_matches(q, type, service)) {
- parsed_packet->questions = q->next;
- free(q->host);
- free(q->service);
- free(q->proto);
- free(q->domain);
- free(q);
- return;
- }
- while (q->next) {
- mdns_parsed_question_t * p = q->next;
- if (_mdns_question_matches(p, type, service)) {
- q->next = p->next;
- free(p->host);
- free(p->service);
- free(p->proto);
- free(p->domain);
- free(p);
- return;
- }
- q = q->next;
- }
- }
- /**
- * @brief Get number of items in TXT parsed data
- */
- static int _mdns_txt_items_count_get(const uint8_t * data, size_t len)
- {
- if (len == 1) {
- return 0;
- }
- int num_items = 0;
- uint16_t i=0;
- size_t partLen = 0;
- while (i < len) {
- partLen = data[i++];
- if (!partLen) {
- break;
- }
- if ((i+partLen) > len) {
- return -1;//error
- }
- i+=partLen;
- num_items++;
- }
- return num_items;
- }
- /**
- * @brief Get the length of TXT item's key name
- */
- static int _mdns_txt_item_name_get_len(const uint8_t * data, size_t len)
- {
- if (*data == '=') {
- return -1;
- }
- for (size_t i = 0; i < len; i++) {
- if (data[i] == '=') {
- return i;
- }
- }
- return len;
- }
- /**
- * @brief Create TXT result array from parsed TXT data
- */
- static void _mdns_result_txt_create(const uint8_t *data, size_t len, mdns_txt_item_t **out_txt, uint8_t **out_value_len,
- size_t *out_count)
- {
- *out_txt = NULL;
- *out_count = 0;
- uint16_t i=0, y;
- size_t partLen = 0;
- int num_items = _mdns_txt_items_count_get(data, len);
- if (num_items < 0) {
- return;//error
- }
- if (!num_items) {
- return;
- }
- mdns_txt_item_t * txt = (mdns_txt_item_t *)malloc(sizeof(mdns_txt_item_t) * num_items);
- if (!txt) {
- HOOK_MALLOC_FAILED;
- return;
- }
- uint8_t * txt_value_len = (uint8_t *)malloc(num_items);
- if (!txt_value_len) {
- free(txt);
- HOOK_MALLOC_FAILED;
- return;
- }
- memset(txt, 0, sizeof(mdns_txt_item_t) * num_items);
- memset(txt_value_len, 0, num_items);
- size_t txt_num = 0;
- while (i < len) {
- partLen = data[i++];
- if (!partLen) {
- break;
- }
- if ((i+partLen) > len) {
- goto handle_error;//error
- }
- int name_len = _mdns_txt_item_name_get_len(data+i, partLen);
- if (name_len < 0) {//invalid item (no name)
- i += partLen;
- continue;
- }
- char * key = (char *)malloc(name_len + 1);
- if (!key) {
- HOOK_MALLOC_FAILED;
- goto handle_error;//error
- }
- mdns_txt_item_t * t = &txt[txt_num];
- uint8_t * value_len = &txt_value_len[txt_num];
- txt_num++;
- memcpy(key, data + i, name_len);
- key[name_len] = 0;
- i += name_len + 1;
- t->key = key;
- int new_value_len = partLen - name_len - 1;
- if (new_value_len > 0) {
- char * value = (char *)malloc(new_value_len + 1);
- if (!value) {
- HOOK_MALLOC_FAILED;
- goto handle_error;//error
- }
- memcpy(value, data + i, new_value_len);
- value[new_value_len] = 0;
- *value_len = new_value_len;
- i += new_value_len;
- t->value = value;
- }
- }
- *out_txt = txt;
- *out_count = txt_num;
- *out_value_len = txt_value_len;
- return;
- handle_error :
- for (y=0; y<txt_num; y++) {
- mdns_txt_item_t * t = &txt[y];
- free((char *)t->key);
- free((char *)t->value);
- }
- free(txt_value_len);
- free(txt);
- }
- /**
- * @brief Duplicate string or return error
- */
- static esp_err_t _mdns_strdup_check(char ** out, char * in)
- {
- if (in && in[0]) {
- *out = strdup(in);
- if (!*out) {
- return ESP_FAIL;
- }
- return ESP_OK;
- }
- *out = NULL;
- return ESP_OK;
- }
- /**
- * @brief main packet parser
- *
- * @param packet the packet
- */
- void mdns_parse_packet(mdns_rx_packet_t * packet)
- {
- static mdns_name_t n;
- mdns_header_t header;
- const uint8_t * data = _mdns_get_packet_data(packet);
- size_t len = _mdns_get_packet_len(packet);
- const uint8_t * content = data + MDNS_HEAD_LEN;
- bool do_not_reply = false;
- mdns_search_once_t * search_result = NULL;
- #ifdef MDNS_ENABLE_DEBUG
- _mdns_dbg_printf("\nRX[%u][%u]: ", packet->tcpip_if, (uint32_t)packet->ip_protocol);
- if (packet->src.type == ESP_IPADDR_TYPE_V4) {
- _mdns_dbg_printf("From: " IPSTR ":%u, To: " IPSTR ", ", IP2STR(&packet->src.u_addr.ip4), packet->src_port, IP2STR(&packet->dest.u_addr.ip4));
- } else {
- _mdns_dbg_printf("From: " IPV6STR ":%u, To: " IPV6STR ", ", IPV62STR(packet->src.u_addr.ip6), packet->src_port, IPV62STR(packet->dest.u_addr.ip6));
- }
- mdns_debug_packet(data, len);
- #endif
- mdns_parsed_packet_t * parsed_packet = (mdns_parsed_packet_t *)malloc(sizeof(mdns_parsed_packet_t));
- if (!parsed_packet) {
- HOOK_MALLOC_FAILED;
- return;
- }
- memset(parsed_packet, 0, sizeof(mdns_parsed_packet_t));
- mdns_name_t * name = &n;
- memset(name, 0, sizeof(mdns_name_t));
- header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
- header.flags.value = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
- header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
- header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
- header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
- header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
- if (header.flags.value == MDNS_FLAGS_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) {
- free(parsed_packet);
- return;
- }
- //if we have not set the hostname, we can not answer questions
- if (header.questions && !header.answers && _str_null_or_empty(_mdns_server->hostname)) {
- free(parsed_packet);
- return;
- }
- parsed_packet->tcpip_if = packet->tcpip_if;
- parsed_packet->ip_protocol = packet->ip_protocol;
- parsed_packet->multicast = packet->multicast;
- parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
- parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
- parsed_packet->id = header.id;
- esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src);
- parsed_packet->src_port = packet->src_port;
- if (header.questions) {
- uint8_t qs = header.questions;
- while (qs--) {
- content = _mdns_parse_fqdn(data, content, name);
- if (!content) {
- header.answers = 0;
- header.additional = 0;
- header.servers = 0;
- goto clear_rx_packet;//error
- }
- uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
- uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
- bool unicast = !!(mdns_class & 0x8000);
- mdns_class &= 0x7FFF;
- content = content + 4;
- if (mdns_class != 0x0001 || name->invalid) {//bad class or invalid name for this question entry
- continue;
- }
- if (_mdns_name_is_discovery(name, type)) {
- //service discovery
- parsed_packet->discovery = true;
- mdns_srv_item_t * a = _mdns_server->services;
- while (a) {
- mdns_parsed_question_t * question = (mdns_parsed_question_t *)calloc(1, sizeof(mdns_parsed_question_t));
- if (!question) {
- HOOK_MALLOC_FAILED;
- goto clear_rx_packet;
- }
- question->next = parsed_packet->questions;
- parsed_packet->questions = question;
- question->unicast = unicast;
- question->type = MDNS_TYPE_SDPTR;
- question->host = NULL;
- question->service = strdup(a->service->service);
- question->proto = strdup(a->service->proto);
- question->domain = strdup(MDNS_DEFAULT_DOMAIN);
- if (!question->service || !question->proto || !question->domain) {
- goto clear_rx_packet;
- }
- a = a->next;
- }
- continue;
- } else if (name->sub || !_mdns_name_is_ours(name)) {
- continue;
- }
- if (type == MDNS_TYPE_ANY && !_str_null_or_empty(name->host)) {
- parsed_packet->probe = true;
- }
- mdns_parsed_question_t * question = (mdns_parsed_question_t *)calloc(1, sizeof(mdns_parsed_question_t));
- if (!question) {
- HOOK_MALLOC_FAILED;
- goto clear_rx_packet;
- }
- question->next = parsed_packet->questions;
- parsed_packet->questions = question;
- question->unicast = unicast;
- question->type = type;
- if (_mdns_strdup_check(&(question->host), name->host)
- || _mdns_strdup_check(&(question->service), name->service)
- || _mdns_strdup_check(&(question->proto), name->proto)
- || _mdns_strdup_check(&(question->domain), name->domain)) {
- goto clear_rx_packet;
- }
- }
- }
- if (header.questions && !parsed_packet->questions && !parsed_packet->discovery && !header.answers) {
- goto clear_rx_packet;
- } else if (header.answers || header.servers || header.additional) {
- uint16_t recordIndex = 0;
- while (content < (data + len)) {
- content = _mdns_parse_fqdn(data, content, name);
- if (!content) {
- goto clear_rx_packet;//error
- }
- uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
- uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
- uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET);
- uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET);
- const uint8_t * data_ptr = content + MDNS_DATA_OFFSET;
- mdns_class &= 0x7FFF;
- content = data_ptr + data_len;
- if (content > (data + len)) {
- goto clear_rx_packet;
- }
- bool discovery = false;
- bool ours = false;
- mdns_srv_item_t * service = NULL;
- mdns_parsed_record_type_t record_type = MDNS_ANSWER;
- if (recordIndex >= (header.answers + header.servers)) {
- record_type = MDNS_EXTRA;
- } else if (recordIndex >= (header.answers)) {
- record_type = MDNS_NS;
- }
- recordIndex++;
- if (type == MDNS_TYPE_NSEC || type == MDNS_TYPE_OPT) {
- //skip NSEC and OPT
- continue;
- }
- if (parsed_packet->discovery && _mdns_name_is_discovery(name, type)) {
- discovery = true;
- } else if (!name->sub && _mdns_name_is_ours(name)) {
- ours = true;
- if (name->service && name->service[0] && name->proto && name->proto[0]) {
- service = _mdns_get_service_item(name->service, name->proto, NULL);
- }
- } else {
- if (!parsed_packet->authoritative || record_type == MDNS_NS) {
- //skip this record
- continue;
- }
- search_result = _mdns_search_find_from(_mdns_server->search_once, name, type, packet->tcpip_if, packet->ip_protocol);
- }
- if (type == MDNS_TYPE_PTR) {
- if (!_mdns_parse_fqdn(data, data_ptr, name)) {
- continue;//error
- }
- if (search_result) {
- _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
- packet->tcpip_if, packet->ip_protocol, ttl);
- } else if ((discovery || ours) && !name->sub && _mdns_name_is_ours(name)) {
- if (discovery) {
- service = _mdns_get_service_item(name->service, name->proto, NULL);
- _mdns_remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service);
- } else if (service && parsed_packet->questions && !parsed_packet->probe) {
- _mdns_remove_parsed_question(parsed_packet, type, service);
- } else if (service) {
- //check if TTL is more than half of the full TTL value (4500)
- if (ttl > 2250) {
- _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
- }
- }
- }
- } else if (type == MDNS_TYPE_SRV) {
- mdns_result_t * result = NULL;
- if (search_result && search_result->type == MDNS_TYPE_PTR) {
- result = search_result->result;
- while (result) {
- if (packet->tcpip_if == result->tcpip_if
- && packet->ip_protocol == result->ip_protocol
- && result->instance_name && !strcmp(name->host, result->instance_name)) {
- break;
- }
- result = result->next;
- }
- if (!result) {
- result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
- packet->tcpip_if, packet->ip_protocol, ttl);
- if (!result) {
- continue;//error
- }
- }
- }
- if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name)) {
- continue;//error
- }
- uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
- uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET);
- uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET);
- if (search_result) {
- if (search_result->type == MDNS_TYPE_PTR) {
- if (!result->hostname) { // assign host/port for this entry only if not previously set
- result->port = port;
- result->hostname = strdup(name->host);
- }
- } else {
- _mdns_search_result_add_srv(search_result, name->host, port, packet->tcpip_if, packet->ip_protocol, ttl);
- }
- } else if (ours) {
- if (parsed_packet->questions && !parsed_packet->probe) {
- _mdns_remove_parsed_question(parsed_packet, type, service);
- continue;
- } else if (parsed_packet->distributed) {
- _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
- continue;
- }
- //detect collision (-1=won, 0=none, 1=lost)
- int col = 0;
- if (mdns_class > 1) {
- col = 1;
- } else if (!mdns_class) {
- col = -1;
- } else if (service) { // only detect srv collision if service existed
- col = _mdns_check_srv_collision(service->service, priority, weight, port, name->host, name->domain);
- }
- if (service && col && (parsed_packet->probe || parsed_packet->authoritative)) {
- if (col > 0 || !port) {
- do_not_reply = true;
- if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
- _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
- if (!_str_null_or_empty(service->service->instance)) {
- char * new_instance = _mdns_mangle_name((char *)service->service->instance);
- if (new_instance) {
- free((char *)service->service->instance);
- service->service->instance = new_instance;
- }
- _mdns_probe_all_pcbs(&service, 1, false, false);
- } else if (!_str_null_or_empty(_mdns_server->instance)) {
- char * new_instance = _mdns_mangle_name((char *)_mdns_server->instance);
- if (new_instance) {
- free((char *)_mdns_server->instance);
- _mdns_server->instance = new_instance;
- }
- _mdns_restart_all_pcbs_no_instance();
- } else {
- char * new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
- if (new_host) {
- _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
- free((char *)_mdns_server->hostname);
- _mdns_server->hostname = new_host;
- _mdns_self_host.hostname = new_host;
- }
- _mdns_restart_all_pcbs();
- }
- } else if (service) {
- _mdns_pcb_send_bye(packet->tcpip_if, packet->ip_protocol, &service, 1, false);
- _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, false);
- }
- }
- } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions) {
- _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
- }
- }
- } else if (type == MDNS_TYPE_TXT) {
- if (search_result) {
- mdns_txt_item_t * txt = NULL;
- uint8_t *txt_value_len = NULL;
- size_t txt_count = 0;
- mdns_result_t * result = NULL;
- if (search_result->type == MDNS_TYPE_PTR) {
- result = search_result->result;
- while (result) {
- if (packet->tcpip_if == result->tcpip_if
- && packet->ip_protocol == result->ip_protocol
- && result->instance_name && !strcmp(name->host, result->instance_name)) {
- break;
- }
- result = result->next;
- }
- if (!result) {
- result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
- packet->tcpip_if, packet->ip_protocol, ttl);
- if (!result) {
- continue;//error
- }
- }
- if (!result->txt) {
- _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
- if (txt_count) {
- result->txt = txt;
- result->txt_count = txt_count;
- result->txt_value_len = txt_value_len;
- }
- }
- } else {
- _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
- if (txt_count) {
- _mdns_search_result_add_txt(search_result, txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl);
- }
- }
- } else if (ours) {
- if (parsed_packet->questions && !parsed_packet->probe) {
- _mdns_remove_parsed_question(parsed_packet, type, service);
- continue;
- }
- //detect collision (-1=won, 0=none, 1=lost)
- int col = 0;
- if (mdns_class > 1) {
- col = 1;
- } else if (!mdns_class) {
- col = -1;
- } else if (service) { // only detect txt collision if service existed
- col = _mdns_check_txt_collision(service->service, data_ptr, data_len);
- }
- if (col && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running && service) {
- do_not_reply = true;
- _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, true);
- } 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) {
- _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service);
- }
- }
- }
- #if CONFIG_LWIP_IPV6
- else if (type == MDNS_TYPE_AAAA) {//ipv6
- esp_ip_addr_t ip6;
- ip6.type = ESP_IPADDR_TYPE_V6;
- memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
- if (search_result) {
- //check for more applicable searches (PTR & A/AAAA at the same time)
- while (search_result) {
- _mdns_search_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl);
- search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
- }
- } else if (ours) {
- if (parsed_packet->questions && !parsed_packet->probe) {
- _mdns_remove_parsed_question(parsed_packet, type, NULL);
- continue;
- }
- //detect collision (-1=won, 0=none, 1=lost)
- int col = 0;
- if (mdns_class > 1) {
- col = 1;
- } else if (!mdns_class) {
- col = -1;
- } else {
- col = _mdns_check_aaaa_collision(&(ip6.u_addr.ip6), packet->tcpip_if);
- }
- if (col == 2) {
- goto clear_rx_packet;
- } else if (col == 1) {
- do_not_reply = true;
- if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
- if (col && (parsed_packet->probe || parsed_packet->authoritative)) {
- _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
- char * new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
- if (new_host) {
- _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
- free((char *)_mdns_server->hostname);
- _mdns_server->hostname = new_host;
- _mdns_self_host.hostname = new_host;
- }
- _mdns_restart_all_pcbs();
- }
- } else {
- _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true);
- }
- } 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) {
- _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL);
- }
- }
- }
- #endif
- else if (type == MDNS_TYPE_A) {
- esp_ip_addr_t ip;
- ip.type = ESP_IPADDR_TYPE_V4;
- memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
- if (search_result) {
- //check for more applicable searches (PTR & A/AAAA at the same time)
- while (search_result) {
- _mdns_search_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl);
- search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
- }
- } else if (ours) {
- if (parsed_packet->questions && !parsed_packet->probe) {
- _mdns_remove_parsed_question(parsed_packet, type, NULL);
- continue;
- }
- //detect collision (-1=won, 0=none, 1=lost)
- int col = 0;
- if (mdns_class > 1) {
- col = 1;
- } else if (!mdns_class) {
- col = -1;
- } else {
- col = _mdns_check_a_collision(&(ip.u_addr.ip4), packet->tcpip_if);
- }
- if (col == 2) {
- goto clear_rx_packet;
- } else if (col == 1) {
- do_not_reply = true;
- if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) {
- if (col && (parsed_packet->probe || parsed_packet->authoritative)) {
- _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++;
- char * new_host = _mdns_mangle_name((char *)_mdns_server->hostname);
- if (new_host) {
- _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host);
- free((char *)_mdns_server->hostname);
- _mdns_server->hostname = new_host;
- _mdns_self_host.hostname = new_host;
- }
- _mdns_restart_all_pcbs();
- }
- } else {
- _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true);
- }
- } 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) {
- _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL);
- }
- }
- }
- }
- //end while
- if (parsed_packet->authoritative) {
- _mdns_search_finish_done();
- }
- }
- if (!do_not_reply && _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].state > PCB_PROBE_3 && (parsed_packet->questions || parsed_packet->discovery)) {
- _mdns_create_answer_from_parsed_packet(parsed_packet);
- }
- clear_rx_packet:
- while (parsed_packet->questions) {
- mdns_parsed_question_t * question = parsed_packet->questions;
- parsed_packet->questions = parsed_packet->questions->next;
- if (question->host) {
- free(question->host);
- }
- if (question->service) {
- free(question->service);
- }
- if (question->proto) {
- free(question->proto);
- }
- if (question->domain) {
- free(question->domain);
- }
- free(question);
- }
- free(parsed_packet);
- }
- /**
- * @brief Enable mDNS interface
- */
- void _mdns_enable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- if (!_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
- if (_mdns_pcb_init(tcpip_if, ip_protocol)) {
- return;
- }
- }
- _mdns_restart_pcb(tcpip_if, ip_protocol);
- }
- /**
- * @brief Disable mDNS interface
- */
- void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- _mdns_clean_netif_ptr(tcpip_if);
- if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
- _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
- _mdns_pcb_deinit(tcpip_if, ip_protocol);
- mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
- if (other_if != MDNS_IF_MAX && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) {
- _mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF;
- _mdns_enable_pcb(other_if, ip_protocol);
- }
- }
- _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state = PCB_OFF;
- }
- /**
- * @brief Dispatch interface changes based on system events
- */
- static void _mdns_handle_system_event(esp_event_base_t event_base,
- int32_t event_id, esp_netif_t* interface)
- {
- if (!_mdns_server) {
- return;
- }
- esp_netif_dhcp_status_t dcst;
- if (event_base == WIFI_EVENT) {
- switch(event_id) {
- case WIFI_EVENT_STA_CONNECTED:
- if (!esp_netif_dhcpc_get_status(_mdns_get_esp_netif(MDNS_IF_STA), &dcst)) {
- if (dcst == ESP_NETIF_DHCP_STOPPED) {
- _mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
- }
- }
- break;
- case WIFI_EVENT_STA_DISCONNECTED:
- _mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
- _mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6);
- break;
- case WIFI_EVENT_AP_START:
- _mdns_enable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4);
- break;
- case WIFI_EVENT_AP_STOP:
- _mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4);
- _mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V6);
- break;
- default:
- break;
- }
- }
- #if CONFIG_ETH_ENABLED
- else if (event_base == ETH_EVENT) {
- switch (event_id) {
- case ETHERNET_EVENT_CONNECTED:
- if (!esp_netif_dhcpc_get_status(_mdns_get_esp_netif(MDNS_IF_ETH), &dcst)) {
- if (dcst == ESP_NETIF_DHCP_STOPPED) {
- _mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
- }
- }
- break;
- case ETHERNET_EVENT_DISCONNECTED:
- _mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
- _mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V6);
- break;
- default:
- break;
- }
- }
- #endif
- else if (event_base == IP_EVENT) {
- switch (event_id) {
- case IP_EVENT_STA_GOT_IP:
- _mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4);
- _mdns_announce_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6, NULL, 0, true);
- break;
- #if CONFIG_ETH_ENABLED
- case IP_EVENT_ETH_GOT_IP:
- _mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4);
- break;
- #endif
- case IP_EVENT_GOT_IP6:
- {
- mdns_if_t mdns_if = _mdns_get_if_from_esp_netif(interface);
- if (mdns_if != MDNS_IF_MAX) {
- _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
- _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4, NULL, 0, true);
- }
- }
- break;
- default:
- break;
- }
- }
- }
- /*
- * MDNS Search
- * */
- /**
- * @brief Free search structure (except the results)
- */
- static void _mdns_search_free(mdns_search_once_t * search)
- {
- free(search->instance);
- free(search->service);
- free(search->proto);
- vSemaphoreDelete(search->done_semaphore);
- free(search);
- }
- /**
- * @brief Allocate new search structure
- */
- static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type,
- uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier)
- {
- mdns_search_once_t * search = (mdns_search_once_t *)malloc(sizeof(mdns_search_once_t));
- if (!search) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- memset(search, 0, sizeof(mdns_search_once_t));
- search->done_semaphore = xSemaphoreCreateBinary();
- if (!search->done_semaphore) {
- free(search);
- return NULL;
- }
- if (!_str_null_or_empty(name)) {
- search->instance = strndup(name, MDNS_NAME_BUF_LEN-1);
- if (!search->instance) {
- _mdns_search_free(search);
- return NULL;
- }
- }
- if (!_str_null_or_empty(service)) {
- search->service = strndup(service, MDNS_NAME_BUF_LEN-1);
- if (!search->service) {
- _mdns_search_free(search);
- return NULL;
- }
- }
- if (!_str_null_or_empty(proto)) {
- search->proto = strndup(proto, MDNS_NAME_BUF_LEN-1);
- if (!search->proto) {
- _mdns_search_free(search);
- return NULL;
- }
- }
- search->type = type;
- search->timeout = timeout;
- search->num_results = 0;
- search->max_results = max_results;
- search->result = NULL;
- search->state = SEARCH_INIT;
- search->sent_at = 0;
- search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS;
- search->notifier = notifier;
- search->next = NULL;
- return search;
- }
- /**
- * @brief Mark search as finished and remove it from search chain
- */
- static void _mdns_search_finish(mdns_search_once_t * search)
- {
- search->state = SEARCH_OFF;
- queueDetach(mdns_search_once_t, _mdns_server->search_once, search);
- if (search->notifier) {
- search->notifier(search);
- }
- xSemaphoreGive(search->done_semaphore);
- }
- /**
- * @brief Add new search to the search chain
- */
- static void _mdns_search_add(mdns_search_once_t * search)
- {
- search->next = _mdns_server->search_once;
- _mdns_server->search_once = search;
- }
- /**
- * @brief Called from parser to finish any searches that have reached maximum results
- */
- static void _mdns_search_finish_done(void)
- {
- mdns_search_once_t * search = _mdns_server->search_once;
- mdns_search_once_t * s = NULL;
- while (search) {
- s = search;
- search = search->next;
- if (s->max_results && s->num_results >= s->max_results) {
- _mdns_search_finish(s);
- }
- }
- }
- /**
- * @brief Create linked IP (copy) from parsed one
- */
- static mdns_ip_addr_t * _mdns_result_addr_create_ip(esp_ip_addr_t * ip)
- {
- mdns_ip_addr_t * a = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
- if (!a) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- memset(a, 0 , sizeof(mdns_ip_addr_t));
- a->addr.type = ip->type;
- if (ip->type == ESP_IPADDR_TYPE_V6) {
- memcpy(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16);
- } else {
- a->addr.u_addr.ip4.addr = ip->u_addr.ip4.addr;
- }
- return a;
- }
- static inline void _mdns_result_update_ttl(mdns_result_t * r, uint32_t ttl)
- {
- r->ttl = r->ttl < ttl ? r->ttl : ttl;
- }
- /**
- * @brief Chain new IP to search result
- */
- static void _mdns_result_add_ip(mdns_result_t * r, esp_ip_addr_t * ip)
- {
- mdns_ip_addr_t * a = r->addr;
- while (a) {
- if (a->addr.type == ip->type) {
- if (a->addr.type == ESP_IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) {
- return;
- }
- if (a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) {
- return;
- }
- }
- a = a->next;
- }
- a = _mdns_result_addr_create_ip(ip);
- if (!a) {
- return;
- }
- a->next = r->addr;
- r->addr = a;
- }
- /**
- * @brief Called from parser to add A/AAAA data to search result
- */
- static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip,
- mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
- {
- mdns_result_t * r = NULL;
- mdns_ip_addr_t * a = NULL;
- if ((search->type == MDNS_TYPE_A && ip->type == ESP_IPADDR_TYPE_V4)
- || (search->type == MDNS_TYPE_AAAA && ip->type == ESP_IPADDR_TYPE_V6)
- || search->type == MDNS_TYPE_ANY) {
- r = search->result;
- while (r) {
- if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
- _mdns_result_add_ip(r, ip);
- _mdns_result_update_ttl(r, ttl);
- return;
- }
- r = r->next;
- }
- if (!search->max_results || search->num_results < search->max_results) {
- r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
- if (!r) {
- HOOK_MALLOC_FAILED;
- return;
- }
- memset(r, 0 , sizeof(mdns_result_t));
- a = _mdns_result_addr_create_ip(ip);
- if (!a) {
- free(r);
- return;
- }
- a->next = r->addr;
- r->hostname = strdup(hostname);
- r->addr = a;
- r->tcpip_if = tcpip_if;
- r->ip_protocol = ip_protocol;
- r->next = search->result;
- r->ttl = ttl;
- search->result = r;
- search->num_results++;
- }
- } else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) {
- r = search->result;
- while (r) {
- if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
- _mdns_result_add_ip(r, ip);
- _mdns_result_update_ttl(r, ttl);
- break;
- }
- r = r->next;
- }
- }
- }
- /**
- * @brief Called from parser to add PTR data to search result
- */
- static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance,
- const char * service_type, const char * proto, mdns_if_t tcpip_if,
- mdns_ip_protocol_t ip_protocol, uint32_t ttl)
- {
- mdns_result_t * r = search->result;
- while (r) {
- if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) {
- _mdns_result_update_ttl(r, ttl);
- return r;
- }
- r = r->next;
- }
- if (!search->max_results || search->num_results < search->max_results) {
- r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
- if (!r) {
- HOOK_MALLOC_FAILED;
- return NULL;
- }
- memset(r, 0 , sizeof(mdns_result_t));
- r->instance_name = strdup(instance);
- r->service_type = strdup(service_type);
- r->proto = strdup(proto);
- if (!r->instance_name) {
- free(r);
- return NULL;
- }
- r->tcpip_if = tcpip_if;
- r->ip_protocol = ip_protocol;
- r->ttl = ttl;
- r->next = search->result;
- search->result = r;
- search->num_results++;
- return r;
- }
- return NULL;
- }
- /**
- * @brief Called from parser to add SRV data to search result
- */
- static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
- mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
- {
- mdns_result_t * r = search->result;
- while (r) {
- if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
- _mdns_result_update_ttl(r, ttl);
- return;
- }
- r = r->next;
- }
- if (!search->max_results || search->num_results < search->max_results) {
- r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
- if (!r) {
- HOOK_MALLOC_FAILED;
- return;
- }
- memset(r, 0 , sizeof(mdns_result_t));
- r->hostname = strdup(hostname);
- if (!r->hostname) {
- free(r);
- return;
- }
- if (search->instance) {
- r->instance_name = strdup(search->instance);
- }
- r->service_type = strdup(search->service);
- r->proto = strdup(search->proto);
- r->port = port;
- r->tcpip_if = tcpip_if;
- r->ip_protocol = ip_protocol;
- r->ttl = ttl;
- r->next = search->result;
- search->result = r;
- search->num_results++;
- }
- }
- /**
- * @brief Called from parser to add TXT data to search result
- */
- static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
- size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
- uint32_t ttl)
- {
- mdns_result_t * r = search->result;
- while (r) {
- if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
- if (r->txt) {
- goto free_txt;
- }
- r->txt = txt;
- r->txt_value_len = txt_value_len;
- r->txt_count = txt_count;
- _mdns_result_update_ttl(r, ttl);
- return;
- }
- r = r->next;
- }
- if (!search->max_results || search->num_results < search->max_results) {
- r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
- if (!r) {
- HOOK_MALLOC_FAILED;
- goto free_txt;
- }
- memset(r, 0 , sizeof(mdns_result_t));
- r->txt = txt;
- r->txt_value_len = txt_value_len;
- r->txt_count = txt_count;
- r->tcpip_if = tcpip_if;
- r->ip_protocol = ip_protocol;
- r->ttl = ttl;
- r->next = search->result;
- search->result = r;
- search->num_results++;
- }
- return;
- free_txt:
- for (size_t i=0; i<txt_count; i++) {
- free((char *)(txt[i].key));
- free((char *)(txt[i].value));
- }
- free(txt);
- }
- /**
- * @brief Called from packet parser to find matching running search
- */
- 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)
- {
- mdns_result_t * r = NULL;
- while (s) {
- if (s->state == SEARCH_OFF) {
- s = s->next;
- continue;
- }
- if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
- if ((s->type == MDNS_TYPE_ANY && s->service != NULL)
- || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV))
- {
- s = s->next;
- continue;
- }
- if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) {
- if (!strcasecmp(name->host, s->instance)) {
- return s;
- }
- s = s->next;
- continue;
- }
- r = s->result;
- while (r) {
- if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) {
- return s;
- }
- r = r->next;
- }
- s = s->next;
- continue;
- }
- if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) {
- if ((s->type == MDNS_TYPE_ANY && s->service == NULL)
- || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR))
- {
- s = s->next;
- continue;
- }
- if (strcasecmp(name->service, s->service)
- || strcasecmp(name->proto, s->proto))
- {
- s = s->next;
- continue;
- }
- if (s->type != MDNS_TYPE_PTR) {
- if (!strcasecmp(name->host, s->instance)) {
- return s;
- }
- s = s->next;
- continue;
- }
- return s;
- }
- if (type == MDNS_TYPE_PTR && type == s->type && !strcasecmp(name->service, s->service) && !strcasecmp(name->proto, s->proto)) {
- return s;
- }
- s = s->next;
- }
- return NULL;
- }
- /**
- * @brief Create search packet for particular interface
- */
- 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)
- {
- mdns_result_t * r = NULL;
- mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol);
- if (!packet) {
- return NULL;
- }
- mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
- if (!q) {
- HOOK_MALLOC_FAILED;
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- q->next = NULL;
- q->unicast = search->type != MDNS_TYPE_PTR;
- q->type = search->type;
- q->host = search->instance;
- q->service = search->service;
- q->proto = search->proto;
- q->domain = MDNS_DEFAULT_DOMAIN;
- q->own_dynamic_memory = false;
- queueToEnd(mdns_out_question_t, packet->questions, q);
- if (search->type == MDNS_TYPE_PTR) {
- r = search->result;
- while (r) {
- //full record on the same interface is available
- if (r->tcpip_if != tcpip_if || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) {
- r = r->next;
- continue;
- }
- mdns_out_answer_t * a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
- if (!a) {
- HOOK_MALLOC_FAILED;
- _mdns_free_tx_packet(packet);
- return NULL;
- }
- a->type = MDNS_TYPE_PTR;
- a->service = NULL;
- a->custom_instance = r->instance_name;
- a->custom_service = search->service;
- a->custom_proto = search->proto;
- a->bye = false;
- a->flush = false;
- a->next = NULL;
- queueToEnd(mdns_out_answer_t, packet->answers, a);
- r = r->next;
- }
- }
- return packet;
- }
- /**
- * @brief Send search packet to particular interface
- */
- static void _mdns_search_send_pcb(mdns_search_once_t * search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
- {
- mdns_tx_packet_t * packet = NULL;
- if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) {
- packet = _mdns_create_search_packet(search, tcpip_if, ip_protocol);
- if (!packet) {
- return;
- }
- _mdns_dispatch_tx_packet(packet);
- _mdns_free_tx_packet(packet);
- }
- }
- /**
- * @brief Send search packet to all available interfaces
- */
- static void _mdns_search_send(mdns_search_once_t * search)
- {
- mdns_search_once_t* queue = _mdns_server->search_once;
- bool found = false;
- // looking for this search in active searches
- while (queue) {
- if (queue == search) {
- found = true;
- break;
- }
- queue = queue->next;
- }
- if (!found) {
- // no longer active -> skip sending this search
- return;
- }
- uint8_t i, j;
- for (i=0; i<MDNS_IF_MAX; i++) {
- for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
- _mdns_search_send_pcb(search, (mdns_if_t)i, (mdns_ip_protocol_t)j);
- }
- }
- }
- static void _mdns_tx_handle_packet(mdns_tx_packet_t * p)
- {
- mdns_tx_packet_t * a = NULL;
- mdns_out_question_t * q = NULL;
- mdns_pcb_t * pcb = &_mdns_server->interfaces[p->tcpip_if].pcbs[p->ip_protocol];
- uint32_t send_after = 1000;
- if (pcb->state == PCB_OFF) {
- _mdns_free_tx_packet(p);
- return;
- }
- _mdns_dispatch_tx_packet(p);
- switch(pcb->state) {
- case PCB_PROBE_1:
- q = p->questions;
- while (q) {
- q->unicast = false;
- q = q->next;
- }
- //fallthrough
- case PCB_PROBE_2:
- _mdns_schedule_tx_packet(p, 250);
- pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1);
- break;
- case PCB_PROBE_3:
- a = _mdns_create_announce_from_probe(p);
- if (!a) {
- _mdns_schedule_tx_packet(p, 250);
- break;
- }
- pcb->probe_running = false;
- pcb->probe_ip = false;
- pcb->probe_services_len = 0;
- pcb->failed_probes = 0;
- free(pcb->probe_services);
- pcb->probe_services = NULL;
- _mdns_free_tx_packet(p);
- p = a;
- send_after = 250;
- //fallthrough
- case PCB_ANNOUNCE_1:
- //fallthrough
- case PCB_ANNOUNCE_2:
- _mdns_schedule_tx_packet(p, send_after);
- pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1);
- break;
- case PCB_ANNOUNCE_3:
- pcb->state = PCB_RUNNING;
- _mdns_free_tx_packet(p);
- break;
- default:
- _mdns_free_tx_packet(p);
- break;
- }
- }
- static void _mdns_remap_self_service_hostname(const char * old_hostname, const char * new_hostname)
- {
- mdns_srv_item_t * service = _mdns_server->services;
- while (service) {
- if (strcmp(service->service->hostname, old_hostname) == 0) {
- free((char *)service->service->hostname);
- service->service->hostname = strdup(new_hostname);
- }
- service = service->next;
- }
- }
- /**
- * @brief Free action data
- */
- static void _mdns_free_action(mdns_action_t * action)
- {
- switch(action->type) {
- case ACTION_HOSTNAME_SET:
- free(action->data.hostname_set.hostname);
- break;
- case ACTION_INSTANCE_SET:
- free(action->data.instance);
- break;
- case ACTION_SERVICE_ADD:
- _mdns_free_service(action->data.srv_add.service->service);
- free(action->data.srv_add.service);
- break;
- case ACTION_SERVICE_INSTANCE_SET:
- free(action->data.srv_instance.instance);
- break;
- case ACTION_SERVICE_TXT_REPLACE:
- _mdns_free_linked_txt(action->data.srv_txt_replace.txt);
- break;
- case ACTION_SERVICE_TXT_SET:
- free(action->data.srv_txt_set.key);
- free(action->data.srv_txt_set.value);
- break;
- case ACTION_SERVICE_TXT_DEL:
- free(action->data.srv_txt_del.key);
- break;
- case ACTION_SEARCH_ADD:
- //fallthrough
- case ACTION_SEARCH_SEND:
- //fallthrough
- case ACTION_SEARCH_END:
- _mdns_search_free(action->data.search_add.search);
- break;
- case ACTION_TX_HANDLE:
- _mdns_free_tx_packet(action->data.tx_handle.packet);
- break;
- case ACTION_RX_HANDLE:
- _mdns_packet_free(action->data.rx_handle.packet);
- break;
- case ACTION_DELEGATE_HOSTNAME_ADD:
- free((char *)action->data.delegate_hostname.hostname);
- free_address_list(action->data.delegate_hostname.address_list);
- break;
- case ACTION_DELEGATE_HOSTNAME_REMOVE:
- free((char *)action->data.delegate_hostname.hostname);
- break;
- default:
- break;
- }
- free(action);
- }
- /**
- * @brief Called from service thread to execute given action
- */
- static void _mdns_execute_action(mdns_action_t * action)
- {
- mdns_srv_item_t * a = NULL;
- mdns_service_t * service;
- char * key;
- char * value;
- mdns_txt_linked_item_t * txt, * t;
- switch(action->type) {
- case ACTION_SYSTEM_EVENT:
- _mdns_handle_system_event(action->data.sys_event.event_base,
- action->data.sys_event.event_id, action->data.sys_event.interface);
- break;
- case ACTION_HOSTNAME_SET:
- _mdns_send_bye_all_pcbs_no_instance(true);
- _mdns_remap_self_service_hostname(_mdns_server->hostname, action->data.hostname_set.hostname);
- free((char*)_mdns_server->hostname);
- _mdns_server->hostname = action->data.hostname_set.hostname;
- _mdns_self_host.hostname = action->data.hostname_set.hostname;
- _mdns_restart_all_pcbs();
- xTaskNotifyGive(action->data.hostname_set.calling_task);
- break;
- case ACTION_INSTANCE_SET:
- _mdns_send_bye_all_pcbs_no_instance(false);
- free((char*)_mdns_server->instance);
- _mdns_server->instance = action->data.instance;
- _mdns_restart_all_pcbs_no_instance();
- break;
- case ACTION_SERVICE_ADD:
- action->data.srv_add.service->next = _mdns_server->services;
- _mdns_server->services = action->data.srv_add.service;
- _mdns_probe_all_pcbs(&action->data.srv_add.service, 1, false, false);
- break;
- case ACTION_SERVICE_INSTANCE_SET:
- if (action->data.srv_instance.service->service->instance) {
- _mdns_send_bye(&action->data.srv_instance.service, 1, false);
- free((char*)action->data.srv_instance.service->service->instance);
- }
- action->data.srv_instance.service->service->instance = action->data.srv_instance.instance;
- _mdns_probe_all_pcbs(&action->data.srv_instance.service, 1, false, false);
- break;
- case ACTION_SERVICE_PORT_SET:
- action->data.srv_port.service->service->port = action->data.srv_port.port;
- _mdns_announce_all_pcbs(&action->data.srv_port.service, 1, true);
- break;
- case ACTION_SERVICE_TXT_REPLACE:
- service = action->data.srv_txt_replace.service->service;
- txt = service->txt;
- service->txt = NULL;
- _mdns_free_linked_txt(txt);
- service->txt = action->data.srv_txt_replace.txt;
- _mdns_announce_all_pcbs(&action->data.srv_txt_replace.service, 1, false);
- break;
- case ACTION_SERVICE_TXT_SET:
- service = action->data.srv_txt_set.service->service;
- key = action->data.srv_txt_set.key;
- value = action->data.srv_txt_set.value;
- txt = service->txt;
- while (txt) {
- if (strcmp(txt->key, key) == 0) {
- free((char *)txt->value);
- free(key);
- txt->value = value;
- txt->value_len = action->data.srv_txt_set.value_len;
- break;
- }
- txt = txt->next;
- }
- if (!txt) {
- txt = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
- if (!txt) {
- HOOK_MALLOC_FAILED;
- _mdns_free_action(action);
- return;
- }
- txt->key = key;
- txt->value = value;
- txt->value_len = action->data.srv_txt_set.value_len;
- txt->next = service->txt;
- service->txt = txt;
- }
- _mdns_announce_all_pcbs(&action->data.srv_txt_set.service, 1, false);
- break;
- case ACTION_SERVICE_TXT_DEL:
- service = action->data.srv_txt_del.service->service;
- key = action->data.srv_txt_del.key;
- txt = service->txt;
- if (!txt) {
- break;
- }
- if (strcmp(txt->key, key) == 0) {
- service->txt = txt->next;
- free((char *)txt->key);
- free((char *)txt->value);
- free(txt);
- } else {
- while (txt->next) {
- if (strcmp(txt->next->key, key) == 0) {
- t = txt->next;
- txt->next = t->next;
- free((char *)t->key);
- free((char *)t->value);
- free(t);
- break;
- } else {
- txt = txt->next;
- }
- }
- }
- free(key);
- _mdns_announce_all_pcbs(&action->data.srv_txt_set.service, 1, false);
- break;
- case ACTION_SERVICE_DEL:
- a = _mdns_server->services;
- if (action->data.srv_del.service) {
- if (_mdns_server->services == action->data.srv_del.service) {
- _mdns_server->services = a->next;
- _mdns_send_bye(&a, 1, false);
- _mdns_remove_scheduled_service_packets(a->service);
- _mdns_free_service(a->service);
- free(a);
- } else {
- while (a->next && a->next != action->data.srv_del.service) {
- a = a->next;
- }
- if (a->next == action->data.srv_del.service) {
- mdns_srv_item_t * b = a->next;
- a->next = a->next->next;
- _mdns_send_bye(&b, 1, false);
- _mdns_remove_scheduled_service_packets(b->service);
- _mdns_free_service(b->service);
- free(b);
- }
- }
- }
- break;
- case ACTION_SERVICES_CLEAR:
- _mdns_send_final_bye(false);
- a = _mdns_server->services;
- _mdns_server->services = NULL;
- while (a) {
- mdns_srv_item_t * s = a;
- a = a->next;
- _mdns_remove_scheduled_service_packets(s->service);
- _mdns_free_service(s->service);
- free(s);
- }
- break;
- case ACTION_SEARCH_ADD:
- _mdns_search_add(action->data.search_add.search);
- break;
- case ACTION_SEARCH_SEND:
- _mdns_search_send(action->data.search_add.search);
- break;
- case ACTION_SEARCH_END:
- _mdns_search_finish(action->data.search_add.search);
- break;
- case ACTION_TX_HANDLE:
- {
- mdns_tx_packet_t * p = _mdns_server->tx_queue_head;
- // packet to be handled should be at tx head, but must be consistent with the one pushed to action queue
- if (p && p==action->data.tx_handle.packet && p->queued) {
- p->queued = false; // clearing, as the packet might be reused (pushed and transmitted again)
- _mdns_server->tx_queue_head = p->next;
- _mdns_tx_handle_packet(p);
- } else {
- ESP_LOGD(TAG, "Skipping transmit of an unexpected packet!");
- }
- }
- break;
- case ACTION_RX_HANDLE:
- mdns_parse_packet(action->data.rx_handle.packet);
- _mdns_packet_free(action->data.rx_handle.packet);
- break;
- case ACTION_DELEGATE_HOSTNAME_ADD:
- _mdns_delegate_hostname_add(action->data.delegate_hostname.hostname,
- action->data.delegate_hostname.address_list);
- break;
- case ACTION_DELEGATE_HOSTNAME_REMOVE:
- _mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname);
- free((char *)action->data.delegate_hostname.hostname);
- break;
- default:
- break;
- }
- free(action);
- }
- /**
- * @brief Queue search action
- */
- static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t * search)
- {
- mdns_action_t * action = NULL;
- action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = type;
- action->data.search_add.search = search;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- /**
- * @brief Called from timer task to run mDNS responder
- *
- * periodically checks first unqueued packet (from tx head).
- * if it is scheduled to be transmitted, then pushes the packet to action queue to be handled.
- *
- */
- static void _mdns_scheduler_run(void)
- {
- MDNS_SERVICE_LOCK();
- mdns_tx_packet_t * p = _mdns_server->tx_queue_head;
- mdns_action_t * action = NULL;
- // find first unqueued packet
- while (p && p->queued) {
- p = p->next;
- }
- if (!p) {
- MDNS_SERVICE_UNLOCK();
- return;
- }
- if ((int32_t)(p->send_at - (xTaskGetTickCount() * portTICK_PERIOD_MS)) < 0) {
- action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (action) {
- action->type = ACTION_TX_HANDLE;
- action->data.tx_handle.packet = p;
- p->queued = true;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- p->queued = false;
- }
- } else {
- HOOK_MALLOC_FAILED;
- // continue
- }
- }
- MDNS_SERVICE_UNLOCK();
- }
- /**
- * @brief Called from timer task to run active searches
- */
- static void _mdns_search_run(void)
- {
- MDNS_SERVICE_LOCK();
- mdns_search_once_t * s = _mdns_server->search_once;
- uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS;
- if (!s) {
- MDNS_SERVICE_UNLOCK();
- return;
- }
- while (s) {
- if (s->state != SEARCH_OFF) {
- if (now > (s->started_at + s->timeout)) {
- s->state = SEARCH_OFF;
- if (_mdns_send_search_action(ACTION_SEARCH_END, s) != ESP_OK) {
- s->state = SEARCH_RUNNING;
- }
- } else if (s->state == SEARCH_INIT || (now - s->sent_at) > 1000) {
- s->state = SEARCH_RUNNING;
- s->sent_at = now;
- if (_mdns_send_search_action(ACTION_SEARCH_SEND, s) != ESP_OK) {
- s->sent_at -= 1000;
- }
- }
- }
- s = s->next;
- }
- MDNS_SERVICE_UNLOCK();
- }
- /**
- * @brief the main MDNS service task. Packets are received and parsed here
- */
- static void _mdns_service_task(void *pvParameters)
- {
- mdns_action_t * a = NULL;
- for (;;) {
- if (_mdns_server && _mdns_server->action_queue) {
- if (xQueueReceive(_mdns_server->action_queue, &a, portMAX_DELAY) == pdTRUE) {
- if (a->type == ACTION_TASK_STOP) {
- break;
- }
- MDNS_SERVICE_LOCK();
- _mdns_execute_action(a);
- MDNS_SERVICE_UNLOCK();
- }
- } else {
- vTaskDelay(500 * portTICK_PERIOD_MS);
- }
- }
- _mdns_service_task_handle = NULL;
- vTaskDelete(NULL);
- }
- static void _mdns_timer_cb(void * arg)
- {
- _mdns_scheduler_run();
- _mdns_search_run();
- }
- static esp_err_t _mdns_start_timer(void){
- esp_timer_create_args_t timer_conf = {
- .callback = _mdns_timer_cb,
- .arg = NULL,
- .dispatch_method = ESP_TIMER_TASK,
- .name = "mdns_timer"
- };
- esp_err_t err = esp_timer_create(&timer_conf, &(_mdns_server->timer_handle));
- if (err) {
- return err;
- }
- return esp_timer_start_periodic(_mdns_server->timer_handle, MDNS_TIMER_PERIOD_US);
- }
- static esp_err_t _mdns_stop_timer(void){
- esp_err_t err = ESP_OK;
- if (_mdns_server->timer_handle) {
- err = esp_timer_stop(_mdns_server->timer_handle);
- if (err) {
- return err;
- }
- err = esp_timer_delete(_mdns_server->timer_handle);
- }
- return err;
- }
- /**
- * @brief Start the service thread if not running
- *
- * @return
- * - ESP_OK on success
- * - ESP_FAIL on error
- */
- static esp_err_t _mdns_service_task_start(void)
- {
- if (!_mdns_service_semaphore) {
- _mdns_service_semaphore = xSemaphoreCreateMutex();
- if (!_mdns_service_semaphore) {
- return ESP_FAIL;
- }
- }
- MDNS_SERVICE_LOCK();
- if (_mdns_start_timer()) {
- MDNS_SERVICE_UNLOCK();
- return ESP_FAIL;
- }
- if (!_mdns_service_task_handle) {
- xTaskCreatePinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY,
- (TaskHandle_t * const)(&_mdns_service_task_handle), MDNS_TASK_AFFINITY);
- if (!_mdns_service_task_handle) {
- _mdns_stop_timer();
- MDNS_SERVICE_UNLOCK();
- vSemaphoreDelete(_mdns_service_semaphore);
- _mdns_service_semaphore = NULL;
- return ESP_FAIL;
- }
- }
- MDNS_SERVICE_UNLOCK();
- return ESP_OK;
- }
- /**
- * @brief Stop the service thread
- *
- * @return
- * - ESP_OK
- */
- static esp_err_t _mdns_service_task_stop(void)
- {
- _mdns_stop_timer();
- if (_mdns_service_task_handle) {
- mdns_action_t action;
- mdns_action_t * a = &action;
- action.type = ACTION_TASK_STOP;
- if (xQueueSend(_mdns_server->action_queue, &a, (portTickType)0) != pdPASS) {
- vTaskDelete(_mdns_service_task_handle);
- _mdns_service_task_handle = NULL;
- }
- while (_mdns_service_task_handle) {
- vTaskDelay(10 / portTICK_PERIOD_MS);
- }
- }
- vSemaphoreDelete(_mdns_service_semaphore);
- _mdns_service_semaphore = NULL;
- return ESP_OK;
- }
- /*
- * Public Methods
- * */
- esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event)
- {
- /* no-op, kept for compatibility */
- return ESP_OK;
- }
- static void event_handler(void* arg, esp_event_base_t event_base,
- int32_t event_id, void* event_data)
- {
- if (!_mdns_server) {
- return;
- }
- mdns_action_t * action = (mdns_action_t *)calloc(1, sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return;
- }
- action->type = ACTION_SYSTEM_EVENT;
- action->data.sys_event.event_base = event_base;
- action->data.sys_event.event_id = event_id;
- if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
- ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data;
- action->data.sys_event.interface = event->esp_netif;
- }
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- }
- }
- esp_err_t mdns_init(void)
- {
- esp_err_t err = ESP_OK;
- if (_mdns_server) {
- return err;
- }
- _mdns_server = (mdns_server_t *)malloc(sizeof(mdns_server_t));
- if (!_mdns_server) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- memset((uint8_t*)_mdns_server, 0, sizeof(mdns_server_t));
- // zero-out local copy of netifs to initiate a fresh search by interface key whenever a netif ptr is needed
- memset(s_esp_netifs, 0, sizeof(s_esp_netifs));
- _mdns_server->lock = xSemaphoreCreateMutex();
- if (!_mdns_server->lock) {
- err = ESP_ERR_NO_MEM;
- goto free_server;
- }
- _mdns_server->action_queue = xQueueCreate(MDNS_ACTION_QUEUE_LEN, sizeof(mdns_action_t *));
- if (!_mdns_server->action_queue) {
- err = ESP_ERR_NO_MEM;
- goto free_lock;
- }
- if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) {
- goto free_event_handlers;
- }
- if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) {
- goto free_event_handlers;
- }
- #if CONFIG_ETH_ENABLED
- if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) {
- goto free_event_handlers;
- }
- #endif
- uint8_t i;
- #if CONFIG_LWIP_IPV6
- esp_ip6_addr_t tmp_addr6;
- #endif
- esp_netif_ip_info_t if_ip_info;
- for (i=0; i<MDNS_IF_MAX; i++) {
- #if CONFIG_LWIP_IPV6
- if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) {
- _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6);
- }
- #endif
- if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) {
- _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4);
- }
- }
- if (_mdns_service_task_start()) {
- //service start failed!
- err = ESP_FAIL;
- goto free_all_and_disable_pcbs;
- }
- return ESP_OK;
- free_all_and_disable_pcbs:
- for (i=0; i<MDNS_IF_MAX; i++) {
- _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V6);
- _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4);
- }
- free_event_handlers:
- esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler);
- esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler);
- #if CONFIG_ETH_ENABLED
- esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler);
- #endif
- vQueueDelete(_mdns_server->action_queue);
- free_lock:
- vSemaphoreDelete(_mdns_server->lock);
- free_server:
- free(_mdns_server);
- _mdns_server = NULL;
- return err;
- }
- void mdns_free(void)
- {
- uint8_t i, j;
- if (!_mdns_server) {
- return;
- }
- // Unregister handlers before destroying the mdns internals to avoid receiving async events while deinit
- esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler);
- esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler);
- #if CONFIG_ETH_ENABLED
- esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler);
- #endif
- mdns_service_remove_all();
- _mdns_service_task_stop();
- for (i=0; i<MDNS_IF_MAX; i++) {
- for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
- _mdns_pcb_deinit(i, j);
- }
- }
- free((char*)_mdns_server->hostname);
- free((char*)_mdns_server->instance);
- if (_mdns_server->action_queue) {
- mdns_action_t * c;
- while (xQueueReceive(_mdns_server->action_queue, &c, 0) == pdTRUE) {
- _mdns_free_action(c);
- }
- vQueueDelete(_mdns_server->action_queue);
- }
- _mdns_clear_tx_queue_head();
- while (_mdns_server->search_once) {
- mdns_search_once_t * h = _mdns_server->search_once;
- _mdns_server->search_once = h->next;
- free(h->instance);
- free(h->service);
- free(h->proto);
- vSemaphoreDelete(h->done_semaphore);
- if (h->result) {
- mdns_query_results_free(h->result);
- }
- free(h);
- }
- vSemaphoreDelete(_mdns_server->lock);
- free(_mdns_server);
- _mdns_server = NULL;
- }
- esp_err_t mdns_hostname_set(const char * hostname)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_ARG;
- }
- if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
- return ESP_ERR_INVALID_ARG;
- }
- char * new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
- if (!new_hostname) {
- return ESP_ERR_NO_MEM;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- free(new_hostname);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_HOSTNAME_SET;
- action->data.hostname_set.hostname = new_hostname;
- action->data.hostname_set.calling_task = xTaskGetCurrentTaskHandle();
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(new_hostname);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
- return ESP_OK;
- }
- esp_err_t mdns_delegate_hostname_add(const char * hostname, const mdns_ip_addr_t * address_list)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1) || address_list == NULL) {
- return ESP_ERR_INVALID_ARG;
- }
- char * new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
- if (!new_hostname) {
- return ESP_ERR_NO_MEM;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- free(new_hostname);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_DELEGATE_HOSTNAME_ADD;
- action->data.delegate_hostname.hostname = new_hostname;
- action->data.delegate_hostname.address_list = copy_address_list(address_list);
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(new_hostname);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_delegate_hostname_remove(const char * hostname)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
- return ESP_ERR_INVALID_ARG;
- }
- char * new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
- if (!new_hostname) {
- return ESP_ERR_NO_MEM;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- free(new_hostname);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_DELEGATE_HOSTNAME_REMOVE;
- action->data.delegate_hostname.hostname = new_hostname;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(new_hostname);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- bool mdns_hostname_exists(const char * hostname)
- {
- return _hostname_is_ours(hostname);
- }
- esp_err_t mdns_instance_name_set(const char * instance)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- if (_str_null_or_empty(instance) || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
- return ESP_ERR_INVALID_ARG;
- }
- char * new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
- if (!new_instance) {
- return ESP_ERR_NO_MEM;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- free(new_instance);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_INSTANCE_SET;
- action->data.instance = new_instance;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(new_instance);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- /*
- * MDNS SERVICES
- * */
- esp_err_t mdns_service_add_for_host(const char * instance, const char * service, const char * proto, const char * hostname,
- uint16_t port, mdns_txt_item_t txt[], size_t num_items)
- {
- if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
- return ESP_ERR_INVALID_ARG;
- }
- if (!_mdns_can_add_more_services()) {
- return ESP_ERR_NO_MEM;
- }
- #if CONFIG_MDNS_MULTIPLE_INSTANCE
- mdns_srv_item_t * item = _mdns_get_service_item_instance(instance, service, proto, hostname);
- #else
- mdns_srv_item_t * item = _mdns_get_service_item(service, proto, hostname);
- #endif // CONFIG_MDNS_MULTIPLE_INSTANCE
- if (item) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_service_t * s = _mdns_create_service(service, proto, hostname, port, instance, num_items, txt);
- if (!s) {
- return ESP_ERR_NO_MEM;
- }
- item = (mdns_srv_item_t *)malloc(sizeof(mdns_srv_item_t));
- if (!item) {
- HOOK_MALLOC_FAILED;
- _mdns_free_service(s);
- return ESP_ERR_NO_MEM;
- }
- item->service = s;
- item->next = NULL;
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- _mdns_free_service(s);
- free(item);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_ADD;
- action->data.srv_add.service = item;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- _mdns_free_service(s);
- free(item);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- size_t start = xTaskGetTickCount();
- size_t timeout_ticks = pdMS_TO_TICKS(MDNS_SERVICE_ADD_TIMEOUT_MS);
- while (_mdns_get_service_item(service, proto, hostname) == NULL) {
- uint32_t expired = xTaskGetTickCount() - start;
- if (expired >= timeout_ticks) {
- return ESP_FAIL; // Timeout
- }
- vTaskDelay(MIN(10 / portTICK_RATE_MS, timeout_ticks - expired));
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_add(const char * instance, const char * service, const char * proto, uint16_t port,
- mdns_txt_item_t txt[], size_t num_items)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_add_for_host(instance, service, proto, _mdns_server->hostname, port, txt, num_items);
- }
- bool mdns_service_exists(const char * service_type, const char * proto, const char * hostname)
- {
- return _mdns_get_service_item(service_type, proto, hostname) != NULL;
- }
- bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto,
- const char *hostname)
- {
- return _mdns_get_service_item_instance(instance, service_type, proto, hostname) != NULL;
- }
- esp_err_t mdns_service_port_set_for_host(const char * service, const char * proto, const char * hostname, uint16_t port)
- {
- if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
- if (!s) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_PORT_SET;
- action->data.srv_port.service = s;
- action->data.srv_port.port = port;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_port_set(const char * service, const char * proto, uint16_t port)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_port_set_for_host(service, proto, _mdns_server->hostname, port);
- }
- esp_err_t mdns_service_txt_set_for_host(const char * service, const char * proto, const char * hostname,
- mdns_txt_item_t txt[], uint8_t num_items)
- {
- if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || (num_items && txt == NULL)) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
- if (!s) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_txt_linked_item_t * new_txt = NULL;
- if (num_items){
- new_txt = _mdns_allocate_txt(num_items, txt);
- if (!new_txt) {
- return ESP_ERR_NO_MEM;
- }
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- _mdns_free_linked_txt(new_txt);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_TXT_REPLACE;
- action->data.srv_txt_replace.service = s;
- action->data.srv_txt_replace.txt = new_txt;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- _mdns_free_linked_txt(new_txt);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_txt_set(const char * service, const char * proto, mdns_txt_item_t txt[], uint8_t num_items)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_txt_set_for_host(service, proto, _mdns_server->hostname, txt, num_items);
- }
- esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *service, const char *proto,
- const char *hostname, const char *key,
- const char *value, uint8_t value_len)
- {
- if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) ||
- _str_null_or_empty(key) || !value) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_srv_item_t *s = _mdns_get_service_item(service, proto, hostname);
- if (!s) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_TXT_SET;
- action->data.srv_txt_set.service = s;
- action->data.srv_txt_set.key = strdup(key);
- if (!action->data.srv_txt_set.key) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- action->data.srv_txt_set.value = (char *)malloc(value_len);
- if (!action->data.srv_txt_set.value) {
- free(action->data.srv_txt_set.key);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- memcpy(action->data.srv_txt_set.value, value, value_len);
- action->data.srv_txt_set.value_len = value_len;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action->data.srv_txt_set.key);
- free(action->data.srv_txt_set.value);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_txt_item_set_for_host(const char *service, const char *proto, const char *hostname,
- const char *key, const char *value)
- {
- return mdns_service_txt_item_set_for_host_with_explicit_value_len(service, proto, hostname, key, value,
- strlen(value));
- }
- esp_err_t mdns_service_txt_item_set(const char *service, const char *proto, const char *key, const char *value)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_txt_item_set_for_host_with_explicit_value_len(service, proto, _mdns_server->hostname, key,
- value, strlen(value));
- }
- esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service, const char *proto, const char *key,
- const char *value, uint8_t value_len)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_txt_item_set_for_host_with_explicit_value_len(service, proto, _mdns_server->hostname, key,
- value, value_len);
- }
- esp_err_t mdns_service_txt_item_remove_for_host(const char * service, const char * proto, const char * hostname,
- const char * key)
- {
- if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || _str_null_or_empty(key)) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
- if (!s) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_TXT_DEL;
- action->data.srv_txt_del.service = s;
- action->data.srv_txt_del.key = strdup(key);
- if (!action->data.srv_txt_del.key) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action->data.srv_txt_del.key);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_txt_item_remove(const char * service, const char * proto, const char * key)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_txt_item_remove_for_host(service, proto, _mdns_server->hostname, key);
- }
- esp_err_t mdns_service_instance_name_set_for_host(const char * service, const char * proto, const char * hostname,
- const char * instance)
- {
- if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
- return ESP_ERR_INVALID_ARG;
- }
- if (_str_null_or_empty(instance) || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
- if (!s) {
- return ESP_ERR_NOT_FOUND;
- }
- char * new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
- if (!new_instance) {
- return ESP_ERR_NO_MEM;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- free(new_instance);
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_INSTANCE_SET;
- action->data.srv_instance.service = s;
- action->data.srv_instance.instance = new_instance;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(new_instance);
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_instance_name_set(const char * service, const char * proto, const char * instance)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_instance_name_set_for_host(service, proto, _mdns_server->hostname, instance);
- }
- esp_err_t mdns_service_remove_for_host(const char * service, const char * proto, const char * hostname)
- {
- if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
- return ESP_ERR_INVALID_ARG;
- }
- mdns_srv_item_t * s = _mdns_get_service_item(service, proto, hostname);
- if (!s) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICE_DEL;
- action->data.srv_del.service = s;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- esp_err_t mdns_service_remove(const char * service_type, const char * proto)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- return mdns_service_remove_for_host(service_type, proto, _mdns_server->hostname);
- }
- esp_err_t mdns_service_remove_all(void)
- {
- if (!_mdns_server) {
- return ESP_ERR_INVALID_ARG;
- }
- if (!_mdns_server->services) {
- return ESP_OK;
- }
- mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
- if (!action) {
- HOOK_MALLOC_FAILED;
- return ESP_ERR_NO_MEM;
- }
- action->type = ACTION_SERVICES_CLEAR;
- if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
- free(action);
- return ESP_ERR_NO_MEM;
- }
- return ESP_OK;
- }
- /*
- * MDNS QUERY
- * */
- void mdns_query_results_free(mdns_result_t * results)
- {
- mdns_result_t * r;
- mdns_ip_addr_t * a;
- while (results) {
- r = results;
- free((char *)(r->hostname));
- free((char *)(r->instance_name));
- free((char *)(r->service_type));
- free((char *)(r->proto));
- for (size_t i=0; i<r->txt_count; i++) {
- free((char *)(r->txt[i].key));
- free((char *)(r->txt[i].value));
- }
- free(r->txt);
- free(r->txt_value_len);
- while (r->addr) {
- a = r->addr;
- r->addr = r->addr->next;
- free(a);
- }
- results = results->next;
- free(r);
- }
- }
- esp_err_t mdns_query_async_delete(mdns_search_once_t* search)
- {
- if (!search) {
- return ESP_ERR_INVALID_ARG;
- }
- if (search->state != SEARCH_OFF) {
- return ESP_ERR_INVALID_STATE;
- }
- MDNS_SERVICE_LOCK();
- _mdns_search_free(search);
- MDNS_SERVICE_UNLOCK();
- return ESP_OK;
- }
- bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout, mdns_result_t ** results)
- {
- if (xSemaphoreTake(search->done_semaphore, pdMS_TO_TICKS(timeout)) == pdTRUE) {
- *results = search->result;
- return true;
- }
- return false;
- }
- mdns_search_once_t *mdns_query_async_new(const char *name, const char *service, const char *proto, uint16_t type,
- uint32_t timeout, size_t max_results, mdns_query_notify_t notifier)
- {
- mdns_search_once_t *search = NULL;
- if (!_mdns_server || !timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
- return NULL;
- }
- search = _mdns_search_init(name, service, proto, type, timeout, max_results, notifier);
- if (!search) {
- return NULL;
- }
- if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) {
- _mdns_search_free(search);
- return NULL;
- }
- return search;
- }
- 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)
- {
- mdns_search_once_t * search = NULL;
- *results = NULL;
- if (!_mdns_server) {
- return ESP_ERR_INVALID_STATE;
- }
- if (!timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
- return ESP_ERR_INVALID_ARG;
- }
- search = _mdns_search_init(name, service, proto, type, timeout, max_results, NULL);
- if (!search) {
- return ESP_ERR_NO_MEM;
- }
- if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) {
- _mdns_search_free(search);
- return ESP_ERR_NO_MEM;
- }
- xSemaphoreTake(search->done_semaphore, portMAX_DELAY);
- *results = search->result;
- _mdns_search_free(search);
- return ESP_OK;
- }
- esp_err_t mdns_query_ptr(const char * service, const char * proto, uint32_t timeout, size_t max_results, mdns_result_t ** results)
- {
- if (_str_null_or_empty(service) || _str_null_or_empty(proto)) {
- return ESP_ERR_INVALID_ARG;
- }
- return mdns_query(NULL, service, proto, MDNS_TYPE_PTR, timeout, max_results, results);
- }
- esp_err_t mdns_query_srv(const char * instance, const char * service, const char * proto, uint32_t timeout, mdns_result_t ** result)
- {
- if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
- return ESP_ERR_INVALID_ARG;
- }
- return mdns_query(instance, service, proto, MDNS_TYPE_SRV, timeout, 1, result);
- }
- esp_err_t mdns_query_txt(const char * instance, const char * service, const char * proto, uint32_t timeout, mdns_result_t ** result)
- {
- if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
- return ESP_ERR_INVALID_ARG;
- }
- return mdns_query(instance, service, proto, MDNS_TYPE_TXT, timeout, 1, result);
- }
- esp_err_t mdns_query_a(const char * name, uint32_t timeout, esp_ip4_addr_t * addr)
- {
- mdns_result_t * result = NULL;
- esp_err_t err;
- if (_str_null_or_empty(name)) {
- return ESP_ERR_INVALID_ARG;
- }
- if (strstr(name, ".local")) {
- ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain");
- }
- err = mdns_query(name, NULL, NULL, MDNS_TYPE_A, timeout, 1, &result);
- if (err) {
- return err;
- }
- if (!result) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_ip_addr_t * a = result->addr;
- while (a) {
- if (a->addr.type == ESP_IPADDR_TYPE_V4) {
- addr->addr = a->addr.u_addr.ip4.addr;
- mdns_query_results_free(result);
- return ESP_OK;
- }
- a = a->next;
- }
- mdns_query_results_free(result);
- return ESP_ERR_NOT_FOUND;
- }
- #if CONFIG_LWIP_IPV6
- esp_err_t mdns_query_aaaa(const char * name, uint32_t timeout, esp_ip6_addr_t * addr)
- {
- mdns_result_t * result = NULL;
- esp_err_t err;
- if (_str_null_or_empty(name)) {
- return ESP_ERR_INVALID_ARG;
- }
- if (strstr(name, ".local")) {
- ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain");
- }
- err = mdns_query(name, NULL, NULL, MDNS_TYPE_AAAA, timeout, 1, &result);
- if (err) {
- return err;
- }
- if (!result) {
- return ESP_ERR_NOT_FOUND;
- }
- mdns_ip_addr_t * a = result->addr;
- while (a) {
- if (a->addr.type == ESP_IPADDR_TYPE_V6) {
- memcpy(addr->addr, a->addr.u_addr.ip6.addr, 16);
- mdns_query_results_free(result);
- return ESP_OK;
- }
- a = a->next;
- }
- mdns_query_results_free(result);
- return ESP_ERR_NOT_FOUND;
- }
- #endif
- #ifdef MDNS_ENABLE_DEBUG
- void mdns_debug_packet(const uint8_t * data, size_t len)
- {
- static mdns_name_t n;
- mdns_header_t header;
- const uint8_t * content = data + MDNS_HEAD_LEN;
- uint32_t t = xTaskGetTickCount() * portTICK_PERIOD_MS;
- mdns_name_t * name = &n;
- memset(name, 0, sizeof(mdns_name_t));
- _mdns_dbg_printf("Packet[%u]: ", t);
- header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
- header.flags.value = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
- header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
- header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
- header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
- header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
- _mdns_dbg_printf("%s",
- (header.flags.value == MDNS_FLAGS_AUTHORITATIVE)?"AUTHORITATIVE\n":
- (header.flags.value == MDNS_FLAGS_DISTRIBUTED)?"DISTRIBUTED\n":
- (header.flags.value == 0)?"\n":" "
- );
- if (header.flags.value && header.flags.value != MDNS_FLAGS_AUTHORITATIVE) {
- _mdns_dbg_printf("0x%04X\n", header.flags.value);
- }
- if (header.questions) {
- uint8_t qs = header.questions;
- while (qs--) {
- content = _mdns_parse_fqdn(data, content, name);
- if (!content) {
- header.answers = 0;
- header.additional = 0;
- header.servers = 0;
- _mdns_dbg_printf("ERROR: parse header questions\n");
- break;
- }
- uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
- uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
- bool unicast = !!(mdns_class & 0x8000);
- mdns_class &= 0x7FFF;
- content = content + 4;
- _mdns_dbg_printf(" Q: ");
- if (unicast) {
- _mdns_dbg_printf("*U* ");
- }
- if (type == MDNS_TYPE_PTR) {
- _mdns_dbg_printf("%s.%s%s.%s.%s. PTR ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_SRV) {
- _mdns_dbg_printf("%s.%s%s.%s.%s. SRV ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_TXT) {
- _mdns_dbg_printf("%s.%s%s.%s.%s. TXT ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_A) {
- _mdns_dbg_printf("%s.%s. A ", name->host, name->domain);
- } else if (type == MDNS_TYPE_AAAA) {
- _mdns_dbg_printf("%s.%s. AAAA ", name->host, name->domain);
- } else if (type == MDNS_TYPE_NSEC) {
- _mdns_dbg_printf("%s.%s%s.%s.%s. NSEC ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_ANY) {
- _mdns_dbg_printf("%s.%s%s.%s.%s. ANY ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain);
- } else {
- _mdns_dbg_printf("%s.%s%s.%s.%s. %04X ", name->host, name->sub?"_sub.":"", name->service, name->proto, name->domain, type);
- }
- if (mdns_class == 0x0001) {
- _mdns_dbg_printf("IN");
- } else {
- _mdns_dbg_printf("%04X", mdns_class);
- }
- _mdns_dbg_printf("\n");
- }
- }
- if (header.answers || header.servers || header.additional) {
- uint16_t recordIndex = 0;
- while (content < (data + len)) {
- content = _mdns_parse_fqdn(data, content, name);
- if (!content) {
- _mdns_dbg_printf("ERROR: parse mdns records\n");
- break;
- }
- uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET);
- uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET);
- uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET);
- uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET);
- const uint8_t * data_ptr = content + MDNS_DATA_OFFSET;
- bool flush = !!(mdns_class & 0x8000);
- mdns_class &= 0x7FFF;
- content = data_ptr + data_len;
- if (content > (data + len)) {
- _mdns_dbg_printf("ERROR: content length overflow\n");
- break;
- }
- mdns_parsed_record_type_t record_type = MDNS_ANSWER;
- if (recordIndex >= (header.answers + header.servers)) {
- record_type = MDNS_EXTRA;
- } else if (recordIndex >= (header.answers)) {
- record_type = MDNS_NS;
- }
- recordIndex++;
- if (record_type == MDNS_EXTRA) {
- _mdns_dbg_printf(" X");
- } else if (record_type == MDNS_NS) {
- _mdns_dbg_printf(" S");
- } else {
- _mdns_dbg_printf(" A");
- }
- if (type == MDNS_TYPE_PTR) {
- _mdns_dbg_printf(": %s%s%s.%s.%s. PTR ", name->host, name->host[0]?".":"", name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_SRV) {
- _mdns_dbg_printf(": %s.%s.%s.%s. SRV ", name->host, name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_TXT) {
- _mdns_dbg_printf(": %s.%s.%s.%s. TXT ", name->host, name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_A) {
- _mdns_dbg_printf(": %s.%s. A ", name->host, name->domain);
- } else if (type == MDNS_TYPE_AAAA) {
- _mdns_dbg_printf(": %s.%s. AAAA ", name->host, name->domain);
- } else if (type == MDNS_TYPE_NSEC) {
- _mdns_dbg_printf(": %s.%s.%s.%s. NSEC ", name->host, name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_ANY) {
- _mdns_dbg_printf(": %s.%s.%s.%s. ANY ", name->host, name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_OPT) {
- _mdns_dbg_printf(": . OPT ");
- } else {
- _mdns_dbg_printf(": %s.%s.%s.%s. %04X ", name->host, name->service, name->proto, name->domain, type);
- }
- if (mdns_class == 0x0001) {
- _mdns_dbg_printf("IN ");
- } else {
- _mdns_dbg_printf("%04X ", mdns_class);
- }
- if (flush) {
- _mdns_dbg_printf("FLUSH ");
- }
- _mdns_dbg_printf("%u ", ttl);
- _mdns_dbg_printf("[%u] ", data_len);
- if (type == MDNS_TYPE_PTR) {
- if (!_mdns_parse_fqdn(data, data_ptr, name)) {
- _mdns_dbg_printf("ERROR: parse PTR\n");
- continue;
- }
- _mdns_dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain);
- } else if (type == MDNS_TYPE_SRV) {
- if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name)) {
- _mdns_dbg_printf("ERROR: parse SRV\n");
- continue;
- }
- uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
- uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET);
- uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET);
- _mdns_dbg_printf("%u %u %u %s.%s.\n", priority, weight, port, name->host, name->domain);
- } else if (type == MDNS_TYPE_TXT) {
- uint16_t i=0, y;
- while (i < data_len) {
- uint8_t partLen = data_ptr[i++];
- if ((i+partLen) > data_len) {
- _mdns_dbg_printf("ERROR: parse TXT\n");
- break;
- }
- char txt[partLen+1];
- for (y=0; y<partLen; y++) {
- char d = data_ptr[i++];
- txt[y] = d;
- }
- txt[partLen] = 0;
- _mdns_dbg_printf("%s", txt);
- if (i<data_len) {
- _mdns_dbg_printf("; ");
- }
- }
- _mdns_dbg_printf("\n");
- } else if (type == MDNS_TYPE_AAAA) {
- esp_ip6_addr_t ip6;
- memcpy(&ip6, data_ptr, sizeof(esp_ip6_addr_t));
- _mdns_dbg_printf(IPV6STR "\n", IPV62STR(ip6));
- } else if (type == MDNS_TYPE_A) {
- esp_ip4_addr_t ip;
- memcpy(&ip, data_ptr, sizeof(esp_ip4_addr_t));
- _mdns_dbg_printf(IPSTR "\n", IP2STR(&ip));
- } else if (type == MDNS_TYPE_NSEC) {
- const uint8_t * old_ptr = data_ptr;
- const uint8_t * new_ptr = _mdns_parse_fqdn(data, data_ptr, name);
- if (new_ptr) {
- _mdns_dbg_printf("%s.%s.%s.%s. ", name->host, name->service, name->proto, name->domain);
- size_t diff = new_ptr - old_ptr;
- data_len -= diff;
- data_ptr = new_ptr;
- }
- size_t i;
- for (i=0; i<data_len; i++) {
- _mdns_dbg_printf(" %02x", data_ptr[i]);
- }
- _mdns_dbg_printf("\n");
- } else if (type == MDNS_TYPE_OPT) {
- uint16_t opCode = _mdns_read_u16(data_ptr, 0);
- uint16_t opLen = _mdns_read_u16(data_ptr, 2);
- _mdns_dbg_printf(" Code: %04x Data[%u]:", opCode, opLen);
- size_t i;
- for (i=4; i<data_len; i++) {
- _mdns_dbg_printf(" %02x", data_ptr[i]);
- }
- _mdns_dbg_printf("\n");
- } else {
- size_t i;
- for (i=0; i<data_len; i++) {
- _mdns_dbg_printf(" %02x", data_ptr[i]);
- }
- _mdns_dbg_printf("\n");
- }
- }
- }
- }
- #endif
|