mdns.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. char * key; /*!< item key name */
  42. 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_ARG when bad tcpip_if is given
  75. * - ESP_ERR_INVALID_STATE when the network returned error
  76. * - ESP_ERR_NO_MEM on memory error
  77. * - ESP_ERR_WIFI_NOT_INIT when WiFi is not initialized by eps_wifi_init
  78. */
  79. esp_err_t mdns_init();
  80. /**
  81. * @brief Stop and free mDNS server
  82. *
  83. */
  84. void mdns_free();
  85. /**
  86. * @brief Set the hostname for mDNS server
  87. * required if you want to advertise services
  88. *
  89. * @param hostname Hostname to set
  90. *
  91. * @return
  92. * - ESP_OK success
  93. * - ESP_ERR_INVALID_ARG Parameter error
  94. * - ESP_ERR_NO_MEM memory error
  95. */
  96. esp_err_t mdns_hostname_set(const char * hostname);
  97. /**
  98. * @brief Set the default instance name for mDNS server
  99. *
  100. * @param instance_name Instance name to set
  101. *
  102. * @return
  103. * - ESP_OK success
  104. * - ESP_ERR_INVALID_ARG Parameter error
  105. * - ESP_ERR_NO_MEM memory error
  106. */
  107. esp_err_t mdns_instance_name_set(const char * instance_name);
  108. /**
  109. * @brief Add service to mDNS server
  110. *
  111. * @param instance_name instance name to set. If NULL,
  112. * global instance name or hostname will be used
  113. * @param service_type service type (_http, _ftp, etc)
  114. * @param proto service protocol (_tcp, _udp)
  115. * @param port service port
  116. * @param num_items number of items in TXT data
  117. * @param txt string array of TXT data (eg. {{"var","val"},{"other","2"}})
  118. *
  119. * @return
  120. * - ESP_OK success
  121. * - ESP_ERR_INVALID_ARG Parameter error
  122. * - ESP_ERR_NO_MEM memory error
  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_FAIL unknown 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. */
  164. esp_err_t mdns_service_port_set(const char * service_type, const char * proto, uint16_t port);
  165. /**
  166. * @brief Replace all TXT items for service
  167. *
  168. * @param service_type service type (_http, _ftp, etc)
  169. * @param proto service protocol (_tcp, _udp)
  170. * @param num_items number of items in TXT data
  171. * @param txt array of TXT data (eg. {{"var","val"},{"other","2"}})
  172. *
  173. * @return
  174. * - ESP_OK success
  175. * - ESP_ERR_INVALID_ARG Parameter error
  176. * - ESP_ERR_NOT_FOUND Service not found
  177. * - ESP_ERR_NO_MEM memory error
  178. */
  179. esp_err_t mdns_service_txt_set(const char * service_type, const char * proto, mdns_txt_item_t txt[], uint8_t num_items);
  180. /**
  181. * @brief Set/Add TXT item for service TXT record
  182. *
  183. * @param service_type service type (_http, _ftp, etc)
  184. * @param proto service protocol (_tcp, _udp)
  185. * @param key the key that you want to add/update
  186. * @param value the new value of the key
  187. *
  188. * @return
  189. * - ESP_OK success
  190. * - ESP_ERR_INVALID_ARG Parameter error
  191. * - ESP_ERR_NOT_FOUND Service not found
  192. * - ESP_ERR_NO_MEM memory error
  193. */
  194. esp_err_t mdns_service_txt_item_set(const char * service_type, const char * proto, const char * key, const char * value);
  195. /**
  196. * @brief Remove TXT item for service TXT record
  197. *
  198. * @param service_type service type (_http, _ftp, etc)
  199. * @param proto service protocol (_tcp, _udp)
  200. * @param key the key that you want to remove
  201. *
  202. * @return
  203. * - ESP_OK success
  204. * - ESP_ERR_INVALID_ARG Parameter error
  205. * - ESP_ERR_NOT_FOUND Service not found
  206. * - ESP_ERR_NO_MEM memory error
  207. */
  208. esp_err_t mdns_service_txt_item_remove(const char * service_type, const char * proto, const char * key);
  209. /**
  210. * @brief Remove and free all services from mDNS server
  211. *
  212. * @return
  213. * - ESP_OK success
  214. * - ESP_ERR_INVALID_ARG Parameter error
  215. */
  216. esp_err_t mdns_service_remove_all();
  217. /**
  218. * @brief Query mDNS for host or service
  219. * All following query methods are derived from this one
  220. *
  221. * @param name service instance or host name (NULL for PTR queries)
  222. * @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
  223. * @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
  224. * @param type type of query (MDNS_TYPE_*)
  225. * @param timeout time in milliseconds to wait for answers.
  226. * @param max_results maximum results to be collected
  227. * @param results pointer to the results of the query
  228. * results must be freed using mdns_query_results_free below
  229. *
  230. * @return
  231. * - ESP_OK success
  232. * - ESP_ERR_INVALID_STATE mDNS is not running
  233. * - ESP_ERR_NO_MEM memory error
  234. * - ESP_ERR_INVALID_ARG timeout was not given
  235. */
  236. 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);
  237. /**
  238. * @brief Free query results
  239. *
  240. * @param results linked list of results to be freed
  241. */
  242. void mdns_query_results_free(mdns_result_t * results);
  243. /**
  244. * @brief Query mDNS for service
  245. *
  246. * @param service_type service type (_http, _arduino, _ftp etc.)
  247. * @param proto service protocol (_tcp, _udp, etc.)
  248. * @param timeout time in milliseconds to wait for answer.
  249. * @param max_results maximum results to be collected
  250. * @param results pointer to the results of the query
  251. *
  252. * @return
  253. * - ESP_OK success
  254. * - ESP_ERR_INVALID_STATE mDNS is not running
  255. * - ESP_ERR_NO_MEM memory error
  256. * - ESP_ERR_INVALID_ARG parameter error
  257. */
  258. esp_err_t mdns_query_ptr(const char * service_type, const char * proto, uint32_t timeout, size_t max_results, mdns_result_t ** results);
  259. /**
  260. * @brief Query mDNS for SRV record
  261. *
  262. * @param instance_name service instance name
  263. * @param service_type service type (_http, _arduino, _ftp etc.)
  264. * @param proto service protocol (_tcp, _udp, etc.)
  265. * @param timeout time in milliseconds to wait for answer.
  266. * @param result pointer to the result of the query
  267. *
  268. * @return
  269. * - ESP_OK success
  270. * - ESP_ERR_INVALID_STATE mDNS is not running
  271. * - ESP_ERR_NO_MEM memory error
  272. * - ESP_ERR_INVALID_ARG parameter error
  273. */
  274. esp_err_t mdns_query_srv(const char * instance_name, const char * service_type, const char * proto, uint32_t timeout, mdns_result_t ** result);
  275. /**
  276. * @brief Query mDNS for TXT record
  277. *
  278. * @param instance_name service instance name
  279. * @param service_type service type (_http, _arduino, _ftp etc.)
  280. * @param proto service protocol (_tcp, _udp, etc.)
  281. * @param timeout time in milliseconds to wait for answer.
  282. * @param result pointer to the result of the query
  283. *
  284. * @return
  285. * - ESP_OK success
  286. * - ESP_ERR_INVALID_STATE mDNS is not running
  287. * - ESP_ERR_NO_MEM memory error
  288. * - ESP_ERR_INVALID_ARG parameter error
  289. */
  290. esp_err_t mdns_query_txt(const char * instance_name, const char * service_type, const char * proto, uint32_t timeout, mdns_result_t ** result);
  291. /**
  292. * @brief Query mDNS for A record
  293. *
  294. * @param host_name host name to look for
  295. * @param timeout time in milliseconds to wait for answer.
  296. * @param addr pointer to the resulting IP4 address
  297. *
  298. * @return
  299. * - ESP_OK success
  300. * - ESP_ERR_INVALID_STATE mDNS is not running
  301. * - ESP_ERR_NO_MEM memory error
  302. * - ESP_ERR_INVALID_ARG parameter error
  303. */
  304. esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, ip4_addr_t * addr);
  305. /**
  306. * @brief Query mDNS for A record
  307. *
  308. * @param host_name host name to look for
  309. * @param timeout time in milliseconds to wait for answer. If 0, max_results needs to be defined
  310. * @param addr pointer to the resulting IP6 address
  311. *
  312. * @return
  313. * - ESP_OK success
  314. * - ESP_ERR_INVALID_STATE mDNS is not running
  315. * - ESP_ERR_NO_MEM memory error
  316. * - ESP_ERR_INVALID_ARG parameter error
  317. */
  318. esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, ip6_addr_t * addr);
  319. /**
  320. * @brief System event handler
  321. * This method controls the service state on all active interfaces and applications are required
  322. * to call it from the system event handler for normal operation of mDNS service.
  323. *
  324. * @param ctx The system event context
  325. * @param event The system event
  326. */
  327. esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event);
  328. #ifdef __cplusplus
  329. }
  330. #endif
  331. #endif /* ESP_MDNS_H_ */