aboutsummaryrefslogtreecommitdiff
path: root/src/nat/test_nat.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-06-25 06:24:01 +0000
committerChristian Grothoff <christian@grothoff.org>2011-06-25 06:24:01 +0000
commit8a3016481ba6aeb36de3950a56e641dda53ca544 (patch)
tree0c5468ec6e5d2165ad5931940e0b8439d09398de /src/nat/test_nat.c
parentb6d4eeb39fd0cd41df164bd0959f58b08aa45e83 (diff)
downloadgnunet-8a3016481ba6aeb36de3950a56e641dda53ca544.tar.gz
gnunet-8a3016481ba6aeb36de3950a56e641dda53ca544.zip
the big NAT change
Diffstat (limited to 'src/nat/test_nat.c')
-rw-r--r--src/nat/test_nat.c114
1 files changed, 72 insertions, 42 deletions
diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c
index d1268c93a..72cff0915 100644
--- a/src/nat/test_nat.c
+++ b/src/nat/test_nat.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -19,20 +19,20 @@
19*/ 19*/
20 20
21/** 21/**
22 * @file nat/test_nat.c
23 * @brief Testcase for NAT library
24 * @author Milan Bouchet-Valat
25 */
26
27/**
28 * Testcase for port redirection and public IP address retrieval. 22 * Testcase for port redirection and public IP address retrieval.
29 * This test never fails, because there need to be a NAT box set up for that. 23 * This test never fails, because there need to be a NAT box set up for that.
30 * So we only get IP address and open the 2086 port using any UPnP and NAT-PMP 24 * So we only get IP address and open the 2086 port using any NAT traversal
31 * routers found, wait for 30s, close ports and return. 25 * method available, wait for 30s, close ports and return.
32 * Have a look at the logs and use NMAP to check that it works with your box. 26 * Have a look at the logs and use NMAP to check that it works with your box.
27 *
28 * @file nat/test_nat.c
29 * @brief Testcase for NAT library
30 * @author Milan Bouchet-Valat
31 * @author Christian Grothoff
32 *
33 * TODO: actually use ARM to start resolver service to make DNS work!
33 */ 34 */
34 35
35
36#include "platform.h" 36#include "platform.h"
37#include "gnunet_common.h" 37#include "gnunet_common.h"
38#include "gnunet_util_lib.h" 38#include "gnunet_util_lib.h"
@@ -40,35 +40,62 @@
40#include "gnunet_scheduler_lib.h" 40#include "gnunet_scheduler_lib.h"
41#include "gnunet_nat_lib.h" 41#include "gnunet_nat_lib.h"
42 42
43
44#define VERBOSE GNUNET_YES
45
46
43/* Time to wait before stopping NAT, in seconds */ 47/* Time to wait before stopping NAT, in seconds */
44#define TIMEOUT 60 48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45 49
46struct addr_cls
47{
48 struct sockaddr *addr;
49 socklen_t addrlen;
50};
51 50
51/**
52 * Function called on each address that the NAT service
53 * believes to be valid for the transport.
54 */
52static void 55static void
53addr_callback (void *cls, int add_remove, 56addr_callback (void *cls, int add_remove,
54 const struct sockaddr *addr, socklen_t addrlen) 57 const struct sockaddr *addr, socklen_t addrlen)
55{ 58{
56 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "External address changed: %s %s\n", 59 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
60 "Address changed: %s `%s' (%u bytes)\n",
57 add_remove == GNUNET_YES ? "added" : "removed", 61 add_remove == GNUNET_YES ? "added" : "removed",
58 GNUNET_a2s (addr, addrlen)); 62 GNUNET_a2s (addr, addrlen),
63 (unsigned int) addrlen);
59} 64}
60 65
66
67/**
68 * Function that terminates the test.
69 */
61static void 70static void
62stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 71stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63{ 72{
64 struct GNUNET_NAT_Handle *nat = cls; 73 struct GNUNET_NAT_Handle *nat = cls;
65 74
66 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); 75 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
76 "Stopping NAT and quitting...\n");
67 GNUNET_NAT_unregister (nat); 77 GNUNET_NAT_unregister (nat);
68} 78}
69 79
70/* Return the address of the default interface, 80
71 * or any interface with a valid address if the default is not valid */ 81struct addr_cls
82{
83 struct sockaddr *addr;
84 socklen_t addrlen;
85};
86
87
88/**
89 * Return the address of the default interface,
90 * or any interface with a valid address if the default is not valid
91 *
92 * @param cls the 'struct addr_cls'
93 * @param name name of the interface
94 * @param isDefault do we think this may be our default interface
95 * @param addr address of the interface
96 * @param addrlen number of bytes in addr
97 * @return GNUNET_OK to continue iterating
98 */
72static int 99static int
73process_if (void *cls, 100process_if (void *cls,
74 const char *name, 101 const char *name,
@@ -76,18 +103,21 @@ process_if (void *cls,
76{ 103{
77 struct addr_cls *data = cls; 104 struct addr_cls *data = cls;
78 105
79 if (addr && addrlen > 0) 106 if (addr == NULL)
80 { 107 return GNUNET_OK;
81 if (data->addr) 108 GNUNET_free_non_null (data->addr);
82 GNUNET_free (data->addr); 109 data->addr = GNUNET_malloc (addrlen);
83 data->addr = memcpy (GNUNET_malloc (addrlen), addr, addrlen); 110 memcpy (data->addr, addr, addrlen);
84 data->addrlen = addrlen; 111 data->addrlen = addrlen;
85 if (isDefault) 112 if (isDefault)
86 return GNUNET_SYSERR; 113 return GNUNET_SYSERR;
87 }
88 return GNUNET_OK; 114 return GNUNET_OK;
89} 115}
90 116
117
118/**
119 * Main function run with scheduler.
120 */
91static void 121static void
92run (void *cls, 122run (void *cls,
93 char *const *args, 123 char *const *args,
@@ -98,19 +128,15 @@ run (void *cls,
98 struct sockaddr *addr; 128 struct sockaddr *addr;
99 129
100 GNUNET_log_setup ("test-nat", "DEBUG", NULL); 130 GNUNET_log_setup ("test-nat", "DEBUG", NULL);
101
102 data.addr = NULL; 131 data.addr = NULL;
103 GNUNET_OS_network_interfaces_list (process_if, &data); 132 GNUNET_OS_network_interfaces_list (process_if, &data);
104 if (!data.addr) 133 if (NULL == data.addr)
105 { 134 {
106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107 "Could not find a valid interface address!\n"); 136 "Could not find a valid interface address!\n");
108 exit (GNUNET_SYSERR); 137 exit (GNUNET_SYSERR);
109 } 138 }
110 139 addr = data.addr;
111 addr = GNUNET_malloc (data.addrlen);
112 memcpy (addr, data.addr, data.addrlen);
113
114 GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6); 140 GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
115 if (addr->sa_family == AF_INET) 141 if (addr->sa_family == AF_INET)
116 ((struct sockaddr_in *) addr)->sin_port = htons (2086); 142 ((struct sockaddr_in *) addr)->sin_port = htons (2086);
@@ -121,14 +147,18 @@ run (void *cls,
121 "Requesting NAT redirection from address %s...\n", 147 "Requesting NAT redirection from address %s...\n",
122 GNUNET_a2s (addr, data.addrlen)); 148 GNUNET_a2s (addr, data.addrlen));
123 149
124 nat = GNUNET_NAT_register (cfg, addr, data.addrlen, addr_callback, NULL); 150 nat = GNUNET_NAT_register (cfg,
151 GNUNET_YES /* tcp */,
152 2086,
153 1,
154 (const struct sockaddr**) &addr,
155 &data.addrlen,
156 &addr_callback, NULL, NULL);
125 GNUNET_free (addr); 157 GNUNET_free (addr);
126 158 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
127 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
128 (GNUNET_TIME_UNIT_SECONDS, TIMEOUT), stop,
129 nat);
130} 159}
131 160
161
132int 162int
133main (int argc, char *const argv[]) 163main (int argc, char *const argv[])
134{ 164{
@@ -139,7 +169,7 @@ main (int argc, char *const argv[])
139 char *const argv_prog[] = { 169 char *const argv_prog[] = {
140 "test-nat", 170 "test-nat",
141 "-c", 171 "-c",
142 "test-nat.conf", 172 "test_nat_data.conf",
143 "-L", 173 "-L",
144#if VERBOSE 174#if VERBOSE
145 "DEBUG", 175 "DEBUG",