Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ target_link_libraries(network
utils
Qt6::Core
Qt6::Network)

target_link_libraries(ip_address
fmt::fmt-header-only)
13 changes: 6 additions & 7 deletions src/network/ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <sstream>
#include <stdexcept>

#include <fmt/format.h>

namespace mp = multipass;

namespace
Expand All @@ -29,10 +31,9 @@ uint8_t as_octet(uint32_t value)
return static_cast<uint8_t>(value);
}

void check_range(int value)
bool is_valid_octet(int value)
{
if (value < 0 || value > 255)
throw std::invalid_argument("invalid IP octet");
return value >= 0 && value < 256;
}

std::array<uint8_t, 4> parse(const std::string& ip)
Expand All @@ -45,10 +46,8 @@ std::array<uint8_t, 4> parse(const std::string& ip)
std::stringstream s(ip);
s >> a >> ch >> b >> ch >> c >> ch >> d;

check_range(a);
check_range(b);
check_range(c);
check_range(d);
if (!is_valid_octet(a) || !is_valid_octet(b) || !is_valid_octet(c) || !is_valid_octet(d))
throw std::invalid_argument(fmt::format("invalid IP address {}", ip));

return {{as_octet(a), as_octet(b), as_octet(c), as_octet(d)}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mp::Subnet get_subnet(const mp::Path& data_dir)
if (MP_FILEOPS.size(subnet_file) > 0)
{
auto content = MP_FILEOPS.read_all(subnet_file).trimmed().toStdString();
mp::Subnet{mp::IPAddress{content + ".0"}, subnet_prefix_length};
return mp::Subnet{mp::IPAddress{content + ".0"}, subnet_prefix_length};
}

auto new_subnet = subnet_allocator.next_available();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_subnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TEST(Subet, throwsOnInvalidIP)

MP_EXPECT_THROW_THAT(mp::Subnet{"192.168.XXX.XXX/16"},
std::invalid_argument,
mpt::match_what(HasSubstr("invalid IP octet")));
mpt::match_what(HasSubstr("invalid IP address 192.168.XXX.XXX")));
}

TEST(SubnetTest, throwsOnLargePrefixLength)
Expand Down
Loading