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>
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/domain.h>
38 #include <avahi-common/llist.h>
39 #include <avahi-common/i18n.h>
40 #include <avahi-client/client.h>
41 #include <avahi-client/lookup.h>
45 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
52 COMMAND_BROWSE_SERVICES,
53 COMMAND_BROWSE_ALL_SERVICES,
54 COMMAND_BROWSE_DOMAINS
55 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
60 typedef struct Config {
62 int terminate_on_all_for_now;
63 int terminate_on_cache_exhausted;
71 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
76 typedef struct ServiceInfo ServiceInfo;
79 AvahiIfIndex interface;
80 AvahiProtocol protocol;
81 char *name, *type, *domain;
83 AvahiServiceResolver *resolver;
86 AVAHI_LLIST_FIELDS(ServiceInfo, info);
89 static AvahiSimplePoll *simple_poll = NULL;
90 static AvahiClient *client = NULL;
91 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
92 static AvahiStringList *browsed_types = NULL;
93 static ServiceInfo *services = NULL;
94 static int n_columns = 80;
95 static int browsing = 0;
97 static void check_terminate(Config *c) {
99 assert(n_all_for_now >= 0);
100 assert(n_cache_exhausted >= 0);
101 assert(n_resolving >= 0);
103 if (n_all_for_now <= 0 && n_resolving <= 0) {
105 if (c->verbose && !c->parsable) {
106 printf(_(": All for now\n"));
107 n_all_for_now++; /* Make sure that this event is not repeated */
110 if (c->terminate_on_all_for_now)
111 avahi_simple_poll_quit(simple_poll);
114 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
116 if (c->verbose && !c->parsable) {
117 printf(_(": Cache exhausted\n"));
118 n_cache_exhausted++; /* Make sure that this event is not repeated */
121 if (c->terminate_on_cache_exhausted)
122 avahi_simple_poll_quit(simple_poll);
126 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
129 for (i = services; i; i = i->info_next)
130 if (i->interface == interface &&
131 i->protocol == protocol &&
132 strcasecmp(i->name, name) == 0 &&
133 avahi_domain_equal(i->type, type) &&
134 avahi_domain_equal(i->domain, domain))
141 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) {
142 char ifname[IF_NAMESIZE];
144 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
145 if (!config->no_db_lookup)
146 type = stdb_lookup(type);
149 if (config->parsable) {
150 char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn;
151 size_t l = sizeof(sn);
153 printf("%c;%s;%s;%s;%s;%s%s",
155 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
156 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
157 avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : "");
160 printf("%c %4s %4s %-*s %-20s %s\n",
162 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
163 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
164 n_columns-35, name, type, domain);
168 static void service_resolver_callback(
169 AvahiServiceResolver *r,
170 AvahiIfIndex interface,
171 AvahiProtocol protocol,
172 AvahiResolverEvent event,
176 const char *host_name,
177 const AvahiAddress *a,
179 AvahiStringList *txt,
180 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
183 ServiceInfo *i = userdata;
189 case AVAHI_RESOLVER_FOUND: {
190 char address[AVAHI_ADDRESS_STR_MAX], *t;
192 avahi_address_snprint(address, sizeof(address), a);
194 t = avahi_string_list_to_string(txt);
196 print_service_line(i->config, '=', interface, protocol, name, type, domain, 0);
198 if (i->config->parsable)
199 printf(";%s;%s;%u;%s\n",
205 printf(" hostname = [%s]\n"
219 case AVAHI_RESOLVER_FAILURE:
221 fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
226 avahi_service_resolver_free(i->resolver);
229 assert(n_resolving > 0);
231 check_terminate(i->config);
235 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
238 i = avahi_new(ServiceInfo, 1);
241 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
243 fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
251 i->interface = interface;
252 i->protocol = protocol;
253 i->name = avahi_strdup(name);
254 i->type = avahi_strdup(type);
255 i->domain = avahi_strdup(domain);
258 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
263 static void remove_service(Config *c, ServiceInfo *i) {
267 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
270 avahi_service_resolver_free(i->resolver);
274 avahi_free(i->domain);
278 static void service_browser_callback(
279 AvahiServiceBrowser *b,
280 AvahiIfIndex interface,
281 AvahiProtocol protocol,
282 AvahiBrowserEvent event,
286 AvahiLookupResultFlags flags,
289 Config *c = userdata;
295 case AVAHI_BROWSER_NEW: {
296 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
299 if (find_service(interface, protocol, name, type, domain))
302 add_service(c, interface, protocol, name, type, domain);
304 print_service_line(c, '+', interface, protocol, name, type, domain, 1);
309 case AVAHI_BROWSER_REMOVE: {
312 if (!(info = find_service(interface, protocol, name, type, domain)))
315 remove_service(c, info);
317 print_service_line(c, '-', interface, protocol, name, type, domain, 1);
321 case AVAHI_BROWSER_FAILURE:
322 fprintf(stderr, _("service_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
323 avahi_simple_poll_quit(simple_poll);
326 case AVAHI_BROWSER_CACHE_EXHAUSTED:
327 n_cache_exhausted --;
331 case AVAHI_BROWSER_ALL_FOR_NOW:
338 static void browse_service_type(Config *c, const char *stype, const char *domain) {
339 AvahiServiceBrowser *b;
346 for (i = browsed_types; i; i = i->next)
347 if (avahi_domain_equal(stype, (char*) i->text))
350 if (!(b = avahi_service_browser_new(
357 service_browser_callback,
360 fprintf(stderr, _("avahi_service_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
361 avahi_simple_poll_quit(simple_poll);
364 browsed_types = avahi_string_list_add(browsed_types, stype);
370 static void service_type_browser_callback(
371 AvahiServiceTypeBrowser *b,
372 AVAHI_GCC_UNUSED AvahiIfIndex interface,
373 AVAHI_GCC_UNUSED AvahiProtocol protocol,
374 AvahiBrowserEvent event,
377 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
380 Config *c = userdata;
387 case AVAHI_BROWSER_NEW:
388 browse_service_type(c, type, domain);
391 case AVAHI_BROWSER_REMOVE:
392 /* We're dirty and never remove the browser again */
395 case AVAHI_BROWSER_FAILURE:
396 fprintf(stderr, _("service_type_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
397 avahi_simple_poll_quit(simple_poll);
400 case AVAHI_BROWSER_CACHE_EXHAUSTED:
401 n_cache_exhausted --;
405 case AVAHI_BROWSER_ALL_FOR_NOW:
412 static void browse_all(Config *c) {
413 AvahiServiceTypeBrowser *b;
417 if (!(b = avahi_service_type_browser_new(
423 service_type_browser_callback,
426 fprintf(stderr, _("avahi_service_type_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
427 avahi_simple_poll_quit(simple_poll);
434 static void domain_browser_callback(
435 AvahiDomainBrowser *b,
436 AVAHI_GCC_UNUSED AvahiIfIndex interface,
437 AVAHI_GCC_UNUSED AvahiProtocol protocol,
438 AvahiBrowserEvent event,
440 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
443 Config *c = userdata;
450 case AVAHI_BROWSER_NEW:
451 case AVAHI_BROWSER_REMOVE: {
452 char ifname[IF_NAMESIZE];
455 printf("%c;%s;%s;%s\n",
456 event == AVAHI_BROWSER_NEW ? '+' : '-',
457 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "",
458 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "",
461 printf("%c %4s %4s %s\n",
462 event == AVAHI_BROWSER_NEW ? '+' : '-',
463 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
464 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
469 case AVAHI_BROWSER_FAILURE:
470 fprintf(stderr, ("domain_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
471 avahi_simple_poll_quit(simple_poll);
474 case AVAHI_BROWSER_CACHE_EXHAUSTED:
475 n_cache_exhausted --;
479 case AVAHI_BROWSER_ALL_FOR_NOW:
486 static void browse_domains(Config *c) {
487 AvahiDomainBrowser *b;
491 if (!(b = avahi_domain_browser_new(
496 AVAHI_DOMAIN_BROWSER_BROWSE,
498 domain_browser_callback,
501 fprintf(stderr, _("avahi_domain_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
502 avahi_simple_poll_quit(simple_poll);
509 static int start(Config *config) {
513 if (config->verbose && !config->parsable) {
514 const char *version, *hn;
516 if (!(version = avahi_client_get_version_string(client))) {
517 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
521 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
522 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
526 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
528 if (config->command == COMMAND_BROWSE_DOMAINS) {
529 /* Translators: This is a column heading with abbreviations for
530 * Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
531 fprintf(stderr, _("E Ifce Prot Domain\n"));
533 /* Translators: This is a column heading with abbreviations for
534 * Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
535 fprintf(stderr, _("E Ifce Prot %-*s %-20s Domain\n"), n_columns-35, _("Name"), _("Type"));
539 if (config->command == COMMAND_BROWSE_SERVICES)
540 browse_service_type(config, config->stype, config->domain);
541 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
544 assert(config->command == COMMAND_BROWSE_DOMAINS);
545 browse_domains(config);
552 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
553 Config *config = userdata;
555 /* This function might be called when avahi_client_new() has not
560 case AVAHI_CLIENT_FAILURE:
562 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
565 /* We have been disconnected, so let reconnect */
567 fprintf(stderr, _("Disconnected, reconnecting ...\n"));
569 avahi_client_free(client);
572 avahi_string_list_free(browsed_types);
573 browsed_types = NULL;
576 remove_service(config, services);
580 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
581 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
582 avahi_simple_poll_quit(simple_poll);
586 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
587 avahi_simple_poll_quit(simple_poll);
592 case AVAHI_CLIENT_S_REGISTERING:
593 case AVAHI_CLIENT_S_RUNNING:
594 case AVAHI_CLIENT_S_COLLISION:
597 if (start(config) < 0)
598 avahi_simple_poll_quit(simple_poll);
602 case AVAHI_CLIENT_CONNECTING:
604 if (config->verbose && !config->parsable)
605 fprintf(stderr, _("Waiting for daemon ...\n"));
611 static void help(FILE *f, const char *argv0) {
612 if (strstr(argv0, "domain"))
613 fprintf(f, "%s [options] \n\n", argv0);
616 "%s [options] <service type>\n"
619 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
623 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
626 argv0, argv0, argv0);
629 _(" -h --help Show this help\n"
630 " -V --version Show version\n"
631 " -D --browse-domains Browse for browsing domains instead of services\n"
632 " -a --all Show all services, regardless of the type\n"
633 " -d --domain=DOMAIN The domain to browse in\n"
634 " -v --verbose Enable verbose mode\n"
635 " -t --terminate Terminate after dumping a more or less complete list\n"
636 " -c --cache Terminate after dumping all entries from the cache\n"
637 " -l --ignore-local Ignore local services\n"
638 " -r --resolve Resolve services found\n"
639 " -f --no-fail Don't fail if the daemon is not available\n"
640 " -p --parsable Output in parsable format\n"),
641 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
642 _(" -k --no-db-lookup Don't lookup service types\n"
643 " -b --dump-db Dump service type database\n")
650 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
653 static const struct option long_options[] = {
654 { "help", no_argument, NULL, 'h' },
655 { "version", no_argument, NULL, 'V' },
656 { "browse-domains", no_argument, NULL, 'D' },
657 { "domain", required_argument, NULL, 'd' },
658 { "all", no_argument, NULL, 'a' },
659 { "verbose", no_argument, NULL, 'v' },
660 { "terminate", no_argument, NULL, 't' },
661 { "cache", no_argument, NULL, 'c' },
662 { "ignore-local", no_argument, NULL, 'l' },
663 { "resolve", no_argument, NULL, 'r' },
664 { "no-fail", no_argument, NULL, 'f' },
665 { "parsable", no_argument, NULL, 'p' },
666 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
667 { "no-db-lookup", no_argument, NULL, 'k' },
668 { "dump-db", no_argument, NULL, 'b' },
675 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
677 c->terminate_on_cache_exhausted =
678 c->terminate_on_all_for_now =
683 c->domain = c->stype = NULL;
685 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
689 while ((o = getopt_long(argc, argv, "hVd:avtclrDfp"
690 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
693 , long_options, NULL)) >= 0) {
697 c->command = COMMAND_HELP;
700 c->command = COMMAND_VERSION;
703 c->command = COMMAND_BROWSE_ALL_SERVICES;
706 c->command = COMMAND_BROWSE_DOMAINS;
709 avahi_free(c->domain);
710 c->domain = avahi_strdup(optarg);
716 c->terminate_on_all_for_now = 1;
719 c->terminate_on_cache_exhausted = 1;
733 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
738 c->command = COMMAND_DUMP_STDB;
746 if (c->command == COMMAND_BROWSE_SERVICES) {
747 if (optind >= argc) {
748 fprintf(stderr, _("Too few arguments\n"));
752 c->stype = avahi_strdup(argv[optind]);
757 fprintf(stderr, _("Too many arguments\n"));
764 int main(int argc, char *argv[]) {
771 setlocale(LC_ALL, "");
773 if ((argv0 = strrchr(argv[0], '/')))
778 if ((ec = getenv("COLUMNS")))
779 n_columns = atoi(ec);
784 if (parse_command_line(&config, argv0, argc, argv) < 0)
787 switch (config.command) {
793 case COMMAND_VERSION:
794 printf("%s "PACKAGE_VERSION"\n", argv0);
798 case COMMAND_BROWSE_SERVICES:
799 case COMMAND_BROWSE_ALL_SERVICES:
800 case COMMAND_BROWSE_DOMAINS:
802 if (!(simple_poll = avahi_simple_poll_new())) {
803 fprintf(stderr, _("Failed to create simple poll object.\n"));
807 if (sigint_install(simple_poll) < 0)
810 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
811 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
815 avahi_simple_poll_loop(simple_poll);
819 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
820 case COMMAND_DUMP_STDB: {
824 while ((t = stdb_getent())) {
825 if (config.no_db_lookup)
828 printf("%s\n", stdb_lookup(t));
841 remove_service(&config, services);
844 avahi_client_free(client);
849 avahi_simple_poll_free(simple_poll);
851 avahi_free(config.domain);
852 avahi_free(config.stype);
854 avahi_string_list_free(browsed_types);
856 #if defined(HAVE_GDBM) || defined(HAVE_DBM)