1
/* $Id: ice_strans.h 4133 2012-05-21 14:00:17Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJNATH_ICE_STRANS_H__
21
#define __PJNATH_ICE_STRANS_H__
26
* @brief ICE Stream Transport
28
#include <pjnath/ice_session.h>
29
#include <pjnath/stun_sock.h>
30
#include <pjnath/turn_sock.h>
31
#include <pjlib-util/resolver.h>
32
#include <pj/ioqueue.h>
40
* @addtogroup PJNATH_ICE_STREAM_TRANSPORT
43
* This module describes ICE stream transport, as represented by #pj_ice_strans
44
* structure, and is part of PJNATH - the Open Source NAT traversal helper
47
* ICE stream transport, as represented by #pj_ice_strans structure, is an ICE
48
* capable class for transporting media streams within a media session.
49
* It consists of one or more transport sockets (typically two for RTP
50
* based communication - one for RTP and one for RTCP), and an
51
* \ref PJNATH_ICE_SESSION for performing connectivity checks among the.
52
* various candidates of the transport addresses.
55
* \section ice_strans_using_sec Using the ICE stream transport
57
* The steps below describe how to use ICE session:
59
* - initialize a #pj_ice_strans_cfg structure. This contains various
60
* settings for the ICE stream transport, and among other things contains
61
* the STUN and TURN settings.\n\n
62
* - create the instance with #pj_ice_strans_create(). Among other things,
63
* the function needs the following arguments:
64
* - the #pj_ice_strans_cfg structure for the main configurations
65
* - number of components to be supported
66
* - instance of #pj_ice_strans_cb structure to report callbacks to
68
* - while the #pj_ice_strans_create() call completes immediately, the
69
* initialization will be running in the background to gather the
70
* candidates (for example STUN and TURN candidates, if they are enabled
71
* in the #pj_ice_strans_cfg setting). Application will be notified when
72
* the initialization completes in the \a on_ice_complete callback of
73
* the #pj_ice_strans_cb structure (the \a op argument of this callback
74
* will be PJ_ICE_STRANS_OP_INIT).\n\n
75
* - when media stream is to be started (for example, a call is to be
76
* started), create an ICE session by calling #pj_ice_strans_init_ice().\n\n
77
* - the application now typically will need to communicate local ICE
78
* information to remote host. It can achieve this by using the following
79
* functions to query local ICE information:
80
* - #pj_ice_strans_get_ufrag_pwd()
81
* - #pj_ice_strans_enum_cands()
82
* - #pj_ice_strans_get_def_cand()\n
83
* The application may need to encode the above information as SDP.\n\n
84
* - when the application receives remote ICE information (for example, from
85
* the SDP received from remote), it can now start ICE negotiation, by
86
* calling #pj_ice_strans_start_ice(). This function requires some
87
* information about remote ICE agent such as remote ICE username fragment
88
* and password as well as array of remote candidates.\n\n
89
* - note that the PJNATH library does not work with SDP; application would
90
* need to encode and parse the SDP itself.\n\n
91
* - once ICE negotiation has been started, application will be notified
92
* about the completion in the \a on_ice_complete() callback of the
93
* #pj_ice_strans_cb.\n\n
94
* - at any time, application may send or receive data. However the ICE
95
* stream transport may not be able to send it depending on its current
96
* state. Before ICE negotiation is started, the data will be sent using
97
* default candidate of the component. After negotiation is completed,
98
* data will be sent using the candidate from the successful/nominated
99
* pair. The ICE stream transport may not be able to send data while
100
* negotiation is in progress.\n\n
101
* - application sends data by using #pj_ice_strans_sendto(). Incoming
102
* data will be reported in \a on_rx_data() callback of the
103
* #pj_ice_strans_cb.\n\n
104
* - once the media session has finished (e.g. user hangs up the call),
105
* destroy the ICE session with #pj_ice_strans_stop_ice().\n\n
106
* - at this point, application may destroy the ICE stream transport itself,
107
* or let it run so that it can be reused to create other ICE session.
108
* The benefit of letting the ICE stream transport alive (without any
109
* session active) is to avoid delay with the initialization, howerver
110
* keeping the transport alive means the transport needs to keep the
111
* STUN binding open by using keep-alive and also TURN allocation alive,
112
* and this will consume power which is an important issue for mobile
116
/** Forward declaration for ICE stream transport. */
117
typedef struct pj_ice_strans pj_ice_strans;
119
/** Transport operation types to be reported on \a on_status() callback */
120
typedef enum pj_ice_strans_op
122
/** Initialization (candidate gathering) */
123
PJ_ICE_STRANS_OP_INIT,
126
PJ_ICE_STRANS_OP_NEGOTIATION,
128
/** This operatino is used to report failure in keep-alive operation.
129
* Currently it is only used to report TURN Refresh failure.
131
PJ_ICE_STRANS_OP_KEEP_ALIVE
136
* This structure contains callbacks that will be called by the
137
* ICE stream transport.
139
typedef struct pj_ice_strans_cb
142
* This callback will be called when the ICE transport receives
143
* incoming packet from the sockets which is not related to ICE
144
* (for example, normal RTP/RTCP packet destined for application).
146
* @param ice_st The ICE stream transport.
147
* @param comp_id The component ID.
148
* @param pkt The packet.
149
* @param size Size of the packet.
150
* @param src_addr Source address of the packet.
151
* @param src_addr_len Length of the source address.
153
void (*on_rx_data)(pj_ice_strans *ice_st,
155
void *pkt, pj_size_t size,
156
const pj_sockaddr_t *src_addr,
157
unsigned src_addr_len);
160
* Callback to report status of various ICE operations.
162
* @param ice_st The ICE stream transport.
163
* @param op The operation which status is being reported.
164
* @param status Operation status.
166
void (*on_ice_complete)(pj_ice_strans *ice_st,
174
* This structure describes ICE stream transport configuration. Application
175
* should initialize the structure by calling #pj_ice_strans_cfg_default()
176
* before changing the settings.
178
typedef struct pj_ice_strans_cfg
181
* Address family, IPv4 or IPv6. Currently only pj_AF_INET() (IPv4)
182
* is supported, and this is the default value.
187
* STUN configuration which contains the timer heap and
188
* ioqueue instance to be used, and STUN retransmission
189
* settings. This setting is mandatory.
191
* The default value is all zero. Application must initialize
192
* this setting with #pj_stun_config_init().
194
pj_stun_config stun_cfg;
197
* DNS resolver to be used to resolve servers. If DNS SRV
198
* resolution is required, the resolver must be set.
200
* The default value is NULL.
202
pj_dns_resolver *resolver;
205
* This contains various STUN session options. Once the ICE stream
206
* transport is created, application may also change the options
207
* with #pj_ice_strans_set_options().
209
pj_ice_sess_options opt;
212
* STUN and local transport settings. This specifies the
213
* settings for local UDP socket, which will be resolved
214
* to get the STUN mapped address.
218
* Optional configuration for STUN transport. The default
219
* value will be initialized with #pj_stun_sock_cfg_default().
221
pj_stun_sock_cfg cfg;
224
* Maximum number of host candidates to be added. If the
225
* value is zero, no host candidates will be added.
229
unsigned max_host_cands;
232
* Include loopback addresses in the host candidates.
239
* Specify the STUN server domain or hostname or IP address.
240
* If DNS SRV resolution is required, application must fill
241
* in this setting with the domain name of the STUN server
242
* and set the resolver instance in the \a resolver field.
243
* Otherwise if the \a resolver setting is not set, this
244
* field will be resolved with hostname resolution and in
245
* this case the \a port field must be set.
247
* The \a port field should also be set even when DNS SRV
248
* resolution is used, in case the DNS SRV resolution fails.
250
* When this field is empty, STUN mapped address resolution
251
* will not be performed. In this case only ICE host candidates
252
* will be added to the ICE transport, unless if \a no_host_cands
253
* field is set. In this case, both host and srflx candidates
256
* The default value is empty.
261
* The port number of the STUN server, when \a server
262
* field specifies a hostname rather than domain name. This
263
* field should also be set even when the \a server
264
* specifies a domain name, to allow DNS SRV resolution
265
* to fallback to DNS A/AAAA resolution when the DNS SRV
268
* The default value is PJ_STUN_PORT.
273
* Ignore STUN resolution error and proceed with just local
276
* The default is PJ_FALSE
278
pj_bool_t ignore_stun_error;
283
* TURN specific settings.
287
* Optional TURN socket settings. The default values will be
288
* initialized by #pj_turn_sock_cfg_default(). This contains
289
* settings such as QoS.
291
pj_turn_sock_cfg cfg;
294
* Specify the TURN server domain or hostname or IP address.
295
* If DNS SRV resolution is required, application must fill
296
* in this setting with the domain name of the TURN server
297
* and set the resolver instance in the \a resolver field.
298
* Otherwise if the \a resolver setting is not set, this
299
* field will be resolved with hostname resolution and in
300
* this case the \a port field must be set.
302
* The \a port field should also be set even when DNS SRV
303
* resolution is used, in case the DNS SRV resolution fails.
305
* When this field is empty, relay candidate will not be
308
* The default value is empty.
313
* The port number of the TURN server, when \a server
314
* field specifies a hostname rather than domain name. This
315
* field should also be set even when the \a server
316
* specifies a domain name, to allow DNS SRV resolution
317
* to fallback to DNS A/AAAA resolution when the DNS SRV
325
* Type of connection to the TURN server.
327
* Default is PJ_TURN_TP_UDP.
329
pj_turn_tp_type conn_type;
332
* Credential to be used for the TURN session. This setting
335
* Default is to have no credential.
337
pj_stun_auth_cred auth_cred;
340
* Optional TURN Allocate parameter. The default value will be
341
* initialized by #pj_turn_alloc_param_default().
343
pj_turn_alloc_param alloc_param;
348
* Component specific settings, which will override the settings in
349
* the STUN and TURN settings above. For example, setting the QoS
350
* parameters here allows the application to have different QoS
351
* traffic type for RTP and RTCP component.
355
* QoS traffic type to be set on this transport. When application
356
* wants to apply QoS tagging to the transport, it's preferable to
357
* set this field rather than \a qos_param fields since this is
360
* Default value is PJ_QOS_TYPE_BEST_EFFORT.
362
pj_qos_type qos_type;
365
* Set the low level QoS parameters to the transport. This is a
366
* lower level operation than setting the \a qos_type field and
367
* may not be supported on all platforms.
369
* By default all settings in this structure are disabled.
371
pj_qos_params qos_params;
373
} comp[PJ_ICE_MAX_COMP];
379
* ICE stream transport's state.
381
typedef enum pj_ice_strans_state
384
* ICE stream transport is not created.
386
PJ_ICE_STRANS_STATE_NULL,
389
* ICE candidate gathering process is in progress.
391
PJ_ICE_STRANS_STATE_INIT,
394
* ICE stream transport initialization/candidate gathering process is
395
* complete, ICE session may be created on this stream transport.
397
PJ_ICE_STRANS_STATE_READY,
400
* New session has been created and the session is ready.
402
PJ_ICE_STRANS_STATE_SESS_READY,
405
* ICE negotiation is in progress.
407
PJ_ICE_STRANS_STATE_NEGO,
410
* ICE negotiation has completed successfully and media is ready
413
PJ_ICE_STRANS_STATE_RUNNING,
416
* ICE negotiation has completed with failure.
418
PJ_ICE_STRANS_STATE_FAILED
420
} pj_ice_strans_state;
424
* Initialize ICE transport configuration with default values.
426
* @param cfg The configuration to be initialized.
428
PJ_DECL(void) pj_ice_strans_cfg_default(pj_ice_strans_cfg *cfg);
432
* Copy configuration.
435
* @param dst Destination.
438
PJ_DECL(void) pj_ice_strans_cfg_copy(pj_pool_t *pool,
439
pj_ice_strans_cfg *dst,
440
const pj_ice_strans_cfg *src);
444
* Create and initialize the ICE stream transport with the specified
447
* @param name Optional name for logging identification.
448
* @param cfg Configuration.
449
* @param comp_cnt Number of components.
450
* @param user_data Arbitrary user data to be associated with this
451
* ICE stream transport.
452
* @param cb Callback.
453
* @param p_ice_st Pointer to receive the ICE stream transport
456
* @return PJ_SUCCESS if ICE stream transport is created
459
PJ_DECL(pj_status_t) pj_ice_strans_create(const char *name,
460
const pj_ice_strans_cfg *cfg,
463
const pj_ice_strans_cb *cb,
464
pj_ice_strans **p_ice_st);
467
* Get ICE session state.
469
* @param ice_st The ICE stream transport.
471
* @return ICE session state.
473
PJ_DECL(pj_ice_strans_state) pj_ice_strans_get_state(pj_ice_strans *ice_st);
477
* Get string representation of ICE state.
479
* @param state ICE stream transport state.
483
PJ_DECL(const char*) pj_ice_strans_state_name(pj_ice_strans_state state);
487
* Destroy the ICE stream transport. This will destroy the ICE session
488
* inside the ICE stream transport, close all sockets and release all
491
* @param ice_st The ICE stream transport.
493
* @return PJ_SUCCESS, or the appropriate error code.
495
PJ_DECL(pj_status_t) pj_ice_strans_destroy(pj_ice_strans *ice_st);
499
* Get the user data associated with the ICE stream transport.
501
* @param ice_st The ICE stream transport.
503
* @return The user data.
505
PJ_DECL(void*) pj_ice_strans_get_user_data(pj_ice_strans *ice_st);
509
* Get the value of various options of the ICE stream transport.
511
* @param ice_st The ICE stream transport.
512
* @param opt The options to be initialized with the values
513
* from the ICE stream transport.
515
* @return PJ_SUCCESS on success, or the appropriate error.
517
PJ_DECL(pj_status_t) pj_ice_strans_get_options(pj_ice_strans *ice_st,
518
pj_ice_sess_options *opt);
521
* Specify various options for this ICE stream transport. Application
522
* should call #pj_ice_strans_get_options() to initialize the options
523
* with their default values.
525
* @param ice_st The ICE stream transport.
526
* @param opt Options to be applied to this ICE stream transport.
528
* @return PJ_SUCCESS on success, or the appropriate error.
530
PJ_DECL(pj_status_t) pj_ice_strans_set_options(pj_ice_strans *ice_st,
531
const pj_ice_sess_options *opt);
535
* Initialize the ICE session in the ICE stream transport.
536
* When application is about to send an offer containing ICE capability,
537
* or when it receives an offer containing ICE capability, it must
538
* call this function to initialize the internal ICE session. This would
539
* register all transport address aliases for each component to the ICE
540
* session as candidates. Then application can enumerate all local
541
* candidates by calling #pj_ice_strans_enum_cands(), and encode these
542
* candidates in the SDP to be sent to remote agent.
544
* @param ice_st The ICE stream transport.
545
* @param role ICE role.
546
* @param local_ufrag Optional local username fragment.
547
* @param local_passwd Optional local password.
549
* @return PJ_SUCCESS, or the appropriate error code.
551
PJ_DECL(pj_status_t) pj_ice_strans_init_ice(pj_ice_strans *ice_st,
552
pj_ice_sess_role role,
553
const pj_str_t *local_ufrag,
554
const pj_str_t *local_passwd);
557
* Check if the ICE stream transport has the ICE session created. The
558
* ICE session is created with #pj_ice_strans_init_ice().
560
* @param ice_st The ICE stream transport.
562
* @return PJ_TRUE if #pj_ice_strans_init_ice() has been
565
PJ_DECL(pj_bool_t) pj_ice_strans_has_sess(pj_ice_strans *ice_st);
569
* Check if ICE negotiation is still running.
571
* @param ice_st The ICE stream transport.
573
* @return PJ_TRUE if ICE session has been created and ICE
574
* negotiation negotiation is in progress.
576
PJ_DECL(pj_bool_t) pj_ice_strans_sess_is_running(pj_ice_strans *ice_st);
580
* Check if ICE negotiation has completed.
582
* @param ice_st The ICE stream transport.
584
* @return PJ_TRUE if ICE session has been created and the
585
* negotiation is complete.
587
PJ_DECL(pj_bool_t) pj_ice_strans_sess_is_complete(pj_ice_strans *ice_st);
591
* Get the current/running component count. If ICE negotiation has not
592
* been started, the number of components will be equal to the number
593
* when the ICE stream transport was created. Once negotiation been
594
* started, the number of components will be the lowest number of
595
* component between local and remote agents.
597
* @param ice_st The ICE stream transport.
599
* @return The running number of components.
601
PJ_DECL(unsigned) pj_ice_strans_get_running_comp_cnt(pj_ice_strans *ice_st);
605
* Get the ICE username fragment and password of the ICE session. The
606
* local username fragment and password can only be retrieved once ICE
607
* session has been created with #pj_ice_strans_init_ice(). The remote
608
* username fragment and password can only be retrieved once ICE session
609
* has been started with #pj_ice_strans_start_ice().
611
* Note that the string returned by this function is only valid throughout
612
* the duration of the ICE session, and the application must not modify
613
* these strings. Once the ICE session has been stopped with
614
* #pj_ice_strans_stop_ice(), the pointer in the string will no longer be
617
* @param ice_st The ICE stream transport.
618
* @param loc_ufrag Optional pointer to receive ICE username fragment
619
* of local endpoint from the ICE session.
620
* @param loc_pwd Optional pointer to receive ICE password of local
621
* endpoint from the ICE session.
622
* @param rem_ufrag Optional pointer to receive ICE username fragment
623
* of remote endpoint from the ICE session.
624
* @param rem_pwd Optional pointer to receive ICE password of remote
625
* endpoint from the ICE session.
627
* @return PJ_SUCCESS if the strings have been retrieved
630
PJ_DECL(pj_status_t) pj_ice_strans_get_ufrag_pwd(pj_ice_strans *ice_st,
638
* Get the number of local candidates for the specified component ID.
640
* @param ice_st The ICE stream transport.
641
* @param comp_id Component ID.
643
* @return The number of candidates.
645
PJ_DECL(unsigned) pj_ice_strans_get_cands_count(pj_ice_strans *ice_st,
649
* Enumerate the local candidates for the specified component.
651
* @param ice_st The ICE stream transport.
652
* @param comp_id Component ID.
653
* @param count On input, it specifies the maximum number of
654
* elements. On output, it will be filled with
655
* the number of candidates copied to the
657
* @param cand Array of candidates.
659
* @return PJ_SUCCESS, or the appropriate error code.
661
PJ_DECL(pj_status_t) pj_ice_strans_enum_cands(pj_ice_strans *ice_st,
664
pj_ice_sess_cand cand[]);
667
* Get the default candidate for the specified component. When this
668
* function is called before ICE negotiation completes, the default
669
* candidate is selected according to local preference criteria. When
670
* this function is called after ICE negotiation completes, the
671
* default candidate is the candidate that forms the valid pair.
673
* @param ice_st The ICE stream transport.
674
* @param comp_id Component ID.
675
* @param cand Pointer to receive the default candidate
678
PJ_DECL(pj_status_t) pj_ice_strans_get_def_cand(pj_ice_strans *ice_st,
680
pj_ice_sess_cand *cand);
683
* Get the current ICE role. ICE session must have been initialized
684
* before this function can be called.
686
* @param ice_st The ICE stream transport.
688
* @return Current ICE role.
690
PJ_DECL(pj_ice_sess_role) pj_ice_strans_get_role(pj_ice_strans *ice_st);
694
* Change session role. This happens for example when ICE session was
695
* created with controlled role when receiving an offer, but it turns out
696
* that the offer contains "a=ice-lite" attribute when the SDP gets
697
* inspected. ICE session must have been initialized before this function
700
* @param ice_st The ICE stream transport.
701
* @param new_role The new role to be set.
703
* @return PJ_SUCCESS on success, or the appropriate error.
705
PJ_DECL(pj_status_t) pj_ice_strans_change_role(pj_ice_strans *ice_st,
706
pj_ice_sess_role new_role);
710
* Start ICE connectivity checks. This function can only be called
711
* after the ICE session has been created in the ICE stream transport
712
* with #pj_ice_strans_init_ice().
714
* This function must be called once application has received remote
715
* candidate list (typically from the remote SDP). This function pairs
716
* local candidates with remote candidates, and starts ICE connectivity
717
* checks. The ICE session/transport will then notify the application
718
* via the callback when ICE connectivity checks completes, either
719
* successfully or with failure.
721
* @param ice_st The ICE stream transport.
722
* @param rem_ufrag Remote ufrag, as seen in the SDP received from
724
* @param rem_passwd Remote password, as seen in the SDP received from
726
* @param rcand_cnt Number of remote candidates in the array.
727
* @param rcand Remote candidates array.
729
* @return PJ_SUCCESS, or the appropriate error code.
731
PJ_DECL(pj_status_t) pj_ice_strans_start_ice(pj_ice_strans *ice_st,
732
const pj_str_t *rem_ufrag,
733
const pj_str_t *rem_passwd,
735
const pj_ice_sess_cand rcand[]);
738
* Retrieve the candidate pair that has been nominated and successfully
739
* checked for the specified component. If ICE negotiation is still in
740
* progress or it has failed, this function will return NULL.
742
* @param ice_st The ICE stream transport.
743
* @param comp_id Component ID.
745
* @return The valid pair as ICE checklist structure if the
748
PJ_DECL(const pj_ice_sess_check*)
749
pj_ice_strans_get_valid_pair(const pj_ice_strans *ice_st,
753
* Stop and destroy the ICE session inside this media transport. Application
754
* needs to call this function once the media session is over (the call has
755
* been disconnected).
757
* Application MAY reuse this ICE stream transport for subsequent calls.
758
* In this case, it must call #pj_ice_strans_stop_ice() when the call is
759
* disconnected, and reinitialize the ICE stream transport for subsequent
760
* call with #pj_ice_strans_init_ice()/#pj_ice_strans_start_ice(). In this
761
* case, the ICE stream transport will maintain the internal sockets and
762
* continue to send STUN keep-alive packets and TURN Refresh request to
763
* keep the NAT binding/TURN allocation open and to detect change in STUN
766
* If application does not want to reuse the ICE stream transport for
767
* subsequent calls, it must call #pj_ice_strans_destroy() to destroy the
768
* ICE stream transport altogether.
770
* @param ice_st The ICE stream transport.
772
* @return PJ_SUCCESS, or the appropriate error code.
774
PJ_DECL(pj_status_t) pj_ice_strans_stop_ice(pj_ice_strans *ice_st);
778
* Send outgoing packet using this transport.
779
* Application can send data (normally RTP or RTCP packets) at any time
780
* by calling this function. This function takes a destination
781
* address as one of the arguments, and this destination address should
782
* be taken from the default transport address of the component (that is
783
* the address in SDP c= and m= lines, or in a=rtcp attribute).
784
* If ICE negotiation is in progress, this function will send the data
785
* to the destination address. Otherwise if ICE negotiation has completed
786
* successfully, this function will send the data to the nominated remote
787
* address, as negotiated by ICE.
789
* @param ice_st The ICE stream transport.
790
* @param comp_id Component ID.
791
* @param data The data or packet to be sent.
792
* @param data_len Size of data or packet, in bytes.
793
* @param dst_addr The destination address.
794
* @param dst_addr_len Length of destination address.
796
* @return PJ_SUCCESS if data is sent successfully.
798
PJ_DECL(pj_status_t) pj_ice_strans_sendto(pj_ice_strans *ice_st,
802
const pj_sockaddr_t *dst_addr,
815
#endif /* __PJNATH_ICE_STRANS_H__ */