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