-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathstun_responder.h
executable file
·357 lines (305 loc) · 10 KB
/
stun_responder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#ifndef __STUN_RESPONDER_H__
#define __STUN_RESPONDER_H__
#include "debug.h"
#include <assert.h>
#define ATTR_PACKED __attribute__ ((packed))
#define STUN_PORT 3478
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef struct {
u16 type; // 0x0001=request 0x0101=response
u16 len;
u32 cookie;
char txid[12];
} ATTR_PACKED stun_hdr_t;
typedef struct {
u16 type;
u16 len;
} ATTR_PACKED attr_base;
#define ATTR_XOR_MAPPED_ADDRESS_SET(x, addr, _port) {x.type = htons(0x20); x.len = htons(0x8); x.proto = htons(0x1); x.port = _port; x.ip = addr;}
#define ATTR_MAPPED_ADDRESS_SET(x, addr, _port) {x.type = htons(0x1); x.len = htons(0x8); x.proto = htons(0x1); x.port = _port; x.ip = addr;}
#define ATTR_RELAY_ADDRESS_SET(x, addr, _port) {x.type = htons(0x16); x.len = htons(0x8); x.proto = htons(0x1); x.port = _port; x.ip = addr;}
#define ATTR_SRC_ADDRESS_SET(x, addr, _port) {x.type = htons(0x04); x.len = htons(0x8); x.proto = htons(0x1); x.port = _port; x.ip = addr;}
#define ATTR_CHG_ADDRESS_SET(x, addr, _port) {x.type = htons(0x05); x.len = htons(0x8); x.proto = htons(0x1); x.port = _port; x.ip = addr;}
typedef struct {
u16 type; // 0x0020
u16 len; // 0x0008;
u16 proto; // 0x0001
u16 port;
u32 ip;
} ATTR_PACKED attr_xor_mapped_address;
typedef struct {
u16 type; // 0x08;
u16 len; // 20
char hmac_sha1[20];
} ATTR_PACKED attr_hmac_sha1;
#define ATTR_FINGERPRINT_SET(x, _crc) {x.type = htons(0x8028); x.len = htons(4); x.crc32 = _crc;}
typedef struct {
u16 type; // 0x8028
u16 len; // 4
u32 crc32;
} ATTR_PACKED attr_fingerprint;
#define ATTR_USERNAME_TYPE 0x06
#define ATTR_USERNAME_SET(x, _name) {(x).type = htons(0x06); (x).len = htons(strlen(_name)); memset((x).name, 0, sizeof((x).name)); strcpy((x).name, _name);}
typedef struct {
u16 type; // 0x06
u16 len; // 32 - PAD
char name[12]; // pad with 0x0
} ATTR_PACKED attr_username_short;
typedef struct {
u16 type; // 0x06
u16 len; // 32 - PAD
char name[20]; // pad with 0x0
} ATTR_PACKED attr_username_med;
typedef struct {
u16 type; // 0x06
u16 len; // 32 - PAD
char name[64]; // pad with 0x0
//char name[28]; // pad with 0x0
} ATTR_PACKED attr_username_long;
typedef struct {
u16 type;
u16 len;
char name[4];
} ATTR_PACKED attr_username;
typedef struct {
u16 type; // 0x06
u16 len; // 32 - PAD
char name[64]; // pad with 0x0
//char name[28]; // pad with 0x0
} ATTR_PACKED attr_softwarename;
typedef struct {
u16 type; // 0x06
u16 len; // 32 - PAD
char name[8]; // pad with 0x0
//char name[28]; // pad with 0x0
} ATTR_PACKED attr_softwarename_libjuice;
#define ATTR_USECANDIDATE_TYPE 0x0025
typedef struct {
u16 type; // 0x0025
u16 len; // 0
} ATTR_PACKED attr_usecandidate;
#define ATTR_PRIORITY_TYPE 0x0024
typedef struct {
u16 type; // 0x0024
u16 len; // 4
u32 pri;
} ATTR_PACKED attr_priority;
#define ATTR_ICECONTROLLING_TYPE 0x802a
typedef struct {
u16 type; // 0x802a
u16 len; // 8
char tie_breaker[8];
} ATTR_PACKED attr_icecontrolling;
#define ATTR_REQUEST_TRANSPORT_INIT(x) {x.type = htons(0x0019); x.len = htons(0x04); x.protocol = htonl(0x11);}
typedef struct {
u16 type; //0x0019
u16 len; //4
u32 protocol; // UDP=11
} ATTR_PACKED attr_request_transport;
#define ATTR_LIFETIME_INIT(x) {x.type = htons(0x000d); x.len = htons(4); x.lifetime = htonl(3600);}
typedef struct {
u16 type; //0x000d
u16 len; //4
u32 lifetime; // 3600
} ATTR_PACKED attr_lifetime;
typedef struct
{
stun_hdr_t hdr;
union {
struct {
attr_username_long username;
attr_usecandidate usecandidate;
attr_priority priority;
attr_icecontrolling icecontrolling;
attr_hmac_sha1 hmac_sha1;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_request1;
struct {
attr_username_med username;
attr_usecandidate usecandidate;
attr_priority priority;
attr_icecontrolling icecontrolling;
attr_hmac_sha1 hmac_sha1;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_request2;
struct {
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_request3;
struct {
attr_priority priority;
attr_icecontrolling icecontrolling;
attr_softwarename_libjuice software;
attr_username_short username;
attr_hmac_sha1 hmac_sha1;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_request4;
struct {
attr_xor_mapped_address xor_mapped_address;
attr_hmac_sha1 hmac_sha1;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_response1;
struct {
attr_xor_mapped_address xor_mapped_address;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_response2;
struct {
attr_xor_mapped_address mapped_address;
attr_xor_mapped_address src_address;
attr_xor_mapped_address chg_address;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_binding_response3;
struct {
attr_request_transport req_trans;
attr_lifetime lifetime;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_allocate_request1;
struct {
attr_xor_mapped_address relayed_address;
attr_xor_mapped_address xor_mapped_address;
attr_lifetime lifetime;
attr_fingerprint fingerprint;
} ATTR_PACKED stun_allocate_response1;
char udp_buffer[1024];
} ATTR_PACKED attrs;
} ATTR_PACKED stun_binding_msg_t;
typedef struct {
stun_hdr_t *hdr;
attr_username *attr_username;
attr_usecandidate* usecandidate;
attr_priority* priority;
attr_icecontrolling* icecontrolling;
attr_hmac_sha1* hmac_sha1;
attr_fingerprint* fingerprint;
unsigned int len;
} stun_build_msg_t;
#define STUN_ATTR_USERNAME_SET(x, uname) {x.attr_username->len = htons(strlen(uname)); x.attr_username->type = htons(ATTR_USERNAME_TYPE); strcpy(x.attr_username->name, uname); }
void stun_build_msg_init(stun_build_msg_t* dest, stun_binding_msg_t* msg, char* usernameA, char* usernameB)
{
int username_len = strlen(usernameA) + strlen(usernameB) + 1;
u8* ptr = (u8*) msg;
dest->hdr = (stun_hdr_t*) ptr;
ptr += sizeof(stun_hdr_t);
int pad = (username_len % 4);
if(pad != 0) pad = 4 - pad;
dest->attr_username = (attr_username*) ptr; ptr += sizeof(attr_base); ptr += (username_len + pad);
dest->usecandidate = (attr_usecandidate*) ptr; ptr += sizeof(attr_usecandidate);
dest->priority = (attr_priority*) ptr; ptr += sizeof(attr_priority);
dest->icecontrolling = (attr_icecontrolling*) ptr; ptr += sizeof(attr_icecontrolling);
dest->hmac_sha1 = (attr_hmac_sha1*) ptr; ptr += sizeof(attr_hmac_sha1);
dest->fingerprint = (attr_fingerprint*) ptr; ptr += sizeof(attr_fingerprint);
dest->len = ptr - (u8*) msg;
char stun_user[64];
sprintf(stun_user, "%s:%s", usernameA, usernameB);
STUN_ATTR_USERNAME_SET( (*dest), stun_user);
}
typedef unsigned int stun_id_t;
typedef enum
{
PKT_TYPE_STUN = 0,
PKT_TYPE_DTLS,
PKT_TYPE_SRTP,
PKT_TYPE_UNKNOWN
} pkt_type_t;
inline static pkt_type_t
pktType(unsigned char* buf, unsigned int len)
{
if(len >= 3 &&
(buf[0] == 0x01 || buf[0] == 0x00) &&
(buf[1] == 0x01 || buf[1] == 0x11 || buf[1] == 0x03) &&
buf[2] == 0x00)
{
return PKT_TYPE_STUN;
}
if(len >= 2 &&
(buf[0] == 90 && buf[1] == 0x6d))
{
/* TURN datachannel message */
return PKT_TYPE_STUN;
}
if(len >= 3 &&
(buf[0] == 0x14 || buf[0] == 0x16) &&
(buf[1] == 0xfe) &&
(buf[2] == 0xfd || buf[2] == 0xff))
{
return PKT_TYPE_DTLS;
}
if(len >= 2 &&
buf[0] == 0x80 || buf[0] == 0x90 || buf[0] == 0x81
)
{
return PKT_TYPE_SRTP;
}
//HD(buf, len);
return PKT_TYPE_UNKNOWN;
}
const static stun_id_t STUN_ID_UNKNOWN = 1;
inline static stun_id_t
stunID(unsigned char* buf, unsigned int len)
{
unsigned int l = STUN_ID_UNKNOWN;
unsigned int offset = 4;
stun_binding_msg_t *bind_msg = (stun_binding_msg_t*) buf;
/*
unsigned int offset = 32;
*/
if(pktType(buf, len) == PKT_TYPE_STUN && len >= offset + sizeof(l))
{
memcpy(&l, &(buf[offset]), sizeof(l));
}
if(len >= sizeof(stun_hdr_t)+sizeof(bind_msg->attrs.stun_binding_request2))
{
l = 0;
int i = 0;
while(i < sizeof(bind_msg->attrs.stun_binding_request2.username.name))
{
l += bind_msg->attrs.stun_binding_request2.username.name[i];
i++;
}
}
else
{
printf("STUN exception: %d len=%d\n", __LINE__, len);
}
return l;
}
void
stun_username(unsigned char* buf, int len, char* uname_out)
{
stun_binding_msg_t *bind_msg = (stun_binding_msg_t*) buf;
uname_out[0] = '\0';
if(len >= sizeof(stun_hdr_t)+sizeof(bind_msg->attrs.stun_binding_request4))
{
unsigned short l = ntohs(bind_msg->attrs.stun_binding_request4.username.len);
if(l <= 64)
{
memcpy(uname_out, bind_msg->attrs.stun_binding_request4.username.name, l);
uname_out[l] = '\0';
}
else
{
// seeing this only during disconnect so save to ignore
//printf("stun_username WARN: l > 64\n", l);
}
}
else if(len <= sizeof(stun_hdr_t)+sizeof(bind_msg->attrs.stun_binding_request1))
{
unsigned short l = ntohs(bind_msg->attrs.stun_binding_request1.username.len);
if(l <= 64)
{
memcpy(uname_out, bind_msg->attrs.stun_binding_request1.username.name, l);
uname_out[l] = '\0';
}
else
{
// seeing this only during disconnect so save to ignore
//printf("stun_username WARN: l > 64\n", l);
}
}
else
{
assert(0);
}
}
#endif