Changeset 1852 for pjproject/trunk/pjnath/src/pjturn-srv/allocation.c
- Timestamp:
- Mar 9, 2008 12:55:00 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjturn-srv/allocation.c
r1850 r1852 18 18 */ 19 19 #include "turn.h" 20 #include "auth.h" 21 20 22 21 23 #define THIS_FILE "allocation.c" … … 31 33 #define PEER_TABLE_SIZE 32 32 34 35 #define MAX_CLIENT_BANDWIDTH 128 /* In Kbps */ 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 33 44 /* ChannelData header */ 34 45 typedef struct channel_data_hdr … … 39 50 40 51 52 /* Parsed Allocation request. */ 53 typedef struct alloc_request 54 { 55 unsigned tp_type; /* Requested transport */ 56 char addr[PJ_INET6_ADDRSTRLEN]; /* Requested IP */ 57 unsigned bandwidth; /* Requested bandwidth */ 58 unsigned lifetime; /* Lifetime. */ 59 unsigned rpp_bits; /* A bits */ 60 unsigned rpp_port; /* Requested port */ 61 } alloc_request; 62 63 64 41 65 /* Prototypes */ 42 static pj_status_t create_relay(pjturn_allocation *alloc, 43 const pjturn_allocation_req *req); 66 static void destroy_allocation(pj_turn_allocation *alloc); 67 static pj_status_t create_relay(pj_turn_srv *srv, 68 pj_turn_allocation *alloc, 69 const pj_stun_msg *msg, 70 const alloc_request *req, 71 pj_turn_relay_res *relay); 72 static void destroy_relay(pj_turn_relay_res *relay); 44 73 static void on_rx_from_peer(pj_ioqueue_key_t *key, 45 74 pj_ioqueue_op_key_t *op_key, 46 75 pj_ssize_t bytes_read); 47 static void destroy_relay(pjturn_relay_res *relay);48 76 static pj_status_t stun_on_send_msg(pj_stun_session *sess, 49 77 const void *pkt, … … 65 93 66 94 /* Log allocation error */ 67 static void alloc_err(pj turn_allocation *alloc, const char *title,95 static void alloc_err(pj_turn_allocation *alloc, const char *title, 68 96 pj_status_t status) 69 97 { … … 75 103 } 76 104 105 106 /* Parse ALLOCATE request */ 107 static pj_status_t parse_allocate_req(alloc_request *cfg, 108 pj_stun_session *sess, 109 const pj_stun_msg *req, 110 const pj_sockaddr_t *src_addr, 111 unsigned src_addr_len) 112 { 113 pj_stun_bandwidth_attr *attr_bw; 114 pj_stun_req_transport_attr *attr_req_tp; 115 pj_stun_res_token_attr *attr_res_token; 116 pj_stun_req_props_attr *attr_rpp; 117 pj_stun_lifetime_attr *attr_lifetime; 118 119 pj_bzero(cfg, sizeof(*cfg)); 120 121 /* Get BANDWIDTH attribute, if any. */ 122 attr_bw = (pj_stun_uint_attr*) 123 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_BANDWIDTH, 0); 124 if (attr_bw) { 125 cfg->bandwidth = attr_bw->value; 126 } else { 127 cfg->bandwidth = DEFA_CLIENT_BANDWIDTH; 128 } 129 130 /* Check if we can satisfy the bandwidth */ 131 if (cfg->bandwidth > MAX_CLIENT_BANDWIDTH) { 132 pj_stun_session_respond(sess, req, PJ_STUN_SC_ALLOCATION_QUOTA_REACHED, 133 "Invalid bandwidth", PJ_TRUE, 134 src_addr, src_addr_len); 135 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_ALLOCATION_QUOTA_REACHED); 136 } 137 138 /* MUST have REQUESTED-TRANSPORT attribute */ 139 attr_req_tp = (pj_stun_uint_attr*) 140 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_TRANSPORT, 0); 141 if (attr_req_tp == NULL) { 142 pj_stun_session_respond(sess, req, PJ_STUN_SC_BAD_REQUEST, 143 "Missing REQUESTED-TRANSPORT attribute", 144 PJ_TRUE, src_addr, src_addr_len); 145 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 146 } 147 148 cfg->tp_type = PJ_STUN_GET_RT_PROTO(attr_req_tp->value); 149 150 /* Can only support UDP for now */ 151 if (cfg->tp_type != PJ_TURN_TP_UDP) { 152 pj_stun_session_respond(sess, req, PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO, 153 NULL, PJ_TRUE, src_addr, src_addr_len); 154 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO); 155 } 156 157 /* Get RESERVATION-TOKEN attribute, if any */ 158 attr_res_token = (pj_stun_res_token_attr*) 159 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_RESERVATION_TOKEN, 160 0); 161 if (attr_res_token) { 162 /* We don't support RESERVATION-TOKEN for now */ 163 pj_stun_session_respond(sess, req, 164 PJ_STUN_SC_BAD_REQUEST, 165 "RESERVATION-TOKEN is not supported", PJ_TRUE, 166 src_addr, src_addr_len); 167 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 168 } 169 170 /* Get REQUESTED-PROPS attribute, if any */ 171 attr_rpp = (pj_stun_req_props_attr*) 172 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_PROPS, 0); 173 if (attr_rpp) { 174 /* We don't support REQUESTED-PROPS for now */ 175 pj_stun_session_respond(sess, req, 176 PJ_STUN_SC_BAD_REQUEST, 177 "REQUESTED-PROPS is not supported", PJ_TRUE, 178 src_addr, src_addr_len); 179 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 180 } 181 182 /* Get LIFETIME attribute */ 183 attr_lifetime = (pj_stun_uint_attr*) 184 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_LIFETIME, 0); 185 if (attr_lifetime) { 186 cfg->lifetime = attr_lifetime->value; 187 if (cfg->lifetime < MIN_LIFETIME) { 188 pj_stun_session_respond(sess, req, PJ_STUN_SC_BAD_REQUEST, 189 "LIFETIME too short", PJ_TRUE, 190 src_addr, src_addr_len); 191 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 192 } 193 if (cfg->lifetime > MAX_LIFETIME) 194 cfg->lifetime = MAX_LIFETIME; 195 } else { 196 cfg->lifetime = DEF_LIFETIME; 197 } 198 199 return PJ_SUCCESS; 200 } 201 202 203 /* Respond to ALLOCATE request */ 204 static pj_status_t send_allocate_response(pj_turn_allocation *alloc, 205 pj_stun_session *srv_sess, 206 const pj_stun_msg *msg) 207 { 208 pj_stun_tx_data *tdata; 209 pj_status_t status; 210 211 /* Respond the original ALLOCATE request */ 212 status = pj_stun_session_create_res(srv_sess, msg, 0, NULL, &tdata); 213 if (status != PJ_SUCCESS) 214 return status; 215 216 /* Add RELAYED-ADDRESS attribute */ 217 pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, 218 PJ_STUN_ATTR_RELAY_ADDR, PJ_TRUE, 219 &alloc->relay.hkey.addr, 220 pj_sockaddr_get_len(&alloc->relay.hkey.addr)); 221 222 /* Add LIFETIME. */ 223 pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 224 PJ_STUN_ATTR_LIFETIME, 225 (unsigned)alloc->relay.lifetime); 226 227 /* Add BANDWIDTH */ 228 pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 229 PJ_STUN_ATTR_BANDWIDTH, 230 alloc->bandwidth); 231 232 /* Add RESERVATION-TOKEN */ 233 PJ_TODO(ADD_RESERVATION_TOKEN); 234 235 /* Add XOR-MAPPED-ADDRESS */ 236 pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, 237 PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE, 238 &alloc->hkey.clt_addr, 239 pj_sockaddr_get_len(&alloc->hkey.clt_addr)); 240 241 /* Send the response */ 242 return pj_stun_session_send_msg(srv_sess, PJ_TRUE, 243 &alloc->hkey.clt_addr, 244 pj_sockaddr_get_len(&alloc->hkey.clt_addr), 245 tdata); 246 } 247 248 249 /* 250 * Init credential for the allocation. We use static credential, meaning that 251 * the user's password must not change during allocation. 252 */ 253 static pj_status_t init_cred(pj_turn_allocation *alloc, const pj_stun_msg *req) 254 { 255 const pj_stun_username_attr *user; 256 const pj_stun_realm_attr *realm; 257 const pj_stun_nonce_attr *nonce; 258 pj_status_t status; 259 260 realm = (const pj_stun_realm_attr*) 261 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REALM, 0); 262 PJ_ASSERT_RETURN(realm != NULL, PJ_EBUG); 263 264 user = (const pj_stun_username_attr*) 265 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_USERNAME, 0); 266 PJ_ASSERT_RETURN(user != NULL, PJ_EBUG); 267 268 nonce = (const pj_stun_nonce_attr*) 269 pj_stun_msg_find_attr(req, PJ_STUN_ATTR_NONCE, 0); 270 PJ_ASSERT_RETURN(nonce != NULL, PJ_EBUG); 271 272 /* Lookup the password */ 273 status = pj_turn_get_password(NULL, NULL, &realm->value, 274 &user->value, alloc->pool, 275 &alloc->cred.data.static_cred.data_type, 276 &alloc->cred.data.static_cred.data); 277 if (status != PJ_SUCCESS) 278 return status; 279 280 /* Save credential */ 281 alloc->cred.type = PJ_STUN_AUTH_CRED_STATIC; 282 pj_strdup(alloc->pool, &alloc->cred.data.static_cred.realm, &realm->value); 283 pj_strdup(alloc->pool, &alloc->cred.data.static_cred.username, &user->value); 284 pj_strdup(alloc->pool, &alloc->cred.data.static_cred.nonce, &nonce->value); 285 286 return PJ_SUCCESS; 287 } 288 289 77 290 /* 78 291 * Create new allocation. 79 292 */ 80 PJ_DEF(pj_status_t) pj turn_allocation_create(pjturn_listener *listener,81 const pj_sockaddr_t *src_addr,82 unsigned src_addr_len,83 const pj_stun_msg *msg,84 const pjturn_allocation_req *req,85 pjturn_allocation **p_alloc)86 { 87 pj turn_srv *srv = listener->server;293 PJ_DEF(pj_status_t) pj_turn_allocation_create(pj_turn_listener *listener, 294 const pj_sockaddr_t *src_addr, 295 unsigned src_addr_len, 296 const pj_stun_msg *msg, 297 pj_stun_session *srv_sess, 298 pj_turn_allocation **p_alloc) 299 { 300 pj_turn_srv *srv = listener->server; 88 301 pj_pool_t *pool; 89 pjturn_allocation *alloc; 302 alloc_request req; 303 pj_turn_allocation *alloc; 90 304 pj_stun_session_cb sess_cb; 91 char relay_info[80];305 char str_tmp[80]; 92 306 pj_status_t status; 93 307 308 /* Parse ALLOCATE request */ 309 status = parse_allocate_req(&req, srv_sess, msg, src_addr, src_addr_len); 310 if (status != PJ_SUCCESS) 311 return status; 312 94 313 pool = pj_pool_create(srv->core.pf, "alloc%p", 1000, 1000, NULL); 95 314 96 315 /* Init allocation structure */ 97 alloc = PJ_POOL_ZALLOC_T(pool, pj turn_allocation);316 alloc = PJ_POOL_ZALLOC_T(pool, pj_turn_allocation); 98 317 alloc->pool = pool; 99 318 alloc->obj_name = pool->obj_name; … … 102 321 alloc->relay.tp.sock = PJ_INVALID_SOCKET; 103 322 104 alloc->bandwidth = req ->bandwidth;323 alloc->bandwidth = req.bandwidth; 105 324 106 325 alloc->hkey.tp_type = listener->tp_type; … … 110 329 &alloc->lock); 111 330 if (status != PJ_SUCCESS) { 112 pjturn_allocation_destroy(alloc); 113 return status; 331 goto on_error; 114 332 } 115 333 … … 121 339 122 340 /* Print info */ 123 pj_ansi_strcpy(alloc->info, pj turn_tp_type_name(listener->tp_type));341 pj_ansi_strcpy(alloc->info, pj_turn_tp_type_name(listener->tp_type)); 124 342 alloc->info[3] = ':'; 125 343 pj_sockaddr_print(src_addr, alloc->info+4, sizeof(alloc->info)-4, 3); … … 133 351 &sess_cb, PJ_FALSE, &alloc->sess); 134 352 if (status != PJ_SUCCESS) { 135 pjturn_allocation_destroy(alloc); 136 return status; 353 goto on_error; 137 354 } 138 355 … … 140 357 pj_stun_session_set_user_data(alloc->sess, alloc); 141 358 359 /* Init authentication credential */ 360 status = init_cred(alloc, msg); 361 if (status != PJ_SUCCESS) { 362 goto on_error; 363 } 364 365 /* Attach authentication credential to STUN session */ 366 pj_stun_session_set_credential(alloc->sess, &alloc->cred); 367 142 368 /* Create the relay resource */ 143 status = pjturn_allocation_create_relay(srv, alloc, msg, req, 144 &alloc->relay); 369 status = create_relay(srv, alloc, msg, &req, &alloc->relay); 145 370 if (status != PJ_SUCCESS) { 146 pjturn_allocation_destroy(alloc); 147 return status; 371 goto on_error; 148 372 } 149 373 150 374 /* Register this allocation */ 151 pjturn_srv_register_allocation(srv, alloc); 152 153 pj_sockaddr_print(&alloc->relay.hkey.addr, relay_info, 154 sizeof(relay_info), 3); 375 pj_turn_srv_register_allocation(srv, alloc); 376 377 /* Respond to ALLOCATE request */ 378 status = send_allocate_response(alloc, srv_sess, msg); 379 if (status != PJ_SUCCESS) 380 goto on_error; 381 382 /* Done */ 383 pj_sockaddr_print(&alloc->relay.hkey.addr, str_tmp, 384 sizeof(str_tmp), 3); 155 385 PJ_LOG(4,(alloc->obj_name, "Client %s created, relay addr=%s:%s", 156 alloc->info, pj turn_tp_type_name(req->tp_type), relay_info));386 alloc->info, pj_turn_tp_type_name(req.tp_type), str_tmp)); 157 387 158 388 /* Success */ 159 389 *p_alloc = alloc; 160 390 return PJ_SUCCESS; 161 } 162 163 164 /* 165 * Destroy allocation. 166 */ 167 PJ_DECL(void) pjturn_allocation_destroy(pjturn_allocation *alloc) 168 { 169 pj_pool_t *pool; 170 171 /* Unregister this allocation */ 172 pjturn_srv_unregister_allocation(alloc->listener->server, alloc); 173 174 /* Destroy relay */ 175 destroy_relay(&alloc->relay); 176 177 /* Must lock only after destroying relay otherwise deadlock */ 178 if (alloc->lock) { 179 pj_lock_acquire(alloc->lock); 180 } 181 182 /* Destroy STUN session */ 183 if (alloc->sess) { 184 pj_stun_session_destroy(alloc->sess); 185 alloc->sess = NULL; 186 } 187 188 /* Destroy lock */ 189 if (alloc->lock) { 190 pj_lock_release(alloc->lock); 191 pj_lock_destroy(alloc->lock); 192 alloc->lock = NULL; 193 } 194 195 /* Destroy pool */ 196 pool = alloc->pool; 197 if (pool) { 198 alloc->pool = NULL; 199 pj_pool_release(pool); 200 } 391 392 on_error: 393 /* Send reply to the ALLOCATE request */ 394 pj_strerror(status, str_tmp, sizeof(str_tmp)); 395 pj_stun_session_respond(srv_sess, msg, PJ_STUN_SC_BAD_REQUEST, str_tmp, 396 PJ_TRUE, src_addr, src_addr_len); 397 398 /* Cleanup */ 399 destroy_allocation(alloc); 400 return status; 201 401 } 202 402 203 403 204 404 /* Destroy relay resource */ 205 static void destroy_relay(pj turn_relay_res *relay)405 static void destroy_relay(pj_turn_relay_res *relay) 206 406 { 207 407 if (relay->timer.id) { … … 224 424 } 225 425 226 /* Initiate shutdown sequence for this allocation */ 227 static void alloc_shutdown(pjturn_allocation *alloc) 426 427 /* 428 * Really destroy allocation. 429 */ 430 static void destroy_allocation(pj_turn_allocation *alloc) 431 { 432 pj_pool_t *pool; 433 434 /* Unregister this allocation */ 435 pj_turn_srv_unregister_allocation(alloc->listener->server, alloc); 436 437 /* Destroy relay */ 438 destroy_relay(&alloc->relay); 439 440 /* Must lock only after destroying relay otherwise deadlock */ 441 if (alloc->lock) { 442 pj_lock_acquire(alloc->lock); 443 } 444 445 /* Destroy STUN session */ 446 if (alloc->sess) { 447 pj_stun_session_destroy(alloc->sess); 448 alloc->sess = NULL; 449 } 450 451 /* Destroy lock */ 452 if (alloc->lock) { 453 pj_lock_release(alloc->lock); 454 pj_lock_destroy(alloc->lock); 455 alloc->lock = NULL; 456 } 457 458 /* Destroy pool */ 459 pool = alloc->pool; 460 if (pool) { 461 alloc->pool = NULL; 462 pj_pool_release(pool); 463 } 464 } 465 466 467 PJ_DECL(void) pj_turn_allocation_destroy(pj_turn_allocation *alloc) 468 { 469 destroy_allocation(alloc); 470 } 471 472 473 /* Initiate shutdown sequence for this allocation and start destroy timer. 474 * Once allocation is marked as shutting down, any packets will be 475 * rejected/discarded 476 */ 477 static void alloc_shutdown(pj_turn_allocation *alloc) 228 478 { 229 479 pj_time_val destroy_delay = DESTROY_DELAY; … … 231 481 /* Work with existing schedule */ 232 482 if (alloc->relay.timer.id == TIMER_ID_TIMEOUT) { 233 /* Cancel existing timer */483 /* Cancel existing shutdown timer */ 234 484 pj_timer_heap_cancel(alloc->listener->server->core.timer_heap, 235 485 &alloc->relay.timer); … … 258 508 } 259 509 510 260 511 /* Reschedule timeout using current lifetime setting */ 261 static pj_status_t resched_timeout(pj turn_allocation *alloc)512 static pj_status_t resched_timeout(pj_turn_allocation *alloc) 262 513 { 263 514 pj_time_val delay; … … 292 543 static void relay_timeout_cb(pj_timer_heap_t *heap, pj_timer_entry *e) 293 544 { 294 pjturn_relay_res *rel; 295 pjturn_allocation *alloc; 296 297 rel = (pjturn_relay_res*) e->user_data; 545 pj_turn_relay_res *rel; 546 pj_turn_allocation *alloc; 547 548 PJ_UNUSED_ARG(heap); 549 550 rel = (pj_turn_relay_res*) e->user_data; 298 551 alloc = rel->allocation; 299 552 … … 314 567 alloc->info)); 315 568 316 pjturn_allocation_destroy(alloc);569 destroy_allocation(alloc); 317 570 } 318 571 } … … 322 575 * Create relay. 323 576 */ 324 PJ_DEF(pj_status_t) pjturn_allocation_create_relay(pjturn_srv *srv,325 pjturn_allocation *alloc,326 327 const pjturn_allocation_req*req,328 pjturn_relay_res *relay)577 static pj_status_t create_relay(pj_turn_srv *srv, 578 pj_turn_allocation *alloc, 579 const pj_stun_msg *msg, 580 const alloc_request *req, 581 pj_turn_relay_res *relay) 329 582 { 330 583 enum { RETRY = 40 }; … … 366 619 367 620 /* Create the socket */ 368 if (req->tp_type == PJ TURN_TP_UDP) {621 if (req->tp_type == PJ_TURN_TP_UDP) { 369 622 sock_type = pj_SOCK_DGRAM(); 370 } else if (req->tp_type == PJ TURN_TP_TCP) {623 } else if (req->tp_type == PJ_TURN_TP_TCP) { 371 624 sock_type = pj_SOCK_STREAM(); 372 625 } else { … … 396 649 if (req->rpp_port) { 397 650 port = (pj_uint16_t) req->rpp_port; 398 } else if (req->tp_type == PJ TURN_TP_UDP) {651 } else if (req->tp_type == PJ_TURN_TP_UDP) { 399 652 port = (pj_uint16_t) srv->ports.next_udp++; 400 653 if (srv->ports.next_udp > srv->ports.max_udp) 401 654 srv->ports.next_udp = srv->ports.min_udp; 402 } else if (req->tp_type == PJ TURN_TP_TCP) {655 } else if (req->tp_type == PJ_TURN_TP_TCP) { 403 656 port = (pj_uint16_t) srv->ports.next_tcp++; 404 657 if (srv->ports.next_tcp > srv->ports.max_tcp) … … 406 659 } else { 407 660 pj_assert(!"Invalid transport"); 661 port = 0; 408 662 } 409 663 … … 464 718 465 719 /* Create and send error response */ 466 static void send_reply_err(pj turn_allocation *alloc,720 static void send_reply_err(pj_turn_allocation *alloc, 467 721 const pj_stun_msg *req, 468 722 pj_bool_t cache, … … 470 724 { 471 725 pj_status_t status; 472 pj_str_t reason; 473 pj_stun_tx_data *tdata; 474 475 status = pj_stun_session_create_res(alloc->sess, req, 476 code, (errmsg?pj_cstr(&reason,errmsg):NULL), 477 &tdata); 478 if (status != PJ_SUCCESS) { 479 alloc_err(alloc, "Error creating STUN error response", status); 480 return; 481 } 482 483 status = pj_stun_session_send_msg(alloc->sess, cache, 484 &alloc->hkey.clt_addr, 485 pj_sockaddr_get_len(&alloc->hkey.clt_addr), 486 tdata); 726 727 status = pj_stun_session_respond(alloc->sess, req, code, errmsg, cache, 728 &alloc->hkey.clt_addr, 729 pj_sockaddr_get_len(&alloc->hkey.clt_addr.addr)); 487 730 if (status != PJ_SUCCESS) { 488 731 alloc_err(alloc, "Error sending STUN error response", status); … … 492 735 493 736 /* Create and send successful response */ 494 static void send_reply_ok(pj turn_allocation *alloc,737 static void send_reply_ok(pj_turn_allocation *alloc, 495 738 const pj_stun_msg *req) 496 739 { … … 535 778 536 779 /* Create new permission */ 537 static pj turn_permission *create_permission(pjturn_allocation *alloc,780 static pj_turn_permission *create_permission(pj_turn_allocation *alloc, 538 781 const pj_sockaddr_t *peer_addr, 539 782 unsigned addr_len) 540 783 { 541 pj turn_permission *perm;542 543 perm = PJ_POOL_ZALLOC_T(alloc->pool, pj turn_permission);784 pj_turn_permission *perm; 785 786 perm = PJ_POOL_ZALLOC_T(alloc->pool, pj_turn_permission); 544 787 pj_memcpy(&perm->hkey.peer_addr, peer_addr, addr_len); 545 788 546 if (alloc->listener->tp_type == PJ TURN_TP_UDP) {789 if (alloc->listener->tp_type == PJ_TURN_TP_UDP) { 547 790 perm->sock = alloc->listener->sock; 548 791 } else { … … 552 795 553 796 perm->allocation = alloc; 554 perm->channel = PJ TURN_INVALID_CHANNEL;797 perm->channel = PJ_TURN_INVALID_CHANNEL; 555 798 556 799 pj_gettimeofday(&perm->expiry); 557 perm->expiry.sec += PJ TURN_PERM_TIMEOUT;800 perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; 558 801 559 802 return perm; … … 561 804 562 805 /* Check if a permission isn't expired. Return NULL if expired. */ 563 static pj turn_permission *check_permission_expiry(pjturn_permission *perm)564 { 565 pj turn_allocation *alloc = perm->allocation;806 static pj_turn_permission *check_permission_expiry(pj_turn_permission *perm) 807 { 808 pj_turn_allocation *alloc = perm->allocation; 566 809 pj_time_val now; 567 810 … … 577 820 578 821 /* Remove from channel hash table, if assigned a channel number */ 579 if (perm->channel != PJ TURN_INVALID_CHANNEL) {822 if (perm->channel != PJ_TURN_INVALID_CHANNEL) { 580 823 pj_hash_set(NULL, alloc->ch_table, &perm->channel, 581 824 sizeof(perm->channel), 0, NULL); … … 586 829 587 830 /* Lookup permission in hash table by the peer address */ 588 static pj turn_permission*589 lookup_permission_by_addr(pj turn_allocation *alloc,831 static pj_turn_permission* 832 lookup_permission_by_addr(pj_turn_allocation *alloc, 590 833 const pj_sockaddr_t *peer_addr, 591 834 unsigned addr_len) 592 835 { 593 pj turn_permission_key key;594 pj turn_permission *perm;836 pj_turn_permission_key key; 837 pj_turn_permission *perm; 595 838 596 839 pj_bzero(&key, sizeof(key)); … … 598 841 599 842 /* Lookup in peer hash table */ 600 perm = (pj turn_permission*) pj_hash_get(alloc->peer_table, &key,843 perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, &key, 601 844 sizeof(key), NULL); 602 845 return check_permission_expiry(perm); … … 604 847 605 848 /* Lookup permission in hash table by the channel number */ 606 static pj turn_permission*607 lookup_permission_by_chnum(pj turn_allocation *alloc,849 static pj_turn_permission* 850 lookup_permission_by_chnum(pj_turn_allocation *alloc, 608 851 unsigned chnum) 609 852 { 610 853 pj_uint16_t chnum16 = (pj_uint16_t)chnum; 611 pj turn_permission *perm;854 pj_turn_permission *perm; 612 855 613 856 /* Lookup in peer hash table */ 614 perm = (pj turn_permission*) pj_hash_get(alloc->peer_table, &chnum16,857 perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, &chnum16, 615 858 sizeof(chnum16), NULL); 616 859 return check_permission_expiry(perm); … … 620 863 * Return PJ_TRUE is permission is found. 621 864 */ 622 static pj_bool_t refresh_permission(pj turn_permission *perm)865 static pj_bool_t refresh_permission(pj_turn_permission *perm) 623 866 { 624 867 pj_gettimeofday(&perm->expiry); 625 if (perm->channel == PJ TURN_INVALID_CHANNEL)626 perm->expiry.sec += PJ TURN_PERM_TIMEOUT;868 if (perm->channel == PJ_TURN_INVALID_CHANNEL) 869 perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; 627 870 else 628 perm->expiry.sec += PJ TURN_CHANNEL_TIMEOUT;871 perm->expiry.sec += PJ_TURN_CHANNEL_TIMEOUT; 629 872 return PJ_TRUE; 630 873 } 631 874 632 875 /* 633 * Handle incoming packet from client. 876 * Handle incoming packet from client. This would have been called by 877 * server upon receiving packet from a listener. 634 878 */ 635 PJ_DEF(void) pj turn_allocation_on_rx_client_pkt( pjturn_allocation *alloc,636 pj turn_pkt *pkt)879 PJ_DEF(void) pj_turn_allocation_on_rx_client_pkt(pj_turn_allocation *alloc, 880 pj_turn_pkt *pkt) 637 881 { 638 882 pj_bool_t is_stun; 639 883 pj_status_t status; 884 885 /* Lock this allocation */ 886 pj_lock_acquire(alloc->lock); 640 887 641 888 /* Quickly check if this is STUN message */ … … 650 897 */ 651 898 unsigned options = PJ_STUN_CHECK_PACKET; 652 if (pkt->listener->tp_type == PJ TURN_TP_UDP)899 if (pkt->listener->tp_type == PJ_TURN_TP_UDP) 653 900 options |= PJ_STUN_IS_DATAGRAM; 654 901 … … 659 906 if (status != PJ_SUCCESS) { 660 907 alloc_err(alloc, "Error handling STUN packet", status); 661 return;908 goto on_return; 662 909 } 663 910 … … 667 914 */ 668 915 channel_data_hdr *cd = (channel_data_hdr*)pkt->pkt; 669 pj turn_permission *perm;916 pj_turn_permission *perm; 670 917 pj_ssize_t len; 671 918 672 919 /* For UDP check the packet length */ 673 if (alloc->listener->tp_type == PJ TURN_TP_UDP) {920 if (alloc->listener->tp_type == PJ_TURN_TP_UDP) { 674 921 if (pkt->len < pj_ntohs(cd->length)+sizeof(*cd)) { 675 922 PJ_LOG(4,(alloc->obj_name, 676 923 "ChannelData from %s discarded: UDP size error", 677 924 alloc->info)); 678 return;925 goto on_return; 679 926 } 680 927 } else { 681 928 pj_assert(!"Unsupported transport"); 682 return;929 goto on_return; 683 930 } 684 931 … … 689 936 "ChannelData from %s discarded: not found", 690 937 alloc->info)); 691 return;938 goto on_return; 692 939 } 693 940 … … 701 948 refresh_permission(perm); 702 949 } 703 } 950 951 on_return: 952 /* Release lock */ 953 pj_lock_release(alloc->lock); 954 } 955 704 956 705 957 /* … … 707 959 * on_rx_from_peer(). 708 960 */ 709 static void on_rx_peer_pkt(pjturn_allocation *alloc,710 pjturn_relay_res *rel,711 char *pkt, pj_size_t len,712 const pj_sockaddr *src_addr)713 { 714 pj turn_permission *perm;961 static void handle_peer_pkt(pj_turn_allocation *alloc, 962 pj_turn_relay_res *rel, 963 char *pkt, pj_size_t len, 964 const pj_sockaddr *src_addr) 965 { 966 pj_turn_permission *perm; 715 967 716 968 /* Lookup permission */ … … 725 977 * this permission is attached to a channel number. 726 978 */ 727 if (perm->channel != PJ TURN_INVALID_CHANNEL) {979 if (perm->channel != PJ_TURN_INVALID_CHANNEL) { 728 980 /* Send ChannelData */ 729 981 channel_data_hdr *cd = (channel_data_hdr*)rel->tp.tx_pkt; 730 982 731 if (len > PJ TURN_MAX_PKT_LEN) {983 if (len > PJ_TURN_MAX_PKT_LEN) { 732 984 char peer_addr[80]; 733 985 pj_sockaddr_print(src_addr, peer_addr, sizeof(peer_addr), 3); 734 PJ_LOG( 1,(alloc->obj_name, "Client %s: discarded data from %s "986 PJ_LOG(4,(alloc->obj_name, "Client %s: discarded data from %s " 735 987 "because it's too long (%d bytes)", 736 988 alloc->info, peer_addr, len)); … … 746 998 747 999 /* Send to client */ 748 pj turn_listener_sendto(alloc->listener, rel->tp.tx_pkt,1000 pj_turn_listener_sendto(alloc->listener, rel->tp.tx_pkt, 749 1001 len+sizeof(channel_data_hdr), 0, 750 1002 &alloc->hkey.clt_addr, … … 771 1023 pj_ssize_t bytes_read) 772 1024 { 773 pj turn_relay_res *rel;1025 pj_turn_relay_res *rel; 774 1026 pj_status_t status; 775 1027 776 rel = (pjturn_relay_res*) pj_ioqueue_get_user_data(key); 1028 rel = (pj_turn_relay_res*) pj_ioqueue_get_user_data(key); 1029 1030 /* Lock the allocation */ 1031 pj_lock_acquire(rel->allocation->lock); 777 1032 778 1033 do { 779 1034 if (bytes_read > 0) { 780 on_rx_peer_pkt(rel->allocation, rel, rel->tp.rx_pkt,781 bytes_read, &rel->tp.src_addr);1035 handle_peer_pkt(rel->allocation, rel, rel->tp.rx_pkt, 1036 bytes_read, &rel->tp.src_addr); 782 1037 } 783 1038 … … 795 1050 } while (status != PJ_EPENDING && status != PJ_ECANCELLED); 796 1051 1052 /* Release allocation lock */ 1053 pj_lock_release(rel->allocation->lock); 797 1054 } 798 1055 … … 807 1064 unsigned addr_len) 808 1065 { 809 pj turn_allocation *alloc;810 811 alloc = (pj turn_allocation*) pj_stun_session_get_user_data(sess);812 813 return pj turn_listener_sendto(alloc->listener, pkt, pkt_size, 0,1066 pj_turn_allocation *alloc; 1067 1068 alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 1069 1070 return pj_turn_listener_sendto(alloc->listener, pkt, pkt_size, 0, 814 1071 dst_addr, addr_len); 815 1072 } … … 818 1075 * Callback notification from STUN session when it receives STUN 819 1076 * requests. This callback was trigger by STUN incoming message 820 * processing in pj turn_allocation_on_rx_client_pkt().1077 * processing in pj_turn_allocation_on_rx_client_pkt(). 821 1078 */ 822 1079 static pj_status_t stun_on_rx_request(pj_stun_session *sess, … … 827 1084 unsigned src_addr_len) 828 1085 { 829 pjturn_allocation *alloc; 830 831 alloc = (pjturn_allocation*) pj_stun_session_get_user_data(sess); 1086 pj_turn_allocation *alloc; 1087 1088 PJ_UNUSED_ARG(pkt); 1089 PJ_UNUSED_ARG(pkt_len); 1090 PJ_UNUSED_ARG(src_addr); 1091 PJ_UNUSED_ARG(src_addr_len); 1092 1093 alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 832 1094 833 1095 /* Refuse to serve any request if we've been shutdown */ 834 1096 if (alloc->relay.lifetime == 0) { 1097 /* Reject with 437 if we're shutting down */ 835 1098 send_reply_err(alloc, msg, PJ_TRUE, 836 1099 PJ_STUN_SC_ALLOCATION_MISMATCH, NULL); … … 895 1158 pj_stun_channel_number_attr *ch_attr; 896 1159 pj_stun_peer_addr_attr *peer_attr; 897 pj turn_permission *p1, *p2;1160 pj_turn_permission *p1, *p2; 898 1161 899 1162 ch_attr = (pj_stun_channel_number_attr*) … … 934 1197 p2 = lookup_permission_by_addr(alloc, &peer_attr->sockaddr, 935 1198 pj_sockaddr_get_len(&peer_attr->sockaddr)); 936 if (p2 && p2->channel != PJ TURN_INVALID_CHANNEL) {1199 if (p2 && p2->channel != PJ_TURN_INVALID_CHANNEL) { 937 1200 send_reply_err(alloc, msg, PJ_TRUE, PJ_STUN_SC_BAD_REQUEST, 938 1201 "Peer address already assigned a channel number"); … … 977 1240 * Callback notification from STUN session when it receives STUN 978 1241 * indications. This callback was trigger by STUN incoming message 979 * processing in pj turn_allocation_on_rx_client_pkt().1242 * processing in pj_turn_allocation_on_rx_client_pkt(). 980 1243 */ 981 1244 static pj_status_t stun_on_rx_indication(pj_stun_session *sess, … … 988 1251 pj_stun_peer_addr_attr *peer_attr; 989 1252 pj_stun_data_attr *data_attr; 990 pjturn_allocation *alloc; 991 pjturn_permission *perm; 992 993 alloc = (pjturn_allocation*) pj_stun_session_get_user_data(sess); 1253 pj_turn_allocation *alloc; 1254 pj_turn_permission *perm; 1255 1256 PJ_UNUSED_ARG(pkt); 1257 PJ_UNUSED_ARG(pkt_len); 1258 PJ_UNUSED_ARG(src_addr); 1259 PJ_UNUSED_ARG(src_addr_len); 1260 1261 alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 994 1262 995 1263 /* Only expect Send Indication */ … … 1025 1293 1026 1294 /* Relay the data to client */ 1027 if (alloc->hkey.tp_type == PJ TURN_TP_UDP) {1295 if (alloc->hkey.tp_type == PJ_TURN_TP_UDP) { 1028 1296 pj_ssize_t len = data_attr->length; 1029 1297 pj_sock_sendto(alloc->listener->sock, data_attr->data,
Note: See TracChangeset
for help on using the changeset viewer.