aboutsummaryrefslogtreecommitdiff
path: root/src/nat-auto
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/nat-auto
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/nat-auto')
-rw-r--r--src/nat-auto/gnunet-nat-auto.c260
-rw-r--r--src/nat-auto/gnunet-nat-auto_legacy.c459
-rw-r--r--src/nat-auto/gnunet-nat-server.c270
-rw-r--r--src/nat-auto/gnunet-service-nat-auto.c289
-rw-r--r--src/nat-auto/gnunet-service-nat-auto_legacy.c1009
-rw-r--r--src/nat-auto/nat-auto.h9
-rw-r--r--src/nat-auto/nat_auto_api.c239
-rw-r--r--src/nat-auto/nat_auto_api_test.c473
8 files changed, 1521 insertions, 1487 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
diff --git a/src/nat-auto/gnunet-nat-auto_legacy.c b/src/nat-auto/gnunet-nat-auto_legacy.c
index 333e75529..0a531d5cd 100644
--- a/src/nat-auto/gnunet-nat-auto_legacy.c
+++ b/src/nat-auto/gnunet-nat-auto_legacy.c
@@ -28,15 +28,16 @@
28#include "gnunet_nat_lib.h" 28#include "gnunet_nat_lib.h"
29#include "nat.h" 29#include "nat.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
32 32
33#define NAT_SERVER_TIMEOUT \ 33#define NAT_SERVER_TIMEOUT \
34 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30) 34 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
35 35
36/** 36/**
37 * Entry we keep for each incoming connection. 37 * Entry we keep for each incoming connection.
38 */ 38 */
39struct NatActivity { 39struct NatActivity
40{
40 /** 41 /**
41 * This is a doubly-linked list. 42 * This is a doubly-linked list.
42 */ 43 */
@@ -67,7 +68,8 @@ struct NatActivity {
67/** 68/**
68 * Entry we keep for each connection to the gnunet-nat-service. 69 * Entry we keep for each connection to the gnunet-nat-service.
69 */ 70 */
70struct ClientActivity { 71struct ClientActivity
72{
71 /** 73 /**
72 * This is a doubly-linked list. 74 * This is a doubly-linked list.
73 */ 75 */
@@ -93,7 +95,8 @@ struct ClientActivity {
93/** 95/**
94 * Handle to a NAT test. 96 * Handle to a NAT test.
95 */ 97 */
96struct GNUNET_NAT_Test { 98struct GNUNET_NAT_Test
99{
97 /** 100 /**
98 * Configuration used 101 * Configuration used
99 */ 102 */
@@ -180,22 +183,22 @@ struct GNUNET_NAT_Test {
180 * @param addrlen actual lenght of the @a addr 183 * @param addrlen actual lenght of the @a addr
181 */ 184 */
182static void 185static void
183reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen) 186reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
184{ 187{
185 struct GNUNET_NAT_Test *h = cls; 188 struct GNUNET_NAT_Test *h = cls;
186 const struct sockaddr_in *sa; 189 const struct sockaddr_in *sa;
187 190
188 if (sizeof(struct sockaddr_in) != addrlen) 191 if (sizeof(struct sockaddr_in) != addrlen)
189 return; 192 return;
190 sa = (const struct sockaddr_in *)addr; 193 sa = (const struct sockaddr_in *) addr;
191 if (h->data != sa->sin_port) 194 if (h->data != sa->sin_port)
192 { 195 {
193 LOG(GNUNET_ERROR_TYPE_DEBUG, 196 LOG (GNUNET_ERROR_TYPE_DEBUG,
194 "Received connection reversal request for wrong port\n"); 197 "Received connection reversal request for wrong port\n");
195 return; /* wrong port */ 198 return; /* wrong port */
196 } 199 }
197 /* report success */ 200 /* report success */
198 h->report(h->report_cls, GNUNET_NAT_ERROR_SUCCESS); 201 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
199} 202}
200 203
201 204
@@ -206,31 +209,31 @@ reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen)
206 * @param cls the `struct GNUNET_NAT_Test` 209 * @param cls the `struct GNUNET_NAT_Test`
207 */ 210 */
208static void 211static void
209do_udp_read(void *cls) 212do_udp_read (void *cls)
210{ 213{
211 struct GNUNET_NAT_Test *tst = cls; 214 struct GNUNET_NAT_Test *tst = cls;
212 uint16_t data; 215 uint16_t data;
213 const struct GNUNET_SCHEDULER_TaskContext *tc; 216 const struct GNUNET_SCHEDULER_TaskContext *tc;
214 217
215 tc = GNUNET_SCHEDULER_get_task_context(); 218 tc = GNUNET_SCHEDULER_get_task_context ();
216 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 219 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
217 tst->lsock, 220 tst->lsock,
218 &do_udp_read, 221 &do_udp_read,
219 tst); 222 tst);
220 if ((NULL != tc->write_ready) && 223 if ((NULL != tc->write_ready) &&
221 (GNUNET_NETWORK_fdset_isset(tc->read_ready, tst->lsock)) && 224 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
222 (sizeof(data) == 225 (sizeof(data) ==
223 GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data)))) 226 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof(data))))
224 { 227 {
225 if (data == tst->data) 228 if (data == tst->data)
226 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 229 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
227 else 230 else
228 LOG(GNUNET_ERROR_TYPE_DEBUG, 231 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Received data mismatches expected value\n"); 232 "Received data mismatches expected value\n");
230 } 233 }
231 else 234 else
232 LOG(GNUNET_ERROR_TYPE_DEBUG, 235 LOG (GNUNET_ERROR_TYPE_DEBUG,
233 "Failed to receive data from inbound connection\n"); 236 "Failed to receive data from inbound connection\n");
234} 237}
235 238
236 239
@@ -241,33 +244,33 @@ do_udp_read(void *cls)
241 * @param cls the `struct NatActivity` 244 * @param cls the `struct NatActivity`
242 */ 245 */
243static void 246static void
244do_read(void *cls) 247do_read (void *cls)
245{ 248{
246 struct NatActivity *na = cls; 249 struct NatActivity *na = cls;
247 struct GNUNET_NAT_Test *tst; 250 struct GNUNET_NAT_Test *tst;
248 uint16_t data; 251 uint16_t data;
249 const struct GNUNET_SCHEDULER_TaskContext *tc; 252 const struct GNUNET_SCHEDULER_TaskContext *tc;
250 253
251 tc = GNUNET_SCHEDULER_get_task_context(); 254 tc = GNUNET_SCHEDULER_get_task_context ();
252 na->rtask = NULL; 255 na->rtask = NULL;
253 tst = na->h; 256 tst = na->h;
254 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, na); 257 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
255 if ((NULL != tc->write_ready) && 258 if ((NULL != tc->write_ready) &&
256 (GNUNET_NETWORK_fdset_isset(tc->read_ready, na->sock)) && 259 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
257 (sizeof(data) == 260 (sizeof(data) ==
258 GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data)))) 261 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof(data))))
259 { 262 {
260 if (data == tst->data) 263 if (data == tst->data)
261 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 264 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
262 else 265 else
263 LOG(GNUNET_ERROR_TYPE_DEBUG, 266 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Received data does not match expected value\n"); 267 "Received data does not match expected value\n");
265 } 268 }
266 else 269 else
267 LOG(GNUNET_ERROR_TYPE_DEBUG, 270 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 "Failed to receive data from inbound connection\n"); 271 "Failed to receive data from inbound connection\n");
269 GNUNET_NETWORK_socket_close(na->sock); 272 GNUNET_NETWORK_socket_close (na->sock);
270 GNUNET_free(na); 273 GNUNET_free (na);
271} 274}
272 275
273 276
@@ -278,32 +281,32 @@ do_read(void *cls)
278 * @param cls the `struct GNUNET_NAT_Test` 281 * @param cls the `struct GNUNET_NAT_Test`
279 */ 282 */
280static void 283static void
281do_accept(void *cls) 284do_accept (void *cls)
282{ 285{
283 struct GNUNET_NAT_Test *tst = cls; 286 struct GNUNET_NAT_Test *tst = cls;
284 struct GNUNET_NETWORK_Handle *s; 287 struct GNUNET_NETWORK_Handle *s;
285 struct NatActivity *wl; 288 struct NatActivity *wl;
286 289
287 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 290 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
288 tst->lsock, 291 tst->lsock,
289 &do_accept, 292 &do_accept,
290 tst); 293 tst);
291 s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL); 294 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
292 if (NULL == s) 295 if (NULL == s)
293 { 296 {
294 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, "accept"); 297 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
295 return; /* odd error */ 298 return; /* odd error */
296 } 299 }
297 LOG(GNUNET_ERROR_TYPE_DEBUG, 300 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Got an inbound connection, waiting for data\n"); 301 "Got an inbound connection, waiting for data\n");
299 wl = GNUNET_new(struct NatActivity); 302 wl = GNUNET_new (struct NatActivity);
300 wl->sock = s; 303 wl->sock = s;
301 wl->h = tst; 304 wl->h = tst;
302 wl->rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 305 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
303 wl->sock, 306 wl->sock,
304 &do_read, 307 &do_read,
305 wl); 308 wl);
306 GNUNET_CONTAINER_DLL_insert(tst->na_head, tst->na_tail, wl); 309 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
307} 310}
308 311
309 312
@@ -315,14 +318,14 @@ do_accept(void *cls)
315 * @param error error code 318 * @param error error code
316 */ 319 */
317static void 320static void
318mq_error_handler(void *cls, enum GNUNET_MQ_Error error) 321mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
319{ 322{
320 struct ClientActivity *ca = cls; 323 struct ClientActivity *ca = cls;
321 struct GNUNET_NAT_Test *tst = ca->h; 324 struct GNUNET_NAT_Test *tst = ca->h;
322 325
323 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, ca); 326 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, ca);
324 GNUNET_MQ_destroy(ca->mq); 327 GNUNET_MQ_destroy (ca->mq);
325 GNUNET_free(ca); 328 GNUNET_free (ca);
326} 329}
327 330
328 331
@@ -336,10 +339,10 @@ mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
336 * @param addrlen actual length of the @a addr 339 * @param addrlen actual length of the @a addr
337 */ 340 */
338static void 341static void
339addr_cb(void *cls, 342addr_cb (void *cls,
340 int add_remove, 343 int add_remove,
341 const struct sockaddr *addr, 344 const struct sockaddr *addr,
342 socklen_t addrlen) 345 socklen_t addrlen)
343{ 346{
344 struct GNUNET_NAT_Test *h = cls; 347 struct GNUNET_NAT_Test *h = cls;
345 struct ClientActivity *ca; 348 struct ClientActivity *ca;
@@ -350,38 +353,38 @@ addr_cb(void *cls,
350 if (GNUNET_YES != add_remove) 353 if (GNUNET_YES != add_remove)
351 return; 354 return;
352 if (addrlen != sizeof(struct sockaddr_in)) 355 if (addrlen != sizeof(struct sockaddr_in))
353 { 356 {
354 LOG(GNUNET_ERROR_TYPE_DEBUG, 357 LOG (GNUNET_ERROR_TYPE_DEBUG,
355 "NAT test ignores IPv6 address `%s' returned from NAT library\n", 358 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
356 GNUNET_a2s(addr, addrlen)); 359 GNUNET_a2s (addr, addrlen));
357 return; /* ignore IPv6 here */ 360 return; /* ignore IPv6 here */
358 } 361 }
359 LOG(GNUNET_ERROR_TYPE_INFO, 362 LOG (GNUNET_ERROR_TYPE_INFO,
360 "Asking gnunet-nat-server to connect to `%s'\n", 363 "Asking gnunet-nat-server to connect to `%s'\n",
361 GNUNET_a2s(addr, addrlen)); 364 GNUNET_a2s (addr, addrlen));
362 365
363 ca = GNUNET_new(struct ClientActivity); 366 ca = GNUNET_new (struct ClientActivity);
364 ca->h = h; 367 ca->h = h;
365 ca->mq = GNUNET_CLIENT_connect(h->cfg, 368 ca->mq = GNUNET_CLIENT_connect (h->cfg,
366 "gnunet-nat-server", 369 "gnunet-nat-server",
367 NULL, 370 NULL,
368 &mq_error_handler, 371 &mq_error_handler,
369 ca); 372 ca);
370 if (NULL == ca->mq) 373 if (NULL == ca->mq)
371 { 374 {
372 GNUNET_free(ca); 375 GNUNET_free (ca);
373 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
374 _("Failed to connect to `gnunet-nat-server'\n")); 377 _ ("Failed to connect to `gnunet-nat-server'\n"));
375 return; 378 return;
376 } 379 }
377 GNUNET_CONTAINER_DLL_insert(h->ca_head, h->ca_tail, ca); 380 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
378 sa = (const struct sockaddr_in *)addr; 381 sa = (const struct sockaddr_in *) addr;
379 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_NAT_TEST); 382 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAT_TEST);
380 msg->dst_ipv4 = sa->sin_addr.s_addr; 383 msg->dst_ipv4 = sa->sin_addr.s_addr;
381 msg->dport = sa->sin_port; 384 msg->dport = sa->sin_port;
382 msg->data = h->data; 385 msg->data = h->data;
383 msg->is_tcp = htonl((uint32_t)h->is_tcp); 386 msg->is_tcp = htonl ((uint32_t) h->is_tcp);
384 GNUNET_MQ_send(ca->mq, env); 387 GNUNET_MQ_send (ca->mq, env);
385} 388}
386 389
387 390
@@ -394,15 +397,15 @@ addr_cb(void *cls,
394 * @param cls handle to the timed out NAT test 397 * @param cls handle to the timed out NAT test
395 */ 398 */
396static void 399static void
397do_timeout(void *cls) 400do_timeout (void *cls)
398{ 401{
399 struct GNUNET_NAT_Test *nh = cls; 402 struct GNUNET_NAT_Test *nh = cls;
400 403
401 nh->ttask = NULL; 404 nh->ttask = NULL;
402 nh->report(nh->report_cls, 405 nh->report (nh->report_cls,
403 (GNUNET_NAT_ERROR_SUCCESS == nh->status) 406 (GNUNET_NAT_ERROR_SUCCESS == nh->status)
404 ? GNUNET_NAT_ERROR_TIMEOUT 407 ? GNUNET_NAT_ERROR_TIMEOUT
405 : nh->status); 408 : nh->status);
406} 409}
407 410
408 411
@@ -422,27 +425,27 @@ do_timeout(void *cls)
422 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback 425 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
423 */ 426 */
424struct GNUNET_NAT_Test * 427struct GNUNET_NAT_Test *
425GNUNET_NAT_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg, 428GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
426 int is_tcp, 429 int is_tcp,
427 uint16_t bnd_port, 430 uint16_t bnd_port,
428 uint16_t adv_port, 431 uint16_t adv_port,
429 struct GNUNET_TIME_Relative timeout, 432 struct GNUNET_TIME_Relative timeout,
430 GNUNET_NAT_TestCallback report, 433 GNUNET_NAT_TestCallback report,
431 void *report_cls) 434 void *report_cls)
432{ 435{
433 struct GNUNET_NAT_Test *nh; 436 struct GNUNET_NAT_Test *nh;
434 struct sockaddr_in sa; 437 struct sockaddr_in sa;
435 const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa }; 438 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
436 const socklen_t addrlens[] = { sizeof(sa) }; 439 const socklen_t addrlens[] = { sizeof(sa) };
437 440
438 memset(&sa, 0, sizeof(sa)); 441 memset (&sa, 0, sizeof(sa));
439 sa.sin_family = AF_INET; 442 sa.sin_family = AF_INET;
440 sa.sin_port = htons(bnd_port); 443 sa.sin_port = htons (bnd_port);
441#if HAVE_SOCKADDR_IN_SIN_LEN 444#if HAVE_SOCKADDR_IN_SIN_LEN
442 sa.sin_len = sizeof(sa); 445 sa.sin_len = sizeof(sa);
443#endif 446#endif
444 447
445 nh = GNUNET_new(struct GNUNET_NAT_Test); 448 nh = GNUNET_new (struct GNUNET_NAT_Test);
446 nh->cfg = cfg; 449 nh->cfg = cfg;
447 nh->is_tcp = is_tcp; 450 nh->is_tcp = is_tcp;
448 nh->data = bnd_port; 451 nh->data = bnd_port;
@@ -451,93 +454,93 @@ GNUNET_NAT_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
451 nh->report_cls = report_cls; 454 nh->report_cls = report_cls;
452 nh->status = GNUNET_NAT_ERROR_SUCCESS; 455 nh->status = GNUNET_NAT_ERROR_SUCCESS;
453 if (0 == bnd_port) 456 if (0 == bnd_port)
457 {
458 nh->nat = GNUNET_NAT_register (cfg,
459 is_tcp,
460 0,
461 0,
462 NULL,
463 NULL,
464 &addr_cb,
465 &reversal_cb,
466 nh,
467 NULL);
468 }
469 else
470 {
471 nh->lsock =
472 GNUNET_NETWORK_socket_create (AF_INET,
473 (is_tcp == GNUNET_YES) ? SOCK_STREAM
474 : SOCK_DGRAM,
475 0);
476 if ((nh->lsock == NULL) ||
477 (GNUNET_OK != GNUNET_NETWORK_socket_bind (nh->lsock,
478 (const struct sockaddr *) &sa,
479 sizeof(sa))))
454 { 480 {
455 nh->nat = GNUNET_NAT_register(cfg, 481 GNUNET_log (
456 is_tcp, 482 GNUNET_ERROR_TYPE_ERROR,
457 0, 483 _ ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
458 0, 484 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof(sa)),
459 NULL, 485 strerror (errno));
460 NULL, 486 if (NULL != nh->lsock)
461 &addr_cb, 487 {
462 &reversal_cb, 488 GNUNET_NETWORK_socket_close (nh->lsock);
463 nh, 489 nh->lsock = NULL;
464 NULL); 490 }
491 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
492 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
493 return nh;
465 } 494 }
466 else 495 if (GNUNET_YES == is_tcp)
496 {
497 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
498 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
499 nh->lsock,
500 &do_accept,
501 nh);
502 }
503 else
504 {
505 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
506 nh->lsock,
507 &do_udp_read,
508 nh);
509 }
510 LOG (GNUNET_ERROR_TYPE_INFO,
511 "NAT test listens on port %u (%s)\n",
512 bnd_port,
513 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
514 nh->nat = GNUNET_NAT_register (cfg,
515 is_tcp,
516 adv_port,
517 1,
518 addrs,
519 addrlens,
520 &addr_cb,
521 NULL,
522 nh,
523 NULL);
524 if (NULL == nh->nat)
467 { 525 {
468 nh->lsock = 526 LOG (GNUNET_ERROR_TYPE_INFO,
469 GNUNET_NETWORK_socket_create(AF_INET, 527 _ ("NAT test failed to start NAT library\n"));
470 (is_tcp == GNUNET_YES) ? SOCK_STREAM 528 if (NULL != nh->ltask)
471 : SOCK_DGRAM, 529 {
472 0); 530 GNUNET_SCHEDULER_cancel (nh->ltask);
473 if ((nh->lsock == NULL) || 531 nh->ltask = NULL;
474 (GNUNET_OK != GNUNET_NETWORK_socket_bind(nh->lsock, 532 }
475 (const struct sockaddr *)&sa, 533 if (NULL != nh->lsock)
476 sizeof(sa)))) 534 {
477 { 535 GNUNET_NETWORK_socket_close (nh->lsock);
478 GNUNET_log( 536 nh->lsock = NULL;
479 GNUNET_ERROR_TYPE_ERROR, 537 }
480 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"), 538 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
481 GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)), 539 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
482 strerror(errno)); 540 return nh;
483 if (NULL != nh->lsock)
484 {
485 GNUNET_NETWORK_socket_close(nh->lsock);
486 nh->lsock = NULL;
487 }
488 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
489 nh->ttask = GNUNET_SCHEDULER_add_now(&do_timeout, nh);
490 return nh;
491 }
492 if (GNUNET_YES == is_tcp)
493 {
494 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_listen(nh->lsock, 5));
495 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
496 nh->lsock,
497 &do_accept,
498 nh);
499 }
500 else
501 {
502 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
503 nh->lsock,
504 &do_udp_read,
505 nh);
506 }
507 LOG(GNUNET_ERROR_TYPE_INFO,
508 "NAT test listens on port %u (%s)\n",
509 bnd_port,
510 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
511 nh->nat = GNUNET_NAT_register(cfg,
512 is_tcp,
513 adv_port,
514 1,
515 addrs,
516 addrlens,
517 &addr_cb,
518 NULL,
519 nh,
520 NULL);
521 if (NULL == nh->nat)
522 {
523 LOG(GNUNET_ERROR_TYPE_INFO,
524 _("NAT test failed to start NAT library\n"));
525 if (NULL != nh->ltask)
526 {
527 GNUNET_SCHEDULER_cancel(nh->ltask);
528 nh->ltask = NULL;
529 }
530 if (NULL != nh->lsock)
531 {
532 GNUNET_NETWORK_socket_close(nh->lsock);
533 nh->lsock = NULL;
534 }
535 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
536 nh->ttask = GNUNET_SCHEDULER_add_now(&do_timeout, nh);
537 return nh;
538 }
539 } 541 }
540 nh->ttask = GNUNET_SCHEDULER_add_delayed(timeout, &do_timeout, nh); 542 }
543 nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
541 return nh; 544 return nh;
542} 545}
543 546
@@ -548,46 +551,46 @@ GNUNET_NAT_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
548 * @param tst test to stop. 551 * @param tst test to stop.
549 */ 552 */
550void 553void
551GNUNET_NAT_test_stop(struct GNUNET_NAT_Test *tst) 554GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
552{ 555{
553 struct NatActivity *pos; 556 struct NatActivity *pos;
554 struct ClientActivity *cpos; 557 struct ClientActivity *cpos;
555 558
556 LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n"); 559 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
557 while (NULL != (cpos = tst->ca_head)) 560 while (NULL != (cpos = tst->ca_head))
558 { 561 {
559 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos); 562 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
560 GNUNET_MQ_destroy(cpos->mq); 563 GNUNET_MQ_destroy (cpos->mq);
561 GNUNET_free(cpos); 564 GNUNET_free (cpos);
562 } 565 }
563 while (NULL != (pos = tst->na_head)) 566 while (NULL != (pos = tst->na_head))
564 { 567 {
565 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, pos); 568 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
566 GNUNET_SCHEDULER_cancel(pos->rtask); 569 GNUNET_SCHEDULER_cancel (pos->rtask);
567 GNUNET_NETWORK_socket_close(pos->sock); 570 GNUNET_NETWORK_socket_close (pos->sock);
568 GNUNET_free(pos); 571 GNUNET_free (pos);
569 } 572 }
570 if (NULL != tst->ttask) 573 if (NULL != tst->ttask)
571 { 574 {
572 GNUNET_SCHEDULER_cancel(tst->ttask); 575 GNUNET_SCHEDULER_cancel (tst->ttask);
573 tst->ttask = NULL; 576 tst->ttask = NULL;
574 } 577 }
575 if (NULL != tst->ltask) 578 if (NULL != tst->ltask)
576 { 579 {
577 GNUNET_SCHEDULER_cancel(tst->ltask); 580 GNUNET_SCHEDULER_cancel (tst->ltask);
578 tst->ltask = NULL; 581 tst->ltask = NULL;
579 } 582 }
580 if (NULL != tst->lsock) 583 if (NULL != tst->lsock)
581 { 584 {
582 GNUNET_NETWORK_socket_close(tst->lsock); 585 GNUNET_NETWORK_socket_close (tst->lsock);
583 tst->lsock = NULL; 586 tst->lsock = NULL;
584 } 587 }
585 if (NULL != tst->nat) 588 if (NULL != tst->nat)
586 { 589 {
587 GNUNET_NAT_unregister(tst->nat); 590 GNUNET_NAT_unregister (tst->nat);
588 tst->nat = NULL; 591 tst->nat = NULL;
589 } 592 }
590 GNUNET_free(tst); 593 GNUNET_free (tst);
591} 594}
592 595
593/* end of nat_test.c */ 596/* end of nat_test.c */
diff --git a/src/nat-auto/gnunet-nat-server.c b/src/nat-auto/gnunet-nat-server.c
index db44d7629..5186cb8c0 100644
--- a/src/nat-auto/gnunet-nat-server.c
+++ b/src/nat-auto/gnunet-nat-server.c
@@ -33,7 +33,8 @@
33/** 33/**
34 * Information we track per client. 34 * Information we track per client.
35 */ 35 */
36struct ClientData { 36struct ClientData
37{
37 /** 38 /**
38 * Timeout task. 39 * Timeout task.
39 */ 40 */
@@ -60,9 +61,9 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO) 61 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
61 */ 62 */
62static void 63static void
63try_anat(uint32_t dst_ipv4, 64try_anat (uint32_t dst_ipv4,
64 uint16_t dport, 65 uint16_t dport,
65 int is_tcp) 66 int is_tcp)
66{ 67{
67 struct GNUNET_NAT_Handle *h; 68 struct GNUNET_NAT_Handle *h;
68 struct sockaddr_in lsa; 69 struct sockaddr_in lsa;
@@ -70,44 +71,45 @@ try_anat(uint32_t dst_ipv4,
70 const struct sockaddr *sa; 71 const struct sockaddr *sa;
71 socklen_t sa_len; 72 socklen_t sa_len;
72 73
73 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74 "Asking for connection reversal with %x and code %u\n", 75 "Asking for connection reversal with %x and code %u\n",
75 (unsigned int)dst_ipv4, 76 (unsigned int) dst_ipv4,
76 (unsigned int)dport); 77 (unsigned int) dport);
77 memset(&lsa, 0, sizeof(lsa)); 78 memset (&lsa, 0, sizeof(lsa));
78 lsa.sin_family = AF_INET; 79 lsa.sin_family = AF_INET;
79#if HAVE_SOCKADDR_IN_SIN_LEN 80#if HAVE_SOCKADDR_IN_SIN_LEN
80 lsa.sin_len = sizeof(sa); 81 lsa.sin_len = sizeof(sa);
81#endif 82#endif
82 lsa.sin_addr.s_addr = 0; 83 lsa.sin_addr.s_addr = 0;
83 lsa.sin_port = htons(dport); 84 lsa.sin_port = htons (dport);
84 memset(&rsa, 0, sizeof(rsa)); 85 memset (&rsa, 0, sizeof(rsa));
85 rsa.sin_family = AF_INET; 86 rsa.sin_family = AF_INET;
86#if HAVE_SOCKADDR_IN_SIN_LEN 87#if HAVE_SOCKADDR_IN_SIN_LEN
87 rsa.sin_len = sizeof(sa); 88 rsa.sin_len = sizeof(sa);
88#endif 89#endif
89 rsa.sin_addr.s_addr = dst_ipv4; 90 rsa.sin_addr.s_addr = dst_ipv4;
90 rsa.sin_port = htons(dport); 91 rsa.sin_port = htons (dport);
91 sa_len = sizeof(lsa); 92 sa_len = sizeof(lsa);
92 sa = (const struct sockaddr *)&lsa; 93 sa = (const struct sockaddr *) &lsa;
93 h = GNUNET_NAT_register(cfg, 94 h = GNUNET_NAT_register (cfg,
94 "none", 95 "none",
95 is_tcp ? IPPROTO_TCP : IPPROTO_UDP, 96 is_tcp ? IPPROTO_TCP : IPPROTO_UDP,
96 1, 97 1,
97 &sa, 98 &sa,
98 &sa_len, 99 &sa_len,
99 NULL, NULL, NULL); 100 NULL, NULL, NULL);
100 GNUNET_NAT_request_reversal(h, 101 GNUNET_NAT_request_reversal (h,
101 &lsa, 102 &lsa,
102 &rsa); 103 &rsa);
103 GNUNET_NAT_unregister(h); 104 GNUNET_NAT_unregister (h);
104} 105}
105 106
106 107
107/** 108/**
108 * Closure for #tcp_send. 109 * Closure for #tcp_send.
109 */ 110 */
110struct TcpContext { 111struct TcpContext
112{
111 /** 113 /**
112 * TCP socket. 114 * TCP socket.
113 */ 115 */
@@ -127,24 +129,24 @@ struct TcpContext {
127 * @param cls the `struct TcpContext` 129 * @param cls the `struct TcpContext`
128 */ 130 */
129static void 131static void
130tcp_send(void *cls) 132tcp_send (void *cls)
131{ 133{
132 struct TcpContext *ctx = cls; 134 struct TcpContext *ctx = cls;
133 const struct GNUNET_SCHEDULER_TaskContext *tc; 135 const struct GNUNET_SCHEDULER_TaskContext *tc;
134 136
135 tc = GNUNET_SCHEDULER_get_task_context(); 137 tc = GNUNET_SCHEDULER_get_task_context ();
136 if ((NULL != tc->write_ready) && 138 if ((NULL != tc->write_ready) &&
137 (GNUNET_NETWORK_fdset_isset(tc->write_ready, ctx->s))) 139 (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s)))
140 {
141 if (-1 ==
142 GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof(ctx->data)))
138 { 143 {
139 if (-1 == 144 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
140 GNUNET_NETWORK_socket_send(ctx->s, &ctx->data, sizeof(ctx->data)))
141 {
142 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "send");
143 }
144 GNUNET_NETWORK_socket_shutdown(ctx->s, SHUT_RDWR);
145 } 145 }
146 GNUNET_NETWORK_socket_close(ctx->s); 146 GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR);
147 GNUNET_free(ctx); 147 }
148 GNUNET_NETWORK_socket_close (ctx->s);
149 GNUNET_free (ctx);
148} 150}
149 151
150 152
@@ -157,52 +159,52 @@ tcp_send(void *cls)
157 * @param data data to send 159 * @param data data to send
158 */ 160 */
159static void 161static void
160try_send_tcp(uint32_t dst_ipv4, 162try_send_tcp (uint32_t dst_ipv4,
161 uint16_t dport, 163 uint16_t dport,
162 uint16_t data) 164 uint16_t data)
163{ 165{
164 struct GNUNET_NETWORK_Handle *s; 166 struct GNUNET_NETWORK_Handle *s;
165 struct sockaddr_in sa; 167 struct sockaddr_in sa;
166 struct TcpContext *ctx; 168 struct TcpContext *ctx;
167 169
168 s = GNUNET_NETWORK_socket_create(AF_INET, 170 s = GNUNET_NETWORK_socket_create (AF_INET,
169 SOCK_STREAM, 171 SOCK_STREAM,
170 0); 172 0);
171 if (NULL == s) 173 if (NULL == s)
172 { 174 {
173 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 175 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
174 "socket"); 176 "socket");
175 return; 177 return;
176 } 178 }
177 memset(&sa, 0, sizeof(sa)); 179 memset (&sa, 0, sizeof(sa));
178 sa.sin_family = AF_INET; 180 sa.sin_family = AF_INET;
179#if HAVE_SOCKADDR_IN_SIN_LEN 181#if HAVE_SOCKADDR_IN_SIN_LEN
180 sa.sin_len = sizeof(sa); 182 sa.sin_len = sizeof(sa);
181#endif 183#endif
182 sa.sin_addr.s_addr = dst_ipv4; 184 sa.sin_addr.s_addr = dst_ipv4;
183 sa.sin_port = htons(dport); 185 sa.sin_port = htons (dport);
184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Sending TCP message to `%s'\n", 187 "Sending TCP message to `%s'\n",
186 GNUNET_a2s((struct sockaddr *)&sa, 188 GNUNET_a2s ((struct sockaddr *) &sa,
187 sizeof(sa))); 189 sizeof(sa)));
188 if ((GNUNET_OK != 190 if ((GNUNET_OK !=
189 GNUNET_NETWORK_socket_connect(s, 191 GNUNET_NETWORK_socket_connect (s,
190 (const struct sockaddr *)&sa, 192 (const struct sockaddr *) &sa,
191 sizeof(sa))) && 193 sizeof(sa))) &&
192 (errno != EINPROGRESS)) 194 (errno != EINPROGRESS))
193 { 195 {
194 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
195 "connect"); 197 "connect");
196 GNUNET_NETWORK_socket_close(s); 198 GNUNET_NETWORK_socket_close (s);
197 return; 199 return;
198 } 200 }
199 ctx = GNUNET_new(struct TcpContext); 201 ctx = GNUNET_new (struct TcpContext);
200 ctx->s = s; 202 ctx->s = s;
201 ctx->data = data; 203 ctx->data = data;
202 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_SECONDS, 204 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
203 s, 205 s,
204 &tcp_send, 206 &tcp_send,
205 ctx); 207 ctx);
206} 208}
207 209
208 210
@@ -215,42 +217,42 @@ try_send_tcp(uint32_t dst_ipv4,
215 * @param data data to send 217 * @param data data to send
216 */ 218 */
217static void 219static void
218try_send_udp(uint32_t dst_ipv4, 220try_send_udp (uint32_t dst_ipv4,
219 uint16_t dport, 221 uint16_t dport,
220 uint16_t data) 222 uint16_t data)
221{ 223{
222 struct GNUNET_NETWORK_Handle *s; 224 struct GNUNET_NETWORK_Handle *s;
223 struct sockaddr_in sa; 225 struct sockaddr_in sa;
224 226
225 s = GNUNET_NETWORK_socket_create(AF_INET, 227 s = GNUNET_NETWORK_socket_create (AF_INET,
226 SOCK_DGRAM, 228 SOCK_DGRAM,
227 0); 229 0);
228 if (NULL == s) 230 if (NULL == s)
229 { 231 {
230 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 232 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
231 "socket"); 233 "socket");
232 return; 234 return;
233 } 235 }
234 memset(&sa, 0, sizeof(sa)); 236 memset (&sa, 0, sizeof(sa));
235 sa.sin_family = AF_INET; 237 sa.sin_family = AF_INET;
236#if HAVE_SOCKADDR_IN_SIN_LEN 238#if HAVE_SOCKADDR_IN_SIN_LEN
237 sa.sin_len = sizeof(sa); 239 sa.sin_len = sizeof(sa);
238#endif 240#endif
239 sa.sin_addr.s_addr = dst_ipv4; 241 sa.sin_addr.s_addr = dst_ipv4;
240 sa.sin_port = htons(dport); 242 sa.sin_port = htons (dport);
241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Sending UDP packet to `%s'\n", 244 "Sending UDP packet to `%s'\n",
243 GNUNET_a2s((struct sockaddr *)&sa, 245 GNUNET_a2s ((struct sockaddr *) &sa,
244 sizeof(sa))); 246 sizeof(sa)));
245 if (-1 == 247 if (-1 ==
246 GNUNET_NETWORK_socket_sendto(s, 248 GNUNET_NETWORK_socket_sendto (s,
247 &data, 249 &data,
248 sizeof(data), 250 sizeof(data),
249 (const struct sockaddr *)&sa, 251 (const struct sockaddr *) &sa,
250 sizeof(sa))) 252 sizeof(sa)))
251 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
252 "sendto"); 254 "sendto");
253 GNUNET_NETWORK_socket_close(s); 255 GNUNET_NETWORK_socket_close (s);
254} 256}
255 257
256 258
@@ -262,28 +264,28 @@ try_send_udp(uint32_t dst_ipv4,
262 * @param msg message with details about what to test 264 * @param msg message with details about what to test
263 */ 265 */
264static void 266static void
265handle_test(void *cls, 267handle_test (void *cls,
266 const struct GNUNET_NAT_AUTO_TestMessage *tm) 268 const struct GNUNET_NAT_AUTO_TestMessage *tm)
267{ 269{
268 struct ClientData *cd = cls; 270 struct ClientData *cd = cls;
269 uint16_t dport; 271 uint16_t dport;
270 272
271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Received test request\n"); 274 "Received test request\n");
273 dport = ntohs(tm->dport); 275 dport = ntohs (tm->dport);
274 if (0 == dport) 276 if (0 == dport)
275 try_anat(tm->dst_ipv4, 277 try_anat (tm->dst_ipv4,
276 ntohs(tm->data), 278 ntohs (tm->data),
277 (int)ntohl(tm->is_tcp)); 279 (int) ntohl (tm->is_tcp));
278 else if (GNUNET_YES == ntohl(tm->is_tcp)) 280 else if (GNUNET_YES == ntohl (tm->is_tcp))
279 try_send_tcp(tm->dst_ipv4, 281 try_send_tcp (tm->dst_ipv4,
280 dport, 282 dport,
281 tm->data); 283 tm->data);
282 else 284 else
283 try_send_udp(tm->dst_ipv4, 285 try_send_udp (tm->dst_ipv4,
284 dport, 286 dport,
285 tm->data); 287 tm->data);
286 GNUNET_SERVICE_client_drop(cd->client); 288 GNUNET_SERVICE_client_drop (cd->client);
287} 289}
288 290
289 291
@@ -295,9 +297,9 @@ handle_test(void *cls,
295 * @param srv service handle 297 * @param srv service handle
296 */ 298 */
297static void 299static void
298run(void *cls, 300run (void *cls,
299 const struct GNUNET_CONFIGURATION_Handle *c, 301 const struct GNUNET_CONFIGURATION_Handle *c,
300 struct GNUNET_SERVICE_Handle *srv) 302 struct GNUNET_SERVICE_Handle *srv)
301{ 303{
302 cfg = c; 304 cfg = c;
303} 305}
@@ -309,12 +311,12 @@ run(void *cls,
309 * @param cls our `struct ClientData` of a client to drop 311 * @param cls our `struct ClientData` of a client to drop
310 */ 312 */
311static void 313static void
312force_timeout(void *cls) 314force_timeout (void *cls)
313{ 315{
314 struct ClientData *cd = cls; 316 struct ClientData *cd = cls;
315 317
316 cd->tt = NULL; 318 cd->tt = NULL;
317 GNUNET_SERVICE_client_drop(cd->client); 319 GNUNET_SERVICE_client_drop (cd->client);
318} 320}
319 321
320 322
@@ -328,17 +330,17 @@ force_timeout(void *cls)
328 * @return our `struct ClientData` 330 * @return our `struct ClientData`
329 */ 331 */
330static void * 332static void *
331client_connect_cb(void *cls, 333client_connect_cb (void *cls,
332 struct GNUNET_SERVICE_Client *c, 334 struct GNUNET_SERVICE_Client *c,
333 struct GNUNET_MQ_Handle *mq) 335 struct GNUNET_MQ_Handle *mq)
334{ 336{
335 struct ClientData *cd; 337 struct ClientData *cd;
336 338
337 cd = GNUNET_new(struct ClientData); 339 cd = GNUNET_new (struct ClientData);
338 cd->client = c; 340 cd->client = c;
339 cd->tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, 341 cd->tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
340 &force_timeout, 342 &force_timeout,
341 cd); 343 cd);
342 return cd; 344 return cd;
343} 345}
344 346
@@ -351,15 +353,15 @@ client_connect_cb(void *cls,
351 * @param internal_cls our `struct ClientData` 353 * @param internal_cls our `struct ClientData`
352 */ 354 */
353static void 355static void
354client_disconnect_cb(void *cls, 356client_disconnect_cb (void *cls,
355 struct GNUNET_SERVICE_Client *c, 357 struct GNUNET_SERVICE_Client *c,
356 void *internal_cls) 358 void *internal_cls)
357{ 359{
358 struct ClientData *cd = internal_cls; 360 struct ClientData *cd = internal_cls;
359 361
360 if (NULL != cd->tt) 362 if (NULL != cd->tt)
361 GNUNET_SCHEDULER_cancel(cd->tt); 363 GNUNET_SCHEDULER_cancel (cd->tt);
362 GNUNET_free(cd); 364 GNUNET_free (cd);
363} 365}
364 366
365 367
@@ -373,11 +375,11 @@ GNUNET_SERVICE_MAIN
373 &client_connect_cb, 375 &client_connect_cb,
374 &client_disconnect_cb, 376 &client_disconnect_cb,
375 NULL, 377 NULL,
376 GNUNET_MQ_hd_fixed_size(test, 378 GNUNET_MQ_hd_fixed_size (test,
377 GNUNET_MESSAGE_TYPE_NAT_TEST, 379 GNUNET_MESSAGE_TYPE_NAT_TEST,
378 struct GNUNET_NAT_AUTO_TestMessage, 380 struct GNUNET_NAT_AUTO_TestMessage,
379 NULL), 381 NULL),
380 GNUNET_MQ_handler_end()); 382 GNUNET_MQ_handler_end ());
381 383
382 384
383#if defined(LINUX) && defined(__GLIBC__) 385#if defined(LINUX) && defined(__GLIBC__)
@@ -387,11 +389,11 @@ GNUNET_SERVICE_MAIN
387 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 389 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
388 */ 390 */
389void __attribute__ ((constructor)) 391void __attribute__ ((constructor))
390GNUNET_ARM_memory_init() 392GNUNET_ARM_memory_init ()
391{ 393{
392 mallopt(M_TRIM_THRESHOLD, 4 * 1024); 394 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
393 mallopt(M_TOP_PAD, 1 * 1024); 395 mallopt (M_TOP_PAD, 1 * 1024);
394 malloc_trim(0); 396 malloc_trim (0);
395} 397}
396#endif 398#endif
397 399
diff --git a/src/nat-auto/gnunet-service-nat-auto.c b/src/nat-auto/gnunet-service-nat-auto.c
index 199a759ea..2c7faae2e 100644
--- a/src/nat-auto/gnunet-service-nat-auto.c
+++ b/src/nat-auto/gnunet-service-nat-auto.c
@@ -45,13 +45,15 @@
45/** 45/**
46 * How long do we wait until we forcefully terminate autoconfiguration? 46 * How long do we wait until we forcefully terminate autoconfiguration?
47 */ 47 */
48#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 48#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply ( \
49 GNUNET_TIME_UNIT_SECONDS, 5)
49 50
50 51
51/** 52/**
52 * Internal data structure we track for each of our clients. 53 * Internal data structure we track for each of our clients.
53 */ 54 */
54struct ClientHandle { 55struct ClientHandle
56{
55 /** 57 /**
56 * Kept in a DLL. 58 * Kept in a DLL.
57 */ 59 */
@@ -77,7 +79,8 @@ struct ClientHandle {
77/** 79/**
78 * Context for autoconfiguration operations. 80 * Context for autoconfiguration operations.
79 */ 81 */
80struct AutoconfigContext { 82struct AutoconfigContext
83{
81 /** 84 /**
82 * Kept in a DLL. 85 * Kept in a DLL.
83 */ 86 */
@@ -167,8 +170,9 @@ static struct GNUNET_STATISTICS_Handle *stats;
167 * @return #GNUNET_OK if message is well-formed 170 * @return #GNUNET_OK if message is well-formed
168 */ 171 */
169static int 172static int
170check_autoconfig_request(void *cls, 173check_autoconfig_request (void *cls,
171 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) 174 const struct
175 GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
172{ 176{
173 return GNUNET_OK; /* checked later */ 177 return GNUNET_OK; /* checked later */
174} 178}
@@ -180,13 +184,13 @@ check_autoconfig_request(void *cls,
180 * @param ac autoconfiguration to terminate activities for 184 * @param ac autoconfiguration to terminate activities for
181 */ 185 */
182static void 186static void
183terminate_ac_activities(struct AutoconfigContext *ac) 187terminate_ac_activities (struct AutoconfigContext *ac)
184{ 188{
185 if (NULL != ac->timeout_task) 189 if (NULL != ac->timeout_task)
186 { 190 {
187 GNUNET_SCHEDULER_cancel(ac->timeout_task); 191 GNUNET_SCHEDULER_cancel (ac->timeout_task);
188 ac->timeout_task = NULL; 192 ac->timeout_task = NULL;
189 } 193 }
190} 194}
191 195
192 196
@@ -197,7 +201,7 @@ terminate_ac_activities(struct AutoconfigContext *ac)
197 * @param cls the `struct AutoconfigContext` to conclude 201 * @param cls the `struct AutoconfigContext` to conclude
198 */ 202 */
199static void 203static void
200conclude_autoconfig_request(void *cls) 204conclude_autoconfig_request (void *cls)
201{ 205{
202 struct AutoconfigContext *ac = cls; 206 struct AutoconfigContext *ac = cls;
203 struct ClientHandle *ch = ac->ch; 207 struct ClientHandle *ch = ac->ch;
@@ -208,34 +212,34 @@ conclude_autoconfig_request(void *cls)
208 struct GNUNET_CONFIGURATION_Handle *diff; 212 struct GNUNET_CONFIGURATION_Handle *diff;
209 213
210 ac->timeout_task = NULL; 214 ac->timeout_task = NULL;
211 terminate_ac_activities(ac); 215 terminate_ac_activities (ac);
212 216
213 /* Send back response */ 217 /* Send back response */
214 diff = GNUNET_CONFIGURATION_get_diff(ac->orig, 218 diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
215 ac->c); 219 ac->c);
216 buf = GNUNET_CONFIGURATION_serialize(diff, 220 buf = GNUNET_CONFIGURATION_serialize (diff,
217 &c_size); 221 &c_size);
218 GNUNET_CONFIGURATION_destroy(diff); 222 GNUNET_CONFIGURATION_destroy (diff);
219 env = GNUNET_MQ_msg_extra(arm, 223 env = GNUNET_MQ_msg_extra (arm,
220 c_size, 224 c_size,
221 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT); 225 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
222 arm->status_code = htonl((uint32_t)ac->status_code); 226 arm->status_code = htonl ((uint32_t) ac->status_code);
223 arm->type = htonl((uint32_t)ac->type); 227 arm->type = htonl ((uint32_t) ac->type);
224 GNUNET_memcpy(&arm[1], 228 GNUNET_memcpy (&arm[1],
225 buf, 229 buf,
226 c_size); 230 c_size);
227 GNUNET_free(buf); 231 GNUNET_free (buf);
228 GNUNET_MQ_send(ch->mq, 232 GNUNET_MQ_send (ch->mq,
229 env); 233 env);
230 234
231 /* clean up */ 235 /* clean up */
232 GNUNET_CONFIGURATION_destroy(ac->orig); 236 GNUNET_CONFIGURATION_destroy (ac->orig);
233 GNUNET_CONFIGURATION_destroy(ac->c); 237 GNUNET_CONFIGURATION_destroy (ac->c);
234 GNUNET_CONTAINER_DLL_remove(ac_head, 238 GNUNET_CONTAINER_DLL_remove (ac_head,
235 ac_tail, 239 ac_tail,
236 ac); 240 ac);
237 GNUNET_free(ac); 241 GNUNET_free (ac);
238 GNUNET_SERVICE_client_continue(ch->client); 242 GNUNET_SERVICE_client_continue (ch->client);
239} 243}
240 244
241 245
@@ -246,12 +250,12 @@ conclude_autoconfig_request(void *cls)
246 * @param ac autoconfiguation context to check 250 * @param ac autoconfiguation context to check
247 */ 251 */
248static void 252static void
249check_autoconfig_finished(struct AutoconfigContext *ac) 253check_autoconfig_finished (struct AutoconfigContext *ac)
250{ 254{
251 GNUNET_SCHEDULER_cancel(ac->timeout_task); 255 GNUNET_SCHEDULER_cancel (ac->timeout_task);
252 ac->timeout_task 256 ac->timeout_task
253 = GNUNET_SCHEDULER_add_now(&conclude_autoconfig_request, 257 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
254 ac); 258 ac);
255} 259}
256 260
257 261
@@ -261,28 +265,28 @@ check_autoconfig_finished(struct AutoconfigContext *ac)
261 * @param ac autoconfiguration to update 265 * @param ac autoconfiguration to update
262 */ 266 */
263static void 267static void
264update_enable_upnpc_option(struct AutoconfigContext *ac) 268update_enable_upnpc_option (struct AutoconfigContext *ac)
265{ 269{
266 switch (ac->enable_upnpc) 270 switch (ac->enable_upnpc)
267 { 271 {
268 case GNUNET_YES: 272 case GNUNET_YES:
269 GNUNET_CONFIGURATION_set_value_string(ac->c, 273 GNUNET_CONFIGURATION_set_value_string (ac->c,
270 "NAT", 274 "NAT",
271 "ENABLE_UPNP", 275 "ENABLE_UPNP",
272 "YES"); 276 "YES");
273 break; 277 break;
274 278
275 case GNUNET_NO: 279 case GNUNET_NO:
276 GNUNET_CONFIGURATION_set_value_string(ac->c, 280 GNUNET_CONFIGURATION_set_value_string (ac->c,
277 "NAT", 281 "NAT",
278 "ENABLE_UPNP", 282 "ENABLE_UPNP",
279 "NO"); 283 "NO");
280 break; 284 break;
281 285
282 case GNUNET_SYSERR: 286 case GNUNET_SYSERR:
283 /* We are unsure, do not change option */ 287 /* We are unsure, do not change option */
284 break; 288 break;
285 } 289 }
286} 290}
287 291
288 292
@@ -294,62 +298,63 @@ update_enable_upnpc_option(struct AutoconfigContext *ac)
294 * @param message the message received 298 * @param message the message received
295 */ 299 */
296static void 300static void
297handle_autoconfig_request(void *cls, 301handle_autoconfig_request (void *cls,
298 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) 302 const struct
303 GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
299{ 304{
300 struct ClientHandle *ch = cls; 305 struct ClientHandle *ch = cls;
301 size_t left = ntohs(message->header.size) - sizeof(*message); 306 size_t left = ntohs (message->header.size) - sizeof(*message);
302 struct AutoconfigContext *ac; 307 struct AutoconfigContext *ac;
303 308
304 ac = GNUNET_new(struct AutoconfigContext); 309 ac = GNUNET_new (struct AutoconfigContext);
305 ac->status_code = GNUNET_NAT_ERROR_SUCCESS; 310 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
306 ac->ch = ch; 311 ac->ch = ch;
307 ac->c = GNUNET_CONFIGURATION_create(); 312 ac->c = GNUNET_CONFIGURATION_create ();
308 if (GNUNET_OK != 313 if (GNUNET_OK !=
309 GNUNET_CONFIGURATION_deserialize(ac->c, 314 GNUNET_CONFIGURATION_deserialize (ac->c,
310 (const char *)&message[1], 315 (const char *) &message[1],
311 left, 316 left,
312 NULL)) 317 NULL))
313 { 318 {
314 GNUNET_break(0); 319 GNUNET_break (0);
315 GNUNET_SERVICE_client_drop(ch->client); 320 GNUNET_SERVICE_client_drop (ch->client);
316 GNUNET_CONFIGURATION_destroy(ac->c); 321 GNUNET_CONFIGURATION_destroy (ac->c);
317 GNUNET_free(ac); 322 GNUNET_free (ac);
318 return; 323 return;
319 } 324 }
320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Received REQUEST_AUTO_CONFIG message from client\n"); 326 "Received REQUEST_AUTO_CONFIG message from client\n");
322 327
323 GNUNET_CONTAINER_DLL_insert(ac_head, 328 GNUNET_CONTAINER_DLL_insert (ac_head,
324 ac_tail, 329 ac_tail,
325 ac); 330 ac);
326 ac->orig 331 ac->orig
327 = GNUNET_CONFIGURATION_dup(ac->c); 332 = GNUNET_CONFIGURATION_dup (ac->c);
328 ac->timeout_task 333 ac->timeout_task
329 = GNUNET_SCHEDULER_add_delayed(AUTOCONFIG_TIMEOUT, 334 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
330 &conclude_autoconfig_request, 335 &conclude_autoconfig_request,
331 ac); 336 ac);
332 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */ 337 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
333 338
334 /* Probe for upnpc */ 339 /* Probe for upnpc */
335 if (GNUNET_SYSERR == 340 if (GNUNET_SYSERR ==
336 GNUNET_OS_check_helper_binary("upnpc", 341 GNUNET_OS_check_helper_binary ("upnpc",
337 GNUNET_NO, 342 GNUNET_NO,
338 NULL)) 343 NULL))
339 { 344 {
340 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
341 _("UPnP client `upnpc` command not found, disabling UPnP\n")); 346 _ ("UPnP client `upnpc` command not found, disabling UPnP\n"));
342 ac->enable_upnpc = GNUNET_NO; 347 ac->enable_upnpc = GNUNET_NO;
343 } 348 }
344 else 349 else
345 { 350 {
346 /* We might at some point be behind NAT, try upnpc */ 351 /* We might at some point be behind NAT, try upnpc */
347 ac->enable_upnpc = GNUNET_YES; 352 ac->enable_upnpc = GNUNET_YES;
348 } 353 }
349 update_enable_upnpc_option(ac); 354 update_enable_upnpc_option (ac);
350 355
351 /* Finally, check if we are already done */ 356 /* Finally, check if we are already done */
352 check_autoconfig_finished(ac); 357 check_autoconfig_finished (ac);
353} 358}
354 359
355 360
@@ -359,24 +364,24 @@ handle_autoconfig_request(void *cls,
359 * @param cls unused 364 * @param cls unused
360 */ 365 */
361static void 366static void
362shutdown_task(void *cls) 367shutdown_task (void *cls)
363{ 368{
364 struct AutoconfigContext *ac; 369 struct AutoconfigContext *ac;
365 370
366 while (NULL != (ac = ac_head)) 371 while (NULL != (ac = ac_head))
367 { 372 {
368 GNUNET_CONTAINER_DLL_remove(ac_head, 373 GNUNET_CONTAINER_DLL_remove (ac_head,
369 ac_tail, 374 ac_tail,
370 ac); 375 ac);
371 terminate_ac_activities(ac); 376 terminate_ac_activities (ac);
372 GNUNET_free(ac); 377 GNUNET_free (ac);
373 } 378 }
374 if (NULL != stats) 379 if (NULL != stats)
375 { 380 {
376 GNUNET_STATISTICS_destroy(stats, 381 GNUNET_STATISTICS_destroy (stats,
377 GNUNET_NO); 382 GNUNET_NO);
378 stats = NULL; 383 stats = NULL;
379 } 384 }
380} 385}
381 386
382 387
@@ -388,15 +393,15 @@ shutdown_task(void *cls)
388 * @param service the initialized service 393 * @param service the initialized service
389 */ 394 */
390static void 395static void
391run(void *cls, 396run (void *cls,
392 const struct GNUNET_CONFIGURATION_Handle *c, 397 const struct GNUNET_CONFIGURATION_Handle *c,
393 struct GNUNET_SERVICE_Handle *service) 398 struct GNUNET_SERVICE_Handle *service)
394{ 399{
395 cfg = c; 400 cfg = c;
396 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, 401 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
397 NULL); 402 NULL);
398 stats = GNUNET_STATISTICS_create("nat-auto", 403 stats = GNUNET_STATISTICS_create ("nat-auto",
399 cfg); 404 cfg);
400} 405}
401 406
402 407
@@ -409,18 +414,18 @@ run(void *cls,
409 * @return a `struct ClientHandle` 414 * @return a `struct ClientHandle`
410 */ 415 */
411static void * 416static void *
412client_connect_cb(void *cls, 417client_connect_cb (void *cls,
413 struct GNUNET_SERVICE_Client *c, 418 struct GNUNET_SERVICE_Client *c,
414 struct GNUNET_MQ_Handle *mq) 419 struct GNUNET_MQ_Handle *mq)
415{ 420{
416 struct ClientHandle *ch; 421 struct ClientHandle *ch;
417 422
418 ch = GNUNET_new(struct ClientHandle); 423 ch = GNUNET_new (struct ClientHandle);
419 ch->mq = mq; 424 ch->mq = mq;
420 ch->client = c; 425 ch->client = c;
421 GNUNET_CONTAINER_DLL_insert(ch_head, 426 GNUNET_CONTAINER_DLL_insert (ch_head,
422 ch_tail, 427 ch_tail,
423 ch); 428 ch);
424 return ch; 429 return ch;
425} 430}
426 431
@@ -433,16 +438,16 @@ client_connect_cb(void *cls,
433 * @param internal_cls a `struct ClientHandle *` 438 * @param internal_cls a `struct ClientHandle *`
434 */ 439 */
435static void 440static void
436client_disconnect_cb(void *cls, 441client_disconnect_cb (void *cls,
437 struct GNUNET_SERVICE_Client *c, 442 struct GNUNET_SERVICE_Client *c,
438 void *internal_cls) 443 void *internal_cls)
439{ 444{
440 struct ClientHandle *ch = internal_cls; 445 struct ClientHandle *ch = internal_cls;
441 446
442 GNUNET_CONTAINER_DLL_remove(ch_head, 447 GNUNET_CONTAINER_DLL_remove (ch_head,
443 ch_tail, 448 ch_tail,
444 ch); 449 ch);
445 GNUNET_free(ch); 450 GNUNET_free (ch);
446} 451}
447 452
448 453
@@ -456,11 +461,11 @@ GNUNET_SERVICE_MAIN
456 &client_connect_cb, 461 &client_connect_cb,
457 &client_disconnect_cb, 462 &client_disconnect_cb,
458 NULL, 463 NULL,
459 GNUNET_MQ_hd_var_size(autoconfig_request, 464 GNUNET_MQ_hd_var_size (autoconfig_request,
460 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, 465 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
461 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage, 466 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
462 NULL), 467 NULL),
463 GNUNET_MQ_handler_end()); 468 GNUNET_MQ_handler_end ());
464 469
465 470
466#if defined(LINUX) && defined(__GLIBC__) 471#if defined(LINUX) && defined(__GLIBC__)
@@ -470,11 +475,11 @@ GNUNET_SERVICE_MAIN
470 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 475 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
471 */ 476 */
472void __attribute__ ((constructor)) 477void __attribute__ ((constructor))
473GNUNET_ARM_memory_init() 478GNUNET_ARM_memory_init ()
474{ 479{
475 mallopt(M_TRIM_THRESHOLD, 4 * 1024); 480 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
476 mallopt(M_TOP_PAD, 1 * 1024); 481 mallopt (M_TOP_PAD, 1 * 1024);
477 malloc_trim(0); 482 malloc_trim (0);
478} 483}
479#endif 484#endif
480 485
diff --git a/src/nat-auto/gnunet-service-nat-auto_legacy.c b/src/nat-auto/gnunet-service-nat-auto_legacy.c
index 30f1f855d..1817c2270 100644
--- a/src/nat-auto/gnunet-service-nat-auto_legacy.c
+++ b/src/nat-auto/gnunet-service-nat-auto_legacy.c
@@ -30,20 +30,22 @@
30#include "gnunet_nat_lib.h" 30#include "gnunet_nat_lib.h"
31#include "nat.h" 31#include "nat.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * How long do we wait for the NAT test to report success? 37 * How long do we wait for the NAT test to report success?
38 */ 38 */
39#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) 39#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
40 40
41#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10) 41#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply ( \
42 GNUNET_TIME_UNIT_SECONDS, 10)
42 43
43/** 44/**
44 * Phases of the auto configuration. 45 * Phases of the auto configuration.
45 */ 46 */
46enum AutoPhase { 47enum AutoPhase
48{
47 /** 49 /**
48 * Initial start value. 50 * Initial start value.
49 */ 51 */
@@ -94,7 +96,8 @@ enum AutoPhase {
94/** 96/**
95 * Handle to auto-configuration in progress. 97 * Handle to auto-configuration in progress.
96 */ 98 */
97struct GNUNET_NAT_AutoHandle { 99struct GNUNET_NAT_AutoHandle
100{
98 /** 101 /**
99 * Handle to the active NAT test. 102 * Handle to the active NAT test.
100 */ 103 */
@@ -205,20 +208,20 @@ static unsigned int stun_port = 3478;
205 * @param ah auto test handle 208 * @param ah auto test handle
206 */ 209 */
207static void 210static void
208next_phase(struct GNUNET_NAT_AutoHandle *ah); 211next_phase (struct GNUNET_NAT_AutoHandle *ah);
209 212
210 213
211static void 214static void
212process_stun_reply(struct sockaddr_in *answer, 215process_stun_reply (struct sockaddr_in *answer,
213 struct GNUNET_NAT_AutoHandle *ah) 216 struct GNUNET_NAT_AutoHandle *ah)
214{ 217{
215 ah->stun_ip = inet_ntoa(answer->sin_addr); 218 ah->stun_ip = inet_ntoa (answer->sin_addr);
216 ah->stun_port = ntohs(answer->sin_port); 219 ah->stun_port = ntohs (answer->sin_port);
217 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 220 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
218 "External IP is: %s , with port %u\n", 221 "External IP is: %s , with port %u\n",
219 ah->stun_ip, 222 ah->stun_ip,
220 ah->stun_port); 223 ah->stun_port);
221 next_phase(ah); 224 next_phase (ah);
222} 225}
223 226
224 227
@@ -226,22 +229,22 @@ process_stun_reply(struct sockaddr_in *answer,
226 * Function that terminates the test. 229 * Function that terminates the test.
227 */ 230 */
228static void 231static void
229stop_stun() 232stop_stun ()
230{ 233{
231 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
232 "Stopping STUN and quitting...\n"); 235 "Stopping STUN and quitting...\n");
233 /* Clean task */ 236 /* Clean task */
234 if (NULL != ltask4) 237 if (NULL != ltask4)
235 { 238 {
236 GNUNET_SCHEDULER_cancel(ltask4); 239 GNUNET_SCHEDULER_cancel (ltask4);
237 ltask4 = NULL; 240 ltask4 = NULL;
238 } 241 }
239 /* Clean socket */ 242 /* Clean socket */
240 if (NULL != lsock4) 243 if (NULL != lsock4)
241 { 244 {
242 GNUNET_NETWORK_socket_close(lsock4); 245 GNUNET_NETWORK_socket_close (lsock4);
243 lsock4 = NULL; 246 lsock4 = NULL;
244 } 247 }
245} 248}
246 249
247 250
@@ -252,7 +255,7 @@ stop_stun()
252 * @param cls 255 * @param cls
253 */ 256 */
254static void 257static void
255do_udp_read(void *cls) 258do_udp_read (void *cls)
256{ 259{
257 struct GNUNET_NAT_AutoHandle *ah = cls; 260 struct GNUNET_NAT_AutoHandle *ah = cls;
258 unsigned char reply_buf[1024]; 261 unsigned char reply_buf[1024];
@@ -260,51 +263,51 @@ do_udp_read(void *cls)
260 struct sockaddr_in answer; 263 struct sockaddr_in answer;
261 const struct GNUNET_SCHEDULER_TaskContext *tc; 264 const struct GNUNET_SCHEDULER_TaskContext *tc;
262 265
263 tc = GNUNET_SCHEDULER_get_task_context(); 266 tc = GNUNET_SCHEDULER_get_task_context ();
264 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && 267 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
265 (GNUNET_NETWORK_fdset_isset(tc->read_ready, 268 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
266 lsock4))) 269 lsock4)))
270 {
271 rlen = GNUNET_NETWORK_socket_recv (lsock4,
272 reply_buf,
273 sizeof(reply_buf));
274
275 // Lets handle the packet
276 memset (&answer, 0, sizeof(struct sockaddr_in));
277 if (ah->phase == AUTO_NAT_PUNCHED)
267 { 278 {
268 rlen = GNUNET_NETWORK_socket_recv(lsock4, 279 // Destroy the connection
269 reply_buf, 280 GNUNET_NETWORK_socket_close (lsock4);
270 sizeof(reply_buf)); 281 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
271 282 "The external server was able to connect back");
272 //Lets handle the packet 283 ah->connected_back = GNUNET_YES;
273 memset(&answer, 0, sizeof(struct sockaddr_in)); 284 next_phase (ah);
274 if (ah->phase == AUTO_NAT_PUNCHED) 285 }
275 { 286 else
276 //Destroy the connection 287 {
277 GNUNET_NETWORK_socket_close(lsock4); 288 if (GNUNET_OK ==
278 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 289 GNUNET_NAT_stun_handle_packet (reply_buf, rlen, &answer))
279 "The external server was able to connect back"); 290 {
280 ah->connected_back = GNUNET_YES; 291 // Process the answer
281 next_phase(ah); 292 process_stun_reply (&answer, ah);
282 } 293 }
283 else 294 else
284 { 295 {
285 if (GNUNET_OK == 296 next_phase (ah);
286 GNUNET_NAT_stun_handle_packet(reply_buf, rlen, &answer)) 297 }
287 {
288 //Process the answer
289 process_stun_reply(&answer, ah);
290 }
291 else
292 {
293 next_phase(ah);
294 }
295 }
296 } 298 }
299 }
297 else 300 else
301 {
302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 "TIMEOUT while waiting for an answer\n");
304 if (ah->phase == AUTO_NAT_PUNCHED)
298 { 305 {
299 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 306 stop_stun ();
300 "TIMEOUT while waiting for an answer\n");
301 if (ah->phase == AUTO_NAT_PUNCHED)
302 {
303 stop_stun();
304 }
305
306 next_phase(ah);
307 } 307 }
308
309 next_phase (ah);
310 }
308} 311}
309 312
310 313
@@ -314,45 +317,45 @@ do_udp_read(void *cls)
314 * @return NULL on error 317 * @return NULL on error
315 */ 318 */
316static struct GNUNET_NETWORK_Handle * 319static struct GNUNET_NETWORK_Handle *
317bind_v4() 320bind_v4 ()
318{ 321{
319 struct GNUNET_NETWORK_Handle *ls; 322 struct GNUNET_NETWORK_Handle *ls;
320 struct sockaddr_in sa4; 323 struct sockaddr_in sa4;
321 int eno; 324 int eno;
322 325
323 memset(&sa4, 0, sizeof(sa4)); 326 memset (&sa4, 0, sizeof(sa4));
324 sa4.sin_family = AF_INET; 327 sa4.sin_family = AF_INET;
325 sa4.sin_port = htons(port); 328 sa4.sin_port = htons (port);
326#if HAVE_SOCKADDR_IN_SIN_LEN 329#if HAVE_SOCKADDR_IN_SIN_LEN
327 sa4.sin_len = sizeof(sa4); 330 sa4.sin_len = sizeof(sa4);
328#endif 331#endif
329 ls = GNUNET_NETWORK_socket_create(AF_INET, 332 ls = GNUNET_NETWORK_socket_create (AF_INET,
330 SOCK_DGRAM, 333 SOCK_DGRAM,
331 0); 334 0);
332 if (NULL == ls) 335 if (NULL == ls)
333 return NULL; 336 return NULL;
334 if (GNUNET_OK != 337 if (GNUNET_OK !=
335 GNUNET_NETWORK_socket_bind(ls, (const struct sockaddr *)&sa4, 338 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
336 sizeof(sa4))) 339 sizeof(sa4)))
337 { 340 {
338 eno = errno; 341 eno = errno;
339 GNUNET_NETWORK_socket_close(ls); 342 GNUNET_NETWORK_socket_close (ls);
340 errno = eno; 343 errno = eno;
341 return NULL; 344 return NULL;
342 } 345 }
343 return ls; 346 return ls;
344} 347}
345 348
346 349
347static void 350static void
348request_callback(void *cls, 351request_callback (void *cls,
349 enum GNUNET_NAT_StatusCode result) 352 enum GNUNET_NAT_StatusCode result)
350{ 353{
351 // struct GNUNET_NAT_AutoHandle *ah = cls; 354 // struct GNUNET_NAT_AutoHandle *ah = cls;
352 355
353 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 "Request callback: stop and quit\n"); 357 "Request callback: stop and quit\n");
355 stop_stun(); 358 stop_stun ();
356 359
357 // next_phase (ah); FIXME this always will be NULL, as called in test_stun() 360 // next_phase (ah); FIXME this always will be NULL, as called in test_stun()
358} 361}
@@ -367,22 +370,23 @@ request_callback(void *cls,
367 * @param emsg NULL on success, otherwise an error message 370 * @param emsg NULL on success, otherwise an error message
368 */ 371 */
369static void 372static void
370result_callback(void *cls, 373result_callback (void *cls,
371 enum GNUNET_NAT_StatusCode ret) 374 enum GNUNET_NAT_StatusCode ret)
372{ 375{
373 struct GNUNET_NAT_AutoHandle *ah = cls; 376 struct GNUNET_NAT_AutoHandle *ah = cls;
374 377
375 if (GNUNET_NAT_ERROR_SUCCESS == ret) 378 if (GNUNET_NAT_ERROR_SUCCESS == ret)
376 GNUNET_NAT_test_stop(ah->tst); 379 GNUNET_NAT_test_stop (ah->tst);
377 ah->tst = NULL; 380 ah->tst = NULL;
378 ah->ret = ret; 381 ah->ret = ret;
379 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 382 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
380 GNUNET_NAT_ERROR_SUCCESS == ret 383 GNUNET_NAT_ERROR_SUCCESS == ret
381 ? _("NAT traversal with ICMP Server succeeded.\n") 384 ? _ ("NAT traversal with ICMP Server succeeded.\n")
382 : _("NAT traversal with ICMP Server failed.\n")); 385 : _ ("NAT traversal with ICMP Server failed.\n"));
383 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "ENABLE_ICMP_SERVER", 386 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
384 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES"); 387 GNUNET_NAT_ERROR_SUCCESS == ret ?
385 next_phase(ah); 388 "NO" : "YES");
389 next_phase (ah);
386} 390}
387 391
388 392
@@ -392,16 +396,16 @@ result_callback(void *cls,
392 * @param cls the `struct GNUNET_NAT_AutoHandle` 396 * @param cls the `struct GNUNET_NAT_AutoHandle`
393 */ 397 */
394static void 398static void
395reversal_test(void *cls) 399reversal_test (void *cls)
396{ 400{
397 struct GNUNET_NAT_AutoHandle *ah = cls; 401 struct GNUNET_NAT_AutoHandle *ah = cls;
398 402
399 ah->task = NULL; 403 ah->task = NULL;
400 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 404 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
401 _("Testing connection reversal with ICMP server.\n")); 405 _ ("Testing connection reversal with ICMP server.\n"));
402 GNUNET_RESOLVER_connect(ah->cfg); 406 GNUNET_RESOLVER_connect (ah->cfg);
403 ah->tst = GNUNET_NAT_test_start(ah->cfg, GNUNET_YES, 0, 0, TIMEOUT, 407 ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
404 &result_callback, ah); 408 &result_callback, ah);
405} 409}
406 410
407 411
@@ -414,9 +418,9 @@ reversal_test(void *cls)
414 * @param emsg NULL on success, otherwise an error message 418 * @param emsg NULL on success, otherwise an error message
415 */ 419 */
416static void 420static void
417set_external_ipv4(void *cls, 421set_external_ipv4 (void *cls,
418 const struct in_addr *addr, 422 const struct in_addr *addr,
419 enum GNUNET_NAT_StatusCode ret) 423 enum GNUNET_NAT_StatusCode ret)
420{ 424{
421 struct GNUNET_NAT_AutoHandle *ah = cls; 425 struct GNUNET_NAT_AutoHandle *ah = cls;
422 char buf[INET_ADDRSTRLEN]; 426 char buf[INET_ADDRSTRLEN];
@@ -424,35 +428,35 @@ set_external_ipv4(void *cls,
424 ah->eh = NULL; 428 ah->eh = NULL;
425 ah->ret = ret; 429 ah->ret = ret;
426 if (GNUNET_NAT_ERROR_SUCCESS != ret) 430 if (GNUNET_NAT_ERROR_SUCCESS != ret)
427 { 431 {
428 next_phase(ah); 432 next_phase (ah);
429 return; 433 return;
430 } 434 }
431 /* enable 'behind nat' */ 435 /* enable 'behind nat' */
432 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 436 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433 _("Detected external IP `%s'\n"), 437 _ ("Detected external IP `%s'\n"),
434 inet_ntop(AF_INET, 438 inet_ntop (AF_INET,
435 addr, 439 addr,
436 buf, 440 buf,
437 sizeof(buf))); 441 sizeof(buf)));
438 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "BEHIND_NAT", "YES"); 442 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
439 443
440 /* set external IP address */ 444 /* set external IP address */
441 if (NULL == inet_ntop(AF_INET, addr, buf, sizeof(buf))) 445 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof(buf)))
442 { 446 {
443 GNUNET_break(0); 447 GNUNET_break (0);
444 /* actually, this should never happen, as the caller already executed just 448 /* actually, this should never happen, as the caller already executed just
445 * this check, but for consistency (eg: future changes in the caller) 449 * this check, but for consistency (eg: future changes in the caller)
446 * we still need to report this error... 450 * we still need to report this error...
447 */ 451 */
448 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; 452 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
449 next_phase(ah); 453 next_phase (ah);
450 return; 454 return;
451 } 455 }
452 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "EXTERNAL_ADDRESS", 456 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
453 buf); 457 buf);
454 ah->upnp_set_external_address = GNUNET_YES; 458 ah->upnp_set_external_address = GNUNET_YES;
455 next_phase(ah); 459 next_phase (ah);
456} 460}
457 461
458 462
@@ -462,15 +466,15 @@ set_external_ipv4(void *cls,
462 * @param ah auto setup context 466 * @param ah auto setup context
463 */ 467 */
464static void 468static void
465test_external_ip(struct GNUNET_NAT_AutoHandle *ah) 469test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
466{ 470{
467 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret) 471 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
468 next_phase(ah); 472 next_phase (ah);
469 473
470 // FIXME: CPS? 474 // FIXME: CPS?
471 /* try to detect external IP */ 475 /* try to detect external IP */
472 ah->eh = GNUNET_NAT_mini_get_external_ipv4(TIMEOUT, 476 ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
473 &set_external_ipv4, ah); 477 &set_external_ipv4, ah);
474} 478}
475 479
476 480
@@ -480,53 +484,53 @@ test_external_ip(struct GNUNET_NAT_AutoHandle *ah)
480 * @param ah auto setup context 484 * @param ah auto setup context
481 */ 485 */
482static void 486static void
483test_stun(struct GNUNET_NAT_AutoHandle *ah) 487test_stun (struct GNUNET_NAT_AutoHandle *ah)
484{ 488{
485 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running STUN test\n"); 489 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
486 490
487 /* Get port from the configuration */ 491 /* Get port from the configuration */
488 if (GNUNET_OK != 492 if (GNUNET_OK !=
489 GNUNET_CONFIGURATION_get_value_number(ah->cfg, 493 GNUNET_CONFIGURATION_get_value_number (ah->cfg,
490 "transport-udp", 494 "transport-udp",
491 "PORT", 495 "PORT",
492 &port)) 496 &port))
493 { 497 {
494 port = 2086; 498 port = 2086;
495 } 499 }
496 500
497 //Lets create the socket 501 // Lets create the socket
498 lsock4 = bind_v4(); 502 lsock4 = bind_v4 ();
499 if (NULL == lsock4) 503 if (NULL == lsock4)
500 { 504 {
501 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind"); 505 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
502 next_phase(ah); 506 next_phase (ah);
503 return; 507 return;
504 } 508 }
505 else 509 else
506 { 510 {
507 //Lets call our function now when it accepts 511 // Lets call our function now when it accepts
508 ltask4 = GNUNET_SCHEDULER_add_read_net(NAT_SERVER_TIMEOUT, 512 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
509 lsock4, 513 lsock4,
510 &do_udp_read, 514 &do_udp_read,
511 ah); 515 ah);
512 } 516 }
513 517
514 518
515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516 "STUN service listens on port %u\n", 520 "STUN service listens on port %u\n",
517 (unsigned int)port); 521 (unsigned int) port);
518 if (GNUNET_NO == 522 if (GNUNET_NO ==
519 GNUNET_NAT_stun_make_request(stun_server, 523 GNUNET_NAT_stun_make_request (stun_server,
520 stun_port, 524 stun_port,
521 lsock4, 525 lsock4,
522 &request_callback, 526 &request_callback,
523 NULL)) 527 NULL))
524 { 528 {
525 /*An error happened*/ 529 /*An error happened*/
526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n"); 530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n");
527 stop_stun(); 531 stop_stun ();
528 next_phase(ah); 532 next_phase (ah);
529 } 533 }
530} 534}
531 535
532 536
@@ -544,13 +548,13 @@ test_stun(struct GNUNET_NAT_AutoHandle *ah)
544 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort 548 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
545 */ 549 */
546static int 550static int
547process_if(void *cls, 551process_if (void *cls,
548 const char *name, 552 const char *name,
549 int isDefault, 553 int isDefault,
550 const struct sockaddr *addr, 554 const struct sockaddr *addr,
551 const struct sockaddr *broadcast_addr, 555 const struct sockaddr *broadcast_addr,
552 const struct sockaddr *netmask, 556 const struct sockaddr *netmask,
553 socklen_t addrlen) 557 socklen_t addrlen)
554{ 558{
555 struct GNUNET_NAT_AutoHandle *ah = cls; 559 struct GNUNET_NAT_AutoHandle *ah = cls;
556 const struct sockaddr_in *in; 560 const struct sockaddr_in *in;
@@ -558,46 +562,51 @@ process_if(void *cls,
558 562
559 563
560 if ((sizeof(struct sockaddr_in6) == addrlen) && 564 if ((sizeof(struct sockaddr_in6) == addrlen) &&
561 (0 != GNUNET_memcmp(&in6addr_loopback, &((const struct sockaddr_in6 *)addr)->sin6_addr)) && 565 (0 != GNUNET_memcmp (&in6addr_loopback, &((const struct
562 (!IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *)addr)->sin6_addr))) 566 sockaddr_in6 *) addr)->
563 { 567 sin6_addr)) &&
564 ah->have_v6 = GNUNET_YES; 568 (! IN6_IS_ADDR_LINKLOCAL (&((const struct
565 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 569 sockaddr_in6 *) addr)->sin6_addr)))
566 _("This system has a global IPv6 address, setting IPv6 to supported.\n")); 570 {
571 ah->have_v6 = GNUNET_YES;
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573 _ (
574 "This system has a global IPv6 address, setting IPv6 to supported.\n"));
567 575
568 return GNUNET_OK; 576 return GNUNET_OK;
569 } 577 }
570 if (addrlen != sizeof(struct sockaddr_in)) 578 if (addrlen != sizeof(struct sockaddr_in))
571 return GNUNET_OK; 579 return GNUNET_OK;
572 in = (const struct sockaddr_in *)addr; 580 in = (const struct sockaddr_in *) addr;
573 581
574 582
575 /* set internal IP address */ 583 /* set internal IP address */
576 if (NULL == inet_ntop(AF_INET, &in->sin_addr, buf, sizeof(buf))) 584 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof(buf)))
577 { 585 {
578 GNUNET_break(0); 586 GNUNET_break (0);
579 return GNUNET_OK; 587 return GNUNET_OK;
580 } 588 }
581 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "INTERNAL_ADDRESS", 589 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
582 buf); 590 buf);
583 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 591 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
584 _("Detected internal network address `%s'.\n"), 592 _ ("Detected internal network address `%s'.\n"),
585 buf); 593 buf);
586 594
587 595
588 ah->ret = GNUNET_NAT_ERROR_SUCCESS; 596 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
589 597
590 /* Check if our internal IP is the same as the External detect by STUN*/ 598 /* Check if our internal IP is the same as the External detect by STUN*/
591 if (ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0)) 599 if (ah->stun_ip && (strcmp (buf, ah->stun_ip) == 0))
592 { 600 {
593 ah->internal_ip_is_public = GNUNET_YES; 601 ah->internal_ip_is_public = GNUNET_YES;
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "A internal IP is the sameas the external"); 602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
595 /* No need to continue*/ 603 "A internal IP is the sameas the external");
596 return GNUNET_SYSERR; 604 /* No need to continue*/
597 } 605 return GNUNET_SYSERR;
606 }
598 607
599 /* no need to continue iteration if we found the default */ 608 /* no need to continue iteration if we found the default */
600 if (!isDefault) 609 if (! isDefault)
601 return GNUNET_OK; 610 return GNUNET_OK;
602 else 611 else
603 return GNUNET_SYSERR; 612 return GNUNET_SYSERR;
@@ -610,15 +619,16 @@ process_if(void *cls,
610 * @param ah auto setup context 619 * @param ah auto setup context
611 */ 620 */
612static void 621static void
613test_local_ip(struct GNUNET_NAT_AutoHandle *ah) 622test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
614{ 623{
615 ah->have_v6 = GNUNET_NO; 624 ah->have_v6 = GNUNET_NO;
616 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP 625 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP
617 GNUNET_OS_network_interfaces_list(&process_if, ah); 626 GNUNET_OS_network_interfaces_list (&process_if, ah);
618 627
619 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "DISABLEV6", 628 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6",
620 (GNUNET_YES == ah->have_v6) ? "NO" : "YES"); 629 (GNUNET_YES == ah->have_v6) ? "NO" :
621 next_phase(ah); 630 "YES");
631 next_phase (ah);
622} 632}
623 633
624 634
@@ -630,15 +640,15 @@ test_local_ip(struct GNUNET_NAT_AutoHandle *ah)
630 * @param error error code 640 * @param error error code
631 */ 641 */
632static void 642static void
633mq_error_handler(void *cls, 643mq_error_handler (void *cls,
634 enum GNUNET_MQ_Error error) 644 enum GNUNET_MQ_Error error)
635{ 645{
636 struct GNUNET_NAT_AutoHandle *ah = cls; 646 struct GNUNET_NAT_AutoHandle *ah = cls;
637 647
638 GNUNET_MQ_destroy(ah->mq); 648 GNUNET_MQ_destroy (ah->mq);
639 ah->mq = NULL; 649 ah->mq = NULL;
640 /* wait a bit first? */ 650 /* wait a bit first? */
641 next_phase(ah); 651 next_phase (ah);
642} 652}
643 653
644 654
@@ -648,50 +658,50 @@ mq_error_handler(void *cls,
648 * @param ah auto setup context 658 * @param ah auto setup context
649 */ 659 */
650static void 660static void
651test_nat_punched(struct GNUNET_NAT_AutoHandle *ah) 661test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
652{ 662{
653 struct GNUNET_NAT_TestMessage *msg; 663 struct GNUNET_NAT_TestMessage *msg;
654 struct GNUNET_MQ_Envelope *env; 664 struct GNUNET_MQ_Envelope *env;
655 665
656 if (!ah->stun_ip) 666 if (! ah->stun_ip)
657 { 667 {
658 LOG(GNUNET_ERROR_TYPE_INFO, 668 LOG (GNUNET_ERROR_TYPE_INFO,
659 "We don't have a STUN IP"); 669 "We don't have a STUN IP");
660 next_phase(ah); 670 next_phase (ah);
661 return; 671 return;
662 } 672 }
663 673
664 LOG(GNUNET_ERROR_TYPE_INFO, 674 LOG (GNUNET_ERROR_TYPE_INFO,
665 "Asking gnunet-nat-server to connect to `%s'\n", 675 "Asking gnunet-nat-server to connect to `%s'\n",
666 ah->stun_ip); 676 ah->stun_ip);
667 ah->mq = GNUNET_CLIENT_connect(ah->cfg, 677 ah->mq = GNUNET_CLIENT_connect (ah->cfg,
668 "gnunet-nat-server", 678 "gnunet-nat-server",
669 NULL, 679 NULL,
670 &mq_error_handler, 680 &mq_error_handler,
671 ah); 681 ah);
672 if (NULL == ah->mq) 682 if (NULL == ah->mq)
673 { 683 {
674 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("Failed to connect to `gnunet-nat-server'\n")); 685 _ ("Failed to connect to `gnunet-nat-server'\n"));
676 next_phase(ah); 686 next_phase (ah);
677 return; 687 return;
678 } 688 }
679 env = GNUNET_MQ_msg(msg, 689 env = GNUNET_MQ_msg (msg,
680 GNUNET_MESSAGE_TYPE_NAT_TEST); 690 GNUNET_MESSAGE_TYPE_NAT_TEST);
681 msg->dst_ipv4 = inet_addr(ah->stun_ip); 691 msg->dst_ipv4 = inet_addr (ah->stun_ip);
682 msg->dport = htons(ah->stun_port); 692 msg->dport = htons (ah->stun_port);
683 msg->data = port; 693 msg->data = port;
684 msg->is_tcp = htonl((uint32_t)GNUNET_NO); 694 msg->is_tcp = htonl ((uint32_t) GNUNET_NO);
685 GNUNET_MQ_send(ah->mq, 695 GNUNET_MQ_send (ah->mq,
686 env); 696 env);
687 if (NULL != ltask4) 697 if (NULL != ltask4)
688 { 698 {
689 GNUNET_SCHEDULER_cancel(ltask4); 699 GNUNET_SCHEDULER_cancel (ltask4);
690 ltask4 = GNUNET_SCHEDULER_add_read_net(NAT_SERVER_TIMEOUT, 700 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
691 lsock4, 701 lsock4,
692 &do_udp_read, 702 &do_udp_read,
693 ah); 703 ah);
694 } 704 }
695} 705}
696 706
697 707
@@ -701,24 +711,25 @@ test_nat_punched(struct GNUNET_NAT_AutoHandle *ah)
701 * @param ah auto setup context 711 * @param ah auto setup context
702 */ 712 */
703static void 713static void
704test_upnpc(struct GNUNET_NAT_AutoHandle *ah) 714test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
705{ 715{
706 int have_upnpc; 716 int have_upnpc;
707 717
708 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret) 718 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
709 next_phase(ah); 719 next_phase (ah);
710 720
711 // test if upnpc is available 721 // test if upnpc is available
712 have_upnpc = (GNUNET_SYSERR != 722 have_upnpc = (GNUNET_SYSERR !=
713 GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL)); 723 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL));
714 //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP 724 // FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP
715 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 725 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
716 (have_upnpc) 726 (have_upnpc)
717 ? _("upnpc found, enabling its use\n") 727 ? _ ("upnpc found, enabling its use\n")
718 : _("upnpc not found\n")); 728 : _ ("upnpc not found\n"));
719 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "ENABLE_UPNP", 729 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP",
720 (GNUNET_YES == have_upnpc) ? "YES" : "NO"); 730 (GNUNET_YES == have_upnpc) ? "YES" :
721 next_phase(ah); 731 "NO");
732 next_phase (ah);
722} 733}
723 734
724 735
@@ -728,7 +739,7 @@ test_upnpc(struct GNUNET_NAT_AutoHandle *ah)
728 * @param ah auto setup context 739 * @param ah auto setup context
729 */ 740 */
730static void 741static void
731test_icmp_server(struct GNUNET_NAT_AutoHandle *ah) 742test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
732{ 743{
733 int ext_ip; 744 int ext_ip;
734 int nated; 745 int nated;
@@ -741,53 +752,55 @@ test_icmp_server(struct GNUNET_NAT_AutoHandle *ah)
741 binary = GNUNET_NO; 752 binary = GNUNET_NO;
742 753
743 tmp = NULL; 754 tmp = NULL;
744 helper = GNUNET_OS_get_libexec_binary_path("gnunet-helper-nat-server"); 755 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
745 if ((GNUNET_OK == 756 if ((GNUNET_OK ==
746 GNUNET_CONFIGURATION_get_value_string(ah->cfg, 757 GNUNET_CONFIGURATION_get_value_string (ah->cfg,
747 "nat", 758 "nat",
748 "EXTERNAL_ADDRESS", 759 "EXTERNAL_ADDRESS",
749 &tmp)) && 760 &tmp)) &&
750 (0 < strlen(tmp))) 761 (0 < strlen (tmp)))
751 { 762 {
752 ext_ip = GNUNET_OK; 763 ext_ip = GNUNET_OK;
753 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 764 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
754 _("test_icmp_server not possible, as we have no public IPv4 address\n")); 765 _ (
755 } 766 "test_icmp_server not possible, as we have no public IPv4 address\n"));
767 }
756 else 768 else
757 goto err; 769 goto err;
758 770
759 if (GNUNET_YES == 771 if (GNUNET_YES ==
760 GNUNET_CONFIGURATION_get_value_yesno(ah->cfg, 772 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg,
761 "nat", 773 "nat",
762 "BEHIND_NAT")) 774 "BEHIND_NAT"))
763 { 775 {
764 nated = GNUNET_YES; 776 nated = GNUNET_YES;
765 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
766 _("test_icmp_server not possible, as we are not behind NAT\n")); 778 _ (
767 } 779 "test_icmp_server not possible, as we are not behind NAT\n"));
780 }
768 else 781 else
769 goto err; 782 goto err;
770 783
771 if (GNUNET_YES == 784 if (GNUNET_YES ==
772 GNUNET_OS_check_helper_binary(helper, 785 GNUNET_OS_check_helper_binary (helper,
773 GNUNET_YES, 786 GNUNET_YES,
774 "-d 127.0.0.1")) 787 "-d 127.0.0.1"))
775 { 788 {
776 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing 789 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
777 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 790 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
778 _("No working gnunet-helper-nat-server found\n")); 791 _ ("No working gnunet-helper-nat-server found\n"));
779 } 792 }
780err: 793err:
781 GNUNET_free_non_null(tmp); 794 GNUNET_free_non_null (tmp);
782 GNUNET_free(helper); 795 GNUNET_free (helper);
783 796
784 if ((GNUNET_OK == ext_ip) && 797 if ((GNUNET_OK == ext_ip) &&
785 (GNUNET_YES == nated) && 798 (GNUNET_YES == nated) &&
786 (GNUNET_OK == binary)) 799 (GNUNET_OK == binary))
787 ah->task = GNUNET_SCHEDULER_add_now(&reversal_test, 800 ah->task = GNUNET_SCHEDULER_add_now (&reversal_test,
788 ah); 801 ah);
789 else 802 else
790 next_phase(ah); 803 next_phase (ah);
791} 804}
792 805
793 806
@@ -797,51 +810,53 @@ err:
797 * @param ah auto setup context 810 * @param ah auto setup context
798 */ 811 */
799static void 812static void
800test_icmp_client(struct GNUNET_NAT_AutoHandle *ah) 813test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
801{ 814{
802 char *tmp; 815 char *tmp;
803 char *helper; 816 char *helper;
804 817
805 tmp = NULL; 818 tmp = NULL;
806 helper = GNUNET_OS_get_libexec_binary_path("gnunet-helper-nat-client"); 819 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
807 if ((GNUNET_OK == 820 if ((GNUNET_OK ==
808 GNUNET_CONFIGURATION_get_value_string(ah->cfg, 821 GNUNET_CONFIGURATION_get_value_string (ah->cfg,
809 "nat", 822 "nat",
810 "INTERNAL_ADDRESS", 823 "INTERNAL_ADDRESS",
811 &tmp)) && 824 &tmp)) &&
812 (0 < strlen(tmp))) 825 (0 < strlen (tmp)))
813 { 826 {
814 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 827 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
815 _("test_icmp_client not possible, as we have no internal IPv4 address\n")); 828 _ (
816 } 829 "test_icmp_client not possible, as we have no internal IPv4 address\n"));
830 }
817 else 831 else
818 goto err; 832 goto err;
819 833
820 if (GNUNET_YES != 834 if (GNUNET_YES !=
821 GNUNET_CONFIGURATION_get_value_yesno(ah->cfg, 835 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg,
822 "nat", 836 "nat",
823 "BEHIND_NAT")) 837 "BEHIND_NAT"))
824 { 838 {
825 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
826 _("test_icmp_server not possible, as we are not behind NAT\n")); 840 _ (
827 } 841 "test_icmp_server not possible, as we are not behind NAT\n"));
842 }
828 else 843 else
829 goto err; 844 goto err;
830 845
831 if (GNUNET_YES == 846 if (GNUNET_YES ==
832 GNUNET_OS_check_helper_binary(helper, 847 GNUNET_OS_check_helper_binary (helper,
833 GNUNET_YES, 848 GNUNET_YES,
834 "-d 127.0.0.1 127.0.0.2 42")) 849 "-d 127.0.0.1 127.0.0.2 42"))
835 { 850 {
836 // none of these parameters are actually used in privilege testing mode 851 // none of these parameters are actually used in privilege testing mode
837 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 852 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
838 _("No working gnunet-helper-nat-server found\n")); 853 _ ("No working gnunet-helper-nat-server found\n"));
839 } 854 }
840err: 855err:
841 GNUNET_free_non_null(tmp); 856 GNUNET_free_non_null (tmp);
842 GNUNET_free(helper); 857 GNUNET_free (helper);
843 858
844 next_phase(ah); 859 next_phase (ah);
845} 860}
846 861
847 862
@@ -849,161 +864,161 @@ err:
849 * Run the next phase of the auto test. 864 * Run the next phase of the auto test.
850 */ 865 */
851static void 866static void
852next_phase(struct GNUNET_NAT_AutoHandle *ah) 867next_phase (struct GNUNET_NAT_AutoHandle *ah)
853{ 868{
854 struct GNUNET_CONFIGURATION_Handle *diff; 869 struct GNUNET_CONFIGURATION_Handle *diff;
855 870
856 ah->phase++; 871 ah->phase++;
857 switch (ah->phase) 872 switch (ah->phase)
873 {
874 case AUTO_INIT:
875 GNUNET_assert (0);
876 break;
877
878 case AUTO_EXTERNAL_IP:
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Will run AUTO_EXTERNAL_IP\n");
881 test_external_ip (ah);
882 break;
883
884 case AUTO_STUN:
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Will run AUTO_STUN\n");
887 test_stun (ah);
888 break;
889
890 case AUTO_LOCAL_IP:
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Will run AUTO_LOCAL_IP\n");
893 test_local_ip (ah);
894 break;
895
896 case AUTO_NAT_PUNCHED:
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Will run AUTO_NAT_PUNCHED\n");
899 test_nat_punched (ah);
900 break;
901
902 case AUTO_UPNPC:
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Will run AUTO_UPNPC\n");
905 test_upnpc (ah);
906 break;
907
908 case AUTO_ICMP_SERVER:
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "Will run AUTO_ICMP_SERVER\n");
911 test_icmp_server (ah);
912 break;
913
914 case AUTO_ICMP_CLIENT:
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Will run AUTO_ICMP_CLIENT\n");
917 test_icmp_client (ah);
918 break;
919
920 case AUTO_DONE:
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Done with tests\n");
923 if (! ah->internal_ip_is_public)
858 { 924 {
859 case AUTO_INIT: 925 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
860 GNUNET_assert(0); 926 "nat",
861 break; 927 "BEHIND_NAT",
862 928 "YES");
863 case AUTO_EXTERNAL_IP: 929
864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 930 if (ah->connected_back)
865 "Will run AUTO_EXTERNAL_IP\n"); 931 {
866 test_external_ip(ah); 932 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
867 break; 933 "nat",
868 934 "PUNCHED_NAT",
869 case AUTO_STUN: 935 "YES");
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 936 }
871 "Will run AUTO_STUN\n"); 937 else
872 test_stun(ah); 938 {
873 break; 939 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
874 940 "nat",
875 case AUTO_LOCAL_IP: 941 "PUNCHED_NAT",
876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 942 "NO");
877 "Will run AUTO_LOCAL_IP\n"); 943 }
878 test_local_ip(ah); 944
879 break; 945 if (ah->stun_ip)
880 946 {
881 case AUTO_NAT_PUNCHED: 947 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 948 "nat",
883 "Will run AUTO_NAT_PUNCHED\n"); 949 "EXTERNAL_ADDRESS",
884 test_nat_punched(ah); 950 ah->stun_ip);
885 break; 951 if (ah->connected_back)
886 952 {
887 case AUTO_UPNPC: 953 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 954 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
889 "Will run AUTO_UPNPC\n"); 955 "nat",
890 test_upnpc(ah); 956 "USE_STUN",
891 break; 957 "YES");
892 958 }
893 case AUTO_ICMP_SERVER: 959 else
894 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
895 "Will run AUTO_ICMP_SERVER\n");
896 test_icmp_server(ah);
897 break;
898
899 case AUTO_ICMP_CLIENT:
900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
901 "Will run AUTO_ICMP_CLIENT\n");
902 test_icmp_client(ah);
903 break;
904
905 case AUTO_DONE:
906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
907 "Done with tests\n");
908 if (!ah->internal_ip_is_public)
909 { 960 {
910 GNUNET_CONFIGURATION_set_value_string(ah->cfg, 961 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
911 "nat", 962 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
912 "BEHIND_NAT", 963 "nat",
913 "YES"); 964 "USE_STUN",
914 965 "NO");
915 if (ah->connected_back)
916 {
917 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
918 "nat",
919 "PUNCHED_NAT",
920 "YES");
921 }
922 else
923 {
924 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
925 "nat",
926 "PUNCHED_NAT",
927 "NO");
928 }
929
930 if (ah->stun_ip)
931 {
932 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
933 "nat",
934 "EXTERNAL_ADDRESS",
935 ah->stun_ip);
936 if (ah->connected_back)
937 {
938 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
939 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
940 "nat",
941 "USE_STUN",
942 "YES");
943 }
944 else
945 {
946 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
947 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
948 "nat",
949 "USE_STUN",
950 "NO");
951 }
952 }
953 if (0 != ah->stun_port)
954 {
955 GNUNET_CONFIGURATION_set_value_number(ah->cfg,
956 "transport-udp",
957 "ADVERTISED_PORT",
958 ah->stun_port);
959 }
960 } 966 }
967 }
968 if (0 != ah->stun_port)
969 {
970 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
971 "transport-udp",
972 "ADVERTISED_PORT",
973 ah->stun_port);
974 }
975 }
976 else
977 {
978 // The internal IP is the same as public, but we didn't got a incoming connection
979 if (ah->connected_back)
980 {
981 ah->type = GNUNET_NAT_TYPE_NO_NAT;
982 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
983 "nat",
984 "BEHIND_NAT",
985 "NO");
986 }
961 else 987 else
988 {
989 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
990 "nat",
991 "BEHIND_NAT",
992 "YES");
993 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
994 if (ah->stun_ip)
995 {
996 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
997 "nat",
998 "EXTERNAL_ADDRESS",
999 ah->stun_ip);
1000 }
1001 if (0 != ah->stun_port)
962 { 1002 {
963 //The internal IP is the same as public, but we didn't got a incoming connection 1003 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
964 if (ah->connected_back) 1004 "transport-udp",
965 { 1005 "ADVERTISED_PORT",
966 ah->type = GNUNET_NAT_TYPE_NO_NAT; 1006 ah->stun_port);
967 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
968 "nat",
969 "BEHIND_NAT",
970 "NO");
971 }
972 else
973 {
974 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
975 "nat",
976 "BEHIND_NAT",
977 "YES");
978 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
979 if (ah->stun_ip)
980 {
981 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
982 "nat",
983 "EXTERNAL_ADDRESS",
984 ah->stun_ip);
985 }
986 if (0 != ah->stun_port)
987 {
988 GNUNET_CONFIGURATION_set_value_number(ah->cfg,
989 "transport-udp",
990 "ADVERTISED_PORT",
991 ah->stun_port);
992 }
993 }
994 } 1007 }
1008 }
1009 }
995 1010
996 diff = GNUNET_CONFIGURATION_get_diff(ah->initial_cfg, 1011 diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
997 ah->cfg); 1012 ah->cfg);
998 1013
999 1014
1000 ah->fin_cb(ah->fin_cb_cls, 1015 ah->fin_cb (ah->fin_cb_cls,
1001 diff, 1016 diff,
1002 ah->ret, 1017 ah->ret,
1003 ah->type); 1018 ah->type);
1004 GNUNET_CONFIGURATION_destroy(diff); 1019 GNUNET_CONFIGURATION_destroy (diff);
1005 GNUNET_NAT_autoconfig_cancel(ah); 1020 GNUNET_NAT_autoconfig_cancel (ah);
1006 } 1021 }
1007} 1022}
1008 1023
1009 1024
@@ -1017,26 +1032,26 @@ next_phase(struct GNUNET_NAT_AutoHandle *ah)
1017 * @return handle to cancel operation 1032 * @return handle to cancel operation
1018 */ 1033 */
1019struct GNUNET_NAT_AutoHandle * 1034struct GNUNET_NAT_AutoHandle *
1020GNUNET_NAT_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg, 1035GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1021 GNUNET_NAT_AutoResultCallback cb, 1036 GNUNET_NAT_AutoResultCallback cb,
1022 void *cb_cls) 1037 void *cb_cls)
1023{ 1038{
1024 struct GNUNET_NAT_AutoHandle *ah; 1039 struct GNUNET_NAT_AutoHandle *ah;
1025 1040
1026 ah = GNUNET_new(struct GNUNET_NAT_AutoHandle); 1041 ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
1027 ah->fin_cb = cb; 1042 ah->fin_cb = cb;
1028 ah->fin_cb_cls = cb_cls; 1043 ah->fin_cb_cls = cb_cls;
1029 ah->ret = GNUNET_NAT_ERROR_SUCCESS; 1044 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
1030 ah->cfg = GNUNET_CONFIGURATION_dup(cfg); 1045 ah->cfg = GNUNET_CONFIGURATION_dup (cfg);
1031 ah->initial_cfg = GNUNET_CONFIGURATION_dup(cfg); 1046 ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg);
1032 1047
1033 /* never use loopback addresses if user wanted autoconfiguration */ 1048 /* never use loopback addresses if user wanted autoconfiguration */
1034 GNUNET_CONFIGURATION_set_value_string(ah->cfg, 1049 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
1035 "nat", 1050 "nat",
1036 "USE_LOCALADDR", 1051 "USE_LOCALADDR",
1037 "NO"); 1052 "NO");
1038 1053
1039 next_phase(ah); 1054 next_phase (ah);
1040 return ah; 1055 return ah;
1041} 1056}
1042 1057
@@ -1047,31 +1062,31 @@ GNUNET_NAT_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
1047 * @param ah handle for operation to abort 1062 * @param ah handle for operation to abort
1048 */ 1063 */
1049void 1064void
1050GNUNET_NAT_autoconfig_cancel(struct GNUNET_NAT_AutoHandle *ah) 1065GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
1051{ 1066{
1052 if (NULL != ah->tst) 1067 if (NULL != ah->tst)
1053 { 1068 {
1054 GNUNET_NAT_test_stop(ah->tst); 1069 GNUNET_NAT_test_stop (ah->tst);
1055 ah->tst = NULL; 1070 ah->tst = NULL;
1056 } 1071 }
1057 if (NULL != ah->eh) 1072 if (NULL != ah->eh)
1058 { 1073 {
1059 GNUNET_NAT_mini_get_external_ipv4_cancel(ah->eh); 1074 GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh);
1060 ah->eh = NULL; 1075 ah->eh = NULL;
1061 } 1076 }
1062 if (NULL != ah->mq) 1077 if (NULL != ah->mq)
1063 { 1078 {
1064 GNUNET_MQ_destroy(ah->mq); 1079 GNUNET_MQ_destroy (ah->mq);
1065 ah->mq = NULL; 1080 ah->mq = NULL;
1066 } 1081 }
1067 if (NULL != ah->task) 1082 if (NULL != ah->task)
1068 { 1083 {
1069 GNUNET_SCHEDULER_cancel(ah->task); 1084 GNUNET_SCHEDULER_cancel (ah->task);
1070 ah->task = NULL; 1085 ah->task = NULL;
1071 } 1086 }
1072 GNUNET_CONFIGURATION_destroy(ah->cfg); 1087 GNUNET_CONFIGURATION_destroy (ah->cfg);
1073 GNUNET_CONFIGURATION_destroy(ah->initial_cfg); 1088 GNUNET_CONFIGURATION_destroy (ah->initial_cfg);
1074 GNUNET_free(ah); 1089 GNUNET_free (ah);
1075} 1090}
1076 1091
1077 1092
diff --git a/src/nat-auto/nat-auto.h b/src/nat-auto/nat-auto.h
index 6ef9b7e1e..bee8ecf26 100644
--- a/src/nat-auto/nat-auto.h
+++ b/src/nat-auto/nat-auto.h
@@ -36,7 +36,8 @@ GNUNET_NETWORK_STRUCT_BEGIN
36 * Request to test NAT traversal, sent to the gnunet-nat-server 36 * Request to test NAT traversal, sent to the gnunet-nat-server
37 * (not the service!). 37 * (not the service!).
38 */ 38 */
39struct GNUNET_NAT_AUTO_TestMessage { 39struct GNUNET_NAT_AUTO_TestMessage
40{
40 /** 41 /**
41 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST 42 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST
42 */ 43 */
@@ -67,7 +68,8 @@ struct GNUNET_NAT_AUTO_TestMessage {
67/** 68/**
68 * Client requesting automatic configuration. 69 * Client requesting automatic configuration.
69 */ 70 */
70struct GNUNET_NAT_AUTO_AutoconfigRequestMessage { 71struct GNUNET_NAT_AUTO_AutoconfigRequestMessage
72{
71 /** 73 /**
72 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG 74 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG
73 */ 75 */
@@ -80,7 +82,8 @@ struct GNUNET_NAT_AUTO_AutoconfigRequestMessage {
80/** 82/**
81 * Service responding with proposed configuration. 83 * Service responding with proposed configuration.
82 */ 84 */
83struct GNUNET_NAT_AUTO_AutoconfigResultMessage { 85struct GNUNET_NAT_AUTO_AutoconfigResultMessage
86{
84 /** 87 /**
85 * Header with type #GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT 88 * Header with type #GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
86 */ 89 */
diff --git a/src/nat-auto/nat_auto_api.c b/src/nat-auto/nat_auto_api.c
index ae7bb6514..f7a821a82 100644
--- a/src/nat-auto/nat_auto_api.c
+++ b/src/nat-auto/nat_auto_api.c
@@ -35,7 +35,8 @@
35/** 35/**
36 * Handle to auto-configuration in progress. 36 * Handle to auto-configuration in progress.
37 */ 37 */
38struct GNUNET_NAT_AUTO_AutoHandle { 38struct GNUNET_NAT_AUTO_AutoHandle
39{
39 /** 40 /**
40 * Configuration we use. 41 * Configuration we use.
41 */ 42 */
@@ -65,70 +66,71 @@ struct GNUNET_NAT_AUTO_AutoHandle {
65 * @return point to a static string containing the error code 66 * @return point to a static string containing the error code
66 */ 67 */
67const char * 68const char *
68GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err) 69GNUNET_NAT_AUTO_status2string (enum GNUNET_NAT_StatusCode err)
69{ 70{
70 switch (err) 71 switch (err)
71 { 72 {
72 case GNUNET_NAT_ERROR_SUCCESS: 73 case GNUNET_NAT_ERROR_SUCCESS:
73 return _("Operation Successful"); 74 return _ ("Operation Successful");
74 75
75 case GNUNET_NAT_ERROR_IPC_FAILURE: 76 case GNUNET_NAT_ERROR_IPC_FAILURE:
76 return _("IPC failure"); 77 return _ ("IPC failure");
77 78
78 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR: 79 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
79 return _("Failure in network subsystem, check permissions."); 80 return _ ("Failure in network subsystem, check permissions.");
80 81
81 case GNUNET_NAT_ERROR_TIMEOUT: 82 case GNUNET_NAT_ERROR_TIMEOUT:
82 return _("Encountered timeout while performing operation"); 83 return _ ("Encountered timeout while performing operation");
83 84
84 case GNUNET_NAT_ERROR_NOT_ONLINE: 85 case GNUNET_NAT_ERROR_NOT_ONLINE:
85 return _("detected that we are offline"); 86 return _ ("detected that we are offline");
86 87
87 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND: 88 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
88 return _("`upnpc` command not found"); 89 return _ ("`upnpc` command not found");
89 90
90 case GNUNET_NAT_ERROR_UPNPC_FAILED: 91 case GNUNET_NAT_ERROR_UPNPC_FAILED:
91 return _("Failed to run `upnpc` command"); 92 return _ ("Failed to run `upnpc` command");
92 93
93 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT: 94 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
94 return _("`upnpc' command took too long, process killed"); 95 return _ ("`upnpc' command took too long, process killed");
95 96
96 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED: 97 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
97 return _("`upnpc' command failed to establish port mapping"); 98 return _ ("`upnpc' command failed to establish port mapping");
98 99
99 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND: 100 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
100 return _("`external-ip' command not found"); 101 return _ ("`external-ip' command not found");
101 102
102 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED: 103 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
103 return _("Failed to run `external-ip` command"); 104 return _ ("Failed to run `external-ip` command");
104 105
105 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID: 106 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
106 return _("`external-ip' command output invalid"); 107 return _ ("`external-ip' command output invalid");
107 108
108 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID: 109 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
109 return _("no valid address was returned by `external-ip'"); 110 return _ ("no valid address was returned by `external-ip'");
110 111
111 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO: 112 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
112 return _("Could not determine interface with internal/local network address"); 113 return _ (
114 "Could not determine interface with internal/local network address");
113 115
114 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND: 116 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
115 return _("No functioning gnunet-helper-nat-server installation found"); 117 return _ ("No functioning gnunet-helper-nat-server installation found");
116 118
117 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED: 119 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
118 return _("NAT test could not be initialized"); 120 return _ ("NAT test could not be initialized");
119 121
120 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT: 122 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
121 return _("NAT test timeout reached"); 123 return _ ("NAT test timeout reached");
122 124
123 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED: 125 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
124 return _("could not register NAT"); 126 return _ ("could not register NAT");
125 127
126 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND: 128 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
127 return _("No working gnunet-helper-nat-client installation found"); 129 return _ ("No working gnunet-helper-nat-client installation found");
128 130
129 default: 131 default:
130 return "unknown status code"; 132 return "unknown status code";
131 } 133 }
132} 134}
133 135
134 136
@@ -140,8 +142,8 @@ GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err)
140 * @return #GNUNET_OK if @a res is well-formed (always for now) 142 * @return #GNUNET_OK if @a res is well-formed (always for now)
141 */ 143 */
142static int 144static int
143check_auto_result(void *cls, 145check_auto_result (void *cls,
144 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res) 146 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
145{ 147{
146 return GNUNET_OK; 148 return GNUNET_OK;
147} 149}
@@ -154,40 +156,40 @@ check_auto_result(void *cls,
154 * @param res the result 156 * @param res the result
155 */ 157 */
156static void 158static void
157handle_auto_result(void *cls, 159handle_auto_result (void *cls,
158 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res) 160 const struct GNUNET_NAT_AUTO_AutoconfigResultMessage *res)
159{ 161{
160 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls; 162 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
161 size_t left; 163 size_t left;
162 struct GNUNET_CONFIGURATION_Handle *cfg; 164 struct GNUNET_CONFIGURATION_Handle *cfg;
163 enum GNUNET_NAT_Type type 165 enum GNUNET_NAT_Type type
164 = (enum GNUNET_NAT_Type)ntohl(res->type); 166 = (enum GNUNET_NAT_Type) ntohl (res->type);
165 enum GNUNET_NAT_StatusCode status 167 enum GNUNET_NAT_StatusCode status
166 = (enum GNUNET_NAT_StatusCode)ntohl(res->status_code); 168 = (enum GNUNET_NAT_StatusCode) ntohl (res->status_code);
167 169
168 left = ntohs(res->header.size) - sizeof(*res); 170 left = ntohs (res->header.size) - sizeof(*res);
169 cfg = GNUNET_CONFIGURATION_create(); 171 cfg = GNUNET_CONFIGURATION_create ();
170 if (GNUNET_OK != 172 if (GNUNET_OK !=
171 GNUNET_CONFIGURATION_deserialize(cfg, 173 GNUNET_CONFIGURATION_deserialize (cfg,
172 (const char *)&res[1], 174 (const char *) &res[1],
173 left, 175 left,
174 NULL)) 176 NULL))
175 { 177 {
176 GNUNET_break(0); 178 GNUNET_break (0);
177 ah->arc(ah->arc_cls, 179 ah->arc (ah->arc_cls,
178 NULL, 180 NULL,
179 GNUNET_NAT_ERROR_IPC_FAILURE, 181 GNUNET_NAT_ERROR_IPC_FAILURE,
180 type); 182 type);
181 } 183 }
182 else 184 else
183 { 185 {
184 ah->arc(ah->arc_cls, 186 ah->arc (ah->arc_cls,
185 cfg, 187 cfg,
186 status, 188 status,
187 type); 189 type);
188 } 190 }
189 GNUNET_CONFIGURATION_destroy(cfg); 191 GNUNET_CONFIGURATION_destroy (cfg);
190 GNUNET_NAT_AUTO_autoconfig_cancel(ah); 192 GNUNET_NAT_AUTO_autoconfig_cancel (ah);
191} 193}
192 194
193 195
@@ -198,16 +200,16 @@ handle_auto_result(void *cls,
198 * @param error details about the error 200 * @param error details about the error
199 */ 201 */
200static void 202static void
201ah_error_handler(void *cls, 203ah_error_handler (void *cls,
202 enum GNUNET_MQ_Error error) 204 enum GNUNET_MQ_Error error)
203{ 205{
204 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls; 206 struct GNUNET_NAT_AUTO_AutoHandle *ah = cls;
205 207
206 ah->arc(ah->arc_cls, 208 ah->arc (ah->arc_cls,
207 NULL, 209 NULL,
208 GNUNET_NAT_ERROR_IPC_FAILURE, 210 GNUNET_NAT_ERROR_IPC_FAILURE,
209 GNUNET_NAT_TYPE_UNKNOWN); 211 GNUNET_NAT_TYPE_UNKNOWN);
210 GNUNET_NAT_AUTO_autoconfig_cancel(ah); 212 GNUNET_NAT_AUTO_autoconfig_cancel (ah);
211} 213}
212 214
213 215
@@ -221,55 +223,56 @@ ah_error_handler(void *cls,
221 * @return handle to cancel operation 223 * @return handle to cancel operation
222 */ 224 */
223struct GNUNET_NAT_AUTO_AutoHandle * 225struct GNUNET_NAT_AUTO_AutoHandle *
224GNUNET_NAT_AUTO_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg, 226GNUNET_NAT_AUTO_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
225 GNUNET_NAT_AUTO_AutoResultCallback cb, 227 GNUNET_NAT_AUTO_AutoResultCallback cb,
226 void *cb_cls) 228 void *cb_cls)
227{ 229{
228 struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new(struct GNUNET_NAT_AUTO_AutoHandle); 230 struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new (struct
231 GNUNET_NAT_AUTO_AutoHandle);
229 struct GNUNET_MQ_MessageHandler handlers[] = { 232 struct GNUNET_MQ_MessageHandler handlers[] = {
230 GNUNET_MQ_hd_var_size(auto_result, 233 GNUNET_MQ_hd_var_size (auto_result,
231 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT, 234 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT,
232 struct GNUNET_NAT_AUTO_AutoconfigResultMessage, 235 struct GNUNET_NAT_AUTO_AutoconfigResultMessage,
233 ah), 236 ah),
234 GNUNET_MQ_handler_end() 237 GNUNET_MQ_handler_end ()
235 }; 238 };
236 struct GNUNET_MQ_Envelope *env; 239 struct GNUNET_MQ_Envelope *env;
237 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *req; 240 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *req;
238 char *buf; 241 char *buf;
239 size_t size; 242 size_t size;
240 243
241 buf = GNUNET_CONFIGURATION_serialize(cfg, 244 buf = GNUNET_CONFIGURATION_serialize (cfg,
242 &size); 245 &size);
243 if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req)) 246 if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req))
244 { 247 {
245 GNUNET_break(0); 248 GNUNET_break (0);
246 GNUNET_free(buf); 249 GNUNET_free (buf);
247 GNUNET_free(ah); 250 GNUNET_free (ah);
248 return NULL; 251 return NULL;
249 } 252 }
250 ah->arc = cb; 253 ah->arc = cb;
251 ah->arc_cls = cb_cls; 254 ah->arc_cls = cb_cls;
252 ah->mq = GNUNET_CLIENT_connect(cfg, 255 ah->mq = GNUNET_CLIENT_connect (cfg,
253 "nat", 256 "nat",
254 handlers, 257 handlers,
255 &ah_error_handler, 258 &ah_error_handler,
256 ah); 259 ah);
257 if (NULL == ah->mq) 260 if (NULL == ah->mq)
258 { 261 {
259 GNUNET_break(0); 262 GNUNET_break (0);
260 GNUNET_free(buf); 263 GNUNET_free (buf);
261 GNUNET_free(ah); 264 GNUNET_free (ah);
262 return NULL; 265 return NULL;
263 } 266 }
264 env = GNUNET_MQ_msg_extra(req, 267 env = GNUNET_MQ_msg_extra (req,
265 size, 268 size,
266 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG); 269 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG);
267 GNUNET_memcpy(&req[1], 270 GNUNET_memcpy (&req[1],
268 buf, 271 buf,
269 size); 272 size);
270 GNUNET_free(buf); 273 GNUNET_free (buf);
271 GNUNET_MQ_send(ah->mq, 274 GNUNET_MQ_send (ah->mq,
272 env); 275 env);
273 return ah; 276 return ah;
274} 277}
275 278
@@ -280,10 +283,10 @@ GNUNET_NAT_AUTO_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
280 * @param ah handle for operation to abort 283 * @param ah handle for operation to abort
281 */ 284 */
282void 285void
283GNUNET_NAT_AUTO_autoconfig_cancel(struct GNUNET_NAT_AUTO_AutoHandle *ah) 286GNUNET_NAT_AUTO_autoconfig_cancel (struct GNUNET_NAT_AUTO_AutoHandle *ah)
284{ 287{
285 GNUNET_MQ_destroy(ah->mq); 288 GNUNET_MQ_destroy (ah->mq);
286 GNUNET_free(ah); 289 GNUNET_free (ah);
287} 290}
288 291
289/* end of nat_api_auto.c */ 292/* end of nat_api_auto.c */
diff --git a/src/nat-auto/nat_auto_api_test.c b/src/nat-auto/nat_auto_api_test.c
index daea3063c..0d35d869f 100644
--- a/src/nat-auto/nat_auto_api_test.c
+++ b/src/nat-auto/nat_auto_api_test.c
@@ -28,15 +28,16 @@
28#include "gnunet_nat_auto_service.h" 28#include "gnunet_nat_auto_service.h"
29#include "nat-auto.h" 29#include "nat-auto.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "nat-auto", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "nat-auto", __VA_ARGS__)
32 32
33#define NAT_SERVER_TIMEOUT \ 33#define NAT_SERVER_TIMEOUT \
34 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30) 34 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
35 35
36/** 36/**
37 * Entry we keep for each incoming connection. 37 * Entry we keep for each incoming connection.
38 */ 38 */
39struct NatActivity { 39struct NatActivity
40{
40 /** 41 /**
41 * This is a doubly-linked list. 42 * This is a doubly-linked list.
42 */ 43 */
@@ -67,7 +68,8 @@ struct NatActivity {
67/** 68/**
68 * Entry we keep for each connection to the gnunet-nat-service. 69 * Entry we keep for each connection to the gnunet-nat-service.
69 */ 70 */
70struct ClientActivity { 71struct ClientActivity
72{
71 /** 73 /**
72 * This is a doubly-linked list. 74 * This is a doubly-linked list.
73 */ 75 */
@@ -93,7 +95,8 @@ struct ClientActivity {
93/** 95/**
94 * Handle to a NAT test. 96 * Handle to a NAT test.
95 */ 97 */
96struct GNUNET_NAT_AUTO_Test { 98struct GNUNET_NAT_AUTO_Test
99{
97 /** 100 /**
98 * Configuration used 101 * Configuration used
99 */ 102 */
@@ -180,22 +183,22 @@ struct GNUNET_NAT_AUTO_Test {
180 * @param addrlen actual lenght of the @a addr 183 * @param addrlen actual lenght of the @a addr
181 */ 184 */
182static void 185static void
183reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen) 186reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
184{ 187{
185 struct GNUNET_NAT_AUTO_Test *h = cls; 188 struct GNUNET_NAT_AUTO_Test *h = cls;
186 const struct sockaddr_in *sa; 189 const struct sockaddr_in *sa;
187 190
188 if (sizeof(struct sockaddr_in) != addrlen) 191 if (sizeof(struct sockaddr_in) != addrlen)
189 return; 192 return;
190 sa = (const struct sockaddr_in *)addr; 193 sa = (const struct sockaddr_in *) addr;
191 if (h->data != sa->sin_port) 194 if (h->data != sa->sin_port)
192 { 195 {
193 LOG(GNUNET_ERROR_TYPE_DEBUG, 196 LOG (GNUNET_ERROR_TYPE_DEBUG,
194 "Received connection reversal request for wrong port\n"); 197 "Received connection reversal request for wrong port\n");
195 return; /* wrong port */ 198 return; /* wrong port */
196 } 199 }
197 /* report success */ 200 /* report success */
198 h->report(h->report_cls, GNUNET_NAT_ERROR_SUCCESS); 201 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
199} 202}
200 203
201 204
@@ -206,31 +209,31 @@ reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen)
206 * @param cls the `struct GNUNET_NAT_AUTO_Test` 209 * @param cls the `struct GNUNET_NAT_AUTO_Test`
207 */ 210 */
208static void 211static void
209do_udp_read(void *cls) 212do_udp_read (void *cls)
210{ 213{
211 struct GNUNET_NAT_AUTO_Test *tst = cls; 214 struct GNUNET_NAT_AUTO_Test *tst = cls;
212 uint16_t data; 215 uint16_t data;
213 const struct GNUNET_SCHEDULER_TaskContext *tc; 216 const struct GNUNET_SCHEDULER_TaskContext *tc;
214 217
215 tc = GNUNET_SCHEDULER_get_task_context(); 218 tc = GNUNET_SCHEDULER_get_task_context ();
216 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 219 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
217 tst->lsock, 220 tst->lsock,
218 &do_udp_read, 221 &do_udp_read,
219 tst); 222 tst);
220 if ((NULL != tc->write_ready) && 223 if ((NULL != tc->write_ready) &&
221 (GNUNET_NETWORK_fdset_isset(tc->read_ready, tst->lsock)) && 224 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
222 (sizeof(data) == 225 (sizeof(data) ==
223 GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data)))) 226 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof(data))))
224 { 227 {
225 if (data == tst->data) 228 if (data == tst->data)
226 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 229 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
227 else 230 else
228 LOG(GNUNET_ERROR_TYPE_DEBUG, 231 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Received data mismatches expected value\n"); 232 "Received data mismatches expected value\n");
230 } 233 }
231 else 234 else
232 LOG(GNUNET_ERROR_TYPE_DEBUG, 235 LOG (GNUNET_ERROR_TYPE_DEBUG,
233 "Failed to receive data from inbound connection\n"); 236 "Failed to receive data from inbound connection\n");
234} 237}
235 238
236 239
@@ -241,33 +244,33 @@ do_udp_read(void *cls)
241 * @param cls the `struct NatActivity` 244 * @param cls the `struct NatActivity`
242 */ 245 */
243static void 246static void
244do_read(void *cls) 247do_read (void *cls)
245{ 248{
246 struct NatActivity *na = cls; 249 struct NatActivity *na = cls;
247 struct GNUNET_NAT_AUTO_Test *tst; 250 struct GNUNET_NAT_AUTO_Test *tst;
248 uint16_t data; 251 uint16_t data;
249 const struct GNUNET_SCHEDULER_TaskContext *tc; 252 const struct GNUNET_SCHEDULER_TaskContext *tc;
250 253
251 tc = GNUNET_SCHEDULER_get_task_context(); 254 tc = GNUNET_SCHEDULER_get_task_context ();
252 na->rtask = NULL; 255 na->rtask = NULL;
253 tst = na->h; 256 tst = na->h;
254 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, na); 257 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
255 if ((NULL != tc->write_ready) && 258 if ((NULL != tc->write_ready) &&
256 (GNUNET_NETWORK_fdset_isset(tc->read_ready, na->sock)) && 259 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
257 (sizeof(data) == 260 (sizeof(data) ==
258 GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data)))) 261 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof(data))))
259 { 262 {
260 if (data == tst->data) 263 if (data == tst->data)
261 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); 264 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
262 else 265 else
263 LOG(GNUNET_ERROR_TYPE_DEBUG, 266 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Received data does not match expected value\n"); 267 "Received data does not match expected value\n");
265 } 268 }
266 else 269 else
267 LOG(GNUNET_ERROR_TYPE_DEBUG, 270 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 "Failed to receive data from inbound connection\n"); 271 "Failed to receive data from inbound connection\n");
269 GNUNET_NETWORK_socket_close(na->sock); 272 GNUNET_NETWORK_socket_close (na->sock);
270 GNUNET_free(na); 273 GNUNET_free (na);
271} 274}
272 275
273 276
@@ -278,32 +281,32 @@ do_read(void *cls)
278 * @param cls the `struct GNUNET_NAT_AUTO_Test` 281 * @param cls the `struct GNUNET_NAT_AUTO_Test`
279 */ 282 */
280static void 283static void
281do_accept(void *cls) 284do_accept (void *cls)
282{ 285{
283 struct GNUNET_NAT_AUTO_Test *tst = cls; 286 struct GNUNET_NAT_AUTO_Test *tst = cls;
284 struct GNUNET_NETWORK_Handle *s; 287 struct GNUNET_NETWORK_Handle *s;
285 struct NatActivity *wl; 288 struct NatActivity *wl;
286 289
287 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 290 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
288 tst->lsock, 291 tst->lsock,
289 &do_accept, 292 &do_accept,
290 tst); 293 tst);
291 s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL); 294 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
292 if (NULL == s) 295 if (NULL == s)
293 { 296 {
294 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, "accept"); 297 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
295 return; /* odd error */ 298 return; /* odd error */
296 } 299 }
297 LOG(GNUNET_ERROR_TYPE_DEBUG, 300 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Got an inbound connection, waiting for data\n"); 301 "Got an inbound connection, waiting for data\n");
299 wl = GNUNET_new(struct NatActivity); 302 wl = GNUNET_new (struct NatActivity);
300 wl->sock = s; 303 wl->sock = s;
301 wl->h = tst; 304 wl->h = tst;
302 wl->rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 305 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
303 wl->sock, 306 wl->sock,
304 &do_read, 307 &do_read,
305 wl); 308 wl);
306 GNUNET_CONTAINER_DLL_insert(tst->na_head, tst->na_tail, wl); 309 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
307} 310}
308 311
309 312
@@ -315,14 +318,14 @@ do_accept(void *cls)
315 * @param error error code 318 * @param error error code
316 */ 319 */
317static void 320static void
318mq_error_handler(void *cls, enum GNUNET_MQ_Error error) 321mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
319{ 322{
320 struct ClientActivity *ca = cls; 323 struct ClientActivity *ca = cls;
321 struct GNUNET_NAT_AUTO_Test *tst = ca->h; 324 struct GNUNET_NAT_AUTO_Test *tst = ca->h;
322 325
323 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, ca); 326 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, ca);
324 GNUNET_MQ_destroy(ca->mq); 327 GNUNET_MQ_destroy (ca->mq);
325 GNUNET_free(ca); 328 GNUNET_free (ca);
326} 329}
327 330
328 331
@@ -339,12 +342,12 @@ mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
339 * @param addrlen actual length of the @a addr 342 * @param addrlen actual length of the @a addr
340 */ 343 */
341static void 344static void
342addr_cb(void *cls, 345addr_cb (void *cls,
343 void **app_ctx, 346 void **app_ctx,
344 int add_remove, 347 int add_remove,
345 enum GNUNET_NAT_AddressClass ac, 348 enum GNUNET_NAT_AddressClass ac,
346 const struct sockaddr *addr, 349 const struct sockaddr *addr,
347 socklen_t addrlen) 350 socklen_t addrlen)
348{ 351{
349 struct GNUNET_NAT_AUTO_Test *h = cls; 352 struct GNUNET_NAT_AUTO_Test *h = cls;
350 struct ClientActivity *ca; 353 struct ClientActivity *ca;
@@ -352,42 +355,42 @@ addr_cb(void *cls,
352 struct GNUNET_NAT_AUTO_TestMessage *msg; 355 struct GNUNET_NAT_AUTO_TestMessage *msg;
353 const struct sockaddr_in *sa; 356 const struct sockaddr_in *sa;
354 357
355 (void)app_ctx; 358 (void) app_ctx;
356 if (GNUNET_YES != add_remove) 359 if (GNUNET_YES != add_remove)
357 return; 360 return;
358 if (addrlen != sizeof(struct sockaddr_in)) 361 if (addrlen != sizeof(struct sockaddr_in))
359 { 362 {
360 LOG(GNUNET_ERROR_TYPE_DEBUG, 363 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "NAT test ignores IPv6 address `%s' returned from NAT library\n", 364 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
362 GNUNET_a2s(addr, addrlen)); 365 GNUNET_a2s (addr, addrlen));
363 return; /* ignore IPv6 here */ 366 return; /* ignore IPv6 here */
364 } 367 }
365 LOG(GNUNET_ERROR_TYPE_INFO, 368 LOG (GNUNET_ERROR_TYPE_INFO,
366 "Asking gnunet-nat-server to connect to `%s'\n", 369 "Asking gnunet-nat-server to connect to `%s'\n",
367 GNUNET_a2s(addr, addrlen)); 370 GNUNET_a2s (addr, addrlen));
368 371
369 ca = GNUNET_new(struct ClientActivity); 372 ca = GNUNET_new (struct ClientActivity);
370 ca->h = h; 373 ca->h = h;
371 ca->mq = GNUNET_CLIENT_connect(h->cfg, 374 ca->mq = GNUNET_CLIENT_connect (h->cfg,
372 "gnunet-nat-server", 375 "gnunet-nat-server",
373 NULL, 376 NULL,
374 &mq_error_handler, 377 &mq_error_handler,
375 ca); 378 ca);
376 if (NULL == ca->mq) 379 if (NULL == ca->mq)
377 { 380 {
378 GNUNET_free(ca); 381 GNUNET_free (ca);
379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380 _("Failed to connect to `gnunet-nat-server'\n")); 383 _ ("Failed to connect to `gnunet-nat-server'\n"));
381 return; 384 return;
382 } 385 }
383 GNUNET_CONTAINER_DLL_insert(h->ca_head, h->ca_tail, ca); 386 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
384 sa = (const struct sockaddr_in *)addr; 387 sa = (const struct sockaddr_in *) addr;
385 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_NAT_TEST); 388 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAT_TEST);
386 msg->dst_ipv4 = sa->sin_addr.s_addr; 389 msg->dst_ipv4 = sa->sin_addr.s_addr;
387 msg->dport = sa->sin_port; 390 msg->dport = sa->sin_port;
388 msg->data = h->data; 391 msg->data = h->data;
389 msg->is_tcp = htonl((uint32_t)(h->proto == IPPROTO_TCP)); 392 msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP));
390 GNUNET_MQ_send(ca->mq, env); 393 GNUNET_MQ_send (ca->mq, env);
391} 394}
392 395
393 396
@@ -399,12 +402,12 @@ addr_cb(void *cls,
399 * @param cls handle to the timed out NAT test 402 * @param cls handle to the timed out NAT test
400 */ 403 */
401static void 404static void
402do_fail(void *cls) 405do_fail (void *cls)
403{ 406{
404 struct GNUNET_NAT_AUTO_Test *nh = cls; 407 struct GNUNET_NAT_AUTO_Test *nh = cls;
405 408
406 nh->ttask = NULL; 409 nh->ttask = NULL;
407 nh->report(nh->report_cls, nh->status); 410 nh->report (nh->report_cls, nh->status);
408} 411}
409 412
410 413
@@ -420,128 +423,128 @@ do_fail(void *cls)
420 * @return handle to cancel NAT test 423 * @return handle to cancel NAT test
421 */ 424 */
422struct GNUNET_NAT_AUTO_Test * 425struct GNUNET_NAT_AUTO_Test *
423GNUNET_NAT_AUTO_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg, 426GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
424 uint8_t proto, 427 uint8_t proto,
425 const char *section_name, 428 const char *section_name,
426 GNUNET_NAT_TestCallback report, 429 GNUNET_NAT_TestCallback report,
427 void *report_cls) 430 void *report_cls)
428{ 431{
429 struct GNUNET_NAT_AUTO_Test *nh; 432 struct GNUNET_NAT_AUTO_Test *nh;
430 unsigned long long bnd_port; 433 unsigned long long bnd_port;
431 struct sockaddr_in sa; 434 struct sockaddr_in sa;
432 const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa }; 435 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
433 const socklen_t addrlens[] = { sizeof(sa) }; 436 const socklen_t addrlens[] = { sizeof(sa) };
434 437
435 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, 438 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
436 section_name, 439 section_name,
437 "PORT", 440 "PORT",
438 &bnd_port)) || 441 &bnd_port)) ||
439 (bnd_port > 65535)) 442 (bnd_port > 65535))
440 { 443 {
441 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("Failed to find valid PORT in section `%s'\n"), 445 _ ("Failed to find valid PORT in section `%s'\n"),
443 section_name); 446 section_name);
444 return NULL; 447 return NULL;
445 } 448 }
446 449
447 memset(&sa, 0, sizeof(sa)); 450 memset (&sa, 0, sizeof(sa));
448 sa.sin_family = AF_INET; 451 sa.sin_family = AF_INET;
449 sa.sin_port = htons((uint16_t)bnd_port); 452 sa.sin_port = htons ((uint16_t) bnd_port);
450#if HAVE_SOCKADDR_IN_SIN_LEN 453#if HAVE_SOCKADDR_IN_SIN_LEN
451 sa.sin_len = sizeof(sa); 454 sa.sin_len = sizeof(sa);
452#endif 455#endif
453 456
454 nh = GNUNET_new(struct GNUNET_NAT_AUTO_Test); 457 nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test);
455 nh->cfg = cfg; 458 nh->cfg = cfg;
456 nh->proto = proto; 459 nh->proto = proto;
457 nh->section_name = GNUNET_strdup(section_name); 460 nh->section_name = GNUNET_strdup (section_name);
458 nh->report = report; 461 nh->report = report;
459 nh->report_cls = report_cls; 462 nh->report_cls = report_cls;
460 nh->status = GNUNET_NAT_ERROR_SUCCESS; 463 nh->status = GNUNET_NAT_ERROR_SUCCESS;
461 if (0 == bnd_port) 464 if (0 == bnd_port)
465 {
466 nh->nat = GNUNET_NAT_register (cfg,
467 section_name,
468 proto,
469 0,
470 NULL,
471 NULL,
472 &addr_cb,
473 &reversal_cb,
474 nh);
475 }
476 else
477 {
478 nh->lsock =
479 GNUNET_NETWORK_socket_create (AF_INET,
480 (IPPROTO_UDP == proto) ? SOCK_DGRAM
481 : SOCK_STREAM,
482 proto);
483 if ((NULL == nh->lsock) ||
484 (GNUNET_OK != GNUNET_NETWORK_socket_bind (nh->lsock,
485 (const struct sockaddr *) &sa,
486 sizeof(sa))))
462 { 487 {
463 nh->nat = GNUNET_NAT_register(cfg, 488 LOG (GNUNET_ERROR_TYPE_ERROR,
464 section_name, 489 _ ("Failed to create socket bound to `%s' for NAT test: %s\n"),
465 proto, 490 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof(sa)),
466 0, 491 strerror (errno));
467 NULL, 492 if (NULL != nh->lsock)
468 NULL, 493 {
469 &addr_cb, 494 GNUNET_NETWORK_socket_close (nh->lsock);
470 &reversal_cb, 495 nh->lsock = NULL;
471 nh); 496 }
497 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
498 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh);
499 return nh;
472 } 500 }
473 else 501 if (IPPROTO_TCP == proto)
502 {
503 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
504 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
505 nh->lsock,
506 &do_accept,
507 nh);
508 }
509 else
474 { 510 {
475 nh->lsock = 511 nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
476 GNUNET_NETWORK_socket_create(AF_INET, 512 nh->lsock,
477 (IPPROTO_UDP == proto) ? SOCK_DGRAM 513 &do_udp_read,
478 : SOCK_STREAM, 514 nh);
479 proto);
480 if ((NULL == nh->lsock) ||
481 (GNUNET_OK != GNUNET_NETWORK_socket_bind(nh->lsock,
482 (const struct sockaddr *)&sa,
483 sizeof(sa))))
484 {
485 LOG(GNUNET_ERROR_TYPE_ERROR,
486 _("Failed to create socket bound to `%s' for NAT test: %s\n"),
487 GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)),
488 strerror(errno));
489 if (NULL != nh->lsock)
490 {
491 GNUNET_NETWORK_socket_close(nh->lsock);
492 nh->lsock = NULL;
493 }
494 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
495 nh->ttask = GNUNET_SCHEDULER_add_now(&do_fail, nh);
496 return nh;
497 }
498 if (IPPROTO_TCP == proto)
499 {
500 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_listen(nh->lsock, 5));
501 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
502 nh->lsock,
503 &do_accept,
504 nh);
505 }
506 else
507 {
508 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
509 nh->lsock,
510 &do_udp_read,
511 nh);
512 }
513 LOG(GNUNET_ERROR_TYPE_INFO,
514 "NAT test listens on port %llu (%s)\n",
515 bnd_port,
516 (IPPROTO_TCP == proto) ? "tcp" : "udp");
517 nh->nat = GNUNET_NAT_register(cfg,
518 section_name,
519 proto,
520 1,
521 addrs,
522 addrlens,
523 &addr_cb,
524 NULL,
525 nh);
526 if (NULL == nh->nat)
527 {
528 LOG(GNUNET_ERROR_TYPE_INFO,
529 _("NAT test failed to start NAT library\n"));
530 if (NULL != nh->ltask)
531 {
532 GNUNET_SCHEDULER_cancel(nh->ltask);
533 nh->ltask = NULL;
534 }
535 if (NULL != nh->lsock)
536 {
537 GNUNET_NETWORK_socket_close(nh->lsock);
538 nh->lsock = NULL;
539 }
540 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
541 nh->ttask = GNUNET_SCHEDULER_add_now(&do_fail, nh);
542 return nh;
543 }
544 } 515 }
516 LOG (GNUNET_ERROR_TYPE_INFO,
517 "NAT test listens on port %llu (%s)\n",
518 bnd_port,
519 (IPPROTO_TCP == proto) ? "tcp" : "udp");
520 nh->nat = GNUNET_NAT_register (cfg,
521 section_name,
522 proto,
523 1,
524 addrs,
525 addrlens,
526 &addr_cb,
527 NULL,
528 nh);
529 if (NULL == nh->nat)
530 {
531 LOG (GNUNET_ERROR_TYPE_INFO,
532 _ ("NAT test failed to start NAT library\n"));
533 if (NULL != nh->ltask)
534 {
535 GNUNET_SCHEDULER_cancel (nh->ltask);
536 nh->ltask = NULL;
537 }
538 if (NULL != nh->lsock)
539 {
540 GNUNET_NETWORK_socket_close (nh->lsock);
541 nh->lsock = NULL;
542 }
543 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
544 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh);
545 return nh;
546 }
547 }
545 return nh; 548 return nh;
546} 549}
547 550
@@ -552,47 +555,47 @@ GNUNET_NAT_AUTO_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
552 * @param tst test to stop. 555 * @param tst test to stop.
553 */ 556 */
554void 557void
555GNUNET_NAT_AUTO_test_stop(struct GNUNET_NAT_AUTO_Test *tst) 558GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst)
556{ 559{
557 struct NatActivity *pos; 560 struct NatActivity *pos;
558 struct ClientActivity *cpos; 561 struct ClientActivity *cpos;
559 562
560 LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n"); 563 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
561 while (NULL != (cpos = tst->ca_head)) 564 while (NULL != (cpos = tst->ca_head))
562 { 565 {
563 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos); 566 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
564 GNUNET_MQ_destroy(cpos->mq); 567 GNUNET_MQ_destroy (cpos->mq);
565 GNUNET_free(cpos); 568 GNUNET_free (cpos);
566 } 569 }
567 while (NULL != (pos = tst->na_head)) 570 while (NULL != (pos = tst->na_head))
568 { 571 {
569 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, pos); 572 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
570 GNUNET_SCHEDULER_cancel(pos->rtask); 573 GNUNET_SCHEDULER_cancel (pos->rtask);
571 GNUNET_NETWORK_socket_close(pos->sock); 574 GNUNET_NETWORK_socket_close (pos->sock);
572 GNUNET_free(pos); 575 GNUNET_free (pos);
573 } 576 }
574 if (NULL != tst->ttask) 577 if (NULL != tst->ttask)
575 { 578 {
576 GNUNET_SCHEDULER_cancel(tst->ttask); 579 GNUNET_SCHEDULER_cancel (tst->ttask);
577 tst->ttask = NULL; 580 tst->ttask = NULL;
578 } 581 }
579 if (NULL != tst->ltask) 582 if (NULL != tst->ltask)
580 { 583 {
581 GNUNET_SCHEDULER_cancel(tst->ltask); 584 GNUNET_SCHEDULER_cancel (tst->ltask);
582 tst->ltask = NULL; 585 tst->ltask = NULL;
583 } 586 }
584 if (NULL != tst->lsock) 587 if (NULL != tst->lsock)
585 { 588 {
586 GNUNET_NETWORK_socket_close(tst->lsock); 589 GNUNET_NETWORK_socket_close (tst->lsock);
587 tst->lsock = NULL; 590 tst->lsock = NULL;
588 } 591 }
589 if (NULL != tst->nat) 592 if (NULL != tst->nat)
590 { 593 {
591 GNUNET_NAT_unregister(tst->nat); 594 GNUNET_NAT_unregister (tst->nat);
592 tst->nat = NULL; 595 tst->nat = NULL;
593 } 596 }
594 GNUNET_free(tst->section_name); 597 GNUNET_free (tst->section_name);
595 GNUNET_free(tst); 598 GNUNET_free (tst);
596} 599}
597 600
598/* end of nat_auto_api_test.c */ 601/* end of nat_auto_api_test.c */