Driver: fortconf: Refactor FORT_CONF_ADDR_LIST

This commit is contained in:
Nodir Temirkhodjaev 2024-10-27 16:53:44 +05:00
parent 1f27fd338f
commit cd73f49f83
7 changed files with 64 additions and 93 deletions

View File

@ -89,9 +89,10 @@ static BOOL fort_conf_ip6_find(
#define fort_conf_ip6_inrange(iprange, ip, count) \
fort_conf_ip6_find(iprange, ip, count, /*is_range=*/TRUE)
#define fort_conf_addr_list_ip6_ref(addr6_list) (addr6_list)->ip
#define fort_conf_addr_list_ip6_ref(addr6_list) ((ip6_addr_t *) (addr6_list)->ip)
#define fort_conf_addr_list_pair6_ref(addr6_list) &(addr6_list)->ip[(addr6_list)->ip_n]
#define fort_conf_addr_list_pair6_ref(addr6_list) \
(fort_conf_addr_list_ip6_ref(addr6_list) + (addr6_list)->ip_n)
FORT_API int fort_mem_cmp(const void *p1, const void *p2, UINT32 len)
{
@ -105,13 +106,12 @@ FORT_API BOOL fort_mem_eql(const void *p1, const void *p2, UINT32 len)
}
FORT_API BOOL fort_conf_ip_inlist(
const UINT32 *ip, const PFORT_CONF_ADDR4_LIST addr_list, BOOL isIPv6)
const UINT32 *ip, const PFORT_CONF_ADDR_LIST addr_list, BOOL isIPv6)
{
if (isIPv6) {
const ip6_addr_t *ip6 = (const ip6_addr_t *) ip;
const PFORT_CONF_ADDR6_LIST addr6_list =
(const PFORT_CONF_ADDR6_LIST)((const PCHAR) addr_list
+ FORT_CONF_ADDR4_LIST_SIZE(addr_list->ip_n, addr_list->pair_n));
const PFORT_CONF_ADDR_LIST addr6_list = (const PFORT_CONF_ADDR_LIST)((const PCHAR) addr_list
+ FORT_CONF_ADDR4_LIST_SIZE(addr_list->ip_n, addr_list->pair_n));
return fort_conf_ip6_inarr(fort_conf_addr_list_ip6_ref(addr6_list), ip6, addr6_list->ip_n)
|| fort_conf_ip6_inrange(
@ -131,7 +131,7 @@ FORT_API PFORT_CONF_ADDR_GROUP fort_conf_addr_group_ref(const PFORT_CONF conf, i
return (PFORT_CONF_ADDR_GROUP) (addr_group_data + addr_group_offsets[addr_group_index]);
}
static BOOL fort_conf_ip_included_check(const PFORT_CONF_ADDR4_LIST addr_list,
static BOOL fort_conf_ip_included_check(const PFORT_CONF_ADDR_LIST addr_list,
fort_conf_zones_ip_included_func zone_func, void *ctx, const UINT32 *remote_ip,
UINT32 zones_mask, BOOL list_is_empty, BOOL isIPv6)
{

View File

@ -109,21 +109,13 @@ typedef struct fort_conf_port_list
UINT16 port[1];
} FORT_CONF_PORT_LIST, *PFORT_CONF_PORT_LIST;
typedef struct fort_conf_addr4_list
typedef struct fort_conf_addr_list
{
UINT32 ip_n;
UINT32 pair_n;
UINT32 ip[1];
} FORT_CONF_ADDR4_LIST, *PFORT_CONF_ADDR4_LIST;
typedef struct fort_conf_addr6_list
{
UINT32 ip_n;
UINT32 pair_n;
ip6_addr_t ip[1];
} FORT_CONF_ADDR6_LIST, *PFORT_CONF_ADDR6_LIST;
} FORT_CONF_ADDR_LIST, *PFORT_CONF_ADDR_LIST;
typedef struct fort_conf_addr_group
{
@ -330,16 +322,15 @@ typedef struct fort_conf_io
#define FORT_CONF_DATA_OFF offsetof(FORT_CONF, data)
#define FORT_CONF_IO_CONF_OFF offsetof(FORT_CONF_IO, conf)
#define FORT_CONF_ADDR4_LIST_OFF offsetof(FORT_CONF_ADDR4_LIST, ip)
#define FORT_CONF_ADDR6_LIST_OFF offsetof(FORT_CONF_ADDR6_LIST, ip)
#define FORT_CONF_ADDR_LIST_OFF offsetof(FORT_CONF_ADDR_LIST, ip)
#define FORT_CONF_ADDR_GROUP_OFF offsetof(FORT_CONF_ADDR_GROUP, data)
#define FORT_CONF_ZONES_DATA_OFF offsetof(FORT_CONF_ZONES, data)
#define FORT_CONF_ADDR4_LIST_SIZE(ip_n, pair_n) \
(FORT_CONF_ADDR4_LIST_OFF + FORT_CONF_IP4_ARR_SIZE(ip_n) + FORT_CONF_IP4_RANGE_SIZE(pair_n))
(FORT_CONF_ADDR_LIST_OFF + FORT_CONF_IP4_ARR_SIZE(ip_n) + FORT_CONF_IP4_RANGE_SIZE(pair_n))
#define FORT_CONF_ADDR6_LIST_SIZE(ip_n, pair_n) \
(FORT_CONF_ADDR6_LIST_OFF + FORT_CONF_IP6_ARR_SIZE(ip_n) + FORT_CONF_IP6_RANGE_SIZE(pair_n))
(FORT_CONF_ADDR_LIST_OFF + FORT_CONF_IP6_ARR_SIZE(ip_n) + FORT_CONF_IP6_RANGE_SIZE(pair_n))
#define FORT_CONF_ADDR_LIST_SIZE(ip4_n, pair4_n, ip6_n, pair6_n) \
(FORT_CONF_ADDR4_LIST_SIZE(ip4_n, pair4_n) + FORT_CONF_ADDR6_LIST_SIZE(ip6_n, pair6_n))
@ -359,16 +350,16 @@ FORT_API int fort_mem_cmp(const void *p1, const void *p2, UINT32 len);
FORT_API BOOL fort_mem_eql(const void *p1, const void *p2, UINT32 len);
FORT_API BOOL fort_conf_ip_inlist(
const UINT32 *ip, const PFORT_CONF_ADDR4_LIST addr_list, BOOL isIPv6);
const UINT32 *ip, const PFORT_CONF_ADDR_LIST addr_list, BOOL isIPv6);
FORT_API PFORT_CONF_ADDR_GROUP fort_conf_addr_group_ref(
const PFORT_CONF conf, int addr_group_index);
#define fort_conf_addr_group_include_list_ref(addr_group) \
((PFORT_CONF_ADDR4_LIST) (addr_group)->data)
((PFORT_CONF_ADDR_LIST) (addr_group)->data)
#define fort_conf_addr_group_exclude_list_ref(addr_group) \
((PFORT_CONF_ADDR4_LIST) ((addr_group)->data + (addr_group)->exclude_off))
((PFORT_CONF_ADDR_LIST) ((addr_group)->data + (addr_group)->exclude_off))
FORT_API BOOL fort_conf_ip_included(const PFORT_CONF conf,
fort_conf_zones_ip_included_func zone_func, void *ctx, const UINT32 *remote_ip, BOOL isIPv6,

View File

@ -453,8 +453,8 @@ FORT_API BOOL fort_conf_zones_ip_included(
zones_mask &= (zones->mask & zones->enabled_mask);
while (zones_mask != 0) {
const int zone_index = bit_scan_forward(zones_mask);
PFORT_CONF_ADDR4_LIST addr_list =
(PFORT_CONF_ADDR4_LIST) (zones->data + zones->addr_off[zone_index]);
PFORT_CONF_ADDR_LIST addr_list =
(PFORT_CONF_ADDR_LIST) (zones->data + zones->addr_off[zone_index]);
if (fort_conf_ip_inlist(remote_ip, addr_list, isIPv6)) {
res = TRUE;

View File

@ -86,6 +86,7 @@ TEST_F(ConfUtilTest, confWriteRead)
ASSERT_TRUE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("192.168.255.255")));
ASSERT_FALSE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("193.0.0.0")));
ASSERT_TRUE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("239.255.255.250")));
ASSERT_TRUE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::1")));
ASSERT_TRUE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::2")));
ASSERT_TRUE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::ffff:0:2")));
ASSERT_FALSE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("65::")));

View File

@ -198,7 +198,7 @@ bool confIpInRange(
if (is_empty)
return false;
const PFORT_CONF_ADDR4_LIST addr_list = included
const PFORT_CONF_ADDR_LIST addr_list = included
? fort_conf_addr_group_include_list_ref(addr_group)
: fort_conf_addr_group_exclude_list_ref(addr_group);

View File

@ -121,11 +121,11 @@ QRegularExpressionMatch ConfUtil::matchWildcard(const QStringView &path)
void ConfUtil::migrateZoneData(char **data, const QByteArray &zoneData)
{
PFORT_CONF_ADDR4_LIST addr_list = (PFORT_CONF_ADDR4_LIST) zoneData.data();
PFORT_CONF_ADDR_LIST addr_list = (PFORT_CONF_ADDR_LIST) zoneData.data();
if (FORT_CONF_ADDR4_LIST_SIZE(addr_list->ip_n, addr_list->pair_n) == zoneData.size()) {
IpRange ipRange;
writeAddress6List(data, ipRange);
writeIpRange(data, ipRange, /*isIPv6=*/true);
}
}
@ -257,90 +257,70 @@ void ConfUtil::writeAddressRange(char **data, const AddressRange &addressRange)
void ConfUtil::writeAddressList(char **data, const IpRange &ipRange)
{
writeAddress4List(data, ipRange);
writeAddress6List(data, ipRange);
writeIpRange(data, ipRange);
writeIpRange(data, ipRange, /*isIPv6=*/true);
}
void ConfUtil::writeAddress4List(char **data, const IpRange &ipRange)
void ConfUtil::writeIpRange(char **data, const IpRange &ipRange, bool isIPv6)
{
PFORT_CONF_ADDR4_LIST addrList = PFORT_CONF_ADDR4_LIST(*data);
PFORT_CONF_ADDR_LIST addrList = PFORT_CONF_ADDR_LIST(*data);
addrList->ip_n = quint32(ipRange.ip4Size());
addrList->pair_n = quint32(ipRange.pair4Size());
addrList->ip_n = quint32(isIPv6 ? ipRange.ip6Size() : ipRange.ip4Size());
addrList->pair_n = quint32(isIPv6 ? ipRange.pair6Size() : ipRange.pair4Size());
*data += FORT_CONF_ADDR4_LIST_OFF;
*data += FORT_CONF_ADDR_LIST_OFF;
writeLongs(data, ipRange.ip4Array());
writeLongs(data, ipRange.pair4FromArray());
writeLongs(data, ipRange.pair4ToArray());
}
void ConfUtil::writeAddress6List(char **data, const IpRange &ipRange)
{
PFORT_CONF_ADDR6_LIST addrList = PFORT_CONF_ADDR6_LIST(*data);
addrList->ip_n = quint32(ipRange.ip6Size());
addrList->pair_n = quint32(ipRange.pair6Size());
*data += FORT_CONF_ADDR6_LIST_OFF;
writeIp6Array(data, ipRange.ip6Array());
writeIp6Array(data, ipRange.pair6FromArray());
writeIp6Array(data, ipRange.pair6ToArray());
if (isIPv6) {
writeIp6Array(data, ipRange.ip6Array());
writeIp6Array(data, ipRange.pair6FromArray());
writeIp6Array(data, ipRange.pair6ToArray());
} else {
writeLongs(data, ipRange.ip4Array());
writeLongs(data, ipRange.pair4FromArray());
writeLongs(data, ipRange.pair4ToArray());
}
}
bool ConfUtil::loadAddressList(const char **data, IpRange &ipRange, uint &bufSize)
{
return loadAddress4List(data, ipRange, bufSize)
&& (bufSize == 0 || loadAddress6List(data, ipRange, bufSize));
return loadIpRange(data, ipRange, bufSize)
&& (bufSize == 0 || loadIpRange(data, ipRange, bufSize, /*isIPv6=*/true));
}
bool ConfUtil::loadAddress4List(const char **data, IpRange &ipRange, uint &bufSize)
bool ConfUtil::loadIpRange(const char **data, IpRange &ipRange, uint &bufSize, bool isIPv6)
{
if (bufSize < FORT_CONF_ADDR4_LIST_OFF)
if (bufSize < FORT_CONF_ADDR_LIST_OFF)
return false;
PFORT_CONF_ADDR4_LIST addr_list = (PFORT_CONF_ADDR4_LIST) *data;
PFORT_CONF_ADDR_LIST addr_list = (PFORT_CONF_ADDR_LIST) *data;
*data = (const char *) addr_list->ip;
const uint addrListSize = FORT_CONF_ADDR4_LIST_SIZE(addr_list->ip_n, addr_list->pair_n);
const uint addrListSize = isIPv6
? FORT_CONF_ADDR6_LIST_SIZE(addr_list->ip_n, addr_list->pair_n)
: FORT_CONF_ADDR4_LIST_SIZE(addr_list->ip_n, addr_list->pair_n);
if (bufSize < addrListSize)
return false;
bufSize -= addrListSize;
ipRange.ip4Array().resize(addr_list->ip_n);
ipRange.pair4FromArray().resize(addr_list->pair_n);
ipRange.pair4ToArray().resize(addr_list->pair_n);
if (isIPv6) {
ipRange.ip6Array().resize(addr_list->ip_n);
ipRange.pair6FromArray().resize(addr_list->pair_n);
ipRange.pair6ToArray().resize(addr_list->pair_n);
loadLongs(data, ipRange.ip4Array());
loadLongs(data, ipRange.pair4FromArray());
loadLongs(data, ipRange.pair4ToArray());
loadIp6Array(data, ipRange.ip6Array());
loadIp6Array(data, ipRange.pair6FromArray());
loadIp6Array(data, ipRange.pair6ToArray());
} else {
ipRange.ip4Array().resize(addr_list->ip_n);
ipRange.pair4FromArray().resize(addr_list->pair_n);
ipRange.pair4ToArray().resize(addr_list->pair_n);
return true;
}
bool ConfUtil::loadAddress6List(const char **data, IpRange &ipRange, uint &bufSize)
{
if (bufSize < FORT_CONF_ADDR6_LIST_OFF)
return false;
PFORT_CONF_ADDR6_LIST addr_list = (PFORT_CONF_ADDR6_LIST) *data;
*data = (const char *) addr_list->ip;
const uint addrListSize = FORT_CONF_ADDR6_LIST_SIZE(addr_list->ip_n, addr_list->pair_n);
if (bufSize < addrListSize)
return false;
bufSize -= addrListSize;
ipRange.ip6Array().resize(addr_list->ip_n);
ipRange.pair6FromArray().resize(addr_list->pair_n);
ipRange.pair6ToArray().resize(addr_list->pair_n);
loadIp6Array(data, ipRange.ip6Array());
loadIp6Array(data, ipRange.pair6FromArray());
loadIp6Array(data, ipRange.pair6ToArray());
loadLongs(data, ipRange.ip4Array());
loadLongs(data, ipRange.pair4FromArray());
loadLongs(data, ipRange.pair4ToArray());
}
return true;
}

View File

@ -49,12 +49,11 @@ public:
static void writeAddressRange(char **data, const AddressRange &addressRange);
static void writeAddressList(char **data, const IpRange &ipRange);
static void writeAddress4List(char **data, const IpRange &ipRange);
static void writeAddress6List(char **data, const IpRange &ipRange);
static void writeIpRange(char **data, const IpRange &ipRange, bool isIPv6 = false);
static bool loadAddressList(const char **data, IpRange &ipRange, uint &bufSize);
static bool loadAddress4List(const char **data, IpRange &ipRange, uint &bufSize);
static bool loadAddress6List(const char **data, IpRange &ipRange, uint &bufSize);
static bool loadIpRange(
const char **data, IpRange &ipRange, uint &bufSize, bool isIPv6 = false);
static void writeApps(char **data, const appdata_map_t &appsMap, bool useHeader = false);