------------------------------------------------------------------------------ ------------------------------------------------------------------------------ -- This file is part of 'UDP', a datagram sockets library. -- -- -- -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org ) -- -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html -- -- -- -- You do not have, nor can you ever acquire the right to use, copy or -- -- distribute this software ; Should you use this software for any purpose, -- -- or copy and distribute it to anyone or in any manner, you are breaking -- -- the laws of whatever soi-disant jurisdiction, and you promise to -- -- continue doing so for the indefinite future. In any case, please -- -- always : read and understand any software ; verify any PGP signatures -- -- that you use - for any purpose. -- -- -- -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . -- ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ with Interfaces, Interfaces.C; use Interfaces, Interfaces.C; with System; use System; package UDP is pragma Preelaborate; -- This is subject to debate: Payload_Size : constant Positive := 512; type Payload is array(1 .. Payload_Size) of Unsigned_8; -- type IP_Address is array(1 .. 4) of Unsigned_8; subtype IP_Address is Unsigned_32; subtype IP_Port is Unsigned_16; -- Magic that puts emitter on 'any' local interface INADDR_ANY : constant Unsigned_32 := 0; -- An local or remote address:port type Endpoint is record Address : IP_Address; Port : IP_Port; end record; -- NOTE that both elements are stored in ~local~ endianness. -- Human Representation of any valid IP Address subtype IP_Address_Text is String(1 .. 15); -- Opaque unix turd that stores a socket's state type Socket is private; -- The public API: -- Generate a human representation of a (local-endian) IP Address function IP_To_String(IP : in IP_Address) return IP_Address_Text; -- Generate a (local-endian) IP Address from given human representation function IP_From_String(IP_Text : in String) return IP_Address; -- Open a UDP socket, with the given local endpoint for both TX and RX procedure Open_Socket(S : out Socket; Local_Endpoint : in Endpoint); -- Permanently close the given open given socket procedure Close_Socket(S : in out Socket); -- Transmit the Payload, via Socket, to given Destination procedure Transmit(S : in out Socket; Destination : in Endpoint; Payload_Buf : in Payload); -- Wait (potentially forever!) for a Payload, via Socket; save its Origin, -- and whether the received Payload was valid (i.e. expected length): procedure Receive(S : in out Socket; Origin : out Endpoint; Payload_Buf : out Payload; Valid : out Boolean); -- Eggogology: UDP_Invalid_Text_IP : exception; UDP_Failed_Open : exception; UDP_Failed_SetOpt : exception; UDP_Failed_Bind : exception; UDP_Failed_Transmit : exception; UDP_Failed_Receive : exception; private -- 'nicht fuer gefingerpoken und mittengrabben!' -- This record's elements are not accessed from ada: type sockaddr_in is record family : Unsigned_16; port : Unsigned_16; sin_addr : Unsigned_32; padding : Unsigned_64; end record; pragma Convention(C, sockaddr_in); -- Here we also don't care about the elements, only total mass: type Socket is record SA : sockaddr_in; FD : Interfaces.C.int; end record; pragma Convention(C, Socket); -- Everything below -- imports from unix_udp.c: procedure Unix_UDP_IP_To_String (IP : Unsigned_32; Output_Buffer : System.Address; Output_Buffer_Size : Unsigned_32); pragma Import(C, Unix_UDP_IP_To_String, "unix_udp_ip_to_string"); function Unix_UDP_String_To_IP (Input_Buffer : System.Address; IP : not null access Unsigned_32) return Interfaces.C.int; pragma Import(C, Unix_UDP_String_To_IP, "unix_udp_string_to_ip"); function Unix_UDP_Socket_Open (Socket : System.Address; Local_IP : Unsigned_32; Local_Port : Unsigned_16) return Interfaces.C.int; pragma Import(C, Unix_UDP_Socket_Open, "unix_udp_socket_open"); procedure Unix_UDP_Socket_Close (Socket : System.Address); pragma Import(C, Unix_UDP_Socket_Close, "unix_udp_socket_close"); function Unix_UDP_Socket_Transmit (Socket : System.Address; Remote_IP : Unsigned_32; Remote_Port : Unsigned_16; Payload_Buf : System.Address; Payload_Len : Unsigned_32) return Interfaces.C.int; pragma Import(C, Unix_UDP_Socket_Transmit, "unix_udp_socket_transmit"); function Unix_UDP_Socket_Receive (Socket : System.Address; Origin_IP : not null access Unsigned_32; Origin_Port : not null access Unsigned_16; Payload_Buf : System.Address; Payload_Len : Unsigned_32) return Interfaces.C.int; pragma Import(C, Unix_UDP_Socket_Receive, "unix_udp_socket_receive"); end UDP;