mdns.c 157 KB

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