raw
udp_genesis             1 ------------------------------------------------------------------------------
udp_genesis 2 ------------------------------------------------------------------------------
udp_genesis 3 -- This file is part of 'UDP', a datagram sockets library. --
udp_genesis 4 -- --
udp_genesis 5 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org ) --
udp_genesis 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
udp_genesis 7 -- --
udp_genesis 8 -- You do not have, nor can you ever acquire the right to use, copy or --
udp_genesis 9 -- distribute this software ; Should you use this software for any purpose, --
udp_genesis 10 -- or copy and distribute it to anyone or in any manner, you are breaking --
udp_genesis 11 -- the laws of whatever soi-disant jurisdiction, and you promise to --
udp_genesis 12 -- continue doing so for the indefinite future. In any case, please --
udp_genesis 13 -- always : read and understand any software ; verify any PGP signatures --
udp_genesis 14 -- that you use - for any purpose. --
udp_genesis 15 -- --
udp_genesis 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
udp_genesis 17 ------------------------------------------------------------------------------
udp_genesis 18 ------------------------------------------------------------------------------
udp_genesis 19
udp_genesis 20 with Interfaces, Interfaces.C; use Interfaces, Interfaces.C;
udp_genesis 21 with System; use System;
udp_genesis 22
udp_tester 23 generic
udp_tester 24 Payload_Size : in Positive; -- to allow for diff sizes at runtime
udp_genesis 25
udp_genesis 26 package UDP is
udp_genesis 27
udp_genesis 28 pragma Preelaborate;
udp_tester 29
udp_genesis 30 type Payload is array(1 .. Payload_Size) of Unsigned_8;
udp_genesis 31
udp_genesis 32 subtype IP_Address is Unsigned_32;
udp_genesis 33 subtype IP_Port is Unsigned_16;
udp_genesis 34
udp_genesis 35 -- Magic that puts emitter on 'any' local interface
udp_genesis 36 INADDR_ANY : constant Unsigned_32 := 0;
udp_tester 37
udp_tester 38 -- Bit-level compare of 2 payloads, returns number of different bits
udp_tester 39 function Bit_Compare(A: in Payload; B: in Payload) return Natural;
udp_genesis 40
udp_genesis 41 -- An local or remote address:port
udp_genesis 42 type Endpoint is
udp_genesis 43 record
udp_genesis 44 Address : IP_Address;
udp_genesis 45 Port : IP_Port;
udp_genesis 46 end record;
udp_genesis 47 -- NOTE that both elements are stored in ~local~ endianness.
udp_genesis 48
udp_genesis 49 -- Human Representation of any valid IP Address
udp_genesis 50 subtype IP_Address_Text is String(1 .. 15);
udp_genesis 51
udp_genesis 52 -- Opaque unix turd that stores a socket's state
udp_genesis 53 type Socket is private;
udp_genesis 54
udp_genesis 55 -- The public API:
udp_genesis 56
udp_genesis 57 -- Generate a human representation of a (local-endian) IP Address
udp_genesis 58 function IP_To_String(IP : in IP_Address) return IP_Address_Text;
udp_genesis 59
udp_genesis 60 -- Generate a (local-endian) IP Address from given human representation
udp_genesis 61 function IP_From_String(IP_Text : in String) return IP_Address;
udp_genesis 62
udp_genesis 63 -- Open a UDP socket, with the given local endpoint for both TX and RX
udp_genesis 64 procedure Open_Socket(S : out Socket;
udp_genesis 65 Local_Endpoint : in Endpoint);
udp_genesis 66
udp_errata_asciil... 67 -- Permanently close the given open socket
udp_genesis 68 procedure Close_Socket(S : in out Socket);
udp_genesis 69
udp_genesis 70 -- Transmit the Payload, via Socket, to given Destination
udp_genesis 71 procedure Transmit(S : in out Socket;
udp_genesis 72 Destination : in Endpoint;
udp_genesis 73 Payload_Buf : in Payload);
udp_genesis 74
udp_genesis 75 -- Wait (potentially forever!) for a Payload, via Socket; save its Origin,
udp_genesis 76 -- and whether the received Payload was valid (i.e. expected length):
udp_tester 77 -- replaced Valid with actual received length for testing purpose
udp_genesis 78 procedure Receive(S : in out Socket;
udp_genesis 79 Origin : out Endpoint;
udp_genesis 80 Payload_Buf : out Payload;
udp_tester 81 Recv_Len : out Unsigned_32);
udp_genesis 82
udp_genesis 83 -- Eggogology:
udp_genesis 84 UDP_Invalid_Text_IP : exception;
udp_genesis 85 UDP_Failed_Open : exception;
udp_genesis 86 UDP_Failed_SetOpt : exception;
udp_genesis 87 UDP_Failed_Bind : exception;
udp_genesis 88 UDP_Failed_Transmit : exception;
udp_errata_asciil... 89 UDP_Truncated_Send : exception;
udp_genesis 90 UDP_Failed_Receive : exception;
udp_genesis 91
udp_genesis 92 private
udp_genesis 93
udp_genesis 94 -- 'nicht fuer gefingerpoken und mittengrabben!'
udp_genesis 95
udp_genesis 96 -- This record's elements are not accessed from ada:
udp_genesis 97 type sockaddr_in is record
udp_genesis 98 family : Unsigned_16;
udp_genesis 99 port : Unsigned_16;
udp_genesis 100 sin_addr : Unsigned_32;
udp_genesis 101 padding : Unsigned_64;
udp_genesis 102 end record;
udp_genesis 103 pragma Convention(C, sockaddr_in);
udp_genesis 104
udp_genesis 105 -- Here we also don't care about the elements, only total mass:
udp_genesis 106 type Socket is
udp_genesis 107 record
udp_genesis 108 SA : sockaddr_in;
udp_genesis 109 FD : Interfaces.C.int;
udp_genesis 110 end record;
udp_genesis 111 pragma Convention(C, Socket);
udp_genesis 112
udp_genesis 113 -- Everything below -- imports from unix_udp.c:
udp_genesis 114
udp_genesis 115 procedure Unix_UDP_IP_To_String
udp_genesis 116 (IP : Unsigned_32;
udp_genesis 117 Output_Buffer : System.Address;
udp_genesis 118 Output_Buffer_Size : Unsigned_32);
udp_genesis 119 pragma Import(C, Unix_UDP_IP_To_String, "unix_udp_ip_to_string");
udp_genesis 120
udp_genesis 121 function Unix_UDP_String_To_IP
udp_genesis 122 (Input_Buffer : System.Address;
udp_genesis 123 IP : not null access Unsigned_32) return Interfaces.C.int;
udp_genesis 124 pragma Import(C, Unix_UDP_String_To_IP, "unix_udp_string_to_ip");
udp_genesis 125
udp_genesis 126 function Unix_UDP_Socket_Open
udp_genesis 127 (Socket : System.Address;
udp_genesis 128 Local_IP : Unsigned_32;
udp_genesis 129 Local_Port : Unsigned_16) return Interfaces.C.int;
udp_genesis 130 pragma Import(C, Unix_UDP_Socket_Open, "unix_udp_socket_open");
udp_genesis 131
udp_genesis 132 procedure Unix_UDP_Socket_Close
udp_genesis 133 (Socket : System.Address);
udp_genesis 134 pragma Import(C, Unix_UDP_Socket_Close, "unix_udp_socket_close");
udp_genesis 135
udp_genesis 136 function Unix_UDP_Socket_Transmit
udp_genesis 137 (Socket : System.Address;
udp_genesis 138 Remote_IP : Unsigned_32;
udp_genesis 139 Remote_Port : Unsigned_16;
udp_genesis 140 Payload_Buf : System.Address;
udp_genesis 141 Payload_Len : Unsigned_32) return Interfaces.C.int;
udp_genesis 142 pragma Import(C, Unix_UDP_Socket_Transmit, "unix_udp_socket_transmit");
udp_genesis 143
udp_genesis 144 function Unix_UDP_Socket_Receive
udp_genesis 145 (Socket : System.Address;
udp_genesis 146 Origin_IP : not null access Unsigned_32;
udp_genesis 147 Origin_Port : not null access Unsigned_16;
udp_genesis 148 Payload_Buf : System.Address;
udp_genesis 149 Payload_Len : Unsigned_32) return Interfaces.C.int;
udp_genesis 150 pragma Import(C, Unix_UDP_Socket_Receive, "unix_udp_socket_receive");
udp_genesis 151
udp_genesis 152 end UDP;