Skip to content

Commit c74070b

Browse files
committed
chore: improve ip_utils
1 parent bbf740e commit c74070b

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/utils/ip_utils.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace duckdb
1818
size_t slashPos = ipWithMask.find ('/');
1919
std::string ip = ipWithMask.substr (0, slashPos);
2020

21-
// Default to /32 if no subnet mask is provided
22-
int maskBits = 32;
21+
// Default to /24 if no subnet mask is provided
22+
int maskBits = 24;
2323
if (slashPos != std::string::npos)
2424
{
2525
std::string maskStr = ipWithMask.substr (slashPos + 1);
@@ -58,18 +58,15 @@ namespace duckdb
5858

5959
if (maskBits != 32)
6060
{
61-
info.network = getNetworkAddress (ip, subnetMask);
61+
info.network = getNetworkAddress (ip, subnetMask, maskBits);
6262
info.broadcast = getBroadcastAddress (info.network, wildcardMask);
6363
info.hostMin = getHostMin (info.network);
6464
info.hostMax = getHostMax (info.broadcast);
6565
}
6666
else
6767
{
68-
// For /32, the IP itself is the only host
69-
info.network = ip;
70-
info.broadcast = ip;
71-
info.hostMin = ip;
72-
info.hostMax = ip;
68+
// For /32, the IP itself is the only host ( Hostroute )
69+
info.network = ip;
7370
}
7471

7572
return info;
@@ -123,7 +120,7 @@ namespace duckdb
123120
return wildcard;
124121
}
125122

126-
std::string IPCalculator::getNetworkAddress (const std::string &ip, const std::string &subnetMask)
123+
std::string IPCalculator::getNetworkAddress (const std::string &ip, const std::string &subnetMask, const int &maskBits)
127124
{
128125
auto ipOctets = parseIP (ip);
129126
auto maskOctets = parseIP (subnetMask);
@@ -132,7 +129,7 @@ namespace duckdb
132129
{
133130
network += std::to_string (ipOctets[i] & maskOctets[i]) + (i < 3 ? "." : "");
134131
}
135-
return network;
132+
return network + "/" + std::to_string (maskBits);
136133
}
137134

138135
std::string IPCalculator::getBroadcastAddress (const std::string &networkAddress, const std::string &wildcardMask)

src/utils/ip_utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace duckdb
3030
static std::vector<int> parseIP (const std::string &ip);
3131
static std::string getSubnetMask (int maskBits);
3232
static std::string getWildcardMask (const std::string &subnetMask);
33-
static std::string getNetworkAddress (const std::string &ip, const std::string &subnetMask);
33+
static std::string getNetworkAddress (const std::string &ip, const std::string &subnetMask, const int &maskBits);
3434
static std::string getBroadcastAddress (const std::string &networkAddress, const std::string &wildcardMask);
3535
static std::string getHostMin (const std::string &networkAddress);
3636
static std::string getHostMax (const std::string &broadcastAddress);

0 commit comments

Comments
 (0)