Commit dc78a1c8 authored by Sébastien Blin's avatar Sébastien Blin Committed by Adrien Béraud

turn: fix first packet detection

This patch fix the file transfer (invalid state in
TlsSession::waitForReady).

Also, add peer connection with on_connection_status instead of
on_connection_attempt (because the connection can fail)

Change-Id: Iae97cb4b4a206024aea64d10e99d9c2150e7c04c
Gitlab: #626
parent 094fab88
......@@ -35,6 +35,7 @@ bash -c "%PATCH_CMD% %UNIXPATH%pjproject/fix_ioqueue_ipv6_sendto.patch"
bash -c "%PATCH_CMD% %UNIXPATH%pjproject/add_dtls_transport.patch"
bash -c "%PATCH_CMD% %UNIXPATH%pjproject/rfc6544.patch"
bash -c "%PATCH_CMD% %UNIXPATH%pjproject/ice_config.patch"
bash -c "%PATCH_CMD% %UNIXPATH%pjproject/fix_first_packet_turn_tcp.patch"
%APPLY_CMD% %SRC%\pjproject\win32_vs_gnutls.patch
%APPLY_CMD% %SRC%\pjproject\win_config.patch
......
pjnath/src/pjnath/turn_sock.c | 64 +++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 33 insertions(+), 31 deletions(-)
diff --git a/pjnath/src/pjnath/turn_sock.c b/pjnath/src/pjnath/turn_sock.c
index 7033fd93..6a3a3e31 100644
--- a/pjnath/src/pjnath/turn_sock.c
+++ b/pjnath/src/pjnath/turn_sock.c
@@ -1052,38 +1052,40 @@ static pj_bool_t dataconn_on_data_read(pj_activesock_t *asock,
return PJ_FALSE;
}
- if (conn->state == DATACONN_STATE_READY) {
- /* Application data */
- if (turn_sock->cb.on_rx_data) {
- (*turn_sock->cb.on_rx_data)(turn_sock, data, size,
- &conn->peer_addr,
- conn->peer_addr_len);
+ *remainder = size;
+ while (*remainder > 0) {
+ if (conn->state == DATACONN_STATE_READY) {
+ /* Application data */
+ if (turn_sock->cb.on_rx_data) {
+ (*turn_sock->cb.on_rx_data)(turn_sock, data, *remainder,
+ &conn->peer_addr,
+ conn->peer_addr_len);
+ }
+ *remainder = 0;
+ } else if (conn->state == DATACONN_STATE_CONN_BINDING) {
+ /* Waiting for ConnectionBind response */
+ pj_bool_t is_stun;
+ pj_turn_session_on_rx_pkt_param prm;
+
+ /* Ignore if this is not a STUN message */
+ is_stun = ((((pj_uint8_t*)data)[0] & 0xC0) == 0);
+ if (!is_stun)
+ goto on_return;
+
+ pj_bzero(&prm, sizeof(prm));
+ prm.pkt = data;
+ prm.pkt_len = *remainder;
+ prm.src_addr = &conn->peer_addr;
+ prm.src_addr_len = conn->peer_addr_len;
+ pj_turn_session_on_rx_pkt2(conn->turn_sock->sess, &prm);
+ /* Got remainder? */
+ if (prm.parsed_len < *remainder && prm.parsed_len > 0) {
+ pj_memmove(data, (pj_uint8_t*)data+prm.parsed_len, *remainder);
+ }
+ *remainder -= prm.parsed_len;
+ } else
+ goto on_return;
}
- } else if (conn->state == DATACONN_STATE_CONN_BINDING) {
- /* Waiting for ConnectionBind response */
- pj_bool_t is_stun;
- pj_turn_session_on_rx_pkt_param prm;
-
- /* Ignore if this is not a STUN message */
- is_stun = ((((pj_uint8_t*)data)[0] & 0xC0) == 0);
- if (!is_stun)
- goto on_return;
-
- pj_bzero(&prm, sizeof(prm));
- prm.pkt = data;
- prm.pkt_len = size;
- prm.src_addr = &conn->peer_addr;
- prm.src_addr_len = conn->peer_addr_len;
- pj_turn_session_on_rx_pkt2(conn->turn_sock->sess, &prm);
- /* Got remainder? */
- if (prm.parsed_len < size) {
- *remainder = size - prm.parsed_len;
- if (prm.parsed_len) {
- pj_memmove(data, (pj_uint8_t*)data+prm.parsed_len,
- *remainder);
- }
- }
- }
on_return:
pj_grp_lock_release(turn_sock->grp_lock);
......@@ -64,6 +64,7 @@ endif
$(APPLY) $(SRC)/pjproject/rfc6544.patch
$(APPLY) $(SRC)/pjproject/ice_config.patch
$(APPLY) $(SRC)/pjproject/sip_config.patch
$(APPLY) $(SRC)/pjproject/fix_first_packet_turn_tcp.patch
$(UPDATE_AUTOCONFIG)
$(MOVE)
......
......@@ -162,7 +162,7 @@ public:
void onTurnState(pj_turn_state_t old_state, pj_turn_state_t new_state);
void onRxData(const uint8_t* pkt, unsigned pkt_len, const pj_sockaddr_t* peer_addr, unsigned addr_len);
pj_status_t onPeerConnection(pj_uint32_t conn_id, const pj_sockaddr_t* peer_addr, unsigned addr_len);
void onPeerConnection(pj_uint32_t conn_id, const pj_sockaddr_t* peer_addr, unsigned addr_len, pj_status_t status);
void ioJob();
std::mutex apiMutex_;
......@@ -238,22 +238,24 @@ TurnTransportPimpl::onRxData(const uint8_t* pkt, unsigned pkt_len,
(channel_it->second) << std::string(reinterpret_cast<const char*>(pkt), pkt_len);
}
pj_status_t
void
TurnTransportPimpl::onPeerConnection(pj_uint32_t conn_id,
const pj_sockaddr_t* addr, unsigned addr_len)
const pj_sockaddr_t* addr, unsigned addr_len,
pj_status_t status)
{
IpAddr peer_addr (*static_cast<const pj_sockaddr*>(addr), addr_len);
JAMI_DBG() << "Received connection attempt from "
<< peer_addr.toString(true, true) << ", id=" << std::hex
<< conn_id;
{
MutexGuard lk {apiMutex_};
peerChannels_.emplace(peer_addr, PeerChannel {});
if (status == PJ_SUCCESS) {
JAMI_DBG() << "Received connection attempt from "
<< peer_addr.toString(true, true) << ", id=" << std::hex
<< conn_id;
{
MutexGuard lk {apiMutex_};
peerChannels_.emplace(peer_addr, PeerChannel {});
}
}
if (settings.onPeerConnection)
settings.onPeerConnection(conn_id, peer_addr, true);
return PJ_SUCCESS;
settings.onPeerConnection(conn_id, peer_addr, status == PJ_SUCCESS);
}
void
......@@ -314,12 +316,13 @@ TurnTransport::TurnTransport(const TurnTransportParams& params)
auto pimpl = static_cast<TurnTransportPimpl*>(pj_turn_sock_get_user_data(relay));
pimpl->onTurnState(old_state, new_state);
};
relay_cb.on_connection_attempt = [](pj_turn_sock *relay,
relay_cb.on_connection_status = [](pj_turn_sock *relay,
pj_status_t status,
pj_uint32_t conn_id,
const pj_sockaddr_t *peer_addr,
unsigned addr_len) {
auto pimpl = static_cast<TurnTransportPimpl *>(pj_turn_sock_get_user_data(relay));
return pimpl->onPeerConnection(conn_id, peer_addr, addr_len);
pimpl->onPeerConnection(conn_id, peer_addr, addr_len, status);
};
// TURN socket config
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment