GNUnet  0.11.x
Data Structures | Functions
nat_api.c File Reference
#include "platform.h"
#include "gnunet_nat_service.h"
#include "nat.h"
#include "nat_stun.h"
Include dependency graph for nat_api.c:

Go to the source code of this file.

Data Structures

struct  AddrEntry
 Entry in DLL of addresses of this peer. More...
 
struct  GNUNET_NAT_Handle
 Handle for active NAT registrations. More...
 

Functions

static void do_connect (void *cls)
 Task to connect to the NAT service. More...
 
static void reconnect (struct GNUNET_NAT_Handle *nh)
 Task to connect to the NAT service. More...
 
static int check_connection_reversal_request (void *cls, const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
 Check connection reversal request. More...
 
static void handle_connection_reversal_request (void *cls, const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
 Handle connection reversal request. More...
 
static int check_address_change_notification (void *cls, const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
 Check address change notification. More...
 
static void handle_address_change_notification (void *cls, const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
 Handle connection reversal request. More...
 
static void mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
 Handle queue errors by reconnecting to NAT. 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...
 
static int test_stun_packet (const void *data, size_t len)
 Check if an incoming message is a STUN message. 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...
 
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...
 

Detailed Description

Author
Christian Grothoff
Milan Bouchet-Valat

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

Definition in file nat_api.c.

Function Documentation

◆ do_connect()

static void do_connect ( void *  cls)
static

Task to connect to the NAT service.

Parameters
clsour struct GNUNET_NAT_Handle *

Definition at line 328 of file nat_api.c.

References GNUNET_NAT_Handle::cfg, env, GNUNET_CLIENT_connect(), GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_MQ_msg_copy(), GNUNET_MQ_send(), GNUNET_NAT_Handle::mq, mq_error_handler(), nh, reconnect(), GNUNET_NAT_Handle::reconnect_task, and GNUNET_NAT_Handle::reg.

Referenced by GNUNET_NAT_register(), and reconnect().

329 {
330  struct GNUNET_NAT_Handle *nh = cls;
332  { GNUNET_MQ_hd_var_size (connection_reversal_request,
334  struct
336  nh),
337  GNUNET_MQ_hd_var_size (address_change_notification,
340  nh),
342  struct GNUNET_MQ_Envelope *env;
343 
344  nh->reconnect_task = NULL;
345  nh->mq =
346  GNUNET_CLIENT_connect (nh->cfg, "nat", handlers, &mq_error_handler, nh);
347  if (NULL == nh->mq)
348  {
349  reconnect (nh);
350  return;
351  }
352  env = GNUNET_MQ_msg_copy (nh->reg);
353  GNUNET_MQ_send (nh->mq, env);
354 }
Service notifying the client about changes in the set of addresses it has.
Definition: nat.h:201
Handle for active NAT registrations.
Definition: nat_api.c:71
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
Create a new envelope by copying an existing message.
Definition: mq.c:653
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
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:81
#define GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED
Message to from NAT service notifying us that connection reversal was requested by another peer...
struct GNUNET_MessageHeader * reg
Our registration message.
Definition: nat_api.c:86
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Handle queue errors by reconnecting to NAT.
Definition: nat_api.c:314
Message handler for a specific message type.
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
Service telling a client that connection reversal was requested.
Definition: nat.h:186
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:116
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE
Message to from NAT service notifying us that one of our addresses changed.
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
static void reconnect(struct GNUNET_NAT_Handle *nh)
Task to connect to the NAT service.
Definition: nat_api.c:140
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_api.c:76
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reconnect()

static void reconnect ( struct GNUNET_NAT_Handle nh)
static

Task to connect to the NAT service.

Parameters
nhhandle to reconnect

Definition at line 140 of file nat_api.c.

References AddrEntry::ac, GNUNET_NAT_Handle::address_callback, AddrEntry::addrlen, GNUNET_NAT_Handle::ae_head, GNUNET_NAT_Handle::ae_tail, AddrEntry::app_ctx, GNUNET_NAT_Handle::callback_cls, do_connect(), GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_MQ_destroy(), GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_STD_BACKOFF, GNUNET_NAT_Handle::mq, GNUNET_NAT_Handle::reconnect_delay, and GNUNET_NAT_Handle::reconnect_task.

Referenced by do_connect(), handle_address_change_notification(), and mq_error_handler().

141 {
142  struct AddrEntry *ae;
143 
144  if (NULL != nh->mq)
145  {
146  GNUNET_MQ_destroy (nh->mq);
147  nh->mq = NULL;
148  }
149  while (NULL != (ae = nh->ae_head))
150  {
153  &ae->app_ctx,
154  GNUNET_NO,
155  ae->ac,
156  (const struct sockaddr *) &ae[1],
157  ae->addrlen);
158  GNUNET_free (ae);
159  }
161  nh->reconnect_task =
163 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct AddrEntry * ae_tail
Tail of address DLL.
Definition: nat_api.c:96
enum GNUNET_NAT_AddressClass ac
Address class of the address.
Definition: nat_api.c:59
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:81
void * app_ctx
Place where the application can store data (on add, and retrieve on remove).
Definition: nat_api.c:54
#define GNUNET_NO
Definition: gnunet_common.h:78
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:111
Entry in DLL of addresses of this peer.
Definition: nat_api.c:38
static void do_connect(void *cls)
Task to connect to the NAT service.
Definition: nat_api.c:328
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:1253
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:91
struct GNUNET_TIME_Relative reconnect_delay
How long to wait until we reconnect.
Definition: nat_api.c:121
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:116
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:101
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:64
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_connection_reversal_request()

static int check_connection_reversal_request ( void *  cls,
const struct GNUNET_NAT_ConnectionReversalRequestedMessage crm 
)
static

Check connection reversal request.

Parameters
clsour struct GNUNET_NAT_Handle
crmthe message
Returns
GNUNET_OK if crm is well-formed

Definition at line 174 of file nat_api.c.

References GNUNET_break, GNUNET_OK, GNUNET_SYSERR, GNUNET_NAT_ConnectionReversalRequestedMessage::header, and GNUNET_MessageHeader::size.

177 {
178  if (ntohs (crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in))
179  {
180  GNUNET_break (0);
181  return GNUNET_SYSERR;
182  }
183  return GNUNET_OK;
184 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED.
Definition: nat.h:191
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...
#define GNUNET_SYSERR
Definition: gnunet_common.h:76

◆ handle_connection_reversal_request()

static void handle_connection_reversal_request ( void *  cls,
const struct GNUNET_NAT_ConnectionReversalRequestedMessage crm 
)
static

Handle connection reversal request.

Parameters
clsour struct GNUNET_NAT_Handle
crmthe message

Definition at line 194 of file nat_api.c.

References GNUNET_NAT_Handle::callback_cls, nh, and GNUNET_NAT_Handle::reversal_callback.

197 {
198  struct GNUNET_NAT_Handle *nh = cls;
199 
201  (const struct sockaddr *) &crm[1],
202  sizeof(struct sockaddr_in));
203 }
Handle for active NAT registrations.
Definition: nat_api.c:71
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:111
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

◆ check_address_change_notification()

static int check_address_change_notification ( void *  cls,
const struct GNUNET_NAT_AddressChangeNotificationMessage acn 
)
static

Check address change notification.

Parameters
clsour struct GNUNET_NAT_Handle
acnthe message
Returns
GNUNET_OK if crm is well-formed

Definition at line 214 of file nat_api.c.

References GNUNET_break, GNUNET_OK, GNUNET_SYSERR, GNUNET_NAT_AddressChangeNotificationMessage::header, and GNUNET_MessageHeader::size.

217 {
218  size_t alen = ntohs (acn->header.size) - sizeof(*acn);
219 
220  switch (alen)
221  {
222  case sizeof(struct sockaddr_in): {
223  const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1];
224  if (AF_INET != s4->sin_family)
225  {
226  GNUNET_break (0);
227  return GNUNET_SYSERR;
228  }
229  }
230  break;
231 
232  case sizeof(struct sockaddr_in6): {
233  const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1];
234  if (AF_INET6 != s6->sin6_family)
235  {
236  GNUNET_break (0);
237  return GNUNET_SYSERR;
238  }
239  }
240  break;
241 
242  default:
243  GNUNET_break (0);
244  return GNUNET_SYSERR;
245  }
246  return GNUNET_OK;
247 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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...
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE.
Definition: nat.h:206
#define GNUNET_SYSERR
Definition: gnunet_common.h:76

◆ handle_address_change_notification()

static void handle_address_change_notification ( void *  cls,
const struct GNUNET_NAT_AddressChangeNotificationMessage acn 
)
static

Handle connection reversal request.

Parameters
clsour struct GNUNET_NAT_Handle
acnthe message

Definition at line 257 of file nat_api.c.

References AddrEntry::ac, GNUNET_NAT_AddressChangeNotificationMessage::add_remove, GNUNET_NAT_AddressChangeNotificationMessage::addr_class, GNUNET_NAT_Handle::address_callback, AddrEntry::addrlen, GNUNET_NAT_Handle::ae_head, GNUNET_NAT_Handle::ae_tail, AddrEntry::app_ctx, GNUNET_NAT_Handle::callback_cls, GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_YES, GNUNET_NAT_AddressChangeNotificationMessage::header, AddrEntry::next, nh, reconnect(), and GNUNET_MessageHeader::size.

260 {
261  struct GNUNET_NAT_Handle *nh = cls;
262  size_t alen = ntohs (acn->header.size) - sizeof(*acn);
263  const struct sockaddr *sa = (const struct sockaddr *) &acn[1];
265  struct AddrEntry *ae;
266 
268  "Received address change notification\n");
269  ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class);
270  if (GNUNET_YES == ntohl (acn->add_remove))
271  {
272  ae = GNUNET_malloc (sizeof(*ae) + alen);
273  ae->ac = ac;
274  ae->addrlen = alen;
275  GNUNET_memcpy (&ae[1], sa, alen);
278  &ae->app_ctx,
279  ntohl (acn->add_remove),
280  ac,
281  sa,
282  alen);
283  }
284  else
285  {
286  for (ae = nh->ae_head; NULL != ae; ae = ae->next)
287  if ((ae->addrlen == alen) && (0 == memcmp (&ae[1], sa, alen)))
288  break;
289  if (NULL == ae)
290  {
291  GNUNET_break (0);
292  reconnect (nh);
293  return;
294  }
297  &ae->app_ctx,
298  ntohl (acn->add_remove),
299  ac,
300  sa,
301  alen);
302  GNUNET_free (ae);
303  }
304 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
Handle for active NAT registrations.
Definition: nat_api.c:71
struct AddrEntry * ae_tail
Tail of address DLL.
Definition: nat_api.c:96
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
enum GNUNET_NAT_AddressClass ac
Address class of the address.
Definition: nat_api.c:59
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void * app_ctx
Place where the application can store data (on add, and retrieve on remove).
Definition: nat_api.c:54
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:111
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...
Entry in DLL of addresses of this peer.
Definition: nat_api.c:38
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE.
Definition: nat.h:206
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:91
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
uint32_t addr_class
Type of the address, an enum GNUNET_NAT_AddressClass in NBO.
Definition: nat.h:216
int32_t add_remove
GNUNET_YES to add, GNUNET_NO to remove the address from the list.
Definition: nat.h:211
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
#define GNUNET_log(kind,...)
struct AddrEntry * next
DLL.
Definition: nat_api.c:43
#define GNUNET_YES
Definition: gnunet_common.h:77
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:101
static void reconnect(struct GNUNET_NAT_Handle *nh)
Task to connect to the NAT service.
Definition: nat_api.c:140
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:64
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:

◆ mq_error_handler()

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

Handle queue errors by reconnecting to NAT.

Parameters
clsthe struct GNUNET_NAT_Handle *
errordetails about the error

Definition at line 314 of file nat_api.c.

References nh, and reconnect().

Referenced by do_connect().

315 {
316  struct GNUNET_NAT_Handle *nh = cls;
317 
318  reconnect (nh);
319 }
Handle for active NAT registrations.
Definition: nat_api.c:71
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
static void reconnect(struct GNUNET_NAT_Handle *nh)
Task to connect to the NAT service.
Definition: nat_api.c:140
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_stun_packet()

static int test_stun_packet ( const void *  data,
size_t  len 
)
static

Check if an incoming message is a STUN message.

Parameters
datathe packet
lenthe length of the packet in data
Returns
GNUNET_YES if data is a STUN packet, GNUNET_NO if the packet is invalid (not a stun packet)

Definition at line 479 of file nat_api.c.

References stun_attr::attr, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_OK, stun_attr::len, stun_header::magic, stun_header::msglen, stun_header::msgtype, and STUN_MAGIC_COOKIE.

Referenced by GNUNET_NAT_stun_handle_packet().

480 {
481  const struct stun_header *hdr;
482  const struct stun_attr *attr;
483  uint32_t advertised_message_size;
484  uint32_t message_magic_cookie;
485 
486  /* On entry, 'len' is the length of the UDP payload. After the
487  * initial checks it becomes the size of unprocessed options,
488  * while 'data' is advanced accordingly.
489  */
490  if (len < sizeof(struct stun_header))
491  {
493  "STUN packet too short (only %d, wanting at least %d)\n",
494  (int) len,
495  (int) sizeof(struct stun_header));
496  return GNUNET_NO;
497  }
498  hdr = (const struct stun_header *) data;
499  /* Skip header as it is already in hdr */
500  len -= sizeof(struct stun_header);
501  data += sizeof(struct stun_header);
502 
503  /* len as advertised in the message */
504  advertised_message_size = ntohs (hdr->msglen);
505 
506  message_magic_cookie = ntohl (hdr->magic);
507  /* Compare if the cookie match */
508  if (STUN_MAGIC_COOKIE != message_magic_cookie)
509  {
510  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n");
511  return GNUNET_NO;
512  }
513 
514  if (advertised_message_size > len)
515  {
517  "Scrambled STUN packet length (got %d, expecting %d)\n",
518  advertised_message_size,
519  (int) len);
520  return GNUNET_NO;
521  }
522  len = advertised_message_size;
523  while (len > 0)
524  {
525  if (len < sizeof(struct stun_attr))
526  {
528  "Attribute too short in STUN packet (got %d, expecting %d)\n",
529  (int) len,
530  (int) sizeof(struct stun_attr));
531  return GNUNET_NO;
532  }
533  attr = (const struct stun_attr *) data;
534 
535  /* compute total attribute length */
536  advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
537 
538  /* Check if we still have space in our buffer */
539  if (advertised_message_size > len)
540  {
541  GNUNET_log (
543  "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
544  advertised_message_size,
545  (int) len);
546  return GNUNET_NO;
547  }
548  data += advertised_message_size;
549  len -= advertised_message_size;
550  }
552  "STUN Packet, msg %04x, length: %d\n",
553  ntohs (hdr->msgtype),
554  advertised_message_size);
555  return GNUNET_OK;
556 }
uint16_t attr
Definition: nat_stun.h:53
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t msglen
Definition: nat_stun.h:45
uint16_t msgtype
Definition: nat_stun.h:44
uint16_t len
Definition: nat_stun.h:54
#define STUN_MAGIC_COOKIE
Definition: nat_stun.h:34
#define GNUNET_log(kind,...)
uint32_t magic
Definition: nat_stun.h:46
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function: