aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-service-nat_mini.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat/gnunet-service-nat_mini.c')
-rw-r--r--src/nat/gnunet-service-nat_mini.c594
1 files changed, 295 insertions, 299 deletions
diff --git a/src/nat/gnunet-service-nat_mini.c b/src/nat/gnunet-service-nat_mini.c
index 375188ff2..94534e81c 100644
--- a/src/nat/gnunet-service-nat_mini.c
+++ b/src/nat/gnunet-service-nat_mini.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 * @file nat/gnunet-service-nat_mini.c 22 * @file nat/gnunet-service-nat_mini.c
@@ -29,24 +29,24 @@
29#include "gnunet-service-nat_mini.h" 29#include "gnunet-service-nat_mini.h"
30#include "nat.h" 30#include "nat.h"
31 31
32#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__)
33 33
34/** 34/**
35 * How long do we give upnpc to create a mapping? 35 * How long do we give upnpc to create a mapping?
36 */ 36 */
37#define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 37#define MAP_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
38 38
39/** 39/**
40 * How long do we give upnpc to remove a mapping? 40 * How long do we give upnpc to remove a mapping?
41 */ 41 */
42#define UNMAP_TIMEOUT \ 42#define UNMAP_TIMEOUT \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) 43 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
44 44
45/** 45/**
46 * How often do we check for changes in the mapping? 46 * How often do we check for changes in the mapping?
47 */ 47 */
48#define MAP_REFRESH_FREQ \ 48#define MAP_REFRESH_FREQ \
49 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 49 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
50 50
51 51
52/* ************************* external-ip calling ************************ */ 52/* ************************* external-ip calling ************************ */
@@ -54,9 +54,7 @@
54/** 54/**
55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation. 55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
56 */ 56 */
57struct GNUNET_NAT_ExternalHandle 57struct GNUNET_NAT_ExternalHandle {
58{
59
60 /** 58 /**
61 * Function to call with the result. 59 * Function to call with the result.
62 */ 60 */
@@ -111,43 +109,43 @@ struct GNUNET_NAT_ExternalHandle
111 * @param cls the `struct GNUNET_NAT_ExternalHandle` 109 * @param cls the `struct GNUNET_NAT_ExternalHandle`
112 */ 110 */
113static void 111static void
114read_external_ipv4 (void *cls) 112read_external_ipv4(void *cls)
115{ 113{
116 struct GNUNET_NAT_ExternalHandle *eh = cls; 114 struct GNUNET_NAT_ExternalHandle *eh = cls;
117 ssize_t ret; 115 ssize_t ret;
118 struct in_addr addr; 116 struct in_addr addr;
119 117
120 eh->task = NULL; 118 eh->task = NULL;
121 ret = GNUNET_DISK_file_read (eh->r, 119 ret = GNUNET_DISK_file_read(eh->r,
122 &eh->buf[eh->off], 120 &eh->buf[eh->off],
123 sizeof (eh->buf) - eh->off); 121 sizeof(eh->buf) - eh->off);
124 if (ret > 0) 122 if (ret > 0)
125 { 123 {
126 /* try to read more */ 124 /* try to read more */
127 eh->off += ret; 125 eh->off += ret;
128 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 126 eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
129 eh->r, 127 eh->r,
130 &read_external_ipv4, 128 &read_external_ipv4,
131 eh); 129 eh);
132 return; 130 return;
133 } 131 }
134 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; 132 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
135 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) 133 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
136 {
137 eh->buf[eh->off - 1] = '\0';
138 if (1 == inet_pton (AF_INET, eh->buf, &addr))
139 { 134 {
140 if (0 == addr.s_addr) 135 eh->buf[eh->off - 1] = '\0';
141 eh->ret = 136 if (1 == inet_pton(AF_INET, eh->buf, &addr))
142 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ 137 {
143 else 138 if (0 == addr.s_addr)
144 eh->ret = GNUNET_NAT_ERROR_SUCCESS; 139 eh->ret =
140 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
141 else
142 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
143 }
145 } 144 }
146 } 145 eh->cb(eh->cb_cls,
147 eh->cb (eh->cb_cls, 146 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL,
148 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL, 147 eh->ret);
149 eh->ret); 148 GNUNET_NAT_mini_get_external_ipv4_cancel_(eh);
150 GNUNET_NAT_mini_get_external_ipv4_cancel_ (eh);
151} 149}
152 150
153 151
@@ -157,13 +155,13 @@ read_external_ipv4 (void *cls)
157 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed) 155 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed)
158 */ 156 */
159static void 157static void
160signal_external_ip_error (void *cls) 158signal_external_ip_error(void *cls)
161{ 159{
162 struct GNUNET_NAT_ExternalHandle *eh = cls; 160 struct GNUNET_NAT_ExternalHandle *eh = cls;
163 161
164 eh->task = NULL; 162 eh->task = NULL;
165 eh->cb (eh->cb_cls, NULL, eh->ret); 163 eh->cb(eh->cb_cls, NULL, eh->ret);
166 GNUNET_free (eh); 164 GNUNET_free(eh);
167} 165}
168 166
169 167
@@ -175,52 +173,52 @@ signal_external_ip_error (void *cls)
175 * @return handle for cancellation (can only be used until @a cb is called), never NULL 173 * @return handle for cancellation (can only be used until @a cb is called), never NULL
176 */ 174 */
177struct GNUNET_NAT_ExternalHandle * 175struct GNUNET_NAT_ExternalHandle *
178GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls) 176GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, void *cb_cls)
179{ 177{
180 struct GNUNET_NAT_ExternalHandle *eh; 178 struct GNUNET_NAT_ExternalHandle *eh;
181 179
182 eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle); 180 eh = GNUNET_new(struct GNUNET_NAT_ExternalHandle);
183 eh->cb = cb; 181 eh->cb = cb;
184 eh->cb_cls = cb_cls; 182 eh->cb_cls = cb_cls;
185 eh->ret = GNUNET_NAT_ERROR_SUCCESS; 183 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
186 if (GNUNET_SYSERR == 184 if (GNUNET_SYSERR ==
187 GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL)) 185 GNUNET_OS_check_helper_binary("external-ip", GNUNET_NO, NULL))
188 { 186 {
189 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n")); 187 LOG(GNUNET_ERROR_TYPE_INFO, _("`external-ip' command not found\n"));
190 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; 188 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
191 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); 189 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh);
192 return eh; 190 return eh;
193 } 191 }
194 LOG (GNUNET_ERROR_TYPE_DEBUG, 192 LOG(GNUNET_ERROR_TYPE_DEBUG,
195 "Running `external-ip' to determine our external IP\n"); 193 "Running `external-ip' to determine our external IP\n");
196 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 194 eh->opipe = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
197 if (NULL == eh->opipe) 195 if (NULL == eh->opipe)
198 { 196 {
199 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; 197 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
200 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); 198 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh);
201 return eh; 199 return eh;
202 } 200 }
203 eh->eip = GNUNET_OS_start_process (GNUNET_NO, 201 eh->eip = GNUNET_OS_start_process(GNUNET_NO,
204 0, 202 0,
205 NULL, 203 NULL,
206 eh->opipe, 204 eh->opipe,
207 NULL, 205 NULL,
208 "external-ip", 206 "external-ip",
209 "external-ip", 207 "external-ip",
210 NULL); 208 NULL);
211 if (NULL == eh->eip) 209 if (NULL == eh->eip)
212 { 210 {
213 GNUNET_DISK_pipe_close (eh->opipe); 211 GNUNET_DISK_pipe_close(eh->opipe);
214 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; 212 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
215 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); 213 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh);
216 return eh; 214 return eh;
217 } 215 }
218 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); 216 GNUNET_DISK_pipe_close_end(eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
219 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); 217 eh->r = GNUNET_DISK_pipe_handle(eh->opipe, GNUNET_DISK_PIPE_END_READ);
220 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 218 eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
221 eh->r, 219 eh->r,
222 &read_external_ipv4, 220 &read_external_ipv4,
223 eh); 221 eh);
224 return eh; 222 return eh;
225} 223}
226 224
@@ -231,25 +229,25 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls)
231 * @param eh operation to cancel 229 * @param eh operation to cancel
232 */ 230 */
233void 231void
234GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh) 232GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh)
235{ 233{
236 if (NULL != eh->eip) 234 if (NULL != eh->eip)
237 { 235 {
238 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL); 236 (void)GNUNET_OS_process_kill(eh->eip, SIGKILL);
239 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip)); 237 GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(eh->eip));
240 GNUNET_OS_process_destroy (eh->eip); 238 GNUNET_OS_process_destroy(eh->eip);
241 } 239 }
242 if (NULL != eh->opipe) 240 if (NULL != eh->opipe)
243 { 241 {
244 GNUNET_DISK_pipe_close (eh->opipe); 242 GNUNET_DISK_pipe_close(eh->opipe);
245 eh->opipe = NULL; 243 eh->opipe = NULL;
246 } 244 }
247 if (NULL != eh->task) 245 if (NULL != eh->task)
248 { 246 {
249 GNUNET_SCHEDULER_cancel (eh->task); 247 GNUNET_SCHEDULER_cancel(eh->task);
250 eh->task = NULL; 248 eh->task = NULL;
251 } 249 }
252 GNUNET_free (eh); 250 GNUNET_free(eh);
253} 251}
254 252
255 253
@@ -259,9 +257,7 @@ GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh)
259/** 257/**
260 * Handle to a mapping created with upnpc. 258 * Handle to a mapping created with upnpc.
261 */ 259 */
262struct GNUNET_NAT_MiniHandle 260struct GNUNET_NAT_MiniHandle {
263{
264
265 /** 261 /**
266 * Function to call on mapping changes. 262 * Function to call on mapping changes.
267 */ 263 */
@@ -326,7 +322,7 @@ struct GNUNET_NAT_MiniHandle
326 * @param cls the `struct GNUNET_NAT_MiniHandle` 322 * @param cls the `struct GNUNET_NAT_MiniHandle`
327 */ 323 */
328static void 324static void
329do_refresh (void *cls); 325do_refresh(void *cls);
330 326
331 327
332/** 328/**
@@ -336,7 +332,7 @@ do_refresh (void *cls);
336 * @param line line of output, NULL at the end 332 * @param line line of output, NULL at the end
337 */ 333 */
338static void 334static void
339process_map_output (void *cls, const char *line); 335process_map_output(void *cls, const char *line);
340 336
341 337
342/** 338/**
@@ -345,29 +341,29 @@ process_map_output (void *cls, const char *line);
345 * @param mini our handle 341 * @param mini our handle
346 */ 342 */
347static void 343static void
348run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) 344run_upnpc_r(struct GNUNET_NAT_MiniHandle *mini)
349{ 345{
350 char pstr[6]; 346 char pstr[6];
351 347
352 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); 348 GNUNET_snprintf(pstr, sizeof(pstr), "%u", (unsigned int)mini->port);
353 mini->map_cmd = GNUNET_OS_command_run (&process_map_output, 349 mini->map_cmd = GNUNET_OS_command_run(&process_map_output,
354 mini, 350 mini,
355 MAP_TIMEOUT, 351 MAP_TIMEOUT,
356 "upnpc", 352 "upnpc",
357 "upnpc", 353 "upnpc",
358 "-r", 354 "-r",
359 pstr, 355 pstr,
360 mini->is_tcp ? "tcp" : "udp", 356 mini->is_tcp ? "tcp" : "udp",
361 NULL); 357 NULL);
362 if (NULL == mini->map_cmd) 358 if (NULL == mini->map_cmd)
363 { 359 {
364 mini->ac (mini->ac_cls, 360 mini->ac(mini->ac_cls,
365 GNUNET_SYSERR, 361 GNUNET_SYSERR,
366 NULL, 362 NULL,
367 0, 363 0,
368 GNUNET_NAT_ERROR_UPNPC_FAILED); 364 GNUNET_NAT_ERROR_UPNPC_FAILED);
369 return; 365 return;
370 } 366 }
371} 367}
372 368
373 369
@@ -379,7 +375,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
379 * @param line line of output, NULL at the end 375 * @param line line of output, NULL at the end
380 */ 376 */
381static void 377static void
382process_refresh_output (void *cls, const char *line) 378process_refresh_output(void *cls, const char *line)
383{ 379{
384 struct GNUNET_NAT_MiniHandle *mini = cls; 380 struct GNUNET_NAT_MiniHandle *mini = cls;
385 char pstr[9]; 381 char pstr[9];
@@ -388,53 +384,53 @@ process_refresh_output (void *cls, const char *line)
388 struct in_addr exip; 384 struct in_addr exip;
389 385
390 if (NULL == line) 386 if (NULL == line)
391 {
392 GNUNET_OS_command_stop (mini->refresh_cmd);
393 mini->refresh_cmd = NULL;
394 if (GNUNET_NO == mini->found)
395 { 387 {
396 /* mapping disappeared, try to re-create */ 388 GNUNET_OS_command_stop(mini->refresh_cmd);
397 if (GNUNET_YES == mini->did_map) 389 mini->refresh_cmd = NULL;
398 { 390 if (GNUNET_NO == mini->found)
399 mini->ac (mini->ac_cls, 391 {
400 GNUNET_NO, 392 /* mapping disappeared, try to re-create */
401 (const struct sockaddr *) &mini->current_addr, 393 if (GNUNET_YES == mini->did_map)
402 sizeof (mini->current_addr), 394 {
403 GNUNET_NAT_ERROR_SUCCESS); 395 mini->ac(mini->ac_cls,
404 mini->did_map = GNUNET_NO; 396 GNUNET_NO,
405 } 397 (const struct sockaddr *)&mini->current_addr,
406 run_upnpc_r (mini); 398 sizeof(mini->current_addr),
399 GNUNET_NAT_ERROR_SUCCESS);
400 mini->did_map = GNUNET_NO;
401 }
402 run_upnpc_r(mini);
403 }
404 return;
407 } 405 }
408 return; 406 if (!mini->did_map)
409 }
410 if (! mini->did_map)
411 return; /* never mapped, won't find our mapping anyway */ 407 return; /* never mapped, won't find our mapping anyway */
412 408
413 /* we're looking for output of the form: 409 /* we're looking for output of the form:
414 * "ExternalIPAddress = 12.134.41.124" */ 410 * "ExternalIPAddress = 12.134.41.124" */
415 411
416 s = strstr (line, "ExternalIPAddress = "); 412 s = strstr(line, "ExternalIPAddress = ");
417 if (NULL != s) 413 if (NULL != s)
418 { 414 {
419 s += strlen ("ExternalIPAddress = "); 415 s += strlen("ExternalIPAddress = ");
420 if (1 != inet_pton (AF_INET, s, &exip)) 416 if (1 != inet_pton(AF_INET, s, &exip))
421 return; /* skip */ 417 return; /* skip */
422 if (exip.s_addr == mini->current_addr.sin_addr.s_addr) 418 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
423 return; /* no change */ 419 return; /* no change */
424 /* update mapping */ 420 /* update mapping */
425 mini->ac (mini->ac_cls, 421 mini->ac(mini->ac_cls,
426 GNUNET_NO, 422 GNUNET_NO,
427 (const struct sockaddr *) &mini->current_addr, 423 (const struct sockaddr *)&mini->current_addr,
428 sizeof (mini->current_addr), 424 sizeof(mini->current_addr),
429 GNUNET_NAT_ERROR_SUCCESS); 425 GNUNET_NAT_ERROR_SUCCESS);
430 mini->current_addr.sin_addr = exip; 426 mini->current_addr.sin_addr = exip;
431 mini->ac (mini->ac_cls, 427 mini->ac(mini->ac_cls,
432 GNUNET_YES, 428 GNUNET_YES,
433 (const struct sockaddr *) &mini->current_addr, 429 (const struct sockaddr *)&mini->current_addr,
434 sizeof (mini->current_addr), 430 sizeof(mini->current_addr),
435 GNUNET_NAT_ERROR_SUCCESS); 431 GNUNET_NAT_ERROR_SUCCESS);
436 return; 432 return;
437 } 433 }
438 /* 434 /*
439 * we're looking for output of the form: 435 * we're looking for output of the form:
440 * 436 *
@@ -446,32 +442,32 @@ process_refresh_output (void *cls, const char *line)
446 * "%s TCP PORT->STRING:OURPORT *" or 442 * "%s TCP PORT->STRING:OURPORT *" or
447 * "%s UDP PORT->STRING:OURPORT *" 443 * "%s UDP PORT->STRING:OURPORT *"
448 */ 444 */
449 GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port); 445 GNUNET_snprintf(pstr, sizeof(pstr), ":%u ", mini->port);
450 if (NULL == (s = strstr (line, "->"))) 446 if (NULL == (s = strstr(line, "->")))
451 return; /* skip */ 447 return; /* skip */
452 if (NULL == strstr (s, pstr)) 448 if (NULL == strstr(s, pstr))
453 return; /* skip */ 449 return; /* skip */
454 if (1 != sscanf (line, 450 if (1 != sscanf(line,
455 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" 451 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s"
456 : "%*u UDP %u->%*s:%*u %*s", 452 : "%*u UDP %u->%*s:%*u %*s",
457 &nport)) 453 &nport))
458 return; /* skip */ 454 return; /* skip */
459 mini->found = GNUNET_YES; 455 mini->found = GNUNET_YES;
460 if (nport == ntohs (mini->current_addr.sin_port)) 456 if (nport == ntohs(mini->current_addr.sin_port))
461 return; /* no change */ 457 return; /* no change */
462 458
463 /* external port changed, update mapping */ 459 /* external port changed, update mapping */
464 mini->ac (mini->ac_cls, 460 mini->ac(mini->ac_cls,
465 GNUNET_NO, 461 GNUNET_NO,
466 (const struct sockaddr *) &mini->current_addr, 462 (const struct sockaddr *)&mini->current_addr,
467 sizeof (mini->current_addr), 463 sizeof(mini->current_addr),
468 GNUNET_NAT_ERROR_SUCCESS); 464 GNUNET_NAT_ERROR_SUCCESS);
469 mini->current_addr.sin_port = htons ((uint16_t) nport); 465 mini->current_addr.sin_port = htons((uint16_t)nport);
470 mini->ac (mini->ac_cls, 466 mini->ac(mini->ac_cls,
471 GNUNET_YES, 467 GNUNET_YES,
472 (const struct sockaddr *) &mini->current_addr, 468 (const struct sockaddr *)&mini->current_addr,
473 sizeof (mini->current_addr), 469 sizeof(mini->current_addr),
474 GNUNET_NAT_ERROR_SUCCESS); 470 GNUNET_NAT_ERROR_SUCCESS);
475} 471}
476 472
477 473
@@ -481,44 +477,44 @@ process_refresh_output (void *cls, const char *line)
481 * @param cls the 'struct GNUNET_NAT_MiniHandle' 477 * @param cls the 'struct GNUNET_NAT_MiniHandle'
482 */ 478 */
483static void 479static void
484do_refresh (void *cls) 480do_refresh(void *cls)
485{ 481{
486 struct GNUNET_NAT_MiniHandle *mini = cls; 482 struct GNUNET_NAT_MiniHandle *mini = cls;
487 int ac; 483 int ac;
488 484
489 mini->refresh_task = 485 mini->refresh_task =
490 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); 486 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini);
491 LOG (GNUNET_ERROR_TYPE_DEBUG, 487 LOG(GNUNET_ERROR_TYPE_DEBUG,
492 "Running `upnpc' to check if our mapping still exists\n"); 488 "Running `upnpc' to check if our mapping still exists\n");
493 mini->found = GNUNET_NO; 489 mini->found = GNUNET_NO;
494 ac = GNUNET_NO; 490 ac = GNUNET_NO;
495 if (NULL != mini->map_cmd) 491 if (NULL != mini->map_cmd)
496 { 492 {
497 /* took way too long, abort it! */ 493 /* took way too long, abort it! */
498 GNUNET_OS_command_stop (mini->map_cmd); 494 GNUNET_OS_command_stop(mini->map_cmd);
499 mini->map_cmd = NULL; 495 mini->map_cmd = NULL;
500 ac = GNUNET_YES; 496 ac = GNUNET_YES;
501 } 497 }
502 if (NULL != mini->refresh_cmd) 498 if (NULL != mini->refresh_cmd)
503 { 499 {
504 /* took way too long, abort it! */ 500 /* took way too long, abort it! */
505 GNUNET_OS_command_stop (mini->refresh_cmd); 501 GNUNET_OS_command_stop(mini->refresh_cmd);
506 mini->refresh_cmd = NULL; 502 mini->refresh_cmd = NULL;
507 ac = GNUNET_YES; 503 ac = GNUNET_YES;
508 } 504 }
509 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, 505 mini->refresh_cmd = GNUNET_OS_command_run(&process_refresh_output,
510 mini, 506 mini,
511 MAP_TIMEOUT, 507 MAP_TIMEOUT,
512 "upnpc", 508 "upnpc",
513 "upnpc", 509 "upnpc",
514 "-l", 510 "-l",
515 NULL); 511 NULL);
516 if (GNUNET_YES == ac) 512 if (GNUNET_YES == ac)
517 mini->ac (mini->ac_cls, 513 mini->ac(mini->ac_cls,
518 GNUNET_SYSERR, 514 GNUNET_SYSERR,
519 NULL, 515 NULL,
520 0, 516 0,
521 GNUNET_NAT_ERROR_UPNPC_TIMEOUT); 517 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
522} 518}
523 519
524 520
@@ -529,7 +525,7 @@ do_refresh (void *cls)
529 * @param line line of output, NULL at the end 525 * @param line line of output, NULL at the end
530 */ 526 */
531static void 527static void
532process_map_output (void *cls, const char *line) 528process_map_output(void *cls, const char *line)
533{ 529{
534 struct GNUNET_NAT_MiniHandle *mini = cls; 530 struct GNUNET_NAT_MiniHandle *mini = cls;
535 const char *ipaddr; 531 const char *ipaddr;
@@ -538,51 +534,51 @@ process_map_output (void *cls, const char *line)
538 unsigned int port; 534 unsigned int port;
539 535
540 if (NULL == line) 536 if (NULL == line)
541 { 537 {
542 GNUNET_OS_command_stop (mini->map_cmd); 538 GNUNET_OS_command_stop(mini->map_cmd);
543 mini->map_cmd = NULL; 539 mini->map_cmd = NULL;
544 if (GNUNET_YES != mini->did_map) 540 if (GNUNET_YES != mini->did_map)
545 mini->ac (mini->ac_cls, 541 mini->ac(mini->ac_cls,
546 GNUNET_SYSERR, 542 GNUNET_SYSERR,
547 NULL, 543 NULL,
548 0, 544 0,
549 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); 545 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
550 if (NULL == mini->refresh_task) 546 if (NULL == mini->refresh_task)
551 mini->refresh_task = 547 mini->refresh_task =
552 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); 548 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini);
553 return; 549 return;
554 } 550 }
555 /* 551 /*
556 * The upnpc output we're after looks like this: 552 * The upnpc output we're after looks like this:
557 * 553 *
558 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" 554 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
559 */ 555 */
560 if ((NULL == (ipaddr = strstr (line, " "))) || 556 if ((NULL == (ipaddr = strstr(line, " "))) ||
561 (NULL == (pstr = strstr (ipaddr, ":"))) || 557 (NULL == (pstr = strstr(ipaddr, ":"))) ||
562 (1 != sscanf (pstr + 1, "%u", &port))) 558 (1 != sscanf(pstr + 1, "%u", &port)))
563 { 559 {
564 return; /* skip line */ 560 return; /* skip line */
565 } 561 }
566 ipa = GNUNET_strdup (ipaddr + 1); 562 ipa = GNUNET_strdup(ipaddr + 1);
567 strstr (ipa, ":")[0] = '\0'; 563 strstr(ipa, ":")[0] = '\0';
568 if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr)) 564 if (1 != inet_pton(AF_INET, ipa, &mini->current_addr.sin_addr))
569 { 565 {
570 GNUNET_free (ipa); 566 GNUNET_free(ipa);
571 return; /* skip line */ 567 return; /* skip line */
572 } 568 }
573 GNUNET_free (ipa); 569 GNUNET_free(ipa);
574 570
575 mini->current_addr.sin_port = htons (port); 571 mini->current_addr.sin_port = htons(port);
576 mini->current_addr.sin_family = AF_INET; 572 mini->current_addr.sin_family = AF_INET;
577#if HAVE_SOCKADDR_IN_SIN_LEN 573#if HAVE_SOCKADDR_IN_SIN_LEN
578 mini->current_addr.sin_len = sizeof (struct sockaddr_in); 574 mini->current_addr.sin_len = sizeof(struct sockaddr_in);
579#endif 575#endif
580 mini->did_map = GNUNET_YES; 576 mini->did_map = GNUNET_YES;
581 mini->ac (mini->ac_cls, 577 mini->ac(mini->ac_cls,
582 GNUNET_YES, 578 GNUNET_YES,
583 (const struct sockaddr *) &mini->current_addr, 579 (const struct sockaddr *)&mini->current_addr,
584 sizeof (mini->current_addr), 580 sizeof(mini->current_addr),
585 GNUNET_NAT_ERROR_SUCCESS); 581 GNUNET_NAT_ERROR_SUCCESS);
586} 582}
587 583
588 584
@@ -600,28 +596,28 @@ process_map_output (void *cls, const char *line)
600 * @return NULL on error (no 'upnpc' installed) 596 * @return NULL on error (no 'upnpc' installed)
601 */ 597 */
602struct GNUNET_NAT_MiniHandle * 598struct GNUNET_NAT_MiniHandle *
603GNUNET_NAT_mini_map_start (uint16_t port, 599GNUNET_NAT_mini_map_start(uint16_t port,
604 int is_tcp, 600 int is_tcp,
605 GNUNET_NAT_MiniAddressCallback ac, 601 GNUNET_NAT_MiniAddressCallback ac,
606 void *ac_cls) 602 void *ac_cls)
607{ 603{
608 struct GNUNET_NAT_MiniHandle *ret; 604 struct GNUNET_NAT_MiniHandle *ret;
609 605
610 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) 606 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL))
611 { 607 {
612 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n")); 608 LOG(GNUNET_ERROR_TYPE_INFO, _("`upnpc' command not found\n"));
613 ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); 609 ac(ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
614 return NULL; 610 return NULL;
615 } 611 }
616 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n"); 612 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n");
617 ret = GNUNET_new (struct GNUNET_NAT_MiniHandle); 613 ret = GNUNET_new(struct GNUNET_NAT_MiniHandle);
618 ret->ac = ac; 614 ret->ac = ac;
619 ret->ac_cls = ac_cls; 615 ret->ac_cls = ac_cls;
620 ret->is_tcp = is_tcp; 616 ret->is_tcp = is_tcp;
621 ret->port = port; 617 ret->port = port;
622 ret->refresh_task = 618 ret->refresh_task =
623 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret); 619 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, ret);
624 run_upnpc_r (ret); 620 run_upnpc_r(ret);
625 return ret; 621 return ret;
626} 622}
627 623
@@ -633,18 +629,18 @@ GNUNET_NAT_mini_map_start (uint16_t port,
633 * @param line line of output, NULL at the end 629 * @param line line of output, NULL at the end
634 */ 630 */
635static void 631static void
636process_unmap_output (void *cls, const char *line) 632process_unmap_output(void *cls, const char *line)
637{ 633{
638 struct GNUNET_NAT_MiniHandle *mini = cls; 634 struct GNUNET_NAT_MiniHandle *mini = cls;
639 635
640 if (NULL == line) 636 if (NULL == line)
641 { 637 {
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n"); 638 LOG(GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n");
643 GNUNET_OS_command_stop (mini->unmap_cmd); 639 GNUNET_OS_command_stop(mini->unmap_cmd);
644 mini->unmap_cmd = NULL; 640 mini->unmap_cmd = NULL;
645 GNUNET_free (mini); 641 GNUNET_free(mini);
646 return; 642 return;
647 } 643 }
648 /* we don't really care about the output... */ 644 /* we don't really care about the output... */
649} 645}
650 646
@@ -658,54 +654,54 @@ process_unmap_output (void *cls, const char *line)
658 * @param mini the handle 654 * @param mini the handle
659 */ 655 */
660void 656void
661GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) 657GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini)
662{ 658{
663 char pstr[6]; 659 char pstr[6];
664 660
665 if (NULL != mini->refresh_task) 661 if (NULL != mini->refresh_task)
666 { 662 {
667 GNUNET_SCHEDULER_cancel (mini->refresh_task); 663 GNUNET_SCHEDULER_cancel(mini->refresh_task);
668 mini->refresh_task = NULL; 664 mini->refresh_task = NULL;
669 } 665 }
670 if (NULL != mini->refresh_cmd) 666 if (NULL != mini->refresh_cmd)
671 { 667 {
672 GNUNET_OS_command_stop (mini->refresh_cmd); 668 GNUNET_OS_command_stop(mini->refresh_cmd);
673 mini->refresh_cmd = NULL; 669 mini->refresh_cmd = NULL;
674 } 670 }
675 if (NULL != mini->map_cmd) 671 if (NULL != mini->map_cmd)
676 { 672 {
677 GNUNET_OS_command_stop (mini->map_cmd); 673 GNUNET_OS_command_stop(mini->map_cmd);
678 mini->map_cmd = NULL; 674 mini->map_cmd = NULL;
679 } 675 }
680 if (GNUNET_NO == mini->did_map) 676 if (GNUNET_NO == mini->did_map)
681 { 677 {
682 GNUNET_free (mini); 678 GNUNET_free(mini);
683 return; 679 return;
684 } 680 }
685 mini->ac (mini->ac_cls, 681 mini->ac(mini->ac_cls,
686 GNUNET_NO, 682 GNUNET_NO,
687 (const struct sockaddr *) &mini->current_addr, 683 (const struct sockaddr *)&mini->current_addr,
688 sizeof (mini->current_addr), 684 sizeof(mini->current_addr),
689 GNUNET_NAT_ERROR_SUCCESS); 685 GNUNET_NAT_ERROR_SUCCESS);
690 /* Note: oddly enough, deletion uses the external port whereas 686 /* Note: oddly enough, deletion uses the external port whereas
691 * addition uses the internal port; this rarely matters since they 687 * addition uses the internal port; this rarely matters since they
692 * often are the same, but it might... */ 688 * often are the same, but it might... */
693 GNUNET_snprintf (pstr, 689 GNUNET_snprintf(pstr,
694 sizeof (pstr), 690 sizeof(pstr),
695 "%u", 691 "%u",
696 (unsigned int) ntohs (mini->current_addr.sin_port)); 692 (unsigned int)ntohs(mini->current_addr.sin_port));
697 LOG (GNUNET_ERROR_TYPE_DEBUG, 693 LOG(GNUNET_ERROR_TYPE_DEBUG,
698 "Unmapping port %u with UPnP\n", 694 "Unmapping port %u with UPnP\n",
699 ntohs (mini->current_addr.sin_port)); 695 ntohs(mini->current_addr.sin_port));
700 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, 696 mini->unmap_cmd = GNUNET_OS_command_run(&process_unmap_output,
701 mini, 697 mini,
702 UNMAP_TIMEOUT, 698 UNMAP_TIMEOUT,
703 "upnpc", 699 "upnpc",
704 "upnpc", 700 "upnpc",
705 "-d", 701 "-d",
706 pstr, 702 pstr,
707 mini->is_tcp ? "tcp" : "udp", 703 mini->is_tcp ? "tcp" : "udp",
708 NULL); 704 NULL);
709} 705}
710 706
711 707