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 
41 struct stun_header {
42  uint16_t msgtype;
43  uint16_t msglen;
44  uint32_t magic;
47 
48 
49 struct stun_attr {
50  uint16_t attr;
51  uint16_t len;
53 
54 
58 struct stun_addr {
59  uint8_t unused;
60 
64  uint8_t family;
65 
69  uint16_t port;
70 
74  uint32_t addr;
76 
77 
83  STUN_REQUEST = 0x0000,
84  STUN_INDICATION = 0x0001,
85  STUN_RESPONSE = 0x0002,
87 };
88 
91  STUN_BINDING = 0x0001,
93  STUN_ALLOCATE = 0x0003,
94  STUN_REFRESH = 0x0004,
95  STUN_SEND = 0x0006,
96  STUN_DATA = 0x0007,
99 };
100 
101 
112  STUN_USERNAME = 0x0006,
113  STUN_PASSWORD = 0x0007,
115  STUN_ERROR_CODE = 0x0009,
118  STUN_REALM = 0x0014,
119  STUN_NONCE = 0x0015,
121  STUN_MS_VERSION = 0x8008,
123  STUN_SOFTWARE = 0x8022,
126 };
127 
128 
135 static enum StunClasses
137 {
138  /* Sorry for the magic, but this maps the class according to rfc5245 */
139  return (enum StunClasses)((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
140 }
141 
142 
149 static enum StunMethods
151 {
152  return (enum StunMethods)(msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2);
153 }
154 
155 
163 static const char *
165 {
166  static const struct {
167  enum StunClasses value;
168  const char *name;
169  } classes[] = {
170  { STUN_REQUEST, "Request" },
171  { STUN_INDICATION, "Indication" },
172  { STUN_RESPONSE, "Response" },
173  { STUN_ERROR_RESPONSE, "Error Response" },
174  { INVALID_CLASS, NULL }
175  };
176  static const struct {
177  enum StunMethods value;
178  const char *name;
179  } methods[] = {
180  { STUN_BINDING, "Binding" },
181  { INVALID_METHOD, NULL }
182  };
183  static char result[64];
184  const char *msg_class = NULL;
185  const char *method = NULL;
186  enum StunClasses cvalue;
187  enum StunMethods mvalue;
188 
189  cvalue = decode_class(msg);
190  for (unsigned int i = 0; classes[i].name; i++)
191  if (classes[i].value == cvalue)
192  {
193  msg_class = classes[i].name;
194  break;
195  }
196  mvalue = decode_method(msg);
197  for (unsigned int i = 0; methods[i].name; i++)
198  if (methods[i].value == mvalue)
199  {
200  method = methods[i].name;
201  break;
202  }
203  GNUNET_snprintf(result,
204  sizeof(result),
205  "%s %s",
206  method ? : "Unknown Method",
207  msg_class ? : "Unknown Class Message");
208  return result;
209 }
210 
211 
219 static const char *
221 {
222  static const struct {
223  enum StunAttributes value;
224  const char *name;
225  } attrs[] = {
226  { STUN_MAPPED_ADDRESS, "Mapped Address" },
227  { STUN_RESPONSE_ADDRESS, "Response Address" },
228  { STUN_CHANGE_ADDRESS, "Change Address" },
229  { STUN_SOURCE_ADDRESS, "Source Address" },
230  { STUN_CHANGED_ADDRESS, "Changed Address" },
231  { STUN_USERNAME, "Username" },
232  { STUN_PASSWORD, "Password" },
233  { STUN_MESSAGE_INTEGRITY, "Message Integrity" },
234  { STUN_ERROR_CODE, "Error Code" },
235  { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" },
236  { STUN_REFLECTED_FROM, "Reflected From" },
237  { STUN_REALM, "Realm" },
238  { STUN_NONCE, "Nonce" },
239  { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" },
240  { STUN_MS_VERSION, "MS Version" },
241  { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" },
242  { STUN_SOFTWARE, "Software" },
243  { STUN_ALTERNATE_SERVER, "Alternate Server" },
244  { STUN_FINGERPRINT, "Fingerprint" },
245  { 0, NULL }
246  };
247 
248  for (unsigned int i = 0; attrs[i].name; i++)
249  if (attrs[i].value == msg)
250  return attrs[i].name;
251  return "Unknown Attribute";
252 }
253 
254 
255 /* end of nat_stun.h */
uint8_t unused
Definition: nat_stun.h:59
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:50
#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:136
uint16_t port
Port number.
Definition: nat_stun.h:69
stun_trans_id id
Definition: nat_stun.h:45
uint16_t msglen
Definition: nat_stun.h:43
uint32_t addr
IPv4 address.
Definition: nat_stun.h:74
static const char * stun_attr2str(enum StunAttributes msg)
Print attribute name.
Definition: nat_stun.h:220
StunMethods
Definition: nat_stun.h:89
static char * value
Value of the record to add/remove.
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
static int result
Global testing status.
StunAttributes
Basic attribute types in stun messages.
Definition: nat_stun.h:106
uint16_t len
Definition: nat_stun.h:51
const char * name
StunClasses
STUN message classes.
Definition: nat_stun.h:81
#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:164
uint32_t magic
Definition: nat_stun.h:44
static enum StunMethods decode_method(int msg)
Convert a message to a StunMethod.
Definition: nat_stun.h:150