GNUnet  0.11.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 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 441 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.

53 {
58 
64 
70 
75 
81 
89 
96 
102  GNUNET_NAT_AC_WLAN = 16,
103 
108  GNUNET_NAT_AC_BT = 32,
109 
114 
122  GNUNET_NAT_AC_EXTERN = 128,
123 
128  GNUNET_NAT_AC_MANUAL = 256,
129 
133  GNUNET_NAT_AC_ANY = 65535
134 };
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 140 of file gnunet_nat_service.h.

141 {
146 
151 
156 
161 
166 
171 
176 
181 
186 
191 
196 
201 
206 
211 
216 
221 
226 
231 
236 };
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 242 of file gnunet_nat_service.h.

243 {
248 
253 
258 
263 
268 };
#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 426 of file nat_auto_api_test.c.

References _, addr_cb(), cfg, GNUNET_NAT_AUTO_Test::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().

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 
457  nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test);
458  nh->cfg = cfg;
459  nh->proto = proto;
461  nh->report = report;
462  nh->report_cls = report_cls;
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  }
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  {
536  nh->ltask = NULL;
537  }
538  if (NULL != nh->lsock)
539  {
541  nh->lsock = NULL;
542  }
545  return nh;
546  }
547  }
548  return nh;
549 }
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:708
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.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
#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:485
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:1280
#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 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:378
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:1486
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:560
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:912
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:966
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 558 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().

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.
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:692
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:824
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:560
#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:966
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 _ (
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 }
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 225 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().

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;
254  ah->mq = GNUNET_CLIENT_connect (cfg,
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 }
#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:1057
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:84
#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:67
Handle to auto-configuration in progress.
Definition: nat_auto_api.c:37
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:202
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:353
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 285 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().

286 {
287  GNUNET_MQ_destroy (ah->mq);
288  GNUNET_free (ah);
289 }
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
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 378 of file nat_api.c.

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

387 {
388  struct GNUNET_NAT_Handle *nh;
389  struct GNUNET_NAT_RegisterMessage *rm;
390  size_t len;
391  size_t str_len;
392  char *off;
393 
394  len = 0;
395  for (unsigned int i = 0; i < num_addrs; i++)
396  len += addrlens[i];
397  str_len = strlen (config_section) + 1;
398  len += str_len;
399  if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) ||
400  (num_addrs > UINT16_MAX))
401  {
402  GNUNET_break (0);
403  return NULL;
404  }
405  rm = GNUNET_malloc (sizeof(*rm) + len);
406  rm->header.size = htons (sizeof(*rm) + len);
409  if (NULL != address_callback)
411  if (NULL != reversal_callback)
413  rm->proto = proto;
414  rm->str_len = htons (str_len);
415  rm->num_addrs = htons ((uint16_t) num_addrs);
416  off = (char *) &rm[1];
417  for (unsigned int i = 0; i < num_addrs; i++)
418  {
419  switch (addrs[i]->sa_family)
420  {
421  case AF_INET:
422  if (sizeof(struct sockaddr_in) != addrlens[i])
423  {
424  GNUNET_break (0);
425  GNUNET_free (rm);
426  return NULL;
427  }
428  break;
429 
430  case AF_INET6:
431  if (sizeof(struct sockaddr_in6) != addrlens[i])
432  {
433  GNUNET_break (0);
434  GNUNET_free (rm);
435  return NULL;
436  }
437  break;
438 
439 #if AF_UNIX
440  case AF_UNIX:
441  if (sizeof(struct sockaddr_un) != addrlens[i])
442  {
443  GNUNET_break (0);
444  GNUNET_free (rm);
445  return NULL;
446  }
447  break;
448 #endif
449  default:
450  GNUNET_break (0);
451  GNUNET_free (rm);
452  return NULL;
453  }
454  GNUNET_memcpy (off, addrs[i], addrlens[i]);
455  off += addrlens[i];
456  }
457  GNUNET_memcpy (off, config_section, str_len);
458 
459  nh = GNUNET_new (struct GNUNET_NAT_Handle);
460  nh->reg = &rm->header;
461  nh->cfg = cfg;
462  nh->address_callback = address_callback;
464  nh->callback_cls = callback_cls;
465  do_connect (nh);
466  return nh;
467 }
Handle for active NAT registrations.
Definition: nat_api.c:71
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
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:94
uint8_t proto
Client&#39;s IPPROTO, e.g.
Definition: nat.h:109
#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:115
struct GNUNET_MessageHeader * reg
Our registration message.
Definition: nat_api.c:86
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:111
#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:328
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:122
uint8_t flags
An enum GNUNET_NAT_RegisterFlags.
Definition: nat.h:104
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
GNUNET_NAT_ReversalCallback reversal_callback
Function to call when another peer requests connection reversal.
Definition: nat_api.c:106
#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:99
This client supports address reversal.
Definition: nat.h:87
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:101
This client wants to be informed about changes to our applicable addresses.
Definition: nat.h:82
#define GNUNET_malloc(size)
Wrapper around malloc.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_api.c:76
#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:76
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 625 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().

628 {
629  struct AddrEntry *ae;
630 
631  if ((addrlen != sizeof(struct sockaddr_in)) &&
632  (addrlen != sizeof(struct sockaddr_in6)))
633  {
634  GNUNET_break (0);
635  return GNUNET_SYSERR;
636  }
637  for (ae = nh->ae_head; NULL != ae; ae = ae->next)
638  if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen)))
639  return GNUNET_YES;
640  return GNUNET_NO;
641 }
#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:91
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct AddrEntry * next
DLL.
Definition: nat_api.c:43
#define GNUNET_YES
Definition: gnunet_common.h:77
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:64
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 657 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().

660 {
661  struct GNUNET_MQ_Envelope *env;
663  char *buf;
664 
665  if (NULL == nh->mq)
666  return GNUNET_SYSERR;
667  GNUNET_break (AF_INET == local_sa->sin_family);
668  GNUNET_break (AF_INET == remote_sa->sin_family);
669  env =
670  GNUNET_MQ_msg_extra (req,
671  2 * sizeof(struct sockaddr_in),
673  req->local_addr_size = htons (sizeof(struct sockaddr_in));
674  req->remote_addr_size = htons (sizeof(struct sockaddr_in));
675  buf = (char *) &req[1];
676  GNUNET_memcpy (buf, local_sa, sizeof(struct sockaddr_in));
677  buf += sizeof(struct sockaddr_in);
678  GNUNET_memcpy (buf, remote_sa, sizeof(struct sockaddr_in));
679  GNUNET_MQ_send (nh->mq, env);
680  return GNUNET_OK;
681 }
uint16_t remote_addr_size
Size of the remote address included, in NBO.
Definition: nat.h:175
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:160
#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:170
#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:353
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 692 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().

693 {
694  if (NULL != nh->mq)
695  {
696  GNUNET_MQ_destroy (nh->mq);
697  nh->mq = NULL;
698  }
699  if (NULL != nh->reconnect_task)
700  {
702  nh->reconnect_task = NULL;
703  }
704  GNUNET_free (nh->reg);
705  GNUNET_free (nh);
706 }
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:81
struct GNUNET_MessageHeader * reg
Our registration message.
Definition: nat_api.c:86
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:116
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
#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:966
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 583 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().

588 {
589  struct GNUNET_MQ_Envelope *env;
590  struct GNUNET_NAT_HandleStunMessage *hsn;
591  char *buf;
592 
594  return GNUNET_NO;
595  if (NULL == nh->mq)
596  return GNUNET_SYSERR;
597  env = GNUNET_MQ_msg_extra (hsn,
598  data_size + sender_addr_len,
600  hsn->sender_addr_size = htons ((uint16_t) sender_addr_len);
601  hsn->payload_size = htons ((uint16_t) data_size);
602  buf = (char *) &hsn[1];
603  GNUNET_memcpy (buf, sender_addr, sender_addr_len);
604  buf += sender_addr_len;
605  GNUNET_memcpy (buf, data, data_size);
606  GNUNET_MQ_send (nh->mq, env);
607  return GNUNET_OK;
608 }
Client telling the service to (possibly) handle a STUN message.
Definition: nat.h:134
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:81
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
#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:149
#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:479
#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:144
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:353
uint32_t data
The data value.
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.

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

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 }
void GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh)
Cancel active STUN request.
Definition: nat_api_stun.c:247
void * cb_cls
Closure for cb.
Definition: nat_api_stun.c:81
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:939
#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:66
char * stun_server
Stun server address.
Definition: nat_api_stun.c:71
#define TIMEOUT
Definition: nat_api_stun.c:48
GNUNET_NAT_TestCallback cb
Function to call when a error occours.
Definition: nat_api_stun.c:76
uint16_t stun_port
STUN port.
Definition: nat_api_stun.c:91
int dns_success
Do we got a DNS resolution successfully?
Definition: nat_api_stun.c:86
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:135
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
Definition: nat_api_stun.c:61
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.

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

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.
char * stun_server
Stun server address.
Definition: nat_api_stun.c:71
#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:61
Here is the call graph for this function:
Here is the caller graph for this function: