mdns_console.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. #include <stdio.h>
  14. #include <string.h>
  15. #include "esp_console.h"
  16. #include "argtable3/argtable3.h"
  17. #include "mdns.h"
  18. static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
  19. static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
  20. static void mdns_print_results(mdns_result_t * results)
  21. {
  22. mdns_result_t * r = results;
  23. mdns_ip_addr_t * a = NULL;
  24. int i = 1, t;
  25. while (r) {
  26. printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
  27. if (r->instance_name) {
  28. printf(" PTR : %s\n", r->instance_name);
  29. }
  30. if (r->hostname) {
  31. printf(" SRV : %s.local:%u\n", r->hostname, r->port);
  32. }
  33. if (r->txt_count) {
  34. printf(" TXT : [%u] ", r->txt_count);
  35. for (t=0; t<r->txt_count; t++) {
  36. printf("%s=%s; ", r->txt[t].key, r->txt[t].value);
  37. }
  38. printf("\n");
  39. }
  40. a = r->addr;
  41. while (a) {
  42. if (a->addr.type == ESP_IPADDR_TYPE_V6) {
  43. printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
  44. } else {
  45. printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
  46. }
  47. a = a->next;
  48. }
  49. r = r->next;
  50. }
  51. }
  52. static struct {
  53. struct arg_str *hostname;
  54. struct arg_int *timeout;
  55. struct arg_end *end;
  56. } mdns_query_a_args;
  57. static int cmd_mdns_query_a(int argc, char** argv)
  58. {
  59. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args);
  60. if (nerrors != 0) {
  61. arg_print_errors(stderr, mdns_query_a_args.end, argv[0]);
  62. return 1;
  63. }
  64. const char * hostname = mdns_query_a_args.hostname->sval[0];
  65. int timeout = mdns_query_a_args.timeout->ival[0];
  66. if (!hostname || !hostname[0]) {
  67. printf("ERROR: Hostname not supplied\n");
  68. return 1;
  69. }
  70. if (timeout <= 0) {
  71. timeout = 1000;
  72. }
  73. printf("Query A: %s.local, Timeout: %d\n", hostname, timeout);
  74. struct esp_ip4_addr addr;
  75. addr.addr = 0;
  76. esp_err_t err = mdns_query_a(hostname, timeout, &addr);
  77. if (err) {
  78. if (err == ESP_ERR_NOT_FOUND) {
  79. printf("ERROR: Host was not found!\n");
  80. return 0;
  81. }
  82. printf("ERROR: Query Failed\n");
  83. return 1;
  84. }
  85. printf(IPSTR "\n", IP2STR(&addr));
  86. return 0;
  87. }
  88. static void register_mdns_query_a(void)
  89. {
  90. mdns_query_a_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Hostname that is searched for");
  91. mdns_query_a_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  92. mdns_query_a_args.end = arg_end(2);
  93. const esp_console_cmd_t cmd_init = {
  94. .command = "mdns_query_a",
  95. .help = "Query MDNS for IPv4",
  96. .hint = NULL,
  97. .func = &cmd_mdns_query_a,
  98. .argtable = &mdns_query_a_args
  99. };
  100. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  101. }
  102. static int cmd_mdns_query_aaaa(int argc, char** argv)
  103. {
  104. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args);
  105. if (nerrors != 0) {
  106. arg_print_errors(stderr, mdns_query_a_args.end, argv[0]);
  107. return 1;
  108. }
  109. const char * hostname = mdns_query_a_args.hostname->sval[0];
  110. int timeout = mdns_query_a_args.timeout->ival[0];
  111. if (!hostname || !hostname[0]) {
  112. printf("ERROR: Hostname not supplied\n");
  113. return 1;
  114. }
  115. if (timeout <= 0) {
  116. timeout = 1000;
  117. }
  118. printf("Query AAAA: %s.local, Timeout: %d\n", hostname, timeout);
  119. struct esp_ip6_addr addr;
  120. memset(addr.addr, 0, 16);
  121. esp_err_t err = mdns_query_aaaa(hostname, timeout, &addr);
  122. if (err) {
  123. if (err == ESP_ERR_NOT_FOUND) {
  124. printf("Host was not found!\n");
  125. return 0;
  126. }
  127. printf("ERROR: Query Failed\n");
  128. return 1;
  129. }
  130. printf(IPV6STR "\n", IPV62STR(addr));
  131. return 0;
  132. }
  133. static void register_mdns_query_aaaa(void)
  134. {
  135. mdns_query_a_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Hostname that is searched for");
  136. mdns_query_a_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  137. mdns_query_a_args.end = arg_end(2);
  138. const esp_console_cmd_t cmd_init = {
  139. .command = "mdns_query_aaaa",
  140. .help = "Query MDNS for IPv6",
  141. .hint = NULL,
  142. .func = &cmd_mdns_query_aaaa,
  143. .argtable = &mdns_query_a_args
  144. };
  145. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  146. }
  147. static struct {
  148. struct arg_str *instance;
  149. struct arg_str *service;
  150. struct arg_str *proto;
  151. struct arg_int *timeout;
  152. struct arg_end *end;
  153. } mdns_query_srv_args;
  154. static int cmd_mdns_query_srv(int argc, char** argv)
  155. {
  156. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_srv_args);
  157. if (nerrors != 0) {
  158. arg_print_errors(stderr, mdns_query_srv_args.end, argv[0]);
  159. return 1;
  160. }
  161. const char * instance = mdns_query_srv_args.instance->sval[0];
  162. const char * service = mdns_query_srv_args.service->sval[0];
  163. const char * proto = mdns_query_srv_args.proto->sval[0];
  164. int timeout = mdns_query_srv_args.timeout->ival[0];
  165. if (timeout <= 0) {
  166. timeout = 1000;
  167. }
  168. printf("Query SRV: %s.%s.%s.local, Timeout: %d\n", instance, service, proto, timeout);
  169. mdns_result_t * results = NULL;
  170. esp_err_t err = mdns_query_srv(instance, service, proto, timeout, &results);
  171. if (err) {
  172. printf("ERROR: Query Failed\n");
  173. return 1;
  174. }
  175. if (!results) {
  176. printf("No results found!\n");
  177. return 0;
  178. }
  179. mdns_print_results(results);
  180. mdns_query_results_free(results);
  181. return 0;
  182. }
  183. static void register_mdns_query_srv(void)
  184. {
  185. mdns_query_srv_args.instance = arg_str1(NULL, NULL, "<instance>", "Instance to search for");
  186. mdns_query_srv_args.service = arg_str1(NULL, NULL, "<service>", "Service to search for (ex. _http, _smb, etc.)");
  187. mdns_query_srv_args.proto = arg_str1(NULL, NULL, "<proto>", "Protocol to search for (_tcp, _udp, etc.)");
  188. mdns_query_srv_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  189. mdns_query_srv_args.end = arg_end(2);
  190. const esp_console_cmd_t cmd_init = {
  191. .command = "mdns_query_srv",
  192. .help = "Query MDNS for Service SRV",
  193. .hint = NULL,
  194. .func = &cmd_mdns_query_srv,
  195. .argtable = &mdns_query_srv_args
  196. };
  197. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  198. }
  199. static struct {
  200. struct arg_str *instance;
  201. struct arg_str *service;
  202. struct arg_str *proto;
  203. struct arg_int *timeout;
  204. struct arg_end *end;
  205. } mdns_query_txt_args;
  206. static int cmd_mdns_query_txt(int argc, char** argv)
  207. {
  208. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_txt_args);
  209. if (nerrors != 0) {
  210. arg_print_errors(stderr, mdns_query_txt_args.end, argv[0]);
  211. return 1;
  212. }
  213. const char * instance = mdns_query_txt_args.instance->sval[0];
  214. const char * service = mdns_query_txt_args.service->sval[0];
  215. const char * proto = mdns_query_txt_args.proto->sval[0];
  216. int timeout = mdns_query_txt_args.timeout->ival[0];
  217. printf("Query TXT: %s.%s.%s.local, Timeout: %d\n", instance, service, proto, timeout);
  218. if (timeout <= 0) {
  219. timeout = 5000;
  220. }
  221. mdns_result_t * results = NULL;
  222. esp_err_t err = mdns_query_txt(instance, service, proto, timeout, &results);
  223. if (err) {
  224. printf("ERROR: Query Failed\n");
  225. return 1;
  226. }
  227. if (!results) {
  228. printf("No results found!\n");
  229. return 0;
  230. }
  231. mdns_print_results(results);
  232. mdns_query_results_free(results);
  233. return 0;
  234. }
  235. static void register_mdns_query_txt(void)
  236. {
  237. mdns_query_txt_args.instance = arg_str1(NULL, NULL, "<instance>", "Instance to search for");
  238. mdns_query_txt_args.service = arg_str1(NULL, NULL, "<service>", "Service to search for (ex. _http, _smb, etc.)");
  239. mdns_query_txt_args.proto = arg_str1(NULL, NULL, "<proto>", "Protocol to search for (_tcp, _udp, etc.)");
  240. mdns_query_txt_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  241. mdns_query_txt_args.end = arg_end(2);
  242. const esp_console_cmd_t cmd_init = {
  243. .command = "mdns_query_txt",
  244. .help = "Query MDNS for Service TXT",
  245. .hint = NULL,
  246. .func = &cmd_mdns_query_txt,
  247. .argtable = &mdns_query_txt_args
  248. };
  249. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  250. }
  251. static struct {
  252. struct arg_str *service;
  253. struct arg_str *proto;
  254. struct arg_int *timeout;
  255. struct arg_int *max_results;
  256. struct arg_end *end;
  257. } mdns_query_ptr_args;
  258. static int cmd_mdns_query_ptr(int argc, char** argv)
  259. {
  260. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_ptr_args);
  261. if (nerrors != 0) {
  262. arg_print_errors(stderr, mdns_query_ptr_args.end, argv[0]);
  263. return 1;
  264. }
  265. const char * service = mdns_query_ptr_args.service->sval[0];
  266. const char * proto = mdns_query_ptr_args.proto->sval[0];
  267. int timeout = mdns_query_ptr_args.timeout->ival[0];
  268. int max_results = mdns_query_ptr_args.max_results->ival[0];
  269. if (timeout <= 0) {
  270. timeout = 5000;
  271. }
  272. if (max_results <= 0 || max_results > 255) {
  273. max_results = 255;
  274. }
  275. printf("Query PTR: %s.%s.local, Timeout: %d, Max Results: %d\n", service, proto, timeout, max_results);
  276. mdns_result_t * results = NULL;
  277. esp_err_t err = mdns_query_ptr(service, proto, timeout, max_results, &results);
  278. if (err) {
  279. printf("ERROR: Query Failed\n");
  280. return 1;
  281. }
  282. if (!results) {
  283. printf("No results found!\n");
  284. return 0;
  285. }
  286. mdns_print_results(results);
  287. mdns_query_results_free(results);
  288. return 0;
  289. }
  290. static void register_mdns_query_ptr(void)
  291. {
  292. mdns_query_ptr_args.service = arg_str1(NULL, NULL, "<service>", "Service to search for (ex. _http, _smb, etc.)");
  293. mdns_query_ptr_args.proto = arg_str1(NULL, NULL, "<proto>", "Protocol to search for (_tcp, _udp, etc.)");
  294. mdns_query_ptr_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  295. mdns_query_ptr_args.max_results = arg_int0("m", "max_results", "<max_results>", "Maximum results returned");
  296. mdns_query_ptr_args.end = arg_end(2);
  297. const esp_console_cmd_t cmd_init = {
  298. .command = "mdns_query_ptr",
  299. .help = "Query MDNS for Service",
  300. .hint = NULL,
  301. .func = &cmd_mdns_query_ptr,
  302. .argtable = &mdns_query_ptr_args
  303. };
  304. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  305. }
  306. static struct {
  307. struct arg_str *hostname;
  308. struct arg_int *timeout;
  309. struct arg_int *max_results;
  310. struct arg_end *end;
  311. } mdns_query_ip_args;
  312. static int cmd_mdns_query_ip(int argc, char** argv)
  313. {
  314. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_ip_args);
  315. if (nerrors != 0) {
  316. arg_print_errors(stderr, mdns_query_ip_args.end, argv[0]);
  317. return 1;
  318. }
  319. const char * hostname = mdns_query_ip_args.hostname->sval[0];
  320. int timeout = mdns_query_ip_args.timeout->ival[0];
  321. int max_results = mdns_query_ip_args.max_results->ival[0];
  322. if (!hostname || !hostname[0]) {
  323. printf("ERROR: Hostname not supplied\n");
  324. return 1;
  325. }
  326. if (timeout <= 0) {
  327. timeout = 1000;
  328. }
  329. if (max_results < 0 || max_results > 255) {
  330. max_results = 255;
  331. }
  332. printf("Query IP: %s.local, Timeout: %d, Max Results: %d\n", hostname, timeout, max_results);
  333. mdns_result_t * results = NULL;
  334. esp_err_t err = mdns_query(hostname, NULL, NULL, MDNS_TYPE_ANY, timeout, max_results, &results);
  335. if (err) {
  336. printf("ERROR: Query Failed\n");
  337. return 1;
  338. }
  339. if (!results) {
  340. printf("No results found!\n");
  341. return 0;
  342. }
  343. mdns_print_results(results);
  344. mdns_query_results_free(results);
  345. return 0;
  346. }
  347. static void register_mdns_query_ip(void)
  348. {
  349. mdns_query_ip_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Hostname that is searched for");
  350. mdns_query_ip_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  351. mdns_query_ip_args.max_results = arg_int0("m", "max_results", "<max_results>", "Maximum results returned");
  352. mdns_query_ip_args.end = arg_end(2);
  353. const esp_console_cmd_t cmd_init = {
  354. .command = "mdns_query_ip",
  355. .help = "Query MDNS for IP",
  356. .hint = NULL,
  357. .func = &cmd_mdns_query_ip,
  358. .argtable = &mdns_query_ip_args
  359. };
  360. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  361. }
  362. static struct {
  363. struct arg_str *instance;
  364. struct arg_str *service;
  365. struct arg_str *proto;
  366. struct arg_int *timeout;
  367. struct arg_int *max_results;
  368. struct arg_end *end;
  369. } mdns_query_svc_args;
  370. static int cmd_mdns_query_svc(int argc, char** argv)
  371. {
  372. int nerrors = arg_parse(argc, argv, (void**) &mdns_query_svc_args);
  373. if (nerrors != 0) {
  374. arg_print_errors(stderr, mdns_query_svc_args.end, argv[0]);
  375. return 1;
  376. }
  377. const char * instance = mdns_query_svc_args.instance->sval[0];
  378. const char * service = mdns_query_svc_args.service->sval[0];
  379. const char * proto = mdns_query_svc_args.proto->sval[0];
  380. int timeout = mdns_query_svc_args.timeout->ival[0];
  381. int max_results = mdns_query_svc_args.max_results->ival[0];
  382. if (timeout <= 0) {
  383. timeout = 5000;
  384. }
  385. if (max_results < 0 || max_results > 255) {
  386. max_results = 255;
  387. }
  388. printf("Query SVC: %s.%s.%s.local, Timeout: %d, Max Results: %d\n", instance, service, proto, timeout, max_results);
  389. mdns_result_t * results = NULL;
  390. esp_err_t err = mdns_query(instance, service, proto, MDNS_TYPE_ANY, timeout, max_results, &results);
  391. if (err) {
  392. printf("ERROR: Query Failed\n");
  393. return 1;
  394. }
  395. if (!results) {
  396. printf("No results found!\n");
  397. return 0;
  398. }
  399. mdns_print_results(results);
  400. mdns_query_results_free(results);
  401. return 0;
  402. }
  403. static void register_mdns_query_svc(void)
  404. {
  405. mdns_query_svc_args.instance = arg_str1(NULL, NULL, "<instance>", "Instance to search for");
  406. mdns_query_svc_args.service = arg_str1(NULL, NULL, "<service>", "Service to search for (ex. _http, _smb, etc.)");
  407. mdns_query_svc_args.proto = arg_str1(NULL, NULL, "<proto>", "Protocol to search for (_tcp, _udp, etc.)");
  408. mdns_query_svc_args.timeout = arg_int0("t", "timeout", "<timeout>", "Timeout for this query");
  409. mdns_query_svc_args.max_results = arg_int0("m", "max_results", "<max_results>", "Maximum results returned");
  410. mdns_query_svc_args.end = arg_end(2);
  411. const esp_console_cmd_t cmd_init = {
  412. .command = "mdns_query_svc",
  413. .help = "Query MDNS for Service TXT & SRV",
  414. .hint = NULL,
  415. .func = &cmd_mdns_query_svc,
  416. .argtable = &mdns_query_svc_args
  417. };
  418. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  419. }
  420. static struct {
  421. struct arg_str *hostname;
  422. struct arg_str *instance;
  423. struct arg_end *end;
  424. } mdns_init_args;
  425. static int cmd_mdns_init(int argc, char** argv)
  426. {
  427. int nerrors = arg_parse(argc, argv, (void**) &mdns_init_args);
  428. if (nerrors != 0) {
  429. arg_print_errors(stderr, mdns_init_args.end, argv[0]);
  430. return 1;
  431. }
  432. ESP_ERROR_CHECK( mdns_init() );
  433. if (mdns_init_args.hostname->sval[0]) {
  434. ESP_ERROR_CHECK( mdns_hostname_set(mdns_init_args.hostname->sval[0]) );
  435. printf("MDNS: Hostname: %s\n", mdns_init_args.hostname->sval[0]);
  436. }
  437. if (mdns_init_args.instance->sval[0]) {
  438. ESP_ERROR_CHECK( mdns_instance_name_set(mdns_init_args.instance->sval[0]) );
  439. printf("MDNS: Instance: %s\n", mdns_init_args.instance->sval[0]);
  440. }
  441. return 0;
  442. }
  443. static void register_mdns_init(void)
  444. {
  445. mdns_init_args.hostname = arg_str0("h", "hostname", "<hostname>", "Hostname that the server will advertise");
  446. mdns_init_args.instance = arg_str0("i", "instance", "<instance>", "Default instance name for services");
  447. mdns_init_args.end = arg_end(2);
  448. const esp_console_cmd_t cmd_init = {
  449. .command = "mdns_init",
  450. .help = "Start MDNS Server",
  451. .hint = NULL,
  452. .func = &cmd_mdns_init,
  453. .argtable = &mdns_init_args
  454. };
  455. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
  456. }
  457. static int cmd_mdns_free(int argc, char** argv)
  458. {
  459. mdns_free();
  460. return 0;
  461. }
  462. static void register_mdns_free(void)
  463. {
  464. const esp_console_cmd_t cmd_free = {
  465. .command = "mdns_free",
  466. .help = "Stop MDNS Server",
  467. .hint = NULL,
  468. .func = &cmd_mdns_free,
  469. .argtable = NULL
  470. };
  471. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_free) );
  472. }
  473. static struct {
  474. struct arg_str *hostname;
  475. struct arg_end *end;
  476. } mdns_set_hostname_args;
  477. static int cmd_mdns_set_hostname(int argc, char** argv)
  478. {
  479. int nerrors = arg_parse(argc, argv, (void**) &mdns_set_hostname_args);
  480. if (nerrors != 0) {
  481. arg_print_errors(stderr, mdns_set_hostname_args.end, argv[0]);
  482. return 1;
  483. }
  484. if (mdns_set_hostname_args.hostname->sval[0] == NULL) {
  485. printf("ERROR: Bad arguments!\n");
  486. return 1;
  487. }
  488. ESP_ERROR_CHECK( mdns_hostname_set(mdns_set_hostname_args.hostname->sval[0]) );
  489. return 0;
  490. }
  491. static void register_mdns_set_hostname(void)
  492. {
  493. mdns_set_hostname_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Hostname that the server will advertise");
  494. mdns_set_hostname_args.end = arg_end(2);
  495. const esp_console_cmd_t cmd_set_hostname = {
  496. .command = "mdns_set_hostname",
  497. .help = "Set MDNS Server hostname",
  498. .hint = NULL,
  499. .func = &cmd_mdns_set_hostname,
  500. .argtable = &mdns_set_hostname_args
  501. };
  502. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_set_hostname) );
  503. }
  504. static struct {
  505. struct arg_str *instance;
  506. struct arg_end *end;
  507. } mdns_set_instance_args;
  508. static int cmd_mdns_set_instance(int argc, char** argv)
  509. {
  510. int nerrors = arg_parse(argc, argv, (void**) &mdns_set_instance_args);
  511. if (nerrors != 0) {
  512. arg_print_errors(stderr, mdns_set_instance_args.end, argv[0]);
  513. return 1;
  514. }
  515. if (mdns_set_instance_args.instance->sval[0] == NULL) {
  516. printf("ERROR: Bad arguments!\n");
  517. return 1;
  518. }
  519. ESP_ERROR_CHECK( mdns_instance_name_set(mdns_set_instance_args.instance->sval[0]) );
  520. return 0;
  521. }
  522. static void register_mdns_set_instance(void)
  523. {
  524. mdns_set_instance_args.instance = arg_str1(NULL, NULL, "<instance>", "Default instance name for services");
  525. mdns_set_instance_args.end = arg_end(2);
  526. const esp_console_cmd_t cmd_set_instance = {
  527. .command = "mdns_set_instance",
  528. .help = "Set MDNS Server Istance Name",
  529. .hint = NULL,
  530. .func = &cmd_mdns_set_instance,
  531. .argtable = &mdns_set_instance_args
  532. };
  533. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_set_instance) );
  534. }
  535. static mdns_txt_item_t * _convert_items(const char **values, int count)
  536. {
  537. int i=0,e;
  538. const char * value = NULL;
  539. mdns_txt_item_t * items = (mdns_txt_item_t*) malloc(sizeof(mdns_txt_item_t) * count);
  540. if (!items) {
  541. printf("ERROR: No Memory!\n");
  542. goto fail;
  543. }
  544. memset(items, 0, sizeof(mdns_txt_item_t) * count);
  545. for (i=0; i<count; i++) {
  546. value = values[i];
  547. char * esign = strchr(value, '=');
  548. if (!esign) {
  549. printf("ERROR: Equal sign not found in '%s'!\n", value);
  550. goto fail;
  551. }
  552. int var_len = esign - value;
  553. int val_len = strlen(value) - var_len - 1;
  554. char * var = (char*)malloc(var_len+1);
  555. if (var == NULL) {
  556. printf("ERROR: No Memory!\n");
  557. goto fail;
  558. }
  559. char * val = (char*)malloc(val_len+1);
  560. if (val == NULL) {
  561. printf("ERROR: No Memory!\n");
  562. free(var);
  563. goto fail;
  564. }
  565. memcpy(var, value, var_len);
  566. var[var_len] = 0;
  567. memcpy(val, esign+1, val_len);
  568. val[val_len] = 0;
  569. items[i].key = var;
  570. items[i].value = val;
  571. }
  572. return items;
  573. fail:
  574. for (e=0;e<i;e++) {
  575. free((char *)items[e].key);
  576. free((char *)items[e].value);
  577. }
  578. free(items);
  579. return NULL;
  580. }
  581. static struct {
  582. struct arg_str *service;
  583. struct arg_str *proto;
  584. struct arg_int *port;
  585. struct arg_str *instance;
  586. struct arg_str *txt;
  587. struct arg_end *end;
  588. } mdns_add_args;
  589. static int cmd_mdns_service_add(int argc, char** argv)
  590. {
  591. int nerrors = arg_parse(argc, argv, (void**) &mdns_add_args);
  592. if (nerrors != 0) {
  593. arg_print_errors(stderr, mdns_add_args.end, argv[0]);
  594. return 1;
  595. }
  596. if (!mdns_add_args.service->sval[0] || !mdns_add_args.proto->sval[0] || !mdns_add_args.port->ival[0]) {
  597. printf("ERROR: Bad arguments!\n");
  598. return 1;
  599. }
  600. const char * instance = NULL;
  601. if (mdns_add_args.instance->sval[0] && mdns_add_args.instance->sval[0][0]) {
  602. instance = mdns_add_args.instance->sval[0];
  603. printf("MDNS: Service Instance: %s\n", instance);
  604. }
  605. mdns_txt_item_t * items = NULL;
  606. if (mdns_add_args.txt->count) {
  607. items = _convert_items(mdns_add_args.txt->sval, mdns_add_args.txt->count);
  608. if (!items) {
  609. printf("ERROR: No Memory!\n");
  610. return 1;
  611. }
  612. }
  613. ESP_ERROR_CHECK( mdns_service_add(instance, mdns_add_args.service->sval[0], mdns_add_args.proto->sval[0], mdns_add_args.port->ival[0], items, mdns_add_args.txt->count) );
  614. free(items);
  615. return 0;
  616. }
  617. static void register_mdns_service_add(void)
  618. {
  619. mdns_add_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  620. mdns_add_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  621. mdns_add_args.port = arg_int1(NULL, NULL, "<port>", "Service Port");
  622. mdns_add_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
  623. mdns_add_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)");
  624. mdns_add_args.end = arg_end(2);
  625. const esp_console_cmd_t cmd_add = {
  626. .command = "mdns_service_add",
  627. .help = "Add service to MDNS",
  628. .hint = NULL,
  629. .func = &cmd_mdns_service_add,
  630. .argtable = &mdns_add_args
  631. };
  632. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_add) );
  633. }
  634. static struct {
  635. struct arg_str *service;
  636. struct arg_str *proto;
  637. struct arg_end *end;
  638. } mdns_remove_args;
  639. static int cmd_mdns_service_remove(int argc, char** argv)
  640. {
  641. int nerrors = arg_parse(argc, argv, (void**) &mdns_remove_args);
  642. if (nerrors != 0) {
  643. arg_print_errors(stderr, mdns_remove_args.end, argv[0]);
  644. return 1;
  645. }
  646. if (!mdns_remove_args.service->sval[0] || !mdns_remove_args.proto->sval[0]) {
  647. printf("ERROR: Bad arguments!\n");
  648. return 1;
  649. }
  650. ESP_ERROR_CHECK( mdns_service_remove(mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0]) );
  651. return 0;
  652. }
  653. static void register_mdns_service_remove(void)
  654. {
  655. mdns_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  656. mdns_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  657. mdns_remove_args.end = arg_end(2);
  658. const esp_console_cmd_t cmd_remove = {
  659. .command = "mdns_service_remove",
  660. .help = "Remove service from MDNS",
  661. .hint = NULL,
  662. .func = &cmd_mdns_service_remove,
  663. .argtable = &mdns_remove_args
  664. };
  665. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_remove) );
  666. }
  667. static struct {
  668. struct arg_str *service;
  669. struct arg_str *proto;
  670. struct arg_str *instance;
  671. struct arg_end *end;
  672. } mdns_service_instance_set_args;
  673. static int cmd_mdns_service_instance_set(int argc, char** argv)
  674. {
  675. int nerrors = arg_parse(argc, argv, (void**) &mdns_service_instance_set_args);
  676. if (nerrors != 0) {
  677. arg_print_errors(stderr, mdns_service_instance_set_args.end, argv[0]);
  678. return 1;
  679. }
  680. if (!mdns_service_instance_set_args.service->sval[0] || !mdns_service_instance_set_args.proto->sval[0] || !mdns_service_instance_set_args.instance->sval[0]) {
  681. printf("ERROR: Bad arguments!\n");
  682. return 1;
  683. }
  684. ESP_ERROR_CHECK( mdns_service_instance_name_set(mdns_service_instance_set_args.service->sval[0], mdns_service_instance_set_args.proto->sval[0], mdns_service_instance_set_args.instance->sval[0]) );
  685. return 0;
  686. }
  687. static void register_mdns_service_instance_set(void)
  688. {
  689. mdns_service_instance_set_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  690. mdns_service_instance_set_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  691. mdns_service_instance_set_args.instance = arg_str1(NULL, NULL, "<instance>", "Instance name");
  692. mdns_service_instance_set_args.end = arg_end(2);
  693. const esp_console_cmd_t cmd_add = {
  694. .command = "mdns_service_instance_set",
  695. .help = "Set MDNS Service Instance Name",
  696. .hint = NULL,
  697. .func = &cmd_mdns_service_instance_set,
  698. .argtable = &mdns_service_instance_set_args
  699. };
  700. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_add) );
  701. }
  702. static struct {
  703. struct arg_str *service;
  704. struct arg_str *proto;
  705. struct arg_int *port;
  706. struct arg_end *end;
  707. } mdns_service_port_set_args;
  708. static int cmd_mdns_service_port_set(int argc, char** argv) {
  709. int nerrors = arg_parse(argc, argv, (void**) &mdns_service_port_set_args);
  710. if (nerrors != 0) {
  711. arg_print_errors(stderr, mdns_service_port_set_args.end, argv[0]);
  712. return 1;
  713. }
  714. if (!mdns_service_port_set_args.service->sval[0] || !mdns_service_port_set_args.proto->sval[0] || !mdns_service_port_set_args.port->ival[0]) {
  715. printf("ERROR: Bad arguments!\n");
  716. return 1;
  717. }
  718. ESP_ERROR_CHECK( mdns_service_port_set(mdns_service_port_set_args.service->sval[0], mdns_service_port_set_args.proto->sval[0], mdns_service_port_set_args.port->ival[0]) );
  719. return 0;
  720. }
  721. static void register_mdns_service_port_set(void)
  722. {
  723. mdns_service_port_set_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  724. mdns_service_port_set_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  725. mdns_service_port_set_args.port = arg_int1(NULL, NULL, "<port>", "Service Port");
  726. mdns_service_port_set_args.end = arg_end(2);
  727. const esp_console_cmd_t cmd_add = {
  728. .command = "mdns_service_port_set",
  729. .help = "Set MDNS Service port",
  730. .hint = NULL,
  731. .func = &cmd_mdns_service_port_set,
  732. .argtable = &mdns_service_port_set_args
  733. };
  734. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_add) );
  735. }
  736. static struct {
  737. struct arg_str *service;
  738. struct arg_str *proto;
  739. struct arg_str *txt;
  740. struct arg_end *end;
  741. } mdns_txt_replace_args;
  742. static int cmd_mdns_service_txt_replace(int argc, char** argv)
  743. {
  744. mdns_txt_item_t * items = NULL;
  745. int nerrors = arg_parse(argc, argv, (void**) &mdns_txt_replace_args);
  746. if (nerrors != 0) {
  747. arg_print_errors(stderr, mdns_txt_replace_args.end, argv[0]);
  748. return 1;
  749. }
  750. if (!mdns_txt_replace_args.service->sval[0] || !mdns_txt_replace_args.proto->sval[0]) {
  751. printf("ERROR: Bad arguments!\n");
  752. return 1;
  753. }
  754. if (mdns_txt_replace_args.txt->count) {
  755. items = _convert_items(mdns_txt_replace_args.txt->sval, mdns_txt_replace_args.txt->count);
  756. if (!items) {
  757. printf("ERROR: No Memory!\n");
  758. return 1;
  759. }
  760. }
  761. ESP_ERROR_CHECK( mdns_service_txt_set(mdns_txt_replace_args.service->sval[0], mdns_txt_replace_args.proto->sval[0], items, mdns_txt_replace_args.txt->count) );
  762. free(items);
  763. return 0;
  764. }
  765. static void register_mdns_service_txt_replace(void)
  766. {
  767. mdns_txt_replace_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  768. mdns_txt_replace_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  769. mdns_txt_replace_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)");
  770. mdns_txt_replace_args.end = arg_end(2);
  771. const esp_console_cmd_t cmd_txt_set = {
  772. .command = "mdns_service_txt_replace",
  773. .help = "Replace MDNS service TXT items",
  774. .hint = NULL,
  775. .func = &cmd_mdns_service_txt_replace,
  776. .argtable = &mdns_txt_replace_args
  777. };
  778. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_txt_set) );
  779. }
  780. static struct {
  781. struct arg_str *service;
  782. struct arg_str *proto;
  783. struct arg_str *var;
  784. struct arg_str *value;
  785. struct arg_end *end;
  786. } mdns_txt_set_args;
  787. static int cmd_mdns_service_txt_set(int argc, char** argv)
  788. {
  789. int nerrors = arg_parse(argc, argv, (void**) &mdns_txt_set_args);
  790. if (nerrors != 0) {
  791. arg_print_errors(stderr, mdns_txt_set_args.end, argv[0]);
  792. return 1;
  793. }
  794. if (!mdns_txt_set_args.service->sval[0] || !mdns_txt_set_args.proto->sval[0] || !mdns_txt_set_args.var->sval[0]) {
  795. printf("ERROR: Bad arguments!\n");
  796. return 1;
  797. }
  798. ESP_ERROR_CHECK( mdns_service_txt_item_set(mdns_txt_set_args.service->sval[0], mdns_txt_set_args.proto->sval[0], mdns_txt_set_args.var->sval[0], mdns_txt_set_args.value->sval[0]) );
  799. return 0;
  800. }
  801. static void register_mdns_service_txt_set(void)
  802. {
  803. mdns_txt_set_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  804. mdns_txt_set_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  805. mdns_txt_set_args.var = arg_str1(NULL, NULL, "<var>", "Item Name");
  806. mdns_txt_set_args.value = arg_str1(NULL, NULL, "<value>", "Item Value");
  807. mdns_txt_set_args.end = arg_end(2);
  808. const esp_console_cmd_t cmd_txt_set = {
  809. .command = "mdns_service_txt_set",
  810. .help = "Add/Set MDNS service TXT item",
  811. .hint = NULL,
  812. .func = &cmd_mdns_service_txt_set,
  813. .argtable = &mdns_txt_set_args
  814. };
  815. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_txt_set) );
  816. }
  817. static struct {
  818. struct arg_str *service;
  819. struct arg_str *proto;
  820. struct arg_str *var;
  821. struct arg_end *end;
  822. } mdns_txt_remove_args;
  823. static int cmd_mdns_service_txt_remove(int argc, char** argv)
  824. {
  825. int nerrors = arg_parse(argc, argv, (void**) &mdns_txt_remove_args);
  826. if (nerrors != 0) {
  827. arg_print_errors(stderr, mdns_txt_remove_args.end, argv[0]);
  828. return 1;
  829. }
  830. if (!mdns_txt_remove_args.service->sval[0] || !mdns_txt_remove_args.proto->sval[0] || !mdns_txt_remove_args.var->sval[0]) {
  831. printf("ERROR: Bad arguments!\n");
  832. return 1;
  833. }
  834. ESP_ERROR_CHECK( mdns_service_txt_item_remove(mdns_txt_remove_args.service->sval[0], mdns_txt_remove_args.proto->sval[0], mdns_txt_remove_args.var->sval[0]) );
  835. return 0;
  836. }
  837. static void register_mdns_service_txt_remove(void)
  838. {
  839. mdns_txt_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
  840. mdns_txt_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
  841. mdns_txt_remove_args.var = arg_str1(NULL, NULL, "<var>", "Item Name");
  842. mdns_txt_remove_args.end = arg_end(2);
  843. const esp_console_cmd_t cmd_txt_remove = {
  844. .command = "mdns_service_txt_remove",
  845. .help = "Remove MDNS service TXT item",
  846. .hint = NULL,
  847. .func = &cmd_mdns_service_txt_remove,
  848. .argtable = &mdns_txt_remove_args
  849. };
  850. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_txt_remove) );
  851. }
  852. static int cmd_mdns_service_remove_all(int argc, char** argv)
  853. {
  854. mdns_service_remove_all();
  855. return 0;
  856. }
  857. static void register_mdns_service_remove_all(void)
  858. {
  859. const esp_console_cmd_t cmd_free = {
  860. .command = "mdns_service_remove_all",
  861. .help = "Remove all MDNS services",
  862. .hint = NULL,
  863. .func = &cmd_mdns_service_remove_all,
  864. .argtable = NULL
  865. };
  866. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_free) );
  867. }
  868. void mdns_console_register(void)
  869. {
  870. register_mdns_init();
  871. register_mdns_free();
  872. register_mdns_set_hostname();
  873. register_mdns_set_instance();
  874. register_mdns_service_add();
  875. register_mdns_service_remove();
  876. register_mdns_service_instance_set();
  877. register_mdns_service_port_set();
  878. register_mdns_service_txt_replace();
  879. register_mdns_service_txt_set();
  880. register_mdns_service_txt_remove();
  881. register_mdns_service_remove_all();
  882. register_mdns_query_a();
  883. register_mdns_query_aaaa();
  884. register_mdns_query_txt();
  885. register_mdns_query_srv();
  886. register_mdns_query_ptr();
  887. register_mdns_query_ip();
  888. register_mdns_query_svc();
  889. }