2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
30 #include <sys/socket.h>
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-common/i18n.h>
40 #include <avahi-client/client.h>
41 #include <avahi-client/lookup.h>
49 COMMAND_RESOLVE_HOST_NAME,
50 COMMAND_RESOLVE_ADDRESS
53 typedef struct Config {
59 static AvahiSimplePoll *simple_poll = NULL;
60 static AvahiClient *client = NULL;
62 static int n_resolving = 0;
64 static void host_name_resolver_callback(
65 AvahiHostNameResolver *r,
66 AVAHI_GCC_UNUSED AvahiIfIndex interface,
67 AVAHI_GCC_UNUSED AvahiProtocol protocol,
68 AvahiResolverEvent event,
70 const AvahiAddress *a,
71 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
72 AVAHI_GCC_UNUSED void *userdata) {
77 case AVAHI_RESOLVER_FOUND: {
78 char address[AVAHI_ADDRESS_STR_MAX];
80 avahi_address_snprint(address, sizeof(address), a);
82 printf("%s\t%s\n", name, address);
87 case AVAHI_RESOLVER_FAILURE:
89 fprintf(stderr, _("Failed to resolve host name '%s': %s\n"), name, avahi_strerror(avahi_client_errno(client)));
94 avahi_host_name_resolver_free(r);
96 assert(n_resolving > 0);
100 avahi_simple_poll_quit(simple_poll);
103 static void address_resolver_callback(
104 AvahiAddressResolver *r,
105 AVAHI_GCC_UNUSED AvahiIfIndex interface,
106 AVAHI_GCC_UNUSED AvahiProtocol protocol,
107 AvahiResolverEvent event,
108 const AvahiAddress *a,
110 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
111 AVAHI_GCC_UNUSED void *userdata) {
113 char address[AVAHI_ADDRESS_STR_MAX];
116 avahi_address_snprint(address, sizeof(address), a);
119 case AVAHI_RESOLVER_FOUND:
121 printf("%s\t%s\n", address, name);
124 case AVAHI_RESOLVER_FAILURE:
126 fprintf(stderr, _("Failed to resolve address '%s': %s\n"), address, avahi_strerror(avahi_client_errno(client)));
131 avahi_address_resolver_free(r);
133 assert(n_resolving > 0);
136 if (n_resolving <= 0)
137 avahi_simple_poll_quit(simple_poll);
140 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
142 case AVAHI_CLIENT_FAILURE:
143 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
144 avahi_simple_poll_quit(simple_poll);
147 case AVAHI_CLIENT_S_REGISTERING:
148 case AVAHI_CLIENT_S_RUNNING:
149 case AVAHI_CLIENT_S_COLLISION:
150 case AVAHI_CLIENT_CONNECTING:
155 static void help(FILE *f, const char *argv0) {
157 _("%s [options] %s <host name ...>\n"
158 "%s [options] %s <address ... >\n\n"
159 " -h --help Show this help\n"
160 " -V --version Show version\n"
161 " -n --name Resolve host name\n"
162 " -a --address Resolve address\n"
163 " -v --verbose Enable verbose mode\n"
164 " -6 Lookup IPv6 address\n"
165 " -4 Lookup IPv4 address\n"),
166 argv0, strstr(argv0, "host-name") ? "[-n]" : "-n",
167 argv0, strstr(argv0, "address") ? "[-a]" : "-a");
170 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
173 static const struct option long_options[] = {
174 { "help", no_argument, NULL, 'h' },
175 { "version", no_argument, NULL, 'V' },
176 { "name", no_argument, NULL, 'n' },
177 { "address", no_argument, NULL, 'a' },
178 { "verbose", no_argument, NULL, 'v' },
184 c->command = strstr(argv0, "address") ? COMMAND_RESOLVE_ADDRESS : (strstr(argv0, "host-name") ? COMMAND_RESOLVE_HOST_NAME : COMMAND_UNSPEC);
185 c->proto = AVAHI_PROTO_UNSPEC;
188 while ((o = getopt_long(argc, argv, "hVnav46", long_options, NULL)) >= 0) {
192 c->command = COMMAND_HELP;
195 c->command = COMMAND_VERSION;
198 c->command = COMMAND_RESOLVE_HOST_NAME;
201 c->command = COMMAND_RESOLVE_ADDRESS;
207 c->proto = AVAHI_PROTO_INET;
210 c->proto = AVAHI_PROTO_INET6;
217 if (c->command == COMMAND_RESOLVE_ADDRESS || c->command == COMMAND_RESOLVE_HOST_NAME) {
218 if (optind >= argc) {
219 fprintf(stderr, _("Too few arguments\n"));
227 int main(int argc, char *argv[]) {
233 setlocale(LC_ALL, "");
235 if ((argv0 = strrchr(argv[0], '/')))
240 if (parse_command_line(&config, argv0, argc, argv) < 0)
243 switch (config.command) {
246 fprintf(stderr, _("No command specified.\n"));
254 case COMMAND_VERSION:
255 printf("%s "PACKAGE_VERSION"\n", argv0);
259 case COMMAND_RESOLVE_HOST_NAME:
260 case COMMAND_RESOLVE_ADDRESS: {
263 if (!(simple_poll = avahi_simple_poll_new())) {
264 fprintf(stderr, _("Failed to create simple poll object.\n"));
268 if (sigint_install(simple_poll) < 0)
271 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
272 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
276 if (config.verbose) {
277 const char *version, *hn;
279 if (!(version = avahi_client_get_version_string(client))) {
280 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
284 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
285 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
289 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
294 for (i = optind; i < argc; i++) {
296 if (config.command == COMMAND_RESOLVE_HOST_NAME) {
298 if (!(avahi_host_name_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, argv[i], config.proto, 0, host_name_resolver_callback, NULL))) {
299 fprintf(stderr, _("Failed to create host name resolver: %s\n"), avahi_strerror(avahi_client_errno(client)));
306 assert(config.command == COMMAND_RESOLVE_ADDRESS);
308 if (!avahi_address_parse(argv[i], AVAHI_PROTO_UNSPEC, &a)) {
309 fprintf(stderr, _("Failed to parse address '%s'\n"), argv[i]);
313 if (!(avahi_address_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &a, 0, address_resolver_callback, NULL))) {
314 fprintf(stderr, _("Failed to create address resolver: %s\n"), avahi_strerror(avahi_client_errno(client)));
322 avahi_simple_poll_loop(simple_poll);
331 avahi_client_free(client);
336 avahi_simple_poll_free(simple_poll);