-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/gnunet-resolver.c
21  * @brief tool to test resolver
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_resolver_service.h"
27
28 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
29
30 /**
31  * Flag for reverse lookup.
32  */
33 static int reverse;
34
35
36 /**
37  * Prints each hostname obtained from DNS.
38  *
39  * @param cls closure (unused)
40  * @param hostname one of the names for the host, NULL
41  *        on the last call to the callback
42  */
43 static void
44 print_hostname (void *cls,
45                 const char *hostname)
46 {
47   (void) cls;
48   if (NULL == hostname)
49     return;
50   FPRINTF (stdout,
51            "%s\n",
52            hostname);
53 }
54
55
56 /**
57  * Callback function to display address.
58  *
59  * @param cls closure (unused)
60  * @param addr one of the addresses of the host, NULL for the last address
61  * @param addrlen length of the address
62  */
63 static void
64 print_sockaddr (void *cls,
65                 const struct sockaddr *addr,
66                 socklen_t addrlen)
67 {
68   (void) cls;
69   if (NULL == addr)
70     return;
71   FPRINTF (stdout,
72            "%s\n",
73            GNUNET_a2s (addr,
74                        addrlen));
75 }
76
77
78 /**
79  * Main function that will be run by the scheduler.
80  *
81  * @param cls closure
82  * @param args remaining command-line arguments
83  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
84  * @param cfg configuration
85  */
86 static void
87 run (void *cls,
88      char *const *args,
89      const char *cfgfile,
90      const struct GNUNET_CONFIGURATION_Handle *cfg)
91 {
92   const struct sockaddr *sa;
93   socklen_t salen;
94   struct sockaddr_in v4;
95   struct sockaddr_in6 v6;
96
97   (void) cls;
98   (void) cfgfile;
99   (void) cfg;
100   if (NULL == args[0])
101     return;
102   if (! reverse)
103   {
104     GNUNET_RESOLVER_ip_get (args[0],
105                             AF_UNSPEC,
106                             GET_TIMEOUT,
107                             &print_sockaddr,
108                             NULL);
109     return;
110   }
111
112   sa = NULL;
113   memset (&v4, 0, sizeof (v4));
114   v4.sin_family = AF_INET;
115 #if HAVE_SOCKADDR_IN_SIN_LEN
116   v4.sin_len = sizeof (v4);
117 #endif
118   if (1 == inet_pton (AF_INET,
119                       args[0],
120                       &v4.sin_addr))
121   {
122     sa = (struct sockaddr *) &v4;
123     salen = sizeof (v4);
124   }
125   memset (&v6, 0, sizeof (v6));
126   v6.sin6_family = AF_INET6;
127 #if HAVE_SOCKADDR_IN_SIN_LEN
128   v6.sin6_len = sizeof (v6);
129 #endif
130   if (1 == inet_pton (AF_INET6,
131                       args[0],
132                       &v6.sin6_addr))
133   {
134     sa = (struct sockaddr *) &v6;
135     salen = sizeof (v6);
136   }
137   if (NULL == sa)
138   {
139     fprintf (stderr,
140              "`%s' is not a valid IP: %s\n",
141              args[0],
142              strerror (errno));
143     return;
144   }
145   GNUNET_RESOLVER_hostname_get (sa, salen,
146                                 GNUNET_YES,
147                                 GET_TIMEOUT,
148                                 &print_hostname,
149                                 NULL);
150 }
151
152
153 /**
154  * The main function to access GNUnet's DNS resolver.
155  *
156  * @param argc number of arguments from the command line
157  * @param argv command line arguments
158  * @return 0 ok, 1 on error
159  */
160 int
161 main (int argc, char *const *argv)
162 {
163   struct GNUNET_GETOPT_CommandLineOption options[] = {
164     GNUNET_GETOPT_option_flag ('r',
165                                   "reverse",
166                                   gettext_noop ("perform a reverse lookup"),
167                                   &reverse),
168     GNUNET_GETOPT_OPTION_END
169   };
170   int ret;
171
172   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
173     return 2;
174
175   ret = (GNUNET_OK ==
176          GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]",
177                              gettext_noop ("Use build-in GNUnet stub resolver"),
178                              options, &run, NULL)) ? 0 : 1;
179   GNUNET_free ((void*) argv);
180   return ret;
181 }
182
183 /* end of gnunet-resolver.c */