From 2c91ff188230fdd2a093fd39ec484d8efc92dc05 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 21 Feb 2024 17:02:38 +0100 Subject: [PATCH 01/13] feat: add ethtype to reth endpoint config --- include/zenoh-pico/system/link/raweth.h | 2 +- src/transport/raweth/link.c | 39 +++++++++++++++++++++++-- src/transport/raweth/tx.c | 4 +-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index 6ce8ad67e..9d7c89920 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -52,7 +52,7 @@ typedef struct { const char *_interface; _z_sys_net_socket_t _sock; uint16_t _vlan; - uint16_t ethtype; + uint16_t _ethtype; uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; uint8_t _smac[_ZP_MAC_ADDR_LENGTH]; _Bool _has_vlan; diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 8eb42abc9..6c475a465 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -27,15 +27,30 @@ #if Z_FEATURE_RAWETH_TRANSPORT == 1 -#define RAWETH_CONFIG_ARGC 1 +#define RAWETH_CONFIG_ARGC 4 #define RAWETH_CONFIG_IFACE_KEY 0x01 #define RAWETH_CONFIG_IFACE_STR "iface" +#define RAWETH_CONFIG_ETHTYPE_KEY 0x02 +#define RAWETH_CONFIG_ETHTYPE_STR "ethtype" + +#define RAWETH_CONFIG_MAPPING_KEY 0x03 +#define RAWETH_CONFIG_MAPPING_STR "mapping" + +#define RAWETH_CONFIG_WHITELIST_KEY 0x04 +#define RAWETH_CONFIG_WHITELIST_STR "whitelist" + #define RAWETH_CONFIG_MAPPING_BUILD \ _z_str_intmapping_t args[RAWETH_CONFIG_ARGC]; \ args[0]._key = RAWETH_CONFIG_IFACE_KEY; \ - args[0]._str = RAWETH_CONFIG_IFACE_STR; + args[0]._str = RAWETH_CONFIG_IFACE_STR; \ + args[1]._key = RAWETH_CONFIG_ETHTYPE_KEY; \ + args[1]._str = RAWETH_CONFIG_ETHTYPE_STR; \ + args[2]._key = RAWETH_CONFIG_MAPPING_KEY; \ + args[2]._str = RAWETH_CONFIG_MAPPING_STR; \ + args[3]._key = RAWETH_CONFIG_WHITELIST_KEY; \ + args[3]._str = RAWETH_CONFIG_WHITELIST_STR; static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config) { const char *iface = _z_str_intmap_get(config, RAWETH_CONFIG_IFACE_KEY); @@ -46,6 +61,20 @@ static const char *_z_get_iface_raweth(_z_str_intmap_t *config) { return _z_str_intmap_get(config, RAWETH_CONFIG_IFACE_KEY); } +static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config) { + const char *s_ethtype = _z_str_intmap_get(config, RAWETH_CONFIG_ETHTYPE_KEY); + if (s_ethtype == NULL) { + return false; + } + int ethtype = atoi(s_ethtype); + return ((ethtype & 0xff) > 0x6); // Ethtype must be above 0x600 in network order +} + +static int _z_get_ethtype_raweth(_z_str_intmap_t *config) { + const char *s_ethtype = _z_str_intmap_get(config, RAWETH_CONFIG_ETHTYPE_KEY); + return atoi(s_ethtype); +} + static _Bool _z_valid_address_raweth(const char *address) { // Check if the string has the correct length size_t len = strlen(address); @@ -103,6 +132,12 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { } else { self->_socket._raweth._interface = _ZP_RAWETH_CFG_INTERFACE; } + // Init socket ethtype + if (_z_valid_ethtype_raweth(&self->_endpoint._config)) { + self->_socket._raweth._ethtype = _z_get_ethtype_raweth(&self->_endpoint._config); + } else { + self->_socket._raweth._ethtype = _ZP_RAWETH_CFG_ETHTYPE; + } // Open raweth link return _z_open_raweth(&self->_socket._raweth._sock, self->_socket._raweth._interface); } diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index ac360cc9e..88c409339 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -128,7 +128,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH); header.vlan_type = _ZP_ETH_TYPE_VLAN; header.tag = resocket->_vlan; - header.ethtype = _ZP_RAWETH_CFG_ETHTYPE; + header.ethtype = resocket->_ethtype; header.data_length = _z_raweth_htons(wpos - sizeof(header)); // Write header _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (uint8_t *)&header, 0, sizeof(header))); @@ -137,7 +137,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { // Set header memcpy(&header.dmac, &resocket->_dmac, _ZP_MAC_ADDR_LENGTH); memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH); - header.ethtype = _ZP_RAWETH_CFG_ETHTYPE; + header.ethtype = resocket->_ethtype; header.data_length = _z_raweth_htons(wpos - sizeof(header)); // Write header _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (uint8_t *)&header, 0, sizeof(header))); From 3ecd9155d39d312e203a4dcdf26ecc65da16436c Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 21 Feb 2024 17:28:40 +0100 Subject: [PATCH 02/13] feat: type renaming --- include/zenoh-pico/system/link/raweth.h | 17 +++++++++++++++++ include/zenoh-pico/transport/raweth/config.h | 18 +++--------------- src/transport/raweth/config.c | 6 +++--- src/transport/raweth/tx.c | 18 +++++++++--------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index 9d7c89920..a99d1fcb0 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -18,6 +18,7 @@ #include #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/system/platform.h" #if Z_FEATURE_RAWETH_TRANSPORT == 1 @@ -31,6 +32,18 @@ // Max frame size #define _ZP_MAX_ETH_FRAME_SIZE 1514 +// Endpoing config types +typedef struct { + _z_keyexpr_t _keyexpr; + uint16_t _vlan; // vlan tag (pcp + dei + id), big endian + uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; + _Bool _has_vlan; +} _zp_raweth_mapping_entry_t; + +typedef struct { + uint8_t _mac[_ZP_MAC_ADDR_LENGTH]; +} _zp_raweth_whitelist_entry_t; + // Ethernet header structure type typedef struct { uint8_t dmac[_ZP_MAC_ADDR_LENGTH]; // Destination mac address @@ -51,6 +64,10 @@ typedef struct { typedef struct { const char *_interface; _z_sys_net_socket_t _sock; + const _zp_raweth_mapping_entry_t *_mapping; + size_t _mapping_size; + const _zp_raweth_whitelist_entry_t *_whitelist; + size_t _whitelist_size; uint16_t _vlan; uint16_t _ethtype; uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; diff --git a/include/zenoh-pico/transport/raweth/config.h b/include/zenoh-pico/transport/raweth/config.h index c8d644747..3792bd0d4 100644 --- a/include/zenoh-pico/transport/raweth/config.h +++ b/include/zenoh-pico/transport/raweth/config.h @@ -17,24 +17,12 @@ #include -#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/transport/transport.h" #include "zenoh-pico/utils/result.h" #if Z_FEATURE_RAWETH_TRANSPORT == 1 -typedef struct { - _z_keyexpr_t _keyexpr; - uint16_t _vlan; // vlan tag (pcp + dei + id), big endian - uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; - _Bool _has_vlan; -} _zp_raweth_cfg_entry; - -typedef struct { - uint8_t _mac[_ZP_MAC_ADDR_LENGTH]; -} _zp_raweth_cfg_whitelist_val; - // Ethertype to use in frame extern const uint16_t _ZP_RAWETH_CFG_ETHTYPE; @@ -45,13 +33,13 @@ extern const char *_ZP_RAWETH_CFG_INTERFACE; extern const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH]; // Main config array -extern const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[]; +extern const _zp_raweth_mapping_entry_t _ZP_RAWETH_CFG_MAPPING[]; // Mac address rx whitelist array -extern const _zp_raweth_cfg_whitelist_val _ZP_RAWETH_CFG_WHITELIST[]; +extern const _zp_raweth_whitelist_entry_t _ZP_RAWETH_CFG_WHITELIST[]; // Array size -extern const size_t _ZP_RAWETH_CFG_SIZE; +extern const size_t _ZP_RAWETH_CFG_MAPPING_SIZE; extern const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE; #endif // Z_FEATURE_RAWETH_TRANSPORT == 1 diff --git a/src/transport/raweth/config.c b/src/transport/raweth/config.c index 08e4684e7..7878ca4ea 100644 --- a/src/transport/raweth/config.c +++ b/src/transport/raweth/config.c @@ -26,7 +26,7 @@ const char *_ZP_RAWETH_CFG_INTERFACE = "lo"; const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}; // Should be generated -const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[] = { +const _zp_raweth_mapping_entry_t _ZP_RAWETH_CFG_MAPPING[] = { {{0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}, // Default mac addr {{0, {0}, "some/key/expr"}, 0x8c00, {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, true}, // entry1 {{0, {0}, "demo/example/zenoh-pico-pub"}, 0xab00, {0x41, 0x55, 0xa8, 0x00, 0x9d, 0xc0}, true}, // entry2 @@ -34,14 +34,14 @@ const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[] = { }; // Should be generated -const _zp_raweth_cfg_whitelist_val _ZP_RAWETH_CFG_WHITELIST[] = { +const _zp_raweth_whitelist_entry_t _ZP_RAWETH_CFG_WHITELIST[] = { {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}}, {{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}}, {{0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}}, }; // Don't modify -const size_t _ZP_RAWETH_CFG_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_ARRAY); +const size_t _ZP_RAWETH_CFG_MAPPING_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_MAPPING); const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_WHITELIST); #endif // Z_FEATURE_RAWETH_TRANSPORT == 1 diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index 88c409339..704bbba77 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -38,9 +38,9 @@ static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUS #endif static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { - for (int i = 1; i < _ZP_RAWETH_CFG_SIZE; i++) { + for (int i = 1; i < _ZP_RAWETH_CFG_MAPPING_SIZE; i++) { // Find matching keyexpr - if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, _ZP_RAWETH_CFG_ARRAY[i]._keyexpr._suffix) != + if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, _ZP_RAWETH_CFG_MAPPING[i]._keyexpr._suffix) != _Z_RES_OK) { continue; } @@ -52,14 +52,14 @@ static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_sock static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { int8_t ret = _Z_RES_OK; - if (_ZP_RAWETH_CFG_SIZE < 1) { + if (_ZP_RAWETH_CFG_MAPPING_SIZE < 1) { return _Z_ERR_GENERIC; } if (keyexpr == NULL) { // Store default value into socket - memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_ARRAY[0]._dmac, _ZP_MAC_ADDR_LENGTH); - uint16_t vlan = _ZP_RAWETH_CFG_ARRAY[0]._vlan; - sock->_has_vlan = _ZP_RAWETH_CFG_ARRAY[0]._has_vlan; + memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_MAPPING[0]._dmac, _ZP_MAC_ADDR_LENGTH); + uint16_t vlan = _ZP_RAWETH_CFG_MAPPING[0]._vlan; + sock->_has_vlan = _ZP_RAWETH_CFG_MAPPING[0]._has_vlan; if (sock->_has_vlan) { memcpy(&sock->_vlan, &vlan, sizeof(vlan)); } @@ -72,9 +72,9 @@ static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socke _Z_DEBUG("Key '%s' wasn't found in config mapping, sending to default address", keyexpr->_suffix); } // Store data into socket - memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_ARRAY[idx]._dmac, _ZP_MAC_ADDR_LENGTH); - uint16_t vlan = _ZP_RAWETH_CFG_ARRAY[idx]._vlan; - sock->_has_vlan = _ZP_RAWETH_CFG_ARRAY[idx]._has_vlan; + memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_MAPPING[idx]._dmac, _ZP_MAC_ADDR_LENGTH); + uint16_t vlan = _ZP_RAWETH_CFG_MAPPING[idx]._vlan; + sock->_has_vlan = _ZP_RAWETH_CFG_MAPPING[idx]._has_vlan; if (sock->_has_vlan) { memcpy(&sock->_vlan, &vlan, sizeof(vlan)); } From 8e94994238d95cff51c96473898fd4493a1b877a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 23 Feb 2024 09:56:11 +0100 Subject: [PATCH 03/13] fix: remove unused macro call --- include/zenoh-pico/session/session.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index a13b8a238..becf8f67a 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -51,7 +51,6 @@ void _z_reply_data_clear(_z_reply_data_t *rd); _Z_ELEM_DEFINE(_z_reply_data, _z_reply_data_t, _z_noop_size, _z_reply_data_clear, _z_noop_copy) _Z_LIST_DEFINE(_z_reply_data, _z_reply_data_t) -_Z_ARRAY_DEFINE(_z_reply_data, _z_reply_data_t) /** * An reply to a :c:func:`z_query`. From 9bbebf2d4b791d3ac8e787b8b7123267de17077e Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 23 Feb 2024 16:47:55 +0100 Subject: [PATCH 04/13] feat: remove static raweth config --- include/zenoh-pico/system/link/raweth.h | 15 +- include/zenoh-pico/transport/raweth/config.h | 46 ----- src/system/unix/link/raweth.c | 23 ++- src/transport/raweth/config.c | 47 ----- src/transport/raweth/link.c | 175 ++++++++++++++++++- src/transport/raweth/rx.c | 3 +- src/transport/raweth/tx.c | 23 +-- 7 files changed, 209 insertions(+), 123 deletions(-) delete mode 100644 include/zenoh-pico/transport/raweth/config.h delete mode 100644 src/transport/raweth/config.c diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index a99d1fcb0..1b04bde47 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -40,10 +40,16 @@ typedef struct { _Bool _has_vlan; } _zp_raweth_mapping_entry_t; +_Z_ELEM_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t, _z_noop_size, _z_noop_clear, _z_noop_copy) +_Z_ARRAY_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t) + typedef struct { uint8_t _mac[_ZP_MAC_ADDR_LENGTH]; } _zp_raweth_whitelist_entry_t; +_Z_ELEM_DEFINE(_zp_raweth_whitelist, _zp_raweth_whitelist_entry_t, _z_noop_size, _z_noop_clear, _z_noop_copy) +_Z_ARRAY_DEFINE(_zp_raweth_whitelist, _zp_raweth_whitelist_entry_t) + // Ethernet header structure type typedef struct { uint8_t dmac[_ZP_MAC_ADDR_LENGTH]; // Destination mac address @@ -64,10 +70,8 @@ typedef struct { typedef struct { const char *_interface; _z_sys_net_socket_t _sock; - const _zp_raweth_mapping_entry_t *_mapping; - size_t _mapping_size; - const _zp_raweth_whitelist_entry_t *_whitelist; - size_t _whitelist_size; + _zp_raweth_mapping_array_t _mapping; + _zp_raweth_whitelist_array_t _whitelist; uint16_t _vlan; uint16_t _ethtype; uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; @@ -78,7 +82,8 @@ typedef struct { int8_t _z_get_smac_raweth(_z_raweth_socket_t *resock); int8_t _z_open_raweth(_z_sys_net_socket_t *sock, const char *interface); size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t buff_len); -size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr); +size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr, + const _zp_raweth_whitelist_array_t *whitelist); int8_t _z_close_raweth(_z_sys_net_socket_t *sock); size_t _z_raweth_ntohs(size_t val); size_t _z_raweth_htons(size_t val); diff --git a/include/zenoh-pico/transport/raweth/config.h b/include/zenoh-pico/transport/raweth/config.h deleted file mode 100644 index 3792bd0d4..000000000 --- a/include/zenoh-pico/transport/raweth/config.h +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright (c) 2022 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -#ifndef ZENOH_PICO_RAWETH_CONFIG_H -#define ZENOH_PICO_RAWETH_CONFIG_H - -#include - -#include "zenoh-pico/system/link/raweth.h" -#include "zenoh-pico/transport/transport.h" -#include "zenoh-pico/utils/result.h" - -#if Z_FEATURE_RAWETH_TRANSPORT == 1 - -// Ethertype to use in frame -extern const uint16_t _ZP_RAWETH_CFG_ETHTYPE; - -// Interface to use -extern const char *_ZP_RAWETH_CFG_INTERFACE; - -// Source mac address -extern const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH]; - -// Main config array -extern const _zp_raweth_mapping_entry_t _ZP_RAWETH_CFG_MAPPING[]; - -// Mac address rx whitelist array -extern const _zp_raweth_whitelist_entry_t _ZP_RAWETH_CFG_WHITELIST[]; - -// Array size -extern const size_t _ZP_RAWETH_CFG_MAPPING_SIZE; -extern const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE; - -#endif // Z_FEATURE_RAWETH_TRANSPORT == 1 -#endif // ZENOH_PICO_RAWETH_CONFIG_H diff --git a/src/system/unix/link/raweth.c b/src/system/unix/link/raweth.c index 78a8aee92..8c64359f0 100644 --- a/src/system/unix/link/raweth.c +++ b/src/system/unix/link/raweth.c @@ -31,8 +31,8 @@ #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/system/platform/unix.h" -#include "zenoh-pico/transport/raweth/config.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/pointers.h" @@ -89,19 +89,24 @@ size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t return (size_t)wb; } -size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr) { +size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr, + const _zp_raweth_whitelist_array_t *whitelist) { // Read from socket ssize_t bytesRead = recvfrom(sock->_fd, buff, buff_len, 0, NULL, NULL); if ((bytesRead < 0) || (bytesRead < sizeof(_zp_eth_header_t))) { return SIZE_MAX; } - // Address filtering - _zp_eth_header_t *header = (_zp_eth_header_t *)buff; - _Bool is_valid = false; - for (size_t i = 0; i < _ZP_RAWETH_CFG_WHITELIST_SIZE; i++) { - if (memcmp(&header->smac, _ZP_RAWETH_CFG_WHITELIST[i]._mac, _ZP_MAC_ADDR_LENGTH) == 0) { // Test byte ordering - is_valid = true; - break; + _Bool is_valid = true; + // Address filtering (only if there is a whitelist) + if (_zp_raweth_whitelist_array_len(whitelist) > 0) { + is_valid = false; + _zp_eth_header_t *header = (_zp_eth_header_t *)buff; + for (size_t i = 0; i < _zp_raweth_whitelist_array_len(whitelist); i++) { + const _zp_raweth_whitelist_entry_t *entry = _zp_raweth_whitelist_array_get(whitelist, i); + if (memcmp(&header->smac, entry->_mac, _ZP_MAC_ADDR_LENGTH) == 0) { + is_valid = true; + break; + } } } // Ignore packet from unknown sources diff --git a/src/transport/raweth/config.c b/src/transport/raweth/config.c deleted file mode 100644 index 7878ca4ea..000000000 --- a/src/transport/raweth/config.c +++ /dev/null @@ -1,47 +0,0 @@ -// -// Copyright (c) 2022 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -#include "zenoh-pico/transport/raweth/config.h" - -#if Z_FEATURE_RAWETH_TRANSPORT == 1 - -// Should be generated (big endian format) -const uint16_t _ZP_RAWETH_CFG_ETHTYPE = 0x72e0; - -// Should be generated -const char *_ZP_RAWETH_CFG_INTERFACE = "lo"; - -// Should be generated -const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}; - -// Should be generated -const _zp_raweth_mapping_entry_t _ZP_RAWETH_CFG_MAPPING[] = { - {{0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}, // Default mac addr - {{0, {0}, "some/key/expr"}, 0x8c00, {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, true}, // entry1 - {{0, {0}, "demo/example/zenoh-pico-pub"}, 0xab00, {0x41, 0x55, 0xa8, 0x00, 0x9d, 0xc0}, true}, // entry2 - {{0, {0}, "another/keyexpr"}, 0x4300, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab}, true}, // entry3 -}; - -// Should be generated -const _zp_raweth_whitelist_entry_t _ZP_RAWETH_CFG_WHITELIST[] = { - {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}}, - {{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}}, - {{0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}}, -}; - -// Don't modify -const size_t _ZP_RAWETH_CFG_MAPPING_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_MAPPING); -const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_WHITELIST); - -#endif // Z_FEATURE_RAWETH_TRANSPORT == 1 diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 6c475a465..d94e4d132 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -20,9 +20,9 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/link/config/raweth.h" #include "zenoh-pico/link/manager.h" +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/system/platform.h" -#include "zenoh-pico/transport/raweth/config.h" #include "zenoh-pico/utils/pointers.h" #if Z_FEATURE_RAWETH_TRANSPORT == 1 @@ -52,6 +52,38 @@ args[3]._key = RAWETH_CONFIG_WHITELIST_KEY; \ args[3]._str = RAWETH_CONFIG_WHITELIST_STR; +const uint16_t _ZP_RAWETH_DEFAULT_ETHTYPE = 0x72e0; +const char *_ZP_RAWETH_DEFAULT_INTERFACE = "lo"; +const uint8_t _ZP_RAWETH_DEFAULT_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}; +// const _zp_raweth_mapping_entry_t _ZP_RAWETH_DEFAULT_MAPPING[] = { +// {{0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}, // Default mac +// addr +// {{0, {0}, "some/key/expr"}, 0x8c00, {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, true}, // entry1 +// {{0, {0}, "demo/example/zenoh-pico-pub"}, 0xab00, {0x41, 0x55, 0xa8, 0x00, 0x9d, 0xc0}, true}, // entry2 +// {{0, {0}, "another/keyexpr"}, 0x4300, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab}, true}, // entry3 +// }; +// some/key/expr 01:23:45:67:89:ab 12,another/ke aa:bb:cc:dd:ee:ff + +static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config); +static const char *_z_get_iface_raweth(_z_str_intmap_t *config); +static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config); +static int _z_get_ethtype_raweth(_z_str_intmap_t *config); +static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config); +static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_array_t *array, size_t size); +static size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config); +static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitelist_array_t *array, size_t size); +static _Bool _z_valid_address_raweth(const char *address); +static uint8_t *_z_parse_address_raweth(const char *address); +static int8_t _z_f_link_open_raweth(_z_link_t *self); +static int8_t _z_f_link_listen_raweth(_z_link_t *self); +static void _z_f_link_close_raweth(_z_link_t *self); +static void _z_f_link_free_raweth(_z_link_t *self); +static size_t _z_f_link_write_raweth(const _z_link_t *self, const uint8_t *ptr, size_t len); +static size_t _z_f_link_write_all_raweth(const _z_link_t *self, const uint8_t *ptr, size_t len); +static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); +static size_t _z_f_link_read_exact_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); +static uint16_t _z_get_link_mtu_raweth(void); + static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config) { const char *iface = _z_str_intmap_get(config, RAWETH_CONFIG_IFACE_KEY); return (iface != NULL); @@ -75,6 +107,126 @@ static int _z_get_ethtype_raweth(_z_str_intmap_t *config) { return atoi(s_ethtype); } +static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { + // Retrieve list + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_MAPPING_KEY); + if (cfg_str == NULL) { + return 0; + } + char *s_mapping = zp_malloc(strlen(cfg_str)); + if (s_mapping == NULL) { + return 0; + } + size_t size = 0; + strcpy(s_mapping, cfg_str); + // Parse list + char delim[] = ","; + char *entry = strtok(s_mapping, delim); + while (entry != NULL) { + // Check entry + size += 1; + entry = strtok(s_mapping, delim); + } + // Clean up + zp_free(s_mapping); + return size; +} + +static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_array_t *array, size_t size) { + // Retrieve data + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_MAPPING_KEY); + // Copy data + // Allocate array + *array = _zp_raweth_mapping_array_make(size); + // Store size + // Copy data + return _Z_RES_OK; +} + +static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { + // Retrieve data + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_WHITELIST_KEY); + if (cfg_str == NULL) { + return 0; + } + // Copy data + char *s_whitelist = zp_malloc(strlen(cfg_str)); + if (s_whitelist == NULL) { + return 0; + } + strcpy(s_whitelist, cfg_str); + // Parse list + size_t size = 0; + char delim[] = ","; + char *entry = strtok(s_whitelist, delim); + while (entry != NULL) { + // Check entry + if (!_z_valid_address_raweth(entry)) { + zp_free(s_whitelist); + return 0; + } + size += 1; + entry = strtok(s_whitelist, delim); + } + // Clean up + zp_free(s_whitelist); + return size; +} + +static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitelist_array_t *array, size_t size) { + // Retrieve data + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_WHITELIST_KEY); + if (cfg_str == NULL) { + return _Z_ERR_GENERIC; + } + // Copy data + char *s_whitelist = zp_malloc(strlen(cfg_str)); + if (s_whitelist == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + strcpy(s_whitelist, cfg_str); + // Allocate array + *array = _zp_raweth_whitelist_array_make(size); + if (array->_len == 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + size_t idx = 0; + // Parse list + char delim[] = ","; + char *entry = strtok(s_whitelist, delim); + while ((entry != NULL) && (idx < array->_len)) { + // Convert address from string to int array + uint8_t *addr = _z_parse_address_raweth(entry); + if (addr == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Copy address to entry + _zp_raweth_whitelist_entry_t *elem = _zp_raweth_whitelist_array_get(array, idx); + memcpy(elem->_mac, addr, _ZP_MAC_ADDR_LENGTH); + zp_free(addr); + // Next iteration + idx += 1; + entry = strtok(s_whitelist, delim); + } + // Clean up + zp_free(s_whitelist); + return _Z_RES_OK; +} + +static _Bool _z_valid_mapping_entry(const char *entry) { + // some/key/expr 01:23:45:67:89:ab 12,another/ke aa:bb:cc:dd:ee:ff + size_t len = strlen(entry); + size_t idx = 0; + const char *p_start = &entry[0]; + const char *p_end = NULL; + char c = '\0'; + do { + c = entry[idx]; + idx += 1; + } while ((idx < len) && (c != ' ')); + return false; +} + static _Bool _z_valid_address_raweth(const char *address) { // Check if the string has the correct length size_t len = strlen(address); @@ -118,25 +270,40 @@ static uint8_t *_z_parse_address_raweth(const char *address) { } static int8_t _z_f_link_open_raweth(_z_link_t *self) { + // Init arrays + self->_socket._raweth._mapping = _zp_raweth_mapping_array_empty(); + self->_socket._raweth._whitelist = _zp_raweth_whitelist_array_empty(); // Init socket smac if (_z_valid_address_raweth(self->_endpoint._locator._address)) { uint8_t *addr = _z_parse_address_raweth(self->_endpoint._locator._address); memcpy(&self->_socket._raweth._smac, addr, _ZP_MAC_ADDR_LENGTH); zp_free(addr); } else { - memcpy(&self->_socket._raweth._smac, _ZP_RAWETH_CFG_SMAC, _ZP_MAC_ADDR_LENGTH); + memcpy(&self->_socket._raweth._smac, _ZP_RAWETH_DEFAULT_SMAC, _ZP_MAC_ADDR_LENGTH); } // Init socket interface if (_z_valid_iface_raweth(&self->_endpoint._config)) { self->_socket._raweth._interface = _z_get_iface_raweth(&self->_endpoint._config); } else { - self->_socket._raweth._interface = _ZP_RAWETH_CFG_INTERFACE; + self->_socket._raweth._interface = _ZP_RAWETH_DEFAULT_INTERFACE; } // Init socket ethtype if (_z_valid_ethtype_raweth(&self->_endpoint._config)) { self->_socket._raweth._ethtype = _z_get_ethtype_raweth(&self->_endpoint._config); } else { - self->_socket._raweth._ethtype = _ZP_RAWETH_CFG_ETHTYPE; + self->_socket._raweth._ethtype = _ZP_RAWETH_DEFAULT_ETHTYPE; + } + // Init socket mapping + size_t size = _z_valid_mapping_raweth(&self->_endpoint._config); + if (size != 0) { + _Z_RETURN_IF_ERR(_z_get_mapping_raweth(&self->_endpoint._config, &self->_socket._raweth._mapping, size)); + } else { + // self->_socket._raweth._mapping = _ZP_RAWETH_DEFAULT_MAPPING; + } + // Init socket whitelist + size = _z_valid_whitelist_raweth(&self->_endpoint._config); + if (size != 0) { + _Z_RETURN_IF_ERR(_z_get_whitelist_raweth(&self->_endpoint._config, &self->_socket._raweth._whitelist, size)); } // Open raweth link return _z_open_raweth(&self->_socket._raweth._sock, self->_socket._raweth._interface); diff --git a/src/transport/raweth/rx.c b/src/transport/raweth/rx.c index 162c66c28..09f2fb2e7 100644 --- a/src/transport/raweth/rx.c +++ b/src/transport/raweth/rx.c @@ -29,7 +29,8 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_bytes_t *addr) { uint8_t *buff = _z_zbuf_get_wptr(zbf); - size_t rb = _z_receive_raweth(&link->_socket._raweth._sock, buff, _z_zbuf_space_left(zbf), addr); + size_t rb = _z_receive_raweth(&link->_socket._raweth._sock, buff, _z_zbuf_space_left(zbf), addr, + &link->_socket._raweth._whitelist); // Check validity if ((rb == SIZE_MAX) || (rb < sizeof(_zp_eth_header_t))) { return SIZE_MAX; diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index 704bbba77..56af858a7 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -24,7 +24,6 @@ #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/system/link/raweth.h" -#include "zenoh-pico/transport/raweth/config.h" #include "zenoh-pico/transport/transport.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -38,10 +37,10 @@ static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUS #endif static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { - for (int i = 1; i < _ZP_RAWETH_CFG_MAPPING_SIZE; i++) { + for (int i = 1; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { // Find matching keyexpr - if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, _ZP_RAWETH_CFG_MAPPING[i]._keyexpr._suffix) != - _Z_RES_OK) { + _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, i); + if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, entry->_keyexpr._suffix) != _Z_RES_OK) { continue; } return i; @@ -52,14 +51,15 @@ static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_sock static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { int8_t ret = _Z_RES_OK; - if (_ZP_RAWETH_CFG_MAPPING_SIZE < 1) { + if (_zp_raweth_mapping_array_len(&sock->_mapping) < 1) { return _Z_ERR_GENERIC; } if (keyexpr == NULL) { // Store default value into socket - memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_MAPPING[0]._dmac, _ZP_MAC_ADDR_LENGTH); - uint16_t vlan = _ZP_RAWETH_CFG_MAPPING[0]._vlan; - sock->_has_vlan = _ZP_RAWETH_CFG_MAPPING[0]._has_vlan; + _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, 0); + memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); + uint16_t vlan = entry->_vlan; + sock->_has_vlan = entry->_has_vlan; if (sock->_has_vlan) { memcpy(&sock->_vlan, &vlan, sizeof(vlan)); } @@ -72,9 +72,10 @@ static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socke _Z_DEBUG("Key '%s' wasn't found in config mapping, sending to default address", keyexpr->_suffix); } // Store data into socket - memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_MAPPING[idx]._dmac, _ZP_MAC_ADDR_LENGTH); - uint16_t vlan = _ZP_RAWETH_CFG_MAPPING[idx]._vlan; - sock->_has_vlan = _ZP_RAWETH_CFG_MAPPING[idx]._has_vlan; + _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, idx); + memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); + uint16_t vlan = entry->_vlan; + sock->_has_vlan = entry->_has_vlan; if (sock->_has_vlan) { memcpy(&sock->_vlan, &vlan, sizeof(vlan)); } From 85c0639fa172866dab7ca077158f07886980ea73 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 26 Feb 2024 10:31:40 +0100 Subject: [PATCH 05/13] feat: use defines for separators --- src/transport/raweth/link.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index d94e4d132..90d526975 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -27,6 +27,9 @@ #if Z_FEATURE_RAWETH_TRANSPORT == 1 +#define RAWETH_CFG_TUPLE_SEPARATOR '#' +#define RAWETH_CFG_LIST_SEPARATOR ',' + #define RAWETH_CONFIG_ARGC 4 #define RAWETH_CONFIG_IFACE_KEY 0x01 @@ -120,7 +123,7 @@ static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { size_t size = 0; strcpy(s_mapping, cfg_str); // Parse list - char delim[] = ","; + char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; char *entry = strtok(s_mapping, delim); while (entry != NULL) { // Check entry @@ -157,7 +160,7 @@ static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { strcpy(s_whitelist, cfg_str); // Parse list size_t size = 0; - char delim[] = ","; + char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; char *entry = strtok(s_whitelist, delim); while (entry != NULL) { // Check entry @@ -192,7 +195,7 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel } size_t idx = 0; // Parse list - char delim[] = ","; + char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; char *entry = strtok(s_whitelist, delim); while ((entry != NULL) && (idx < array->_len)) { // Convert address from string to int array @@ -220,10 +223,15 @@ static _Bool _z_valid_mapping_entry(const char *entry) { const char *p_start = &entry[0]; const char *p_end = NULL; char c = '\0'; + // Parse first tuple member (keyexpr) do { c = entry[idx]; idx += 1; - } while ((idx < len) && (c != ' ')); + // Out of bounds check + if (idx >= len) { + return false; + } + } while (c != RAWETH_CFG_TUPLE_SEPARATOR); return false; } From 66bb7128d9818d378a5374009d08aa55b12ece64 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 26 Feb 2024 16:45:57 +0100 Subject: [PATCH 06/13] feat: add mapping function --- include/zenoh-pico/system/link/raweth.h | 5 +- src/system/unix/link/raweth.c | 3 + src/transport/raweth/link.c | 120 +++++++++++++++++++----- 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index 1b04bde47..6ac5523d8 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -40,7 +40,10 @@ typedef struct { _Bool _has_vlan; } _zp_raweth_mapping_entry_t; -_Z_ELEM_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t, _z_noop_size, _z_noop_clear, _z_noop_copy) +void _z_raweth_clear_mapping_entry(_zp_raweth_mapping_entry_t *entry); + +_Z_ELEM_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t, _z_noop_size, _z_raweth_clear_mapping_entry, + _z_noop_copy) _Z_ARRAY_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t) typedef struct { diff --git a/src/system/unix/link/raweth.c b/src/system/unix/link/raweth.c index 8c64359f0..1befc302d 100644 --- a/src/system/unix/link/raweth.c +++ b/src/system/unix/link/raweth.c @@ -31,6 +31,7 @@ #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/system/platform/unix.h" #include "zenoh-pico/utils/logging.h" @@ -43,6 +44,8 @@ #else #include +void _z_raweth_clear_mapping_entry(_zp_raweth_mapping_entry_t *entry) { _z_keyexpr_clear(&entry->_keyexpr); } + int8_t _z_open_raweth(_z_sys_net_socket_t *sock, const char *interface) { int8_t ret = _Z_RES_OK; // Open a raw network socket in promiscuous mode diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 90d526975..8d452e2cf 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -70,11 +70,13 @@ const uint8_t _ZP_RAWETH_DEFAULT_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config); static const char *_z_get_iface_raweth(_z_str_intmap_t *config); static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config); -static int _z_get_ethtype_raweth(_z_str_intmap_t *config); +static long _z_get_ethtype_raweth(_z_str_intmap_t *config); static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config); static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_array_t *array, size_t size); static size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config); static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitelist_array_t *array, size_t size); +static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *storage); +static _Bool _z_valid_mapping_entry(char *entry); static _Bool _z_valid_address_raweth(const char *address); static uint8_t *_z_parse_address_raweth(const char *address); static int8_t _z_f_link_open_raweth(_z_link_t *self); @@ -101,13 +103,13 @@ static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config) { if (s_ethtype == NULL) { return false; } - int ethtype = atoi(s_ethtype); + long ethtype = strtol(s_ethtype, NULL, 16); return ((ethtype & 0xff) > 0x6); // Ethtype must be above 0x600 in network order } -static int _z_get_ethtype_raweth(_z_str_intmap_t *config) { +static long _z_get_ethtype_raweth(_z_str_intmap_t *config) { const char *s_ethtype = _z_str_intmap_get(config, RAWETH_CONFIG_ETHTYPE_KEY); - return atoi(s_ethtype); + return strtol(s_ethtype, NULL, 16); } static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { @@ -127,6 +129,10 @@ static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { char *entry = strtok(s_mapping, delim); while (entry != NULL) { // Check entry + if (!_z_valid_mapping_entry(entry)) { + zp_free(s_mapping); + return 0; + } size += 1; entry = strtok(s_mapping, delim); } @@ -138,11 +144,34 @@ static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_array_t *array, size_t size) { // Retrieve data const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_MAPPING_KEY); + if (cfg_str == NULL) { + return _Z_ERR_GENERIC; + } // Copy data + char *s_mapping = zp_malloc(strlen(cfg_str)); + if (s_mapping == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + strcpy(s_mapping, cfg_str); // Allocate array *array = _zp_raweth_mapping_array_make(size); - // Store size - // Copy data + if (_zp_raweth_mapping_array_len(array) == 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + size_t idx = 0; + // Parse list + char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; + char *entry = strtok(s_mapping, delim); + while ((entry != NULL) && (idx < _zp_raweth_mapping_array_len(array))) { + // Copy data into array + _Z_CLEAN_RETURN_IF_ERR(_z_get_mapping_entry(entry, _zp_raweth_mapping_array_get(array, idx)), + zp_free(s_mapping)); + // Next iteration + idx += 1; + entry = strtok(s_mapping, delim); + } + // Clean up + zp_free(s_mapping); return _Z_RES_OK; } @@ -190,14 +219,14 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel strcpy(s_whitelist, cfg_str); // Allocate array *array = _zp_raweth_whitelist_array_make(size); - if (array->_len == 0) { + if (_zp_raweth_whitelist_array_len(array) == 0) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } size_t idx = 0; // Parse list char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; char *entry = strtok(s_whitelist, delim); - while ((entry != NULL) && (idx < array->_len)) { + while ((entry != NULL) && (idx < _zp_raweth_whitelist_array_len(array))) { // Convert address from string to int array uint8_t *addr = _z_parse_address_raweth(entry); if (addr == NULL) { @@ -216,23 +245,66 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel return _Z_RES_OK; } -static _Bool _z_valid_mapping_entry(const char *entry) { +static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *storage) { + size_t len = strlen(entry); + const char *entry_end = entry + (len - 1); + + // Get first tuple member (keyexpr) + char *p_start = &entry[0]; + char *p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + size_t ke_len = p_end - p_start; + char *ke_suffix = (char *)zp_malloc(ke_len); + if (ke_suffix == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + memcpy(ke_suffix, p_start, ke_len); + storage->_keyexpr = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, ke_suffix); + + // Check second entry (address) + p_start = p_end + 1; + p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + *p_end = '\0'; + uint8_t *addr = _z_parse_address_raweth(p_start); + memcpy(storage->_dmac, addr, _ZP_MAC_ADDR_LENGTH); + zp_free(addr); + *p_end = RAWETH_CFG_TUPLE_SEPARATOR; + + // Check optional third entry (vlan id) + p_start = p_end + 1; + if (p_start >= entry_end) { // No entry + storage->_has_vlan = false; + } else { + storage->_has_vlan = true; + storage->_vlan = (uint16_t)strtol(p_start, NULL, 16); + } + return _Z_RES_OK; +} +static _Bool _z_valid_mapping_entry(char *entry) { // some/key/expr 01:23:45:67:89:ab 12,another/ke aa:bb:cc:dd:ee:ff size_t len = strlen(entry); - size_t idx = 0; - const char *p_start = &entry[0]; - const char *p_end = NULL; - char c = '\0'; - // Parse first tuple member (keyexpr) - do { - c = entry[idx]; - idx += 1; - // Out of bounds check - if (idx >= len) { - return false; - } - } while (c != RAWETH_CFG_TUPLE_SEPARATOR); - return false; + const char *entry_end = entry + (len - 1); + // Check first tuple member (keyexpr) + char *p_start = &entry[0]; + char *p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + if (p_end == NULL) { + return false; + } + // Check second entry (address) + p_start = p_end + 1; + if (p_start > entry_end) { + return false; + } + p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + if (p_end == NULL) { + return false; + } + *p_end = '\0'; + if (!_z_valid_address_raweth(p_start)) { + *p_end = RAWETH_CFG_TUPLE_SEPARATOR; + return false; + } + *p_end = RAWETH_CFG_TUPLE_SEPARATOR; + return true; } static _Bool _z_valid_address_raweth(const char *address) { @@ -297,7 +369,7 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { } // Init socket ethtype if (_z_valid_ethtype_raweth(&self->_endpoint._config)) { - self->_socket._raweth._ethtype = _z_get_ethtype_raweth(&self->_endpoint._config); + self->_socket._raweth._ethtype = (uint16_t)_z_get_ethtype_raweth(&self->_endpoint._config); } else { self->_socket._raweth._ethtype = _ZP_RAWETH_DEFAULT_ETHTYPE; } From 4de8edb62d96a5d4cce975c669b2426b8afccd07 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 28 Feb 2024 11:24:18 +0100 Subject: [PATCH 07/13] fix: add default raweth mapping --- src/transport/raweth/link.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 8d452e2cf..538ac12c4 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -58,14 +58,8 @@ const uint16_t _ZP_RAWETH_DEFAULT_ETHTYPE = 0x72e0; const char *_ZP_RAWETH_DEFAULT_INTERFACE = "lo"; const uint8_t _ZP_RAWETH_DEFAULT_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}; -// const _zp_raweth_mapping_entry_t _ZP_RAWETH_DEFAULT_MAPPING[] = { -// {{0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}, // Default mac -// addr -// {{0, {0}, "some/key/expr"}, 0x8c00, {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, true}, // entry1 -// {{0, {0}, "demo/example/zenoh-pico-pub"}, 0xab00, {0x41, 0x55, 0xa8, 0x00, 0x9d, 0xc0}, true}, // entry2 -// {{0, {0}, "another/keyexpr"}, 0x4300, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab}, true}, // entry3 -// }; -// some/key/expr 01:23:45:67:89:ab 12,another/ke aa:bb:cc:dd:ee:ff +const _zp_raweth_mapping_entry_t _ZP_RAWETH_DEFAULT_MAPPING = { + {0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}; static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config); static const char *_z_get_iface_raweth(_z_str_intmap_t *config); @@ -378,7 +372,12 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { if (size != 0) { _Z_RETURN_IF_ERR(_z_get_mapping_raweth(&self->_endpoint._config, &self->_socket._raweth._mapping, size)); } else { - // self->_socket._raweth._mapping = _ZP_RAWETH_DEFAULT_MAPPING; + self->_socket._raweth._mapping = _zp_raweth_mapping_array_make(1); + if (_zp_raweth_mapping_array_len(&self->_socket._raweth._mapping) == 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&self->_socket._raweth._mapping, 0); + *entry = _ZP_RAWETH_DEFAULT_MAPPING; } // Init socket whitelist size = _z_valid_whitelist_raweth(&self->_endpoint._config); From 776fd7f2eed91cd52ce92d05fbe9d6721cf9f0bf Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 28 Feb 2024 16:40:05 +0100 Subject: [PATCH 08/13] feat: add debug traces on raweth config parsing --- src/transport/raweth/link.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 538ac12c4..f57808659 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -23,6 +23,7 @@ #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/pointers.h" #if Z_FEATURE_RAWETH_TRANSPORT == 1 @@ -274,7 +275,6 @@ static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *stor return _Z_RES_OK; } static _Bool _z_valid_mapping_entry(char *entry) { - // some/key/expr 01:23:45:67:89:ab 12,another/ke aa:bb:cc:dd:ee:ff size_t len = strlen(entry); const char *entry_end = entry + (len - 1); // Check first tuple member (keyexpr) @@ -353,18 +353,21 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { memcpy(&self->_socket._raweth._smac, addr, _ZP_MAC_ADDR_LENGTH); zp_free(addr); } else { + _Z_DEBUG("Invalid locator source mac addr, using default value."); memcpy(&self->_socket._raweth._smac, _ZP_RAWETH_DEFAULT_SMAC, _ZP_MAC_ADDR_LENGTH); } // Init socket interface if (_z_valid_iface_raweth(&self->_endpoint._config)) { self->_socket._raweth._interface = _z_get_iface_raweth(&self->_endpoint._config); } else { + _Z_DEBUG("Invalid locator interface, using default value %s", _ZP_RAWETH_DEFAULT_INTERFACE); self->_socket._raweth._interface = _ZP_RAWETH_DEFAULT_INTERFACE; } // Init socket ethtype if (_z_valid_ethtype_raweth(&self->_endpoint._config)) { self->_socket._raweth._ethtype = (uint16_t)_z_get_ethtype_raweth(&self->_endpoint._config); } else { + _Z_DEBUG("Invalid locator ethtype, using default value 0x%04x", _ZP_RAWETH_DEFAULT_ETHTYPE); self->_socket._raweth._ethtype = _ZP_RAWETH_DEFAULT_ETHTYPE; } // Init socket mapping @@ -372,6 +375,7 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { if (size != 0) { _Z_RETURN_IF_ERR(_z_get_mapping_raweth(&self->_endpoint._config, &self->_socket._raweth._mapping, size)); } else { + _Z_DEBUG("Invalid locator mapping, using default value."); self->_socket._raweth._mapping = _zp_raweth_mapping_array_make(1); if (_zp_raweth_mapping_array_len(&self->_socket._raweth._mapping) == 0) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -383,6 +387,8 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { size = _z_valid_whitelist_raweth(&self->_endpoint._config); if (size != 0) { _Z_RETURN_IF_ERR(_z_get_whitelist_raweth(&self->_endpoint._config, &self->_socket._raweth._whitelist, size)); + } else { + _Z_DEBUG("Invalid locator whitelist, filtering deactivated."); } // Open raweth link return _z_open_raweth(&self->_socket._raweth._sock, self->_socket._raweth._interface); From a9994a647e7c8954be2e3f318a0b27c5de0214c4 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 28 Feb 2024 16:40:22 +0100 Subject: [PATCH 09/13] fix: ethtype validity test --- src/transport/raweth/link.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index f57808659..e9d4aef00 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -99,7 +99,7 @@ static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config) { return false; } long ethtype = strtol(s_ethtype, NULL, 16); - return ((ethtype & 0xff) > 0x6); // Ethtype must be above 0x600 in network order + return(_z_raweth_htons(ethtype) > 0x600); // Ethtype must be at least 0x600 in network order } static long _z_get_ethtype_raweth(_z_str_intmap_t *config) { From f210d9e2b31707ba6fc137a11f8eec0ed81bcd7b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 28 Feb 2024 16:40:43 +0100 Subject: [PATCH 10/13] fix: strtok parsing calls --- src/transport/raweth/link.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index e9d4aef00..a88598176 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -129,7 +129,7 @@ static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { return 0; } size += 1; - entry = strtok(s_mapping, delim); + entry = strtok(NULL, delim); } // Clean up zp_free(s_mapping); @@ -163,7 +163,7 @@ static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_ zp_free(s_mapping)); // Next iteration idx += 1; - entry = strtok(s_mapping, delim); + entry = strtok(NULL, delim); } // Clean up zp_free(s_mapping); @@ -193,8 +193,10 @@ static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { return 0; } size += 1; - entry = strtok(s_whitelist, delim); + entry = strtok(NULL, delim); } + // Parse last entry + // Clean up zp_free(s_whitelist); return size; @@ -233,7 +235,7 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel zp_free(addr); // Next iteration idx += 1; - entry = strtok(s_whitelist, delim); + entry = strtok(NULL, delim); } // Clean up zp_free(s_whitelist); From 224db0d7980e9d7da9b8f3ec2ab0a55dcc19d6e8 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 28 Feb 2024 16:41:03 +0100 Subject: [PATCH 11/13] chore: clang-format --- src/transport/raweth/link.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index a88598176..83f69fdb5 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -99,7 +99,7 @@ static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config) { return false; } long ethtype = strtol(s_ethtype, NULL, 16); - return(_z_raweth_htons(ethtype) > 0x600); // Ethtype must be at least 0x600 in network order + return (_z_raweth_htons(ethtype) > 0x600); // Ethtype must be at least 0x600 in network order } static long _z_get_ethtype_raweth(_z_str_intmap_t *config) { From 5636bfa36d2f73c4d347afb90b14b7d595824cac Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 28 Feb 2024 17:31:41 +0100 Subject: [PATCH 12/13] fix: include first mapping entry in lookup --- src/transport/raweth/tx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index 56af858a7..5c82b84ac 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -37,7 +37,7 @@ static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUS #endif static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { - for (int i = 1; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { + for (int i = 0; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { // Find matching keyexpr _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, i); if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, entry->_keyexpr._suffix) != _Z_RES_OK) { From 061f071b8aab18aa9cd23bd04b3ba6015fa54f2b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 5 Mar 2024 09:59:36 +0100 Subject: [PATCH 13/13] fix: appease the tyran codacy --- src/system/unix/link/raweth.c | 2 +- src/transport/raweth/link.c | 38 +++++++++++++++++++---------------- src/transport/raweth/tx.c | 6 +++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/system/unix/link/raweth.c b/src/system/unix/link/raweth.c index 1befc302d..10e35c565 100644 --- a/src/system/unix/link/raweth.c +++ b/src/system/unix/link/raweth.c @@ -103,7 +103,7 @@ size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buf // Address filtering (only if there is a whitelist) if (_zp_raweth_whitelist_array_len(whitelist) > 0) { is_valid = false; - _zp_eth_header_t *header = (_zp_eth_header_t *)buff; + const _zp_eth_header_t *header = (_zp_eth_header_t *)buff; for (size_t i = 0; i < _zp_raweth_whitelist_array_len(whitelist); i++) { const _zp_raweth_whitelist_entry_t *entry = _zp_raweth_whitelist_array_get(whitelist, i); if (memcmp(&header->smac, entry->_mac, _ZP_MAC_ADDR_LENGTH) == 0) { diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 83f69fdb5..6cb90877c 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -29,7 +29,7 @@ #if Z_FEATURE_RAWETH_TRANSPORT == 1 #define RAWETH_CFG_TUPLE_SEPARATOR '#' -#define RAWETH_CFG_LIST_SEPARATOR ',' +#define RAWETH_CFG_LIST_SEPARATOR "," #define RAWETH_CONFIG_ARGC 4 @@ -120,7 +120,7 @@ static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { size_t size = 0; strcpy(s_mapping, cfg_str); // Parse list - char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; + const char *delim = RAWETH_CFG_LIST_SEPARATOR; char *entry = strtok(s_mapping, delim); while (entry != NULL) { // Check entry @@ -128,7 +128,7 @@ static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { zp_free(s_mapping); return 0; } - size += 1; + size++; entry = strtok(NULL, delim); } // Clean up @@ -155,14 +155,14 @@ static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_ } size_t idx = 0; // Parse list - char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; + const char *delim = RAWETH_CFG_LIST_SEPARATOR; char *entry = strtok(s_mapping, delim); while ((entry != NULL) && (idx < _zp_raweth_mapping_array_len(array))) { // Copy data into array _Z_CLEAN_RETURN_IF_ERR(_z_get_mapping_entry(entry, _zp_raweth_mapping_array_get(array, idx)), zp_free(s_mapping)); // Next iteration - idx += 1; + idx++; entry = strtok(NULL, delim); } // Clean up @@ -184,7 +184,7 @@ static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { strcpy(s_whitelist, cfg_str); // Parse list size_t size = 0; - char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; + const char *delim = RAWETH_CFG_LIST_SEPARATOR; char *entry = strtok(s_whitelist, delim); while (entry != NULL) { // Check entry @@ -192,7 +192,7 @@ static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { zp_free(s_whitelist); return 0; } - size += 1; + size++; entry = strtok(NULL, delim); } // Parse last entry @@ -221,7 +221,7 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel } size_t idx = 0; // Parse list - char delim[] = {RAWETH_CFG_LIST_SEPARATOR}; + const char *delim = RAWETH_CFG_LIST_SEPARATOR; char *entry = strtok(s_whitelist, delim); while ((entry != NULL) && (idx < _zp_raweth_whitelist_array_len(array))) { // Convert address from string to int array @@ -234,7 +234,7 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel memcpy(elem->_mac, addr, _ZP_MAC_ADDR_LENGTH); zp_free(addr); // Next iteration - idx += 1; + idx++; entry = strtok(NULL, delim); } // Clean up @@ -244,12 +244,12 @@ static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitel static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *storage) { size_t len = strlen(entry); - const char *entry_end = entry + (len - 1); + const char *entry_end = &entry[len - (size_t)1]; // Get first tuple member (keyexpr) char *p_start = &entry[0]; char *p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); - size_t ke_len = p_end - p_start; + size_t ke_len = (uintptr_t)p_end - (uintptr_t)p_start; char *ke_suffix = (char *)zp_malloc(ke_len); if (ke_suffix == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -258,7 +258,8 @@ static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *stor storage->_keyexpr = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, ke_suffix); // Check second entry (address) - p_start = p_end + 1; + p_start = p_end; + p_start++; p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); *p_end = '\0'; uint8_t *addr = _z_parse_address_raweth(p_start); @@ -267,7 +268,8 @@ static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *stor *p_end = RAWETH_CFG_TUPLE_SEPARATOR; // Check optional third entry (vlan id) - p_start = p_end + 1; + p_start = p_end; + p_start++; if (p_start >= entry_end) { // No entry storage->_has_vlan = false; } else { @@ -278,7 +280,8 @@ static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *stor } static _Bool _z_valid_mapping_entry(char *entry) { size_t len = strlen(entry); - const char *entry_end = entry + (len - 1); + const char *entry_end = &entry[len - (size_t)1]; + // Check first tuple member (keyexpr) char *p_start = &entry[0]; char *p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); @@ -286,7 +289,8 @@ static _Bool _z_valid_mapping_entry(char *entry) { return false; } // Check second entry (address) - p_start = p_end + 1; + p_start = p_end; + p_start++; if (p_start > entry_end) { return false; } @@ -374,7 +378,7 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { } // Init socket mapping size_t size = _z_valid_mapping_raweth(&self->_endpoint._config); - if (size != 0) { + if (size != (size_t)0) { _Z_RETURN_IF_ERR(_z_get_mapping_raweth(&self->_endpoint._config, &self->_socket._raweth._mapping, size)); } else { _Z_DEBUG("Invalid locator mapping, using default value."); @@ -387,7 +391,7 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { } // Init socket whitelist size = _z_valid_whitelist_raweth(&self->_endpoint._config); - if (size != 0) { + if (size != (size_t)0) { _Z_RETURN_IF_ERR(_z_get_whitelist_raweth(&self->_endpoint._config, &self->_socket._raweth._whitelist, size)); } else { _Z_DEBUG("Invalid locator whitelist, filtering deactivated."); diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index 5c82b84ac..3eb399d6f 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -39,7 +39,7 @@ static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUS static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { for (int i = 0; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { // Find matching keyexpr - _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, i); + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, i); if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, entry->_keyexpr._suffix) != _Z_RES_OK) { continue; } @@ -56,7 +56,7 @@ static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socke } if (keyexpr == NULL) { // Store default value into socket - _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, 0); + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, 0); memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); uint16_t vlan = entry->_vlan; sock->_has_vlan = entry->_has_vlan; @@ -72,7 +72,7 @@ static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socke _Z_DEBUG("Key '%s' wasn't found in config mapping, sending to default address", keyexpr->_suffix); } // Store data into socket - _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, idx); + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, idx); memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); uint16_t vlan = entry->_vlan; sock->_has_vlan = entry->_has_vlan;