diff options
Diffstat (limited to 'src/nat-auto/nat_auto_api_test.c')
-rw-r--r-- | src/nat-auto/nat_auto_api_test.c | 490 |
1 files changed, 243 insertions, 247 deletions
diff --git a/src/nat-auto/nat_auto_api_test.c b/src/nat-auto/nat_auto_api_test.c index 56a2b8d72..daea3063c 100644 --- a/src/nat-auto/nat_auto_api_test.c +++ b/src/nat-auto/nat_auto_api_test.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file nat/nat_auto_api_test.c | 21 | * @file nat/nat_auto_api_test.c |
22 | * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server) | 22 | * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server) |
@@ -28,16 +28,15 @@ | |||
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 | */ |
39 | struct NatActivity | 39 | struct NatActivity { |
40 | { | ||
41 | /** | 40 | /** |
42 | * This is a doubly-linked list. | 41 | * This is a doubly-linked list. |
43 | */ | 42 | */ |
@@ -68,8 +67,7 @@ struct NatActivity | |||
68 | /** | 67 | /** |
69 | * Entry we keep for each connection to the gnunet-nat-service. | 68 | * Entry we keep for each connection to the gnunet-nat-service. |
70 | */ | 69 | */ |
71 | struct ClientActivity | 70 | struct ClientActivity { |
72 | { | ||
73 | /** | 71 | /** |
74 | * This is a doubly-linked list. | 72 | * This is a doubly-linked list. |
75 | */ | 73 | */ |
@@ -95,9 +93,7 @@ struct ClientActivity | |||
95 | /** | 93 | /** |
96 | * Handle to a NAT test. | 94 | * Handle to a NAT test. |
97 | */ | 95 | */ |
98 | struct GNUNET_NAT_AUTO_Test | 96 | struct GNUNET_NAT_AUTO_Test { |
99 | { | ||
100 | |||
101 | /** | 97 | /** |
102 | * Configuration used | 98 | * Configuration used |
103 | */ | 99 | */ |
@@ -184,22 +180,22 @@ struct GNUNET_NAT_AUTO_Test | |||
184 | * @param addrlen actual lenght of the @a addr | 180 | * @param addrlen actual lenght of the @a addr |
185 | */ | 181 | */ |
186 | static void | 182 | static void |
187 | reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen) | 183 | reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen) |
188 | { | 184 | { |
189 | struct GNUNET_NAT_AUTO_Test *h = cls; | 185 | struct GNUNET_NAT_AUTO_Test *h = cls; |
190 | const struct sockaddr_in *sa; | 186 | const struct sockaddr_in *sa; |
191 | 187 | ||
192 | if (sizeof (struct sockaddr_in) != addrlen) | 188 | if (sizeof(struct sockaddr_in) != addrlen) |
193 | return; | 189 | return; |
194 | sa = (const struct sockaddr_in *) addr; | 190 | sa = (const struct sockaddr_in *)addr; |
195 | if (h->data != sa->sin_port) | 191 | if (h->data != sa->sin_port) |
196 | { | 192 | { |
197 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 193 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
198 | "Received connection reversal request for wrong port\n"); | 194 | "Received connection reversal request for wrong port\n"); |
199 | return; /* wrong port */ | 195 | return; /* wrong port */ |
200 | } | 196 | } |
201 | /* report success */ | 197 | /* report success */ |
202 | h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS); | 198 | h->report(h->report_cls, GNUNET_NAT_ERROR_SUCCESS); |
203 | } | 199 | } |
204 | 200 | ||
205 | 201 | ||
@@ -210,31 +206,31 @@ reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen) | |||
210 | * @param cls the `struct GNUNET_NAT_AUTO_Test` | 206 | * @param cls the `struct GNUNET_NAT_AUTO_Test` |
211 | */ | 207 | */ |
212 | static void | 208 | static void |
213 | do_udp_read (void *cls) | 209 | do_udp_read(void *cls) |
214 | { | 210 | { |
215 | struct GNUNET_NAT_AUTO_Test *tst = cls; | 211 | struct GNUNET_NAT_AUTO_Test *tst = cls; |
216 | uint16_t data; | 212 | uint16_t data; |
217 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 213 | const struct GNUNET_SCHEDULER_TaskContext *tc; |
218 | 214 | ||
219 | tc = GNUNET_SCHEDULER_get_task_context (); | 215 | tc = GNUNET_SCHEDULER_get_task_context(); |
220 | tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 216 | tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, |
221 | tst->lsock, | 217 | tst->lsock, |
222 | &do_udp_read, | 218 | &do_udp_read, |
223 | tst); | 219 | tst); |
224 | if ((NULL != tc->write_ready) && | 220 | if ((NULL != tc->write_ready) && |
225 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) && | 221 | (GNUNET_NETWORK_fdset_isset(tc->read_ready, tst->lsock)) && |
226 | (sizeof (data) == | 222 | (sizeof(data) == |
227 | GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) | 223 | GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data)))) |
228 | { | 224 | { |
229 | if (data == tst->data) | 225 | if (data == tst->data) |
230 | tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); | 226 | tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); |
231 | else | 227 | else |
232 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 228 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
233 | "Received data mismatches expected value\n"); | 229 | "Received data mismatches expected value\n"); |
234 | } | 230 | } |
235 | else | 231 | else |
236 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 232 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
237 | "Failed to receive data from inbound connection\n"); | 233 | "Failed to receive data from inbound connection\n"); |
238 | } | 234 | } |
239 | 235 | ||
240 | 236 | ||
@@ -245,33 +241,33 @@ do_udp_read (void *cls) | |||
245 | * @param cls the `struct NatActivity` | 241 | * @param cls the `struct NatActivity` |
246 | */ | 242 | */ |
247 | static void | 243 | static void |
248 | do_read (void *cls) | 244 | do_read(void *cls) |
249 | { | 245 | { |
250 | struct NatActivity *na = cls; | 246 | struct NatActivity *na = cls; |
251 | struct GNUNET_NAT_AUTO_Test *tst; | 247 | struct GNUNET_NAT_AUTO_Test *tst; |
252 | uint16_t data; | 248 | uint16_t data; |
253 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 249 | const struct GNUNET_SCHEDULER_TaskContext *tc; |
254 | 250 | ||
255 | tc = GNUNET_SCHEDULER_get_task_context (); | 251 | tc = GNUNET_SCHEDULER_get_task_context(); |
256 | na->rtask = NULL; | 252 | na->rtask = NULL; |
257 | tst = na->h; | 253 | tst = na->h; |
258 | GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na); | 254 | GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, na); |
259 | if ((NULL != tc->write_ready) && | 255 | if ((NULL != tc->write_ready) && |
260 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) && | 256 | (GNUNET_NETWORK_fdset_isset(tc->read_ready, na->sock)) && |
261 | (sizeof (data) == | 257 | (sizeof(data) == |
262 | GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) | 258 | GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data)))) |
263 | { | 259 | { |
264 | if (data == tst->data) | 260 | if (data == tst->data) |
265 | tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); | 261 | tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); |
266 | else | 262 | else |
267 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 263 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
268 | "Received data does not match expected value\n"); | 264 | "Received data does not match expected value\n"); |
269 | } | 265 | } |
270 | else | 266 | else |
271 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 267 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
272 | "Failed to receive data from inbound connection\n"); | 268 | "Failed to receive data from inbound connection\n"); |
273 | GNUNET_NETWORK_socket_close (na->sock); | 269 | GNUNET_NETWORK_socket_close(na->sock); |
274 | GNUNET_free (na); | 270 | GNUNET_free(na); |
275 | } | 271 | } |
276 | 272 | ||
277 | 273 | ||
@@ -282,32 +278,32 @@ do_read (void *cls) | |||
282 | * @param cls the `struct GNUNET_NAT_AUTO_Test` | 278 | * @param cls the `struct GNUNET_NAT_AUTO_Test` |
283 | */ | 279 | */ |
284 | static void | 280 | static void |
285 | do_accept (void *cls) | 281 | do_accept(void *cls) |
286 | { | 282 | { |
287 | struct GNUNET_NAT_AUTO_Test *tst = cls; | 283 | struct GNUNET_NAT_AUTO_Test *tst = cls; |
288 | struct GNUNET_NETWORK_Handle *s; | 284 | struct GNUNET_NETWORK_Handle *s; |
289 | struct NatActivity *wl; | 285 | struct NatActivity *wl; |
290 | 286 | ||
291 | tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 287 | tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, |
292 | tst->lsock, | 288 | tst->lsock, |
293 | &do_accept, | 289 | &do_accept, |
294 | tst); | 290 | tst); |
295 | s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL); | 291 | s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL); |
296 | if (NULL == s) | 292 | if (NULL == s) |
297 | { | 293 | { |
298 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept"); | 294 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, "accept"); |
299 | return; /* odd error */ | 295 | return; /* odd error */ |
300 | } | 296 | } |
301 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 297 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
302 | "Got an inbound connection, waiting for data\n"); | 298 | "Got an inbound connection, waiting for data\n"); |
303 | wl = GNUNET_new (struct NatActivity); | 299 | wl = GNUNET_new(struct NatActivity); |
304 | wl->sock = s; | 300 | wl->sock = s; |
305 | wl->h = tst; | 301 | wl->h = tst; |
306 | wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 302 | wl->rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, |
307 | wl->sock, | 303 | wl->sock, |
308 | &do_read, | 304 | &do_read, |
309 | wl); | 305 | wl); |
310 | GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl); | 306 | GNUNET_CONTAINER_DLL_insert(tst->na_head, tst->na_tail, wl); |
311 | } | 307 | } |
312 | 308 | ||
313 | 309 | ||
@@ -319,14 +315,14 @@ do_accept (void *cls) | |||
319 | * @param error error code | 315 | * @param error error code |
320 | */ | 316 | */ |
321 | static void | 317 | static void |
322 | mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | 318 | mq_error_handler(void *cls, enum GNUNET_MQ_Error error) |
323 | { | 319 | { |
324 | struct ClientActivity *ca = cls; | 320 | struct ClientActivity *ca = cls; |
325 | struct GNUNET_NAT_AUTO_Test *tst = ca->h; | 321 | struct GNUNET_NAT_AUTO_Test *tst = ca->h; |
326 | 322 | ||
327 | GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, ca); | 323 | GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, ca); |
328 | GNUNET_MQ_destroy (ca->mq); | 324 | GNUNET_MQ_destroy(ca->mq); |
329 | GNUNET_free (ca); | 325 | GNUNET_free(ca); |
330 | } | 326 | } |
331 | 327 | ||
332 | 328 | ||
@@ -343,12 +339,12 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | |||
343 | * @param addrlen actual length of the @a addr | 339 | * @param addrlen actual length of the @a addr |
344 | */ | 340 | */ |
345 | static void | 341 | static void |
346 | addr_cb (void *cls, | 342 | addr_cb(void *cls, |
347 | void **app_ctx, | 343 | void **app_ctx, |
348 | int add_remove, | 344 | int add_remove, |
349 | enum GNUNET_NAT_AddressClass ac, | 345 | enum GNUNET_NAT_AddressClass ac, |
350 | const struct sockaddr *addr, | 346 | const struct sockaddr *addr, |
351 | socklen_t addrlen) | 347 | socklen_t addrlen) |
352 | { | 348 | { |
353 | struct GNUNET_NAT_AUTO_Test *h = cls; | 349 | struct GNUNET_NAT_AUTO_Test *h = cls; |
354 | struct ClientActivity *ca; | 350 | struct ClientActivity *ca; |
@@ -356,42 +352,42 @@ addr_cb (void *cls, | |||
356 | struct GNUNET_NAT_AUTO_TestMessage *msg; | 352 | struct GNUNET_NAT_AUTO_TestMessage *msg; |
357 | const struct sockaddr_in *sa; | 353 | const struct sockaddr_in *sa; |
358 | 354 | ||
359 | (void) app_ctx; | 355 | (void)app_ctx; |
360 | if (GNUNET_YES != add_remove) | 356 | if (GNUNET_YES != add_remove) |
361 | return; | 357 | return; |
362 | if (addrlen != sizeof (struct sockaddr_in)) | 358 | if (addrlen != sizeof(struct sockaddr_in)) |
363 | { | 359 | { |
364 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 360 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
365 | "NAT test ignores IPv6 address `%s' returned from NAT library\n", | 361 | "NAT test ignores IPv6 address `%s' returned from NAT library\n", |
366 | GNUNET_a2s (addr, addrlen)); | 362 | GNUNET_a2s(addr, addrlen)); |
367 | return; /* ignore IPv6 here */ | 363 | return; /* ignore IPv6 here */ |
368 | } | 364 | } |
369 | LOG (GNUNET_ERROR_TYPE_INFO, | 365 | LOG(GNUNET_ERROR_TYPE_INFO, |
370 | "Asking gnunet-nat-server to connect to `%s'\n", | 366 | "Asking gnunet-nat-server to connect to `%s'\n", |
371 | GNUNET_a2s (addr, addrlen)); | 367 | GNUNET_a2s(addr, addrlen)); |
372 | 368 | ||
373 | ca = GNUNET_new (struct ClientActivity); | 369 | ca = GNUNET_new(struct ClientActivity); |
374 | ca->h = h; | 370 | ca->h = h; |
375 | ca->mq = GNUNET_CLIENT_connect (h->cfg, | 371 | ca->mq = GNUNET_CLIENT_connect(h->cfg, |
376 | "gnunet-nat-server", | 372 | "gnunet-nat-server", |
377 | NULL, | 373 | NULL, |
378 | &mq_error_handler, | 374 | &mq_error_handler, |
379 | ca); | 375 | ca); |
380 | if (NULL == ca->mq) | 376 | if (NULL == ca->mq) |
381 | { | 377 | { |
382 | GNUNET_free (ca); | 378 | GNUNET_free(ca); |
383 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 379 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
384 | _ ("Failed to connect to `gnunet-nat-server'\n")); | 380 | _("Failed to connect to `gnunet-nat-server'\n")); |
385 | return; | 381 | return; |
386 | } | 382 | } |
387 | GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca); | 383 | GNUNET_CONTAINER_DLL_insert(h->ca_head, h->ca_tail, ca); |
388 | sa = (const struct sockaddr_in *) addr; | 384 | sa = (const struct sockaddr_in *)addr; |
389 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAT_TEST); | 385 | env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_NAT_TEST); |
390 | msg->dst_ipv4 = sa->sin_addr.s_addr; | 386 | msg->dst_ipv4 = sa->sin_addr.s_addr; |
391 | msg->dport = sa->sin_port; | 387 | msg->dport = sa->sin_port; |
392 | msg->data = h->data; | 388 | msg->data = h->data; |
393 | msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP)); | 389 | msg->is_tcp = htonl((uint32_t)(h->proto == IPPROTO_TCP)); |
394 | GNUNET_MQ_send (ca->mq, env); | 390 | GNUNET_MQ_send(ca->mq, env); |
395 | } | 391 | } |
396 | 392 | ||
397 | 393 | ||
@@ -403,12 +399,12 @@ addr_cb (void *cls, | |||
403 | * @param cls handle to the timed out NAT test | 399 | * @param cls handle to the timed out NAT test |
404 | */ | 400 | */ |
405 | static void | 401 | static void |
406 | do_fail (void *cls) | 402 | do_fail(void *cls) |
407 | { | 403 | { |
408 | struct GNUNET_NAT_AUTO_Test *nh = cls; | 404 | struct GNUNET_NAT_AUTO_Test *nh = cls; |
409 | 405 | ||
410 | nh->ttask = NULL; | 406 | nh->ttask = NULL; |
411 | nh->report (nh->report_cls, nh->status); | 407 | nh->report(nh->report_cls, nh->status); |
412 | } | 408 | } |
413 | 409 | ||
414 | 410 | ||
@@ -424,128 +420,128 @@ do_fail (void *cls) | |||
424 | * @return handle to cancel NAT test | 420 | * @return handle to cancel NAT test |
425 | */ | 421 | */ |
426 | struct GNUNET_NAT_AUTO_Test * | 422 | struct GNUNET_NAT_AUTO_Test * |
427 | GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | 423 | GNUNET_NAT_AUTO_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg, |
428 | uint8_t proto, | 424 | uint8_t proto, |
429 | const char *section_name, | 425 | const char *section_name, |
430 | GNUNET_NAT_TestCallback report, | 426 | GNUNET_NAT_TestCallback report, |
431 | void *report_cls) | 427 | void *report_cls) |
432 | { | 428 | { |
433 | struct GNUNET_NAT_AUTO_Test *nh; | 429 | struct GNUNET_NAT_AUTO_Test *nh; |
434 | unsigned long long bnd_port; | 430 | unsigned long long bnd_port; |
435 | struct sockaddr_in sa; | 431 | struct sockaddr_in sa; |
436 | const struct sockaddr *addrs[] = {(const struct sockaddr *) &sa}; | 432 | const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa }; |
437 | const socklen_t addrlens[] = {sizeof (sa)}; | 433 | const socklen_t addrlens[] = { sizeof(sa) }; |
438 | 434 | ||
439 | if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, | 435 | if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, |
440 | section_name, | 436 | section_name, |
441 | "PORT", | 437 | "PORT", |
442 | &bnd_port)) || | 438 | &bnd_port)) || |
443 | (bnd_port > 65535)) | 439 | (bnd_port > 65535)) |
444 | { | 440 | { |
445 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 441 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
446 | _ ("Failed to find valid PORT in section `%s'\n"), | 442 | _("Failed to find valid PORT in section `%s'\n"), |
447 | section_name); | 443 | section_name); |
448 | return NULL; | 444 | return NULL; |
449 | } | 445 | } |
450 | 446 | ||
451 | memset (&sa, 0, sizeof (sa)); | 447 | memset(&sa, 0, sizeof(sa)); |
452 | sa.sin_family = AF_INET; | 448 | sa.sin_family = AF_INET; |
453 | sa.sin_port = htons ((uint16_t) bnd_port); | 449 | sa.sin_port = htons((uint16_t)bnd_port); |
454 | #if HAVE_SOCKADDR_IN_SIN_LEN | 450 | #if HAVE_SOCKADDR_IN_SIN_LEN |
455 | sa.sin_len = sizeof (sa); | 451 | sa.sin_len = sizeof(sa); |
456 | #endif | 452 | #endif |
457 | 453 | ||
458 | nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test); | 454 | nh = GNUNET_new(struct GNUNET_NAT_AUTO_Test); |
459 | nh->cfg = cfg; | 455 | nh->cfg = cfg; |
460 | nh->proto = proto; | 456 | nh->proto = proto; |
461 | nh->section_name = GNUNET_strdup (section_name); | 457 | nh->section_name = GNUNET_strdup(section_name); |
462 | nh->report = report; | 458 | nh->report = report; |
463 | nh->report_cls = report_cls; | 459 | nh->report_cls = report_cls; |
464 | nh->status = GNUNET_NAT_ERROR_SUCCESS; | 460 | nh->status = GNUNET_NAT_ERROR_SUCCESS; |
465 | if (0 == bnd_port) | 461 | if (0 == bnd_port) |
466 | { | ||
467 | nh->nat = GNUNET_NAT_register (cfg, | ||
468 | section_name, | ||
469 | proto, | ||
470 | 0, | ||
471 | NULL, | ||
472 | NULL, | ||
473 | &addr_cb, | ||
474 | &reversal_cb, | ||
475 | nh); | ||
476 | } | ||
477 | else | ||
478 | { | ||
479 | nh->lsock = | ||
480 | GNUNET_NETWORK_socket_create (AF_INET, | ||
481 | (IPPROTO_UDP == proto) ? SOCK_DGRAM | ||
482 | : SOCK_STREAM, | ||
483 | proto); | ||
484 | if ((NULL == nh->lsock) || | ||
485 | (GNUNET_OK != GNUNET_NETWORK_socket_bind (nh->lsock, | ||
486 | (const struct sockaddr *) &sa, | ||
487 | sizeof (sa)))) | ||
488 | { | ||
489 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
490 | _ ("Failed to create socket bound to `%s' for NAT test: %s\n"), | ||
491 | GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)), | ||
492 | strerror (errno)); | ||
493 | if (NULL != nh->lsock) | ||
494 | { | ||
495 | GNUNET_NETWORK_socket_close (nh->lsock); | ||
496 | nh->lsock = NULL; | ||
497 | } | ||
498 | nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR; | ||
499 | nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh); | ||
500 | return nh; | ||
501 | } | ||
502 | if (IPPROTO_TCP == proto) | ||
503 | { | 462 | { |
504 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5)); | 463 | nh->nat = GNUNET_NAT_register(cfg, |
505 | nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 464 | section_name, |
506 | nh->lsock, | 465 | proto, |
507 | &do_accept, | 466 | 0, |
508 | nh); | 467 | NULL, |
468 | NULL, | ||
469 | &addr_cb, | ||
470 | &reversal_cb, | ||
471 | nh); | ||
509 | } | 472 | } |
510 | else | 473 | else |
511 | { | ||
512 | nh->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
513 | nh->lsock, | ||
514 | &do_udp_read, | ||
515 | nh); | ||
516 | } | ||
517 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
518 | "NAT test listens on port %llu (%s)\n", | ||
519 | bnd_port, | ||
520 | (IPPROTO_TCP == proto) ? "tcp" : "udp"); | ||
521 | nh->nat = GNUNET_NAT_register (cfg, | ||
522 | section_name, | ||
523 | proto, | ||
524 | 1, | ||
525 | addrs, | ||
526 | addrlens, | ||
527 | &addr_cb, | ||
528 | NULL, | ||
529 | nh); | ||
530 | if (NULL == nh->nat) | ||
531 | { | 474 | { |
532 | LOG (GNUNET_ERROR_TYPE_INFO, | 475 | nh->lsock = |
533 | _ ("NAT test failed to start NAT library\n")); | 476 | GNUNET_NETWORK_socket_create(AF_INET, |
534 | if (NULL != nh->ltask) | 477 | (IPPROTO_UDP == proto) ? SOCK_DGRAM |
535 | { | 478 | : SOCK_STREAM, |
536 | GNUNET_SCHEDULER_cancel (nh->ltask); | 479 | proto); |
537 | nh->ltask = NULL; | 480 | if ((NULL == nh->lsock) || |
538 | } | 481 | (GNUNET_OK != GNUNET_NETWORK_socket_bind(nh->lsock, |
539 | if (NULL != nh->lsock) | 482 | (const struct sockaddr *)&sa, |
540 | { | 483 | sizeof(sa)))) |
541 | GNUNET_NETWORK_socket_close (nh->lsock); | 484 | { |
542 | nh->lsock = NULL; | 485 | LOG(GNUNET_ERROR_TYPE_ERROR, |
543 | } | 486 | _("Failed to create socket bound to `%s' for NAT test: %s\n"), |
544 | nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; | 487 | GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)), |
545 | nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh); | 488 | strerror(errno)); |
546 | return nh; | 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 | } | ||
547 | } | 544 | } |
548 | } | ||
549 | return nh; | 545 | return nh; |
550 | } | 546 | } |
551 | 547 | ||
@@ -556,47 +552,47 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
556 | * @param tst test to stop. | 552 | * @param tst test to stop. |
557 | */ | 553 | */ |
558 | void | 554 | void |
559 | GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst) | 555 | GNUNET_NAT_AUTO_test_stop(struct GNUNET_NAT_AUTO_Test *tst) |
560 | { | 556 | { |
561 | struct NatActivity *pos; | 557 | struct NatActivity *pos; |
562 | struct ClientActivity *cpos; | 558 | struct ClientActivity *cpos; |
563 | 559 | ||
564 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n"); | 560 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n"); |
565 | while (NULL != (cpos = tst->ca_head)) | 561 | while (NULL != (cpos = tst->ca_head)) |
566 | { | 562 | { |
567 | GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos); | 563 | GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos); |
568 | GNUNET_MQ_destroy (cpos->mq); | 564 | GNUNET_MQ_destroy(cpos->mq); |
569 | GNUNET_free (cpos); | 565 | GNUNET_free(cpos); |
570 | } | 566 | } |
571 | while (NULL != (pos = tst->na_head)) | 567 | while (NULL != (pos = tst->na_head)) |
572 | { | 568 | { |
573 | GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos); | 569 | GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, pos); |
574 | GNUNET_SCHEDULER_cancel (pos->rtask); | 570 | GNUNET_SCHEDULER_cancel(pos->rtask); |
575 | GNUNET_NETWORK_socket_close (pos->sock); | 571 | GNUNET_NETWORK_socket_close(pos->sock); |
576 | GNUNET_free (pos); | 572 | GNUNET_free(pos); |
577 | } | 573 | } |
578 | if (NULL != tst->ttask) | 574 | if (NULL != tst->ttask) |
579 | { | 575 | { |
580 | GNUNET_SCHEDULER_cancel (tst->ttask); | 576 | GNUNET_SCHEDULER_cancel(tst->ttask); |
581 | tst->ttask = NULL; | 577 | tst->ttask = NULL; |
582 | } | 578 | } |
583 | if (NULL != tst->ltask) | 579 | if (NULL != tst->ltask) |
584 | { | 580 | { |
585 | GNUNET_SCHEDULER_cancel (tst->ltask); | 581 | GNUNET_SCHEDULER_cancel(tst->ltask); |
586 | tst->ltask = NULL; | 582 | tst->ltask = NULL; |
587 | } | 583 | } |
588 | if (NULL != tst->lsock) | 584 | if (NULL != tst->lsock) |
589 | { | 585 | { |
590 | GNUNET_NETWORK_socket_close (tst->lsock); | 586 | GNUNET_NETWORK_socket_close(tst->lsock); |
591 | tst->lsock = NULL; | 587 | tst->lsock = NULL; |
592 | } | 588 | } |
593 | if (NULL != tst->nat) | 589 | if (NULL != tst->nat) |
594 | { | 590 | { |
595 | GNUNET_NAT_unregister (tst->nat); | 591 | GNUNET_NAT_unregister(tst->nat); |
596 | tst->nat = NULL; | 592 | tst->nat = NULL; |
597 | } | 593 | } |
598 | GNUNET_free (tst->section_name); | 594 | GNUNET_free(tst->section_name); |
599 | GNUNET_free (tst); | 595 | GNUNET_free(tst); |
600 | } | 596 | } |
601 | 597 | ||
602 | /* end of nat_auto_api_test.c */ | 598 | /* end of nat_auto_api_test.c */ |