1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2004 Ada Core Technologies, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This package provides an interface to the sockets communication
35 -- facility provided on many operating systems. This is implemented
36 -- on the following platforms:
38 -- All native ports, except Interix, with restrictions as follows
40 -- Multicast is available only on systems which provide support
41 -- for this feature, so it is not available if Multicast is not
42 -- supported, or not installed. In particular Multicast is not
43 -- available with the Windows version.
45 -- The VMS implementation has implemented using the DECC RTL Socket
46 -- API, and is thus subject to limitations in the implementation of
49 -- This package is not supported on the Interix port of GNAT.
51 -- VxWorks cross ports fully implement this package.
53 -- This package is not yet implemented on LynxOS.
57 with Ada.Unchecked_Deallocation;
61 package GNAT.Sockets is
63 -- Sockets are designed to provide a consistent communication facility
64 -- between applications. This package provides an Ada-like interface
65 -- similar to that proposed as part of the BSD socket layer.
67 -- GNAT.Sockets has been designed with several ideas in mind.
69 -- This is a system independent interface. Therefore, we try as
70 -- much as possible to mask system incompatibilities. Some
71 -- functionalities are not available because there are not fully
72 -- supported on some systems.
74 -- This is a thick binding. For instance, a major effort has been
75 -- done to avoid using memory addresses or untyped ints. We
76 -- preferred to define streams and enumeration types. Errors are
77 -- not returned as returned values but as exceptions.
79 -- This package provides a POSIX-compliant interface (between two
80 -- different implementations of the same routine, we adopt the one
81 -- closest to the POSIX specification). For instance, using
82 -- select(), the notification of an asynchronous connect failure
83 -- is delivered in the write socket set (POSIX) instead of the
84 -- exception socket set (NT).
86 -- Here is a typical example of what you can do:
88 -- with GNAT.Sockets; use GNAT.Sockets;
91 -- with Ada.Exceptions; use Ada.Exceptions;
93 -- procedure PingPong is
95 -- Group : constant String := "239.255.128.128";
96 -- -- Multicast group: administratively scoped IP address
104 -- Address : Sock_Addr_Type;
105 -- Server : Socket_Type;
106 -- Socket : Socket_Type;
107 -- Channel : Stream_Access;
112 -- -- Get an Internet address of a host (here the local host name).
113 -- -- Note that a host can have several addresses. Here we get
114 -- -- the first one which is supposed to be the official one.
116 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
118 -- -- Get a socket address that is an Internet address and a port
120 -- Address.Port := 5876;
122 -- -- The first step is to create a socket. Once created, this
123 -- -- socket must be associated to with an address. Usually only
124 -- -- a server (Pong here) needs to bind an address explicitly.
125 -- -- Most of the time clients can skip this step because the
126 -- -- socket routines will bind an arbitrary address to an unbound
129 -- Create_Socket (Server);
131 -- -- Allow reuse of local addresses
136 -- (Reuse_Address, True));
138 -- Bind_Socket (Server, Address);
140 -- -- A server marks a socket as willing to receive connect events
142 -- Listen_Socket (Server);
144 -- -- Once a server calls Listen_Socket, incoming connects events
145 -- -- can be accepted. The returned Socket is a new socket that
146 -- -- represents the server side of the connection. Server remains
147 -- -- available to receive further connections.
149 -- Accept_Socket (Server, Socket, Address);
151 -- -- Return a stream associated to the connected socket
153 -- Channel := Stream (Socket);
155 -- -- Force Pong to block
159 -- -- Receive and print message from client Ping
162 -- Message : String := String'Input (Channel);
165 -- Ada.Text_IO.Put_Line (Message);
167 -- -- Send same message back to client Ping
169 -- String'Output (Channel, Message);
172 -- Close_Socket (Server);
173 -- Close_Socket (Socket);
175 -- -- Part of the multicast example
177 -- -- Create a datagram socket to send connectionless, unreliable
178 -- -- messages of a fixed maximum length.
180 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
182 -- -- Allow reuse of local addresses
187 -- (Reuse_Address, True));
189 -- -- Join a multicast group
193 -- IP_Protocol_For_IP_Level,
194 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
196 -- -- Controls the live time of the datagram to avoid it being
197 -- -- looped forever due to routing errors. Routers decrement
198 -- -- the TTL of every datagram as it traverses from one network
199 -- -- to another and when its value reaches 0 the packet is
200 -- -- dropped. Default is 1.
204 -- IP_Protocol_For_IP_Level,
205 -- (Multicast_TTL, 1));
207 -- -- Want the data you send to be looped back to your host
211 -- IP_Protocol_For_IP_Level,
212 -- (Multicast_Loop, True));
214 -- -- If this socket is intended to receive messages, bind it
215 -- -- to a given socket address.
217 -- Address.Addr := Any_Inet_Addr;
218 -- Address.Port := 55505;
220 -- Bind_Socket (Socket, Address);
222 -- -- If this socket is intended to send messages, provide the
223 -- -- receiver socket address.
225 -- Address.Addr := Inet_Addr (Group);
226 -- Address.Port := 55506;
228 -- Channel := Stream (Socket, Address);
230 -- -- Receive and print message from client Ping
233 -- Message : String := String'Input (Channel);
236 -- -- Get the address of the sender
238 -- Address := Get_Address (Channel);
239 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
241 -- -- Send same message back to client Ping
243 -- String'Output (Channel, Message);
246 -- Close_Socket (Socket);
250 -- exception when E : others =>
251 -- Ada.Text_IO.Put_Line
252 -- (Exception_Name (E) & ": " & Exception_Message (E));
261 -- Address : Sock_Addr_Type;
262 -- Socket : Socket_Type;
263 -- Channel : Stream_Access;
268 -- -- See comments in Ping section for the first steps
270 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
271 -- Address.Port := 5876;
272 -- Create_Socket (Socket);
277 -- (Reuse_Address, True));
279 -- -- Force Pong to block
283 -- -- If the client's socket is not bound, Connect_Socket will
284 -- -- bind to an unused address. The client uses Connect_Socket to
285 -- -- create a logical connection between the client's socket and
286 -- -- a server's socket returned by Accept_Socket.
288 -- Connect_Socket (Socket, Address);
290 -- Channel := Stream (Socket);
292 -- -- Send message to server Pong.
294 -- String'Output (Channel, "Hello world");
296 -- -- Force Ping to block
300 -- -- Receive and print message from server Pong
302 -- Ada.Text_IO.Put_Line (String'Input (Channel));
303 -- Close_Socket (Socket);
305 -- -- Part of multicast example. Code similar to Pong's one
307 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
312 -- (Reuse_Address, True));
316 -- IP_Protocol_For_IP_Level,
317 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
321 -- IP_Protocol_For_IP_Level,
322 -- (Multicast_TTL, 1));
326 -- IP_Protocol_For_IP_Level,
327 -- (Multicast_Loop, True));
329 -- Address.Addr := Any_Inet_Addr;
330 -- Address.Port := 55506;
332 -- Bind_Socket (Socket, Address);
334 -- Address.Addr := Inet_Addr (Group);
335 -- Address.Port := 55505;
337 -- Channel := Stream (Socket, Address);
339 -- -- Send message to server Pong
341 -- String'Output (Channel, "Hello world");
343 -- -- Receive and print message from server Pong
346 -- Message : String := String'Input (Channel);
349 -- Address := Get_Address (Channel);
350 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
353 -- Close_Socket (Socket);
357 -- exception when E : others =>
358 -- Ada.Text_IO.Put_Line
359 -- (Exception_Name (E) & ": " & Exception_Message (E));
363 -- -- Indicate whether the thread library provides process
364 -- -- blocking IO. Basically, if you are not using FSU threads
365 -- -- the default is ok.
367 -- Initialize (Process_Blocking_IO => False);
375 procedure Initialize (Process_Blocking_IO : Boolean := False);
376 -- Initialize must be called before using any other socket routines.
377 -- The Process_Blocking_IO parameter indicates whether the thread
378 -- library provides process-blocking or thread-blocking input/output
379 -- operations. In the former case (typically with FSU threads)
380 -- GNAT.Sockets should be initialized with a value of True to
381 -- provide task-blocking IO through an emulation mechanism.
382 -- Only the first call to Initialize is taken into account (further
383 -- calls will be ignored). Note that with the default value
384 -- of Process_Blocking_IO, this operation is a no-op on UNIX
385 -- platforms, but applications should make sure to call it
386 -- if portability is expected: some platforms (such as Windows)
387 -- require initialization before any other socket operations.
390 -- After Finalize is called it is not possible to use any routines
391 -- exported in by this package. This procedure is idempotent.
393 type Socket_Type is private;
394 -- Sockets are used to implement a reliable bi-directional
395 -- point-to-point, stream-based connections between
396 -- hosts. No_Socket provides a special value to denote
397 -- uninitialized sockets.
399 No_Socket : constant Socket_Type;
401 Socket_Error : exception;
402 -- There is only one exception in this package to deal with an error during
403 -- a socket routine. Once raised, its message contains a string describing
406 function Image (Socket : Socket_Type) return String;
407 -- Return a printable string for Socket
409 function To_C (Socket : Socket_Type) return Integer;
410 -- Return a file descriptor to be used by external subprograms. This is
411 -- useful for C functions that are not yet interfaced in this package.
413 type Family_Type is (Family_Inet, Family_Inet6);
414 -- Address family (or protocol family) identifies the communication domain
415 -- and groups protocols with similar address formats. IPv6 will soon be
418 type Mode_Type is (Socket_Stream, Socket_Datagram);
419 -- Stream sockets provide connection-oriented byte streams. Datagram
420 -- sockets support unreliable connectionless message based communication.
422 type Shutmode_Type is (Shut_Read, Shut_Write, Shut_Read_Write);
423 -- When a process closes a socket, the policy is to retain any data queued
424 -- until either a delivery or a timeout expiration (in this case, the data
425 -- are discarded). A finer control is available through shutdown. With
426 -- Shut_Read, no more data can be received from the socket. With_Write, no
427 -- more data can be transmitted. Neither transmission nor reception can be
428 -- performed with Shut_Read_Write.
430 type Port_Type is new Natural;
431 -- Classical port definition. No_Port provides a special value to
432 -- denote uninitialized port. Any_Port provides a special value
433 -- enabling all ports.
435 Any_Port : constant Port_Type;
436 No_Port : constant Port_Type;
438 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
439 -- An Internet address depends on an address family (IPv4 contains
440 -- 4 octets and Ipv6 contains 16 octets). Any_Inet_Addr is a special
441 -- value treated like a wildcard enabling all addresses.
442 -- No_Inet_Addr provides a special value to denote uninitialized
445 Any_Inet_Addr : constant Inet_Addr_Type;
446 No_Inet_Addr : constant Inet_Addr_Type;
448 type Sock_Addr_Type (Family : Family_Type := Family_Inet) is record
449 Addr : Inet_Addr_Type (Family);
452 -- Socket addresses fully define a socket connection with a
453 -- protocol family, an Internet address and a port. No_Sock_Addr
454 -- provides a special value for uninitialized socket addresses.
456 No_Sock_Addr : constant Sock_Addr_Type;
458 function Image (Value : Inet_Addr_Type) return String;
459 -- Return an image of an Internet address. IPv4 notation consists
460 -- in 4 octets in decimal format separated by dots. IPv6 notation
461 -- consists in 16 octets in hexadecimal format separated by
462 -- colons (and possibly dots).
464 function Image (Value : Sock_Addr_Type) return String;
465 -- Return inet address image and port image separated by a colon.
467 function Inet_Addr (Image : String) return Inet_Addr_Type;
468 -- Convert address image from numbers-and-dots notation into an
471 -- Host entries provide complete information on a given host:
472 -- the official name, an array of alternative names or aliases and
473 -- array of network addresses.
476 (Aliases_Length, Addresses_Length : Natural) is private;
478 function Official_Name (E : Host_Entry_Type) return String;
479 -- Return official name in host entry
481 function Aliases_Length (E : Host_Entry_Type) return Natural;
482 -- Return number of aliases in host entry
484 function Addresses_Length (E : Host_Entry_Type) return Natural;
485 -- Return number of addresses in host entry
488 (E : Host_Entry_Type;
489 N : Positive := 1) return String;
490 -- Return N'th aliases in host entry. The first index is 1.
493 (E : Host_Entry_Type;
494 N : Positive := 1) return Inet_Addr_Type;
495 -- Return N'th addresses in host entry. The first index is 1.
497 Host_Error : exception;
498 -- Exception raised by the two following procedures. Once raised,
499 -- its message contains a string describing the error code. This
500 -- exception is raised when an host entry can not be retrieved.
502 function Get_Host_By_Address
503 (Address : Inet_Addr_Type;
504 Family : Family_Type := Family_Inet) return Host_Entry_Type;
505 -- Return host entry structure for the given Inet address.
506 -- Note that no result will be returned if there is no mapping of this
507 -- IP address to a host name in the system tables (host database,
508 -- DNS or otherwise).
510 function Get_Host_By_Name
511 (Name : String) return Host_Entry_Type;
512 -- Return host entry structure for the given host name. Here name
513 -- is either a host name, or an IP address. If Name is an IP address,
514 -- this is equivalent to Get_Host_By_Address (Inet_Addr (Name)).
516 function Host_Name return String;
517 -- Return the name of the current host
519 type Service_Entry_Type (Aliases_Length : Natural) is private;
520 -- Service entries provide complete information on a given
521 -- service: the official name, an array of alternative names or
522 -- aliases and the port number.
524 function Official_Name (S : Service_Entry_Type) return String;
525 -- Return official name in service entry
527 function Port_Number (S : Service_Entry_Type) return Port_Type;
528 -- Return port number in service entry
530 function Protocol_Name (S : Service_Entry_Type) return String;
531 -- Return Protocol in service entry (usually UDP or TCP)
533 function Aliases_Length (S : Service_Entry_Type) return Natural;
534 -- Return number of aliases in service entry
537 (S : Service_Entry_Type;
538 N : Positive := 1) return String;
539 -- Return N'th aliases in service entry. The first index is 1.
541 function Get_Service_By_Name
543 Protocol : String) return Service_Entry_Type;
544 -- Return service entry structure for the given service name
546 function Get_Service_By_Port
548 Protocol : String) return Service_Entry_Type;
549 -- Return service entry structure for the given service port number
551 Service_Error : exception;
552 -- Comment required ???
554 -- Errors are described by an enumeration type. There is only one
555 -- exception Socket_Error in this package to deal with an error
556 -- during a socket routine. Once raised, its message contains the
557 -- error code between brackets and a string describing the error code.
559 -- The name of the enumeration constant documents the error condition
564 Address_Already_In_Use,
565 Cannot_Assign_Requested_Address,
566 Address_Family_Not_Supported_By_Protocol,
567 Operation_Already_In_Progress,
569 Software_Caused_Connection_Abort,
571 Connection_Reset_By_Peer,
572 Destination_Address_Required,
576 Operation_Now_In_Progress,
577 Interrupted_System_Call,
580 Transport_Endpoint_Already_Connected,
581 Too_Many_Symbolic_Links,
586 Network_Dropped_Connection_Because_Of_Reset,
587 Network_Is_Unreachable,
588 No_Buffer_Space_Available,
589 Protocol_Not_Available,
590 Transport_Endpoint_Not_Connected,
591 Socket_Operation_On_Non_Socket,
592 Operation_Not_Supported,
593 Protocol_Family_Not_Supported,
594 Protocol_Not_Supported,
595 Protocol_Wrong_Type_For_Socket,
596 Cannot_Send_After_Transport_Endpoint_Shutdown,
597 Socket_Type_Not_Supported,
598 Connection_Timed_Out,
600 Resource_Temporarily_Unavailable,
602 Host_Name_Lookup_Failure,
603 Non_Recoverable_Error,
604 Unknown_Server_Error,
605 Cannot_Resolve_Error);
607 -- Get_Socket_Options and Set_Socket_Options manipulate options
608 -- associated with a socket. Options may exist at multiple
609 -- protocol levels in the communication stack. Socket_Level is the
610 -- uppermost socket level.
614 IP_Protocol_For_IP_Level,
615 IP_Protocol_For_UDP_Level,
616 IP_Protocol_For_TCP_Level);
618 -- There are several options available to manipulate sockets. Each
619 -- option has a name and several values available. Most of the
620 -- time, the value is a boolean to enable or disable this option.
622 type Option_Name is (
623 Keep_Alive, -- Enable sending of keep-alive messages
624 Reuse_Address, -- Allow bind to reuse local address
625 Broadcast, -- Enable datagram sockets to recv/send broadcast packets
626 Send_Buffer, -- Set/get the maximum socket send buffer in bytes
627 Receive_Buffer, -- Set/get the maximum socket recv buffer in bytes
628 Linger, -- Shutdown wait for msg to be sent or timeout occur
629 Error, -- Get and clear the pending socket error
630 No_Delay, -- Do not delay send to coalesce packets (TCP_NODELAY)
631 Add_Membership, -- Join a multicast group
632 Drop_Membership, -- Leave a multicast group
633 Multicast_TTL, -- Indicate the time-to-live of sent multicast packets
634 Multicast_Loop); -- Sent multicast packets are looped to the local socket
636 type Option_Type (Name : Option_Name := Keep_Alive) is record
660 when Add_Membership |
662 Multicast_Address : Inet_Addr_Type;
663 Local_Interface : Inet_Addr_Type;
665 when Multicast_TTL =>
666 Time_To_Live : Natural;
671 -- There are several controls available to manipulate
672 -- sockets. Each option has a name and several values available.
673 -- These controls differ from the socket options in that they are
674 -- not specific to sockets but are available for any device.
676 type Request_Name is (
677 Non_Blocking_IO, -- Cause a caller not to wait on blocking operations.
678 N_Bytes_To_Read); -- Return the number of bytes available to read
680 type Request_Type (Name : Request_Name := Non_Blocking_IO) is record
682 when Non_Blocking_IO =>
685 when N_Bytes_To_Read =>
691 -- A request flag allows to specify the type of message
692 -- transmissions or receptions. A request flag can be a
693 -- combination of zero or more predefined request flags.
695 type Request_Flag_Type is private;
697 No_Request_Flag : constant Request_Flag_Type;
698 -- This flag corresponds to the normal execution of an operation.
700 Process_Out_Of_Band_Data : constant Request_Flag_Type;
701 -- This flag requests that the receive or send function operates
702 -- on out-of-band data when the socket supports this notion (e.g.
705 Peek_At_Incoming_Data : constant Request_Flag_Type;
706 -- This flag causes the receive operation to return data from the
707 -- beginning of the receive queue without removing that data from
708 -- the queue. A subsequent receive call will return the same data.
710 Wait_For_A_Full_Reception : constant Request_Flag_Type;
711 -- This flag requests that the operation block until the full
712 -- request is satisfied. However, the call may still return less
713 -- data than requested if a signal is caught, an error or
714 -- disconnect occurs, or the next data to be received is of a dif-
715 -- ferent type than that returned.
717 Send_End_Of_Record : constant Request_Flag_Type;
718 -- This flag indicates that the entire message has been sent and
719 -- so this terminates the record.
721 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type;
722 -- Combine flag L with flag R
724 type Stream_Element_Reference is access all Ada.Streams.Stream_Element;
726 type Vector_Element is record
727 Base : Stream_Element_Reference;
728 Length : Ada.Streams.Stream_Element_Count;
731 type Vector_Type is array (Integer range <>) of Vector_Element;
733 procedure Create_Socket
734 (Socket : out Socket_Type;
735 Family : Family_Type := Family_Inet;
736 Mode : Mode_Type := Socket_Stream);
737 -- Create an endpoint for communication. Raises Socket_Error on error.
739 procedure Accept_Socket
740 (Server : Socket_Type;
741 Socket : out Socket_Type;
742 Address : out Sock_Addr_Type);
743 -- Extract the first connection request on the queue of pending
744 -- connections, creates a new connected socket with mostly the
745 -- same properties as Server, and allocates a new socket. The
746 -- returned Address is filled in with the address of the
747 -- connection. Raises Socket_Error on error.
749 procedure Bind_Socket
750 (Socket : Socket_Type;
751 Address : Sock_Addr_Type);
752 -- Once a socket is created, assign a local address to it. Raise
753 -- Socket_Error on error.
755 procedure Close_Socket (Socket : Socket_Type);
756 -- Close a socket and more specifically a non-connected socket.
758 procedure Connect_Socket
759 (Socket : Socket_Type;
760 Server : in out Sock_Addr_Type);
761 -- Make a connection to another socket which has the address of
762 -- Server. Raises Socket_Error on error.
764 procedure Control_Socket
765 (Socket : Socket_Type;
766 Request : in out Request_Type);
767 -- Obtain or set parameter values that control the socket. This
768 -- control differs from the socket options in that they are not
769 -- specific to sockets but are available for any device.
771 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
772 -- Return the peer or remote socket address of a socket. Raise
773 -- Socket_Error on error.
775 function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
776 -- Return the local or current socket address of a socket. Return
777 -- No_Sock_Addr on error (for instance, socket closed or not
780 function Get_Socket_Option
781 (Socket : Socket_Type;
782 Level : Level_Type := Socket_Level;
783 Name : Option_Name) return Option_Type;
784 -- Get the options associated with a socket. Raises Socket_Error
787 procedure Listen_Socket
788 (Socket : Socket_Type;
789 Length : Positive := 15);
790 -- To accept connections, a socket is first created with
791 -- Create_Socket, a willingness to accept incoming connections and
792 -- a queue Length for incoming connections are specified. Raise
793 -- Socket_Error on error.
795 procedure Receive_Socket
796 (Socket : Socket_Type;
797 Item : out Ada.Streams.Stream_Element_Array;
798 Last : out Ada.Streams.Stream_Element_Offset;
799 Flags : Request_Flag_Type := No_Request_Flag);
800 -- Receive message from Socket. Last is the index value such that
801 -- Item (Last) is the last character assigned. Note that Last is
802 -- set to Item'First - 1 when the socket has been closed by
803 -- peer. This is not an error and no exception is raised. Flags
804 -- allows to control the reception. Raise Socket_Error on error.
806 procedure Receive_Socket
807 (Socket : Socket_Type;
808 Item : out Ada.Streams.Stream_Element_Array;
809 Last : out Ada.Streams.Stream_Element_Offset;
810 From : out Sock_Addr_Type;
811 Flags : Request_Flag_Type := No_Request_Flag);
812 -- Receive message from Socket. If Socket is not
813 -- connection-oriented, the source address From of the message is
814 -- filled in. Last is the index value such that Item (Last) is the
815 -- last character assigned. Flags allows to control the
816 -- reception. Raises Socket_Error on error.
818 procedure Receive_Vector
819 (Socket : Socket_Type;
820 Vector : Vector_Type;
821 Count : out Ada.Streams.Stream_Element_Count);
822 -- Receive data from a socket and scatter it into the set of vector
823 -- elements Vector. Count is set to the count of received stream elements.
825 function Resolve_Exception
826 (Occurrence : Ada.Exceptions.Exception_Occurrence) return Error_Type;
827 -- When Socket_Error or Host_Error are raised, the exception
828 -- message contains the error code between brackets and a string
829 -- describing the error code. Resolve_Error extracts the error
830 -- code from an exception message and translate it into an
831 -- enumeration value.
833 procedure Send_Socket
834 (Socket : Socket_Type;
835 Item : Ada.Streams.Stream_Element_Array;
836 Last : out Ada.Streams.Stream_Element_Offset;
837 Flags : Request_Flag_Type := No_Request_Flag);
838 -- Transmit a message to another socket. Note that Last is set to
839 -- Item'First-1 when socket has been closed by peer. This is not
840 -- considered an error and no exception is raised. Flags allows to
841 -- control the transmission. Raises Socket_Error on any other
844 procedure Send_Socket
845 (Socket : Socket_Type;
846 Item : Ada.Streams.Stream_Element_Array;
847 Last : out Ada.Streams.Stream_Element_Offset;
849 Flags : Request_Flag_Type := No_Request_Flag);
850 -- Transmit a message to another socket. The address is given by
851 -- To. Flags allows to control the transmission. Raises
852 -- Socket_Error on error.
854 procedure Send_Vector
855 (Socket : Socket_Type;
856 Vector : Vector_Type;
857 Count : out Ada.Streams.Stream_Element_Count);
858 -- Transmit data gathered from the set of vector elements Vector to a
859 -- socket. Count is set to the count of transmitted stream elements.
861 procedure Set_Socket_Option
862 (Socket : Socket_Type;
863 Level : Level_Type := Socket_Level;
864 Option : Option_Type);
865 -- Manipulate socket options. Raises Socket_Error on error.
867 procedure Shutdown_Socket
868 (Socket : Socket_Type;
869 How : Shutmode_Type := Shut_Read_Write);
870 -- Shutdown a connected socket. If How is Shut_Read, further
871 -- receives will be disallowed. If How is Shut_Write, further
872 -- sends will be disallowed. If how is Shut_Read_Write, further
873 -- sends and receives will be disallowed.
875 type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
876 -- Same interface as Ada.Streams.Stream_IO
879 (Socket : Socket_Type) return Stream_Access;
880 -- Create a stream associated with a stream-based socket that is
881 -- already connected.
884 (Socket : Socket_Type;
885 Send_To : Sock_Addr_Type) return Stream_Access;
886 -- Create a stream associated with a datagram-based socket that is
887 -- already bound. Send_To is the socket address to which messages are
891 (Stream : Stream_Access) return Sock_Addr_Type;
892 -- Return the socket address from which the last message was received.
894 procedure Free is new Ada.Unchecked_Deallocation
895 (Ada.Streams.Root_Stream_Type'Class, Stream_Access);
896 -- Destroy a stream created by one of the Stream functions above,
897 -- releasing the corresponding resources. The user is responsible
898 -- for calling this subprogram when the stream is not needed anymore.
900 type Socket_Set_Type is limited private;
901 -- This type allows to manipulate sets of sockets. It allows to
902 -- wait for events on multiple endpoints at one time. This is an
903 -- access type on a system dependent structure. To avoid memory
904 -- leaks it is highly recommended to clean the access value with
907 procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
908 -- Remove Socket from Item
910 procedure Copy (Source : Socket_Set_Type; Target : in out Socket_Set_Type);
911 -- Copy Source into Target as Socket_Set_Type is limited private
913 procedure Empty (Item : in out Socket_Set_Type);
914 -- Remove all Sockets from Item and deallocate internal data
916 procedure Get (Item : in out Socket_Set_Type; Socket : out Socket_Type);
917 -- Extract a Socket from socket set Item. Socket is set to
918 -- No_Socket when the set is empty.
921 (Item : Socket_Set_Type) return Boolean;
922 -- Return True iff Item is empty
925 (Item : Socket_Set_Type;
926 Socket : Socket_Type) return Boolean;
927 -- Return True iff Socket is present in Item
929 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type);
930 -- Insert Socket into Item
932 -- C select() waits for a number of file descriptors to change
933 -- status. Usually, three independent sets of descriptors are
934 -- watched (read, write and exception). A timeout gives an upper
935 -- bound on the amount of time elapsed before select returns.
936 -- This function blocks until an event occurs. On some platforms,
937 -- C select can block the full process.
939 -- Check_Selector provides the very same behaviour. The only
940 -- difference is that it does not watch for exception events. Note
941 -- that on some platforms it is kept process blocking in purpose.
942 -- The timeout parameter allows the user to have the behaviour he
943 -- wants. Abort_Selector allows to abort safely a Check_Selector
944 -- that is blocked forever. A special file descriptor is opened by
945 -- Create_Selector and included in each call to
946 -- Check_Selector. Abort_Selector causes an event to occur on this
947 -- descriptor in order to unblock Check_Selector. The user must
948 -- call Close_Selector to discard this special file. A reason to
949 -- abort a select operation is typically to add a socket in one of
950 -- the socket sets when the timeout is set to forever.
952 type Selector_Type is limited private;
953 type Selector_Access is access all Selector_Type;
955 -- Selector_Duration is a subtype of Standard.Duration because the
956 -- full range of Standard.Duration cannot be represented in the
957 -- equivalent C structure. Moreover, negative values are not
958 -- allowed to avoid system incompatibilities.
960 Immediate : constant := 0.0;
961 Forever : constant := Duration (Integer'Last) * 1.0;
963 subtype Selector_Duration is Duration range Immediate .. Forever;
965 procedure Create_Selector (Selector : out Selector_Type);
966 -- Create a new selector
968 procedure Close_Selector (Selector : in out Selector_Type);
969 -- Close Selector and all internal descriptors associated
971 type Selector_Status is (Completed, Expired, Aborted);
973 procedure Check_Selector
974 (Selector : in out Selector_Type;
975 R_Socket_Set : in out Socket_Set_Type;
976 W_Socket_Set : in out Socket_Set_Type;
977 Status : out Selector_Status;
978 Timeout : Selector_Duration := Forever);
979 -- Return when one Socket in R_Socket_Set has some data to be read
980 -- or if one Socket in W_Socket_Set is ready to receive some
981 -- data. In these cases Status is set to Completed and sockets
982 -- that are ready are set in R_Socket_Set or W_Socket_Set. Status
983 -- is set to Expired if no socket was ready after a Timeout
984 -- expiration. Status is set to Aborted if an abort signal has been
985 -- received while checking socket status. As this procedure
986 -- returns when Timeout occurs, it is a design choice to keep this
987 -- procedure process blocking. Note that a Timeout of 0.0 returns
988 -- immediately. Also note that two different objects must be passed
989 -- as R_Socket_Set and W_Socket_Set (even if they contain the same
990 -- set of Sockets), or some event will be lost.
992 procedure Check_Selector
993 (Selector : in out Selector_Type;
994 R_Socket_Set : in out Socket_Set_Type;
995 W_Socket_Set : in out Socket_Set_Type;
996 E_Socket_Set : in out Socket_Set_Type;
997 Status : out Selector_Status;
998 Timeout : Selector_Duration := Forever);
999 -- This refined version of Check_Selector allows to watch for
1000 -- exception events (that is notifications of out-of-band
1001 -- transmission and reception). As above, all of R_Socket_Set,
1002 -- W_Socket_Set and E_Socket_Set must be different objects.
1004 procedure Abort_Selector (Selector : Selector_Type);
1005 -- Send an abort signal to the selector.
1009 type Socket_Type is new Integer;
1010 No_Socket : constant Socket_Type := -1;
1012 type Selector_Type is limited record
1013 R_Sig_Socket : Socket_Type;
1014 W_Sig_Socket : Socket_Type;
1017 pragma Volatile (Selector_Type);
1019 -- The two signalling sockets are used to abort a select
1022 subtype Socket_Set_Access is System.Address;
1023 No_Socket_Set : constant Socket_Set_Access := System.Null_Address;
1025 type Socket_Set_Type is record
1026 Last : Socket_Type := No_Socket;
1027 Set : Socket_Set_Access := No_Socket_Set;
1030 subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
1031 -- Octet for Internet address
1033 type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;
1035 subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 .. 4);
1036 subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);
1038 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
1041 Sin_V4 : Inet_Addr_V4_Type := (others => 0);
1043 when Family_Inet6 =>
1044 Sin_V6 : Inet_Addr_V6_Type := (others => 0);
1048 Any_Port : constant Port_Type := 0;
1049 No_Port : constant Port_Type := 0;
1051 Any_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
1052 No_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
1054 No_Sock_Addr : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
1056 Max_Name_Length : constant := 64;
1057 -- The constant MAXHOSTNAMELEN is usually set to 64
1059 subtype Name_Index is Natural range 1 .. Max_Name_Length;
1062 (Length : Name_Index := Max_Name_Length)
1064 Name : String (1 .. Length);
1066 -- We need fixed strings to avoid access types in host entry type
1068 type Name_Array is array (Natural range <>) of Name_Type;
1069 type Inet_Addr_Array is array (Natural range <>) of Inet_Addr_Type;
1071 type Host_Entry_Type (Aliases_Length, Addresses_Length : Natural) is record
1072 Official : Name_Type;
1073 Aliases : Name_Array (1 .. Aliases_Length);
1074 Addresses : Inet_Addr_Array (1 .. Addresses_Length);
1077 type Service_Entry_Type (Aliases_Length : Natural) is record
1078 Official : Name_Type;
1079 Aliases : Name_Array (1 .. Aliases_Length);
1081 Protocol : Name_Type;
1084 type Request_Flag_Type is mod 2 ** 8;
1085 No_Request_Flag : constant Request_Flag_Type := 0;
1086 Process_Out_Of_Band_Data : constant Request_Flag_Type := 1;
1087 Peek_At_Incoming_Data : constant Request_Flag_Type := 2;
1088 Wait_For_A_Full_Reception : constant Request_Flag_Type := 4;
1089 Send_End_Of_Record : constant Request_Flag_Type := 8;