mdns.c 163 KB

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