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
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;
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) {
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) {
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) {
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 printf("%c %4s %4s %-*s %-20s %s\n",
151 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
152 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
153 n_columns-35, name, type, domain);
157 static void service_resolver_callback(
158 AvahiServiceResolver *r,
159 AvahiIfIndex interface,
160 AvahiProtocol protocol,
161 AvahiResolverEvent event,
165 const char *host_name,
166 const AvahiAddress *a,
168 AvahiStringList *txt,
169 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
172 ServiceInfo *i = userdata;
178 case AVAHI_RESOLVER_FOUND: {
179 char address[AVAHI_ADDRESS_STR_MAX], *t;
181 avahi_address_snprint(address, sizeof(address), a);
183 t = avahi_string_list_to_string(txt);
185 print_service_line(i->config, '=', interface, protocol, name, type, domain);
187 printf(" hostname = [%s]\n"
200 case AVAHI_RESOLVER_FAILURE:
202 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
207 avahi_service_resolver_free(i->resolver);
210 assert(n_resolving > 0);
212 check_terminate(i->config);
216 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
219 i = avahi_new(ServiceInfo, 1);
222 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
224 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
232 i->interface = interface;
233 i->protocol = protocol;
234 i->name = avahi_strdup(name);
235 i->type = avahi_strdup(type);
236 i->domain = avahi_strdup(domain);
239 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
244 static void remove_service(Config *c, ServiceInfo *i) {
248 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
251 avahi_service_resolver_free(i->resolver);
255 avahi_free(i->domain);
259 static void service_browser_callback(
260 AvahiServiceBrowser *b,
261 AvahiIfIndex interface,
262 AvahiProtocol protocol,
263 AvahiBrowserEvent event,
267 AvahiLookupResultFlags flags,
270 Config *c = userdata;
276 case AVAHI_BROWSER_NEW: {
277 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
280 if (find_service(interface, protocol, name, type, domain))
283 add_service(c, interface, protocol, name, type, domain);
285 print_service_line(c, '+', interface, protocol, name, type, domain);
290 case AVAHI_BROWSER_REMOVE: {
293 if (!(info = find_service(interface, protocol, name, type, domain)))
296 remove_service(c, info);
298 print_service_line(c, '-', interface, protocol, name, type, domain);
302 case AVAHI_BROWSER_FAILURE:
303 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
304 avahi_simple_poll_quit(simple_poll);
307 case AVAHI_BROWSER_CACHE_EXHAUSTED:
308 n_cache_exhausted --;
312 case AVAHI_BROWSER_ALL_FOR_NOW:
319 static void browse_service_type(Config *c, const char *stype, const char *domain) {
320 AvahiServiceBrowser *b;
327 for (i = browsed_types; i; i = i->next)
328 if (avahi_domain_equal(stype, (char*) i->text))
331 if (!(b = avahi_service_browser_new(
338 service_browser_callback,
341 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
342 avahi_simple_poll_quit(simple_poll);
345 browsed_types = avahi_string_list_add(browsed_types, stype);
351 static void service_type_browser_callback(
352 AvahiServiceTypeBrowser *b,
353 AVAHI_GCC_UNUSED AvahiIfIndex interface,
354 AVAHI_GCC_UNUSED AvahiProtocol protocol,
355 AvahiBrowserEvent event,
358 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
361 Config *c = userdata;
368 case AVAHI_BROWSER_NEW:
369 browse_service_type(c, type, domain);
372 case AVAHI_BROWSER_REMOVE:
373 /* We're dirty and never remove the browser again */
376 case AVAHI_BROWSER_FAILURE:
377 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
378 avahi_simple_poll_quit(simple_poll);
381 case AVAHI_BROWSER_CACHE_EXHAUSTED:
382 n_cache_exhausted --;
386 case AVAHI_BROWSER_ALL_FOR_NOW:
393 static void browse_all(Config *c) {
394 AvahiServiceTypeBrowser *b;
398 if (!(b = avahi_service_type_browser_new(
404 service_type_browser_callback,
407 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
408 avahi_simple_poll_quit(simple_poll);
415 static void domain_browser_callback(
416 AvahiDomainBrowser *b,
417 AVAHI_GCC_UNUSED AvahiIfIndex interface,
418 AVAHI_GCC_UNUSED AvahiProtocol protocol,
419 AvahiBrowserEvent event,
421 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
424 Config *c = userdata;
431 case AVAHI_BROWSER_NEW:
432 case AVAHI_BROWSER_REMOVE: {
433 char ifname[IF_NAMESIZE];
435 printf("%c %4s %4s %s\n",
436 event == AVAHI_BROWSER_NEW ? '+' : '-',
437 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
438 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
443 case AVAHI_BROWSER_FAILURE:
444 fprintf(stderr, "domain_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
445 avahi_simple_poll_quit(simple_poll);
448 case AVAHI_BROWSER_CACHE_EXHAUSTED:
449 n_cache_exhausted --;
453 case AVAHI_BROWSER_ALL_FOR_NOW:
460 static void browse_domains(Config *c) {
461 AvahiDomainBrowser *b;
465 if (!(b = avahi_domain_browser_new(
470 AVAHI_DOMAIN_BROWSER_BROWSE,
472 domain_browser_callback,
475 fprintf(stderr, "avahi_domain_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
476 avahi_simple_poll_quit(simple_poll);
483 static int start(Config *config) {
487 if (config->verbose) {
488 const char *version, *hn;
490 if (!(version = avahi_client_get_version_string(client))) {
491 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
495 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
496 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
500 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
502 if (config->command == COMMAND_BROWSE_DOMAINS)
503 fprintf(stderr, "E Ifce Prot Domain\n");
505 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
508 if (config->command == COMMAND_BROWSE_SERVICES)
509 browse_service_type(config, config->stype, config->domain);
510 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
513 assert(config->command == COMMAND_BROWSE_DOMAINS);
514 browse_domains(config);
521 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
522 Config *config = userdata;
524 /* This function might be called when avahi_client_new() has not
529 case AVAHI_CLIENT_FAILURE:
531 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
534 /* We have been disconnected, so let reconnect */
536 fprintf(stderr, "Disconnected, reconnecting ...\n");
538 avahi_client_free(client);
541 avahi_string_list_free(browsed_types);
542 browsed_types = NULL;
545 remove_service(config, services);
549 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
550 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
551 avahi_simple_poll_quit(simple_poll);
555 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
556 avahi_simple_poll_quit(simple_poll);
561 case AVAHI_CLIENT_S_REGISTERING:
562 case AVAHI_CLIENT_S_RUNNING:
563 case AVAHI_CLIENT_S_COLLISION:
566 if (start(config) < 0)
567 avahi_simple_poll_quit(simple_poll);
571 case AVAHI_CLIENT_CONNECTING:
574 fprintf(stderr, "Waiting for daemon ...\n");
580 static void help(FILE *f, const char *argv0) {
581 if (strstr(argv0, "domain"))
582 fprintf(f, "%s [options] \n\n", argv0);
585 "%s [options] <service type>\n"
588 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
592 argv0, argv0, argv0);
595 " -h --help Show this help\n"
596 " -V --version Show version\n"
597 " -D --browse-domains Browse for browsing domains instead of services\n"
598 " -a --all Show all services, regardless of the type\n"
599 " -d --domain=DOMAIN The domain to browse in\n"
600 " -v --verbose Enable verbose mode\n"
601 " -t --terminate Terminate after dumping a more or less complete list\n"
602 " -c --cache Terminate after dumping all entries from the cache\n"
603 " -l --ignore-local Ignore local services\n"
604 " -r --resolve Resolve services found\n"
605 " -f --no-fail Don't fail if the daemon is not available\n"
606 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
607 " -k --no-db-lookup Don't lookup service types\n"
608 " -b --dump-db Dump service type database\n"
613 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
616 static const struct option long_options[] = {
617 { "help", no_argument, NULL, 'h' },
618 { "version", no_argument, NULL, 'V' },
619 { "browse-domains", no_argument, NULL, 'D' },
620 { "domain", required_argument, NULL, 'd' },
621 { "all", no_argument, NULL, 'a' },
622 { "verbose", no_argument, NULL, 'v' },
623 { "terminate", no_argument, NULL, 't' },
624 { "cache", no_argument, NULL, 'c' },
625 { "ignore-local", no_argument, NULL, 'l' },
626 { "resolve", no_argument, NULL, 'r' },
627 { "no-fail", no_argument, NULL, 'f' },
628 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
629 { "no-db-lookup", no_argument, NULL, 'k' },
630 { "dump-db", no_argument, NULL, 'b' },
637 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
639 c->terminate_on_cache_exhausted =
640 c->terminate_on_all_for_now =
644 c->domain = c->stype = NULL;
646 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
651 while ((o = getopt_long(argc, argv, "hVd:avtclrDf"
652 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
655 , long_options, NULL)) >= 0) {
659 c->command = COMMAND_HELP;
662 c->command = COMMAND_VERSION;
665 c->command = COMMAND_BROWSE_ALL_SERVICES;
668 c->command = COMMAND_BROWSE_DOMAINS;
671 avahi_free(c->domain);
672 c->domain = avahi_strdup(optarg);
678 c->terminate_on_all_for_now = 1;
681 c->terminate_on_cache_exhausted = 1;
692 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
697 c->command = COMMAND_DUMP_STDB;
701 fprintf(stderr, "Invalid command line argument: %s\n", argv[optind-1]);
706 if (c->command == COMMAND_BROWSE_SERVICES) {
707 if (optind >= argc) {
708 fprintf(stderr, "Too few arguments\n");
712 c->stype = avahi_strdup(argv[optind]);
717 fprintf(stderr, "Too many arguments\n");
724 int main(int argc, char *argv[]) {
730 setlocale(LC_ALL, "");
732 if ((argv0 = strrchr(argv[0], '/')))
737 if ((ec = getenv("COLUMNS")))
738 n_columns = atoi(ec);
743 if (parse_command_line(&config, argv0, argc, argv) < 0)
746 switch (config.command) {
752 case COMMAND_VERSION:
753 printf("%s "PACKAGE_VERSION"\n", argv0);
757 case COMMAND_BROWSE_SERVICES:
758 case COMMAND_BROWSE_ALL_SERVICES:
759 case COMMAND_BROWSE_DOMAINS:
761 if (!(simple_poll = avahi_simple_poll_new())) {
762 fprintf(stderr, "Failed to create simple poll object.\n");
766 if (sigint_install(simple_poll) < 0)
769 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
770 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
774 avahi_simple_poll_loop(simple_poll);
778 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
779 case COMMAND_DUMP_STDB: {
783 while ((t = stdb_getent())) {
784 if (config.no_db_lookup)
787 printf("%s\n", stdb_lookup(t));
800 remove_service(&config, services);
803 avahi_client_free(client);
808 avahi_simple_poll_free(simple_poll);
810 avahi_free(config.domain);
811 avahi_free(config.stype);
813 avahi_string_list_free(browsed_types);
815 #if defined(HAVE_GDBM) || defined(HAVE_DBM)