mdns_private.h 14 KB

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