aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2014-05-19 12:03:26 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2014-05-19 12:03:26 +0000
commit7528bcf5a5af0d90c06aeab964e5c6471900ae06 (patch)
tree24d181f1ae50e2cca9a2dcd49e90278e0a4389d7
parent26c076220f09adcbb79ba296985b78deeb53dd9a (diff)
downloadgnunet-7528bcf5a5af0d90c06aeab964e5c6471900ae06.tar.gz
gnunet-7528bcf5a5af0d90c06aeab964e5c6471900ae06.zip
- updated NAT callback function defintion to no longer carry around strings,
- NAT lib now uses error-code enums to signal success and errors - reworked related code to return errorcodes - added and updated a missing doxygen documention - todo next: testcases
-rw-r--r--src/include/gnunet_nat_lib.h69
-rw-r--r--src/nat/nat.c26
-rw-r--r--src/nat/nat_auto.c11
-rw-r--r--src/nat/nat_mini.c28
-rw-r--r--src/nat/nat_test.c6
-rw-r--r--src/transport/gnunet-transport.c4
6 files changed, 94 insertions, 50 deletions
diff --git a/src/include/gnunet_nat_lib.h b/src/include/gnunet_nat_lib.h
index d012353b5..40a5135bd 100644
--- a/src/include/gnunet_nat_lib.h
+++ b/src/include/gnunet_nat_lib.h
@@ -71,6 +71,56 @@ struct GNUNET_NAT_Handle;
71 71
72 72
73/** 73/**
74 * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
75 */
76enum GNUNET_NAT_FailureCode {
77 /**
78 * Just the default
79 */
80 GNUNET_NAT_ERROR_SUCCESS = 0,
81
82 /**
83 * `upnpc` command not found
84 */
85 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
86
87 /**
88 * Failed to run `upnpc` command
89 */
90 GNUNET_NAT_ERROR_UPNPC_FAILED,
91
92 /**
93 * `upnpc' command took too long, process killed
94 */
95 GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
96
97 /**
98 * `upnpc' command failed to establish port mapping
99 */
100 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
101
102 /**
103 * `external-ip' command not found
104 */
105 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
106
107 /**
108 * "no valid address was returned by `external-ip'"
109 */
110 GNUNET_NAT_ERROR_EXTERNAL_IP_NO_VALID_ADDRESS_FOUND,
111
112
113
114 /**
115 *
116 */
117 GNUNET_NAT_ERROR_,
118
119
120};
121
122
123/**
74 * Attempt to enable port redirection and detect public IP address 124 * Attempt to enable port redirection and detect public IP address
75 * contacting UPnP or NAT-PMP routers on the local network. Use addr 125 * contacting UPnP or NAT-PMP routers on the local network. Use addr
76 * to specify to which of the local host's addresses should the 126 * to specify to which of the local host's addresses should the
@@ -157,14 +207,10 @@ struct GNUNET_NAT_Test;
157 * NAT configuration test. 207 * NAT configuration test.
158 * 208 *
159 * @param cls closure 209 * @param cls closure
160 * @param success #GNUNET_OK on success, #GNUNET_NO on failure, 210 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
161 * #GNUNET_SYSERR if the test could not be
162 * properly started (internal failure)
163 * @param emsg NULL on success, otherwise may include an error message
164 */ 211 */
165typedef void (*GNUNET_NAT_TestCallback) (void *cls, 212typedef void (*GNUNET_NAT_TestCallback) (void *cls,
166 int success, 213 enum GNUNET_NAT_FailureCode result);
167 const char *emsg);
168 214
169 215
170/** 216/**
@@ -202,11 +248,11 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
202 * 248 *
203 * @param cls closure 249 * @param cls closure
204 * @param addr the address, NULL on errors 250 * @param addr the address, NULL on errors
205 * @param emsg NULL on success, otherwise may include an error message 251 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
206 */ 252 */
207typedef void (*GNUNET_NAT_IPCallback) (void *cls, 253typedef void (*GNUNET_NAT_IPCallback) (void *cls,
208 const struct in_addr *addr, 254 const struct in_addr *addr,
209 const char *emsg); 255 enum GNUNET_NAT_FailureCode result);
210 256
211 257
212 258
@@ -254,13 +300,14 @@ struct GNUNET_NAT_MiniHandle;
254 * the previous (now invalid) one 300 * the previous (now invalid) one
255 * @param addr either the previous or the new public IP address 301 * @param addr either the previous or the new public IP address
256 * @param addrlen actual length of the @a addr 302 * @param addrlen actual length of the @a addr
303 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
257 */ 304 */
258typedef void 305typedef void
259(*GNUNET_NAT_MiniAddressCallback) (void *cls, 306(*GNUNET_NAT_MiniAddressCallback) (void *cls,
260 int add_remove, 307 int add_remove,
261 const struct sockaddr *addr, 308 const struct sockaddr *addr,
262 socklen_t addrlen, 309 socklen_t addrlen,
263 const char *emsg); 310 enum GNUNET_NAT_FailureCode result);
264 311
265 312
266/** 313/**
@@ -307,12 +354,12 @@ struct GNUNET_NAT_AutoHandle;
307 * @param cls closure 354 * @param cls closure
308 * @param diff minimal suggested changes to the original configuration 355 * @param diff minimal suggested changes to the original configuration
309 * to make it work (as best as we can) 356 * to make it work (as best as we can)
310 * @param emsg NULL on success, otherwise may include an error message 357 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
311 */ 358 */
312typedef void 359typedef void
313(*GNUNET_NAT_AutoResultCallback)(void *cls, 360(*GNUNET_NAT_AutoResultCallback)(void *cls,
314 const struct GNUNET_CONFIGURATION_Handle *diff, 361 const struct GNUNET_CONFIGURATION_Handle *diff,
315 const char *emsg); 362 enum GNUNET_NAT_FailureCode result);
316 363
317 364
318/** 365/**
diff --git a/src/nat/nat.c b/src/nat/nat.c
index 039544d57..670ae6a27 100644
--- a/src/nat/nat.c
+++ b/src/nat/nat.c
@@ -1011,20 +1011,31 @@ resolve_dns (void *cls,
1011 * the previous (now invalid) one 1011 * the previous (now invalid) one
1012 * @param addr either the previous or the new public IP address 1012 * @param addr either the previous or the new public IP address
1013 * @param addrlen actual lenght of @a addr 1013 * @param addrlen actual lenght of @a addr
1014 * @param emsg NULL on success, otherwise an error message 1014 * @param ret GNUNET_NAT_ERROR_SUCCESS on success, otherwise an error code
1015 */ 1015 */
1016static void 1016static void
1017upnp_add (void *cls, 1017upnp_add (void *cls,
1018 int add_remove, 1018 int add_remove,
1019 const struct sockaddr *addr, 1019 const struct sockaddr *addr,
1020 socklen_t addrlen, 1020 socklen_t addrlen,
1021 const char *emsg) 1021 enum GNUNET_NAT_FailureCode ret)
1022{ 1022{
1023 struct GNUNET_NAT_Handle *h = cls; 1023 struct GNUNET_NAT_Handle *h = cls;
1024 struct LocalAddressList *pos; 1024 struct LocalAddressList *pos;
1025 struct LocalAddressList *next; 1025 struct LocalAddressList *next;
1026 1026
1027 1027
1028 if (GNUNET_NAT_ERROR_SUCCESS != ret)
1029 {
1030 /* Error while running upnp client */
1031 LOG (GNUNET_ERROR_TYPE_ERROR,
1032 _("Error while running upnp client:\n"));
1033
1034 //FIXME: convert error code to string
1035
1036 return;
1037 }
1038
1028 if (GNUNET_YES == add_remove) 1039 if (GNUNET_YES == add_remove)
1029 { 1040 {
1030 add_to_address_list (h, LAL_UPNP, addr, addrlen); 1041 add_to_address_list (h, LAL_UPNP, addr, addrlen);
@@ -1053,17 +1064,6 @@ upnp_add (void *cls,
1053 GNUNET_a2s(addr, addrlen)); 1064 GNUNET_a2s(addr, addrlen));
1054 GNUNET_break (0); 1065 GNUNET_break (0);
1055 } 1066 }
1056 else if (GNUNET_SYSERR == add_remove)
1057 {
1058 /* Error while running upnp client */
1059 if (NULL != emsg)
1060 LOG (GNUNET_ERROR_TYPE_ERROR,
1061 _("Error while running upnp client: `%s'\n"), emsg);
1062 else
1063 LOG (GNUNET_ERROR_TYPE_ERROR,
1064 _("Error while running upnp client \n"));
1065 return;
1066 }
1067 else 1067 else
1068 { 1068 {
1069 1069
diff --git a/src/nat/nat_auto.c b/src/nat/nat_auto.c
index 070099b23..184f23538 100644
--- a/src/nat/nat_auto.c
+++ b/src/nat/nat_auto.c
@@ -189,8 +189,7 @@ fail_timeout (void *cls,
189 */ 189 */
190static void 190static void
191result_callback (void *cls, 191result_callback (void *cls,
192 int success, 192 enum GNUNET_NAT_FailureCode ret)
193 const char *emsg)
194{ 193{
195 struct GNUNET_NAT_AutoHandle *ah = cls; 194 struct GNUNET_NAT_AutoHandle *ah = cls;
196 195
@@ -199,11 +198,11 @@ result_callback (void *cls,
199 GNUNET_NAT_test_stop (ah->tst); 198 GNUNET_NAT_test_stop (ah->tst);
200 ah->tst = NULL; 199 ah->tst = NULL;
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
202 success 201 ret
203 ? _("NAT traversal with ICMP Server succeeded.\n") 202 ? _("NAT traversal with ICMP Server succeeded.\n")
204 : _("NAT traversal with ICMP Server failed.\n")); 203 : _("NAT traversal with ICMP Server failed.\n"));
205 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER", 204 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
206 success ? "YES": "NO"); 205 ret ? "NO" : "YES");
207 next_phase (ah); 206 next_phase (ah);
208} 207}
209 208
@@ -258,7 +257,7 @@ test_online (struct GNUNET_NAT_AutoHandle *ah)
258static void 257static void
259set_external_ipv4 (void *cls, 258set_external_ipv4 (void *cls,
260 const struct in_addr *addr, 259 const struct in_addr *addr,
261 const char *emsg) 260 enum GNUNET_NAT_FailureCode ret)
262{ 261{
263 struct GNUNET_NAT_AutoHandle *ah = cls; 262 struct GNUNET_NAT_AutoHandle *ah = cls;
264 char buf[INET_ADDRSTRLEN]; 263 char buf[INET_ADDRSTRLEN];
@@ -525,7 +524,7 @@ next_phase (struct GNUNET_NAT_AutoHandle *ah)
525 ah->cfg); 524 ah->cfg);
526 ah->fin_cb (ah->fin_cb_cls, 525 ah->fin_cb (ah->fin_cb_cls,
527 diff, 526 diff,
528 NULL); 527 GNUNET_NAT_ERROR_SUCCESS);
529 GNUNET_CONFIGURATION_destroy (diff); 528 GNUNET_CONFIGURATION_destroy (diff);
530 GNUNET_NAT_autoconfig_cancel (ah); 529 GNUNET_NAT_autoconfig_cancel (ah);
531 return; 530 return;
diff --git a/src/nat/nat_mini.c b/src/nat/nat_mini.c
index 877c385d5..27a1361b2 100644
--- a/src/nat/nat_mini.c
+++ b/src/nat/nat_mini.c
@@ -150,9 +150,7 @@ read_external_ipv4 (void *cls,
150 (GNUNET_OK == iret) 150 (GNUNET_OK == iret)
151 ? &addr : 151 ? &addr :
152 NULL, 152 NULL,
153 (GNUNET_OK == iret) 153 GNUNET_NAT_ERROR_EXTERNAL_IP_NO_VALID_ADDRESS_FOUND);
154 ? NULL
155 : _("no valid address was returned by `external-ip'"));
156 GNUNET_NAT_mini_get_external_ipv4_cancel (eh); 154 GNUNET_NAT_mini_get_external_ipv4_cancel (eh);
157} 155}
158 156
@@ -172,7 +170,7 @@ signal_external_ip_error (void *cls,
172 eh->task = GNUNET_SCHEDULER_NO_TASK; 170 eh->task = GNUNET_SCHEDULER_NO_TASK;
173 eh->cb (eh->cb_cls, 171 eh->cb (eh->cb_cls,
174 NULL, 172 NULL,
175 _("`external-ip' command not found")); 173 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND);
176 GNUNET_free (eh); 174 GNUNET_free (eh);
177} 175}
178 176
@@ -363,7 +361,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
363 mini->ac (mini->ac_cls, 361 mini->ac (mini->ac_cls,
364 GNUNET_SYSERR, 362 GNUNET_SYSERR,
365 NULL, 0, 363 NULL, 0,
366 _("Failed to run `upnpc` command")); 364 GNUNET_NAT_ERROR_UPNPC_FAILED);
367 return; 365 return;
368 } 366 }
369} 367}
@@ -398,7 +396,7 @@ process_refresh_output (void *cls, const char *line)
398 GNUNET_NO, 396 GNUNET_NO,
399 (const struct sockaddr *) &mini->current_addr, 397 (const struct sockaddr *) &mini->current_addr,
400 sizeof (mini->current_addr), 398 sizeof (mini->current_addr),
401 NULL); 399 GNUNET_NAT_ERROR_SUCCESS);
402 mini->did_map = GNUNET_NO; 400 mini->did_map = GNUNET_NO;
403 } 401 }
404 run_upnpc_r (mini); 402 run_upnpc_r (mini);
@@ -423,12 +421,12 @@ process_refresh_output (void *cls, const char *line)
423 mini->ac (mini->ac_cls, GNUNET_NO, 421 mini->ac (mini->ac_cls, GNUNET_NO,
424 (const struct sockaddr *) &mini->current_addr, 422 (const struct sockaddr *) &mini->current_addr,
425 sizeof (mini->current_addr), 423 sizeof (mini->current_addr),
426 NULL); 424 GNUNET_NAT_ERROR_SUCCESS);
427 mini->current_addr.sin_addr = exip; 425 mini->current_addr.sin_addr = exip;
428 mini->ac (mini->ac_cls, GNUNET_YES, 426 mini->ac (mini->ac_cls, GNUNET_YES,
429 (const struct sockaddr *) &mini->current_addr, 427 (const struct sockaddr *) &mini->current_addr,
430 sizeof (mini->current_addr), 428 sizeof (mini->current_addr),
431 NULL); 429 GNUNET_NAT_ERROR_SUCCESS);
432 return; 430 return;
433 } 431 }
434 /* 432 /*
@@ -460,12 +458,12 @@ process_refresh_output (void *cls, const char *line)
460 mini->ac (mini->ac_cls, GNUNET_NO, 458 mini->ac (mini->ac_cls, GNUNET_NO,
461 (const struct sockaddr *) &mini->current_addr, 459 (const struct sockaddr *) &mini->current_addr,
462 sizeof (mini->current_addr), 460 sizeof (mini->current_addr),
463 NULL); 461 GNUNET_NAT_ERROR_SUCCESS);
464 mini->current_addr.sin_port = htons ((uint16_t) nport); 462 mini->current_addr.sin_port = htons ((uint16_t) nport);
465 mini->ac (mini->ac_cls, GNUNET_YES, 463 mini->ac (mini->ac_cls, GNUNET_YES,
466 (const struct sockaddr *) &mini->current_addr, 464 (const struct sockaddr *) &mini->current_addr,
467 sizeof (mini->current_addr), 465 sizeof (mini->current_addr),
468 NULL); 466 GNUNET_NAT_ERROR_SUCCESS);
469} 467}
470 468
471 469
@@ -509,7 +507,7 @@ do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
509 mini->ac (mini->ac_cls, 507 mini->ac (mini->ac_cls,
510 GNUNET_SYSERR, 508 GNUNET_SYSERR,
511 NULL, 0, 509 NULL, 0,
512 _("`upnpc' command took too long, process killed")); 510 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
513} 511}
514 512
515 513
@@ -537,7 +535,7 @@ process_map_output (void *cls,
537 mini->ac (mini->ac_cls, 535 mini->ac (mini->ac_cls,
538 GNUNET_SYSERR, 536 GNUNET_SYSERR,
539 NULL, 0, 537 NULL, 0,
540 _("`upnpc' command failed to establish port mapping")); 538 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
541 if (GNUNET_SCHEDULER_NO_TASK == mini->refresh_task) 539 if (GNUNET_SCHEDULER_NO_TASK == mini->refresh_task)
542 mini->refresh_task = 540 mini->refresh_task =
543 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); 541 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
@@ -572,7 +570,7 @@ process_map_output (void *cls,
572 mini->ac (mini->ac_cls, GNUNET_YES, 570 mini->ac (mini->ac_cls, GNUNET_YES,
573 (const struct sockaddr *) &mini->current_addr, 571 (const struct sockaddr *) &mini->current_addr,
574 sizeof (mini->current_addr), 572 sizeof (mini->current_addr),
575 NULL); 573 GNUNET_NAT_ERROR_SUCCESS);
576} 574}
577 575
578 576
@@ -605,7 +603,7 @@ GNUNET_NAT_mini_map_start (uint16_t port,
605 ac (ac_cls, 603 ac (ac_cls,
606 GNUNET_SYSERR, 604 GNUNET_SYSERR,
607 NULL, 0, 605 NULL, 0,
608 _("`upnpc` command not found")); 606 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
609 return NULL; 607 return NULL;
610 } 608 }
611 LOG (GNUNET_ERROR_TYPE_DEBUG, 609 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -682,7 +680,7 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
682 mini->ac (mini->ac_cls, GNUNET_NO, 680 mini->ac (mini->ac_cls, GNUNET_NO,
683 (const struct sockaddr *) &mini->current_addr, 681 (const struct sockaddr *) &mini->current_addr,
684 sizeof (mini->current_addr), 682 sizeof (mini->current_addr),
685 NULL); 683 GNUNET_NAT_ERROR_SUCCESS);
686 /* Note: oddly enough, deletion uses the external port whereas 684 /* Note: oddly enough, deletion uses the external port whereas
687 * addition uses the internal port; this rarely matters since they 685 * addition uses the internal port; this rarely matters since they
688 * often are the same, but it might... */ 686 * often are the same, but it might... */
diff --git a/src/nat/nat_test.c b/src/nat/nat_test.c
index 866c15e5e..7d9d1a49c 100644
--- a/src/nat/nat_test.c
+++ b/src/nat/nat_test.c
@@ -187,7 +187,7 @@ reversal_cb (void *cls,
187 return; /* wrong port */ 187 return; /* wrong port */
188 } 188 }
189 /* report success */ 189 /* report success */
190 h->report (h->report_cls, GNUNET_OK, NULL); 190 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
191} 191}
192 192
193 193
@@ -215,7 +215,7 @@ do_udp_read (void *cls,
215 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) 215 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
216 { 216 {
217 if (data == tst->data) 217 if (data == tst->data)
218 tst->report (tst->report_cls, GNUNET_OK, NULL); 218 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
219 else 219 else
220 LOG (GNUNET_ERROR_TYPE_DEBUG, 220 LOG (GNUNET_ERROR_TYPE_DEBUG,
221 "Received data mismatches expected value\n"); 221 "Received data mismatches expected value\n");
@@ -250,7 +250,7 @@ do_read (void *cls,
250 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) 250 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
251 { 251 {
252 if (data == tst->data) 252 if (data == tst->data)
253 tst->report (tst->report_cls, GNUNET_OK, NULL); 253 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
254 else 254 else
255 LOG (GNUNET_ERROR_TYPE_DEBUG, 255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Received data does not match expected value\n"); 256 "Received data does not match expected value\n");
diff --git a/src/transport/gnunet-transport.c b/src/transport/gnunet-transport.c
index 28c4c84fb..4487fc0e7 100644
--- a/src/transport/gnunet-transport.c
+++ b/src/transport/gnunet-transport.c
@@ -545,10 +545,10 @@ display_test_result (struct TestContext *tc, enum TestResult result)
545 * @param emsg error message, NULL on success 545 * @param emsg error message, NULL on success
546 */ 546 */
547static void 547static void
548result_callback (void *cls, int success, const char *emsg) 548result_callback (void *cls, enum GNUNET_NAT_FailureCode result)
549{ 549{
550 struct TestContext *tc = cls; 550 struct TestContext *tc = cls;
551 display_test_result (tc, success); 551 display_test_result (tc, result);
552} 552}
553 553
554/** 554/**