mdns.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ESP_MDNS_H_
  7. #define ESP_MDNS_H_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include "sdkconfig.h"
  12. #include <esp_netif.h>
  13. #define MDNS_TYPE_A 0x0001
  14. #define MDNS_TYPE_PTR 0x000C
  15. #define MDNS_TYPE_TXT 0x0010
  16. #define MDNS_TYPE_AAAA 0x001C
  17. #define MDNS_TYPE_SRV 0x0021
  18. #define MDNS_TYPE_OPT 0x0029
  19. #define MDNS_TYPE_NSEC 0x002F
  20. #define MDNS_TYPE_ANY 0x00FF
  21. #if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES)
  22. #define MDNS_NAME_MAX_LEN (64+4) // Need to account for IPv6 reverse queries (64 char address + ".ip6" )
  23. #else
  24. #define MDNS_NAME_MAX_LEN 64 // Maximum string length of hostname, instance, service and proto
  25. #endif
  26. #define MDNS_NAME_BUF_LEN (MDNS_NAME_MAX_LEN+1) // Maximum char buffer size to hold hostname, instance, service or proto
  27. /**
  28. * @brief Asynchronous query handle
  29. */
  30. typedef struct mdns_search_once_s mdns_search_once_t;
  31. /**
  32. * @brief Daemon query handle
  33. */
  34. typedef struct mdns_browse_s mdns_browse_t;
  35. typedef enum {
  36. MDNS_EVENT_ENABLE_IP4 = 1 << 1,
  37. MDNS_EVENT_ENABLE_IP6 = 1 << 2,
  38. MDNS_EVENT_ANNOUNCE_IP4 = 1 << 3,
  39. MDNS_EVENT_ANNOUNCE_IP6 = 1 << 4,
  40. MDNS_EVENT_DISABLE_IP4 = 1 << 5,
  41. MDNS_EVENT_DISABLE_IP6 = 1 << 6,
  42. MDNS_EVENT_IP4_REVERSE_LOOKUP = 1 << 7,
  43. MDNS_EVENT_IP6_REVERSE_LOOKUP = 1 << 8,
  44. } mdns_event_actions_t;
  45. /**
  46. * @brief mDNS enum to specify the ip_protocol type
  47. */
  48. typedef enum {
  49. MDNS_IP_PROTOCOL_V4,
  50. MDNS_IP_PROTOCOL_V6,
  51. MDNS_IP_PROTOCOL_MAX
  52. } mdns_ip_protocol_t;
  53. /**
  54. * @brief mDNS basic text item structure
  55. * Used in mdns_service_add()
  56. */
  57. typedef struct {
  58. const char *key; /*!< item key name */
  59. const char *value; /*!< item value string */
  60. } mdns_txt_item_t;
  61. /**
  62. * @brief mDNS basic subtype item structure
  63. * Used in mdns_service_subtype_xxx() APIs
  64. */
  65. typedef struct {
  66. const char *subtype; /*!< subtype name */
  67. } mdns_subtype_item_t;
  68. /**
  69. * @brief mDNS query linked list IP item
  70. */
  71. typedef struct mdns_ip_addr_s {
  72. esp_ip_addr_t addr; /*!< IP address */
  73. struct mdns_ip_addr_s *next; /*!< next IP, or NULL for the last IP in the list */
  74. } mdns_ip_addr_t;
  75. /**
  76. * @brief mDNS query type to be explicitly set to either Unicast or Multicast
  77. */
  78. typedef enum {
  79. MDNS_QUERY_UNICAST,
  80. MDNS_QUERY_MULTICAST,
  81. } mdns_query_transmission_type_t;
  82. /**
  83. * @brief mDNS query result structure
  84. */
  85. typedef struct mdns_result_s {
  86. struct mdns_result_s *next; /*!< next result, or NULL for the last result in the list */
  87. esp_netif_t *esp_netif; /*!< ptr to corresponding esp-netif */
  88. uint32_t ttl; /*!< time to live */
  89. mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */
  90. // PTR
  91. char *instance_name; /*!< instance name */
  92. char *service_type; /*!< service type */
  93. char *proto; /*!< srevice protocol */
  94. // SRV
  95. char *hostname; /*!< hostname */
  96. uint16_t port; /*!< service port */
  97. // TXT
  98. mdns_txt_item_t *txt; /*!< txt record */
  99. uint8_t *txt_value_len; /*!< array of txt value len of each record */
  100. size_t txt_count; /*!< number of txt items */
  101. // A and AAAA
  102. mdns_ip_addr_t *addr; /*!< linked list of IP addresses found */
  103. } mdns_result_t;
  104. typedef void (*mdns_query_notify_t)(mdns_search_once_t *search);
  105. typedef void (*mdns_browse_notify_t)(mdns_result_t *result);
  106. /**
  107. * @brief Initialize mDNS on given interface
  108. *
  109. * @return
  110. * - ESP_OK on success
  111. * - ESP_ERR_INVALID_STATE when failed to register event handler
  112. * - ESP_ERR_NO_MEM on memory error
  113. * - ESP_FAIL when failed to start mdns task
  114. */
  115. esp_err_t mdns_init(void);
  116. /**
  117. * @brief Stop and free mDNS server
  118. *
  119. */
  120. void mdns_free(void);
  121. /**
  122. * @brief Set the hostname for mDNS server
  123. * required if you want to advertise services
  124. *
  125. * @param hostname Hostname to set
  126. *
  127. * @return
  128. * - ESP_OK success
  129. * - ESP_ERR_INVALID_ARG Parameter error
  130. * - ESP_ERR_NO_MEM memory error
  131. */
  132. esp_err_t mdns_hostname_set(const char *hostname);
  133. /**
  134. * @brief Get the hostname for mDNS server
  135. *
  136. * @param hostname pointer to the hostname, it should be allocated
  137. * and hold at least MDNS_NAME_BUF_LEN chars
  138. *
  139. * @return
  140. * - ESP_OK success
  141. * - ESP_ERR_INVALID_ARG Parameter error
  142. * - ESP_ERR_INVALID_STATE when mdns is not initialized
  143. */
  144. esp_err_t mdns_hostname_get(char *hostname);
  145. /**
  146. * @brief Adds a hostname and address to be delegated
  147. * A/AAAA queries will be replied for the hostname and
  148. * services can be added to this host.
  149. *
  150. * @param hostname Hostname to add
  151. * @param address_list The IP address list of the host
  152. *
  153. * @return
  154. * - ESP_OK success
  155. * - ESP_ERR_INVALID_STATE mDNS is not running
  156. * - ESP_ERR_INVALID_ARG Parameter error
  157. * - ESP_ERR_NO_MEM memory error
  158. *
  159. */
  160. esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list);
  161. /**
  162. * @brief Set the address to a delegated hostname
  163. *
  164. * @param hostname Hostname to set
  165. * @param address_list The IP address list of the host
  166. *
  167. * @return
  168. * - ESP_OK success
  169. * - ESP_ERR_INVALID_STATE mDNS is not running
  170. * - ESP_ERR_INVALID_ARG Parameter error
  171. * - ESP_ERR_NO_MEM memory error
  172. *
  173. */
  174. esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list);
  175. /**
  176. * @brief Remove a delegated hostname
  177. * All the services added to this host will also be removed.
  178. *
  179. * @param hostname Hostname to remove
  180. *
  181. * @return
  182. * - ESP_OK success
  183. * - ESP_ERR_INVALID_STATE mDNS is not running
  184. * - ESP_ERR_INVALID_ARG Parameter error
  185. * - ESP_ERR_NO_MEM memory error
  186. *
  187. */
  188. esp_err_t mdns_delegate_hostname_remove(const char *hostname);
  189. /**
  190. * @brief Query whether a hostname has been added
  191. *
  192. * @param hostname Hostname to query
  193. *
  194. * @return
  195. * - true The hostname has been added.
  196. * - false The hostname has not been added.
  197. *
  198. */
  199. bool mdns_hostname_exists(const char *hostname);
  200. /**
  201. * @brief Set the default instance name for mDNS server
  202. *
  203. * @param instance_name Instance name to set
  204. *
  205. * @return
  206. * - ESP_OK success
  207. * - ESP_ERR_INVALID_ARG Parameter error
  208. * - ESP_ERR_NO_MEM memory error
  209. */
  210. esp_err_t mdns_instance_name_set(const char *instance_name);
  211. /**
  212. * @brief Add service to mDNS server
  213. *
  214. * @note The value length of txt items will be automatically decided by strlen
  215. *
  216. * @param instance_name instance name to set. If NULL,
  217. * global instance name or hostname will be used.
  218. * Note that MDNS_MULTIPLE_INSTANCE config option
  219. * needs to be enabled for adding multiple instances
  220. * with the same instance type.
  221. * @param service_type service type (_http, _ftp, etc)
  222. * @param proto service protocol (_tcp, _udp)
  223. * @param port service port
  224. * @param txt string array of TXT data (eg. {{"var","val"},{"other","2"}})
  225. * @param num_items number of items in TXT data
  226. *
  227. * @return
  228. * - ESP_OK success
  229. * - ESP_ERR_INVALID_ARG Parameter error
  230. * - ESP_ERR_NO_MEM memory error
  231. * - ESP_FAIL failed to add service
  232. */
  233. esp_err_t mdns_service_add(const char *instance_name, const char *service_type, const char *proto, uint16_t port, mdns_txt_item_t txt[], size_t num_items);
  234. /**
  235. * @brief Add service to mDNS server with a delegated hostname
  236. *
  237. * @note The value length of txt items will be automatically decided by strlen
  238. *
  239. * @param instance_name instance name to set. If NULL,
  240. * global instance name or hostname will be used
  241. * Note that MDNS_MULTIPLE_INSTANCE config option
  242. * needs to be enabled for adding multiple instances
  243. * with the same instance type.
  244. * @param service_type service type (_http, _ftp, etc)
  245. * @param proto service protocol (_tcp, _udp)
  246. * @param hostname service hostname. If NULL, local hostname will be used.
  247. * @param port service port
  248. * @param txt string array of TXT data (eg. {{"var","val"},{"other","2"}})
  249. * @param num_items number of items in TXT data
  250. *
  251. * @return
  252. * - ESP_OK success
  253. * - ESP_ERR_INVALID_ARG Parameter error
  254. * - ESP_ERR_NO_MEM memory error
  255. * - ESP_FAIL failed to add service
  256. */
  257. esp_err_t mdns_service_add_for_host(const char *instance_name, const char *service_type, const char *proto,
  258. const char *hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items);
  259. /**
  260. * @brief Check whether a service has been added.
  261. *
  262. * @param service_type service type (_http, _ftp, etc)
  263. * @param proto service protocol (_tcp, _udp)
  264. * @param hostname service hostname. If NULL, checks for the local hostname.
  265. *
  266. * @return
  267. * - true Correspondding service has been added.
  268. * - false Service not found.
  269. */
  270. bool mdns_service_exists(const char *service_type, const char *proto, const char *hostname);
  271. /**
  272. * @brief Check whether a service has been added.
  273. *
  274. * @param instance instance name
  275. * @param service_type service type (_http, _ftp, etc)
  276. * @param proto service protocol (_tcp, _udp)
  277. * @param hostname service hostname. If NULL, checks for the local hostname.
  278. *
  279. * @return
  280. * - true Correspondding service has been added.
  281. * - false Service not found.
  282. */
  283. bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto,
  284. const char *hostname);
  285. /**
  286. * @brief Remove service from mDNS server
  287. *
  288. * @param service_type service type (_http, _ftp, etc)
  289. * @param proto service protocol (_tcp, _udp)
  290. *
  291. * @return
  292. * - ESP_OK success
  293. * - ESP_ERR_INVALID_ARG Parameter error
  294. * - ESP_ERR_NOT_FOUND Service not found
  295. * - ESP_ERR_NO_MEM memory error
  296. */
  297. esp_err_t mdns_service_remove(const char *service_type, const char *proto);
  298. /**
  299. * @brief Remove service from mDNS server with hostname
  300. *
  301. * @param instance instance name
  302. * @param service_type service type (_http, _ftp, etc)
  303. * @param proto service protocol (_tcp, _udp)
  304. * @param hostname service hostname. If NULL, local hostname will be used.
  305. *
  306. * @return
  307. * - ESP_OK success
  308. * - ESP_ERR_INVALID_ARG Parameter error
  309. * - ESP_ERR_NOT_FOUND Service not found
  310. * - ESP_ERR_NO_MEM memory error
  311. */
  312. esp_err_t mdns_service_remove_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname);
  313. /**
  314. * @brief Set instance name for service
  315. *
  316. * @param service_type service type (_http, _ftp, etc)
  317. * @param proto service protocol (_tcp, _udp)
  318. * @param instance_name instance name to set
  319. *
  320. * @return
  321. * - ESP_OK success
  322. * - ESP_ERR_INVALID_ARG Parameter error
  323. * - ESP_ERR_NOT_FOUND Service not found
  324. * - ESP_ERR_NO_MEM memory error
  325. */
  326. esp_err_t mdns_service_instance_name_set(const char *service_type, const char *proto, const char *instance_name);
  327. /**
  328. * @brief Set instance name for service with hostname
  329. *
  330. * @param instance_old original instance name
  331. * @param service_type service type (_http, _ftp, etc)
  332. * @param proto service protocol (_tcp, _udp)
  333. * @param hostname service hostname. If NULL, local hostname will be used.
  334. * @param instance_name instance name to set
  335. *
  336. * @return
  337. * - ESP_OK success
  338. * - ESP_ERR_INVALID_ARG Parameter error
  339. * - ESP_ERR_NOT_FOUND Service not found
  340. * - ESP_ERR_NO_MEM memory error
  341. */
  342. esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service_type, const char *proto, const char *hostname,
  343. const char *instance_name);
  344. /**
  345. * @brief Set service port
  346. *
  347. * @param service_type service type (_http, _ftp, etc)
  348. * @param proto service protocol (_tcp, _udp)
  349. * @param port service port
  350. *
  351. * @return
  352. * - ESP_OK success
  353. * - ESP_ERR_INVALID_ARG Parameter error
  354. * - ESP_ERR_NOT_FOUND Service not found
  355. * - ESP_ERR_NO_MEM memory error
  356. */
  357. esp_err_t mdns_service_port_set(const char *service_type, const char *proto, uint16_t port);
  358. /**
  359. * @brief Set service port with hostname
  360. *
  361. * @param instance instance name
  362. * @param service_type service type (_http, _ftp, etc)
  363. * @param proto service protocol (_tcp, _udp)
  364. * @param hostname service hostname. If NULL, local hostname will be used.
  365. * @param port service port
  366. *
  367. * @return
  368. * - ESP_OK success
  369. * - ESP_ERR_INVALID_ARG Parameter error
  370. * - ESP_ERR_NOT_FOUND Service not found
  371. * - ESP_ERR_NO_MEM memory error
  372. */
  373. esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
  374. uint16_t port);
  375. /**
  376. * @brief Replace all TXT items for service
  377. *
  378. * @note The value length of txt items will be automatically decided by strlen
  379. *
  380. * @param service_type service type (_http, _ftp, etc)
  381. * @param proto service protocol (_tcp, _udp)
  382. * @param txt array of TXT data (eg. {{"var","val"},{"other","2"}})
  383. * @param num_items number of items in TXT data
  384. *
  385. * @return
  386. * - ESP_OK success
  387. * - ESP_ERR_INVALID_ARG Parameter error
  388. * - ESP_ERR_NOT_FOUND Service not found
  389. * - ESP_ERR_NO_MEM memory error
  390. */
  391. esp_err_t mdns_service_txt_set(const char *service_type, const char *proto, mdns_txt_item_t txt[], uint8_t num_items);
  392. /**
  393. * @brief Replace all TXT items for service with hostname
  394. *
  395. * @note The value length of txt items will be automatically decided by strlen
  396. *
  397. * @param instance instance name
  398. * @param service_type service type (_http, _ftp, etc)
  399. * @param proto service protocol (_tcp, _udp)
  400. * @param hostname service hostname. If NULL, local hostname will be used.
  401. * @param txt array of TXT data (eg. {{"var","val"},{"other","2"}})
  402. * @param num_items number of items in TXT data
  403. *
  404. * @return
  405. * - ESP_OK success
  406. * - ESP_ERR_INVALID_ARG Parameter error
  407. * - ESP_ERR_NOT_FOUND Service not found
  408. * - ESP_ERR_NO_MEM memory error
  409. */
  410. esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
  411. mdns_txt_item_t txt[], uint8_t num_items);
  412. /**
  413. * @brief Set/Add TXT item for service TXT record
  414. *
  415. * @note The value length will be automatically decided by strlen
  416. *
  417. * @param service_type service type (_http, _ftp, etc)
  418. * @param proto service protocol (_tcp, _udp)
  419. * @param key the key that you want to add/update
  420. * @param value the new value of the key
  421. *
  422. * @return
  423. * - ESP_OK success
  424. * - ESP_ERR_INVALID_ARG Parameter error
  425. * - ESP_ERR_NOT_FOUND Service not found
  426. * - ESP_ERR_NO_MEM memory error
  427. */
  428. esp_err_t mdns_service_txt_item_set(const char *service_type, const char *proto, const char *key, const char *value);
  429. /**
  430. * @brief Set/Add TXT item for service TXT record
  431. *
  432. * @param service_type service type (_http, _ftp, etc)
  433. * @param proto service protocol (_tcp, _udp)
  434. * @param key the key that you want to add/update
  435. * @param value the new value of the key
  436. * @param value_len the length of the value
  437. *
  438. * @return
  439. * - ESP_OK success
  440. * - ESP_ERR_INVALID_ARG Parameter error
  441. * - ESP_ERR_NOT_FOUND Service not found
  442. * - ESP_ERR_NO_MEM memory error
  443. */
  444. esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service_type, const char *proto,
  445. const char *key, const char *value, uint8_t value_len);
  446. /**
  447. * @brief Set/Add TXT item for service TXT record with hostname
  448. *
  449. * @note The value length will be automatically decided by strlen
  450. *
  451. * @param instance instance name
  452. * @param service_type service type (_http, _ftp, etc)
  453. * @param proto service protocol (_tcp, _udp)
  454. * @param hostname service hostname. If NULL, local hostname will be used.
  455. * @param key the key that you want to add/update
  456. * @param value the new value of the key
  457. *
  458. * @return
  459. * - ESP_OK success
  460. * - ESP_ERR_INVALID_ARG Parameter error
  461. * - ESP_ERR_NOT_FOUND Service not found
  462. * - ESP_ERR_NO_MEM memory error
  463. */
  464. esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
  465. const char *key, const char *value);
  466. /**
  467. * @brief Set/Add TXT item for service TXT record with hostname and txt value length
  468. *
  469. * @param instance instance name
  470. * @param service_type service type (_http, _ftp, etc)
  471. * @param proto service protocol (_tcp, _udp)
  472. * @param hostname service hostname. If NULL, local hostname will be used.
  473. * @param key the key that you want to add/update
  474. * @param value the new value of the key
  475. * @param value_len the length of the value
  476. *
  477. * @return
  478. * - ESP_OK success
  479. * - ESP_ERR_INVALID_ARG Parameter error
  480. * - ESP_ERR_NOT_FOUND Service not found
  481. * - ESP_ERR_NO_MEM memory error
  482. */
  483. esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service_type, const char *proto,
  484. const char *hostname, const char *key,
  485. const char *value, uint8_t value_len);
  486. /**
  487. * @brief Remove TXT item for service TXT record
  488. *
  489. * @param service_type service type (_http, _ftp, etc)
  490. * @param proto service protocol (_tcp, _udp)
  491. * @param key the key that you want to remove
  492. *
  493. * @return
  494. * - ESP_OK success
  495. * - ESP_ERR_INVALID_ARG Parameter error
  496. * - ESP_ERR_NOT_FOUND Service not found
  497. * - ESP_ERR_NO_MEM memory error
  498. */
  499. esp_err_t mdns_service_txt_item_remove(const char *service_type, const char *proto, const char *key);
  500. /**
  501. * @brief Remove TXT item for service TXT record with hostname
  502. *
  503. * @param instance instance name
  504. * @param service_type service type (_http, _ftp, etc)
  505. * @param proto service protocol (_tcp, _udp)
  506. * @param hostname service hostname. If NULL, local hostname will be used.
  507. * @param key the key that you want to remove
  508. *
  509. * @return
  510. * - ESP_OK success
  511. * - ESP_ERR_INVALID_ARG Parameter error
  512. * - ESP_ERR_NOT_FOUND Service not found
  513. * - ESP_ERR_NO_MEM memory error
  514. */
  515. esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
  516. const char *key);
  517. /**
  518. * @brief Add a subtype for service.
  519. *
  520. * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol.
  521. * @param service_type service type (_http, _ftp, etc)
  522. * @param proto service protocol (_tcp, _udp)
  523. * @param hostname service hostname. If NULL, local hostname will be used.
  524. * @param subtype The subtype to add.
  525. *
  526. * @return
  527. * - ESP_OK success
  528. * - ESP_ERR_INVALID_ARG Parameter error
  529. * - ESP_ERR_NOT_FOUND Service not found
  530. * - ESP_ERR_NO_MEM memory error
  531. */
  532. esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service_type, const char *proto,
  533. const char *hostname, const char *subtype);
  534. /**
  535. * @brief Remove a subtype for service.
  536. *
  537. * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol.
  538. * @param service_type service type (_http, _ftp, etc)
  539. * @param proto service protocol (_tcp, _udp)
  540. * @param hostname service hostname. If NULL, local hostname will be used.
  541. * @param subtype The subtype to remove.
  542. *
  543. * @return
  544. * - ESP_OK success
  545. * - ESP_ERR_INVALID_ARG Parameter error
  546. * - ESP_ERR_NOT_FOUND Service not found
  547. */
  548. esp_err_t mdns_service_subtype_remove_for_host(const char *instance_name, const char *service_type, const char *proto,
  549. const char *hostname, const char *subtype);
  550. /**
  551. * @brief Add multiple subtypes for service at once.
  552. *
  553. * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol.
  554. * @param service_type service type (_http, _ftp, etc)
  555. * @param proto service protocol (_tcp, _udp)
  556. * @param hostname service hostname. If NULL, local hostname will be used.
  557. * @param subtype the pointer of subtype array to add.
  558. * @param num_items number of items in subtype array
  559. *
  560. * @return
  561. * - ESP_OK success
  562. * - ESP_ERR_INVALID_ARG Parameter error
  563. * - ESP_ERR_NOT_FOUND Service not found
  564. * - ESP_ERR_NO_MEM memory error
  565. */
  566. esp_err_t mdns_service_subtype_add_multiple_items_for_host(const char *instance_name, const char *service_type, const char *proto,
  567. const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items);
  568. /**
  569. * @brief Update subtype for service.
  570. *
  571. * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol.
  572. * @param service_type service type (_http, _ftp, etc)
  573. * @param proto service protocol (_tcp, _udp)
  574. * @param hostname service hostname. If NULL, local hostname will be used.
  575. * @param subtype the pointer of subtype array to add.
  576. * @param num_items number of items in subtype array
  577. *
  578. * @note If `num_items` is 0, then remove all subtypes.
  579. *
  580. * @return
  581. * - ESP_OK success
  582. * - ESP_ERR_INVALID_ARG Parameter error
  583. * - ESP_ERR_NOT_FOUND Service not found
  584. * - ESP_ERR_NO_MEM memory error
  585. */
  586. esp_err_t mdns_service_subtype_update_multiple_items_for_host(const char *instance_name, const char *service_type, const char *proto,
  587. const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items);
  588. /**
  589. * @brief Remove and free all services from mDNS server
  590. *
  591. * @return
  592. * - ESP_OK success
  593. * - ESP_ERR_INVALID_ARG Parameter error
  594. */
  595. esp_err_t mdns_service_remove_all(void);
  596. /**
  597. * @brief Deletes the finished query. Call this only after the search has ended!
  598. *
  599. * @param search pointer to search object
  600. *
  601. * @return
  602. * - ESP_OK success
  603. * - ESP_ERR_INVALID_STATE search has not finished
  604. * - ESP_ERR_INVALID_ARG pointer to search object is NULL
  605. */
  606. esp_err_t mdns_query_async_delete(mdns_search_once_t *search);
  607. /**
  608. * @brief Get results from search pointer. Results available as a pointer to the output parameter.
  609. * Pointer to search object has to be deleted via `mdns_query_async_delete` once the query has finished.
  610. * The results although have to be freed manually.
  611. *
  612. * @param search pointer to search object
  613. * @param timeout time in milliseconds to wait for answers
  614. * @param results pointer to the results of the query
  615. * @param num_results pointer to the number of the actual result items (set to NULL to ignore this return value)
  616. *
  617. * @return
  618. * True if search has finished before or at timeout
  619. * False if search timeout is over
  620. */
  621. bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, mdns_result_t **results, uint8_t *num_results);
  622. /**
  623. * @brief Query mDNS for host or service asynchronousely.
  624. * Search has to be tested for progress and deleted manually!
  625. *
  626. * @param name service instance or host name (NULL for PTR queries)
  627. * @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
  628. * @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
  629. * @param type type of query (MDNS_TYPE_*)
  630. * @param timeout time in milliseconds during which mDNS query is active
  631. * @param max_results maximum results to be collected
  632. * @param notifier Notification function to be called when the result is ready, can be NULL
  633. *
  634. * @return mdns_search_once_s pointer to new search object if query initiated successfully.
  635. * NULL otherwise.
  636. */
  637. mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_type, const char *proto, uint16_t type,
  638. uint32_t timeout, size_t max_results, mdns_query_notify_t notifier);
  639. /**
  640. * @brief Generic mDNS query
  641. * All following query methods are derived from this one
  642. *
  643. * @param name service instance or host name (NULL for PTR queries)
  644. * @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
  645. * @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
  646. * @param type type of query (MDNS_TYPE_*)
  647. * @param transmission_type either Unicast query, or Multicast query
  648. * @param timeout time in milliseconds to wait for answers.
  649. * @param max_results maximum results to be collected
  650. * @param results pointer to the results of the query
  651. * results must be freed using mdns_query_results_free below
  652. *
  653. * @return
  654. * - ESP_OK success
  655. * - ESP_ERR_INVALID_STATE mDNS is not running
  656. * - ESP_ERR_NO_MEM memory error
  657. * - ESP_ERR_INVALID_ARG timeout was not given
  658. */
  659. esp_err_t mdns_query_generic(const char *name, const char *service_type, const char *proto, uint16_t type,
  660. mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t **results);
  661. /**
  662. * @brief Query mDNS for host or service
  663. *
  664. * Note that querying PTR types sends Multicast query, all other types send Unicast queries
  665. *
  666. * @param name service instance or host name (NULL for PTR queries)
  667. * @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
  668. * @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
  669. * @param type type of query (MDNS_TYPE_*)
  670. * @param timeout time in milliseconds to wait for answers.
  671. * @param max_results maximum results to be collected
  672. * @param results pointer to the results of the query
  673. * results must be freed using mdns_query_results_free below
  674. *
  675. * @return
  676. * - ESP_OK success
  677. * - ESP_ERR_INVALID_STATE mDNS is not running
  678. * - ESP_ERR_NO_MEM memory error
  679. * - ESP_ERR_INVALID_ARG timeout was not given
  680. */
  681. esp_err_t mdns_query(const char *name, const char *service_type, const char *proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t **results);
  682. /**
  683. * @brief Free query results
  684. *
  685. * @param results linked list of results to be freed
  686. */
  687. void mdns_query_results_free(mdns_result_t *results);
  688. /**
  689. * @brief Query mDNS for service
  690. *
  691. * @param service_type service type (_http, _arduino, _ftp etc.)
  692. * @param proto service protocol (_tcp, _udp, etc.)
  693. * @param timeout time in milliseconds to wait for answer.
  694. * @param max_results maximum results to be collected
  695. * @param results pointer to the results of the query
  696. *
  697. * @return
  698. * - ESP_OK success
  699. * - ESP_ERR_INVALID_STATE mDNS is not running
  700. * - ESP_ERR_NO_MEM memory error
  701. * - ESP_ERR_INVALID_ARG parameter error
  702. */
  703. esp_err_t mdns_query_ptr(const char *service_type, const char *proto, uint32_t timeout, size_t max_results, mdns_result_t **results);
  704. /**
  705. * @brief Query mDNS for SRV record
  706. *
  707. * @param instance_name service instance name
  708. * @param service_type service type (_http, _arduino, _ftp etc.)
  709. * @param proto service protocol (_tcp, _udp, etc.)
  710. * @param timeout time in milliseconds to wait for answer.
  711. * @param result pointer to the result of the query
  712. *
  713. * @return
  714. * - ESP_OK success
  715. * - ESP_ERR_INVALID_STATE mDNS is not running
  716. * - ESP_ERR_NO_MEM memory error
  717. * - ESP_ERR_INVALID_ARG parameter error
  718. */
  719. esp_err_t mdns_query_srv(const char *instance_name, const char *service_type, const char *proto, uint32_t timeout, mdns_result_t **result);
  720. /**
  721. * @brief Query mDNS for TXT record
  722. *
  723. * @param instance_name service instance name
  724. * @param service_type service type (_http, _arduino, _ftp etc.)
  725. * @param proto service protocol (_tcp, _udp, etc.)
  726. * @param timeout time in milliseconds to wait for answer.
  727. * @param result pointer to the result of the query
  728. *
  729. * @return
  730. * - ESP_OK success
  731. * - ESP_ERR_INVALID_STATE mDNS is not running
  732. * - ESP_ERR_NO_MEM memory error
  733. * - ESP_ERR_INVALID_ARG parameter error
  734. */
  735. esp_err_t mdns_query_txt(const char *instance_name, const char *service_type, const char *proto, uint32_t timeout, mdns_result_t **result);
  736. /**
  737. * @brief Look up delegated services.
  738. *
  739. * @param instance instance name (NULL for uncertain instance)
  740. * @param service_type service type (_http, _ftp, etc)
  741. * @param proto service protocol (_tcp, _udp)
  742. * @param max_results maximum results to be collected
  743. * @param result pointer to the result of the search
  744. *
  745. * @return
  746. * - ESP_OK success
  747. * - ESP_ERR_INVALID_STATE mDNS is not running
  748. * - ESP_ERR_NO_MEM memory error
  749. * - ESP_ERR_INVALID_ARG parameter error
  750. */
  751. esp_err_t mdns_lookup_delegated_service(const char *instance, const char *service_type, const char *proto, size_t max_results,
  752. mdns_result_t **result);
  753. /**
  754. * @brief Look up self hosted services.
  755. *
  756. * @param instance instance name (NULL for uncertain instance)
  757. * @param service_type service type (_http, _ftp, etc)
  758. * @param proto service protocol (_tcp, _udp)
  759. * @param max_results maximum results to be collected
  760. * @param result pointer to the result of the search
  761. *
  762. * @return
  763. * - ESP_OK success
  764. * - ESP_ERR_INVALID_STATE mDNS is not running
  765. * - ESP_ERR_NO_MEM memory error
  766. * - ESP_ERR_INVALID_ARG parameter error
  767. */
  768. esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *service_type, const char *proto, size_t max_results,
  769. mdns_result_t **result);
  770. /**
  771. * @brief Query mDNS for A record
  772. *
  773. * @param host_name host name to look for
  774. * @param timeout time in milliseconds to wait for answer.
  775. * @param addr pointer to the resulting IP4 address
  776. *
  777. * @return
  778. * - ESP_OK success
  779. * - ESP_ERR_INVALID_STATE mDNS is not running
  780. * - ESP_ERR_NO_MEM memory error
  781. * - ESP_ERR_INVALID_ARG parameter error
  782. */
  783. esp_err_t mdns_query_a(const char *host_name, uint32_t timeout, esp_ip4_addr_t *addr);
  784. #if CONFIG_LWIP_IPV6
  785. /**
  786. * @brief Query mDNS for A record
  787. *
  788. * Please note that hostname must not contain domain name, as mDNS uses '.local' domain.
  789. *
  790. * @param host_name host name to look for
  791. * @param timeout time in milliseconds to wait for answer. If 0, max_results needs to be defined
  792. * @param addr pointer to the resulting IP6 address
  793. *
  794. * @return
  795. * - ESP_OK success
  796. * - ESP_ERR_INVALID_STATE mDNS is not running
  797. * - ESP_ERR_NO_MEM memory error
  798. * - ESP_ERR_INVALID_ARG parameter error
  799. */
  800. esp_err_t mdns_query_aaaa(const char *host_name, uint32_t timeout, esp_ip6_addr_t *addr);
  801. #endif
  802. /**
  803. * @brief Register custom esp_netif with mDNS functionality
  804. * mDNS service runs by default on preconfigured interfaces (STA, AP, ETH).
  805. * This API enables running the service on any customized interface,
  806. * either using standard WiFi or Ethernet driver or any kind of user defined driver.
  807. *
  808. * @param esp_netif Pointer to esp-netif interface
  809. * @return
  810. * - ESP_OK success
  811. * - ESP_ERR_INVALID_STATE mDNS is not running or this netif is already registered
  812. * - ESP_ERR_NO_MEM not enough memory for this in interface in the netif list (see CONFIG_MDNS_MAX_INTERFACES)
  813. */
  814. esp_err_t mdns_register_netif(esp_netif_t *esp_netif);
  815. /**
  816. * @brief Unregister esp-netif already registered in mDNS service
  817. *
  818. * @param esp_netif Pointer to esp-netif interface
  819. * @return
  820. * - ESP_OK success
  821. * - ESP_ERR_INVALID_STATE mDNS is not running
  822. * - ESP_ERR_NOT_FOUND this esp-netif was not registered in mDNS service
  823. */
  824. esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif);
  825. /**
  826. * @brief Set esp_netif to a desired state, or perform a desired action, such as enable/disable this interface
  827. * or send announcement packets to this netif
  828. *
  829. * * This function is used to enable (probe, resolve conflicts and announce), announce, or disable (send bye) mDNS
  830. * services on the specified network interface.
  831. * * This function must be called if users registers a specific interface using mdns_register_netif()
  832. * to enable mDNS services on that interface.
  833. * * This function could be used in IP/connection event handlers to automatically enable/announce mDNS services
  834. * when network properties change and/or disable them on disconnection.
  835. *
  836. * @param esp_netif Pointer to esp-netif interface
  837. * @param event_action Disable/Enable/Announce on this interface over IPv4/IPv6 protocol.
  838. * Actions enumerated in mdns_event_actions_t type.
  839. * @return
  840. * - ESP_OK success
  841. * - ESP_ERR_INVALID_STATE mDNS is not running or this netif is not registered
  842. * - ESP_ERR_NO_MEM memory error
  843. */
  844. esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action);
  845. /**
  846. * @brief Browse mDNS for a service `_service._proto`.
  847. *
  848. * @param service Pointer to the `_service` which will be browsed.
  849. * @param proto Pointer to the `_proto` which will be browsed.
  850. * @param notifier The callback which will be called when the browsing service changed.
  851. * @return mdns_browse_t pointer to new browse object if initiated successfully.
  852. * NULL otherwise.
  853. */
  854. mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_browse_notify_t notifier);
  855. /**
  856. * @brief Stop the `_service._proto` browse.
  857. * @param service Pointer to the `_service` which will be browsed.
  858. * @param proto Pointer to the `_proto` which will be browsed.
  859. * @return
  860. * - ESP_OK success.
  861. * - ESP_ERR_FAIL mDNS is not running or the browsing of `_service._proto` is never started.
  862. * - ESP_ERR_NO_MEM memory error.
  863. */
  864. esp_err_t mdns_browse_delete(const char *service, const char *proto);
  865. #ifdef __cplusplus
  866. }
  867. #endif
  868. #endif /* ESP_MDNS_H_ */