OSDN Git Service

* 41intnam.ads, 42intnam.ads, 4aintnam.ads, 4cintnam.ads,
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socket.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                         G N A T . S O C K E T S                          --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                            $Revision$
10 --                                                                          --
11 --              Copyright (C) 2001-2002 Ada Core Technologies, Inc.         --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This package provides an interface to the sockets communication facility
36 --  provided on many operating systems. Currently this is implemented on all
37 --  native GNAT ports except for VMS. It is not yet implemented on the Lynx
38 --  cross-ports.
39
40 --  Another restriction is that there is no multicast support under Windows
41 --  or under any system on which the multicast support is not available or
42 --  installed.
43
44 with Ada.Exceptions;
45 with Ada.Streams;
46
47 package GNAT.Sockets is
48
49    --  Sockets are designed to provide a consistent communication facility
50    --  between applications. This package provides an Ada-like intrerface
51    --  similar to that proposed as part of the BSD socket layer. This is a
52    --  system independent thick binding.
53
54    --  Here is a typical example of what you can do:
55
56    --  with GNAT.Sockets; use GNAT.Sockets;
57    --
58    --  with Ada.Text_IO;
59    --  with Ada.Exceptions; use Ada.Exceptions;
60    --
61    --  procedure PingPong is
62    --
63    --     Group : constant String := "239.255.128.128";
64    --     --  Multicast groupe: administratively scoped IP address
65    --
66    --     task Pong is
67    --        entry Start;
68    --        entry Stop;
69    --     end Pong;
70    --
71    --     task body Pong is
72    --        Address  : Sock_Addr_Type;
73    --        Server   : Socket_Type;
74    --        Socket   : Socket_Type;
75    --        Channel  : Stream_Access;
76    --
77    --     begin
78    --        accept Start;
79    --
80    --        --  Get an Internet address of a host (here the local host name).
81    --        --  Note that a host can have several addresses. Here we get
82    --        --  the first one which is supposed to be the official one.
83    --
84    --        Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
85    --
86    --        --  Get a socket address that is an Internet address and a port
87    --
88    --        Address.Port := 5432;
89    --
90    --        --  The first step is to create a socket. Once created, this
91    --        --  socket must be associated to with an address. Usually only
92    --        --  a server (Pong here) needs to bind an address explicitly.
93    --        --  Most of the time clients can skip this step because the
94    --        --  socket routines will bind an arbitrary address to an unbound
95    --        --  socket.
96    --
97    --        Create_Socket (Server);
98    --
99    --        --  Allow reuse of local addresses.
100    --
101    --        Set_Socket_Option
102    --          (Server,
103    --           Socket_Level,
104    --           (Reuse_Address, True));
105    --
106    --        Bind_Socket (Server, Address);
107    --
108    --        --  A server marks a socket as willing to receive connect events.
109    --
110    --        Listen_Socket (Server);
111    --
112    --        --  Once a server calls Listen_Socket, incoming connects events
113    --        --  can be accepted. The returned Socket is a new socket that
114    --        --  represents the server side of the connection. Server remains
115    --        --  available to receive further connections.
116    --
117    --        Accept_Socket (Server, Socket, Address);
118    --
119    --        --  Return a stream associated to the connected socket.
120    --
121    --        Channel := Stream (Socket);
122    --
123    --        --  Force Pong to block
124    --
125    --        delay 0.2;
126    --
127    --        --  Receive and print message from client Ping.
128    --
129    --        declare
130    --           Message : String := String'Input (Channel);
131    --
132    --        begin
133    --           Ada.Text_IO.Put_Line (Message);
134    --
135    --           --  Send same message to server Pong.
136    --
137    --           String'Output (Channel, Message);
138    --        end;
139    --
140    --        Close_Socket (Server);
141    --        Close_Socket (Socket);
142    --
143    --        --  Part of the multicast example
144    --
145    --        --  Create a datagram socket to send connectionless, unreliable
146    --        --  messages of a fixed maximum length.
147    --
148    --        Create_Socket (Socket, Family_Inet, Socket_Datagram);
149    --
150    --        --  Allow reuse of local addresses.
151    --
152    --        Set_Socket_Option
153    --          (Socket,
154    --           Socket_Level,
155    --           (Reuse_Address, True));
156    --
157    --        --  Join a multicast group.
158    --
159    --        Set_Socket_Option
160    --          (Socket,
161    --           IP_Protocol_For_IP_Level,
162    --           (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
163    --
164    --        --  Controls the live time of the datagram to avoid it being
165    --        --  looped forever due to routing errors. Routers decrement
166    --        --  the TTL of every datagram as it traverses from one network
167    --        --  to another and when its value reaches 0 the packet is
168    --        --  dropped. Default is 1.
169    --
170    --        Set_Socket_Option
171    --          (Socket,
172    --           IP_Protocol_For_IP_Level,
173    --           (Multicast_TTL, 1));
174    --
175    --        --  Want the data you send to be looped back to your host.
176    --
177    --        Set_Socket_Option
178    --          (Socket,
179    --           IP_Protocol_For_IP_Level,
180    --           (Multicast_Loop, True));
181    --
182    --        --  If this socket is intended to receive messages, bind it to a
183    --        --  given socket address.
184    --
185    --        Address.Addr := Any_Inet_Addr;
186    --        Address.Port := 55505;
187    --
188    --        Bind_Socket (Socket, Address);
189    --
190    --        --  If this socket is intended to send messages, provide the
191    --        --  receiver socket address.
192    --
193    --        Address.Addr := Inet_Addr (Group);
194    --        Address.Port := 55506;
195    --
196    --        Channel := Stream (Socket, Address);
197    --
198    --        --  Receive and print message from client Ping.
199    --
200    --        declare
201    --           Message : String := String'Input (Channel);
202    --
203    --        begin
204    --
205    --           --  Get the address of the sender.
206    --
207    --           Address := Get_Address (Channel);
208    --           Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
209    --
210    --           --  Send same message to server Pong.
211    --
212    --           String'Output (Channel, Message);
213    --        end;
214    --
215    --        Close_Socket (Socket);
216    --
217    --        accept Stop;
218    --
219    --     exception when E : others =>
220    --        Ada.Text_IO.Put_Line
221    --          (Exception_Name (E) & ": " & Exception_Message (E));
222    --     end Pong;
223    --
224    --     task Ping is
225    --        entry Start;
226    --        entry Stop;
227    --     end Ping;
228    --
229    --     task body Ping is
230    --        Address  : Sock_Addr_Type;
231    --        Socket   : Socket_Type;
232    --        Channel  : Stream_Access;
233    --
234    --     begin
235    --        accept Start;
236    --
237    --        --  See comments in Ping section for the first steps.
238    --
239    --        Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
240    --        Address.Port := 5432;
241    --        Create_Socket (Socket);
242    --
243    --        Set_Socket_Option
244    --          (Socket,
245    --           Socket_Level,
246    --           (Reuse_Address, True));
247    --
248    --        --  Force Pong to block
249    --
250    --        delay 0.2;
251    --
252    --        --  If the client's socket is not bound, Connect_Socket will
253    --        --  bind to an unused address. The client uses Connect_Socket to
254    --        --  create a logical connection between the client's socket and
255    --        --  a server's socket returned by Accept_Socket.
256    --
257    --        Connect_Socket (Socket, Address);
258    --
259    --        Channel := Stream (Socket);
260    --
261    --        --  Send message to server Pong.
262    --
263    --        String'Output (Channel, "Hello world");
264    --
265    --        --  Force Ping to block
266    --
267    --        delay 0.2;
268    --
269    --        --  Receive and print message from server Pong.
270    --
271    --        Ada.Text_IO.Put_Line (String'Input (Channel));
272    --        Close_Socket (Socket);
273    --
274    --        --  Part of multicast example. Code similar to Pong's one.
275    --
276    --        Create_Socket (Socket, Family_Inet, Socket_Datagram);
277    --
278    --        Set_Socket_Option
279    --          (Socket,
280    --           Socket_Level,
281    --           (Reuse_Address, True));
282    --
283    --        Set_Socket_Option
284    --          (Socket,
285    --           IP_Protocol_For_IP_Level,
286    --           (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
287    --
288    --        Set_Socket_Option
289    --          (Socket,
290    --           IP_Protocol_For_IP_Level,
291    --           (Multicast_TTL, 1));
292    --
293    --        Set_Socket_Option
294    --          (Socket,
295    --           IP_Protocol_For_IP_Level,
296    --           (Multicast_Loop, True));
297    --
298    --        Address.Addr := Any_Inet_Addr;
299    --        Address.Port := 55506;
300    --
301    --        Bind_Socket (Socket, Address);
302    --
303    --        Address.Addr := Inet_Addr (Group);
304    --        Address.Port := 55505;
305    --
306    --        Channel := Stream (Socket, Address);
307    --
308    --        --  Send message to server Pong.
309    --
310    --        String'Output (Channel, "Hello world");
311    --
312    --        --  Receive and print message from server Pong.
313    --
314    --        declare
315    --           Message : String := String'Input (Channel);
316    --
317    --        begin
318    --           Address := Get_Address (Channel);
319    --           Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
320    --        end;
321    --
322    --        Close_Socket (Socket);
323    --
324    --        accept Stop;
325    --
326    --     exception when E : others =>
327    --        Ada.Text_IO.Put_Line
328    --          (Exception_Name (E) & ": " & Exception_Message (E));
329    --     end Ping;
330    --
331    --  begin
332    --     --  Indicate whether the thread library provides process
333    --     --  blocking IO. Basically, if you are not using FSU threads
334    --     --  the default is ok.
335    --
336    --     Initialize (Process_Blocking_IO => False);
337    --     Ping.Start;
338    --     Pong.Start;
339    --     Ping.Stop;
340    --     Pong.Stop;
341    --     Finalize;
342    --  end PingPong;
343
344    procedure Initialize (Process_Blocking_IO : Boolean := False);
345    --  Initialize must be called before using any socket routines. If
346    --  the thread library provides process blocking IO - basically
347    --  with FSU threads - GNAT.Sockets should be initialized with a
348    --  value of True to simulate thread blocking IO. Further calls to
349    --  Initialize will be ignored.
350
351    procedure Finalize;
352    --  After Finalize is called it is not possible to use any routines
353    --  exported in by this package. This procedure is idempotent.
354
355    type Socket_Type is private;
356    --  Sockets are used to implement a reliable bi-directional
357    --  point-to-point, stream-based connections between
358    --  hosts. No_Socket provides a special value to denote
359    --  uninitialized sockets.
360
361    No_Socket : constant Socket_Type;
362
363    Socket_Error : exception;
364    --  There is only one exception in this package to deal with an
365    --  error during a socket routine. Once raised, its message
366    --  contains a string describing the error code.
367
368    function Image (Socket : Socket_Type) return String;
369    --  Return a printable string for Socket
370
371    function To_C (Socket : Socket_Type) return Integer;
372    --  Return a file descriptor to be used by external subprograms
373    --  especially the C functions that are not yet interfaced in this
374    --  package.
375
376    type Family_Type is (Family_Inet, Family_Inet6);
377    --  Address family (or protocol family) identifies the
378    --  communication domain and groups protocols with similar address
379    --  formats. IPv6 will soon be supported.
380
381    type Mode_Type is (Socket_Stream, Socket_Datagram);
382    --  Stream sockets provide connection-oriented byte
383    --  streams. Datagram sockets support unreliable connectionless
384    --  message based communication.
385
386    type Shutmode_Type is (Shut_Read, Shut_Write, Shut_Read_Write);
387    --  When a process closes a socket, the policy is to retain any
388    --  data queued until either a delivery or a timeout expiration (in
389    --  this case, the data are discarded). A finer control is
390    --  available through shutdown. With Shut_Read, no more data can be
391    --  received from the socket. With_Write, no more data can be
392    --  transmitted. Neither transmission nor reception can be
393    --  performed with Shut_Read_Write.
394
395    type Port_Type is new Natural;
396    --  Classical port definition. No_Port provides a special value to
397    --  denote uninitialized port. Any_Port provides a special value
398    --  enabling all ports.
399
400    Any_Port : constant Port_Type;
401    No_Port  : constant Port_Type;
402
403    type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
404    --  An Internet address depends on an address family (IPv4 contains
405    --  4 octets and Ipv6 contains 16 octets). Any_Inet_Address is a
406    --  special value treated like a wildcard enabling all addresses.
407    --  No_Inet_Addr provides a special value to denote uninitialized
408    --  inet addresses.
409
410    Any_Inet_Addr : constant Inet_Addr_Type;
411    No_Inet_Addr  : constant Inet_Addr_Type;
412
413    type Sock_Addr_Type (Family : Family_Type := Family_Inet) is record
414       Addr : Inet_Addr_Type (Family);
415       Port : Port_Type;
416    end record;
417    --  Socket addresses fully define a socket connection with a
418    --  protocol family, an Internet address and a port. No_Sock_Addr
419    --  provides a special value for uninitialized socket addresses.
420
421    No_Sock_Addr : constant Sock_Addr_Type;
422
423    function Image (Value : Inet_Addr_Type) return String;
424    --  Return an image of an Internet address. IPv4 notation consists
425    --  in 4 octets in decimal format separated by dots. IPv6 notation
426    --  consists in 16 octets in hexadecimal format separated by
427    --  colons (and possibly dots).
428
429    function Image (Value : Sock_Addr_Type) return String;
430    --  Return inet address image and port image separated by a colon.
431
432    function Inet_Addr (Image : String) return Inet_Addr_Type;
433    --  Convert address image from numbers-and-dots notation into an
434    --  inet address.
435
436    --  Host entries provide a complete information on a given host:
437    --  the official name, an array of alternative names or aliases and
438    --  array of network addresses.
439
440    type Host_Entry_Type
441      (Aliases_Length, Addresses_Length : Natural) is private;
442
443    function Official_Name (E : Host_Entry_Type) return String;
444    --  Return official name in host entry
445
446    function Aliases_Length (E : Host_Entry_Type) return Natural;
447    --  Return number of aliases in host entry
448
449    function Addresses_Length (E : Host_Entry_Type) return Natural;
450    --  Return number of addresses in host entry
451
452    function Aliases
453      (E    : Host_Entry_Type;
454       N    : Positive := 1)
455       return String;
456    --  Return N'th aliases in host entry. The first index is 1.
457
458    function Addresses
459      (E    : Host_Entry_Type;
460       N    : Positive := 1)
461       return Inet_Addr_Type;
462    --  Return N'th addresses in host entry. The first index is 1.
463
464    Host_Error : exception;
465    --  Exception raised by the two following procedures. Once raised,
466    --  its message contains a string describing the error code. This
467    --  exception is raised when an host entry can not be retrieved.
468
469    function Get_Host_By_Address
470      (Address : Inet_Addr_Type;
471       Family  : Family_Type := Family_Inet)
472       return    Host_Entry_Type;
473    --  Return host entry structure for the given inet address
474
475    function Get_Host_By_Name
476      (Name : String)
477       return Host_Entry_Type;
478    --  Return host entry structure for the given host name
479
480    function Host_Name return String;
481    --  Return the name of the current host
482
483    --  Errors are described by an enumeration type. There is only one
484    --  exception Socket_Error in this package to deal with an error
485    --  during a socket routine. Once raised, its message contains the
486    --  error code between brackets and a string describing the error code.
487
488    --  The name of the enumeration constant documents the error condition.
489
490    type Error_Type is
491      (Permission_Denied,
492       Address_Already_In_Use,
493       Cannot_Assign_Requested_Address,
494       Address_Family_Not_Supported_By_Protocol,
495       Operation_Already_In_Progress,
496       Bad_File_Descriptor,
497       Connection_Refused,
498       Bad_Address,
499       Operation_Now_In_Progress,
500       Interrupted_System_Call,
501       Invalid_Argument,
502       Input_Output_Error,
503       Transport_Endpoint_Already_Connected,
504       Message_Too_Long,
505       Network_Is_Unreachable,
506       No_Buffer_Space_Available,
507       Protocol_Not_Available,
508       Transport_Endpoint_Not_Connected,
509       Operation_Not_Supported,
510       Protocol_Not_Supported,
511       Socket_Type_Not_Supported,
512       Connection_Timed_Out,
513       Resource_Temporarily_Unavailable,
514       Unknown_Host,
515       Host_Name_Lookup_Failure,
516       No_Address_Associated_With_Name,
517       Unknown_Server_Error,
518       Cannot_Resolve_Error);
519
520    --  Get_Socket_Options and Set_Socket_Options manipulate options
521    --  associated with a socket. Options may exist at multiple
522    --  protocol levels in the communication stack. Socket_Level is the
523    --  uppermost socket level.
524
525    type Level_Type is (
526      Socket_Level,
527      IP_Protocol_For_IP_Level,
528      IP_Protocol_For_UDP_Level,
529      IP_Protocol_For_TCP_Level);
530
531    --  There are several options available to manipulate sockets. Each
532    --  option has a name and several values available. Most of the
533    --  time, the value is a boolean to enable or disable this option.
534
535    type Option_Name is (
536      Keep_Alive,      -- Enable sending of keep-alive messages
537      Reuse_Address,   -- Allow bind to reuse local address
538      Broadcast,       -- Enable datagram sockets to recv/send broadcast packets
539      Send_Buffer,     -- Set/get the maximum socket send buffer in bytes
540      Receive_Buffer,  -- Set/get the maximum socket recv buffer in bytes
541      Linger,          -- Shutdown wait for msg to be sent or timeout occur
542      Error,           -- Get and clear the pending socket error
543      No_Delay,        -- Do not delay send to coalesce packets (TCP_NODELAY)
544      Add_Membership,  -- Join a multicast group
545      Drop_Membership, -- Leave a multicast group
546      Multicast_TTL,   -- Indicates the time-to-live of sent multicast packets
547      Multicast_Loop); -- Sent multicast packets are looped to the local socket
548
549    type Option_Type (Name : Option_Name := Keep_Alive) is record
550       case Name is
551          when Keep_Alive      |
552               Reuse_Address   |
553               Broadcast       |
554               Linger          |
555               No_Delay        |
556               Multicast_Loop  =>
557             Enabled : Boolean;
558
559             case Name is
560                when Linger    =>
561                   Seconds : Natural;
562                when others    =>
563                   null;
564             end case;
565
566          when Send_Buffer     |
567               Receive_Buffer  =>
568             Size : Natural;
569
570          when Error           =>
571             Error : Error_Type;
572
573          when Add_Membership  |
574               Drop_Membership =>
575             Multiaddr : Inet_Addr_Type;
576             Interface : Inet_Addr_Type;
577
578          when Multicast_TTL   =>
579             Time_To_Live : Natural;
580
581       end case;
582    end record;
583
584    --  There are several controls available to manipulate
585    --  sockets. Each option has a name and several values available.
586    --  These controls differ from the socket options in that they are
587    --  not specific to sockets but are available for any device.
588
589    type Request_Name is (
590       Non_Blocking_IO,  --  Cause a caller not to wait on blocking operations.
591       N_Bytes_To_Read); --  Return the number of bytes available to read
592
593    type Request_Type (Name : Request_Name := Non_Blocking_IO) is record
594       case Name is
595          when Non_Blocking_IO =>
596             Enabled : Boolean;
597
598          when N_Bytes_To_Read =>
599             Size : Natural;
600
601       end case;
602    end record;
603
604    procedure Create_Socket
605      (Socket : out Socket_Type;
606       Family : Family_Type := Family_Inet;
607       Mode   : Mode_Type   := Socket_Stream);
608    --  Create an endpoint for communication. Raise Socket_Error on error.
609
610    procedure Accept_Socket
611      (Server  : Socket_Type;
612       Socket  : out Socket_Type;
613       Address : out Sock_Addr_Type);
614    --  Extract the first connection request on the queue of pending
615    --  connections, creates a new connected socket with mostly the
616    --  same properties as Server, and allocates a new socket. The
617    --  returned Address is filled in with the address of the
618    --  connection. Raise Socket_Error on error.
619
620    procedure Bind_Socket
621      (Socket  : Socket_Type;
622       Address : Sock_Addr_Type);
623    --  Once a socket is created, assign a local address to it. Raise
624    --  Socket_Error on error.
625
626    procedure Close_Socket (Socket : Socket_Type);
627    --  Close a socket and more specifically a non-connected socket.
628
629    procedure Connect_Socket
630      (Socket : Socket_Type;
631       Server : in out Sock_Addr_Type);
632    --  Make a connection to another socket which has the address of
633    --  Server. Raise Socket_Error on error.
634
635    procedure Control_Socket
636      (Socket  : Socket_Type;
637       Request : in out Request_Type);
638    --  Obtain or set parameter values that control the socket. This
639    --  control differs from the socket options in that they are not
640    --  specific to sockets but are avaiable for any device.
641
642    function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
643    --  Return the peer or remote socket address of a socket. Raise
644    --  Socket_Error on error.
645
646    function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
647    --  Return the local or current socket address of a socket. Raise
648    --  Socket_Error on error.
649
650    function Get_Socket_Option
651      (Socket : Socket_Type;
652       Level  : Level_Type := Socket_Level;
653       Name   : Option_Name)
654       return   Option_Type;
655    --  Get the options associated with a socket. Raise Socket_Error on
656    --  error.
657
658    procedure Listen_Socket
659      (Socket : Socket_Type;
660       Length : Positive := 15);
661    --  To accept connections, a socket is first created with
662    --  Create_Socket, a willingness to accept incoming connections and
663    --  a queue Length for incoming connections are specified. Raise
664    --  Socket_Error on error.
665
666    procedure Receive_Socket
667      (Socket : Socket_Type;
668       Item   : out Ada.Streams.Stream_Element_Array;
669       Last   : out Ada.Streams.Stream_Element_Offset);
670    --  Receive message from Socket. Last is the index value such that
671    --  Item (Last) is the last character assigned. Note that Last is
672    --  set to Item'First - 1 when the socket has been closed by
673    --  peer. This is not an error and no exception is raised. Raise
674    --  Socket_Error on error.
675
676    procedure Receive_Socket
677      (Socket : Socket_Type;
678       Item   : out Ada.Streams.Stream_Element_Array;
679       Last   : out Ada.Streams.Stream_Element_Offset;
680       From   : out Sock_Addr_Type);
681    --  Receive message from Socket. If Socket is not
682    --  connection-oriented, the source address From of the message is
683    --  filled in. Last is the index value such that Item (Last) is the
684    --  last character assigned. Raise Socket_Error on error.
685
686    function Resolve_Exception
687      (Occurrence : Ada.Exceptions.Exception_Occurrence)
688      return        Error_Type;
689    --  When Socket_Error or Host_Error are raised, the exception
690    --  message contains the error code between brackets and a string
691    --  describing the error code. Resolve_Error extracts the error
692    --  code from an exception message and translate it into an
693    --  enumeration value.
694
695    procedure Send_Socket
696      (Socket : Socket_Type;
697       Item   : Ada.Streams.Stream_Element_Array;
698       Last   : out Ada.Streams.Stream_Element_Offset);
699    --  Transmit a message to another socket. Note that Last is set to
700    --  Item'First when socket has been closed by peer. This is not an
701    --  error and no exception is raised. Raise Socket_Error on error;
702
703    procedure Send_Socket
704      (Socket : Socket_Type;
705       Item   : Ada.Streams.Stream_Element_Array;
706       Last   : out Ada.Streams.Stream_Element_Offset;
707       To     : Sock_Addr_Type);
708    --  Transmit a message to another socket. The address is given by
709    --  To. Raise Socket_Error on error;
710
711    procedure Set_Socket_Option
712      (Socket : Socket_Type;
713       Level  : Level_Type := Socket_Level;
714       Option : Option_Type);
715    --  Manipulate socket options. Raise Socket_Error on error.
716
717    procedure Shutdown_Socket
718      (Socket : Socket_Type;
719       How    : Shutmode_Type := Shut_Read_Write);
720    --  Shutdown a connected socket. If How is Shut_Read, further
721    --  receives will be disallowed. If How is Shut_Write, further
722    --  sends will be disallowed. If how is Shut_Read_Write, further
723    --  sends and receives will be disallowed.
724
725    type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
726    --  Same interface as Ada.Streams.Stream_IO
727
728    function Stream
729      (Socket : Socket_Type)
730       return   Stream_Access;
731    --  Associate a stream with a stream-based socket that is already
732    --  connected.
733
734    function Stream
735      (Socket  : Socket_Type;
736       Send_To : Sock_Addr_Type)
737       return    Stream_Access;
738    --  Associate a stream with a datagram-based socket that is already
739    --  bound. Send_To is the socket address to which messages are
740    --  being sent.
741
742    function Get_Address
743      (Stream : Stream_Access)
744      return Sock_Addr_Type;
745    --  Return the socket address from which the last message was
746    --  received.
747
748    type Socket_Set_Type is private;
749    --  This type allows to manipulate sets of sockets. It allows to
750    --  wait for events on multiple endpoints at one time. This is an
751    --  access type on a system dependent structure. To avoid memory
752    --  leaks it is highly recommended to clean the access value with
753    --  procedure Empty.
754
755    procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
756    --  Remove Socket from Item
757
758    procedure Set   (Item : in out Socket_Set_Type; Socket : Socket_Type);
759    --  Insert Socket into Item
760
761    procedure Empty (Item : in out Socket_Set_Type);
762    --  Remove all Sockets from Item and deallocate internal data
763
764    function Is_Empty
765      (Item : Socket_Set_Type)
766       return  Boolean;
767    --  Return True if Item is empty
768
769    function Is_Set
770      (Item   : Socket_Set_Type;
771       Socket : Socket_Type)
772       return   Boolean;
773    --  Return True if Socket is present in Item
774
775    --  C select() waits for a number of file descriptors to change
776    --  status. Usually, three independent sets of descriptors are
777    --  watched (read, write and exception). A timeout gives an upper
778    --  bound on the amount of time elapsed before select returns.
779    --  This function blocks until an event occurs. On some platforms,
780    --  C select can block the full process.
781    --
782    --  Check_Selector provides the very same behaviour. The only
783    --  difference is that it does not watch for exception events. Note
784    --  that on some platforms it is kept process blocking in purpose.
785    --  The timeout parameter allows the user to have the behaviour he
786    --  wants. Abort_Selector allows to abort safely a Check_Selector
787    --  that is blocked forever. A special file descriptor is opened by
788    --  Create_Selector and included in each call to
789    --  Check_Selector. Abort_Selector causes an event to occur on this
790    --  descriptor in order to unblock Check_Selector. The user must
791    --  call Close_Selector to discard this special file. A reason to
792    --  abort a select operation is typically to add a socket in one of
793    --  the socket sets when the timeout is set to forever.
794
795    Forever : constant Duration;
796
797    type Selector_Type is limited private;
798    type Selector_Access is access all Selector_Type;
799
800    procedure Create_Selector (Selector : out Selector_Type);
801    --  Create a new selector
802
803    procedure Close_Selector (Selector : in out Selector_Type);
804    --  Close Selector and all internal descriptors associated
805
806    type Selector_Status is (Completed, Expired, Aborted);
807
808    procedure Check_Selector
809      (Selector     : in out Selector_Type;
810       R_Socket_Set : in out Socket_Set_Type;
811       W_Socket_Set : in out Socket_Set_Type;
812       Status       : out Selector_Status;
813       Timeout      : Duration := Forever);
814    --  Return when one Socket in R_Socket_Set has some data to be read
815    --  or if one Socket in W_Socket_Set is ready to receive some
816    --  data. In these cases Status is set to Completed and sockets
817    --  that are ready are set in R_Socket_Set or W_Socket_Set. Status
818    --  is set to Expired if no socket was ready after a Timeout
819    --  expiration. Status is set to Aborted if an abort signal has been
820    --  received while checking socket status. As this procedure
821    --  returns when Timeout occurs, it is a design choice to keep this
822    --  procedure process blocking. Note that a Timeout of 0.0 returns
823    --  immediatly.
824
825    procedure Abort_Selector (Selector : Selector_Type);
826    --  Send an abort signal to the selector.
827
828 private
829
830    type Socket_Type is new Integer;
831    No_Socket : constant Socket_Type := -1;
832
833    Forever : constant Duration := Duration'Last;
834
835    type Selector_Type is limited record
836       R_Sig_Socket : Socket_Type;
837       W_Sig_Socket : Socket_Type;
838       In_Progress  : Boolean := False;
839    end record;
840    --  The two signalling sockets are used to abort a select
841    --  operation.
842
843    type Socket_Set_Record;
844    type Socket_Set_Type is access all Socket_Set_Record;
845
846    subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
847    --  Octet for Internet address
848
849    type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;
850
851    subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 ..  4);
852    subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);
853
854    type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
855       case Family is
856          when Family_Inet =>
857             Sin_V4 : Inet_Addr_V4_Type := (others => 0);
858
859          when Family_Inet6 =>
860             Sin_V6 : Inet_Addr_V6_Type := (others => 0);
861       end case;
862    end record;
863
864    Any_Port : constant Port_Type := 0;
865    No_Port  : constant Port_Type := 0;
866
867    Any_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
868    No_Inet_Addr  : constant Inet_Addr_Type := (Family_Inet, (others => 0));
869
870    No_Sock_Addr  : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
871
872    Max_Host_Name_Length : constant := 64;
873    --  The constant MAXHOSTNAMELEN is usually set to 64
874
875    subtype Host_Name_Index is Natural range 1 .. Max_Host_Name_Length;
876
877    type Host_Name_Type
878      (Length : Host_Name_Index := Max_Host_Name_Length)
879    is record
880       Name : String (1 .. Length);
881    end record;
882    --  We need fixed strings to avoid access types in host entry type
883
884    type Host_Name_Array is array (Natural range <>) of Host_Name_Type;
885    type Inet_Addr_Array is array (Natural range <>) of Inet_Addr_Type;
886
887    type Host_Entry_Type (Aliases_Length, Addresses_Length : Natural) is record
888       Official  : Host_Name_Type;
889       Aliases   : Host_Name_Array (1 .. Aliases_Length);
890       Addresses : Inet_Addr_Array (1 .. Addresses_Length);
891    end record;
892
893 end GNAT.Sockets;