summaryrefslogtreecommitdiff
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.c260
1 files changed, 130 insertions, 130 deletions
diff --git a/src/nat-auto/gnunet-nat-auto.c b/src/nat-auto/gnunet-nat-auto.c
index 87850509d..d40f4ef32 100644
--- a/src/nat-auto/gnunet-nat-auto.c
+++ b/src/nat-auto/gnunet-nat-auto.c
@@ -90,13 +90,13 @@ static uint8_t proto;
90 * terminate. 90 * terminate.
91 */ 91 */
92static void 92static void
93test_finished() 93test_finished ()
94{ 94{
95 if (NULL != ah) 95 if (NULL != ah)
96 return; 96 return;
97 if (NULL != nt) 97 if (NULL != nt)
98 return; 98 return;
99 GNUNET_SCHEDULER_shutdown(); 99 GNUNET_SCHEDULER_shutdown ();
100} 100}
101 101
102 102
@@ -109,16 +109,16 @@ test_finished()
109 * @param value value of the option 109 * @param value value of the option
110 */ 110 */
111static void 111static void
112auto_conf_iter(void *cls, 112auto_conf_iter (void *cls,
113 const char *section, 113 const char *section,
114 const char *option, 114 const char *option,
115 const char *value) 115 const char *value)
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", option, value); 119 printf ("%s: %s\n", option, value);
120 if (NULL != new_cfg) 120 if (NULL != new_cfg)
121 GNUNET_CONFIGURATION_set_value_string(new_cfg, section, option, value); 121 GNUNET_CONFIGURATION_set_value_string (new_cfg, section, option, value);
122} 122}
123 123
124 124
@@ -132,10 +132,10 @@ auto_conf_iter(void *cls,
132 * @param type what the situation of the NAT 132 * @param type what the situation of the NAT
133 */ 133 */
134static void 134static void
135auto_config_cb(void *cls, 135auto_config_cb (void *cls,
136 const struct GNUNET_CONFIGURATION_Handle *diff, 136 const struct GNUNET_CONFIGURATION_Handle *diff,
137 enum GNUNET_NAT_StatusCode result, 137 enum GNUNET_NAT_StatusCode result,
138 enum GNUNET_NAT_Type type) 138 enum GNUNET_NAT_Type type)
139{ 139{
140 const char *nat_type; 140 const char *nat_type;
141 char unknown_type[64]; 141 char unknown_type[64];
@@ -143,84 +143,84 @@ auto_config_cb(void *cls,
143 143
144 ah = NULL; 144 ah = NULL;
145 switch (type) 145 switch (type)
146 { 146 {
147 case GNUNET_NAT_TYPE_NO_NAT: 147 case GNUNET_NAT_TYPE_NO_NAT:
148 nat_type = "NO NAT"; 148 nat_type = "NO NAT";
149 break; 149 break;
150 150
151 case GNUNET_NAT_TYPE_UNREACHABLE_NAT: 151 case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
152 nat_type = "NAT but we can traverse"; 152 nat_type = "NAT but we can traverse";
153 break; 153 break;
154 154
155 case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT: 155 case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
156 nat_type = "NAT but STUN is able to identify the correct information"; 156 nat_type = "NAT but STUN is able to identify the correct information";
157 break; 157 break;
158 158
159 case GNUNET_NAT_TYPE_UPNP_NAT: 159 case GNUNET_NAT_TYPE_UPNP_NAT:
160 nat_type = "NAT but UPNP opened the ports"; 160 nat_type = "NAT but UPNP opened the ports";
161 break; 161 break;
162 162
163 default: 163 default:
164 sprintf(unknown_type, "NAT unknown, type %u", type); 164 sprintf (unknown_type, "NAT unknown, type %u", type);
165 nat_type = unknown_type; 165 nat_type = unknown_type;
166 break; 166 break;
167 } 167 }
168 168
169 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 169 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
170 "NAT status: %s/%s\n", 170 "NAT status: %s/%s\n",
171 GNUNET_NAT_AUTO_status2string(result), 171 GNUNET_NAT_AUTO_status2string (result),
172 nat_type); 172 nat_type);
173 173
174 if (NULL == diff) 174 if (NULL == diff)
175 return; 175 return;
176 176
177 /* Shortcut: if there are no changes suggested, bail out early. */ 177 /* Shortcut: if there are no changes suggested, bail out early. */
178 if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty(diff)) 178 if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty (diff))
179 { 179 {
180 test_finished(); 180 test_finished ();
181 return; 181 return;
182 } 182 }
183 183
184 /* Apply diff to original configuration and show changes 184 /* Apply diff to original configuration and show changes
185 to the user */ 185 to the user */
186 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup(cfg) : NULL; 186 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
187 187
188 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 188 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
189 _("Suggested configuration changes:\n")); 189 _ ("Suggested configuration changes:\n"));
190 GNUNET_CONFIGURATION_iterate_section_values(diff, 190 GNUNET_CONFIGURATION_iterate_section_values (diff,
191 "nat", 191 "nat",
192 &auto_conf_iter, 192 &auto_conf_iter,
193 new_cfg); 193 new_cfg);
194 194
195 /* If desired, write configuration to file; we write only the 195 /* If desired, write configuration to file; we write only the
196 changes to the defaults to keep things compact. */ 196 changes to the defaults to keep things compact. */
197 if (write_cfg) 197 if (write_cfg)
198 {
199 struct GNUNET_CONFIGURATION_Handle *def_cfg;
200
201 GNUNET_CONFIGURATION_set_value_string (new_cfg, "ARM", "CONFIG", NULL);
202 def_cfg = GNUNET_CONFIGURATION_create ();
203 GNUNET_break (GNUNET_OK == GNUNET_CONFIGURATION_load (def_cfg, NULL));
204 if (GNUNET_OK !=
205 GNUNET_CONFIGURATION_write_diffs (def_cfg, new_cfg, cfg_file))
198 { 206 {
199 struct GNUNET_CONFIGURATION_Handle *def_cfg; 207 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
200 208 _ ("Failed to write configuration to `%s'\n"),
201 GNUNET_CONFIGURATION_set_value_string(new_cfg, "ARM", "CONFIG", NULL); 209 cfg_file);
202 def_cfg = GNUNET_CONFIGURATION_create(); 210 global_ret = 1;
203 GNUNET_break(GNUNET_OK == GNUNET_CONFIGURATION_load(def_cfg, NULL)); 211 }
204 if (GNUNET_OK != 212 else
205 GNUNET_CONFIGURATION_write_diffs(def_cfg, new_cfg, cfg_file)) 213 {
206 { 214 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
207 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, 215 _ ("Wrote updated configuration to `%s'\n"),
208 _("Failed to write configuration to `%s'\n"), 216 cfg_file);
209 cfg_file);
210 global_ret = 1;
211 }
212 else
213 {
214 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
215 _("Wrote updated configuration to `%s'\n"),
216 cfg_file);
217 }
218 GNUNET_CONFIGURATION_destroy(def_cfg);
219 } 217 }
218 GNUNET_CONFIGURATION_destroy (def_cfg);
219 }
220 220
221 if (NULL != new_cfg) 221 if (NULL != new_cfg)
222 GNUNET_CONFIGURATION_destroy(new_cfg); 222 GNUNET_CONFIGURATION_destroy (new_cfg);
223 test_finished(); 223 test_finished ();
224} 224}
225 225
226 226
@@ -232,11 +232,11 @@ auto_config_cb(void *cls,
232 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 232 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
233 */ 233 */
234static void 234static void
235test_report_cb(void *cls, enum GNUNET_NAT_StatusCode result) 235test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result)
236{ 236{
237 nt = NULL; 237 nt = NULL;
238 printf("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string(result)); 238 printf ("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string (result));
239 test_finished(); 239 test_finished ();
240} 240}
241 241
242 242
@@ -246,18 +246,18 @@ test_report_cb(void *cls, enum GNUNET_NAT_StatusCode result)
246 * @param cls NULL 246 * @param cls NULL
247 */ 247 */
248static void 248static void
249do_shutdown(void *cls) 249do_shutdown (void *cls)
250{ 250{
251 if (NULL != ah) 251 if (NULL != ah)
252 { 252 {
253 GNUNET_NAT_AUTO_autoconfig_cancel(ah); 253 GNUNET_NAT_AUTO_autoconfig_cancel (ah);
254 ah = NULL; 254 ah = NULL;
255 } 255 }
256 if (NULL != nt) 256 if (NULL != nt)
257 { 257 {
258 GNUNET_NAT_AUTO_test_stop(nt); 258 GNUNET_NAT_AUTO_test_stop (nt);
259 nt = NULL; 259 nt = NULL;
260 } 260 }
261} 261}
262 262
263 263
@@ -270,29 +270,29 @@ do_shutdown(void *cls)
270 * @param c configuration 270 * @param c configuration
271 */ 271 */
272static void 272static void
273run(void *cls, 273run (void *cls,
274 char *const *args, 274 char *const *args,
275 const char *cfgfile, 275 const char *cfgfile,
276 const struct GNUNET_CONFIGURATION_Handle *c) 276 const struct GNUNET_CONFIGURATION_Handle *c)
277{ 277{
278 cfg_file = cfgfile; 278 cfg_file = cfgfile;
279 cfg = c; 279 cfg = c;
280 280
281 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 281 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
282 282
283 if (do_auto) 283 if (do_auto)
284 { 284 {
285 ah = GNUNET_NAT_AUTO_autoconfig_start(c, &auto_config_cb, NULL); 285 ah = GNUNET_NAT_AUTO_autoconfig_start (c, &auto_config_cb, NULL);
286 } 286 }
287 287
288 if (use_tcp && use_udp) 288 if (use_tcp && use_udp)
289 { 289 {
290 if (do_auto) 290 if (do_auto)
291 return;
292 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
293 global_ret = 1;
294 return; 291 return;
295 } 292 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
293 global_ret = 1;
294 return;
295 }
296 proto = 0; 296 proto = 0;
297 if (use_tcp) 297 if (use_tcp)
298 proto = IPPROTO_TCP; 298 proto = IPPROTO_TCP;
@@ -300,14 +300,14 @@ run(void *cls,
300 proto = IPPROTO_UDP; 300 proto = IPPROTO_UDP;
301 301
302 if (NULL != section_name) 302 if (NULL != section_name)
303 { 303 {
304 nt = GNUNET_NAT_AUTO_test_start(c, 304 nt = GNUNET_NAT_AUTO_test_start (c,
305 proto, 305 proto,
306 section_name, 306 section_name,
307 &test_report_cb, 307 &test_report_cb,
308 NULL); 308 NULL);
309 } 309 }
310 test_finished(); 310 test_finished ();
311} 311}
312 312
313 313
@@ -319,47 +319,47 @@ run(void *cls,
319 * @return 0 on success, -1 on error 319 * @return 0 on success, -1 on error
320 */ 320 */
321int 321int
322main(int argc, char *const argv[]) 322main (int argc, char *const argv[])
323{ 323{
324 struct GNUNET_GETOPT_CommandLineOption options[] = 324 struct GNUNET_GETOPT_CommandLineOption options[] =
325 { GNUNET_GETOPT_option_flag('a', 325 { GNUNET_GETOPT_option_flag ('a',
326 "auto", 326 "auto",
327 gettext_noop("run autoconfiguration"), 327 gettext_noop ("run autoconfiguration"),
328 &do_auto), 328 &do_auto),
329 329
330 GNUNET_GETOPT_option_string( 330 GNUNET_GETOPT_option_string (
331 'S', 331 'S',
332 "section", 332 "section",
333 "NAME", 333 "NAME",
334 gettext_noop( 334 gettext_noop (
335 "section name providing the configuration for the adapter"), 335 "section name providing the configuration for the adapter"),
336 &section_name), 336 &section_name),
337 337
338 GNUNET_GETOPT_option_flag('t', "tcp", gettext_noop("use TCP"), &use_tcp), 338 GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp),
339 339
340 GNUNET_GETOPT_option_flag('u', "udp", gettext_noop("use UDP"), &use_udp), 340 GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp),
341 341
342 GNUNET_GETOPT_option_flag( 342 GNUNET_GETOPT_option_flag (
343 'w', 343 'w',
344 "write", 344 "write",
345 gettext_noop("write configuration file (for autoconfiguration)"), 345 gettext_noop ("write configuration file (for autoconfiguration)"),
346 &write_cfg), 346 &write_cfg),
347 GNUNET_GETOPT_OPTION_END }; 347 GNUNET_GETOPT_OPTION_END };
348 348
349 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 349 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
350 return 2; 350 return 2;
351 if (GNUNET_OK != 351 if (GNUNET_OK !=
352 GNUNET_PROGRAM_run(argc, 352 GNUNET_PROGRAM_run (argc,
353 argv, 353 argv,
354 "gnunet-nat-auto [options]", 354 "gnunet-nat-auto [options]",
355 _("GNUnet NAT traversal autoconfiguration"), 355 _ ("GNUnet NAT traversal autoconfiguration"),
356 options, 356 options,
357 &run, 357 &run,
358 NULL)) 358 NULL))
359 { 359 {
360 global_ret = 1; 360 global_ret = 1;
361 } 361 }
362 GNUNET_free((void *)argv); 362 GNUNET_free ((void *) argv);
363 return global_ret; 363 return global_ret;
364} 364}
365 365