| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "turn.h" |
| 20 |
#include "auth.h" |
| 21 |
|
| 22 |
#define MAX_CLIENTS 32 |
| 23 |
#define MAX_PEERS_PER_CLIENT 8 |
| 24 |
|
| 25 |
#define MAX_HANDLES PJ_IOQUEUE_MAX_HANDLES |
| 26 |
#define MAX_TIMER (MAX_HANDLES * 2) |
| 27 |
#define MIN_PORT 49152 |
| 28 |
#define MAX_PORT 65535 |
| 29 |
#define MAX_LISTENERS 16 |
| 30 |
#define MAX_THREADS 2 |
| 31 |
#define MAX_NET_EVENTS 1000 |
| 32 |
|
| 33 |
|
| 34 |
static int server_thread_proc(void *arg); |
| 35 |
static pj_status_t on_tx_stun_msg( pj_stun_session *sess, |
| 36 |
void *token, |
| 37 |
const void *pkt, |
| 38 |
pj_size_t pkt_size, |
| 39 |
const pj_sockaddr_t *dst_addr, |
| 40 |
unsigned addr_len); |
| 41 |
static pj_status_t on_rx_stun_request(pj_stun_session *sess, |
| 42 |
const pj_uint8_t *pkt, |
| 43 |
unsigned pkt_len, |
| 44 |
const pj_stun_rx_data *rdata, |
| 45 |
void *user_data, |
| 46 |
const pj_sockaddr_t *src_addr, |
| 47 |
unsigned src_addr_len); |
| 48 |
|
| 49 |
struct saved_cred |
| 50 |
{ |
| 51 |
pj_str_t realm; |
| 52 |
pj_str_t username; |
| 53 |
pj_str_t nonce; |
| 54 |
int data_type; |
| 55 |
pj_str_t data; |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
PJ_DEF(const char*) pj_turn_tp_type_name(int tp_type) |
| 63 |
{ |
| 64 |
|
| 65 |
if (tp_type == PJ_TURN_TP_UDP) { |
| 66 |
return "UDP"; |
| 67 |
} else if (tp_type == PJ_TURN_TP_TCP) { |
| 68 |
return "TCP"; |
| 69 |
} else { |
| 70 |
pj_assert(!"Unsupported transport"); |
| 71 |
return "???"; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
PJ_DEF(pj_status_t) pj_turn_srv_create(pj_pool_factory *pf, |
| 79 |
pj_turn_srv **p_srv) |
| 80 |
{ |
| 81 |
pj_pool_t *pool; |
| 82 |
pj_stun_session_cb sess_cb; |
| 83 |
pj_turn_srv *srv; |
| 84 |
unsigned i; |
| 85 |
pj_status_t status; |
| 86 |
|
| 87 |
PJ_ASSERT_RETURN(pf && p_srv, PJ_EINVAL); |
| 88 |
|
| 89 |
|
| 90 |
pool = pj_pool_create(pf, "srv%p", 1000, 1000, NULL); |
| 91 |
srv = PJ_POOL_ZALLOC_T(pool, pj_turn_srv); |
| 92 |
srv->obj_name = pool->obj_name; |
| 93 |
srv->core.pf = pf; |
| 94 |
srv->core.pool = pool; |
| 95 |
srv->core.tls_key = srv->core.tls_data = -1; |
| 96 |
|
| 97 |
|
| 98 |
status = pj_ioqueue_create(pool, MAX_HANDLES, &srv->core.ioqueue); |
| 99 |
if (status != PJ_SUCCESS) |
| 100 |
goto on_error; |
| 101 |
|
| 102 |
|
| 103 |
status = pj_lock_create_recursive_mutex(pool, srv->obj_name, |
| 104 |
&srv->core.lock); |
| 105 |
if (status != PJ_SUCCESS) |
| 106 |
goto on_error; |
| 107 |
|
| 108 |
|
| 109 |
status = pj_thread_local_alloc(&srv->core.tls_key); |
| 110 |
if (status != PJ_SUCCESS) |
| 111 |
goto on_error; |
| 112 |
|
| 113 |
status = pj_thread_local_alloc(&srv->core.tls_data); |
| 114 |
if (status != PJ_SUCCESS) |
| 115 |
goto on_error; |
| 116 |
|
| 117 |
|
| 118 |
status = pj_timer_heap_create(pool, MAX_TIMER, &srv->core.timer_heap); |
| 119 |
if (status != PJ_SUCCESS) |
| 120 |
goto on_error; |
| 121 |
|
| 122 |
|
| 123 |
pj_timer_heap_set_lock(srv->core.timer_heap, srv->core.lock, PJ_FALSE); |
| 124 |
|
| 125 |
|
| 126 |
srv->core.listener = (pj_turn_listener**) |
| 127 |
pj_pool_calloc(pool, MAX_LISTENERS, |
| 128 |
sizeof(srv->core.listener[0])); |
| 129 |
|
| 130 |
|
| 131 |
srv->tables.alloc = pj_hash_create(pool, MAX_CLIENTS); |
| 132 |
srv->tables.res = pj_hash_create(pool, MAX_CLIENTS); |
| 133 |
|
| 134 |
|
| 135 |
srv->ports.min_udp = srv->ports.next_udp = MIN_PORT; |
| 136 |
srv->ports.max_udp = MAX_PORT; |
| 137 |
srv->ports.min_tcp = srv->ports.next_tcp = MIN_PORT; |
| 138 |
srv->ports.max_tcp = MAX_PORT; |
| 139 |
|
| 140 |
|
| 141 |
pj_stun_config_init(&srv->core.stun_cfg, pf, 0, srv->core.ioqueue, |
| 142 |
srv->core.timer_heap); |
| 143 |
|
| 144 |
|
| 145 |
srv->core.cred.type = PJ_STUN_AUTH_CRED_DYNAMIC; |
| 146 |
srv->core.cred.data.dyn_cred.user_data = srv; |
| 147 |
srv->core.cred.data.dyn_cred.get_auth = &pj_turn_get_auth; |
| 148 |
srv->core.cred.data.dyn_cred.get_password = &pj_turn_get_password; |
| 149 |
srv->core.cred.data.dyn_cred.verify_nonce = &pj_turn_verify_nonce; |
| 150 |
|
| 151 |
|
| 152 |
pj_bzero(&sess_cb, sizeof(sess_cb)); |
| 153 |
sess_cb.on_rx_request = &on_rx_stun_request; |
| 154 |
sess_cb.on_send_msg = &on_tx_stun_msg; |
| 155 |
|
| 156 |
status = pj_stun_session_create(&srv->core.stun_cfg, srv->obj_name, |
| 157 |
&sess_cb, PJ_FALSE, NULL, |
| 158 |
&srv->core.stun_sess); |
| 159 |
if (status != PJ_SUCCESS) { |
| 160 |
goto on_error; |
| 161 |
} |
| 162 |
|
| 163 |
pj_stun_session_set_user_data(srv->core.stun_sess, srv); |
| 164 |
pj_stun_session_set_credential(srv->core.stun_sess, PJ_STUN_AUTH_LONG_TERM, |
| 165 |
&srv->core.cred); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
srv->core.thread_cnt = MAX_THREADS; |
| 170 |
srv->core.thread = (pj_thread_t**) |
| 171 |
pj_pool_calloc(pool, srv->core.thread_cnt, |
| 172 |
sizeof(pj_thread_t*)); |
| 173 |
|
| 174 |
|
| 175 |
for (i=0; i<srv->core.thread_cnt; ++i) { |
| 176 |
status = pj_thread_create(pool, srv->obj_name, &server_thread_proc, |
| 177 |
srv, 0, 0, &srv->core.thread[i]); |
| 178 |
if (status != PJ_SUCCESS) |
| 179 |
goto on_error; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
PJ_LOG(4,(srv->obj_name, "TURN server v%s is running", |
| 184 |
pj_get_version())); |
| 185 |
|
| 186 |
*p_srv = srv; |
| 187 |
return PJ_SUCCESS; |
| 188 |
|
| 189 |
on_error: |
| 190 |
pj_turn_srv_destroy(srv); |
| 191 |
return status; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static void srv_handle_events(pj_turn_srv *srv, const pj_time_val *max_timeout) |
| 199 |
{ |
| 200 |
|
| 201 |
pj_time_val timeout = { 0, 0}; |
| 202 |
unsigned net_event_count = 0; |
| 203 |
int c; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
timeout.sec = timeout.msec = 0; |
| 209 |
c = pj_timer_heap_poll( srv->core.timer_heap, &timeout ); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
pj_assert(timeout.sec >= 0 && timeout.msec >= 0); |
| 215 |
if (timeout.msec >= 1000) timeout.msec = 999; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if (max_timeout && PJ_TIME_VAL_GT(timeout, *max_timeout)) { |
| 221 |
timeout = *max_timeout; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
do { |
| 236 |
c = pj_ioqueue_poll( srv->core.ioqueue, &timeout); |
| 237 |
if (c < 0) { |
| 238 |
pj_thread_sleep(PJ_TIME_VAL_MSEC(timeout)); |
| 239 |
return; |
| 240 |
} else if (c == 0) { |
| 241 |
break; |
| 242 |
} else { |
| 243 |
net_event_count += c; |
| 244 |
timeout.sec = timeout.msec = 0; |
| 245 |
} |
| 246 |
} while (c > 0 && net_event_count < MAX_NET_EVENTS); |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static int server_thread_proc(void *arg) |
| 254 |
{ |
| 255 |
pj_turn_srv *srv = (pj_turn_srv*)arg; |
| 256 |
|
| 257 |
while (!srv->core.quit) { |
| 258 |
pj_time_val timeout_max = {0, 100}; |
| 259 |
srv_handle_events(srv, &timeout_max); |
| 260 |
} |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
PJ_DEF(pj_status_t) pj_turn_srv_destroy(pj_turn_srv *srv) |
| 269 |
{ |
| 270 |
pj_hash_iterator_t itbuf, *it; |
| 271 |
unsigned i; |
| 272 |
|
| 273 |
|
| 274 |
srv->core.quit = PJ_TRUE; |
| 275 |
for (i=0; i<srv->core.thread_cnt; ++i) { |
| 276 |
if (srv->core.thread[i]) { |
| 277 |
pj_thread_join(srv->core.thread[i]); |
| 278 |
pj_thread_destroy(srv->core.thread[i]); |
| 279 |
srv->core.thread[i] = NULL; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
if (srv->tables.alloc) { |
| 285 |
it = pj_hash_first(srv->tables.alloc, &itbuf); |
| 286 |
while (it != NULL) { |
| 287 |
pj_turn_allocation *alloc = (pj_turn_allocation*) |
| 288 |
pj_hash_this(srv->tables.alloc, it); |
| 289 |
pj_hash_iterator_t *next = pj_hash_next(srv->tables.alloc, it); |
| 290 |
pj_turn_allocation_destroy(alloc); |
| 291 |
it = next; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
for (i=0; i<srv->core.lis_cnt; ++i) { |
| 297 |
if (srv->core.listener[i]) { |
| 298 |
pj_turn_listener_destroy(srv->core.listener[i]); |
| 299 |
srv->core.listener[i] = NULL; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
if (srv->core.stun_sess) { |
| 305 |
pj_stun_session_destroy(srv->core.stun_sess); |
| 306 |
srv->core.stun_sess = NULL; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
if (srv->tables.alloc) { |
| 311 |
srv->tables.alloc = NULL; |
| 312 |
srv->tables.res = NULL; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
if (srv->core.timer_heap) { |
| 317 |
pj_timer_heap_destroy(srv->core.timer_heap); |
| 318 |
srv->core.timer_heap = NULL; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
if (srv->core.ioqueue) { |
| 323 |
pj_ioqueue_destroy(srv->core.ioqueue); |
| 324 |
srv->core.ioqueue = NULL; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if (srv->core.tls_key != -1) { |
| 329 |
pj_thread_local_free(srv->core.tls_key); |
| 330 |
srv->core.tls_key = -1; |
| 331 |
} |
| 332 |
if (srv->core.tls_data != -1) { |
| 333 |
pj_thread_local_free(srv->core.tls_data); |
| 334 |
srv->core.tls_data = -1; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
if (srv->core.lock) { |
| 339 |
pj_lock_destroy(srv->core.lock); |
| 340 |
srv->core.lock = NULL; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
if (srv->core.pool) { |
| 345 |
pj_pool_t *pool = srv->core.pool; |
| 346 |
srv->core.pool = NULL; |
| 347 |
pj_pool_release(pool); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
return PJ_SUCCESS; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
PJ_DEF(pj_status_t) pj_turn_srv_add_listener(pj_turn_srv *srv, |
| 359 |
pj_turn_listener *lis) |
| 360 |
{ |
| 361 |
unsigned index; |
| 362 |
|
| 363 |
PJ_ASSERT_RETURN(srv && lis, PJ_EINVAL); |
| 364 |
PJ_ASSERT_RETURN(srv->core.lis_cnt < MAX_LISTENERS, PJ_ETOOMANY); |
| 365 |
|
| 366 |
|
| 367 |
index = srv->core.lis_cnt; |
| 368 |
srv->core.listener[index] = lis; |
| 369 |
lis->server = srv; |
| 370 |
lis->id = index; |
| 371 |
srv->core.lis_cnt++; |
| 372 |
|
| 373 |
PJ_LOG(4,(srv->obj_name, "Listener %s/%s added at index %d", |
| 374 |
lis->obj_name, lis->info, lis->id)); |
| 375 |
|
| 376 |
return PJ_SUCCESS; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
PJ_DEF(pj_status_t) pj_turn_listener_destroy(pj_turn_listener *listener) |
| 384 |
{ |
| 385 |
pj_turn_srv *srv = listener->server; |
| 386 |
unsigned i; |
| 387 |
|
| 388 |
|
| 389 |
pj_lock_acquire(srv->core.lock); |
| 390 |
for (i=0; i<srv->core.lis_cnt; ++i) { |
| 391 |
if (srv->core.listener[i] == listener) { |
| 392 |
srv->core.listener[i] = NULL; |
| 393 |
srv->core.lis_cnt--; |
| 394 |
listener->id = PJ_TURN_INVALID_LIS_ID; |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
pj_lock_release(srv->core.lock); |
| 399 |
|
| 400 |
|
| 401 |
return listener->destroy(listener); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
PJ_DEF(void) pj_turn_transport_add_ref( pj_turn_transport *transport, |
| 409 |
pj_turn_allocation *alloc) |
| 410 |
{ |
| 411 |
transport->add_ref(transport, alloc); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
PJ_DEF(void) pj_turn_transport_dec_ref( pj_turn_transport *transport, |
| 419 |
pj_turn_allocation *alloc) |
| 420 |
{ |
| 421 |
transport->dec_ref(transport, alloc); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
PJ_DEF(pj_status_t) pj_turn_srv_register_allocation(pj_turn_srv *srv, |
| 429 |
pj_turn_allocation *alloc) |
| 430 |
{ |
| 431 |
|
| 432 |
pj_lock_acquire(srv->core.lock); |
| 433 |
pj_hash_set(alloc->pool, srv->tables.alloc, |
| 434 |
&alloc->hkey, sizeof(alloc->hkey), 0, alloc); |
| 435 |
pj_hash_set(alloc->pool, srv->tables.res, |
| 436 |
&alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, |
| 437 |
&alloc->relay); |
| 438 |
pj_lock_release(srv->core.lock); |
| 439 |
|
| 440 |
return PJ_SUCCESS; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
PJ_DEF(pj_status_t) pj_turn_srv_unregister_allocation(pj_turn_srv *srv, |
| 448 |
pj_turn_allocation *alloc) |
| 449 |
{ |
| 450 |
|
| 451 |
pj_lock_acquire(srv->core.lock); |
| 452 |
pj_hash_set(alloc->pool, srv->tables.alloc, |
| 453 |
&alloc->hkey, sizeof(alloc->hkey), 0, NULL); |
| 454 |
pj_hash_set(alloc->pool, srv->tables.res, |
| 455 |
&alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, NULL); |
| 456 |
pj_lock_release(srv->core.lock); |
| 457 |
|
| 458 |
return PJ_SUCCESS; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
static pj_status_t on_tx_stun_msg( pj_stun_session *sess, |
| 466 |
void *token, |
| 467 |
const void *pdu, |
| 468 |
pj_size_t pdu_size, |
| 469 |
const pj_sockaddr_t *dst_addr, |
| 470 |
unsigned addr_len) |
| 471 |
{ |
| 472 |
pj_turn_transport *transport = (pj_turn_transport*) token; |
| 473 |
|
| 474 |
PJ_ASSERT_RETURN(transport!=NULL, PJ_EINVALIDOP); |
| 475 |
|
| 476 |
PJ_UNUSED_ARG(sess); |
| 477 |
|
| 478 |
return transport->sendto(transport, pdu, pdu_size, 0, |
| 479 |
dst_addr, addr_len); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
static pj_status_t stun_respond(pj_stun_session *sess, |
| 485 |
pj_turn_transport *transport, |
| 486 |
const pj_stun_rx_data *rdata, |
| 487 |
unsigned code, |
| 488 |
const char *errmsg, |
| 489 |
pj_bool_t cache, |
| 490 |
const pj_sockaddr_t *dst_addr, |
| 491 |
unsigned addr_len) |
| 492 |
{ |
| 493 |
pj_status_t status; |
| 494 |
pj_str_t reason; |
| 495 |
pj_stun_tx_data *tdata; |
| 496 |
|
| 497 |
|
| 498 |
status = pj_stun_session_create_res(sess, rdata, code, |
| 499 |
(errmsg?pj_cstr(&reason,errmsg):NULL), |
| 500 |
&tdata); |
| 501 |
if (status != PJ_SUCCESS) |
| 502 |
return status; |
| 503 |
|
| 504 |
|
| 505 |
return pj_stun_session_send_msg(sess, transport, cache, PJ_FALSE, |
| 506 |
dst_addr, addr_len, tdata); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
static pj_status_t on_rx_stun_request(pj_stun_session *sess, |
| 515 |
const pj_uint8_t *pdu, |
| 516 |
unsigned pdu_len, |
| 517 |
const pj_stun_rx_data *rdata, |
| 518 |
void *token, |
| 519 |
const pj_sockaddr_t *src_addr, |
| 520 |
unsigned src_addr_len) |
| 521 |
{ |
| 522 |
pj_turn_transport *transport; |
| 523 |
const pj_stun_msg *msg = rdata->msg; |
| 524 |
pj_turn_allocation *alloc; |
| 525 |
pj_status_t status; |
| 526 |
|
| 527 |
PJ_UNUSED_ARG(pdu); |
| 528 |
PJ_UNUSED_ARG(pdu_len); |
| 529 |
|
| 530 |
transport = (pj_turn_transport*) token; |
| 531 |
|
| 532 |
|
| 533 |
if (msg->hdr.type != PJ_STUN_ALLOCATE_REQUEST) { |
| 534 |
stun_respond(sess, transport, rdata, PJ_STUN_SC_ALLOCATION_MISMATCH, |
| 535 |
NULL, PJ_FALSE, src_addr, src_addr_len); |
| 536 |
return PJ_SUCCESS; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
status = pj_turn_allocation_create(transport, src_addr, src_addr_len, |
| 543 |
rdata, sess, &alloc); |
| 544 |
if (status != PJ_SUCCESS) { |
| 545 |
|
| 546 |
return PJ_SUCCESS; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
return PJ_SUCCESS; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
static void handle_binding_request(pj_turn_pkt *pkt, |
| 555 |
unsigned options) |
| 556 |
{ |
| 557 |
pj_stun_msg *request, *response; |
| 558 |
pj_uint8_t pdu[200]; |
| 559 |
pj_size_t len; |
| 560 |
pj_status_t status; |
| 561 |
|
| 562 |
|
| 563 |
status = pj_stun_msg_decode(pkt->pool, pkt->pkt, pkt->len, options, |
| 564 |
&request, NULL, NULL); |
| 565 |
if (status != PJ_SUCCESS) |
| 566 |
return; |
| 567 |
|
| 568 |
|
| 569 |
status = pj_stun_msg_create_response(pkt->pool, request, 0, NULL, |
| 570 |
&response); |
| 571 |
if (status != PJ_SUCCESS) |
| 572 |
return; |
| 573 |
|
| 574 |
|
| 575 |
pj_stun_msg_add_sockaddr_attr(pkt->pool, response, |
| 576 |
PJ_STUN_ATTR_XOR_MAPPED_ADDR, |
| 577 |
PJ_TRUE, |
| 578 |
&pkt->src.clt_addr, |
| 579 |
pkt->src_addr_len); |
| 580 |
|
| 581 |
|
| 582 |
status = pj_stun_msg_encode(response, pdu, sizeof(pdu), 0, NULL, &len); |
| 583 |
if (status != PJ_SUCCESS) |
| 584 |
return; |
| 585 |
|
| 586 |
|
| 587 |
pkt->transport->sendto(pkt->transport, pdu, len, 0, |
| 588 |
&pkt->src.clt_addr, pkt->src_addr_len); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
PJ_DEF(void) pj_turn_srv_on_rx_pkt(pj_turn_srv *srv, |
| 599 |
pj_turn_pkt *pkt) |
| 600 |
{ |
| 601 |
pj_turn_allocation *alloc; |
| 602 |
|
| 603 |
|
| 604 |
pj_lock_acquire(srv->core.lock); |
| 605 |
alloc = (pj_turn_allocation*) |
| 606 |
pj_hash_get(srv->tables.alloc, &pkt->src, sizeof(pkt->src), NULL); |
| 607 |
pj_lock_release(srv->core.lock); |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
if (alloc) { |
| 613 |
pj_turn_allocation_on_rx_client_pkt(alloc, pkt); |
| 614 |
} else { |
| 615 |
|
| 616 |
unsigned options; |
| 617 |
pj_size_t parsed_len; |
| 618 |
pj_status_t status; |
| 619 |
|
| 620 |
|
| 621 |
options = PJ_STUN_CHECK_PACKET | PJ_STUN_NO_FINGERPRINT_CHECK; |
| 622 |
if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) |
| 623 |
options |= PJ_STUN_IS_DATAGRAM; |
| 624 |
|
| 625 |
status = pj_stun_msg_check(pkt->pkt, pkt->len, options); |
| 626 |
if (status != PJ_SUCCESS) { |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
if ((*pkt->pkt != 0x00 && *pkt->pkt != 0x01) || |
| 637 |
pkt->len > 1600 || |
| 638 |
(options & PJ_STUN_IS_DATAGRAM)) |
| 639 |
{ |
| 640 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 641 |
char ip[PJ_INET6_ADDRSTRLEN+10]; |
| 642 |
|
| 643 |
pkt->len = 0; |
| 644 |
|
| 645 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 646 |
PJ_LOG(5,(srv->obj_name, |
| 647 |
"Non-STUN packet from %s is dropped: %s", |
| 648 |
pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3), |
| 649 |
errmsg)); |
| 650 |
} |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
if (pkt->pkt[1] == 1) { |
| 658 |
handle_binding_request(pkt, options); |
| 659 |
return; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
options &= ~PJ_STUN_CHECK_PACKET; |
| 667 |
parsed_len = 0; |
| 668 |
status = pj_stun_session_on_rx_pkt(srv->core.stun_sess, pkt->pkt, |
| 669 |
pkt->len, options, pkt->transport, |
| 670 |
&parsed_len, &pkt->src.clt_addr, |
| 671 |
pkt->src_addr_len); |
| 672 |
if (status != PJ_SUCCESS) { |
| 673 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 674 |
char ip[PJ_INET6_ADDRSTRLEN+10]; |
| 675 |
|
| 676 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 677 |
PJ_LOG(5,(srv->obj_name, |
| 678 |
"Error processing STUN packet from %s: %s", |
| 679 |
pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3), |
| 680 |
errmsg)); |
| 681 |
} |
| 682 |
|
| 683 |
if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) { |
| 684 |
pkt->len = 0; |
| 685 |
} else if (parsed_len > 0) { |
| 686 |
if (parsed_len == pkt->len) { |
| 687 |
pkt->len = 0; |
| 688 |
} else { |
| 689 |
pj_memmove(pkt->pkt, pkt->pkt+parsed_len, |
| 690 |
pkt->len - parsed_len); |
| 691 |
pkt->len -= parsed_len; |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|