UI: ConfUtil: Add writeServiceSids()

This commit is contained in:
Nodir Temirkhodjaev 2024-10-28 12:15:05 +05:00
parent 6463bf37d4
commit afe974924b
5 changed files with 79 additions and 49 deletions

View File

@ -82,7 +82,7 @@ typedef struct fort_service_sid_list
UINT16 services_n;
UINT16 names_n;
DWORD data[1];
char data[1];
} FORT_SERVICE_SID_LIST, *PFORT_SERVICE_SID_LIST;
#define FORT_SERVICE_SID_LIST_DATA_OFF offsetof(FORT_SERVICE_SID_LIST, data)

View File

@ -54,8 +54,7 @@ int writeServiceInfo(char *data, const ServiceInfo &serviceInfo)
return FORT_SERVICE_INFO_NAME_OFF + FORT_CONF_STR_DATA_SIZE(nameLen);
}
void collectServiceSidsNames(const QVector<ServiceInfo> &services,
QMap<QByteArray, int> &sidNameIndexMap, QStringList &namesList)
void collectServiceSidsNames(const QVector<ServiceInfo> &services, WriteServiceSidsArgs &wssa)
{
QHash<QString, int> nameIndexMap;
@ -66,39 +65,14 @@ void collectServiceSidsNames(const QVector<ServiceInfo> &services,
int nameIndex = nameIndexMap.value(name, -1);
if (nameIndex == -1) {
nameIndex = namesList.size();
namesList.append(name);
nameIndex = wssa.namesList.size();
wssa.namesList.append(name);
}
sidNameIndexMap.insert(sid, nameIndex);
wssa.sidNameIndexMap.insert(sid, nameIndex);
}
}
int writeServiceSidsHeader(char *data, int servicesCount, int namesCount)
{
PFORT_SERVICE_SID_LIST sidList = (PFORT_SERVICE_SID_LIST) data;
sidList->services_n = servicesCount;
sidList->names_n = namesCount;
return FORT_SERVICE_SID_LIST_DATA_OFF;
}
int writeServiceSidsList(char *data, const QMap<QByteArray, int> &sidNameIndexMap)
{
const int servicesCount = sidNameIndexMap.size();
char *sid = data;
quint16 *nameIndex = (quint16 *) (data + servicesCount * FORT_SERVICE_SID_SIZE);
for (const auto &[sidData, index] : sidNameIndexMap.asKeyValueRange()) {
ConfUtil::writeArray(&sid, sidData);
*nameIndex++ = index;
}
return servicesCount * (FORT_SERVICE_SID_SIZE + sizeof(quint16));
}
void writeAppGroupFlags(PFORT_CONF_GROUP out, const FirewallConf &conf)
{
out->group_bits = 0;
@ -223,23 +197,22 @@ void ConfBuffer::writeServices(const QVector<ServiceInfo> &services, int running
void ConfBuffer::writeServiceSids(const QVector<ServiceInfo> &services)
{
QMap<QByteArray, int> sidNameIndexMap;
QStringList namesList;
WriteServiceSidsArgs wssa;
collectServiceSidsNames(services, sidNameIndexMap, namesList);
collectServiceSidsNames(services, wssa);
// Resize the buffer to max size
const int servicesCount = sidNameIndexMap.size();
const int namesCount = namesList.size();
{
const int servicesCount = wssa.sidNameIndexMap.size();
const int namesCount = wssa.namesList.size();
buffer().resize(FORT_SERVICE_SID_LIST_MAX_SIZE(servicesCount, namesCount));
buffer().resize(FORT_SERVICE_SID_LIST_MAX_SIZE(servicesCount, namesCount));
}
// Fill the buffer
char *data = buffer().data();
int outSize = writeServiceSidsHeader(data, servicesCount, namesCount);
outSize += writeServiceSidsList(data + outSize, sidNameIndexMap);
const int outSize = ConfUtil::writeServiceSids(&data, wssa);
buffer().resize(outSize); // shrink to actual size
}
@ -276,7 +249,7 @@ bool ConfBuffer::write(
+ FORT_CONF_STR_DATA_SIZE(opt.prefixAppsSize)
+ FORT_CONF_STR_DATA_SIZE(opt.exeAppsSize));
buffer().resize(confIoSize); // shrink to actual size
buffer().resize(confIoSize);
// Fill the buffer
char *data = buffer().data();

View File

@ -5,7 +5,6 @@
#include <util/conf/confappswalker.h>
#include <util/conf/confruleswalker.h>
#include <util/service/serviceinfo.h>
#include "confutil.h"

View File

@ -119,14 +119,40 @@ QRegularExpressionMatch ConfUtil::matchWildcard(const QStringView &path)
return StringUtil::match(wildMatcher, path);
}
void ConfUtil::migrateZoneData(char **data, const QByteArray &zoneData)
int ConfUtil::writeServiceSids(char **data, const WriteServiceSidsArgs &wssa)
{
PFORT_CONF_ADDR_LIST addr_list = (PFORT_CONF_ADDR_LIST) zoneData.data();
PFORT_SERVICE_SID_LIST sidList = PFORT_SERVICE_SID_LIST(*data);
if (FORT_CONF_ADDR4_LIST_SIZE(addr_list->ip_n, addr_list->pair_n) == zoneData.size()) {
IpRange ipRange;
writeIpRange(data, ipRange, /*isIPv6=*/true);
const int servicesCount = wssa.sidNameIndexMap.size();
const int namesCount = wssa.namesList.size();
sidList->services_n = servicesCount;
sidList->names_n = namesCount;
// Write Service SID-s and Name Indexes
char *sid = sidList->data;
quint16 *nameIndex = (quint16 *) (sid + servicesCount * FORT_SERVICE_SID_SIZE);
for (const auto &[sidData, index] : wssa.sidNameIndexMap.asKeyValueRange()) {
writeArray(&sid, sidData);
sid += FORT_SERVICE_SID_SIZE;
*nameIndex++ = index;
}
// Write Service Names: Offsets and Texts
quint32 *nameOffset = (quint32 *) nameIndex;
char *nameData = (char *) (nameOffset + namesCount);
char *nameText = nameData;
for (const auto &name : wssa.namesList) {
*nameOffset++ = nameText - nameData;
writeString(&nameText, name);
}
return (nameText - *data);
}
QString ConfUtil::parseAppPath(const QStringView &line, bool &isWild, bool &isPrefix)
@ -294,7 +320,7 @@ bool ConfUtil::loadIpRange(const char **data, IpRange &ipRange, uint &bufSize, b
if (bufSize < FORT_CONF_ADDR_LIST_OFF)
return false;
PFORT_CONF_ADDR_LIST addr_list = (PFORT_CONF_ADDR_LIST) *data;
PFORT_CONF_ADDR_LIST addr_list = PFORT_CONF_ADDR_LIST(*data);
*data = (const char *) addr_list->ip;
const uint addrListSize = isIPv6
@ -351,7 +377,7 @@ void ConfUtil::writeApps(char **data, const appdata_map_t &appsMap, bool useHead
const quint16 appPathLen = quint16(kernelPathSize * sizeof(wchar_t));
const quint32 appSize = FORT_CONF_APP_ENTRY_SIZE(appPathLen);
PFORT_APP_ENTRY entry = (PFORT_APP_ENTRY) p;
PFORT_APP_ENTRY entry = PFORT_APP_ENTRY(p);
entry->app_data = appData;
entry->path_len = appPathLen;
@ -368,6 +394,16 @@ void ConfUtil::writeApps(char **data, const appdata_map_t &appsMap, bool useHead
*data += offTableSize + FORT_CONF_STR_DATA_SIZE(off);
}
void ConfUtil::migrateZoneData(char **data, const QByteArray &zoneData)
{
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;
writeIpRange(data, ipRange, /*isIPv6=*/true);
}
}
void ConfUtil::writeShorts(char **data, const shorts_arr_t &array)
{
writeData(data, array.constData(), array.size(), sizeof(quint16));
@ -410,6 +446,17 @@ void ConfUtil::writeArray(char **data, const QByteArray &array)
*data += arraySize;
}
void ConfUtil::writeString(char **data, const QString &s)
{
wchar_t *array = (wchar_t *) *data;
const int n = s.toWCharArray(array);
array[n] = L'\0';
*data += n + 1; // +1 for the null terminator
}
void ConfUtil::loadLongs(const char **data, longs_arr_t &array)
{
loadData(data, array.data(), array.size(), sizeof(quint32));

View File

@ -7,6 +7,8 @@
#include <QRegularExpressionMatch>
#include <QVector>
#include <util/service/serviceinfo.h>
#include "appparseoptions.h"
class FirewallConf;
@ -15,6 +17,12 @@ using longs_arr_t = QVector<quint32>;
using shorts_arr_t = QVector<quint16>;
using chars_arr_t = QVector<qint8>;
struct WriteServiceSidsArgs
{
QMap<QByteArray, int> sidNameIndexMap;
QStringList namesList;
};
struct ParseAddressGroupsArgs
{
addrranges_arr_t addressRanges;
@ -40,6 +48,8 @@ public:
static QRegularExpressionMatch matchWildcard(const QStringView &path);
static int writeServiceSids(char **data, const WriteServiceSidsArgs &wssa);
static QString parseAppPath(const QStringView &line, bool &isWild, bool &isPrefix);
static void writeConf(char **data, const WriteConfArgs &wca, AppParseOptions &opt);
@ -65,6 +75,7 @@ public:
static void writeData(char **data, void const *src, int elemCount, uint elemSize);
static void writeChars(char **data, const chars_arr_t &array);
static void writeArray(char **data, const QByteArray &array);
static void writeString(char **data, const QString &s);
static void loadLongs(const char **data, longs_arr_t &array);
static void loadIp6Array(const char **data, ip6_arr_t &array);