Feature/zeroing rx buffer remain (#90)

* Added zeroing rx buffer remain

* Added zeroing rx buffer remain

* Added safety margin for the rx-buffer in the different ports.

* Added safety margin for the receive buffer.

* Added DoxyGen comments.

* Fixed checking return value when calculating distance between opening and closing tag on multiple properties.
This commit is contained in:
Roy Schneider
2020-05-24 16:19:52 +02:00
committed by GitHub
parent 3818f3d844
commit 764e0e8448
16 changed files with 238 additions and 40 deletions
+13
View File
@@ -244,6 +244,7 @@ uint16_t bip_receive(BACNET_ADDRESS *src, /* source address */
unsigned timeout)
{
int received_bytes = 0;
int max = 0;
uint16_t pdu_len = 0; /* return value */
uint8_t src_addr[] = { 0, 0, 0, 0 };
uint16_t src_port = 0;
@@ -274,6 +275,18 @@ uint16_t bip_receive(BACNET_ADDRESS *src, /* source address */
if (pdu[0] != BVLL_TYPE_BACNET_IP)
return 0;
/* Erase up to 16 bytes after the received bytes as safety margin to
* ensure that the decoding functions will run into a 'safe field'
* of zero, if for any reason they would overrun, when parsing the
* message. */
max = (int)max_pdu - received_bytes;
if (max > 0) {
if (max > 16) {
max = 16;
}
memset(&pdu[received_bytes], 0, max);
}
if (bvlc_for_non_bbmd(src_addr, &src_port, pdu, received_bytes) > 0) {
/* Handled, usually with a NACK. */
#if PRINT_ENABLED
+12 -2
View File
@@ -70,7 +70,17 @@ void setup()
#endif
}
static uint8_t PDUBuffer[MAX_MPDU];
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t PDUBuffer[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */];
/** Main */
int main(void)
{
uint16_t pdu_len = 0;
@@ -84,7 +94,7 @@ int main(void)
for (;;) {
/* other tasks */
/* BACnet handling */
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
pdu_len = datalink_receive(&src, &PDUBuffer[0], MAX_MPDU, 0);
if (pdu_len) {
npdu_handler(&src, &PDUBuffer[0], pdu_len);
}
+12 -2
View File
@@ -162,7 +162,17 @@ static inline void bacnet_init(void)
handler_device_communication_control);
}
static uint8_t Receive_PDU[MAX_MPDU]; /* PDU data */
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t Receive_PDU[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */];
/** Main */
int main(void)
{
unsigned long IdleCount = 0; /* idle loop blink counter */
@@ -240,7 +250,7 @@ int main(void)
IdleCount++;
/* BACnet handling */
pdu_len =
datalink_receive(&src, &Receive_PDU[0], sizeof(Receive_PDU), 0);
datalink_receive(&src, &Receive_PDU[0], MAX_MPDU, 0);
if (pdu_len) {
pPIO->PIO_CODR = LED3;
npdu_handler(&src, &Receive_PDU[0], pdu_len);
+13 -2
View File
@@ -135,7 +135,18 @@ static void input_switch_read(void)
}
}
static uint8_t PDUBuffer[MAX_MPDU];
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t PDUBuffer[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */];
/** Main */
int main(void)
{
uint16_t pdu_len = 0;
@@ -153,7 +164,7 @@ int main(void)
task_milliseconds();
/* other tasks */
/* BACnet handling */
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
pdu_len = datalink_receive(&src, &PDUBuffer[0], MAX_MPDU, 0);
if (pdu_len) {
LED_NPDU_ON();
npdu_handler(&src, &PDUBuffer[0], pdu_len);
+12 -2
View File
@@ -141,7 +141,17 @@ void bacnet_init(void)
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
static uint8_t PDUBuffer[MAX_MPDU];
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t PDUBuffer[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */];
/** BACnet task doing receive and transmit. */
void bacnet_task(void)
{
uint8_t mstp_mac_address;
@@ -208,7 +218,7 @@ void bacnet_task(void)
dcc_timer_seconds(DCC_CYCLE_SECONDS);
}
/* handle the messaging */
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
pdu_len = datalink_receive(&src, &PDUBuffer[0], MAX_MPDU, 0);
if (pdu_len) {
npdu_handler(&src, &PDUBuffer[0], pdu_len);
}
+12 -1
View File
@@ -319,7 +319,7 @@ uint16_t bip_receive(
max = BIP_Socket;
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = recvfrom(BIP_Socket, (char *)&npdu[0], max_npdu, 0,
received_bytes = recvfrom(max, (char *)&npdu[0], max_npdu, 0,
(struct sockaddr *)&sin, &sin_len);
} else {
return 0;
@@ -336,6 +336,17 @@ uint16_t bip_receive(
if (npdu[0] != BVLL_TYPE_BACNET_IP) {
return 0;
}
/* Erase up to 16 bytes after the received bytes as safety margin to
* ensure that the decoding functions will run into a 'safe field'
* of zero, if for any reason they would overrun, when parsing the
* message. */
max = (int)max_npdu - received_bytes;
if (max > 0) {
if (max > 16) {
max = 16;
}
memset(&npdu[received_bytes], 0, max);
}
/* Data link layer addressing between B/IPv4 nodes consists of a 32-bit
IPv4 address followed by a two-octet UDP port number (both of which
shall be transmitted with the most significant octet first). This
+8 -2
View File
@@ -51,8 +51,14 @@
bool Who_Is_Request = true;
/* buffers used for receiving */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t Rx_Buf[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */] = { 0 };
static void LocalIAmHandler(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
+10 -2
View File
@@ -48,7 +48,15 @@ wifi_config_t wifi_config = {
#define BACNET_LED 5
uint8_t Handler_Transmit_Buffer[MAX_PDU] = { 0 };
uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
uint8_t Rx_Buf[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */] = { 0 };
EventGroupHandle_t wifi_event_group;
const static int CONNECTED_BIT = BIT0;
@@ -207,4 +215,4 @@ void app_main()
NULL, /* Task input parameter */
20, /* Priority of the task */
NULL); /* Task handle. */
}
}
+12 -1
View File
@@ -331,7 +331,7 @@ uint16_t bip_receive(
max = BIP_Socket;
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = recvfrom(BIP_Socket, (char *)&npdu[0], max_npdu, 0,
received_bytes = recvfrom(max, (char *)&npdu[0], max_npdu, 0,
(struct sockaddr *)&sin, &sin_len);
} else {
return 0;
@@ -348,6 +348,17 @@ uint16_t bip_receive(
if (npdu[0] != BVLL_TYPE_BACNET_IP) {
return 0;
}
/* Erase up to 16 bytes after the received bytes as safety margin to
* ensure that the decoding functions will run into a 'safe field'
* of zero, if for any reason they would overrun, when parsing the
* message. */
max = (int)max_npdu - received_bytes;
if (max > 0) {
if (max > 16) {
max = 16;
}
memset(&npdu[received_bytes], 0, max);
}
/* Data link layer addressing between B/IPv4 nodes consists of a 32-bit
IPv4 address followed by a two-octet UDP port number (both of which
shall be transmitted with the most significant octet first). This
+10 -2
View File
@@ -74,7 +74,15 @@ void bacnet_init(void)
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
static uint8_t PDUBuffer[MAX_MPDU];
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t PDUBuffer[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */];
void bacnet_task(void)
{
uint16_t pdu_len;
@@ -110,7 +118,7 @@ void bacnet_task(void)
dcc_timer_seconds(DCC_CYCLE_SECONDS);
}
/* handle the messaging */
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
pdu_len = datalink_receive(&src, &PDUBuffer[0], MAX_MPDU, 0);
if (pdu_len) {
npdu_handler(&src, &PDUBuffer[0], pdu_len);
}
+12 -2
View File
@@ -79,7 +79,17 @@ void bacnet_init(void)
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
static uint8_t PDUBuffer[MAX_MPDU];
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t PDUBuffer[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */];
/** BACnet task handling receiving and transmitting of messages. */
void bacnet_task(void)
{
uint16_t pdu_len;
@@ -123,7 +133,7 @@ void bacnet_task(void)
dcc_timer_seconds(DCC_CYCLE_SECONDS);
}
/* handle the messaging */
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
pdu_len = datalink_receive(&src, &PDUBuffer[0], MAX_MPDU, 0);
if (pdu_len) {
npdu_handler(&src, &PDUBuffer[0], pdu_len);
}
+12 -1
View File
@@ -484,7 +484,7 @@ uint16_t bip_receive(
max = BIP_Socket;
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = recvfrom(BIP_Socket, (char *)&npdu[0], max_npdu, 0,
received_bytes = recvfrom(max, (char *)&npdu[0], max_npdu, 0,
(struct sockaddr *)&sin, &sin_len);
} else {
return 0;
@@ -501,6 +501,17 @@ uint16_t bip_receive(
if (npdu[0] != BVLL_TYPE_BACNET_IP) {
return 0;
}
/* Erase up to 16 bytes after the received bytes as safety margin to
* ensure that the decoding functions will run into a 'safe field'
* of zero, if for any reason they would overrun, when parsing the
* message. */
max = (int)max_npdu - received_bytes;
if (max > 0) {
if (max > 16) {
max = 16;
}
memset(&npdu[received_bytes], 0, max);
}
/* Data link layer addressing between B/IPv4 nodes consists of a 32-bit
IPv4 address followed by a two-octet UDP port number (both of which
shall be transmitted with the most significant octet first). This
+8 -2
View File
@@ -59,8 +59,14 @@
#include "bacnet/basic/object/bacfile.h"
#endif
/* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
static uint8_t Rx_Buf[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */] = { 0 };
/* send a whois to see who is on the network */
static bool Who_Is_Request = true;