diff options
Diffstat (limited to 'src/nat/test_stun.c')
-rw-r--r-- | src/nat/test_stun.c | 278 |
1 files changed, 139 insertions, 139 deletions
diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c index ee2db35e5..ee755905e 100644 --- a/src/nat/test_stun.c +++ b/src/nat/test_stun.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * Testcase for STUN server resolution | 22 | * Testcase for STUN server resolution |
@@ -36,12 +36,12 @@ | |||
36 | 36 | ||
37 | 37 | ||
38 | 38 | ||
39 | #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__) | 39 | #define LOG(kind, ...) GNUNET_log_from(kind, "test-stun", __VA_ARGS__) |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * Time to wait before stopping NAT, in seconds | 42 | * Time to wait before stopping NAT, in seconds |
43 | */ | 43 | */ |
44 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | 44 | #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) |
45 | 45 | ||
46 | 46 | ||
47 | /** | 47 | /** |
@@ -74,10 +74,10 @@ static struct GNUNET_NAT_STUN_Handle *rh; | |||
74 | static void | 74 | static void |
75 | print_answer(struct sockaddr_in* answer) | 75 | print_answer(struct sockaddr_in* answer) |
76 | { | 76 | { |
77 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 77 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
78 | "External IP is: %s , with port %d\n", | 78 | "External IP is: %s , with port %d\n", |
79 | inet_ntoa (answer->sin_addr), | 79 | inet_ntoa(answer->sin_addr), |
80 | ntohs (answer->sin_port)); | 80 | ntohs(answer->sin_port)); |
81 | } | 81 | } |
82 | 82 | ||
83 | 83 | ||
@@ -85,25 +85,25 @@ print_answer(struct sockaddr_in* answer) | |||
85 | * Function that terminates the test. | 85 | * Function that terminates the test. |
86 | */ | 86 | */ |
87 | static void | 87 | static void |
88 | stop () | 88 | stop() |
89 | { | 89 | { |
90 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 90 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
91 | "Stopping NAT and quitting...\n"); | 91 | "Stopping NAT and quitting...\n"); |
92 | if (NULL != ltask4) | 92 | if (NULL != ltask4) |
93 | { | 93 | { |
94 | GNUNET_SCHEDULER_cancel (ltask4); | 94 | GNUNET_SCHEDULER_cancel(ltask4); |
95 | ltask4 = NULL; | 95 | ltask4 = NULL; |
96 | } | 96 | } |
97 | if(NULL != lsock4) | 97 | if (NULL != lsock4) |
98 | { | 98 | { |
99 | GNUNET_NETWORK_socket_close(lsock4); | 99 | GNUNET_NETWORK_socket_close(lsock4); |
100 | lsock4 = NULL; | 100 | lsock4 = NULL; |
101 | } | 101 | } |
102 | if (NULL != rh) | 102 | if (NULL != rh) |
103 | { | 103 | { |
104 | GNUNET_NAT_stun_make_request_cancel (rh); | 104 | GNUNET_NAT_stun_make_request_cancel(rh); |
105 | rh = NULL; | 105 | rh = NULL; |
106 | } | 106 | } |
107 | } | 107 | } |
108 | 108 | ||
109 | 109 | ||
@@ -114,7 +114,7 @@ stop () | |||
114 | * @param cls | 114 | * @param cls |
115 | */ | 115 | */ |
116 | static void | 116 | static void |
117 | do_udp_read (void *cls) | 117 | do_udp_read(void *cls) |
118 | { | 118 | { |
119 | //struct GNUNET_NAT_Test *tst = cls; | 119 | //struct GNUNET_NAT_Test *tst = cls; |
120 | unsigned char reply_buf[1024]; | 120 | unsigned char reply_buf[1024]; |
@@ -123,36 +123,36 @@ do_udp_read (void *cls) | |||
123 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 123 | const struct GNUNET_SCHEDULER_TaskContext *tc; |
124 | 124 | ||
125 | ltask4 = NULL; | 125 | ltask4 = NULL; |
126 | tc = GNUNET_SCHEDULER_get_task_context (); | 126 | tc = GNUNET_SCHEDULER_get_task_context(); |
127 | if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) || | 127 | if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) || |
128 | (! GNUNET_NETWORK_fdset_isset (tc->read_ready, | 128 | (!GNUNET_NETWORK_fdset_isset(tc->read_ready, |
129 | lsock4)) ) | 129 | lsock4))) |
130 | { | 130 | { |
131 | fprintf (stderr, | 131 | fprintf(stderr, |
132 | "Timeout waiting for STUN response\n"); | 132 | "Timeout waiting for STUN response\n"); |
133 | stop(); | 133 | stop(); |
134 | } | 134 | } |
135 | rlen = GNUNET_NETWORK_socket_recv (lsock4, | 135 | rlen = GNUNET_NETWORK_socket_recv(lsock4, |
136 | reply_buf, | 136 | reply_buf, |
137 | sizeof (reply_buf)); | 137 | sizeof(reply_buf)); |
138 | memset (&answer, | 138 | memset(&answer, |
139 | 0, | 139 | 0, |
140 | sizeof(struct sockaddr_in)); | 140 | sizeof(struct sockaddr_in)); |
141 | if (GNUNET_OK != | 141 | if (GNUNET_OK != |
142 | GNUNET_NAT_stun_handle_packet (reply_buf, | 142 | GNUNET_NAT_stun_handle_packet(reply_buf, |
143 | rlen, | 143 | rlen, |
144 | &answer)) | 144 | &answer)) |
145 | { | 145 | { |
146 | fprintf (stderr, | 146 | fprintf(stderr, |
147 | "Unexpected UDP packet, trying to read more\n"); | 147 | "Unexpected UDP packet, trying to read more\n"); |
148 | ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, | 148 | ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT, |
149 | lsock4, | 149 | lsock4, |
150 | &do_udp_read, NULL); | 150 | &do_udp_read, NULL); |
151 | return; | 151 | return; |
152 | } | 152 | } |
153 | ret = 0; | 153 | ret = 0; |
154 | print_answer (&answer); | 154 | print_answer(&answer); |
155 | stop (); | 155 | stop(); |
156 | } | 156 | } |
157 | 157 | ||
158 | 158 | ||
@@ -162,33 +162,33 @@ do_udp_read (void *cls) | |||
162 | * @return NULL on error | 162 | * @return NULL on error |
163 | */ | 163 | */ |
164 | static struct GNUNET_NETWORK_Handle * | 164 | static struct GNUNET_NETWORK_Handle * |
165 | bind_v4 () | 165 | bind_v4() |
166 | { | 166 | { |
167 | struct GNUNET_NETWORK_Handle *ls; | 167 | struct GNUNET_NETWORK_Handle *ls; |
168 | struct sockaddr_in sa4; | 168 | struct sockaddr_in sa4; |
169 | int eno; | 169 | int eno; |
170 | 170 | ||
171 | memset (&sa4, 0, sizeof (sa4)); | 171 | memset(&sa4, 0, sizeof(sa4)); |
172 | sa4.sin_family = AF_INET; | 172 | sa4.sin_family = AF_INET; |
173 | sa4.sin_port = htons (port); | 173 | sa4.sin_port = htons(port); |
174 | #if HAVE_SOCKADDR_IN_SIN_LEN | 174 | #if HAVE_SOCKADDR_IN_SIN_LEN |
175 | sa4.sin_len = sizeof (sa4); | 175 | sa4.sin_len = sizeof(sa4); |
176 | #endif | 176 | #endif |
177 | ls = GNUNET_NETWORK_socket_create (AF_INET, | 177 | ls = GNUNET_NETWORK_socket_create(AF_INET, |
178 | SOCK_DGRAM, | 178 | SOCK_DGRAM, |
179 | 0); | 179 | 0); |
180 | if (NULL == ls) | 180 | if (NULL == ls) |
181 | return NULL; | 181 | return NULL; |
182 | if (GNUNET_OK != | 182 | if (GNUNET_OK != |
183 | GNUNET_NETWORK_socket_bind (ls, | 183 | GNUNET_NETWORK_socket_bind(ls, |
184 | (const struct sockaddr *) &sa4, | 184 | (const struct sockaddr *)&sa4, |
185 | sizeof (sa4))) | 185 | sizeof(sa4))) |
186 | { | 186 | { |
187 | eno = errno; | 187 | eno = errno; |
188 | GNUNET_NETWORK_socket_close (ls); | 188 | GNUNET_NETWORK_socket_close(ls); |
189 | errno = eno; | 189 | errno = eno; |
190 | return NULL; | 190 | return NULL; |
191 | } | 191 | } |
192 | return ls; | 192 | return ls; |
193 | } | 193 | } |
194 | 194 | ||
@@ -200,29 +200,29 @@ bind_v4 () | |||
200 | * @param error status code from STUN | 200 | * @param error status code from STUN |
201 | */ | 201 | */ |
202 | static void | 202 | static void |
203 | request_callback (void *cls, | 203 | request_callback(void *cls, |
204 | enum GNUNET_NAT_StatusCode error) | 204 | enum GNUNET_NAT_StatusCode error) |
205 | { | 205 | { |
206 | rh = NULL; | 206 | rh = NULL; |
207 | if (GNUNET_NAT_ERROR_SUCCESS == error) | 207 | if (GNUNET_NAT_ERROR_SUCCESS == error) |
208 | { | 208 | { |
209 | /* all good, start to receive */ | 209 | /* all good, start to receive */ |
210 | ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, | 210 | ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT, |
211 | lsock4, | 211 | lsock4, |
212 | &do_udp_read, | 212 | &do_udp_read, |
213 | NULL); | 213 | NULL); |
214 | return; | 214 | return; |
215 | } | 215 | } |
216 | if (error == GNUNET_NAT_ERROR_NOT_ONLINE) | 216 | if (error == GNUNET_NAT_ERROR_NOT_ONLINE) |
217 | { | 217 | { |
218 | ret = 77; /* report 'skip' */ | 218 | ret = 77; /* report 'skip' */ |
219 | fprintf (stderr, | 219 | fprintf(stderr, |
220 | "System is offline, cannot test STUN request.\n"); | 220 | "System is offline, cannot test STUN request.\n"); |
221 | } | 221 | } |
222 | else | 222 | else |
223 | { | 223 | { |
224 | ret = error; | 224 | ret = error; |
225 | } | 225 | } |
226 | stop(); | 226 | stop(); |
227 | } | 227 | } |
228 | 228 | ||
@@ -231,81 +231,81 @@ request_callback (void *cls, | |||
231 | * Main function run with scheduler. | 231 | * Main function run with scheduler. |
232 | */ | 232 | */ |
233 | static void | 233 | static void |
234 | run (void *cls, | 234 | run(void *cls, |
235 | char *const *args, | 235 | char *const *args, |
236 | const char *cfgfile, | 236 | const char *cfgfile, |
237 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 237 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
238 | { | 238 | { |
239 | //Lets create the socket | 239 | //Lets create the socket |
240 | lsock4 = bind_v4 (); | 240 | lsock4 = bind_v4(); |
241 | if (NULL == lsock4) | 241 | if (NULL == lsock4) |
242 | { | 242 | { |
243 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | 243 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, |
244 | "bind"); | 244 | "bind"); |
245 | GNUNET_SCHEDULER_shutdown (); | 245 | GNUNET_SCHEDULER_shutdown(); |
246 | return; | 246 | return; |
247 | } | 247 | } |
248 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 248 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
249 | "Service listens on port %u\n", | 249 | "Service listens on port %u\n", |
250 | (unsigned int) port); | 250 | (unsigned int)port); |
251 | rh = GNUNET_NAT_stun_make_request (stun_server, | 251 | rh = GNUNET_NAT_stun_make_request(stun_server, |
252 | stun_port, | 252 | stun_port, |
253 | lsock4, | 253 | lsock4, |
254 | &request_callback, NULL); | 254 | &request_callback, NULL); |
255 | GNUNET_SCHEDULER_add_delayed (TIMEOUT, | 255 | GNUNET_SCHEDULER_add_delayed(TIMEOUT, |
256 | &stop, NULL); | 256 | &stop, NULL); |
257 | } | 257 | } |
258 | 258 | ||
259 | 259 | ||
260 | int | 260 | int |
261 | main (int argc, char *const argv[]) | 261 | main(int argc, char *const argv[]) |
262 | { | 262 | { |
263 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 263 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
264 | GNUNET_GETOPT_OPTION_END | 264 | GNUNET_GETOPT_OPTION_END |
265 | }; | 265 | }; |
266 | char *const argv_prog[] = { | 266 | char *const argv_prog[] = { |
267 | "test-stun", | 267 | "test-stun", |
268 | "-c", | 268 | "-c", |
269 | "test_stun.conf", | 269 | "test_stun.conf", |
270 | NULL | 270 | NULL |
271 | }; | 271 | }; |
272 | char *fn; | 272 | char *fn; |
273 | struct GNUNET_OS_Process *proc; | 273 | struct GNUNET_OS_Process *proc; |
274 | 274 | ||
275 | GNUNET_log_setup ("test-stun", | 275 | GNUNET_log_setup("test-stun", |
276 | "WARNING", | 276 | "WARNING", |
277 | NULL); | 277 | NULL); |
278 | 278 | ||
279 | /* Lets start resolver */ | 279 | /* Lets start resolver */ |
280 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | 280 | fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); |
281 | proc = GNUNET_OS_start_process (GNUNET_YES, | 281 | proc = GNUNET_OS_start_process(GNUNET_YES, |
282 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | 282 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, |
283 | NULL, NULL, NULL, | 283 | NULL, NULL, NULL, |
284 | fn, | 284 | fn, |
285 | "gnunet-service-resolver", | 285 | "gnunet-service-resolver", |
286 | "-c", "test_stun.conf", NULL); | 286 | "-c", "test_stun.conf", NULL); |
287 | 287 | ||
288 | if (NULL == proc) | 288 | if (NULL == proc) |
289 | { | 289 | { |
290 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 290 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
291 | "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); | 291 | "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); |
292 | exit(1); | 292 | exit(1); |
293 | } | 293 | } |
294 | 294 | ||
295 | GNUNET_PROGRAM_run (3, argv_prog, | 295 | GNUNET_PROGRAM_run(3, argv_prog, |
296 | "test-stun", "nohelp", | 296 | "test-stun", "nohelp", |
297 | options, | 297 | options, |
298 | &run, NULL); | 298 | &run, NULL); |
299 | 299 | ||
300 | /* Now kill the resolver */ | 300 | /* Now kill the resolver */ |
301 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | 301 | if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) |
302 | { | 302 | { |
303 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | 303 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); |
304 | } | 304 | } |
305 | GNUNET_OS_process_wait (proc); | 305 | GNUNET_OS_process_wait(proc); |
306 | GNUNET_OS_process_destroy (proc); | 306 | GNUNET_OS_process_destroy(proc); |
307 | proc = NULL; | 307 | proc = NULL; |
308 | GNUNET_free (fn); | 308 | GNUNET_free(fn); |
309 | 309 | ||
310 | return ret; | 310 | return ret; |
311 | } | 311 | } |