Changeset 4728 for pjproject/trunk/pjnath/src/pjnath/ice_strans.c
- Timestamp:
- Feb 4, 2014 10:13:56 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath/ice_strans.c
r4606 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 <pjnath/ice_strans.h> … … 66 66 67 67 /* The candidate type preference when STUN candidate is used */ 68 static pj_uint8_t srflx_pref_table[PJ_ICE_CAND_TYPE_MAX] = 68 static pj_uint8_t srflx_pref_table[PJ_ICE_CAND_TYPE_MAX] = 69 69 { 70 70 #if PJNATH_ICE_PRIO_STD … … 85 85 /* ICE callbacks */ 86 86 static void on_ice_complete(pj_ice_sess *ice, pj_status_t status); 87 static pj_status_t ice_tx_pkt(pj_ice_sess *ice, 87 static pj_status_t ice_tx_pkt(pj_ice_sess *ice, 88 88 unsigned comp_id, 89 89 unsigned transport_id, … … 91 91 const pj_sockaddr_t *dst_addr, 92 92 unsigned dst_addr_len); 93 static void ice_rx_data(pj_ice_sess *ice, 94 unsigned comp_id, 93 static void ice_rx_data(pj_ice_sess *ice, 94 unsigned comp_id, 95 95 unsigned transport_id, 96 96 void *pkt, pj_size_t size, … … 111 111 pj_ssize_t sent); 112 112 /* Notification when the status of the STUN transport has changed. */ 113 static pj_bool_t stun_on_status(pj_stun_sock *stun_sock, 113 static pj_bool_t stun_on_status(pj_stun_sock *stun_sock, 114 114 pj_stun_sock_op op, 115 115 pj_status_t status); … … 137 137 * in ICE stream transport typically corresponds to a single socket created 138 138 * for this component, and bound to a specific transport address. This 139 * component may have multiple alias addresses, for example one alias 139 * component may have multiple alias addresses, for example one alias 140 140 * address for each interfaces in multi-homed host, another for server 141 141 * reflexive alias, and another for relayed alias. For each transport … … 299 299 /* Override with component specific socket buffer size settings, if any */ 300 300 if (ice_st->cfg.comp[comp->comp_id-1].so_rcvbuf_size > 0) { 301 ice_st->cfg.turn.cfg.so_rcvbuf_size = 301 ice_st->cfg.turn.cfg.so_rcvbuf_size = 302 302 ice_st->cfg.comp[comp->comp_id-1].so_rcvbuf_size; 303 303 } 304 304 if (ice_st->cfg.comp[comp->comp_id-1].so_sndbuf_size > 0) { 305 ice_st->cfg.turn.cfg.so_sndbuf_size = 305 ice_st->cfg.turn.cfg.so_sndbuf_size = 306 306 ice_st->cfg.comp[comp->comp_id-1].so_sndbuf_size; 307 307 } … … 382 382 stun_sock_cb.on_status = &stun_on_status; 383 383 stun_sock_cb.on_data_sent = &stun_on_data_sent; 384 384 385 385 /* Override component specific QoS settings, if any */ 386 386 if (ice_st->cfg.comp[comp_id-1].qos_type) { 387 ice_st->cfg.stun.cfg.qos_type = 387 ice_st->cfg.stun.cfg.qos_type = 388 388 ice_st->cfg.comp[comp_id-1].qos_type; 389 389 } … … 396 396 /* Override component specific socket buffer size settings, if any */ 397 397 if (ice_st->cfg.comp[comp_id-1].so_rcvbuf_size > 0) { 398 ice_st->cfg.stun.cfg.so_rcvbuf_size = 398 ice_st->cfg.stun.cfg.so_rcvbuf_size = 399 399 ice_st->cfg.comp[comp_id-1].so_rcvbuf_size; 400 400 } 401 401 if (ice_st->cfg.comp[comp_id-1].so_sndbuf_size > 0) { 402 ice_st->cfg.stun.cfg.so_sndbuf_size = 402 ice_st->cfg.stun.cfg.so_sndbuf_size = 403 403 ice_st->cfg.comp[comp_id-1].so_sndbuf_size; 404 404 } … … 412 412 return status; 413 413 414 /* Start STUN Binding resolution and add srflx candidate 415 * only if server is set 414 /* Start STUN Binding resolution and add srflx candidate 415 * only if server is set 416 416 */ 417 417 if (ice_st->cfg.stun.server.slen) { … … 421 421 ///sess_add_ref(ice_st); 422 422 423 PJ_LOG(4,(ice_st->obj_name, 423 PJ_LOG(4,(ice_st->obj_name, 424 424 "Comp %d: srflx candidate starts Binding discovery", 425 425 comp_id)); … … 428 428 429 429 /* Start Binding resolution */ 430 status = pj_stun_sock_start(comp->stun_sock, 430 status = pj_stun_sock_start(comp->stun_sock, 431 431 &ice_st->cfg.stun.server, 432 ice_st->cfg.stun.port, 432 ice_st->cfg.stun.port, 433 433 ice_st->cfg.resolver); 434 434 if (status != PJ_SUCCESS) { … … 476 476 return status; 477 477 478 for (i=0; i<stun_sock_info.alias_cnt && 479 i<ice_st->cfg.stun.max_host_cands; ++i) 478 for (i=0; i<stun_sock_info.alias_cnt && 479 i<ice_st->cfg.stun.max_host_cands; ++i) 480 480 { 481 481 char addrinfo[PJ_INET6_ADDRSTRLEN+10]; … … 488 488 } 489 489 490 /* Ignore loopback addresses unless cfg->stun.loop_addr 491 * is set 490 /* Ignore loopback addresses unless cfg->stun.loop_addr 491 * is set 492 492 */ 493 493 if ((pj_ntohl(addr->ipv4.sin_addr.s_addr)>>24)==127) { … … 509 509 cand->type, &cand->base_addr); 510 510 511 PJ_LOG(4,(ice_st->obj_name, 511 PJ_LOG(4,(ice_st->obj_name, 512 512 "Comp %d: host candidate %s added", 513 513 comp_id, pj_sockaddr_print(&cand->addr, addrinfo, … … 533 533 534 534 535 /* 536 * Create ICE stream transport 535 /* 536 * Create ICE stream transport 537 537 */ 538 538 PJ_DEF(pj_status_t) pj_ice_strans_create( const char *name, … … 565 565 ice_st->user_data = user_data; 566 566 567 PJ_LOG(4,(ice_st->obj_name, 567 PJ_LOG(4,(ice_st->obj_name, 568 568 "Creating ICE stream transport with %d component(s)", 569 569 comp_cnt)); … … 587 587 588 588 ice_st->comp_cnt = comp_cnt; 589 ice_st->comp = (pj_ice_strans_comp**) 589 ice_st->comp = (pj_ice_strans_comp**) 590 590 pj_pool_calloc(pool, comp_cnt, sizeof(pj_ice_strans_comp*)); 591 591 … … 593 593 ice_st->state = PJ_ICE_STRANS_STATE_INIT; 594 594 595 /* Acquire initialization mutex to prevent callback to be 595 /* Acquire initialization mutex to prevent callback to be 596 596 * called before we finish initialization. 597 597 */ … … 750 750 ice_st->state = PJ_ICE_STRANS_STATE_READY; 751 751 if (ice_st->cb.on_ice_complete) 752 (*ice_st->cb.on_ice_complete)(ice_st, PJ_ICE_STRANS_OP_INIT, 752 (*ice_st->cb.on_ice_complete)(ice_st, PJ_ICE_STRANS_OP_INIT, 753 753 PJ_SUCCESS); 754 754 } … … 786 786 787 787 /* 788 * Specify various options for this ICE stream transport. 788 * Specify various options for this ICE stream transport. 789 789 */ 790 790 PJ_DEF(pj_status_t) pj_ice_strans_set_options(pj_ice_strans *ice_st, … … 835 835 /* Create! */ 836 836 status = pj_ice_sess_create(&ice_st->cfg.stun_cfg, ice_st->obj_name, role, 837 ice_st->comp_cnt, &ice_cb, 837 ice_st->comp_cnt, &ice_cb, 838 838 local_ufrag, local_passwd, 839 839 ice_st->grp_lock, … … 853 853 */ 854 854 if (ice_st->comp[0]->default_cand >= 0 && 855 ice_st->comp[0]->cand_list[ice_st->comp[0]->default_cand].type 855 ice_st->comp[0]->cand_list[ice_st->comp[0]->default_cand].type 856 856 == PJ_ICE_CAND_TYPE_SRFLX) 857 857 { … … 866 866 /* Re-enable logging for Send/Data indications */ 867 867 if (comp->turn_sock) { 868 PJ_LOG(5,(ice_st->obj_name, 868 PJ_LOG(5,(ice_st->obj_name, 869 869 "Disabling STUN Indication logging for " 870 870 "component %d", i+1)); … … 879 879 /* Skip if candidate is not ready */ 880 880 if (cand->status != PJ_SUCCESS) { 881 PJ_LOG(5,(ice_st->obj_name, 881 PJ_LOG(5,(ice_st->obj_name, 882 882 "Candidate %d of comp %d is not added (pending)", 883 883 j, i)); … … 889 889 890 890 /* Add the candidate */ 891 status = pj_ice_sess_add_cand(ice_st->ice, comp->comp_id, 892 cand->transport_id, cand->type, 893 cand->local_pref, 894 &cand->foundation, &cand->addr, 891 status = pj_ice_sess_add_cand(ice_st->ice, comp->comp_id, 892 cand->transport_id, cand->type, 893 cand->local_pref, 894 &cand->foundation, &cand->addr, 895 895 &cand->base_addr, &cand->rel_addr, 896 896 pj_sockaddr_get_len(&cand->addr), … … 912 912 913 913 /* 914 * Check if the ICE stream transport has the ICE session created. 914 * Check if the ICE stream transport has the ICE session created. 915 915 */ 916 916 PJ_DEF(pj_bool_t) pj_ice_strans_has_sess(pj_ice_strans *ice_st) … … 985 985 unsigned i, cnt; 986 986 987 PJ_ASSERT_RETURN(ice_st && ice_st->ice && comp_id && 987 PJ_ASSERT_RETURN(ice_st && ice_st->ice && comp_id && 988 988 comp_id <= ice_st->comp_cnt, 0); 989 989 … … 1008 1008 unsigned i, cnt; 1009 1009 1010 PJ_ASSERT_RETURN(ice_st && ice_st->ice && comp_id && 1010 PJ_ASSERT_RETURN(ice_st && ice_st->ice && comp_id && 1011 1011 comp_id <= ice_st->comp_cnt && count && cand, PJ_EINVAL); 1012 1012 … … 1042 1042 pj_ice_strans_comp *comp = ice_st->comp[comp_id - 1]; 1043 1043 pj_assert(comp->default_cand>=0 && comp->default_cand<comp->cand_cnt); 1044 pj_memcpy(cand, &comp->cand_list[comp->default_cand], 1044 pj_memcpy(cand, &comp->cand_list[comp->default_cand], 1045 1045 sizeof(pj_ice_sess_cand)); 1046 1046 } … … 1108 1108 1109 1109 if (count) { 1110 status = pj_turn_sock_set_perm(comp->turn_sock, count, 1110 status = pj_turn_sock_set_perm(comp->turn_sock, count, 1111 1111 addrs, 0); 1112 1112 if (status != PJ_SUCCESS) { … … 1132 1132 * Get valid pair. 1133 1133 */ 1134 PJ_DEF(const pj_ice_sess_check*) 1134 PJ_DEF(const pj_ice_sess_check*) 1135 1135 pj_ice_strans_get_valid_pair(const pj_ice_strans *ice_st, 1136 1136 unsigned comp_id) … … 1138 1138 PJ_ASSERT_RETURN(ice_st && comp_id && comp_id <= ice_st->comp_cnt, 1139 1139 NULL); 1140 1140 1141 1141 if (ice_st->ice == NULL) 1142 1142 return NULL; 1143 1143 1144 1144 return ice_st->ice->comp[comp_id-1].valid_check; 1145 1145 } … … 1171 1171 int dst_addr_len) 1172 1172 { 1173 pj_ssize_t pkt_size;1174 1173 pj_ice_strans_comp *comp; 1175 1174 unsigned def_cand; … … 1220 1219 if (!comp->turn_log_off) { 1221 1220 /* Disable logging for Send/Data indications */ 1222 PJ_LOG(5,(ice_st->obj_name, 1221 PJ_LOG(5,(ice_st->obj_name, 1223 1222 "Disabling STUN Indication logging for " 1224 1223 "component %d", comp->comp_id)); … … 1227 1226 } 1228 1227 1229 status = pj_turn_sock_sendto(comp->turn_sock, 1230 (const pj_uint8_t*)data, 1228 status = pj_turn_sock_sendto(comp->turn_sock, 1229 (const pj_uint8_t*)data, 1231 1230 (unsigned)data_len, 1232 1231 dst_addr, dst_addr_len); 1233 return (status==PJ_SUCCESS||status==PJ_EPENDING) ? 1232 return (status==PJ_SUCCESS||status==PJ_EPENDING) ? 1234 1233 PJ_SUCCESS : status; 1235 1234 } else { 1236 pkt_size = data_len; 1237 status = pj_stun_sock_sendto(comp->stun_sock, NULL, data, 1238 (unsigned)data_len, 0, dst_addr, 1235 status = pj_stun_sock_sendto(comp->stun_sock, NULL, data, 1236 (unsigned)data_len, 0, dst_addr, 1239 1237 dst_addr_len); 1240 return (status==PJ_SUCCESS||status==PJ_EPENDING) ? 1238 return (status==PJ_SUCCESS||status==PJ_EPENDING) ? 1241 1239 PJ_SUCCESS : status; 1242 1240 } … … 1266 1264 char errmsg[PJ_ERR_MSG_SIZE]; 1267 1265 pj_strerror(status, errmsg, sizeof(errmsg)); 1268 PJ_LOG(4,(ice_st->obj_name, 1269 "ICE negotiation failed after %ds:%03d: %s", 1266 PJ_LOG(4,(ice_st->obj_name, 1267 "ICE negotiation failed after %ds:%03d: %s", 1270 1268 msec/1000, msec%1000, errmsg)); 1271 1269 } else { … … 1277 1275 }; 1278 1276 1279 PJ_LOG(4,(ice_st->obj_name, 1277 PJ_LOG(4,(ice_st->obj_name, 1280 1278 "ICE negotiation success after %ds:%03d", 1281 1279 msec/1000, msec%1000)); … … 1289 1287 char rip[PJ_INET6_ADDRSTRLEN+10]; 1290 1288 1291 pj_sockaddr_print(&check->lcand->addr, lip, 1289 pj_sockaddr_print(&check->lcand->addr, lip, 1292 1290 sizeof(lip), 3); 1293 pj_sockaddr_print(&check->rcand->addr, rip, 1291 pj_sockaddr_print(&check->rcand->addr, rip, 1294 1292 sizeof(rip), 3); 1295 1293 … … 1304 1302 1305 1303 /* Disable logging for Send/Data indications */ 1306 PJ_LOG(5,(ice_st->obj_name, 1304 PJ_LOG(5,(ice_st->obj_name, 1307 1305 "Disabling STUN Indication logging for " 1308 1306 "component %d", i+1)); … … 1315 1313 "sending from %s candidate %s to " 1316 1314 "%s candidate %s", 1317 i+1, 1315 i+1, 1318 1316 pj_ice_get_cand_type_name(check->lcand->type), 1319 1317 lip, 1320 1318 pj_ice_get_cand_type_name(check->rcand->type), 1321 1319 rip)); 1322 1320 1323 1321 } else { 1324 PJ_LOG(4,(ice_st->obj_name, 1322 PJ_LOG(4,(ice_st->obj_name, 1325 1323 "Comp %d: disabled", i+1)); 1326 1324 } … … 1332 1330 1333 1331 pj_log_push_indent(); 1334 (*ice_st->cb.on_ice_complete)(ice_st, PJ_ICE_STRANS_OP_NEGOTIATION, 1332 (*ice_st->cb.on_ice_complete)(ice_st, PJ_ICE_STRANS_OP_NEGOTIATION, 1335 1333 status); 1336 1334 pj_log_pop_indent(); 1337 1335 1338 1336 } 1339 1337 … … 1344 1342 * Callback called by ICE session when it wants to send outgoing packet. 1345 1343 */ 1346 static pj_status_t ice_tx_pkt(pj_ice_sess *ice, 1347 unsigned comp_id, 1344 static pj_status_t ice_tx_pkt(pj_ice_sess *ice, 1345 unsigned comp_id, 1348 1346 unsigned transport_id, 1349 1347 const void *pkt, pj_size_t size, … … 1362 1360 comp = ice_st->comp[comp_id-1]; 1363 1361 1364 TRACE_PKT((comp->ice_st->obj_name, 1362 TRACE_PKT((comp->ice_st->obj_name, 1365 1363 "Component %d TX packet to %s:%d with transport %d", 1366 comp_id, 1364 comp_id, 1367 1365 pj_sockaddr_print(dst_addr, daddr, sizeof(addr), 0), 1368 1366 pj_sockaddr_get_port(dst_addr), … … 1371 1369 if (transport_id == TP_TURN) { 1372 1370 if (comp->turn_sock) { 1373 status = pj_turn_sock_sendto(comp->turn_sock, 1374 (const pj_uint8_t*)pkt, 1371 status = pj_turn_sock_sendto(comp->turn_sock, 1372 (const pj_uint8_t*)pkt, 1375 1373 (unsigned)size, 1376 1374 dst_addr, dst_addr_len); … … 1379 1377 } 1380 1378 } else if (transport_id == TP_STUN) { 1381 status = pj_stun_sock_sendto(comp->stun_sock, NULL, 1379 status = pj_stun_sock_sendto(comp->stun_sock, NULL, 1382 1380 pkt, (unsigned)size, 0, 1383 1381 dst_addr, dst_addr_len); … … 1386 1384 status = PJ_EINVALIDOP; 1387 1385 } 1388 1386 1389 1387 return (status==PJ_SUCCESS||status==PJ_EPENDING) ? PJ_SUCCESS : status; 1390 1388 } … … 1393 1391 * Callback called by ICE session when it receives application data. 1394 1392 */ 1395 static void ice_rx_data(pj_ice_sess *ice, 1396 unsigned comp_id, 1393 static void ice_rx_data(pj_ice_sess *ice, 1394 unsigned comp_id, 1397 1395 unsigned transport_id, 1398 1396 void *pkt, pj_size_t size, … … 1405 1403 1406 1404 if (ice_st->cb.on_rx_data) { 1407 (*ice_st->cb.on_rx_data)(ice_st, comp_id, pkt, size, 1405 (*ice_st->cb.on_rx_data)(ice_st, comp_id, pkt, size, 1408 1406 src_addr, src_addr_len); 1409 1407 } … … 1411 1409 1412 1410 /* Notification when incoming packet has been received from 1413 * the STUN socket. 1411 * the STUN socket. 1414 1412 */ 1415 1413 static pj_bool_t stun_on_rx_data(pj_stun_sock *stun_sock, … … 1439 1437 */ 1440 1438 if (ice_st->cb.on_rx_data) { 1441 (*ice_st->cb.on_rx_data)(ice_st, comp->comp_id, pkt, pkt_len, 1439 (*ice_st->cb.on_rx_data)(ice_st, comp->comp_id, pkt, pkt_len, 1442 1440 src_addr, addr_len); 1443 1441 } … … 1446 1444 1447 1445 /* Hand over the packet to ICE session */ 1448 status = pj_ice_sess_on_rx_pkt(comp->ice_st->ice, comp->comp_id, 1446 status = pj_ice_sess_on_rx_pkt(comp->ice_st->ice, comp->comp_id, 1449 1447 TP_STUN, pkt, pkt_len, 1450 1448 src_addr, addr_len); 1451 1449 1452 1450 if (status != PJ_SUCCESS) { 1453 ice_st_perror(comp->ice_st, "Error processing packet", 1451 ice_st_perror(comp->ice_st, "Error processing packet", 1454 1452 status); 1455 1453 } … … 1460 1458 1461 1459 /* Notifification when asynchronous send operation to the STUN socket 1462 * has completed. 1460 * has completed. 1463 1461 */ 1464 1462 static pj_bool_t stun_on_data_sent(pj_stun_sock *stun_sock, … … 1473 1471 1474 1472 /* Notification when the status of the STUN transport has changed. */ 1475 static pj_bool_t stun_on_status(pj_stun_sock *stun_sock, 1473 static pj_bool_t stun_on_status(pj_stun_sock *stun_sock, 1476 1474 pj_stun_sock_op op, 1477 1475 pj_status_t status) … … 1573 1571 } 1574 1572 1575 PJ_LOG(4,(comp->ice_st->obj_name, 1573 PJ_LOG(4,(comp->ice_st->obj_name, 1576 1574 "Comp %d: %s, " 1577 1575 "srflx address is %s", 1578 comp->comp_id, op_name, 1579 pj_sockaddr_print(&info.mapped_addr, ipaddr, 1576 comp->comp_id, op_name, 1577 pj_sockaddr_print(&info.mapped_addr, ipaddr, 1580 1578 sizeof(ipaddr), 3))); 1581 1579 … … 1663 1661 1664 1662 if (status != PJ_SUCCESS) { 1665 ice_st_perror(comp->ice_st, 1666 "Error processing packet from TURN relay", 1663 ice_st_perror(comp->ice_st, 1664 "Error processing packet from TURN relay", 1667 1665 status); 1668 1666 } … … 1722 1720 pj_sockaddr_cp(&cand->base_addr, &rel_info.relay_addr); 1723 1721 pj_sockaddr_cp(&cand->rel_addr, &rel_info.mapped_addr); 1724 pj_ice_calc_foundation(comp->ice_st->pool, &cand->foundation, 1725 PJ_ICE_CAND_TYPE_RELAYED, 1722 pj_ice_calc_foundation(comp->ice_st->pool, &cand->foundation, 1723 PJ_ICE_CAND_TYPE_RELAYED, 1726 1724 &rel_info.relay_addr); 1727 1725 cand->status = PJ_SUCCESS; … … 1730 1728 comp->default_cand = (unsigned)(cand - comp->cand_list); 1731 1729 1732 PJ_LOG(4,(comp->ice_st->obj_name, 1730 PJ_LOG(4,(comp->ice_st->obj_name, 1733 1731 "Comp %d: TURN allocation complete, relay address is %s", 1734 comp->comp_id, 1735 pj_sockaddr_print(&rel_info.relay_addr, ipaddr, 1732 comp->comp_id, 1733 pj_sockaddr_print(&rel_info.relay_addr, ipaddr, 1736 1734 sizeof(ipaddr), 3))); 1737 1735
Note: See TracChangeset
for help on using the changeset viewer.