diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-11-30 07:31:09 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-11-30 07:31:09 +0100 |
commit | 1b4333f3a6a4f76aa76fc3abc21e8476ea0a92e3 (patch) | |
tree | 86bbecfcd30d250ab5a123c793595f8ff6ff33b1 /src/nat/nat_stun.h | |
parent | 27068700bceca89cd940816a7b5cd03933d3cc0b (diff) | |
download | gnunet-1b4333f3a6a4f76aa76fc3abc21e8476ea0a92e3.tar.gz gnunet-1b4333f3a6a4f76aa76fc3abc21e8476ea0a92e3.zip |
towards moving STUN logic into new NAT service
Diffstat (limited to 'src/nat/nat_stun.h')
-rw-r--r-- | src/nat/nat_stun.h | 170 |
1 files changed, 157 insertions, 13 deletions
diff --git a/src/nat/nat_stun.h b/src/nat/nat_stun.h index f8d99b164..4c6c178fb 100644 --- a/src/nat/nat_stun.h +++ b/src/nat/nat_stun.h | |||
@@ -17,15 +17,14 @@ | |||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. |
19 | */ | 19 | */ |
20 | |||
21 | /** | 20 | /** |
22 | * Testcase for STUN server resolution | 21 | * Message types for STUN server resolution |
23 | * | 22 | * |
24 | * @file nat/nat_stun.h | 23 | * @file nat/nat_stun.h |
25 | * @brief Testcase for STUN library | 24 | * @brief Testcase for STUN library |
26 | * @author Bruno Souza Cabral | 25 | * @author Bruno Souza Cabral |
27 | * @autor Mark Spencer (Original code borrowed from Asterisk) | 26 | * @autor Mark Spencer (Original code borrowed from Asterisk) |
28 | * | 27 | * @author Christian Grothoff |
29 | */ | 28 | */ |
30 | 29 | ||
31 | 30 | ||
@@ -34,7 +33,10 @@ | |||
34 | 33 | ||
35 | #define STUN_MAGIC_COOKIE 0x2112A442 | 34 | #define STUN_MAGIC_COOKIE 0x2112A442 |
36 | 35 | ||
37 | typedef struct { uint32_t id[3]; } GNUNET_PACKED stun_trans_id; | 36 | typedef struct { |
37 | uint32_t id[3]; | ||
38 | } GNUNET_PACKED stun_trans_id; | ||
39 | |||
38 | 40 | ||
39 | struct stun_header | 41 | struct stun_header |
40 | { | 42 | { |
@@ -44,32 +46,47 @@ struct stun_header | |||
44 | stun_trans_id id; | 46 | stun_trans_id id; |
45 | } GNUNET_PACKED; | 47 | } GNUNET_PACKED; |
46 | 48 | ||
49 | |||
47 | struct stun_attr | 50 | struct stun_attr |
48 | { | 51 | { |
49 | uint16_t attr; | 52 | uint16_t attr; |
50 | uint16_t len; | 53 | uint16_t len; |
51 | } GNUNET_PACKED; | 54 | } GNUNET_PACKED; |
52 | 55 | ||
53 | /* | 56 | |
57 | /** | ||
54 | * The format normally used for addresses carried by STUN messages. | 58 | * The format normally used for addresses carried by STUN messages. |
55 | */ | 59 | */ |
56 | struct stun_addr | 60 | struct stun_addr |
57 | { | 61 | { |
58 | uint8_t unused; | 62 | uint8_t unused; |
63 | |||
64 | /** | ||
65 | * Address family, we expect AF_INET. | ||
66 | */ | ||
59 | uint8_t family; | 67 | uint8_t family; |
68 | |||
69 | /** | ||
70 | * Port number. | ||
71 | */ | ||
60 | uint16_t port; | 72 | uint16_t port; |
73 | |||
74 | /** | ||
75 | * IPv4 address. Should this be "struct in_addr"? | ||
76 | */ | ||
61 | uint32_t addr; | 77 | uint32_t addr; |
62 | } GNUNET_PACKED; | 78 | } GNUNET_PACKED; |
63 | 79 | ||
64 | 80 | ||
65 | 81 | /** | |
66 | /* STUN message classes */ | 82 | * STUN message classes |
83 | */ | ||
67 | enum StunClasses { | 84 | enum StunClasses { |
68 | INVALID_CLASS = 0, | 85 | INVALID_CLASS = 0, |
69 | STUN_REQUEST = 0x0000, | 86 | STUN_REQUEST = 0x0000, |
70 | STUN_INDICATION = 0x0001, | 87 | STUN_INDICATION = 0x0001, |
71 | STUN_RESPONSE = 0x0002, | 88 | STUN_RESPONSE = 0x0002, |
72 | STUN_ERROR_RESPONSE = 0x0003 | 89 | STUN_ERROR_RESPONSE = 0x0003 |
73 | }; | 90 | }; |
74 | 91 | ||
75 | enum StunMethods { | 92 | enum StunMethods { |
@@ -84,7 +101,9 @@ enum StunMethods { | |||
84 | STUN_CHANNEL_BIND = 0x0009 | 101 | STUN_CHANNEL_BIND = 0x0009 |
85 | }; | 102 | }; |
86 | 103 | ||
87 | /* Basic attribute types in stun messages. | 104 | |
105 | /** | ||
106 | * Basic attribute types in stun messages. | ||
88 | * Messages can also contain custom attributes (codes above 0x7fff) | 107 | * Messages can also contain custom attributes (codes above 0x7fff) |
89 | */ | 108 | */ |
90 | enum StunAttributes { | 109 | enum StunAttributes { |
@@ -108,3 +127,128 @@ enum StunAttributes { | |||
108 | STUN_ALTERNATE_SERVER = 0x8023, | 127 | STUN_ALTERNATE_SERVER = 0x8023, |
109 | STUN_FINGERPRINT = 0x8028 | 128 | STUN_FINGERPRINT = 0x8028 |
110 | }; | 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 */ | ||