-
Notifications
You must be signed in to change notification settings - Fork 16
/
stun.c
416 lines (355 loc) · 10.9 KB
/
stun.c
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#define _GNU_SOURCE
#define MAX_BUFFER_SIZE 1024
#define STUN_NAT_SYMN 0x1
#define STUN_NAT_SYMF 0x2
#define STUN_NAT_OPEN 0x4
#define STUN_NAT_FULL 0x8
#define STUN_NAT_PORT 0x10
#define STUN_NAT_RES 0x20
#define STUN_NAT_BLOCK 0x0
#define STUN_CHANGE_NONE 0x0000000
#define STUN_CHANGE_PORT 0x00000002
#define STUN_CHANGE_IP 0x00000004
#define STUN_CHANGE_BOTH 0x00000006
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/signal.h>
#include <sys/poll.h>
#include <sys/time.h>
#include "stun.h"
#include <pthread.h>
void stun_handle_packet(struct stun_state *st, struct sockaddr_in *src,void *buf, int len);
static void stun_send(unsigned short msgtype,struct stun_state *st, void* data, int len,int testid);
struct sockaddr_in stunserver={AF_INET,};
static const char *stun_msg2str(int msg) {
switch(msg) {
case STUN_BINDREQ:
return "Binding Request";
case STUN_BINDRESP:
return "Binding Response";
case STUN_BINDERR:
return "Binding Error Response";
case STUN_SECREQ:
return "Shared Secret Request";
case STUN_SECRESP:
return "Shared Secret Response";
case STUN_SECERR:
return "Shared Secret Error Response";
}
return "Non-RFC3489 Message";
}
static const char* stun_attr2str(int msg) {
switch(msg) {
case STUN_MAPPED_ADDRESS:
return "Mapped Address";
case STUN_RESPONSE_ADDRESS:
return "Response Address";
case STUN_CHANGE_REQUEST:
return "Change Request";
case STUN_SOURCE_ADDRESS:
return "Source Address";
case STUN_CHANGED_ADDRESS:
return "Changed Address";
case STUN_USERNAME:
return "Username";
case STUN_PASSWORD:
return "Password";
case STUN_MESSAGE_INTEGRITY:
return "Message Integrity";
case STUN_ERROR_CODE:
return "Error Code";
case STUN_UNKNOWN_ATTRIBUTES:
return "Unknown Attributes";
case STUN_REFLECTED_FROM:
return "Reflected From";
}
return "Non-RFC3489 Attribute";
}
struct stun_attr* stun_message() {
struct stun_attr *stunmsg;
stunmsg=malloc(MAX_BUFFER_SIZE-sizeof(struct stun_header));
bzero(stunmsg,sizeof(stunmsg));
return stunmsg;
}
static int stun_attr_change(long changeflag,void *data, int offset) {
unsigned short attrlen=sizeof(changeflag);
long val;
struct stun_attr *attr;
attr=(struct stun_attr*)(data+offset);
attr->attr=htons(STUN_CHANGE_REQUEST);
attr->len=htons(attrlen);
val=htonl(changeflag);
memcpy(&attr->value,(long*)&val,sizeof(val));
return attrlen+sizeof(struct stun_attr);
}
static int stun_attr_string(char *s, short int msgtype, void *data, int offset) {
short int attrlen;
struct stun_attr *attr;
attr=(struct stun_attr*)(data+offset);
attr->attr=htons(msgtype);
attrlen=strlen(s);
memcpy(&attr->value,s,attrlen);
while (attrlen % 4) {
printf("Str Len: %i\n",attrlen);
attrlen++;
}
printf("Str Len: %i\n",attrlen);
attr->len=htons(attrlen);
return attrlen+sizeof(struct stun_attr);
}
static int stun_attr_addr(struct sockaddr_in *sin, short int msgtype, void *data, int offset) {
short int attrlen=sizeof(struct stun_addr);
struct stun_attr *attr;
struct stun_addr *addr;
attr=(struct stun_attr*)(data+offset);
attr->attr=htons(msgtype);
attr->len=htons(attrlen);
addr=(struct stun_addr*)attr->value;
addr->unused = 0;
addr->family = 0x01;
addr->port = sin->sin_port;
addr->addr = sin->sin_addr.s_addr;
return attrlen+sizeof(struct stun_attr);
}
struct sockaddr_in stun_addr_message(struct stun_addr *attrval) {
struct sockaddr_in attraddr={AF_INET,};
attraddr.sin_port=attrval->port;
attraddr.sin_addr.s_addr=attrval->addr;
return attraddr;
}
void ast_rtp_stun_request_peer(struct stun_state *st) {
struct stun_attr *stunmsg=stun_message();
int msglen=stun_attr_change(STUN_CHANGE_NONE,stunmsg,0);
if (st->username) {
msglen+=stun_attr_string(st->username, STUN_USERNAME, stunmsg, msglen);
}
stun_send(STUN_BINDREQ,st,stunmsg,msglen,0);
}
static void stun_send(unsigned short msgtype,struct stun_state *st, void* data, int len,int testid) {
void *buf;
struct stun_header *req;
int x;
struct sockaddr_in *dst;
if (testid > 1) {
dst=&st->caddr;
} else {
dst=&stunserver;
}
buf=malloc(MAX_BUFFER_SIZE);
req=(struct stun_header*)buf;
for (x = 0; x < 4; x++)
req->id[x] = random();
req->id[3] = (req->id[3] & 0xFFFFFF00) | testid;
req->msgtype = htons(msgtype);
if (data) {
if (len) {
memcpy(buf+sizeof(struct stun_header),data,len);
req->msglen = htons(len);
} else
req->msglen = htons(0);
free(data);
} else {
struct stun_attr *stunmsg=stun_message();
len=stun_attr_change(STUN_CHANGE_NONE,stunmsg,0);
memcpy(buf+sizeof(struct stun_header),stunmsg,len);
req->msglen = htons(len);
free(stunmsg);
}
sendto(st->sock, buf, len + sizeof(struct stun_header), 0,(struct sockaddr *)dst, sizeof(*dst));
free(buf);
}
void *data_thread(void *data) {
int rv,len;
struct stun_state *st=data;
struct pollfd psock[1];
unsigned char *buf;
socklen_t sinlen=sizeof(struct sockaddr_in);
struct sockaddr_in sin;
buf=malloc(MAX_BUFFER_SIZE);
bzero(buf,sizeof(buf));
psock[0].fd=st->sock;
psock[0].events=POLLIN;
st->result=0;
st->pcnt=0;
gettimeofday(&st->laststun,0);
while(1) {
rv = poll(psock, 1, -1);
if ((psock[0].revents & POLLIN) && (rv > 0)){
gettimeofday(&st->laststun,0);
len=recvfrom(st->sock,buf,MAX_BUFFER_SIZE,MSG_WAITALL,&sin,&sinlen);
if (len > 0) {
stun_handle_packet(st,&sin,buf,len);
}
}
}
free(buf);
pthread_exit((void *) 0);
}
void stun_handle_packet(struct stun_state *st,struct sockaddr_in *src, void *buf, int len) {
struct stun_header *hdr;
unsigned char *data;
int pcnt,msglen, option_debug=1,stundebug=1,msgtype;
struct stun_attr *stunmsg;
struct stun_attr *attr;
struct sockaddr_in maddr,caddr;
hdr=(struct stun_header *)buf;
msgtype=ntohs(hdr->msgtype);
if ((msgtype != STUN_BINDREQ) && (msgtype != STUN_BINDRESP)) {
if (option_debug)
printf("Dunno what to do with STUN message %04x (%s)\n", msgtype, stun_msg2str(msgtype));
return;
}
if (len < sizeof(struct stun_header)) {
if (option_debug)
printf("Runt STUN packet (only %zd, wanting at least %zd)\n", len, sizeof(struct stun_header));
return;
}
if (stundebug)
printf("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), ntohs(hdr->msglen));
if (ntohs(hdr->msglen) > len - sizeof(struct stun_header)) {
printf("Scrambled STUN packet length (got %d, expecting %d)\n", ntohs(hdr->msglen), (int)(len - sizeof(struct stun_header)));
return;
} else {
len = ntohs(hdr->msglen);
}
maddr=st->bindaddr;
pcnt=hdr->id[3] & 0x000000FF;
data = buf+sizeof(struct stun_header);
if (len > 0) {
while(len) {
if (len < sizeof(struct stun_attr)) {
if (option_debug)
printf("Runt Attribute (got %zd, expecting %zd)\n", len, sizeof(struct stun_attr));
break;
}
attr = (struct stun_attr*)data;
if (ntohs(attr->len) > len) {
if (option_debug)
printf("Inconsistent Attribute (length %d exceeds remaining msg len %zd)\n", ntohs(attr->len), len);
break;
}
switch (ntohs(attr->attr)) {
case STUN_USERNAME:
st->username=(char*)attr->value;
break;
case STUN_PASSWORD:
st->password=(char*)attr->value;
break;
case STUN_MAPPED_ADDRESS:
maddr=stun_addr_message((struct stun_addr*)attr->value);
break;
case STUN_CHANGED_ADDRESS:
caddr=stun_addr_message((struct stun_addr*)attr->value);
break;
case STUN_CHANGE_REQUEST:
printf("Change Request Sent Value %d\n",ntohl(*(long*)attr->value));
break;
default:
if (stundebug)
printf("Ignoring STUN attribute %s (%04x), length %d\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
if (option_debug)
printf("Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
break;
}
data += ntohs(attr->len) + sizeof(struct stun_attr);
len -= ntohs(attr->len) + sizeof(struct stun_attr);
}
} else {
return;
}
switch (ntohs(hdr->msgtype)) {
case STUN_BINDREQ:
stunmsg=stun_message();
msglen=0;
if (st->username) {
msglen=stun_attr_string(st->username, STUN_USERNAME, stunmsg, msglen);
}
msglen+=stun_attr_addr(src,STUN_MAPPED_ADDRESS, stunmsg, msglen);
stun_send(STUN_BINDRESP,st,stunmsg,msglen,0);
break;
case STUN_BINDRESP:
switch (pcnt) {
case 0:
st->maddr=maddr;
st->caddr=caddr;
st->result |= STUN_NAT_SYMN;
if ((st->bindaddr.sin_addr.s_addr == maddr.sin_addr.s_addr) && (st->bindaddr.sin_port == maddr.sin_port))
st->result |= STUN_NAT_SYMF;
stunmsg=stun_message();
msglen=stun_attr_change(STUN_CHANGE_PORT | STUN_CHANGE_IP,stunmsg,0);
stun_send(STUN_BINDREQ,st,stunmsg,msglen,1);
break;
case 1:
if (st->result & STUN_NAT_SYMF) {
st->result |= STUN_NAT_OPEN;
} else {
st->result |= STUN_NAT_FULL;
}
break;
case 2:
if ((maddr.sin_addr.s_addr == st->maddr.sin_addr.s_addr) && (maddr.sin_port == st->maddr.sin_port))
st->result |= STUN_NAT_PORT;
stunmsg=stun_message();
msglen=stun_attr_change(STUN_CHANGE_PORT,stunmsg,0);
stun_send(STUN_BINDREQ,st,stunmsg,msglen,3);
break;
case 3:
st->result |= STUN_NAT_RES;
break;
}
st->pcnt++;
break;
}
return;
}
int main(int argc, char *argv[]) {
struct hostent *hp;
int flags;
struct timeval tv;
struct stun_state st;
pthread_attr_t attr;
pthread_t thread;
gettimeofday(&tv, 0);
srandom(tv.tv_sec + tv.tv_usec);
hp=gethostbyname(argv[1]);
memcpy(&stunserver.sin_addr, hp->h_addr, sizeof(stunserver.sin_addr));
stunserver.sin_port = htons(3478);
st.sock=socket(PF_INET,SOCK_DGRAM,0);
flags = fcntl(st.sock, F_GETFL);
fcntl(st.sock, F_SETFL, flags | O_NONBLOCK);
st.bindaddr.sin_family=AF_INET;
st.bindaddr.sin_addr.s_addr=inet_addr(argv[2]);
st.bindaddr.sin_port=htons((random() % (65535-1023))+1023);
bind(st.sock,(struct sockaddr *)&st.bindaddr,sizeof(struct sockaddr_in));
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
pthread_create(&thread, &attr, data_thread, &st);
stun_send(STUN_BINDREQ,&st,NULL,0,0);
while(1) {
usleep(20000);
gettimeofday(&tv,0);
if ((tv.tv_sec*1000000+tv.tv_usec)-(st.laststun.tv_sec*1000000+st.laststun.tv_usec) > atoi(argv[3])*1000) {
if ((st.result & STUN_NAT_SYMN) && (st.pcnt == 1)) {
stun_send(STUN_BINDREQ,&st,NULL,0,2);
} else {
if (st.result < STUN_NAT_OPEN)
printf("NEW IP:%s:%i Result: %i\n",inet_ntoa(st.bindaddr.sin_addr),ntohs(st.bindaddr.sin_port),st.result);
else
printf("NEW IP:%s:%i Result: %i\n",inet_ntoa(st.maddr.sin_addr),ntohs(st.maddr.sin_port),st.result);
break;
}
}
}
pthread_attr_destroy(&attr);
shutdown(st.sock,SHUT_RDWR);
exit(0);
}