| 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 |
|
| 23 |
#define THIS_FILE "allocation.c" |
| 24 |
|
| 25 |
|
| 26 |
enum { |
| 27 |
TIMER_ID_NONE, |
| 28 |
TIMER_ID_TIMEOUT, |
| 29 |
TIMER_ID_DESTROY |
| 30 |
}; |
| 31 |
|
| 32 |
#define DESTROY_DELAY {0, 500} |
| 33 |
#define PEER_TABLE_SIZE 32 |
| 34 |
|
| 35 |
#define MAX_CLIENT_BANDWIDTH 128 |
| 36 |
#define DEFA_CLIENT_BANDWIDTH 64 |
| 37 |
|
| 38 |
#define MIN_LIFETIME 30 |
| 39 |
#define MAX_LIFETIME 600 |
| 40 |
#define DEF_LIFETIME 300 |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
typedef struct alloc_request |
| 45 |
{ |
| 46 |
unsigned tp_type; |
| 47 |
char addr[PJ_INET6_ADDRSTRLEN]; |
| 48 |
unsigned bandwidth; |
| 49 |
unsigned lifetime; |
| 50 |
unsigned rpp_bits; |
| 51 |
unsigned rpp_port; |
| 52 |
} alloc_request; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static void destroy_allocation(pj_turn_allocation *alloc); |
| 58 |
static pj_status_t create_relay(pj_turn_srv *srv, |
| 59 |
pj_turn_allocation *alloc, |
| 60 |
const pj_stun_msg *msg, |
| 61 |
const alloc_request *req, |
| 62 |
pj_turn_relay_res *relay); |
| 63 |
static void destroy_relay(pj_turn_relay_res *relay); |
| 64 |
static void on_rx_from_peer(pj_ioqueue_key_t *key, |
| 65 |
pj_ioqueue_op_key_t *op_key, |
| 66 |
pj_ssize_t bytes_read); |
| 67 |
static pj_status_t stun_on_send_msg(pj_stun_session *sess, |
| 68 |
void *token, |
| 69 |
const void *pkt, |
| 70 |
pj_size_t pkt_size, |
| 71 |
const pj_sockaddr_t *dst_addr, |
| 72 |
unsigned addr_len); |
| 73 |
static pj_status_t stun_on_rx_request(pj_stun_session *sess, |
| 74 |
const pj_uint8_t *pkt, |
| 75 |
unsigned pkt_len, |
| 76 |
const pj_stun_rx_data *rdata, |
| 77 |
void *token, |
| 78 |
const pj_sockaddr_t *src_addr, |
| 79 |
unsigned src_addr_len); |
| 80 |
static pj_status_t stun_on_rx_indication(pj_stun_session *sess, |
| 81 |
const pj_uint8_t *pkt, |
| 82 |
unsigned pkt_len, |
| 83 |
const pj_stun_msg *msg, |
| 84 |
void *token, |
| 85 |
const pj_sockaddr_t *src_addr, |
| 86 |
unsigned src_addr_len); |
| 87 |
|
| 88 |
|
| 89 |
static void alloc_err(pj_turn_allocation *alloc, const char *title, |
| 90 |
pj_status_t status) |
| 91 |
{ |
| 92 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 93 |
|
| 94 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 95 |
PJ_LOG(4,(alloc->obj_name, "%s for client %s: %s", |
| 96 |
title, alloc->info, errmsg)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static pj_status_t parse_allocate_req(alloc_request *cfg, |
| 102 |
pj_stun_session *sess, |
| 103 |
const pj_stun_rx_data *rdata, |
| 104 |
const pj_sockaddr_t *src_addr, |
| 105 |
unsigned src_addr_len) |
| 106 |
{ |
| 107 |
const pj_stun_msg *req = rdata->msg; |
| 108 |
pj_stun_bandwidth_attr *attr_bw; |
| 109 |
pj_stun_req_transport_attr *attr_req_tp; |
| 110 |
pj_stun_res_token_attr *attr_res_token; |
| 111 |
pj_stun_lifetime_attr *attr_lifetime; |
| 112 |
|
| 113 |
pj_bzero(cfg, sizeof(*cfg)); |
| 114 |
|
| 115 |
|
| 116 |
attr_bw = (pj_stun_uint_attr*) |
| 117 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_BANDWIDTH, 0); |
| 118 |
if (attr_bw) { |
| 119 |
cfg->bandwidth = attr_bw->value; |
| 120 |
} else { |
| 121 |
cfg->bandwidth = DEFA_CLIENT_BANDWIDTH; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
if (cfg->bandwidth > MAX_CLIENT_BANDWIDTH) { |
| 126 |
pj_stun_session_respond(sess, rdata, |
| 127 |
PJ_STUN_SC_ALLOCATION_QUOTA_REACHED, |
| 128 |
"Invalid bandwidth", NULL, PJ_TRUE, |
| 129 |
src_addr, src_addr_len); |
| 130 |
return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_ALLOCATION_QUOTA_REACHED); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
attr_req_tp = (pj_stun_uint_attr*) |
| 135 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_TRANSPORT, 0); |
| 136 |
if (attr_req_tp == NULL) { |
| 137 |
pj_stun_session_respond(sess, rdata, PJ_STUN_SC_BAD_REQUEST, |
| 138 |
"Missing REQUESTED-TRANSPORT attribute", |
| 139 |
NULL, PJ_TRUE, src_addr, src_addr_len); |
| 140 |
return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); |
| 141 |
} |
| 142 |
|
| 143 |
cfg->tp_type = PJ_STUN_GET_RT_PROTO(attr_req_tp->value); |
| 144 |
|
| 145 |
|
| 146 |
if (cfg->tp_type != PJ_TURN_TP_UDP) { |
| 147 |
pj_stun_session_respond(sess, rdata, PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO, |
| 148 |
NULL, NULL, PJ_TRUE, src_addr, src_addr_len); |
| 149 |
return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
attr_res_token = (pj_stun_res_token_attr*) |
| 154 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_RESERVATION_TOKEN, |
| 155 |
0); |
| 156 |
if (attr_res_token) { |
| 157 |
|
| 158 |
pj_stun_session_respond(sess, rdata, |
| 159 |
PJ_STUN_SC_BAD_REQUEST, |
| 160 |
"RESERVATION-TOKEN is not supported", NULL, |
| 161 |
PJ_TRUE, src_addr, src_addr_len); |
| 162 |
return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
attr_lifetime = (pj_stun_uint_attr*) |
| 167 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_LIFETIME, 0); |
| 168 |
if (attr_lifetime) { |
| 169 |
cfg->lifetime = attr_lifetime->value; |
| 170 |
if (cfg->lifetime < MIN_LIFETIME) { |
| 171 |
pj_stun_session_respond(sess, rdata, PJ_STUN_SC_BAD_REQUEST, |
| 172 |
"LIFETIME too short", NULL, |
| 173 |
PJ_TRUE, src_addr, src_addr_len); |
| 174 |
return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); |
| 175 |
} |
| 176 |
if (cfg->lifetime > MAX_LIFETIME) |
| 177 |
cfg->lifetime = MAX_LIFETIME; |
| 178 |
} else { |
| 179 |
cfg->lifetime = DEF_LIFETIME; |
| 180 |
} |
| 181 |
|
| 182 |
return PJ_SUCCESS; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static pj_status_t send_allocate_response(pj_turn_allocation *alloc, |
| 188 |
pj_stun_session *srv_sess, |
| 189 |
pj_turn_transport *transport, |
| 190 |
const pj_stun_rx_data *rdata) |
| 191 |
{ |
| 192 |
pj_stun_tx_data *tdata; |
| 193 |
pj_status_t status; |
| 194 |
|
| 195 |
|
| 196 |
status = pj_stun_session_create_res(srv_sess, rdata, 0, NULL, &tdata); |
| 197 |
if (status != PJ_SUCCESS) |
| 198 |
return status; |
| 199 |
|
| 200 |
|
| 201 |
pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, |
| 202 |
PJ_STUN_ATTR_XOR_RELAYED_ADDR, PJ_TRUE, |
| 203 |
&alloc->relay.hkey.addr, |
| 204 |
pj_sockaddr_get_len(&alloc->relay.hkey.addr)); |
| 205 |
|
| 206 |
|
| 207 |
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, |
| 208 |
PJ_STUN_ATTR_LIFETIME, |
| 209 |
(unsigned)alloc->relay.lifetime); |
| 210 |
|
| 211 |
|
| 212 |
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, |
| 213 |
PJ_STUN_ATTR_BANDWIDTH, |
| 214 |
alloc->bandwidth); |
| 215 |
|
| 216 |
|
| 217 |
PJ_TODO(ADD_RESERVATION_TOKEN); |
| 218 |
|
| 219 |
|
| 220 |
pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, |
| 221 |
PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE, |
| 222 |
&alloc->hkey.clt_addr, |
| 223 |
pj_sockaddr_get_len(&alloc->hkey.clt_addr)); |
| 224 |
|
| 225 |
|
| 226 |
return pj_stun_session_send_msg(srv_sess, transport, PJ_TRUE, |
| 227 |
PJ_FALSE, &alloc->hkey.clt_addr, |
| 228 |
pj_sockaddr_get_len(&alloc->hkey.clt_addr), |
| 229 |
tdata); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
static pj_status_t init_cred(pj_turn_allocation *alloc, const pj_stun_msg *req) |
| 238 |
{ |
| 239 |
const pj_stun_username_attr *user; |
| 240 |
const pj_stun_realm_attr *realm; |
| 241 |
const pj_stun_nonce_attr *nonce; |
| 242 |
pj_status_t status; |
| 243 |
|
| 244 |
realm = (const pj_stun_realm_attr*) |
| 245 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REALM, 0); |
| 246 |
PJ_ASSERT_RETURN(realm != NULL, PJ_EBUG); |
| 247 |
|
| 248 |
user = (const pj_stun_username_attr*) |
| 249 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_USERNAME, 0); |
| 250 |
PJ_ASSERT_RETURN(user != NULL, PJ_EBUG); |
| 251 |
|
| 252 |
nonce = (const pj_stun_nonce_attr*) |
| 253 |
pj_stun_msg_find_attr(req, PJ_STUN_ATTR_NONCE, 0); |
| 254 |
PJ_ASSERT_RETURN(nonce != NULL, PJ_EBUG); |
| 255 |
|
| 256 |
|
| 257 |
status = pj_turn_get_password(NULL, NULL, &realm->value, |
| 258 |
&user->value, alloc->pool, |
| 259 |
&alloc->cred.data.static_cred.data_type, |
| 260 |
&alloc->cred.data.static_cred.data); |
| 261 |
if (status != PJ_SUCCESS) |
| 262 |
return status; |
| 263 |
|
| 264 |
|
| 265 |
alloc->cred.type = PJ_STUN_AUTH_CRED_STATIC; |
| 266 |
pj_strdup(alloc->pool, &alloc->cred.data.static_cred.realm, &realm->value); |
| 267 |
pj_strdup(alloc->pool, &alloc->cred.data.static_cred.username, &user->value); |
| 268 |
pj_strdup(alloc->pool, &alloc->cred.data.static_cred.nonce, &nonce->value); |
| 269 |
|
| 270 |
return PJ_SUCCESS; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
PJ_DEF(pj_status_t) pj_turn_allocation_create(pj_turn_transport *transport, |
| 278 |
const pj_sockaddr_t *src_addr, |
| 279 |
unsigned src_addr_len, |
| 280 |
const pj_stun_rx_data *rdata, |
| 281 |
pj_stun_session *srv_sess, |
| 282 |
pj_turn_allocation **p_alloc) |
| 283 |
{ |
| 284 |
pj_turn_srv *srv = transport->listener->server; |
| 285 |
const pj_stun_msg *msg = rdata->msg; |
| 286 |
pj_pool_t *pool; |
| 287 |
alloc_request req; |
| 288 |
pj_turn_allocation *alloc; |
| 289 |
pj_stun_session_cb sess_cb; |
| 290 |
char str_tmp[80]; |
| 291 |
pj_status_t status; |
| 292 |
|
| 293 |
|
| 294 |
status = parse_allocate_req(&req, srv_sess, rdata, src_addr, src_addr_len); |
| 295 |
if (status != PJ_SUCCESS) |
| 296 |
return status; |
| 297 |
|
| 298 |
pool = pj_pool_create(srv->core.pf, "alloc%p", 1000, 1000, NULL); |
| 299 |
|
| 300 |
|
| 301 |
alloc = PJ_POOL_ZALLOC_T(pool, pj_turn_allocation); |
| 302 |
alloc->pool = pool; |
| 303 |
alloc->obj_name = pool->obj_name; |
| 304 |
alloc->relay.tp.sock = PJ_INVALID_SOCKET; |
| 305 |
alloc->server = transport->listener->server; |
| 306 |
|
| 307 |
alloc->bandwidth = req.bandwidth; |
| 308 |
|
| 309 |
|
| 310 |
alloc->transport = transport; |
| 311 |
pj_turn_transport_add_ref(transport, alloc); |
| 312 |
|
| 313 |
alloc->hkey.tp_type = transport->listener->tp_type; |
| 314 |
pj_memcpy(&alloc->hkey.clt_addr, src_addr, src_addr_len); |
| 315 |
|
| 316 |
status = pj_lock_create_recursive_mutex(pool, alloc->obj_name, |
| 317 |
&alloc->lock); |
| 318 |
if (status != PJ_SUCCESS) { |
| 319 |
goto on_error; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
alloc->peer_table = pj_hash_create(pool, PEER_TABLE_SIZE); |
| 324 |
|
| 325 |
|
| 326 |
alloc->ch_table = pj_hash_create(pool, PEER_TABLE_SIZE); |
| 327 |
|
| 328 |
|
| 329 |
pj_ansi_strxcpy(alloc->info, |
| 330 |
pj_turn_tp_type_name(transport->listener->tp_type), |
| 331 |
sizeof(alloc->info)); |
| 332 |
alloc->info[3] = ':'; |
| 333 |
pj_sockaddr_print(src_addr, alloc->info+4, sizeof(alloc->info)-4, 3); |
| 334 |
|
| 335 |
|
| 336 |
pj_bzero(&sess_cb, sizeof(sess_cb)); |
| 337 |
sess_cb.on_send_msg = &stun_on_send_msg; |
| 338 |
sess_cb.on_rx_request = &stun_on_rx_request; |
| 339 |
sess_cb.on_rx_indication = &stun_on_rx_indication; |
| 340 |
status = pj_stun_session_create(&srv->core.stun_cfg, alloc->obj_name, |
| 341 |
&sess_cb, PJ_FALSE, NULL, &alloc->sess); |
| 342 |
if (status != PJ_SUCCESS) { |
| 343 |
goto on_error; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
pj_stun_session_set_user_data(alloc->sess, alloc); |
| 348 |
|
| 349 |
|
| 350 |
status = init_cred(alloc, msg); |
| 351 |
if (status != PJ_SUCCESS) { |
| 352 |
goto on_error; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
pj_stun_session_set_credential(alloc->sess, PJ_STUN_AUTH_LONG_TERM, |
| 357 |
&alloc->cred); |
| 358 |
|
| 359 |
|
| 360 |
status = create_relay(srv, alloc, msg, &req, &alloc->relay); |
| 361 |
if (status != PJ_SUCCESS) { |
| 362 |
goto on_error; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
pj_turn_srv_register_allocation(srv, alloc); |
| 367 |
|
| 368 |
|
| 369 |
status = send_allocate_response(alloc, srv_sess, transport, rdata); |
| 370 |
if (status != PJ_SUCCESS) |
| 371 |
goto on_error; |
| 372 |
|
| 373 |
|
| 374 |
pj_sockaddr_print(&alloc->relay.hkey.addr, str_tmp, |
| 375 |
sizeof(str_tmp), 3); |
| 376 |
PJ_LOG(4,(alloc->obj_name, "Client %s created, relay addr=%s:%s", |
| 377 |
alloc->info, pj_turn_tp_type_name(req.tp_type), str_tmp)); |
| 378 |
|
| 379 |
|
| 380 |
*p_alloc = alloc; |
| 381 |
return PJ_SUCCESS; |
| 382 |
|
| 383 |
on_error: |
| 384 |
|
| 385 |
pj_strerror(status, str_tmp, sizeof(str_tmp)); |
| 386 |
pj_stun_session_respond(srv_sess, rdata, PJ_STUN_SC_BAD_REQUEST, str_tmp, |
| 387 |
transport, PJ_TRUE, src_addr, src_addr_len); |
| 388 |
|
| 389 |
|
| 390 |
destroy_allocation(alloc); |
| 391 |
return status; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
static void destroy_relay(pj_turn_relay_res *relay) |
| 397 |
{ |
| 398 |
if (relay->timer.id) { |
| 399 |
pj_timer_heap_cancel(relay->allocation->server->core.timer_heap, |
| 400 |
&relay->timer); |
| 401 |
relay->timer.id = PJ_FALSE; |
| 402 |
} |
| 403 |
|
| 404 |
if (relay->tp.key) { |
| 405 |
pj_ioqueue_unregister(relay->tp.key); |
| 406 |
relay->tp.key = NULL; |
| 407 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 408 |
} else if (relay->tp.sock != PJ_INVALID_SOCKET) { |
| 409 |
pj_sock_close(relay->tp.sock); |
| 410 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
relay->lifetime = 0; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
static void destroy_allocation(pj_turn_allocation *alloc) |
| 422 |
{ |
| 423 |
pj_pool_t *pool; |
| 424 |
|
| 425 |
|
| 426 |
pj_turn_srv_unregister_allocation(alloc->server, alloc); |
| 427 |
|
| 428 |
|
| 429 |
destroy_relay(&alloc->relay); |
| 430 |
|
| 431 |
|
| 432 |
if (alloc->lock) { |
| 433 |
pj_lock_acquire(alloc->lock); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
if (alloc->transport) { |
| 438 |
pj_turn_transport_dec_ref(alloc->transport, alloc); |
| 439 |
alloc->transport = NULL; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
if (alloc->sess) { |
| 444 |
pj_stun_session_destroy(alloc->sess); |
| 445 |
alloc->sess = NULL; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
if (alloc->lock) { |
| 450 |
pj_lock_release(alloc->lock); |
| 451 |
pj_lock_destroy(alloc->lock); |
| 452 |
alloc->lock = NULL; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
pool = alloc->pool; |
| 457 |
if (pool) { |
| 458 |
alloc->pool = NULL; |
| 459 |
pj_pool_release(pool); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
PJ_DEF(void) pj_turn_allocation_destroy(pj_turn_allocation *alloc) |
| 465 |
{ |
| 466 |
destroy_allocation(alloc); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
PJ_DEF(void) pj_turn_allocation_on_transport_closed( pj_turn_allocation *alloc, |
| 474 |
pj_turn_transport *tp) |
| 475 |
{ |
| 476 |
PJ_LOG(5,(alloc->obj_name, "Transport %s unexpectedly closed, destroying " |
| 477 |
"allocation %s", tp->info, alloc->info)); |
| 478 |
pj_turn_transport_dec_ref(tp, alloc); |
| 479 |
alloc->transport = NULL; |
| 480 |
destroy_allocation(alloc); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
static void alloc_shutdown(pj_turn_allocation *alloc) |
| 489 |
{ |
| 490 |
pj_time_val destroy_delay = DESTROY_DELAY; |
| 491 |
|
| 492 |
|
| 493 |
if (alloc->relay.timer.id == TIMER_ID_TIMEOUT) { |
| 494 |
|
| 495 |
pj_timer_heap_cancel(alloc->server->core.timer_heap, |
| 496 |
&alloc->relay.timer); |
| 497 |
alloc->relay.timer.id = TIMER_ID_NONE; |
| 498 |
|
| 499 |
} else if (alloc->relay.timer.id == TIMER_ID_DESTROY) { |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
pj_assert(alloc->relay.timer.id == TIMER_ID_NONE); |
| 507 |
|
| 508 |
|
| 509 |
destroy_relay(&alloc->relay); |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
alloc->relay.timer.id = TIMER_ID_DESTROY; |
| 517 |
pj_timer_heap_schedule(alloc->server->core.timer_heap, |
| 518 |
&alloc->relay.timer, &destroy_delay); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
static pj_status_t resched_timeout(pj_turn_allocation *alloc) |
| 524 |
{ |
| 525 |
pj_time_val delay; |
| 526 |
pj_status_t status; |
| 527 |
|
| 528 |
pj_gettimeofday(&alloc->relay.expiry); |
| 529 |
alloc->relay.expiry.sec += alloc->relay.lifetime; |
| 530 |
|
| 531 |
pj_assert(alloc->relay.timer.id != TIMER_ID_DESTROY); |
| 532 |
if (alloc->relay.timer.id != 0) { |
| 533 |
pj_timer_heap_cancel(alloc->server->core.timer_heap, |
| 534 |
&alloc->relay.timer); |
| 535 |
alloc->relay.timer.id = TIMER_ID_NONE; |
| 536 |
} |
| 537 |
|
| 538 |
delay.sec = alloc->relay.lifetime; |
| 539 |
delay.msec = 0; |
| 540 |
|
| 541 |
alloc->relay.timer.id = TIMER_ID_TIMEOUT; |
| 542 |
status = pj_timer_heap_schedule(alloc->server->core.timer_heap, |
| 543 |
&alloc->relay.timer, &delay); |
| 544 |
if (status != PJ_SUCCESS) { |
| 545 |
alloc->relay.timer.id = TIMER_ID_NONE; |
| 546 |
return status; |
| 547 |
} |
| 548 |
|
| 549 |
return PJ_SUCCESS; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static void relay_timeout_cb(pj_timer_heap_t *heap, pj_timer_entry *e) |
| 555 |
{ |
| 556 |
pj_turn_relay_res *rel; |
| 557 |
pj_turn_allocation *alloc; |
| 558 |
|
| 559 |
PJ_UNUSED_ARG(heap); |
| 560 |
|
| 561 |
rel = (pj_turn_relay_res*) e->user_data; |
| 562 |
alloc = rel->allocation; |
| 563 |
|
| 564 |
if (e->id == TIMER_ID_TIMEOUT) { |
| 565 |
|
| 566 |
e->id = TIMER_ID_NONE; |
| 567 |
|
| 568 |
PJ_LOG(4,(alloc->obj_name, |
| 569 |
"Client %s refresh timed-out, shutting down..", |
| 570 |
alloc->info)); |
| 571 |
|
| 572 |
alloc_shutdown(alloc); |
| 573 |
|
| 574 |
} else if (e->id == TIMER_ID_DESTROY) { |
| 575 |
e->id = TIMER_ID_NONE; |
| 576 |
|
| 577 |
PJ_LOG(4,(alloc->obj_name, "Client %s destroying..", |
| 578 |
alloc->info)); |
| 579 |
|
| 580 |
destroy_allocation(alloc); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
static pj_status_t create_relay(pj_turn_srv *srv, |
| 589 |
pj_turn_allocation *alloc, |
| 590 |
const pj_stun_msg *msg, |
| 591 |
const alloc_request *req, |
| 592 |
pj_turn_relay_res *relay) |
| 593 |
{ |
| 594 |
enum { RETRY = 40 }; |
| 595 |
pj_pool_t *pool = alloc->pool; |
| 596 |
int retry, retry_max, sock_type; |
| 597 |
pj_ioqueue_callback icb; |
| 598 |
int af, namelen; |
| 599 |
pj_stun_string_attr *sa; |
| 600 |
pj_status_t status; |
| 601 |
|
| 602 |
pj_bzero(relay, sizeof(*relay)); |
| 603 |
|
| 604 |
relay->allocation = alloc; |
| 605 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 606 |
|
| 607 |
|
| 608 |
af = alloc->transport->listener->addr.addr.sa_family; |
| 609 |
|
| 610 |
|
| 611 |
sa = (pj_stun_string_attr*) |
| 612 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_REALM, 0); |
| 613 |
PJ_ASSERT_RETURN(sa, PJ_EINVALIDOP); |
| 614 |
pj_strdup(pool, &relay->realm, &sa->value); |
| 615 |
|
| 616 |
|
| 617 |
sa = (pj_stun_string_attr*) |
| 618 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_USERNAME, 0); |
| 619 |
PJ_ASSERT_RETURN(sa, PJ_EINVALIDOP); |
| 620 |
pj_strdup(pool, &relay->user, &sa->value); |
| 621 |
|
| 622 |
|
| 623 |
relay->lifetime = req->lifetime; |
| 624 |
pj_timer_entry_init(&relay->timer, TIMER_ID_NONE, relay, |
| 625 |
&relay_timeout_cb); |
| 626 |
resched_timeout(alloc); |
| 627 |
|
| 628 |
|
| 629 |
relay->hkey.tp_type = req->tp_type; |
| 630 |
|
| 631 |
|
| 632 |
if (req->tp_type == PJ_TURN_TP_UDP) { |
| 633 |
sock_type = pj_SOCK_DGRAM(); |
| 634 |
} else if (req->tp_type == PJ_TURN_TP_TCP) { |
| 635 |
sock_type = pj_SOCK_STREAM(); |
| 636 |
} else { |
| 637 |
pj_assert(!"Unknown transport"); |
| 638 |
return PJ_EINVALIDOP; |
| 639 |
} |
| 640 |
|
| 641 |
status = pj_sock_socket(af, sock_type, 0, &relay->tp.sock); |
| 642 |
if (status != PJ_SUCCESS) { |
| 643 |
pj_bzero(relay, sizeof(*relay)); |
| 644 |
return status; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
if (req->rpp_port) { |
| 649 |
retry_max = 1; |
| 650 |
} else { |
| 651 |
retry_max = RETRY; |
| 652 |
} |
| 653 |
|
| 654 |
for (retry=0; retry<retry_max; ++retry) { |
| 655 |
pj_uint16_t port; |
| 656 |
pj_sockaddr bound_addr; |
| 657 |
|
| 658 |
pj_lock_acquire(srv->core.lock); |
| 659 |
|
| 660 |
if (req->rpp_port) { |
| 661 |
port = (pj_uint16_t) req->rpp_port; |
| 662 |
} else if (req->tp_type == PJ_TURN_TP_UDP) { |
| 663 |
port = (pj_uint16_t) srv->ports.next_udp++; |
| 664 |
if (srv->ports.next_udp > srv->ports.max_udp) |
| 665 |
srv->ports.next_udp = srv->ports.min_udp; |
| 666 |
} else if (req->tp_type == PJ_TURN_TP_TCP) { |
| 667 |
port = (pj_uint16_t) srv->ports.next_tcp++; |
| 668 |
if (srv->ports.next_tcp > srv->ports.max_tcp) |
| 669 |
srv->ports.next_tcp = srv->ports.min_tcp; |
| 670 |
} else { |
| 671 |
pj_assert(!"Invalid transport"); |
| 672 |
port = 0; |
| 673 |
} |
| 674 |
|
| 675 |
pj_lock_release(srv->core.lock); |
| 676 |
|
| 677 |
pj_sockaddr_init(af, &bound_addr, NULL, port); |
| 678 |
|
| 679 |
status = pj_sock_bind(relay->tp.sock, &bound_addr, |
| 680 |
pj_sockaddr_get_len(&bound_addr)); |
| 681 |
if (status == PJ_SUCCESS) |
| 682 |
break; |
| 683 |
} |
| 684 |
|
| 685 |
if (status != PJ_SUCCESS) { |
| 686 |
|
| 687 |
PJ_LOG(4,(THIS_FILE, "Unable to allocate relay, giving up: err %d", |
| 688 |
status)); |
| 689 |
pj_sock_close(relay->tp.sock); |
| 690 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 691 |
return status; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
namelen = sizeof(relay->hkey.addr); |
| 696 |
status = pj_sock_getsockname(relay->tp.sock, &relay->hkey.addr, &namelen); |
| 697 |
if (status != PJ_SUCCESS) { |
| 698 |
PJ_LOG(4,(THIS_FILE, "pj_sock_getsockname() failed: err %d", |
| 699 |
status)); |
| 700 |
pj_sock_close(relay->tp.sock); |
| 701 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 702 |
return status; |
| 703 |
} |
| 704 |
if (!pj_sockaddr_has_addr(&relay->hkey.addr)) { |
| 705 |
pj_sockaddr_copy_addr(&relay->hkey.addr, |
| 706 |
&alloc->transport->listener->addr); |
| 707 |
} |
| 708 |
if (!pj_sockaddr_has_addr(&relay->hkey.addr)) { |
| 709 |
pj_sockaddr tmp_addr; |
| 710 |
status = pj_gethostip(af, &tmp_addr); |
| 711 |
if (status != PJ_SUCCESS) { |
| 712 |
PJ_LOG(4,(THIS_FILE, "pj_gethostip() failed: err %d", |
| 713 |
status)); |
| 714 |
pj_sock_close(relay->tp.sock); |
| 715 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 716 |
return status; |
| 717 |
} |
| 718 |
pj_sockaddr_copy_addr(&relay->hkey.addr, &tmp_addr); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
pj_bzero(&icb, sizeof(icb)); |
| 723 |
icb.on_read_complete = &on_rx_from_peer; |
| 724 |
|
| 725 |
status = pj_ioqueue_register_sock(pool, srv->core.ioqueue, relay->tp.sock, |
| 726 |
relay, &icb, &relay->tp.key); |
| 727 |
if (status != PJ_SUCCESS) { |
| 728 |
PJ_LOG(4,(THIS_FILE, "pj_ioqueue_register_sock() failed: err %d", |
| 729 |
status)); |
| 730 |
pj_sock_close(relay->tp.sock); |
| 731 |
relay->tp.sock = PJ_INVALID_SOCKET; |
| 732 |
return status; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
pj_ioqueue_op_key_init(&relay->tp.read_key, sizeof(relay->tp.read_key)); |
| 737 |
on_rx_from_peer(relay->tp.key, &relay->tp.read_key, 0); |
| 738 |
|
| 739 |
|
| 740 |
return PJ_SUCCESS; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
static void send_reply_err(pj_turn_allocation *alloc, |
| 745 |
const pj_stun_rx_data *rdata, |
| 746 |
pj_bool_t cache, |
| 747 |
int code, const char *errmsg) |
| 748 |
{ |
| 749 |
pj_status_t status; |
| 750 |
|
| 751 |
status = pj_stun_session_respond(alloc->sess, rdata, code, errmsg, NULL, |
| 752 |
cache, &alloc->hkey.clt_addr, |
| 753 |
pj_sockaddr_get_len(&alloc->hkey.clt_addr.addr)); |
| 754 |
if (status != PJ_SUCCESS) { |
| 755 |
alloc_err(alloc, "Error sending STUN error response", status); |
| 756 |
return; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
static void send_reply_ok(pj_turn_allocation *alloc, |
| 762 |
const pj_stun_rx_data *rdata) |
| 763 |
{ |
| 764 |
pj_status_t status; |
| 765 |
unsigned interval; |
| 766 |
pj_stun_tx_data *tdata; |
| 767 |
|
| 768 |
status = pj_stun_session_create_res(alloc->sess, rdata, 0, NULL, &tdata); |
| 769 |
if (status != PJ_SUCCESS) { |
| 770 |
alloc_err(alloc, "Error creating STUN success response", status); |
| 771 |
return; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
if (alloc->relay.lifetime != 0) { |
| 776 |
pj_time_val now; |
| 777 |
pj_gettimeofday(&now); |
| 778 |
interval = alloc->relay.expiry.sec - now.sec; |
| 779 |
} else { |
| 780 |
interval = 0; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
if (PJ_STUN_GET_METHOD(tdata->msg->hdr.type)!=PJ_STUN_CHANNEL_BIND_METHOD){ |
| 785 |
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, |
| 786 |
PJ_STUN_ATTR_LIFETIME, interval); |
| 787 |
|
| 788 |
|
| 789 |
if (interval != 0) { |
| 790 |
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, |
| 791 |
PJ_STUN_ATTR_BANDWIDTH, |
| 792 |
alloc->bandwidth); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
status = pj_stun_session_send_msg(alloc->sess, NULL, PJ_TRUE, |
| 797 |
PJ_FALSE, &alloc->hkey.clt_addr, |
| 798 |
pj_sockaddr_get_len(&alloc->hkey.clt_addr), |
| 799 |
tdata); |
| 800 |
if (status != PJ_SUCCESS) { |
| 801 |
alloc_err(alloc, "Error sending STUN success response", status); |
| 802 |
return; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
static pj_turn_permission *create_permission(pj_turn_allocation *alloc, |
| 809 |
const pj_sockaddr_t *peer_addr, |
| 810 |
unsigned addr_len) |
| 811 |
{ |
| 812 |
pj_turn_permission *perm; |
| 813 |
|
| 814 |
perm = PJ_POOL_ZALLOC_T(alloc->pool, pj_turn_permission); |
| 815 |
pj_memcpy(&perm->hkey.peer_addr, peer_addr, addr_len); |
| 816 |
|
| 817 |
perm->allocation = alloc; |
| 818 |
perm->channel = PJ_TURN_INVALID_CHANNEL; |
| 819 |
|
| 820 |
pj_gettimeofday(&perm->expiry); |
| 821 |
perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; |
| 822 |
|
| 823 |
|
| 824 |
pj_hash_set(alloc->pool, alloc->peer_table, |
| 825 |
pj_sockaddr_get_addr(&perm->hkey.peer_addr), |
| 826 |
pj_sockaddr_get_addr_len(&perm->hkey.peer_addr), 0, perm); |
| 827 |
|
| 828 |
return perm; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
static pj_turn_permission *check_permission_expiry(pj_turn_permission *perm) |
| 833 |
{ |
| 834 |
pj_turn_allocation *alloc = perm->allocation; |
| 835 |
pj_time_val now; |
| 836 |
|
| 837 |
pj_gettimeofday(&now); |
| 838 |
if (PJ_TIME_VAL_GT(perm->expiry, now)) { |
| 839 |
|
| 840 |
return perm; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
pj_hash_set(NULL, alloc->peer_table, |
| 845 |
pj_sockaddr_get_addr(&perm->hkey.peer_addr), |
| 846 |
pj_sockaddr_get_addr_len(&perm->hkey.peer_addr), 0, NULL); |
| 847 |
|
| 848 |
|
| 849 |
if (perm->channel != PJ_TURN_INVALID_CHANNEL) { |
| 850 |
pj_hash_set(NULL, alloc->ch_table, &perm->channel, |
| 851 |
sizeof(perm->channel), 0, NULL); |
| 852 |
} |
| 853 |
|
| 854 |
return NULL; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
static pj_turn_permission* |
| 859 |
lookup_permission_by_addr(pj_turn_allocation *alloc, |
| 860 |
const pj_sockaddr_t *peer_addr, |
| 861 |
unsigned addr_len) |
| 862 |
{ |
| 863 |
pj_turn_permission *perm; |
| 864 |
|
| 865 |
PJ_UNUSED_ARG(addr_len); |
| 866 |
|
| 867 |
|
| 868 |
perm = (pj_turn_permission*) |
| 869 |
pj_hash_get(alloc->peer_table, |
| 870 |
pj_sockaddr_get_addr(peer_addr), |
| 871 |
pj_sockaddr_get_addr_len(peer_addr), |
| 872 |
NULL); |
| 873 |
return perm ? check_permission_expiry(perm) : NULL; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
static pj_turn_permission* |
| 878 |
lookup_permission_by_chnum(pj_turn_allocation *alloc, |
| 879 |
unsigned chnum) |
| 880 |
{ |
| 881 |
pj_uint16_t chnum16 = (pj_uint16_t)chnum; |
| 882 |
pj_turn_permission *perm; |
| 883 |
|
| 884 |
|
| 885 |
perm = (pj_turn_permission*) pj_hash_get(alloc->ch_table, &chnum16, |
| 886 |
sizeof(chnum16), NULL); |
| 887 |
return perm ? check_permission_expiry(perm) : NULL; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
static pj_bool_t refresh_permission(pj_turn_permission *perm) |
| 894 |
{ |
| 895 |
pj_gettimeofday(&perm->expiry); |
| 896 |
if (perm->channel == PJ_TURN_INVALID_CHANNEL) |
| 897 |
perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; |
| 898 |
else |
| 899 |
perm->expiry.sec += PJ_TURN_CHANNEL_TIMEOUT; |
| 900 |
return PJ_TRUE; |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
PJ_DEF(void) pj_turn_allocation_on_rx_client_pkt(pj_turn_allocation *alloc, |
| 908 |
pj_turn_pkt *pkt) |
| 909 |
{ |
| 910 |
pj_bool_t is_stun; |
| 911 |
pj_status_t status; |
| 912 |
|
| 913 |
|
| 914 |
pj_lock_acquire(alloc->lock); |
| 915 |
|
| 916 |
|
| 917 |
is_stun = ((*((pj_uint8_t*)pkt->pkt) & 0xC0) == 0); |
| 918 |
|
| 919 |
if (is_stun) { |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
unsigned options = PJ_STUN_CHECK_PACKET | PJ_STUN_NO_FINGERPRINT_CHECK; |
| 932 |
pj_size_t parsed_len = 0; |
| 933 |
|
| 934 |
if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) |
| 935 |
options |= PJ_STUN_IS_DATAGRAM; |
| 936 |
|
| 937 |
status = pj_stun_session_on_rx_pkt(alloc->sess, pkt->pkt, pkt->len, |
| 938 |
options, NULL, &parsed_len, |
| 939 |
&pkt->src.clt_addr, |
| 940 |
pkt->src_addr_len); |
| 941 |
|
| 942 |
if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) { |
| 943 |
pkt->len = 0; |
| 944 |
} else if (parsed_len > 0) { |
| 945 |
if (parsed_len == pkt->len) { |
| 946 |
pkt->len = 0; |
| 947 |
} else { |
| 948 |
pj_memmove(pkt->pkt, pkt->pkt+parsed_len, |
| 949 |
pkt->len - parsed_len); |
| 950 |
pkt->len -= parsed_len; |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
if (status != PJ_SUCCESS) { |
| 955 |
alloc_err(alloc, "Error handling STUN packet", status); |
| 956 |
goto on_return; |
| 957 |
} |
| 958 |
|
| 959 |
} else { |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
pj_turn_channel_data *cd = (pj_turn_channel_data*)pkt->pkt; |
| 964 |
pj_turn_permission *perm; |
| 965 |
pj_ssize_t len; |
| 966 |
|
| 967 |
pj_assert(sizeof(*cd)==4); |
| 968 |
|
| 969 |
|
| 970 |
if (alloc->transport->listener->tp_type == PJ_TURN_TP_UDP) { |
| 971 |
if (pkt->len < pj_ntohs(cd->length)+sizeof(*cd)) { |
| 972 |
PJ_LOG(4,(alloc->obj_name, |
| 973 |
"ChannelData from %s discarded: UDP size error", |
| 974 |
alloc->info)); |
| 975 |
goto on_return; |
| 976 |
} |
| 977 |
} else { |
| 978 |
pj_assert(!"Unsupported transport"); |
| 979 |
goto on_return; |
| 980 |
} |
| 981 |
|
| 982 |
perm = lookup_permission_by_chnum(alloc, pj_ntohs(cd->ch_number)); |
| 983 |
if (!perm) { |
| 984 |
|
| 985 |
PJ_LOG(4,(alloc->obj_name, |
| 986 |
"ChannelData from %s discarded: ch#0x%x not found", |
| 987 |
alloc->info, pj_ntohs(cd->ch_number))); |
| 988 |
goto on_return; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
len = pj_ntohs(cd->length); |
| 993 |
pj_sock_sendto(alloc->relay.tp.sock, cd+1, &len, 0, |
| 994 |
&perm->hkey.peer_addr, |
| 995 |
pj_sockaddr_get_len(&perm->hkey.peer_addr)); |
| 996 |
|
| 997 |
|
| 998 |
refresh_permission(perm); |
| 999 |
} |
| 1000 |
|
| 1001 |
on_return: |
| 1002 |
|
| 1003 |
pj_lock_release(alloc->lock); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
static void handle_peer_pkt(pj_turn_allocation *alloc, |
| 1012 |
pj_turn_relay_res *rel, |
| 1013 |
char *pkt, pj_size_t len, |
| 1014 |
const pj_sockaddr *src_addr) |
| 1015 |
{ |
| 1016 |
pj_turn_permission *perm; |
| 1017 |
|
| 1018 |
|
| 1019 |
perm = lookup_permission_by_addr(alloc, src_addr, |
| 1020 |
pj_sockaddr_get_len(src_addr)); |
| 1021 |
if (perm == NULL) { |
| 1022 |
|
| 1023 |
return; |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
if (perm->channel != PJ_TURN_INVALID_CHANNEL) { |
| 1030 |
|
| 1031 |
pj_turn_channel_data *cd = (pj_turn_channel_data*)rel->tp.tx_pkt; |
| 1032 |
|
| 1033 |
if (len > PJ_TURN_MAX_PKT_LEN) { |
| 1034 |
char peer_addr[80]; |
| 1035 |
pj_sockaddr_print(src_addr, peer_addr, sizeof(peer_addr), 3); |
| 1036 |
PJ_LOG(4,(alloc->obj_name, "Client %s: discarded data from %s " |
| 1037 |
"because it's too long (%lu bytes)", |
| 1038 |
alloc->info, peer_addr, (unsigned long)len)); |
| 1039 |
return; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
cd->ch_number = pj_htons(perm->channel); |
| 1044 |
cd->length = pj_htons((pj_uint16_t)len); |
| 1045 |
|
| 1046 |
|
| 1047 |
pj_memcpy(rel->tp.tx_pkt+sizeof(pj_turn_channel_data), pkt, len); |
| 1048 |
|
| 1049 |
|
| 1050 |
alloc->transport->sendto(alloc->transport, rel->tp.tx_pkt, |
| 1051 |
len+sizeof(pj_turn_channel_data), 0, |
| 1052 |
&alloc->hkey.clt_addr, |
| 1053 |
pj_sockaddr_get_len(&alloc->hkey.clt_addr)); |
| 1054 |
} else { |
| 1055 |
|
| 1056 |
pj_stun_tx_data *tdata; |
| 1057 |
pj_status_t status; |
| 1058 |
|
| 1059 |
status = pj_stun_session_create_ind(alloc->sess, |
| 1060 |
PJ_STUN_DATA_INDICATION, &tdata); |
| 1061 |
if (status != PJ_SUCCESS) { |
| 1062 |
alloc_err(alloc, "Error creating Data indication", status); |
| 1063 |
return; |
| 1064 |
} |
| 1065 |
|
| 1066 |
pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, |
| 1067 |
PJ_STUN_ATTR_XOR_PEER_ADDR, PJ_TRUE, |
| 1068 |
src_addr, pj_sockaddr_get_len(src_addr)); |
| 1069 |
pj_stun_msg_add_binary_attr(tdata->pool, tdata->msg, |
| 1070 |
PJ_STUN_ATTR_DATA, |
| 1071 |
(const pj_uint8_t*)pkt, len); |
| 1072 |
|
| 1073 |
pj_stun_session_send_msg(alloc->sess, NULL, PJ_FALSE, |
| 1074 |
PJ_FALSE, &alloc->hkey.clt_addr, |
| 1075 |
pj_sockaddr_get_len(&alloc->hkey.clt_addr), |
| 1076 |
tdata); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
static void on_rx_from_peer(pj_ioqueue_key_t *key, |
| 1084 |
pj_ioqueue_op_key_t *op_key, |
| 1085 |
pj_ssize_t bytes_read) |
| 1086 |
{ |
| 1087 |
pj_turn_relay_res *rel; |
| 1088 |
pj_status_t status; |
| 1089 |
|
| 1090 |
rel = (pj_turn_relay_res*) pj_ioqueue_get_user_data(key); |
| 1091 |
|
| 1092 |
|
| 1093 |
pj_lock_acquire(rel->allocation->lock); |
| 1094 |
|
| 1095 |
do { |
| 1096 |
if (bytes_read > 0) { |
| 1097 |
handle_peer_pkt(rel->allocation, rel, rel->tp.rx_pkt, |
| 1098 |
bytes_read, &rel->tp.src_addr); |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
bytes_read = sizeof(rel->tp.rx_pkt); |
| 1103 |
rel->tp.src_addr_len = sizeof(rel->tp.src_addr); |
| 1104 |
status = pj_ioqueue_recvfrom(key, op_key, |
| 1105 |
rel->tp.rx_pkt, &bytes_read, 0, |
| 1106 |
&rel->tp.src_addr, |
| 1107 |
&rel->tp.src_addr_len); |
| 1108 |
|
| 1109 |
if (status != PJ_EPENDING && status != PJ_SUCCESS) |
| 1110 |
bytes_read = -status; |
| 1111 |
|
| 1112 |
} while (status != PJ_EPENDING && status != PJ_ECANCELLED); |
| 1113 |
|
| 1114 |
|
| 1115 |
pj_lock_release(rel->allocation->lock); |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
static pj_status_t stun_on_send_msg(pj_stun_session *sess, |
| 1123 |
void *token, |
| 1124 |
const void *pkt, |
| 1125 |
pj_size_t pkt_size, |
| 1126 |
const pj_sockaddr_t *dst_addr, |
| 1127 |
unsigned addr_len) |
| 1128 |
{ |
| 1129 |
pj_turn_allocation *alloc; |
| 1130 |
|
| 1131 |
PJ_UNUSED_ARG(token); |
| 1132 |
|
| 1133 |
alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); |
| 1134 |
|
| 1135 |
return alloc->transport->sendto(alloc->transport, pkt, pkt_size, 0, |
| 1136 |
dst_addr, addr_len); |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
static pj_status_t stun_on_rx_request(pj_stun_session *sess, |
| 1145 |
const pj_uint8_t *pkt, |
| 1146 |
unsigned pkt_len, |
| 1147 |
const pj_stun_rx_data *rdata, |
| 1148 |
void *token, |
| 1149 |
const pj_sockaddr_t *src_addr, |
| 1150 |
unsigned src_addr_len) |
| 1151 |
{ |
| 1152 |
const pj_stun_msg *msg = rdata->msg; |
| 1153 |
pj_turn_allocation *alloc; |
| 1154 |
|
| 1155 |
PJ_UNUSED_ARG(pkt); |
| 1156 |
PJ_UNUSED_ARG(pkt_len); |
| 1157 |
PJ_UNUSED_ARG(token); |
| 1158 |
PJ_UNUSED_ARG(src_addr); |
| 1159 |
PJ_UNUSED_ARG(src_addr_len); |
| 1160 |
|
| 1161 |
alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); |
| 1162 |
|
| 1163 |
|
| 1164 |
if (alloc->relay.lifetime == 0) { |
| 1165 |
|
| 1166 |
send_reply_err(alloc, rdata, PJ_TRUE, |
| 1167 |
PJ_STUN_SC_ALLOCATION_MISMATCH, NULL); |
| 1168 |
return PJ_SUCCESS; |
| 1169 |
} |
| 1170 |
|
| 1171 |
if (msg->hdr.type == PJ_STUN_REFRESH_REQUEST) { |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
pj_stun_lifetime_attr *lifetime; |
| 1176 |
pj_stun_bandwidth_attr *bandwidth; |
| 1177 |
|
| 1178 |
|
| 1179 |
lifetime = (pj_stun_lifetime_attr*) |
| 1180 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_LIFETIME, 0); |
| 1181 |
|
| 1182 |
|
| 1183 |
bandwidth = (pj_stun_bandwidth_attr*) |
| 1184 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_BANDWIDTH, 0); |
| 1185 |
|
| 1186 |
|
| 1187 |
PJ_UNUSED_ARG(bandwidth); |
| 1188 |
|
| 1189 |
if (lifetime && lifetime->value==0) { |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
alloc->relay.lifetime = 0; |
| 1194 |
|
| 1195 |
|
| 1196 |
send_reply_ok(alloc, rdata); |
| 1197 |
|
| 1198 |
|
| 1199 |
PJ_LOG(4,(alloc->obj_name, |
| 1200 |
"Client %s request to dealloc, shutting down", |
| 1201 |
alloc->info)); |
| 1202 |
|
| 1203 |
alloc_shutdown(alloc); |
| 1204 |
|
| 1205 |
} else { |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
if (lifetime) { |
| 1212 |
alloc->relay.lifetime = lifetime->value; |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
resched_timeout(alloc); |
| 1220 |
|
| 1221 |
|
| 1222 |
send_reply_ok(alloc, rdata); |
| 1223 |
} |
| 1224 |
|
| 1225 |
} else if (msg->hdr.type == PJ_STUN_CHANNEL_BIND_REQUEST) { |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
pj_stun_channel_number_attr *ch_attr; |
| 1230 |
pj_stun_xor_peer_addr_attr *peer_attr; |
| 1231 |
pj_turn_permission *p1, *p2; |
| 1232 |
|
| 1233 |
ch_attr = (pj_stun_channel_number_attr*) |
| 1234 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_CHANNEL_NUMBER, 0); |
| 1235 |
peer_attr = (pj_stun_xor_peer_addr_attr*) |
| 1236 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_XOR_PEER_ADDR, 0); |
| 1237 |
|
| 1238 |
if (!ch_attr || !peer_attr) { |
| 1239 |
send_reply_err(alloc, rdata, PJ_TRUE, |
| 1240 |
PJ_STUN_SC_BAD_REQUEST, NULL); |
| 1241 |
return PJ_SUCCESS; |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
p1 = lookup_permission_by_chnum(alloc, PJ_STUN_GET_CH_NB(ch_attr->value)); |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
if (p1) { |
| 1251 |
if (pj_sockaddr_cmp(&p1->hkey.peer_addr, &peer_attr->sockaddr)) { |
| 1252 |
|
| 1253 |
send_reply_err(alloc, rdata, PJ_TRUE, |
| 1254 |
PJ_STUN_SC_BAD_REQUEST, |
| 1255 |
"Peer address mismatch"); |
| 1256 |
return PJ_SUCCESS; |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
refresh_permission(p1); |
| 1261 |
|
| 1262 |
|
| 1263 |
send_reply_ok(alloc, rdata); |
| 1264 |
|
| 1265 |
|
| 1266 |
return PJ_SUCCESS; |
| 1267 |
} |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
p2 = lookup_permission_by_addr(alloc, &peer_attr->sockaddr, |
| 1273 |
pj_sockaddr_get_len(&peer_attr->sockaddr)); |
| 1274 |
if (p2 && p2->channel != PJ_TURN_INVALID_CHANNEL) { |
| 1275 |
send_reply_err(alloc, rdata, PJ_TRUE, PJ_STUN_SC_BAD_REQUEST, |
| 1276 |
"Peer address already assigned a channel number"); |
| 1277 |
return PJ_SUCCESS; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
if (!p2) { |
| 1282 |
p2 = create_permission(alloc, &peer_attr->sockaddr, |
| 1283 |
pj_sockaddr_get_len(&peer_attr->sockaddr)); |
| 1284 |
if (!p2) |
| 1285 |
return PJ_SUCCESS; |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
p2->channel = PJ_STUN_GET_CH_NB(ch_attr->value); |
| 1290 |
|
| 1291 |
|
| 1292 |
pj_assert(sizeof(p2->channel)==2); |
| 1293 |
pj_hash_set(alloc->pool, alloc->ch_table, &p2->channel, |
| 1294 |
sizeof(p2->channel), 0, p2); |
| 1295 |
|
| 1296 |
|
| 1297 |
refresh_permission(p2); |
| 1298 |
|
| 1299 |
|
| 1300 |
send_reply_ok(alloc, rdata); |
| 1301 |
|
| 1302 |
return PJ_SUCCESS; |
| 1303 |
|
| 1304 |
} else if (msg->hdr.type == PJ_STUN_ALLOCATE_REQUEST) { |
| 1305 |
|
| 1306 |
|
| 1307 |
send_reply_err(alloc, rdata, PJ_TRUE, PJ_STUN_SC_ALLOCATION_MISMATCH, |
| 1308 |
NULL); |
| 1309 |
|
| 1310 |
} else { |
| 1311 |
|
| 1312 |
|
| 1313 |
send_reply_err(alloc, rdata, PJ_TRUE, PJ_STUN_SC_BAD_REQUEST, NULL); |
| 1314 |
|
| 1315 |
} |
| 1316 |
|
| 1317 |
return PJ_SUCCESS; |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
static pj_status_t stun_on_rx_indication(pj_stun_session *sess, |
| 1326 |
const pj_uint8_t *pkt, |
| 1327 |
unsigned pkt_len, |
| 1328 |
const pj_stun_msg *msg, |
| 1329 |
void *token, |
| 1330 |
const pj_sockaddr_t *src_addr, |
| 1331 |
unsigned src_addr_len) |
| 1332 |
{ |
| 1333 |
pj_stun_xor_peer_addr_attr *peer_attr; |
| 1334 |
pj_stun_data_attr *data_attr; |
| 1335 |
pj_turn_allocation *alloc; |
| 1336 |
pj_turn_permission *perm; |
| 1337 |
pj_ssize_t len; |
| 1338 |
|
| 1339 |
PJ_UNUSED_ARG(pkt); |
| 1340 |
PJ_UNUSED_ARG(pkt_len); |
| 1341 |
PJ_UNUSED_ARG(token); |
| 1342 |
PJ_UNUSED_ARG(src_addr); |
| 1343 |
PJ_UNUSED_ARG(src_addr_len); |
| 1344 |
|
| 1345 |
alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); |
| 1346 |
|
| 1347 |
|
| 1348 |
if (msg->hdr.type != PJ_STUN_SEND_INDICATION) { |
| 1349 |
|
| 1350 |
return PJ_SUCCESS; |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
peer_attr = (pj_stun_xor_peer_addr_attr*) |
| 1355 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_XOR_PEER_ADDR, 0); |
| 1356 |
|
| 1357 |
|
| 1358 |
if (!peer_attr) |
| 1359 |
return PJ_SUCCESS; |
| 1360 |
|
| 1361 |
|
| 1362 |
data_attr = (pj_stun_data_attr*) |
| 1363 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_DATA, 0); |
| 1364 |
|
| 1365 |
|
| 1366 |
perm = lookup_permission_by_addr(alloc, &peer_attr->sockaddr, |
| 1367 |
pj_sockaddr_get_len(&peer_attr->sockaddr)); |
| 1368 |
if (perm == NULL) { |
| 1369 |
perm = create_permission(alloc, &peer_attr->sockaddr, |
| 1370 |
pj_sockaddr_get_len(&peer_attr->sockaddr)); |
| 1371 |
} |
| 1372 |
refresh_permission(perm); |
| 1373 |
|
| 1374 |
|
| 1375 |
if (data_attr == NULL) |
| 1376 |
return PJ_SUCCESS; |
| 1377 |
|
| 1378 |
|
| 1379 |
len = data_attr->length; |
| 1380 |
pj_sock_sendto(alloc->relay.tp.sock, data_attr->data, |
| 1381 |
&len, 0, &peer_attr->sockaddr, |
| 1382 |
pj_sockaddr_get_len(&peer_attr->sockaddr)); |
| 1383 |
|
| 1384 |
return PJ_SUCCESS; |
| 1385 |
} |
| 1386 |
|
| 1387 |
|