GNUnet  0.20.0
gnunet-service-nat_stun.c File Reference

This code provides some support for doing STUN transactions. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "nat_stun.h"
Include dependency graph for gnunet-service-nat_stun.c:

Go to the source code of this file.

Data Structures

struct  StunState
 Context for stun_get_mapped(). More...


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


static int stun_get_mapped (struct StunState *st, const struct stun_attr *attr, uint32_t magic, struct sockaddr_in *arg)
 Extract the STUN_MAPPED_ADDRESS from the stun response. More...
int GNUNET_NAT_stun_handle_packet_ (const void *data, size_t len, struct sockaddr_in *arg)
 Handle an incoming STUN response. More...

Detailed Description

This code provides some support for doing STUN transactions.

We receive the simplest possible packet as the STUN server and try to respond properly.

All STUN packets start with a simple header made of a type, length (excluding the header) and a 16-byte random transaction id. Following the header we may have zero or more attributes, each structured as a type, length and a value (whose format depends on the type, but often contains addresses). Of course all fields are in network format.

This code was based on ministun.c.

Functions for STUN functionality

Bruno Souza Cabral

Definition in file gnunet-service-nat_stun.c.

Macro Definition Documentation


#define LOG (   kind,
)    GNUNET_log_from (kind, "stun", __VA_ARGS__)

Definition at line 43 of file gnunet-service-nat_stun.c.

Function Documentation

◆ stun_get_mapped()

static int stun_get_mapped ( struct StunState st,
const struct stun_attr attr,
uint32_t  magic,
struct sockaddr_in *  arg 

Extract the STUN_MAPPED_ADDRESS from the stun response.

This is used as a callback for stun_handle_response when called from stun_request.

[out]stpointer where we will set the type
attrreceived stun attribute
magicMagic cookie
[out]argpointer to a sockaddr_in where we will set the reported IP and port
GNUNET_OK if arg was initialized

Definition at line 68 of file gnunet-service-nat_stun.c.

72 {
73  const struct stun_addr *returned_addr;
74  struct sockaddr_in *sa = (struct sockaddr_in *) arg;
75  uint16_t type = ntohs (attr->attr);
77  switch (type)
78  {
80  if ((st->attr == STUN_XOR_MAPPED_ADDRESS) ||
81  (st->attr == STUN_MS_XOR_MAPPED_ADDRESS))
82  return GNUNET_NO;
83  magic = 0;
84  break;
87  if (st->attr == STUN_XOR_MAPPED_ADDRESS)
88  return GNUNET_NO;
89  break;
92  break;
94  default:
95  return GNUNET_NO;
96  }
98  if (ntohs (attr->len) < sizeof(struct stun_addr))
99  return GNUNET_NO;
100  returned_addr = (const struct stun_addr *) (attr + 1);
101  if (AF_INET != returned_addr->family)
102  return GNUNET_NO;
103  st->attr = type;
104  sa->sin_family = AF_INET;
105  sa->sin_port = returned_addr->port ^ htons (ntohl (magic) >> 16);
106  sa->sin_addr.s_addr = returned_addr->addr ^ magic;
107  return GNUNET_OK;
108 }
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
Definition: nat_stun.h:129
Definition: nat_stun.h:114
Definition: nat_stun.h:127
The format normally used for addresses carried by STUN messages.
Definition: nat_stun.h:62
uint8_t family
Address family, we expect AF_INET.
Definition: nat_stun.h:68
uint32_t addr
IPv4 address.
Definition: nat_stun.h:78
uint16_t port
Port number.
Definition: nat_stun.h:73
uint16_t len
Definition: nat_stun.h:54
uint16_t attr
Definition: nat_stun.h:53
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

References stun_addr::addr, find_typedefs::arg, stun_attr::attr, stun_addr::family, GNUNET_NO, GNUNET_OK, stun_attr::len, stun_addr::port, st, STUN_MAPPED_ADDRESS, STUN_MS_XOR_MAPPED_ADDRESS, STUN_XOR_MAPPED_ADDRESS, and type.

Referenced by GNUNET_NAT_stun_handle_packet_().

Here is the caller graph for this function:

◆ GNUNET_NAT_stun_handle_packet_()

int GNUNET_NAT_stun_handle_packet_ ( const void *  data,
size_t  len,
struct sockaddr_in *  arg 

Handle an incoming STUN response.

Do some basic sanity checks on packet size and content, try to extract information. At the moment this only processes BIND requests, and returns the externally visible address of the original request.

datathe packet
lenthe length of the packet in data
[out]argsockaddr_in where we will set our discovered address
GNUNET_OK on success, GNUNET_NO if the packet is invalid (not a stun packet)

Definition at line 125 of file gnunet-service-nat_stun.c.

128 {
129  const struct stun_header *hdr;
130  const struct stun_attr *attr;
131  struct StunState st;
132  uint32_t advertised_message_size;
133  uint32_t message_magic_cookie;
134  int ret = GNUNET_SYSERR;
136  /* On entry, 'len' is the length of the UDP payload. After the
137  * initial checks it becomes the size of unprocessed options,
138  * while 'data' is advanced accordingly.
139  */
140  if (len < sizeof(struct stun_header))
141  {
143  "Packet too short to be a STUN packet\n");
144  return GNUNET_NO;
145  }
146  hdr = data;
147  /* Skip header as it is already in hdr */
148  len -= sizeof(struct stun_header);
149  data += sizeof(struct stun_header);
151  /* len as advertised in the message */
152  advertised_message_size = ntohs (hdr->msglen);
153  message_magic_cookie = ntohl (hdr->magic);
154  /* Compare if the cookie match */
155  if (STUN_MAGIC_COOKIE != message_magic_cookie)
156  {
158  "Invalid magic cookie for STUN packet\n");
159  return GNUNET_NO;
160  }
163  "STUN Packet, msg %s (%04x), length: %d\n",
164  stun_msg2str (ntohs (hdr->msgtype)),
165  ntohs (hdr->msgtype),
166  advertised_message_size);
167  if (advertised_message_size > len)
168  {
170  "Scrambled STUN packet length (got %d, expecting %d)\n",
171  advertised_message_size,
172  (int) len);
173  return GNUNET_NO;
174  }
175  len = advertised_message_size;
176  memset (&st, 0, sizeof(st));
178  while (len > 0)
179  {
180  if (len < sizeof(struct stun_attr))
181  {
183  "Attribute too short (got %d, expecting %d)\n",
184  (int) len,
185  (int) sizeof(struct stun_attr));
186  break;
187  }
188  attr = (const struct stun_attr *) data;
190  /* compute total attribute length */
191  advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
193  /* Check if we still have space in our buffer */
194  if (advertised_message_size > len)
195  {
197  "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
198  advertised_message_size,
199  (int) len);
200  break;
201  }
202  if (GNUNET_OK ==
204  attr,
205  hdr->magic,
206  arg))
207  ret = GNUNET_OK;
208  data += advertised_message_size;
209  len -= advertised_message_size;
210  }
211  return ret;
212 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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...
#define LOG(kind,...)
static int stun_get_mapped(struct StunState *st, const struct stun_attr *attr, uint32_t magic, struct sockaddr_in *arg)
Extract the STUN_MAPPED_ADDRESS from the stun response.
Definition: nat_stun.h:34
static const char * stun_msg2str(int msg)
Print a class and method from a STUN message.
Definition: nat_stun.h:173
Context for stun_get_mapped().
uint16_t msglen
Definition: nat_stun.h:45
uint16_t msgtype
Definition: nat_stun.h:44
uint32_t magic
Definition: nat_stun.h:46

References find_typedefs::arg, stun_attr::attr, data, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, len, LOG, stun_header::magic, stun_header::msglen, stun_header::msgtype, ret, st, stun_get_mapped(), STUN_MAGIC_COOKIE, and stun_msg2str().

Referenced by handle_stun().

Here is the call graph for this function:
Here is the caller graph for this function: