GNUnet  0.10.x
Data Structures | Macros | Functions
gnunet-nat-auto_legacy.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_nat_lib.h"
#include "nat.h"
Include dependency graph for gnunet-nat-auto_legacy.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_Test
 Handle to a NAT test. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from(kind, "nat", __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, int add_remove, const struct sockaddr *addr, socklen_t addrlen)
 Address-callback, used to send message to gnunet-nat-server. More...
 
static void do_timeout (void *cls)
 Timeout task for a nat test. More...
 
struct GNUNET_NAT_TestGNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp, uint16_t bnd_port, uint16_t adv_port, struct GNUNET_TIME_Relative timeout, GNUNET_NAT_TestCallback report, void *report_cls)
 Start testing if NAT traversal works using the given configuration (IPv4-only). More...
 
void GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
 Stop an active NAT test. More...
 

Macro Definition Documentation

◆ LOG

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

◆ NAT_SERVER_TIMEOUT

#define NAT_SERVER_TIMEOUT   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)

Definition at line 33 of file gnunet-nat-auto_legacy.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 183 of file gnunet-nat-auto_legacy.c.

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

Referenced by GNUNET_NAT_test_start().

184 {
185  struct GNUNET_NAT_Test *h = cls;
186  const struct sockaddr_in *sa;
187 
188  if (sizeof(struct sockaddr_in) != addrlen)
189  return;
190  sa = (const struct sockaddr_in *)addr;
191  if (h->data != sa->sin_port)
192  {
194  "Received connection reversal request for wrong port\n");
195  return; /* wrong port */
196  }
197  /* report success */
199 }
uint16_t data
Data that should be transmitted or source-port.
void * report_cls
Closure for report.
GNUNET_NAT_TestCallback report
Function to call with success report.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
#define LOG(kind,...)
Handle to a NAT test.
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_Test

Definition at line 209 of file gnunet-nat-auto_legacy.c.

References data, GNUNET_NAT_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_Test::lsock, GNUNET_NAT_Test::ltask, GNUNET_SCHEDULER_TaskContext::read_ready, GNUNET_NAT_Test::report, GNUNET_NAT_Test::report_cls, tc, and GNUNET_SCHEDULER_TaskContext::write_ready.

Referenced by GNUNET_NAT_test_start().

210 {
211  struct GNUNET_NAT_Test *tst = cls;
212  uint16_t data;
213  const struct GNUNET_SCHEDULER_TaskContext *tc;
214 
217  tst->lsock,
218  &do_udp_read,
219  tst);
220  if ((NULL != tc->write_ready) &&
222  (sizeof(data) ==
223  GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data))))
224  {
225  if (data == tst->data)
227  else
229  "Received data mismatches expected value\n");
230  }
231  else
233  "Failed to receive data from inbound connection\n");
234 }
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:737
uint16_t data
Data that should be transmitted or source-port.
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:775
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:410
void * report_cls
Closure for report.
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 ...
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 ...
GNUNET_NAT_TestCallback report
Function to call with success report.
#define LOG(kind,...)
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void do_udp_read(void *cls)
Activity on our incoming socket.
Handle to a NAT test.
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
struct GNUNET_NETWORK_Handle * lsock
Handle to listen socket, or NULL.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
int 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:1017
uint32_t data
The data value.
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 244 of file gnunet-nat-auto_legacy.c.

References data, GNUNET_NAT_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_Test::na_head, GNUNET_NAT_Test::na_tail, GNUNET_SCHEDULER_TaskContext::read_ready, GNUNET_NAT_Test::report, GNUNET_NAT_Test::report_cls, NatActivity::rtask, NatActivity::sock, tc, and GNUNET_SCHEDULER_TaskContext::write_ready.

Referenced by do_accept().

245 {
246  struct NatActivity *na = cls;
247  struct GNUNET_NAT_Test *tst;
248  uint16_t data;
249  const struct GNUNET_SCHEDULER_TaskContext *tc;
250 
252  na->rtask = NULL;
253  tst = na->h;
255  if ((NULL != tc->write_ready) &&
257  (sizeof(data) ==
258  GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data))))
259  {
260  if (data == tst->data)
262  else
264  "Received data does not match expected value\n");
265  }
266  else
268  "Failed to receive data from inbound connection\n");
270  GNUNET_free(na);
271 }
#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:737
uint16_t data
Data that should be transmitted or source-port.
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:775
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:410
void * report_cls
Closure for report.
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_tail
Tail of list of nat activities.
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 ...
GNUNET_NAT_TestCallback report
Function to call with success report.
struct GNUNET_SCHEDULER_Task * rtask
Task reading from the incoming connection.
Entry we keep for each incoming connection.
#define LOG(kind,...)
struct NatActivity * na_head
Head of list of nat activities.
Handle to a NAT test.
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:1017
uint32_t data
The data value.
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
#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_Test

Definition at line 281 of file gnunet-nat-auto_legacy.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_Test::lsock, GNUNET_NAT_Test::ltask, GNUNET_NAT_Test::na_head, GNUNET_NAT_Test::na_tail, NatActivity::rtask, and NatActivity::sock.

Referenced by GNUNET_NAT_test_start().

282 {
283  struct GNUNET_NAT_Test *tst = cls;
284  struct GNUNET_NETWORK_Handle *s;
285  struct NatActivity *wl;
286 
288  tst->lsock,
289  &do_accept,
290  tst);
291  s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL);
292  if (NULL == s)
293  {
295  return; /* odd error */
296  }
298  "Got an inbound connection, waiting for data\n");
299  wl = GNUNET_new(struct NatActivity);
300  wl->sock = s;
301  wl->h = tst;
303  wl->sock,
304  &do_read,
305  wl);
307 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct GNUNET_NETWORK_Handle * sock
Socket of the incoming connection.
static void do_read(void *cls)
Activity on our incoming socket.
struct NatActivity * na_tail
Tail 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:420
#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...
#define LOG(kind,...)
struct NatActivity * na_head
Head of list of nat activities.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void do_accept(void *cls)
Activity on our listen socket.
Handle to a NAT test.
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
struct GNUNET_NETWORK_Handle * lsock
Handle to listen socket, or NULL.
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:1467
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 318 of file gnunet-nat-auto_legacy.c.

References GNUNET_NAT_Test::ca_head, GNUNET_NAT_Test::ca_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_MQ_destroy(), ClientActivity::h, and ClientActivity::mq.

Referenced by addr_cb().

319 {
320  struct ClientActivity *ca = cls;
321  struct GNUNET_NAT_Test *tst = ca->h;
322 
324  GNUNET_MQ_destroy(ca->mq);
325  GNUNET_free(ca);
326 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_MQ_Handle * mq
Socket of the incoming connection.
struct ClientActivity * ca_tail
Tail of list of client activities.
struct ClientActivity * ca_head
Head of list of client activities.
struct GNUNET_NAT_Test * h
Handle to overall NAT test.
Handle to a NAT test.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
Entry we keep for each connection to the gnunet-nat-service.
#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,
int  add_remove,
const struct sockaddr *  addr,
socklen_t  addrlen 
)
static

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

Parameters
clsclosure
add_removeGNUNET_YES to mean the new public IP address, GNUNET_NO to mean the previous (now invalid) one
addreither the previous or the new public IP address
addrlenactual length of the addr

Definition at line 339 of file gnunet-nat-auto_legacy.c.

References _, GNUNET_NAT_Test::ca_head, GNUNET_NAT_Test::ca_tail, GNUNET_NAT_Test::cfg, GNUNET_NAT_TestMessage::data, GNUNET_NAT_Test::data, GNUNET_NAT_TestMessage::dport, GNUNET_NAT_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_TestMessage::is_tcp, GNUNET_NAT_Test::is_tcp, LOG, ClientActivity::mq, mq_error_handler(), and msg.

Referenced by GNUNET_NAT_test_start().

343 {
344  struct GNUNET_NAT_Test *h = cls;
345  struct ClientActivity *ca;
346  struct GNUNET_MQ_Envelope *env;
347  struct GNUNET_NAT_TestMessage *msg;
348  const struct sockaddr_in *sa;
349 
350  if (GNUNET_YES != add_remove)
351  return;
352  if (addrlen != sizeof(struct sockaddr_in))
353  {
355  "NAT test ignores IPv6 address `%s' returned from NAT library\n",
356  GNUNET_a2s(addr, addrlen));
357  return; /* ignore IPv6 here */
358  }
360  "Asking gnunet-nat-server to connect to `%s'\n",
361  GNUNET_a2s(addr, addrlen));
362 
363  ca = GNUNET_new(struct ClientActivity);
364  ca->h = h;
365  ca->mq = GNUNET_CLIENT_connect(h->cfg,
366  "gnunet-nat-server",
367  NULL,
369  ca);
370  if (NULL == ca->mq)
371  {
372  GNUNET_free(ca);
374  _("Failed to connect to `gnunet-nat-server'\n"));
375  return;
376  }
378  sa = (const struct sockaddr_in *)addr;
380  msg->dst_ipv4 = sa->sin_addr.s_addr;
381  msg->dport = sa->sin_port;
382  msg->data = h->data;
383  msg->is_tcp = htonl((uint32_t)h->is_tcp);
384  GNUNET_MQ_send(ca->mq, env);
385 }
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration used.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
uint32_t dst_ipv4
IPv4 target IP address.
Definition: nat.h:48
uint16_t data
Data that should be transmitted or source-port.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:900
struct GNUNET_MQ_Handle * mq
Socket of the incoming connection.
int32_t is_tcp
GNUNET_YES for TCP, GNUNET_NO for UDP.
Definition: nat.h:63
#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:181
uint16_t dport
Port to use, 0 to send dummy ICMP response.
Definition: nat.h:53
struct ClientActivity * ca_tail
Tail of list of client activities.
struct ClientActivity * ca_head
Head of list of client activities.
#define LOG(kind,...)
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 mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got disconnected from the NAT server.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_NAT_Test * h
Handle to overall NAT test.
Handle to a NAT test.
Request to test NAT traversal, sent to the gnunet-nat-server (not the service!).
Definition: nat.h:39
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:77
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
Entry we keep for each connection to the gnunet-nat-service.
int is_tcp
GNUNET_YES if we're testing TCP
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t data
Data to send OR advertised-port (in NBO) to use for dummy ICMP.
Definition: nat.h:58
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_timeout()

static void do_timeout ( void *  cls)
static

Timeout task for a nat test.

Calls the report-callback with a timeout return value

Destroys the nat handle after the callback has been processed.

Parameters
clshandle to the timed out NAT test

Definition at line 397 of file gnunet-nat-auto_legacy.c.

References GNUNET_NAT_ERROR_SUCCESS, GNUNET_NAT_ERROR_TIMEOUT, nh, GNUNET_NAT_Test::report, GNUNET_NAT_Test::report_cls, GNUNET_NAT_Test::status, and GNUNET_NAT_Test::ttask.

Referenced by GNUNET_NAT_test_start().

398 {
399  struct GNUNET_NAT_Test *nh = cls;
400 
401  nh->ttask = NULL;
402  nh->report(nh->report_cls,
405  : nh->status);
406 }
void * report_cls
Closure for report.
GNUNET_NAT_TestCallback report
Function to call with success report.
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
struct GNUNET_SCHEDULER_Task * ttask
Task identifier for the timeout (if any)
Handle to a NAT test.
enum GNUNET_NAT_StatusCode status
Status code to be reported to the timeout/status call.
Here is the caller graph for this function:

◆ GNUNET_NAT_test_start()

struct GNUNET_NAT_Test* GNUNET_NAT_test_start ( const struct GNUNET_CONFIGURATION_Handle cfg,
int  is_tcp,
uint16_t  bnd_port,
uint16_t  adv_port,
struct GNUNET_TIME_Relative  timeout,
GNUNET_NAT_TestCallback  report,
void *  report_cls 
)

Start testing if NAT traversal works using the given configuration (IPv4-only).

ALL failures are reported directly to the report callback

Parameters
cfgconfiguration for the NAT traversal
is_tcpGNUNET_YES to test TCP, GNUNET_NO to test UDP
bnd_portport to bind to, 0 for connection reversal
adv_portexternally advertised port to use
timeoutdelay after which the test should be aborted
reportfunction to call with the result of the test
report_clsclosure for report
Returns
handle to cancel NAT test or NULL. The error is always indicated via the report callback

Definition at line 425 of file gnunet-nat-auto_legacy.c.

References _, addr_cb(), GNUNET_NAT_Test::adv_port, GNUNET_NAT_Test::cfg, cfg, GNUNET_NAT_Test::data, do_accept(), do_timeout(), do_udp_read(), GNUNET_a2s(), GNUNET_break, 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_delayed(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_cancel(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, GNUNET_NAT_Test::is_tcp, LOG, GNUNET_NAT_Test::lsock, GNUNET_NAT_Test::ltask, GNUNET_NAT_Test::nat, nh, GNUNET_NAT_Test::report, GNUNET_NAT_Test::report_cls, reversal_cb(), GNUNET_NAT_Test::status, and GNUNET_NAT_Test::ttask.

Referenced by reversal_test().

432 {
433  struct GNUNET_NAT_Test *nh;
434  struct sockaddr_in sa;
435  const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa };
436  const socklen_t addrlens[] = { sizeof(sa) };
437 
438  memset(&sa, 0, sizeof(sa));
439  sa.sin_family = AF_INET;
440  sa.sin_port = htons(bnd_port);
441 #if HAVE_SOCKADDR_IN_SIN_LEN
442  sa.sin_len = sizeof(sa);
443 #endif
444 
445  nh = GNUNET_new(struct GNUNET_NAT_Test);
446  nh->cfg = cfg;
447  nh->is_tcp = is_tcp;
448  nh->data = bnd_port;
449  nh->adv_port = adv_port;
450  nh->report = report;
451  nh->report_cls = report_cls;
453  if (0 == bnd_port)
454  {
455  nh->nat = GNUNET_NAT_register(cfg,
456  is_tcp,
457  0,
458  0,
459  NULL,
460  NULL,
461  &addr_cb,
462  &reversal_cb,
463  nh,
464  NULL);
465  }
466  else
467  {
468  nh->lsock =
470  (is_tcp == GNUNET_YES) ? SOCK_STREAM
471  : SOCK_DGRAM,
472  0);
473  if ((nh->lsock == NULL) ||
475  (const struct sockaddr *)&sa,
476  sizeof(sa))))
477  {
478  GNUNET_log(
480  _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
481  GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)),
482  strerror(errno));
483  if (NULL != nh->lsock)
484  {
486  nh->lsock = NULL;
487  }
490  return nh;
491  }
492  if (GNUNET_YES == is_tcp)
493  {
496  nh->lsock,
497  &do_accept,
498  nh);
499  }
500  else
501  {
503  nh->lsock,
504  &do_udp_read,
505  nh);
506  }
508  "NAT test listens on port %u (%s)\n",
509  bnd_port,
510  (GNUNET_YES == is_tcp) ? "tcp" : "udp");
511  nh->nat = GNUNET_NAT_register(cfg,
512  is_tcp,
513  adv_port,
514  1,
515  addrs,
516  addrlens,
517  &addr_cb,
518  NULL,
519  nh,
520  NULL);
521  if (NULL == nh->nat)
522  {
524  _("NAT test failed to start NAT library\n"));
525  if (NULL != nh->ltask)
526  {
528  nh->ltask = NULL;
529  }
530  if (NULL != nh->lsock)
531  {
533  nh->lsock = NULL;
534  }
537  return nh;
538  }
539  }
540  nh->ttask = GNUNET_SCHEDULER_add_delayed(timeout, &do_timeout, nh);
541  return nh;
542 }
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:696
NAT test failed to initiate.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration used.
uint16_t data
Data that should be transmitted or source-port.
int GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:474
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.
GNUNET_NAT_TestCallback report
Function to call with success report.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_NAT_Handle * nat
Handle to NAT traversal in use.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
uint16_t adv_port
Advertised port to the other peer.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
#define LOG(kind,...)
#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_udp_read(void *cls)
Activity on our incoming socket.
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
Failure in network subsystem, check permissions.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static void do_accept(void *cls)
Activity on our listen socket.
static void addr_cb(void *cls, int add_remove, const struct sockaddr *addr, socklen_t addrlen)
Address-callback, used to send message to gnunet-nat-server.
struct GNUNET_SCHEDULER_Task * ttask
Task identifier for the timeout (if any)
struct GNUNET_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition: nat_api.c:376
Handle to a NAT test.
static void do_timeout(void *cls)
Timeout task for a nat test.
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...
struct GNUNET_SCHEDULER_Task * ltask
Identity of task for the listen socket (if any)
#define GNUNET_log(kind,...)
struct GNUNET_NETWORK_Handle * lsock
Handle to listen socket, or NULL.
enum GNUNET_NAT_StatusCode status
Status code to be reported to the timeout/status call.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1467
int is_tcp
GNUNET_YES if we're testing TCP
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:900
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NAT_test_stop()

void GNUNET_NAT_test_stop ( struct GNUNET_NAT_Test tst)

Stop an active NAT test.

Parameters
tsttest to stop.

Definition at line 551 of file gnunet-nat-auto_legacy.c.

References GNUNET_NAT_Test::ca_head, GNUNET_NAT_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_Test::lsock, GNUNET_NAT_Test::ltask, ClientActivity::mq, GNUNET_NAT_Test::na_head, GNUNET_NAT_Test::na_tail, GNUNET_NAT_Test::nat, NatActivity::rtask, NatActivity::sock, and GNUNET_NAT_Test::ttask.

Referenced by GNUNET_NAT_autoconfig_cancel(), and result_callback().

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