GNUnet  0.10.x
Typedefs | Enumerations | Functions
NAT testing library

Service for handling UPnP and NAT-PMP port forwarding and external IP address retrieval. More...

Typedefs

typedef void(* GNUNET_NAT_AUTO_AutoResultCallback) (void *cls, const struct GNUNET_CONFIGURATION_Handle *diff, enum GNUNET_NAT_StatusCode result, enum GNUNET_NAT_Type type)
 Function called with the result from the autoconfiguration. More...
 
typedef void(* GNUNET_NAT_AddressCallback) (void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
 Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes. More...
 
typedef void(* GNUNET_NAT_ReversalCallback) (void *cls, const struct sockaddr *remote_addr, socklen_t remote_addrlen)
 Signature of the callback passed to GNUNET_NAT_register(). More...
 
typedef void(* GNUNET_NAT_TestCallback) (void *cls, enum GNUNET_NAT_StatusCode result)
 Function called to report success or failure for NAT configuration test. More...
 

Enumerations

enum  GNUNET_NAT_AddressClass {
  GNUNET_NAT_AC_NONE = 0, GNUNET_NAT_AC_OTHER = 1, GNUNET_NAT_AC_PRIVATE = 2, GNUNET_NAT_AC_GLOBAL = 4,
  GNUNET_NAT_AC_GLOBAL_PRIVATE = 6, GNUNET_NAT_AC_LAN = 8, GNUNET_NAT_AC_LAN_PRIVATE = 10, GNUNET_NAT_AC_WLAN = 16,
  GNUNET_NAT_AC_BT = 32, GNUNET_NAT_AC_LOOPBACK = 64, GNUNET_NAT_AC_EXTERN = 128, GNUNET_NAT_AC_MANUAL = 256,
  GNUNET_NAT_AC_ANY = 65535
}
 Some addresses contain sensitive information or are not suitable for global distribution. More...
 
enum  GNUNET_NAT_StatusCode {
  GNUNET_NAT_ERROR_SUCCESS = 1, GNUNET_NAT_ERROR_IPC_FAILURE, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR, GNUNET_NAT_ERROR_TIMEOUT,
  GNUNET_NAT_ERROR_NOT_ONLINE, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND, GNUNET_NAT_ERROR_UPNPC_FAILED, GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
  GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED, GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND, GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED, GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
  GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID, GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO, GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND, GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
  GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT, GNUNET_NAT_ERROR_NAT_REGISTER_FAILED, GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND
}
 Error Types for the NAT subsystem (which can then later be converted/resolved to a string) More...
 
enum  GNUNET_NAT_Type {
  GNUNET_NAT_TYPE_NO_NAT = 1, GNUNET_NAT_TYPE_UNREACHABLE_NAT, GNUNET_NAT_TYPE_STUN_PUNCHED_NAT, GNUNET_NAT_TYPE_UPNP_NAT,
  GNUNET_NAT_TYPE_UNKNOWN
}
 What the situation of the NAT connectivity. More...
 

Functions

struct GNUNET_NAT_AUTO_TestGNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, uint8_t proto, const char *section_name, GNUNET_NAT_TestCallback report, void *report_cls)
 Start testing if NAT traversal works using the given configuration. More...
 
void GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst)
 Stop an active NAT test. More...
 
const char * GNUNET_NAT_AUTO_status2string (enum GNUNET_NAT_StatusCode err)
 Converts enum GNUNET_NAT_StatusCode to string. More...
 
struct GNUNET_NAT_AUTO_AutoHandleGNUNET_NAT_AUTO_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_NAT_AUTO_AutoResultCallback cb, void *cb_cls)
 Start auto-configuration routine. More...
 
void GNUNET_NAT_AUTO_autoconfig_cancel (struct GNUNET_NAT_AUTO_AutoHandle *ah)
 Abort autoconfiguration. More...
 
struct GNUNET_NAT_HandleGNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
 Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on the local network. More...
 
int GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh, const void *addr, socklen_t addrlen)
 Test if the given address is (currently) a plausible IP address for this peer. More...
 
int GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, const struct sockaddr_in *local_sa, const struct sockaddr_in *remote_sa)
 We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP responses to cause that peer to connect to us (connection reversal). More...
 
void GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh)
 Stop port redirection and public IP address detection for the given handle. More...
 
int GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh, const struct sockaddr *sender_addr, size_t sender_addr_len, const void *data, size_t data_size)
 Handle an incoming STUN message. More...
 
struct GNUNET_NAT_STUN_HandleGNUNET_NAT_stun_make_request (const char *server, uint16_t port, struct GNUNET_NETWORK_Handle *sock, GNUNET_NAT_TestCallback cb, void *cb_cls)
 Make Generic STUN request. More...
 
void GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh)
 Cancel active STUN request. More...
 

Detailed Description

Service for handling UPnP and NAT-PMP port forwarding and external IP address retrieval.

Typedef Documentation

◆ GNUNET_NAT_AUTO_AutoResultCallback

typedef void(* GNUNET_NAT_AUTO_AutoResultCallback) (void *cls, const struct GNUNET_CONFIGURATION_Handle *diff, enum GNUNET_NAT_StatusCode result, enum GNUNET_NAT_Type type)

Function called with the result from the autoconfiguration.

Parameters
clsclosure
diffminimal suggested changes to the original configuration to make it work (as best as we can)
resultGNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
typewhat the situation of the NAT

Definition at line 101 of file gnunet_nat_auto_service.h.

◆ GNUNET_NAT_AddressCallback

typedef void(* GNUNET_NAT_AddressCallback) (void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)

Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes.

Parameters
clsclosure
app_ctx[in,out]location where the app can store stuff on add and retrieve it on remove
add_removeGNUNET_YES to add a new public IP address, GNUNET_NO to remove a previous (now invalid) one
acaddress class the address belongs to
addreither the previous or the new public IP address
addrlenactual length of the addr

Definition at line 284 of file gnunet_nat_service.h.

◆ GNUNET_NAT_ReversalCallback

typedef void(* GNUNET_NAT_ReversalCallback) (void *cls, const struct sockaddr *remote_addr, socklen_t remote_addrlen)

Signature of the callback passed to GNUNET_NAT_register().

for a function to call whenever someone asks us to do connection reversal.

Parameters
clsclosure
remote_addrpublic IP address of the other peer
remote_addrlenactual length of the remote_addr

Definition at line 302 of file gnunet_nat_service.h.

◆ GNUNET_NAT_TestCallback

typedef void(* GNUNET_NAT_TestCallback) (void *cls, enum GNUNET_NAT_StatusCode result)

Function called to report success or failure for NAT configuration test.

Parameters
clsclosure
resultGNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code

Definition at line 440 of file gnunet_nat_service.h.

Enumeration Type Documentation

◆ GNUNET_NAT_AddressClass

Some addresses contain sensitive information or are not suitable for global distribution.

We use address classes to filter addresses by which domain they make sense to be used in. These are used in a bitmask.

FIXME: might want to define this elsewhere; we have an equivalent enum in gnunet_transport_hello_service.h; might ultimately belong with the new HELLO definition.

Enumerator
GNUNET_NAT_AC_NONE 

No address.

GNUNET_NAT_AC_OTHER 

Addresses that fall into no other category (i.e.

incoming which we cannot use elsewhere).

GNUNET_NAT_AC_PRIVATE 

Flag for addresses that are highly sensitive (i.e.

IPv6 with our MAC).

GNUNET_NAT_AC_GLOBAL 

Addresses that are global (i.e.

IPv4).

GNUNET_NAT_AC_GLOBAL_PRIVATE 

Addresses that are global and are sensitive (i.e.

IPv6 with our MAC).

GNUNET_NAT_AC_LAN 

Addresses useful in the local wired network, i.e.

a MAC. Sensitive, but obvious to people nearby.

Useful for broadcasts.

GNUNET_NAT_AC_LAN_PRIVATE 

Addresses useful in the local wired network, i.e.

a MAC. Sensitive, but obvious to people nearby. Useful for broadcasts.

GNUNET_NAT_AC_WLAN 

Addresses useful in the local wireless network, i.e.

a MAC. Sensitive, but obvious to people nearby. Useful for broadcasts.

GNUNET_NAT_AC_BT 

Addresses useful in the local bluetooth network.

Sensitive, but obvious to people nearby. Useful for broadcasts.

GNUNET_NAT_AC_LOOPBACK 

Loopback addresses, only useful under special cirumstances.

GNUNET_NAT_AC_EXTERN 

Addresses that should be our external IP address on the outside of a NAT.

Might be incorrectly determined. Used as a bit in combination with GNUNET_NAT_AC_GLOBAL, or in case of double-NAT with GNUNET_NAT_AC_LAN.

GNUNET_NAT_AC_MANUAL 

Addresses that were manually configured by the user.

Used as a bit in combination with GNUNET_NAT_AC_GLOBAL.

GNUNET_NAT_AC_ANY 

Bitmask for "any" address.

Definition at line 52 of file gnunet_nat_service.h.

52  {
57 
63 
69 
74 
80 
88 
95 
101  GNUNET_NAT_AC_WLAN = 16,
102 
107  GNUNET_NAT_AC_BT = 32,
108 
113 
121  GNUNET_NAT_AC_EXTERN = 128,
122 
127  GNUNET_NAT_AC_MANUAL = 256,
128 
132  GNUNET_NAT_AC_ANY = 65535
133 };
Addresses that were manually configured by the user.
Bitmask for "any" address.
Addresses that should be our external IP address on the outside of a NAT.
Addresses useful in the local wired network, i.e.
Addresses useful in the local bluetooth network.
Loopback addresses, only useful under special cirumstances.
Addresses that are global (i.e.
Addresses useful in the local wired network, i.e.
Flag for addresses that are highly sensitive (i.e.
Addresses that fall into no other category (i.e.
Addresses that are global and are sensitive (i.e.
Addresses useful in the local wireless network, i.e.

◆ GNUNET_NAT_StatusCode

Error Types for the NAT subsystem (which can then later be converted/resolved to a string)

Enumerator
GNUNET_NAT_ERROR_SUCCESS 

Just the default.

GNUNET_NAT_ERROR_IPC_FAILURE 

IPC Failure.

GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR 

Failure in network subsystem, check permissions.

GNUNET_NAT_ERROR_TIMEOUT 

test timed out

GNUNET_NAT_ERROR_NOT_ONLINE 

detected that we are offline

GNUNET_NAT_ERROR_UPNPC_NOT_FOUND 

upnpc command not found

GNUNET_NAT_ERROR_UPNPC_FAILED 

Failed to run upnpc command.

GNUNET_NAT_ERROR_UPNPC_TIMEOUT 

`upnpc' command took too long, process killed

GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED 

`upnpc' command failed to establish port mapping

GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND 

`external-ip' command not found

GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED 

Failed to run external-ip command.

GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID 

`external-ip' command output invalid

GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID 

"no valid address was returned by `external-ip'"

GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO 

Could not determine interface with internal/local network address.

GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND 

No working gnunet-helper-nat-server found.

GNUNET_NAT_ERROR_NAT_TEST_START_FAILED 

NAT test could not be initialized.

GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT 

NAT test timeout.

GNUNET_NAT_ERROR_NAT_REGISTER_FAILED 

NAT test failed to initiate.

GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND 

Definition at line 139 of file gnunet_nat_service.h.

139  {
144 
149 
154 
159 
164 
169 
174 
179 
184 
189 
194 
199 
204 
209 
214 
219 
224 
229 
234 };
NAT test failed to initiate.
detected that we are offline
`upnpc' command failed to establish port mapping
`upnpc' command took too long, process killed
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
`external-ip' command output invalid
Could not determine interface with internal/local network address.
No working gnunet-helper-nat-server found.
Failure in network subsystem, check permissions.
NAT test could not be initialized.
`external-ip' command not found
Failed to run upnpc command.
"no valid address was returned by `external-ip'"
Failed to run external-ip command.

◆ GNUNET_NAT_Type

What the situation of the NAT connectivity.

Enumerator
GNUNET_NAT_TYPE_NO_NAT 

We have a direct connection.

GNUNET_NAT_TYPE_UNREACHABLE_NAT 

We are under a NAT but cannot traverse it.

GNUNET_NAT_TYPE_STUN_PUNCHED_NAT 

We can traverse using STUN.

GNUNET_NAT_TYPE_UPNP_NAT 

We can traverse using UPNP.

GNUNET_NAT_TYPE_UNKNOWN 

We know nothing about the NAT.

Definition at line 241 of file gnunet_nat_service.h.

241  {
246 
251 
256 
261 
266 };
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
We are under a NAT but cannot traverse it.
We know nothing about the NAT.
We can traverse using UPNP.
We have a direct connection.
We can traverse using STUN.

Function Documentation

◆ GNUNET_NAT_AUTO_test_start()

struct GNUNET_NAT_AUTO_Test* GNUNET_NAT_AUTO_test_start ( const struct GNUNET_CONFIGURATION_Handle cfg,
uint8_t  proto,
const char *  section_name,
GNUNET_NAT_TestCallback  report,
void *  report_cls 
)

Start testing if NAT traversal works using the given configuration.

The transport adapters should be down while using this function.

Parameters
cfgconfiguration for the NAT traversal
protoprotocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
section_nameconfiguration section to use for configuration
reportfunction to call with the result of the test
report_clsclosure for report
Returns
handle to cancel NAT test

Definition at line 423 of file nat_auto_api_test.c.

References _, addr_cb(), GNUNET_NAT_AUTO_Test::cfg, cfg, do_accept(), do_fail(), do_udp_read(), GNUNET_a2s(), GNUNET_break, GNUNET_CONFIGURATION_get_value_number(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR, GNUNET_NAT_ERROR_NAT_REGISTER_FAILED, GNUNET_NAT_ERROR_SUCCESS, GNUNET_NAT_register(), GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NETWORK_socket_listen(), GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_cancel(), GNUNET_strdup, GNUNET_TIME_UNIT_FOREVER_REL, LOG, GNUNET_NAT_AUTO_Test::lsock, GNUNET_NAT_AUTO_Test::ltask, GNUNET_NAT_AUTO_Test::nat, nh, proto, GNUNET_NAT_AUTO_Test::proto, GNUNET_NAT_AUTO_Test::report, GNUNET_NAT_AUTO_Test::report_cls, reversal_cb(), GNUNET_NAT_AUTO_Test::section_name, GNUNET_NAT_AUTO_Test::status, and GNUNET_NAT_AUTO_Test::ttask.

Referenced by run().

428 {
429  struct GNUNET_NAT_AUTO_Test *nh;
430  unsigned long long bnd_port;
431  struct sockaddr_in sa;
432  const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa };
433  const socklen_t addrlens[] = { sizeof(sa) };
434 
436  section_name,
437  "PORT",
438  &bnd_port)) ||
439  (bnd_port > 65535))
440  {
442  _("Failed to find valid PORT in section `%s'\n"),
443  section_name);
444  return NULL;
445  }
446 
447  memset(&sa, 0, sizeof(sa));
448  sa.sin_family = AF_INET;
449  sa.sin_port = htons((uint16_t)bnd_port);
450 #if HAVE_SOCKADDR_IN_SIN_LEN
451  sa.sin_len = sizeof(sa);
452 #endif
453 
454  nh = GNUNET_new(struct GNUNET_NAT_AUTO_Test);
455  nh->cfg = cfg;
456  nh->proto = proto;
458  nh->report = report;
459  nh->report_cls = report_cls;
461  if (0 == bnd_port)
462  {
463  nh->nat = GNUNET_NAT_register(cfg,
464  section_name,
465  proto,
466  0,
467  NULL,
468  NULL,
469  &addr_cb,
470  &reversal_cb,
471  nh);
472  }
473  else
474  {
475  nh->lsock =
477  (IPPROTO_UDP == proto) ? SOCK_DGRAM
478  : SOCK_STREAM,
479  proto);
480  if ((NULL == nh->lsock) ||
482  (const struct sockaddr *)&sa,
483  sizeof(sa))))
484  {
486  _("Failed to create socket bound to `%s' for NAT test: %s\n"),
487  GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)),
488  strerror(errno));
489  if (NULL != nh->lsock)
490  {
492  nh->lsock = NULL;
493  }
496  return nh;
497  }
498  if (IPPROTO_TCP == proto)
499  {
502  nh->lsock,
503  &do_accept,
504  nh);
505  }
506  else
507  {
509  nh->lsock,
510  &do_udp_read,
511  nh);
512  }
514  "NAT test listens on port %llu (%s)\n",
515  bnd_port,
516  (IPPROTO_TCP == proto) ? "tcp" : "udp");
517  nh->nat = GNUNET_NAT_register(cfg,
518  section_name,
519  proto,
520  1,
521  addrs,
522  addrlens,
523  &addr_cb,
524  NULL,
525  nh);
526  if (NULL == nh->nat)
527  {
529  _("NAT test failed to start NAT library\n"));
530  if (NULL != nh->ltask)
531  {
533  nh->ltask = NULL;
534  }
535  if (NULL != nh->lsock)
536  {
538  nh->lsock = NULL;
539  }
542  return nh;
543  }
544  }
545  return nh;
546 }
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:696
NAT test failed to initiate.
char * section_name
Section name of plugin to test.
GNUNET_NAT_TestCallback report
Function to call with success report.
struct GNUNET_NAT_Handle * nat
Handle to NAT traversal in use.
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
#define LOG(kind,...)
static void reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Function called from GNUNET_NAT_register whenever someone asks us to do connection reversal...
static uint8_t proto
Protocol to use.
Definition: gnunet-nat.c:60
static void do_udp_read(void *cls)
Activity on our incoming socket.
int GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:474
Handle to a NAT test.
void * report_cls
Closure for report.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static void addr_cb(void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
Address-callback, used to send message to gnunet-nat-server.
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
static void do_accept(void *cls)
Activity on our listen socket.
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
Failure in network subsystem, check permissions.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static void do_fail(void *cls)
Calls the report-callback reporting failure.
struct GNUNET_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition: nat_api.c:376
enum GNUNET_NAT_StatusCode status
Status code to be reported to the timeout/status call.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration used.
static char * section_name
Name of section in configuration file to use for additional options.
Definition: gnunet-nat.c:40
#define GNUNET_log(kind,...)
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
int proto
IPPROTO_TCP or IPPROTO_UDP.
struct GNUNET_SCHEDULER_Task * ttask
Task identifier for the timeout (if any)
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:900
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
struct GNUNET_NETWORK_Handle * lsock
Handle to listen socket, or NULL.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_AUTO_test_stop()

void GNUNET_NAT_AUTO_test_stop ( struct GNUNET_NAT_AUTO_Test tst)

Stop an active NAT test.

Parameters
tsttest to stop.

Definition at line 555 of file nat_auto_api_test.c.

References GNUNET_NAT_AUTO_Test::ca_head, GNUNET_NAT_AUTO_Test::ca_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_MQ_destroy(), GNUNET_NAT_unregister(), GNUNET_NETWORK_socket_close(), GNUNET_SCHEDULER_cancel(), LOG, GNUNET_NAT_AUTO_Test::lsock, GNUNET_NAT_AUTO_Test::ltask, ClientActivity::mq, GNUNET_NAT_AUTO_Test::na_head, GNUNET_NAT_AUTO_Test::na_tail, GNUNET_NAT_AUTO_Test::nat, NatActivity::rtask, GNUNET_NAT_AUTO_Test::section_name, NatActivity::sock, and GNUNET_NAT_AUTO_Test::ttask.

Referenced by do_shutdown().

556 {
557  struct NatActivity *pos;
558  struct ClientActivity *cpos;
559 
560  LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
561  while (NULL != (cpos = tst->ca_head))
562  {
563  GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos);
564  GNUNET_MQ_destroy(cpos->mq);
565  GNUNET_free(cpos);
566  }
567  while (NULL != (pos = tst->na_head))
568  {
572  GNUNET_free(pos);
573  }
574  if (NULL != tst->ttask)
575  {
577  tst->ttask = NULL;
578  }
579  if (NULL != tst->ltask)
580  {
582  tst->ltask = NULL;
583  }
584  if (NULL != tst->lsock)
585  {
587  tst->lsock = NULL;
588  }
589  if (NULL != tst->nat)
590  {
592  tst->nat = NULL;
593  }
595  GNUNET_free(tst);
596 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
char * section_name
Section name of plugin to test.
struct GNUNET_NAT_Handle * nat
Handle to NAT traversal in use.
#define LOG(kind,...)
struct ClientActivity * ca_tail
Tail of list of client activities.
struct GNUNET_MQ_Handle * mq
Socket of the incoming connection.
struct GNUNET_NETWORK_Handle * sock
Socket of the incoming connection.
struct NatActivity * na_head
Head of list of nat activities.
struct GNUNET_SCHEDULER_Task * rtask
Task reading from the incoming connection.
Entry we keep for each incoming connection.
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:690
struct ClientActivity * ca_head
Head of list of client activities.
struct NatActivity * na_tail
Tail of list of nat activities.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
Entry we keep for each connection to the gnunet-nat-service.
struct GNUNET_SCHEDULER_Task * ttask
Task identifier for the timeout (if any)
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
struct GNUNET_NETWORK_Handle * lsock
Handle to listen socket, or NULL.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_AUTO_status2string()

const char* GNUNET_NAT_AUTO_status2string ( enum GNUNET_NAT_StatusCode  err)

Converts enum GNUNET_NAT_StatusCode to string.

Parameters
errerror code to resolve to a string
Returns
point to a static string containing the error code

Definition at line 68 of file nat_auto_api.c.

References _, GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID, GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED, GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND, GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID, GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND, GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR, GNUNET_NAT_ERROR_IPC_FAILURE, GNUNET_NAT_ERROR_NAT_REGISTER_FAILED, GNUNET_NAT_ERROR_NAT_TEST_START_FAILED, GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT, GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO, GNUNET_NAT_ERROR_NOT_ONLINE, GNUNET_NAT_ERROR_SUCCESS, GNUNET_NAT_ERROR_TIMEOUT, GNUNET_NAT_ERROR_UPNPC_FAILED, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND, GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED, and GNUNET_NAT_ERROR_UPNPC_TIMEOUT.

Referenced by auto_config_cb(), and test_report_cb().

69 {
70  switch (err)
71  {
73  return _("Operation Successful");
74 
76  return _("IPC failure");
77 
79  return _("Failure in network subsystem, check permissions.");
80 
82  return _("Encountered timeout while performing operation");
83 
85  return _("detected that we are offline");
86 
88  return _("`upnpc` command not found");
89 
91  return _("Failed to run `upnpc` command");
92 
94  return _("`upnpc' command took too long, process killed");
95 
97  return _("`upnpc' command failed to establish port mapping");
98 
100  return _("`external-ip' command not found");
101 
103  return _("Failed to run `external-ip` command");
104 
106  return _("`external-ip' command output invalid");
107 
109  return _("no valid address was returned by `external-ip'");
110 
112  return _("Could not determine interface with internal/local network address");
113 
115  return _("No functioning gnunet-helper-nat-server installation found");
116 
118  return _("NAT test could not be initialized");
119 
121  return _("NAT test timeout reached");
122 
124  return _("could not register NAT");
125 
127  return _("No working gnunet-helper-nat-client installation found");
128 
129  default:
130  return "unknown status code";
131  }
132 }
NAT test failed to initiate.
detected that we are offline
`upnpc' command failed to establish port mapping
`upnpc' command took too long, process killed
`external-ip' command output invalid
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
Could not determine interface with internal/local network address.
No working gnunet-helper-nat-server found.
Failure in network subsystem, check permissions.
NAT test could not be initialized.
`external-ip' command not found
Failed to run upnpc command.
"no valid address was returned by `external-ip'"
Failed to run external-ip command.
Here is the caller graph for this function:

◆ GNUNET_NAT_AUTO_autoconfig_start()

struct GNUNET_NAT_AUTO_AutoHandle* GNUNET_NAT_AUTO_autoconfig_start ( const struct GNUNET_CONFIGURATION_Handle cfg,
GNUNET_NAT_AUTO_AutoResultCallback  cb,
void *  cb_cls 
)

Start auto-configuration routine.

The transport adapters should be stopped while this function is called.

Parameters
cfginitial configuration
cbfunction to call with autoconfiguration result
cb_clsclosure for cb
Returns
handle to cancel operation

Definition at line 224 of file nat_auto_api.c.

References ah, ah_error_handler(), GNUNET_NAT_AUTO_AutoHandle::arc, GNUNET_NAT_AUTO_AutoHandle::arc_cls, buf, env, GNUNET_break, GNUNET_CLIENT_connect(), GNUNET_CONFIGURATION_serialize(), GNUNET_free, GNUNET_MAX_MESSAGE_SIZE, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT, GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_new, GNUNET_NAT_AUTO_AutoHandle::mq, and size.

Referenced by run().

227 {
230  GNUNET_MQ_hd_var_size(auto_result,
233  ah),
235  };
236  struct GNUNET_MQ_Envelope *env;
238  char *buf;
239  size_t size;
240 
242  &size);
243  if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req))
244  {
245  GNUNET_break(0);
246  GNUNET_free(buf);
247  GNUNET_free(ah);
248  return NULL;
249  }
250  ah->arc = cb;
251  ah->arc_cls = cb_cls;
252  ah->mq = GNUNET_CLIENT_connect(cfg,
253  "nat",
254  handlers,
256  ah);
257  if (NULL == ah->mq)
258  {
259  GNUNET_break(0);
260  GNUNET_free(buf);
261  GNUNET_free(ah);
262  return NULL;
263  }
264  env = GNUNET_MQ_msg_extra(req,
265  size,
267  GNUNET_memcpy(&req[1],
268  buf,
269  size);
270  GNUNET_free(buf);
271  GNUNET_MQ_send(ah->mq,
272  env);
273  return ah;
274 }
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
Message from NAT service with the autoconfiguration result.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:900
GNUNET_NAT_AUTO_AutoResultCallback arc
Function called with the result from the autoconfiguration.
Definition: nat_auto_api.c:52
Client requesting automatic configuration.
Definition: nat-auto.h:70
Service responding with proposed configuration.
Definition: nat-auto.h:83
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
static char buf[2048]
Message handler for a specific message type.
static unsigned int size
Size of the "table".
Definition: peer.c:66
Handle to auto-configuration in progress.
Definition: nat_auto_api.c:38
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG
Message to ask NAT service to request autoconfiguration.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
char * GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
Serializes the given configuration.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
static void ah_error_handler(void *cls, enum GNUNET_MQ_Error error)
Handle queue errors by reporting autoconfiguration failure.
Definition: nat_auto_api.c:201
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_auto_api.c:47
void * arc_cls
Closure for arc.
Definition: nat_auto_api.c:57
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_AUTO_autoconfig_cancel()

void GNUNET_NAT_AUTO_autoconfig_cancel ( struct GNUNET_NAT_AUTO_AutoHandle ah)

Abort autoconfiguration.

Parameters
ahhandle for operation to abort

Definition at line 283 of file nat_auto_api.c.

References GNUNET_free, GNUNET_MQ_destroy(), and GNUNET_NAT_AUTO_AutoHandle::mq.

Referenced by ah_error_handler(), do_shutdown(), and handle_auto_result().

284 {
285  GNUNET_MQ_destroy(ah->mq);
286  GNUNET_free(ah);
287 }
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_auto_api.c:47
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_register()

struct GNUNET_NAT_Handle* GNUNET_NAT_register ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  config_section,
uint8_t  proto,
unsigned int  num_addrs,
const struct sockaddr **  addrs,
const socklen_t *  addrlens,
GNUNET_NAT_AddressCallback  address_callback,
GNUNET_NAT_ReversalCallback  reversal_callback,
void *  callback_cls 
)

Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on the local network.

Use addr to specify to which of the local host's addresses should the external port be mapped. The port is taken from the corresponding sockaddr_in[6] field. The NAT module should call the given address_callback for any 'plausible' external address.

Parameters
cfgconfiguration to use
config_sectionname of the configuration section for options
protoprotocol this is about, IPPROTO_TCP or IPPROTO_UDP
num_addrsnumber of addresses in addrs
addrslist of local addresses packets should be redirected to
addrlensactual lengths of the addresses in addrs
address_callbackfunction to call everytime the public IP address changes
reversal_callbackfunction to call if someone wants connection reversal from us, NULL if connection reversal is not supported
callback_clsclosure for callbacks
Returns
NULL on error, otherwise handle that can be used to unregister

Use addr to specify to which of the local host's addresses should the external port be mapped. The port is taken from the corresponding sockaddr_in[6] field. The NAT module should call the given address_callback for any 'plausible' external address.

Parameters
cfgconfiguration to use
config_sectionname of the configuration section for optionsx
protoprotocol this is about, IPPROTO_TCP or IPPROTO_UDP
num_addrsnumber of addresses in addrs
addrslist of local addresses packets should be redirected to
addrlensactual lengths of the addresses in addrs
address_callbackfunction to call everytime the public IP address changes
reversal_callbackfunction to call if someone wants connection reversal from us, NULL if connection reversal is not supported
callback_clsclosure for callbacks
Returns
NULL on error, otherwise handle that can be used to unregister

Definition at line 376 of file nat_api.c.

References GNUNET_NAT_Handle::address_callback, GNUNET_NAT_Handle::callback_cls, GNUNET_NAT_Handle::cfg, cfg, do_connect(), GNUNET_NAT_RegisterMessage::flags, GNUNET_break, GNUNET_free, GNUNET_malloc, GNUNET_MAX_MESSAGE_SIZE, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_NAT_REGISTER, GNUNET_NAT_RF_ADDRESSES, GNUNET_NAT_RF_NONE, GNUNET_NAT_RF_REVERSAL, GNUNET_new, GNUNET_NAT_RegisterMessage::header, len, nh, GNUNET_NAT_RegisterMessage::num_addrs, proto, GNUNET_NAT_RegisterMessage::proto, GNUNET_NAT_Handle::reg, GNUNET_NAT_Handle::reversal_callback, reversal_callback(), GNUNET_MessageHeader::size, GNUNET_NAT_RegisterMessage::str_len, and GNUNET_MessageHeader::type.

Referenced by GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_test_start(), libgnunet_plugin_transport_tcp_init(), run(), server_start_report_addresses(), setup_sockets(), and try_anat().

385 {
386  struct GNUNET_NAT_Handle *nh;
387  struct GNUNET_NAT_RegisterMessage *rm;
388  size_t len;
389  size_t str_len;
390  char *off;
391 
392  len = 0;
393  for (unsigned int i = 0; i < num_addrs; i++)
394  len += addrlens[i];
395  str_len = strlen(config_section) + 1;
396  len += str_len;
397  if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) ||
398  (num_addrs > UINT16_MAX))
399  {
400  GNUNET_break(0);
401  return NULL;
402  }
403  rm = GNUNET_malloc(sizeof(*rm) + len);
404  rm->header.size = htons(sizeof(*rm) + len);
407  if (NULL != address_callback)
409  if (NULL != reversal_callback)
411  rm->proto = proto;
412  rm->str_len = htons(str_len);
413  rm->num_addrs = htons((uint16_t)num_addrs);
414  off = (char *)&rm[1];
415  for (unsigned int i = 0; i < num_addrs; i++)
416  {
417  switch (addrs[i]->sa_family)
418  {
419  case AF_INET:
420  if (sizeof(struct sockaddr_in) != addrlens[i])
421  {
422  GNUNET_break(0);
423  GNUNET_free(rm);
424  return NULL;
425  }
426  break;
427 
428  case AF_INET6:
429  if (sizeof(struct sockaddr_in6) != addrlens[i])
430  {
431  GNUNET_break(0);
432  GNUNET_free(rm);
433  return NULL;
434  }
435  break;
436 
437 #if AF_UNIX
438  case AF_UNIX:
439  if (sizeof(struct sockaddr_un) != addrlens[i])
440  {
441  GNUNET_break(0);
442  GNUNET_free(rm);
443  return NULL;
444  }
445  break;
446 #endif
447  default:
448  GNUNET_break(0);
449  GNUNET_free(rm);
450  return NULL;
451  }
452  GNUNET_memcpy(off, addrs[i], addrlens[i]);
453  off += addrlens[i];
454  }
455  GNUNET_memcpy(off, config_section, str_len);
456 
457  nh = GNUNET_new(struct GNUNET_NAT_Handle);
458  nh->reg = &rm->header;
459  nh->cfg = cfg;
460  nh->address_callback = address_callback;
462  nh->callback_cls = callback_cls;
463  do_connect(nh);
464  return nh;
465 }
Handle for active NAT registrations.
Definition: nat_api.c:70
static uint8_t proto
Protocol to use.
Definition: gnunet-nat.c:60
Message sent by a client to register with its addresses.
Definition: nat.h:93
uint8_t proto
Client&#39;s IPPROTO, e.g.
Definition: nat.h:107
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t str_len
Number of bytes in the string that follow which specifies a section name in the configuration.
Definition: nat.h:113
struct GNUNET_MessageHeader * reg
Our registration message.
Definition: nat_api.c:84
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:109
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static void do_connect(void *cls)
Task to connect to the NAT service.
Definition: nat_api.c:326
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
static void reversal_callback(void *cls, const struct sockaddr_in *ra)
We got a connection reversal request from another peer.
uint16_t num_addrs
Number of addresses that this service is bound to that follow.
Definition: nat.h:120
uint8_t flags
An enum GNUNET_NAT_RegisterFlags.
Definition: nat.h:102
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
GNUNET_NAT_ReversalCallback reversal_callback
Function to call when another peer requests connection reversal.
Definition: nat_api.c:104
#define GNUNET_MESSAGE_TYPE_NAT_REGISTER
Message to ask NAT service to register a client.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_REGISTER.
Definition: nat.h:97
This client supports address reversal.
Definition: nat.h:86
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:99
This client wants to be informed about changes to our applicable addresses.
Definition: nat.h:81
#define GNUNET_malloc(size)
Wrapper around malloc.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_api.c:74
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
This client does not want any notifications.
Definition: nat.h:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_test_address()

int GNUNET_NAT_test_address ( struct GNUNET_NAT_Handle nh,
const void *  addr,
socklen_t  addrlen 
)

Test if the given address is (currently) a plausible IP address for this peer.

Mostly a convenience function so that clients do not have to explicitly track all IPs that the GNUNET_NAT_AddressCallback has returned so far.

Parameters
nhthe handle returned by register
addrIP address to test (IPv4 or IPv6)
addrlennumber of bytes in addr
Returns
GNUNET_YES if the address is plausible, GNUNET_NO if the address is not plausible, GNUNET_SYSERR if the address is malformed

Definition at line 623 of file nat_api.c.

References AddrEntry::addrlen, GNUNET_NAT_Handle::ae_head, GNUNET_break, GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, and AddrEntry::next.

Referenced by tcp_plugin_check_address(), and udp_plugin_check_address().

626 {
627  struct AddrEntry *ae;
628 
629  if ((addrlen != sizeof(struct sockaddr_in)) &&
630  (addrlen != sizeof(struct sockaddr_in6)))
631  {
632  GNUNET_break(0);
633  return GNUNET_SYSERR;
634  }
635  for (ae = nh->ae_head; NULL != ae; ae = ae->next)
636  if ((addrlen == ae->addrlen) && (0 == memcmp(addr, &ae[1], addrlen)))
637  return GNUNET_YES;
638  return GNUNET_NO;
639 }
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Entry in DLL of addresses of this peer.
Definition: nat_api.c:38
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct AddrEntry * next
DLL.
Definition: nat_api.c:42
#define GNUNET_YES
Definition: gnunet_common.h:77
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:63
Here is the caller graph for this function:

◆ GNUNET_NAT_request_reversal()

int GNUNET_NAT_request_reversal ( struct GNUNET_NAT_Handle nh,
const struct sockaddr_in *  local_sa,
const struct sockaddr_in *  remote_sa 
)

We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP responses to cause that peer to connect to us (connection reversal).

Parameters
nhhandle (used for configuration)
local_saour local address of the peer (IPv4-only)
remote_sathe remote address of the peer (IPv4-only)
Returns
GNUNET_SYSERR on error, GNUNET_NO if connection reversal is unavailable, GNUNET_OK otherwise (presumably in progress)

Definition at line 655 of file nat_api.c.

References buf, env, GNUNET_break, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_OK, GNUNET_SYSERR, GNUNET_NAT_RequestConnectionReversalMessage::local_addr_size, GNUNET_NAT_Handle::mq, and GNUNET_NAT_RequestConnectionReversalMessage::remote_addr_size.

Referenced by run(), tcp_plugin_get_session(), and try_anat().

658 {
659  struct GNUNET_MQ_Envelope *env;
661  char *buf;
662 
663  if (NULL == nh->mq)
664  return GNUNET_SYSERR;
665  GNUNET_break(AF_INET == local_sa->sin_family);
666  GNUNET_break(AF_INET == remote_sa->sin_family);
667  env =
669  2 * sizeof(struct sockaddr_in),
671  req->local_addr_size = htons(sizeof(struct sockaddr_in));
672  req->remote_addr_size = htons(sizeof(struct sockaddr_in));
673  buf = (char *)&req[1];
674  GNUNET_memcpy(buf, local_sa, sizeof(struct sockaddr_in));
675  buf += sizeof(struct sockaddr_in);
676  GNUNET_memcpy(buf, remote_sa, sizeof(struct sockaddr_in));
677  GNUNET_MQ_send(nh->mq, env);
678  return GNUNET_OK;
679 }
uint16_t remote_addr_size
Size of the remote address included, in NBO.
Definition: nat.h:171
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:79
Client asking the service to initiate connection reversal.
Definition: nat.h:157
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t local_addr_size
Size of the local address included, in NBO.
Definition: nat.h:166
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL
Message to ask NAT service to request connection reversal.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
static char buf[2048]
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_unregister()

void GNUNET_NAT_unregister ( struct GNUNET_NAT_Handle nh)

Stop port redirection and public IP address detection for the given handle.

This frees the handle, after having sent the needed commands to close open ports.

Parameters
nhthe handle to unregister

This frees the handle, after having sent the needed commands to close open ports.

Parameters
nhthe handle to stop

Definition at line 690 of file nat_api.c.

References GNUNET_free, GNUNET_MQ_destroy(), GNUNET_SCHEDULER_cancel(), GNUNET_NAT_Handle::mq, GNUNET_NAT_Handle::reconnect_task, and GNUNET_NAT_Handle::reg.

Referenced by do_shutdown(), GNUNET_NAT_AUTO_test_stop(), GNUNET_NAT_test_stop(), libgnunet_plugin_transport_tcp_done(), libgnunet_plugin_transport_tcp_init(), libgnunet_plugin_transport_udp_done(), libgnunet_plugin_transport_udp_init(), server_stop_report_addresses(), and try_anat().

691 {
692  if (NULL != nh->mq)
693  {
694  GNUNET_MQ_destroy(nh->mq);
695  nh->mq = NULL;
696  }
697  if (NULL != nh->reconnect_task)
698  {
700  nh->reconnect_task = NULL;
701  }
702  GNUNET_free(nh->reg);
703  GNUNET_free(nh);
704 }
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:79
struct GNUNET_MessageHeader * reg
Our registration message.
Definition: nat_api.c:84
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:114
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_stun_handle_packet()

int GNUNET_NAT_stun_handle_packet ( struct GNUNET_NAT_Handle nh,
const struct sockaddr *  sender_addr,
size_t  sender_addr_len,
const void *  data,
size_t  data_size 
)

Handle an incoming STUN message.

This function is useful as some GNUnet service may be listening on a UDP port and might thus receive STUN messages while trying to receive other data. In this case, this function can be used to process replies to STUN requests.

The function does some basic sanity checks on packet size and content, try to extract a bit of information.

At the moment this only processes BIND requests, and returns the externally visible address of the request to the rest of the NAT logic.

Parameters
nhhandle to the NAT service
sender_addraddress from which we got data
sender_addr_lennumber of bytes in sender_addr
datathe packet
data_sizenumber of bytes in data
Returns
GNUNET_OK on success GNUNET_NO if the packet is not a STUN packet GNUNET_SYSERR on internal error handling the packet

Definition at line 581 of file nat_api.c.

References buf, env, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, GNUNET_NAT_Handle::mq, GNUNET_NAT_HandleStunMessage::payload_size, GNUNET_NAT_HandleStunMessage::sender_addr_size, and test_stun_packet().

Referenced by do_udp_read(), stun_read_task(), and udp_select_read().

586 {
587  struct GNUNET_MQ_Envelope *env;
588  struct GNUNET_NAT_HandleStunMessage *hsn;
589  char *buf;
590 
592  return GNUNET_NO;
593  if (NULL == nh->mq)
594  return GNUNET_SYSERR;
595  env = GNUNET_MQ_msg_extra(hsn,
596  data_size + sender_addr_len,
598  hsn->sender_addr_size = htons((uint16_t)sender_addr_len);
599  hsn->payload_size = htons((uint16_t)data_size);
600  buf = (char *)&hsn[1];
601  GNUNET_memcpy(buf, sender_addr, sender_addr_len);
602  buf += sender_addr_len;
604  GNUNET_MQ_send(nh->mq, env);
605  return GNUNET_OK;
606 }
Client telling the service to (possibly) handle a STUN message.
Definition: nat.h:132
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:79
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t payload_size
Number of bytes of payload included, in NBO.
Definition: nat.h:146
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int test_stun_packet(const void *data, size_t len)
Check if an incoming message is a STUN message.
Definition: nat_api.c:477
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
uint16_t sender_addr_size
Size of the sender address included, in NBO.
Definition: nat.h:141
static char buf[2048]
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN
Message to ask NAT service to handle a STUN packet.
#define GNUNET_YES
Definition: gnunet_common.h:77
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_stun_make_request()

struct GNUNET_NAT_STUN_Handle* GNUNET_NAT_stun_make_request ( const char *  server,
uint16_t  port,
struct GNUNET_NETWORK_Handle sock,
GNUNET_NAT_TestCallback  cb,
void *  cb_cls 
)

Make Generic STUN request.

Sends a generic stun request to the server specified using the specified socket. If we do this, we need to watch for possible responses and call GNUNET_NAT_stun_handle_packet() on incoming packets.

Parameters
serverthe address of the stun server
portport of the stun server, in host byte order
sockthe socket used to send the request, must be a UDP socket
cbcallback in case of error
cb_clsclosure for cb
Returns
NULL on error

Sends a generic stun request to the server specified using the specified socket.

Parameters
serverthe address of the stun server
portport of the stun server, in host byte order
sockthe socket used to send the request
cbcallback in case of error
cb_clsclosure for cb
Returns
NULL on error

Definition at line 209 of file nat_api_stun.c.

References GNUNET_NAT_STUN_Handle::cb, GNUNET_NAT_STUN_Handle::cb_cls, GNUNET_NAT_STUN_Handle::dns_active, GNUNET_NAT_STUN_Handle::dns_success, GNUNET_NAT_stun_make_request_cancel(), GNUNET_new, GNUNET_NO, GNUNET_RESOLVER_ip_get(), GNUNET_strdup, port, GNUNET_NAT_STUN_Handle::sock, stun_dns_callback(), GNUNET_NAT_STUN_Handle::stun_port, GNUNET_NAT_STUN_Handle::stun_server, and TIMEOUT.

Referenced by test_stun().

214 {
215  struct GNUNET_NAT_STUN_Handle *rh;
216 
217  rh = GNUNET_new(struct GNUNET_NAT_STUN_Handle);
218  rh->sock = sock;
219  rh->cb = cb;
220  rh->cb_cls = cb_cls;
221  rh->stun_server = GNUNET_strdup(server);
222  rh->stun_port = port;
223  rh->dns_success = GNUNET_NO;
225  AF_INET,
226  TIMEOUT,
228  rh);
229  if (NULL == rh->dns_active)
230  {
232  return NULL;
233  }
234  return rh;
235 }
void GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh)
Cancel active STUN request.
Definition: nat_api_stun.c:245
void * cb_cls
Closure for cb.
Definition: nat_api_stun.c:80
Handle to a request given to the resolver.
Definition: nat_api_stun.c:56
#define GNUNET_NO
Definition: gnunet_common.h:78
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get(const char *hostname, int af, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_AddressCallback callback, void *callback_cls)
Convert a string to one or more IP addresses.
Definition: resolver_api.c:933
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct GNUNET_NETWORK_Handle * sock
Handle to the listen socket.
Definition: nat_api_stun.c:65
char * stun_server
Stun server address.
Definition: nat_api_stun.c:70
#define TIMEOUT
Definition: nat_api_stun.c:48
GNUNET_NAT_TestCallback cb
Function to call when a error occours.
Definition: nat_api_stun.c:75
uint16_t stun_port
STUN port.
Definition: nat_api_stun.c:90
int dns_success
Do we got a DNS resolution successfully?
Definition: nat_api_stun.c:85
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
static void stun_dns_callback(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Try to establish a connection given the specified address.
Definition: nat_api_stun.c:133
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
Definition: nat_api_stun.c:60
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_stun_make_request_cancel()

void GNUNET_NAT_stun_make_request_cancel ( struct GNUNET_NAT_STUN_Handle rh)

Cancel active STUN request.

Frees associated resources and ensures that the callback is no longer invoked.

Parameters
rhrequest to cancel

Definition at line 245 of file nat_api_stun.c.

References GNUNET_NAT_STUN_Handle::dns_active, GNUNET_free, GNUNET_RESOLVER_request_cancel(), and GNUNET_NAT_STUN_Handle::stun_server.

Referenced by GNUNET_NAT_stun_make_request(), and stun_dns_callback().

246 {
247  if (NULL != rh->dns_active)
248  {
250  rh->dns_active = NULL;
251  }
253  GNUNET_free(rh);
254 }
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
char * stun_server
Stun server address.
Definition: nat_api_stun.c:70
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
Definition: nat_api_stun.c:60
Here is the call graph for this function:
Here is the caller graph for this function: