diff options
author | Bruno Cabral <bcabral@uw.edu> | 2015-08-28 05:47:13 +0000 |
---|---|---|
committer | Bruno Cabral <bcabral@uw.edu> | 2015-08-28 05:47:13 +0000 |
commit | b4fa14499c64140273850569247abda687803053 (patch) | |
tree | 63637c01742f5c8bf21fa702ec5f910de70fd907 /src/nat | |
parent | 6e1bde4fb5a4c4c8a88fa2470b69e95e537f6ab8 (diff) | |
download | gnunet-b4fa14499c64140273850569247abda687803053.tar.gz gnunet-b4fa14499c64140273850569247abda687803053.zip |
Fix review,
- Many fixes.
- Do not mark test as failed if no connection
PATCH 2/3
Diffstat (limited to 'src/nat')
-rw-r--r-- | src/nat/nat_auto.c | 2 | ||||
-rw-r--r-- | src/nat/nat_stun.c | 14 | ||||
-rw-r--r-- | src/nat/test_stun.c | 228 | ||||
-rw-r--r-- | src/nat/test_stun.conf | 2 |
4 files changed, 145 insertions, 101 deletions
diff --git a/src/nat/nat_auto.c b/src/nat/nat_auto.c index dc7eecb3e..4701d744e 100644 --- a/src/nat/nat_auto.c +++ b/src/nat/nat_auto.c | |||
@@ -243,7 +243,7 @@ stop_stun () | |||
243 | GNUNET_SCHEDULER_cancel (ltask4); | 243 | GNUNET_SCHEDULER_cancel (ltask4); |
244 | 244 | ||
245 | //Clean socket | 245 | //Clean socket |
246 | if(NULL != ltask4) | 246 | if(NULL != lsock4) |
247 | GNUNET_NETWORK_socket_close (lsock4); | 247 | GNUNET_NETWORK_socket_close (lsock4); |
248 | 248 | ||
249 | } | 249 | } |
diff --git a/src/nat/nat_stun.c b/src/nat/nat_stun.c index 57b25e20a..7b41b3387 100644 --- a/src/nat/nat_stun.c +++ b/src/nat/nat_stun.c | |||
@@ -82,7 +82,7 @@ struct GNUNET_NAT_STUN_Handle { | |||
82 | /** | 82 | /** |
83 | * Function to call when a error occours | 83 | * Function to call when a error occours |
84 | */ | 84 | */ |
85 | GNUNET_NAT_stun_RequestCallback cb; | 85 | GNUNET_NAT_STUN_ErrorCallback cb; |
86 | 86 | ||
87 | /** | 87 | /** |
88 | * Closure for @e cb. | 88 | * Closure for @e cb. |
@@ -441,7 +441,10 @@ GNUNET_NAT_stun_handle_packet(const void *data, size_t len, struct sockaddr_in * | |||
441 | static void | 441 | static void |
442 | clean(struct GNUNET_NAT_STUN_Handle * handle) | 442 | clean(struct GNUNET_NAT_STUN_Handle * handle) |
443 | { | 443 | { |
444 | if(handle->stun_server) | ||
445 | { | ||
444 | GNUNET_free(handle->stun_server); | 446 | GNUNET_free(handle->stun_server); |
447 | } | ||
445 | GNUNET_free(handle); | 448 | GNUNET_free(handle); |
446 | 449 | ||
447 | } | 450 | } |
@@ -485,7 +488,7 @@ stun_dns_callback (void *cls, | |||
485 | 488 | ||
486 | if( GNUNET_NO == request->dns_success){ | 489 | if( GNUNET_NO == request->dns_success){ |
487 | LOG (GNUNET_ERROR_TYPE_INFO, "Error resolving host %s\n", request->stun_server); | 490 | LOG (GNUNET_ERROR_TYPE_INFO, "Error resolving host %s\n", request->stun_server); |
488 | request->cb(request->cb_cls, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); | 491 | request->cb(request->cb_cls, GNUNET_NAT_ERROR_NOT_ONLINE); |
489 | clean(request); | 492 | clean(request); |
490 | 493 | ||
491 | } | 494 | } |
@@ -536,11 +539,14 @@ stun_dns_callback (void *cls, | |||
536 | * @param server, the address of the stun server | 539 | * @param server, the address of the stun server |
537 | * @param port, port of the stun server | 540 | * @param port, port of the stun server |
538 | * @param sock the socket used to send the request | 541 | * @param sock the socket used to send the request |
542 | * @param cb, callback in case of error | ||
539 | * @return #GNUNET_OK success, #GNUNET_NO on error. | 543 | * @return #GNUNET_OK success, #GNUNET_NO on error. |
540 | */ | 544 | */ |
541 | int | 545 | int |
542 | GNUNET_NAT_stun_make_request(char * server, int port, | 546 | GNUNET_NAT_stun_make_request(char * server, |
543 | struct GNUNET_NETWORK_Handle * sock,GNUNET_NAT_stun_RequestCallback cb, | 547 | int port, |
548 | struct GNUNET_NETWORK_Handle * sock, | ||
549 | GNUNET_NAT_STUN_ErrorCallback cb, | ||
544 | void *cb_cls) | 550 | void *cb_cls) |
545 | { | 551 | { |
546 | 552 | ||
diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c index f14661969..edb7a89eb 100644 --- a/src/nat/test_stun.c +++ b/src/nat/test_stun.c | |||
@@ -75,6 +75,32 @@ print_answer(struct sockaddr_in* answer) | |||
75 | } | 75 | } |
76 | 76 | ||
77 | 77 | ||
78 | |||
79 | /** | ||
80 | * Function that terminates the test. | ||
81 | */ | ||
82 | static void | ||
83 | stop () | ||
84 | { | ||
85 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); | ||
86 | |||
87 | //Clean task | ||
88 | if(NULL != ltask4) | ||
89 | { | ||
90 | GNUNET_SCHEDULER_cancel (ltask4); | ||
91 | ltask4 = NULL; | ||
92 | } | ||
93 | |||
94 | //Clean socket | ||
95 | if(NULL != lsock4) | ||
96 | { | ||
97 | GNUNET_NETWORK_socket_close(lsock4); | ||
98 | lsock4 = NULL; | ||
99 | } | ||
100 | |||
101 | } | ||
102 | |||
103 | |||
78 | /** | 104 | /** |
79 | * Activity on our incoming socket. Read data from the | 105 | * Activity on our incoming socket. Read data from the |
80 | * incoming connection. | 106 | * incoming connection. |
@@ -91,7 +117,7 @@ do_udp_read (void *cls, | |||
91 | ssize_t rlen; | 117 | ssize_t rlen; |
92 | struct sockaddr_in answer; | 118 | struct sockaddr_in answer; |
93 | 119 | ||
94 | if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && | 120 | if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && |
95 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, | 121 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, |
96 | lsock4))) | 122 | lsock4))) |
97 | { | 123 | { |
@@ -100,16 +126,34 @@ do_udp_read (void *cls, | |||
100 | 126 | ||
101 | //Lets handle the packet | 127 | //Lets handle the packet |
102 | memset(&answer, 0, sizeof(struct sockaddr_in)); | 128 | memset(&answer, 0, sizeof(struct sockaddr_in)); |
103 | GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer); | ||
104 | 129 | ||
105 | //Print the answer | 130 | if(GNUNET_OK == GNUNET_NAT_stun_handle_packet(reply_buf, rlen, &answer)) |
106 | ret = 0; | 131 | { |
107 | print_answer(&answer); | 132 | //Print the answer |
133 | ret = 0; | ||
134 | print_answer(&answer); | ||
135 | |||
136 | //Destroy the connection | ||
137 | GNUNET_NETWORK_socket_close(lsock4); | ||
138 | lsock4 = NULL; | ||
139 | |||
140 | } | ||
141 | else | ||
142 | { | ||
143 | //Lets try again, its a invalid message | ||
144 | ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, | ||
145 | lsock4, &do_udp_read, NULL); | ||
146 | } | ||
108 | 147 | ||
109 | //Destroy the connection | ||
110 | GNUNET_NETWORK_socket_close(lsock4); | ||
111 | |||
112 | } | 148 | } |
149 | else | ||
150 | { | ||
151 | //We got a timeout | ||
152 | ltask4 = NULL; | ||
153 | stop(); | ||
154 | } | ||
155 | |||
156 | ltask4 = NULL; | ||
113 | 157 | ||
114 | 158 | ||
115 | } | 159 | } |
@@ -150,30 +194,24 @@ bind_v4 () | |||
150 | return ls; | 194 | return ls; |
151 | } | 195 | } |
152 | 196 | ||
153 | /** | ||
154 | * Function that terminates the test. | ||
155 | */ | ||
156 | static void | ||
157 | stop () | ||
158 | { | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); | ||
160 | |||
161 | //Clean task | ||
162 | if(NULL != ltask4) | ||
163 | GNUNET_SCHEDULER_cancel (ltask4); | ||
164 | |||
165 | //Clean socket | ||
166 | if(NULL != ltask4) | ||
167 | GNUNET_NETWORK_socket_close (lsock4); | ||
168 | |||
169 | } | ||
170 | 197 | ||
171 | 198 | ||
172 | static void request_callback(void *cls, | 199 | static void request_callback(void *cls, |
173 | enum GNUNET_NAT_StatusCode result) | 200 | enum GNUNET_NAT_StatusCode error) |
174 | { | 201 | { |
175 | ret = result; | 202 | if(error == GNUNET_NAT_ERROR_NOT_ONLINE) |
176 | stop(); | 203 | { |
204 | //If we are not online, mark the test as success | ||
205 | ret = 0; | ||
206 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
207 | "test-stun detected as offline, cant make STUN request.\n" | ||
208 | ); | ||
209 | } | ||
210 | else | ||
211 | { | ||
212 | ret = error; | ||
213 | } | ||
214 | stop(); | ||
177 | }; | 215 | }; |
178 | 216 | ||
179 | 217 | ||
@@ -185,30 +223,29 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
185 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 223 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
186 | { | 224 | { |
187 | 225 | ||
188 | 226 | //Lets create the socket | |
189 | //Lets create the socket | 227 | lsock4 = bind_v4 (); |
190 | lsock4 = bind_v4 (); | 228 | ltask4 = NULL; |
191 | if (NULL == lsock4) | 229 | if (NULL == lsock4) |
192 | { | 230 | { |
193 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); | 231 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); |
194 | GNUNET_SCHEDULER_shutdown (); | 232 | GNUNET_SCHEDULER_shutdown (); |
195 | return; | 233 | return; |
196 | } | 234 | } |
197 | else | 235 | else |
198 | { | 236 | { |
199 | //Lets call our function now when it accepts | 237 | //Lets call our function now when it accepts |
200 | ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 238 | ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, |
201 | lsock4, &do_udp_read, NULL); | 239 | lsock4, &do_udp_read, NULL ); |
202 | /* So you read once and what will happen if you get an irregular message? Repeat and add timeout */ | 240 | |
203 | 241 | } | |
204 | } | 242 | |
205 | 243 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | |
206 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 244 | "Service listens on port %u\n", |
207 | "Service listens on port %u\n", | 245 | port); |
208 | port); | 246 | GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4, &request_callback, NULL); |
209 | GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4, &request_callback, NULL); | 247 | |
210 | 248 | GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL); | |
211 | //GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL); | ||
212 | 249 | ||
213 | } | 250 | } |
214 | 251 | ||
@@ -216,51 +253,52 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
216 | int | 253 | int |
217 | main (int argc, char *const argv[]) | 254 | main (int argc, char *const argv[]) |
218 | { | 255 | { |
219 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 256 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
220 | GNUNET_GETOPT_OPTION_END | 257 | GNUNET_GETOPT_OPTION_END |
221 | }; | 258 | }; |
222 | 259 | ||
223 | char *const argv_prog[] = { | 260 | char *const argv_prog[] = { |
224 | "test-stun", | 261 | "test-stun", |
225 | "-c", | 262 | "-c", |
226 | "test_stun.conf", | 263 | "test_stun.conf", |
227 | NULL | 264 | NULL |
228 | }; | 265 | }; |
229 | GNUNET_log_setup ("test-stun", | 266 | GNUNET_log_setup ("test-stun", |
230 | "WARNING", | 267 | "WARNING", |
231 | NULL); | 268 | NULL); |
232 | 269 | ||
233 | /* Lets start resolver */ | 270 | /* Lets start resolver */ |
234 | char *fn; | 271 | char *fn; |
235 | struct GNUNET_OS_Process *proc; | 272 | struct GNUNET_OS_Process *proc; |
236 | 273 | ||
237 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | 274 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); |
238 | proc = GNUNET_OS_start_process (GNUNET_YES, | 275 | proc = GNUNET_OS_start_process (GNUNET_YES, |
239 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | 276 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, |
240 | NULL, NULL, NULL, | 277 | NULL, NULL, NULL, |
241 | fn, | 278 | fn, |
242 | "gnunet-service-resolver", | 279 | "gnunet-service-resolver", |
243 | "-c", "test_stun.conf", NULL); | 280 | "-c", "test_stun.conf", NULL); |
244 | 281 | ||
245 | if (NULL != proc) | 282 | if (NULL == proc) |
246 | { | 283 | { |
247 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); | 284 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); |
248 | exit(1); | 285 | exit(1); |
249 | } | 286 | } |
287 | |||
288 | GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, NULL); | ||
289 | |||
290 | |||
291 | /* Now kill the resolver */ | ||
292 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
293 | { | ||
294 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
295 | } | ||
296 | GNUNET_OS_process_wait (proc); | ||
297 | GNUNET_OS_process_destroy (proc); | ||
298 | proc = NULL; | ||
299 | GNUNET_free (fn); | ||
250 | 300 | ||
251 | GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, NULL); | ||
252 | |||
253 | /* Now kill the resolver */ | ||
254 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
255 | { | ||
256 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
257 | } | ||
258 | GNUNET_OS_process_wait (proc); | ||
259 | GNUNET_OS_process_destroy (proc); | ||
260 | proc = NULL; | ||
261 | GNUNET_free (fn); | ||
262 | 301 | ||
263 | |||
264 | return ret; | 302 | return ret; |
265 | } | 303 | } |
266 | 304 | ||
diff --git a/src/nat/test_stun.conf b/src/nat/test_stun.conf index 42b36d66a..c8104f0a7 100644 --- a/src/nat/test_stun.conf +++ b/src/nat/test_stun.conf | |||
@@ -1,5 +1,5 @@ | |||
1 | [PATHS] | 1 | [PATHS] |
2 | GNUNET_TEST_HOME = /tmp/test-gnunetd-statistics/ | 2 | GNUNET_TEST_HOME = /tmp/test-stun |
3 | 3 | ||
4 | [resolver] | 4 | [resolver] |
5 | PORT = 22354 | 5 | PORT = 22354 |