Driver: fortdev: Minor refactor

This commit is contained in:
Nodir Temirkhodjaev 2023-04-02 18:02:26 +03:00
parent b3c56a1599
commit 7d9f04f034

View File

@ -25,14 +25,14 @@ static void fort_device_init(PDEVICE_OBJECT device)
{
fort_device_set(device->DeviceExtension);
RtlZeroMemory(g_device, sizeof(FORT_DEVICE));
RtlZeroMemory(fort_device(), sizeof(FORT_DEVICE));
g_device->object = device;
fort_device()->object = device;
}
static void NTAPI fort_worker_reauth(void)
{
const FORT_CONF_FLAGS conf_flags = g_device->conf.conf_flags;
const FORT_CONF_FLAGS conf_flags = fort_device()->conf.conf_flags;
NTSTATUS status;
status = fort_callout_force_reauth(conf_flags);
@ -45,8 +45,8 @@ static void NTAPI fort_worker_reauth(void)
static void NTAPI fort_app_period_timer(void)
{
if (fort_conf_ref_period_update(&g_device->conf, /*force=*/FALSE, /*periods_n=*/NULL)) {
fort_worker_queue(&g_device->worker, FORT_WORKER_REAUTH);
if (fort_conf_ref_period_update(&fort_device()->conf, /*force=*/FALSE, /*periods_n=*/NULL)) {
fort_worker_queue(&fort_device()->worker, FORT_WORKER_REAUTH);
}
}
@ -64,14 +64,15 @@ FORT_API NTSTATUS fort_device_create(PDEVICE_OBJECT device, PIRP irp)
NTSTATUS status = STATUS_SUCCESS;
/* Device opened */
if ((fort_device_flag_set(&g_device->conf, FORT_DEVICE_IS_OPENED, TRUE) & FORT_DEVICE_IS_OPENED)
if ((fort_device_flag_set(&fort_device()->conf, FORT_DEVICE_IS_OPENED, TRUE)
& FORT_DEVICE_IS_OPENED)
!= 0) {
status = STATUS_SHARING_VIOLATION; /* Only one client may connect */
}
if (NT_SUCCESS(status)) {
/* Clear buffer */
fort_buffer_clear(&g_device->buffer);
fort_buffer_clear(&fort_device()->buffer);
}
fort_request_complete(irp, status);
@ -94,22 +95,22 @@ FORT_API NTSTATUS fort_device_cleanup(PDEVICE_OBJECT device, PIRP irp)
/* Device closed */
fort_device_flag_set(
&g_device->conf, (FORT_DEVICE_IS_OPENED | FORT_DEVICE_IS_VALIDATED), FALSE);
&fort_device()->conf, (FORT_DEVICE_IS_OPENED | FORT_DEVICE_IS_VALIDATED), FALSE);
/* Clear conf */
{
const FORT_CONF_FLAGS old_conf_flags = fort_conf_ref_set(&g_device->conf, NULL);
FORT_CONF_FLAGS conf_flags = g_device->conf.conf_flags;
const FORT_CONF_FLAGS old_conf_flags = fort_conf_ref_set(&fort_device()->conf, NULL);
FORT_CONF_FLAGS conf_flags = fort_device()->conf.conf_flags;
fort_conf_zones_set(&g_device->conf, NULL);
fort_conf_zones_set(&fort_device()->conf, NULL);
fort_stat_conf_flags_update(&g_device->stat, &conf_flags);
fort_stat_conf_flags_update(&fort_device()->stat, &conf_flags);
fort_callout_force_reauth(old_conf_flags);
}
/* Clear buffer */
fort_buffer_clear(&g_device->buffer);
fort_buffer_clear(&fort_device()->buffer);
fort_request_complete(irp, STATUS_SUCCESS);
@ -122,7 +123,7 @@ static void fort_device_cancel_pending(PDEVICE_OBJECT device, PIRP irp)
ULONG_PTR info;
const NTSTATUS status = fort_buffer_cancel_pending(&g_device->buffer, irp, &info);
const NTSTATUS status = fort_buffer_cancel_pending(&fort_device()->buffer, irp, &info);
IoSetCancelRoutine(irp, NULL);
IoReleaseCancelSpinLock(irp->CancelIrql); /* before IoCompleteRequest()! */
@ -134,7 +135,7 @@ static NTSTATUS fort_device_control_validate(const PFORT_CONF_VERSION conf_ver,
{
if (len == sizeof(FORT_CONF_VERSION)) {
if (conf_ver->driver_version == DRIVER_VERSION) {
fort_device_flag_set(&g_device->conf, FORT_DEVICE_IS_VALIDATED, TRUE);
fort_device_flag_set(&fort_device()->conf, FORT_DEVICE_IS_VALIDATED, TRUE);
return STATUS_SUCCESS;
}
}
@ -151,10 +152,11 @@ static NTSTATUS fort_device_control_setconf(const PFORT_CONF_IO conf_io, ULONG l
if (conf_ref == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
} else {
const FORT_CONF_FLAGS old_conf_flags = fort_conf_ref_set(&g_device->conf, conf_ref);
const FORT_CONF_FLAGS old_conf_flags =
fort_conf_ref_set(&fort_device()->conf, conf_ref);
fort_shaper_conf_update(&g_device->shaper, conf_io);
fort_stat_conf_update(&g_device->stat, conf_io);
fort_shaper_conf_update(&fort_device()->shaper, conf_io);
fort_stat_conf_update(&fort_device()->stat, conf_io);
return fort_callout_force_reauth(old_conf_flags);
}
@ -166,10 +168,11 @@ static NTSTATUS fort_device_control_setconf(const PFORT_CONF_IO conf_io, ULONG l
static NTSTATUS fort_device_control_setflags(const PFORT_CONF_FLAGS conf_flags, ULONG len)
{
if (len == sizeof(FORT_CONF_FLAGS)) {
const FORT_CONF_FLAGS old_conf_flags = fort_conf_ref_flags_set(&g_device->conf, conf_flags);
const FORT_CONF_FLAGS old_conf_flags =
fort_conf_ref_flags_set(&fort_device()->conf, conf_flags);
fort_shaper_conf_flags_update(&g_device->shaper, conf_flags);
fort_stat_conf_flags_update(&g_device->stat, conf_flags);
fort_shaper_conf_flags_update(&fort_device()->shaper, conf_flags);
fort_stat_conf_flags_update(&fort_device()->stat, conf_flags);
return fort_callout_force_reauth(old_conf_flags);
}
@ -182,7 +185,7 @@ static NTSTATUS fort_device_control_getlog(PVOID out, ULONG out_len, PIRP irp, U
if (out_len < FORT_BUFFER_SIZE) {
return STATUS_BUFFER_TOO_SMALL;
} else {
const NTSTATUS status = fort_buffer_xmove(&g_device->buffer, irp, out, out_len, info);
const NTSTATUS status = fort_buffer_xmove(&fort_device()->buffer, irp, out, out_len, info);
if (status == STATUS_PENDING) {
IoMarkIrpPending(irp);
@ -203,7 +206,7 @@ static NTSTATUS fort_device_control_getlog(PVOID out, ULONG out_len, PIRP irp, U
static NTSTATUS fort_device_control_app(const PFORT_APP_ENTRY app_entry, ULONG len, BOOL is_adding)
{
if (len > sizeof(FORT_APP_ENTRY) && len >= (sizeof(FORT_APP_ENTRY) + app_entry->path_len)) {
PFORT_CONF_REF conf_ref = fort_conf_ref_take(&g_device->conf);
PFORT_CONF_REF conf_ref = fort_conf_ref_take(&fort_device()->conf);
if (conf_ref == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
@ -217,7 +220,7 @@ static NTSTATUS fort_device_control_app(const PFORT_APP_ENTRY app_entry, ULONG l
status = STATUS_SUCCESS;
}
fort_conf_ref_put(&g_device->conf, conf_ref);
fort_conf_ref_put(&fort_device()->conf, conf_ref);
if (NT_SUCCESS(status)) {
fort_worker_reauth();
@ -238,7 +241,7 @@ static NTSTATUS fort_device_control_setzones(const PFORT_CONF_ZONES zones, ULONG
if (conf_zones == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
} else {
fort_conf_zones_set(&g_device->conf, conf_zones);
fort_conf_zones_set(&fort_device()->conf, conf_zones);
fort_worker_reauth();
@ -252,7 +255,7 @@ static NTSTATUS fort_device_control_setzones(const PFORT_CONF_ZONES zones, ULONG
static NTSTATUS fort_device_control_setzoneflag(const PFORT_CONF_ZONE_FLAG zone_flag, ULONG len)
{
if (len == sizeof(FORT_CONF_ZONE_FLAG)) {
fort_conf_zone_flag_set(&g_device->conf, zone_flag);
fort_conf_zone_flag_set(&fort_device()->conf, zone_flag);
fort_worker_reauth();
@ -268,7 +271,7 @@ static NTSTATUS fort_device_control_process(
const int control_code = irp_stack->Parameters.DeviceIoControl.IoControlCode;
if (control_code != FORT_IOCTL_VALIDATE
&& fort_device_flag(&g_device->conf, FORT_DEVICE_IS_VALIDATED) == 0)
&& fort_device_flag(&fort_device()->conf, FORT_DEVICE_IS_VALIDATED) == 0)
return STATUS_INVALID_PARAMETER;
PVOID buffer = irp->AssociatedIrp.SystemBuffer;