mdns.h 12 KB

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