Skip to content

Commit addf02c

Browse files
authored
Clamp HEP capture payload to the buffer instead of dropping chunks (#321)
The capture builders broke out of the copy loop at the first iov fragment that would exceed the buffer cap, before copying any of it, so an oversize message lost everything from that fragment on. A UDP datagram arrives as a single fragment, so the whole message was dropped and the HEP frame carried an empty payload; a TCP read arrives as several fragments, so some were kept and the tail lost. Clamp each fragment's copy length to the remaining buffer room instead of dropping it, in both the HEPv2 and HEPv3 builders, filling up to the cap. This also fixes an over-declared HEPv3 length: the sizing and copy passes counted different totals against the cap, so `header.length` could exceed the bytes actually sent. The copy pass now emits exactly the counted `payload_len`, keeping declared and sent length in sync.
1 parent bd16af3 commit addf02c

1 file changed

Lines changed: 34 additions & 30 deletions

File tree

libsofia-sip-ua/tport/tport_logging.c

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -602,17 +602,19 @@ int tport_capt_msg_hepv2 (tport_t const *self, msg_t *msg, size_t n,
602602
buflen += sizeof(struct hep_timehdr);
603603
}
604604

605-
for (i = 0; i < iovused && n > 0; i++) {
605+
for (i = 0; i < iovused && n > 0 && buflen < capt_bufsize; i++) {
606606
size_t len = iov[i].mv_len;
607+
size_t room = capt_bufsize - buflen;
608+
607609
if (len > n)
608-
len = n;
609-
/* if the packet too big for us */
610-
if((buflen + len) > capt_bufsize)
611-
break;
612-
613-
memcpy(*buffer + buflen , (void*)iov[i].mv_base, len);
614-
buflen +=len;
615-
n -= len;
610+
len = n;
611+
/* clamp to remaining capture buffer */
612+
if (len > room)
613+
len = room;
614+
615+
memcpy(*buffer + buflen, (void*)iov[i].mv_base, len);
616+
buflen += len;
617+
n -= len;
616618
}
617619

618620
return buflen;
@@ -638,8 +640,7 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
638640
su_time_t now;
639641
hep_chunk_ip4_t src_ip4 = {{0}}, dst_ip4 = {{0}};
640642
hep_chunk_t payload_chunk;
641-
int orig_n = 0;
642-
643+
643644
#if SU_HAVE_IN6
644645
hep_chunk_ip6_t src_ip6 = {{0}}, dst_ip6 = {{0}};
645646
#endif
@@ -785,16 +786,19 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
785786

786787

787788
/* Payload caclulation */
788-
orig_n = n;
789-
for (i = 0; i < iovused && n > 0; i++) {
790-
size_t len = iov[i].mv_len;
791-
if (len > n) len = n;
792-
if((payload_len + len) > capt_bufsize) break;
793-
payload_len +=len;
794-
n -= len;
789+
for (i = 0; i < iovused && n > 0 && payload_len < capt_bufsize; i++) {
790+
size_t len = iov[i].mv_len;
791+
size_t room = capt_bufsize - payload_len;
792+
793+
if (len > n)
794+
len = n;
795+
/* clamp to remaining capture buffer instead of dropping the chunk */
796+
if (len > room)
797+
len = room;
798+
799+
payload_len += len;
800+
n -= len;
795801
}
796-
/* restore n */
797-
n = orig_n;
798802

799803
/* Payload */
800804
payload_chunk.vendor_id = htons(0x0000);
@@ -841,17 +845,17 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
841845
memcpy((char*) *buffer+buflen, &payload_chunk, sizeof(struct hep_chunk));
842846
buflen += sizeof(struct hep_chunk);
843847

844-
/* PAYLOAD */
845-
for (i = 0; i < iovused && n > 0; i++) {
848+
/* PAYLOAD: copy the payload_len bytes counted above (keeps buflen == tlen).
849+
* payload_len is already folded into tlen/header.length, so consume it. */
850+
for (i = 0; i < iovused && payload_len > 0; i++) {
846851
size_t len = iov[i].mv_len;
847-
if (len > n) len = n;
848-
/* if the packet too big for us */
849-
if((buflen + len) > capt_bufsize)
850-
break;
851-
852-
memcpy(*buffer + buflen , (void*)iov[i].mv_base, len);
853-
buflen +=len;
854-
n -= len;
852+
853+
if (len > payload_len)
854+
len = payload_len;
855+
856+
memcpy(*buffer + buflen, (void*)iov[i].mv_base, len);
857+
buflen += len;
858+
payload_len -= len;
855859
}
856860

857861
free(hg);

0 commit comments

Comments
 (0)