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;
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 void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) {
143 char ifname[IF_NAMESIZE];
145 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
146 if (!config->no_db_lookup)
147 type = stdb_lookup(type);
150 if (config->parsable) {
151 char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn;
152 size_t l = sizeof(sn);
154 printf("%c;%s;%s;%s;%s;%s%s",
156 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
157 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
158 avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : "");
161 printf("%c %4s %4s %-*s %-20s %s\n",
163 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
164 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
165 n_columns-35, name, type, domain);
169 static void service_resolver_callback(
170 AvahiServiceResolver *r,
171 AvahiIfIndex interface,
172 AvahiProtocol protocol,
173 AvahiResolverEvent event,
177 const char *host_name,
178 const AvahiAddress *a,
180 AvahiStringList *txt,
181 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
184 ServiceInfo *i = userdata;
190 case AVAHI_RESOLVER_FOUND: {
191 char address[AVAHI_ADDRESS_STR_MAX], *t;
193 avahi_address_snprint(address, sizeof(address), a);
195 t = avahi_string_list_to_string(txt);
197 print_service_line(i->config, '=', interface, protocol, name, type, domain, 0);
199 if (i->config->parsable)
200 printf(";%s;%s;%u;%s\n",
206 printf(" hostname = [%s]\n"
220 case AVAHI_RESOLVER_FAILURE:
222 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
227 avahi_service_resolver_free(i->resolver);
230 assert(n_resolving > 0);
232 check_terminate(i->config);
236 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
239 i = avahi_new(ServiceInfo, 1);
242 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
244 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
252 i->interface = interface;
253 i->protocol = protocol;
254 i->name = avahi_strdup(name);
255 i->type = avahi_strdup(type);
256 i->domain = avahi_strdup(domain);
259 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
264 static void remove_service(Config *c, ServiceInfo *i) {
268 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
271 avahi_service_resolver_free(i->resolver);
275 avahi_free(i->domain);
279 static void service_browser_callback(
280 AvahiServiceBrowser *b,
281 AvahiIfIndex interface,
282 AvahiProtocol protocol,
283 AvahiBrowserEvent event,
287 AvahiLookupResultFlags flags,
290 Config *c = userdata;
296 case AVAHI_BROWSER_NEW: {
297 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
300 if (find_service(interface, protocol, name, type, domain))
303 add_service(c, interface, protocol, name, type, domain);
305 print_service_line(c, '+', interface, protocol, name, type, domain, 1);
310 case AVAHI_BROWSER_REMOVE: {
313 if (!(info = find_service(interface, protocol, name, type, domain)))
316 remove_service(c, info);
318 print_service_line(c, '-', interface, protocol, name, type, domain, 1);
322 case AVAHI_BROWSER_FAILURE:
323 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
324 avahi_simple_poll_quit(simple_poll);
327 case AVAHI_BROWSER_CACHE_EXHAUSTED:
328 n_cache_exhausted --;
332 case AVAHI_BROWSER_ALL_FOR_NOW:
339 static void browse_service_type(Config *c, const char *stype, const char *domain) {
340 AvahiServiceBrowser *b;
347 for (i = browsed_types; i; i = i->next)
348 if (avahi_domain_equal(stype, (char*) i->text))
351 if (!(b = avahi_service_browser_new(
358 service_browser_callback,
361 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
362 avahi_simple_poll_quit(simple_poll);
365 browsed_types = avahi_string_list_add(browsed_types, stype);
371 static void service_type_browser_callback(
372 AvahiServiceTypeBrowser *b,
373 AVAHI_GCC_UNUSED AvahiIfIndex interface,
374 AVAHI_GCC_UNUSED AvahiProtocol protocol,
375 AvahiBrowserEvent event,
378 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
381 Config *c = userdata;
388 case AVAHI_BROWSER_NEW:
389 browse_service_type(c, type, domain);
392 case AVAHI_BROWSER_REMOVE:
393 /* We're dirty and never remove the browser again */
396 case AVAHI_BROWSER_FAILURE:
397 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
398 avahi_simple_poll_quit(simple_poll);
401 case AVAHI_BROWSER_CACHE_EXHAUSTED:
402 n_cache_exhausted --;
406 case AVAHI_BROWSER_ALL_FOR_NOW:
413 static void browse_all(Config *c) {
414 AvahiServiceTypeBrowser *b;
418 if (!(b = avahi_service_type_browser_new(
424 service_type_browser_callback,
427 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
428 avahi_simple_poll_quit(simple_poll);
435 static void domain_browser_callback(
436 AvahiDomainBrowser *b,
437 AVAHI_GCC_UNUSED AvahiIfIndex interface,
438 AVAHI_GCC_UNUSED AvahiProtocol protocol,
439 AvahiBrowserEvent event,
441 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
444 Config *c = userdata;
451 case AVAHI_BROWSER_NEW:
452 case AVAHI_BROWSER_REMOVE: {
453 char ifname[IF_NAMESIZE];
456 printf("%c;%s;%s;%s\n",
457 event == AVAHI_BROWSER_NEW ? '+' : '-',
458 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "",
459 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "",
462 printf("%c %4s %4s %s\n",
463 event == AVAHI_BROWSER_NEW ? '+' : '-',
464 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
465 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
470 case AVAHI_BROWSER_FAILURE:
471 fprintf(stderr, "domain_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
472 avahi_simple_poll_quit(simple_poll);
475 case AVAHI_BROWSER_CACHE_EXHAUSTED:
476 n_cache_exhausted --;
480 case AVAHI_BROWSER_ALL_FOR_NOW:
487 static void browse_domains(Config *c) {
488 AvahiDomainBrowser *b;
492 if (!(b = avahi_domain_browser_new(
497 AVAHI_DOMAIN_BROWSER_BROWSE,
499 domain_browser_callback,
502 fprintf(stderr, "avahi_domain_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
503 avahi_simple_poll_quit(simple_poll);
510 static int start(Config *config) {
514 if (config->verbose && !config->parsable) {
515 const char *version, *hn;
517 if (!(version = avahi_client_get_version_string(client))) {
518 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
522 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
523 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
527 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
529 if (config->command == COMMAND_BROWSE_DOMAINS)
530 fprintf(stderr, "E Ifce Prot Domain\n");
532 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
535 if (config->command == COMMAND_BROWSE_SERVICES)
536 browse_service_type(config, config->stype, config->domain);
537 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
540 assert(config->command == COMMAND_BROWSE_DOMAINS);
541 browse_domains(config);
548 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
549 Config *config = userdata;
551 /* This function might be called when avahi_client_new() has not
556 case AVAHI_CLIENT_FAILURE:
558 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
561 /* We have been disconnected, so let reconnect */
563 fprintf(stderr, "Disconnected, reconnecting ...\n");
565 avahi_client_free(client);
568 avahi_string_list_free(browsed_types);
569 browsed_types = NULL;
572 remove_service(config, services);
576 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
577 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
578 avahi_simple_poll_quit(simple_poll);
582 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
583 avahi_simple_poll_quit(simple_poll);
588 case AVAHI_CLIENT_S_REGISTERING:
589 case AVAHI_CLIENT_S_RUNNING:
590 case AVAHI_CLIENT_S_COLLISION:
593 if (start(config) < 0)
594 avahi_simple_poll_quit(simple_poll);
598 case AVAHI_CLIENT_CONNECTING:
600 if (config->verbose && !config->parsable)
601 fprintf(stderr, "Waiting for daemon ...\n");
607 static void help(FILE *f, const char *argv0) {
608 if (strstr(argv0, "domain"))
609 fprintf(f, "%s [options] \n\n", argv0);
612 "%s [options] <service type>\n"
615 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
619 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
622 argv0, argv0, argv0);
625 " -h --help Show this help\n"
626 " -V --version Show version\n"
627 " -D --browse-domains Browse for browsing domains instead of services\n"
628 " -a --all Show all services, regardless of the type\n"
629 " -d --domain=DOMAIN The domain to browse in\n"
630 " -v --verbose Enable verbose mode\n"
631 " -t --terminate Terminate after dumping a more or less complete list\n"
632 " -c --cache Terminate after dumping all entries from the cache\n"
633 " -l --ignore-local Ignore local services\n"
634 " -r --resolve Resolve services found\n"
635 " -f --no-fail Don't fail if the daemon is not available\n"
636 " -p --parsable Output in parsable format\n"
637 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
638 " -k --no-db-lookup Don't lookup service types\n"
639 " -b --dump-db Dump service type database\n"
644 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
647 static const struct option long_options[] = {
648 { "help", no_argument, NULL, 'h' },
649 { "version", no_argument, NULL, 'V' },
650 { "browse-domains", no_argument, NULL, 'D' },
651 { "domain", required_argument, NULL, 'd' },
652 { "all", no_argument, NULL, 'a' },
653 { "verbose", no_argument, NULL, 'v' },
654 { "terminate", no_argument, NULL, 't' },
655 { "cache", no_argument, NULL, 'c' },
656 { "ignore-local", no_argument, NULL, 'l' },
657 { "resolve", no_argument, NULL, 'r' },
658 { "no-fail", no_argument, NULL, 'f' },
659 { "parsable", no_argument, NULL, 'p' },
660 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
661 { "no-db-lookup", no_argument, NULL, 'k' },
662 { "dump-db", no_argument, NULL, 'b' },
669 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
671 c->terminate_on_cache_exhausted =
672 c->terminate_on_all_for_now =
677 c->domain = c->stype = NULL;
679 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
683 while ((o = getopt_long(argc, argv, "hVd:avtclrDfp"
684 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
687 , long_options, NULL)) >= 0) {
691 c->command = COMMAND_HELP;
694 c->command = COMMAND_VERSION;
697 c->command = COMMAND_BROWSE_ALL_SERVICES;
700 c->command = COMMAND_BROWSE_DOMAINS;
703 avahi_free(c->domain);
704 c->domain = avahi_strdup(optarg);
710 c->terminate_on_all_for_now = 1;
713 c->terminate_on_cache_exhausted = 1;
727 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
732 c->command = COMMAND_DUMP_STDB;
740 if (c->command == COMMAND_BROWSE_SERVICES) {
741 if (optind >= argc) {
742 fprintf(stderr, "Too few arguments\n");
746 c->stype = avahi_strdup(argv[optind]);
751 fprintf(stderr, "Too many arguments\n");
758 int main(int argc, char *argv[]) {
764 setlocale(LC_ALL, "");
766 if ((argv0 = strrchr(argv[0], '/')))
771 if ((ec = getenv("COLUMNS")))
772 n_columns = atoi(ec);
777 if (parse_command_line(&config, argv0, argc, argv) < 0)
780 switch (config.command) {
786 case COMMAND_VERSION:
787 printf("%s "PACKAGE_VERSION"\n", argv0);
791 case COMMAND_BROWSE_SERVICES:
792 case COMMAND_BROWSE_ALL_SERVICES:
793 case COMMAND_BROWSE_DOMAINS:
795 if (!(simple_poll = avahi_simple_poll_new())) {
796 fprintf(stderr, "Failed to create simple poll object.\n");
800 if (sigint_install(simple_poll) < 0)
803 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
804 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
808 avahi_simple_poll_loop(simple_poll);
812 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
813 case COMMAND_DUMP_STDB: {
817 while ((t = stdb_getent())) {
818 if (config.no_db_lookup)
821 printf("%s\n", stdb_lookup(t));
834 remove_service(&config, services);
837 avahi_client_free(client);
842 avahi_simple_poll_free(simple_poll);
844 avahi_free(config.domain);
845 avahi_free(config.stype);
847 avahi_string_list_free(browsed_types);
849 #if defined(HAVE_GDBM) || defined(HAVE_DBM)