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-common/i18n.h>
42 #include <avahi-client/client.h>
43 #include <avahi-client/lookup.h>
51 COMMAND_RESOLVE_HOST_NAME,
52 COMMAND_RESOLVE_ADDRESS
55 typedef struct Config {
61 static AvahiSimplePoll *simple_poll = NULL;
62 static AvahiClient *client = NULL;
64 static int n_resolving = 0;
66 static void host_name_resolver_callback(
67 AvahiHostNameResolver *r,
68 AVAHI_GCC_UNUSED AvahiIfIndex interface,
69 AVAHI_GCC_UNUSED AvahiProtocol protocol,
70 AvahiResolverEvent event,
72 const AvahiAddress *a,
73 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
74 AVAHI_GCC_UNUSED void *userdata) {
79 case AVAHI_RESOLVER_FOUND: {
80 char address[AVAHI_ADDRESS_STR_MAX];
82 avahi_address_snprint(address, sizeof(address), a);
84 printf("%s\t%s\n", name, address);
89 case AVAHI_RESOLVER_FAILURE:
91 fprintf(stderr, _("Failed to resolve host name '%s': %s\n"), name, avahi_strerror(avahi_client_errno(client)));
96 avahi_host_name_resolver_free(r);
98 assert(n_resolving > 0);
101 if (n_resolving <= 0)
102 avahi_simple_poll_quit(simple_poll);
105 static void address_resolver_callback(
106 AvahiAddressResolver *r,
107 AVAHI_GCC_UNUSED AvahiIfIndex interface,
108 AVAHI_GCC_UNUSED AvahiProtocol protocol,
109 AvahiResolverEvent event,
110 const AvahiAddress *a,
112 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
113 AVAHI_GCC_UNUSED void *userdata) {
115 char address[AVAHI_ADDRESS_STR_MAX];
118 avahi_address_snprint(address, sizeof(address), a);
121 case AVAHI_RESOLVER_FOUND:
123 printf("%s\t%s\n", address, name);
126 case AVAHI_RESOLVER_FAILURE:
128 fprintf(stderr, _("Failed to resolve address '%s': %s\n"), address, avahi_strerror(avahi_client_errno(client)));
133 avahi_address_resolver_free(r);
135 assert(n_resolving > 0);
138 if (n_resolving <= 0)
139 avahi_simple_poll_quit(simple_poll);
142 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
144 case AVAHI_CLIENT_FAILURE:
145 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
146 avahi_simple_poll_quit(simple_poll);
149 case AVAHI_CLIENT_S_REGISTERING:
150 case AVAHI_CLIENT_S_RUNNING:
151 case AVAHI_CLIENT_S_COLLISION:
152 case AVAHI_CLIENT_CONNECTING:
157 static void help(FILE *f, const char *argv0) {
159 _("%s [options] %s <host name ...>\n"
160 "%s [options] %s <address ... >\n\n"
161 " -h --help Show this help\n"
162 " -V --version Show version\n"
163 " -n --name Resolve host name\n"
164 " -a --address Resolve address\n"
165 " -v --verbose Enable verbose mode\n"
166 " -6 Lookup IPv6 address\n"
167 " -4 Lookup IPv4 address\n"),
168 argv0, strstr(argv0, "host-name") ? "[-n]" : "-n",
169 argv0, strstr(argv0, "address") ? "[-a]" : "-a");
172 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
175 static const struct option long_options[] = {
176 { "help", no_argument, NULL, 'h' },
177 { "version", no_argument, NULL, 'V' },
178 { "name", no_argument, NULL, 'n' },
179 { "address", no_argument, NULL, 'a' },
180 { "verbose", no_argument, NULL, 'v' },
186 c->command = strstr(argv0, "address") ? COMMAND_RESOLVE_ADDRESS : (strstr(argv0, "host-name") ? COMMAND_RESOLVE_HOST_NAME : COMMAND_UNSPEC);
187 c->proto = AVAHI_PROTO_UNSPEC;
190 while ((o = getopt_long(argc, argv, "hVnav46", long_options, NULL)) >= 0) {
194 c->command = COMMAND_HELP;
197 c->command = COMMAND_VERSION;
200 c->command = COMMAND_RESOLVE_HOST_NAME;
203 c->command = COMMAND_RESOLVE_ADDRESS;
209 c->proto = AVAHI_PROTO_INET;
212 c->proto = AVAHI_PROTO_INET6;
219 if (c->command == COMMAND_RESOLVE_ADDRESS || c->command == COMMAND_RESOLVE_HOST_NAME) {
220 if (optind >= argc) {
221 fprintf(stderr, _("Too few arguments\n"));
229 int main(int argc, char *argv[]) {
235 setlocale(LC_ALL, "");
237 if ((argv0 = strrchr(argv[0], '/')))
242 if (parse_command_line(&config, argv0, argc, argv) < 0)
245 switch (config.command) {
248 fprintf(stderr, _("No command specified.\n"));
256 case COMMAND_VERSION:
257 printf("%s "PACKAGE_VERSION"\n", argv0);
261 case COMMAND_RESOLVE_HOST_NAME:
262 case COMMAND_RESOLVE_ADDRESS: {
265 if (!(simple_poll = avahi_simple_poll_new())) {
266 fprintf(stderr, _("Failed to create simple poll object.\n"));
270 if (sigint_install(simple_poll) < 0)
273 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
274 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
278 if (config.verbose) {
279 const char *version, *hn;
281 if (!(version = avahi_client_get_version_string(client))) {
282 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
286 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
287 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
291 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
296 for (i = optind; i < argc; i++) {
298 if (config.command == COMMAND_RESOLVE_HOST_NAME) {
300 if (!(avahi_host_name_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, argv[i], config.proto, 0, host_name_resolver_callback, NULL))) {
301 fprintf(stderr, _("Failed to create host name resolver: %s\n"), avahi_strerror(avahi_client_errno(client)));
308 assert(config.command == COMMAND_RESOLVE_ADDRESS);
310 if (!avahi_address_parse(argv[i], AVAHI_PROTO_UNSPEC, &a)) {
311 fprintf(stderr, _("Failed to parse address '%s'\n"), argv[i]);
315 if (!(avahi_address_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &a, 0, address_resolver_callback, NULL))) {
316 fprintf(stderr, _("Failed to create address resolver: %s\n"), avahi_strerror(avahi_client_errno(client)));
324 avahi_simple_poll_loop(simple_poll);
333 avahi_client_free(client);
338 avahi_simple_poll_free(simple_poll);