2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
30 #include <sys/socket.h>
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/domain.h>
39 #include <avahi-common/llist.h>
40 #include <avahi-common/i18n.h>
41 #include <avahi-client/client.h>
42 #include <avahi-client/lookup.h>
46 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
53 COMMAND_BROWSE_SERVICES,
54 COMMAND_BROWSE_ALL_SERVICES,
55 COMMAND_BROWSE_DOMAINS
56 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
61 typedef struct Config {
63 int terminate_on_all_for_now;
64 int terminate_on_cache_exhausted;
72 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
77 typedef struct ServiceInfo ServiceInfo;
80 AvahiIfIndex interface;
81 AvahiProtocol protocol;
82 char *name, *type, *domain;
84 AvahiServiceResolver *resolver;
87 AVAHI_LLIST_FIELDS(ServiceInfo, info);
90 static AvahiSimplePoll *simple_poll = NULL;
91 static AvahiClient *client = NULL;
92 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
93 static AvahiStringList *browsed_types = NULL;
94 static ServiceInfo *services = NULL;
95 static int n_columns = 80;
96 static int browsing = 0;
98 static void check_terminate(Config *c) {
100 assert(n_all_for_now >= 0);
101 assert(n_cache_exhausted >= 0);
102 assert(n_resolving >= 0);
104 if (n_all_for_now <= 0 && n_resolving <= 0) {
106 if (c->verbose && !c->parsable) {
107 printf(_(": All for now\n"));
108 n_all_for_now++; /* Make sure that this event is not repeated */
111 if (c->terminate_on_all_for_now)
112 avahi_simple_poll_quit(simple_poll);
115 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
117 if (c->verbose && !c->parsable) {
118 printf(_(": Cache exhausted\n"));
119 n_cache_exhausted++; /* Make sure that this event is not repeated */
122 if (c->terminate_on_cache_exhausted)
123 avahi_simple_poll_quit(simple_poll);
127 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
130 for (i = services; i; i = i->info_next)
131 if (i->interface == interface &&
132 i->protocol == protocol &&
133 strcasecmp(i->name, name) == 0 &&
134 avahi_domain_equal(i->type, type) &&
135 avahi_domain_equal(i->domain, domain))
142 static char *make_printable(const char *from, char *to) {
146 for (f = from, t = to; *f; f++, t++)
147 *t = isprint(*f) ? *f : '_';
154 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) {
155 char ifname[IF_NAMESIZE];
157 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
158 if (!config->no_db_lookup)
159 type = stdb_lookup(type);
162 if (config->parsable) {
163 char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn;
164 size_t l = sizeof(sn);
166 printf("%c;%s;%s;%s;%s;%s%s",
168 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
169 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
170 avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : "");
173 char label[AVAHI_LABEL_MAX];
174 make_printable(name, label);
176 printf("%c %6s %4s %-*s %-20s %s\n",
178 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
179 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
180 n_columns-35, label, type, domain);
186 static void service_resolver_callback(
187 AvahiServiceResolver *r,
188 AvahiIfIndex interface,
189 AvahiProtocol protocol,
190 AvahiResolverEvent event,
194 const char *host_name,
195 const AvahiAddress *a,
197 AvahiStringList *txt,
198 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
201 ServiceInfo *i = userdata;
207 case AVAHI_RESOLVER_FOUND: {
208 char address[AVAHI_ADDRESS_STR_MAX], *t;
210 avahi_address_snprint(address, sizeof(address), a);
212 t = avahi_string_list_to_string(txt);
214 print_service_line(i->config, '=', interface, protocol, name, type, domain, 0);
216 if (i->config->parsable)
217 printf(";%s;%s;%u;%s\n",
223 printf(" hostname = [%s]\n"
237 case AVAHI_RESOLVER_FAILURE:
239 fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
244 avahi_service_resolver_free(i->resolver);
247 assert(n_resolving > 0);
249 check_terminate(i->config);
253 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
256 i = avahi_new(ServiceInfo, 1);
259 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
261 fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
269 i->interface = interface;
270 i->protocol = protocol;
271 i->name = avahi_strdup(name);
272 i->type = avahi_strdup(type);
273 i->domain = avahi_strdup(domain);
276 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
281 static void remove_service(Config *c, ServiceInfo *i) {
285 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
288 avahi_service_resolver_free(i->resolver);
292 avahi_free(i->domain);
296 static void service_browser_callback(
297 AvahiServiceBrowser *b,
298 AvahiIfIndex interface,
299 AvahiProtocol protocol,
300 AvahiBrowserEvent event,
304 AvahiLookupResultFlags flags,
307 Config *c = userdata;
313 case AVAHI_BROWSER_NEW: {
314 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
317 if (find_service(interface, protocol, name, type, domain))
320 add_service(c, interface, protocol, name, type, domain);
322 print_service_line(c, '+', interface, protocol, name, type, domain, 1);
327 case AVAHI_BROWSER_REMOVE: {
330 if (!(info = find_service(interface, protocol, name, type, domain)))
333 remove_service(c, info);
335 print_service_line(c, '-', interface, protocol, name, type, domain, 1);
339 case AVAHI_BROWSER_FAILURE:
340 fprintf(stderr, _("service_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
341 avahi_simple_poll_quit(simple_poll);
344 case AVAHI_BROWSER_CACHE_EXHAUSTED:
345 n_cache_exhausted --;
349 case AVAHI_BROWSER_ALL_FOR_NOW:
356 static void browse_service_type(Config *c, const char *stype, const char *domain) {
357 AvahiServiceBrowser *b;
364 for (i = browsed_types; i; i = i->next)
365 if (avahi_domain_equal(stype, (char*) i->text))
368 if (!(b = avahi_service_browser_new(
375 service_browser_callback,
378 fprintf(stderr, _("avahi_service_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
379 avahi_simple_poll_quit(simple_poll);
382 browsed_types = avahi_string_list_add(browsed_types, stype);
388 static void service_type_browser_callback(
389 AvahiServiceTypeBrowser *b,
390 AVAHI_GCC_UNUSED AvahiIfIndex interface,
391 AVAHI_GCC_UNUSED AvahiProtocol protocol,
392 AvahiBrowserEvent event,
395 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
398 Config *c = userdata;
405 case AVAHI_BROWSER_NEW:
406 browse_service_type(c, type, domain);
409 case AVAHI_BROWSER_REMOVE:
410 /* We're dirty and never remove the browser again */
413 case AVAHI_BROWSER_FAILURE:
414 fprintf(stderr, _("service_type_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
415 avahi_simple_poll_quit(simple_poll);
418 case AVAHI_BROWSER_CACHE_EXHAUSTED:
419 n_cache_exhausted --;
423 case AVAHI_BROWSER_ALL_FOR_NOW:
430 static void browse_all(Config *c) {
431 AvahiServiceTypeBrowser *b;
435 if (!(b = avahi_service_type_browser_new(
441 service_type_browser_callback,
444 fprintf(stderr, _("avahi_service_type_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
445 avahi_simple_poll_quit(simple_poll);
452 static void domain_browser_callback(
453 AvahiDomainBrowser *b,
454 AVAHI_GCC_UNUSED AvahiIfIndex interface,
455 AVAHI_GCC_UNUSED AvahiProtocol protocol,
456 AvahiBrowserEvent event,
458 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
461 Config *c = userdata;
468 case AVAHI_BROWSER_NEW:
469 case AVAHI_BROWSER_REMOVE: {
470 char ifname[IF_NAMESIZE];
473 printf("%c;%s;%s;%s\n",
474 event == AVAHI_BROWSER_NEW ? '+' : '-',
475 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "",
476 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "",
479 printf("%c %4s %4s %s\n",
480 event == AVAHI_BROWSER_NEW ? '+' : '-',
481 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
482 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
487 case AVAHI_BROWSER_FAILURE:
488 fprintf(stderr, ("domain_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
489 avahi_simple_poll_quit(simple_poll);
492 case AVAHI_BROWSER_CACHE_EXHAUSTED:
493 n_cache_exhausted --;
497 case AVAHI_BROWSER_ALL_FOR_NOW:
504 static void browse_domains(Config *c) {
505 AvahiDomainBrowser *b;
509 if (!(b = avahi_domain_browser_new(
514 AVAHI_DOMAIN_BROWSER_BROWSE,
516 domain_browser_callback,
519 fprintf(stderr, _("avahi_domain_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
520 avahi_simple_poll_quit(simple_poll);
527 static int start(Config *config) {
531 if (config->verbose && !config->parsable) {
532 const char *version, *hn;
534 if (!(version = avahi_client_get_version_string(client))) {
535 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
539 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
540 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
544 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
546 if (config->command == COMMAND_BROWSE_DOMAINS) {
547 /* Translators: This is a column heading with abbreviations for
548 * Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
549 fprintf(stderr, _("E Ifce Prot Domain\n"));
551 /* Translators: This is a column heading with abbreviations for
552 * Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
553 fprintf(stderr, _("E Ifce Prot %-*s %-20s Domain\n"), n_columns-35, _("Name"), _("Type"));
557 if (config->command == COMMAND_BROWSE_SERVICES)
558 browse_service_type(config, config->stype, config->domain);
559 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
562 assert(config->command == COMMAND_BROWSE_DOMAINS);
563 browse_domains(config);
570 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
571 Config *config = userdata;
573 /* This function might be called when avahi_client_new() has not
578 case AVAHI_CLIENT_FAILURE:
580 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
583 /* We have been disconnected, so let reconnect */
585 fprintf(stderr, _("Disconnected, reconnecting ...\n"));
587 avahi_client_free(client);
590 avahi_string_list_free(browsed_types);
591 browsed_types = NULL;
594 remove_service(config, services);
598 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
599 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
600 avahi_simple_poll_quit(simple_poll);
604 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
605 avahi_simple_poll_quit(simple_poll);
610 case AVAHI_CLIENT_S_REGISTERING:
611 case AVAHI_CLIENT_S_RUNNING:
612 case AVAHI_CLIENT_S_COLLISION:
615 if (start(config) < 0)
616 avahi_simple_poll_quit(simple_poll);
620 case AVAHI_CLIENT_CONNECTING:
622 if (config->verbose && !config->parsable)
623 fprintf(stderr, _("Waiting for daemon ...\n"));
629 static void help(FILE *f, const char *argv0) {
630 if (strstr(argv0, "domain"))
631 fprintf(f, "%s [options] \n\n", argv0);
634 "%s [options] <service type>\n"
637 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
641 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
644 argv0, argv0, argv0);
647 _(" -h --help Show this help\n"
648 " -V --version Show version\n"
649 " -D --browse-domains Browse for browsing domains instead of services\n"
650 " -a --all Show all services, regardless of the type\n"
651 " -d --domain=DOMAIN The domain to browse in\n"
652 " -v --verbose Enable verbose mode\n"
653 " -t --terminate Terminate after dumping a more or less complete list\n"
654 " -c --cache Terminate after dumping all entries from the cache\n"
655 " -l --ignore-local Ignore local services\n"
656 " -r --resolve Resolve services found\n"
657 " -f --no-fail Don't fail if the daemon is not available\n"
658 " -p --parsable Output in parsable format\n"),
659 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
660 _(" -k --no-db-lookup Don't lookup service types\n"
661 " -b --dump-db Dump service type database\n")
668 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
671 static const struct option long_options[] = {
672 { "help", no_argument, NULL, 'h' },
673 { "version", no_argument, NULL, 'V' },
674 { "browse-domains", no_argument, NULL, 'D' },
675 { "domain", required_argument, NULL, 'd' },
676 { "all", no_argument, NULL, 'a' },
677 { "verbose", no_argument, NULL, 'v' },
678 { "terminate", no_argument, NULL, 't' },
679 { "cache", no_argument, NULL, 'c' },
680 { "ignore-local", no_argument, NULL, 'l' },
681 { "resolve", no_argument, NULL, 'r' },
682 { "no-fail", no_argument, NULL, 'f' },
683 { "parsable", no_argument, NULL, 'p' },
684 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
685 { "no-db-lookup", no_argument, NULL, 'k' },
686 { "dump-db", no_argument, NULL, 'b' },
693 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
695 c->terminate_on_cache_exhausted =
696 c->terminate_on_all_for_now =
701 c->domain = c->stype = NULL;
703 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
707 while ((o = getopt_long(argc, argv, "hVd:avtclrDfp"
708 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
711 , long_options, NULL)) >= 0) {
715 c->command = COMMAND_HELP;
718 c->command = COMMAND_VERSION;
721 c->command = COMMAND_BROWSE_ALL_SERVICES;
724 c->command = COMMAND_BROWSE_DOMAINS;
727 avahi_free(c->domain);
728 c->domain = avahi_strdup(optarg);
734 c->terminate_on_all_for_now = 1;
737 c->terminate_on_cache_exhausted = 1;
751 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
756 c->command = COMMAND_DUMP_STDB;
764 if (c->command == COMMAND_BROWSE_SERVICES) {
765 if (optind >= argc) {
766 fprintf(stderr, _("Too few arguments\n"));
770 c->stype = avahi_strdup(argv[optind]);
775 fprintf(stderr, _("Too many arguments\n"));
782 int main(int argc, char *argv[]) {
789 setlocale(LC_ALL, "");
791 if ((argv0 = strrchr(argv[0], '/')))
796 if ((ec = getenv("COLUMNS")))
797 n_columns = atoi(ec);
802 if (parse_command_line(&config, argv0, argc, argv) < 0)
805 switch (config.command) {
811 case COMMAND_VERSION:
812 printf("%s "PACKAGE_VERSION"\n", argv0);
816 case COMMAND_BROWSE_SERVICES:
817 case COMMAND_BROWSE_ALL_SERVICES:
818 case COMMAND_BROWSE_DOMAINS:
820 if (!(simple_poll = avahi_simple_poll_new())) {
821 fprintf(stderr, _("Failed to create simple poll object.\n"));
825 if (sigint_install(simple_poll) < 0)
828 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
829 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
833 avahi_simple_poll_loop(simple_poll);
837 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
838 case COMMAND_DUMP_STDB: {
842 while ((t = stdb_getent())) {
843 if (config.no_db_lookup)
846 printf("%s\n", stdb_lookup(t));
859 remove_service(&config, services);
862 avahi_client_free(client);
867 avahi_simple_poll_free(simple_poll);
869 avahi_free(config.domain);
870 avahi_free(config.stype);
872 avahi_string_list_free(browsed_types);
874 #if defined(HAVE_GDBM) || defined(HAVE_DBM)