aboutsummaryrefslogtreecommitdiff
path: root/src/nat/test_nat.c
diff options
context:
space:
mode:
authorMoon <moon@140774ce-b5e7-0310-ab8b-a85725594a96>2009-10-25 12:10:01 +0000
committerMoon <moon@140774ce-b5e7-0310-ab8b-a85725594a96>2009-10-25 12:10:01 +0000
commiteb3a6ed76cec19bc42d31a416fc9a87863cfe1ad (patch)
tree2344d46946e85b2b67ed9d7563f1ceaa7eb70612 /src/nat/test_nat.c
parent2107f7a34fad332eb3ff2f858cdf4c303bb48a34 (diff)
downloadgnunet-eb3a6ed76cec19bc42d31a416fc9a87863cfe1ad.tar.gz
gnunet-eb3a6ed76cec19bc42d31a416fc9a87863cfe1ad.zip
add missing test_net.c and fix bug about sockaddr* cast
Diffstat (limited to 'src/nat/test_nat.c')
-rw-r--r--src/nat/test_nat.c152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c
new file mode 100644
index 000000000..e535e7e50
--- /dev/null
+++ b/src/nat/test_nat.c
@@ -0,0 +1,152 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
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
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
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.
29 * 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
31 * routers found, 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.
33 */
34
35
36#include "platform.h"
37#include "gnunet_common.h"
38#include "gnunet_util_lib.h"
39#include "gnunet_program_lib.h"
40#include "gnunet_scheduler_lib.h"
41#include "gnunet_nat_lib.h"
42
43/* Time to wait before stopping NAT, in seconds */
44#define TIMEOUT 30
45
46struct addr_cls
47{
48 const struct sockaddr *addr;
49 socklen_t addrlen;
50};
51//typedef addr_cls addr_cls;
52
53static void
54addr_callback (void *cls, int add_remove,
55 const struct sockaddr *addr, socklen_t addrlen)
56{
57 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "External address changed: %s %s\n",
58 add_remove == GNUNET_YES ? "added" : "removed",
59 GNUNET_a2s (addr, addrlen));
60}
61
62static void
63stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64{
65 GNUNET_NAT_Handle *nat = cls;
66
67 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
68 GNUNET_NAT_unregister (nat);
69}
70
71/* Return the address of the default interface,
72 * or any interface with a valid address if the default is not valid */
73static int
74process_if (void *cls,
75 const char *name,
76 int isDefault,
77 const struct sockaddr *addr,
78 socklen_t addrlen)
79{
80 struct addr_cls *data = cls;
81
82 if (addr)
83 {
84 data->addr = addr;
85 data->addrlen = addrlen;
86 }
87
88 if (isDefault && addr)
89 return GNUNET_SYSERR;
90 else
91 return GNUNET_OK;
92}
93
94static void
95run (void *cls,
96 struct GNUNET_SCHEDULER_Handle *sched,
97 char *const *args,
98 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
99{
100 GNUNET_NAT_Handle *nat;
101 struct addr_cls data;
102 struct sockaddr *addr;
103
104 GNUNET_log_setup ("test-nat", "DEBUG", NULL);
105
106 data.addr = NULL;
107 GNUNET_OS_network_interfaces_list (process_if, &data);
108 if (!data.addr)
109 {
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find a valid interface address!\n");
111 exit (GNUNET_SYSERR);
112 }
113
114 addr = GNUNET_malloc (data.addrlen);
115 memcpy (addr, data.addr, data.addrlen);
116
117 GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
118 if (addr->sa_family == AF_INET)
119 ((struct sockaddr_in *) addr)->sin_port = htons (2086);
120 else
121 ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086);
122
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Requesting NAT redirection from address %s...\n", GNUNET_a2s (addr, data.addrlen));
124
125 nat = GNUNET_NAT_register (sched, addr, data.addrlen, addr_callback, NULL);
126 GNUNET_free (addr);
127
128 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_NO,
129 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
130 GNUNET_SCHEDULER_NO_TASK,
131 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, TIMEOUT),
132 stop, nat);
133}
134
135int
136main (int argc, const char *argv[])
137{
138 struct GNUNET_GETOPT_CommandLineOption options[] = {
139 GNUNET_GETOPT_OPTION_END
140 };
141
142 GNUNET_log_setup ("test-nat", "DEBUG", NULL);
143
144 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
145 "Testing NAT library, timeout set to %d seconds\n", TIMEOUT);
146
147 GNUNET_PROGRAM_run (argc, argv, "test-nat", "nohelp", options, &run, NULL);
148
149 return 0;
150}
151
152/* end of test_nat.c */