1
/* $Id: ice_strans.h 3028 2009-12-08 13:11:25Z bennylp $ */
3
* Copyright (C) 2008-2009 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
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
31
#ifndef __PJNATH_ICE_STRANS_H__
32
#define __PJNATH_ICE_STRANS_H__
37
* @brief ICE Stream Transport
39
#include <pjnath/ice_session.h>
40
#include <pjnath/stun_sock.h>
41
#include <pjnath/turn_sock.h>
42
#include <pjlib-util/resolver.h>
43
#include <pj/ioqueue.h>
51
* @addtogroup PJNATH_ICE_STREAM_TRANSPORT
54
* This module describes ICE stream transport, as represented by #pj_ice_strans
55
* structure, and is part of PJNATH - the Open Source NAT traversal helper
58
* ICE stream transport, as represented by #pj_ice_strans structure, is an ICE
59
* capable class for transporting media streams within a media session.
60
* It consists of one or more transport sockets (typically two for RTP
61
* based communication - one for RTP and one for RTCP), and an
62
* \ref PJNATH_ICE_SESSION for performing connectivity checks among the.
63
* various candidates of the transport addresses.
66
* \section ice_strans_using_sec Using the ICE stream transport
68
* The steps below describe how to use ICE session:
70
* - initialize a #pj_ice_strans_cfg structure. This contains various
71
* settings for the ICE stream transport, and among other things contains
72
* the STUN and TURN settings.\n\n
73
* - create the instance with #pj_ice_strans_create(). Among other things,
74
* the function needs the following arguments:
75
* - the #pj_ice_strans_cfg structure for the main configurations
76
* - number of components to be supported
77
* - instance of #pj_ice_strans_cb structure to report callbacks to
79
* - while the #pj_ice_strans_create() call completes immediately, the
80
* initialization will be running in the background to gather the
81
* candidates (for example STUN and TURN candidates, if they are enabled
82
* in the #pj_ice_strans_cfg setting). Application will be notified when
83
* the initialization completes in the \a on_ice_complete callback of
84
* the #pj_ice_strans_cb structure (the \a op argument of this callback
85
* will be PJ_ICE_STRANS_OP_INIT).\n\n
86
* - when media stream is to be started (for example, a call is to be
87
* started), create an ICE session by calling #pj_ice_strans_init_ice().\n\n
88
* - the application now typically will need to communicate local ICE
89
* information to remote host. It can achieve this by using the following
90
* functions to query local ICE information:
91
* - #pj_ice_strans_get_ufrag_pwd()
92
* - #pj_ice_strans_enum_cands()
93
* - #pj_ice_strans_get_def_cand()\n
94
* The application may need to encode the above information as SDP.\n\n
95
* - when the application receives remote ICE information (for example, from
96
* the SDP received from remote), it can now start ICE negotiation, by
97
* calling #pj_ice_strans_start_ice(). This function requires some
98
* information about remote ICE agent such as remote ICE username fragment
99
* and password as well as array of remote candidates.\n\n
100
* - note that the PJNATH library does not work with SDP; application would
101
* need to encode and parse the SDP itself.\n\n
102
* - once ICE negotiation has been started, application will be notified
103
* about the completion in the \a on_ice_complete() callback of the
104
* #pj_ice_strans_cb.\n\n
105
* - at any time, application may send or receive data. However the ICE
106
* stream transport may not be able to send it depending on its current
107
* state. Before ICE negotiation is started, the data will be sent using
108
* default candidate of the component. After negotiation is completed,
109
* data will be sent using the candidate from the successful/nominated
110
* pair. The ICE stream transport may not be able to send data while
111
* negotiation is in progress.\n\n
112
* - application sends data by using #pj_ice_strans_sendto(). Incoming
113
* data will be reported in \a on_rx_data() callback of the
114
* #pj_ice_strans_cb.\n\n
115
* - once the media session has finished (e.g. user hangs up the call),
116
* destroy the ICE session with #pj_ice_strans_stop_ice().\n\n
117
* - at this point, application may destroy the ICE stream transport itself,
118
* or let it run so that it can be reused to create other ICE session.
119
* The benefit of letting the ICE stream transport alive (without any
120
* session active) is to avoid delay with the initialization, howerver
121
* keeping the transport alive means the transport needs to keep the
122
* STUN binding open by using keep-alive and also TURN allocation alive,
123
* and this will consume power which is an important issue for mobile
127
/** Forward declaration for ICE stream transport. */
128
typedef struct pj_ice_strans pj_ice_strans;
130
/** Transport operation types to be reported on \a on_status() callback */
131
typedef enum pj_ice_strans_op
133
/** Initialization (candidate gathering) */
134
PJ_ICE_STRANS_OP_INIT,
137
PJ_ICE_STRANS_OP_NEGOTIATION
142
* This structure contains callbacks that will be called by the
143
* ICE stream transport.
145
typedef struct pj_ice_strans_cb
148
* This callback will be called when the ICE transport receives
149
* incoming packet from the sockets which is not related to ICE
150
* (for example, normal RTP/RTCP packet destined for application).
152
* @param ice_st The ICE stream transport.
153
* @param comp_id The component ID.
154
* @param pkt The packet.
155
* @param size Size of the packet.
156
* @param src_addr Source address of the packet.
157
* @param src_addr_len Length of the source address.
159
void (*on_rx_data)(pj_ice_strans *ice_st,
161
void *pkt, pj_size_t size,
162
const pj_sockaddr_t *src_addr,
163
unsigned src_addr_len);
166
* Callback to report status.
168
* @param ice_st The ICE stream transport.
169
* @param op The operation
170
* @param status Operation status.
172
void (*on_ice_complete)(pj_ice_strans *ice_st,
180
* This structure describes ICE stream transport configuration. Application
181
* should initialize the structure by calling #pj_ice_strans_cfg_default()
182
* before changing the settings.
184
typedef struct pj_ice_strans_cfg
187
* Address family, IPv4 or IPv6. Currently only pj_AF_INET() (IPv4)
188
* is supported, and this is the default value.
193
* STUN configuration which contains the timer heap and
194
* ioqueue instance to be used, and STUN retransmission
195
* settings. This setting is mandatory.
197
* The default value is all zero. Application must initialize
198
* this setting with #pj_stun_config_init().
200
pj_stun_config stun_cfg;
203
* DNS resolver to be used to resolve servers. If DNS SRV
204
* resolution is required, the resolver must be set.
206
* The default value is NULL.
208
pj_dns_resolver *resolver;
211
* This contains various STUN session options. Once the ICE stream
212
* transport is created, application may also change the options
213
* with #pj_ice_strans_set_options().
215
pj_ice_sess_options opt;
218
* STUN and local transport settings. This specifies the
219
* settings for local UDP socket, which will be resolved
220
* to get the STUN mapped address.
224
* Optional configuration for STUN transport. The default
225
* value will be initialized with #pj_stun_sock_cfg_default().
227
pj_stun_sock_cfg cfg;
230
* Maximum number of host candidates to be added. If the
231
* value is zero, no host candidates will be added.
235
unsigned max_host_cands;
238
* Include loopback addresses in the host candidates.
245
* Specify the STUN server domain or hostname or IP address.
246
* If DNS SRV resolution is required, application must fill
247
* in this setting with the domain name of the STUN server
248
* and set the resolver instance in the \a resolver field.
249
* Otherwise if the \a resolver setting is not set, this
250
* field will be resolved with hostname resolution and in
251
* this case the \a port field must be set.
253
* The \a port field should also be set even when DNS SRV
254
* resolution is used, in case the DNS SRV resolution fails.
256
* When this field is empty, STUN mapped address resolution
257
* will not be performed. In this case only ICE host candidates
258
* will be added to the ICE transport, unless if \a no_host_cands
259
* field is set. In this case, both host and srflx candidates
262
* The default value is empty.
267
* The port number of the STUN server, when \a server
268
* field specifies a hostname rather than domain name. This
269
* field should also be set even when the \a server
270
* specifies a domain name, to allow DNS SRV resolution
271
* to fallback to DNS A/AAAA resolution when the DNS SRV
274
* The default value is PJ_STUN_PORT.
281
* TURN specific settings.
285
* Optional TURN socket settings. The default values will be
286
* initialized by #pj_turn_sock_cfg_default(). This contains
287
* settings such as QoS.
289
pj_turn_sock_cfg cfg;
292
* Specify the TURN server domain or hostname or IP address.
293
* If DNS SRV resolution is required, application must fill
294
* in this setting with the domain name of the TURN server
295
* and set the resolver instance in the \a resolver field.
296
* Otherwise if the \a resolver setting is not set, this
297
* field will be resolved with hostname resolution and in
298
* this case the \a port field must be set.
300
* The \a port field should also be set even when DNS SRV
301
* resolution is used, in case the DNS SRV resolution fails.
303
* When this field is empty, relay candidate will not be
306
* The default value is empty.
311
* The port number of the TURN server, when \a server
312
* field specifies a hostname rather than domain name. This
313
* field should also be set even when the \a server
314
* specifies a domain name, to allow DNS SRV resolution
315
* to fallback to DNS A/AAAA resolution when the DNS SRV
323
* Type of connection to the TURN server.
325
* Default is PJ_TURN_TP_UDP.
327
pj_turn_tp_type conn_type;
330
* Credential to be used for the TURN session. This setting
333
* Default is to have no credential.
335
pj_stun_auth_cred auth_cred;
338
* Optional TURN Allocate parameter. The default value will be
339
* initialized by #pj_turn_alloc_param_default().
341
pj_turn_alloc_param alloc_param;
346
* Component specific settings, which will override the settings in
347
* the STUN and TURN settings above. For example, setting the QoS
348
* parameters here allows the application to have different QoS
349
* traffic type for RTP and RTCP component.
353
* QoS traffic type to be set on this transport. When application
354
* wants to apply QoS tagging to the transport, it's preferable to
355
* set this field rather than \a qos_param fields since this is
358
* Default value is PJ_QOS_TYPE_BEST_EFFORT.
360
pj_qos_type qos_type;
363
* Set the low level QoS parameters to the transport. This is a
364
* lower level operation than setting the \a qos_type field and
365
* may not be supported on all platforms.
367
* By default all settings in this structure are disabled.
369
pj_qos_params qos_params;
371
} comp[PJ_ICE_MAX_COMP];
377
* ICE stream transport's state.
379
typedef enum pj_ice_strans_state
382
* ICE stream transport is not created.
384
PJ_ICE_STRANS_STATE_NULL,
387
* ICE candidate gathering process is in progress.
389
PJ_ICE_STRANS_STATE_INIT,
392
* ICE stream transport initialization/candidate gathering process is
393
* complete, ICE session may be created on this stream transport.
395
PJ_ICE_STRANS_STATE_READY,
398
* New session has been created and the session is ready.
400
PJ_ICE_STRANS_STATE_SESS_READY,
403
* ICE negotiation is in progress.
405
PJ_ICE_STRANS_STATE_NEGO,
408
* ICE negotiation has completed successfully and media is ready
411
PJ_ICE_STRANS_STATE_RUNNING,
414
* ICE negotiation has completed with failure.
416
PJ_ICE_STRANS_STATE_FAILED
418
} pj_ice_strans_state;
422
* Initialize ICE transport configuration with default values.
424
* @param cfg The configuration to be initialized.
426
PJ_DECL(void) pj_ice_strans_cfg_default(pj_ice_strans_cfg *cfg);
430
* Copy configuration.
433
* @param dst Destination.
436
PJ_DECL(void) pj_ice_strans_cfg_copy(pj_pool_t *pool,
437
pj_ice_strans_cfg *dst,
438
const pj_ice_strans_cfg *src);
442
* Create and initialize the ICE stream transport with the specified
445
* @param name Optional name for logging identification.
446
* @param cfg Configuration.
447
* @param comp_cnt Number of components.
448
* @param user_data Arbitrary user data to be associated with this
449
* ICE stream transport.
450
* @param cb Callback.
451
* @param p_ice_st Pointer to receive the ICE stream transport
454
* @return PJ_SUCCESS if ICE stream transport is created
457
PJ_DECL(pj_status_t) pj_ice_strans_create(const char *name,
458
const pj_ice_strans_cfg *cfg,
461
const pj_ice_strans_cb *cb,
462
pj_ice_strans **p_ice_st);
465
* Get ICE session state.
467
* @param ice_st The ICE stream transport.
469
* @return ICE session state.
471
PJ_DECL(pj_ice_strans_state) pj_ice_strans_get_state(pj_ice_strans *ice_st);
475
* Get string representation of ICE state.
477
* @param state ICE stream transport state.
481
PJ_DECL(const char*) pj_ice_strans_state_name(pj_ice_strans_state state);
485
* Destroy the ICE stream transport. This will destroy the ICE session
486
* inside the ICE stream transport, close all sockets and release all
489
* @param ice_st The ICE stream transport.
491
* @return PJ_SUCCESS, or the appropriate error code.
493
PJ_DECL(pj_status_t) pj_ice_strans_destroy(pj_ice_strans *ice_st);
497
* Get the user data associated with the ICE stream transport.
499
* @param ice_st The ICE stream transport.
501
* @return The user data.
503
PJ_DECL(void*) pj_ice_strans_get_user_data(pj_ice_strans *ice_st);
507
* Get the value of various options of the ICE stream transport.
509
* @param ice_st The ICE stream transport.
510
* @param opt The options to be initialized with the values
511
* from the ICE stream transport.
513
* @return PJ_SUCCESS on success, or the appropriate error.
515
PJ_DECL(pj_status_t) pj_ice_strans_get_options(pj_ice_strans *ice_st,
516
pj_ice_sess_options *opt);
519
* Specify various options for this ICE stream transport. Application
520
* should call #pj_ice_strans_get_options() to initialize the options
521
* with their default values.
523
* @param ice_st The ICE stream transport.
524
* @param opt Options to be applied to this ICE stream transport.
526
* @return PJ_SUCCESS on success, or the appropriate error.
528
PJ_DECL(pj_status_t) pj_ice_strans_set_options(pj_ice_strans *ice_st,
529
const pj_ice_sess_options *opt);
533
* Initialize the ICE session in the ICE stream transport.
534
* When application is about to send an offer containing ICE capability,
535
* or when it receives an offer containing ICE capability, it must
536
* call this function to initialize the internal ICE session. This would
537
* register all transport address aliases for each component to the ICE
538
* session as candidates. Then application can enumerate all local
539
* candidates by calling #pj_ice_strans_enum_cands(), and encode these
540
* candidates in the SDP to be sent to remote agent.
542
* @param ice_st The ICE stream transport.
543
* @param role ICE role.
544
* @param local_ufrag Optional local username fragment.
545
* @param local_passwd Optional local password.
547
* @return PJ_SUCCESS, or the appropriate error code.
549
PJ_DECL(pj_status_t) pj_ice_strans_init_ice(pj_ice_strans *ice_st,
550
pj_ice_sess_role role,
551
const pj_str_t *local_ufrag,
552
const pj_str_t *local_passwd);
555
* Check if the ICE stream transport has the ICE session created. The
556
* ICE session is created with #pj_ice_strans_init_ice().
558
* @param ice_st The ICE stream transport.
560
* @return PJ_TRUE if #pj_ice_strans_init_ice() has been
563
PJ_DECL(pj_bool_t) pj_ice_strans_has_sess(pj_ice_strans *ice_st);
567
* Check if ICE negotiation is still running.
569
* @param ice_st The ICE stream transport.
571
* @return PJ_TRUE if ICE session has been created and ICE
572
* negotiation negotiation is in progress.
574
PJ_DECL(pj_bool_t) pj_ice_strans_sess_is_running(pj_ice_strans *ice_st);
578
* Check if ICE negotiation has completed.
580
* @param ice_st The ICE stream transport.
582
* @return PJ_TRUE if ICE session has been created and the
583
* negotiation is complete.
585
PJ_DECL(pj_bool_t) pj_ice_strans_sess_is_complete(pj_ice_strans *ice_st);
589
* Get the current/running component count. If ICE negotiation has not
590
* been started, the number of components will be equal to the number
591
* when the ICE stream transport was created. Once negotiation been
592
* started, the number of components will be the lowest number of
593
* component between local and remote agents.
595
* @param ice_st The ICE stream transport.
597
* @return The running number of components.
599
PJ_DECL(unsigned) pj_ice_strans_get_running_comp_cnt(pj_ice_strans *ice_st);
603
* Get the ICE username fragment and password of the ICE session. The
604
* local username fragment and password can only be retrieved once ICE
605
* session has been created with #pj_ice_strans_init_ice(). The remote
606
* username fragment and password can only be retrieved once ICE session
607
* has been started with #pj_ice_strans_start_ice().
609
* Note that the string returned by this function is only valid throughout
610
* the duration of the ICE session, and the application must not modify
611
* these strings. Once the ICE session has been stopped with
612
* #pj_ice_strans_stop_ice(), the pointer in the string will no longer be
615
* @param ice_st The ICE stream transport.
616
* @param loc_ufrag Optional pointer to receive ICE username fragment
617
* of local endpoint from the ICE session.
618
* @param loc_pwd Optional pointer to receive ICE password of local
619
* endpoint from the ICE session.
620
* @param rem_ufrag Optional pointer to receive ICE username fragment
621
* of remote endpoint from the ICE session.
622
* @param rem_pwd Optional pointer to receive ICE password of remote
623
* endpoint from the ICE session.
625
* @return PJ_SUCCESS if the strings have been retrieved
628
PJ_DECL(pj_status_t) pj_ice_strans_get_ufrag_pwd(pj_ice_strans *ice_st,
636
* Get the number of local candidates for the specified component ID.
638
* @param ice_st The ICE stream transport.
639
* @param comp_id Component ID.
641
* @return The number of candidates.
643
PJ_DECL(unsigned) pj_ice_strans_get_cands_count(pj_ice_strans *ice_st,
647
* Enumerate the local candidates for the specified component.
649
* @param ice_st The ICE stream transport.
650
* @param comp_id Component ID.
651
* @param count On input, it specifies the maximum number of
652
* elements. On output, it will be filled with
653
* the number of candidates copied to the
655
* @param cand Array of candidates.
657
* @return PJ_SUCCESS, or the appropriate error code.
659
PJ_DECL(pj_status_t) pj_ice_strans_enum_cands(pj_ice_strans *ice_st,
662
pj_ice_sess_cand cand[]);
665
* Get the default candidate for the specified component. When this
666
* function is called before ICE negotiation completes, the default
667
* candidate is selected according to local preference criteria. When
668
* this function is called after ICE negotiation completes, the
669
* default candidate is the candidate that forms the valid pair.
671
* @param ice_st The ICE stream transport.
672
* @param comp_id Component ID.
673
* @param cand Pointer to receive the default candidate
676
PJ_DECL(pj_status_t) pj_ice_strans_get_def_cand(pj_ice_strans *ice_st,
678
pj_ice_sess_cand *cand);
681
* Get the current ICE role. ICE session must have been initialized
682
* before this function can be called.
684
* @param ice_st The ICE stream transport.
686
* @return Current ICE role.
688
PJ_DECL(pj_ice_sess_role) pj_ice_strans_get_role(pj_ice_strans *ice_st);
692
* Change session role. This happens for example when ICE session was
693
* created with controlled role when receiving an offer, but it turns out
694
* that the offer contains "a=ice-lite" attribute when the SDP gets
695
* inspected. ICE session must have been initialized before this function
698
* @param ice_st The ICE stream transport.
699
* @param new_role The new role to be set.
701
* @return PJ_SUCCESS on success, or the appropriate error.
703
PJ_DECL(pj_status_t) pj_ice_strans_change_role(pj_ice_strans *ice_st,
704
pj_ice_sess_role new_role);
708
* Start ICE connectivity checks. This function can only be called
709
* after the ICE session has been created in the ICE stream transport
710
* with #pj_ice_strans_init_ice().
712
* This function must be called once application has received remote
713
* candidate list (typically from the remote SDP). This function pairs
714
* local candidates with remote candidates, and starts ICE connectivity
715
* checks. The ICE session/transport will then notify the application
716
* via the callback when ICE connectivity checks completes, either
717
* successfully or with failure.
719
* @param ice_st The ICE stream transport.
720
* @param rem_ufrag Remote ufrag, as seen in the SDP received from
722
* @param rem_passwd Remote password, as seen in the SDP received from
724
* @param rcand_cnt Number of remote candidates in the array.
725
* @param rcand Remote candidates array.
727
* @return PJ_SUCCESS, or the appropriate error code.
729
PJ_DECL(pj_status_t) pj_ice_strans_start_ice(pj_ice_strans *ice_st,
730
const pj_str_t *rem_ufrag,
731
const pj_str_t *rem_passwd,
733
const pj_ice_sess_cand rcand[]);
736
* Retrieve the candidate pair that has been nominated and successfully
737
* checked for the specified component. If ICE negotiation is still in
738
* progress or it has failed, this function will return NULL.
740
* @param ice_st The ICE stream transport.
741
* @param comp_id Component ID.
743
* @return The valid pair as ICE checklist structure if the
746
PJ_DECL(const pj_ice_sess_check*)
747
pj_ice_strans_get_valid_pair(const pj_ice_strans *ice_st,
751
* Stop and destroy the ICE session inside this media transport. Application
752
* needs to call this function once the media session is over (the call has
753
* been disconnected).
755
* Application MAY reuse this ICE stream transport for subsequent calls.
756
* In this case, it must call #pj_ice_strans_stop_ice() when the call is
757
* disconnected, and reinitialize the ICE stream transport for subsequent
758
* call with #pj_ice_strans_init_ice()/#pj_ice_strans_start_ice(). In this
759
* case, the ICE stream transport will maintain the internal sockets and
760
* continue to send STUN keep-alive packets and TURN Refresh request to
761
* keep the NAT binding/TURN allocation open and to detect change in STUN
764
* If application does not want to reuse the ICE stream transport for
765
* subsequent calls, it must call #pj_ice_strans_destroy() to destroy the
766
* ICE stream transport altogether.
768
* @param ice_st The ICE stream transport.
770
* @return PJ_SUCCESS, or the appropriate error code.
772
PJ_DECL(pj_status_t) pj_ice_strans_stop_ice(pj_ice_strans *ice_st);
776
* Send outgoing packet using this transport.
777
* Application can send data (normally RTP or RTCP packets) at any time
778
* by calling this function. This function takes a destination
779
* address as one of the arguments, and this destination address should
780
* be taken from the default transport address of the component (that is
781
* the address in SDP c= and m= lines, or in a=rtcp attribute).
782
* If ICE negotiation is in progress, this function will send the data
783
* to the destination address. Otherwise if ICE negotiation has completed
784
* successfully, this function will send the data to the nominated remote
785
* address, as negotiated by ICE.
787
* @param ice_st The ICE stream transport.
788
* @param comp_id Component ID.
789
* @param data The data or packet to be sent.
790
* @param data_len Size of data or packet, in bytes.
791
* @param dst_addr The destination address.
792
* @param dst_addr_len Length of destination address.
794
* @return PJ_SUCCESS if data is sent successfully.
796
PJ_DECL(pj_status_t) pj_ice_strans_sendto(pj_ice_strans *ice_st,
800
const pj_sockaddr_t *dst_addr,
813
#endif /* __PJNATH_ICE_STRANS_H__ */