blob: d850301af3745bb0cc3fd1f6b98adf2934e35bda [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
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
19 */
20#ifndef __PJNATH_ICE_SESSION_H__
21#define __PJNATH_ICE_SESSION_H__
22
23/**
24 * @file ice_session.h
25 * @brief ICE session management
26 */
27#include <pjnath/types.h>
28#include <pjnath/stun_session.h>
29#include <pjnath/errno.h>
30#include <pj/sock.h>
31#include <pj/timer.h>
32
33PJ_BEGIN_DECL
34
35
36/**
37 * @addtogroup PJNATH_ICE_SESSION
38 * @{
39 *
40 * This module describes #pj_ice_sess, a transport independent ICE session,
41 * part of PJNATH - the Open Source NAT helper library.
42 *
43 * \section pj_ice_sess_sec ICE Session
44 *
45 * An ICE session, represented by #pj_ice_sess structure, is the lowest
46 * abstraction of ICE in PJNATH, and it is used to perform and manage
47 * connectivity checks of transport address candidates <b>within a
48 * single media stream</b> (note: this differs from what is described
49 * in ICE draft, where an ICE session manages the whole media sessions
50 * rather than just a single stream).
51 *
52 * The ICE session described here is independent from any transports,
53 * meaning that the actual network I/O for this session would have to
54 * be performed by the application, or higher layer abstraction.
55 * Using this framework, application would give any incoming packets to
56 * the ICE session, and it would provide the ICE session with a callback
57 * to send outgoing message.
58 *
59 * For higher abstraction of ICE where transport is included, please
60 * see \ref PJNATH_ICE_STREAM_TRANSPORT.
61 *
62 * \subsection pj_ice_sess_using_sec Using The ICE Session
63 *
64 * The steps below describe how to use ICE session. Alternatively application
65 * can use the higher level ICE API, \ref PJNATH_ICE_STREAM_TRANSPORT,
66 * which has provided the integration of ICE with socket transport.
67 *
68 * The steps to use ICE session is similar for both offerer and
69 * answerer:
70 * - create ICE session with #pj_ice_sess_create(). Among other things,
71 * application needs to specify:
72 * - STUN configuration (pj_stun_config), containing STUN settings
73 * such as timeout values and the instances of timer heap and
74 * ioqueue.
75 * - Session name, useful for identifying this session in the log.
76 * - Initial ICE role (#pj_ice_sess_role). The role can be changed
77 * at later time with #pj_ice_sess_change_role(), and ICE session
78 * can also change its role automatically when it detects role
79 * conflict.
80 * - Number of components in the media session.
81 * - Callback to receive ICE events (#pj_ice_sess_cb)
82 * - Optional local ICE username and password. If these arguments
83 * are NULL, they will be generated randomly.
84 * - Add local candidates for each component, with #pj_ice_sess_add_cand().
85 * A candidate is represented with #pj_ice_sess_cand structure.
86 * Each component must be provided with at least one candidate, and
87 * all components must have the same number of candidates. Failing
88 * to comply with this will cause failure during pairing process.
89 * - Create offer to describe local ICE candidates. ICE session does not
90 * provide a function to create such offer, but application should be
91 * able to create one since it knows about all components and candidates.
92 * If application uses \ref PJNATH_ICE_STREAM_TRANSPORT, it can
93 * enumerate local candidates by calling #pj_ice_strans_enum_cands().
94 * Application may use #pj_ice_sess_find_default_cand() to let ICE
95 * session chooses the default transport address to be used in SDP
96 * c= and m= lines.
97 * - Send the offer to remote endpoint using signaling such as SIP.
98 * - Once application has received the answer, it should parse this
99 * answer, build array of remote candidates, and create check lists by
100 * calling #pj_ice_sess_create_check_list(). This process is known as
101 * pairing the candidates, and will result in the creation of check lists.
102 * - Once checklist has been created, application then can call
103 * #pj_ice_sess_start_check() to instruct ICE session to start
104 * performing connectivity checks. The ICE session performs the
105 * connectivity checks by processing each check in the checklists.
106 * - Application will be notified about the result of ICE connectivity
107 * checks via the callback that was given in #pj_ice_sess_create()
108 * above.
109 *
110 * To send data, application calls #pj_ice_sess_send_data(). If ICE
111 * negotiation has not completed, ICE session would simply drop the data,
112 * and return error to caller. If ICE negotiation has completed
113 * successfully, ICE session will in turn call the \a on_tx_pkt
114 * callback of #pj_ice_sess_cb instance that was previously registered
115 * in #pj_ice_sess_create() above.
116 *
117 * When application receives any packets on the underlying sockets, it
118 * must call #pj_ice_sess_on_rx_pkt(). The ICE session will inspect the
119 * packet to decide whether to process it locally (if the packet is a
120 * STUN message and is part of ICE session) or otherwise pass it back to
121 * application via \a on_rx_data callback.
122 */
123
124/**
125 * Forward declaration for checklist.
126 */
127typedef struct pj_ice_sess_checklist pj_ice_sess_checklist;
128
129/**
130 * This enumeration describes the type of an ICE candidate.
131 */
132typedef enum pj_ice_cand_type
133{
134 /**
135 * ICE host candidate. A host candidate represents the actual local
136 * transport address in the host.
137 */
138 PJ_ICE_CAND_TYPE_HOST,
139
140 /**
141 * ICE server reflexive candidate, which represents the public mapped
142 * address of the local address, and is obtained by sending STUN
143 * Binding request from the host candidate to a STUN server.
144 */
145 PJ_ICE_CAND_TYPE_SRFLX,
146
147 /**
148 * ICE peer reflexive candidate, which is the address as seen by peer
149 * agent during connectivity check.
150 */
151 PJ_ICE_CAND_TYPE_PRFLX,
152
153 /**
154 * ICE relayed candidate, which represents the address allocated in
155 * TURN server.
156 */
157 PJ_ICE_CAND_TYPE_RELAYED,
158
159 /**
160 * Number of defined ICE candidate types.
161 */
162 PJ_ICE_CAND_TYPE_MAX
163
164} pj_ice_cand_type;
165
166
167/** Forward declaration for pj_ice_sess */
168typedef struct pj_ice_sess pj_ice_sess;
169
170/** Forward declaration for pj_ice_sess_check */
171typedef struct pj_ice_sess_check pj_ice_sess_check;
172
173
174/**
175 * This structure describes ICE component.
176 * A media stream may require multiple components, each of which has
177 * to work for the media stream as a whole to work. For media streams
178 * based on RTP, there are two components per media stream - one for RTP,
179 * and one for RTCP.
180 */
181typedef struct pj_ice_sess_comp
182{
183 /**
184 * Pointer to ICE check with highest priority which connectivity check
185 * has been successful. The value will be NULL if a no successful check
186 * has not been found for this component.
187 */
188 pj_ice_sess_check *valid_check;
189
190 /**
191 * Pointer to ICE check with highest priority which connectivity check
192 * has been successful and it has been nominated. The value may be NULL
193 * if there is no such check yet.
194 */
195 pj_ice_sess_check *nominated_check;
196
197 /**
198 * The STUN session to be used to send and receive STUN messages for this
199 * component.
200 */
201 pj_stun_session *stun_sess;
202
203} pj_ice_sess_comp;
204
205
206/**
207 * Data structure to be attached to internal message processing.
208 */
209typedef struct pj_ice_msg_data
210{
211 /** Transport ID for this message */
212 unsigned transport_id;
213
214 /** Flag to indicate whether data.req contains data */
215 pj_bool_t has_req_data;
216
217 /** The data */
218 union data {
219 /** Request data */
220 struct request_data {
221 pj_ice_sess *ice; /**< ICE session */
222 pj_ice_sess_checklist *clist; /**< Checklist */
223 unsigned ckid; /**< Check ID */
224 } req;
225 } data;
226
227} pj_ice_msg_data;
228
229
230/**
231 * This structure describes an ICE candidate.
232 * ICE candidate is a transport address that is to be tested by ICE
233 * procedures in order to determine its suitability for usage for
234 * receipt of media. Candidates also have properties - their type
235 * (server reflexive, relayed or host), priority, foundation, and
236 * base.
237 */
238typedef struct pj_ice_sess_cand
239{
240 /**
241 * The candidate type, as described in #pj_ice_cand_type enumeration.
242 */
243 pj_ice_cand_type type;
244
245 /**
246 * Status of this candidate. The value will be PJ_SUCCESS if candidate
247 * address has been resolved successfully, PJ_EPENDING when the address
248 * resolution process is in progress, or other value when the address
249 * resolution has completed with failure.
250 */
251 pj_status_t status;
252
253 /**
254 * The component ID of this candidate. Note that component IDs starts
255 * with one for RTP and two for RTCP. In other words, it's not zero
256 * based.
257 */
258 pj_uint8_t comp_id;
259
260 /**
261 * Transport ID to be used to send packets for this candidate.
262 */
263 pj_uint8_t transport_id;
264
265 /**
266 * Local preference value, which typically is 65535.
267 */
268 pj_uint16_t local_pref;
269
270 /**
271 * The foundation string, which is an identifier which value will be
272 * equivalent for two candidates that are of the same type, share the
273 * same base, and come from the same STUN server. The foundation is
274 * used to optimize ICE performance in the Frozen algorithm.
275 */
276 pj_str_t foundation;
277
278 /**
279 * The candidate's priority, a 32-bit unsigned value which value will be
280 * calculated by the ICE session when a candidate is registered to the
281 * ICE session.
282 */
283 pj_uint32_t prio;
284
285 /**
286 * IP address of this candidate. For host candidates, this represents
287 * the local address of the socket. For reflexive candidates, the value
288 * will be the public address allocated in NAT router for the host
289 * candidate and as reported in MAPPED-ADDRESS or XOR-MAPPED-ADDRESS
290 * attribute of STUN Binding request. For relayed candidate, the value
291 * will be the address allocated in the TURN server by STUN Allocate
292 * request.
293 */
294 pj_sockaddr addr;
295
296 /**
297 * Base address of this candidate. "Base" refers to the address an agent
298 * sends from for a particular candidate. For host candidates, the base
299 * is the same as the host candidate itself. For reflexive candidates,
300 * the base is the local IP address of the socket. For relayed candidates,
301 * the base address is the transport address allocated in the TURN server
302 * for this candidate.
303 */
304 pj_sockaddr base_addr;
305
306 /**
307 * Related address, which is used for informational only and is not used
308 * in any way by the ICE session.
309 */
310 pj_sockaddr rel_addr;
311
312} pj_ice_sess_cand;
313
314
315/**
316 * This enumeration describes the state of ICE check.
317 */
318typedef enum pj_ice_sess_check_state
319{
320 /**
321 * A check for this pair hasn't been performed, and it can't
322 * yet be performed until some other check succeeds, allowing this
323 * pair to unfreeze and move into the Waiting state.
324 */
325 PJ_ICE_SESS_CHECK_STATE_FROZEN,
326
327 /**
328 * A check has not been performed for this pair, and can be
329 * performed as soon as it is the highest priority Waiting pair on
330 * the check list.
331 */
332 PJ_ICE_SESS_CHECK_STATE_WAITING,
333
334 /**
335 * A check has not been performed for this pair, and can be
336 * performed as soon as it is the highest priority Waiting pair on
337 * the check list.
338 */
339 PJ_ICE_SESS_CHECK_STATE_IN_PROGRESS,
340
341 /**
342 * A check has not been performed for this pair, and can be
343 * performed as soon as it is the highest priority Waiting pair on
344 * the check list.
345 */
346 PJ_ICE_SESS_CHECK_STATE_SUCCEEDED,
347
348 /**
349 * A check for this pair was already done and failed, either
350 * never producing any response or producing an unrecoverable failure
351 * response.
352 */
353 PJ_ICE_SESS_CHECK_STATE_FAILED
354
355} pj_ice_sess_check_state;
356
357
358/**
359 * This structure describes an ICE connectivity check. An ICE check
360 * contains a candidate pair, and will involve sending STUN Binding
361 * Request transaction for the purposes of verifying connectivity.
362 * A check is sent from the local candidate to the remote candidate
363 * of a candidate pair.
364 */
365struct pj_ice_sess_check
366{
367 /**
368 * Pointer to local candidate entry of this check.
369 */
370 pj_ice_sess_cand *lcand;
371
372 /**
373 * Pointer to remote candidate entry of this check.
374 */
375 pj_ice_sess_cand *rcand;
376
377 /**
378 * Check priority.
379 */
380 pj_timestamp prio;
381
382 /**
383 * Connectivity check state.
384 */
385 pj_ice_sess_check_state state;
386
387 /**
388 * STUN transmit data containing STUN Binding request that was sent
389 * as part of this check. The value will only be set when this check
390 * has a pending transaction, and is used to cancel the transaction
391 * when other check has succeeded.
392 */
393 pj_stun_tx_data *tdata;
394
395 /**
396 * Flag to indicate whether this check is nominated. A nominated check
397 * contains USE-CANDIDATE attribute in its STUN Binding request.
398 */
399 pj_bool_t nominated;
400
401 /**
402 * When the check failed, this will contain the failure status of the
403 * STUN transaction.
404 */
405 pj_status_t err_code;
406};
407
408
409/**
410 * This enumeration describes ICE checklist state.
411 */
412typedef enum pj_ice_sess_checklist_state
413{
414 /**
415 * The checklist is not yet running.
416 */
417 PJ_ICE_SESS_CHECKLIST_ST_IDLE,
418
419 /**
420 * In this state, ICE checks are still in progress for this
421 * media stream.
422 */
423 PJ_ICE_SESS_CHECKLIST_ST_RUNNING,
424
425 /**
426 * In this state, ICE checks have completed for this media stream,
427 * either successfully or with failure.
428 */
429 PJ_ICE_SESS_CHECKLIST_ST_COMPLETED
430
431} pj_ice_sess_checklist_state;
432
433
434/**
435 * This structure represents ICE check list, that is an ordered set of
436 * candidate pairs that an agent will use to generate checks.
437 */
438struct pj_ice_sess_checklist
439{
440 /**
441 * The checklist state.
442 */
443 pj_ice_sess_checklist_state state;
444
445 /**
446 * Number of candidate pairs (checks).
447 */
448 unsigned count;
449
450 /**
451 * Array of candidate pairs (checks).
452 */
453 pj_ice_sess_check checks[PJ_ICE_MAX_CHECKS];
454
455 /**
456 * A timer used to perform periodic check for this checklist.
457 */
458 pj_timer_entry timer;
459
460};
461
462
463/**
464 * This structure contains callbacks that will be called by the ICE
465 * session.
466 */
467typedef struct pj_ice_sess_cb
468{
469 /**
470 * An optional callback that will be called by the ICE session when
471 * ICE negotiation has completed, successfully or with failure.
472 *
473 * @param ice The ICE session.
474 * @param status Will contain PJ_SUCCESS if ICE negotiation is
475 * successful, or some error code.
476 */
477 void (*on_ice_complete)(pj_ice_sess *ice, pj_status_t status);
478
479 /**
480 * A mandatory callback which will be called by the ICE session when
481 * it needs to send outgoing STUN packet.
482 *
483 * @param ice The ICE session.
484 * @param comp_id ICE component ID.
485 * @param transport_id Transport ID.
486 * @param pkt The STUN packet.
487 * @param size The size of the packet.
488 * @param dst_addr Packet destination address.
489 * @param dst_addr_len Length of destination address.
490 */
491 pj_status_t (*on_tx_pkt)(pj_ice_sess *ice, unsigned comp_id,
492 unsigned transport_id,
493 const void *pkt, pj_size_t size,
494 const pj_sockaddr_t *dst_addr,
495 unsigned dst_addr_len);
496
497 /**
498 * A mandatory callback which will be called by the ICE session when
499 * it receives packet which is not part of ICE negotiation.
500 *
501 * @param ice The ICE session.
502 * @param comp_id ICE component ID.
503 * @param transport_id Transport ID.
504 * @param pkt The whole packet.
505 * @param size Size of the packet.
506 * @param src_addr Source address where this packet was received
507 * from.
508 * @param src_addr_len The length of source address.
509 */
510 void (*on_rx_data)(pj_ice_sess *ice, unsigned comp_id,
511 unsigned transport_id,
512 void *pkt, pj_size_t size,
513 const pj_sockaddr_t *src_addr,
514 unsigned src_addr_len);
515} pj_ice_sess_cb;
516
517
518/**
519 * This enumeration describes the role of the ICE agent.
520 */
521typedef enum pj_ice_sess_role
522{
523 /**
524 * The role is unknown.
525 */
526 PJ_ICE_SESS_ROLE_UNKNOWN,
527
528 /**
529 * The ICE agent is in controlled role.
530 */
531 PJ_ICE_SESS_ROLE_CONTROLLED,
532
533 /**
534 * The ICE agent is in controlling role.
535 */
536 PJ_ICE_SESS_ROLE_CONTROLLING
537
538} pj_ice_sess_role;
539
540
541/**
542 * This structure represents an incoming check (an incoming Binding
543 * request message), and is mainly used to keep early checks in the
544 * list in the ICE session. An early check is a request received
545 * from remote when we haven't received SDP answer yet, therefore we
546 * can't perform triggered check. For such cases, keep the incoming
547 * request in a list, and we'll do triggered checks (simultaneously)
548 * as soon as we receive answer.
549 */
550typedef struct pj_ice_rx_check
551{
552 PJ_DECL_LIST_MEMBER(struct pj_ice_rx_check); /**< Standard list */
553
554 unsigned comp_id; /**< Component ID. */
555 unsigned transport_id; /**< Transport ID. */
556
557 pj_sockaddr src_addr; /**< Source address of request */
558 unsigned src_addr_len; /**< Length of src address. */
559
560 pj_bool_t use_candidate; /**< USE-CANDIDATE is present? */
561 pj_uint32_t priority; /**< PRIORITY value in the req. */
562 pj_stun_uint64_attr *role_attr; /**< ICE-CONTROLLING/CONTROLLED */
563
564} pj_ice_rx_check;
565
566
567/**
568 * This structure describes various ICE session options. Application
569 * configure the ICE session with these options by calling
570 * #pj_ice_sess_set_options().
571 */
572typedef struct pj_ice_sess_options
573{
574 /**
575 * Specify whether to use aggressive nomination.
576 */
577 pj_bool_t aggressive;
578
579 /**
580 * For controlling agent if it uses regular nomination, specify the delay
581 * to perform nominated check (connectivity check with USE-CANDIDATE
582 * attribute) after all components have a valid pair.
583 *
584 * Default value is PJ_ICE_NOMINATED_CHECK_DELAY.
585 */
586 unsigned nominated_check_delay;
587
588 /**
589 * For a controlled agent, specify how long it wants to wait (in
590 * milliseconds) for the controlling agent to complete sending
591 * connectivity check with nominated flag set to true for all components
592 * after the controlled agent has found that all connectivity checks in
593 * its checklist have been completed and there is at least one successful
594 * (but not nominated) check for every component.
595 *
596 * Default value for this option is
597 * ICE_CONTROLLED_AGENT_WAIT_NOMINATION_TIMEOUT. Specify -1 to disable
598 * this timer.
599 */
600 int controlled_agent_want_nom_timeout;
601
602} pj_ice_sess_options;
603
604
605/**
606 * This structure describes the ICE session. For this version of PJNATH,
607 * an ICE session corresponds to a single media stream (unlike the ICE
608 * session described in the ICE standard where an ICE session covers the
609 * whole media and may consist of multiple media streams). The decision
610 * to support only a single media session was chosen for simplicity,
611 * while still allowing application to utilize multiple media streams by
612 * creating multiple ICE sessions, one for each media stream.
613 */
614struct pj_ice_sess
615{
616 char obj_name[PJ_MAX_OBJ_NAME]; /**< Object name. */
617
618 pj_pool_t *pool; /**< Pool instance. */
619 void *user_data; /**< App. data. */
620 pj_grp_lock_t *grp_lock; /**< Group lock */
621 pj_ice_sess_role role; /**< ICE role. */
622 pj_ice_sess_options opt; /**< Options */
623 pj_timestamp tie_breaker; /**< Tie breaker value */
624 pj_uint8_t *prefs; /**< Type preference. */
625 pj_bool_t is_nominating; /**< Nominating stage */
626 pj_bool_t is_complete; /**< Complete? */
627 pj_bool_t is_destroying; /**< Destroy is called */
628 pj_status_t ice_status; /**< Error status. */
629 pj_timer_entry timer; /**< ICE timer. */
630 pj_ice_sess_cb cb; /**< Callback. */
631
632 pj_stun_config stun_cfg; /**< STUN settings. */
633
634 /* STUN credentials */
635 pj_str_t tx_ufrag; /**< Remote ufrag. */
636 pj_str_t tx_uname; /**< Uname for TX. */
637 pj_str_t tx_pass; /**< Remote password. */
638 pj_str_t rx_ufrag; /**< Local ufrag. */
639 pj_str_t rx_uname; /**< Uname for RX */
640 pj_str_t rx_pass; /**< Local password. */
641
642 /* Components */
643 unsigned comp_cnt; /**< # of components. */
644 pj_ice_sess_comp comp[PJ_ICE_MAX_COMP]; /**< Component array */
645 unsigned comp_ka; /**< Next comp for KA */
646
647 /* Local candidates */
648 unsigned lcand_cnt; /**< # of local cand. */
649 pj_ice_sess_cand lcand[PJ_ICE_MAX_CAND]; /**< Array of cand. */
650
651 /* Remote candidates */
652 unsigned rcand_cnt; /**< # of remote cand. */
653 pj_ice_sess_cand rcand[PJ_ICE_MAX_CAND]; /**< Array of cand. */
654
655 /** Array of transport datas */
656 pj_ice_msg_data tp_data[4];
657
658 /* List of eearly checks */
659 pj_ice_rx_check early_check; /**< Early checks. */
660
661 /* Checklist */
662 pj_ice_sess_checklist clist; /**< Active checklist */
663
664 /* Valid list */
665 pj_ice_sess_checklist valid_list; /**< Valid list. */
666
667 /** Temporary buffer for misc stuffs to avoid using stack too much */
668 union {
669 char txt[128];
670 char errmsg[PJ_ERR_MSG_SIZE];
671 } tmp;
672};
673
674
675/**
676 * This is a utility function to retrieve the string name for the
677 * particular candidate type.
678 *
679 * @param type Candidate type.
680 *
681 * @return The string representation of the candidate type.
682 */
683PJ_DECL(const char*) pj_ice_get_cand_type_name(pj_ice_cand_type type);
684
685
686/**
687 * This is a utility function to retrieve the string name for the
688 * particular role type.
689 *
690 * @param role Role type.
691 *
692 * @return The string representation of the role.
693 */
694PJ_DECL(const char*) pj_ice_sess_role_name(pj_ice_sess_role role);
695
696
697/**
698 * This is a utility function to calculate the foundation identification
699 * for a candidate.
700 *
701 * @param pool Pool to allocate the foundation string.
702 * @param foundation Pointer to receive the foundation string.
703 * @param type Candidate type.
704 * @param base_addr Base address of the candidate.
705 */
706PJ_DECL(void) pj_ice_calc_foundation(pj_pool_t *pool,
707 pj_str_t *foundation,
708 pj_ice_cand_type type,
709 const pj_sockaddr *base_addr);
710
711/**
712 * Initialize ICE session options with library default values.
713 *
714 * @param opt ICE session options.
715 */
716PJ_DECL(void) pj_ice_sess_options_default(pj_ice_sess_options *opt);
717
718/**
719 * Create ICE session with the specified role and number of components.
720 * Application would typically need to create an ICE session before
721 * sending an offer or upon receiving one. After the session is created,
722 * application can register candidates to the ICE session by calling
723 * #pj_ice_sess_add_cand() function.
724 *
725 * @param stun_cfg The STUN configuration settings, containing among
726 * other things the timer heap instance to be used
727 * by the ICE session.
728 * @param name Optional name to identify this ICE instance in
729 * the log file.
730 * @param role ICE role.
731 * @param comp_cnt Number of components.
732 * @param cb ICE callback.
733 * @param local_ufrag Optional string to be used as local username to
734 * authenticate incoming STUN binding request. If
735 * the value is NULL, a random string will be
736 * generated.
737 * @param local_passwd Optional string to be used as local password.
738 * @param grp_lock Optional group lock to be used by this session.
739 * If NULL, the session will create one itself.
740 * @param p_ice Pointer to receive the ICE session instance.
741 *
742 * @return PJ_SUCCESS if ICE session is created successfully.
743 */
744PJ_DECL(pj_status_t) pj_ice_sess_create(pj_stun_config *stun_cfg,
745 const char *name,
746 pj_ice_sess_role role,
747 unsigned comp_cnt,
748 const pj_ice_sess_cb *cb,
749 const pj_str_t *local_ufrag,
750 const pj_str_t *local_passwd,
751 pj_grp_lock_t *grp_lock,
752 pj_ice_sess **p_ice);
753
754/**
755 * Get the value of various options of the ICE session.
756 *
757 * @param ice The ICE session.
758 * @param opt The options to be initialized with the values
759 * from the ICE session.
760 *
761 * @return PJ_SUCCESS on success, or the appropriate error.
762 */
763PJ_DECL(pj_status_t) pj_ice_sess_get_options(pj_ice_sess *ice,
764 pj_ice_sess_options *opt);
765
766/**
767 * Specify various options for this ICE session. Application MUST only
768 * call this function after the ICE session has been created but before
769 * any connectivity check is started.
770 *
771 * Application should call #pj_ice_sess_get_options() to initialize the
772 * options with their default values.
773 *
774 * @param ice The ICE session.
775 * @param opt Options to be applied to the ICE session.
776 *
777 * @return PJ_SUCCESS on success, or the appropriate error.
778 */
779PJ_DECL(pj_status_t) pj_ice_sess_set_options(pj_ice_sess *ice,
780 const pj_ice_sess_options *opt);
781
782/**
783 * Destroy ICE session. This will cancel any connectivity checks currently
784 * running, if any, and any other events scheduled by this session, as well
785 * as all memory resources.
786 *
787 * @param ice ICE session instance.
788 *
789 * @return PJ_SUCCESS on success.
790 */
791PJ_DECL(pj_status_t) pj_ice_sess_destroy(pj_ice_sess *ice);
792
793
794/**
795 * Change session role. This happens for example when ICE session was
796 * created with controlled role when receiving an offer, but it turns out
797 * that the offer contains "a=ice-lite" attribute when the SDP gets
798 * inspected.
799 *
800 * @param ice The ICE session.
801 * @param new_role The new role to be set.
802 *
803 * @return PJ_SUCCESS on success, or the appropriate error.
804 */
805PJ_DECL(pj_status_t) pj_ice_sess_change_role(pj_ice_sess *ice,
806 pj_ice_sess_role new_role);
807
808
809/**
810 * Assign a custom preference values for ICE candidate types. By assigning
811 * custom preference value, application can control the order of candidates
812 * to be checked first. The default preference settings is to use 126 for
813 * host candidates, 100 for server reflexive candidates, 110 for peer
814 * reflexive candidates, an 0 for relayed candidates.
815 *
816 * Note that this function must be called before any candidates are added
817 * to the ICE session.
818 *
819 * @param ice The ICE session.
820 * @param prefs Array of candidate preference value. The values are
821 * put in the array indexed by the candidate type as
822 * specified in pj_ice_cand_type.
823 *
824 * @return PJ_SUCCESS on success, or the appropriate error code.
825 */
826PJ_DECL(pj_status_t) pj_ice_sess_set_prefs(pj_ice_sess *ice,
827 const pj_uint8_t prefs[4]);
828
829
830
831/**
832 * Add a candidate to this ICE session. Application must add candidates for
833 * each components ID before it can start pairing the candidates and
834 * performing connectivity checks.
835 *
836 * @param ice ICE session instance.
837 * @param comp_id Component ID of this candidate.
838 * @param transport_id Transport ID to be used to send packets for this
839 * candidate.
840 * @param type Candidate type.
841 * @param local_pref Local preference for this candidate, which
842 * normally should be set to 65535.
843 * @param foundation Foundation identification.
844 * @param addr The candidate address.
845 * @param base_addr The candidate's base address.
846 * @param rel_addr Optional related address.
847 * @param addr_len Length of addresses.
848 * @param p_cand_id Optional pointer to receive the candidate ID.
849 *
850 * @return PJ_SUCCESS if candidate is successfully added.
851 */
852PJ_DECL(pj_status_t) pj_ice_sess_add_cand(pj_ice_sess *ice,
853 unsigned comp_id,
854 unsigned transport_id,
855 pj_ice_cand_type type,
856 pj_uint16_t local_pref,
857 const pj_str_t *foundation,
858 const pj_sockaddr_t *addr,
859 const pj_sockaddr_t *base_addr,
860 const pj_sockaddr_t *rel_addr,
861 int addr_len,
862 unsigned *p_cand_id);
863
864/**
865 * Find default candidate for the specified component ID, using this
866 * rule:
867 * - if the component has a successful candidate pair, then the
868 * local candidate of this pair will be returned.
869 * - otherwise a relay, reflexive, or host candidate will be selected
870 * on that specified order.
871 *
872 * @param ice The ICE session instance.
873 * @param comp_id The component ID.
874 * @param p_cand_id Pointer to receive the candidate ID.
875 *
876 * @return PJ_SUCCESS if a candidate has been selected.
877 */
878PJ_DECL(pj_status_t) pj_ice_sess_find_default_cand(pj_ice_sess *ice,
879 unsigned comp_id,
880 int *p_cand_id);
881
882/**
883 * Pair the local and remote candidates to create check list. Application
884 * typically would call this function after receiving SDP containing ICE
885 * candidates from the remote host (either upon receiving the initial
886 * offer, for UAS, or upon receiving the answer, for UAC).
887 *
888 * Note that ICE connectivity check will not start until application calls
889 * #pj_ice_sess_start_check().
890 *
891 * @param ice ICE session instance.
892 * @param rem_ufrag Remote ufrag, as seen in the SDP received from
893 * the remote agent.
894 * @param rem_passwd Remote password, as seen in the SDP received from
895 * the remote agent.
896 * @param rem_cand_cnt Number of remote candidates.
897 * @param rem_cand Remote candidate array. Remote candidates are
898 * gathered from the SDP received from the remote
899 * agent.
900 *
901 * @return PJ_SUCCESS or the appropriate error code.
902 */
903PJ_DECL(pj_status_t)
904pj_ice_sess_create_check_list(pj_ice_sess *ice,
905 const pj_str_t *rem_ufrag,
906 const pj_str_t *rem_passwd,
907 unsigned rem_cand_cnt,
908 const pj_ice_sess_cand rem_cand[]);
909
910/**
911 * Start ICE periodic check. This function will return immediately, and
912 * application will be notified about the connectivity check status in
913 * #pj_ice_sess_cb callback.
914 *
915 * @param ice The ICE session instance.
916 *
917 * @return PJ_SUCCESS or the appropriate error code.
918 */
919PJ_DECL(pj_status_t) pj_ice_sess_start_check(pj_ice_sess *ice);
920
921
922/**
923 * Send data using this ICE session. If ICE checks have not produced a
924 * valid check for the specified component ID, this function will return
925 * with failure. Otherwise ICE session will send the packet to remote
926 * destination using the nominated local candidate for the specified
927 * component.
928 *
929 * This function will in turn call \a on_tx_pkt function in
930 * #pj_ice_sess_cb callback to actually send the packet to the wire.
931 *
932 * @param ice The ICE session.
933 * @param comp_id Component ID.
934 * @param data The data or packet to be sent.
935 * @param data_len Size of data or packet, in bytes.
936 *
937 * @return PJ_SUCCESS if data is sent successfully.
938 */
939PJ_DECL(pj_status_t) pj_ice_sess_send_data(pj_ice_sess *ice,
940 unsigned comp_id,
941 const void *data,
942 pj_size_t data_len);
943
944/**
945 * Report the arrival of packet to the ICE session. Since ICE session
946 * itself doesn't have any transports, it relies on application or
947 * higher layer component to give incoming packets to the ICE session.
948 * If the packet is not a STUN packet, this packet will be given back
949 * to application via \a on_rx_data() callback in #pj_ice_sess_cb.
950 *
951 * @param ice The ICE session.
952 * @param comp_id Component ID.
953 * @param transport_id Number to identify where this packet was received
954 * from. This parameter will be returned back to
955 * application in \a on_tx_pkt() callback.
956 * @param pkt Incoming packet.
957 * @param pkt_size Size of incoming packet.
958 * @param src_addr Source address of the packet.
959 * @param src_addr_len Length of the address.
960 *
961 * @return PJ_SUCCESS or the appropriate error code.
962 */
963PJ_DECL(pj_status_t) pj_ice_sess_on_rx_pkt(pj_ice_sess *ice,
964 unsigned comp_id,
965 unsigned transport_id,
966 void *pkt,
967 pj_size_t pkt_size,
968 const pj_sockaddr_t *src_addr,
969 int src_addr_len);
970
971
972
973/**
974 * @}
975 */
976
977
978PJ_END_DECL
979
980
981#endif /* __PJNATH_ICE_SESSION_H__ */
982