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-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
58 typedef struct Config {
60 int terminate_on_all_for_now;
61 int terminate_on_cache_exhausted;
68 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
73 typedef struct ServiceInfo ServiceInfo;
76 AvahiIfIndex interface;
77 AvahiProtocol protocol;
78 char *name, *type, *domain;
80 AvahiServiceResolver *resolver;
83 AVAHI_LLIST_FIELDS(ServiceInfo, info);
86 static AvahiSimplePoll *simple_poll = NULL;
87 static AvahiClient *client = NULL;
88 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
89 static AvahiStringList *browsed_types = NULL;
90 static ServiceInfo *services = NULL;
91 static int n_columns = 80;
92 static int browsing = 0;
94 static void check_terminate(Config *c) {
96 assert(n_all_for_now >= 0);
97 assert(n_cache_exhausted >= 0);
98 assert(n_resolving >= 0);
100 if (n_all_for_now <= 0 && n_resolving <= 0) {
103 printf(": All for now\n");
104 n_all_for_now++; /* Make sure that this event is not repeated */
107 if (c->terminate_on_all_for_now)
108 avahi_simple_poll_quit(simple_poll);
111 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
114 printf(": Cache exhausted\n");
115 n_cache_exhausted++; /* Make sure that this event is not repeated */
118 if (c->terminate_on_cache_exhausted)
119 avahi_simple_poll_quit(simple_poll);
123 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
126 for (i = services; i; i = i->info_next)
127 if (i->interface == interface &&
128 i->protocol == protocol &&
129 strcasecmp(i->name, name) == 0 &&
130 avahi_domain_equal(i->type, type) &&
131 avahi_domain_equal(i->domain, domain))
138 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
139 char ifname[IF_NAMESIZE];
141 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
142 if (!config->no_db_lookup)
143 type = stdb_lookup(type);
146 printf("%c %4s %4s %-*s %-20s %s\n",
148 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
149 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
150 n_columns-35, name, type, domain);
154 static void service_resolver_callback(
155 AvahiServiceResolver *r,
156 AvahiIfIndex interface,
157 AvahiProtocol protocol,
158 AvahiResolverEvent event,
162 const char *host_name,
163 const AvahiAddress *a,
165 AvahiStringList *txt,
166 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
169 ServiceInfo *i = userdata;
175 case AVAHI_RESOLVER_FOUND: {
176 char address[AVAHI_ADDRESS_STR_MAX], *t;
178 avahi_address_snprint(address, sizeof(address), a);
180 t = avahi_string_list_to_string(txt);
182 print_service_line(i->config, '=', interface, protocol, name, type, domain);
184 printf(" hostname = [%s]\n"
197 case AVAHI_RESOLVER_FAILURE:
199 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
204 avahi_service_resolver_free(i->resolver);
207 assert(n_resolving > 0);
209 check_terminate(i->config);
213 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
216 i = avahi_new(ServiceInfo, 1);
219 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
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)));
229 i->interface = interface;
230 i->protocol = protocol;
231 i->name = avahi_strdup(name);
232 i->type = avahi_strdup(type);
233 i->domain = avahi_strdup(domain);
236 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
241 static void remove_service(Config *c, ServiceInfo *i) {
245 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
248 avahi_service_resolver_free(i->resolver);
252 avahi_free(i->domain);
256 static void service_browser_callback(
257 AvahiServiceBrowser *b,
258 AvahiIfIndex interface,
259 AvahiProtocol protocol,
260 AvahiBrowserEvent event,
264 AvahiLookupResultFlags flags,
267 Config *c = userdata;
273 case AVAHI_BROWSER_NEW: {
274 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
277 if (find_service(interface, protocol, name, type, domain))
280 add_service(c, interface, protocol, name, type, domain);
282 print_service_line(c, '+', interface, protocol, name, type, domain);
287 case AVAHI_BROWSER_REMOVE: {
290 if (!(info = find_service(interface, protocol, name, type, domain)))
293 remove_service(c, info);
295 print_service_line(c, '-', interface, protocol, name, type, domain);
299 case AVAHI_BROWSER_FAILURE:
300 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
301 avahi_simple_poll_quit(simple_poll);
304 case AVAHI_BROWSER_CACHE_EXHAUSTED:
305 n_cache_exhausted --;
309 case AVAHI_BROWSER_ALL_FOR_NOW:
316 static void browse_service_type(Config *c, const char *stype, const char *domain) {
317 AvahiServiceBrowser *b;
324 for (i = browsed_types; i; i = i->next)
325 if (avahi_domain_equal(stype, (char*) i->text))
328 if (!(b = avahi_service_browser_new(
335 service_browser_callback,
338 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
339 avahi_simple_poll_quit(simple_poll);
342 browsed_types = avahi_string_list_add(browsed_types, stype);
348 static void service_type_browser_callback(
349 AvahiServiceTypeBrowser *b,
350 AVAHI_GCC_UNUSED AvahiIfIndex interface,
351 AVAHI_GCC_UNUSED AvahiProtocol protocol,
352 AvahiBrowserEvent event,
355 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
358 Config *c = userdata;
365 case AVAHI_BROWSER_NEW:
366 browse_service_type(c, type, domain);
369 case AVAHI_BROWSER_REMOVE:
370 /* We're dirty and never remove the browser again */
373 case AVAHI_BROWSER_FAILURE:
374 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
375 avahi_simple_poll_quit(simple_poll);
378 case AVAHI_BROWSER_CACHE_EXHAUSTED:
379 n_cache_exhausted --;
383 case AVAHI_BROWSER_ALL_FOR_NOW:
390 static void browse_all(Config *c) {
391 AvahiServiceTypeBrowser *b;
395 if (!(b = avahi_service_type_browser_new(
401 service_type_browser_callback,
404 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
405 avahi_simple_poll_quit(simple_poll);
412 static void domain_browser_callback(
413 AvahiDomainBrowser *b,
414 AVAHI_GCC_UNUSED AvahiIfIndex interface,
415 AVAHI_GCC_UNUSED AvahiProtocol protocol,
416 AvahiBrowserEvent event,
418 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
421 Config *c = userdata;
428 case AVAHI_BROWSER_NEW:
429 case AVAHI_BROWSER_REMOVE: {
430 char ifname[IF_NAMESIZE];
432 printf("%c %4s %4s %s\n",
433 event == AVAHI_BROWSER_NEW ? '+' : '-',
434 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
435 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
440 case AVAHI_BROWSER_FAILURE:
441 fprintf(stderr, "domain_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
442 avahi_simple_poll_quit(simple_poll);
445 case AVAHI_BROWSER_CACHE_EXHAUSTED:
446 n_cache_exhausted --;
450 case AVAHI_BROWSER_ALL_FOR_NOW:
457 static void browse_domains(Config *c) {
458 AvahiDomainBrowser *b;
462 if (!(b = avahi_domain_browser_new(
467 AVAHI_DOMAIN_BROWSER_BROWSE,
469 domain_browser_callback,
472 fprintf(stderr, "avahi_domain_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
473 avahi_simple_poll_quit(simple_poll);
480 static int start(Config *config) {
484 if (config->verbose) {
485 const char *version, *hn;
487 if (!(version = avahi_client_get_version_string(client))) {
488 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
492 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
493 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
497 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
499 if (config->command == COMMAND_BROWSE_DOMAINS)
500 fprintf(stderr, "E Ifce Prot Domain\n");
502 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
505 if (config->command == COMMAND_BROWSE_SERVICES)
506 browse_service_type(config, config->stype, config->domain);
507 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
510 assert(config->command == COMMAND_BROWSE_DOMAINS);
511 browse_domains(config);
518 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
519 Config *config = userdata;
521 /* This function might be called when avahi_client_new() has not
526 case AVAHI_CLIENT_FAILURE:
528 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
531 /* We have been disconnected, so let reconnect */
533 fprintf(stderr, "Disconnected, reconnecting ...\n");
535 avahi_client_free(client);
538 avahi_string_list_free(browsed_types);
539 browsed_types = NULL;
542 remove_service(config, services);
546 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
547 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
548 avahi_simple_poll_quit(simple_poll);
552 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
553 avahi_simple_poll_quit(simple_poll);
558 case AVAHI_CLIENT_S_REGISTERING:
559 case AVAHI_CLIENT_S_RUNNING:
560 case AVAHI_CLIENT_S_COLLISION:
563 if (start(config) < 0)
564 avahi_simple_poll_quit(simple_poll);
568 case AVAHI_CLIENT_CONNECTING:
571 fprintf(stderr, "Waiting for daemon ...\n");
577 static void help(FILE *f, const char *argv0) {
578 if (strstr(argv0, "domain"))
579 fprintf(f, "%s [options] \n\n", argv0);
582 "%s [options] <service type>\n"
584 "%s [options] -D\n\n",
585 argv0, argv0, argv0);
588 " -h --help Show this help\n"
589 " -V --version Show version\n"
590 " -D --browse-domains Browse for browsing domains instead of services\n"
591 " -a --all Show all services, regardless of the type\n"
592 " -d --domain=DOMAIN The domain to browse in\n"
593 " -v --verbose Enable verbose mode\n"
594 " -t --terminate Terminate after dumping a more or less complete list\n"
595 " -c --cache Terminate after dumping all entries from the cache\n"
596 " -l --ignore-local Ignore local services\n"
597 " -r --resolve Resolve services found\n"
598 " -f --no-fail Don't fail if the daemon is not available\n"
599 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
600 " -k --no-db-lookup Don't lookup service types\n"
605 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
608 static const struct option long_options[] = {
609 { "help", no_argument, NULL, 'h' },
610 { "version", no_argument, NULL, 'V' },
611 { "browse-domains", no_argument, NULL, 'D' },
612 { "domain", required_argument, NULL, 'd' },
613 { "all", no_argument, NULL, 'a' },
614 { "verbose", no_argument, NULL, 'v' },
615 { "terminate", no_argument, NULL, 't' },
616 { "cache", no_argument, NULL, 'c' },
617 { "ignore-local", no_argument, NULL, 'l' },
618 { "resolve", no_argument, NULL, 'r' },
619 { "no-fail", no_argument, NULL, 'f' },
620 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
621 { "no-db-lookup", no_argument, NULL, 'k' },
628 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
630 c->terminate_on_cache_exhausted =
631 c->terminate_on_all_for_now =
635 c->domain = c->stype = NULL;
637 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
642 while ((o = getopt_long(argc, argv, "hVd:avtclrDf"
643 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
646 , long_options, NULL)) >= 0) {
650 c->command = COMMAND_HELP;
653 c->command = COMMAND_VERSION;
656 c->command = COMMAND_BROWSE_ALL_SERVICES;
659 c->command = COMMAND_BROWSE_DOMAINS;
662 avahi_free(c->domain);
663 c->domain = avahi_strdup(optarg);
669 c->terminate_on_all_for_now = 1;
672 c->terminate_on_cache_exhausted = 1;
683 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
689 fprintf(stderr, "Invalid command line argument: %c\n", o);
694 if (c->command == COMMAND_BROWSE_SERVICES) {
695 if (optind >= argc) {
696 fprintf(stderr, "Too few arguments\n");
700 c->stype = avahi_strdup(argv[optind]);
705 fprintf(stderr, "Too many arguments\n");
712 int main(int argc, char *argv[]) {
718 setlocale(LC_ALL, "");
720 if ((argv0 = strrchr(argv[0], '/')))
725 if ((ec = getenv("COLUMNS")))
726 n_columns = atoi(ec);
731 if (parse_command_line(&config, argv0, argc, argv) < 0)
734 switch (config.command) {
740 case COMMAND_VERSION:
741 printf("%s "PACKAGE_VERSION"\n", argv0);
745 case COMMAND_BROWSE_SERVICES:
746 case COMMAND_BROWSE_ALL_SERVICES:
747 case COMMAND_BROWSE_DOMAINS:
749 if (!(simple_poll = avahi_simple_poll_new())) {
750 fprintf(stderr, "Failed to create simple poll object.\n");
754 if (sigint_install(simple_poll) < 0)
757 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
758 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
762 avahi_simple_poll_loop(simple_poll);
771 remove_service(&config, services);
774 avahi_client_free(client);
779 avahi_simple_poll_free(simple_poll);
781 avahi_free(config.domain);
782 avahi_free(config.stype);
784 avahi_string_list_free(browsed_types);
786 #if defined(HAVE_GDBM) || defined(HAVE_DBM)