Changeset 1110 for pjproject/trunk/pjnath/include/pjnath/ice.h
- Timestamp:
- Mar 27, 2007 11:29:27 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/include/pjnath/ice.h
r1106 r1110 22 22 /** 23 23 * @file ice.h 24 * @brief ICE .24 * @brief ICE session management 25 25 */ 26 26 #include <pjnath/types.h> … … 32 32 * @defgroup PJNATH_ICE Interactive Connectivity Establishment (ICE) 33 33 * @brief Interactive Connectivity Establishment (ICE) 34 * @ingroup PJNATH35 34 */ 36 35 … … 40 39 41 40 /** 42 * @defgroup PJNATH_ICE_S TREAM Transport Independent ICE Media Stream43 * @brief Transport Independent ICE Media Stream41 * @defgroup PJNATH_ICE_SESSION ICE Session 42 * @brief Transport Independent ICE Session 44 43 * @ingroup PJNATH_ICE 45 44 * @{ 45 * 46 * This module describes #pj_ice, a transport independent ICE session, 47 * part of PJNATH - the Open Source NAT helper library. 48 * 49 * An ICE session, represented by #pj_ice structure, is the lowest 50 * abstraction of ICE in PJNATH, and it is used to perform and manage 51 * connectivity checks of transport address candidates <b>within a 52 * single media stream</b> (note: this differs from what is described 53 * in ICE draft, where an ICE session manages the whole media sessions 54 * rather than just a single stream). 55 * 56 * The ICE session described here is independent from any transports, 57 * meaning that the actual network I/O for this session would have to 58 * be performed by the application, or higher layer abstraction. 59 * Using this framework, application would give any incoming packets to 60 * the ICE session, and it would provide the ICE session with a callback 61 * to send outgoing message. 62 * 63 * For higher abstraction of ICE where transport is included, please 64 * see \ref PJNATH_ICE_STREAM_TRANSPORT. 46 65 */ 47 66 … … 51 70 typedef enum pj_ice_cand_type 52 71 { 53 PJ_ICE_CAND_TYPE_HOST, 54 PJ_ICE_CAND_TYPE_SRFLX, 55 PJ_ICE_CAND_TYPE_PRFLX, 56 PJ_ICE_CAND_TYPE_RELAYED 72 PJ_ICE_CAND_TYPE_HOST, /**< ICE host candidate. */ 73 PJ_ICE_CAND_TYPE_SRFLX, /**< ICE server reflexive candidate. */ 74 PJ_ICE_CAND_TYPE_PRFLX, /**< ICE peer reflexive candidate. */ 75 PJ_ICE_CAND_TYPE_RELAYED /**< ICE relayed candidate. */ 57 76 } pj_ice_cand_type; 58 77 59 /** 60 * 78 79 /** 80 * This enumeration describes the default preference for the ICE 81 * candidate types as described by ICE standard. 61 82 */ 62 83 enum pj_ice_type_pref 63 84 { 64 PJ_ICE_HOST_PREF = 126, 65 PJ_ICE_SRFLX_PREF = 100, 66 PJ_ICE_PRFLX_PREF = 110, 67 PJ_ICE_RELAYED_PREF = 0 85 PJ_ICE_HOST_PREF = 126, /**< Preference value for host. */ 86 PJ_ICE_SRFLX_PREF = 100, /**< Preference value for SRFLX. */ 87 PJ_ICE_PRFLX_PREF = 110, /**< Preference value for PRFLX */ 88 PJ_ICE_RELAYED_PREF = 0 /**< Preference value for relay */ 68 89 }; 69 90 91 /** Forward declaration for pj_ice */ 70 92 typedef struct pj_ice pj_ice; 93 94 /** Forward declaration for pj_ice_check */ 71 95 typedef struct pj_ice_check pj_ice_check; 72 96 73 #define PJ_ICE_MAX_CAND 16 74 #define PJ_ICE_MAX_COMP 8 75 #define PJ_ICE_MAX_CHECKS 32 76 #define PJ_ICE_TA_VAL 20 77 78 /** 79 * ICE component97 98 /** 99 * This structure describes ICE component. 100 * A media stream may require multiple components, each of which has 101 * to work for the media stream as a whole to work. For media streams 102 * based on RTP, there are two components per media stream - one for RTP, 103 * and one for RTCP. 80 104 */ 81 105 typedef struct pj_ice_comp 82 106 { 107 /** 108 * The pointer to ICE check which was nominated for this component. 109 * The value will be NULL if a nominated check has not been found 110 * for this component. 111 */ 83 112 pj_ice_check *valid_check; 113 84 114 } pj_ice_comp; 85 115 … … 87 117 /** 88 118 * This structure describes an ICE candidate. 119 * ICE candidate is a transport address that is to be tested by ICE 120 * procedures in order to determine its suitability for usage for 121 * receipt of media. Candidates also have properties - their type 122 * (server reflexive, relayed or host), priority, foundation, and 123 * base. 89 124 */ 90 125 typedef struct pj_ice_cand 91 126 { 127 /** 128 * The component ID of this candidate. Note that component IDs starts 129 * with one for RTP and two for RTCP. In other words, it's not zero 130 * based. 131 */ 92 132 pj_uint32_t comp_id; 133 134 /** 135 * The candidate type, as described in #pj_ice_cand_type enumeration. 136 */ 93 137 pj_ice_cand_type type; 138 139 /** 140 * The foundation string, which is an identifier which value will be 141 * equivalent for two candidates that are of the same type, share the 142 * same base, and come from the same STUN server. The foundation is 143 * used to optimize ICE performance in the Frozen algorithm. 144 */ 94 145 pj_str_t foundation; 146 147 /** 148 * The candidate's priority, a 32-bit unsigned value which value will be 149 * calculated by the ICE session when a candidate is registered to the 150 * ICE session. 151 */ 95 152 pj_uint32_t prio; 153 154 /** 155 * IP address of this candidate. For host candidates, this represents 156 * the local address of the socket. For reflexive candidates, the value 157 * will be the public address allocated in NAT router for the host 158 * candidate and as reported in MAPPED-ADDRESS or XOR-MAPPED-ADDRESS 159 * attribute of STUN Binding request. For relayed candidate, the value 160 * will be the address allocated in the TURN server by STUN Allocate 161 * request. 162 */ 96 163 pj_sockaddr addr; 164 165 /** 166 * Base address of this candidate. "Base" refers to the address an agent 167 * sends from for a particular candidate. For host candidates, the base 168 * is the same as the host candidate itself. For reflexive candidates, 169 * the base is the local IP address of the socket. For relayed candidates, 170 * the base address is the transport address allocated in the TURN server 171 * for this candidate. 172 */ 97 173 pj_sockaddr base_addr; 174 175 /** 176 * Related address, which is used for informational only and is not used 177 * in any way by the ICE session. 178 */ 98 179 pj_sockaddr rel_addr; 180 181 /** 182 * The STUN session to be used to send and receive STUN messages for this 183 * candidate. 184 */ 99 185 pj_stun_session *stun_sess; 186 100 187 } pj_ice_cand; 101 188 189 190 /** 191 * This enumeration describes the state of ICE check. 192 */ 102 193 typedef enum pj_ice_check_state 103 194 { 195 /** 196 * A check for this pair hasn't been performed, and it can't 197 * yet be performed until some other check succeeds, allowing this 198 * pair to unfreeze and move into the Waiting state. 199 */ 104 200 PJ_ICE_CHECK_STATE_FROZEN, 201 202 /** 203 * A check has not been performed for this pair, and can be 204 * performed as soon as it is the highest priority Waiting pair on 205 * the check list. 206 */ 105 207 PJ_ICE_CHECK_STATE_WAITING, 208 209 /** 210 * A check has not been performed for this pair, and can be 211 * performed as soon as it is the highest priority Waiting pair on 212 * the check list. 213 */ 106 214 PJ_ICE_CHECK_STATE_IN_PROGRESS, 215 216 /** 217 * A check has not been performed for this pair, and can be 218 * performed as soon as it is the highest priority Waiting pair on 219 * the check list. 220 */ 107 221 PJ_ICE_CHECK_STATE_SUCCEEDED, 222 223 /** 224 * A check for this pair was already done and failed, either 225 * never producing any response or producing an unrecoverable failure 226 * response. 227 */ 108 228 PJ_ICE_CHECK_STATE_FAILED 229 109 230 } pj_ice_check_state; 110 231 111 232 233 /** 234 * This structure describes an ICE connectivity check. An ICE check 235 * contains a candidate pair, and will involve sending STUN Binding 236 * Request transaction for the purposes of verifying connectivity. 237 * A check is sent from the local candidate to the remote candidate 238 * of a candidate pair. 239 */ 112 240 struct pj_ice_check 113 241 { 242 /** 243 * Pointer to local candidate entry of this check. 244 */ 114 245 pj_ice_cand *lcand; 246 247 /** 248 * Pointer to remote candidate entry of this check. 249 */ 115 250 pj_ice_cand *rcand; 116 251 252 /** 253 * Check priority. 254 */ 117 255 pj_uint64_t prio; 256 257 /** 258 * Connectivity check state. 259 */ 118 260 pj_ice_check_state state; 261 262 /** 263 * STUN transmit data containing STUN Binding request that was sent 264 * as part of this check. The value will only be set when this check 265 * has a pending transaction, and is used to cancel the transaction 266 * when other check has succeeded. 267 */ 119 268 pj_stun_tx_data *tdata; 269 270 /** 271 * Flag to indicate whether this check is nominated. A nominated check 272 * contains USE-CANDIDATE attribute in its STUN Binding request. 273 */ 120 274 pj_bool_t nominated; 275 276 /** 277 * When the check failed, this will contain the failure status of the 278 * STUN transaction. 279 */ 121 280 pj_status_t err_code; 122 281 }; 123 282 124 283 284 /** 285 * This enumeration describes ICE checklist state. 286 */ 125 287 typedef enum pj_ice_checklist_state 126 288 { 289 /** 290 * The checklist is not yet running. 291 */ 127 292 PJ_ICE_CHECKLIST_ST_IDLE, 293 294 /** 295 * In this state, ICE checks are still in progress for this 296 * media stream. 297 */ 128 298 PJ_ICE_CHECKLIST_ST_RUNNING, 299 300 /** 301 * In this state, ICE checks have completed for this media stream, 302 * either successfully or with failure. 303 */ 129 304 PJ_ICE_CHECKLIST_ST_COMPLETED 305 130 306 } pj_ice_checklist_state; 131 307 308 309 /** 310 * This structure represents ICE check list, that is an ordered set of 311 * candidate pairs that an agent will use to generate checks. 312 */ 132 313 typedef struct pj_ice_checklist 133 314 { 315 /** 316 * The checklist state. 317 */ 134 318 pj_ice_checklist_state state; 319 320 /** 321 * Number of candidate pairs (checks). 322 */ 135 323 unsigned count; 324 325 /** 326 * Array of candidate pairs (checks). 327 */ 136 328 pj_ice_check checks[PJ_ICE_MAX_CHECKS]; 329 330 /** 331 * A timer used to perform periodic check for this checklist. 332 */ 137 333 pj_timer_entry timer; 334 138 335 } pj_ice_checklist; 139 336 140 337 141 338 /** 142 * ICE sock callback. 339 * This structure contains callbacks that will be called by the ICE 340 * session. 143 341 */ 144 342 typedef struct pj_ice_cb 145 343 { 344 /** 345 * An optional callback that will be called by the ICE session when 346 * ICE negotiation has completed, successfully or with failure. 347 * 348 * @param ice The ICE session. 349 * @param status Will contain PJ_SUCCESS if ICE negotiation is 350 * successful, or some error code. 351 */ 146 352 void (*on_ice_complete)(pj_ice *ice, pj_status_t status); 353 354 /** 355 * A mandatory callback which will be called by the ICE session when 356 * it needs to send outgoing STUN packet. 357 * 358 * @param ice The ICE session. 359 * @param comp_id ICE component ID. 360 * @param cand_id ICE candidate ID. 361 * @param pkt The STUN packet. 362 * @param size The size of the packet. 363 * @param dst_addr Packet destination address. 364 * @param dst_addr_len Length of destination address. 365 */ 147 366 pj_status_t (*on_tx_pkt)(pj_ice *ice, unsigned comp_id, 148 367 unsigned cand_id, … … 150 369 const pj_sockaddr_t *dst_addr, 151 370 unsigned dst_addr_len); 371 372 /** 373 * A mandatory callback which will be called by the ICE session when 374 * it receives packet which is not part of ICE negotiation. 375 * 376 * @param ice The ICE session. 377 * @param comp_id ICE component ID. 378 * @param pkt The whole packet. 379 * @param size Size of the packet. 380 * @param src_addr Source address where this packet was received 381 * from. 382 * @param src_addr_len The length of source address. 383 */ 152 384 void (*on_rx_data)(pj_ice *ice, unsigned comp_id, 153 385 void *pkt, pj_size_t size, … … 157 389 158 390 391 /** 392 * This enumeration describes ICE role. 393 */ 159 394 typedef enum pj_ice_role 160 395 { 396 /** 397 * The ICE agent is in controlled role. 398 */ 161 399 PJ_ICE_ROLE_CONTROLLED, 400 401 /** 402 * The ICE agent is in controlling role. 403 */ 162 404 PJ_ICE_ROLE_CONTROLLING 405 163 406 } pj_ice_role; 164 407 165 /** 166 * ICE structure. 408 409 /** 410 * This structure describes the ICE session. For this version of PJNATH, 411 * an ICE session corresponds to a single media stream (unlike the ICE 412 * session described in the ICE standard where an ICE session covers the 413 * whole media and may consist of multiple media streams). The decision 414 * to support only a single media session was chosen for simplicity, 415 * while still allowing application to utilize multiple media streams by 416 * creating multiple ICE sessions, one for each media stream. 167 417 */ 168 418 struct pj_ice 169 419 { 170 char obj_name[PJ_MAX_OBJ_NAME]; 171 172 pj_pool_t *pool; 173 void *user_data; 174 pj_mutex_t *mutex; 175 pj_ice_role role; 176 pj_bool_t is_complete; 177 pj_status_t ice_status; 178 pj_ice_cb cb; 179 180 pj_stun_config stun_cfg; 420 char obj_name[PJ_MAX_OBJ_NAME]; /**< Object name. */ 421 422 pj_pool_t *pool; /**< Pool instance. */ 423 void *user_data; /**< App. data. */ 424 pj_mutex_t *mutex; /**< Mutex. */ 425 pj_ice_role role; /**< ICE role. */ 426 pj_bool_t is_complete; /**< Complete? */ 427 pj_status_t ice_status; /**< Error status. */ 428 pj_ice_cb cb; /**< Callback. */ 429 430 pj_stun_config stun_cfg; /**< STUN settings. */ 181 431 182 432 /* STUN credentials */ 183 pj_str_t tx_ufrag; 184 pj_str_t tx_uname; 185 pj_str_t tx_pass; 186 pj_str_t rx_ufrag; 187 pj_str_t rx_uname; 188 pj_str_t rx_pass; 433 pj_str_t tx_ufrag; /**< Remote ufrag. */ 434 pj_str_t tx_uname; /**< Uname for TX. */ 435 pj_str_t tx_pass; /**< Remote password. */ 436 pj_str_t rx_ufrag; /**< Local ufrag. */ 437 pj_str_t rx_uname; /**< Uname for RX */ 438 pj_str_t rx_pass; /**< Local password. */ 189 439 190 440 /* Components */ 191 unsigned comp_cnt; 192 pj_ice_comp comp[PJ_ICE_MAX_COMP]; 441 unsigned comp_cnt; /**< # of components. */ 442 pj_ice_comp comp[PJ_ICE_MAX_COMP]; /**< Component array */ 193 443 194 444 /* Local candidates */ 195 unsigned lcand_cnt; 196 pj_ice_cand lcand[PJ_ICE_MAX_CAND]; 445 unsigned lcand_cnt; /**< # of local cand. */ 446 pj_ice_cand lcand[PJ_ICE_MAX_CAND]; /**< Array of cand. */ 197 447 198 448 /* Remote candidates */ 199 unsigned rcand_cnt; 200 pj_ice_cand rcand[PJ_ICE_MAX_CAND]; 449 unsigned rcand_cnt; /**< # of remote cand. */ 450 pj_ice_cand rcand[PJ_ICE_MAX_CAND]; /**< Array of cand. */ 201 451 202 452 /* Checklist */ 203 pj_ice_checklist clist; 453 pj_ice_checklist clist; /**< Active checklist */ 204 454 205 455 /* Valid list */ 206 pj_ice_checklist valid_list; 456 pj_ice_checklist valid_list; /**< Valid list. */ 207 457 }; 208 458 459 460 /** 461 * This is a utility function to retrieve the string name for the 462 * particular candidate type. 463 * 464 * @param type Candidate type. 465 * 466 * @return The string representation of the candidate type. 467 */ 209 468 PJ_DECL(const char*) pj_ice_get_cand_type_name(pj_ice_cand_type type); 210 469 211 470 471 /** 472 * Create ICE session with the specified role and number of components. 473 * Application would typically need to create an ICE session before 474 * sending an offer or upon receiving one. After the session is created, 475 * application can register candidates to the ICE session by calling 476 * #pj_ice_add_cand() function. 477 * 478 * @param stun_cfg The STUN configuration settings, containing among 479 * other things the timer heap instance to be used 480 * by the ICE session. 481 * @param name Optional name to identify this ICE instance in 482 * the log file. 483 * @param role ICE role. 484 * @param comp_cnt Number of components. 485 * @param cb ICE callback. 486 * @param local_ufrag Optional string to be used as local username to 487 * authenticate incoming STUN binding request. If 488 * the value is NULL, a random string will be 489 * generated. 490 * @param local_passwd Optional string to be used as local password. 491 * @param p_ice Pointer to receive the ICE session instance. 492 * 493 * @return PJ_SUCCESS if ICE session is created successfully. 494 */ 212 495 PJ_DECL(pj_status_t) pj_ice_create(pj_stun_config *stun_cfg, 213 496 const char *name, … … 218 501 const pj_str_t *local_passwd, 219 502 pj_ice **p_ice); 503 504 /** 505 * Destroy ICE session. 506 * 507 * @param ice ICE session instance. 508 * 509 * @return PJ_SUCCESS on success. 510 */ 220 511 PJ_DECL(pj_status_t) pj_ice_destroy(pj_ice *ice); 512 513 514 /** 515 * Add a candidate to this ICE session. 516 * 517 * @param ice ICE session instance. 518 * @param comp_id Component ID of this candidate. 519 * @param type Candidate type. 520 * @param local_pref Local preference for this candidate, which 521 * normally should be set to 65535. 522 * @param foundation Foundation identification. 523 * @param addr The candidate address. 524 * @param base_addr The candidate's base address. 525 * @param rel_addr Optional related address. 526 * @param addr_len Length of addresses. 527 * @param p_cand_id Optional pointer to receive the candidate ID. 528 * 529 * @return PJ_SUCCESS if candidate is successfully added. 530 */ 221 531 PJ_DECL(pj_status_t) pj_ice_add_cand(pj_ice *ice, 222 532 unsigned comp_id, … … 228 538 const pj_sockaddr_t *rel_addr, 229 539 int addr_len, 230 unsigned *cand_id); 231 540 unsigned *p_cand_id); 541 542 /** 543 * Find default candidate for the specified component ID, using this 544 * rule: 545 * - if the component has a successful candidate pair, then the 546 * local candidate of this pair will be returned. 547 * - otherwise a relay, reflexive, or host candidate will be selected 548 * on that specified order. 549 * 550 * @param ice The ICE session instance. 551 * @param comp_id The component ID. 552 * @param p_cand_id Pointer to receive the candidate ID. 553 * 554 * @return PJ_SUCCESS if a candidate has been selected. 555 */ 232 556 PJ_DECL(pj_status_t) pj_ice_find_default_cand(pj_ice *ice, 233 557 unsigned comp_id, 234 int *cand_id); 235 558 int *p_cand_id); 559 560 /** 561 * Pair the local and remote candidates to create check list. Application 562 * typically would call this function after receiving SDP containing ICE 563 * candidates from the remote host (either upon receiving the initial 564 * offer, for UAS, or upon receiving the answer, for UAC). 565 * 566 * Note that ICE connectivity check will not start until application calls 567 * #pj_ice_start_check(). 568 * 569 * @param ice ICE session instance. 570 * @param rem_ufrag Remote ufrag, as seen in the SDP received from 571 * the remote agent. 572 * @param rem_passwd Remote password, as seen in the SDP received from 573 * the remote agent. 574 * @param rem_cand_cnt Number of remote candidates. 575 * @param rem_cand Remote candidate array. Remote candidates are 576 * gathered from the SDP received from the remote 577 * agent. 578 * 579 * @return PJ_SUCCESS or the appropriate error code. 580 */ 236 581 PJ_DECL(pj_status_t) pj_ice_create_check_list(pj_ice *ice, 237 582 const pj_str_t *rem_ufrag, … … 239 584 unsigned rem_cand_cnt, 240 585 const pj_ice_cand rem_cand[]); 586 587 /** 588 * Start ICE periodic check. This function will return immediately, and 589 * application will be notified about the connectivity check status in 590 * #pj_ice_cb callback. 591 * 592 * @param ice The ICE session instance. 593 * 594 * @return PJ_SUCCESS or the appropriate error code. 595 */ 241 596 PJ_DECL(pj_status_t) pj_ice_start_check(pj_ice *ice); 242 597 598 599 /** 600 * Send data using this ICE session. If ICE checks have not produced a 601 * valid check for the specified component ID, this function will return 602 * with failure. Otherwise ICE session will send the packet to remote 603 * destination using the nominated local candidate for the specified 604 * component. 605 * 606 * @param ice The ICE session. 607 * @param comp_id Component ID. 608 * @param data The data or packet to be sent. 609 * @param data_len Size of data or packet, in bytes. 610 * 611 * @return PJ_SUCCESS if data is sent successfully. 612 */ 243 613 PJ_DECL(pj_status_t) pj_ice_send_data(pj_ice *ice, 244 614 unsigned comp_id, 245 615 const void *data, 246 616 pj_size_t data_len); 617 618 /** 619 * Report the arrival of packet to the ICE session. Since ICE session 620 * itself doesn't have any transports, it relies on application or 621 * higher layer component to give incoming packets to the ICE session. 622 * If the packet is not a STUN packet, this packet will be given back 623 * to application via \a on_rx_data() callback in #pj_ice_cb. 624 * 625 * @param ice The ICE session. 626 * @param comp_id Component ID. 627 * @param cand_id Candidate ID. 628 * @param pkt Incoming packet. 629 * @param pkt_size Size of incoming packet. 630 * @param src_addr Source address of the packet. 631 * @param src_addr_len Length of the address. 632 * 633 * @return PJ_SUCCESS or the appropriate error code. 634 */ 247 635 PJ_DECL(pj_status_t) pj_ice_on_rx_pkt(pj_ice *ice, 248 636 unsigned comp_id,
Note: See TracChangeset
for help on using the changeset viewer.