Skip to content
Permalink
Browse files

Close #2197: Support TURN extensions for TCP allocations (RFC 6062).

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@5987 74dad513-b988-da41-8d7b-12977e46ad98
  • Loading branch information...
nanangizz committed May 14, 2019
1 parent 2a8a1ff commit fa6616c43c7e19797084f4e02a67d1fb6fd99473
@@ -220,6 +220,13 @@
# define PJ_TURN_KEEP_ALIVE_SEC 15
#endif

/**
* Maximum number of TCP data connection to peer(s) that a TURN client can
* open/accept for each TURN allocation (or TURN control connection).
*/
#ifndef PJ_TURN_MAX_TCP_CONN_CNT
# define PJ_TURN_MAX_TCP_CONN_CNT 8
#endif

/* **************************************************************************
* ICE CONFIGURATION
@@ -92,6 +92,21 @@ enum pj_stun_method_e
*/
PJ_STUN_CHANNEL_BIND_METHOD = 9,

/**
* STUN/TURN Connect as defined by RFC 6062
*/
PJ_STUN_CONNECT_METHOD = 10,

/**
* STUN/TURN ConnectionBind as defined by RFC 6062
*/
PJ_STUN_CONNECTION_BIND_METHOD = 11,

/**
* STUN/TURN ConnectionAttempt as defined by RFC 6062
*/
PJ_STUN_CONNECTION_ATTEMPT_METHOD = 12,

/**
* All known methods.
*/
@@ -291,7 +306,18 @@ typedef enum pj_stun_msg_type
/**
* Error response to STUN ChannelBind request.
*/
PJ_STUN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119
PJ_STUN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,


/**
* STUN/TURN ConnectBind Request
*/
PJ_STUN_CONNECTION_BIND_REQUEST = 0x000b,

/**
* TURN ConnectionAttempt indication
*/
PJ_STUN_CONNECTION_ATTEMPT_INDICATION = 0x001c,

} pj_stun_msg_type;

@@ -333,6 +359,7 @@ typedef enum pj_stun_attr_type
PJ_STUN_ATTR_XOR_REFLECTED_FROM = 0x0023,/**< XOR-REFLECTED-FROM */
PJ_STUN_ATTR_PRIORITY = 0x0024,/**< PRIORITY */
PJ_STUN_ATTR_USE_CANDIDATE = 0x0025,/**< USE-CANDIDATE */
PJ_STUN_ATTR_CONNECTION_ID = 0x002a,/**< CONNECTION-ID */
PJ_STUN_ATTR_ICMP = 0x0030,/**< ICMP (TURN) */

PJ_STUN_ATTR_END_MANDATORY_ATTR,
@@ -298,6 +298,43 @@ typedef struct pj_turn_session_cb
pj_turn_state_t old_state,
pj_turn_state_t new_state);

/**
* Notification when TURN client received a ConnectionAttempt Indication
* from the TURN server, which indicates that peer initiates a TCP
* connection to allocated slot in the TURN server. Application must
* implement this callback if it uses RFC 6062 (TURN TCP allocations).
*
* After receiving this callback, application should establish a new TCP
* connection to the TURN server and send ConnectionBind request (using
* pj_turn_session_connection_bind()). After the connection binding
* succeeds, this new connection will become a data only connection.
*
* @param sess The TURN session.
* @param conn_id The connection ID assigned by TURN server.
* @param peer_addr Peer address that tried to connect to the TURN server.
* @param addr_len Length of the peer address.
*/
void (*on_connection_attempt)(pj_turn_session *sess,
pj_uint32_t conn_id,
const pj_sockaddr_t *peer_addr,
unsigned addr_len);

/**
* Notification for ConnectionBind request sent using
* pj_turn_session_connection_bind().
*
* @param sess The TURN session.
* @param status The status code.
* @param conn_id The connection ID.
* @param peer_addr Peer address.
* @param addr_len Length of the peer address.
*/
void (*on_connection_bind_status)(pj_turn_session *sess,
pj_status_t status,
pj_uint32_t conn_id,
const pj_sockaddr_t *peer_addr,
unsigned addr_len);

} pj_turn_session_cb;


@@ -339,6 +376,13 @@ typedef struct pj_turn_alloc_param
*/
int af;

/**
* Type of connection to from TURN server to peer. Supported values are
* PJ_TURN_TP_UDP (RFC 5766) and PJ_TURN_TP_TCP (RFC 6062)
*
* Default is PJ_TURN_TP_UDP.
*/
pj_turn_tp_type peer_conn_type;

} pj_turn_alloc_param;

@@ -386,6 +430,40 @@ typedef struct pj_turn_session_info
} pj_turn_session_info;


/**
* Parameters for function pj_turn_session_on_rx_pkt2().
*/
typedef struct pj_turn_session_on_rx_pkt_param
{
/**
* The packet as received from the TURN server. This should contain
* either STUN encapsulated message or a ChannelData packet.
*/
void *pkt;

/**
* The length of the packet.
*/
pj_size_t pkt_len;

/**
* The number of parsed or processed data from the packet.
*/
pj_size_t parsed_len;

/**
* Source address where the packet is received from.
*/
const pj_sockaddr_t *src_addr;

/**
* Length of the source address.
*/
unsigned src_addr_len;

} pj_turn_session_on_rx_pkt_param;


/**
* Initialize pj_turn_alloc_param with the default values.
*
@@ -741,6 +819,56 @@ PJ_DECL(pj_status_t) pj_turn_session_on_rx_pkt(pj_turn_session *sess,
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
* \a on_rx_data() callback.
*
* This function is variant of pj_turn_session_on_rx_pkt() with additional
* parameters such as source address. Source address will allow STUN/TURN
* session to resend the request (e.g: with updated authentication) to the
* provided source address which may be different to the initial connection,
* for example in RFC 6062 scenario that there can be some data connection
* and a control connection.
*
* @param sess The TURN client session.
* @param prm The function parameters, e.g: packet, source address.
*
* @return The function may return non-PJ_SUCCESS if it receives
* non-STUN and non-ChannelData packet, or if the
* \a on_rx_data() returns non-PJ_SUCCESS;
*/
PJ_DECL(pj_status_t) pj_turn_session_on_rx_pkt2(
pj_turn_session *sess,
pj_turn_session_on_rx_pkt_param *prm);

/**
* Initiate connection binding to the specified peer using ConnectionBind
* request. Application must call this function when it uses RFC 6062
* (TURN TCP allocations) to establish a data connection with peer after
* opening/accepting connection to/from peer. The connection binding status
* will be notified via on_connection_bind_status callback.
*
* @param sess The TURN session.
* @param pool The memory pool.
* @param conn_id The connection ID assigned by TURN server.
* @param peer_addr Peer address.
* @param addr_len Length of the peer address.
*
* @return PJ_SUCCESS if the operation has been successfully
* issued, or the appropriate error code. Note that
* the operation itself will complete asynchronously.
*/
PJ_DECL(pj_status_t) pj_turn_session_connection_bind(
pj_turn_session *sess,
pj_pool_t *pool,
pj_uint32_t conn_id,
const pj_sockaddr_t *peer_addr,
unsigned addr_len);

/**
* @}
@@ -98,6 +98,49 @@ typedef struct pj_turn_sock_cb
pj_turn_state_t old_state,
pj_turn_state_t new_state);

/**
* Notification when TURN client received a ConnectionAttempt Indication
* from the TURN server, which indicates that peer initiates a TCP
* connection to allocated slot in the TURN server. Application should
* implement this callback if it uses RFC 6062 (TURN TCP allocations),
* otherwise TURN client will automatically accept it.
*
* If application accepts the peer connection attempt (i.e: by returning
* PJ_SUCCESS or not implementing this callback), the TURN socket will
* initiate a new connection to the TURN server and send ConnectionBind
* request, and eventually will notify application via
* on_connection_status callback, if implemented.
*
* @param turn_sock The TURN client transport.
* @param conn_id The connection ID assigned by TURN server.
* @param peer_addr Peer address that tried to connect to the
* TURN server.
* @param addr_len Length of the peer address.
*
* @return The callback must return PJ_SUCCESS to accept
* the connection attempt.
*/
pj_status_t (*on_connection_attempt)(pj_turn_sock *turn_sock,
pj_uint32_t conn_id,
const pj_sockaddr_t *peer_addr,
unsigned addr_len);

/**
* Notification for initiated TCP data connection to peer (RFC 6062),
* for example after peer connection attempt is accepted.
*
* @param turn_sock The TURN client transport.
* @param status The status code.
* @param conn_id The connection ID.
* @param peer_addr Peer address.
* @param addr_len Length of the peer address.
*/
void (*on_connection_status)(pj_turn_sock *turn_sock,
pj_status_t status,
pj_uint32_t conn_id,
const pj_sockaddr_t *peer_addr,
unsigned addr_len);

} pj_turn_sock_cb;


@@ -45,6 +45,9 @@ static const char *stun_method_names[PJ_STUN_METHOD_MAX] =
"Data", /* 7 */
"CreatePermission", /* 8 */
"ChannelBind", /* 9 */
"Connect", /* 10 */
"ConnectionBind", /* 11 */
"ConnectionAttempt", /* 12 */
};

static struct
@@ -476,11 +479,11 @@ static struct attr_desc mandatory_attr_desc[] =
NULL
},
{
/* ID 0x002a is not assigned */
NULL,
NULL,
NULL,
NULL
/* PJ_STUN_ATTR_CONNECTION_ID, */
"CONNECTION-ID",
&decode_uint_attr,
&encode_uint_attr,
&clone_uint_attr
},
{
/* ID 0x002b is not assigned */

0 comments on commit fa6616c

Please sign in to comment.
You can’t perform that action at this time.