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_RESOLVE_HOST_NAME,
54 COMMAND_RESOLVE_ADDRESS
57 typedef struct Config {
63 static AvahiSimplePoll *simple_poll = NULL;
64 static AvahiClient *client = NULL;
66 static int n_resolving = 0;
68 static void host_name_resolver_callback(
69 AvahiHostNameResolver *r,
70 AVAHI_GCC_UNUSED AvahiIfIndex interface,
71 AVAHI_GCC_UNUSED AvahiProtocol protocol,
72 AvahiResolverEvent event,
74 const AvahiAddress *a,
75 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
76 AVAHI_GCC_UNUSED void *userdata) {
81 case AVAHI_RESOLVER_FOUND: {
82 char address[AVAHI_ADDRESS_STR_MAX];
84 avahi_address_snprint(address, sizeof(address), a);
86 printf("%s\t%s\n", name, address);
91 case AVAHI_RESOLVER_FAILURE:
93 fprintf(stderr, "Failed to resolve host name '%s': %s\n", name, avahi_strerror(avahi_client_errno(client)));
98 avahi_host_name_resolver_free(r);
100 assert(n_resolving > 0);
103 if (n_resolving <= 0)
104 avahi_simple_poll_quit(simple_poll);
107 static void address_resolver_callback(
108 AvahiAddressResolver *r,
109 AVAHI_GCC_UNUSED AvahiIfIndex interface,
110 AVAHI_GCC_UNUSED AvahiProtocol protocol,
111 AvahiResolverEvent event,
112 const AvahiAddress *a,
114 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
115 AVAHI_GCC_UNUSED void *userdata) {
117 char address[AVAHI_ADDRESS_STR_MAX];
120 avahi_address_snprint(address, sizeof(address), a);
123 case AVAHI_RESOLVER_FOUND:
125 printf("%s\t%s\n", address, name);
128 case AVAHI_RESOLVER_FAILURE:
130 fprintf(stderr, "Failed to resolve address '%s': %s\n", address, avahi_strerror(avahi_client_errno(client)));
135 avahi_address_resolver_free(r);
137 assert(n_resolving > 0);
140 if (n_resolving <= 0)
141 avahi_simple_poll_quit(simple_poll);
144 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
146 case AVAHI_CLIENT_FAILURE:
147 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
148 avahi_simple_poll_quit(simple_poll);
151 case AVAHI_CLIENT_S_REGISTERING:
152 case AVAHI_CLIENT_S_RUNNING:
153 case AVAHI_CLIENT_S_COLLISION:
154 case AVAHI_CLIENT_CONNECTING:
159 static void help(FILE *f, const char *argv0) {
161 "%s [options] %s <name ...>\n"
162 "%s [options] %s <address ... >\n\n"
163 " -h --help Show this help\n"
164 " -V --version Show version\n"
165 " -n --name Resolve host name\n"
166 " -a --address Resolve address\n"
167 " -v --verbose Enable verbose mode\n"
168 " -6 Lookup IPv6 address\n"
169 " -4 Lookup IPv4 address\n"
171 argv0, strstr(argv0, "address") ? "-n" : "[-n]",
172 argv0, strstr(argv0, "address") ? "[-a]" : "-a");
175 static int parse_command_line(Config *c, int argc, char *argv[]) {
178 static const struct option long_options[] = {
179 { "help", no_argument, NULL, 'h' },
180 { "version", no_argument, NULL, 'V' },
181 { "name", no_argument, NULL, 'n' },
182 { "address", no_argument, NULL, 'a' },
183 { "verbose", no_argument, NULL, 'v' },
189 c->command = strstr(argv[0], "address") ? COMMAND_RESOLVE_ADDRESS : COMMAND_RESOLVE_HOST_NAME;
190 c->proto = AVAHI_PROTO_UNSPEC;
194 while ((o = getopt_long(argc, argv, "hVnav46", long_options, NULL)) >= 0) {
198 c->command = COMMAND_HELP;
201 c->command = COMMAND_VERSION;
204 c->command = COMMAND_RESOLVE_HOST_NAME;
207 c->command = COMMAND_RESOLVE_ADDRESS;
213 c->proto = AVAHI_PROTO_INET;
216 c->proto = AVAHI_PROTO_INET6;
219 fprintf(stderr, "Invalid command line argument: %c\n", o);
224 if (c->command == COMMAND_RESOLVE_ADDRESS || c->command == COMMAND_RESOLVE_HOST_NAME) {
225 if (optind >= argc) {
226 fprintf(stderr, "Too few arguments\n");
234 int main(int argc, char *argv[]) {
239 if ((argv0 = strrchr(argv[0], '/')))
244 if (parse_command_line(&config, argc, argv) < 0)
247 switch (config.command) {
253 case COMMAND_VERSION:
254 printf("%s "PACKAGE_VERSION"\n", argv0);
258 case COMMAND_RESOLVE_HOST_NAME:
259 case COMMAND_RESOLVE_ADDRESS: {
262 if (!(simple_poll = avahi_simple_poll_new())) {
263 fprintf(stderr, "Failed to create simple poll object.\n");
267 if (sigint_install(simple_poll) < 0)
270 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
271 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
275 if (config.verbose) {
276 const char *version, *hn;
278 if (!(version = avahi_client_get_version_string(client))) {
279 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
283 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
284 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
288 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
293 for (i = optind; i < argc; i++) {
295 if (config.command == COMMAND_RESOLVE_HOST_NAME) {
297 if (!(avahi_host_name_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, argv[i], config.proto, 0, host_name_resolver_callback, NULL))) {
298 fprintf(stderr, "Failed to create host name resolver: %s\n", avahi_strerror(avahi_client_errno(client)));
305 assert(config.command == COMMAND_RESOLVE_ADDRESS);
307 if (!avahi_address_parse(argv[i], AVAHI_PROTO_UNSPEC, &a)) {
308 fprintf(stderr, "Failed to parse address '%s'\n", argv[i]);
312 if (!(avahi_address_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &a, 0, address_resolver_callback, NULL))) {
313 fprintf(stderr, "Failed to create address resolver: %s\n", avahi_strerror(avahi_client_errno(client)));
321 avahi_simple_poll_loop(simple_poll);
331 avahi_client_free(client);
336 avahi_simple_poll_free(simple_poll);