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_genesis 23
udp_genesis 24 package UDP is
udp_genesis 25
udp_genesis 26 pragma Preelaborate;
udp_genesis 27
udp_genesis 28 -- This is subject to debate:
udp_genesis 29 Payload_Size : constant Positive := 512;
udp_genesis 30
udp_genesis 31 type Payload is array(1 .. Payload_Size) of Unsigned_8;
udp_genesis 32
udp_genesis 33 -- type IP_Address is array(1 .. 4) of Unsigned_8;
udp_genesis 34 subtype IP_Address is Unsigned_32;
udp_genesis 35 subtype IP_Port is Unsigned_16;
udp_genesis 36
udp_genesis 37 -- Magic that puts emitter on 'any' local interface
udp_genesis 38 INADDR_ANY : constant Unsigned_32 := 0;
udp_genesis 39
udp_genesis 40 -- An local or remote address:port
udp_genesis 41 type Endpoint is
udp_genesis 42 record
udp_genesis 43 Address : IP_Address;
udp_genesis 44 Port : IP_Port;
udp_genesis 45 end record;
udp_genesis 46 -- NOTE that both elements are stored in ~local~ endianness.
udp_genesis 47
udp_genesis 48 -- Human Representation of any valid IP Address
udp_genesis 49 subtype IP_Address_Text is String(1 .. 15);
udp_genesis 50
udp_genesis 51 -- Opaque unix turd that stores a socket's state
udp_genesis 52 type Socket is private;
udp_genesis 53
udp_genesis 54 -- The public API:
udp_genesis 55
udp_genesis 56 -- Generate a human representation of a (local-endian) IP Address
udp_genesis 57 function IP_To_String(IP : in IP_Address) return IP_Address_Text;
udp_genesis 58
udp_genesis 59 -- Generate a (local-endian) IP Address from given human representation
udp_genesis 60 function IP_From_String(IP_Text : in String) return IP_Address;
udp_genesis 61
udp_genesis 62 -- Open a UDP socket, with the given local endpoint for both TX and RX
udp_genesis 63 procedure Open_Socket(S : out Socket;
udp_genesis 64 Local_Endpoint : in Endpoint);
udp_genesis 65
udp_genesis 66 -- Permanently close the given open given socket
udp_genesis 67 procedure Close_Socket(S : in out Socket);
udp_genesis 68
udp_genesis 69 -- Transmit the Payload, via Socket, to given Destination
udp_genesis 70 procedure Transmit(S : in out Socket;
udp_genesis 71 Destination : in Endpoint;
udp_genesis 72 Payload_Buf : in Payload);
udp_genesis 73
udp_genesis 74 -- Wait (potentially forever!) for a Payload, via Socket; save its Origin,
udp_genesis 75 -- and whether the received Payload was valid (i.e. expected length):
udp_genesis 76 procedure Receive(S : in out Socket;
udp_genesis 77 Origin : out Endpoint;
udp_genesis 78 Payload_Buf : out Payload;
udp_genesis 79 Valid : out Boolean);
udp_genesis 80
udp_genesis 81 -- Eggogology:
udp_genesis 82 UDP_Invalid_Text_IP : exception;
udp_genesis 83 UDP_Failed_Open : exception;
udp_genesis 84 UDP_Failed_SetOpt : exception;
udp_genesis 85 UDP_Failed_Bind : exception;
udp_genesis 86 UDP_Failed_Transmit : exception;
udp_genesis 87 UDP_Failed_Receive : exception;
udp_genesis 88
udp_genesis 89 private
udp_genesis 90
udp_genesis 91 -- 'nicht fuer gefingerpoken und mittengrabben!'
udp_genesis 92
udp_genesis 93 -- This record's elements are not accessed from ada:
udp_genesis 94 type sockaddr_in is record
udp_genesis 95 family : Unsigned_16;
udp_genesis 96 port : Unsigned_16;
udp_genesis 97 sin_addr : Unsigned_32;
udp_genesis 98 padding : Unsigned_64;
udp_genesis 99 end record;
udp_genesis 100 pragma Convention(C, sockaddr_in);
udp_genesis 101
udp_genesis 102 -- Here we also don't care about the elements, only total mass:
udp_genesis 103 type Socket is
udp_genesis 104 record
udp_genesis 105 SA : sockaddr_in;
udp_genesis 106 FD : Interfaces.C.int;
udp_genesis 107 end record;
udp_genesis 108 pragma Convention(C, Socket);
udp_genesis 109
udp_genesis 110 -- Everything below -- imports from unix_udp.c:
udp_genesis 111
udp_genesis 112 procedure Unix_UDP_IP_To_String
udp_genesis 113 (IP : Unsigned_32;
udp_genesis 114 Output_Buffer : System.Address;
udp_genesis 115 Output_Buffer_Size : Unsigned_32);
udp_genesis 116 pragma Import(C, Unix_UDP_IP_To_String, "unix_udp_ip_to_string");
udp_genesis 117
udp_genesis 118 function Unix_UDP_String_To_IP
udp_genesis 119 (Input_Buffer : System.Address;
udp_genesis 120 IP : not null access Unsigned_32) return Interfaces.C.int;
udp_genesis 121 pragma Import(C, Unix_UDP_String_To_IP, "unix_udp_string_to_ip");
udp_genesis 122
udp_genesis 123 function Unix_UDP_Socket_Open
udp_genesis 124 (Socket : System.Address;
udp_genesis 125 Local_IP : Unsigned_32;
udp_genesis 126 Local_Port : Unsigned_16) return Interfaces.C.int;
udp_genesis 127 pragma Import(C, Unix_UDP_Socket_Open, "unix_udp_socket_open");
udp_genesis 128
udp_genesis 129 procedure Unix_UDP_Socket_Close
udp_genesis 130 (Socket : System.Address);
udp_genesis 131 pragma Import(C, Unix_UDP_Socket_Close, "unix_udp_socket_close");
udp_genesis 132
udp_genesis 133 function Unix_UDP_Socket_Transmit
udp_genesis 134 (Socket : System.Address;
udp_genesis 135 Remote_IP : Unsigned_32;
udp_genesis 136 Remote_Port : Unsigned_16;
udp_genesis 137 Payload_Buf : System.Address;
udp_genesis 138 Payload_Len : Unsigned_32) return Interfaces.C.int;
udp_genesis 139 pragma Import(C, Unix_UDP_Socket_Transmit, "unix_udp_socket_transmit");
udp_genesis 140
udp_genesis 141 function Unix_UDP_Socket_Receive
udp_genesis 142 (Socket : System.Address;
udp_genesis 143 Origin_IP : not null access Unsigned_32;
udp_genesis 144 Origin_Port : not null access Unsigned_16;
udp_genesis 145 Payload_Buf : System.Address;
udp_genesis 146 Payload_Len : Unsigned_32) return Interfaces.C.int;
udp_genesis 147 pragma Import(C, Unix_UDP_Socket_Receive, "unix_udp_socket_receive");
udp_genesis 148
udp_genesis 149 end UDP;