diff options
Diffstat (limited to 'src/nat/test_stun.c')
-rw-r--r-- | src/nat/test_stun.c | 94 |
1 files changed, 75 insertions, 19 deletions
diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c index f0956d021..e75d55bd0 100644 --- a/src/nat/test_stun.c +++ b/src/nat/test_stun.c | |||
@@ -23,7 +23,7 @@ | |||
23 | * | 23 | * |
24 | * @file nat/test_stun.c | 24 | * @file nat/test_stun.c |
25 | * @brief Testcase for STUN library | 25 | * @brief Testcase for STUN library |
26 | * @author Bruno Souza Cabral - Major rewrite. | 26 | * @author Bruno Souza Cabral |
27 | 27 | ||
28 | * | 28 | * |
29 | */ | 29 | */ |
@@ -40,13 +40,19 @@ | |||
40 | #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__) | 40 | #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__) |
41 | 41 | ||
42 | /** | 42 | /** |
43 | * Time to wait before stopping NAT, in seconds | ||
44 | */ | ||
45 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
46 | |||
47 | |||
48 | /** | ||
43 | * The port the test service is running on (default 7895) | 49 | * The port the test service is running on (default 7895) |
44 | */ | 50 | */ |
45 | static unsigned long port = 7895; | 51 | static unsigned long port = 7895; |
46 | static int ret = 1; | 52 | static int ret = 1; |
47 | 53 | ||
48 | static char *stun_server = "stun.ekiga.net"; | 54 | static char *stun_server = "stun2.l.google.com"; |
49 | static int stun_port = 3478; | 55 | static int stun_port = 19302; |
50 | 56 | ||
51 | /** | 57 | /** |
52 | * The listen socket of the service for IPv4 | 58 | * The listen socket of the service for IPv4 |
@@ -85,23 +91,26 @@ do_udp_read (void *cls, | |||
85 | ssize_t rlen; | 91 | ssize_t rlen; |
86 | struct sockaddr_in answer; | 92 | struct sockaddr_in answer; |
87 | 93 | ||
94 | printf("UDP READ\n"); | ||
95 | |||
88 | 96 | ||
89 | if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && | 97 | if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && |
90 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, | 98 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, |
91 | lsock4))) | 99 | lsock4))) |
92 | { | 100 | { |
93 | rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf)); | 101 | rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf)); |
94 | 102 | ||
95 | 103 | ||
96 | //Lets handle the packet | 104 | //Lets handle the packet |
97 | memset(&answer, 0, sizeof(struct sockaddr_in)); | 105 | memset(&answer, 0, sizeof(struct sockaddr_in)); |
98 | GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer); | 106 | GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer); |
99 | 107 | ||
100 | //Print the answer | 108 | //Print the answer |
101 | //TODO: Delete the object | ||
102 | ret = 0; | 109 | ret = 0; |
103 | print_answer(&answer); | 110 | print_answer(&answer); |
104 | 111 | ||
112 | //Destroy the connection | ||
113 | GNUNET_NETWORK_socket_close(lsock4); | ||
105 | 114 | ||
106 | } | 115 | } |
107 | 116 | ||
@@ -144,13 +153,38 @@ bind_v4 () | |||
144 | return ls; | 153 | return ls; |
145 | } | 154 | } |
146 | 155 | ||
156 | /** | ||
157 | * Function that terminates the test. | ||
158 | */ | ||
159 | static void | ||
160 | stop () | ||
161 | { | ||
162 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); | ||
163 | |||
164 | printf("Stopped !!\n"); | ||
165 | //Clean task | ||
166 | if(NULL != ltask4) | ||
167 | GNUNET_SCHEDULER_cancel (ltask4); | ||
168 | |||
169 | //Clean socket | ||
170 | if(NULL != ltask4) | ||
171 | GNUNET_NETWORK_socket_close (lsock4); | ||
172 | |||
173 | } | ||
174 | |||
175 | |||
176 | static void request_callback(void *cls, | ||
177 | enum GNUNET_NAT_StatusCode result) | ||
178 | { | ||
179 | ret = result; | ||
180 | stop(); | ||
181 | printf("Called back\n"); | ||
182 | }; | ||
147 | 183 | ||
148 | 184 | ||
149 | /** | 185 | /** |
150 | * Main function run with scheduler. | 186 | * Main function run with scheduler. |
151 | */ | 187 | */ |
152 | |||
153 | |||
154 | static void | 188 | static void |
155 | run (void *cls, char *const *args, const char *cfgfile, | 189 | run (void *cls, char *const *args, const char *cfgfile, |
156 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 190 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
@@ -162,29 +196,26 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
162 | if (NULL == lsock4) | 196 | if (NULL == lsock4) |
163 | { | 197 | { |
164 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); | 198 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); |
199 | GNUNET_SCHEDULER_shutdown (); | ||
200 | return; | ||
165 | } | 201 | } |
166 | else | 202 | else |
167 | { | 203 | { |
168 | printf("Binded, now will call add_read\n"); | ||
169 | //Lets call our function now when it accepts | 204 | //Lets call our function now when it accepts |
170 | ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 205 | ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
171 | lsock4, &do_udp_read, NULL); | 206 | lsock4, &do_udp_read, NULL); |
172 | /* So you read once and what will happen if you get an irregular message? Repeat and add timeout */ | 207 | /* So you read once and what will happen if you get an irregular message? Repeat and add timeout */ |
173 | 208 | ||
174 | } | 209 | } |
175 | if(NULL == lsock4 ) | 210 | |
176 | { | ||
177 | /* FIXME: duplicate check */ | ||
178 | GNUNET_SCHEDULER_shutdown (); | ||
179 | return; | ||
180 | } | ||
181 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 211 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
182 | "Service listens on port %u\n", | 212 | "Service listens on port %u\n", |
183 | port); | 213 | port); |
184 | printf("Start main event\n"); | 214 | printf("Start main event\n"); |
185 | GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4); | 215 | GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4, &request_callback, NULL); |
186 | //Main event | 216 | printf("Made the requeest\n"); |
187 | //main_task = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh); | 217 | |
218 | //GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL); | ||
188 | 219 | ||
189 | } | 220 | } |
190 | 221 | ||
@@ -198,13 +229,38 @@ main (int argc, char *const argv[]) | |||
198 | 229 | ||
199 | char *const argv_prog[] = { | 230 | char *const argv_prog[] = { |
200 | "test-stun", | 231 | "test-stun", |
232 | "-c", | ||
233 | "test_stun.conf", | ||
201 | NULL | 234 | NULL |
202 | }; | 235 | }; |
203 | GNUNET_log_setup ("test-stun", | 236 | GNUNET_log_setup ("test-stun", |
204 | "WARNING", | 237 | "WARNING", |
205 | NULL); | 238 | NULL); |
206 | 239 | ||
207 | GNUNET_PROGRAM_run (1, argv_prog, "test-stun", "nohelp", options, &run, NULL); | 240 | /* Lets start resolver */ |
241 | char *fn; | ||
242 | struct GNUNET_OS_Process *proc; | ||
243 | |||
244 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
245 | proc = GNUNET_OS_start_process (GNUNET_YES, | ||
246 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
247 | NULL, NULL, NULL, | ||
248 | fn, | ||
249 | "gnunet-service-resolver", | ||
250 | "-c", "test_stun.conf", NULL); | ||
251 | GNUNET_assert (NULL != proc); | ||
252 | |||
253 | GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, NULL); | ||
254 | |||
255 | /* Now kill the resolver */ | ||
256 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
257 | { | ||
258 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
259 | } | ||
260 | GNUNET_OS_process_wait (proc); | ||
261 | GNUNET_OS_process_destroy (proc); | ||
262 | proc = NULL; | ||
263 | |||
208 | 264 | ||
209 | return ret; | 265 | return ret; |
210 | } | 266 | } |