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