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
33 #include <avahi-common/simple-watch.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/domain.h>
37 #include <avahi-common/llist.h>
38 #include <avahi-client/client.h>
39 #include <avahi-client/lookup.h>
49 typedef struct Config {
51 int terminate_on_all_for_now;
52 int terminate_on_cache_exhausted;
62 typedef struct ServiceInfo ServiceInfo;
65 AvahiIfIndex interface;
66 AvahiProtocol protocol;
67 char *name, *type, *domain;
69 AvahiServiceResolver *resolver;
72 AVAHI_LLIST_FIELDS(ServiceInfo, info);
75 static AvahiSimplePoll *simple_poll = NULL;
76 static AvahiClient *client = NULL;
77 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
78 static AvahiStringList *browsed_types = NULL;
79 static ServiceInfo *services = NULL;
80 static int n_columns = 80;
82 static void check_terminate(Config *c) {
84 assert(n_all_for_now >= 0);
85 assert(n_cache_exhausted >= 0);
86 assert(n_resolving >= 0);
88 if (n_all_for_now <= 0 && n_resolving <= 0) {
91 printf(": All for now\n");
92 n_all_for_now++; /* Make sure that this event is not repeated */
95 if (c->terminate_on_all_for_now)
96 avahi_simple_poll_quit(simple_poll);
99 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
102 printf(": Cache exhausted\n");
103 n_cache_exhausted++; /* Make sure that this event is not repeated */
106 if (c->terminate_on_cache_exhausted)
107 avahi_simple_poll_quit(simple_poll);
111 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
114 for (i = services; i; i = i->info_next)
115 if (i->interface == interface &&
116 i->protocol == protocol &&
117 strcasecmp(i->name, name) == 0 &&
118 avahi_domain_equal(i->type, type) == 0 &&
119 avahi_domain_equal(i->domain, domain) == 0)
126 static void service_resolver_callback(
127 AvahiServiceResolver *r,
128 AvahiIfIndex interface,
129 AvahiProtocol protocol,
130 AvahiResolverEvent event,
134 const char *host_name,
135 const AvahiAddress *a,
137 AvahiStringList *txt,
138 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
141 ServiceInfo *i = userdata;
147 case AVAHI_RESOLVER_FOUND: {
148 char address[AVAHI_ADDRESS_STR_MAX], *t;
149 char ifname[IF_NAMESIZE];
151 avahi_address_snprint(address, sizeof(address), a);
153 t = avahi_string_list_to_string(txt);
155 printf("= %4s %4s %-*s %-20s %s\n"
160 if_indextoname(interface, ifname), avahi_proto_to_string(protocol),
161 n_columns-35, name, type, domain,
171 case AVAHI_RESOLVER_FAILURE:
173 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
178 avahi_service_resolver_free(i->resolver);
181 assert(n_resolving > 0);
183 check_terminate(i->config);
186 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
189 i = avahi_new(ServiceInfo, 1);
192 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
194 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
202 i->interface = interface;
203 i->protocol = protocol;
204 i->name = avahi_strdup(name);
205 i->type = avahi_strdup(type);
206 i->domain = avahi_strdup(domain);
209 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
214 static void remove_service(Config *c, ServiceInfo *i) {
218 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
221 avahi_service_resolver_free(i->resolver);
225 avahi_free(i->domain);
229 static void service_browser_callback(
230 AvahiServiceBrowser *b,
231 AvahiIfIndex interface,
232 AvahiProtocol protocol,
233 AvahiBrowserEvent event,
237 AvahiLookupResultFlags flags,
240 Config *c = userdata;
246 case AVAHI_BROWSER_NEW: {
247 char ifname[IF_NAMESIZE];
248 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
251 if (find_service(interface, protocol, name, type, domain))
254 add_service(c, interface, protocol, name, type, domain);
256 printf("+ %4s %4s %-*s %-20s %s\n", if_indextoname(interface, ifname), avahi_proto_to_string(protocol), n_columns-35, name, type, domain);
261 case AVAHI_BROWSER_REMOVE: {
262 char ifname[IF_NAMESIZE];
265 if (!(info = find_service(interface, protocol, name, type, domain)))
268 remove_service(c, info);
270 printf("- %4s %4s %-*s %-20s %s\n", if_indextoname(interface, ifname), avahi_proto_to_string(protocol), n_columns-35, name, type, domain);
274 case AVAHI_BROWSER_FAILURE:
275 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
276 avahi_simple_poll_quit(simple_poll);
279 case AVAHI_BROWSER_CACHE_EXHAUSTED:
280 n_cache_exhausted --;
284 case AVAHI_BROWSER_ALL_FOR_NOW:
291 static void browse_service_type(Config *c, const char *stype, const char *domain) {
292 AvahiServiceBrowser *b;
299 for (i = browsed_types; i; i = i->next)
300 if (avahi_domain_equal(stype, (char*) i->text))
303 if (!(b = avahi_service_browser_new(
310 service_browser_callback,
313 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
314 avahi_simple_poll_quit(simple_poll);
317 browsed_types = avahi_string_list_add(browsed_types, stype);
323 static void service_type_browser_callback(
324 AvahiServiceTypeBrowser *b,
325 AVAHI_GCC_UNUSED AvahiIfIndex interface,
326 AVAHI_GCC_UNUSED AvahiProtocol protocol,
327 AvahiBrowserEvent event,
330 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
333 Config *c = userdata;
340 case AVAHI_BROWSER_NEW:
341 browse_service_type(c, type, domain);
344 case AVAHI_BROWSER_REMOVE:
345 /* We're dirty and never remove the browser again */
348 case AVAHI_BROWSER_FAILURE:
349 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
350 avahi_simple_poll_quit(simple_poll);
353 case AVAHI_BROWSER_CACHE_EXHAUSTED:
354 n_cache_exhausted --;
358 case AVAHI_BROWSER_ALL_FOR_NOW:
365 static void browse_all(Config *c) {
366 AvahiServiceTypeBrowser *b;
370 if (!(b = avahi_service_type_browser_new(
376 service_type_browser_callback,
379 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
380 avahi_simple_poll_quit(simple_poll);
387 static void client_callback(AVAHI_GCC_UNUSED AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
389 case AVAHI_CLIENT_DISCONNECTED:
390 fprintf(stderr, "Client disconnected, exiting.\n");
391 avahi_simple_poll_quit(simple_poll);
394 case AVAHI_CLIENT_S_REGISTERING:
395 case AVAHI_CLIENT_S_RUNNING:
396 case AVAHI_CLIENT_S_COLLISION:
401 static void help(FILE *f, const char *argv0) {
403 "%s [options] <type>\n"
404 "%s [options] -a\n\n"
405 " -h --help Show this help\n"
406 " -V --version Show version\n"
407 " -d --domain=DOMAIN The domain to browse\n"
408 " -a --all Show all services, regardless of the type\n"
409 " -v --verbose Enable verbose mode\n"
410 " -t --terminate Terminate after getting or more or less complete list\n"
411 " -c --cache Terminate after dumping all entries from the cache\n"
412 " -l --ignore-local Ignore local services\n"
413 " -r --resolve Resolve services found\n",
418 static int parse_command_line(Config *c, int argc, char *argv[]) {
421 static const struct option long_options[] = {
422 { "help", no_argument, NULL, 'h' },
423 { "version", no_argument, NULL, 'V' },
424 { "domain", required_argument, NULL, 'd' },
425 { "all", no_argument, NULL, 'a' },
426 { "verbose", no_argument, NULL, 'v' },
427 { "terminate", no_argument, NULL, 't' },
428 { "cache", no_argument, NULL, 'c' },
429 { "ignore-local", no_argument, NULL, 'l' },
430 { "resolve", no_argument, NULL, 'r' },
436 c->command = COMMAND_RUN;
438 c->terminate_on_cache_exhausted =
439 c->terminate_on_all_for_now =
443 c->domain = c->stype = NULL;
446 while ((o = getopt_long(argc, argv, "hVd:avtclr", long_options, NULL)) >= 0) {
450 c->command = COMMAND_HELP;
453 c->command = COMMAND_VERSION;
456 c->domain = avahi_strdup(optarg);
465 c->terminate_on_all_for_now = 1;
468 c->terminate_on_cache_exhausted = 1;
477 fprintf(stderr, "Invalid command line argument: %c\n", o);
482 if (c->command == COMMAND_RUN && !c->show_all) {
483 if (optind >= argc) {
484 fprintf(stderr, "Too few arguments\n");
488 c->stype = avahi_strdup(argv[optind]);
493 fprintf(stderr, "Too many arguments\n");
500 int main(int argc, char *argv[]) {
506 if ((argv0 = strrchr(argv[0], '/')))
511 if ((ec = getenv("COLUMNS")))
512 n_columns = atoi(ec);
517 if (parse_command_line(&config, argc, argv) < 0)
520 switch (config.command) {
526 case COMMAND_VERSION:
527 printf("%s "PACKAGE_VERSION"\n", argv0);
533 if (!(simple_poll = avahi_simple_poll_new())) {
534 fprintf(stderr, "Failed to create simple poll object.\n");
538 if (sigint_install(simple_poll) < 0)
541 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) {
542 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
546 if (config.verbose) {
547 const char *version, *hn;
549 if (!(version = avahi_client_get_version_string(client))) {
550 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
554 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
555 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
559 fprintf(stderr, "Server version: %s; Host name: %s\n\n", version, hn);
560 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
566 browse_service_type(&config, config.stype, config.domain);
568 avahi_simple_poll_loop(simple_poll);
577 remove_service(&config, services);
580 avahi_client_free(client);
585 avahi_simple_poll_free(simple_poll);
587 avahi_free(config.domain);
588 avahi_free(config.stype);
590 avahi_string_list_free(browsed_types);