diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-11-27 12:07:07 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-11-27 12:07:07 +0100 |
commit | b035390c73b241ddb513f420671043e9113c237c (patch) | |
tree | 46dd1043c900eec2e0119df29fa8f5b804cd5258 /src/nat/gnunet-nat.c | |
parent | 82e765caeb53a1de54676738169dab98ca76c47e (diff) | |
download | gnunet-b035390c73b241ddb513f420671043e9113c237c.tar.gz gnunet-b035390c73b241ddb513f420671043e9113c237c.zip |
work on new gnunet-nat tool, towards new NAT API
Diffstat (limited to 'src/nat/gnunet-nat.c')
-rw-r--r-- | src/nat/gnunet-nat.c | 271 |
1 files changed, 213 insertions, 58 deletions
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c index fcd61a136..10d5aef8e 100644 --- a/src/nat/gnunet-nat.c +++ b/src/nat/gnunet-nat.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2015 GNUnet e.V. | 3 | Copyright (C) 2015, 2016 GNUnet e.V. |
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 |
@@ -26,13 +26,101 @@ | |||
26 | */ | 26 | */ |
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
29 | #include "gnunet_nat_lib.h" | 29 | #include "gnunet_nat_service.h" |
30 | #include "gnunet_protocols.h" | ||
31 | #include "nat.h" | ||
32 | 30 | ||
31 | /** | ||
32 | * Value to return from #main(). | ||
33 | */ | ||
34 | static int global_ret; | ||
35 | |||
36 | /** | ||
37 | * Handle to ongoing autoconfiguration. | ||
38 | */ | ||
39 | static struct GNUNET_NAT_AutoHandle *ah; | ||
40 | |||
41 | /** | ||
42 | * Port we use. | ||
43 | */ | ||
44 | static unsigned int port; | ||
45 | |||
46 | /** | ||
47 | * Flag set to 1 if we use IPPROTO_UDP. | ||
48 | */ | ||
49 | static int use_udp; | ||
50 | |||
51 | /** | ||
52 | * Flag set to 1 if we are to listen for connection reversal requests. | ||
53 | */ | ||
54 | static int listen_reversal; | ||
55 | |||
56 | /** | ||
57 | * Flag set to 1 if we use IPPROTO_TCP. | ||
58 | */ | ||
59 | static int use_tcp; | ||
60 | |||
61 | /** | ||
62 | * Protocol to use. | ||
63 | */ | ||
64 | static uint8_t proto; | ||
65 | |||
66 | /** | ||
67 | * Address we are bound to (in test), or should bind to | ||
68 | * (if #do_stun is set). | ||
69 | */ | ||
70 | static char *bind_addr; | ||
33 | 71 | ||
34 | struct GNUNET_CONFIGURATION_Handle *cfg; | 72 | /** |
73 | * External IP address and port to use for the test. | ||
74 | * If not set, use #bind_addr. | ||
75 | */ | ||
76 | static char *extern_addr; | ||
35 | 77 | ||
78 | /** | ||
79 | * Local address to use for connection reversal request. | ||
80 | */ | ||
81 | static char *local_addr; | ||
82 | |||
83 | /** | ||
84 | * Remote address to use for connection reversal request. | ||
85 | */ | ||
86 | static char *remote_addr; | ||
87 | |||
88 | /** | ||
89 | * Should we actually bind to #bind_addr and receive and process STUN requests? | ||
90 | */ | ||
91 | static unsigned int do_stun; | ||
92 | |||
93 | /** | ||
94 | * Should we run autoconfiguration? | ||
95 | */ | ||
96 | static unsigned int do_auto; | ||
97 | |||
98 | /** | ||
99 | * Handle to a NAT test operation. | ||
100 | */ | ||
101 | static struct GNUNET_NAT_Test *nt; | ||
102 | |||
103 | /** | ||
104 | * Handle to NAT operation. | ||
105 | */ | ||
106 | static struct GNUNET_NAT_Handle *nh; | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Test if all activities have finished, and if so, | ||
111 | * terminate. | ||
112 | */ | ||
113 | static void | ||
114 | test_finished () | ||
115 | { | ||
116 | if (NULL != ah) | ||
117 | return; | ||
118 | if (NULL != nt) | ||
119 | return; | ||
120 | if (NULL != nh) | ||
121 | return; | ||
122 | GNUNET_SCHEDULER_shutdown (); | ||
123 | } | ||
36 | 124 | ||
37 | 125 | ||
38 | /** | 126 | /** |
@@ -49,12 +137,12 @@ auto_conf_iter (void *cls, | |||
49 | const char *option, | 137 | const char *option, |
50 | const char *value) | 138 | const char *value) |
51 | { | 139 | { |
52 | 140 | PRINTF ("%s: %s\n", | |
53 | PRINTF ( "%s: %s \n", option, value); | 141 | option, |
142 | value); | ||
54 | } | 143 | } |
55 | 144 | ||
56 | 145 | ||
57 | |||
58 | /** | 146 | /** |
59 | * Function called with the result from the autoconfiguration. | 147 | * Function called with the result from the autoconfiguration. |
60 | * | 148 | * |
@@ -64,15 +152,16 @@ auto_conf_iter (void *cls, | |||
64 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code | 152 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code |
65 | * @param type what the situation of the NAT | 153 | * @param type what the situation of the NAT |
66 | */ | 154 | */ |
67 | 155 | static void | |
68 | void | 156 | auto_config_cb (void *cls, |
69 | auto_config_cb(void *cls, | 157 | const struct GNUNET_CONFIGURATION_Handle *diff, |
70 | const struct GNUNET_CONFIGURATION_Handle *diff, | 158 | enum GNUNET_NAT_StatusCode result, |
71 | enum GNUNET_NAT_StatusCode result, enum GNUNET_NAT_Type type) | 159 | enum GNUNET_NAT_Type type) |
72 | { | 160 | { |
73 | char* nat_type; | 161 | const char *nat_type; |
74 | char unknown_type[64]; | 162 | char unknown_type[64]; |
75 | 163 | ||
164 | ah = NULL; | ||
76 | switch (type) | 165 | switch (type) |
77 | { | 166 | { |
78 | case GNUNET_NAT_TYPE_NO_NAT: | 167 | case GNUNET_NAT_TYPE_NO_NAT: |
@@ -88,24 +177,50 @@ auto_config_cb(void *cls, | |||
88 | nat_type = "NAT but UPNP opened the ports"; | 177 | nat_type = "NAT but UPNP opened the ports"; |
89 | break; | 178 | break; |
90 | default: | 179 | default: |
91 | SPRINTF (unknown_type, "NAT unknown, type %u", type); | 180 | SPRINTF (unknown_type, |
181 | "NAT unknown, type %u", | ||
182 | type); | ||
92 | nat_type = unknown_type; | 183 | nat_type = unknown_type; |
93 | } | 184 | } |
94 | 185 | ||
95 | PRINTF ("NAT status: %s \n", nat_type ); | 186 | PRINTF ("NAT status: %s/%s\n", |
96 | PRINTF ("SUGGESTED CHANGES: \n" ); | 187 | GNUNET_NAT_status2string (result), |
97 | 188 | nat_type); | |
189 | |||
190 | PRINTF ("SUGGESTED CHANGES:\n"); | ||
98 | GNUNET_CONFIGURATION_iterate_section_values (diff, | 191 | GNUNET_CONFIGURATION_iterate_section_values (diff, |
99 | "nat", | 192 | "nat", |
100 | &auto_conf_iter, | 193 | &auto_conf_iter, |
101 | NULL); | 194 | NULL); |
102 | 195 | // Have option to save config | |
103 | //TODO: Save config | 196 | test_finished (); |
104 | |||
105 | } | 197 | } |
106 | 198 | ||
107 | 199 | ||
108 | 200 | /** | |
201 | * Task run on shutdown. | ||
202 | * | ||
203 | * @param cls NULL | ||
204 | */ | ||
205 | static void | ||
206 | do_shutdown (void *cls) | ||
207 | { | ||
208 | if (NULL != ah) | ||
209 | { | ||
210 | GNUNET_NAT_autoconfig_cancel (ah); | ||
211 | ah = NULL; | ||
212 | } | ||
213 | if (NULL != nt) | ||
214 | { | ||
215 | GNUNET_NAT_test_stop (nt); | ||
216 | nt = NULL; | ||
217 | } | ||
218 | if (NULL != nh) | ||
219 | { | ||
220 | GNUNET_NAT_unregister (nh); | ||
221 | nh = NULL; | ||
222 | } | ||
223 | } | ||
109 | 224 | ||
110 | 225 | ||
111 | /** | 226 | /** |
@@ -117,10 +232,39 @@ auto_config_cb(void *cls, | |||
117 | * @param c configuration | 232 | * @param c configuration |
118 | */ | 233 | */ |
119 | static void | 234 | static void |
120 | run (void *cls, char *const *args, const char *cfgfile, | 235 | run (void *cls, |
236 | char *const *args, | ||
237 | const char *cfgfile, | ||
121 | const struct GNUNET_CONFIGURATION_Handle *c) | 238 | const struct GNUNET_CONFIGURATION_Handle *c) |
122 | { | 239 | { |
123 | GNUNET_NAT_autoconfig_start (c, auto_config_cb, NULL); | 240 | if (use_tcp && use_udp) |
241 | { | ||
242 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
243 | "Cannot use TCP and UDP\n"); | ||
244 | global_ret = 1; | ||
245 | return; | ||
246 | } | ||
247 | proto = 0; | ||
248 | if (use_tcp) | ||
249 | proto = IPPROTO_TCP; | ||
250 | if (use_udp) | ||
251 | proto = IPPROTO_UDP; | ||
252 | if (0 == proto) | ||
253 | { | ||
254 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
255 | "Must specify either TCP or UDP\n"); | ||
256 | global_ret = 1; | ||
257 | return; | ||
258 | } | ||
259 | if (do_auto) | ||
260 | { | ||
261 | ah = GNUNET_NAT_autoconfig_start (c, | ||
262 | &auto_config_cb, | ||
263 | NULL); | ||
264 | } | ||
265 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
266 | NULL); | ||
267 | test_finished (); | ||
124 | } | 268 | } |
125 | 269 | ||
126 | 270 | ||
@@ -132,49 +276,60 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
132 | * @return 0 on success, -1 on error | 276 | * @return 0 on success, -1 on error |
133 | */ | 277 | */ |
134 | int | 278 | int |
135 | main (int argc, char *const argv[]) | 279 | main (int argc, |
280 | char *const argv[]) | ||
136 | { | 281 | { |
137 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 282 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
138 | GNUNET_GETOPT_OPTION_END | 283 | {'a', "auto", NULL, |
284 | gettext_noop ("run autoconfiguration"), | ||
285 | GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto }, | ||
286 | {'b', "bind", "ADDRESS", | ||
287 | gettext_noop ("which IP and port are we bound to"), | ||
288 | GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr}, | ||
289 | {'e', "external", "ADDRESS", | ||
290 | gettext_noop ("which external IP and port should be used to test"), | ||
291 | GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr}, | ||
292 | {'l', "local", "ADDRESS", | ||
293 | gettext_noop ("which IP and port are we locally using to listen to for connection reversals"), | ||
294 | GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr}, | ||
295 | {'r', "remote", "ADDRESS", | ||
296 | gettext_noop ("which remote IP and port should be asked for connection reversal"), | ||
297 | GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr}, | ||
298 | {'L', "listen", NULL, | ||
299 | gettext_noop ("listen for connection reversal requests"), | ||
300 | GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal }, | ||
301 | {'p', "port", NULL, | ||
302 | gettext_noop ("port to use"), | ||
303 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &port}, | ||
304 | {'s', "stun", NULL, | ||
305 | gettext_noop ("enable STUN processing"), | ||
306 | GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun }, | ||
307 | {'t', "tcp", NULL, | ||
308 | gettext_noop ("use TCP"), | ||
309 | GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp }, | ||
310 | {'u', "udp", NULL, | ||
311 | gettext_noop ("use UDP"), | ||
312 | GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp }, | ||
313 | GNUNET_GETOPT_OPTION_END | ||
139 | }; | 314 | }; |
140 | 315 | ||
141 | int ret = 0; | 316 | if (GNUNET_OK != |
142 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 317 | GNUNET_STRINGS_get_utf8_args (argc, argv, |
318 | &argc, &argv)) | ||
143 | return 2; | 319 | return 2; |
144 | |||
145 | /* Lets start resolver */ | ||
146 | char *fn; | ||
147 | struct GNUNET_OS_Process *proc; | ||
148 | |||
149 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
150 | proc = GNUNET_OS_start_process (GNUNET_YES, | ||
151 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
152 | NULL, NULL, NULL, | ||
153 | fn, | ||
154 | "gnunet-service-resolver"); | ||
155 | GNUNET_assert (NULL != proc); | ||
156 | |||
157 | if (GNUNET_OK != | 320 | if (GNUNET_OK != |
158 | GNUNET_PROGRAM_run (argc, argv, "gnunet-nat [options]", | 321 | GNUNET_PROGRAM_run (argc, argv, |
159 | _("GNUnet NAT traversal autoconfigure daemon"), options, | 322 | "gnunet-nat [options]", |
160 | &run, NULL)) | 323 | _("GNUnet NAT traversal autoconfigure daemon"), |
324 | options, | ||
325 | &run, | ||
326 | NULL)) | ||
161 | { | 327 | { |
162 | ret = 1; | 328 | global_ret = 1; |
163 | } | 329 | } |
164 | |||
165 | /* Now kill the resolver */ | ||
166 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
167 | { | ||
168 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
169 | } | ||
170 | GNUNET_OS_process_wait (proc); | ||
171 | GNUNET_OS_process_destroy (proc); | ||
172 | proc = NULL; | ||
173 | |||
174 | |||
175 | GNUNET_free ((void*) argv); | 330 | GNUNET_free ((void*) argv); |
176 | return ret; | 331 | return global_ret; |
177 | } | 332 | } |
178 | 333 | ||
179 | 334 | ||
180 | /* end of gnunet-nat-server.c */ | 335 | /* end of gnunet-nat.c */ |