GNUnet  0.11.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 {
38  uint32_t id[3];
40 
41 
43 {
44  uint16_t msgtype;
45  uint16_t msglen;
46  uint32_t magic;
49 
50 
51 struct stun_attr
52 {
53  uint16_t attr;
54  uint16_t len;
56 
57 
61 struct stun_addr
62 {
63  uint8_t unused;
64 
68  uint8_t family;
69 
73  uint16_t port;
74 
78  uint32_t addr;
80 
81 
86 {
88  STUN_REQUEST = 0x0000,
89  STUN_INDICATION = 0x0001,
90  STUN_RESPONSE = 0x0002,
92 };
93 
95 {
97  STUN_BINDING = 0x0001,
99  STUN_ALLOCATE = 0x0003,
100  STUN_REFRESH = 0x0004,
101  STUN_SEND = 0x0006,
102  STUN_DATA = 0x0007,
105 };
106 
107 
113 {
119  STUN_USERNAME = 0x0006,
120  STUN_PASSWORD = 0x0007,
122  STUN_ERROR_CODE = 0x0009,
125  STUN_REALM = 0x0014,
126  STUN_NONCE = 0x0015,
128  STUN_MS_VERSION = 0x8008,
130  STUN_SOFTWARE = 0x8022,
133 };
134 
135 
142 static enum StunClasses
144 {
145  /* Sorry for the magic, but this maps the class according to rfc5245 */
146  return (enum StunClasses) ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
147 }
148 
149 
156 static enum StunMethods
158 {
159  return (enum StunMethods) (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg
160  & 0x3e00)
161  >> 2);
162 }
163 
164 
172 static const char *
174 {
175  static const struct
176  {
177  enum StunClasses value;
178  const char *name;
179  } classes[] = {
180  { STUN_REQUEST, "Request" },
181  { STUN_INDICATION, "Indication" },
182  { STUN_RESPONSE, "Response" },
183  { STUN_ERROR_RESPONSE, "Error Response" },
184  { INVALID_CLASS, NULL }
185  };
186  static const struct
187  {
188  enum StunMethods value;
189  const char *name;
190  } methods[] = {
191  { STUN_BINDING, "Binding" },
192  { INVALID_METHOD, NULL }
193  };
194  static char result[64];
195  const char *msg_class = NULL;
196  const char *method = NULL;
197  enum StunClasses cvalue;
198  enum StunMethods mvalue;
199 
200  cvalue = decode_class (msg);
201  for (unsigned int i = 0; classes[i].name; i++)
202  if (classes[i].value == cvalue)
203  {
204  msg_class = classes[i].name;
205  break;
206  }
207  mvalue = decode_method (msg);
208  for (unsigned int i = 0; methods[i].name; i++)
209  if (methods[i].value == mvalue)
210  {
211  method = methods[i].name;
212  break;
213  }
214  GNUNET_snprintf (result,
215  sizeof(result),
216  "%s %s",
217  method ? : "Unknown Method",
218  msg_class ? : "Unknown Class Message");
219  return result;
220 }
221 
222 
230 static const char *
232 {
233  static const struct
234  {
235  enum StunAttributes value;
236  const char *name;
237  } attrs[] = {
238  { STUN_MAPPED_ADDRESS, "Mapped Address" },
239  { STUN_RESPONSE_ADDRESS, "Response Address" },
240  { STUN_CHANGE_ADDRESS, "Change Address" },
241  { STUN_SOURCE_ADDRESS, "Source Address" },
242  { STUN_CHANGED_ADDRESS, "Changed Address" },
243  { STUN_USERNAME, "Username" },
244  { STUN_PASSWORD, "Password" },
245  { STUN_MESSAGE_INTEGRITY, "Message Integrity" },
246  { STUN_ERROR_CODE, "Error Code" },
247  { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" },
248  { STUN_REFLECTED_FROM, "Reflected From" },
249  { STUN_REALM, "Realm" },
250  { STUN_NONCE, "Nonce" },
251  { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" },
252  { STUN_MS_VERSION, "MS Version" },
253  { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" },
254  { STUN_SOFTWARE, "Software" },
255  { STUN_ALTERNATE_SERVER, "Alternate Server" },
256  { STUN_FINGERPRINT, "Fingerprint" },
257  { 0, NULL }
258  };
259 
260  for (unsigned int i = 0; attrs[i].name; i++)
261  if (attrs[i].value == msg)
262  return attrs[i].name;
263  return "Unknown Attribute";
264 }
265 
266 
267 /* end of nat_stun.h */
uint8_t unused
Definition: nat_stun.h:63
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:53
#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:143
uint16_t port
Port number.
Definition: nat_stun.h:73
stun_trans_id id
Definition: nat_stun.h:47
uint16_t msglen
Definition: nat_stun.h:45
uint32_t addr
IPv4 address.
Definition: nat_stun.h:78
static const char * stun_attr2str(enum StunAttributes msg)
Print attribute name.
Definition: nat_stun.h:231
StunMethods
Definition: nat_stun.h:94
static char * value
Value of the record to add/remove.
The format normally used for addresses carried by STUN messages.
Definition: nat_stun.h:61
uint16_t msgtype
Definition: nat_stun.h:44
uint8_t family
Address family, we expect AF_INET.
Definition: nat_stun.h:68
static int result
Global testing status.
StunAttributes
Basic attribute types in stun messages.
Definition: nat_stun.h:112
uint16_t len
Definition: nat_stun.h:54
StunClasses
STUN message classes.
Definition: nat_stun.h:85
#define GNUNET_PACKED
gcc-ism to get packed structs.
const char * name
static const char * stun_msg2str(int msg)
Print a class and method from a STUN message.
Definition: nat_stun.h:173
uint32_t magic
Definition: nat_stun.h:46
static enum StunMethods decode_method(int msg)
Convert a message to a StunMethod.
Definition: nat_stun.h:157