Debian Codegraph @

Xpjproject/pjnath/src/pjnath-test/stun_sock_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20
21 #define THIS_FILE "stun_sock_test.c"
22
23 enum {
24 RESPOND_STUN = 1,
25 WITH_MAPPED = 2,
26 WITH_XOR_MAPPED = 4,
27 ECHO = 8
28 };
29
30 /*
31 * Simple STUN server
32 */
33 struct stun_srv
34 {
35 pj_activesock_t *asock;
36 unsigned flag;
37 pj_sockaddr addr;
38 unsigned rx_cnt;
39 pj_ioqueue_op_key_t send_key;
40 pj_str_t ip_to_send;
41 pj_uint16_t port_to_send;
42 };
43
44 static pj_bool_t srv_on_data_recvfrom(pj_activesock_t *asock,
45 void *data,
46 pj_size_t size,
47 const pj_sockaddr_t *src_addr,
48 int addr_len,
49 pj_status_t status)
50 {
51 struct stun_srv *srv;
52 pj_ssize_t sent;
53
54 srv = (struct stun_srv*) pj_activesock_get_user_data(asock);
55
56 /* Ignore error */
57 if (status != PJ_SUCCESS)
58 return PJ_TRUE;
59
60 ++srv->rx_cnt;
61
62 /* Ignore if we're not responding */
63 if (srv->flag & RESPOND_STUN) {
64 pj_pool_t *pool;
65 pj_stun_msg *req_msg, *res_msg;
66
67 pool = pj_pool_create(mem, "stunsrv", 512, 512, NULL);
68
69 /* Parse request */
70 status = pj_stun_msg_decode(pool, (pj_uint8_t*)data, size,
71 PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET,
72 &req_msg, NULL, NULL);
73 if (status != PJ_SUCCESS) {
74 app_perror(" pj_stun_msg_decode()", status);
75 pj_pool_release(pool);
76 return PJ_TRUE;
77 }
78
79 /* Create response */
80 status = pj_stun_msg_create(pool, PJ_STUN_BINDING_RESPONSE, PJ_STUN_MAGIC,
81 req_msg->hdr.tsx_id, &res_msg);
82 if (status != PJ_SUCCESS) {
83 app_perror(" pj_stun_msg_create()", status);
84 pj_pool_release(pool);
85 return PJ_TRUE;
86 }
87
88 /* Add MAPPED-ADDRESS or XOR-MAPPED-ADDRESS (or don't add) */
89 if (srv->flag & WITH_MAPPED) {
90 pj_sockaddr addr;
91 pj_bool_t use_ipv6 = (srv->addr.addr.sa_family == pj_AF_INET6());
92
93 pj_sockaddr_init(GET_AF(use_ipv6), &addr, &srv->ip_to_send,
94 srv->port_to_send);
95
96 pj_stun_msg_add_sockaddr_attr(pool, res_msg, PJ_STUN_ATTR_MAPPED_ADDR,
97 PJ_FALSE, &addr, sizeof(addr));
98 } else if (srv->flag & WITH_XOR_MAPPED) {
99 pj_sockaddr addr;
100 pj_bool_t use_ipv6 = (srv->addr.addr.sa_family == pj_AF_INET6());
101
102 pj_sockaddr_init(GET_AF(use_ipv6), &addr, &srv->ip_to_send,
103 srv->port_to_send);
104
105 pj_stun_msg_add_sockaddr_attr(pool, res_msg,
106 PJ_STUN_ATTR_XOR_MAPPED_ADDR,
107 PJ_TRUE, &addr, sizeof(addr));
108 }
109
110 /* Encode */
111 status = pj_stun_msg_encode(res_msg, (pj_uint8_t*)data, 100, 0,
112 NULL, &size);
113 if (status != PJ_SUCCESS) {
114 app_perror(" pj_stun_msg_encode()", status);
115 pj_pool_release(pool);
116 return PJ_TRUE;
117 }
118
119 /* Send back */
120 sent = size;
121 pj_activesock_sendto(asock, &srv->send_key, data, &sent, 0,
122 src_addr, addr_len);
123
124 pj_pool_release(pool);
125
126 } else if (srv->flag & ECHO) {
127 /* Send back */
128 sent = size;
129 pj_activesock_sendto(asock, &srv->send_key, data, &sent, 0,
130 src_addr, addr_len);
131
132 }
133
134 return PJ_TRUE;
135 }
136
137 static pj_status_t create_server(pj_pool_t *pool,
138 pj_ioqueue_t *ioqueue,
139 unsigned flag,
140 pj_bool_t use_ipv6,
141 struct stun_srv **p_srv)
142 {
143 struct stun_srv *srv;
144 pj_activesock_cb activesock_cb;
145 pj_status_t status;
146
147 srv = PJ_POOL_ZALLOC_T(pool, struct stun_srv);
148 srv->flag = flag;
149 srv->ip_to_send = (use_ipv6)?pj_str("2002:101:101::"):pj_str("1.1.1.1");
150 srv->port_to_send = 1000;
151
152 status = pj_sockaddr_init(GET_AF(use_ipv6), &srv->addr, NULL, 0);
153 if (status != PJ_SUCCESS)
154 return status;
155
156 pj_bzero(&activesock_cb, sizeof(activesock_cb));
157 activesock_cb.on_data_recvfrom = &srv_on_data_recvfrom;
158 status = pj_activesock_create_udp(pool, &srv->addr, NULL, ioqueue,
159 &activesock_cb, srv, &srv->asock,
160 &srv->addr);
161 if (status != PJ_SUCCESS)
162 return status;
163
164 pj_ioqueue_op_key_init(&srv->send_key, sizeof(srv->send_key));
165
166 status = pj_activesock_start_recvfrom(srv->asock, pool, 512, 0);
167 if (status != PJ_SUCCESS) {
168 pj_activesock_close(srv->asock);
169 return status;
170 }
171
172 *p_srv = srv;
173 return PJ_SUCCESS;
174 }
175
176 static void destroy_server(struct stun_srv *srv)
177 {
178 pj_activesock_close(srv->asock);
179 }
180
181
182 struct stun_client
183 {
184 pj_pool_t *pool;
185 pj_stun_sock *sock;
186
187 pj_ioqueue_op_key_t send_key;
188 pj_bool_t destroy_on_err;
189
190 unsigned on_status_cnt;
191 pj_stun_sock_op last_op;
192 pj_status_t last_status;
193
194 unsigned on_rx_data_cnt;
195 };
196
197 static pj_bool_t stun_sock_on_status(pj_stun_sock *stun_sock,
198 pj_stun_sock_op op,
199 pj_status_t status)
200 {
201 struct stun_client *client;
202
203 client = (struct stun_client*) pj_stun_sock_get_user_data(stun_sock);
204 client->on_status_cnt++;
205 client->last_op = op;
206 client->last_status = status;
207
208 if (status != PJ_SUCCESS && client->destroy_on_err) {
209 pj_stun_sock_destroy(client->sock);
210 client->sock = NULL;
211 return PJ_FALSE;
212 }
213
214 return PJ_TRUE;
215 }
216
217 static pj_bool_t stun_sock_on_rx_data(pj_stun_sock *stun_sock,
218 void *pkt,
219 unsigned pkt_len,
220 const pj_sockaddr_t *src_addr,
221 unsigned addr_len)
222 {
223 struct stun_client *client;
224
225 PJ_UNUSED_ARG(pkt);
226 PJ_UNUSED_ARG(pkt_len);
227 PJ_UNUSED_ARG(src_addr);
228 PJ_UNUSED_ARG(addr_len);
229
230 client = (struct stun_client*) pj_stun_sock_get_user_data(stun_sock);
231 client->on_rx_data_cnt++;
232
233 return PJ_TRUE;
234 }
235
236 static pj_status_t create_client(pj_stun_config *cfg,
237 struct stun_client **p_client,
238 pj_bool_t destroy_on_err,
239 pj_bool_t use_ipv6)
240 {
241 pj_pool_t *pool;
242 struct stun_client *client;
243 pj_stun_sock_cfg sock_cfg;
244 pj_stun_sock_cb cb;
245 pj_status_t status;
246
247 pool = pj_pool_create(mem, "test", 512, 512, NULL);
248 client = PJ_POOL_ZALLOC_T(pool, struct stun_client);
249 client->pool = pool;
250
251 pj_stun_sock_cfg_default(&sock_cfg);
252
253 pj_bzero(&cb, sizeof(cb));
254 cb.on_status = &stun_sock_on_status;
255 cb.on_rx_data = &stun_sock_on_rx_data;
256 status = pj_stun_sock_create(cfg, NULL, GET_AF(use_ipv6), &cb, &sock_cfg,
257 client, &client->sock);
258 if (status != PJ_SUCCESS) {
259 app_perror(" pj_stun_sock_create()", status);
260 pj_pool_release(pool);
261 return status;
262 }
263
264 pj_stun_sock_set_user_data(client->sock, client);
265
266 pj_ioqueue_op_key_init(&client->send_key, sizeof(client->send_key));
267
268 client->destroy_on_err = destroy_on_err;
269
270 *p_client = client;
271
272 return PJ_SUCCESS;
273 }
274
275
276 static void destroy_client(struct stun_client *client)
277 {
278 if (client->sock) {
279 pj_stun_sock_destroy(client->sock);
280 client->sock = NULL;
281 }
282 pj_pool_release(client->pool);
283 }
284
285 static void handle_events(pj_stun_config *cfg, unsigned msec_delay)
286 {
287 pj_time_val delay;
288
289 pj_timer_heap_poll(cfg->timer_heap, NULL);
290
291 delay.sec = 0;
292 delay.msec = msec_delay;
293 pj_time_val_normalize(&delay);
294
295 pj_ioqueue_poll(cfg->ioqueue, &delay);
296 }
297
298 /*
299 * Timeout test: scenario when no response is received from server
300 */
301 static int timeout_test(pj_stun_config *cfg, pj_bool_t destroy_on_err,
302 pj_bool_t use_ipv6)
303 {
304 struct stun_srv *srv;
305 struct stun_client *client;
306 pj_str_t srv_addr;
307 pj_time_val timeout, t;
308 int i, ret = 0;
309 pj_status_t status;
310
311 PJ_LOG(3,(THIS_FILE, " timeout test [%d] - (%s)", destroy_on_err,
312 (use_ipv6)?"IPv6":"IPv4"));
313
314 status = create_client(cfg, &client, destroy_on_err, use_ipv6);
315 if (status != PJ_SUCCESS)
316 return -10;
317
318 status = create_server(client->pool, cfg->ioqueue, 0, use_ipv6, &srv);
319 if (status != PJ_SUCCESS) {
320 destroy_client(client);
321 return -20;
322 }
323
324 srv_addr = (use_ipv6)?pj_str("::1"):pj_str("127.0.0.1");
325
326 status = pj_stun_sock_start(client->sock, &srv_addr,
327 pj_sockaddr_get_port(&srv->addr), NULL);
328 if (status != PJ_SUCCESS) {
329 destroy_server(srv);
330 destroy_client(client);
331 return -30;
332 }
333
334 /* Wait until on_status() callback is called with the failure */
335 pj_gettimeofday(&timeout);
336 timeout.sec += 60;
337 do {
338 handle_events(cfg, 100);
339 pj_gettimeofday(&t);
340 } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout));
341
342 /* Check that callback with correct operation is called */
343 if (client->last_op != PJ_STUN_SOCK_BINDING_OP) {
344 PJ_LOG(3,(THIS_FILE, " error: expecting Binding operation status"));
345 ret = -40;
346 goto on_return;
347 }
348 /* .. and with the correct status */
349 if (client->last_status != PJNATH_ESTUNTIMEDOUT) {
350 PJ_LOG(3,(THIS_FILE, " error: expecting PJNATH_ESTUNTIMEDOUT"));
351 ret = -50;
352 goto on_return;
353 }
354 /* Check that server received correct retransmissions */
355 if (srv->rx_cnt != PJ_STUN_MAX_TRANSMIT_COUNT) {
356 PJ_LOG(3,(THIS_FILE, " error: expecting %d retransmissions, got %d",
357 PJ_STUN_MAX_TRANSMIT_COUNT, srv->rx_cnt));
358 ret = -60;
359 goto on_return;
360 }
361 /* Check that client doesn't receive anything */
362 if (client->on_rx_data_cnt != 0) {
363 PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything"));
364 ret = -70;
365 goto on_return;
366 }
367
368 on_return:
369 destroy_server(srv);
370 destroy_client(client);
371 for (i=0; i<7; ++i)
372 handle_events(cfg, 100);
373
374 if ((ret == 0) && use_ipv6)
375 ret = timeout_test(cfg, destroy_on_err, 0);
376
377 return ret;
378 }
379
380
381 /*
382 * Invalid response scenario: when server returns no MAPPED-ADDRESS or
383 * XOR-MAPPED-ADDRESS attribute.
384 */
385 static int missing_attr_test(pj_stun_config *cfg, pj_bool_t destroy_on_err,
386 pj_bool_t use_ipv6)
387 {
388 struct stun_srv *srv;
389 struct stun_client *client;
390 pj_str_t srv_addr;
391 pj_time_val timeout, t;
392 int i, ret = 0;
393 pj_status_t status;
394
395 PJ_LOG(3,(THIS_FILE, " missing attribute test [%d] - (%s)",
396 destroy_on_err, (use_ipv6)?"IPv6":"IPv4"));
397
398 status = create_client(cfg, &client, destroy_on_err, use_ipv6);
399 if (status != PJ_SUCCESS)
400 return -110;
401
402 status = create_server(client->pool, cfg->ioqueue, RESPOND_STUN, use_ipv6,
403 &srv);
404 if (status != PJ_SUCCESS) {
405 destroy_client(client);
406 return -120;
407 }
408 srv_addr = (use_ipv6)?pj_str("::1"):pj_str("127.0.0.1");
409
410 status = pj_stun_sock_start(client->sock, &srv_addr,
411 pj_sockaddr_get_port(&srv->addr), NULL);
412 if (status != PJ_SUCCESS) {
413 destroy_server(srv);
414 destroy_client(client);
415 return -130;
416 }
417
418 /* Wait until on_status() callback is called with the failure */
419 pj_gettimeofday(&timeout);
420 timeout.sec += 60;
421 do {
422 handle_events(cfg, 100);
423 pj_gettimeofday(&t);
424 } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout));
425
426 /* Check that callback with correct operation is called */
427 if (client->last_op != PJ_STUN_SOCK_BINDING_OP) {
428 PJ_LOG(3,(THIS_FILE, " error: expecting Binding operation status"));
429 ret = -140;
430 goto on_return;
431 }
432 if (client->last_status != PJNATH_ESTUNNOMAPPEDADDR) {
433 PJ_LOG(3,(THIS_FILE, " error: expecting PJNATH_ESTUNNOMAPPEDADDR"));
434 ret = -150;
435 goto on_return;
436 }
437 /* Check that client doesn't receive anything */
438 if (client->on_rx_data_cnt != 0) {
439 PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything"));
440 ret = -170;
441 goto on_return;
442 }
443
444 on_return:
445 destroy_server(srv);
446 destroy_client(client);
447 for (i=0; i<7; ++i)
448 handle_events(cfg, 100);
449
450 if ((ret == 0) && use_ipv6)
451 ret = missing_attr_test(cfg, destroy_on_err, 0);
452
453 return ret;
454 }
455
456 /*
457 * Keep-alive test.
458 */
459 static int keep_alive_test(pj_stun_config *cfg, pj_bool_t use_ipv6)
460 {
461 struct stun_srv *srv;
462 struct stun_client *client;
463 pj_sockaddr mapped_addr;
464 pj_stun_sock_info info;
465 pj_str_t srv_addr;
466 pj_time_val timeout, t;
467 int i, ret = 0;
468 pj_status_t status;
469
470 PJ_LOG(3,(THIS_FILE, " normal operation - (%s)",
471 (use_ipv6)?"IPv6":"IPv4"));
472
473 status = create_client(cfg, &client, PJ_TRUE, use_ipv6);
474 if (status != PJ_SUCCESS)
475 return -310;
476
477 status = create_server(client->pool, cfg->ioqueue, RESPOND_STUN|WITH_XOR_MAPPED,
478 use_ipv6, &srv);
479 if (status != PJ_SUCCESS) {
480 destroy_client(client);
481 return -320;
482 }
483
484 /*
485 * Part 1: initial Binding resolution.
486 */
487 PJ_LOG(3,(THIS_FILE, " initial Binding request"));
488
489 srv_addr = (use_ipv6)?pj_str("::1"):pj_str("127.0.0.1");
490
491 status = pj_stun_sock_start(client->sock, &srv_addr,
492 pj_sockaddr_get_port(&srv->addr), NULL);
493 if (status != PJ_SUCCESS) {
494 destroy_server(srv);
495 destroy_client(client);
496 return -330;
497 }
498
499 /* Wait until on_status() callback is called with success status */
500 pj_gettimeofday(&timeout);
501 timeout.sec += 60;
502 do {
503 handle_events(cfg, 100);
504 pj_gettimeofday(&t);
505 } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout));
506
507 /* Check that callback with correct operation is called */
508 if (client->last_op != PJ_STUN_SOCK_BINDING_OP) {
509 PJ_LOG(3,(THIS_FILE, " error: expecting Binding operation status"));
510 ret = -340;
511 goto on_return;
512 }
513 if (client->last_status != PJ_SUCCESS) {
514 PJ_LOG(3,(THIS_FILE, " error: expecting PJ_SUCCESS status"));
515 ret = -350;
516 goto on_return;
517 }
518 /* Check that client doesn't receive anything */
519 if (client->on_rx_data_cnt != 0) {
520 PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything"));
521 ret = -370;
522 goto on_return;
523 }
524
525 /* Get info */
526 pj_bzero(&info, sizeof(info));
527 pj_stun_sock_get_info(client->sock, &info);
528
529 /* Check that we have server address */
530 if (!pj_sockaddr_has_addr(&info.srv_addr)) {
531 PJ_LOG(3,(THIS_FILE, " error: missing server address"));
532 ret = -380;
533 goto on_return;
534 }
535 /* .. and bound address port must not be zero */
536 if (pj_sockaddr_get_port(&info.bound_addr)==0) {
537 PJ_LOG(3,(THIS_FILE, " error: bound address is zero"));
538 ret = -381;
539 goto on_return;
540 }
541 /* .. and mapped address */
542 if (!pj_sockaddr_has_addr(&info.mapped_addr)) {
543 PJ_LOG(3,(THIS_FILE, " error: missing mapped address"));
544 ret = -382;
545 goto on_return;
546 }
547 /* verify the mapped address */
548 pj_sockaddr_init(GET_AF(use_ipv6), &mapped_addr,
549 &srv->ip_to_send, srv->port_to_send);
550 if (pj_sockaddr_cmp(&info.mapped_addr, &mapped_addr) != 0) {
551 PJ_LOG(3,(THIS_FILE, " error: mapped address mismatched"));
552 ret = -383;
553 goto on_return;
554 }
555
556 /* .. and at least one alias */
557 if (info.alias_cnt == 0) {
558 PJ_LOG(3,(THIS_FILE, " error: must have at least one alias"));
559 ret = -384;
560 goto on_return;
561 }
562 if (!pj_sockaddr_has_addr(&info.aliases[0])) {
563 PJ_LOG(3,(THIS_FILE, " error: missing alias"));
564 ret = -386;
565 goto on_return;
566 }
567
568
569 /*
570 * Part 2: sending and receiving data
571 */
572 PJ_LOG(3,(THIS_FILE, " sending/receiving data"));
573
574 /* Change server operation mode to echo back data */
575 srv->flag = ECHO;
576
577 /* Reset server */
578 srv->rx_cnt = 0;
579
580 /* Client sending data to echo server */
581 {
582 char txt[100];
583 PJ_LOG(3,(THIS_FILE, " sending to %s", pj_sockaddr_print(&info.srv_addr, txt, sizeof(txt), 3)));
584 }
585 status = pj_stun_sock_sendto(client->sock, NULL, &ret, sizeof(ret),
586 0, &info.srv_addr,
587 pj_sockaddr_get_len(&info.srv_addr));
588 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
589 app_perror(" error: server sending data", status);
590 ret = -390;
591 goto on_return;
592 }
593
594 /* Wait for a short period until client receives data. We can't wait for
595 * too long otherwise the keep-alive will kick in.
596 */
597 pj_gettimeofday(&timeout);
598 timeout.sec += 1;
599 do {
600 handle_events(cfg, 100);
601 pj_gettimeofday(&t);
602 } while (client->on_rx_data_cnt==0 && PJ_TIME_VAL_LT(t, timeout));
603
604 /* Check that data is received in server */
605 if (srv->rx_cnt == 0) {
606 PJ_LOG(3,(THIS_FILE, " error: server didn't receive data"));
607 ret = -395;
608 goto on_return;
609 }
610
611 /* Check that status is still OK */
612 if (client->last_status != PJ_SUCCESS) {
613 app_perror(" error: client has failed", client->last_status);
614 ret = -400;
615 goto on_return;
616 }
617 /* Check that data has been received */
618 if (client->on_rx_data_cnt == 0) {
619 PJ_LOG(3,(THIS_FILE, " error: client doesn't receive data"));
620 ret = -410;
621 goto on_return;
622 }
623
624 /*
625 * Part 3: Successful keep-alive,
626 */
627 PJ_LOG(3,(THIS_FILE, " successful keep-alive scenario"));
628
629 /* Change server operation mode to normal mode */
630 srv->flag = RESPOND_STUN | WITH_XOR_MAPPED;
631
632 /* Reset server */
633 srv->rx_cnt = 0;
634
635 /* Reset client */
636 client->on_status_cnt = 0;
637 client->last_status = PJ_SUCCESS;
638 client->on_rx_data_cnt = 0;
639
640 /* Wait for keep-alive duration to see if client actually sends the
641 * keep-alive.
642 */
643 pj_gettimeofday(&timeout);
644 timeout.sec += (PJ_STUN_KEEP_ALIVE_SEC + 1);
645 do {
646 handle_events(cfg, 100);
647 pj_gettimeofday(&t);
648 } while (PJ_TIME_VAL_LT(t, timeout));
649
650 /* Check that server receives some packets */
651 if (srv->rx_cnt == 0) {
652 PJ_LOG(3, (THIS_FILE, " error: no keep-alive was received"));
653 ret = -420;
654 goto on_return;
655 }
656 /* Check that client status is still okay and on_status() callback is NOT
657 * called
658 */
659 /* No longer valid due to this ticket:
660 * https://github.com/pjsip/pjproject/issues/742
661
662 if (client->on_status_cnt != 0) {
663 PJ_LOG(3, (THIS_FILE, " error: on_status() must not be called on successful"
664 "keep-alive when mapped-address does not change"));
665 ret = -430;
666 goto on_return;
667 }
668 */
669 /* Check that client doesn't receive anything */
670 if (client->on_rx_data_cnt != 0) {
671 PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything"));
672 ret = -440;
673 goto on_return;
674 }
675
676
677 /*
678 * Part 4: Successful keep-alive with IP address change
679 */
680 PJ_LOG(3,(THIS_FILE, " mapped IP address change"));
681
682 /* Change server operation mode to normal mode */
683 srv->flag = RESPOND_STUN | WITH_XOR_MAPPED;
684
685 /* Change mapped address in the response */
686 srv->ip_to_send = (use_ipv6)?pj_str("2002:202:202::"):pj_str("2.2.2.2");
687 srv->port_to_send++;
688
689 /* Reset server */
690 srv->rx_cnt = 0;
691
692 /* Reset client */
693 client->on_status_cnt = 0;
694 client->last_status = PJ_SUCCESS;
695 client->on_rx_data_cnt = 0;
696
697 /* Wait for keep-alive duration to see if client actually sends the
698 * keep-alive.
699 */
700 pj_gettimeofday(&timeout);
701 timeout.sec += (PJ_STUN_KEEP_ALIVE_SEC + 1);
702 do {
703 handle_events(cfg, 100);
704 pj_gettimeofday(&t);
705 } while (PJ_TIME_VAL_LT(t, timeout));
706
707 /* Check that server receives some packets */
708 if (srv->rx_cnt == 0) {
709 PJ_LOG(3, (THIS_FILE, " error: no keep-alive was received"));
710 ret = -450;
711 goto on_return;
712 }
713 /* Check that on_status() callback is called (because mapped address
714 * has changed)
715 */
716 if (client->on_status_cnt != 1) {
717 PJ_LOG(3, (THIS_FILE, " error: on_status() was not called"));
718 ret = -460;
719 goto on_return;
720 }
721 /* Check that callback was called with correct operation */
722 if (client->last_op != PJ_STUN_SOCK_MAPPED_ADDR_CHANGE) {
723 PJ_LOG(3,(THIS_FILE, " error: expecting keep-alive operation status"));
724 ret = -470;
725 goto on_return;
726 }
727 /* Check that last status is still success */
728 if (client->last_status != PJ_SUCCESS) {
729 PJ_LOG(3, (THIS_FILE, " error: expecting successful status"));
730 ret = -480;
731 goto on_return;
732 }
733 /* Check that client doesn't receive anything */
734 if (client->on_rx_data_cnt != 0) {
735 PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything"));
736 ret = -490;
737 goto on_return;
738 }
739
740 /* Get info */
741 pj_bzero(&info, sizeof(info));
742 pj_stun_sock_get_info(client->sock, &info);
743
744 /* Check that we have server address */
745 if (!pj_sockaddr_has_addr(&info.srv_addr)) {
746 PJ_LOG(3,(THIS_FILE, " error: missing server address"));
747 ret = -500;
748 goto on_return;
749 }
750 /* .. and mapped address */
751 if (!pj_sockaddr_has_addr(&info.mapped_addr)) {
752 PJ_LOG(3,(THIS_FILE, " error: missing mapped address"));
753 ret = -510;
754 goto on_return;
755 }
756 /* verify the mapped address */
757 pj_sockaddr_init(GET_AF(use_ipv6), &mapped_addr,
758 &srv->ip_to_send, srv->port_to_send);
759 if (pj_sockaddr_cmp(&info.mapped_addr, &mapped_addr) != 0) {
760 PJ_LOG(3,(THIS_FILE, " error: mapped address mismatched"));
761 ret = -520;
762 goto on_return;
763 }
764
765 /* .. and at least one alias */
766 if (info.alias_cnt == 0) {
767 PJ_LOG(3,(THIS_FILE, " error: must have at least one alias"));
768 ret = -530;
769 goto on_return;
770 }
771 if (!pj_sockaddr_has_addr(&info.aliases[0])) {
772 PJ_LOG(3,(THIS_FILE, " error: missing alias"));
773 ret = -540;
774 goto on_return;
775 }
776
777
778 /*
779 * Part 5: Failed keep-alive
780 */
781 PJ_LOG(3,(THIS_FILE, " failed keep-alive scenario"));
782
783 /* Change server operation mode to respond without attribute */
784 srv->flag = RESPOND_STUN;
785
786 /* Reset server */
787 srv->rx_cnt = 0;
788
789 /* Reset client */
790 client->on_status_cnt = 0;
791 client->last_status = PJ_SUCCESS;
792 client->on_rx_data_cnt = 0;
793
794 /* Wait until on_status() is called with failure. */
795 pj_gettimeofday(&timeout);
796 timeout.sec += (PJ_STUN_KEEP_ALIVE_SEC + PJ_STUN_TIMEOUT_VALUE + 5);
797 do {
798 handle_events(cfg, 100);
799 pj_gettimeofday(&t);
800 } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout));
801
802 /* Check that callback with correct operation is called */
803 if (client->last_op != PJ_STUN_SOCK_KEEP_ALIVE_OP) {
804 PJ_LOG(3,(THIS_FILE, " error: expecting keep-alive operation status"));
805 ret = -600;
806 goto on_return;
807 }
808 if (client->last_status == PJ_SUCCESS) {
809 PJ_LOG(3,(THIS_FILE, " error: expecting failed keep-alive"));
810 ret = -610;
811 goto on_return;
812 }
813 /* Check that client doesn't receive anything */
814 if (client->on_rx_data_cnt != 0) {
815 PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything"));
816 ret = -620;
817 goto on_return;
818 }
819
820
821 on_return:
822 destroy_server(srv);
823 destroy_client(client);
824 for (i=0; i<7; ++i)
825 handle_events(cfg, 100);
826
827 if ((ret == 0) && use_ipv6)
828 ret = keep_alive_test(cfg, 0);
829
830 return ret;
831 }
832
833
834 #define DO_TEST(expr) \
835 capture_pjlib_state(&app_sess.stun_cfg, &pjlib_state); \
836 ret = expr; \
837 if (ret != 0) goto on_return; \
838 ret = check_pjlib_state(&app_sess.stun_cfg, &pjlib_state); \
839 if (ret != 0) goto on_return;
840
841
842 int stun_sock_test(void)
843 {
844 struct pjlib_state pjlib_state;
845 app_sess_t app_sess;
846 pj_status_t status;
847 int ret = 0;
848
849 status = create_stun_config(&app_sess);
850 if (status)
851 return -11;
852
853 DO_TEST(timeout_test(&app_sess.stun_cfg, PJ_FALSE, USE_IPV6));
854 DO_TEST(timeout_test(&app_sess.stun_cfg, PJ_TRUE, USE_IPV6));
855
856 DO_TEST(missing_attr_test(&app_sess.stun_cfg, PJ_FALSE, USE_IPV6));
857 DO_TEST(missing_attr_test(&app_sess.stun_cfg, PJ_TRUE, USE_IPV6));
858
859 DO_TEST(keep_alive_test(&app_sess.stun_cfg, USE_IPV6));
860
861 on_return:
862 destroy_stun_config(&app_sess);
863 return ret;
864 }
865
866