mdns_private.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef MDNS_PRIVATE_H_
  7. #define MDNS_PRIVATE_H_
  8. #include "sdkconfig.h"
  9. #include "mdns.h"
  10. #include "esp_task.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/queue.h"
  14. #include "freertos/semphr.h"
  15. #include "esp_timer.h"
  16. #include "esp_system.h"
  17. #ifdef CONFIG_MDNS_ENABLE_DEBUG_PRINTS
  18. #define MDNS_ENABLE_DEBUG
  19. #define _mdns_dbg_printf(...) printf(__VA_ARGS__)
  20. #endif
  21. /** Number of predefined interfaces */
  22. #ifndef CONFIG_MDNS_PREDEF_NETIF_STA
  23. #define CONFIG_MDNS_PREDEF_NETIF_STA 0
  24. #endif
  25. #ifndef CONFIG_MDNS_PREDEF_NETIF_AP
  26. #define CONFIG_MDNS_PREDEF_NETIF_AP 0
  27. #endif
  28. #ifndef CONFIG_MDNS_PREDEF_NETIF_ETH
  29. #define CONFIG_MDNS_PREDEF_NETIF_ETH 0
  30. #endif
  31. #define MDNS_MAX_PREDEF_INTERFACES (CONFIG_MDNS_PREDEF_NETIF_STA + CONFIG_MDNS_PREDEF_NETIF_AP + CONFIG_MDNS_PREDEF_NETIF_ETH)
  32. #ifdef CONFIG_LWIP_IPV6_NUM_ADDRESSES
  33. #define NETIF_IPV6_MAX_NUMS CONFIG_LWIP_IPV6_NUM_ADDRESSES
  34. #else
  35. #define NETIF_IPV6_MAX_NUMS 3
  36. #endif
  37. #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
  38. /* CONFIG_LWIP_IPV4 was introduced in IDF v5.1 */
  39. /* For IDF v5.0, set CONFIG_LWIP_IPV4 to 1 by default */
  40. #ifndef CONFIG_LWIP_IPV4
  41. #define CONFIG_LWIP_IPV4 1
  42. #endif // CONFIG_LWIP_IPV4
  43. #endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
  44. /** Number of configured interfaces */
  45. #if MDNS_MAX_PREDEF_INTERFACES > CONFIG_MDNS_MAX_INTERFACES
  46. #warning Number of configured interfaces is less then number of predefined interfaces. Please update CONFIG_MDNS_MAX_INTERFACES.
  47. #define MDNS_MAX_INTERFACES (MDNS_MAX_PREDEF_INTERFACES)
  48. #else
  49. #define MDNS_MAX_INTERFACES (CONFIG_MDNS_MAX_INTERFACES)
  50. #endif
  51. /** The maximum number of services */
  52. #define MDNS_MAX_SERVICES CONFIG_MDNS_MAX_SERVICES
  53. #define MDNS_ANSWER_PTR_TTL 4500
  54. #define MDNS_ANSWER_TXT_TTL 4500
  55. #define MDNS_ANSWER_SRV_TTL 120
  56. #define MDNS_ANSWER_A_TTL 120
  57. #define MDNS_ANSWER_AAAA_TTL 120
  58. #define MDNS_FLAGS_QUERY_REPSONSE 0x8000
  59. #define MDNS_FLAGS_AUTHORITATIVE 0x0400
  60. #define MDNS_FLAGS_QR_AUTHORITATIVE (MDNS_FLAGS_QUERY_REPSONSE | MDNS_FLAGS_AUTHORITATIVE)
  61. #define MDNS_FLAGS_DISTRIBUTED 0x0200
  62. #define MDNS_NAME_REF 0xC000
  63. //custom type! only used by this implementation
  64. //to help manage service discovery handling
  65. #define MDNS_TYPE_SDPTR 0x0032
  66. #define MDNS_CLASS_IN 0x0001
  67. #define MDNS_CLASS_ANY 0x00FF
  68. #define MDNS_CLASS_IN_FLUSH_CACHE 0x8001
  69. #define MDNS_ANSWER_ALL 0x3F
  70. #define MDNS_ANSWER_PTR 0x08
  71. #define MDNS_ANSWER_TXT 0x04
  72. #define MDNS_ANSWER_SRV 0x02
  73. #define MDNS_ANSWER_A 0x01
  74. #define MDNS_ANSWER_AAAA 0x10
  75. #define MDNS_ANSWER_NSEC 0x20
  76. #define MDNS_ANSWER_SDPTR 0x80
  77. #define MDNS_ANSWER_AAAA_SIZE 16
  78. #define MDNS_SERVICE_PORT 5353 // UDP port that the server runs on
  79. #define MDNS_SERVICE_STACK_DEPTH CONFIG_MDNS_TASK_STACK_SIZE
  80. #define MDNS_TASK_PRIORITY CONFIG_MDNS_TASK_PRIORITY
  81. #if (MDNS_TASK_PRIORITY > ESP_TASK_PRIO_MAX)
  82. #error "mDNS task priority is higher than ESP_TASK_PRIO_MAX"
  83. #elif (MDNS_TASK_PRIORITY > ESP_TASKD_EVENT_PRIO)
  84. #warning "mDNS task priority is higher than ESP_TASKD_EVENT_PRIO, mDNS library might not work correctly"
  85. #endif
  86. #define MDNS_TASK_AFFINITY CONFIG_MDNS_TASK_AFFINITY
  87. #define MDNS_SERVICE_ADD_TIMEOUT_MS CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS
  88. #define MDNS_PACKET_QUEUE_LEN 16 // Maximum packets that can be queued for parsing
  89. #define MDNS_ACTION_QUEUE_LEN CONFIG_MDNS_ACTION_QUEUE_LEN // Maximum actions pending to the server
  90. #define MDNS_TXT_MAX_LEN 1024 // Maximum string length of text data in TXT record
  91. #define MDNS_MAX_PACKET_SIZE 1460 // Maximum size of mDNS outgoing packet
  92. #define MDNS_HEAD_LEN 12
  93. #define MDNS_HEAD_ID_OFFSET 0
  94. #define MDNS_HEAD_FLAGS_OFFSET 2
  95. #define MDNS_HEAD_QUESTIONS_OFFSET 4
  96. #define MDNS_HEAD_ANSWERS_OFFSET 6
  97. #define MDNS_HEAD_SERVERS_OFFSET 8
  98. #define MDNS_HEAD_ADDITIONAL_OFFSET 10
  99. #define MDNS_TYPE_OFFSET 0
  100. #define MDNS_CLASS_OFFSET 2
  101. #define MDNS_TTL_OFFSET 4
  102. #define MDNS_LEN_OFFSET 8
  103. #define MDNS_DATA_OFFSET 10
  104. #define MDNS_SRV_PRIORITY_OFFSET 0
  105. #define MDNS_SRV_WEIGHT_OFFSET 2
  106. #define MDNS_SRV_PORT_OFFSET 4
  107. #define MDNS_SRV_FQDN_OFFSET 6
  108. #define MDNS_TIMER_PERIOD_US (CONFIG_MDNS_TIMER_PERIOD_MS*1000)
  109. #define MDNS_SERVICE_LOCK() xSemaphoreTake(_mdns_service_semaphore, portMAX_DELAY)
  110. #define MDNS_SERVICE_UNLOCK() xSemaphoreGive(_mdns_service_semaphore)
  111. #define queueToEnd(type, queue, item) \
  112. if (!queue) { \
  113. queue = item; \
  114. } else { \
  115. type * _q = queue; \
  116. while (_q->next) { _q = _q->next; } \
  117. _q->next = item; \
  118. }
  119. #define queueDetach(type, queue, item) \
  120. if (queue) { \
  121. if (queue == item) { \
  122. queue = queue->next; \
  123. } else { \
  124. type * _q = queue; \
  125. while (_q->next && _q->next != item) { \
  126. _q = _q->next; \
  127. } \
  128. if (_q->next == item) { \
  129. _q->next = item->next; \
  130. item->next = NULL; \
  131. } \
  132. } \
  133. }
  134. #define queueFree(type, queue) while (queue) { type * _q = queue; queue = queue->next; free(_q); }
  135. #define PCB_STATE_IS_PROBING(s) (s->state > PCB_OFF && s->state < PCB_ANNOUNCE_1)
  136. #define PCB_STATE_IS_ANNOUNCING(s) (s->state > PCB_PROBE_3 && s->state < PCB_RUNNING)
  137. #define PCB_STATE_IS_RUNNING(s) (s->state == PCB_RUNNING)
  138. #ifndef HOOK_MALLOC_FAILED
  139. #define HOOK_MALLOC_FAILED ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %" PRIu32 " bytes)", __LINE__, esp_get_free_heap_size());
  140. #endif
  141. typedef size_t mdns_if_t;
  142. typedef enum {
  143. PCB_OFF, PCB_DUP, PCB_INIT,
  144. PCB_PROBE_1, PCB_PROBE_2, PCB_PROBE_3,
  145. PCB_ANNOUNCE_1, PCB_ANNOUNCE_2, PCB_ANNOUNCE_3,
  146. PCB_RUNNING
  147. } mdns_pcb_state_t;
  148. typedef enum {
  149. MDNS_ANSWER, MDNS_NS, MDNS_EXTRA
  150. } mdns_parsed_record_type_t;
  151. typedef enum {
  152. ACTION_SYSTEM_EVENT,
  153. ACTION_HOSTNAME_SET,
  154. ACTION_INSTANCE_SET,
  155. ACTION_SEARCH_ADD,
  156. ACTION_SEARCH_SEND,
  157. ACTION_SEARCH_END,
  158. ACTION_BROWSE_ADD,
  159. ACTION_BROWSE_SYNC,
  160. ACTION_BROWSE_END,
  161. ACTION_TX_HANDLE,
  162. ACTION_RX_HANDLE,
  163. ACTION_TASK_STOP,
  164. ACTION_DELEGATE_HOSTNAME_ADD,
  165. ACTION_DELEGATE_HOSTNAME_REMOVE,
  166. ACTION_DELEGATE_HOSTNAME_SET_ADDR,
  167. ACTION_MAX
  168. } mdns_action_type_t;
  169. typedef struct {
  170. uint16_t id;
  171. uint16_t flags;
  172. uint16_t questions; //QDCOUNT
  173. uint16_t answers; //ANCOUNT
  174. uint16_t servers; //NSCOUNT
  175. uint16_t additional;//ARCOUNT
  176. } mdns_header_t;
  177. typedef struct {
  178. char host[MDNS_NAME_BUF_LEN]; // hostname for A/AAAA records, instance name for SRV records
  179. char service[MDNS_NAME_BUF_LEN];
  180. char proto[MDNS_NAME_BUF_LEN];
  181. char domain[MDNS_NAME_BUF_LEN];
  182. uint8_t parts;
  183. uint8_t sub;
  184. bool invalid;
  185. } mdns_name_t;
  186. typedef struct mdns_parsed_question_s {
  187. struct mdns_parsed_question_s *next;
  188. uint16_t type;
  189. bool sub;
  190. bool unicast;
  191. char *host;
  192. char *service;
  193. char *proto;
  194. char *domain;
  195. } mdns_parsed_question_t;
  196. typedef struct mdns_parsed_record_s {
  197. struct mdns_parsed_record_s *next;
  198. mdns_parsed_record_type_t record_type;
  199. uint16_t type;
  200. uint16_t clas;
  201. uint8_t flush;
  202. uint32_t ttl;
  203. char *host;
  204. char *service;
  205. char *proto;
  206. char *domain;
  207. uint16_t data_len;
  208. uint8_t *data;
  209. } mdns_parsed_record_t;
  210. typedef struct {
  211. mdns_if_t tcpip_if;
  212. mdns_ip_protocol_t ip_protocol;
  213. esp_ip_addr_t src;
  214. uint16_t src_port;
  215. uint8_t multicast;
  216. uint8_t authoritative;
  217. uint8_t probe;
  218. uint8_t discovery;
  219. uint8_t distributed;
  220. mdns_parsed_question_t *questions;
  221. mdns_parsed_record_t *records;
  222. uint16_t id;
  223. } mdns_parsed_packet_t;
  224. typedef struct {
  225. mdns_if_t tcpip_if;
  226. mdns_ip_protocol_t ip_protocol;
  227. struct pbuf *pb;
  228. esp_ip_addr_t src;
  229. esp_ip_addr_t dest;
  230. uint16_t src_port;
  231. uint8_t multicast;
  232. } mdns_rx_packet_t;
  233. typedef struct mdns_txt_linked_item_s {
  234. const char *key; /*!< item key name */
  235. char *value; /*!< item value string */
  236. uint8_t value_len; /*!< item value length */
  237. struct mdns_txt_linked_item_s *next; /*!< next result, or NULL for the last result in the list */
  238. } mdns_txt_linked_item_t;
  239. typedef struct mdns_subtype_s {
  240. const char *subtype; /*!< subtype */
  241. struct mdns_subtype_s *next; /*!< next result, or NULL for the last result in the list */
  242. } mdns_subtype_t;
  243. typedef struct {
  244. const char *instance;
  245. const char *service;
  246. const char *proto;
  247. const char *hostname;
  248. uint16_t priority;
  249. uint16_t weight;
  250. uint16_t port;
  251. mdns_txt_linked_item_t *txt;
  252. mdns_subtype_t *subtype;
  253. } mdns_service_t;
  254. typedef struct mdns_srv_item_s {
  255. struct mdns_srv_item_s *next;
  256. mdns_service_t *service;
  257. } mdns_srv_item_t;
  258. typedef struct mdns_out_question_s {
  259. struct mdns_out_question_s *next;
  260. uint16_t type;
  261. bool unicast;
  262. const char *host;
  263. const char *service;
  264. const char *proto;
  265. const char *domain;
  266. bool own_dynamic_memory;
  267. } mdns_out_question_t;
  268. typedef struct mdns_host_item_t {
  269. const char *hostname;
  270. mdns_ip_addr_t *address_list;
  271. struct mdns_host_item_t *next;
  272. } mdns_host_item_t;
  273. typedef struct mdns_out_answer_s {
  274. struct mdns_out_answer_s *next;
  275. uint16_t type;
  276. uint8_t bye;
  277. uint8_t flush;
  278. mdns_service_t *service;
  279. mdns_host_item_t *host;
  280. const char *custom_instance;
  281. const char *custom_service;
  282. const char *custom_proto;
  283. } mdns_out_answer_t;
  284. typedef struct mdns_tx_packet_s {
  285. struct mdns_tx_packet_s *next;
  286. uint32_t send_at;
  287. mdns_if_t tcpip_if;
  288. mdns_ip_protocol_t ip_protocol;
  289. esp_ip_addr_t dst;
  290. uint16_t port;
  291. uint16_t flags;
  292. uint8_t distributed;
  293. mdns_out_question_t *questions;
  294. mdns_out_answer_t *answers;
  295. mdns_out_answer_t *servers;
  296. mdns_out_answer_t *additional;
  297. bool queued;
  298. uint16_t id;
  299. } mdns_tx_packet_t;
  300. typedef struct {
  301. mdns_pcb_state_t state;
  302. mdns_srv_item_t **probe_services;
  303. uint8_t probe_services_len;
  304. uint8_t probe_ip;
  305. uint8_t probe_running;
  306. uint16_t failed_probes;
  307. } mdns_pcb_t;
  308. typedef enum {
  309. SEARCH_OFF,
  310. SEARCH_INIT,
  311. SEARCH_RUNNING,
  312. SEARCH_MAX
  313. } mdns_search_once_state_t;
  314. typedef enum {
  315. BROWSE_OFF,
  316. BROWSE_INIT,
  317. BROWSE_RUNNING,
  318. BROWSE_MAX
  319. } mdns_browse_state_t;
  320. typedef struct mdns_search_once_s {
  321. struct mdns_search_once_s *next;
  322. mdns_search_once_state_t state;
  323. uint32_t started_at;
  324. uint32_t sent_at;
  325. uint32_t timeout;
  326. mdns_query_notify_t notifier;
  327. SemaphoreHandle_t done_semaphore;
  328. uint16_t type;
  329. bool unicast;
  330. uint8_t max_results;
  331. uint8_t num_results;
  332. char *instance;
  333. char *service;
  334. char *proto;
  335. mdns_result_t *result;
  336. } mdns_search_once_t;
  337. typedef struct mdns_browse_s {
  338. struct mdns_browse_s *next;
  339. mdns_browse_state_t state;
  340. mdns_browse_notify_t notifier;
  341. char *service;
  342. char *proto;
  343. mdns_result_t *result;
  344. } mdns_browse_t;
  345. typedef struct mdns_browse_result_sync_t {
  346. mdns_result_t *result;
  347. struct mdns_browse_result_sync_t *next;
  348. } mdns_browse_result_sync_t;
  349. typedef struct mdns_browse_sync {
  350. mdns_browse_t *browse;
  351. mdns_browse_result_sync_t *sync_result;
  352. } mdns_browse_sync_t;
  353. typedef struct mdns_server_s {
  354. struct {
  355. mdns_pcb_t pcbs[MDNS_IP_PROTOCOL_MAX];
  356. } interfaces[MDNS_MAX_INTERFACES];
  357. const char *hostname;
  358. const char *instance;
  359. mdns_srv_item_t *services;
  360. QueueHandle_t action_queue;
  361. SemaphoreHandle_t action_sema;
  362. mdns_tx_packet_t *tx_queue_head;
  363. mdns_search_once_t *search_once;
  364. esp_timer_handle_t timer_handle;
  365. mdns_browse_t *browse;
  366. } mdns_server_t;
  367. typedef struct {
  368. mdns_action_type_t type;
  369. union {
  370. struct {
  371. char *hostname;
  372. } hostname_set;
  373. char *instance;
  374. struct {
  375. mdns_if_t interface;
  376. mdns_event_actions_t event_action;
  377. } sys_event;
  378. struct {
  379. mdns_search_once_t *search;
  380. } search_add;
  381. struct {
  382. mdns_tx_packet_t *packet;
  383. } tx_handle;
  384. struct {
  385. mdns_rx_packet_t *packet;
  386. } rx_handle;
  387. struct {
  388. const char *hostname;
  389. mdns_ip_addr_t *address_list;
  390. } delegate_hostname;
  391. struct {
  392. mdns_browse_t *browse;
  393. } browse_add;
  394. struct {
  395. mdns_browse_sync_t *browse_sync;
  396. } browse_sync;
  397. } data;
  398. } mdns_action_t;
  399. /*
  400. * @brief Convert mnds if to esp-netif handle
  401. *
  402. * @param tcpip_if mdns supported interface as internal enum
  403. *
  404. * @return
  405. * - ptr to esp-netif on success
  406. * - NULL if no available netif for current interface index
  407. */
  408. esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if);
  409. #endif /* MDNS_PRIVATE_H_ */