mdns.c 160 KB

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