mdns_console.c 32 KB

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