GNUnet  0.20.0
NAT 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 = GNUNET_OK , 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 = GNUNET_OK , 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 285 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 303 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 447 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 circumstances.

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 53 of file gnunet_nat_service.h.

54 {
59 
65 
71 
76 
82 
90 
97 
103  GNUNET_NAT_AC_WLAN = 16,
104 
109  GNUNET_NAT_AC_BT = 32,
110 
115 
123  GNUNET_NAT_AC_EXTERN = 128,
124 
129  GNUNET_NAT_AC_MANUAL = 256,
130 
134  GNUNET_NAT_AC_ANY = 65535
135 };
@ GNUNET_NAT_AC_LAN_PRIVATE
Addresses useful in the local wired network, i.e.
@ GNUNET_NAT_AC_LAN
Addresses useful in the local wired network, i.e.
@ GNUNET_NAT_AC_GLOBAL_PRIVATE
Addresses that are global and are sensitive (i.e.
@ GNUNET_NAT_AC_GLOBAL
Addresses that are global (i.e.
@ GNUNET_NAT_AC_PRIVATE
Flag for addresses that are highly sensitive (i.e.
@ GNUNET_NAT_AC_EXTERN
Addresses that should be our external IP address on the outside of a NAT.
@ GNUNET_NAT_AC_ANY
Bitmask for "any" address.
@ GNUNET_NAT_AC_WLAN
Addresses useful in the local wireless network, i.e.
@ GNUNET_NAT_AC_OTHER
Addresses that fall into no other category (i.e.
@ GNUNET_NAT_AC_BT
Addresses useful in the local bluetooth network.
@ GNUNET_NAT_AC_NONE
No address.
@ GNUNET_NAT_AC_MANUAL
Addresses that were manually configured by the user.
@ GNUNET_NAT_AC_LOOPBACK
Loopback addresses, only useful under special circumstances.

◆ 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 141 of file gnunet_nat_service.h.

142 {
147 
152 
157 
162 
167 
172 
177 
182 
187 
192 
197 
202 
207 
212 
217 
222 
227 
232 
237 };
@ GNUNET_OK
@ GNUNET_NAT_ERROR_UPNPC_NOT_FOUND
upnpc command not found
@ GNUNET_NAT_ERROR_NOT_ONLINE
detected that we are offline
@ GNUNET_NAT_ERROR_UPNPC_TIMEOUT
‘upnpc’ command took too long, process killed
@ GNUNET_NAT_ERROR_SUCCESS
Just the default.
@ GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID
‘external-ip’ command output invalid
@ GNUNET_NAT_ERROR_UPNPC_FAILED
Failed to run upnpc command.
@ GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED
Failed to run external-ip command.
@ GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR
Failure in network subsystem, check permissions.
@ GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND
@ 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_NAT_REGISTER_FAILED
NAT test failed to initiate.
@ GNUNET_NAT_ERROR_IPC_FAILURE
IPC Failure.
@ 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_EXTERNAL_IP_ADDRESS_INVALID
"no valid address was returned by `external-ip'"
@ 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_TIMEOUT
test timed out

◆ 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 243 of file gnunet_nat_service.h.

244 {
249 
254 
259 
264 
269 };
@ GNUNET_NAT_TYPE_UPNP_NAT
We can traverse using UPNP.
@ GNUNET_NAT_TYPE_UNKNOWN
We know nothing about the NAT.
@ GNUNET_NAT_TYPE_UNREACHABLE_NAT
We are under a NAT but cannot traverse it.
@ GNUNET_NAT_TYPE_NO_NAT
We have a direct connection.
@ GNUNET_NAT_TYPE_STUN_PUNCHED_NAT
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 426 of file nat_auto_api_test.c.

431 {
432  struct GNUNET_NAT_AUTO_Test *nh;
433  unsigned long long bnd_port;
434  struct sockaddr_in sa;
435  const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
436  const socklen_t addrlens[] = { sizeof(sa) };
437 
439  section_name,
440  "PORT",
441  &bnd_port)) ||
442  (bnd_port > 65535))
443  {
445  _ ("Failed to find valid PORT in section `%s'\n"),
446  section_name);
447  return NULL;
448  }
449 
450  memset (&sa, 0, sizeof(sa));
451  sa.sin_family = AF_INET;
452  sa.sin_port = htons ((uint16_t) bnd_port);
453 #if HAVE_SOCKADDR_IN_SIN_LEN
454  sa.sin_len = sizeof(sa);
455 #endif
456 
458  nh->cfg = cfg;
459  nh->proto = proto;
460  nh->section_name = GNUNET_strdup (section_name);
461  nh->report = report;
462  nh->report_cls = report_cls;
463  nh->status = GNUNET_NAT_ERROR_SUCCESS;
464  if (0 == bnd_port)
465  {
466  nh->nat = GNUNET_NAT_register (cfg,
467  section_name,
468  proto,
469  0,
470  NULL,
471  NULL,
472  &addr_cb,
473  &reversal_cb,
474  nh);
475  }
476  else
477  {
478  nh->lsock =
480  (IPPROTO_UDP == proto) ? SOCK_DGRAM
481  : SOCK_STREAM,
482  proto);
483  if ((NULL == nh->lsock) ||
485  (const struct sockaddr *) &sa,
486  sizeof(sa))))
487  {
489  _ ("Failed to create socket bound to `%s' for NAT test: %s\n"),
490  GNUNET_a2s ((const struct sockaddr *) &sa, sizeof(sa)),
491  strerror (errno));
492  if (NULL != nh->lsock)
493  {
495  nh->lsock = NULL;
496  }
498  nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh);
499  return nh;
500  }
501  if (IPPROTO_TCP == proto)
502  {
505  nh->lsock,
506  &do_accept,
507  nh);
508  }
509  else
510  {
512  nh->lsock,
513  &do_udp_read,
514  nh);
515  }
517  "NAT test listens on port %llu (%s)\n",
518  bnd_port,
519  (IPPROTO_TCP == proto) ? "tcp" : "udp");
520  nh->nat = GNUNET_NAT_register (cfg,
521  section_name,
522  proto,
523  1,
524  addrs,
525  addrlens,
526  &addr_cb,
527  NULL,
528  nh);
529  if (NULL == nh->nat)
530  {
532  _ ("NAT test failed to start NAT library\n"));
533  if (NULL != nh->ltask)
534  {
535  GNUNET_SCHEDULER_cancel (nh->ltask);
536  nh->ltask = NULL;
537  }
538  if (NULL != nh->lsock)
539  {
541  nh->lsock = NULL;
542  }
544  nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail, nh);
545  return nh;
546  }
547  }
548  return nh;
549 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
static char * section_name
Name of section in configuration file to use for additional options.
Definition: gnunet-nat.c:40
static uint8_t proto
Protocol to use.
Definition: gnunet-nat.c:60
enum GNUNET_GenericReturnValue 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 GNUNET_log(kind,...)
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
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).
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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:366
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:509
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
enum GNUNET_GenericReturnValue 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:440
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:652
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:1299
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:1506
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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 void do_fail(void *cls)
Calls the report-callback reporting failure.
static void do_accept(void *cls)
Activity on our listen socket.
static void do_udp_read(void *cls)
Activity on our incoming socket.
#define LOG(kind,...)
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Handle to a NAT test.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_api.c:76

References _, addr_cb(), cfg, GNUNET_NAT_Handle::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, nh, proto, reversal_cb(), and section_name.

Referenced by run().

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 558 of file nat_auto_api_test.c.

559 {
560  struct NatActivity *pos;
561  struct ClientActivity *cpos;
562 
563  LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
564  while (NULL != (cpos = tst->ca_head))
565  {
566  GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
567  GNUNET_MQ_destroy (cpos->mq);
568  GNUNET_free (cpos);
569  }
570  while (NULL != (pos = tst->na_head))
571  {
572  GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
575  GNUNET_free (pos);
576  }
577  if (NULL != tst->ttask)
578  {
580  tst->ttask = NULL;
581  }
582  if (NULL != tst->ltask)
583  {
585  tst->ltask = NULL;
586  }
587  if (NULL != tst->lsock)
588  {
590  tst->lsock = NULL;
591  }
592  if (NULL != tst->nat)
593  {
594  GNUNET_NAT_unregister (tst->nat);
595  tst->nat = NULL;
596  }
597  GNUNET_free (tst->section_name);
598  GNUNET_free (tst);
599 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
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:674
Entry we keep for each connection to the gnunet-nat-service.
struct GNUNET_MQ_Handle * mq
Socket of the incoming connection.
char * section_name
Section name of plugin to test.
struct NatActivity * na_tail
Tail of list of nat activities.
struct GNUNET_NAT_Handle * nat
Handle to NAT traversal in use.
struct GNUNET_NETWORK_Handle * lsock
Handle to listen socket, or NULL.
struct NatActivity * na_head
Head of list of nat activities.
struct GNUNET_SCHEDULER_Task * ttask
Task identifier for the timeout (if any)
struct ClientActivity * ca_head
Head of list of client activities.
struct ClientActivity * ca_tail
Tail of list of client activities.
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
Entry we keep for each incoming connection.
struct GNUNET_SCHEDULER_Task * rtask
Task reading from the incoming connection.
struct GNUNET_NETWORK_Handle * sock
Socket of the incoming connection.

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().

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.

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 _ (
113  "Could not determine interface with internal/local network address");
114 
116  return _ ("No functioning gnunet-helper-nat-server installation found");
117 
119  return _ ("NAT test could not be initialized");
120 
122  return _ ("NAT test timeout reached");
123 
125  return _ ("could not register NAT");
126 
128  return _ ("No working gnunet-helper-nat-client installation found");
129 
130  default:
131  return "unknown status code";
132  }
133 }

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().

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 225 of file nat_auto_api.c.

228 {
229  struct GNUNET_NAT_AUTO_AutoHandle *ah = GNUNET_new (struct
232  GNUNET_MQ_hd_var_size (auto_result,
235  ah),
237  };
238  struct GNUNET_MQ_Envelope *env;
240  char *buf;
241  size_t size;
242 
244  &size);
245  if (size > GNUNET_MAX_MESSAGE_SIZE - sizeof(*req))
246  {
247  GNUNET_break (0);
248  GNUNET_free (buf);
249  GNUNET_free (ah);
250  return NULL;
251  }
252  ah->arc = cb;
253  ah->arc_cls = cb_cls;
255  "nat",
256  handlers,
258  ah);
259  if (NULL == ah->mq)
260  {
261  GNUNET_break (0);
262  GNUNET_free (buf);
263  GNUNET_free (ah);
264  return NULL;
265  }
266  env = GNUNET_MQ_msg_extra (req,
267  size,
269  GNUNET_memcpy (&req[1],
270  buf,
271  size);
272  GNUNET_free (buf);
273  GNUNET_MQ_send (ah->mq,
274  env);
275  return ah;
276 }
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
static char buf[2048]
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
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:1057
char * GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
Serializes the given configuration.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#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:63
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
Message from NAT service with the autoconfiguration result.
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG
Message to ask NAT service to request 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:202
static unsigned int size
Size of the "table".
Definition: peer.c:68
Message handler for a specific message type.
Handle to auto-configuration in progress.
Definition: nat_auto_api.c:38
void * arc_cls
Closure for arc.
Definition: nat_auto_api.c:57
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_auto_api.c:47
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:71
Service responding with proposed configuration.
Definition: nat-auto.h:85

References ah, ah_error_handler(), GNUNET_NAT_AUTO_AutoHandle::arc, GNUNET_NAT_AUTO_AutoHandle::arc_cls, buf, cfg, 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, handlers, GNUNET_NAT_AUTO_AutoHandle::mq, and size.

Referenced by run().

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 285 of file nat_auto_api.c.

286 {
288  GNUNET_free (ah);
289 }

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

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

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 every time 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 366 of file nat_api.c.

375 {
376  struct GNUNET_NAT_Handle *nh;
377  struct GNUNET_NAT_RegisterMessage *rm;
378  size_t len;
379  size_t str_len;
380  char *off;
381 
382  len = 0;
383  for (unsigned int i = 0; i < num_addrs; i++)
384  len += addrlens[i];
385  str_len = strlen (config_section) + 1;
386  len += str_len;
387  if ( (len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) ||
388  (num_addrs > UINT16_MAX) ||
389  (str_len > UINT16_MAX) )
390  {
391  GNUNET_break (0);
392  return NULL;
393  }
394  rm = GNUNET_malloc (sizeof(*rm) + len);
395  rm->header.size = htons (sizeof(*rm) + len);
398  if (NULL != address_callback)
400  if (NULL != reversal_callback)
402  rm->proto = proto;
403  rm->str_len = htons (str_len);
404  rm->num_addrs = htons ((uint16_t) num_addrs);
405  off = (char *) &rm[1];
406  for (unsigned int i = 0; i < num_addrs; i++)
407  {
408  switch (addrs[i]->sa_family)
409  {
410  case AF_INET:
411  if (sizeof(struct sockaddr_in) != addrlens[i])
412  {
413  GNUNET_break (0);
414  GNUNET_free (rm);
415  return NULL;
416  }
417  break;
418 
419  case AF_INET6:
420  if (sizeof(struct sockaddr_in6) != addrlens[i])
421  {
422  GNUNET_break (0);
423  GNUNET_free (rm);
424  return NULL;
425  }
426  break;
427 
428 #if AF_UNIX
429  case AF_UNIX:
430  if (sizeof(struct sockaddr_un) != addrlens[i])
431  {
432  GNUNET_break (0);
433  GNUNET_free (rm);
434  return NULL;
435  }
436  break;
437 #endif
438  default:
439  GNUNET_break (0);
440  GNUNET_free (rm);
441  return NULL;
442  }
443  GNUNET_memcpy (off, addrs[i], addrlens[i]);
444  off += addrlens[i];
445  }
446  GNUNET_memcpy (off, config_section, str_len);
447 
448  nh = GNUNET_new (struct GNUNET_NAT_Handle);
449  nh->reg = &rm->header;
450  nh->cfg = cfg;
451  nh->address_callback = address_callback;
453  nh->callback_cls = callback_cls;
454  do_connect (nh);
455  return nh;
456 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static void reversal_callback(void *cls, const struct sockaddr_in *ra)
We got a connection reversal request from another peer.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_MESSAGE_TYPE_NAT_REGISTER
Message to ask NAT service to register a client.
@ GNUNET_NAT_RF_ADDRESSES
This client wants to be informed about changes to our applicable addresses.
Definition: nat.h:82
@ GNUNET_NAT_RF_NONE
This client does not want any notifications.
Definition: nat.h:76
@ GNUNET_NAT_RF_REVERSAL
This client supports address reversal.
Definition: nat.h:87
static void do_connect(void *cls)
Task to connect to the NAT service.
Definition: nat_api.c:329
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
Handle for active NAT registrations.
Definition: nat_api.c:72
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:111
struct GNUNET_MessageHeader * reg
Our registration message.
Definition: nat_api.c:86
GNUNET_NAT_ReversalCallback reversal_callback
Function to call when another peer requests connection reversal.
Definition: nat_api.c:106
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:101
Message sent by a client to register with its addresses.
Definition: nat.h:95
uint16_t num_addrs
Number of addresses that this service is bound to that follow.
Definition: nat.h:122
uint16_t str_len
Number of bytes in the string that follow which specifies a section name in the configuration.
Definition: nat.h:115
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_REGISTER.
Definition: nat.h:99
uint8_t flags
An enum GNUNET_NAT_RegisterFlags.
Definition: nat.h:104
uint8_t proto
Client's IPPROTO, e.g.
Definition: nat.h:109

References GNUNET_NAT_Handle::address_callback, GNUNET_NAT_Handle::callback_cls, cfg, GNUNET_NAT_Handle::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, reversal_callback(), GNUNET_NAT_Handle::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(), nat_register(), run(), server_start_report_addresses(), setup_sockets(), and try_anat().

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 614 of file nat_api.c.

617 {
618  struct AddrEntry *ae;
619 
620  if ((addrlen != sizeof(struct sockaddr_in)) &&
621  (addrlen != sizeof(struct sockaddr_in6)))
622  {
623  GNUNET_break (0);
624  return GNUNET_SYSERR;
625  }
626  for (ae = nh->ae_head; NULL != ae; ae = ae->next)
627  if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen)))
628  return GNUNET_YES;
629  return GNUNET_NO;
630 }
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
Entry in DLL of addresses of this peer.
Definition: nat_api.c:39
struct AddrEntry * next
DLL.
Definition: nat_api.c:43
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:64
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:91

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

Referenced by tcp_plugin_check_address(), and udp_plugin_check_address().

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 646 of file nat_api.c.

649 {
650  struct GNUNET_MQ_Envelope *env;
652  char *buf;
653 
654  if (NULL == nh->mq)
655  return GNUNET_SYSERR;
656  GNUNET_break (AF_INET == local_sa->sin_family);
657  GNUNET_break (AF_INET == remote_sa->sin_family);
658  env =
659  GNUNET_MQ_msg_extra (req,
660  2 * sizeof(struct sockaddr_in),
662  req->local_addr_size = htons (sizeof(struct sockaddr_in));
663  req->remote_addr_size = htons (sizeof(struct sockaddr_in));
664  buf = (char *) &req[1];
665  GNUNET_memcpy (buf, local_sa, sizeof(struct sockaddr_in));
666  buf += sizeof(struct sockaddr_in);
667  GNUNET_memcpy (buf, remote_sa, sizeof(struct sockaddr_in));
668  GNUNET_MQ_send (nh->mq, env);
669  return GNUNET_OK;
670 }
#define GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL
Message to ask NAT service to request connection reversal.
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:81
Client asking the service to initiate connection reversal.
Definition: nat.h:161
uint16_t local_addr_size
Size of the local address included, in NBO.
Definition: nat.h:170
uint16_t remote_addr_size
Size of the remote address included, in NBO.
Definition: nat.h:175

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, nh, and GNUNET_NAT_RequestConnectionReversalMessage::remote_addr_size.

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

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.

FIXME: No, the implementation of this API does not do anything beyond cleaning up the handle. This is a problem for applications that use GNUNET_NAT_AddressCallback and use the app_ctx to store objects, because the callback will never be called for cleanup on unregister.

Parameters
nhthe handle to unregister

Definition at line 674 of file nat_api.c.

675 {
676  struct AddrEntry *ae;
677  struct AddrEntry *next;
678 
679  if (NULL != nh->mq)
680  {
682  nh->mq = NULL;
683  }
684  if (NULL != nh->reconnect_task)
685  {
687  nh->reconnect_task = NULL;
688  }
689  next = nh->ae_head;
690  while (NULL != next)
691  {
692  ae = next;
693  next = next->next;
695  GNUNET_free (ae);
696  }
697  GNUNET_free (nh->reg);
698  GNUNET_free (nh);
699 }
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:116
struct AddrEntry * ae_tail
Tail of address DLL.
Definition: nat_api.c:96

References GNUNET_NAT_Handle::ae_head, GNUNET_NAT_Handle::ae_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_MQ_destroy(), GNUNET_SCHEDULER_cancel(), GNUNET_NAT_Handle::mq, AddrEntry::next, nh, 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_init(), server_stop_report_addresses(), and try_anat().

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 572 of file nat_api.c.

577 {
578  struct GNUNET_MQ_Envelope *env;
579  struct GNUNET_NAT_HandleStunMessage *hsn;
580  char *buf;
581 
583  return GNUNET_NO;
584  if (NULL == nh->mq)
585  return GNUNET_SYSERR;
586  env = GNUNET_MQ_msg_extra (hsn,
587  data_size + sender_addr_len,
589  hsn->sender_addr_size = htons ((uint16_t) sender_addr_len);
590  hsn->payload_size = htons ((uint16_t) data_size);
591  buf = (char *) &hsn[1];
592  GNUNET_memcpy (buf, sender_addr, sender_addr_len);
593  buf += sender_addr_len;
595  GNUNET_MQ_send (nh->mq, env);
596  return GNUNET_OK;
597 }
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t data
The data value.
#define GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN
Message to ask NAT service to handle a STUN packet.
static enum GNUNET_GenericReturnValue test_stun_packet(const void *data, size_t len)
Check if an incoming message is a STUN message.
Definition: nat_api.c:468
Client telling the service to (possibly) handle a STUN message.
Definition: nat.h:135
uint16_t payload_size
Number of bytes of payload included, in NBO.
Definition: nat.h:149
uint16_t sender_addr_size
Size of the sender address included, in NBO.
Definition: nat.h:144

References buf, data, data_size, 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, nh, 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().

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 211 of file nat_api_stun.c.

216 {
217  struct GNUNET_NAT_STUN_Handle *rh;
218 
219  rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle);
220  rh->sock = sock;
221  rh->cb = cb;
222  rh->cb_cls = cb_cls;
223  rh->stun_server = GNUNET_strdup (server);
224  rh->stun_port = port;
225  rh->dns_success = GNUNET_NO;
227  AF_INET,
228  TIMEOUT,
230  rh);
231  if (NULL == rh->dns_active)
232  {
234  return NULL;
235  }
236  return rh;
237 }
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
void GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh)
Cancel active STUN request.
Definition: nat_api_stun.c:247
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:940
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:135
#define TIMEOUT
Definition: nat_api_stun.c:48
Handle to a request given to the resolver.
Definition: nat_api_stun.c:57
int dns_success
Do we got a DNS resolution successfully?
Definition: nat_api_stun.c:86
GNUNET_NAT_TestCallback cb
Function to call when a error occurs.
Definition: nat_api_stun.c:76
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
Definition: nat_api_stun.c:61
void * cb_cls
Closure for cb.
Definition: nat_api_stun.c:81
char * stun_server
Stun server address.
Definition: nat_api_stun.c:71
uint16_t stun_port
STUN port.
Definition: nat_api_stun.c:91
struct GNUNET_NETWORK_Handle * sock
Handle to the listen socket.
Definition: nat_api_stun.c:66

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().

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 247 of file nat_api_stun.c.

248 {
249  if (NULL != rh->dns_active)
250  {
252  rh->dns_active = NULL;
253  }
254  GNUNET_free (rh->stun_server);
255  GNUNET_free (rh);
256 }
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.

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().

Here is the call graph for this function:
Here is the caller graph for this function: