summaryrefslogtreecommitdiff
path: root/src/nat/nat_stun.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat/nat_stun.h')
-rw-r--r--src/nat/nat_stun.h74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/nat/nat_stun.h b/src/nat/nat_stun.h
index 85b177271..86b4bd0cb 100644
--- a/src/nat/nat_stun.h
+++ b/src/nat/nat_stun.h
@@ -33,12 +33,14 @@
33 33
34#define STUN_MAGIC_COOKIE 0x2112A442 34#define STUN_MAGIC_COOKIE 0x2112A442
35 35
36typedef struct { 36typedef struct
37{
37 uint32_t id[3]; 38 uint32_t id[3];
38} GNUNET_PACKED stun_trans_id; 39} GNUNET_PACKED stun_trans_id;
39 40
40 41
41struct stun_header { 42struct stun_header
43{
42 uint16_t msgtype; 44 uint16_t msgtype;
43 uint16_t msglen; 45 uint16_t msglen;
44 uint32_t magic; 46 uint32_t magic;
@@ -46,7 +48,8 @@ struct stun_header {
46} GNUNET_PACKED; 48} GNUNET_PACKED;
47 49
48 50
49struct stun_attr { 51struct stun_attr
52{
50 uint16_t attr; 53 uint16_t attr;
51 uint16_t len; 54 uint16_t len;
52} GNUNET_PACKED; 55} GNUNET_PACKED;
@@ -55,7 +58,8 @@ struct stun_attr {
55/** 58/**
56 * The format normally used for addresses carried by STUN messages. 59 * The format normally used for addresses carried by STUN messages.
57 */ 60 */
58struct stun_addr { 61struct stun_addr
62{
59 uint8_t unused; 63 uint8_t unused;
60 64
61 /** 65 /**
@@ -78,7 +82,8 @@ struct stun_addr {
78/** 82/**
79 * STUN message classes 83 * STUN message classes
80 */ 84 */
81enum StunClasses { 85enum StunClasses
86{
82 INVALID_CLASS = 0, 87 INVALID_CLASS = 0,
83 STUN_REQUEST = 0x0000, 88 STUN_REQUEST = 0x0000,
84 STUN_INDICATION = 0x0001, 89 STUN_INDICATION = 0x0001,
@@ -86,7 +91,8 @@ enum StunClasses {
86 STUN_ERROR_RESPONSE = 0x0003 91 STUN_ERROR_RESPONSE = 0x0003
87}; 92};
88 93
89enum StunMethods { 94enum StunMethods
95{
90 INVALID_METHOD = 0, 96 INVALID_METHOD = 0,
91 STUN_BINDING = 0x0001, 97 STUN_BINDING = 0x0001,
92 STUN_SHARED_SECRET = 0x0002, 98 STUN_SHARED_SECRET = 0x0002,
@@ -103,7 +109,8 @@ enum StunMethods {
103 * Basic attribute types in stun messages. 109 * Basic attribute types in stun messages.
104 * Messages can also contain custom attributes (codes above 0x7fff) 110 * Messages can also contain custom attributes (codes above 0x7fff)
105 */ 111 */
106enum StunAttributes { 112enum StunAttributes
113{
107 STUN_MAPPED_ADDRESS = 0x0001, 114 STUN_MAPPED_ADDRESS = 0x0001,
108 STUN_RESPONSE_ADDRESS = 0x0002, 115 STUN_RESPONSE_ADDRESS = 0x0002,
109 STUN_CHANGE_ADDRESS = 0x0003, 116 STUN_CHANGE_ADDRESS = 0x0003,
@@ -133,10 +140,10 @@ enum StunAttributes {
133 * @return the converted StunClass 140 * @return the converted StunClass
134 */ 141 */
135static enum StunClasses 142static enum StunClasses
136decode_class(int msg) 143decode_class (int msg)
137{ 144{
138 /* Sorry for the magic, but this maps the class according to rfc5245 */ 145 /* Sorry for the magic, but this maps the class according to rfc5245 */
139 return (enum StunClasses)((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); 146 return (enum StunClasses) ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
140} 147}
141 148
142 149
@@ -147,9 +154,11 @@ decode_class(int msg)
147 * @return the converted StunMethod 154 * @return the converted StunMethod
148 */ 155 */
149static enum StunMethods 156static enum StunMethods
150decode_method(int msg) 157decode_method (int msg)
151{ 158{
152 return (enum StunMethods)(msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); 159 return (enum StunMethods) (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg
160 & 0x3e00)
161 >> 2);
153} 162}
154 163
155 164
@@ -161,9 +170,10 @@ decode_method(int msg)
161 */ 170 */
162GNUNET_UNUSED 171GNUNET_UNUSED
163static const char * 172static const char *
164stun_msg2str(int msg) 173stun_msg2str (int msg)
165{ 174{
166 static const struct { 175 static const struct
176 {
167 enum StunClasses value; 177 enum StunClasses value;
168 const char *name; 178 const char *name;
169 } classes[] = { 179 } classes[] = {
@@ -173,7 +183,8 @@ stun_msg2str(int msg)
173 { STUN_ERROR_RESPONSE, "Error Response" }, 183 { STUN_ERROR_RESPONSE, "Error Response" },
174 { INVALID_CLASS, NULL } 184 { INVALID_CLASS, NULL }
175 }; 185 };
176 static const struct { 186 static const struct
187 {
177 enum StunMethods value; 188 enum StunMethods value;
178 const char *name; 189 const char *name;
179 } methods[] = { 190 } methods[] = {
@@ -186,25 +197,25 @@ stun_msg2str(int msg)
186 enum StunClasses cvalue; 197 enum StunClasses cvalue;
187 enum StunMethods mvalue; 198 enum StunMethods mvalue;
188 199
189 cvalue = decode_class(msg); 200 cvalue = decode_class (msg);
190 for (unsigned int i = 0; classes[i].name; i++) 201 for (unsigned int i = 0; classes[i].name; i++)
191 if (classes[i].value == cvalue) 202 if (classes[i].value == cvalue)
192 { 203 {
193 msg_class = classes[i].name; 204 msg_class = classes[i].name;
194 break; 205 break;
195 } 206 }
196 mvalue = decode_method(msg); 207 mvalue = decode_method (msg);
197 for (unsigned int i = 0; methods[i].name; i++) 208 for (unsigned int i = 0; methods[i].name; i++)
198 if (methods[i].value == mvalue) 209 if (methods[i].value == mvalue)
199 { 210 {
200 method = methods[i].name; 211 method = methods[i].name;
201 break; 212 break;
202 } 213 }
203 GNUNET_snprintf(result, 214 GNUNET_snprintf (result,
204 sizeof(result), 215 sizeof(result),
205 "%s %s", 216 "%s %s",
206 method ? : "Unknown Method", 217 method ? : "Unknown Method",
207 msg_class ? : "Unknown Class Message"); 218 msg_class ? : "Unknown Class Message");
208 return result; 219 return result;
209} 220}
210 221
@@ -217,9 +228,10 @@ stun_msg2str(int msg)
217 */ 228 */
218GNUNET_UNUSED 229GNUNET_UNUSED
219static const char * 230static const char *
220stun_attr2str(enum StunAttributes msg) 231stun_attr2str (enum StunAttributes msg)
221{ 232{
222 static const struct { 233 static const struct
234 {
223 enum StunAttributes value; 235 enum StunAttributes value;
224 const char *name; 236 const char *name;
225 } attrs[] = { 237 } attrs[] = {