mdns_private.h 13 KB

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