diff --git a/CHANGELOG.md b/CHANGELOG.md index 08d954d0..590909ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -196,6 +196,7 @@ The git repositories are hosted at the following sites: ### Fixed +* Fixed FQDN hostname size in minimal hostnport implementation. (#1263) * Fixed segmentation fault in Schedule_Recalculate_PV() during application startup. (#1258) * Fixed the ReadRange-ACK of the Address_List property. (#1149) diff --git a/src/bacnet/hostnport.c b/src/bacnet/hostnport.c index 70922b6f..c268c689 100644 --- a/src/bacnet/hostnport.c +++ b/src/bacnet/hostnport.c @@ -11,7 +11,6 @@ #include #include #include -#include #include "bacnet/hostnport.h" #include "bacnet/bacdcode.h" @@ -441,6 +440,37 @@ int host_n_port_minimal_encode( return apdu_len; } +/** + * @brief Encode a context encoded BACnetHostNPort_Minimal complex data type + * @param apdu - the APDU buffer, or NULL for length + * @param tag_number - context tag number to be encoded + * @param host - BACnetHostNPort_Minimal SEQUENCE + * @return length of the encoded APDU buffer + */ +int host_n_port_minimal_context_encode( + uint8_t *apdu, uint8_t tag_number, const BACNET_HOST_N_PORT_MINIMAL *host) +{ + int len = 0; + int apdu_len = 0; + + if (host) { + len = encode_opening_tag(apdu, tag_number); + apdu_len += len; + if (apdu) { + apdu += len; + } + len = host_n_port_minimal_encode(apdu, host); + apdu_len += len; + if (apdu) { + apdu += len; + } + len = encode_closing_tag(apdu, tag_number); + apdu_len += len; + } + + return apdu_len; +} + /** * @brief Decode the BACnetHostAddress * @param apdu - the APDU buffer @@ -694,7 +724,7 @@ void host_n_port_minimal_ip_init( const uint8_t *address, size_t address_len) { - unsigned i, imax; + uint8_t i, imax; if (host) { host->tag = BACNET_HOST_ADDRESS_TAG_IP_ADDRESS; @@ -722,7 +752,7 @@ void host_n_port_minimal_ip_init( void host_n_port_minimal_hostname_init( BACNET_HOST_N_PORT_MINIMAL *host, uint16_t port, const char *hostname) { - unsigned i; + uint8_t i; if (host) { host->tag = BACNET_HOST_ADDRESS_TAG_NAME; @@ -753,7 +783,7 @@ bool host_n_port_minimal_copy( BACNET_HOST_N_PORT_MINIMAL *dest, const BACNET_HOST_N_PORT_MINIMAL *src) { bool status = false; - int i; + uint8_t i; if (dest && src) { dest->tag = src->tag; @@ -873,7 +903,7 @@ bool host_n_port_minimal_same( const BACNET_HOST_N_PORT_MINIMAL *src) { bool status = false; - int i; + uint8_t i; if (dst && src) { if (dst->tag == src->tag) { diff --git a/src/bacnet/hostnport.h b/src/bacnet/hostnport.h index 3b0dc48d..40809dab 100644 --- a/src/bacnet/hostnport.h +++ b/src/bacnet/hostnport.h @@ -42,6 +42,9 @@ typedef struct BACnetHostNPort { #define BACNET_HOST_ADDRESS_TAG_NONE 0 #define BACNET_HOST_ADDRESS_TAG_IP_ADDRESS 1 #define BACNET_HOST_ADDRESS_TAG_NAME 2 +/* RFC 1035 255-octet total limit and 63-octet label limit + including dots and null terminator */ +#define BACNET_HOST_NAME_MAX 255 /* BACnetHostNPort with smaller RAM footprint using C datatypes */ typedef struct BACnetHostNPort_Minimal { uint8_t tag; @@ -51,13 +54,19 @@ typedef struct BACnetHostNPort_Minimal { uint8_t length; } ip_address; struct BACnetHostCharacterString { - char fqdn[256]; + char fqdn[BACNET_HOST_NAME_MAX]; uint8_t length; } name; } host; uint16_t port; } BACNET_HOST_N_PORT_MINIMAL; +/* Structure to hold the host IP address and hostname for lookup */ +typedef struct BACnetHostAddressPair { + struct BACnetHostOctetString ip_address; + struct BACnetHostCharacterString name; +} BACNET_HOST_ADDRESS_PAIR; + /** * BACnetBDTEntry ::= SEQUENCE { * bbmd-address [0] BACnetHostNPort, @@ -158,6 +167,11 @@ int host_n_port_minimal_decode( BACNET_ERROR_CODE *error_code, BACNET_HOST_N_PORT_MINIMAL *address); BACNET_STACK_EXPORT +int host_n_port_minimal_context_encode( + uint8_t *apdu, + uint8_t tag_number, + const BACNET_HOST_N_PORT_MINIMAL *address); +BACNET_STACK_EXPORT int host_n_port_minimal_context_decode( const uint8_t *apdu, uint32_t apdu_size, diff --git a/test/bacnet/hostnport/src/main.c b/test/bacnet/hostnport/src/main.c index 6274f5de..c2796ec3 100644 --- a/test/bacnet/hostnport/src/main.c +++ b/test/bacnet/hostnport/src/main.c @@ -83,6 +83,72 @@ static void test_HostNPortMinimal_Codec(void) } } +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST(host_n_port_tests, test_HostNPortMinimalContext_Codec) +#else +static void test_HostNPortMinimalContext_Codec(void) +#endif +{ + uint8_t apdu[MAX_APDU] = { 0 }; + int len = 0, apdu_len = 0, null_len = 0, test_len = 0; + BACNET_HOST_N_PORT_MINIMAL test_data_1 = { 0 }, test_data_2 = { 0 }; + uint8_t address[IP6_ADDRESS_MAX] = { 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16 }; + uint16_t port = 0xBAC0; + uint8_t tag_number = 1; + BACNET_ERROR_CODE error_code = ERROR_CODE_SUCCESS; + + /* test IP address */ + host_n_port_minimal_ip_init(&test_data_1, port, address, sizeof(address)); + null_len = + host_n_port_minimal_context_encode(NULL, tag_number, &test_data_1); + apdu_len = + host_n_port_minimal_context_encode(apdu, tag_number, &test_data_1); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = host_n_port_minimal_context_decode( + apdu, apdu_len, tag_number, NULL, NULL); + test_len = host_n_port_minimal_context_decode( + apdu, apdu_len, tag_number, &error_code, &test_data_2); + zassert_equal(test_len, null_len, NULL); + zassert_equal( + apdu_len, test_len, "apdu_len=%d test_len=%d", apdu_len, test_len); + zassert_true( + host_n_port_minimal_same(&test_data_1, &test_data_2), + "test_data_1 != test_data_2"); + while (test_len) { + test_len--; + len = host_n_port_minimal_context_decode( + apdu, test_len, tag_number, NULL, NULL); + zassert_true(len < 0, "len=%d test_len=%d", len, test_len); + } + + /* test hostname */ + host_n_port_minimal_hostname_init(&test_data_1, port, "bacnet.org"); + null_len = + host_n_port_minimal_context_encode(NULL, tag_number, &test_data_1); + apdu_len = + host_n_port_minimal_context_encode(apdu, tag_number, &test_data_1); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = host_n_port_minimal_context_decode( + apdu, apdu_len, tag_number, NULL, NULL); + test_len = host_n_port_minimal_context_decode( + apdu, apdu_len, tag_number, &error_code, &test_data_2); + zassert_equal(test_len, null_len, NULL); + zassert_equal( + apdu_len, test_len, "apdu_len=%d test_len=%d", apdu_len, test_len); + zassert_true( + host_n_port_minimal_same(&test_data_1, &test_data_2), + "test_data_1 != test_data_2"); + while (test_len) { + test_len--; + len = host_n_port_minimal_context_decode( + apdu, test_len, tag_number, NULL, NULL); + zassert_true(len < 0, "len=%d test_len=%d", len, test_len); + } +} + static void test_HostNPortMinimal_Copy(BACNET_HOST_N_PORT *data) { BACNET_HOST_N_PORT_MINIMAL test_data_1 = { 0 }, test_data_2 = { 0 }; @@ -364,6 +430,7 @@ void test_main(void) ztest_test_suite( host_n_port_tests, ztest_unit_test(test_HostNPort), ztest_unit_test(test_HostNPortMinimal_Codec), + ztest_unit_test(test_HostNPortMinimalContext_Codec), ztest_unit_test(test_is_valid_hostname), ztest_unit_test(test_fdt_entry));