Skip to content

Commit

Permalink
Add Length_Delimited_String, As_Arp_Header, As_Ether_Addr, Address_To…
Browse files Browse the repository at this point in the history
…_Access_String, WIP modified As_Arp function
  • Loading branch information
masonticehurst committed Feb 20, 2024
1 parent de0cdce commit fa35c2b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
70 changes: 64 additions & 6 deletions eRINA_STM32F7/src/net/net-buffers.adb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
-- limitations under the License.
-----------------------------------------------------------------------
with Ada.Unchecked_Conversion;
package body Net.Buffers is
with System.Storage_Elements; use System.Storage_Elements;
with Net.Utils;

ETHER_POS : constant Uint16 := 0;
package body Net.Buffers is

type Offset_Table is array (Packet_Type) of Uint16;

Expand All @@ -29,8 +30,65 @@ package body Net.Buffers is
function As_Ethernet is new Ada.Unchecked_Conversion
(Source => System.Address, Target => Net.Headers.Ether_Header_Access);

function As_Arp is new Ada.Unchecked_Conversion
(Source => System.Address, Target => Net.Headers.Arp_Packet_Access);
-- An unchecked conversion no longer works here since we have length-delimited strings
-- i.e. we have no idea the length of the source and dest IPCP strings at compile time

-- function As_Arp is new Ada.Unchecked_Conversion
-- (Source => System.Address, Target => Net.Headers.Arp_Packet_Access);

function As_Arp_Header is new Ada.Unchecked_Conversion
(Source => System.Address, Target => Net.Headers.Arp_Header_Access);

type Ether_Addr_Access is access all Ether_Addr;

function As_Ether_Addr is new Ada.Unchecked_Conversion
(Source => System.Address, Target => Ether_Addr_Access);

function Address_To_Access_String (Address : System.Address; Length : Positive) return Net.Headers.Length_Delimited_String is
subtype Byte is System.Storage_Elements.Storage_Element;
type Byte_Array is array (Positive range <>) of Byte;

Temp : Byte_Array(1 .. Length);
for Temp'Address use Address;

Allocated_String : Net.Headers.Length_Delimited_String := new String'(1 .. Length => ' ');
begin
for I in 1 .. Length loop
Allocated_String(I) := Character'Val(Temp(I));
end loop;

return Allocated_String;
end Address_To_Access_String;

function As_Arp (Source : System.Address) return Net.Headers.Arp_Packet_Access is
-- Arp packet to be returned
Arp : constant Net.Headers.Arp_Packet_Access := new Net.Headers.Arp_Packet;
-- Ethernet header (this can be unchecked converted)
Ether : constant Net.Headers.Ether_Header_Access := As_Ethernet (Source);
-- Address starting where ARP packet begins (this can also be unchecked converted)
Ea_Hdr : constant Net.Headers.Arp_Header_Access := As_ARP_Header (Source + Storage_Offset(Offsets(ETHER_PACKET)));
-- Source MAC addr
Arp_Sha : constant Ether_Addr_Access := As_Ether_Addr (Source + Storage_Offset(Offsets(ARP_PACKET)));
begin
Arp.Ethernet := Ether.all;

-- I don't know why but the below doesn't work for me
-- Arp.Arp.Ea_Hdr := Ea_Hdr.all;

Arp.Arp.Ea_Hdr.Ar_Hdr := Ea_Hdr.Ar_Hdr;
Arp.Arp.Ea_Hdr.Ar_Pro := Ea_Hdr.Ar_Pro;
Arp.Arp.Ea_Hdr.Ar_Hln := Ea_Hdr.Ar_Hln;
Arp.Arp.Ea_Hdr.Ar_Pln := Ea_Hdr.Ar_Pln;
Arp.Arp.Ea_Hdr.Ar_Op := Ea_Hdr.Ar_Op;

Arp.Arp.Arp_Sha := Arp_Sha.all;
Arp.Arp.Arp_Spa := Address_To_Access_String (Source + Storage_Offset(Offsets(ARP_PACKET)), Positive(Arp.Arp.Ea_Hdr.Ar_Pro));

--Arp_Tha : Ether_Addr;
--Arp_Tpa : Length_Delimited_String;

return Arp;
end As_Arp;

function As_EFCP is new Ada.Unchecked_Conversion
(Source => System.Address, Target => Net.Headers.EFCP_Packet_Access);
Expand Down Expand Up @@ -316,15 +374,15 @@ package body Net.Buffers is
-- Get access to the ARP packet.
-- ------------------------------
function Arp (Buf : in Buffer_Type) return Net.Headers.Arp_Packet_Access is
Result : Net.Headers.Arp_Packet_Access;
begin
return As_Arp (Buf.Packet.Data (Buf.Packet.Data'First)'Address);
end Arp;

-- ------------------------------
-- Get access to the EFCP packet.
-- ------------------------------
function EFCP (Buf : in Buffer_Type) return Net.Headers.EFCP_Packet_Access
is
function EFCP (Buf : in Buffer_Type) return Net.Headers.EFCP_Packet_Access is
begin
return As_EFCP (Buf.Packet.Data (Buf.Packet.Data'First)'Address);
end EFCP;
Expand Down
11 changes: 7 additions & 4 deletions eRINA_STM32F7/src/net/net-headers.ads
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ package Net.Headers is
Ar_Op : Uint16;
end record;

type Arp_Header_Access is access Arp_Header;
type Length_Delimited_String is access String;

type Ether_Arp is record
Ea_Hdr : Arp_Header;
Arp_Sha : Ether_Addr;
Arp_Spa : String (1 .. 6);
Arp_Tha : Ether_Addr;
Arp_Tpa : String (1 .. 6);
Arp_Sha : Ether_Addr := (others => 0);
Arp_Spa : Length_Delimited_String;
Arp_Tha : Ether_Addr := (others => 0);
Arp_Tpa : Length_Delimited_String;
end record;
type Ether_Arp_Access is access all Ether_Arp;

Expand Down

0 comments on commit fa35c2b

Please sign in to comment.