diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-09-27 23:20:33 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-09-27 23:23:55 +0200 |
commit | 9388822eb43827ee4f343a881d86a6225beca939 (patch) | |
tree | 1b73f861e4f53bb38fd64d62652cdce2bcb343da /src/gns/gnunet-gns.c | |
parent | c8ed84e481b4abfb7d4ed844a4d74b979917c466 (diff) | |
download | gnunet-9388822eb43827ee4f343a881d86a6225beca939.tar.gz gnunet-9388822eb43827ee4f343a881d86a6225beca939.zip |
do not attempt name resolution or terminating a peer in gnunet-arm if the peer is not running
Diffstat (limited to 'src/gns/gnunet-gns.c')
-rw-r--r-- | src/gns/gnunet-gns.c | 266 |
1 files changed, 142 insertions, 124 deletions
diff --git a/src/gns/gnunet-gns.c b/src/gns/gnunet-gns.c index 352ccce04..1ddcbf090 100644 --- a/src/gns/gnunet-gns.c +++ b/src/gns/gnunet-gns.c | |||
@@ -90,24 +90,24 @@ static int global_ret; | |||
90 | * @param cls unused | 90 | * @param cls unused |
91 | */ | 91 | */ |
92 | static void | 92 | static void |
93 | do_shutdown(void *cls) | 93 | do_shutdown (void *cls) |
94 | { | 94 | { |
95 | (void)cls; | 95 | (void) cls; |
96 | if (NULL != to_task) | 96 | if (NULL != to_task) |
97 | { | 97 | { |
98 | GNUNET_SCHEDULER_cancel(to_task); | 98 | GNUNET_SCHEDULER_cancel (to_task); |
99 | to_task = NULL; | 99 | to_task = NULL; |
100 | } | 100 | } |
101 | if (NULL != lr) | 101 | if (NULL != lr) |
102 | { | 102 | { |
103 | GNUNET_GNS_lookup_with_tld_cancel(lr); | 103 | GNUNET_GNS_lookup_with_tld_cancel (lr); |
104 | lr = NULL; | 104 | lr = NULL; |
105 | } | 105 | } |
106 | if (NULL != gns) | 106 | if (NULL != gns) |
107 | { | 107 | { |
108 | GNUNET_GNS_disconnect(gns); | 108 | GNUNET_GNS_disconnect (gns); |
109 | gns = NULL; | 109 | gns = NULL; |
110 | } | 110 | } |
111 | } | 111 | } |
112 | 112 | ||
113 | /** | 113 | /** |
@@ -116,11 +116,11 @@ do_shutdown(void *cls) | |||
116 | * @param cls unused | 116 | * @param cls unused |
117 | */ | 117 | */ |
118 | static void | 118 | static void |
119 | do_timeout(void* cls) | 119 | do_timeout (void*cls) |
120 | { | 120 | { |
121 | to_task = NULL; | 121 | to_task = NULL; |
122 | global_ret = 3; //Timeout | 122 | global_ret = 3; // Timeout |
123 | GNUNET_SCHEDULER_shutdown(); | 123 | GNUNET_SCHEDULER_shutdown (); |
124 | } | 124 | } |
125 | 125 | ||
126 | /** | 126 | /** |
@@ -132,10 +132,10 @@ do_timeout(void* cls) | |||
132 | * @param rd array of @a rd_count records with the results | 132 | * @param rd array of @a rd_count records with the results |
133 | */ | 133 | */ |
134 | static void | 134 | static void |
135 | process_lookup_result(void *cls, | 135 | process_lookup_result (void *cls, |
136 | int was_gns, | 136 | int was_gns, |
137 | uint32_t rd_count, | 137 | uint32_t rd_count, |
138 | const struct GNUNET_GNSRECORD_Data *rd) | 138 | const struct GNUNET_GNSRECORD_Data *rd) |
139 | { | 139 | { |
140 | const char *name = cls; | 140 | const char *name = cls; |
141 | const char *typename; | 141 | const char *typename; |
@@ -143,41 +143,41 @@ process_lookup_result(void *cls, | |||
143 | 143 | ||
144 | lr = NULL; | 144 | lr = NULL; |
145 | if (GNUNET_NO == was_gns) | 145 | if (GNUNET_NO == was_gns) |
146 | { | 146 | { |
147 | global_ret = 4; /* not for GNS */ | 147 | global_ret = 4; /* not for GNS */ |
148 | GNUNET_SCHEDULER_shutdown(); | 148 | GNUNET_SCHEDULER_shutdown (); |
149 | return; | 149 | return; |
150 | } | 150 | } |
151 | if (!raw) | 151 | if (! raw) |
152 | { | 152 | { |
153 | if (0 == rd_count) | 153 | if (0 == rd_count) |
154 | printf("No results.\n"); | 154 | printf ("No results.\n"); |
155 | else | 155 | else |
156 | printf("%s:\n", name); | 156 | printf ("%s:\n", name); |
157 | } | 157 | } |
158 | for (uint32_t i = 0; i < rd_count; i++) | 158 | for (uint32_t i = 0; i < rd_count; i++) |
159 | { | ||
160 | if ((rd[i].record_type != rtype) && (GNUNET_GNSRECORD_TYPE_ANY != rtype)) | ||
161 | continue; | ||
162 | typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); | ||
163 | string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
164 | rd[i].data, | ||
165 | rd[i].data_size); | ||
166 | if (NULL == string_val) | ||
159 | { | 167 | { |
160 | if ((rd[i].record_type != rtype) && (GNUNET_GNSRECORD_TYPE_ANY != rtype)) | 168 | fprintf (stderr, |
161 | continue; | 169 | "Record %u of type %d malformed, skipping\n", |
162 | typename = GNUNET_GNSRECORD_number_to_typename(rd[i].record_type); | 170 | (unsigned int) i, |
163 | string_val = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, | 171 | (int) rd[i].record_type); |
164 | rd[i].data, | 172 | continue; |
165 | rd[i].data_size); | ||
166 | if (NULL == string_val) | ||
167 | { | ||
168 | fprintf(stderr, | ||
169 | "Record %u of type %d malformed, skipping\n", | ||
170 | (unsigned int)i, | ||
171 | (int)rd[i].record_type); | ||
172 | continue; | ||
173 | } | ||
174 | if (raw) | ||
175 | printf("%s\n", string_val); | ||
176 | else | ||
177 | printf("Got `%s' record: %s\n", typename, string_val); | ||
178 | GNUNET_free(string_val); | ||
179 | } | 173 | } |
180 | GNUNET_SCHEDULER_shutdown(); | 174 | if (raw) |
175 | printf ("%s\n", string_val); | ||
176 | else | ||
177 | printf ("Got `%s' record: %s\n", typename, string_val); | ||
178 | GNUNET_free (string_val); | ||
179 | } | ||
180 | GNUNET_SCHEDULER_shutdown (); | ||
181 | } | 181 | } |
182 | 182 | ||
183 | 183 | ||
@@ -190,53 +190,69 @@ process_lookup_result(void *cls, | |||
190 | * @param c configuration | 190 | * @param c configuration |
191 | */ | 191 | */ |
192 | static void | 192 | static void |
193 | run(void *cls, | 193 | run (void *cls, |
194 | char *const *args, | 194 | char *const *args, |
195 | const char *cfgfile, | 195 | const char *cfgfile, |
196 | const struct GNUNET_CONFIGURATION_Handle *c) | 196 | const struct GNUNET_CONFIGURATION_Handle *c) |
197 | { | 197 | { |
198 | (void)cls; | 198 | (void) cls; |
199 | (void)args; | 199 | (void) args; |
200 | (void)cfgfile; | 200 | (void) cfgfile; |
201 | 201 | ||
202 | cfg = c; | 202 | cfg = c; |
203 | to_task = NULL; | 203 | to_task = NULL; |
204 | if (GNUNET_OK != GNUNET_DNSPARSER_check_name(lookup_name)) | 204 | if (GNUNET_OK != GNUNET_DNSPARSER_check_name (lookup_name)) |
205 | { | 205 | { |
206 | fprintf(stderr, _("`%s' is not a valid domain name\n"), lookup_name); | 206 | fprintf (stderr, |
207 | global_ret = 3; | 207 | _ ("`%s' is not a valid domain name\n"), |
208 | return; | 208 | lookup_name); |
209 | } | 209 | global_ret = 3; |
210 | to_task = GNUNET_SCHEDULER_add_delayed(timeout, &do_timeout, NULL); | 210 | return; |
211 | gns = GNUNET_GNS_connect(cfg); | 211 | } |
212 | if (GNUNET_YES != | ||
213 | GNUNET_CLIENT_test (cfg, | ||
214 | "arm")) | ||
215 | { | ||
216 | fprintf (stderr, | ||
217 | _ ("GNUnet not running\n")); | ||
218 | global_ret = 2; | ||
219 | return; | ||
220 | } | ||
221 | to_task = GNUNET_SCHEDULER_add_delayed (timeout, | ||
222 | &do_timeout, | ||
223 | NULL); | ||
224 | gns = GNUNET_GNS_connect (cfg); | ||
212 | if (NULL == gns) | 225 | if (NULL == gns) |
213 | { | 226 | { |
214 | fprintf(stderr, _("Failed to connect to GNS\n")); | 227 | fprintf (stderr, |
215 | global_ret = 2; | 228 | _ ("Failed to connect to GNS\n")); |
216 | return; | 229 | global_ret = 2; |
217 | } | 230 | return; |
218 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); | 231 | } |
232 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
233 | NULL); | ||
219 | if (NULL != lookup_type) | 234 | if (NULL != lookup_type) |
220 | rtype = GNUNET_GNSRECORD_typename_to_number(lookup_type); | 235 | rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type); |
221 | else | 236 | else |
222 | rtype = GNUNET_DNSPARSER_TYPE_A; | 237 | rtype = GNUNET_DNSPARSER_TYPE_A; |
223 | if (UINT32_MAX == rtype) | 238 | if (UINT32_MAX == rtype) |
224 | { | 239 | { |
225 | fprintf(stderr, _("Invalid typename specified, assuming `ANY'\n")); | 240 | fprintf (stderr, |
226 | rtype = GNUNET_GNSRECORD_TYPE_ANY; | 241 | _ ("Invalid typename specified, assuming `ANY'\n")); |
227 | } | 242 | rtype = GNUNET_GNSRECORD_TYPE_ANY; |
228 | lr = GNUNET_GNS_lookup_with_tld(gns, | 243 | } |
229 | lookup_name, | 244 | lr = GNUNET_GNS_lookup_with_tld (gns, |
230 | rtype, | 245 | lookup_name, |
231 | GNUNET_GNS_LO_DEFAULT, | 246 | rtype, |
232 | &process_lookup_result, | 247 | GNUNET_GNS_LO_DEFAULT, |
233 | lookup_name); | 248 | &process_lookup_result, |
249 | lookup_name); | ||
234 | if (NULL == lr) | 250 | if (NULL == lr) |
235 | { | 251 | { |
236 | global_ret = 2; | 252 | global_ret = 2; |
237 | GNUNET_SCHEDULER_shutdown(); | 253 | GNUNET_SCHEDULER_shutdown (); |
238 | return; | 254 | return; |
239 | } | 255 | } |
240 | } | 256 | } |
241 | 257 | ||
242 | 258 | ||
@@ -248,48 +264,50 @@ run(void *cls, | |||
248 | * @return 0 ok, 1 on error | 264 | * @return 0 ok, 1 on error |
249 | */ | 265 | */ |
250 | int | 266 | int |
251 | main(int argc, char *const *argv) | 267 | main (int argc, char *const *argv) |
252 | { | 268 | { |
253 | timeout = GNUNET_TIME_UNIT_FOREVER_REL; | 269 | timeout = GNUNET_TIME_UNIT_FOREVER_REL; |
254 | struct GNUNET_GETOPT_CommandLineOption options[] = | 270 | struct GNUNET_GETOPT_CommandLineOption options[] = |
255 | { GNUNET_GETOPT_option_mandatory( | 271 | { GNUNET_GETOPT_option_mandatory ( |
256 | GNUNET_GETOPT_option_string('u', | 272 | GNUNET_GETOPT_option_string ('u', |
257 | "lookup", | 273 | "lookup", |
258 | "NAME", | 274 | "NAME", |
259 | gettext_noop( | 275 | gettext_noop ( |
260 | "Lookup a record for the given name"), | 276 | "Lookup a record for the given name"), |
261 | &lookup_name)), | 277 | &lookup_name)), |
262 | GNUNET_GETOPT_option_string('t', | 278 | GNUNET_GETOPT_option_string ('t', |
263 | "type", | 279 | "type", |
264 | "TYPE", | 280 | "TYPE", |
265 | gettext_noop( | 281 | gettext_noop ( |
266 | "Specify the type of the record to lookup"), | 282 | "Specify the type of the record to lookup"), |
267 | &lookup_type), | 283 | &lookup_type), |
268 | GNUNET_GETOPT_option_relative_time('T', | 284 | GNUNET_GETOPT_option_relative_time ('T', |
269 | "timeout", | 285 | "timeout", |
270 | "TIMEOUT", | 286 | "TIMEOUT", |
271 | gettext_noop( | 287 | gettext_noop ( |
272 | "Specify a timeout for the lookup"), | 288 | "Specify a timeout for the lookup"), |
273 | &timeout), | 289 | &timeout), |
274 | GNUNET_GETOPT_option_flag('r', | 290 | GNUNET_GETOPT_option_flag ('r', |
275 | "raw", | 291 | "raw", |
276 | gettext_noop("No unneeded output"), | 292 | gettext_noop ("No unneeded output"), |
277 | &raw), | 293 | &raw), |
278 | GNUNET_GETOPT_OPTION_END }; | 294 | GNUNET_GETOPT_OPTION_END }; |
279 | int ret; | 295 | int ret; |
280 | 296 | ||
281 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) | 297 | if (GNUNET_OK != |
298 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
299 | &argc, &argv)) | ||
282 | return 2; | 300 | return 2; |
283 | 301 | ||
284 | GNUNET_log_setup("gnunet-gns", "WARNING", NULL); | 302 | GNUNET_log_setup ("gnunet-gns", "WARNING", NULL); |
285 | ret = GNUNET_PROGRAM_run(argc, | 303 | ret = GNUNET_PROGRAM_run (argc, |
286 | argv, | 304 | argv, |
287 | "gnunet-gns", | 305 | "gnunet-gns", |
288 | _("GNUnet GNS resolver tool"), | 306 | _ ("GNUnet GNS resolver tool"), |
289 | options, | 307 | options, |
290 | &run, | 308 | &run, |
291 | NULL); | 309 | NULL); |
292 | GNUNET_free((void *)argv); | 310 | GNUNET_free ((void *) argv); |
293 | if (GNUNET_OK != ret) | 311 | if (GNUNET_OK != ret) |
294 | return 1; | 312 | return 1; |
295 | return global_ret; | 313 | return global_ret; |