Skip to content

Commit

Permalink
Regex improvements for IPv4Address.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen committed Dec 10, 2024
1 parent 128ae8f commit e6977ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions ecal/core/include/ecal/types/ecal_custom_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 10 additions & 5 deletions ecal/core/src/types/ecal_custom_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

namespace{
const std::array<const std::regex, 3> 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
Expand All @@ -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)
Expand Down

0 comments on commit e6977ac

Please sign in to comment.