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
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/domain.h>
38 #include <avahi-common/llist.h>
39 #include <avahi-client/client.h>
40 #include <avahi-client/lookup.h>
54 typedef struct Config {
56 int terminate_on_all_for_now;
57 int terminate_on_cache_exhausted;
69 typedef struct ServiceInfo ServiceInfo;
72 AvahiIfIndex interface;
73 AvahiProtocol protocol;
74 char *name, *type, *domain;
76 AvahiServiceResolver *resolver;
79 AVAHI_LLIST_FIELDS(ServiceInfo, info);
82 static AvahiSimplePoll *simple_poll = NULL;
83 static AvahiClient *client = NULL;
84 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
85 static AvahiStringList *browsed_types = NULL;
86 static ServiceInfo *services = NULL;
87 static int n_columns = 80;
89 static void check_terminate(Config *c) {
91 assert(n_all_for_now >= 0);
92 assert(n_cache_exhausted >= 0);
93 assert(n_resolving >= 0);
95 if (n_all_for_now <= 0 && n_resolving <= 0) {
98 printf(": All for now\n");
99 n_all_for_now++; /* Make sure that this event is not repeated */
102 if (c->terminate_on_all_for_now)
103 avahi_simple_poll_quit(simple_poll);
106 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
109 printf(": Cache exhausted\n");
110 n_cache_exhausted++; /* Make sure that this event is not repeated */
113 if (c->terminate_on_cache_exhausted)
114 avahi_simple_poll_quit(simple_poll);
118 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
121 for (i = services; i; i = i->info_next)
122 if (i->interface == interface &&
123 i->protocol == protocol &&
124 strcasecmp(i->name, name) == 0 &&
125 avahi_domain_equal(i->type, type) == 0 &&
126 avahi_domain_equal(i->domain, domain) == 0)
133 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
134 char ifname[IF_NAMESIZE];
137 if (!config->no_db_lookup)
138 type = stdb_lookup(type);
141 printf("%c %4s %4s %-*s %-20s %s\n",
143 if_indextoname(interface, ifname), avahi_proto_to_string(protocol),
144 n_columns-35, name, type, domain);
147 static void service_resolver_callback(
148 AvahiServiceResolver *r,
149 AvahiIfIndex interface,
150 AvahiProtocol protocol,
151 AvahiResolverEvent event,
155 const char *host_name,
156 const AvahiAddress *a,
158 AvahiStringList *txt,
159 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
162 ServiceInfo *i = userdata;
168 case AVAHI_RESOLVER_FOUND: {
169 char address[AVAHI_ADDRESS_STR_MAX], *t;
171 avahi_address_snprint(address, sizeof(address), a);
173 t = avahi_string_list_to_string(txt);
175 print_service_line(i->config, '=', interface, protocol, name, type, domain);
177 printf(" hostname = [%s]\n"
190 case AVAHI_RESOLVER_FAILURE:
192 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
197 avahi_service_resolver_free(i->resolver);
200 assert(n_resolving > 0);
202 check_terminate(i->config);
205 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
208 i = avahi_new(ServiceInfo, 1);
211 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
213 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
221 i->interface = interface;
222 i->protocol = protocol;
223 i->name = avahi_strdup(name);
224 i->type = avahi_strdup(type);
225 i->domain = avahi_strdup(domain);
228 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
233 static void remove_service(Config *c, ServiceInfo *i) {
237 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
240 avahi_service_resolver_free(i->resolver);
244 avahi_free(i->domain);
248 static void service_browser_callback(
249 AvahiServiceBrowser *b,
250 AvahiIfIndex interface,
251 AvahiProtocol protocol,
252 AvahiBrowserEvent event,
256 AvahiLookupResultFlags flags,
259 Config *c = userdata;
265 case AVAHI_BROWSER_NEW: {
266 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
269 if (find_service(interface, protocol, name, type, domain))
272 add_service(c, interface, protocol, name, type, domain);
274 print_service_line(c, '+', interface, protocol, name, type, domain);
279 case AVAHI_BROWSER_REMOVE: {
282 if (!(info = find_service(interface, protocol, name, type, domain)))
285 remove_service(c, info);
287 print_service_line(c, '-', interface, protocol, name, type, domain);
291 case AVAHI_BROWSER_FAILURE:
292 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
293 avahi_simple_poll_quit(simple_poll);
296 case AVAHI_BROWSER_CACHE_EXHAUSTED:
297 n_cache_exhausted --;
301 case AVAHI_BROWSER_ALL_FOR_NOW:
308 static void browse_service_type(Config *c, const char *stype, const char *domain) {
309 AvahiServiceBrowser *b;
316 for (i = browsed_types; i; i = i->next)
317 if (avahi_domain_equal(stype, (char*) i->text))
320 if (!(b = avahi_service_browser_new(
327 service_browser_callback,
330 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
331 avahi_simple_poll_quit(simple_poll);
334 browsed_types = avahi_string_list_add(browsed_types, stype);
340 static void service_type_browser_callback(
341 AvahiServiceTypeBrowser *b,
342 AVAHI_GCC_UNUSED AvahiIfIndex interface,
343 AVAHI_GCC_UNUSED AvahiProtocol protocol,
344 AvahiBrowserEvent event,
347 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
350 Config *c = userdata;
357 case AVAHI_BROWSER_NEW:
358 browse_service_type(c, type, domain);
361 case AVAHI_BROWSER_REMOVE:
362 /* We're dirty and never remove the browser again */
365 case AVAHI_BROWSER_FAILURE:
366 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
367 avahi_simple_poll_quit(simple_poll);
370 case AVAHI_BROWSER_CACHE_EXHAUSTED:
371 n_cache_exhausted --;
375 case AVAHI_BROWSER_ALL_FOR_NOW:
382 static void browse_all(Config *c) {
383 AvahiServiceTypeBrowser *b;
387 if (!(b = avahi_service_type_browser_new(
393 service_type_browser_callback,
396 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
397 avahi_simple_poll_quit(simple_poll);
404 static void client_callback(AVAHI_GCC_UNUSED AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
406 case AVAHI_CLIENT_DISCONNECTED:
407 fprintf(stderr, "Client disconnected, exiting.\n");
408 avahi_simple_poll_quit(simple_poll);
411 case AVAHI_CLIENT_S_REGISTERING:
412 case AVAHI_CLIENT_S_RUNNING:
413 case AVAHI_CLIENT_S_COLLISION:
418 static void help(FILE *f, const char *argv0) {
420 "%s [options] <type>\n"
421 "%s [options] -a\n\n"
422 " -h --help Show this help\n"
423 " -V --version Show version\n"
424 " -d --domain=DOMAIN The domain to browse\n"
425 " -a --all Show all services, regardless of the type\n"
426 " -v --verbose Enable verbose mode\n"
427 " -t --terminate Terminate after getting or more or less complete list\n"
428 " -c --cache Terminate after dumping all entries from the cache\n"
429 " -l --ignore-local Ignore local services\n"
430 " -r --resolve Resolve services found\n"
432 " -k --no-db-lookup Don't lookup service types\n"
438 static int parse_command_line(Config *c, int argc, char *argv[]) {
441 static const struct option long_options[] = {
442 { "help", no_argument, NULL, 'h' },
443 { "version", no_argument, NULL, 'V' },
444 { "domain", required_argument, NULL, 'd' },
445 { "all", no_argument, NULL, 'a' },
446 { "verbose", no_argument, NULL, 'v' },
447 { "terminate", no_argument, NULL, 't' },
448 { "cache", no_argument, NULL, 'c' },
449 { "ignore-local", no_argument, NULL, 'l' },
450 { "resolve", no_argument, NULL, 'r' },
452 { "no-db-lookup", no_argument, NULL, 'k' },
459 c->command = COMMAND_RUN;
461 c->terminate_on_cache_exhausted =
462 c->terminate_on_all_for_now =
466 c->domain = c->stype = NULL;
473 while ((o = getopt_long(argc, argv, "hVd:avtclr"
477 , long_options, NULL)) >= 0) {
481 c->command = COMMAND_HELP;
484 c->command = COMMAND_VERSION;
487 c->domain = avahi_strdup(optarg);
496 c->terminate_on_all_for_now = 1;
499 c->terminate_on_cache_exhausted = 1;
513 fprintf(stderr, "Invalid command line argument: %c\n", o);
518 if (c->command == COMMAND_RUN && !c->show_all) {
519 if (optind >= argc) {
520 fprintf(stderr, "Too few arguments\n");
524 c->stype = avahi_strdup(argv[optind]);
529 fprintf(stderr, "Too many arguments\n");
536 int main(int argc, char *argv[]) {
542 setlocale(LC_ALL, "");
544 if ((argv0 = strrchr(argv[0], '/')))
549 if ((ec = getenv("COLUMNS")))
550 n_columns = atoi(ec);
555 if (parse_command_line(&config, argc, argv) < 0)
558 switch (config.command) {
564 case COMMAND_VERSION:
565 printf("%s "PACKAGE_VERSION"\n", argv0);
571 if (!(simple_poll = avahi_simple_poll_new())) {
572 fprintf(stderr, "Failed to create simple poll object.\n");
576 if (sigint_install(simple_poll) < 0)
579 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) {
580 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
584 if (config.verbose) {
585 const char *version, *hn;
587 if (!(version = avahi_client_get_version_string(client))) {
588 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
592 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
593 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
597 fprintf(stderr, "Server version: %s; Host name: %s\n\n", version, hn);
598 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
604 browse_service_type(&config, config.stype, config.domain);
606 avahi_simple_poll_loop(simple_poll);
615 remove_service(&config, services);
618 avahi_client_free(client);
623 avahi_simple_poll_free(simple_poll);
625 avahi_free(config.domain);
626 avahi_free(config.stype);
628 avahi_string_list_free(browsed_types);