BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJNATH Reference

Transport independent TURN client session. More...

Data Structures

struct  pj_turn_channel_data
 
struct  pj_turn_session_cb
 
struct  pj_turn_alloc_param
 
struct  pj_turn_session_info
 

Typedefs

typedef struct pj_turn_session pj_turn_session
 

Enumerations

enum  pj_turn_tp_type {
  PJ_TURN_TP_UDP = 17,
  PJ_TURN_TP_TCP = 6,
  PJ_TURN_TP_TLS = 255
}
 
enum  pj_turn_state_t {
  PJ_TURN_STATE_NULL,
  PJ_TURN_STATE_RESOLVING,
  PJ_TURN_STATE_RESOLVED,
  PJ_TURN_STATE_ALLOCATING,
  PJ_TURN_STATE_READY,
  PJ_TURN_STATE_DEALLOCATING,
  PJ_TURN_STATE_DEALLOCATED,
  PJ_TURN_STATE_DESTROYING
}
 

Functions

void pj_turn_alloc_param_default (pj_turn_alloc_param *prm)
 
void pj_turn_alloc_param_copy (pj_pool_t *pool, pj_turn_alloc_param *dst, const pj_turn_alloc_param *src)
 
const char * pj_turn_state_name (pj_turn_state_t state)
 
pj_status_t pj_turn_session_create (const pj_stun_config *cfg, const char *name, int af, pj_turn_tp_type conn_type, pj_grp_lock_t *grp_lock, const pj_turn_session_cb *cb, unsigned options, void *user_data, pj_turn_session **p_sess)
 
pj_status_t pj_turn_session_shutdown (pj_turn_session *sess)
 
pj_status_t pj_turn_session_destroy (pj_turn_session *sess, pj_status_t last_err)
 
pj_status_t pj_turn_session_get_info (pj_turn_session *sess, pj_turn_session_info *info)
 
pj_status_t pj_turn_session_set_user_data (pj_turn_session *sess, void *user_data)
 
void * pj_turn_session_get_user_data (pj_turn_session *sess)
 
pj_grp_lock_tpj_turn_session_get_grp_lock (pj_turn_session *sess)
 
void pj_turn_session_set_log (pj_turn_session *sess, unsigned flags)
 
pj_status_t pj_turn_session_set_software_name (pj_turn_session *sess, const pj_str_t *sw)
 
pj_status_t pj_turn_session_set_server (pj_turn_session *sess, const pj_str_t *domain, int default_port, pj_dns_resolver *resolver)
 
pj_status_t pj_turn_session_set_credential (pj_turn_session *sess, const pj_stun_auth_cred *cred)
 
pj_status_t pj_turn_session_alloc (pj_turn_session *sess, const pj_turn_alloc_param *param)
 
pj_status_t pj_turn_session_set_perm (pj_turn_session *sess, unsigned addr_cnt, const pj_sockaddr addr[], unsigned options)
 
pj_status_t pj_turn_session_sendto (pj_turn_session *sess, const pj_uint8_t *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
 
pj_status_t pj_turn_session_bind_channel (pj_turn_session *sess, const pj_sockaddr_t *peer, unsigned addr_len)
 
pj_status_t pj_turn_session_on_rx_pkt (pj_turn_session *sess, void *pkt, pj_size_t pkt_len, pj_size_t *parsed_len)
 

Detailed Description

The TURN client session is a transport-independent object to manage a client TURN session. It contains the core logic for manage the TURN client session as listed in Overview of TURN operations, but in transport-independent manner (i.e. it doesn't have a socket), so that developer can integrate TURN client functionality into existing framework that already has its own means to send and receive data, or to support new transport types to TURN, such as TLS.

Using the TURN session

These steps describes how to use the TURN session:

  • Creating the session:
    use pj_turn_session_create() to create the session.
  • Configuring credential:
    all TURN operations requires the use of authentication (it uses STUN long term autentication method). Use pj_turn_session_set_credential() to configure the TURN credential to be used by the session.
  • Configuring server:
    application must call pj_turn_session_set_server() before it can send Allocate request (with pj_turn_session_alloc()). This function will resolve the TURN server using DNS SRV resolution if the resolver is set. The server resolution process will complete asynchronously, and application will be notified in on_state() callback of the pj_turn_session_cb structurewith the session state set to PJ_TURN_STATE_RESOLVED.
  • Creating allocation:
    create one "relay port" (or called relayed-transport-address in TURN terminology) in the TURN server by using pj_turn_session_alloc(). This will send Allocate request to the server. This function will complete immediately, and application will be notified about the allocation result in the on_state() callback of the pj_turn_session_cb structure.
  • Getting the allocation result:
    if allocation is successful, the session state will progress to PJ_TURN_STATE_READY, otherwise the state will be PJ_TURN_STATE_DEALLOCATED or higher. Session state progression is reported in the on_state() callback of the pj_turn_session_cb structure. On successful allocation, application may retrieve the allocation info by calling pj_turn_session_get_info().
  • Sending data through the relay.
    Once allocation has been created, client may send data to any remote endpoints (called peers in TURN terminology) via the "relay port". It does so by calling pj_turn_session_sendto(), giving the peer address in the function argument. But note that at this point peers are not allowed to send data towards the client (via the "relay port") before permission is installed for that peer.
  • Creating permissions.
    Permission needs to be created in the TURN server so that a peer can send data to the client via the relay port (a peer in this case is identified by its IP address). Without this, when the TURN server receives data from the peer in the "relay port", it will drop this data. Create the permission by calling pj_turn_session_set_perm(), specifying the peer IP address in the argument (the port part of the address is ignored). More than one IP addresses may be specified.
  • Receiving data from peers.
    Once permission has been installed for the peer, any data received by the TURN server (from that peer) in the "relay port" will be relayed back to client by the server, and application will be notified via on_rx_data callback of the pj_turn_session_cb.
  • Using ChannelData.
    TURN provides optimized framing to the data by using ChannelData packetization. The client activates this format for the specified peer by calling pj_turn_session_bind_channel(). Data sent or received to/for this peer will then use ChannelData format instead of Send or Data Indications.
  • Refreshing the allocation, permissions, and channel bindings.
    Allocations, permissions, and channel bindings will be refreshed by the session automatically when they about to expire.
  • Destroying the allocation.
    Once the "relay port" is no longer needed, client destroys the allocation by calling pj_turn_session_shutdown(). This function will return immediately, and application will be notified about the deallocation result in the on_state() callback of the pj_turn_session_cb structure. Once the state has reached PJ_TURN_STATE_DESTROYING, application must assume that the session will be destroyed shortly after.

Typedef Documentation

Opaque declaration for TURN client session.

Enumeration Type Documentation

TURN session state

Enumerator
PJ_TURN_STATE_NULL 

TURN session has just been created.

PJ_TURN_STATE_RESOLVING 

TURN server has been configured and now is being resolved via DNS SRV resolution.

PJ_TURN_STATE_RESOLVED 

TURN server has been resolved. If there is pending allocation to be done, it will be invoked immediately.

PJ_TURN_STATE_ALLOCATING 

TURN session has issued ALLOCATE request and is waiting for response from the TURN server.

PJ_TURN_STATE_READY 

TURN session has successfully allocated relay resoruce and now is ready to be used.

PJ_TURN_STATE_DEALLOCATING 

TURN session has issued deallocate request and is waiting for a response from the TURN server.

PJ_TURN_STATE_DEALLOCATED 

Deallocate response has been received. Normally the session will proceed to DESTROYING state immediately.

PJ_TURN_STATE_DESTROYING 

TURN session is being destroyed.

TURN transport types, which will be used both to specify the connection type for reaching TURN server and the type of allocation transport to be requested to server (the REQUESTED-TRANSPORT attribute).

Enumerator
PJ_TURN_TP_UDP 

UDP transport, which value corresponds to IANA protocol number.

PJ_TURN_TP_TCP 

TCP transport, which value corresponds to IANA protocol number.

PJ_TURN_TP_TLS 

TLS transport. The TLS transport will only be used as the connection type to reach the server and never as the allocation transport type.

Function Documentation

void pj_turn_alloc_param_copy ( pj_pool_t pool,
pj_turn_alloc_param dst,
const pj_turn_alloc_param src 
)

Duplicate pj_turn_alloc_param.

Parameters
poolPool to allocate memory (currently not used)
dstDestination parameter.
srcSource parameter.
void pj_turn_alloc_param_default ( pj_turn_alloc_param prm)

Initialize pj_turn_alloc_param with the default values.

Parameters
prmThe TURN allocation parameter to be initialized.
pj_status_t pj_turn_session_alloc ( pj_turn_session sess,
const pj_turn_alloc_param param 
)

Allocate a relay address/resource in the TURN server by sending TURN Allocate request. Application must first initiate the server resolution process with pj_turn_session_set_server() and set the credential to be used with pj_turn_session_set_credential() before calling this function.

This function will complete asynchronously, and the application will be notified about the allocation result in on_state() callback. The TURN session state will move to PJ_TURN_STATE_READY if allocation is successful, and PJ_TURN_STATE_DEALLOCATING or greater state if allocation has failed.

Once allocation has been successful, the TURN session will keep this allocation alive until the session is destroyed, by sending periodic allocation refresh to the TURN server.

Parameters
sessThe TURN client session.
paramOptional TURN allocation parameter.
Returns
PJ_SUCCESS if the operation has been successfully initiated or the appropriate error code on failure.
pj_status_t pj_turn_session_bind_channel ( pj_turn_session sess,
const pj_sockaddr_t peer,
unsigned  addr_len 
)

Optionally establish channel binding for the specified a peer address. This function will assign a unique channel number for the peer address and request channel binding to the TURN server for this address. When a channel has been bound to a peer, the TURN client and TURN server will exchange data using ChannelData encapsulation format, which has lower bandwidth overhead than Send Indication (the default format used when peer address is not bound to a channel).

This function will complete asynchronously, and application will be notified about the result in on_channel_bound() callback.

Parameters
sessThe TURN client session.
peerThe remote peer address.
addr_lenLength of the address.
Returns
PJ_SUCCESS if the operation has been successfully initiated, or the appropriate error code on failure.
pj_status_t pj_turn_session_create ( const pj_stun_config cfg,
const char *  name,
int  af,
pj_turn_tp_type  conn_type,
pj_grp_lock_t grp_lock,
const pj_turn_session_cb cb,
unsigned  options,
void *  user_data,
pj_turn_session **  p_sess 
)

Create a TURN session instance with the specified address family and connection type. Once TURN session instance is created, application must call pj_turn_session_alloc() to allocate a relay address in the TURN server.

Parameters
cfgThe STUN configuration which contains among other things the ioqueue and timer heap instance for the operation of this session.
nameOptional name to identify this session in the log.
afAddress family of the client connection. Currently pj_AF_INET() and pj_AF_INET6() are supported.
conn_typeConnection type to the TURN server.
grp_lockOptional group lock object to be used by this session. If this value is NULL, the session will create a group lock internally.
cbCallback to receive events from the TURN session.
optionsOption flags, currently this value must be zero.
user_dataArbitrary application data to be associated with this transport.
p_sessPointer to receive the created instance of the TURN session.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
pj_status_t pj_turn_session_destroy ( pj_turn_session sess,
pj_status_t  last_err 
)

Forcefully destroy the TURN session. This will destroy the session immediately. If there is an active allocation, the server will not be notified about the client destruction.

Parameters
sessThe TURN client session.
last_errOptional error code to be set to the session, which would be returned back in the info parameter of pj_turn_session_get_info(). If this argument value is PJ_SUCCESS, the error code will not be set. If the session already has an error code set, this function will not overwrite that error code either.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
pj_grp_lock_t* pj_turn_session_get_grp_lock ( pj_turn_session sess)

Get the group lock for this TURN session.

Parameters
sessThe TURN client session.
Returns
The group lock.
pj_status_t pj_turn_session_get_info ( pj_turn_session sess,
pj_turn_session_info info 
)

Get the information about this TURN session and the allocation, if any.

Parameters
sessThe TURN client session.
infoThe structure to be initialized with the TURN session info.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
void* pj_turn_session_get_user_data ( pj_turn_session sess)

Retrieve the previously assigned user data associated with this TURN session.

Parameters
sessThe TURN client session.
Returns
The user/application data.
pj_status_t pj_turn_session_on_rx_pkt ( pj_turn_session sess,
void *  pkt,
pj_size_t  pkt_len,
pj_size_t parsed_len 
)

Notify TURN client session upon receiving a packet from server. Since the TURN session is transport independent, it does not read packet from any sockets, and rather relies on application giving it packets that are received from the TURN server. The session then processes this packet and decides whether it is part of TURN protocol exchange or if it is a data to be reported back to user, which in this case it will call the on_rx_data() callback.

Parameters
sessThe TURN client session.
pktThe packet as received from the TURN server. This should contain either STUN encapsulated message or a ChannelData packet.
pkt_lenThe length of the packet.
parsed_lenOptional argument to receive the number of parsed or processed data from the packet.
Returns
The function may return non-PJ_SUCCESS if it receives non-STUN and non-ChannelData packet, or if the on_rx_data() returns non-PJ_SUCCESS;
pj_status_t pj_turn_session_sendto ( pj_turn_session sess,
const pj_uint8_t pkt,
unsigned  pkt_len,
const pj_sockaddr_t peer_addr,
unsigned  addr_len 
)

Send a data to the specified peer address via the TURN relay. This function will encapsulate the data as STUN Send Indication or TURN ChannelData packet and send the message to the TURN server. The TURN server then will send the data to the peer.

The allocation (pj_turn_session_alloc()) must have been successfully created before application can relay any data.

Since TURN session is transport independent, this function will ultimately call on_send_pkt() callback to request the application to actually send the packet containing the data to the TURN server.

Parameters
sessThe TURN client session.
pktThe data/packet to be sent to peer.
pkt_lenLength of the data.
peer_addrThe remote peer address (the ultimate destination of the data, and not the TURN server address).
addr_lenLength of the address.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
pj_status_t pj_turn_session_set_credential ( pj_turn_session sess,
const pj_stun_auth_cred cred 
)

Set credential to be used to authenticate against TURN server. Application must call this function before sending Allocate request with pj_turn_session_alloc().

Parameters
sessThe TURN client session
credSTUN credential to be used.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
void pj_turn_session_set_log ( pj_turn_session sess,
unsigned  flags 
)

Configure message logging. By default all flags are enabled.

Parameters
sessThe TURN client session.
flagsBitmask combination of pj_stun_sess_msg_log_flag
pj_status_t pj_turn_session_set_perm ( pj_turn_session sess,
unsigned  addr_cnt,
const pj_sockaddr  addr[],
unsigned  options 
)

Create or renew permission in the TURN server for the specified peer IP addresses. Application must install permission for a particular (peer) IP address before it sends any data to that IP address, or otherwise the TURN server will drop the data.

Parameters
sessThe TURN client session.
addr_cntNumber of IP addresses.
addrArray of peer IP addresses. Only the address family and IP address portion of the socket address matter.
optionsSpecify 1 to let the TURN client session automatically renew the permission later when they are about to expire.
Returns
PJ_SUCCESS if the operation has been successfully issued, or the appropriate error code. Note that the operation itself will complete asynchronously.
pj_status_t pj_turn_session_set_server ( pj_turn_session sess,
const pj_str_t domain,
int  default_port,
pj_dns_resolver resolver 
)

Set the server or domain name of the server. Before the application can send Allocate request (with pj_turn_session_alloc()), it must first resolve the server address(es) using this function. This function will resolve the TURN server using DNS SRV resolution if the resolver is set. The server resolution process will complete asynchronously, and application will be notified in on_state() callback with the session state set to PJ_TURN_STATE_RESOLVED.

Application may call with pj_turn_session_alloc() before the server resolution completes. In this case, the operation will be queued by the session, and it will be sent once the server resolution completes.

Parameters
sessThe TURN client session.
domainThe domain, hostname, or IP address of the TURN server. When this parameter contains domain name, the resolver parameter must be set to activate DNS SRV resolution.
default_portThe default TURN port number to use when DNS SRV resolution is not used. If DNS SRV resolution is used, the server port number will be set from the DNS SRV records.
resolverIf this parameter is not NULL, then the domain parameter will be first resolved with DNS SRV and then fallback to using DNS A/AAAA resolution when DNS SRV resolution fails. If this parameter is NULL, the domain parameter will be resolved as hostname.
Returns
PJ_SUCCESS if the operation has been successfully queued, or the appropriate error code on failure. When this function returns PJ_SUCCESS, the final result of the resolution process will be notified to application in on_state() callback.
pj_status_t pj_turn_session_set_software_name ( pj_turn_session sess,
const pj_str_t sw 
)

Configure the SOFTWARE name to be sent in all STUN requests by the TURN session.

Parameters
sessThe TURN client session.
swSoftware name string. If this argument is NULL or empty, the session will not include SOFTWARE attribute in STUN requests and responses.
Returns
PJ_SUCCESS on success, or the appropriate error code.
pj_status_t pj_turn_session_set_user_data ( pj_turn_session sess,
void *  user_data 
)

Associate a user data with this TURN session. The user data may then be retrieved later with pj_turn_session_get_user_data().

Parameters
sessThe TURN client session.
user_dataArbitrary data.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
pj_status_t pj_turn_session_shutdown ( pj_turn_session sess)

Shutdown TURN client session. This will gracefully deallocate and destroy the client session.

Parameters
sessThe TURN client session.
Returns
PJ_SUCCESS if the operation has been successful, or the appropriate error code on failure.
const char* pj_turn_state_name ( pj_turn_state_t  state)

Get string representation for the given TURN state.

Parameters
stateThe TURN session state.
Returns
The state name as NULL terminated string.

 


PJNATH - Open Source NAT traversal helper library supporting STUN, TURN, and ICE
Copyright (C) 2006-2009 Teluu Inc.