aboutsummaryrefslogtreecommitdiff
path: root/src/nat-auto/gnunet-nat-auto.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat-auto/gnunet-nat-auto.c')
-rw-r--r--src/nat-auto/gnunet-nat-auto.c149
1 files changed, 61 insertions, 88 deletions
diff --git a/src/nat-auto/gnunet-nat-auto.c b/src/nat-auto/gnunet-nat-auto.c
index bd8f4ff45..106d60228 100644
--- a/src/nat-auto/gnunet-nat-auto.c
+++ b/src/nat-auto/gnunet-nat-auto.c
@@ -116,14 +116,9 @@ auto_conf_iter (void *cls,
116{ 116{
117 struct GNUNET_CONFIGURATION_Handle *new_cfg = cls; 117 struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
118 118
119 PRINTF ("%s: %s\n", 119 printf ("%s: %s\n", option, value);
120 option,
121 value);
122 if (NULL != new_cfg) 120 if (NULL != new_cfg)
123 GNUNET_CONFIGURATION_set_value_string (new_cfg, 121 GNUNET_CONFIGURATION_set_value_string (new_cfg, section, option, value);
124 section,
125 option,
126 value);
127} 122}
128 123
129 124
@@ -138,9 +133,9 @@ auto_conf_iter (void *cls,
138 */ 133 */
139static void 134static void
140auto_config_cb (void *cls, 135auto_config_cb (void *cls,
141 const struct GNUNET_CONFIGURATION_Handle *diff, 136 const struct GNUNET_CONFIGURATION_Handle *diff,
142 enum GNUNET_NAT_StatusCode result, 137 enum GNUNET_NAT_StatusCode result,
143 enum GNUNET_NAT_Type type) 138 enum GNUNET_NAT_Type type)
144{ 139{
145 const char *nat_type; 140 const char *nat_type;
146 char unknown_type[64]; 141 char unknown_type[64];
@@ -162,24 +157,21 @@ auto_config_cb (void *cls,
162 nat_type = "NAT but UPNP opened the ports"; 157 nat_type = "NAT but UPNP opened the ports";
163 break; 158 break;
164 default: 159 default:
165 SPRINTF (unknown_type, 160 sprintf (unknown_type, "NAT unknown, type %u", type);
166 "NAT unknown, type %u",
167 type);
168 nat_type = unknown_type; 161 nat_type = unknown_type;
169 break; 162 break;
170 } 163 }
171 164
172 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 165 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
173 "NAT status: %s/%s\n", 166 "NAT status: %s/%s\n",
174 GNUNET_NAT_AUTO_status2string (result), 167 GNUNET_NAT_AUTO_status2string (result),
175 nat_type); 168 nat_type);
176 169
177 if (NULL == diff) 170 if (NULL == diff)
178 return; 171 return;
179 172
180 /* Shortcut: if there are no changes suggested, bail out early. */ 173 /* Shortcut: if there are no changes suggested, bail out early. */
181 if (GNUNET_NO == 174 if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty (diff))
182 GNUNET_CONFIGURATION_is_dirty (diff))
183 { 175 {
184 test_finished (); 176 test_finished ();
185 return; 177 return;
@@ -190,7 +182,7 @@ auto_config_cb (void *cls,
190 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL; 182 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
191 183
192 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 184 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
193 _("Suggested configuration changes:\n")); 185 _ ("Suggested configuration changes:\n"));
194 GNUNET_CONFIGURATION_iterate_section_values (diff, 186 GNUNET_CONFIGURATION_iterate_section_values (diff,
195 "nat", 187 "nat",
196 &auto_conf_iter, 188 &auto_conf_iter,
@@ -202,29 +194,22 @@ auto_config_cb (void *cls,
202 { 194 {
203 struct GNUNET_CONFIGURATION_Handle *def_cfg; 195 struct GNUNET_CONFIGURATION_Handle *def_cfg;
204 196
205 GNUNET_CONFIGURATION_set_value_string (new_cfg, 197 GNUNET_CONFIGURATION_set_value_string (new_cfg, "ARM", "CONFIG", NULL);
206 "ARM",
207 "CONFIG",
208 NULL);
209 def_cfg = GNUNET_CONFIGURATION_create (); 198 def_cfg = GNUNET_CONFIGURATION_create ();
210 GNUNET_break (GNUNET_OK == 199 GNUNET_break (GNUNET_OK == GNUNET_CONFIGURATION_load (def_cfg, NULL));
211 GNUNET_CONFIGURATION_load (def_cfg,
212 NULL));
213 if (GNUNET_OK != 200 if (GNUNET_OK !=
214 GNUNET_CONFIGURATION_write_diffs (def_cfg, 201 GNUNET_CONFIGURATION_write_diffs (def_cfg, new_cfg, cfg_file))
215 new_cfg,
216 cfg_file))
217 { 202 {
218 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 203 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
219 _("Failed to write configuration to `%s'\n"), 204 _ ("Failed to write configuration to `%s'\n"),
220 cfg_file); 205 cfg_file);
221 global_ret = 1; 206 global_ret = 1;
222 } 207 }
223 else 208 else
224 { 209 {
225 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 210 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
226 _("Wrote updated configuration to `%s'\n"), 211 _ ("Wrote updated configuration to `%s'\n"),
227 cfg_file); 212 cfg_file);
228 } 213 }
229 GNUNET_CONFIGURATION_destroy (def_cfg); 214 GNUNET_CONFIGURATION_destroy (def_cfg);
230 } 215 }
@@ -243,12 +228,10 @@ auto_config_cb (void *cls,
243 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 228 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
244 */ 229 */
245static void 230static void
246test_report_cb (void *cls, 231test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result)
247 enum GNUNET_NAT_StatusCode result)
248{ 232{
249 nt = NULL; 233 nt = NULL;
250 PRINTF ("NAT test result: %s\n", 234 printf ("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string (result));
251 GNUNET_NAT_AUTO_status2string (result));
252 test_finished (); 235 test_finished ();
253} 236}
254 237
@@ -291,22 +274,18 @@ run (void *cls,
291 cfg_file = cfgfile; 274 cfg_file = cfgfile;
292 cfg = c; 275 cfg = c;
293 276
294 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 277 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
295 NULL);
296 278
297 if (do_auto) 279 if (do_auto)
298 { 280 {
299 ah = GNUNET_NAT_AUTO_autoconfig_start (c, 281 ah = GNUNET_NAT_AUTO_autoconfig_start (c, &auto_config_cb, NULL);
300 &auto_config_cb,
301 NULL);
302 } 282 }
303 283
304 if (use_tcp && use_udp) 284 if (use_tcp && use_udp)
305 { 285 {
306 if (do_auto) 286 if (do_auto)
307 return; 287 return;
308 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 288 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
309 "Cannot use TCP and UDP\n");
310 global_ret = 1; 289 global_ret = 1;
311 return; 290 return;
312 } 291 }
@@ -319,10 +298,10 @@ run (void *cls,
319 if (NULL != section_name) 298 if (NULL != section_name)
320 { 299 {
321 nt = GNUNET_NAT_AUTO_test_start (c, 300 nt = GNUNET_NAT_AUTO_test_start (c,
322 proto, 301 proto,
323 section_name, 302 section_name,
324 &test_report_cb, 303 &test_report_cb,
325 NULL); 304 NULL);
326 } 305 }
327 test_finished (); 306 test_finished ();
328} 307}
@@ -336,53 +315,47 @@ run (void *cls,
336 * @return 0 on success, -1 on error 315 * @return 0 on success, -1 on error
337 */ 316 */
338int 317int
339main (int argc, 318main (int argc, char *const argv[])
340 char *const argv[])
341{ 319{
342 struct GNUNET_GETOPT_CommandLineOption options[] = { 320 struct GNUNET_GETOPT_CommandLineOption options[] =
343 GNUNET_GETOPT_option_flag ('a', 321 {GNUNET_GETOPT_option_flag ('a',
344 "auto", 322 "auto",
345 gettext_noop ("run autoconfiguration"), 323 gettext_noop ("run autoconfiguration"),
346 &do_auto), 324 &do_auto),
347 325
348 GNUNET_GETOPT_option_string ('S', 326 GNUNET_GETOPT_option_string (
349 "section", 327 'S',
350 "NAME", 328 "section",
351 gettext_noop ("section name providing the configuration for the adapter"), 329 "NAME",
352 &section_name), 330 gettext_noop (
353 331 "section name providing the configuration for the adapter"),
354 GNUNET_GETOPT_option_flag ('t', 332 &section_name),
355 "tcp", 333
356 gettext_noop ("use TCP"), 334 GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp),
357 &use_tcp), 335
358 336 GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp),
359 GNUNET_GETOPT_option_flag ('u', 337
360 "udp", 338 GNUNET_GETOPT_option_flag (
361 gettext_noop ("use UDP"), 339 'w',
362 &use_udp), 340 "write",
363 341 gettext_noop ("write configuration file (for autoconfiguration)"),
364 GNUNET_GETOPT_option_flag ('w', 342 &write_cfg),
365 "write", 343 GNUNET_GETOPT_OPTION_END};
366 gettext_noop ("write configuration file (for autoconfiguration)"), 344
367 &write_cfg), 345 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
368 GNUNET_GETOPT_OPTION_END
369 };
370
371 if (GNUNET_OK !=
372 GNUNET_STRINGS_get_utf8_args (argc, argv,
373 &argc, &argv))
374 return 2; 346 return 2;
375 if (GNUNET_OK != 347 if (GNUNET_OK !=
376 GNUNET_PROGRAM_run (argc, argv, 348 GNUNET_PROGRAM_run (argc,
377 "gnunet-nat-auto [options]", 349 argv,
378 _("GNUnet NAT traversal autoconfiguration"), 350 "gnunet-nat-auto [options]",
379 options, 351 _ ("GNUnet NAT traversal autoconfiguration"),
352 options,
380 &run, 353 &run,
381 NULL)) 354 NULL))
382 { 355 {
383 global_ret = 1; 356 global_ret = 1;
384 } 357 }
385 GNUNET_free ((void*) argv); 358 GNUNET_free ((void *) argv);
386 return global_ret; 359 return global_ret;
387} 360}
388 361