mdns.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #ifndef ESP_MDNS_H_
  14. #define ESP_MDNS_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #ifndef MDNS_TEST_MODE
  19. #include <tcpip_adapter.h>
  20. #include "esp_event.h"
  21. #else
  22. #include "esp32_compat.h"
  23. #endif
  24. #define MDNS_TYPE_A 0x0001
  25. #define MDNS_TYPE_PTR 0x000C
  26. #define MDNS_TYPE_TXT 0x0010
  27. #define MDNS_TYPE_AAAA 0x001C
  28. #define MDNS_TYPE_SRV 0x0021
  29. #define MDNS_TYPE_OPT 0x0029
  30. #define MDNS_TYPE_NSEC 0x002F
  31. #define MDNS_TYPE_ANY 0x00FF
  32. /**
  33. * @brief mDNS enum to specify the ip_protocol type
  34. */
  35. typedef enum {
  36. MDNS_IP_PROTOCOL_V4,
  37. MDNS_IP_PROTOCOL_V6,
  38. MDNS_IP_PROTOCOL_MAX
  39. } mdns_ip_protocol_t;
  40. /**
  41. * @brief mDNS basic text item structure
  42. * Used in mdns_service_add()
  43. */
  44. typedef struct {
  45. char * key; /*!< item key name */
  46. char * value; /*!< item value string */
  47. } mdns_txt_item_t;
  48. /**
  49. * @brief mDNS query linked list IP item
  50. */
  51. typedef struct mdns_ip_addr_s {
  52. ip_addr_t addr; /*!< IP address */
  53. struct mdns_ip_addr_s * next; /*!< next IP, or NULL for the last IP in the list */
  54. } mdns_ip_addr_t;
  55. /**
  56. * @brief mDNS query result structure
  57. */
  58. typedef struct mdns_result_s {
  59. struct mdns_result_s * next; /*!< next result, or NULL for the last result in the list */
  60. tcpip_adapter_if_t tcpip_if; /*!< interface on which the result came (AP/STA/ETH) */
  61. mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */
  62. // PTR
  63. char * instance_name; /*!< instance name */
  64. // SRV
  65. char * hostname; /*!< hostname */
  66. uint16_t port; /*!< service port */
  67. // TXT
  68. mdns_txt_item_t * txt; /*!< txt record */
  69. size_t txt_count; /*!< number of txt items */
  70. // A and AAAA
  71. mdns_ip_addr_t * addr; /*!< linked list of IP addreses found */
  72. } mdns_result_t;
  73. /**
  74. * @brief Initialize mDNS on given interface
  75. *
  76. * @return
  77. * - ESP_OK on success
  78. * - ESP_ERR_INVALID_ARG when bad tcpip_if is given
  79. * - ESP_ERR_INVALID_STATE when the network returned error
  80. * - ESP_ERR_NO_MEM on memory error
  81. * - ESP_ERR_WIFI_NOT_INIT when WiFi is not initialized by eps_wifi_init
  82. */
  83. esp_err_t mdns_init();
  84. /**
  85. * @brief Stop and free mDNS server
  86. *
  87. */
  88. void mdns_free();
  89. /**
  90. * @brief Set the hostname for mDNS server
  91. * required if you want to advertise services
  92. *
  93. * @param hostname Hostname to set
  94. *
  95. * @return
  96. * - ESP_OK success
  97. * - ESP_ERR_INVALID_ARG Parameter error
  98. * - ESP_ERR_NO_MEM memory error
  99. */
  100. esp_err_t mdns_hostname_set(const char * hostname);
  101. /**
  102. * @brief Set the default instance name for mDNS server
  103. *
  104. * @param instance_name Instance name to set
  105. *
  106. * @return
  107. * - ESP_OK success
  108. * - ESP_ERR_INVALID_ARG Parameter error
  109. * - ESP_ERR_NO_MEM memory error
  110. */
  111. esp_err_t mdns_instance_name_set(const char * instance_name);
  112. /**
  113. * @brief Add service to mDNS server
  114. *
  115. * @param instance_name instance name to set. If NULL,
  116. * global instance name or hostname will be used
  117. * @param service_type service type (_http, _ftp, etc)
  118. * @param proto service protocol (_tcp, _udp)
  119. * @param port service port
  120. * @param num_items number of items in TXT data
  121. * @param txt string array of TXT data (eg. {{"var","val"},{"other","2"}})
  122. *
  123. * @return
  124. * - ESP_OK success
  125. * - ESP_ERR_INVALID_ARG Parameter error
  126. * - ESP_ERR_NO_MEM memory error
  127. */
  128. 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);
  129. /**
  130. * @brief Remove service from mDNS server
  131. *
  132. * @param service_type service type (_http, _ftp, etc)
  133. * @param proto service protocol (_tcp, _udp)
  134. *
  135. * @return
  136. * - ESP_OK success
  137. * - ESP_ERR_INVALID_ARG Parameter error
  138. * - ESP_ERR_NOT_FOUND Service not found
  139. * - ESP_FAIL unknown error
  140. */
  141. esp_err_t mdns_service_remove(const char * service_type, const char * proto);
  142. /**
  143. * @brief Set instance name for service
  144. *
  145. * @param service_type service type (_http, _ftp, etc)
  146. * @param proto service protocol (_tcp, _udp)
  147. * @param instance_name instance name to set
  148. *
  149. * @return
  150. * - ESP_OK success
  151. * - ESP_ERR_INVALID_ARG Parameter error
  152. * - ESP_ERR_NOT_FOUND Service not found
  153. * - ESP_ERR_NO_MEM memory error
  154. */
  155. esp_err_t mdns_service_instance_name_set(const char * service_type, const char * proto, const char * instance_name);
  156. /**
  157. * @brief Set service port
  158. *
  159. * @param service_type service type (_http, _ftp, etc)
  160. * @param proto service protocol (_tcp, _udp)
  161. * @param port service port
  162. *
  163. * @return
  164. * - ESP_OK success
  165. * - ESP_ERR_INVALID_ARG Parameter error
  166. * - ESP_ERR_NOT_FOUND Service not found
  167. */
  168. esp_err_t mdns_service_port_set(const char * service_type, const char * proto, uint16_t port);
  169. /**
  170. * @brief Replace all TXT items for service
  171. *
  172. * @param service_type service type (_http, _ftp, etc)
  173. * @param proto service protocol (_tcp, _udp)
  174. * @param num_items number of items in TXT data
  175. * @param txt array of TXT data (eg. {{"var","val"},{"other","2"}})
  176. *
  177. * @return
  178. * - ESP_OK success
  179. * - ESP_ERR_INVALID_ARG Parameter error
  180. * - ESP_ERR_NOT_FOUND Service not found
  181. * - ESP_ERR_NO_MEM memory error
  182. */
  183. esp_err_t mdns_service_txt_set(const char * service_type, const char * proto, mdns_txt_item_t txt[], uint8_t num_items);
  184. /**
  185. * @brief Set/Add TXT item for service TXT record
  186. *
  187. * @param service_type service type (_http, _ftp, etc)
  188. * @param proto service protocol (_tcp, _udp)
  189. * @param key the key that you want to add/update
  190. * @param value the new value of the key
  191. *
  192. * @return
  193. * - ESP_OK success
  194. * - ESP_ERR_INVALID_ARG Parameter error
  195. * - ESP_ERR_NOT_FOUND Service not found
  196. * - ESP_ERR_NO_MEM memory error
  197. */
  198. esp_err_t mdns_service_txt_item_set(const char * service_type, const char * proto, const char * key, const char * value);
  199. /**
  200. * @brief Remove TXT item for service TXT record
  201. *
  202. * @param service_type service type (_http, _ftp, etc)
  203. * @param proto service protocol (_tcp, _udp)
  204. * @param key the key that you want to remove
  205. *
  206. * @return
  207. * - ESP_OK success
  208. * - ESP_ERR_INVALID_ARG Parameter error
  209. * - ESP_ERR_NOT_FOUND Service not found
  210. * - ESP_ERR_NO_MEM memory error
  211. */
  212. esp_err_t mdns_service_txt_item_remove(const char * service_type, const char * proto, const char * key);
  213. /**
  214. * @brief Remove and free all services from mDNS server
  215. *
  216. * @return
  217. * - ESP_OK success
  218. * - ESP_ERR_INVALID_ARG Parameter error
  219. */
  220. esp_err_t mdns_service_remove_all();
  221. /**
  222. * @brief Query mDNS for host or service
  223. * All following query methods are derived from this one
  224. *
  225. * @param name service instance or host name (NULL for PTR queries)
  226. * @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
  227. * @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
  228. * @param type type of query (MDNS_TYPE_*)
  229. * @param timeout time in milliseconds to wait for answers.
  230. * @param max_results maximum results to be collected
  231. * @param results pointer to the results of the query
  232. * results must be freed using mdns_query_results_free below
  233. *
  234. * @return
  235. * - ESP_OK success
  236. * - ESP_ERR_INVALID_STATE mDNS is not running
  237. * - ESP_ERR_NO_MEM memory error
  238. * - ESP_ERR_INVALID_ARG timeout was not given
  239. */
  240. 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);
  241. /**
  242. * @brief Free query results
  243. *
  244. * @param results linked list of results to be freed
  245. */
  246. void mdns_query_results_free(mdns_result_t * results);
  247. /**
  248. * @brief Query mDNS for service
  249. *
  250. * @param service_type service type (_http, _arduino, _ftp etc.)
  251. * @param proto service protocol (_tcp, _udp, etc.)
  252. * @param timeout time in milliseconds to wait for answer.
  253. * @param max_results maximum results to be collected
  254. * @param results pointer to the results of the query
  255. *
  256. * @return
  257. * - ESP_OK success
  258. * - ESP_ERR_INVALID_STATE mDNS is not running
  259. * - ESP_ERR_NO_MEM memory error
  260. * - ESP_ERR_INVALID_ARG parameter error
  261. */
  262. esp_err_t mdns_query_ptr(const char * service_type, const char * proto, uint32_t timeout, size_t max_results, mdns_result_t ** results);
  263. /**
  264. * @brief Query mDNS for SRV record
  265. *
  266. * @param instance_name service instance name
  267. * @param service_type service type (_http, _arduino, _ftp etc.)
  268. * @param proto service protocol (_tcp, _udp, etc.)
  269. * @param timeout time in milliseconds to wait for answer.
  270. * @param result pointer to the result of the query
  271. *
  272. * @return
  273. * - ESP_OK success
  274. * - ESP_ERR_INVALID_STATE mDNS is not running
  275. * - ESP_ERR_NO_MEM memory error
  276. * - ESP_ERR_INVALID_ARG parameter error
  277. */
  278. esp_err_t mdns_query_srv(const char * instance_name, const char * service_type, const char * proto, uint32_t timeout, mdns_result_t ** result);
  279. /**
  280. * @brief Query mDNS for TXT record
  281. *
  282. * @param instance_name service instance name
  283. * @param service_type service type (_http, _arduino, _ftp etc.)
  284. * @param proto service protocol (_tcp, _udp, etc.)
  285. * @param timeout time in milliseconds to wait for answer.
  286. * @param result pointer to the result of the query
  287. *
  288. * @return
  289. * - ESP_OK success
  290. * - ESP_ERR_INVALID_STATE mDNS is not running
  291. * - ESP_ERR_NO_MEM memory error
  292. * - ESP_ERR_INVALID_ARG parameter error
  293. */
  294. esp_err_t mdns_query_txt(const char * instance_name, const char * service_type, const char * proto, uint32_t timeout, mdns_result_t ** result);
  295. /**
  296. * @brief Query mDNS for A record
  297. *
  298. * @param host_name host name to look for
  299. * @param timeout time in milliseconds to wait for answer.
  300. * @param addr pointer to the resulting IP4 address
  301. *
  302. * @return
  303. * - ESP_OK success
  304. * - ESP_ERR_INVALID_STATE mDNS is not running
  305. * - ESP_ERR_NO_MEM memory error
  306. * - ESP_ERR_INVALID_ARG parameter error
  307. */
  308. esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, ip4_addr_t * addr);
  309. /**
  310. * @brief Query mDNS for A record
  311. *
  312. * @param host_name host name to look for
  313. * @param timeout time in milliseconds to wait for answer. If 0, max_results needs to be defined
  314. * @param addr pointer to the resulting IP6 address
  315. *
  316. * @return
  317. * - ESP_OK success
  318. * - ESP_ERR_INVALID_STATE mDNS is not running
  319. * - ESP_ERR_NO_MEM memory error
  320. * - ESP_ERR_INVALID_ARG parameter error
  321. */
  322. esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, ip6_addr_t * addr);
  323. /**
  324. * @brief System event handler
  325. * This method controls the service state on all active interfaces and applications are required
  326. * to call it from the system event handler for normal operation of mDNS service.
  327. *
  328. * @param ctx The system event context
  329. * @param event The system event
  330. */
  331. esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event);
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif /* ESP_MDNS_H_ */