4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include <avahi-common/simple-watch.h>
37 #include <avahi-common/error.h>
38 #include <avahi-common/malloc.h>
39 #include <avahi-common/domain.h>
40 #include <avahi-common/llist.h>
41 #include <avahi-common/i18n.h>
42 #include <avahi-client/client.h>
43 #include <avahi-client/lookup.h>
47 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
54 COMMAND_BROWSE_SERVICES,
55 COMMAND_BROWSE_ALL_SERVICES,
56 COMMAND_BROWSE_DOMAINS
57 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
62 typedef struct Config {
64 int terminate_on_all_for_now;
65 int terminate_on_cache_exhausted;
73 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
78 typedef struct ServiceInfo ServiceInfo;
81 AvahiIfIndex interface;
82 AvahiProtocol protocol;
83 char *name, *type, *domain;
85 AvahiServiceResolver *resolver;
88 AVAHI_LLIST_FIELDS(ServiceInfo, info);
91 static AvahiSimplePoll *simple_poll = NULL;
92 static AvahiClient *client = NULL;
93 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
94 static AvahiStringList *browsed_types = NULL;
95 static ServiceInfo *services = NULL;
96 static int n_columns = 80;
97 static int browsing = 0;
99 static void check_terminate(Config *c) {
101 assert(n_all_for_now >= 0);
102 assert(n_cache_exhausted >= 0);
103 assert(n_resolving >= 0);
105 if (n_all_for_now <= 0 && n_resolving <= 0) {
107 if (c->verbose && !c->parsable) {
108 printf(_(": All for now\n"));
109 n_all_for_now++; /* Make sure that this event is not repeated */
112 if (c->terminate_on_all_for_now)
113 avahi_simple_poll_quit(simple_poll);
116 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
118 if (c->verbose && !c->parsable) {
119 printf(_(": Cache exhausted\n"));
120 n_cache_exhausted++; /* Make sure that this event is not repeated */
123 if (c->terminate_on_cache_exhausted)
124 avahi_simple_poll_quit(simple_poll);
128 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
131 for (i = services; i; i = i->info_next)
132 if (i->interface == interface &&
133 i->protocol == protocol &&
134 strcasecmp(i->name, name) == 0 &&
135 avahi_domain_equal(i->type, type) &&
136 avahi_domain_equal(i->domain, domain))
143 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) {
144 char ifname[IF_NAMESIZE];
146 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
147 if (!config->no_db_lookup)
148 type = stdb_lookup(type);
151 if (config->parsable) {
152 char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn;
153 size_t l = sizeof(sn);
155 printf("%c;%s;%s;%s;%s;%s%s",
157 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
158 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
159 avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : "");
162 printf("%c %4s %4s %-*s %-20s %s\n",
164 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
165 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
166 n_columns-35, name, type, domain);
170 static void service_resolver_callback(
171 AvahiServiceResolver *r,
172 AvahiIfIndex interface,
173 AvahiProtocol protocol,
174 AvahiResolverEvent event,
178 const char *host_name,
179 const AvahiAddress *a,
181 AvahiStringList *txt,
182 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
185 ServiceInfo *i = userdata;
191 case AVAHI_RESOLVER_FOUND: {
192 char address[AVAHI_ADDRESS_STR_MAX], *t;
194 avahi_address_snprint(address, sizeof(address), a);
196 t = avahi_string_list_to_string(txt);
198 print_service_line(i->config, '=', interface, protocol, name, type, domain, 0);
200 if (i->config->parsable)
201 printf(";%s;%s;%u;%s\n",
207 printf(" hostname = [%s]\n"
221 case AVAHI_RESOLVER_FAILURE:
223 fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
228 avahi_service_resolver_free(i->resolver);
231 assert(n_resolving > 0);
233 check_terminate(i->config);
237 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
240 i = avahi_new(ServiceInfo, 1);
243 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
245 fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
253 i->interface = interface;
254 i->protocol = protocol;
255 i->name = avahi_strdup(name);
256 i->type = avahi_strdup(type);
257 i->domain = avahi_strdup(domain);
260 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
265 static void remove_service(Config *c, ServiceInfo *i) {
269 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
272 avahi_service_resolver_free(i->resolver);
276 avahi_free(i->domain);
280 static void service_browser_callback(
281 AvahiServiceBrowser *b,
282 AvahiIfIndex interface,
283 AvahiProtocol protocol,
284 AvahiBrowserEvent event,
288 AvahiLookupResultFlags flags,
291 Config *c = userdata;
297 case AVAHI_BROWSER_NEW: {
298 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
301 if (find_service(interface, protocol, name, type, domain))
304 add_service(c, interface, protocol, name, type, domain);
306 print_service_line(c, '+', interface, protocol, name, type, domain, 1);
311 case AVAHI_BROWSER_REMOVE: {
314 if (!(info = find_service(interface, protocol, name, type, domain)))
317 remove_service(c, info);
319 print_service_line(c, '-', interface, protocol, name, type, domain, 1);
323 case AVAHI_BROWSER_FAILURE:
324 fprintf(stderr, _("service_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
325 avahi_simple_poll_quit(simple_poll);
328 case AVAHI_BROWSER_CACHE_EXHAUSTED:
329 n_cache_exhausted --;
333 case AVAHI_BROWSER_ALL_FOR_NOW:
340 static void browse_service_type(Config *c, const char *stype, const char *domain) {
341 AvahiServiceBrowser *b;
348 for (i = browsed_types; i; i = i->next)
349 if (avahi_domain_equal(stype, (char*) i->text))
352 if (!(b = avahi_service_browser_new(
359 service_browser_callback,
362 fprintf(stderr, _("avahi_service_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
363 avahi_simple_poll_quit(simple_poll);
366 browsed_types = avahi_string_list_add(browsed_types, stype);
372 static void service_type_browser_callback(
373 AvahiServiceTypeBrowser *b,
374 AVAHI_GCC_UNUSED AvahiIfIndex interface,
375 AVAHI_GCC_UNUSED AvahiProtocol protocol,
376 AvahiBrowserEvent event,
379 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
382 Config *c = userdata;
389 case AVAHI_BROWSER_NEW:
390 browse_service_type(c, type, domain);
393 case AVAHI_BROWSER_REMOVE:
394 /* We're dirty and never remove the browser again */
397 case AVAHI_BROWSER_FAILURE:
398 fprintf(stderr, _("service_type_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
399 avahi_simple_poll_quit(simple_poll);
402 case AVAHI_BROWSER_CACHE_EXHAUSTED:
403 n_cache_exhausted --;
407 case AVAHI_BROWSER_ALL_FOR_NOW:
414 static void browse_all(Config *c) {
415 AvahiServiceTypeBrowser *b;
419 if (!(b = avahi_service_type_browser_new(
425 service_type_browser_callback,
428 fprintf(stderr, _("avahi_service_type_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
429 avahi_simple_poll_quit(simple_poll);
436 static void domain_browser_callback(
437 AvahiDomainBrowser *b,
438 AVAHI_GCC_UNUSED AvahiIfIndex interface,
439 AVAHI_GCC_UNUSED AvahiProtocol protocol,
440 AvahiBrowserEvent event,
442 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
445 Config *c = userdata;
452 case AVAHI_BROWSER_NEW:
453 case AVAHI_BROWSER_REMOVE: {
454 char ifname[IF_NAMESIZE];
457 printf("%c;%s;%s;%s\n",
458 event == AVAHI_BROWSER_NEW ? '+' : '-',
459 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "",
460 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "",
463 printf("%c %4s %4s %s\n",
464 event == AVAHI_BROWSER_NEW ? '+' : '-',
465 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
466 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
471 case AVAHI_BROWSER_FAILURE:
472 fprintf(stderr, ("domain_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
473 avahi_simple_poll_quit(simple_poll);
476 case AVAHI_BROWSER_CACHE_EXHAUSTED:
477 n_cache_exhausted --;
481 case AVAHI_BROWSER_ALL_FOR_NOW:
488 static void browse_domains(Config *c) {
489 AvahiDomainBrowser *b;
493 if (!(b = avahi_domain_browser_new(
498 AVAHI_DOMAIN_BROWSER_BROWSE,
500 domain_browser_callback,
503 fprintf(stderr, _("avahi_domain_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
504 avahi_simple_poll_quit(simple_poll);
511 static int start(Config *config) {
515 if (config->verbose && !config->parsable) {
516 const char *version, *hn;
518 if (!(version = avahi_client_get_version_string(client))) {
519 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
523 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
524 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
528 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
530 if (config->command == COMMAND_BROWSE_DOMAINS) {
531 /* Translators: This is a column heading with abbreviations for
532 * Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
533 fprintf(stderr, _("E Ifce Prot Domain\n"));
535 /* Translators: This is a column heading with abbreviations for
536 * Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
537 fprintf(stderr, _("E Ifce Prot %-*s %-20s Domain\n"), n_columns-35, _("Name"), _("Type"));
541 if (config->command == COMMAND_BROWSE_SERVICES)
542 browse_service_type(config, config->stype, config->domain);
543 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
546 assert(config->command == COMMAND_BROWSE_DOMAINS);
547 browse_domains(config);
554 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
555 Config *config = userdata;
557 /* This function might be called when avahi_client_new() has not
562 case AVAHI_CLIENT_FAILURE:
564 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
567 /* We have been disconnected, so let reconnect */
569 fprintf(stderr, _("Disconnected, reconnecting ...\n"));
571 avahi_client_free(client);
574 avahi_string_list_free(browsed_types);
575 browsed_types = NULL;
578 remove_service(config, services);
582 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
583 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
584 avahi_simple_poll_quit(simple_poll);
588 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
589 avahi_simple_poll_quit(simple_poll);
594 case AVAHI_CLIENT_S_REGISTERING:
595 case AVAHI_CLIENT_S_RUNNING:
596 case AVAHI_CLIENT_S_COLLISION:
599 if (start(config) < 0)
600 avahi_simple_poll_quit(simple_poll);
604 case AVAHI_CLIENT_CONNECTING:
606 if (config->verbose && !config->parsable)
607 fprintf(stderr, _("Waiting for daemon ...\n"));
613 static void help(FILE *f, const char *argv0) {
614 if (strstr(argv0, "domain"))
615 fprintf(f, "%s [options] \n\n", argv0);
618 "%s [options] <service type>\n"
621 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
625 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
628 argv0, argv0, argv0);
631 _(" -h --help Show this help\n"
632 " -V --version Show version\n"
633 " -D --browse-domains Browse for browsing domains instead of services\n"
634 " -a --all Show all services, regardless of the type\n"
635 " -d --domain=DOMAIN The domain to browse in\n"
636 " -v --verbose Enable verbose mode\n"
637 " -t --terminate Terminate after dumping a more or less complete list\n"
638 " -c --cache Terminate after dumping all entries from the cache\n"
639 " -l --ignore-local Ignore local services\n"
640 " -r --resolve Resolve services found\n"
641 " -f --no-fail Don't fail if the daemon is not available\n"
642 " -p --parsable Output in parsable format\n"),
643 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
644 _(" -k --no-db-lookup Don't lookup service types\n"
645 " -b --dump-db Dump service type database\n")
652 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
655 static const struct option long_options[] = {
656 { "help", no_argument, NULL, 'h' },
657 { "version", no_argument, NULL, 'V' },
658 { "browse-domains", no_argument, NULL, 'D' },
659 { "domain", required_argument, NULL, 'd' },
660 { "all", no_argument, NULL, 'a' },
661 { "verbose", no_argument, NULL, 'v' },
662 { "terminate", no_argument, NULL, 't' },
663 { "cache", no_argument, NULL, 'c' },
664 { "ignore-local", no_argument, NULL, 'l' },
665 { "resolve", no_argument, NULL, 'r' },
666 { "no-fail", no_argument, NULL, 'f' },
667 { "parsable", no_argument, NULL, 'p' },
668 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
669 { "no-db-lookup", no_argument, NULL, 'k' },
670 { "dump-db", no_argument, NULL, 'b' },
677 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
679 c->terminate_on_cache_exhausted =
680 c->terminate_on_all_for_now =
685 c->domain = c->stype = NULL;
687 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
691 while ((o = getopt_long(argc, argv, "hVd:avtclrDfp"
692 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
695 , long_options, NULL)) >= 0) {
699 c->command = COMMAND_HELP;
702 c->command = COMMAND_VERSION;
705 c->command = COMMAND_BROWSE_ALL_SERVICES;
708 c->command = COMMAND_BROWSE_DOMAINS;
711 avahi_free(c->domain);
712 c->domain = avahi_strdup(optarg);
718 c->terminate_on_all_for_now = 1;
721 c->terminate_on_cache_exhausted = 1;
735 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
740 c->command = COMMAND_DUMP_STDB;
748 if (c->command == COMMAND_BROWSE_SERVICES) {
749 if (optind >= argc) {
750 fprintf(stderr, _("Too few arguments\n"));
754 c->stype = avahi_strdup(argv[optind]);
759 fprintf(stderr, _("Too many arguments\n"));
766 int main(int argc, char *argv[]) {
773 setlocale(LC_ALL, "");
775 if ((argv0 = strrchr(argv[0], '/')))
780 if ((ec = getenv("COLUMNS")))
781 n_columns = atoi(ec);
786 if (parse_command_line(&config, argv0, argc, argv) < 0)
789 switch (config.command) {
795 case COMMAND_VERSION:
796 printf("%s "PACKAGE_VERSION"\n", argv0);
800 case COMMAND_BROWSE_SERVICES:
801 case COMMAND_BROWSE_ALL_SERVICES:
802 case COMMAND_BROWSE_DOMAINS:
804 if (!(simple_poll = avahi_simple_poll_new())) {
805 fprintf(stderr, _("Failed to create simple poll object.\n"));
809 if (sigint_install(simple_poll) < 0)
812 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
813 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
817 avahi_simple_poll_loop(simple_poll);
821 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
822 case COMMAND_DUMP_STDB: {
826 while ((t = stdb_getent())) {
827 if (config.no_db_lookup)
830 printf("%s\n", stdb_lookup(t));
843 remove_service(&config, services);
846 avahi_client_free(client);
851 avahi_simple_poll_free(simple_poll);
853 avahi_free(config.domain);
854 avahi_free(config.stype);
856 avahi_string_list_free(browsed_types);
858 #if defined(HAVE_GDBM) || defined(HAVE_DBM)