Merge branch 'credentials' of git+ssh://gnunet.org/gnunet into credentials
[oweals/gnunet.git] / src / nat / nat_stun.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2015 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * Message types for STUN server resolution
22  *
23  * @file nat/nat_stun.h
24  * @brief Testcase for STUN library
25  * @author Bruno Souza Cabral
26  * @autor Mark Spencer (Original code borrowed from Asterisk)
27  * @author Christian Grothoff
28  */
29
30
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];
38 } GNUNET_PACKED stun_trans_id;
39
40
41 struct stun_header
42 {
43   uint16_t msgtype;
44   uint16_t msglen;
45   uint32_t magic;
46   stun_trans_id id;
47 } GNUNET_PACKED;
48
49
50 struct stun_attr
51 {
52   uint16_t attr;
53   uint16_t len;
54 } GNUNET_PACKED;
55
56
57 /**
58  * The format normally used for addresses carried by STUN messages.
59  */
60 struct stun_addr
61 {
62   uint8_t  unused;
63
64   /**
65    * Address family, we expect AF_INET.
66    */
67   uint8_t  family;
68
69   /**
70    * Port number.
71    */
72   uint16_t port;
73   
74   /**
75    * IPv4 address. Should this be "struct in_addr"?
76    */
77   uint32_t   addr;
78 } GNUNET_PACKED;
79
80
81 /**
82  * STUN message classes 
83  */
84 enum StunClasses {
85   INVALID_CLASS = 0,
86   STUN_REQUEST = 0x0000,
87   STUN_INDICATION = 0x0001,
88   STUN_RESPONSE = 0x0002,
89   STUN_ERROR_RESPONSE = 0x0003
90 };
91
92 enum StunMethods {
93   INVALID_METHOD = 0,
94   STUN_BINDING = 0x0001,
95   STUN_SHARED_SECRET = 0x0002,
96   STUN_ALLOCATE = 0x0003,
97   STUN_REFRESH = 0x0004,
98   STUN_SEND = 0x0006,
99   STUN_DATA = 0x0007,
100   STUN_CREATE_PERMISSION = 0x0008,
101   STUN_CHANNEL_BIND = 0x0009
102 };
103
104
105 /**
106  * Basic attribute types in stun messages.
107  * Messages can also contain custom attributes (codes above 0x7fff)
108  */
109 enum StunAttributes {
110   STUN_MAPPED_ADDRESS = 0x0001,
111   STUN_RESPONSE_ADDRESS = 0x0002,
112   STUN_CHANGE_ADDRESS = 0x0003,
113   STUN_SOURCE_ADDRESS = 0x0004,
114   STUN_CHANGED_ADDRESS = 0x0005,
115   STUN_USERNAME = 0x0006,
116   STUN_PASSWORD = 0x0007,
117   STUN_MESSAGE_INTEGRITY = 0x0008,
118   STUN_ERROR_CODE = 0x0009,
119   STUN_UNKNOWN_ATTRIBUTES = 0x000a,
120   STUN_REFLECTED_FROM = 0x000b,
121   STUN_REALM = 0x0014,
122   STUN_NONCE = 0x0015,
123   STUN_XOR_MAPPED_ADDRESS = 0x0020,
124   STUN_MS_VERSION = 0x8008,
125   STUN_MS_XOR_MAPPED_ADDRESS = 0x8020,
126   STUN_SOFTWARE = 0x8022,
127   STUN_ALTERNATE_SERVER = 0x8023,
128   STUN_FINGERPRINT = 0x8028
129 };
130
131
132 /**
133  * Convert a message to a StunClass
134  *
135  * @param msg the received message
136  * @return the converted StunClass
137  */
138 static int
139 decode_class(int msg)
140 {
141   /* Sorry for the magic, but this maps the class according to rfc5245 */
142   return ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
143 }
144
145 /**
146  * Convert a message to a StunMethod
147  *
148  * @param msg the received message
149  * @return the converted StunMethod
150  */
151 static int
152 decode_method(int msg)
153 {
154   return (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2);
155 }
156
157
158 /**
159  * Print a class and method from a STUN message
160  *
161  * @param msg
162  * @return string with the message class and method
163  */
164 static const char *
165 stun_msg2str (int msg)
166 {
167   static const struct {
168     enum StunClasses value;
169     const char *name;
170   } classes[] = {
171     { STUN_REQUEST, "Request" },
172     { STUN_INDICATION, "Indication" },
173     { STUN_RESPONSE, "Response" },
174     { STUN_ERROR_RESPONSE, "Error Response" },
175     { 0, NULL }
176   };
177   static const struct {
178     enum StunMethods value;
179     const char *name;
180   } methods[] = {
181     { STUN_BINDING, "Binding" },
182     { 0, NULL }
183   };
184   static char result[64];
185   const char *msg_class = NULL;
186   const char *method = NULL;
187   int value;
188
189   value = decode_class (msg);
190   for (unsigned int i = 0; classes[i].name; i++)
191     if (classes[i].value == value)
192     {
193       msg_class = classes[i].name;
194       break;
195     }
196   value = decode_method (msg);
197   for (unsigned int i = 0; methods[i].name; i++)
198     if (methods[i].value == value)
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
212 /**
213  * Print attribute name
214  *
215  * @param msg with a attribute type
216  * @return string with the attribute name
217  */
218 static const char *
219 stun_attr2str (enum StunAttributes msg)
220 {
221   static const struct {
222     enum StunAttributes value;
223     const char *name;
224   } attrs[] = {
225     { STUN_MAPPED_ADDRESS, "Mapped Address" },
226     { STUN_RESPONSE_ADDRESS, "Response Address" },
227     { STUN_CHANGE_ADDRESS, "Change Address" },
228     { STUN_SOURCE_ADDRESS, "Source Address" },
229     { STUN_CHANGED_ADDRESS, "Changed Address" },
230     { STUN_USERNAME, "Username" },
231     { STUN_PASSWORD, "Password" },
232     { STUN_MESSAGE_INTEGRITY, "Message Integrity" },
233     { STUN_ERROR_CODE, "Error Code" },
234     { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" },
235     { STUN_REFLECTED_FROM, "Reflected From" },
236     { STUN_REALM, "Realm" },
237     { STUN_NONCE, "Nonce" },
238     { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" },
239     { STUN_MS_VERSION, "MS Version" },
240     { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" },
241     { STUN_SOFTWARE, "Software" },
242     { STUN_ALTERNATE_SERVER, "Alternate Server" },
243     { STUN_FINGERPRINT, "Fingerprint" },
244     { 0, NULL }
245   };
246
247   for (unsigned int i = 0; attrs[i].name; i++)
248     if (attrs[i].value == msg)
249       return attrs[i].name;
250   return "Unknown Attribute";
251 }
252
253
254 /* end of nat_stun.h */