1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00
Files
BakaShrot/OldSource/Commons/Networking/Packets.pbi
T
aziascreations e044fcf359 Moved the PureBasic version to OldSource/, Started the Rust rewrite in Source/
The rewrite's plan and decisions are in revamp.md. Steps 0 to 10 are done:
the workspace and checks, bakashrot-proto, bakashrot-pki and
bakashrot-cert, bakashrot-config, bakashrot-storage (schema v9), the
server and the client library (every request but replication's,
attachments and retention included), the C DLL with its C, PureBasic and
Python bindings, and the bakashrot tool. Step 11 (replication) is next.

OldSource/ is the old implementation, kept building and passing as the
reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RwFeVTGs2SsufaH6zquzcb
2026-11-14 12:47:40 +01:00

230 lines
9.2 KiB
Plaintext

;{- Code Header
; ==- Basic Info -================================
; Name: Packets.pbi
; Version: 0.0.1
; Author: Herwin Bozet (NibblePoker)
;
; ==- Compatibility -=============================
; Tested compiler versions:
; * PureBasic 5.73 LTS (Windows x64, Linux x64)
; * PureBasic 6.40 C Backend (Windows x64)
; * PureBasic 6.41 (Linux x64)
; * PureBasic 6.41 C Backend (Linux x64)
;
; ==- Links -=====================================
; GitHub: https://github.com/aziascreations/BakaShrot
;}
XIncludeFile "../../Includes/PB-MessagePack/Includes/MessagePack.pbi"
XIncludeFile "Names.pbi"
; The refusal codes (RequestRefused) and the client's error codes.
XIncludeFile "../../Client/BakaShrot_ErrorCodes.pbi"
; Represents the root of all BakaShrot packets.
; Any packets below this size MUST be thrown out.
Structure BakaShrot_PacketBase
Magic.l
Version.l
PacketType.l
EndStructure
; Fixed values every BakaShrot_PacketBase must carry. Checked on decode so a
; packet from a different protocol/version is thrown out instead of being
; misparsed.
#BakaShrot_ProtocolMagic = $42534854 ; "BSHT"
; 2: topics and nodes by name (Commons/Networking/Names.pbi), see Design/Packets.md.
; 3: the Hello sealed for the server, a key per connection, everything after
; the ChallengeRequest encrypted (Connections.pbi). Messages carry when
; they were pushed (PushedAt).
; 4: attachments (Design/Attachments.md).
; 5: the Hello carries the client's node only: its default topic is the
; server's to say (DefaultTopic in its client section).
; 6: RequestRefused carries a Code (Client/BakaShrot_ErrorCodes.pbi).
#BakaShrot_ProtocolVersion = 6
; Size in bytes of the nonce sent in a challenge request and echoed in its response.
; It MUST be filled with CryptRandomData(), never reused, and be checked against the one the
; server issued for that connection, so a captured response can't be replayed.
#BakaShrot_NonceSize = 32
; Size in bytes of the random challenge sent Base64-encoded in a challenge request.
; The client proves it holds its private key by signing the challenge followed by the nonce.
#BakaShrot_ChallengeSize = 32
; Size in bytes of the public half of a connection's key pair, sent by the
; client in its Hello and by the server in its ChallengeRequest
; (#BakaShrot_Session_PublicKeySize, Commons/Cryptography/Session.pbi).
#BakaShrot_SessionKeySize = 32
; Size in bytes of the server's signature in a ChallengeRequest
; (#BakaShrot_Ed25519_SignatureSize).
#BakaShrot_HandshakeSignatureSize = 64
; When a message was pushed: milliseconds since 1970-01-01 00:00:00 UTC, set
; by the server it was pushed to, and kept as is by every server it's
; replicated to. Anything outside 1 to #BakaShrot_MaxTimestamp (9999-12-31)
; is invalid on the wire.
#BakaShrot_MaxTimestamp = 253402300799999
Procedure.i BakaShrot_IsValidTimestamp(Timestamp.q)
ProcedureReturn Bool(Timestamp >= 1 And Timestamp <= #BakaShrot_MaxTimestamp)
EndProcedure
; Attachments (Design/Attachments.md): at most one per message, sent and
; stored in parts of #BakaShrot_AttachmentChunkSize bytes (the last one what's
; left), checked with their SHA-256. #BakaShrot_MaxAttachmentSize is the
; protocol's limit, each server sets its own ([BakaShrot.Server]
; MaxAttachmentSize).
#BakaShrot_AttachmentChunkSize = 1048576
#BakaShrot_MaxAttachmentSize = 1099511627776
#BakaShrot_MaxAttachmentChunks = #BakaShrot_MaxAttachmentSize / #BakaShrot_AttachmentChunkSize
#BakaShrot_Sha256Size = 32
; How many parts Size bytes take.
Procedure.q BakaShrot_AttachmentChunkCount(Size.q)
ProcedureReturn (Size + #BakaShrot_AttachmentChunkSize - 1) / #BakaShrot_AttachmentChunkSize
EndProcedure
; How big part Index of Size bytes is, 0 if there's no such part.
Procedure.i BakaShrot_AttachmentChunkBytes(Size.q, Index.q)
Protected Left.q = Size - Index * #BakaShrot_AttachmentChunkSize
If Index < 0 Or Left <= 0
ProcedureReturn 0
ElseIf Left > #BakaShrot_AttachmentChunkSize
ProcedureReturn #BakaShrot_AttachmentChunkSize
EndIf
ProcedureReturn Left
EndProcedure
; Biggest packet accepted on the wire once authenticated, anything bigger
; aborts the connection. Before that, only #BakaShrot_MaxHandshakePacketSize
; (Connections.pbi).
#BakaShrot_MaxPacketSize = 16 * 1024 * 1024
; Biggest message a client may push (its JSON, in UTF-8 bytes). Leaves room
; for the fields around it, so a stored message fits in every packet that
; carries one: LatestMessage, a ReplicateMessages batch of one.
#BakaShrot_MaxMessageSize = #BakaShrot_MaxPacketSize - 4096
Enumeration BakaShrot_PacketTypes
; Safety fallback value, should be dropped when encountered
#BakaShrot_PacketType_None = 0
; 1-3 were used by the removed AuthenticationPacket.pbi, don't reuse them.
#BakaShrot_PacketType_ClientHello = 4
#BakaShrot_PacketType_ChallengeRequest = 5
#BakaShrot_PacketType_ChallengeResponse = 6
#BakaShrot_PacketType_AuthAccepted = 7
; Only accepted once authenticated.
#BakaShrot_PacketType_PushMessage = 8
#BakaShrot_PacketType_MessagePushed = 9
#BakaShrot_PacketType_GetLatestMessage = 10
#BakaShrot_PacketType_LatestMessage = 11
#BakaShrot_PacketType_RequestRefused = 12
; Replication, only accepted from peers once authenticated.
#BakaShrot_PacketType_SyncStateRequest = 13
#BakaShrot_PacketType_SyncState = 14
#BakaShrot_PacketType_ReplicateMessages = 15
; Raw read-only SQL, only accepted once authenticated, from clients allowed to.
#BakaShrot_PacketType_QueryRequest = 16
#BakaShrot_PacketType_QueryResult = 17
; Name <-> id of a topic or node, only accepted once authenticated.
#BakaShrot_PacketType_ResolveName = 18
#BakaShrot_PacketType_NameResolved = 19
; Every topic or node the client may see, only accepted once authenticated.
#BakaShrot_PacketType_ListNames = 20
#BakaShrot_PacketType_NamesList = 21
; A run of messages of a topic/node, only accepted once authenticated.
#BakaShrot_PacketType_GetMessages = 22
#BakaShrot_PacketType_Messages = 23
; Attachments, only accepted once authenticated: uploading one (before
; its PushMessage), and reading one, in parts.
#BakaShrot_PacketType_UploadStart = 24
#BakaShrot_PacketType_UploadStarted = 25
#BakaShrot_PacketType_UploadChunk = 26
#BakaShrot_PacketType_ChunkStored = 27
#BakaShrot_PacketType_GetAttachment = 28
#BakaShrot_PacketType_AttachmentChunk = 29
; Replicating attachments, only accepted from peers, once their messages are.
#BakaShrot_PacketType_MissingAttachmentsRequest = 30
#BakaShrot_PacketType_MissingAttachments = 31
#BakaShrot_PacketType_ReplicateAttachmentChunk = 32
EndEnumeration
; Checks and reads the given MsgPack data into a packet structure's common fields.
; The caller can handle the MsgPack data validation however it see fit.
; On success, the structure's fields will be populated, and the MsgPack structure will be ready to ready the packet's remaining fields.
; On failure, all data should be discarded and be treated as tainted.
Procedure.b BakaShrot_ReadPacketBase(*PacketStruct.BakaShrot_PacketBase, *MsgPackData.MsgPackData)
; Returns true on success, false otherwise
; The caller will usually drop all data and tell the remote client to fuck off if this fails.
Protected TempValue.q
If Not *PacketStruct Or Not *MsgPackData
ProcedureReturn #False
EndIf
; Magic
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
ProcedureReturn #False
EndIf
TempValue = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or TempValue <> #BakaShrot_ProtocolMagic
ProcedureReturn #False
EndIf
*PacketStruct\Magic = TempValue
; Version
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
ProcedureReturn #False
EndIf
TempValue = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or TempValue <> #BakaShrot_ProtocolVersion
ProcedureReturn #False
EndIf
*PacketStruct\Version = TempValue
; PacketType
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
ProcedureReturn #False
EndIf
TempValue = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or TempValue <= #BakaShrot_PacketType_None Or TempValue > 2147483647
ProcedureReturn #False
EndIf
*PacketStruct\PacketType = TempValue
ProcedureReturn #True
EndProcedure
; Writes a packet structure's common fields into the given MsgPack data.
; On success, the MsgPack structure will be ready to write the packet's remaining fields.
; On failure, the MsgPack data should be discarded and be treated as tainted.
Procedure.b BakaShrot_WritePacketBase(*PacketStruct.BakaShrot_PacketBase, *MsgPackData.MsgPackData)
; Returns true on success, false otherwise
If MsgPackWriteInteger(*MsgPackData, *PacketStruct\Magic) <> #MsgPack_Error_Success
ProcedureReturn #False
EndIf
If MsgPackWriteInteger(*MsgPackData, *PacketStruct\Version) <> #MsgPack_Error_Success
ProcedureReturn #False
EndIf
If MsgPackWriteInteger(*MsgPackData, *PacketStruct\PacketType) <> #MsgPack_Error_Success
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure