GNUnet  0.10.x
gnunet-service-nat_stun.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2015, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
39 #include "platform.h"
40 #include "gnunet_util_lib.h"
41 #include "nat_stun.h"
42 
43 #define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
44 
45 
50 struct StunState
51 {
52  uint16_t attr;
53 };
54 
55 
67 static int
69  const struct stun_attr *attr,
70  uint32_t magic,
71  struct sockaddr_in *arg)
72 {
73  const struct stun_addr *returned_addr;
74  struct sockaddr_in *sa = (struct sockaddr_in *) arg;
75  uint16_t type = ntohs (attr->attr);
76 
77  switch (type)
78  {
80  if ( (st->attr == STUN_XOR_MAPPED_ADDRESS) ||
82  return GNUNET_NO;
83  magic = 0;
84  break;
86  if (st->attr == STUN_XOR_MAPPED_ADDRESS)
87  return GNUNET_NO;
88  break;
90  break;
91  default:
92  return GNUNET_NO;
93  }
94 
95  if (ntohs (attr->len) < sizeof (struct stun_addr))
96  return GNUNET_NO;
97  returned_addr = (const struct stun_addr *)(attr + 1);
98  if (AF_INET != returned_addr->family)
99  return GNUNET_NO;
100  st->attr = type;
101  sa->sin_family = AF_INET;
102  sa->sin_port = returned_addr->port ^ htons (ntohl(magic) >> 16);
103  sa->sin_addr.s_addr = returned_addr->addr ^ magic;
104  return GNUNET_OK;
105 }
106 
107 
121 int
123  size_t len,
124  struct sockaddr_in *arg)
125 {
126  const struct stun_header *hdr;
127  const struct stun_attr *attr;
128  struct StunState st;
129  uint32_t advertised_message_size;
130  uint32_t message_magic_cookie;
131  int ret = GNUNET_SYSERR;
132 
133  /* On entry, 'len' is the length of the UDP payload. After the
134  * initial checks it becomes the size of unprocessed options,
135  * while 'data' is advanced accordingly.
136  */
137  if (len < sizeof(struct stun_header))
138  {
140  "Packet too short to be a STUN packet\n");
141  return GNUNET_NO;
142  }
143  hdr = data;
144  /* Skip header as it is already in hdr */
145  len -= sizeof(struct stun_header);
146  data += sizeof(struct stun_header);
147 
148  /* len as advertised in the message */
149  advertised_message_size = ntohs (hdr->msglen);
150  message_magic_cookie = ntohl (hdr->magic);
151  /* Compare if the cookie match */
152  if (STUN_MAGIC_COOKIE != message_magic_cookie)
153  {
155  "Invalid magic cookie for STUN packet\n");
156  return GNUNET_NO;
157  }
158 
160  "STUN Packet, msg %s (%04x), length: %d\n",
161  stun_msg2str (ntohs (hdr->msgtype)),
162  ntohs (hdr->msgtype),
163  advertised_message_size);
164  if (advertised_message_size > len)
165  {
167  "Scrambled STUN packet length (got %d, expecting %d)\n",
168  advertised_message_size,
169  (int) len);
170  return GNUNET_NO;
171  }
172  len = advertised_message_size;
173  memset (&st, 0, sizeof(st));
174 
175  while (len > 0)
176  {
177  if (len < sizeof (struct stun_attr))
178  {
180  "Attribute too short (got %d, expecting %d)\n",
181  (int) len,
182  (int) sizeof (struct stun_attr));
183  break;
184  }
185  attr = (const struct stun_attr *) data;
186 
187  /* compute total attribute length */
188  advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr);
189 
190  /* Check if we still have space in our buffer */
191  if (advertised_message_size > len)
192  {
194  "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
195  advertised_message_size,
196  (int) len);
197  break;
198  }
199  if (GNUNET_OK ==
200  stun_get_mapped (&st,
201  attr,
202  hdr->magic,
203  arg))
204  ret = GNUNET_OK;
205  data += advertised_message_size;
206  len -= advertised_message_size;
207  }
208  return ret;
209 }
210 
211 /* end of gnunet-service-nat_stun.c */
Context for stun_get_mapped().
uint16_t attr
Definition: nat_stun.h:52
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.
Message types for STUN server resolution.
uint16_t port
Port number.
Definition: nat_stun.h:72
#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
uint32_t addr
IPv4 address.
Definition: nat_stun.h:77
static int ret
Final status code.
Definition: gnunet-arm.c:89
int GNUNET_NAT_stun_handle_packet_(const void *data, size_t len, struct sockaddr_in *arg)
Handle an incoming STUN response.
The format normally used for addresses carried by STUN messages.
Definition: nat_stun.h:60
uint16_t msgtype
Definition: nat_stun.h:43
uint8_t family
Address family, we expect AF_INET.
Definition: nat_stun.h:67
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
uint16_t len
Definition: nat_stun.h:53
#define LOG(kind,...)
#define STUN_MAGIC_COOKIE
Definition: nat_stun.h:34
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static const char * stun_msg2str(int msg)
Print a class and method from a STUN message.
Definition: nat_stun.h:167
uint32_t magic
Definition: nat_stun.h:45
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
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...