From 25cdcd8b44df2bb4271b19648308f6ecd47d80c2 Mon Sep 17 00:00:00 2001
From: Edric Milaret <edric.ladent-milaret@savoirfairelinux.com>
Date: Tue, 12 Apr 2016 15:49:15 -0400
Subject: [PATCH] sip: fix pkt receive

- Sending profile highlighted a flaw in the processing of packet
  * The receive packet function from pjsip can't handle a packet
    larger than PJSIP_MAX_PKT_LEN (4000) but we were not enforcing
    that correctly
  * The received callback was appending packet to the last one instead
    of appending it to the received packet vector

Change-Id: I603cf19cb2951708edd5cca44d71b77bb83114d6
Tuleap: #538
---
 src/ringdht/sips_transport_ice.cpp | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/ringdht/sips_transport_ice.cpp b/src/ringdht/sips_transport_ice.cpp
index c30f553d78..5146e3ab33 100644
--- a/src/ringdht/sips_transport_ice.cpp
+++ b/src/ringdht/sips_transport_ice.cpp
@@ -319,19 +319,19 @@ SipsIceTransport::handleEvents()
         auto& pck = *it;
         pj_pool_reset(rdata_.tp_info.pool);
         pj_gettimeofday(&rdata_.pkt_info.timestamp);
-        rdata_.pkt_info.len = pck.size();
-        std::copy_n(pck.data(), pck.size(), rdata_.pkt_info.packet);
+        rdata_.pkt_info.len = std::min(pck.size(), (size_t) PJSIP_MAX_PKT_LEN);
+        std::copy_n(pck.data(), rdata_.pkt_info.len, rdata_.pkt_info.packet);
         auto eaten = pjsip_tpmgr_receive_packet(trData_.base.tpmgr, &rdata_);
 
         // Uncomplet parsing? (may be a partial sip packet received)
-        if (eaten != rdata_.pkt_info.len) {
+        if (eaten != (pj_ssize_t)pck.size()) {
             auto npck_it = std::next(it);
             if (npck_it != rx.end()) {
                 // drop current packet, merge reminder with next one
                 auto& npck = *npck_it;
                 npck.insert(npck.begin(), pck.begin()+eaten, pck.end());
             } else {
-                // erase eaten part, keep reminder
+                // erase eaten part, keep remainder
                 pck.erase(pck.begin(), pck.begin()+eaten);
                 {
                     std::lock_guard<std::mutex> l(rxMtx_);
@@ -369,12 +369,7 @@ void
 SipsIceTransport::onRxData(std::vector<uint8_t>&& buf)
 {
     std::lock_guard<std::mutex> l(rxMtx_);
-    if (rxPending_.empty())
-        rxPending_.emplace_back(std::move(buf));
-    else {
-        auto& last = rxPending_.back();
-        last.insert(std::end(last), std::begin(buf), std::end(buf));
-    }
+    rxPending_.emplace_back(std::move(buf));
 }
 
 /* Update local & remote certificates info. This function should be
-- 
GitLab