UI: ConfUtil: Refactor inner function arguments

This commit is contained in:
Nodir Temirkhodjaev 2024-04-12 19:52:38 +03:00
parent 2c0aebbd51
commit d5122dc481
2 changed files with 128 additions and 118 deletions

View File

@ -75,6 +75,85 @@ void writeConfFlags(const FirewallConf &conf, PFORT_CONF_FLAGS confFlags)
confFlags->group_bits = conf.appGroupBits(); confFlags->group_bits = conf.appGroupBits();
} }
void writeAppGroupFlags(PFORT_CONF_GROUP out, const FirewallConf &conf)
{
out->group_bits = 0;
out->log_blocked = 0;
out->log_conn = 0;
int i = 0;
for (const AppGroup *appGroup : conf.appGroups()) {
if (appGroup->enabled()) {
out->group_bits |= (1 << i);
}
if (appGroup->logBlocked()) {
out->log_blocked |= (1 << i);
}
if (appGroup->logConn()) {
out->log_conn |= (1 << i);
}
++i;
}
}
void writeLimitBps(PFORT_SPEED_LIMIT limit, quint32 kBits)
{
limit->bps = quint64(kBits) * (1024LL / 8); /* to bytes per second */
}
void writeLimitIn(PFORT_SPEED_LIMIT limit, const AppGroup *appGroup)
{
limit->plr = appGroup->limitPacketLoss();
limit->latency_ms = appGroup->limitLatency();
limit->buffer_bytes = appGroup->limitBufferSizeIn();
writeLimitBps(limit, appGroup->speedLimitIn());
}
void writeLimitOut(PFORT_SPEED_LIMIT limit, const AppGroup *appGroup)
{
limit->plr = appGroup->limitPacketLoss();
limit->latency_ms = appGroup->limitLatency();
limit->buffer_bytes = appGroup->limitBufferSizeOut();
writeLimitBps(limit, appGroup->speedLimitOut());
}
void writeLimits(PFORT_CONF_GROUP out, const QList<AppGroup *> &appGroups)
{
PFORT_SPEED_LIMIT limits = out->limits;
out->limit_bits = 0;
out->limit_io_bits = 0;
const int groupsCount = appGroups.size();
for (int i = 0; i < groupsCount; ++i, limits += 2) {
const AppGroup *appGroup = appGroups.at(i);
const quint32 limitIn = appGroup->enabledSpeedLimitIn();
const quint32 limitOut = appGroup->enabledSpeedLimitOut();
const bool isLimitIn = (limitIn != 0);
const bool isLimitOut = (limitOut != 0);
if (isLimitIn || isLimitOut) {
out->limit_bits |= (1 << i);
if (isLimitIn) {
out->limit_io_bits |= (1 << (i * 2 + 0));
writeLimitIn(&limits[0], appGroup);
}
if (isLimitOut) {
out->limit_io_bits |= (1 << (i * 2 + 1));
writeLimitOut(&limits[1], appGroup);
}
}
}
}
} }
ConfUtil::ConfUtil(const QByteArray &buffer, QObject *parent) : ConfUtil::ConfUtil(const QByteArray &buffer, QObject *parent) :
@ -141,23 +220,20 @@ void ConfUtil::writeServices(const QVector<ServiceInfo> &services, int runningSe
bool ConfUtil::write( bool ConfUtil::write(
const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager) const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager)
{ {
WriteConfArgs wca = { .conf = conf,
.ad = { .addressRanges = addrranges_arr_t(conf.addressGroups().size()) } };
quint32 addressGroupsSize = 0; quint32 addressGroupsSize = 0;
longs_arr_t addressGroupOffsets;
addrranges_arr_t addressRanges(conf.addressGroups().size());
if (!parseAddressGroups( if (!parseAddressGroups(conf.addressGroups(), wca.ad, addressGroupsSize))
conf.addressGroups(), addressRanges, addressGroupOffsets, addressGroupsSize))
return false; return false;
quint8 appPeriodsCount = 0;
chars_arr_t appPeriods;
AppParseOptions opt; AppParseOptions opt;
if (!parseExeApps(envManager, confAppsWalker, opt)) if (!parseExeApps(envManager, confAppsWalker, opt))
return false; return false;
if (!parseAppGroups(envManager, conf.appGroups(), appPeriods, appPeriodsCount, opt)) if (!parseAppGroups(envManager, conf.appGroups(), wca.gr, opt))
return false; return false;
const quint32 appsSize = opt.wildAppsSize + opt.prefixAppsSize + opt.exeAppsSize; const quint32 appsSize = opt.wildAppsSize + opt.prefixAppsSize + opt.exeAppsSize;
@ -176,8 +252,7 @@ bool ConfUtil::write(
buffer().resize(confIoSize); buffer().resize(confIoSize);
writeConf(buffer().data(), conf, addressRanges, addressGroupOffsets, appPeriods, writeConf(buffer().data(), wca, opt);
appPeriodsCount, opt);
return true; return true;
} }
@ -289,8 +364,7 @@ bool ConfUtil::loadZone(IpRange &ipRange)
} }
bool ConfUtil::parseAddressGroups(const QList<AddressGroup *> &addressGroups, bool ConfUtil::parseAddressGroups(const QList<AddressGroup *> &addressGroups,
addrranges_arr_t &addressRanges, longs_arr_t &addressGroupOffsets, ParseAddressGroupsArgs &ad, quint32 &addressGroupsSize)
quint32 &addressGroupsSize)
{ {
const int groupsCount = addressGroups.size(); const int groupsCount = addressGroups.size();
@ -299,7 +373,7 @@ bool ConfUtil::parseAddressGroups(const QList<AddressGroup *> &addressGroups,
for (int i = 0; i < groupsCount; ++i) { for (int i = 0; i < groupsCount; ++i) {
AddressGroup *addressGroup = addressGroups.at(i); AddressGroup *addressGroup = addressGroups.at(i);
AddressRange &addressRange = addressRanges[i]; AddressRange &addressRange = ad.addressRanges[i];
addressRange.setIncludeAll(addressGroup->includeAll()); addressRange.setIncludeAll(addressGroup->includeAll());
addressRange.setExcludeAll(addressGroup->excludeAll()); addressRange.setExcludeAll(addressGroup->excludeAll());
addressRange.setIncludeZones(addressGroup->includeZones()); addressRange.setIncludeZones(addressGroup->includeZones());
@ -329,7 +403,7 @@ bool ConfUtil::parseAddressGroups(const QList<AddressGroup *> &addressGroups,
return false; return false;
} }
addressGroupOffsets.append(addressGroupsSize); ad.addressGroupOffsets.append(addressGroupsSize);
addressGroupsSize += FORT_CONF_ADDR_GROUP_OFF addressGroupsSize += FORT_CONF_ADDR_GROUP_OFF
+ FORT_CONF_ADDR_LIST_SIZE(incRange.ip4Size(), incRange.pair4Size(), + FORT_CONF_ADDR_LIST_SIZE(incRange.ip4Size(), incRange.pair4Size(),
@ -342,7 +416,7 @@ bool ConfUtil::parseAddressGroups(const QList<AddressGroup *> &addressGroups,
} }
bool ConfUtil::parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &appGroups, bool ConfUtil::parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &appGroups,
chars_arr_t &appPeriods, quint8 &appPeriodsCount, AppParseOptions &opt) ParseAppGroupsArgs &gr, AppParseOptions &opt)
{ {
const int groupsCount = appGroups.size(); const int groupsCount = appGroups.size();
if (groupsCount < 1 || groupsCount > APP_GROUP_MAX) { if (groupsCount < 1 || groupsCount > APP_GROUP_MAX) {
@ -386,7 +460,7 @@ bool ConfUtil::parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &a
return false; return false;
// Enabled Period // Enabled Period
parseAppPeriod(appGroup, appPeriods, appPeriodsCount); parseAppPeriod(appGroup, gr);
} }
return true; return true;
@ -519,8 +593,7 @@ QString ConfUtil::parseAppPath(const QStringView line, bool &isWild, bool &isPre
return path.toString(); return path.toString();
} }
void ConfUtil::parseAppPeriod( void ConfUtil::parseAppPeriod(const AppGroup *appGroup, ParseAppGroupsArgs &gr)
const AppGroup *appGroup, chars_arr_t &appPeriods, quint8 &appPeriodsCount)
{ {
quint8 fromHour = 0, fromMinute = 0; quint8 fromHour = 0, fromMinute = 0;
quint8 toHour = 0, toMinute = 0; quint8 toHour = 0, toMinute = 0;
@ -533,19 +606,17 @@ void ConfUtil::parseAppPeriod(
const bool toIsEmpty = (toHour == 0 && toMinute == 0); const bool toIsEmpty = (toHour == 0 && toMinute == 0);
if (!fromIsEmpty || !toIsEmpty) { if (!fromIsEmpty || !toIsEmpty) {
++appPeriodsCount; ++gr.appPeriodsCount;
} }
} }
appPeriods.append(qint8(fromHour)); gr.appPeriods.append(qint8(fromHour));
appPeriods.append(qint8(fromMinute)); gr.appPeriods.append(qint8(fromMinute));
appPeriods.append(qint8(toHour)); gr.appPeriods.append(qint8(toHour));
appPeriods.append(qint8(toMinute)); gr.appPeriods.append(qint8(toMinute));
} }
void ConfUtil::writeConf(char *output, const FirewallConf &conf, void ConfUtil::writeConf(char *output, const WriteConfArgs &wca, AppParseOptions &opt)
const addrranges_arr_t &addressRanges, const longs_arr_t &addressGroupOffsets,
const chars_arr_t &appPeriods, quint8 appPeriodsCount, AppParseOptions &opt)
{ {
PFORT_CONF_IO drvConfIo = (PFORT_CONF_IO) output; PFORT_CONF_IO drvConfIo = (PFORT_CONF_IO) output;
PFORT_CONF drvConf = &drvConfIo->conf; PFORT_CONF drvConf = &drvConfIo->conf;
@ -556,11 +627,11 @@ void ConfUtil::writeConf(char *output, const FirewallConf &conf,
#define CONF_DATA_OFFSET quint32(data - drvConf->data) #define CONF_DATA_OFFSET quint32(data - drvConf->data)
addrGroupsOff = CONF_DATA_OFFSET; addrGroupsOff = CONF_DATA_OFFSET;
writeLongs(&data, addressGroupOffsets); writeLongs(&data, wca.ad.addressGroupOffsets);
writeAddressRanges(&data, addressRanges); writeAddressRanges(&data, wca.ad.addressRanges);
appPeriodsOff = CONF_DATA_OFFSET; appPeriodsOff = CONF_DATA_OFFSET;
writeChars(&data, appPeriods); writeChars(&data, wca.gr.appPeriods);
wildAppsOff = CONF_DATA_OFFSET; wildAppsOff = CONF_DATA_OFFSET;
writeApps(&data, opt.wildAppsMap); writeApps(&data, opt.wildAppsMap);
@ -572,17 +643,15 @@ void ConfUtil::writeConf(char *output, const FirewallConf &conf,
writeApps(&data, opt.exeAppsMap); writeApps(&data, opt.exeAppsMap);
#undef CONF_DATA_OFFSET #undef CONF_DATA_OFFSET
writeAppGroupFlags(&drvConfIo->conf_group.group_bits, &drvConfIo->conf_group.log_blocked, writeAppGroupFlags(&drvConfIo->conf_group, wca.conf);
&drvConfIo->conf_group.log_conn, conf);
writeLimits(drvConfIo->conf_group.limits, &drvConfIo->conf_group.limit_bits, writeLimits(&drvConfIo->conf_group, wca.conf.appGroups());
&drvConfIo->conf_group.limit_io_bits, conf.appGroups());
writeConfFlags(conf, &drvConf->flags); writeConfFlags(wca.conf, &drvConf->flags);
DriverCommon::confAppPermsMaskInit(drvConf); DriverCommon::confAppPermsMaskInit(drvConf);
drvConf->app_periods_n = appPeriodsCount; drvConf->app_periods_n = wca.gr.appPeriodsCount;
drvConf->proc_wild = opt.procWild; drvConf->proc_wild = opt.procWild;
@ -599,73 +668,6 @@ void ConfUtil::writeConf(char *output, const FirewallConf &conf,
drvConf->exe_apps_off = exeAppsOff; drvConf->exe_apps_off = exeAppsOff;
} }
void ConfUtil::writeAppGroupFlags(
quint16 *groupBits, quint16 *logBlockedBits, quint16 *logConnBits, const FirewallConf &conf)
{
*groupBits = 0;
*logBlockedBits = 0;
*logConnBits = 0;
int i = 0;
for (const AppGroup *appGroup : conf.appGroups()) {
if (appGroup->enabled()) {
*groupBits |= (1 << i);
}
if (appGroup->logBlocked()) {
*logBlockedBits |= (1 << i);
}
if (appGroup->logConn()) {
*logConnBits |= (1 << i);
}
++i;
}
}
void ConfUtil::writeLimits(struct fort_speed_limit *limits, quint16 *limitBits,
quint32 *limitIoBits, const QList<AppGroup *> &appGroups)
{
*limitBits = 0;
*limitIoBits = 0;
const int groupsCount = appGroups.size();
for (int i = 0; i < groupsCount; ++i, limits += 2) {
const AppGroup *appGroup = appGroups.at(i);
const quint32 limitIn = appGroup->enabledSpeedLimitIn();
const quint32 limitOut = appGroup->enabledSpeedLimitOut();
const bool isLimitIn = (limitIn != 0);
const bool isLimitOut = (limitOut != 0);
if (isLimitIn || isLimitOut) {
*limitBits |= (1 << i);
if (isLimitIn) {
*limitIoBits |= (1 << (i * 2 + 0));
writeLimit(&limits[0], limitIn, appGroup->limitBufferSizeIn(),
appGroup->limitLatency(), appGroup->limitPacketLoss());
}
if (isLimitOut) {
*limitIoBits |= (1 << (i * 2 + 1));
writeLimit(&limits[1], limitOut, appGroup->limitBufferSizeOut(),
appGroup->limitLatency(), appGroup->limitPacketLoss());
}
}
}
}
void ConfUtil::writeLimit(fort_speed_limit *limit, quint32 kBits, quint32 bufferSize,
quint32 latencyMsec, quint16 packetLoss)
{
limit->plr = packetLoss;
limit->latency_ms = latencyMsec;
limit->buffer_bytes = bufferSize;
limit->bps = quint64(kBits) * (1024LL / 8); /* to bytes per second */
}
void ConfUtil::writeAddressRanges(char **data, const addrranges_arr_t &addressRanges) void ConfUtil::writeAddressRanges(char **data, const addrranges_arr_t &addressRanges)
{ {
for (const AddressRange &addressRange : addressRanges) { for (const AddressRange &addressRange : addressRanges) {

View File

@ -63,13 +63,32 @@ public slots:
private: private:
void setErrorMessage(const QString &errorMessage) { m_errorMessage = errorMessage; } void setErrorMessage(const QString &errorMessage) { m_errorMessage = errorMessage; }
bool parseAddressGroups(const QList<AddressGroup *> &addressGroups, struct ParseAddressGroupsArgs
addrranges_arr_t &addressRanges, longs_arr_t &addressGroupOffsets, {
addrranges_arr_t addressRanges;
longs_arr_t addressGroupOffsets;
};
struct ParseAppGroupsArgs
{
chars_arr_t appPeriods;
quint8 appPeriodsCount = 0;
};
struct WriteConfArgs
{
const FirewallConf &conf;
ParseAddressGroupsArgs ad;
ParseAppGroupsArgs gr;
};
bool parseAddressGroups(const QList<AddressGroup *> &addressGroups, ParseAddressGroupsArgs &ad,
quint32 &addressGroupsSize); quint32 &addressGroupsSize);
// Convert app. groups to plain lists // Convert app. groups to plain lists
bool parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &appGroups, bool parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &appGroups,
chars_arr_t &appPeriods, quint8 &appPeriodsCount, AppParseOptions &opt); ParseAppGroupsArgs &gr, AppParseOptions &opt);
bool parseExeApps(EnvManager &envManager, ConfAppsWalker *confAppsWalker, AppParseOptions &opt); bool parseExeApps(EnvManager &envManager, ConfAppsWalker *confAppsWalker, AppParseOptions &opt);
@ -81,20 +100,9 @@ private:
static QString parseAppPath(const QStringView line, bool &isWild, bool &isPrefix); static QString parseAppPath(const QStringView line, bool &isWild, bool &isPrefix);
static void parseAppPeriod( static void parseAppPeriod(const AppGroup *appGroup, ParseAppGroupsArgs &gr);
const AppGroup *appGroup, chars_arr_t &appPeriods, quint8 &appPeriodsCount);
static void writeConf(char *output, const FirewallConf &conf, static void writeConf(char *output, const WriteConfArgs &wca, AppParseOptions &opt);
const addrranges_arr_t &addressRanges, const longs_arr_t &addressGroupOffsets,
const chars_arr_t &appPeriods, quint8 appPeriodsCount, AppParseOptions &opt);
static void writeAppGroupFlags(quint16 *groupBits, quint16 *logBlockedBits,
quint16 *logConnBits, const FirewallConf &conf);
static void writeLimits(struct fort_speed_limit *limits, quint16 *limitBits,
quint32 *limitIoBits, const QList<AppGroup *> &appGroups);
static void writeLimit(struct fort_speed_limit *limit, quint32 kBits, quint32 bufferSize,
quint32 latencyMsec, quint16 packetLoss);
static void writeAddressRanges(char **data, const addrranges_arr_t &addressRanges); static void writeAddressRanges(char **data, const addrranges_arr_t &addressRanges);
static void writeAddressRange(char **data, const AddressRange &addressRange); static void writeAddressRange(char **data, const AddressRange &addressRange);