diff -uNr a/smg_comms/manifest b/smg_comms/manifest --- a/smg_comms/manifest 865132e483b57dcf86c4beb6f0123019747cbcb3745f8e297103d0e931af958d9a4e57f213b6b41ba8b7ad0fcd6b0dd0967150243d24a9bc24c5f6fc6a365b1c +++ b/smg_comms/manifest 7d5acfd1981b93ad649daab47b2ec552e09ac46ad0329914b6c5503c092c899a9804c9a506b4c22fb8c930e2a3acedcd55470f9f4cba5c4c7993c7903f5722f4 @@ -1,2 +1,2 @@ 532398 smg_comms_genesis diana_coman The first seed of an implementation of S.MG's communication protocol for Eulora: definitions for basic types, methods to/from network format, basic client/server test running locally on the same machine. - +546000 smg_comms_raw_types diana_coman Part of layer 0 - raw types for the updated version of the protocol containing only two types of packets: 1470 octets RSA packet and 1472 octets Serpent packet. diff -uNr a/smg_comms/restrict.adc b/smg_comms/restrict.adc --- a/smg_comms/restrict.adc false +++ b/smg_comms/restrict.adc 52ae9657ad5abadac6412597e48d26257703f293eb9348353097eeb9f2b00934fc862b075776b751b68e0423cbf452fbd1f3a9db0fb99c93e6d9f551114b33d7 @@ -0,0 +1,64 @@ +pragma Restrictions(Immediate_Reclamation); +pragma Restrictions(Max_Asynchronous_Select_Nesting => 0); +pragma Restrictions(Max_Protected_Entries => 0); +pragma Restrictions(Max_Select_Alternatives => 0); +pragma Restrictions(Max_Task_Entries => 0); +pragma Restrictions(Max_Tasks => 0); +pragma Restrictions(No_Abort_Statements); +pragma Restrictions(No_Access_Parameter_Allocators); +pragma Restrictions(No_Allocators); +pragma Restrictions(No_Asynchronous_Control); +pragma Restrictions(No_Calendar); +pragma Restrictions(No_Coextensions); +pragma Restrictions(No_Default_Stream_Attributes); +pragma Restrictions(No_Delay); +pragma Restrictions(No_Dispatch); +pragma Restrictions(No_Dispatching_Calls); +pragma Restrictions(No_Dynamic_Attachment); +pragma Restrictions(No_Dynamic_Priorities); +pragma Restrictions(No_Entry_Calls_In_Elaboration_Code); +pragma Restrictions(No_Entry_Queue); +pragma Restrictions(No_Enumeration_Maps); +--pragma Restrictions(No_Exception_Propagation); +pragma Restrictions(No_Exception_Registration); +pragma Restrictions(No_Finalization); +pragma Restrictions(No_Fixed_Io); +--pragma Restrictions(No_Floating_Point); +pragma Restrictions(No_Implementation_Aspect_Specifications); +pragma Restrictions(No_Implementation_Units); +pragma Restrictions(No_Implicit_Conditionals); +pragma Restrictions(No_Implicit_Dynamic_Code); +pragma Restrictions(No_Implicit_Heap_Allocations); +pragma Restrictions(No_Implicit_Protected_Object_Allocations); +pragma Restrictions(No_Implicit_Task_Allocations); +pragma Restrictions(No_Initialize_Scalars); +pragma Restrictions(No_Local_Protected_Objects); +pragma Restrictions(No_Local_Timing_Events); +pragma Restrictions(No_Multiple_Elaboration); +pragma Restrictions(No_Nested_Finalization); +pragma Restrictions(No_Protected_Type_Allocators); +pragma Restrictions(No_Protected_Types); +pragma Restrictions(No_Relative_Delay); +pragma Restrictions(No_Requeue_Statements); +pragma Restrictions(No_Secondary_Stack); +pragma Restrictions(No_Select_Statements); +pragma Restrictions(No_Specific_Termination_Handlers); +pragma Restrictions(No_Standard_Allocators_After_Elaboration); +pragma Restrictions(No_Stream_Optimizations); +pragma Restrictions(No_Streams); +pragma Restrictions(No_Task_Allocators); +pragma Restrictions(No_Task_At_Interrupt_Priority); +pragma Restrictions(No_Task_Attributes_Package); +pragma Restrictions(No_Task_Hierarchy); +pragma Restrictions(No_Tasking); +pragma Restrictions(No_Task_Termination); +pragma Restrictions(No_Terminate_Alternatives); +pragma Restrictions(No_Unchecked_Access); +--pragma Restrictions(No_Unchecked_Conversion); +pragma Restrictions(No_Unchecked_Deallocation); +pragma Restrictions(No_Wide_Characters); +pragma Restrictions(Pure_Barriers); +pragma Restrictions(Simple_Barriers); +pragma Restrictions(Static_Priorities); +pragma Restrictions(Static_Storage_Size); +pragma Validity_Checks(ALL_CHECKS); diff -uNr a/smg_comms/smg_comms.gpr b/smg_comms/smg_comms.gpr --- a/smg_comms/smg_comms.gpr 7ca4898ea436586cb87f25161c03b7118cd3f7ff3805079ea8e73a523b2a6f97b0e698256954ab2c3fcfcbd2db8d6fe1545d618054cdb04072cd3d784f17be2f +++ b/smg_comms/smg_comms.gpr 5834abb16df880d02ac7208fb19a1c2cb14cad43afa73537246d0734d9a845f156f5519bc93d2df74aa479611fc015e5f5a62023025606e5c2e43cfe894730dd @@ -3,22 +3,46 @@ -- http://trilema.com/2018/euloras-communication-protocol-restated/ project SMG_comms is + + type Mode_Type is ("debug", "release"); + Mode : Mode_Type := external ("mode", "release"); + for Languages use ("Ada"); for Source_Dirs use ("src"); for Ignore_Source_Sub_Dirs use (".svn", ".git", "@*"); for Object_Dir use "obj"; - for Exec_Dir use "."; - for Main use ("test_comms.adb"); + package Compiler is + case Mode is + when "debug" => + for Switches ("Ada") + use ("-g"); + when "release" => + for Switches ("Ada") + use ("-O2", "-fdump-scos", "-gnata", "-fstack-check", + "-gnatyd", "-gnatym", + "-fdata-sections", "-ffunction-sections", "-gnatwr", "-gnatw.d", + "-gnatec=" & SMG_Comms'Project_Dir & "restrict.adc"); + end case; + end Compiler; + package Builder is - for Executable ("test_comms.adb") use "test_comms"; + for Switches ("Ada") + use ("-nostdlib"); end Builder; - - package Compiler is - for Default_Switches ("Ada") use ("-O2"); - end Compiler; + + package Binder is + case Mode is + when "debug" => + for Switches ("Ada") + use (); + when "release" => + for Switches ("Ada") + use ("-static"); + end case; + end Binder; end SMG_comms; diff -uNr a/smg_comms/src/raw_types.ads b/smg_comms/src/raw_types.ads --- a/smg_comms/src/raw_types.ads false +++ b/smg_comms/src/raw_types.ads be61934d440dad950d4f63ea9a1bb941a7994c832adb4748a1e24083887853fdd52fdf024d940740a9d3b65759aad66a05f42b0365624b0affa0e12418e2fe8d @@ -0,0 +1,69 @@ + -- raw types for the communication protocol + -- these are used throughout at the lowest level of the protocol + -- essentially they are the units of packets and of messages + -- SMG.Comms has only 2 types of packets: RSA and Serpent + -- a message is the decrypted content of a packet + -- S.MG, 2018 + +with Interfaces; use Interfaces; -- Unsigned_n and Integer_n +with Ada.Unchecked_Conversion; + +package Raw_Types is + + -- constants from SMG.COMMS standard specification + -- size of a serpent-encrypted packet and message, in octets + -- note that this corresponds to 1472/16 = 92 Serpent blocks + -- NB: lengths are the same but the distinction makes the code clearer + SERPENT_PKT_OCTETS : constant Positive := 1472; + SERPENT_MSG_OCTETS : constant Positive := SERPENT_PKT_OCTETS; + + -- size of a RSA-encrypted packet and message in octets and bits + RSA_PKT_OCTETS : constant Positive := 1470; + RSA_MSG_OCTETS : constant Positive := 234; + RSA_MSG_BITS : constant Positive := RSA_MSG_OCTETS * 8; --1872 + + -- raw, low-level types + -- all messages and packets are simply arrays of octets at low level/raw + type Octets is array( Natural range <> ) of Interfaces.Unsigned_8; + + -- raw representations of basic types (with fixed, well-defined sizes) + subtype Octets_1 is Octets( 1 .. 1 ); + subtype Octets_2 is Octets( 1 .. 2 ); + subtype Octets_4 is Octets( 1 .. 4 ); + subtype Octets_8 is Octets( 1 .. 8 ); + + -- RSA packets and contained raw messages + subtype RSA_Pkt is Octets( 1 .. RSA_PKT_OCTETS ); + subtype RSA_Msg is Octets( 1 .. RSA_MSG_OCTETS ); + + -- Serpent packets and contained raw messages + -- NB: length is the same but the distinction makes the code clearer + subtype Serpent_Pkt is Octets( 1 .. SERPENT_PKT_OCTETS ); + subtype Serpent_Msg is Octets( 1 .. SERPENT_MSG_OCTETS ); + + -- blind, unchecked casts ( memcpy style ) + function Cast is new Ada.Unchecked_Conversion( Integer_8 , Octets_1 ); + function Cast is new Ada.Unchecked_Conversion( Octets_1 , Integer_8 ); + function Cast is new Ada.Unchecked_Conversion( Unsigned_8 , Octets_1 ); + function Cast is new Ada.Unchecked_Conversion( Octets_1 , Unsigned_8 ); + + function Cast is new Ada.Unchecked_Conversion( Integer_16 , Octets_2 ); + function Cast is new Ada.Unchecked_Conversion( Octets_2 , Integer_16 ); + function Cast is new Ada.Unchecked_Conversion( Unsigned_16, Octets_2 ); + function Cast is new Ada.Unchecked_Conversion( Octets_2 , Unsigned_16 ); + + function Cast is new Ada.Unchecked_Conversion( Integer_32 , Octets_4 ); + function Cast is new Ada.Unchecked_Conversion( Octets_4 , Integer_32 ); + function Cast is new Ada.Unchecked_Conversion( Unsigned_32, Octets_4 ); + function Cast is new Ada.Unchecked_Conversion( Octets_4 , Unsigned_32 ); + + -- Gnat's Float has 32 bits but this might be different with other compilers + function Cast is new Ada.Unchecked_Conversion( Float, Octets_4 ); + function Cast is new Ada.Unchecked_Conversion( Octets_4, Float ); + + function Cast is new Ada.Unchecked_Conversion( Integer_64, Octets_8 ); + function Cast is new Ada.Unchecked_Conversion( Octets_8, Integer_64 ); + function Cast is new Ada.Unchecked_Conversion( Unsigned_64, Octets_8 ); + function Cast is new Ada.Unchecked_Conversion( Octets_8, Unsigned_64 ); + +end Raw_Types; diff -uNr a/smg_comms/src/smg_comms_types.adb b/smg_comms/src/smg_comms_types.adb --- a/smg_comms/src/smg_comms_types.adb 59000f9e3569f550c5994913e7ba3cdb6cbb964deb307a47676b5feba9220f70b0bef3f187b22a4e69948037e6a046ed91001bcb66adab6eeee42199a0e84208 +++ b/smg_comms/src/smg_comms_types.adb false @@ -1,69 +0,0 @@ - -- S.MG, 2018 - -- prototype implementation of S.MG communication protocol - -with SMG_comms_types; use SMG_comms_types; -with System; use System; -- endianness -with Ada.Exceptions; -with Ada.Streams; use Ada.Streams; - -package body SMG_comms_types is - - -- to and from network format (i.e. big endian, stream_element_array) - procedure ToNetworkFormat( - Item : in Octet_Array; - Buffer : out Stream_Element_Array) is - begin - if Item'Length /= Buffer'Length then - raise Constraint_Error with "Item and Buffer lengths do NOT match!"; - end if; - - if Default_Bit_Order = Low_Order_First then - for I in 0 .. Item'Length - 1 loop - Buffer( Buffer'Last - Stream_Element_Offset(I) ) := Stream_Element(Item(Item'First + I)); - end loop; - else - for I in 0 .. Item'Length - 1 loop - Buffer( Buffer'First + Stream_Element_Offset(I) ) := Stream_Element(Item(Item'First + I)); - end loop; - end if; - end ToNetworkFormat; - - procedure FromNetworkFormat( - Buffer : in Stream_Element_Array; - Item : out Octet_Array) is - begin - if Item'Length /= Buffer'Length then - raise Constraint_Error with "Buffer and Item length do NOT match!"; - end if; - - if Default_Bit_Order = Low_Order_First then - for I in 0 .. Buffer'Length - 1 loop - Item( Item'Last - I ) := - Unsigned_8( Buffer( Buffer'First + Stream_Element_Offset( I ) ) ); - end loop; - else - for I in 0 .. Buffer'Length - 1 loop - Item( Item'First + I ) := - Unsigned_8( Buffer( Buffer'First + Stream_Element_Offset( I ) ) ); - end loop; - end if; - end FromNetworkFormat; - - -- Integer_8 - procedure ToNetworkFormat( - Item : in Integer_8; - Buffer : out Stream_Element_Array) is - begin - ToNetworkFormat( Cast( Item ), Buffer ); - end ToNetworkFormat; - - procedure FromNetworkFormat( - Buffer : in Stream_Element_Array; - Item : out Integer_8) is - octets: Octets_1; - begin - FromNetworkFormat(Buffer, octets); - Item := Cast( octets ); - end FromNetworkFormat; - -end SMG_comms_types; diff -uNr a/smg_comms/src/smg_comms_types.ads b/smg_comms/src/smg_comms_types.ads --- a/smg_comms/src/smg_comms_types.ads a2253e5b8f18b3f3d7d9f7d0198761771470d53dc72986c9be61b9809f101290f7def2f92970dd973e523e567a84375a25ffae2c7a80f227ae49542b75f9b259 +++ b/smg_comms/src/smg_comms_types.ads false @@ -1,52 +0,0 @@ - -- S.MG, 2018 - -- prototype implementation of S.MG communication protocol - -with Ada.Streams; use Ada.Streams; -with Interfaces; use Interfaces; -- Integer_n and Unsigned_n -with Ada.Unchecked_Conversion; -- converting int/uint to array of octets - -package SMG_comms_types is - -- basic types with guaranteed lengths - type Octet_Array is array(Natural range <>) of Unsigned_8; - - subtype Octets_1 is Octet_Array( 1 .. 1 ); - subtype Octets_2 is Octet_Array( 1 .. 2 ); - subtype Octets_4 is Octet_Array( 1 .. 4 ); - subtype Octets_8 is Octet_Array( 1 .. 8 ); - - subtype Message is Octet_Array( 1 .. 512 ); - subtype RSAMessage is Octet_Array( 1 .. 245 ); - - -- blind, unchecked casts ( memcpy style ) - function Cast is new Ada.Unchecked_Conversion( Integer_8, Octets_1 ); - function Cast is new Ada.Unchecked_Conversion( Octets_1, Integer_8 ); - - function Cast is new Ada.Unchecked_Conversion( Integer_16, Octets_2 ); - function Cast is new Ada.Unchecked_Conversion( Octets_2, Integer_16 ); - - function Cast is new Ada.Unchecked_Conversion( Integer_32, Octets_4 ); - function Cast is new Ada.Unchecked_Conversion( Octets_4, Integer_32 ); - - function Cast is new Ada.Unchecked_Conversion( Integer_64, Octets_8 ); - function Cast is new Ada.Unchecked_Conversion( Octets_8, Integer_64 ); - - -- to and from streams for network communications - general - procedure ToNetworkFormat( - Item : in Octet_Array; - Buffer : out Stream_Element_Array); - - procedure FromNetworkFormat( - Buffer : in Stream_Element_Array; - Item : out Octet_Array); - - -- specific, convenience methods for the basic types - -- Integer_8 - procedure ToNetworkFormat( - Item : in Integer_8; - Buffer : out Stream_Element_Array); - - procedure FromNetworkFormat( - Buffer : in Stream_Element_Array; - Item : out Integer_8); - -end SMG_comms_types; diff -uNr a/smg_comms/src/test_comms.adb b/smg_comms/src/test_comms.adb --- a/smg_comms/src/test_comms.adb abf3a0700182a7622a7468465c37f038b72818ddd9500efe683a923e7ca8f020c08a236bfdb0bce0c0d65fbc2425d90251688e21b834b1ef8ef5982c6f94071c +++ b/smg_comms/src/test_comms.adb false @@ -1,87 +0,0 @@ - -- S.MG, 2018 - -- prototype implementation of S.MG communication protocol - -with GNAT.Sockets; use GNAT.Sockets; -with Ada.Text_IO; use Ada.Text_IO; -with Ada.Streams; use Ada.Streams; -with Interfaces; use Interfaces; - -with SMG_comms_types; use SMG_comms_types; - -procedure test_comms is - Port_No : constant := 2222; - - task type Client is - entry Send; - end Client; - - task type Server is - entry Listen; - entry Ready; - end Server; - - task body Client is - Sock: Socket_Type; - Address: Sock_Addr_Type; - Data: Ada.Streams.Stream_Element_Array(1..10) := (others => 42); - Last: Ada.Streams.Stream_Element_Offset; - N : Integer_8 := -36; - begin - accept Send; -- task WILL block here until asked to send - Address.Port := Port_No; - Address.Addr := Inet_Addr("127.0.0.1"); - Create_Socket(Sock, Family_Inet, Socket_Datagram); - - ToNetworkFormat( N, Data(1..1)); - - Send_Socket(Sock, Data, Last, Address); - Put_Line("Client sent data " & "last: " & Last'Img); - end Client; - - task body Server is - Sock: Socket_Type; - Address, From: Sock_Addr_Type; - Data: Ada.Streams.Stream_Element_Array(1..512); - Last: Ada.Streams.Stream_Element_Offset; - N : Integer_8; - begin - accept Listen; -- wait to be started! - Put_Line("Server started!"); - -- create UDP socket - Create_Socket( Sock, Family_Inet, Socket_Datagram ); - - -- set options on UDP socket - Set_Socket_Option( Sock, Socket_Level, (Reuse_Address, True)); - Set_Socket_Option( Sock, Socket_Level, (Receive_Timeout, Timeout => 10.0)); - - -- set address and bind - Address.Addr := Any_Inet_Addr; - Address.Port := Port_No; - Bind_Socket( Sock, Address ); - - accept Ready; -- server IS ready, when here - -- receive on socket - begin - Receive_Socket( Sock, Data, Last, From ); - Put_Line("last: " & Last'Img); - Put_Line("from: " & Image(From.Addr)); - Put_Line("data is:"); - for I in Data'First .. Last loop - FromNetworkFormat(Data(I..I), N); - Put_Line(N'Image); - end loop; - exception - when Socket_Error => - Put_Line("Socket error! (timeout?)"); - end; -- end of receive - - end Server; - - S: Server; - C: Client; -begin - S.Listen; - S.Ready; -- WAIT for server to be ready! - C.Send; -- client is started only after server! -end test_comms; -