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), flags: cast[uint16](FLAG_ENCRYPTED),
size: 0'u32, size: 0'u32,
agentId: uuidToUint32(config.agentId), agentId: uuidToUint32(config.agentId),
seqNr: 0'u64, seqNr: 0'u32,
iv: generateIV(), iv: generateIV(),
gmac: default(AuthenticationTag) gmac: default(AuthenticationTag)
), ),

View File

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

View File

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

View File

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

View File

@@ -7,7 +7,7 @@ import streams
const const
MAGIC* = 0x514E3043'u32 # Magic value: C0NQ MAGIC* = 0x514E3043'u32 # Magic value: C0NQ
VERSION* = 1'u8 # Version 1 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 type
PacketType* = enum PacketType* = enum
@@ -69,7 +69,7 @@ type
flags*: uint16 # [2 bytes ] message flags flags*: uint16 # [2 bytes ] message flags
size*: uint32 # [4 bytes ] size of the payload body size*: uint32 # [4 bytes ] size of the payload body
agentId*: uint32 # [4 bytes ] agent id, used as AAD for encryptio 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 iv*: Iv # [12 bytes] random IV for AES256 GCM encryption
gmac*: AuthenticationTag # [16 bytes] authentication tag for AES256 GCM encryption gmac*: AuthenticationTag # [16 bytes] authentication tag for AES256 GCM encryption