GNUnet 0.22.1
gnunet-service-nat_stun.h File Reference

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

#include "platform.h"
Include dependency graph for gnunet-service-nat_stun.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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

Author
Bruno Souza Cabral

Definition in file gnunet-service-nat_stun.h.

Function Documentation

◆ 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.

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

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

129{
130 const struct stun_header *hdr;
131 const struct stun_attr *attr;
132 struct StunState st;
133 uint32_t advertised_message_size;
134 uint32_t message_magic_cookie;
135 int ret = GNUNET_SYSERR;
136
137 /* On entry, 'len' is the length of the UDP payload. After the
138 * initial checks it becomes the size of unprocessed options,
139 * while 'data' is advanced accordingly.
140 */
141 if (len < sizeof(struct stun_header))
142 {
144 "Packet too short to be a STUN packet\n");
145 return GNUNET_NO;
146 }
147 hdr = data;
148 /* Skip header as it is already in hdr */
149 len -= sizeof(struct stun_header);
150 data += sizeof(struct stun_header);
151
152 /* len as advertised in the message */
153 advertised_message_size = ntohs (hdr->msglen);
154 message_magic_cookie = ntohl (hdr->magic);
155 /* Compare if the cookie match */
156 if (STUN_MAGIC_COOKIE != message_magic_cookie)
157 {
159 "Invalid magic cookie for STUN packet\n");
160 return GNUNET_NO;
161 }
162
164 "STUN Packet, msg %s (%04x), length: %d\n",
165 stun_msg2str (ntohs (hdr->msgtype)),
166 ntohs (hdr->msgtype),
167 advertised_message_size);
168 if (advertised_message_size > len)
169 {
171 "Scrambled STUN packet length (got %d, expecting %d)\n",
172 advertised_message_size,
173 (int) len);
174 return GNUNET_NO;
175 }
176 len = advertised_message_size;
177 memset (&st, 0, sizeof(st));
178
179 while (len > 0)
180 {
181 if (len < sizeof(struct stun_attr))
182 {
184 "Attribute too short (got %d, expecting %d)\n",
185 (int) len,
186 (int) sizeof(struct stun_attr));
187 break;
188 }
189 attr = (const struct stun_attr *) data;
190
191 /* compute total attribute length */
192 advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
193
194 /* Check if we still have space in our buffer */
195 if (advertised_message_size > len)
196 {
198 "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
199 advertised_message_size,
200 (int) len);
201 break;
202 }
203 if (GNUNET_OK ==
205 attr,
206 hdr->magic,
207 arg))
208 ret = GNUNET_OK;
209 data += advertised_message_size;
210 len -= advertised_message_size;
211 }
212 return ret;
213}
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
static char * data
The data to insert into the dht.
#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.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define STUN_MAGIC_COOKIE
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 len
Definition: nat_stun.h:54
uint16_t attr
Definition: nat_stun.h:53
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, stun_attr::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: