From 4dd074630b00cdee551b2eefcd7c66491306250c Mon Sep 17 00:00:00 2001 From: skarg Date: Mon, 27 Oct 2008 19:53:11 +0000 Subject: [PATCH] Corrected the MS/TP capture to limit data to the input buffer. --- bacnet-stack/demo/mstpcap/main.c | 4 +++- bacnet-stack/ports/linux/mstpsnap.c | 14 ++++++++------ bacnet-stack/ports/linux/rx_fsm.c | 6 ++++-- bacnet-stack/ports/win32/rx_fsm.c | 3 ++- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/bacnet-stack/demo/mstpcap/main.c b/bacnet-stack/demo/mstpcap/main.c index 4f191ae6..4e100ceb 100644 --- a/bacnet-stack/demo/mstpcap/main.c +++ b/bacnet-stack/demo/mstpcap/main.c @@ -190,6 +190,7 @@ static void write_received_packet( uint32_t orig_len; /* actual length of packet */ uint8_t header[8]; /* MS/TP header */ struct timeval tv; + size_t max_data; if (pFile) { gettimeofday(&tv, NULL); @@ -214,7 +215,8 @@ static void write_received_packet( header[7] = mstp_port->HeaderCRCActual; fwrite(header, sizeof(header), 1, pFile); if (mstp_port->DataLength) { - fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile); + max_data = min(mstp_port->InputBufferSize,mstp_port->DataLength); + fwrite(mstp_port->InputBuffer, max_data, 1, pFile); fwrite((char *) &mstp_port->DataCRCActualMSB, 1, 1, pFile); fwrite((char *) &mstp_port->DataCRCActualLSB, 1, 1, pFile); } diff --git a/bacnet-stack/ports/linux/mstpsnap.c b/bacnet-stack/ports/linux/mstpsnap.c index 8c22b843..6da74893 100644 --- a/bacnet-stack/ports/linux/mstpsnap.c +++ b/bacnet-stack/ports/linux/mstpsnap.c @@ -165,7 +165,8 @@ static void snap_received_packet( { uint16_t mtu_len = 0; /* number of octets of packet saved in file */ unsigned i = 0; /* counter */ - static uint8_t mtu[1500] = { 0 }; + static uint8_t mtu[1500] = { 0 }; + size_t max_data = 0; mtu[0] = 0; mtu[1] = 0; @@ -198,13 +199,14 @@ static void snap_received_packet( mtu[29] = LO_BYTE(mstp_port->DataLength); mtu[30] = mstp_port->HeaderCRCActual; mtu_len = 31; - if (mstp_port->DataLength) { - for (i = 0; i < mstp_port->DataLength; i++) { + if (mstp_port->DataLength) { + max_data = min(mstp_port->InputBufferSize, mstp_port->DataLength); + for (i = 0; i < max_data; i++) { mtu[31 + i] = mstp_port->InputBuffer[i]; } - mtu[31 + mstp_port->DataLength] = mstp_port->DataCRCActualMSB; - mtu[31 + mstp_port->DataLength + 1] = mstp_port->DataCRCActualLSB; - mtu_len += (mstp_port->DataLength + 2); + mtu[31 + max_data] = mstp_port->DataCRCActualMSB; + mtu[31 + max_data + 1] = mstp_port->DataCRCActualLSB; + mtu_len += (max_data + 2); } /* Ethernet length is data only - not address or length bytes */ encode_unsigned16(&mtu[12], mtu_len - 14); diff --git a/bacnet-stack/ports/linux/rx_fsm.c b/bacnet-stack/ports/linux/rx_fsm.c index b54b5960..e0715ba4 100644 --- a/bacnet-stack/ports/linux/rx_fsm.c +++ b/bacnet-stack/ports/linux/rx_fsm.c @@ -209,7 +209,8 @@ static void write_received_packet( header[7] = mstp_port->HeaderCRCActual; fwrite(header, sizeof(header), 1, pFile); if (mstp_port->DataLength) { - fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile); + max_data = min(mstp_port->InputBufferSize, mstp_port->DataLength); + fwrite(mstp_port->InputBuffer, max_data, 1, pFile); fwrite(&(mstp_port->DataCRCActualMSB), 1, 1, pFile); fwrite(&(mstp_port->DataCRCActualLSB), 1, 1, pFile); } @@ -253,7 +254,8 @@ static void print_received_packet( mstp_port->SourceAddress, HI_BYTE(mstp_port->DataLength), LO_BYTE(mstp_port->DataLength), mstp_port->HeaderCRCActual); if (mstp_port->DataLength) { - for (i = 0; i < mstp_port->DataLength; i++) { + max_data = min(mstp_port->InputBufferSize, mstp_port->DataLength); + for (i = 0; i < max_data; i++) { fprintf(stderr, "%02X ", mstp_port->InputBuffer[i]); } fprintf(stderr, "%02X %02X ", mstp_port->DataCRCActualMSB, diff --git a/bacnet-stack/ports/win32/rx_fsm.c b/bacnet-stack/ports/win32/rx_fsm.c index 52f2593b..a60dea17 100644 --- a/bacnet-stack/ports/win32/rx_fsm.c +++ b/bacnet-stack/ports/win32/rx_fsm.c @@ -156,7 +156,8 @@ static void print_received_packet( mstp_port->SourceAddress, HI_BYTE(mstp_port->DataLength), LO_BYTE(mstp_port->DataLength), mstp_port->HeaderCRCActual); if (mstp_port->DataLength) { - for (i = 0; i < mstp_port->DataLength; i++) { + max_data = min(mstp_port->InputBufferSize, mstp_port->DataLength); + for (i = 0; i < max_data; i++) { fprintf(stderr, "%02X ", mstp_port->InputBuffer[i]); } fprintf(stderr, "%02X %02X ", mstp_port->DataCRCActualMSB,