ice_session.h 41 KB

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