Debian Codegraph @

Xpjproject/pjnath/src/pjnath-test/server.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 "server.h"
20 #include "test.h"
21
22 #define THIS_FILE "server.c"
23 #define MAX_STUN_PKT 1500
24 #define TURN_NONCE "thenonce"
25 #define CERT_DIR "../../pjlib/build/"
26 #define CERT_CA_FILE CERT_DIR "cacert.pem"
27 #define CERT_FILE CERT_DIR "cacert.pem"
28 #define CERT_PRIVKEY_FILE CERT_DIR "privkey.pem"
29 #define CERT_PRIVKEY_PASS "privkeypass"
30
31 #define RETURN_ERROR(rc) {app_perror("", rc);return rc;}
32
33 static pj_bool_t stun_on_data_recvfrom(pj_activesock_t *asock,
34 void *data,
35 pj_size_t size,
36 const pj_sockaddr_t *src_addr,
37 int addr_len,
38 pj_status_t status);
39 static pj_bool_t turn_tcp_on_data_read(pj_activesock_t *asock,
40 void *data,
41 pj_size_t size,
42 pj_status_t status,
43 pj_size_t *remainder);
44 #if USE_TLS
45 static pj_bool_t turn_tls_on_data_read(pj_ssl_sock_t *ssock,
46 void *data,
47 pj_size_t size,
48 pj_status_t status,
49 pj_size_t *remainder);
50 #endif
51 static pj_bool_t turn_udp_on_data_recvfrom(pj_activesock_t *asock,
52 void *data,
53 pj_size_t size,
54 const pj_sockaddr_t *src_addr,
55 int addr_len,
56 pj_status_t status);
57 static pj_bool_t turn_on_data_read(test_server *asock,
58 void *data,
59 pj_size_t size,
60 const pj_sockaddr_t *src_addr,
61 int addr_len,
62 pj_status_t status);
63 static pj_bool_t turn_tcp_on_accept_complete(pj_activesock_t *asock,
64 pj_sock_t newsock,
65 const pj_sockaddr_t *src_addr,
66 int src_addr_len,
67 pj_status_t status);
68 #if USE_TLS
69 static pj_bool_t turn_tls_on_accept_complete2(pj_ssl_sock_t *ssock,
70 pj_ssl_sock_t *newsock,
71 const pj_sockaddr_t *src_addr,
72 int src_addr_len,
73 pj_status_t status);
74 #endif
75 static pj_bool_t alloc_on_data_recvfrom(pj_activesock_t *asock,
76 void *data,
77 pj_size_t size,
78 const pj_sockaddr_t *src_addr,
79 int addr_len,
80 pj_status_t status);
81
82 pj_status_t create_test_server(pj_stun_config *stun_cfg,
83 pj_uint32_t flags,
84 const char *domain,
85 test_server **p_test_srv)
86 {
87 pj_pool_t *pool;
88 test_server *test_srv;
89 pj_sockaddr hostip, bound_addr;
90 char strbuf[100];
91 pj_status_t status = PJ_EINVAL;
92 pj_bool_t use_ipv6 = flags & SERVER_IPV6;
93
94 PJ_ASSERT_RETURN(stun_cfg && domain && p_test_srv, PJ_EINVAL);
95
96 if (use_ipv6) {
97 /* pj_gethostip() may return IPv6 link-local and will cause EINVAL
98 * error, so let's just hardcode it.
99 */
100 pj_sockaddr_init(pj_AF_INET6(), &hostip, NULL, 0);
101 hostip.ipv6.sin6_addr.s6_addr[15] = 1;
102 } else {
103 status = pj_gethostip(GET_AF(use_ipv6), &hostip);
104 if (status != PJ_SUCCESS)
105 RETURN_ERROR(status);
106 }
107
108 pool = pj_pool_create(stun_cfg->pf, THIS_FILE, 512, 512, NULL);
109 test_srv = (test_server*) PJ_POOL_ZALLOC_T(pool, test_server);
110 test_srv->pool = pool;
111 test_srv->flags = flags;
112 test_srv->stun_cfg = stun_cfg;
113
114 pj_strdup2(pool, &test_srv->domain, domain);
115 test_srv->username = pj_str(TURN_USERNAME);
116 test_srv->passwd = pj_str(TURN_PASSWD);
117
118 pj_ioqueue_op_key_init(&test_srv->send_key, sizeof(test_srv->send_key));
119
120 if (flags & CREATE_DNS_SERVER) {
121 status = pj_dns_server_create(stun_cfg->pf, test_srv->stun_cfg->ioqueue,
122 GET_AF(use_ipv6), 0,
123 0, &test_srv->dns_server);
124 if (status != PJ_SUCCESS) {
125 destroy_test_server(test_srv);
126 RETURN_ERROR(status);
127 }
128
129 pj_dns_server_get_addr(test_srv->dns_server, &bound_addr);
130 test_srv->dns_server_port = pj_sockaddr_get_port(&bound_addr);
131
132 /* Add DNS A record for the domain, for fallback */
133 if (flags & CREATE_A_RECORD_FOR_DOMAIN) {
134 pj_dns_parsed_rr rr;
135 pj_str_t res_name;
136
137 pj_strdup2(pool, &res_name, domain);
138
139 if (use_ipv6) {
140 pj_dns_init_aaaa_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60,
141 &hostip.ipv6.sin6_addr);
142 } else {
143 pj_dns_init_a_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60,
144 &hostip.ipv4.sin_addr);
145 }
146
147 pj_dns_server_add_rec(test_srv->dns_server, 1, &rr);
148 }
149
150 }
151
152 if (flags & CREATE_STUN_SERVER) {
153 pj_activesock_cb stun_sock_cb;
154 pj_sockaddr stun_bound_addr;
155
156 pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb));
157 stun_sock_cb.on_data_recvfrom = &stun_on_data_recvfrom;
158
159 pj_sockaddr_init(GET_AF(use_ipv6), &stun_bound_addr,
160 NULL, 0);
161
162 status = pj_activesock_create_udp(pool, &stun_bound_addr, NULL,
163 test_srv->stun_cfg->ioqueue,
164 &stun_sock_cb, test_srv,
165 &test_srv->stun_sock, &stun_bound_addr);
166 if (status != PJ_SUCCESS) {
167 destroy_test_server(test_srv);
168 RETURN_ERROR(status);
169 }
170
171 test_srv->stun_server_port = pj_sockaddr_get_port(&stun_bound_addr);
172 status = pj_activesock_start_recvfrom(test_srv->stun_sock, pool,
173 MAX_STUN_PKT, 0);
174 if (status != PJ_SUCCESS) {
175 destroy_test_server(test_srv);
176 RETURN_ERROR(status);
177 }
178
179 if (test_srv->dns_server && (flags & CREATE_STUN_SERVER_DNS_SRV)) {
180 pj_str_t res_name, target;
181 pj_dns_parsed_rr rr;
182
183 /* Add DNS entries:
184 * _stun._udp.domain 60 IN SRV 0 0 PORT stun.domain.
185 * stun.domain IN A 127.0.0.1
186 */
187 pj_ansi_snprintf(strbuf, sizeof(strbuf),
188 "_stun._udp.%s", domain);
189 pj_strdup2(pool, &res_name, strbuf);
190 pj_ansi_snprintf(strbuf, sizeof(strbuf),
191 "stun.%s", domain);
192 pj_strdup2(pool, &target, strbuf);
193 pj_dns_init_srv_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60, 0, 0,
194 test_srv->stun_server_port, &target);
195 pj_dns_server_add_rec(test_srv->dns_server, 1, &rr);
196
197 res_name = target;
198 if (use_ipv6) {
199 pj_dns_init_aaaa_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60,
200 &hostip.ipv6.sin6_addr);
201 } else {
202 pj_dns_init_a_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60,
203 &hostip.ipv4.sin_addr);
204 }
205 pj_dns_server_add_rec(test_srv->dns_server, 1, &rr);
206 }
207
208 }
209
210 if (flags & CREATE_TURN_SERVER) {
211
212 pj_sockaddr turn_bound_addr;
213 pj_turn_tp_type tp_type = get_turn_tp_type(flags);
214
215 pj_sockaddr_init(GET_AF(use_ipv6), &turn_bound_addr, NULL, 0);
216
217 if (tp_type == PJ_TURN_TP_UDP) {
218 pj_activesock_cb turn_sock_cb;
219
220 pj_bzero(&turn_sock_cb, sizeof(turn_sock_cb));
221 turn_sock_cb.on_data_recvfrom = &turn_udp_on_data_recvfrom;
222
223 status = pj_activesock_create_udp(pool, &turn_bound_addr, NULL,
224 test_srv->stun_cfg->ioqueue,
225 &turn_sock_cb, test_srv,
226 &test_srv->turn_sock,
227 &turn_bound_addr);
228
229 if (status != PJ_SUCCESS) {
230 destroy_test_server(test_srv);
231 RETURN_ERROR(status);
232 }
233
234 test_srv->turn_server_port = pj_sockaddr_get_port(&turn_bound_addr);
235 status = pj_activesock_start_recvfrom(test_srv->turn_sock, pool,
236 MAX_STUN_PKT, 0);
237 } else if (tp_type == PJ_TURN_TP_TCP) {
238 pj_sock_t sock_fd;
239 pj_activesock_cb turn_sock_cb;
240 int name_len = sizeof(turn_bound_addr);
241
242 pj_bzero(&turn_sock_cb, sizeof(turn_sock_cb));
243 turn_sock_cb.on_accept_complete2 = &turn_tcp_on_accept_complete;
244 status = pj_sock_socket(GET_AF(use_ipv6), pj_SOCK_STREAM(), 0,
245 &sock_fd);
246 if (status != PJ_SUCCESS) {
247 RETURN_ERROR(status);
248 }
249
250 {
251 int val = 1;
252 pj_sock_setsockopt(sock_fd, pj_SOL_SOCKET(), pj_SO_REUSEADDR(),
253 &val, sizeof(val));
254 }
255
256 status = pj_sock_bind(sock_fd, &turn_bound_addr,
257 pj_sockaddr_get_len(&turn_bound_addr));
258 if (status != PJ_SUCCESS) {
259 pj_sock_close(sock_fd);
260 RETURN_ERROR(status);
261 }
262
263 status = pj_sock_listen(sock_fd, 4);
264 if (status != PJ_SUCCESS) {
265 pj_sock_close(sock_fd);
266 RETURN_ERROR(status);
267 }
268
269 status = pj_activesock_create(pool, sock_fd, pj_SOCK_STREAM(),
270 NULL,
271 test_srv->stun_cfg->ioqueue,
272 &turn_sock_cb, test_srv,
273 &test_srv->turn_sock);
274 if (status != PJ_SUCCESS) {
275 pj_sock_close(sock_fd);
276 RETURN_ERROR(status);
277 }
278
279 status = pj_activesock_start_accept(test_srv->turn_sock,
280 pool);
281
282 if (pj_sock_getsockname(sock_fd, &turn_bound_addr, &name_len)==PJ_SUCCESS) {
283 test_srv->turn_server_port = pj_sockaddr_get_port(&turn_bound_addr);
284 }
285
286 }
287 #if USE_TLS
288 else if (tp_type == PJ_TURN_TP_TLS) {
289 pj_ssl_sock_t *ssock_serv = NULL;
290 pj_ssl_sock_param ssl_param;
291 pj_ssl_cert_t *cert = NULL;
292 pj_str_t ca_file = pj_str(CERT_CA_FILE);
293 pj_str_t cert_file = pj_str(CERT_FILE);
294 pj_str_t privkey_file = pj_str(CERT_PRIVKEY_FILE);
295 pj_str_t privkey_pass = pj_str(CERT_PRIVKEY_PASS);
296 pj_ssl_sock_info ssock_info;
297
298 pj_ssl_sock_param_default(&ssl_param);
299 ssl_param.cb.on_accept_complete2 = &turn_tls_on_accept_complete2;
300 ssl_param.cb.on_data_read = &turn_tls_on_data_read;
301 ssl_param.ioqueue = test_srv->stun_cfg->ioqueue;
302 ssl_param.timer_heap = test_srv->stun_cfg->timer_heap;
303 ssl_param.user_data = test_srv;
304 ssl_param.sock_af = GET_AF(use_ipv6);
305
306 status = pj_ssl_sock_create(pool, &ssl_param, &ssock_serv);
307 if (status != PJ_SUCCESS) {
308 if (ssock_serv)
309 pj_ssl_sock_close(ssock_serv);
310 }
311
312 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
313 status = pj_ssl_cert_load_from_files(pool, &ca_file, &cert_file,
314 &privkey_file, &privkey_pass,
315 &cert);
316 if (status != PJ_SUCCESS) {
317 if (ssock_serv)
318 pj_ssl_sock_close(ssock_serv);
319 }
320 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
321 if (status != PJ_SUCCESS) {
322 if (ssock_serv)
323 pj_ssl_sock_close(ssock_serv);
324 }
325 #else
326 /* Schannel backend currently can only load certificates from
327 * OS cert store, here we don't load any cert so the SSL socket
328 * will create & use a self-signed cert.
329 */
330 PJ_UNUSED_ARG(ca_file);
331 PJ_UNUSED_ARG(cert_file);
332 PJ_UNUSED_ARG(privkey_file);
333 PJ_UNUSED_ARG(privkey_pass);
334 PJ_UNUSED_ARG(cert);
335 #endif
336 test_srv->ssl_srv_sock = ssock_serv;
337
338 status = pj_ssl_sock_start_accept(ssock_serv, pool, &turn_bound_addr,
339 pj_sockaddr_get_len(&turn_bound_addr));
340
341 /* Can only get local address after start_accept() */
342 if (pj_ssl_sock_get_info(ssock_serv, &ssock_info)==PJ_SUCCESS) {
343 test_srv->turn_server_port =
344 pj_sockaddr_get_port(&ssock_info.local_addr);
345 }
346
347 }
348 #endif
349 if (status != PJ_SUCCESS) {
350 destroy_test_server(test_srv);
351 RETURN_ERROR(status);
352 }
353
354 if (test_srv->dns_server && (flags & CREATE_TURN_SERVER_DNS_SRV)) {
355 pj_str_t res_name, target;
356 pj_dns_parsed_rr rr;
357
358 /* Add DNS entries:
359 * _turn._udp.domain 60 IN SRV 0 0 PORT turn.domain.
360 * turn.domain IN A 127.0.0.1
361 */
362 switch (tp_type) {
363 case PJ_TURN_TP_TCP:
364 pj_ansi_snprintf(strbuf, sizeof(strbuf),
365 "_turn._tcp.%s", domain);
366 break;
367 case PJ_TURN_TP_TLS:
368 pj_ansi_snprintf(strbuf, sizeof(strbuf),
369 "_turns._tcp.%s", domain);
370 break;
371 default:
372 pj_ansi_snprintf(strbuf, sizeof(strbuf),
373 "_turn._udp.%s", domain);
374
375 }
376 pj_strdup2(pool, &res_name, strbuf);
377 pj_ansi_snprintf(strbuf, sizeof(strbuf),
378 "turn.%s", domain);
379 pj_strdup2(pool, &target, strbuf);
380 pj_dns_init_srv_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60, 0, 0,
381 test_srv->turn_server_port, &target);
382 pj_dns_server_add_rec(test_srv->dns_server, 1, &rr);
383
384 res_name = target;
385
386 if (use_ipv6) {
387 pj_dns_init_aaaa_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60,
388 &hostip.ipv6.sin6_addr);
389 } else {
390 pj_dns_init_a_rr(&rr, &res_name, PJ_DNS_CLASS_IN, 60,
391 &hostip.ipv4.sin_addr);
392 }
393 pj_dns_server_add_rec(test_srv->dns_server, 1, &rr);
394 }
395 }
396
397 *p_test_srv = test_srv;
398 return PJ_SUCCESS;
399 }
400
401 void destroy_test_server(test_server *test_srv)
402 {
403 unsigned i;
404
405 PJ_ASSERT_ON_FAIL(test_srv, return);
406
407 for (i=0; i<test_srv->turn_alloc_cnt; ++i) {
408 pj_activesock_close(test_srv->turn_alloc[i].sock);
409 pj_pool_release(test_srv->turn_alloc[i].pool);
410 }
411 test_srv->turn_alloc_cnt = 0;
412
413 if (test_srv->turn_sock) {
414 pj_activesock_close(test_srv->turn_sock);
415 test_srv->turn_sock = NULL;
416 }
417
418 if (test_srv->cl_turn_sock) {
419 pj_activesock_close(test_srv->cl_turn_sock);
420 test_srv->cl_turn_sock = NULL;
421 }
422
423 #if USE_TLS
424 if (test_srv->ssl_srv_sock) {
425 pj_ssl_sock_close(test_srv->ssl_srv_sock);
426 test_srv->ssl_srv_sock = NULL;
427 }
428 if (test_srv->ssl_cl_sock) {
429 pj_ssl_sock_close(test_srv->ssl_cl_sock);
430 test_srv->ssl_cl_sock = NULL;
431 }
432 #endif
433
434 if (test_srv->stun_sock) {
435 pj_activesock_close(test_srv->stun_sock);
436 test_srv->stun_sock = NULL;
437 }
438
439 if (test_srv->dns_server) {
440 pj_dns_server_destroy(test_srv->dns_server);
441 test_srv->dns_server = NULL;
442 }
443
444 pj_pool_safe_release(&test_srv->pool);
445 }
446
447 static pj_bool_t stun_on_data_recvfrom(pj_activesock_t *asock,
448 void *data,
449 pj_size_t size,
450 const pj_sockaddr_t *src_addr,
451 int addr_len,
452 pj_status_t status)
453 {
454 test_server *test_srv;
455 pj_stun_msg *req, *resp = NULL;
456 pj_pool_t *pool;
457 pj_ssize_t len;
458
459 if (status != PJ_SUCCESS)
460 return PJ_TRUE;
461
462 test_srv = (test_server*) pj_activesock_get_user_data(asock);
463 pool = pj_pool_create(test_srv->stun_cfg->pf, NULL, 512, 512, NULL);
464
465 status = pj_stun_msg_decode(pool, (pj_uint8_t*)data, size,
466 PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET,
467 &req, NULL, NULL);
468 if (status != PJ_SUCCESS)
469 goto on_return;
470
471 if (req->hdr.type != PJ_STUN_BINDING_REQUEST) {
472 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_BAD_REQUEST,
473 NULL, &resp);
474 goto send_pkt;
475 }
476
477 status = pj_stun_msg_create_response(pool, req, 0, NULL, &resp);
478 if (status != PJ_SUCCESS)
479 goto on_return;
480
481 pj_stun_msg_add_sockaddr_attr(pool, resp, PJ_STUN_ATTR_XOR_MAPPED_ADDR,
482 PJ_TRUE, src_addr, addr_len);
483
484 send_pkt:
485 status = pj_stun_msg_encode(resp, (pj_uint8_t*)data, MAX_STUN_PKT,
486 0, NULL, &size);
487 if (status != PJ_SUCCESS)
488 goto on_return;
489
490 len = size;
491 status = pj_activesock_sendto(asock, &test_srv->send_key, data, &len,
492 0, src_addr, addr_len);
493
494 on_return:
495 pj_pool_release(pool);
496 return PJ_TRUE;
497 }
498
499
500 static pj_stun_msg* create_success_response(test_server *test_srv,
501 turn_allocation *alloc,
502 pj_stun_msg *req,
503 pj_pool_t *pool,
504 unsigned lifetime,
505 pj_str_t *auth_key)
506 {
507 pj_stun_msg *resp;
508 pj_str_t tmp;
509 pj_status_t status;
510
511 /* Create response */
512 status = pj_stun_msg_create_response(pool, req, 0, NULL, &resp);
513 if (status != PJ_SUCCESS) {
514 return NULL;
515 }
516 /* Add TURN_NONCE */
517 pj_stun_msg_add_string_attr(pool, resp, PJ_STUN_ATTR_NONCE, pj_cstr(&tmp, TURN_NONCE));
518 /* Add LIFETIME */
519 pj_stun_msg_add_uint_attr(pool, resp, PJ_STUN_ATTR_LIFETIME, lifetime);
520 if (lifetime != 0) {
521 /* Add XOR-RELAYED-ADDRESS */
522 pj_stun_msg_add_sockaddr_attr(pool, resp, PJ_STUN_ATTR_XOR_RELAYED_ADDR, PJ_TRUE, &alloc->alloc_addr,
523 pj_sockaddr_get_len(&alloc->alloc_addr));
524 /* Add XOR-MAPPED-ADDRESS */
525 pj_stun_msg_add_sockaddr_attr(pool, resp, PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE, &alloc->client_addr,
526 pj_sockaddr_get_len(&alloc->client_addr));
527 }
528
529 /* Add blank MESSAGE-INTEGRITY */
530 pj_stun_msg_add_msgint_attr(pool, resp);
531
532 /* Set auth key */
533 pj_stun_create_key(pool, auth_key, &test_srv->domain, &test_srv->username,
534 PJ_STUN_PASSWD_PLAIN, &test_srv->passwd);
535
536 return resp;
537 }
538
539 static pj_bool_t turn_tcp_on_data_read(pj_activesock_t *asock,
540 void *data,
541 pj_size_t size,
542 pj_status_t status,
543 pj_size_t *remainder)
544 {
545 test_server *test_srv = (test_server *)pj_activesock_get_user_data(asock);
546
547 PJ_UNUSED_ARG(remainder);
548 return turn_on_data_read(test_srv, data, size, &test_srv->remote_addr,
549 sizeof(test_srv->remote_addr), status);
550 }
551
552 #if USE_TLS
553 static pj_bool_t turn_tls_on_data_read(pj_ssl_sock_t *ssl_sock,
554 void *data,
555 pj_size_t size,
556 pj_status_t status,
557 pj_size_t *remainder)
558 {
559 test_server *test_srv = (test_server *)pj_ssl_sock_get_user_data(ssl_sock);
560
561 PJ_UNUSED_ARG(remainder);
562 return turn_on_data_read(test_srv, data, size,
563 &test_srv->remote_addr,
564 sizeof(test_srv->remote_addr),
565 status);
566 }
567 #endif
568
569 static pj_bool_t turn_udp_on_data_recvfrom(pj_activesock_t *asock,
570 void *data,
571 pj_size_t size,
572 const pj_sockaddr_t *src_addr,
573 int addr_len,
574 pj_status_t status)
575 {
576 test_server *test_srv;
577 test_srv = (test_server*) pj_activesock_get_user_data(asock);
578 return turn_on_data_read(test_srv, data, size, src_addr, addr_len, status);
579 }
580
581 static pj_bool_t turn_on_data_read(test_server *test_srv,
582 void *data,
583 pj_size_t size,
584 const pj_sockaddr_t *src_addr,
585 int addr_len,
586 pj_status_t status)
587 {
588
589 pj_pool_t *pool;
590 turn_allocation *alloc;
591 pj_stun_msg *req, *resp = NULL;
592 pj_str_t auth_key = { NULL, 0 };
593 char client_info[PJ_INET6_ADDRSTRLEN+10];
594 unsigned i;
595 pj_ssize_t len;
596 pj_bool_t use_ipv6 = PJ_FALSE;
597
598 if (status != PJ_SUCCESS)
599 return PJ_TRUE;
600
601 pj_sockaddr_print(src_addr, client_info, sizeof(client_info), 3);
602
603 use_ipv6 = test_srv->flags & SERVER_IPV6;
604 pool = pj_pool_create(test_srv->stun_cfg->pf, NULL, 512, 512, NULL);
605
606 /* Find the client */
607 for (i=0; i<test_srv->turn_alloc_cnt; i++) {
608 if (pj_sockaddr_cmp(&test_srv->turn_alloc[i].client_addr, src_addr)==0)
609 break;
610 }
611
612 if (pj_stun_msg_check((pj_uint8_t*)data, size,
613 PJ_STUN_NO_FINGERPRINT_CHECK)!=PJ_SUCCESS)
614 {
615 /* Not STUN message, this probably is a ChannelData */
616 pj_turn_channel_data cd;
617 const pj_turn_channel_data *pcd = (const pj_turn_channel_data*)data;
618 char peer_info[PJ_INET6_ADDRSTRLEN];
619 pj_ssize_t sent;
620 unsigned j;
621
622 if (i==test_srv->turn_alloc_cnt) {
623 /* Invalid data */
624 PJ_LOG(1,(THIS_FILE,
625 "TURN Server received strayed data"));
626 goto on_return;
627 }
628
629 alloc = &test_srv->turn_alloc[i];
630
631 cd.ch_number = pj_ntohs(pcd->ch_number);
632 cd.length = pj_ntohs(pcd->length);
633
634 /* For UDP check the packet length */
635 if (size < cd.length+sizeof(cd)) {
636 PJ_LOG(1,(THIS_FILE,
637 "TURN Server: ChannelData discarded: UDP size error"));
638 goto on_return;
639 }
640
641 /* Lookup peer */
642 for (j=0; j<alloc->perm_cnt; ++j) {
643 if (alloc->chnum[j] == cd.ch_number)
644 break;
645 }
646
647 if (j==alloc->perm_cnt) {
648 PJ_LOG(1,(THIS_FILE,
649 "TURN Server: ChannelData discarded: invalid channel number"));
650 goto on_return;
651 }
652
653 /* Relay the data to peer */
654 pj_sockaddr_print(&alloc->perm[j], peer_info, sizeof(peer_info), 3);
655 PJ_LOG(5,(THIS_FILE, "Relaying %d bytes data from client %s to peer %s",
656 cd.length, client_info, peer_info));
657 sent = cd.length;
658 pj_activesock_sendto(alloc->sock, &alloc->send_key,
659 pcd+1, &sent, 0,
660 &alloc->perm[j],
661 pj_sockaddr_get_len(&alloc->perm[j]));
662
663 /* Done */
664 goto on_return;
665 }
666
667 status = pj_stun_msg_decode(pool, (pj_uint8_t*)data, size,
668 PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET |
669 PJ_STUN_NO_FINGERPRINT_CHECK,
670 &req, NULL, NULL);
671 if (status != PJ_SUCCESS) {
672 char errmsg[PJ_ERR_MSG_SIZE];
673 pj_strerror(status, errmsg, sizeof(errmsg));
674 PJ_LOG(1,("", "STUN message decode error from client %s: %s", client_info, errmsg));
675 goto on_return;
676 }
677
678 if (i==test_srv->turn_alloc_cnt) {
679 /* New client */
680 //pj_str_t ip_addr;
681 pj_stun_username_attr *uname;
682 pj_activesock_cb alloc_sock_cb;
683 ///turn_allocation *alloc;
684
685 /* Must be Allocate request */
686 if (req->hdr.type != PJ_STUN_ALLOCATE_REQUEST) {
687 PJ_LOG(1,(THIS_FILE, "Invalid %s %s from client %s",
688 pj_stun_get_method_name(req->hdr.type),
689 pj_stun_get_class_name(req->hdr.type),
690 client_info));
691
692 if (PJ_STUN_IS_REQUEST(req->hdr.type))
693 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_BAD_REQUEST, NULL, &resp);
694 goto send_pkt;
695 }
696
697 test_srv->turn_stat.rx_allocate_cnt++;
698
699 /* Skip if we're not responding to Allocate request */
700 if (!test_srv->turn_respond_allocate)
701 return PJ_TRUE;
702
703 /* Check if we have too many clients */
704 if (test_srv->turn_alloc_cnt == MAX_TURN_ALLOC) {
705 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_INSUFFICIENT_CAPACITY, NULL, &resp);
706 goto send_pkt;
707 }
708
709 /* Get USERNAME attribute */
710 uname = (pj_stun_username_attr*)
711 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_USERNAME, 0);
712
713 /* Reject if it doesn't have MESSAGE-INTEGRITY or USERNAME attributes or
714 * the user is incorrect
715 */
716 if (pj_stun_msg_find_attr(req, PJ_STUN_ATTR_MESSAGE_INTEGRITY, 0) == NULL ||
717 uname==NULL || pj_stricmp2(&uname->value, TURN_USERNAME) != 0)
718 {
719 pj_str_t tmp;
720
721 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_UNAUTHORIZED, NULL, &resp);
722 pj_stun_msg_add_string_attr(pool, resp, PJ_STUN_ATTR_REALM, &test_srv->domain);
723 pj_stun_msg_add_string_attr(pool, resp, PJ_STUN_ATTR_NONCE, pj_cstr(&tmp, TURN_NONCE));
724 goto send_pkt;
725 }
726
727 pj_bzero(&alloc_sock_cb, sizeof(alloc_sock_cb));
728 alloc_sock_cb.on_data_recvfrom = &alloc_on_data_recvfrom;
729
730 /* Create allocation */
731 alloc = &test_srv->turn_alloc[test_srv->turn_alloc_cnt];
732 alloc->perm_cnt = 0;
733 alloc->test_srv = test_srv;
734 pj_memcpy(&alloc->client_addr, src_addr, addr_len);
735 pj_ioqueue_op_key_init(&alloc->send_key, sizeof(alloc->send_key));
736
737 alloc->pool = pj_pool_create(test_srv->stun_cfg->pf, "alloc", 512, 512, NULL);
738
739 /* Create relay socket */
740 pj_sockaddr_init(GET_AF(use_ipv6), &alloc->alloc_addr, NULL, 0);
741 if (use_ipv6) {
742 /* pj_gethostip() may return IPv6 link-local and will cause EINVAL
743 * error, so let's just hardcode it.
744 */
745 pj_sockaddr_init(pj_AF_INET6(), &alloc->alloc_addr, NULL, 0);
746 alloc->alloc_addr.ipv6.sin6_addr.s6_addr[15] = 1;
747 } else {
748 status = pj_gethostip(GET_AF(use_ipv6), &alloc->alloc_addr);
749 if (status != PJ_SUCCESS) {
750 pj_pool_release(alloc->pool);
751 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_SERVER_ERROR,
752 NULL, &resp);
753 goto send_pkt;
754 }
755 }
756
757 status = pj_activesock_create_udp(alloc->pool, &alloc->alloc_addr, NULL,
758 test_srv->stun_cfg->ioqueue,
759 &alloc_sock_cb, alloc,
760 &alloc->sock, &alloc->alloc_addr);
761 if (status != PJ_SUCCESS) {
762 pj_pool_release(alloc->pool);
763 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_SERVER_ERROR, NULL, &resp);
764 goto send_pkt;
765 }
766 //pj_sockaddr_set_str_addr(pj_AF_INET(), &alloc->alloc_addr, &ip_addr);
767
768 pj_activesock_set_user_data(alloc->sock, alloc);
769
770 status = pj_activesock_start_recvfrom(alloc->sock, alloc->pool, 1500, 0);
771 if (status != PJ_SUCCESS) {
772 pj_activesock_close(alloc->sock);
773 pj_pool_release(alloc->pool);
774 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_SERVER_ERROR, NULL, &resp);
775 goto send_pkt;
776 }
777
778 /* Create Data indication */
779 status = pj_stun_msg_create(alloc->pool, PJ_STUN_DATA_INDICATION,
780 PJ_STUN_MAGIC, NULL, &alloc->data_ind);
781 if (status != PJ_SUCCESS) {
782 pj_activesock_close(alloc->sock);
783 pj_pool_release(alloc->pool);
784 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_SERVER_ERROR, NULL, &resp);
785 goto send_pkt;
786 }
787 pj_stun_msg_add_sockaddr_attr(alloc->pool, alloc->data_ind,
788 PJ_STUN_ATTR_XOR_PEER_ADDR, PJ_TRUE,
789 &alloc->alloc_addr,
790 pj_sockaddr_get_len(&alloc->alloc_addr));
791 pj_stun_msg_add_binary_attr(alloc->pool, alloc->data_ind,
792 PJ_STUN_ATTR_DATA, (pj_uint8_t*)"", 1);
793
794 /* Create response */
795 resp = create_success_response(test_srv, alloc, req, pool, 600, &auth_key);
796 if (resp == NULL) {
797 pj_activesock_close(alloc->sock);
798 pj_pool_release(alloc->pool);
799 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_SERVER_ERROR, NULL, &resp);
800 goto send_pkt;
801 }
802
803 ++test_srv->turn_alloc_cnt;
804
805 } else {
806 alloc = &test_srv->turn_alloc[i];
807
808 if (req->hdr.type == PJ_STUN_ALLOCATE_REQUEST) {
809
810 test_srv->turn_stat.rx_allocate_cnt++;
811
812 /* Skip if we're not responding to Allocate request */
813 if (!test_srv->turn_respond_allocate)
814 return PJ_TRUE;
815
816 resp = create_success_response(test_srv, alloc, req, pool, 0, &auth_key);
817
818 } else if (req->hdr.type == PJ_STUN_REFRESH_REQUEST) {
819 pj_stun_lifetime_attr *lf_attr;
820
821 test_srv->turn_stat.rx_refresh_cnt++;
822
823 /* Skip if we're not responding to Refresh request */
824 if (!test_srv->turn_respond_refresh)
825 return PJ_TRUE;
826
827 lf_attr = (pj_stun_lifetime_attr*)
828 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_LIFETIME, 0);
829 if (lf_attr && lf_attr->value != 0) {
830 resp = create_success_response(test_srv, alloc, req, pool, 600, &auth_key);
831 pj_array_erase(test_srv->turn_alloc, sizeof(test_srv->turn_alloc[0]),
832 test_srv->turn_alloc_cnt, i);
833 --test_srv->turn_alloc_cnt;
834 } else
835 resp = create_success_response(test_srv, alloc, req, pool, 0, &auth_key);
836 } else if (req->hdr.type == PJ_STUN_CREATE_PERM_REQUEST) {
837 for (i=0; i<req->attr_count; ++i) {
838 if (req->attr[i]->type == PJ_STUN_ATTR_XOR_PEER_ADDR) {
839 pj_stun_xor_peer_addr_attr *pa = (pj_stun_xor_peer_addr_attr*)req->attr[i];
840 unsigned j;
841
842 for (j=0; j<alloc->perm_cnt; ++j) {
843 if (pj_sockaddr_cmp(&alloc->perm[j], &pa->sockaddr)==0)
844 break;
845 }
846
847 if (j==alloc->perm_cnt && alloc->perm_cnt < MAX_TURN_PERM) {
848 char peer_info[PJ_INET6_ADDRSTRLEN];
849 pj_sockaddr_print(&pa->sockaddr, peer_info, sizeof(peer_info), 3);
850
851 pj_sockaddr_cp(&alloc->perm[alloc->perm_cnt], &pa->sockaddr);
852 ++alloc->perm_cnt;
853
854 PJ_LOG(5,("", "Permission %s added to client %s, perm_cnt=%d",
855 peer_info, client_info, alloc->perm_cnt));
856 }
857
858 }
859 }
860 resp = create_success_response(test_srv, alloc, req, pool, 0, &auth_key);
861 } else if (req->hdr.type == PJ_STUN_SEND_INDICATION) {
862 pj_stun_xor_peer_addr_attr *pa;
863 pj_stun_data_attr *da;
864
865 test_srv->turn_stat.rx_send_ind_cnt++;
866
867 pa = (pj_stun_xor_peer_addr_attr*)
868 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_XOR_PEER_ADDR, 0);
869 da = (pj_stun_data_attr*)
870 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_DATA, 0);
871 if (pa && da) {
872 unsigned j;
873 char peer_info[PJ_INET6_ADDRSTRLEN];
874 pj_ssize_t sent;
875
876 pj_sockaddr_print(&pa->sockaddr, peer_info, sizeof(peer_info), 3);
877
878 for (j=0; j<alloc->perm_cnt; ++j) {
879 if (pj_sockaddr_cmp(&alloc->perm[j], &pa->sockaddr)==0)
880 break;
881 }
882
883 if (j==alloc->perm_cnt) {
884 PJ_LOG(5,("", "SendIndication to %s is rejected (no permission)",
885 peer_info));
886 } else {
887 PJ_LOG(5,(THIS_FILE, "Relaying %d bytes data from client %s to peer %s, "
888 "perm_cnt=%d",
889 da->length, client_info, peer_info, alloc->perm_cnt));
890
891 sent = da->length;
892 pj_activesock_sendto(alloc->sock, &alloc->send_key,
893 da->data, &sent, 0,
894 &pa->sockaddr,
895 pj_sockaddr_get_len(&pa->sockaddr));
896 }
897 } else {
898 PJ_LOG(1,(THIS_FILE, "Invalid Send Indication from %s", client_info));
899 }
900 } else if (req->hdr.type == PJ_STUN_CHANNEL_BIND_REQUEST) {
901 pj_stun_xor_peer_addr_attr *pa;
902 pj_stun_channel_number_attr *cna;
903 unsigned j, cn;
904
905 pa = (pj_stun_xor_peer_addr_attr*)
906 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_XOR_PEER_ADDR, 0);
907 cna = (pj_stun_channel_number_attr*)
908 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_CHANNEL_NUMBER, 0);
909 cn = PJ_STUN_GET_CH_NB(cna->value);
910
911 resp = create_success_response(test_srv, alloc, req, pool, 0, &auth_key);
912
913 for (j=0; j<alloc->perm_cnt; ++j) {
914 if (pj_sockaddr_cmp(&alloc->perm[j], &pa->sockaddr)==0)
915 break;
916 }
917
918 if (j==alloc->perm_cnt) {
919 if (alloc->perm_cnt==MAX_TURN_PERM) {
920 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_INSUFFICIENT_CAPACITY, NULL, &resp);
921 goto send_pkt;
922 }
923 pj_sockaddr_cp(&alloc->perm[j], &pa->sockaddr);
924 ++alloc->perm_cnt;
925 }
926 alloc->chnum[j] = cn;
927
928 resp = create_success_response(test_srv, alloc, req, pool, 0, &auth_key);
929
930 } else if (PJ_STUN_IS_REQUEST(req->hdr.type)) {
931 pj_stun_msg_create_response(pool, req, PJ_STUN_SC_BAD_REQUEST, NULL, &resp);
932 }
933 }
934
935
936 send_pkt:
937 if (resp) {
938 pj_turn_tp_type tp_type = get_turn_tp_type(test_srv->flags);
939
940 status = pj_stun_msg_encode(resp, (pj_uint8_t*)data, MAX_STUN_PKT,
941 0, &auth_key, &size);
942 if (status != PJ_SUCCESS)
943 goto on_return;
944
945 len = size;
946 switch (tp_type) {
947 case PJ_TURN_TP_TCP:
948 status = pj_activesock_send(test_srv->cl_turn_sock,
949 &test_srv->send_key, data, &len, 0);
950 break;
951 #if USE_TLS
952 case PJ_TURN_TP_TLS:
953 status = pj_ssl_sock_send(test_srv->ssl_cl_sock,
954 &test_srv->send_key, data, &len, 0);
955 break;
956 #endif
957 default:
958 status = pj_activesock_sendto(test_srv->turn_sock,
959 &test_srv->send_key, data,
960 &len, 0, src_addr, addr_len);
961 }
962 }
963
964 on_return:
965 pj_pool_release(pool);
966 return PJ_TRUE;
967 }
968
969 static pj_bool_t turn_tcp_on_accept_complete(pj_activesock_t *asock,
970 pj_sock_t newsock,
971 const pj_sockaddr_t *src_addr,
972 int src_addr_len,
973 pj_status_t status)
974 {
975 pj_status_t sstatus;
976 pj_activesock_cb asock_cb;
977 test_server *test_srv = (test_server *) pj_activesock_get_user_data(asock);
978
979 PJ_UNUSED_ARG(src_addr_len);
980
981 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
982 return PJ_FALSE;
983 }
984
985 pj_sockaddr_cp(&test_srv->remote_addr, src_addr);
986 pj_bzero(&asock_cb, sizeof(asock_cb));
987 asock_cb.on_data_read = &turn_tcp_on_data_read;
988
989 sstatus = pj_activesock_create(test_srv->pool, newsock, pj_SOCK_STREAM(),
990 NULL, test_srv->stun_cfg->ioqueue,
991 &asock_cb, test_srv,
992 &test_srv->cl_turn_sock);
993 if (sstatus != PJ_SUCCESS) {
994 goto on_exit;
995 }
996
997 sstatus = pj_activesock_start_read(test_srv->cl_turn_sock,
998 test_srv->pool, MAX_STUN_PKT, 0);
999 if (sstatus != PJ_SUCCESS) {
1000 goto on_exit;
1001 }
1002
1003 pj_ioqueue_op_key_init(&test_srv->send_key, sizeof(test_srv->send_key));
1004
1005 return PJ_TRUE;
1006
1007 on_exit:
1008 if (test_srv->cl_turn_sock)
1009 pj_activesock_close(test_srv->turn_sock);
1010 else
1011 pj_sock_close(newsock);
1012
1013 return PJ_FALSE;
1014
1015 }
1016
1017 #if USE_TLS
1018 static pj_bool_t turn_tls_on_accept_complete2(pj_ssl_sock_t *ssock,
1019 pj_ssl_sock_t *newsock,
1020 const pj_sockaddr_t *src_addr,
1021 int src_addr_len,
1022 pj_status_t status)
1023 {
1024 pj_status_t sstatus;
1025 test_server *test_srv = (test_server *) pj_ssl_sock_get_user_data(ssock);
1026
1027 PJ_UNUSED_ARG(src_addr_len);
1028
1029 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1030 return PJ_FALSE;
1031 }
1032
1033 pj_ssl_sock_set_user_data(newsock, test_srv);
1034 pj_sockaddr_cp(&test_srv->remote_addr, src_addr);
1035 test_srv->ssl_cl_sock = newsock;
1036
1037 sstatus = pj_ssl_sock_start_read(newsock, test_srv->pool, MAX_STUN_PKT, 0);
1038 if (sstatus != PJ_SUCCESS) {
1039 pj_ssl_sock_close(newsock);
1040 test_srv->ssl_cl_sock = NULL;
1041
1042 }
1043 return PJ_TRUE;
1044 }
1045 #endif
1046
1047 /* On received data from peer */
1048 static pj_bool_t alloc_on_data_recvfrom(pj_activesock_t *asock,
1049 void *data,
1050 pj_size_t size,
1051 const pj_sockaddr_t *src_addr,
1052 int addr_len,
1053 pj_status_t status)
1054 {
1055 turn_allocation *alloc;
1056 pj_stun_xor_peer_addr_attr *pa;
1057 pj_stun_data_attr *da;
1058 char peer_info[PJ_INET6_ADDRSTRLEN+10];
1059 char client_info[PJ_INET6_ADDRSTRLEN+10];
1060 pj_uint8_t buffer[1500];
1061 pj_ssize_t sent;
1062 unsigned i;
1063
1064 PJ_UNUSED_ARG(addr_len);
1065
1066 if (status != PJ_SUCCESS)
1067 return PJ_TRUE;
1068
1069 alloc = (turn_allocation*) pj_activesock_get_user_data(asock);
1070
1071 pj_sockaddr_print(&alloc->client_addr, client_info, sizeof(client_info), 3);
1072 pj_sockaddr_print(src_addr, peer_info, sizeof(peer_info), 3);
1073
1074 /* Check that this peer has a permission */
1075 for (i=0; i<alloc->perm_cnt; ++i) {
1076 if (pj_sockaddr_cmp(&alloc->perm[i], src_addr) == 0)
1077 {
1078 break;
1079 }
1080 }
1081 if (i==alloc->perm_cnt) {
1082 PJ_LOG(5,("", "Client %s received %lu bytes unauthorized data from peer %s",
1083 client_info, (unsigned long)size, peer_info));
1084 if (alloc->perm_cnt == 0)
1085 PJ_LOG(5,("", "Client %s has no permission", client_info));
1086 return PJ_TRUE;
1087 }
1088
1089 /* Format a Data indication */
1090 pa = (pj_stun_xor_peer_addr_attr*)
1091 pj_stun_msg_find_attr(alloc->data_ind, PJ_STUN_ATTR_XOR_PEER_ADDR, 0);
1092 da = (pj_stun_data_attr*)
1093 pj_stun_msg_find_attr(alloc->data_ind, PJ_STUN_ATTR_DATA, 0);
1094 pj_assert(pa && da);
1095
1096 pj_sockaddr_cp(&pa->sockaddr, src_addr);
1097 da->data = (pj_uint8_t*)data;
1098 da->length = (unsigned)size;
1099
1100 /* Encode Data indication */
1101 status = pj_stun_msg_encode(alloc->data_ind, buffer, sizeof(buffer), 0,
1102 NULL, &size);
1103 if (status != PJ_SUCCESS)
1104 return PJ_TRUE;
1105
1106 /* Send */
1107 sent = size;
1108 PJ_LOG(5,("", "Forwarding %lu bytes data from peer %s to client %s",
1109 (unsigned long)sent, peer_info, client_info));
1110
1111 pj_activesock_sendto(alloc->test_srv->turn_sock, &alloc->send_key, buffer,
1112 &sent, 0, &alloc->client_addr,
1113 pj_sockaddr_get_len(&alloc->client_addr));
1114
1115 return PJ_TRUE;
1116 }
1117