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;
72 typedef struct ServiceInfo ServiceInfo;
75 AvahiIfIndex interface;
76 AvahiProtocol protocol;
77 char *name, *type, *domain;
79 AvahiServiceResolver *resolver;
82 AVAHI_LLIST_FIELDS(ServiceInfo, info);
85 static AvahiSimplePoll *simple_poll = NULL;
86 static AvahiClient *client = NULL;
87 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
88 static AvahiStringList *browsed_types = NULL;
89 static ServiceInfo *services = NULL;
90 static int n_columns = 80;
92 static void check_terminate(Config *c) {
94 assert(n_all_for_now >= 0);
95 assert(n_cache_exhausted >= 0);
96 assert(n_resolving >= 0);
98 if (n_all_for_now <= 0 && n_resolving <= 0) {
101 printf(": All for now\n");
102 n_all_for_now++; /* Make sure that this event is not repeated */
105 if (c->terminate_on_all_for_now)
106 avahi_simple_poll_quit(simple_poll);
109 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
112 printf(": Cache exhausted\n");
113 n_cache_exhausted++; /* Make sure that this event is not repeated */
116 if (c->terminate_on_cache_exhausted)
117 avahi_simple_poll_quit(simple_poll);
121 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
124 for (i = services; i; i = i->info_next)
125 if (i->interface == interface &&
126 i->protocol == protocol &&
127 strcasecmp(i->name, name) == 0 &&
128 avahi_domain_equal(i->type, type) == 0 &&
129 avahi_domain_equal(i->domain, domain) == 0)
136 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
137 char ifname[IF_NAMESIZE];
140 if (!config->no_db_lookup)
141 type = stdb_lookup(type);
144 printf("%c %4s %4s %-*s %-20s %s\n",
146 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
147 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
148 n_columns-35, name, type, domain);
151 static void service_resolver_callback(
152 AvahiServiceResolver *r,
153 AvahiIfIndex interface,
154 AvahiProtocol protocol,
155 AvahiResolverEvent event,
159 const char *host_name,
160 const AvahiAddress *a,
162 AvahiStringList *txt,
163 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
166 ServiceInfo *i = userdata;
172 case AVAHI_RESOLVER_FOUND: {
173 char address[AVAHI_ADDRESS_STR_MAX], *t;
175 avahi_address_snprint(address, sizeof(address), a);
177 t = avahi_string_list_to_string(txt);
179 print_service_line(i->config, '=', interface, protocol, name, type, domain);
181 printf(" hostname = [%s]\n"
194 case AVAHI_RESOLVER_FAILURE:
196 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
201 avahi_service_resolver_free(i->resolver);
204 assert(n_resolving > 0);
206 check_terminate(i->config);
209 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
212 i = avahi_new(ServiceInfo, 1);
215 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
217 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
225 i->interface = interface;
226 i->protocol = protocol;
227 i->name = avahi_strdup(name);
228 i->type = avahi_strdup(type);
229 i->domain = avahi_strdup(domain);
232 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
237 static void remove_service(Config *c, ServiceInfo *i) {
241 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
244 avahi_service_resolver_free(i->resolver);
248 avahi_free(i->domain);
252 static void service_browser_callback(
253 AvahiServiceBrowser *b,
254 AvahiIfIndex interface,
255 AvahiProtocol protocol,
256 AvahiBrowserEvent event,
260 AvahiLookupResultFlags flags,
263 Config *c = userdata;
269 case AVAHI_BROWSER_NEW: {
270 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
273 if (find_service(interface, protocol, name, type, domain))
276 add_service(c, interface, protocol, name, type, domain);
278 print_service_line(c, '+', interface, protocol, name, type, domain);
283 case AVAHI_BROWSER_REMOVE: {
286 if (!(info = find_service(interface, protocol, name, type, domain)))
289 remove_service(c, info);
291 print_service_line(c, '-', interface, protocol, name, type, domain);
295 case AVAHI_BROWSER_FAILURE:
296 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
297 avahi_simple_poll_quit(simple_poll);
300 case AVAHI_BROWSER_CACHE_EXHAUSTED:
301 n_cache_exhausted --;
305 case AVAHI_BROWSER_ALL_FOR_NOW:
312 static void browse_service_type(Config *c, const char *stype, const char *domain) {
313 AvahiServiceBrowser *b;
320 for (i = browsed_types; i; i = i->next)
321 if (avahi_domain_equal(stype, (char*) i->text))
324 if (!(b = avahi_service_browser_new(
331 service_browser_callback,
334 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
335 avahi_simple_poll_quit(simple_poll);
338 browsed_types = avahi_string_list_add(browsed_types, stype);
344 static void service_type_browser_callback(
345 AvahiServiceTypeBrowser *b,
346 AVAHI_GCC_UNUSED AvahiIfIndex interface,
347 AVAHI_GCC_UNUSED AvahiProtocol protocol,
348 AvahiBrowserEvent event,
351 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
354 Config *c = userdata;
361 case AVAHI_BROWSER_NEW:
362 browse_service_type(c, type, domain);
365 case AVAHI_BROWSER_REMOVE:
366 /* We're dirty and never remove the browser again */
369 case AVAHI_BROWSER_FAILURE:
370 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
371 avahi_simple_poll_quit(simple_poll);
374 case AVAHI_BROWSER_CACHE_EXHAUSTED:
375 n_cache_exhausted --;
379 case AVAHI_BROWSER_ALL_FOR_NOW:
386 static void browse_all(Config *c) {
387 AvahiServiceTypeBrowser *b;
391 if (!(b = avahi_service_type_browser_new(
397 service_type_browser_callback,
400 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
401 avahi_simple_poll_quit(simple_poll);
408 static void domain_browser_callback(
409 AvahiDomainBrowser *b,
410 AVAHI_GCC_UNUSED AvahiIfIndex interface,
411 AVAHI_GCC_UNUSED AvahiProtocol protocol,
412 AvahiBrowserEvent event,
414 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
417 Config *c = userdata;
424 case AVAHI_BROWSER_NEW:
425 case AVAHI_BROWSER_REMOVE: {
426 char ifname[IF_NAMESIZE];
428 printf("%c %4s %4s %s\n",
429 event == AVAHI_BROWSER_NEW ? '+' : '-',
430 interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
431 protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
436 case AVAHI_BROWSER_FAILURE:
437 fprintf(stderr, "domain_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
438 avahi_simple_poll_quit(simple_poll);
441 case AVAHI_BROWSER_CACHE_EXHAUSTED:
442 n_cache_exhausted --;
446 case AVAHI_BROWSER_ALL_FOR_NOW:
453 static void browse_domains(Config *c) {
454 AvahiDomainBrowser *b;
458 if (!(b = avahi_domain_browser_new(
463 AVAHI_DOMAIN_BROWSER_BROWSE,
465 domain_browser_callback,
468 fprintf(stderr, "avahi_domain_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
469 avahi_simple_poll_quit(simple_poll);
476 static void client_callback(AVAHI_GCC_UNUSED AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
478 case AVAHI_CLIENT_DISCONNECTED:
479 fprintf(stderr, "Client disconnected, exiting.\n");
480 avahi_simple_poll_quit(simple_poll);
483 case AVAHI_CLIENT_S_REGISTERING:
484 case AVAHI_CLIENT_S_RUNNING:
485 case AVAHI_CLIENT_S_COLLISION:
490 static void help(FILE *f, const char *argv0) {
492 "%s [options] <service type>\n"
494 "%s [options] -D\n\n"
495 " -h --help Show this help\n"
496 " -V --version Show version\n"
497 " -D --browse-domains Browse for browsing domains instead of services\n"
498 " -a --all Show all services, regardless of the type\n"
499 " -d --domain=DOMAIN The domain to browse in\n"
500 " -v --verbose Enable verbose mode\n"
501 " -t --terminate Terminate after dumping a more or less complete list\n"
502 " -c --cache Terminate after dumping all entries from the cache\n"
503 " -l --ignore-local Ignore local services\n"
504 " -r --resolve Resolve services found\n"
506 " -k --no-db-lookup Don't lookup service types\n"
508 , argv0, argv0, argv0);
511 static int parse_command_line(Config *c, int argc, char *argv[]) {
514 static const struct option long_options[] = {
515 { "help", no_argument, NULL, 'h' },
516 { "version", no_argument, NULL, 'V' },
517 { "browse-domains", no_argument, NULL, 'D' },
518 { "domain", required_argument, NULL, 'd' },
519 { "all", no_argument, NULL, 'a' },
520 { "verbose", no_argument, NULL, 'v' },
521 { "terminate", no_argument, NULL, 't' },
522 { "cache", no_argument, NULL, 'c' },
523 { "ignore-local", no_argument, NULL, 'l' },
524 { "resolve", no_argument, NULL, 'r' },
526 { "no-db-lookup", no_argument, NULL, 'k' },
533 c->command = COMMAND_BROWSE_SERVICES;
535 c->terminate_on_cache_exhausted =
536 c->terminate_on_all_for_now =
539 c->domain = c->stype = NULL;
546 while ((o = getopt_long(argc, argv, "hVd:avtclrD"
550 , long_options, NULL)) >= 0) {
554 c->command = COMMAND_HELP;
557 c->command = COMMAND_VERSION;
560 c->command = COMMAND_BROWSE_ALL_SERVICES;
563 c->command = COMMAND_BROWSE_DOMAINS;
566 c->domain = avahi_strdup(optarg);
572 c->terminate_on_all_for_now = 1;
575 c->terminate_on_cache_exhausted = 1;
589 fprintf(stderr, "Invalid command line argument: %c\n", o);
594 if (c->command == COMMAND_BROWSE_SERVICES) {
595 if (optind >= argc) {
596 fprintf(stderr, "Too few arguments\n");
600 c->stype = avahi_strdup(argv[optind]);
605 fprintf(stderr, "Too many arguments\n");
612 int main(int argc, char *argv[]) {
618 setlocale(LC_ALL, "");
620 if ((argv0 = strrchr(argv[0], '/')))
625 if ((ec = getenv("COLUMNS")))
626 n_columns = atoi(ec);
631 if (parse_command_line(&config, argc, argv) < 0)
634 switch (config.command) {
640 case COMMAND_VERSION:
641 printf("%s "PACKAGE_VERSION"\n", argv0);
645 case COMMAND_BROWSE_SERVICES:
646 case COMMAND_BROWSE_ALL_SERVICES:
647 case COMMAND_BROWSE_DOMAINS:
649 if (!(simple_poll = avahi_simple_poll_new())) {
650 fprintf(stderr, "Failed to create simple poll object.\n");
654 if (sigint_install(simple_poll) < 0)
657 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) {
658 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
662 if (config.verbose) {
663 const char *version, *hn;
665 if (!(version = avahi_client_get_version_string(client))) {
666 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
670 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
671 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
675 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
677 if (config.command == COMMAND_BROWSE_DOMAINS)
678 fprintf(stderr, "E Ifce Prot Domain\n");
680 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
683 if (config.command == COMMAND_BROWSE_SERVICES)
684 browse_service_type(&config, config.stype, config.domain);
685 else if (config.command == COMMAND_BROWSE_ALL_SERVICES)
688 assert(config.command == COMMAND_BROWSE_DOMAINS);
689 browse_domains(&config);
692 avahi_simple_poll_loop(simple_poll);
701 remove_service(&config, services);
704 avahi_client_free(client);
709 avahi_simple_poll_free(simple_poll);
711 avahi_free(config.domain);
712 avahi_free(config.stype);
714 avahi_string_list_free(browsed_types);