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>
56 typedef struct Config {
58 int terminate_on_all_for_now;
59 int terminate_on_cache_exhausted;
71 typedef struct ServiceInfo ServiceInfo;
74 AvahiIfIndex interface;
75 AvahiProtocol protocol;
76 char *name, *type, *domain;
78 AvahiServiceResolver *resolver;
81 AVAHI_LLIST_FIELDS(ServiceInfo, info);
84 static AvahiSimplePoll *simple_poll = NULL;
85 static AvahiClient *client = NULL;
86 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
87 static AvahiStringList *browsed_types = NULL;
88 static ServiceInfo *services = NULL;
89 static int n_columns = 80;
91 static void check_terminate(Config *c) {
93 assert(n_all_for_now >= 0);
94 assert(n_cache_exhausted >= 0);
95 assert(n_resolving >= 0);
97 if (n_all_for_now <= 0 && n_resolving <= 0) {
100 printf(": All for now\n");
101 n_all_for_now++; /* Make sure that this event is not repeated */
104 if (c->terminate_on_all_for_now)
105 avahi_simple_poll_quit(simple_poll);
108 if (n_cache_exhausted <= 0 && n_resolving <= 0) {
111 printf(": Cache exhausted\n");
112 n_cache_exhausted++; /* Make sure that this event is not repeated */
115 if (c->terminate_on_cache_exhausted)
116 avahi_simple_poll_quit(simple_poll);
120 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
123 for (i = services; i; i = i->info_next)
124 if (i->interface == interface &&
125 i->protocol == protocol &&
126 strcasecmp(i->name, name) == 0 &&
127 avahi_domain_equal(i->type, type) == 0 &&
128 avahi_domain_equal(i->domain, domain) == 0)
135 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
136 char ifname[IF_NAMESIZE];
139 if (!config->no_db_lookup)
140 type = stdb_lookup(type);
143 printf("%c %4s %4s %-*s %-20s %s\n",
145 if_indextoname(interface, ifname), avahi_proto_to_string(protocol),
146 n_columns-35, name, type, domain);
149 static void service_resolver_callback(
150 AvahiServiceResolver *r,
151 AvahiIfIndex interface,
152 AvahiProtocol protocol,
153 AvahiResolverEvent event,
157 const char *host_name,
158 const AvahiAddress *a,
160 AvahiStringList *txt,
161 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
164 ServiceInfo *i = userdata;
170 case AVAHI_RESOLVER_FOUND: {
171 char address[AVAHI_ADDRESS_STR_MAX], *t;
173 avahi_address_snprint(address, sizeof(address), a);
175 t = avahi_string_list_to_string(txt);
177 print_service_line(i->config, '=', interface, protocol, name, type, domain);
179 printf(" hostname = [%s]\n"
192 case AVAHI_RESOLVER_FAILURE:
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)));
199 avahi_service_resolver_free(i->resolver);
202 assert(n_resolving > 0);
204 check_terminate(i->config);
207 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
210 i = avahi_new(ServiceInfo, 1);
213 if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
215 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
223 i->interface = interface;
224 i->protocol = protocol;
225 i->name = avahi_strdup(name);
226 i->type = avahi_strdup(type);
227 i->domain = avahi_strdup(domain);
230 AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
235 static void remove_service(Config *c, ServiceInfo *i) {
239 AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
242 avahi_service_resolver_free(i->resolver);
246 avahi_free(i->domain);
250 static void service_browser_callback(
251 AvahiServiceBrowser *b,
252 AvahiIfIndex interface,
253 AvahiProtocol protocol,
254 AvahiBrowserEvent event,
258 AvahiLookupResultFlags flags,
261 Config *c = userdata;
267 case AVAHI_BROWSER_NEW: {
268 if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
271 if (find_service(interface, protocol, name, type, domain))
274 add_service(c, interface, protocol, name, type, domain);
276 print_service_line(c, '+', interface, protocol, name, type, domain);
281 case AVAHI_BROWSER_REMOVE: {
284 if (!(info = find_service(interface, protocol, name, type, domain)))
287 remove_service(c, info);
289 print_service_line(c, '-', interface, protocol, name, type, domain);
293 case AVAHI_BROWSER_FAILURE:
294 fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
295 avahi_simple_poll_quit(simple_poll);
298 case AVAHI_BROWSER_CACHE_EXHAUSTED:
299 n_cache_exhausted --;
303 case AVAHI_BROWSER_ALL_FOR_NOW:
310 static void browse_service_type(Config *c, const char *stype, const char *domain) {
311 AvahiServiceBrowser *b;
318 for (i = browsed_types; i; i = i->next)
319 if (avahi_domain_equal(stype, (char*) i->text))
322 if (!(b = avahi_service_browser_new(
329 service_browser_callback,
332 fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
333 avahi_simple_poll_quit(simple_poll);
336 browsed_types = avahi_string_list_add(browsed_types, stype);
342 static void service_type_browser_callback(
343 AvahiServiceTypeBrowser *b,
344 AVAHI_GCC_UNUSED AvahiIfIndex interface,
345 AVAHI_GCC_UNUSED AvahiProtocol protocol,
346 AvahiBrowserEvent event,
349 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
352 Config *c = userdata;
359 case AVAHI_BROWSER_NEW:
360 browse_service_type(c, type, domain);
363 case AVAHI_BROWSER_REMOVE:
364 /* We're dirty and never remove the browser again */
367 case AVAHI_BROWSER_FAILURE:
368 fprintf(stderr, "service_type_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
369 avahi_simple_poll_quit(simple_poll);
372 case AVAHI_BROWSER_CACHE_EXHAUSTED:
373 n_cache_exhausted --;
377 case AVAHI_BROWSER_ALL_FOR_NOW:
384 static void browse_all(Config *c) {
385 AvahiServiceTypeBrowser *b;
389 if (!(b = avahi_service_type_browser_new(
395 service_type_browser_callback,
398 fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
399 avahi_simple_poll_quit(simple_poll);
406 static void client_callback(AVAHI_GCC_UNUSED AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
408 case AVAHI_CLIENT_DISCONNECTED:
409 fprintf(stderr, "Client disconnected, exiting.\n");
410 avahi_simple_poll_quit(simple_poll);
413 case AVAHI_CLIENT_S_REGISTERING:
414 case AVAHI_CLIENT_S_RUNNING:
415 case AVAHI_CLIENT_S_COLLISION:
420 static void help(FILE *f, const char *argv0) {
422 "%s [options] <type>\n"
423 "%s [options] -a\n\n"
424 " -h --help Show this help\n"
425 " -V --version Show version\n"
426 " -d --domain=DOMAIN The domain to browse\n"
427 " -a --all Show all services, regardless of the type\n"
428 " -v --verbose Enable verbose mode\n"
429 " -t --terminate Terminate after getting or more or less complete list\n"
430 " -c --cache Terminate after dumping all entries from the cache\n"
431 " -l --ignore-local Ignore local services\n"
432 " -r --resolve Resolve services found\n"
434 " -k --no-db-lookup Don't lookup service types\n"
440 static int parse_command_line(Config *c, int argc, char *argv[]) {
443 static const struct option long_options[] = {
444 { "help", no_argument, NULL, 'h' },
445 { "version", no_argument, NULL, 'V' },
446 { "domain", required_argument, NULL, 'd' },
447 { "all", no_argument, NULL, 'a' },
448 { "verbose", no_argument, NULL, 'v' },
449 { "terminate", no_argument, NULL, 't' },
450 { "cache", no_argument, NULL, 'c' },
451 { "ignore-local", no_argument, NULL, 'l' },
452 { "resolve", no_argument, NULL, 'r' },
454 { "no-db-lookup", no_argument, NULL, 'k' },
461 c->command = COMMAND_RUN;
463 c->terminate_on_cache_exhausted =
464 c->terminate_on_all_for_now =
468 c->domain = c->stype = NULL;
475 while ((o = getopt_long(argc, argv, "hVd:avtclr"
479 , long_options, NULL)) >= 0) {
483 c->command = COMMAND_HELP;
486 c->command = COMMAND_VERSION;
489 c->domain = avahi_strdup(optarg);
498 c->terminate_on_all_for_now = 1;
501 c->terminate_on_cache_exhausted = 1;
515 fprintf(stderr, "Invalid command line argument: %c\n", o);
520 if (c->command == COMMAND_RUN && !c->show_all) {
521 if (optind >= argc) {
522 fprintf(stderr, "Too few arguments\n");
526 c->stype = avahi_strdup(argv[optind]);
531 fprintf(stderr, "Too many arguments\n");
538 int main(int argc, char *argv[]) {
544 setlocale(LC_ALL, "");
546 if ((argv0 = strrchr(argv[0], '/')))
551 if ((ec = getenv("COLUMNS")))
552 n_columns = atoi(ec);
557 if (parse_command_line(&config, argc, argv) < 0)
560 switch (config.command) {
566 case COMMAND_VERSION:
567 printf("%s "PACKAGE_VERSION"\n", argv0);
573 if (!(simple_poll = avahi_simple_poll_new())) {
574 fprintf(stderr, "Failed to create simple poll object.\n");
578 if (sigint_install(simple_poll) < 0)
581 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) {
582 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
586 if (config.verbose) {
587 const char *version, *hn;
589 if (!(version = avahi_client_get_version_string(client))) {
590 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
594 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
595 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
599 fprintf(stderr, "Server version: %s; Host name: %s\n\n", version, hn);
600 fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
606 browse_service_type(&config, config.stype, config.domain);
608 avahi_simple_poll_loop(simple_poll);
617 remove_service(&config, services);
620 avahi_client_free(client);
625 avahi_simple_poll_free(simple_poll);
627 avahi_free(config.domain);
628 avahi_free(config.stype);
630 avahi_string_list_free(browsed_types);