raw
smg_comms_skeys_s...    1  -- Message reader & writers for SMG Communication Protocol
smg_comms_skeys_s... 2 -- This part effectively serializes/deserializes game data structures
smg_comms_skeys_s... 3 -- for transmission between client and server.
smg_comms_skeys_s... 4 -- Note that messages themeselves are simply arrays of octets.
smg_comms_skeys_s... 5 -- (see also raw_types.ads/adb and packing.ads/adb for related parts)
smg_comms_skeys_s... 6 -- NB: message ids and padding as per protocol spec are handled HERE ONLY.
smg_comms_skeys_s... 7 -- Relies on:
smg_comms_skeys_s... 8 -- RNG (for random padding)
smg_comms_skeys_s... 9 -- CRC32 (for CRC calculations)
smg_comms_skeys_s... 10 -- Raw_Types
smg_comms_skeys_s... 11 -- Data_Structs
smg_comms_skeys_s... 12 -- S.MG, 2018
smg_comms_skeys_s... 13
smg_comms_skeys_s... 14 with Raw_Types;
smg_comms_skeys_s... 15 with RNG;
smg_comms_skeys_s... 16 with CRC32;
smg_comms_skeys_s... 17 with Data_Structs; use Data_Structs;
smg_comms_skeys_s... 18 with Interfaces;
smg_comms_skeys_s... 19
smg_comms_skeys_s... 20 package Messages is
smg_comms_skeys_s... 21 -- exception raised when given message to read fails sanity checks
smg_comms_skeys_s... 22 Invalid_Msg: exception;
smg_comms_skeys_s... 23
smg_comms_keymgm 24 ----------------------
smg_comms_keymgm 25 -- Serpent Messages --
smg_comms_keymgm 26 ----------------------
smg_comms_keymgm 27
smg_comms_keymgm 28 -------------------------Keys----------------------------------------
smg_comms_skeys_s... 29 -- Writes a Serpent Keyset to the given Serpent Message
smg_comms_skeys_s... 30 --
smg_comms_skeys_s... 31 -- Keyset - the set of keys to write to message
smg_comms_skeys_s... 32 -- Counter - the message count
smg_comms_skeys_s... 33 procedure Write_SKeys_SMsg( Keyset : in Serpent_Keyset;
smg_comms_skeys_s... 34 Counter : in Interfaces.Unsigned_16;
smg_comms_skeys_s... 35 Msg : out Raw_Types.Serpent_Msg);
smg_comms_skeys_s... 36
smg_comms_skeys_s... 37 -- Reads a Serpent Keyset from the given Serpent Message
smg_comms_skeys_s... 38 -- The opposite of Write_SKeys_SMsg above
smg_comms_skeys_s... 39 -- Raises Invalid_Message exception if given message fails sanity checks
smg_comms_skeys_s... 40 procedure Read_SKeys_SMsg( Msg : in Raw_Types.Serpent_Msg;
smg_comms_skeys_s... 41 Counter : out Interfaces.Unsigned_16;
smg_comms_skeys_s... 42 Keyset : out Serpent_Keyset);
smg_comms_skeys_s... 43
smg_comms_keymgm 44 ------------------------Keys Management------------------------------
smg_comms_keymgm 45 -- Writes a Key Management structure to the given Serpent Message
smg_comms_keymgm 46 --
smg_comms_keymgm 47 -- KMgm - the keys management structure to write to message
smg_comms_keymgm 48 -- Counter - the message count
smg_comms_keymgm 49 procedure Write_KMgm_SMsg( KMgm : in Keys_Mgm;
smg_comms_keymgm 50 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 51 Msg : out Raw_Types.Serpent_Msg);
smg_comms_keymgm 52
smg_comms_keymgm 53 -- Reads a Key management structure from the given Serpent Message
smg_comms_keymgm 54 -- The opposite of Write_KMgm_SMsg above
smg_comms_keymgm 55 -- Raises Invalid_Message exception if given message fails sanity checks
smg_comms_keymgm 56 procedure Read_KMgm_SMsg( Msg : in Raw_Types.Serpent_Msg;
smg_comms_keymgm 57 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 58 KMgm : out Keys_Mgm);
smg_comms_keymgm 59
smg_comms_files 60 ----------------- File Transfer ----------------------
smg_comms_files 61
smg_comms_files 62 -- Writes the given File Chunk to a File Transfer type of message
smg_comms_files 63 -- Chunk - chunk of file to write; contains counter, filename, content
smg_comms_files 64 procedure Write_File_Transfer( Chunk : in File_Chunk;
smg_comms_files 65 Msg : out Raw_Types.Serpent_Msg);
smg_comms_files 66
smg_comms_files 67 -- The opposite of Write_File_Transfer method above.
smg_comms_files 68 -- Chunk will contain the counter, filename and content
smg_comms_files 69 procedure Read_File_Transfer( Msg : in Raw_Types.Serpent_Msg;
smg_comms_files 70 Chunk : out File_Chunk);
smg_comms_files 71
smg_comms_files 72 ----------------- File Request ----------------------
smg_comms_files 73 -- Writes a message to request the files specified through their names
smg_comms_files 74 -- Written parameter will hold the number of filenames actually written
smg_comms_files 75 -- NB: this can be less than the number of filenames provided!
smg_comms_files 76 -- When Written < FR.F_No, the FIRST Written filenames were written; the rest
smg_comms_files 77 -- did not fit into the message and it's up to caller to decide what to do.
smg_comms_files 78 procedure Write_File_Request( FR : in Filenames;
smg_comms_files 79 Counter : in Interfaces.Unsigned_16;
smg_comms_files 80 Msg : out Raw_Types.Serpent_Msg;
smg_comms_files 81 Written : out Natural);
smg_comms_keymgm 82
smg_comms_files 83 -- Reads a request for files; the opposite of Write_File_Request above
smg_comms_files 84 -- Raises Invalid_Msg exception if the provided message fails checks.
smg_comms_files 85 procedure Read_File_Request( Msg : in Raw_Types.Serpent_Msg;
smg_comms_files 86 Counter : out Interfaces.Unsigned_16;
smg_comms_files 87 FR : out Filenames);
smg_comms_keymgm 88
smg_comms_keymgm 89 ------------------
smg_comms_keymgm 90 -- RSA Messages --
smg_comms_keymgm 91 ------------------
smg_comms_keymgm 92
smg_comms_keymgm 93 -------------------------Keys----------------------------------------
smg_comms_keymgm 94 -- Writes a Serpent Keyset to the given RSA Message
smg_comms_keymgm 95 --
smg_comms_keymgm 96 -- Keyset - the set of keys to write to message
smg_comms_keymgm 97 -- Counter - the message count
smg_comms_keymgm 98 procedure Write_SKeys_RMsg( Keyset : in Serpent_Keyset;
smg_comms_keymgm 99 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 100 Msg : out Raw_Types.RSA_Msg);
smg_comms_keymgm 101
smg_comms_keymgm 102 -- Reads a Serpent Keyset from the given RSA Message
smg_comms_keymgm 103 -- The opposite of Write_SKeys_RMsg above
smg_comms_keymgm 104 -- Raises Invalid_Message exception if given message fails sanity checks
smg_comms_keymgm 105 procedure Read_SKeys_RMsg( Msg : in Raw_Types.RSA_Msg;
smg_comms_keymgm 106 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 107 Keyset : out Serpent_Keyset);
smg_comms_keymgm 108
smg_comms_keymgm 109 ------------------------Keys Management------------------------------
smg_comms_keymgm 110 -- Writes a Key Management structure to the given RSA Message
smg_comms_keymgm 111 --
smg_comms_keymgm 112 -- KMgm - the keys management structure to write to message
smg_comms_keymgm 113 -- Counter - the message count
smg_comms_keymgm 114 procedure Write_KMgm_RMsg( KMgm : in Keys_Mgm;
smg_comms_keymgm 115 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 116 Msg : out Raw_Types.RSA_Msg);
smg_comms_keymgm 117
smg_comms_keymgm 118 -- Reads a Key management structure from the given RSA Message
smg_comms_keymgm 119 -- The opposite of Write_KMgm_SMsg above
smg_comms_files 120 -- Raises Invalid_Msg exception if given message fails sanity checks
smg_comms_keymgm 121 procedure Read_KMgm_RMsg( Msg : in Raw_Types.RSA_Msg;
smg_comms_keymgm 122 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 123 KMgm : out Keys_Mgm);
smg_comms_keymgm 124
smg_comms_keymgm 125
smg_comms_files 126 -- String to Octets conversion
smg_comms_files 127 -- NB: Str'Length has to be EQUAL to Octets'Length!
smg_comms_files 128 procedure String_To_Octets(Str: in String; O: out Raw_Types.Octets);
smg_comms_files 129
smg_comms_files 130 -- Octets to string conversion
smg_comms_files 131 -- NB: Str'Length has to be EQUAL to Octets'Length!
smg_comms_files 132 procedure Octets_To_String(O: in Raw_Types.Octets; Str: out String);
smg_comms_files 133
smg_comms_skeys_s... 134 private
smg_comms_skeys_s... 135
smg_comms_skeys_s... 136 -- if native is little endian, does nothing;
smg_comms_skeys_s... 137 -- if native is big endian, it flips the input's octets.
smg_comms_skeys_s... 138 -- (strictly for arrays of octets)
smg_comms_skeys_s... 139 procedure Cast_LE( LE: in out Raw_Types.Octets );
smg_comms_skeys_s... 140
smg_comms_files 141 -- separator used for list of filenames
smg_comms_files 142 F_Sep: constant String := ";"; --as string
smg_comms_files 143 Sep: constant Interfaces.Unsigned_8 := 16#3B#; -- ascii code
smg_comms_files 144
smg_comms_skeys_s... 145 -- protocol message types IDs, fixed as per protocol specification
smg_comms_skeys_s... 146 -- Serpent messages end in "S_Type"
smg_comms_skeys_s... 147 -- RSA messages end in "R_Type"
smg_comms_skeys_s... 148 -- Character action types end in "A_Type"
smg_comms_skeys_s... 149
smg_comms_skeys_s... 150 -- Serpent message types
smg_comms_skeys_s... 151 SKeys_S_Type : constant Interfaces.Unsigned_8 := 1;
smg_comms_skeys_s... 152 Key_Mgm_S_Type : constant Interfaces.Unsigned_8 := 2;
smg_comms_skeys_s... 153 File_Transfer_S_Type : constant Interfaces.Unsigned_8 := 3;
smg_comms_skeys_s... 154 File_Req_S_Type : constant Interfaces.Unsigned_8 := 4;
smg_comms_skeys_s... 155 Client_Action_S_Type : constant Interfaces.Unsigned_8 := 5;
smg_comms_skeys_s... 156 World_Bulletin_S_Type: constant Interfaces.Unsigned_8 := 6;
smg_comms_skeys_s... 157 Obj_Request_S_Type : constant Interfaces.Unsigned_8 := 7;
smg_comms_skeys_s... 158 Obj_Info_S_Type : constant Interfaces.Unsigned_8 := 8;
smg_comms_skeys_s... 159
smg_comms_skeys_s... 160 -- RSA message types
smg_comms_skeys_s... 161 RKeys_R_Type : constant Interfaces.Unsigned_8 := 251;
smg_comms_skeys_s... 162 SKeys_R_Type : constant Interfaces.Unsigned_8 := 157;
smg_comms_skeys_s... 163 Key_Mgm_R_Type : constant Interfaces.Unsigned_8 := 233;
smg_comms_skeys_s... 164
smg_comms_skeys_s... 165 -- Character action types
smg_comms_skeys_s... 166 Lock_A_Type : constant Interfaces.Unsigned_8 := 0;
smg_comms_skeys_s... 167 Make_A_Type : constant Interfaces.Unsigned_8 := 1;
smg_comms_skeys_s... 168 Explore_A_Type : constant Interfaces.Unsigned_8 := 2;
smg_comms_skeys_s... 169 Exchange_A_Type : constant Interfaces.Unsigned_8 := 3;
smg_comms_skeys_s... 170 Attack_A_Type : constant Interfaces.Unsigned_8 := 4;
smg_comms_skeys_s... 171 Repair_A_Type : constant Interfaces.Unsigned_8 := 5;
smg_comms_skeys_s... 172 Move_A_Type : constant Interfaces.Unsigned_8 := 6;
smg_comms_skeys_s... 173 Train_A_Type : constant Interfaces.Unsigned_8 := 7;
smg_comms_skeys_s... 174
smg_comms_keymgm 175 -- internal read/write of Serpent Keys
smg_comms_keymgm 176 -- those are called by both interface methods for RSA and Serpent messages
smg_comms_keymgm 177
smg_comms_keymgm 178 -- NB: caller HAS TO provide ENOUGH space in Msg AND valid Type_ID
smg_comms_keymgm 179 -- Msg will be padded with random octets until full length.
smg_comms_keymgm 180 procedure Write_SKeys( Keyset : in Serpent_Keyset;
smg_comms_keymgm 181 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 182 Type_ID : in Interfaces.Unsigned_8;
smg_comms_keymgm 183 Msg : out Raw_Types.Octets);
smg_comms_keymgm 184
smg_comms_keymgm 185 -- NB: caller has to ensure that Msg is a valid RSA or Serpent message
smg_comms_keymgm 186 procedure Read_SKeys( Msg : in Raw_Types.Octets;
smg_comms_keymgm 187 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 188 Keyset : out Serpent_Keyset);
smg_comms_keymgm 189
smg_comms_keymgm 190
smg_comms_keymgm 191 -- internal read/write of Key Management structures
smg_comms_keymgm 192 -- those are called by both interface methods for RSA and Serpent messages
smg_comms_keymgm 193
smg_comms_keymgm 194 -- NB: caller HAS TO provide ENOUGH space in Msg AND valid Type_ID
smg_comms_keymgm 195 -- Msg will be padded with random octets until full length.
smg_comms_keymgm 196 procedure Write_KMgm( KMgm : in Keys_Mgm;
smg_comms_keymgm 197 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 198 Type_ID : in Interfaces.Unsigned_8;
smg_comms_keymgm 199 Msg : out Raw_Types.Octets);
smg_comms_keymgm 200
smg_comms_keymgm 201 -- NB: caller has to ensure that Msg is a valid RSA or Serpent message
smg_comms_keymgm 202 procedure Read_KMgm( Msg : in Raw_Types.Octets;
smg_comms_keymgm 203 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 204 KMgm : out Keys_Mgm);
smg_comms_keymgm 205
smg_comms_files 206 -- helper methods to read/write a value on 16 bits (using Cast_LE as needed)
smg_comms_files 207
smg_comms_files 208 -- Writing a 16-bits value to the given Octets at specified position.
smg_comms_files 209 -- NB: Caller has to make sure Pos <= Msg'Last -1 ; NO check here!
smg_comms_files 210 -- Msg - the array of octets into which the value is to be written
smg_comms_files 211 -- Pos - the position in Msg at which to write the value;
smg_comms_files 212 -- NB: Pos will be increased by 2 after the write!
smg_comms_files 213 -- U16 - the value to write
smg_comms_files 214 procedure Write_U16( Msg: in out Raw_Types.Octets;
smg_comms_files 215 Pos: in out Natural;
smg_comms_files 216 U16: in Interfaces.Unsigned_16);
smg_comms_files 217
smg_comms_files 218 -- Reading a 16-bits value from the given Octets at specified position.
smg_comms_files 219 -- NB: Caller has to make sure Pos <= Msg'Last -1 ; NO check here!
smg_comms_files 220 -- Pos - the position in Msg from which to read the value;
smg_comms_files 221 -- NB: Pos will be increased by 2 after the read!
smg_comms_files 222 -- The opposite of Write_U16 above.
smg_comms_files 223 procedure Read_U16( Msg: in Raw_Types.Octets;
smg_comms_files 224 Pos: in out Natural;
smg_comms_files 225 U16: out Interfaces.Unsigned_16);
smg_comms_skeys_s... 226 end Messages;