Changeset 4728 for pjproject/trunk/pjnath/src/pjturn-srv/server.c
- Timestamp:
- Feb 4, 2014 10:13:56 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjturn-srv/server.c
r4360 r4728 1 1 /* $Id$ */ 2 /* 2 /* 3 3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) 4 4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> … … 16 16 * You should have received a copy of the GNU General Public License 17 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 19 */ 20 20 #include "turn.h" … … 77 77 * Create server. 78 78 */ 79 PJ_DEF(pj_status_t) pj_turn_srv_create(pj_pool_factory *pf, 79 PJ_DEF(pj_status_t) pj_turn_srv_create(pj_pool_factory *pf, 80 80 pj_turn_srv **p_srv) 81 81 { … … 95 95 srv->core.pool = pool; 96 96 srv->core.tls_key = srv->core.tls_data = -1; 97 97 98 98 /* Create ioqueue */ 99 99 status = pj_ioqueue_create(pool, MAX_HANDLES, &srv->core.ioqueue); … … 102 102 103 103 /* Server mutex */ 104 status = pj_lock_create_recursive_mutex(pool, srv->obj_name, 104 status = pj_lock_create_recursive_mutex(pool, srv->obj_name, 105 105 &srv->core.lock); 106 106 if (status != PJ_SUCCESS) … … 115 115 if (status != PJ_SUCCESS) 116 116 goto on_error; 117 117 118 118 /* Create timer heap */ 119 119 status = pj_timer_heap_create(pool, MAX_TIMER, &srv->core.timer_heap); … … 126 126 /* Array of listeners */ 127 127 srv->core.listener = (pj_turn_listener**) 128 pj_pool_calloc(pool, MAX_LISTENERS, 128 pj_pool_calloc(pool, MAX_LISTENERS, 129 129 sizeof(srv->core.listener[0])); 130 130 … … 170 170 srv->core.thread_cnt = MAX_THREADS; 171 171 srv->core.thread = (pj_thread_t**) 172 pj_pool_calloc(pool, srv->core.thread_cnt, 172 pj_pool_calloc(pool, srv->core.thread_cnt, 173 173 sizeof(pj_thread_t*)); 174 174 175 175 /* Start the worker threads */ 176 176 for (i=0; i<srv->core.thread_cnt; ++i) { 177 status = pj_thread_create(pool, srv->obj_name, &server_thread_proc, 177 status = pj_thread_create(pool, srv->obj_name, &server_thread_proc, 178 178 srv, 0, 0, &srv->core.thread[i]); 179 179 if (status != PJ_SUCCESS) … … 182 182 183 183 /* We're done. Application should add listeners now */ 184 PJ_LOG(4,(srv->obj_name, "TURN server v%s is running", 184 PJ_LOG(4,(srv->obj_name, "TURN server v%s is running", 185 185 pj_get_version())); 186 186 … … 194 194 195 195 196 /* 197 * Handle timer and network events 196 /* 197 * Handle timer and network events 198 198 */ 199 199 static void srv_handle_events(pj_turn_srv *srv, const pj_time_val *max_timeout) … … 204 204 int c; 205 205 206 /* Poll the timer. The timer heap has its own mutex for better 207 * granularity, so we don't need to lock the server. 206 /* Poll the timer. The timer heap has its own mutex for better 207 * granularity, so we don't need to lock the server. 208 208 */ 209 209 timeout.sec = timeout.msec = 0; … … 223 223 } 224 224 225 /* Poll ioqueue. 225 /* Poll ioqueue. 226 226 * Repeat polling the ioqueue while we have immediate events, because 227 227 * timer heap may process more than one events, so if we only process … … 293 293 } 294 294 } 295 295 296 296 /* Destroy all listeners. */ 297 297 for (i=0; i<srv->core.lis_cnt; ++i) { … … 313 313 srv->tables.res = NULL; 314 314 } 315 315 316 316 /* Destroy timer heap */ 317 317 if (srv->core.timer_heap) { … … 461 461 462 462 463 /* Callback from our own STUN session whenever it needs to send 463 /* Callback from our own STUN session whenever it needs to send 464 464 * outgoing STUN packet. 465 465 */ … … 472 472 { 473 473 pj_turn_transport *transport = (pj_turn_transport*) token; 474 474 475 475 PJ_ASSERT_RETURN(transport!=NULL, PJ_EINVALIDOP); 476 476 477 477 PJ_UNUSED_ARG(sess); 478 478 479 return transport->sendto(transport, pdu, pdu_size, 0, 479 return transport->sendto(transport, pdu, pdu_size, 0, 480 480 dst_addr, addr_len); 481 481 } … … 483 483 484 484 /* Respond to STUN request */ 485 static pj_status_t stun_respond(pj_stun_session *sess, 485 static pj_status_t stun_respond(pj_stun_session *sess, 486 486 pj_turn_transport *transport, 487 487 const pj_stun_rx_data *rdata, 488 unsigned code, 488 unsigned code, 489 489 const char *errmsg, 490 pj_bool_t cache, 491 const pj_sockaddr_t *dst_addr, 490 pj_bool_t cache, 491 const pj_sockaddr_t *dst_addr, 492 492 unsigned addr_len) 493 493 { … … 497 497 498 498 /* Create response */ 499 status = pj_stun_session_create_res(sess, rdata, code, 499 status = pj_stun_session_create_res(sess, rdata, code, 500 500 (errmsg?pj_cstr(&reason,errmsg):NULL), 501 501 &tdata); … … 504 504 505 505 /* Send the response */ 506 return pj_stun_session_send_msg(sess, transport, cache, PJ_FALSE, 506 return pj_stun_session_send_msg(sess, transport, cache, PJ_FALSE, 507 507 dst_addr, addr_len, tdata); 508 508 } … … 523 523 pj_turn_transport *transport; 524 524 const pj_stun_msg *msg = rdata->msg; 525 pj_turn_srv *srv;526 525 pj_turn_allocation *alloc; 527 526 pj_status_t status; … … 531 530 532 531 transport = (pj_turn_transport*) token; 533 srv = transport->listener->server;534 532 535 533 /* Respond any requests other than ALLOCATE with 437 response */ … … 570 568 571 569 /* Create response */ 572 status = pj_stun_msg_create_response(pkt->pool, request, 0, NULL, 570 status = pj_stun_msg_create_response(pkt->pool, request, 0, NULL, 573 571 &response); 574 572 if (status != PJ_SUCCESS) … … 576 574 577 575 /* Add XOR-MAPPED-ADDRESS */ 578 pj_stun_msg_add_sockaddr_attr(pkt->pool, response, 576 pj_stun_msg_add_sockaddr_attr(pkt->pool, response, 579 577 PJ_STUN_ATTR_XOR_MAPPED_ADDR, 580 578 PJ_TRUE, … … 588 586 589 587 /* Send response */ 590 pkt->transport->sendto(pkt->transport, pdu, len, 0, 588 pkt->transport->sendto(pkt->transport, pdu, len, 0, 591 589 &pkt->src.clt_addr, pkt->src_addr_len); 592 590 } … … 599 597 * if an allocation is not found. 600 598 */ 601 PJ_DEF(void) pj_turn_srv_on_rx_pkt(pj_turn_srv *srv, 599 PJ_DEF(void) pj_turn_srv_on_rx_pkt(pj_turn_srv *srv, 602 600 pj_turn_pkt *pkt) 603 601 { … … 639 637 if ((*pkt->pkt != 0x00 && *pkt->pkt != 0x01) || 640 638 pkt->len > 1600 || 641 (options & PJ_STUN_IS_DATAGRAM)) 639 (options & PJ_STUN_IS_DATAGRAM)) 642 640 { 643 641 char errmsg[PJ_ERR_MSG_SIZE]; … … 647 645 648 646 pj_strerror(status, errmsg, sizeof(errmsg)); 649 PJ_LOG(5,(srv->obj_name, 647 PJ_LOG(5,(srv->obj_name, 650 648 "Non-STUN packet from %s is dropped: %s", 651 649 pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3), … … 655 653 } 656 654 657 /* Special handling for Binding Request. We won't give it to the 655 /* Special handling for Binding Request. We won't give it to the 658 656 * STUN session since this request is not authenticated. 659 657 */ … … 669 667 options &= ~PJ_STUN_CHECK_PACKET; 670 668 parsed_len = 0; 671 status = pj_stun_session_on_rx_pkt(srv->core.stun_sess, pkt->pkt, 669 status = pj_stun_session_on_rx_pkt(srv->core.stun_sess, pkt->pkt, 672 670 pkt->len, options, pkt->transport, 673 &parsed_len, &pkt->src.clt_addr, 671 &parsed_len, &pkt->src.clt_addr, 674 672 pkt->src_addr_len); 675 673 if (status != PJ_SUCCESS) { … … 678 676 679 677 pj_strerror(status, errmsg, sizeof(errmsg)); 680 PJ_LOG(5,(srv->obj_name, 678 PJ_LOG(5,(srv->obj_name, 681 679 "Error processing STUN packet from %s: %s", 682 680 pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3),
Note: See TracChangeset
for help on using the changeset viewer.