From e6977acd4ac573465d67c249cd121b34ef23ca46 Mon Sep 17 00:00:00 2001 From: Peguen <73380451+Peguen@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:33:55 +0100 Subject: [PATCH] Regex improvements for IPv4Address. --- .../include/ecal/types/ecal_custom_data_types.h | 1 + ecal/core/src/types/ecal_custom_data_types.cpp | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ecal/core/include/ecal/types/ecal_custom_data_types.h b/ecal/core/include/ecal/types/ecal_custom_data_types.h index 6907519f1f..8f931e5462 100644 --- a/ecal/core/include/ecal/types/ecal_custom_data_types.h +++ b/ecal/core/include/ecal/types/ecal_custom_data_types.h @@ -47,6 +47,7 @@ namespace eCAL { public: ECAL_API IpAddressV4(const std::string& ip_address_); + ECAL_API IpAddressV4(const char* ip_address_); ECAL_API std::string Get() const; diff --git a/ecal/core/src/types/ecal_custom_data_types.cpp b/ecal/core/src/types/ecal_custom_data_types.cpp index e6fe22c541..f79d13011d 100644 --- a/ecal/core/src/types/ecal_custom_data_types.cpp +++ b/ecal/core/src/types/ecal_custom_data_types.cpp @@ -31,12 +31,12 @@ namespace{ const std::array INVALID_IPV4_ADDRESSES = { - std::regex("((255|[fF][fF])\\.){3}(255|[fF][fF])"), // 255.255.255.255 - std::regex("((127|7[fF]).((0|00|000)\\.){2}(1|01|001))"), // 127.0.0.1 - std::regex("((0|00|000)\\.){3}(0|00|000)") // 0.0.0.0 + std::regex("^(((255)|([fF]{2}))\\.){3}((255)|([fF]{2}))$"), // 255.255.255.255 + std::regex("((127|7[fF]).((0|00|000)\\.){2}(1|01|001))"), // 127.0.0.1 + std::regex("((0|00|000)\\.){3}(0|00|000)") // 0.0.0.0 }; - const std::regex IPV4_DEC_REGEX = std::regex("(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"); - const std::regex IPV4_HEX_REGEX = std::regex("(([0-9a-fA-F]|[0-9a-fA-F][0-9a-fA-F])\\.){3}([0-9a-fA-F]|[0-9a-fA-F][0-9a-fA-F])"); + const std::regex IPV4_DEC_REGEX = std::regex("^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$"); + const std::regex IPV4_HEX_REGEX = std::regex("^([0-9a-fA-F]{1,2}\\.){3}[0-9a-fA-F]{1,2}$"); } namespace eCAL @@ -48,6 +48,11 @@ namespace eCAL validateIpString(ip_address_); } + IpAddressV4::IpAddressV4(const char* ip_address_) + { + validateIpString(ip_address_); + } + void IpAddressV4::validateIpString(const std::string& ip_address_) { if ( std::regex_match(ip_address_, IPV4_DEC_REGEX)