GNUnet  0.10.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:206
Handle for active NAT registrations.
Definition: nat_api.c:72
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:901
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:83
#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:88
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:190
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:118
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:142
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_api.c:78
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 142 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().

143 {
144  struct AddrEntry *ae;
145 
146  if (NULL != nh->mq)
147  {
148  GNUNET_MQ_destroy (nh->mq);
149  nh->mq = NULL;
150  }
151  while (NULL != (ae = nh->ae_head))
152  {
155  &ae->app_ctx,
156  GNUNET_NO,
157  ae->ac,
158  (const struct sockaddr *) &ae[1],
159  ae->addrlen);
160  GNUNET_free (ae);
161  }
163  nh->reconnect_task =
165 }
#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:98
enum GNUNET_NAT_AddressClass ac
Address class of the address.
Definition: nat_api.c:60
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:83
void * app_ctx
Place where the application can store data (on add, and retrieve on remove).
Definition: nat_api.c:55
#define GNUNET_NO
Definition: gnunet_common.h:81
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:113
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:1246
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:93
struct GNUNET_TIME_Relative reconnect_delay
How long to wait until we reconnect.
Definition: nat_api.c:123
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:118
#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:103
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:65
#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 176 of file nat_api.c.

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

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

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

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

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

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

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

219 {
220  size_t alen = ntohs (acn->header.size) - sizeof (*acn);
221 
222  switch (alen)
223  {
224  case sizeof (struct sockaddr_in): {
225  const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1];
226  if (AF_INET != s4->sin_family)
227  {
228  GNUNET_break (0);
229  return GNUNET_SYSERR;
230  }
231  }
232  break;
233  case sizeof (struct sockaddr_in6): {
234  const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1];
235  if (AF_INET6 != s6->sin6_family)
236  {
237  GNUNET_break (0);
238  return GNUNET_SYSERR;
239  }
240  }
241  break;
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:78
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:211
#define GNUNET_SYSERR
Definition: gnunet_common.h:79

◆ 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:72
struct AddrEntry * ae_tail
Tail of address DLL.
Definition: nat_api.c:98
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:60
#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:55
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:113
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
#define GNUNET_memcpy(dst, src, n)
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE.
Definition: nat.h:211
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:93
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:221
int32_t add_remove
GNUNET_YES to add, GNUNET_NO to remove the address from the list.
Definition: nat.h:216
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:44
#define GNUNET_YES
Definition: gnunet_common.h:80
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:103
static void reconnect(struct GNUNET_NAT_Handle *nh)
Task to connect to the NAT service.
Definition: nat_api.c:142
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:65
#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:72
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:142
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 477 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().

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