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

327 {
328  struct GNUNET_NAT_Handle *nh = cls;
330  { GNUNET_MQ_hd_var_size(connection_reversal_request,
332  struct
334  nh),
335  GNUNET_MQ_hd_var_size(address_change_notification,
338  nh),
340  struct GNUNET_MQ_Envelope *env;
341 
342  nh->reconnect_task = NULL;
343  nh->mq =
344  GNUNET_CLIENT_connect(nh->cfg, "nat", handlers, &mq_error_handler, nh);
345  if (NULL == nh->mq)
346  {
347  reconnect(nh);
348  return;
349  }
350  env = GNUNET_MQ_msg_copy(nh->reg);
351  GNUNET_MQ_send(nh->mq, env);
352 }
Service notifying the client about changes in the set of addresses it has.
Definition: nat.h:196
Handle for active NAT registrations.
Definition: nat_api.c:70
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:651
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
Message queue for communicating with the NAT service.
Definition: nat_api.c:79
#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:84
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:312
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:182
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:114
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:351
static void reconnect(struct GNUNET_NAT_Handle *nh)
Task to connect to the NAT service.
Definition: nat_api.c:138
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: nat_api.c:74
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 138 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().

139 {
140  struct AddrEntry *ae;
141 
142  if (NULL != nh->mq)
143  {
144  GNUNET_MQ_destroy(nh->mq);
145  nh->mq = NULL;
146  }
147  while (NULL != (ae = nh->ae_head))
148  {
151  &ae->app_ctx,
152  GNUNET_NO,
153  ae->ac,
154  (const struct sockaddr *)&ae[1],
155  ae->addrlen);
156  GNUNET_free(ae);
157  }
159  nh->reconnect_task =
161 }
#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:94
enum GNUNET_NAT_AddressClass ac
Address class of the address.
Definition: nat_api.c:58
struct GNUNET_MQ_Handle * mq
Message queue for communicating with the NAT service.
Definition: nat_api.c:79
void * app_ctx
Place where the application can store data (on add, and retrieve on remove).
Definition: nat_api.c:53
#define GNUNET_NO
Definition: gnunet_common.h:78
void * callback_cls
Closure for the various callbacks.
Definition: nat_api.c:109
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:326
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
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:89
struct GNUNET_TIME_Relative reconnect_delay
How long to wait until we reconnect.
Definition: nat_api.c:119
struct GNUNET_SCHEDULER_Task * reconnect_task
Task scheduled to reconnect to the service.
Definition: nat_api.c:114
#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:821
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:99
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:63
#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 172 of file nat_api.c.

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

175 {
176  if (ntohs(crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in))
177  {
178  GNUNET_break(0);
179  return GNUNET_SYSERR;
180  }
181  return GNUNET_OK;
182 }
#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:186
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 192 of file nat_api.c.

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

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

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

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

215 {
216  size_t alen = ntohs(acn->header.size) - sizeof(*acn);
217 
218  switch (alen)
219  {
220  case sizeof(struct sockaddr_in): {
221  const struct sockaddr_in *s4 = (const struct sockaddr_in *)&acn[1];
222  if (AF_INET != s4->sin_family)
223  {
224  GNUNET_break(0);
225  return GNUNET_SYSERR;
226  }
227  }
228  break;
229 
230  case sizeof(struct sockaddr_in6): {
231  const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&acn[1];
232  if (AF_INET6 != s6->sin6_family)
233  {
234  GNUNET_break(0);
235  return GNUNET_SYSERR;
236  }
237  }
238  break;
239 
240  default:
241  GNUNET_break(0);
242  return GNUNET_SYSERR;
243  }
244  return GNUNET_OK;
245 }
#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:200
#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 255 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.

258 {
259  struct GNUNET_NAT_Handle *nh = cls;
260  size_t alen = ntohs(acn->header.size) - sizeof(*acn);
261  const struct sockaddr *sa = (const struct sockaddr *)&acn[1];
263  struct AddrEntry *ae;
264 
266  "Received address change notification\n");
267  ac = (enum GNUNET_NAT_AddressClass)ntohl(acn->addr_class);
268  if (GNUNET_YES == ntohl(acn->add_remove))
269  {
270  ae = GNUNET_malloc(sizeof(*ae) + alen);
271  ae->ac = ac;
272  ae->addrlen = alen;
273  GNUNET_memcpy(&ae[1], sa, alen);
276  &ae->app_ctx,
277  ntohl(acn->add_remove),
278  ac,
279  sa,
280  alen);
281  }
282  else
283  {
284  for (ae = nh->ae_head; NULL != ae; ae = ae->next)
285  if ((ae->addrlen == alen) && (0 == memcmp(&ae[1], sa, alen)))
286  break;
287  if (NULL == ae)
288  {
289  GNUNET_break(0);
290  reconnect(nh);
291  return;
292  }
295  &ae->app_ctx,
296  ntohl(acn->add_remove),
297  ac,
298  sa,
299  alen);
300  GNUNET_free(ae);
301  }
302 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
Handle for active NAT registrations.
Definition: nat_api.c:70
struct AddrEntry * ae_tail
Tail of address DLL.
Definition: nat_api.c:94
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:58
#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:53
#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:109
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:200
struct AddrEntry * ae_head
Head of address DLL.
Definition: nat_api.c:89
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:210
int32_t add_remove
GNUNET_YES to add, GNUNET_NO to remove the address from the list.
Definition: nat.h:205
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:42
#define GNUNET_YES
Definition: gnunet_common.h:77
GNUNET_NAT_AddressCallback address_callback
Function to call when our addresses change.
Definition: nat_api.c:99
static void reconnect(struct GNUNET_NAT_Handle *nh)
Task to connect to the NAT service.
Definition: nat_api.c:138
socklen_t addrlen
Number of bytes that follow.
Definition: nat_api.c:63
#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 312 of file nat_api.c.

References nh, and reconnect().

Referenced by do_connect().

313 {
314  struct GNUNET_NAT_Handle *nh = cls;
315 
316  reconnect(nh);
317 }
Handle for active NAT registrations.
Definition: nat_api.c:70
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:138
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:50
#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:43
uint16_t msgtype
Definition: nat_stun.h:42
uint16_t len
Definition: nat_stun.h:51
#define STUN_MAGIC_COOKIE
Definition: nat_stun.h:34
#define GNUNET_log(kind,...)
uint32_t magic
Definition: nat_stun.h:44
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: