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  uint16_t attr;
52 };
53 
54 
66 static int
68  const struct stun_attr *attr,
69  uint32_t magic,
70  struct sockaddr_in *arg)
71 {
72  const struct stun_addr *returned_addr;
73  struct sockaddr_in *sa = (struct sockaddr_in *)arg;
74  uint16_t type = ntohs(attr->attr);
75 
76  switch (type)
77  {
79  if ((st->attr == STUN_XOR_MAPPED_ADDRESS) ||
81  return GNUNET_NO;
82  magic = 0;
83  break;
84 
86  if (st->attr == STUN_XOR_MAPPED_ADDRESS)
87  return GNUNET_NO;
88  break;
89 
91  break;
92 
93  default:
94  return GNUNET_NO;
95  }
96 
97  if (ntohs(attr->len) < sizeof(struct stun_addr))
98  return GNUNET_NO;
99  returned_addr = (const struct stun_addr *)(attr + 1);
100  if (AF_INET != returned_addr->family)
101  return GNUNET_NO;
102  st->attr = type;
103  sa->sin_family = AF_INET;
104  sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16);
105  sa->sin_addr.s_addr = returned_addr->addr ^ magic;
106  return GNUNET_OK;
107 }
108 
109 
123 int
125  size_t len,
126  struct sockaddr_in *arg)
127 {
128  const struct stun_header *hdr;
129  const struct stun_attr *attr;
130  struct StunState st;
131  uint32_t advertised_message_size;
132  uint32_t message_magic_cookie;
133  int ret = GNUNET_SYSERR;
134 
135  /* On entry, 'len' is the length of the UDP payload. After the
136  * initial checks it becomes the size of unprocessed options,
137  * while 'data' is advanced accordingly.
138  */
139  if (len < sizeof(struct stun_header))
140  {
142  "Packet too short to be a STUN packet\n");
143  return GNUNET_NO;
144  }
145  hdr = data;
146  /* Skip header as it is already in hdr */
147  len -= sizeof(struct stun_header);
148  data += sizeof(struct stun_header);
149 
150  /* len as advertised in the message */
151  advertised_message_size = ntohs(hdr->msglen);
152  message_magic_cookie = ntohl(hdr->magic);
153  /* Compare if the cookie match */
154  if (STUN_MAGIC_COOKIE != message_magic_cookie)
155  {
157  "Invalid magic cookie for STUN packet\n");
158  return GNUNET_NO;
159  }
160 
162  "STUN Packet, msg %s (%04x), length: %d\n",
163  stun_msg2str(ntohs(hdr->msgtype)),
164  ntohs(hdr->msgtype),
165  advertised_message_size);
166  if (advertised_message_size > len)
167  {
169  "Scrambled STUN packet length (got %d, expecting %d)\n",
170  advertised_message_size,
171  (int)len);
172  return GNUNET_NO;
173  }
174  len = advertised_message_size;
175  memset(&st, 0, sizeof(st));
176 
177  while (len > 0)
178  {
179  if (len < sizeof(struct stun_attr))
180  {
182  "Attribute too short (got %d, expecting %d)\n",
183  (int)len,
184  (int)sizeof(struct stun_attr));
185  break;
186  }
187  attr = (const struct stun_attr *)data;
188 
189  /* compute total attribute length */
190  advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
191 
192  /* Check if we still have space in our buffer */
193  if (advertised_message_size > len)
194  {
196  "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
197  advertised_message_size,
198  (int)len);
199  break;
200  }
201  if (GNUNET_OK ==
202  stun_get_mapped(&st,
203  attr,
204  hdr->magic,
205  arg))
206  ret = GNUNET_OK;
207  data += advertised_message_size;
208  len -= advertised_message_size;
209  }
210  return ret;
211 }
212 
213 /* end of gnunet-service-nat_stun.c */
Context for stun_get_mapped().
uint16_t attr
Definition: nat_stun.h:50
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:69
#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
uint32_t addr
IPv4 address.
Definition: nat_stun.h:74
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:58
uint16_t msgtype
Definition: nat_stun.h:42
uint8_t family
Address family, we expect AF_INET.
Definition: nat_stun.h:64
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint16_t len
Definition: nat_stun.h:51
#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:164
uint32_t magic
Definition: nat_stun.h:44
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...