GNUnet  0.10.x
nat_stun.h
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 */
31 #define STUN_IGNORE (0)
32 #define STUN_ACCEPT (1)
33 
34 #define STUN_MAGIC_COOKIE 0x2112A442
35 
36 typedef struct {
37  uint32_t id[3];
39 
40 
42 {
43  uint16_t msgtype;
44  uint16_t msglen;
45  uint32_t magic;
48 
49 
50 struct stun_attr
51 {
52  uint16_t attr;
53  uint16_t len;
55 
56 
60 struct stun_addr
61 {
62  uint8_t unused;
63 
67  uint8_t family;
68 
72  uint16_t port;
73 
77  uint32_t addr;
79 
80 
86  STUN_REQUEST = 0x0000,
87  STUN_INDICATION = 0x0001,
88  STUN_RESPONSE = 0x0002,
90 };
91 
94  STUN_BINDING = 0x0001,
96  STUN_ALLOCATE = 0x0003,
97  STUN_REFRESH = 0x0004,
98  STUN_SEND = 0x0006,
99  STUN_DATA = 0x0007,
102 };
103 
104 
115  STUN_USERNAME = 0x0006,
116  STUN_PASSWORD = 0x0007,
118  STUN_ERROR_CODE = 0x0009,
121  STUN_REALM = 0x0014,
122  STUN_NONCE = 0x0015,
124  STUN_MS_VERSION = 0x8008,
126  STUN_SOFTWARE = 0x8022,
129 };
130 
131 
138 static enum StunClasses
140 {
141  /* Sorry for the magic, but this maps the class according to rfc5245 */
142  return (enum StunClasses) ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
143 }
144 
145 
152 static enum StunMethods
154 {
155  return (enum StunMethods) (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2);
156 }
157 
158 
166 static const char *
168 {
169  static const struct {
170  enum StunClasses value;
171  const char *name;
172  } classes[] = {
173  { STUN_REQUEST, "Request" },
174  { STUN_INDICATION, "Indication" },
175  { STUN_RESPONSE, "Response" },
176  { STUN_ERROR_RESPONSE, "Error Response" },
177  { INVALID_CLASS, NULL }
178  };
179  static const struct {
180  enum StunMethods value;
181  const char *name;
182  } methods[] = {
183  { STUN_BINDING, "Binding" },
184  { INVALID_METHOD, NULL }
185  };
186  static char result[64];
187  const char *msg_class = NULL;
188  const char *method = NULL;
189  enum StunClasses cvalue;
190  enum StunMethods mvalue;
191 
192  cvalue = decode_class (msg);
193  for (unsigned int i = 0; classes[i].name; i++)
194  if (classes[i].value == cvalue)
195  {
196  msg_class = classes[i].name;
197  break;
198  }
199  mvalue = decode_method (msg);
200  for (unsigned int i = 0; methods[i].name; i++)
201  if (methods[i].value == mvalue)
202  {
203  method = methods[i].name;
204  break;
205  }
206  GNUNET_snprintf (result,
207  sizeof(result),
208  "%s %s",
209  method ? : "Unknown Method",
210  msg_class ? : "Unknown Class Message");
211  return result;
212 }
213 
214 
222 static const char *
224 {
225  static const struct {
226  enum StunAttributes value;
227  const char *name;
228  } attrs[] = {
229  { STUN_MAPPED_ADDRESS, "Mapped Address" },
230  { STUN_RESPONSE_ADDRESS, "Response Address" },
231  { STUN_CHANGE_ADDRESS, "Change Address" },
232  { STUN_SOURCE_ADDRESS, "Source Address" },
233  { STUN_CHANGED_ADDRESS, "Changed Address" },
234  { STUN_USERNAME, "Username" },
235  { STUN_PASSWORD, "Password" },
236  { STUN_MESSAGE_INTEGRITY, "Message Integrity" },
237  { STUN_ERROR_CODE, "Error Code" },
238  { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" },
239  { STUN_REFLECTED_FROM, "Reflected From" },
240  { STUN_REALM, "Realm" },
241  { STUN_NONCE, "Nonce" },
242  { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" },
243  { STUN_MS_VERSION, "MS Version" },
244  { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" },
245  { STUN_SOFTWARE, "Software" },
246  { STUN_ALTERNATE_SERVER, "Alternate Server" },
247  { STUN_FINGERPRINT, "Fingerprint" },
248  { 0, NULL }
249  };
250 
251  for (unsigned int i = 0; attrs[i].name; i++)
252  if (attrs[i].value == msg)
253  return attrs[i].name;
254  return "Unknown Attribute";
255 }
256 
257 
258 /* end of nat_stun.h */
uint8_t unused
Definition: nat_stun.h:62
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
uint16_t attr
Definition: nat_stun.h:52
#define GNUNET_UNUSED
gcc-ism to document unused arguments
static enum StunClasses decode_class(int msg)
Convert a message to a StunClass.
Definition: nat_stun.h:139
uint16_t port
Port number.
Definition: nat_stun.h:72
stun_trans_id id
Definition: nat_stun.h:46
uint16_t msglen
Definition: nat_stun.h:44
uint32_t addr
IPv4 address.
Definition: nat_stun.h:77
static const char * stun_attr2str(enum StunAttributes msg)
Print attribute name.
Definition: nat_stun.h:223
StunMethods
Definition: nat_stun.h:92
static char * value
Value of the record to add/remove.
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
static int result
Global testing status.
StunAttributes
Basic attribute types in stun messages.
Definition: nat_stun.h:109
uint16_t len
Definition: nat_stun.h:53
const char * name
StunClasses
STUN message classes.
Definition: nat_stun.h:84
#define GNUNET_PACKED
gcc-ism to get packed structs.
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 enum StunMethods decode_method(int msg)
Convert a message to a StunMethod.
Definition: nat_stun.h:153