Changeset 1913 for pjproject/trunk/pjnath/src/pjturn-srv/allocation.c
- Timestamp:
- Apr 9, 2008 9:38:12 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjturn-srv/allocation.c
r1879 r1913 66 66 pj_ssize_t bytes_read); 67 67 static pj_status_t stun_on_send_msg(pj_stun_session *sess, 68 void *token, 68 69 const void *pkt, 69 70 pj_size_t pkt_size, … … 74 75 unsigned pkt_len, 75 76 const pj_stun_rx_data *rdata, 77 void *token, 76 78 const pj_sockaddr_t *src_addr, 77 79 unsigned src_addr_len); … … 80 82 unsigned pkt_len, 81 83 const pj_stun_msg *msg, 84 void *token, 82 85 const pj_sockaddr_t *src_addr, 83 86 unsigned src_addr_len); … … 124 127 pj_stun_session_respond(sess, rdata, 125 128 PJ_STUN_SC_ALLOCATION_QUOTA_REACHED, 126 "Invalid bandwidth", PJ_TRUE,129 "Invalid bandwidth", NULL, PJ_TRUE, 127 130 src_addr, src_addr_len); 128 131 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_ALLOCATION_QUOTA_REACHED); … … 135 138 pj_stun_session_respond(sess, rdata, PJ_STUN_SC_BAD_REQUEST, 136 139 "Missing REQUESTED-TRANSPORT attribute", 137 PJ_TRUE, src_addr, src_addr_len);140 NULL, PJ_TRUE, src_addr, src_addr_len); 138 141 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 139 142 } … … 144 147 if (cfg->tp_type != PJ_TURN_TP_UDP) { 145 148 pj_stun_session_respond(sess, rdata, PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO, 146 NULL, PJ_TRUE, src_addr, src_addr_len);149 NULL, NULL, PJ_TRUE, src_addr, src_addr_len); 147 150 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO); 148 151 } … … 156 159 pj_stun_session_respond(sess, rdata, 157 160 PJ_STUN_SC_BAD_REQUEST, 158 "RESERVATION-TOKEN is not supported", PJ_TRUE,159 src_addr, src_addr_len);161 "RESERVATION-TOKEN is not supported", NULL, 162 PJ_TRUE, src_addr, src_addr_len); 160 163 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 161 164 } … … 168 171 pj_stun_session_respond(sess, rdata, 169 172 PJ_STUN_SC_BAD_REQUEST, 170 "REQUESTED-PROPS is not supported", PJ_TRUE,171 src_addr, src_addr_len);173 "REQUESTED-PROPS is not supported", 174 NULL, PJ_TRUE, src_addr, src_addr_len); 172 175 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 173 176 } … … 180 183 if (cfg->lifetime < MIN_LIFETIME) { 181 184 pj_stun_session_respond(sess, rdata, PJ_STUN_SC_BAD_REQUEST, 182 "LIFETIME too short", PJ_TRUE,183 src_addr, src_addr_len);185 "LIFETIME too short", NULL, 186 PJ_TRUE, src_addr, src_addr_len); 184 187 return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 185 188 } … … 197 200 static pj_status_t send_allocate_response(pj_turn_allocation *alloc, 198 201 pj_stun_session *srv_sess, 202 pj_turn_transport *transport, 199 203 const pj_stun_rx_data *rdata) 200 204 { … … 233 237 234 238 /* Send the response */ 235 return pj_stun_session_send_msg(srv_sess, PJ_TRUE,236 &alloc->hkey.clt_addr,239 return pj_stun_session_send_msg(srv_sess, transport, PJ_TRUE, 240 PJ_FALSE, &alloc->hkey.clt_addr, 237 241 pj_sockaddr_get_len(&alloc->hkey.clt_addr), 238 242 tdata); … … 284 288 * Create new allocation. 285 289 */ 286 PJ_DEF(pj_status_t) pj_turn_allocation_create(pj_turn_ listener *listener,290 PJ_DEF(pj_status_t) pj_turn_allocation_create(pj_turn_transport *transport, 287 291 const pj_sockaddr_t *src_addr, 288 292 unsigned src_addr_len, … … 291 295 pj_turn_allocation **p_alloc) 292 296 { 293 pj_turn_srv *srv = listener->server;297 pj_turn_srv *srv = transport->listener->server; 294 298 const pj_stun_msg *msg = rdata->msg; 295 299 pj_pool_t *pool; … … 311 315 alloc->pool = pool; 312 316 alloc->obj_name = pool->obj_name; 313 alloc->listener = listener;314 alloc->clt_sock = PJ_INVALID_SOCKET;315 317 alloc->relay.tp.sock = PJ_INVALID_SOCKET; 318 alloc->server = transport->listener->server; 316 319 317 320 alloc->bandwidth = req.bandwidth; 318 321 319 alloc->hkey.tp_type = listener->tp_type; 322 /* Set transport */ 323 alloc->transport = transport; 324 pj_turn_transport_add_ref(transport, alloc); 325 326 alloc->hkey.tp_type = transport->listener->tp_type; 320 327 pj_memcpy(&alloc->hkey.clt_addr, src_addr, src_addr_len); 321 328 … … 333 340 334 341 /* Print info */ 335 pj_ansi_strcpy(alloc->info, pj_turn_tp_type_name(listener->tp_type)); 342 pj_ansi_strcpy(alloc->info, 343 pj_turn_tp_type_name(transport->listener->tp_type)); 336 344 alloc->info[3] = ':'; 337 345 pj_sockaddr_print(src_addr, alloc->info+4, sizeof(alloc->info)-4, 3); … … 371 379 372 380 /* Respond to ALLOCATE request */ 373 status = send_allocate_response(alloc, srv_sess, rdata);381 status = send_allocate_response(alloc, srv_sess, transport, rdata); 374 382 if (status != PJ_SUCCESS) 375 383 goto on_error; … … 389 397 pj_strerror(status, str_tmp, sizeof(str_tmp)); 390 398 pj_stun_session_respond(srv_sess, rdata, PJ_STUN_SC_BAD_REQUEST, str_tmp, 391 PJ_TRUE, src_addr, src_addr_len);399 transport, PJ_TRUE, src_addr, src_addr_len); 392 400 393 401 /* Cleanup */ … … 401 409 { 402 410 if (relay->timer.id) { 403 pj_timer_heap_cancel(relay->allocation-> listener->server->core.timer_heap,411 pj_timer_heap_cancel(relay->allocation->server->core.timer_heap, 404 412 &relay->timer); 405 413 relay->timer.id = PJ_FALSE; … … 428 436 429 437 /* Unregister this allocation */ 430 pj_turn_srv_unregister_allocation(alloc-> listener->server, alloc);438 pj_turn_srv_unregister_allocation(alloc->server, alloc); 431 439 432 440 /* Destroy relay */ … … 436 444 if (alloc->lock) { 437 445 pj_lock_acquire(alloc->lock); 446 } 447 448 /* Unreference transport */ 449 if (alloc->transport) { 450 pj_turn_transport_dec_ref(alloc->transport, alloc); 451 alloc->transport = NULL; 438 452 } 439 453 … … 466 480 467 481 482 /* 483 * Handle transport closure. 484 */ 485 PJ_DEF(void) pj_turn_allocation_on_transport_closed( pj_turn_allocation *alloc, 486 pj_turn_transport *tp) 487 { 488 PJ_LOG(5,(alloc->obj_name, "Transport %s unexpectedly closed, destroying " 489 "allocation %s", tp->info, alloc->info)); 490 pj_turn_transport_dec_ref(tp, alloc); 491 alloc->transport = NULL; 492 destroy_allocation(alloc); 493 } 494 495 468 496 /* Initiate shutdown sequence for this allocation and start destroy timer. 469 497 * Once allocation is marked as shutting down, any packets will be … … 477 505 if (alloc->relay.timer.id == TIMER_ID_TIMEOUT) { 478 506 /* Cancel existing shutdown timer */ 479 pj_timer_heap_cancel(alloc-> listener->server->core.timer_heap,507 pj_timer_heap_cancel(alloc->server->core.timer_heap, 480 508 &alloc->relay.timer); 481 509 alloc->relay.timer.id = TIMER_ID_NONE; … … 499 527 /* Schedule destroy timer */ 500 528 alloc->relay.timer.id = TIMER_ID_DESTROY; 501 pj_timer_heap_schedule(alloc-> listener->server->core.timer_heap,529 pj_timer_heap_schedule(alloc->server->core.timer_heap, 502 530 &alloc->relay.timer, &destroy_delay); 503 531 } … … 515 543 pj_assert(alloc->relay.timer.id != TIMER_ID_DESTROY); 516 544 if (alloc->relay.timer.id != 0) { 517 pj_timer_heap_cancel(alloc-> listener->server->core.timer_heap,545 pj_timer_heap_cancel(alloc->server->core.timer_heap, 518 546 &alloc->relay.timer); 519 547 alloc->relay.timer.id = TIMER_ID_NONE; … … 524 552 525 553 alloc->relay.timer.id = TIMER_ID_TIMEOUT; 526 status = pj_timer_heap_schedule(alloc-> listener->server->core.timer_heap,554 status = pj_timer_heap_schedule(alloc->server->core.timer_heap, 527 555 &alloc->relay.timer, &delay); 528 556 if (status != PJ_SUCCESS) { … … 590 618 591 619 /* TODO: get the requested address family from somewhere */ 592 af = alloc-> listener->addr.addr.sa_family;620 af = alloc->transport->listener->addr.addr.sa_family; 593 621 594 622 /* Save realm */ … … 687 715 } 688 716 if (!pj_sockaddr_has_addr(&relay->hkey.addr)) { 689 pj_sockaddr_copy_addr(&relay->hkey.addr, &alloc->listener->addr); 717 pj_sockaddr_copy_addr(&relay->hkey.addr, 718 &alloc->transport->listener->addr); 690 719 } 691 720 if (!pj_sockaddr_has_addr(&relay->hkey.addr)) { … … 725 754 pj_status_t status; 726 755 727 status = pj_stun_session_respond(alloc->sess, rdata, code, errmsg, cache,728 &alloc->hkey.clt_addr,756 status = pj_stun_session_respond(alloc->sess, rdata, code, errmsg, NULL, 757 cache, &alloc->hkey.clt_addr, 729 758 pj_sockaddr_get_len(&alloc->hkey.clt_addr.addr)); 730 759 if (status != PJ_SUCCESS) { … … 770 799 } 771 800 772 status = pj_stun_session_send_msg(alloc->sess, PJ_TRUE,773 &alloc->hkey.clt_addr,801 status = pj_stun_session_send_msg(alloc->sess, NULL, PJ_TRUE, 802 PJ_FALSE, &alloc->hkey.clt_addr, 774 803 pj_sockaddr_get_len(&alloc->hkey.clt_addr), 775 804 tdata); … … 791 820 pj_memcpy(&perm->hkey.peer_addr, peer_addr, addr_len); 792 821 793 if (alloc->listener->tp_type == PJ_TURN_TP_UDP) {794 perm->sock = alloc->listener->sock;795 } else {796 pj_assert(!"TCP is not supported yet");797 return NULL;798 }799 800 822 perm->allocation = alloc; 801 823 perm->channel = PJ_TURN_INVALID_CHANNEL; … … 901 923 */ 902 924 unsigned options = PJ_STUN_CHECK_PACKET; 903 if (pkt->listener->tp_type == PJ_TURN_TP_UDP) 925 unsigned parsed_len = 0; 926 927 if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) 904 928 options |= PJ_STUN_IS_DATAGRAM; 905 929 906 930 status = pj_stun_session_on_rx_pkt(alloc->sess, pkt->pkt, pkt->len, 907 options, NULL, 931 options, NULL, &parsed_len, 908 932 &pkt->src.clt_addr, 909 933 pkt->src_addr_len); 934 935 if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) { 936 pkt->len = 0; 937 } else if (parsed_len > 0) { 938 if (parsed_len == pkt->len) { 939 pkt->len = 0; 940 } else { 941 pj_memmove(pkt->pkt, pkt->pkt+parsed_len, 942 pkt->len - parsed_len); 943 pkt->len -= parsed_len; 944 } 945 } 946 910 947 if (status != PJ_SUCCESS) { 911 948 alloc_err(alloc, "Error handling STUN packet", status); … … 924 961 925 962 /* For UDP check the packet length */ 926 if (alloc-> listener->tp_type == PJ_TURN_TP_UDP) {963 if (alloc->transport->listener->tp_type == PJ_TURN_TP_UDP) { 927 964 if (pkt->len < pj_ntohs(cd->length)+sizeof(*cd)) { 928 965 PJ_LOG(4,(alloc->obj_name, … … 1004 1041 1005 1042 /* Send to client */ 1006 pj_turn_listener_sendto(alloc->listener, rel->tp.tx_pkt,1007 len+sizeof(pj_turn_channel_data), 0,1008 &alloc->hkey.clt_addr,1009 pj_sockaddr_get_len(&alloc->hkey.clt_addr));1043 alloc->transport->sendto(alloc->transport, rel->tp.tx_pkt, 1044 len+sizeof(pj_turn_channel_data), 0, 1045 &alloc->hkey.clt_addr, 1046 pj_sockaddr_get_len(&alloc->hkey.clt_addr)); 1010 1047 } else { 1011 1048 /* Send Data Indication */ … … 1027 1064 (const pj_uint8_t*)pkt, len); 1028 1065 1029 pj_stun_session_send_msg(alloc->sess, PJ_FALSE,1030 &alloc->hkey.clt_addr,1066 pj_stun_session_send_msg(alloc->sess, NULL, PJ_FALSE, 1067 PJ_FALSE, &alloc->hkey.clt_addr, 1031 1068 pj_sockaddr_get_len(&alloc->hkey.clt_addr), 1032 1069 tdata); … … 1077 1114 */ 1078 1115 static pj_status_t stun_on_send_msg(pj_stun_session *sess, 1116 void *token, 1079 1117 const void *pkt, 1080 1118 pj_size_t pkt_size, … … 1084 1122 pj_turn_allocation *alloc; 1085 1123 1124 PJ_UNUSED_ARG(token); 1125 1086 1126 alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 1087 1127 1088 return pj_turn_listener_sendto(alloc->listener, pkt, pkt_size, 0,1089 dst_addr, addr_len);1128 return alloc->transport->sendto(alloc->transport, pkt, pkt_size, 0, 1129 dst_addr, addr_len); 1090 1130 } 1091 1131 … … 1099 1139 unsigned pkt_len, 1100 1140 const pj_stun_rx_data *rdata, 1141 void *token, 1101 1142 const pj_sockaddr_t *src_addr, 1102 1143 unsigned src_addr_len) … … 1107 1148 PJ_UNUSED_ARG(pkt); 1108 1149 PJ_UNUSED_ARG(pkt_len); 1150 PJ_UNUSED_ARG(token); 1109 1151 PJ_UNUSED_ARG(src_addr); 1110 1152 PJ_UNUSED_ARG(src_addr_len); … … 1275 1317 unsigned pkt_len, 1276 1318 const pj_stun_msg *msg, 1319 void *token, 1277 1320 const pj_sockaddr_t *src_addr, 1278 1321 unsigned src_addr_len) … … 1282 1325 pj_turn_allocation *alloc; 1283 1326 pj_turn_permission *perm; 1327 pj_ssize_t len; 1284 1328 1285 1329 PJ_UNUSED_ARG(pkt); 1286 1330 PJ_UNUSED_ARG(pkt_len); 1331 PJ_UNUSED_ARG(token); 1287 1332 PJ_UNUSED_ARG(src_addr); 1288 1333 PJ_UNUSED_ARG(src_addr_len); … … 1321 1366 return PJ_SUCCESS; 1322 1367 1323 /* Relay the data to client */ 1324 if (alloc->hkey.tp_type == PJ_TURN_TP_UDP) { 1325 pj_ssize_t len = data_attr->length; 1326 pj_sock_sendto(alloc->listener->sock, data_attr->data, 1327 &len, 0, &peer_attr->sockaddr, 1328 pj_sockaddr_get_len(&peer_attr->sockaddr)); 1329 } else { 1330 pj_assert(!"TCP is not supported"); 1331 } 1368 /* Relay the data to peer */ 1369 len = data_attr->length; 1370 pj_sock_sendto(alloc->relay.tp.sock, data_attr->data, 1371 &len, 0, &peer_attr->sockaddr, 1372 pj_sockaddr_get_len(&peer_attr->sockaddr)); 1332 1373 1333 1374 return PJ_SUCCESS;
Note: See TracChangeset
for help on using the changeset viewer.