mirror of
https://github.com/tnodir/fort
synced 2024-11-15 05:18:07 +00:00
UI: ConfUtil: Refactor buffer handling
This commit is contained in:
parent
f26458c49a
commit
2c0aebbd51
@ -40,10 +40,10 @@ void validateDriver(Device &device)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int verSize = confUtil.writeVersion();
|
||||
ASSERT_NE(verSize, 0);
|
||||
confUtil.writeVersion();
|
||||
ASSERT_FALSE(confUtil.buffer().isEmpty());
|
||||
|
||||
ASSERT_TRUE(device.ioctl(DriverCommon::ioctlValidate(), confUtil.data(), verSize));
|
||||
ASSERT_TRUE(device.ioctl(DriverCommon::ioctlValidate(), confUtil.data()));
|
||||
}
|
||||
|
||||
void setConf(Device &device)
|
||||
|
@ -572,16 +572,16 @@ bool ConfAppManager::updateDriverConf(bool onlyFlags)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int confSize = onlyFlags ? confUtil.writeFlags(*conf())
|
||||
: confUtil.write(*conf(), this, *IoC<EnvManager>());
|
||||
const bool ok = onlyFlags ? (confUtil.writeFlags(*conf()), true)
|
||||
: confUtil.write(*conf(), this, *IoC<EnvManager>());
|
||||
|
||||
if (confSize == 0) {
|
||||
if (!ok) {
|
||||
qCWarning(LC) << "Driver config error:" << confUtil.errorMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
if (!driverManager->writeConf(confUtil.buffer(), confSize, onlyFlags)) {
|
||||
if (!driverManager->writeConf(confUtil.buffer(), onlyFlags)) {
|
||||
qCWarning(LC) << "Update driver error:" << driverManager->errorMessage();
|
||||
return false;
|
||||
}
|
||||
@ -644,15 +644,13 @@ bool ConfAppManager::updateDriverUpdateApp(const App &app, bool remove)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int entrySize = confUtil.writeAppEntry(app);
|
||||
|
||||
if (entrySize == 0) {
|
||||
if (!confUtil.writeAppEntry(app)) {
|
||||
qCWarning(LC) << "Driver config error:" << confUtil.errorMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
if (!driverManager->writeApp(confUtil.buffer(), entrySize, remove)) {
|
||||
if (!driverManager->writeApp(confUtil.buffer(), remove)) {
|
||||
qCWarning(LC) << "Update driver error:" << driverManager->errorMessage();
|
||||
return false;
|
||||
}
|
||||
|
@ -826,10 +826,9 @@ bool ConfManager::validateDriver()
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int verSize = confUtil.writeVersion();
|
||||
confUtil.writeVersion();
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
return driverManager->validate(confUtil.buffer(), verSize);
|
||||
return IoC<DriverManager>()->validate(confUtil.buffer());
|
||||
}
|
||||
|
||||
void ConfManager::updateServices()
|
||||
@ -853,10 +852,9 @@ void ConfManager::updateDriverServices(
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int outSize = confUtil.writeServices(services, runningServicesCount);
|
||||
confUtil.writeServices(services, runningServicesCount);
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
driverManager->writeServices(confUtil.buffer(), outSize);
|
||||
IoC<DriverManager>()->writeServices(confUtil.buffer());
|
||||
}
|
||||
|
||||
bool ConfManager::loadFromDb(FirewallConf &conf, bool &isNew)
|
||||
|
@ -60,15 +60,15 @@ const char *const sqlUpdateZoneResult =
|
||||
" source_modtime = ?5, last_run = ?6, last_success = ?7"
|
||||
" WHERE zone_id = ?1;";
|
||||
|
||||
bool driverWriteZones(ConfUtil &confUtil, int entrySize, bool onlyFlags = false)
|
||||
bool driverWriteZones(ConfUtil &confUtil, bool onlyFlags = false)
|
||||
{
|
||||
if (entrySize == 0) {
|
||||
if (confUtil.hasError()) {
|
||||
qCWarning(LC) << "Driver config error:" << confUtil.errorMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
if (!driverManager->writeZones(confUtil.buffer(), entrySize, onlyFlags)) {
|
||||
if (!driverManager->writeZones(confUtil.buffer(), onlyFlags)) {
|
||||
qCWarning(LC) << "Update driver error:" << driverManager->errorMessage();
|
||||
return false;
|
||||
}
|
||||
@ -241,18 +241,18 @@ void ConfZoneManager::updateDriverZones(quint32 zonesMask, quint32 enabledMask,
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int entrySize = confUtil.writeZones(zonesMask, enabledMask, dataSize, zonesData);
|
||||
confUtil.writeZones(zonesMask, enabledMask, dataSize, zonesData);
|
||||
|
||||
driverWriteZones(confUtil, entrySize);
|
||||
driverWriteZones(confUtil);
|
||||
}
|
||||
|
||||
bool ConfZoneManager::updateDriverZoneFlag(int zoneId, bool enabled)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
|
||||
const int entrySize = confUtil.writeZoneFlag(zoneId, enabled);
|
||||
confUtil.writeZoneFlag(zoneId, enabled);
|
||||
|
||||
return driverWriteZones(confUtil, entrySize, /*onlyFlags=*/true);
|
||||
return driverWriteZones(confUtil, /*onlyFlags=*/true);
|
||||
}
|
||||
|
||||
bool ConfZoneManager::beginTransaction()
|
||||
|
@ -91,41 +91,41 @@ bool DriverManager::closeDevice()
|
||||
return res;
|
||||
}
|
||||
|
||||
bool DriverManager::validate(QByteArray &buf, int size)
|
||||
bool DriverManager::validate(QByteArray &buf)
|
||||
{
|
||||
return writeData(DriverCommon::ioctlValidate(), buf, size);
|
||||
return writeData(DriverCommon::ioctlValidate(), buf);
|
||||
}
|
||||
|
||||
bool DriverManager::writeServices(QByteArray &buf, int size)
|
||||
bool DriverManager::writeServices(QByteArray &buf)
|
||||
{
|
||||
return writeData(DriverCommon::ioctlSetServices(), buf, size);
|
||||
return writeData(DriverCommon::ioctlSetServices(), buf);
|
||||
}
|
||||
|
||||
bool DriverManager::writeConf(QByteArray &buf, int size, bool onlyFlags)
|
||||
bool DriverManager::writeConf(QByteArray &buf, bool onlyFlags)
|
||||
{
|
||||
return writeData(
|
||||
onlyFlags ? DriverCommon::ioctlSetFlags() : DriverCommon::ioctlSetConf(), buf, size);
|
||||
return writeData(onlyFlags ? DriverCommon::ioctlSetFlags() : DriverCommon::ioctlSetConf(), buf);
|
||||
}
|
||||
|
||||
bool DriverManager::writeApp(QByteArray &buf, int size, bool remove)
|
||||
bool DriverManager::writeApp(QByteArray &buf, bool remove)
|
||||
{
|
||||
return writeData(remove ? DriverCommon::ioctlDelApp() : DriverCommon::ioctlAddApp(), buf, size);
|
||||
return writeData(remove ? DriverCommon::ioctlDelApp() : DriverCommon::ioctlAddApp(), buf);
|
||||
}
|
||||
|
||||
bool DriverManager::writeZones(QByteArray &buf, int size, bool onlyFlags)
|
||||
bool DriverManager::writeZones(QByteArray &buf, bool onlyFlags)
|
||||
{
|
||||
return writeData(onlyFlags ? DriverCommon::ioctlSetZoneFlag() : DriverCommon::ioctlSetZones(),
|
||||
buf, size);
|
||||
const auto code = onlyFlags ? DriverCommon::ioctlSetZoneFlag() : DriverCommon::ioctlSetZones();
|
||||
|
||||
return writeData(code, buf);
|
||||
}
|
||||
|
||||
bool DriverManager::writeData(quint32 code, QByteArray &buf, int size)
|
||||
bool DriverManager::writeData(quint32 code, QByteArray &buf)
|
||||
{
|
||||
if (!isDeviceOpened())
|
||||
return true;
|
||||
|
||||
const bool wasCancelled = driverWorker()->cancelAsyncIo();
|
||||
|
||||
const bool res = device()->ioctl(code, buf.data(), size);
|
||||
const bool res = device()->ioctl(code, buf.data(), buf.size());
|
||||
|
||||
updateErrorCode(res);
|
||||
|
||||
|
@ -40,12 +40,12 @@ public slots:
|
||||
virtual bool openDevice();
|
||||
virtual bool closeDevice();
|
||||
|
||||
bool validate(QByteArray &buf, int size);
|
||||
bool validate(QByteArray &buf);
|
||||
|
||||
bool writeServices(QByteArray &buf, int size);
|
||||
bool writeConf(QByteArray &buf, int size, bool onlyFlags = false);
|
||||
bool writeApp(QByteArray &buf, int size, bool remove = false);
|
||||
bool writeZones(QByteArray &buf, int size, bool onlyFlags = false);
|
||||
bool writeServices(QByteArray &buf);
|
||||
bool writeConf(QByteArray &buf, bool onlyFlags = false);
|
||||
bool writeApp(QByteArray &buf, bool remove = false);
|
||||
bool writeZones(QByteArray &buf, bool onlyFlags = false);
|
||||
|
||||
protected:
|
||||
void setErrorCode(quint32 v);
|
||||
@ -56,7 +56,7 @@ private:
|
||||
void setupWorker();
|
||||
void closeWorker();
|
||||
|
||||
bool writeData(quint32 code, QByteArray &buf, int size);
|
||||
bool writeData(quint32 code, QByteArray &buf);
|
||||
|
||||
static bool executeCommand(const QString &fileName);
|
||||
|
||||
|
@ -122,11 +122,12 @@ bool TaskZoneDownloader::storeAddresses(const StringViewList &list)
|
||||
|
||||
// Store binary file
|
||||
ConfUtil confUtil;
|
||||
const int bufSize = confUtil.writeZone(ipRange);
|
||||
if (bufSize == 0)
|
||||
return false;
|
||||
|
||||
confUtil.writeZone(ipRange);
|
||||
|
||||
m_zoneData = confUtil.buffer();
|
||||
if (m_zoneData.isEmpty())
|
||||
return false;
|
||||
|
||||
const auto binData = qCompress(m_zoneData);
|
||||
|
||||
|
@ -105,39 +105,40 @@ int ConfUtil::zoneMaxCount()
|
||||
return FORT_CONF_ZONE_MAX;
|
||||
}
|
||||
|
||||
int ConfUtil::writeVersion()
|
||||
void ConfUtil::writeVersion()
|
||||
{
|
||||
const int verSize = sizeof(FORT_CONF_VERSION);
|
||||
|
||||
buffer().reserve(verSize);
|
||||
buffer().resize(verSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_VERSION confVer = (PFORT_CONF_VERSION) buffer().data();
|
||||
|
||||
confVer->driver_version = DRIVER_VERSION;
|
||||
|
||||
return verSize;
|
||||
}
|
||||
|
||||
int ConfUtil::writeServices(const QVector<ServiceInfo> &services, int runningServicesCount)
|
||||
void ConfUtil::writeServices(const QVector<ServiceInfo> &services, int runningServicesCount)
|
||||
{
|
||||
buffer().reserve(FORT_SERVICE_INFO_LIST_MIN_SIZE);
|
||||
const int servicesSize =
|
||||
FORT_SERVICE_INFO_LIST_MIN_SIZE + runningServicesCount * FORT_SERVICE_INFO_MAX_SIZE;
|
||||
|
||||
int outSize = writeServicesHeader(buffer().data(), runningServicesCount);
|
||||
buffer().resize(servicesSize);
|
||||
|
||||
char *data = buffer().data();
|
||||
|
||||
int outSize = writeServicesHeader(data, runningServicesCount);
|
||||
|
||||
for (const ServiceInfo &info : services) {
|
||||
if (!info.isRunning)
|
||||
continue;
|
||||
|
||||
buffer().reserve(outSize + FORT_SERVICE_INFO_MAX_SIZE);
|
||||
|
||||
outSize += writeServiceInfo(buffer().data() + outSize, info);
|
||||
outSize += writeServiceInfo(data + outSize, info);
|
||||
}
|
||||
|
||||
return outSize;
|
||||
buffer().resize(outSize);
|
||||
}
|
||||
|
||||
int ConfUtil::write(
|
||||
bool ConfUtil::write(
|
||||
const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager)
|
||||
{
|
||||
quint32 addressGroupsSize = 0;
|
||||
@ -146,7 +147,7 @@ int ConfUtil::write(
|
||||
|
||||
if (!parseAddressGroups(
|
||||
conf.addressGroups(), addressRanges, addressGroupOffsets, addressGroupsSize))
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
quint8 appPeriodsCount = 0;
|
||||
chars_arr_t appPeriods;
|
||||
@ -154,15 +155,15 @@ int ConfUtil::write(
|
||||
AppParseOptions opt;
|
||||
|
||||
if (!parseExeApps(envManager, confAppsWalker, opt))
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
if (!parseAppGroups(envManager, conf.appGroups(), appPeriods, appPeriodsCount, opt))
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
const quint32 appsSize = opt.wildAppsSize + opt.prefixAppsSize + opt.exeAppsSize;
|
||||
if (appsSize > FORT_CONF_APPS_LEN_MAX) {
|
||||
setErrorMessage(tr("Too many application paths"));
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fill the buffer
|
||||
@ -173,67 +174,63 @@ int ConfUtil::write(
|
||||
+ FORT_CONF_STR_DATA_SIZE(opt.prefixAppsSize)
|
||||
+ FORT_CONF_STR_DATA_SIZE(opt.exeAppsSize));
|
||||
|
||||
buffer().reserve(confIoSize);
|
||||
buffer().resize(confIoSize);
|
||||
|
||||
writeConf(buffer().data(), conf, addressRanges, addressGroupOffsets, appPeriods,
|
||||
appPeriodsCount, opt);
|
||||
|
||||
return confIoSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
int ConfUtil::writeFlags(const FirewallConf &conf)
|
||||
void ConfUtil::writeFlags(const FirewallConf &conf)
|
||||
{
|
||||
const int flagsSize = sizeof(FORT_CONF_FLAGS);
|
||||
|
||||
buffer().reserve(flagsSize);
|
||||
buffer().resize(flagsSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_FLAGS confFlags = (PFORT_CONF_FLAGS) buffer().data();
|
||||
|
||||
writeConfFlags(conf, confFlags);
|
||||
|
||||
return flagsSize;
|
||||
}
|
||||
|
||||
int ConfUtil::writeAppEntry(const App &app, bool isNew)
|
||||
bool ConfUtil::writeAppEntry(const App &app, bool isNew)
|
||||
{
|
||||
appdata_map_t appsMap;
|
||||
quint32 appsSize = 0;
|
||||
|
||||
if (!addApp(app, isNew, appsMap, appsSize))
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
buffer().reserve(appsSize);
|
||||
buffer().resize(appsSize);
|
||||
|
||||
// Fill the buffer
|
||||
char *data = (char *) buffer().data();
|
||||
char *data = buffer().data();
|
||||
|
||||
writeApps(&data, appsMap);
|
||||
|
||||
return int(appsSize);
|
||||
return true;
|
||||
}
|
||||
|
||||
int ConfUtil::writeZone(const IpRange &ipRange)
|
||||
void ConfUtil::writeZone(const IpRange &ipRange)
|
||||
{
|
||||
const int addrSize = FORT_CONF_ADDR_LIST_SIZE(
|
||||
ipRange.ip4Size(), ipRange.pair4Size(), ipRange.ip6Size(), ipRange.pair6Size());
|
||||
|
||||
buffer().reserve(addrSize);
|
||||
buffer().resize(addrSize);
|
||||
|
||||
// Fill the buffer
|
||||
char *data = (char *) buffer().data();
|
||||
char *data = buffer().data();
|
||||
|
||||
writeAddressList(&data, ipRange);
|
||||
|
||||
return addrSize;
|
||||
}
|
||||
|
||||
int ConfUtil::writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize,
|
||||
void ConfUtil::writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize,
|
||||
const QList<QByteArray> &zonesData)
|
||||
{
|
||||
const int zonesSize = FORT_CONF_ZONES_DATA_OFF + dataSize;
|
||||
|
||||
buffer().reserve(zonesSize);
|
||||
buffer().resize(zonesSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_ZONES confZones = (PFORT_CONF_ZONES) buffer().data();
|
||||
@ -258,8 +255,6 @@ int ConfUtil::writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSiz
|
||||
|
||||
zonesMask ^= zoneMask;
|
||||
}
|
||||
|
||||
return zonesSize;
|
||||
}
|
||||
|
||||
void ConfUtil::migrateZoneData(char **data, const QByteArray &zoneData)
|
||||
@ -272,19 +267,17 @@ void ConfUtil::migrateZoneData(char **data, const QByteArray &zoneData)
|
||||
}
|
||||
}
|
||||
|
||||
int ConfUtil::writeZoneFlag(int zoneId, bool enabled)
|
||||
void ConfUtil::writeZoneFlag(int zoneId, bool enabled)
|
||||
{
|
||||
const int flagSize = sizeof(FORT_CONF_ZONE_FLAG);
|
||||
|
||||
buffer().reserve(flagSize);
|
||||
buffer().resize(flagSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_ZONE_FLAG confZoneFlag = (PFORT_CONF_ZONE_FLAG) buffer().data();
|
||||
|
||||
confZoneFlag->zone_id = zoneId;
|
||||
confZoneFlag->enabled = enabled;
|
||||
|
||||
return flagSize;
|
||||
}
|
||||
|
||||
bool ConfUtil::loadZone(IpRange &ipRange)
|
||||
|
@ -32,6 +32,8 @@ public:
|
||||
|
||||
QString errorMessage() const { return m_errorMessage; }
|
||||
|
||||
bool hasError() const { return !errorMessage().isEmpty(); }
|
||||
|
||||
const QByteArray &buffer() const { return m_buffer; }
|
||||
QByteArray &buffer() { return m_buffer; }
|
||||
|
||||
@ -45,16 +47,16 @@ public:
|
||||
static int zoneMaxCount();
|
||||
|
||||
public slots:
|
||||
int writeVersion();
|
||||
int writeServices(const QVector<ServiceInfo> &services, int runningServicesCount);
|
||||
int write(const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager);
|
||||
int writeFlags(const FirewallConf &conf);
|
||||
int writeAppEntry(const App &app, bool isNew = false);
|
||||
int writeZone(const IpRange &ipRange);
|
||||
int writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize,
|
||||
void writeVersion();
|
||||
void writeServices(const QVector<ServiceInfo> &services, int runningServicesCount);
|
||||
bool write(const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager);
|
||||
void writeFlags(const FirewallConf &conf);
|
||||
bool writeAppEntry(const App &app, bool isNew = false);
|
||||
void writeZone(const IpRange &ipRange);
|
||||
void writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize,
|
||||
const QList<QByteArray> &zonesData);
|
||||
void migrateZoneData(char **data, const QByteArray &zoneData);
|
||||
int writeZoneFlag(int zoneId, bool enabled);
|
||||
void writeZoneFlag(int zoneId, bool enabled);
|
||||
|
||||
bool loadZone(IpRange &ipRange);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user