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>
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;
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) == 0 &&
131 avahi_domain_equal(i->domain, domain) == 0)
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];
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);
153 static void service_resolver_callback(
154 AvahiServiceResolver *r,
155 AvahiIfIndex interface,
156 AvahiProtocol protocol,
157 AvahiResolverEvent event,
161 const char *host_name,
162 const AvahiAddress *a,
164 AvahiStringList *txt,
165 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
168 ServiceInfo *i = userdata;
174 case AVAHI_RESOLVER_FOUND: {
175 char address[AVAHI_ADDRESS_STR_MAX], *t;
177 avahi_address_snprint(address, sizeof(address), a);
179 t = avahi_string_list_to_string(txt);
181 print_service_line(i->config, '=', interface, protocol, name, type, domain);
183 printf(" hostname = [%s]\n"
196 case AVAHI_RESOLVER_FAILURE:
198 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
203 avahi_service_resolver_free(i->resolver);
206 assert(n_resolving > 0);
208 check_terminate(i->config);
211 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
214 i = avahi_new(ServiceInfo, 1);
217 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
219 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 i->interface = interface;
228 i->protocol = protocol;
229 i->name = avahi_strdup(name);
230 i->type = avahi_strdup(type);
231 i->domain = avahi_strdup(domain);
234 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
239 static void remove_service(Config *c, ServiceInfo *i) {
243 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
246 avahi_service_resolver_free(i->resolver);
250 avahi_free(i->domain);
254 static void service_browser_callback(
255 AvahiServiceBrowser *b,
256 AvahiIfIndex interface,
257 AvahiProtocol protocol,
258 AvahiBrowserEvent event,
262 AvahiLookupResultFlags flags,
265 Config *c = userdata;
271 case AVAHI_BROWSER_NEW: {
272 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
275 if (find_service(interface, protocol, name, type, domain))
278 add_service(c, interface, protocol, name, type, domain);
280 print_service_line(c, '+', interface, protocol, name, type, domain);
285 case AVAHI_BROWSER_REMOVE: {
288 if (!(info = find_service(interface, protocol, name, type, domain)))
291 remove_service(c, info);
293 print_service_line(c, '-', interface, protocol, name, type, domain);
297 case AVAHI_BROWSER_FAILURE:
298 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
299 avahi_simple_poll_quit(simple_poll);
302 case AVAHI_BROWSER_CACHE_EXHAUSTED:
303 n_cache_exhausted --;
307 case AVAHI_BROWSER_ALL_FOR_NOW:
314 static void browse_service_type(Config *c, const char *stype, const char *domain) {
315 AvahiServiceBrowser *b;
322 for (i = browsed_types; i; i = i->next)
323 if (avahi_domain_equal(stype, (char*) i->text))
326 if (!(b = avahi_service_browser_new(
333 service_browser_callback,
336 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
337 avahi_simple_poll_quit(simple_poll);
340 browsed_types = avahi_string_list_add(browsed_types, stype);
346 static void service_type_browser_callback(
347 AvahiServiceTypeBrowser *b,
348 AVAHI_GCC_UNUSED AvahiIfIndex interface,
349 AVAHI_GCC_UNUSED AvahiProtocol protocol,
350 AvahiBrowserEvent event,
353 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
356 Config *c = userdata;
363 case AVAHI_BROWSER_NEW:
364 browse_service_type(c, type, domain);
367 case AVAHI_BROWSER_REMOVE:
368 /* We're dirty and never remove the browser again */
371 case AVAHI_BROWSER_FAILURE:
372 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
373 avahi_simple_poll_quit(simple_poll);
376 case AVAHI_BROWSER_CACHE_EXHAUSTED:
377 n_cache_exhausted --;
381 case AVAHI_BROWSER_ALL_FOR_NOW:
388 static void browse_all(Config *c) {
389 AvahiServiceTypeBrowser *b;
393 if (!(b = avahi_service_type_browser_new(
399 service_type_browser_callback,
402 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
403 avahi_simple_poll_quit(simple_poll);
410 static void domain_browser_callback(
411 AvahiDomainBrowser *b,
412 AVAHI_GCC_UNUSED AvahiIfIndex interface,
413 AVAHI_GCC_UNUSED AvahiProtocol protocol,
414 AvahiBrowserEvent event,
416 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
419 Config *c = userdata;
426 case AVAHI_BROWSER_NEW:
427 case AVAHI_BROWSER_REMOVE: {
428 char ifname[IF_NAMESIZE];
430 printf("%c %4s %4s %s\n",
431 event == AVAHI_BROWSER_NEW ? '+' : '-',
432 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
433 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
438 case AVAHI_BROWSER_FAILURE:
439 fprintf(stderr, "domain_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
440 avahi_simple_poll_quit(simple_poll);
443 case AVAHI_BROWSER_CACHE_EXHAUSTED:
444 n_cache_exhausted --;
448 case AVAHI_BROWSER_ALL_FOR_NOW:
455 static void browse_domains(Config *c) {
456 AvahiDomainBrowser *b;
460 if (!(b = avahi_domain_browser_new(
465 AVAHI_DOMAIN_BROWSER_BROWSE,
467 domain_browser_callback,
470 fprintf(stderr, "avahi_domain_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
471 avahi_simple_poll_quit(simple_poll);
478 static int start(Config *config) {
482 if (config->verbose) {
483 const char *version, *hn;
485 if (!(version = avahi_client_get_version_string(client))) {
486 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
490 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
491 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
495 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
497 if (config->command == COMMAND_BROWSE_DOMAINS)
498 fprintf(stderr, "E Ifce Prot Domain\n");
500 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
503 if (config->command == COMMAND_BROWSE_SERVICES)
504 browse_service_type(config, config->stype, config->domain);
505 else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
508 assert(config->command == COMMAND_BROWSE_DOMAINS);
509 browse_domains(config);
516 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
517 Config *config = userdata;
519 /* This function might be called when avahi_client_new() has not
524 case AVAHI_CLIENT_FAILURE:
526 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
529 /* We have been disconnected, so let reconnect */
531 fprintf(stderr, "Disconnected, reconnecting ...\n");
533 avahi_client_free(client);
536 avahi_string_list_free(browsed_types);
537 browsed_types = NULL;
540 remove_service(config, services);
544 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
545 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
546 avahi_simple_poll_quit(simple_poll);
550 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
551 avahi_simple_poll_quit(simple_poll);
556 case AVAHI_CLIENT_S_REGISTERING:
557 case AVAHI_CLIENT_S_RUNNING:
558 case AVAHI_CLIENT_S_COLLISION:
561 if (start(config) < 0)
562 avahi_simple_poll_quit(simple_poll);
566 case AVAHI_CLIENT_CONNECTING:
569 fprintf(stderr, "Waiting for daemon ...\n");
575 static void help(FILE *f, const char *argv0) {
576 if (strstr(argv0, "domain"))
577 fprintf(f, "%s [options] \n\n", argv0);
580 "%s [options] <service type>\n"
582 "%s [options] -D\n\n",
583 argv0, argv0, argv0);
586 " -h --help Show this help\n"
587 " -V --version Show version\n"
588 " -D --browse-domains Browse for browsing domains instead of services\n"
589 " -a --all Show all services, regardless of the type\n"
590 " -d --domain=DOMAIN The domain to browse in\n"
591 " -v --verbose Enable verbose mode\n"
592 " -t --terminate Terminate after dumping a more or less complete list\n"
593 " -c --cache Terminate after dumping all entries from the cache\n"
594 " -l --ignore-local Ignore local services\n"
595 " -r --resolve Resolve services found\n"
596 " -f --no-fail Don't fail if the server is not available\n"
598 " -k --no-db-lookup Don't lookup service types\n");
602 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
605 static const struct option long_options[] = {
606 { "help", no_argument, NULL, 'h' },
607 { "version", no_argument, NULL, 'V' },
608 { "browse-domains", no_argument, NULL, 'D' },
609 { "domain", required_argument, NULL, 'd' },
610 { "all", no_argument, NULL, 'a' },
611 { "verbose", no_argument, NULL, 'v' },
612 { "terminate", no_argument, NULL, 't' },
613 { "cache", no_argument, NULL, 'c' },
614 { "ignore-local", no_argument, NULL, 'l' },
615 { "resolve", no_argument, NULL, 'r' },
616 { "no-fail", no_argument, NULL, 'f' },
618 { "no-db-lookup", no_argument, NULL, 'k' },
625 c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
627 c->terminate_on_cache_exhausted =
628 c->terminate_on_all_for_now =
632 c->domain = c->stype = NULL;
639 while ((o = getopt_long(argc, argv, "hVd:avtclrDf"
643 , long_options, NULL)) >= 0) {
647 c->command = COMMAND_HELP;
650 c->command = COMMAND_VERSION;
653 c->command = COMMAND_BROWSE_ALL_SERVICES;
656 c->command = COMMAND_BROWSE_DOMAINS;
659 c->domain = avahi_strdup(optarg);
665 c->terminate_on_all_for_now = 1;
668 c->terminate_on_cache_exhausted = 1;
685 fprintf(stderr, "Invalid command line argument: %c\n", o);
690 if (c->command == COMMAND_BROWSE_SERVICES) {
691 if (optind >= argc) {
692 fprintf(stderr, "Too few arguments\n");
696 c->stype = avahi_strdup(argv[optind]);
701 fprintf(stderr, "Too many arguments\n");
708 int main(int argc, char *argv[]) {
714 setlocale(LC_ALL, "");
716 if ((argv0 = strrchr(argv[0], '/')))
721 if ((ec = getenv("COLUMNS")))
722 n_columns = atoi(ec);
727 if (parse_command_line(&config, argv0, argc, argv) < 0)
730 switch (config.command) {
736 case COMMAND_VERSION:
737 printf("%s "PACKAGE_VERSION"\n", argv0);
741 case COMMAND_BROWSE_SERVICES:
742 case COMMAND_BROWSE_ALL_SERVICES:
743 case COMMAND_BROWSE_DOMAINS:
745 if (!(simple_poll = avahi_simple_poll_new())) {
746 fprintf(stderr, "Failed to create simple poll object.\n");
750 if (sigint_install(simple_poll) < 0)
753 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
754 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
758 avahi_simple_poll_loop(simple_poll);
767 remove_service(&config, services);
770 avahi_client_free(client);
775 avahi_simple_poll_free(simple_poll);
777 avahi_free(config.domain);
778 avahi_free(config.stype);
780 avahi_string_list_free(browsed_types);