mdns.h 12 KB

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