first batch of license fixes (boring)
[oweals/gnunet.git] / src / util / gnunet-resolver.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15
16 /**
17  * @file util/gnunet-resolver.c
18  * @brief tool to test resolver
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_resolver_service.h"
24
25 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
26
27 /**
28  * Flag for reverse lookup.
29  */
30 static int reverse;
31
32
33 /**
34  * Prints each hostname obtained from DNS.
35  *
36  * @param cls closure (unused)
37  * @param hostname one of the names for the host, NULL
38  *        on the last call to the callback
39  */
40 static void
41 print_hostname (void *cls,
42                 const char *hostname)
43 {
44   (void) cls;
45   if (NULL == hostname)
46     return;
47   FPRINTF (stdout,
48            "%s\n",
49            hostname);
50 }
51
52
53 /**
54  * Callback function to display address.
55  *
56  * @param cls closure (unused)
57  * @param addr one of the addresses of the host, NULL for the last address
58  * @param addrlen length of the address
59  */
60 static void
61 print_sockaddr (void *cls,
62                 const struct sockaddr *addr,
63                 socklen_t addrlen)
64 {
65   (void) cls;
66   if (NULL == addr)
67     return;
68   FPRINTF (stdout,
69            "%s\n",
70            GNUNET_a2s (addr,
71                        addrlen));
72 }
73
74
75 /**
76  * Main function that will be run by the scheduler.
77  *
78  * @param cls closure
79  * @param args remaining command-line arguments
80  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
81  * @param cfg configuration
82  */
83 static void
84 run (void *cls,
85      char *const *args,
86      const char *cfgfile,
87      const struct GNUNET_CONFIGURATION_Handle *cfg)
88 {
89   const struct sockaddr *sa;
90   socklen_t salen;
91   struct sockaddr_in v4;
92   struct sockaddr_in6 v6;
93
94   (void) cls;
95   (void) cfgfile;
96   (void) cfg;
97   if (NULL == args[0])
98     return;
99   if (! reverse)
100   {
101     GNUNET_RESOLVER_ip_get (args[0],
102                             AF_UNSPEC,
103                             GET_TIMEOUT,
104                             &print_sockaddr,
105                             NULL);
106     return;
107   }
108
109   sa = NULL;
110   memset (&v4, 0, sizeof (v4));
111   v4.sin_family = AF_INET;
112 #if HAVE_SOCKADDR_IN_SIN_LEN
113   v4.sin_len = sizeof (v4);
114 #endif
115   if (1 == inet_pton (AF_INET,
116                       args[0],
117                       &v4.sin_addr))
118   {
119     sa = (struct sockaddr *) &v4;
120     salen = sizeof (v4);
121   }
122   memset (&v6, 0, sizeof (v6));
123   v6.sin6_family = AF_INET6;
124 #if HAVE_SOCKADDR_IN_SIN_LEN
125   v6.sin6_len = sizeof (v6);
126 #endif
127   if (1 == inet_pton (AF_INET6,
128                       args[0],
129                       &v6.sin6_addr))
130   {
131     sa = (struct sockaddr *) &v6;
132     salen = sizeof (v6);
133   }
134   if (NULL == sa)
135   {
136     fprintf (stderr,
137              "`%s' is not a valid IP: %s\n",
138              args[0],
139              strerror (errno));
140     return;
141   }
142   GNUNET_RESOLVER_hostname_get (sa, salen,
143                                 GNUNET_YES,
144                                 GET_TIMEOUT,
145                                 &print_hostname,
146                                 NULL);
147 }
148
149
150 /**
151  * The main function to access GNUnet's DNS resolver.
152  *
153  * @param argc number of arguments from the command line
154  * @param argv command line arguments
155  * @return 0 ok, 1 on error
156  */
157 int
158 main (int argc, char *const *argv)
159 {
160   struct GNUNET_GETOPT_CommandLineOption options[] = {
161     GNUNET_GETOPT_option_flag ('r',
162                                   "reverse",
163                                   gettext_noop ("perform a reverse lookup"),
164                                   &reverse),
165     GNUNET_GETOPT_OPTION_END
166   };
167   int ret;
168
169   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
170     return 2;
171
172   ret = (GNUNET_OK ==
173          GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]",
174                              gettext_noop ("Use build-in GNUnet stub resolver"),
175                              options, &run, NULL)) ? 0 : 1;
176   GNUNET_free ((void*) argv);
177   return ret;
178 }
179
180 /* end of gnunet-resolver.c */