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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2007 Nokia Corporation. All rights reserved.
* Contact: Rémi Denis-Courmont
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Rémi Denis-Courmont, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include "stun/stunagent.h"
#include "stun/usages/bind.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define MSG_DONTWAIT 0
#define MSG_NOSIGNAL 0
#define alarm(...)
#define close closesocket
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#endif
#undef NDEBUG /* ensure assertions are built-in */
#include <assert.h>
static int listen_dgram (void)
{
struct addrinfo hints, *res;
const struct addrinfo *ptr;
int val = -1;
memset (&hints, 0, sizeof (hints));
hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo (NULL, "0", &hints, &res))
return -1;
for (ptr = res; ptr != NULL; ptr = ptr->ai_next)
{
int fd = socket (ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (fd == -1)
continue;
if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
{
close (fd);
continue;
}
val = fd;
break;
}
freeaddrinfo (res);
return val;
}
/** Incorrect socket family test */
static void bad_family (void)
{
struct sockaddr addr;
struct sockaddr_storage dummy;
int val;
socklen_t dummylen = sizeof(dummy);
memset (&addr, 0, sizeof (addr));
addr.sa_family = AF_UNSPEC;
#ifdef HAVE_SA_LEN
addr.sa_len = sizeof (addr);
#endif
val = stun_usage_bind_run (&addr, sizeof (addr),
&dummy, &dummylen);
assert (val != 0);
}
/** Too small socket address test */
static void small_srv_addr (void)
{
struct sockaddr addr;
struct sockaddr_storage dummy;
int val;
socklen_t dummylen = sizeof(dummy);
memset (&addr, 0, sizeof (addr));
addr.sa_family = AF_INET;
#ifdef HAVE_SA_LEN
addr.sa_len = sizeof (addr);
#endif
val = stun_usage_bind_run (&addr, 1,
&dummy, &dummylen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR);
}
/** Too big socket address test */
static void big_srv_addr (void)
{
uint8_t buf[sizeof (struct sockaddr_storage) + 16];
struct sockaddr_storage dummy;
int val;
socklen_t dummylen = sizeof(dummy);
memset (buf, 0, sizeof (buf));
val = stun_usage_bind_run ((struct sockaddr *)buf, sizeof (buf),
&dummy, &dummylen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR);
}
/** Timeout test */
static void timeout (void)
{
struct sockaddr_storage srv, dummy;
socklen_t srvlen = sizeof (srv);
socklen_t dummylen = sizeof(dummy);
int val;
/* Allocate a local UDP port, so we are 100% sure nobody responds there */
int servfd = listen_dgram ();
assert (servfd != -1);
val = getsockname (servfd, (struct sockaddr *)&srv, &srvlen);
assert (val == 0);
val = stun_usage_bind_run ((struct sockaddr *)&srv, srvlen,
&dummy, &dummylen);
assert (val == STUN_USAGE_BIND_RETURN_TIMEOUT);
close (servfd);
}
/** Malformed responses test */
static void bad_responses (void)
{
struct sockaddr_storage addr;
socklen_t addrlen = sizeof (addr);
ssize_t val, len;
uint8_t buf[STUN_MAX_MESSAGE_SIZE];
uint8_t req[STUN_MAX_MESSAGE_SIZE];
size_t req_len;
StunAgent agent;
StunMessage msg;
StunMessage req_msg;
int servfd, fd;
uint16_t known_attributes[] = {
STUN_ATTRIBUTE_MAPPED_ADDRESS,
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
STUN_ATTRIBUTE_PRIORITY,
STUN_ATTRIBUTE_USERNAME,
STUN_ATTRIBUTE_MESSAGE_INTEGRITY,
STUN_ATTRIBUTE_ERROR_CODE, 0};
stun_agent_init (&agent, known_attributes,
STUN_COMPATIBILITY_RFC5389, 0);
/* Allocate a local UDP port */
servfd = listen_dgram ();
assert (servfd != -1);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
fd = socket (addr.ss_family, SOCK_DGRAM, 0);
assert (fd != -1);
req_len = stun_usage_bind_create (&agent, &req_msg, req, sizeof(req));
assert (req_len > 0);
val = sendto (fd, req, req_len, MSG_DONTWAIT | MSG_NOSIGNAL,
(struct sockaddr *)&addr, addrlen);
assert (val >= 0);
/* Send to/receive from our client instance only */
val = getsockname (fd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
/* Send request instead of response */
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
len = recvfrom (servfd, buf, 1000, MSG_DONTWAIT, NULL, 0);
assert (len >= 20);
assert (stun_agent_validate (&agent, &msg, buf, len, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
val = stun_usage_bind_process (&msg, (struct sockaddr *) &addr, &addrlen,
(struct sockaddr *) &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_INVALID);
/* Send response with wrong request type */
buf[0] |= 0x03;
buf[0] ^= 0x02;
/* Send error response without ERROR-CODE */
buf[1] |= 0x10;
val = stun_usage_bind_process (&msg, (struct sockaddr *) &addr, &addrlen,
(struct sockaddr *) &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_INVALID);
close (fd);
close (servfd);
}
/** Various responses test */
static void responses (void)
{
struct sockaddr_storage addr;
socklen_t addrlen = sizeof (addr);
ssize_t val;
size_t len;
int servfd, fd;
uint8_t buf[STUN_MAX_MESSAGE_SIZE];
uint8_t req[STUN_MAX_MESSAGE_SIZE];
size_t req_len;
StunAgent agent;
StunMessage msg;
StunMessage req_msg;
uint16_t known_attributes[] = {
STUN_ATTRIBUTE_MAPPED_ADDRESS,
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
STUN_ATTRIBUTE_PRIORITY,
STUN_ATTRIBUTE_USERNAME,
STUN_ATTRIBUTE_MESSAGE_INTEGRITY,
STUN_ATTRIBUTE_ERROR_CODE, 0};
stun_agent_init (&agent, known_attributes,
STUN_COMPATIBILITY_RFC5389, 0);
/* Allocate a local UDP port for server */
servfd = listen_dgram ();
assert (servfd != -1);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
/* Allocate a client socket and connect to server */
fd = socket (addr.ss_family, SOCK_DGRAM, 0);
assert (fd != -1);
/* Send error response */
req_len = stun_usage_bind_create (&agent, &req_msg, req, sizeof(req));
assert (req_len > 0);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = sendto (fd, req, req_len, MSG_DONTWAIT | MSG_NOSIGNAL,
(struct sockaddr *)&addr, addrlen);
assert (val >= 0);
val = recvfrom (servfd, buf, 1000, MSG_DONTWAIT, NULL, 0);
assert (val >= 0);
assert (stun_agent_validate (&agent, &msg, buf, val, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
stun_agent_init_error (&agent, &msg, buf, sizeof (buf),
&msg, STUN_ERROR_SERVER_ERROR);
len = stun_agent_finish_message (&agent, &msg, NULL, 0);
assert (len > 0);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = stun_usage_bind_process (&msg, (struct sockaddr *) &addr, &addrlen,
(struct sockaddr *) &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR);
/* Send response with a no mapped address at all */
req_len = stun_usage_bind_create (&agent, &req_msg, req, sizeof(req));
assert (req_len > 0);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = sendto (fd, req, req_len, MSG_DONTWAIT | MSG_NOSIGNAL,
(struct sockaddr *)&addr, addrlen);
assert (val >= 0);
val = recvfrom (servfd, buf, 1000, MSG_DONTWAIT, NULL, 0);
assert (val >= 0);
assert (stun_agent_validate (&agent, &msg, buf, val, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
stun_agent_init_response (&agent, &msg, buf, sizeof (buf), &msg);
len = stun_agent_finish_message (&agent, &msg, NULL, 0);
assert (len > 0);
assert (stun_agent_validate (&agent, &msg, buf, len, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = stun_usage_bind_process (&msg, (struct sockaddr *) &addr, &addrlen,
(struct sockaddr *) &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR);
/* Send old-style response */
req_len = stun_usage_bind_create (&agent, &req_msg, req, sizeof(req));
assert (req_len > 0);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = sendto (fd, req, req_len, MSG_DONTWAIT | MSG_NOSIGNAL,
(struct sockaddr *)&addr, addrlen);
assert (val >= 0);
val = recvfrom (servfd, buf, 1000, MSG_DONTWAIT, NULL, 0);
assert (val >= 0);
assert (stun_agent_validate (&agent, &msg, buf, val, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
stun_agent_init_response (&agent, &msg, buf, sizeof (buf), &msg);
assert (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
(struct sockaddr *) &addr, addrlen) == STUN_MESSAGE_RETURN_SUCCESS);
len = stun_agent_finish_message (&agent, &msg, NULL, 0);
assert (len > 0);
assert (stun_agent_validate (&agent, &msg, buf, len, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = stun_usage_bind_process (&msg, (struct sockaddr *) &addr, &addrlen,
(struct sockaddr *) &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_SUCCESS);
/* End */
close (servfd);
val = close (fd);
assert (val == 0);
}
static void keepalive (void)
{
struct sockaddr_storage addr = {0};
socklen_t addrlen = sizeof (addr);
int val, servfd, fd;
uint8_t buf[STUN_MAX_MESSAGE_SIZE];
size_t len;
StunAgent agent;
StunMessage msg;
uint16_t known_attributes[] = {
STUN_ATTRIBUTE_MAPPED_ADDRESS,
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
STUN_ATTRIBUTE_PRIORITY,
STUN_ATTRIBUTE_USERNAME,
STUN_ATTRIBUTE_MESSAGE_INTEGRITY,
STUN_ATTRIBUTE_ERROR_CODE, 0};
stun_agent_init (&agent, known_attributes,
STUN_COMPATIBILITY_RFC5389, 0);
/* Allocate a local UDP port for server */
servfd = listen_dgram ();
assert (servfd != -1);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
/* Allocate a client socket and connect to server */
fd = socket (addr.ss_family, SOCK_DGRAM, 0);
assert (fd != -1);
/* Keep alive sending smoke test */
len = stun_usage_bind_keepalive (&agent, &msg, buf, sizeof(buf));
assert (len == 20);
val = sendto (fd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL,
(struct sockaddr *)&addr, addrlen);
assert (val >= 0);
/* End */
close (servfd);
val = close (fd);
assert (val == 0);
}
static void test (void (*func) (void), const char *name)
{
alarm (30);
printf ("%s test... ", name);
func ();
puts ("OK");
}
int main (void)
{
#ifdef _WIN32
WSADATA w;
WSAStartup(0x0202, &w);
#endif
test (bad_family, "Bad socket family");
test (small_srv_addr, "Too small server address");
test (big_srv_addr, "Too big server address");
test (bad_responses, "Bad responses");
test (responses, "Error responses");
test (keepalive, "Keep alives");
#ifdef HAVE_POLL
test (timeout, "Binding discovery timeout");
#endif
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}
|