GNUnet  0.10.x
Data Structures | Macros | Functions
nat_auto_api_test.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_nat_service.h"
#include "gnunet_nat_auto_service.h"
#include "nat-auto.h"
Include dependency graph for nat_auto_api_test.c:

Go to the source code of this file.

Data Structures

struct  NatActivity
 Entry we keep for each incoming connection. More...
 
struct  ClientActivity
 Entry we keep for each connection to the gnunet-nat-service. More...
 
struct  GNUNET_NAT_AUTO_Test
 Handle to a NAT test. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "nat-auto", __VA_ARGS__)
 
#define NAT_SERVER_TIMEOUT   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 

Functions

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. More...
 
static void do_udp_read (void *cls)
 Activity on our incoming socket. More...
 
static void do_read (void *cls)
 Activity on our incoming socket. More...
 
static void do_accept (void *cls)
 Activity on our listen socket. More...
 
static void mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
 We got disconnected from the NAT server. More...
 
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. More...
 
static void do_fail (void *cls)
 Calls the report-callback reporting failure. More...
 
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...
 

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "nat-auto", __VA_ARGS__)

◆ NAT_SERVER_TIMEOUT

#define NAT_SERVER_TIMEOUT   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)

Definition at line 33 of file nat_auto_api_test.c.

Function Documentation

◆ reversal_cb()

static void reversal_cb ( void *  cls,
const struct sockaddr *  addr,
socklen_t  addrlen 
)
static

Function called from GNUNET_NAT_register whenever someone asks us to do connection reversal.

Parameters
clsclosure, our struct GNUNET_NAT_Handle
addrpublic IP address of the other peer
addrlenactual lenght of the addr

Definition at line 187 of file nat_auto_api_test.c.

References GNUNET_NAT_AUTO_Test::data, GNUNET_ERROR_TYPE_DEBUG, GNUNET_NAT_ERROR_SUCCESS, NatActivity::h, LOG, GNUNET_NAT_AUTO_Test::report, and GNUNET_NAT_AUTO_Test::report_cls.

Referenced by GNUNET_NAT_AUTO_test_start().

190 {
191  struct GNUNET_NAT_AUTO_Test *h = cls;
192  const struct sockaddr_in *sa;
193 
194  if (sizeof (struct sockaddr_in) != addrlen)
195  return;
196  sa = (const struct sockaddr_in *) addr;
197  if (h->data != sa->sin_port)
198  {
200  "Received connection reversal request for wrong port\n");
201  return; /* wrong port */
202  }
203  /* report success */
204  h->report (h->report_cls,
206 }
GNUNET_NAT_TestCallback report
Function to call with success report.
#define LOG(kind,...)
Handle to a NAT test.
void * report_cls
Closure for report.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
uint16_t data
Data that should be transmitted or source-port.
Here is the caller graph for this function:

◆ do_udp_read()

static void do_udp_read ( void *  cls)
static

Activity on our incoming socket.

Read data from the incoming connection.

Parameters
clsthe struct GNUNET_NAT_AUTO_Test

Definition at line 216 of file nat_auto_api_test.c.

References data, GNUNET_NAT_AUTO_Test::data, GNUNET_ERROR_TYPE_DEBUG, GNUNET_NAT_ERROR_SUCCESS, GNUNET_NETWORK_fdset_isset(), GNUNET_NETWORK_socket_recv(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_get_task_context(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, GNUNET_NAT_AUTO_Test::lsock, GNUNET_NAT_AUTO_Test::ltask, GNUNET_SCHEDULER_TaskContext::read_ready, GNUNET_NAT_AUTO_Test::report, GNUNET_NAT_AUTO_Test::report_cls, tc, and GNUNET_SCHEDULER_TaskContext::write_ready.

Referenced by GNUNET_NAT_AUTO_test_start().

217 {
218  struct GNUNET_NAT_AUTO_Test *tst = cls;
219  uint16_t data;
220  const struct GNUNET_SCHEDULER_TaskContext *tc;
221 
223  tst->ltask =
225  tst->lsock,
226  &do_udp_read,
227  tst);
228  if ((NULL != tc->write_ready) &&
230  tst->lsock)) &&
231  (sizeof (data) ==
233  &data,
234  sizeof (data))))
235  {
236  if (data == tst->data)
237  tst->report (tst->report_cls,
239  else
241  "Received data mismatches expected value\n");
242  }
243  else
245  "Failed to receive data from inbound connection\n");
246 }
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:746
GNUNET_NAT_TestCallback report
Function to call with success report.
#define LOG(kind,...)
Context information passed to each scheduler task.
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:894
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:417
static void do_udp_read(void *cls)
Activity on our incoming socket.
Handle to a NAT test.
const struct GNUNET_NETWORK_FDSet * write_ready
Set of file descriptors ready for writing; note that additional bits may be set that were not in the ...
void * report_cls
Closure for report.
const struct GNUNET_NETWORK_FDSet * read_ready
Set of file descriptors ready for reading; note that additional bits may be set that were not in the ...
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
uint16_t data
Data that should be transmitted or source-port.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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:1478
int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Check whether a socket is part of the fd set.
Definition: network.c:1181
uint32_t data
The data value.
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:

◆ do_read()

static void do_read ( void *  cls)
static

Activity on our incoming socket.

Read data from the incoming connection.

Parameters
clsthe struct NatActivity

Definition at line 256 of file nat_auto_api_test.c.

References data, GNUNET_NAT_AUTO_Test::data, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_NAT_ERROR_SUCCESS, GNUNET_NETWORK_fdset_isset(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_recv(), GNUNET_SCHEDULER_get_task_context(), NatActivity::h, LOG, GNUNET_NAT_AUTO_Test::na_head, GNUNET_NAT_AUTO_Test::na_tail, GNUNET_SCHEDULER_TaskContext::read_ready, GNUNET_NAT_AUTO_Test::report, GNUNET_NAT_AUTO_Test::report_cls, NatActivity::rtask, NatActivity::sock, tc, and GNUNET_SCHEDULER_TaskContext::write_ready.

Referenced by do_accept().

257 {
258  struct NatActivity *na = cls;
259  struct GNUNET_NAT_AUTO_Test *tst;
260  uint16_t data;
261  const struct GNUNET_SCHEDULER_TaskContext *tc;
262 
264  na->rtask = NULL;
265  tst = na->h;
267  tst->na_tail,
268  na);
269  if ((NULL != tc->write_ready) &&
271  na->sock)) &&
272  (sizeof (data) ==
274  &data,
275  sizeof (data))))
276  {
277  if (data == tst->data)
278  tst->report (tst->report_cls,
280  else
282  "Received data does not match expected value\n");
283  }
284  else
286  "Failed to receive data from inbound connection\n");
288  GNUNET_free (na);
289 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:746
GNUNET_NAT_TestCallback report
Function to call with success report.
#define LOG(kind,...)
Context information passed to each scheduler task.
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:894
struct GNUNET_NETWORK_Handle * sock
Socket of the incoming connection.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:417
Handle to a NAT test.
const struct GNUNET_NETWORK_FDSet * write_ready
Set of file descriptors ready for writing; note that additional bits may be set that were not in the ...
struct NatActivity * na_head
Head of list of nat activities.
void * report_cls
Closure for report.
const struct GNUNET_NETWORK_FDSet * read_ready
Set of file descriptors ready for reading; note that additional bits may be set that were not in the ...
struct GNUNET_SCHEDULER_Task * rtask
Task reading from the incoming connection.
Entry we keep for each incoming connection.
uint16_t data
Data that should be transmitted or source-port.
struct NatActivity * na_tail
Tail of list of nat activities.
struct GNUNET_NAT_Test * h
Handle of the master context.
int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Check whether a socket is part of the fd set.
Definition: network.c:1181
uint32_t data
The data value.
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:604
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_accept()

static void do_accept ( void *  cls)
static

Activity on our listen socket.

Accept the incoming connection.

Parameters
clsthe struct GNUNET_NAT_AUTO_Test

Definition at line 299 of file nat_auto_api_test.c.

References do_read(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_log_strerror, GNUNET_NETWORK_socket_accept(), GNUNET_new, GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_UNIT_FOREVER_REL, NatActivity::h, LOG, GNUNET_NAT_AUTO_Test::lsock, GNUNET_NAT_AUTO_Test::ltask, GNUNET_NAT_AUTO_Test::na_head, GNUNET_NAT_AUTO_Test::na_tail, NatActivity::rtask, and NatActivity::sock.

Referenced by GNUNET_NAT_AUTO_test_start().

300 {
301  struct GNUNET_NAT_AUTO_Test *tst = cls;
302  struct GNUNET_NETWORK_Handle *s;
303  struct NatActivity *wl;
304 
306  tst->lsock,
307  &do_accept,
308  tst);
310  NULL,
311  NULL);
312  if (NULL == s)
313  {
315  "accept");
316  return; /* odd error */
317  }
319  "Got an inbound connection, waiting for data\n");
320  wl = GNUNET_new (struct NatActivity);
321  wl->sock = s;
322  wl->h = tst;
323  wl->rtask =
325  wl->sock,
326  &do_read,
327  wl);
329  tst->na_tail,
330  wl);
331 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define LOG(kind,...)
struct GNUNET_NETWORK_Handle * sock
Socket of the incoming connection.
Handle to a NAT test.
struct NatActivity * na_head
Head of list of nat activities.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_SCHEDULER_Task * rtask
Task reading from the incoming connection.
Entry we keep for each incoming connection.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:468
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
static void do_read(void *cls)
Activity on our incoming socket.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void do_accept(void *cls)
Activity on our listen socket.
struct NatActivity * na_tail
Tail of list of nat activities.
handle to a socket
Definition: network.c:46
struct GNUNET_NAT_Test * h
Handle of the master context.
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:1478
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:

◆ mq_error_handler()

static void mq_error_handler ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

We got disconnected from the NAT server.

Stop waiting for a reply.

Parameters
clsthe struct ClientActivity
errorerror code

Definition at line 342 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_free, GNUNET_MQ_destroy(), ClientActivity::h, and ClientActivity::mq.

Referenced by addr_cb().

344 {
345  struct ClientActivity *ca = cls;
346  struct GNUNET_NAT_AUTO_Test *tst = ca->h;
347 
349  tst->ca_tail,
350  ca);
351  GNUNET_MQ_destroy (ca->mq);
352  GNUNET_free (ca);
353 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct ClientActivity * ca_tail
Tail of list of client activities.
struct GNUNET_MQ_Handle * mq
Socket of the incoming connection.
Handle to a NAT test.
struct ClientActivity * ca_head
Head of list of client activities.
struct GNUNET_NAT_Test * h
Handle to overall NAT test.
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.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ addr_cb()

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

Address-callback, used to send message to gnunet-nat-server.

Parameters
clsclosure
app_ctx[in,out]location where the app can store stuff on add and retrieve it on remove
add_removeGNUNET_YES to mean the new public IP address, GNUNET_NO to mean the 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 369 of file nat_auto_api_test.c.

References _, GNUNET_NAT_AUTO_Test::ca_head, GNUNET_NAT_AUTO_Test::ca_tail, GNUNET_NAT_AUTO_Test::cfg, GNUNET_NAT_AUTO_TestMessage::data, GNUNET_NAT_AUTO_Test::data, GNUNET_NAT_AUTO_TestMessage::dport, GNUNET_NAT_AUTO_TestMessage::dst_ipv4, env, GNUNET_a2s(), GNUNET_CLIENT_connect(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, GNUNET_MESSAGE_TYPE_NAT_TEST, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_new, GNUNET_YES, NatActivity::h, ClientActivity::h, GNUNET_NAT_AUTO_TestMessage::is_tcp, LOG, ClientActivity::mq, mq_error_handler(), msg, and GNUNET_NAT_AUTO_Test::proto.

Referenced by GNUNET_NAT_AUTO_test_start().

375 {
376  struct GNUNET_NAT_AUTO_Test *h = cls;
377  struct ClientActivity *ca;
378  struct GNUNET_MQ_Envelope *env;
380  const struct sockaddr_in *sa;
381 
382  (void) app_ctx;
383  if (GNUNET_YES != add_remove)
384  return;
385  if (addrlen != sizeof (struct sockaddr_in))
386  {
388  "NAT test ignores IPv6 address `%s' returned from NAT library\n",
389  GNUNET_a2s (addr,
390  addrlen));
391  return; /* ignore IPv6 here */
392  }
394  "Asking gnunet-nat-server to connect to `%s'\n",
395  GNUNET_a2s (addr,
396  addrlen));
397 
398  ca = GNUNET_new (struct ClientActivity);
399  ca->h = h;
400  ca->mq = GNUNET_CLIENT_connect (h->cfg,
401  "gnunet-nat-server",
402  NULL,
404  ca);
405  if (NULL == ca->mq)
406  {
407  GNUNET_free (ca);
409  _("Failed to connect to `gnunet-nat-server'\n"));
410  return;
411  }
413  h->ca_tail,
414  ca);
415  sa = (const struct sockaddr_in *) addr;
416  env = GNUNET_MQ_msg (msg,
418  msg->dst_ipv4 = sa->sin_addr.s_addr;
419  msg->dport = sa->sin_port;
420  msg->data = h->data;
421  msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP));
422  GNUNET_MQ_send (ca->mq,
423  env);
424 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got disconnected from the NAT server.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define LOG(kind,...)
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:901
struct ClientActivity * ca_tail
Tail of list of client activities.
struct GNUNET_MQ_Handle * mq
Socket of the incoming connection.
Handle to a NAT test.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
#define GNUNET_MESSAGE_TYPE_NAT_TEST
Message to ask NAT server to perform traversal test.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
uint16_t data
Data that should be transmitted or source-port.
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)...
int32_t is_tcp
GNUNET_YES for TCP, GNUNET_NO for UDP.
Definition: nat-auto.h:64
struct ClientActivity * ca_head
Head of list of client activities.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_NAT_Test * h
Handle to overall NAT test.
uint32_t dst_ipv4
IPv4 target IP address.
Definition: nat-auto.h:49
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration used.
#define GNUNET_log(kind,...)
uint16_t dport
Port to use, 0 to send dummy ICMP response.
Definition: nat-auto.h:54
#define GNUNET_YES
Definition: gnunet_common.h:80
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
uint16_t data
Data to send OR advertised-port (in NBO) to use for dummy ICMP.
Definition: nat-auto.h:59
int proto
IPPROTO_TCP or IPPROTO_UDP.
Entry we keep for each connection to the gnunet-nat-service.
Request to test NAT traversal, sent to the gnunet-nat-server (not the service!).
Definition: nat-auto.h:39
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_fail()

static void do_fail ( void *  cls)
static

Calls the report-callback reporting failure.

Destroys the nat handle after the callback has been processed.

Parameters
clshandle to the timed out NAT test

Definition at line 435 of file nat_auto_api_test.c.

References nh, GNUNET_NAT_AUTO_Test::report, GNUNET_NAT_AUTO_Test::report_cls, GNUNET_NAT_AUTO_Test::status, and GNUNET_NAT_AUTO_Test::ttask.

Referenced by GNUNET_NAT_AUTO_test_start().

436 {
437  struct GNUNET_NAT_AUTO_Test *nh = cls;
438 
439  nh->ttask = NULL;
440  nh->report (nh->report_cls,
441  nh->status);
442 }
GNUNET_NAT_TestCallback report
Function to call with success report.
Handle to a NAT test.
void * report_cls
Closure for report.
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
enum GNUNET_NAT_StatusCode status
Status code to be reported to the timeout/status call.
struct GNUNET_SCHEDULER_Task * ttask
Task identifier for the timeout (if any)
Here is the caller graph for this function: