Skip to content

Commit

Permalink
Fix more traceucast / ndagtcp bugs
Browse files Browse the repository at this point in the history
 * fix bugs that occur when handling a packet larger than 10K
   bytes (both in traceucast and in the ndagtcp receiving
   code).
 * fix lock-up when trying to halt a program that is reading
   from an ndagtcp input.
 * fix erroneous "Malformed beacon" message when reconnecting
   a disconnected ndagtcp input.
 * fix "getaddrinfo" memory leak in traceucast.
  • Loading branch information
salcock committed Dec 4, 2023
1 parent 34ce09d commit 92531cb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 5 additions & 6 deletions lib/format_ndag.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include "format_ndag.h"

#define NDAG_IDLE_TIMEOUT (600)
#define ENCAP_BUFSIZE (10000)
#define ENCAP_BUFSIZE (65536)
#define CTRL_BUF_SIZE (10000)
#define ENCAP_BUFFERS (1000)

Expand Down Expand Up @@ -440,7 +440,7 @@ static int ndag_parse_control_message(libtrace_t *libtrace, char *msgbuf,
numstreams = ntohs(*ptr);
ptr ++;

if ((uint32_t)msgsize != ((numstreams + 1) * sizeof(uint16_t)))
if ((uint32_t)msgsize < ((numstreams + 1) * sizeof(uint16_t)))
{
fprintf(stderr, "Malformed beacon (length doesn't match number of streams).\n");
fprintf(stderr, "%u %u\n", msgsize, numstreams);
Expand Down Expand Up @@ -556,7 +556,7 @@ static int accept_ndagtcp_connection(libtrace_t *libtrace,

fcntl(sock, F_SETFL, O_NONBLOCK);

while (is_halted(libtrace) == -1) {
while (is_halted(libtrace) == -1 && !ndag_paused) {
r = select_on_sock(sock);
if (r < 0) {
fprintf(stderr, "Error in select while accepting connection on socket for %s:%s -- %s\n",
Expand Down Expand Up @@ -1098,7 +1098,7 @@ static int ndag_prepare_packet_stream_encaperf(libtrace_t *restrict libtrace,
nr = ssock->nextreadind;
available = ssock->savedsize[nr] - (ssock->nextread - ssock->saved[nr]);

if (ssock->nextrlen == 0 || ssock->nextrlen > ENCAP_BUFSIZE) {
if (ssock->nextrlen == 0) {
return -1;
}

Expand Down Expand Up @@ -1175,7 +1175,7 @@ static int ndag_prepare_packet_stream_corsarotag(libtrace_t *restrict libtrace,
nr = ssock->nextreadind;
available = ssock->savedsize[nr] - (ssock->nextread - ssock->saved[nr]);

if (ssock->nextrlen == 0 || ssock->nextrlen > ENCAP_BUFSIZE) {
if (ssock->nextrlen == 0) {
return -1;
}

Expand Down Expand Up @@ -1574,7 +1574,6 @@ static int receive_from_single_socket(streamsock_t *ssock, struct timeval *tv,
ssock->sock = -1;
return 0;
}

ssock->startidle = 0;

ssock->savedsize[ssock->nextwriteind] = ret;
Expand Down
22 changes: 21 additions & 1 deletion tools/tracemcast/traceucast.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ typedef struct read_thread_data {
int streamfd;

uint8_t *pbuffer;
uint32_t bufsize;
ndag_encap_t *encaphdr;
uint8_t *writeptr;
uint32_t seqno;
Expand Down Expand Up @@ -146,6 +147,9 @@ static int create_stream_socket(uint16_t port, char *clientaddr,
return -1;
}
if (targetinfo) {
if (*targetinfo) {
free(*targetinfo);
}
*targetinfo = gotten;
}

Expand Down Expand Up @@ -247,6 +251,7 @@ static void *init_reader_thread(libtrace_t *trace,
rdata->streamport = gparams->firstport + rdata->threadid;
rdata->streamfd = -1;
rdata->pbuffer = calloc(MAX_PACKET_SIZE, sizeof(uint8_t));
rdata->bufsize = MAX_PACKET_SIZE;
rdata->writeptr = rdata->pbuffer;
rdata->seqno = 1;
rdata->target = NULL;
Expand Down Expand Up @@ -514,7 +519,22 @@ static libtrace_packet_t *packet_reader_thread(libtrace_t *trace UNUSED,
}
}

/* append this packet to the buffer (truncate if necessary) */
/* extend the buffer size if we happen to be working with very large
* packets
*/
while (rem + dag_record_size + sizeof(ndag_encap_t) + sizeof(ndag_common_t)
> rdata->bufsize) {
int writeoff = rdata->writeptr - rdata->pbuffer;
int encapoff = ((uint8_t *)rdata->encaphdr) - rdata->pbuffer;

rdata->pbuffer = realloc(rdata->pbuffer,
rdata->bufsize + MAX_PACKET_SIZE);
rdata->bufsize += MAX_PACKET_SIZE;
rdata->writeptr = rdata->pbuffer + writeoff;
rdata->encaphdr = rdata->pbuffer + encapoff;
}

/* append this packet to the buffer */

/* if the buffer is empty, put on a common and encap header on the
* front, before adding any packets */
Expand Down

0 comments on commit 92531cb

Please sign in to comment.