diff options
Diffstat (limited to 'src/nat/gnunet-service-nat_helper.c')
-rw-r--r-- | src/nat/gnunet-service-nat_helper.c | 211 |
1 files changed, 86 insertions, 125 deletions
diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c index 43ac54adf..cf5038dd7 100644 --- a/src/nat/gnunet-service-nat_helper.c +++ b/src/nat/gnunet-service-nat_helper.c | |||
@@ -101,12 +101,10 @@ static void | |||
101 | try_again (struct HelperContext *h) | 101 | try_again (struct HelperContext *h) |
102 | { | 102 | { |
103 | GNUNET_assert (NULL == h->server_read_task); | 103 | GNUNET_assert (NULL == h->server_read_task); |
104 | h->server_retry_delay | 104 | h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay); |
105 | = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay); | 105 | h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay, |
106 | h->server_read_task | 106 | &restart_nat_server, |
107 | = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay, | 107 | h); |
108 | &restart_nat_server, | ||
109 | h); | ||
110 | } | 108 | } |
111 | 109 | ||
112 | 110 | ||
@@ -128,23 +126,16 @@ nat_server_read (void *cls) | |||
128 | struct sockaddr_in sin_addr; | 126 | struct sockaddr_in sin_addr; |
129 | 127 | ||
130 | h->server_read_task = NULL; | 128 | h->server_read_task = NULL; |
131 | memset (mybuf, | 129 | memset (mybuf, 0, sizeof (mybuf)); |
132 | 0, | 130 | bytes = |
133 | sizeof (mybuf)); | 131 | GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf)); |
134 | bytes | ||
135 | = GNUNET_DISK_file_read (h->server_stdout_handle, | ||
136 | mybuf, | ||
137 | sizeof (mybuf)); | ||
138 | if (bytes < 1) | 132 | if (bytes < 1) |
139 | { | 133 | { |
140 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 134 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
141 | "Finished reading from server stdout with code: %d\n", | 135 | "Finished reading from server stdout with code: %d\n", |
142 | (int) bytes); | 136 | (int) bytes); |
143 | if (0 != GNUNET_OS_process_kill (h->server_proc, | 137 | if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) |
144 | GNUNET_TERM_SIG)) | 138 | GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill"); |
145 | GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
146 | "nat", | ||
147 | "kill"); | ||
148 | GNUNET_OS_process_wait (h->server_proc); | 139 | GNUNET_OS_process_wait (h->server_proc); |
149 | GNUNET_OS_process_destroy (h->server_proc); | 140 | GNUNET_OS_process_destroy (h->server_proc); |
150 | h->server_proc = NULL; | 141 | h->server_proc = NULL; |
@@ -171,44 +162,37 @@ nat_server_read (void *cls) | |||
171 | } | 162 | } |
172 | 163 | ||
173 | /* construct socket address of sender */ | 164 | /* construct socket address of sender */ |
174 | memset (&sin_addr, | 165 | memset (&sin_addr, 0, sizeof (sin_addr)); |
175 | 0, | ||
176 | sizeof (sin_addr)); | ||
177 | sin_addr.sin_family = AF_INET; | 166 | sin_addr.sin_family = AF_INET; |
178 | #if HAVE_SOCKADDR_IN_SIN_LEN | 167 | #if HAVE_SOCKADDR_IN_SIN_LEN |
179 | sin_addr.sin_len = sizeof (sin_addr); | 168 | sin_addr.sin_len = sizeof (sin_addr); |
180 | #endif | 169 | #endif |
181 | if ( (NULL == port_start) || | 170 | if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) || |
182 | (1 != SSCANF (port_start, | 171 | (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) |
183 | "%d", | ||
184 | &port)) || | ||
185 | (-1 == inet_pton (AF_INET, | ||
186 | mybuf, | ||
187 | &sin_addr.sin_addr))) | ||
188 | { | 172 | { |
189 | /* should we restart gnunet-helper-nat-server? */ | 173 | /* should we restart gnunet-helper-nat-server? */ |
190 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 174 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
191 | _("gnunet-helper-nat-server generated malformed address `%s'\n"), | 175 | _ ( |
192 | mybuf); | 176 | "gnunet-helper-nat-server generated malformed address `%s'\n"), |
193 | h->server_read_task | 177 | mybuf); |
194 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 178 | h->server_read_task = |
195 | h->server_stdout_handle, | 179 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
196 | &nat_server_read, | 180 | h->server_stdout_handle, |
197 | h); | 181 | &nat_server_read, |
182 | h); | ||
198 | return; | 183 | return; |
199 | } | 184 | } |
200 | sin_addr.sin_port = htons ((uint16_t) port); | 185 | sin_addr.sin_port = htons ((uint16_t) port); |
201 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 186 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
202 | "gnunet-helper-nat-server read: %s:%d\n", | 187 | "gnunet-helper-nat-server read: %s:%d\n", |
203 | mybuf, | 188 | mybuf, |
204 | port); | 189 | port); |
205 | h->cb (h->cb_cls, | 190 | h->cb (h->cb_cls, &sin_addr); |
206 | &sin_addr); | 191 | h->server_read_task = |
207 | h->server_read_task | 192 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
208 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 193 | h->server_stdout_handle, |
209 | h->server_stdout_handle, | 194 | &nat_server_read, |
210 | &nat_server_read, | 195 | h); |
211 | h); | ||
212 | } | 196 | } |
213 | 197 | ||
214 | 198 | ||
@@ -227,71 +211,59 @@ restart_nat_server (void *cls) | |||
227 | 211 | ||
228 | h->server_read_task = NULL; | 212 | h->server_read_task = NULL; |
229 | GNUNET_assert (NULL != | 213 | GNUNET_assert (NULL != |
230 | inet_ntop (AF_INET, | 214 | inet_ntop (AF_INET, &h->internal_address, ia, sizeof (ia))); |
231 | &h->internal_address, | ||
232 | ia, | ||
233 | sizeof (ia))); | ||
234 | /* Start the server process */ | 215 | /* Start the server process */ |
235 | binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server"); | 216 | binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server"); |
236 | if (GNUNET_YES != | 217 | if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia)) |
237 | GNUNET_OS_check_helper_binary (binary, | ||
238 | GNUNET_YES, | ||
239 | ia)) | ||
240 | { | 218 | { |
241 | /* move instantly to max delay, as this is unlikely to be fixed */ | 219 | /* move instantly to max delay, as this is unlikely to be fixed */ |
242 | h->server_retry_delay | 220 | h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; |
243 | = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; | ||
244 | GNUNET_free (binary); | 221 | GNUNET_free (binary); |
245 | try_again (h); | 222 | try_again (h); |
246 | return; | 223 | return; |
247 | } | 224 | } |
248 | h->server_stdout | 225 | h->server_stdout = |
249 | = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, | 226 | GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); |
250 | GNUNET_NO, GNUNET_YES); | ||
251 | if (NULL == h->server_stdout) | 227 | if (NULL == h->server_stdout) |
252 | { | 228 | { |
253 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | 229 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); |
254 | "pipe"); | ||
255 | GNUNET_free (binary); | 230 | GNUNET_free (binary); |
256 | try_again (h); | 231 | try_again (h); |
257 | return; | 232 | return; |
258 | } | 233 | } |
259 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 234 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
260 | "Starting `%s' at `%s'\n", | 235 | "Starting `%s' at `%s'\n", |
261 | "gnunet-helper-nat-server", | 236 | "gnunet-helper-nat-server", |
262 | ia); | 237 | ia); |
263 | h->server_proc | 238 | h->server_proc = GNUNET_OS_start_process (GNUNET_NO, |
264 | = GNUNET_OS_start_process (GNUNET_NO, | 239 | 0, |
265 | 0, | 240 | NULL, |
266 | NULL, | 241 | h->server_stdout, |
267 | h->server_stdout, | 242 | NULL, |
268 | NULL, | 243 | binary, |
269 | binary, | 244 | "gnunet-helper-nat-server", |
270 | "gnunet-helper-nat-server", | 245 | ia, |
271 | ia, | 246 | NULL); |
272 | NULL); | ||
273 | GNUNET_free (binary); | 247 | GNUNET_free (binary); |
274 | if (NULL == h->server_proc) | 248 | if (NULL == h->server_proc) |
275 | { | 249 | { |
276 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 250 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
277 | _("Failed to start %s\n"), | 251 | _ ("Failed to start %s\n"), |
278 | "gnunet-helper-nat-server"); | 252 | "gnunet-helper-nat-server"); |
279 | GNUNET_DISK_pipe_close (h->server_stdout); | 253 | GNUNET_DISK_pipe_close (h->server_stdout); |
280 | h->server_stdout = NULL; | 254 | h->server_stdout = NULL; |
281 | try_again (h); | 255 | try_again (h); |
282 | return; | 256 | return; |
283 | } | 257 | } |
284 | /* Close the write end of the read pipe */ | 258 | /* Close the write end of the read pipe */ |
285 | GNUNET_DISK_pipe_close_end (h->server_stdout, | 259 | GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); |
286 | GNUNET_DISK_PIPE_END_WRITE); | 260 | h->server_stdout_handle = |
287 | h->server_stdout_handle | 261 | GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ); |
288 | = GNUNET_DISK_pipe_handle (h->server_stdout, | 262 | h->server_read_task = |
289 | GNUNET_DISK_PIPE_END_READ); | 263 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
290 | h->server_read_task | 264 | h->server_stdout_handle, |
291 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 265 | &nat_server_read, |
292 | h->server_stdout_handle, | 266 | h); |
293 | &nat_server_read, | ||
294 | h); | ||
295 | } | 267 | } |
296 | 268 | ||
297 | 269 | ||
@@ -307,9 +279,9 @@ restart_nat_server (void *cls) | |||
307 | */ | 279 | */ |
308 | struct HelperContext * | 280 | struct HelperContext * |
309 | GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, | 281 | GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, |
310 | GN_ReversalCallback cb, | 282 | GN_ReversalCallback cb, |
311 | void *cb_cls, | 283 | void *cb_cls, |
312 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 284 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
313 | { | 285 | { |
314 | struct HelperContext *h; | 286 | struct HelperContext *h; |
315 | 287 | ||
@@ -344,10 +316,8 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h) | |||
344 | } | 316 | } |
345 | if (NULL != h->server_proc) | 317 | if (NULL != h->server_proc) |
346 | { | 318 | { |
347 | if (0 != GNUNET_OS_process_kill (h->server_proc, | 319 | if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) |
348 | GNUNET_TERM_SIG)) | 320 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); |
349 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
350 | "kill"); | ||
351 | GNUNET_OS_process_wait (h->server_proc); | 321 | GNUNET_OS_process_wait (h->server_proc); |
352 | GNUNET_OS_process_destroy (h->server_proc); | 322 | GNUNET_OS_process_destroy (h->server_proc); |
353 | h->server_proc = NULL; | 323 | h->server_proc = NULL; |
@@ -379,9 +349,9 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h) | |||
379 | */ | 349 | */ |
380 | int | 350 | int |
381 | GN_request_connection_reversal (const struct in_addr *internal_address, | 351 | GN_request_connection_reversal (const struct in_addr *internal_address, |
382 | uint16_t internal_port, | 352 | uint16_t internal_port, |
383 | const struct in_addr *remote_v4, | 353 | const struct in_addr *remote_v4, |
384 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 354 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
385 | { | 355 | { |
386 | char intv4[INET_ADDRSTRLEN]; | 356 | char intv4[INET_ADDRSTRLEN]; |
387 | char remv4[INET_ADDRSTRLEN]; | 357 | char remv4[INET_ADDRSTRLEN]; |
@@ -389,22 +359,14 @@ GN_request_connection_reversal (const struct in_addr *internal_address, | |||
389 | struct GNUNET_OS_Process *proc; | 359 | struct GNUNET_OS_Process *proc; |
390 | char *binary; | 360 | char *binary; |
391 | 361 | ||
392 | if (NULL == inet_ntop (AF_INET, | 362 | if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN)) |
393 | internal_address, | ||
394 | intv4, | ||
395 | INET_ADDRSTRLEN)) | ||
396 | { | 363 | { |
397 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | 364 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); |
398 | "inet_ntop"); | ||
399 | return GNUNET_SYSERR; | 365 | return GNUNET_SYSERR; |
400 | } | 366 | } |
401 | if (NULL == inet_ntop (AF_INET, | 367 | if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN)) |
402 | remote_v4, | ||
403 | remv4, | ||
404 | INET_ADDRSTRLEN)) | ||
405 | { | 368 | { |
406 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | 369 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); |
407 | "inet_ntop"); | ||
408 | return GNUNET_SYSERR; | 370 | return GNUNET_SYSERR; |
409 | } | 371 | } |
410 | GNUNET_snprintf (port_as_string, | 372 | GNUNET_snprintf (port_as_string, |
@@ -412,23 +374,22 @@ GN_request_connection_reversal (const struct in_addr *internal_address, | |||
412 | "%d", | 374 | "%d", |
413 | internal_port); | 375 | internal_port); |
414 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 376 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
415 | "Running gnunet-helper-nat-client %s %s %u\n", | 377 | "Running gnunet-helper-nat-client %s %s %u\n", |
416 | intv4, | 378 | intv4, |
417 | remv4, | 379 | remv4, |
418 | internal_port); | 380 | internal_port); |
419 | binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client"); | 381 | binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client"); |
420 | proc | 382 | proc = GNUNET_OS_start_process (GNUNET_NO, |
421 | = GNUNET_OS_start_process (GNUNET_NO, | 383 | 0, |
422 | 0, | 384 | NULL, |
423 | NULL, | 385 | NULL, |
424 | NULL, | 386 | NULL, |
425 | NULL, | 387 | binary, |
426 | binary, | 388 | "gnunet-helper-nat-client", |
427 | "gnunet-helper-nat-client", | 389 | intv4, |
428 | intv4, | 390 | remv4, |
429 | remv4, | 391 | port_as_string, |
430 | port_as_string, | 392 | NULL); |
431 | NULL); | ||
432 | GNUNET_free (binary); | 393 | GNUNET_free (binary); |
433 | if (NULL == proc) | 394 | if (NULL == proc) |
434 | return GNUNET_SYSERR; | 395 | return GNUNET_SYSERR; |