1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2005 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 facility
35 -- provided on many operating systems. This is implemented on the following
38 -- All native ports, with restrictions as follows
40 -- Multicast is available only on systems which provide support for this
41 -- feature, so it is not available if Multicast is not supported, or not
42 -- installed. In particular Multicast is not available with the Windows
45 -- The VMS implementation has implemented using the DECC RTL Socket API,
46 -- and is thus subject to limitations in the implementation of this API.
48 -- VxWorks cross ports fully implement this package
50 -- This package is not yet implemented on LynxOS or other cross ports
54 with Ada
.Unchecked_Deallocation
;
58 package GNAT
.Sockets
is
60 -- Sockets are designed to provide a consistent communication facility
61 -- between applications. This package provides an Ada-like interface
62 -- similar to that proposed as part of the BSD socket layer.
64 -- GNAT.Sockets has been designed with several ideas in mind
66 -- This is a system independent interface. Therefore, we try as much as
67 -- possible to mask system incompatibilities. Some functionalities are not
68 -- available because there are not fully supported on some systems.
70 -- This is a thick binding. For instance, a major effort has been done to
71 -- avoid using memory addresses or untyped ints. We preferred to define
72 -- streams and enumeration types. Errors are not returned as returned
73 -- values but as exceptions.
75 -- This package provides a POSIX-compliant interface (between two
76 -- different implementations of the same routine, we adopt the one closest
77 -- to the POSIX specification). For instance, using select(), the
78 -- notification of an asynchronous connect failure is delivered in the
79 -- write socket set (POSIX) instead of the exception socket set (NT).
81 -- Here is a typical example of what you can do:
83 -- with GNAT.Sockets; use GNAT.Sockets;
86 -- with Ada.Exceptions; use Ada.Exceptions;
88 -- procedure PingPong is
90 -- Group : constant String := "239.255.128.128";
91 -- -- Multicast group: administratively scoped IP address
99 -- Address : Sock_Addr_Type;
100 -- Server : Socket_Type;
101 -- Socket : Socket_Type;
102 -- Channel : Stream_Access;
107 -- -- Get an Internet address of a host (here the local host name).
108 -- -- Note that a host can have several addresses. Here we get
109 -- -- the first one which is supposed to be the official one.
111 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
113 -- -- Get a socket address that is an Internet address and a port
115 -- Address.Port := 5876;
117 -- -- The first step is to create a socket. Once created, this
118 -- -- socket must be associated to with an address. Usually only a
119 -- -- server (Pong here) needs to bind an address explicitly. Most
120 -- -- of the time clients can skip this step because the socket
121 -- -- routines will bind an arbitrary address to an unbound socket.
123 -- Create_Socket (Server);
125 -- -- Allow reuse of local addresses
130 -- (Reuse_Address, True));
132 -- Bind_Socket (Server, Address);
134 -- -- A server marks a socket as willing to receive connect events
136 -- Listen_Socket (Server);
138 -- -- Once a server calls Listen_Socket, incoming connects events
139 -- -- can be accepted. The returned Socket is a new socket that
140 -- -- represents the server side of the connection. Server remains
141 -- -- available to receive further connections.
143 -- Accept_Socket (Server, Socket, Address);
145 -- -- Return a stream associated to the connected socket
147 -- Channel := Stream (Socket);
149 -- -- Force Pong to block
153 -- -- Receive and print message from client Ping
156 -- Message : String := String'Input (Channel);
159 -- Ada.Text_IO.Put_Line (Message);
161 -- -- Send same message back to client Ping
163 -- String'Output (Channel, Message);
166 -- Close_Socket (Server);
167 -- Close_Socket (Socket);
169 -- -- Part of the multicast example
171 -- -- Create a datagram socket to send connectionless, unreliable
172 -- -- messages of a fixed maximum length.
174 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
176 -- -- Allow reuse of local addresses
181 -- (Reuse_Address, True));
183 -- -- Join a multicast group
187 -- IP_Protocol_For_IP_Level,
188 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
190 -- -- Controls the live time of the datagram to avoid it being
191 -- -- looped forever due to routing errors. Routers decrement
192 -- -- the TTL of every datagram as it traverses from one network
193 -- -- to another and when its value reaches 0 the packet is
194 -- -- dropped. Default is 1.
198 -- IP_Protocol_For_IP_Level,
199 -- (Multicast_TTL, 1));
201 -- -- Want the data you send to be looped back to your host
205 -- IP_Protocol_For_IP_Level,
206 -- (Multicast_Loop, True));
208 -- -- If this socket is intended to receive messages, bind it
209 -- -- to a given socket address.
211 -- Address.Addr := Any_Inet_Addr;
212 -- Address.Port := 55505;
214 -- Bind_Socket (Socket, Address);
216 -- -- If this socket is intended to send messages, provide the
217 -- -- receiver socket address.
219 -- Address.Addr := Inet_Addr (Group);
220 -- Address.Port := 55506;
222 -- Channel := Stream (Socket, Address);
224 -- -- Receive and print message from client Ping
227 -- Message : String := String'Input (Channel);
230 -- -- Get the address of the sender
232 -- Address := Get_Address (Channel);
233 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
235 -- -- Send same message back to client Ping
237 -- String'Output (Channel, Message);
240 -- Close_Socket (Socket);
244 -- exception when E : others =>
245 -- Ada.Text_IO.Put_Line
246 -- (Exception_Name (E) & ": " & Exception_Message (E));
255 -- Address : Sock_Addr_Type;
256 -- Socket : Socket_Type;
257 -- Channel : Stream_Access;
262 -- -- See comments in Ping section for the first steps
264 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
265 -- Address.Port := 5876;
266 -- Create_Socket (Socket);
271 -- (Reuse_Address, True));
273 -- -- Force Pong to block
277 -- -- If the client's socket is not bound, Connect_Socket will
278 -- -- bind to an unused address. The client uses Connect_Socket to
279 -- -- create a logical connection between the client's socket and
280 -- -- a server's socket returned by Accept_Socket.
282 -- Connect_Socket (Socket, Address);
284 -- Channel := Stream (Socket);
286 -- -- Send message to server Pong
288 -- String'Output (Channel, "Hello world");
290 -- -- Force Ping to block
294 -- -- Receive and print message from server Pong
296 -- Ada.Text_IO.Put_Line (String'Input (Channel));
297 -- Close_Socket (Socket);
299 -- -- Part of multicast example. Code similar to Pong's one
301 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
306 -- (Reuse_Address, True));
310 -- IP_Protocol_For_IP_Level,
311 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
315 -- IP_Protocol_For_IP_Level,
316 -- (Multicast_TTL, 1));
320 -- IP_Protocol_For_IP_Level,
321 -- (Multicast_Loop, True));
323 -- Address.Addr := Any_Inet_Addr;
324 -- Address.Port := 55506;
326 -- Bind_Socket (Socket, Address);
328 -- Address.Addr := Inet_Addr (Group);
329 -- Address.Port := 55505;
331 -- Channel := Stream (Socket, Address);
333 -- -- Send message to server Pong
335 -- String'Output (Channel, "Hello world");
337 -- -- Receive and print message from server Pong
340 -- Message : String := String'Input (Channel);
343 -- Address := Get_Address (Channel);
344 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
347 -- Close_Socket (Socket);
351 -- exception when E : others =>
352 -- Ada.Text_IO.Put_Line
353 -- (Exception_Name (E) & ": " & Exception_Message (E));
357 -- -- Indicate whether the thread library provides process
358 -- -- blocking IO. Basically, if you are not using FSU threads
359 -- -- the default is ok.
361 -- Initialize (Process_Blocking_IO => False);
369 procedure Initialize
(Process_Blocking_IO
: Boolean := False);
370 -- Initialize must be called before using any other socket routines. The
371 -- Process_Blocking_IO parameter indicates whether the thread library
372 -- provides process-blocking or thread-blocking input/output operations.
373 -- In the former case (typically with FSU threads) GNAT.Sockets should be
374 -- initialized with a value of True to provide task-blocking IO through an
375 -- emulation mechanism. Only the first call to Initialize is taken into
376 -- account (further calls will be ignored). Note that with the default
377 -- value of Process_Blocking_IO, this operation is a no-op on UNIX
378 -- platforms, but applications should make sure to call it if portability
379 -- is expected: some platforms (such as Windows) require initialization
380 -- before any other socket operations.
383 -- After Finalize is called it is not possible to use any routines
384 -- exported in by this package. This procedure is idempotent.
386 type Socket_Type
is private;
387 -- Sockets are used to implement a reliable bi-directional point-to-point,
388 -- stream-based connections between hosts. No_Socket provides a special
389 -- value to denote uninitialized sockets.
391 No_Socket
: constant Socket_Type
;
393 Socket_Error
: exception;
394 -- There is only one exception in this package to deal with an error during
395 -- a socket routine. Once raised, its message contains a string describing
398 function Image
(Socket
: Socket_Type
) return String;
399 -- Return a printable string for Socket
401 function To_C
(Socket
: Socket_Type
) return Integer;
402 -- Return a file descriptor to be used by external subprograms. This is
403 -- useful for C functions that are not yet interfaced in this package.
405 type Family_Type
is (Family_Inet
, Family_Inet6
);
406 -- Address family (or protocol family) identifies the communication domain
407 -- and groups protocols with similar address formats. IPv6 will soon be
410 type Mode_Type
is (Socket_Stream
, Socket_Datagram
);
411 -- Stream sockets provide connection-oriented byte streams. Datagram
412 -- sockets support unreliable connectionless message based communication.
414 type Shutmode_Type
is (Shut_Read
, Shut_Write
, Shut_Read_Write
);
415 -- When a process closes a socket, the policy is to retain any data queued
416 -- until either a delivery or a timeout expiration (in this case, the data
417 -- are discarded). A finer control is available through shutdown. With
418 -- Shut_Read, no more data can be received from the socket. With_Write, no
419 -- more data can be transmitted. Neither transmission nor reception can be
420 -- performed with Shut_Read_Write.
422 type Port_Type
is new Natural;
423 -- Classical port definition. No_Port provides a special value to
424 -- denote uninitialized port. Any_Port provides a special value
425 -- enabling all ports.
427 Any_Port
: constant Port_Type
;
428 No_Port
: constant Port_Type
;
430 type Inet_Addr_Type
(Family
: Family_Type
:= Family_Inet
) is private;
431 -- An Internet address depends on an address family (IPv4 contains 4
432 -- octets and Ipv6 contains 16 octets). Any_Inet_Addr is a special value
433 -- treated like a wildcard enabling all addresses. No_Inet_Addr provides a
434 -- special value to denote uninitialized inet addresses.
436 Any_Inet_Addr
: constant Inet_Addr_Type
;
437 No_Inet_Addr
: constant Inet_Addr_Type
;
438 Broadcast_Inet_Addr
: constant Inet_Addr_Type
;
440 type Sock_Addr_Type
(Family
: Family_Type
:= Family_Inet
) is record
441 Addr
: Inet_Addr_Type
(Family
);
444 -- Socket addresses fully define a socket connection with protocol family,
445 -- an Internet address and a port. No_Sock_Addr provides a special value
446 -- for uninitialized socket addresses.
448 No_Sock_Addr
: constant Sock_Addr_Type
;
450 function Image
(Value
: Inet_Addr_Type
) return String;
451 -- Return an image of an Internet address. IPv4 notation consists in 4
452 -- octets in decimal format separated by dots. IPv6 notation consists in
453 -- 16 octets in hexadecimal format separated by colons (and possibly
456 function Image
(Value
: Sock_Addr_Type
) return String;
457 -- Return inet address image and port image separated by a colon
459 function Inet_Addr
(Image
: String) return Inet_Addr_Type
;
460 -- Convert address image from numbers-and-dots notation into an
463 -- Host entries provide complete information on a given host: the official
464 -- name, an array of alternative names or aliases and array of network
468 (Aliases_Length
, Addresses_Length
: Natural) is private;
470 function Official_Name
(E
: Host_Entry_Type
) return String;
471 -- Return official name in host entry
473 function Aliases_Length
(E
: Host_Entry_Type
) return Natural;
474 -- Return number of aliases in host entry
476 function Addresses_Length
(E
: Host_Entry_Type
) return Natural;
477 -- Return number of addresses in host entry
480 (E
: Host_Entry_Type
;
481 N
: Positive := 1) return String;
482 -- Return N'th aliases in host entry. The first index is 1
485 (E
: Host_Entry_Type
;
486 N
: Positive := 1) return Inet_Addr_Type
;
487 -- Return N'th addresses in host entry. The first index is 1
489 Host_Error
: exception;
490 -- Exception raised by the two following procedures. Once raised, its
491 -- message contains a string describing the error code. This exception is
492 -- raised when an host entry can not be retrieved.
494 function Get_Host_By_Address
495 (Address
: Inet_Addr_Type
;
496 Family
: Family_Type
:= Family_Inet
) return Host_Entry_Type
;
497 -- Return host entry structure for the given Inet address. Note that no
498 -- result will be returned if there is no mapping of this IP address to a
499 -- host name in the system tables (host database, DNS or otherwise).
501 function Get_Host_By_Name
502 (Name
: String) return Host_Entry_Type
;
503 -- Return host entry structure for the given host name. Here name is
504 -- either a host name, or an IP address. If Name is an IP address, this is
505 -- equivalent to Get_Host_By_Address (Inet_Addr (Name)).
507 function Host_Name
return String;
508 -- Return the name of the current host
510 type Service_Entry_Type
(Aliases_Length
: Natural) is private;
511 -- Service entries provide complete information on a given service: the
512 -- official name, an array of alternative names or aliases and the port
515 function Official_Name
(S
: Service_Entry_Type
) return String;
516 -- Return official name in service entry
518 function Port_Number
(S
: Service_Entry_Type
) return Port_Type
;
519 -- Return port number in service entry
521 function Protocol_Name
(S
: Service_Entry_Type
) return String;
522 -- Return Protocol in service entry (usually UDP or TCP)
524 function Aliases_Length
(S
: Service_Entry_Type
) return Natural;
525 -- Return number of aliases in service entry
528 (S
: Service_Entry_Type
;
529 N
: Positive := 1) return String;
530 -- Return N'th aliases in service entry (the first index is 1)
532 function Get_Service_By_Name
534 Protocol
: String) return Service_Entry_Type
;
535 -- Return service entry structure for the given service name
537 function Get_Service_By_Port
539 Protocol
: String) return Service_Entry_Type
;
540 -- Return service entry structure for the given service port number
542 Service_Error
: exception;
543 -- Comment required ???
545 -- Errors are described by an enumeration type. There is only one
546 -- exception Socket_Error in this package to deal with an error during a
547 -- socket routine. Once raised, its message contains the error code
548 -- between brackets and a string describing the error code.
550 -- The name of the enumeration constant documents the error condition
555 Address_Already_In_Use
,
556 Cannot_Assign_Requested_Address
,
557 Address_Family_Not_Supported_By_Protocol
,
558 Operation_Already_In_Progress
,
560 Software_Caused_Connection_Abort
,
562 Connection_Reset_By_Peer
,
563 Destination_Address_Required
,
567 Operation_Now_In_Progress
,
568 Interrupted_System_Call
,
571 Transport_Endpoint_Already_Connected
,
572 Too_Many_Symbolic_Links
,
577 Network_Dropped_Connection_Because_Of_Reset
,
578 Network_Is_Unreachable
,
579 No_Buffer_Space_Available
,
580 Protocol_Not_Available
,
581 Transport_Endpoint_Not_Connected
,
582 Socket_Operation_On_Non_Socket
,
583 Operation_Not_Supported
,
584 Protocol_Family_Not_Supported
,
585 Protocol_Not_Supported
,
586 Protocol_Wrong_Type_For_Socket
,
587 Cannot_Send_After_Transport_Endpoint_Shutdown
,
588 Socket_Type_Not_Supported
,
589 Connection_Timed_Out
,
591 Resource_Temporarily_Unavailable
,
593 Host_Name_Lookup_Failure
,
594 Non_Recoverable_Error
,
595 Unknown_Server_Error
,
596 Cannot_Resolve_Error
);
598 -- Get_Socket_Options and Set_Socket_Options manipulate options associated
599 -- with a socket. Options may exist at multiple protocol levels in the
600 -- communication stack. Socket_Level is the uppermost socket level.
604 IP_Protocol_For_IP_Level
,
605 IP_Protocol_For_UDP_Level
,
606 IP_Protocol_For_TCP_Level
);
608 -- There are several options available to manipulate sockets. Each option
609 -- has a name and several values available. Most of the time, the value is
610 -- a boolean to enable or disable this option.
612 type Option_Name
is (
613 Keep_Alive
, -- Enable sending of keep-alive messages
614 Reuse_Address
, -- Allow bind to reuse local address
615 Broadcast
, -- Enable datagram sockets to recv/send broadcast packets
616 Send_Buffer
, -- Set/get the maximum socket send buffer in bytes
617 Receive_Buffer
, -- Set/get the maximum socket recv buffer in bytes
618 Linger
, -- Shutdown wait for msg to be sent or timeout occur
619 Error
, -- Get and clear the pending socket error
620 No_Delay
, -- Do not delay send to coalesce packets (TCP_NODELAY)
621 Add_Membership
, -- Join a multicast group
622 Drop_Membership
, -- Leave a multicast group
623 Multicast_TTL
, -- Indicate the time-to-live of sent multicast packets
624 Multicast_Loop
); -- Sent multicast packets are looped to the local socket
626 type Option_Type
(Name
: Option_Name
:= Keep_Alive
) is record
650 when Add_Membership |
652 Multicast_Address
: Inet_Addr_Type
;
653 Local_Interface
: Inet_Addr_Type
;
655 when Multicast_TTL
=>
656 Time_To_Live
: Natural;
661 -- There are several controls available to manipulate sockets. Each option
662 -- has a name and several values available. These controls differ from the
663 -- socket options in that they are not specific to sockets but are
664 -- available for any device.
666 type Request_Name
is (
667 Non_Blocking_IO
, -- Cause a caller not to wait on blocking operations.
668 N_Bytes_To_Read
); -- Return the number of bytes available to read
670 type Request_Type
(Name
: Request_Name
:= Non_Blocking_IO
) is record
672 when Non_Blocking_IO
=>
675 when N_Bytes_To_Read
=>
681 -- A request flag allows to specify the type of message transmissions or
682 -- receptions. A request flag can be combination of zero or more
683 -- predefined request flags.
685 type Request_Flag_Type
is private;
687 No_Request_Flag
: constant Request_Flag_Type
;
688 -- This flag corresponds to the normal execution of an operation
690 Process_Out_Of_Band_Data
: constant Request_Flag_Type
;
691 -- This flag requests that the receive or send function operates on
692 -- out-of-band data when the socket supports this notion (e.g.
695 Peek_At_Incoming_Data
: constant Request_Flag_Type
;
696 -- This flag causes the receive operation to return data from the
697 -- beginning of the receive queue without removing that data from the
698 -- queue. A subsequent receive call will return the same data.
700 Wait_For_A_Full_Reception
: constant Request_Flag_Type
;
701 -- This flag requests that the operation block until the full request is
702 -- satisfied. However, the call may still return less data than requested
703 -- if a signal is caught, an error or disconnect occurs, or the next data
704 -- to be received is of a different type than that returned. Note that
705 -- this flag depends on support in the underlying sockets implementation,
706 -- and is not supported under Windows.
708 Send_End_Of_Record
: constant Request_Flag_Type
;
709 -- This flag indicates that the entire message has been sent and so this
710 -- terminates the record.
712 function "+" (L
, R
: Request_Flag_Type
) return Request_Flag_Type
;
713 -- Combine flag L with flag R
715 type Stream_Element_Reference
is access all Ada
.Streams
.Stream_Element
;
717 type Vector_Element
is record
718 Base
: Stream_Element_Reference
;
719 Length
: Ada
.Streams
.Stream_Element_Count
;
722 type Vector_Type
is array (Integer range <>) of Vector_Element
;
724 procedure Create_Socket
725 (Socket
: out Socket_Type
;
726 Family
: Family_Type
:= Family_Inet
;
727 Mode
: Mode_Type
:= Socket_Stream
);
728 -- Create an endpoint for communication. Raises Socket_Error on error
730 procedure Accept_Socket
731 (Server
: Socket_Type
;
732 Socket
: out Socket_Type
;
733 Address
: out Sock_Addr_Type
);
734 -- Extracts the first connection request on the queue of pending
735 -- connections, creates a new connected socket with mostly the same
736 -- properties as Server, and allocates a new socket. The returned Address
737 -- is filled in with the address of the connection. Raises Socket_Error on
740 procedure Bind_Socket
741 (Socket
: Socket_Type
;
742 Address
: Sock_Addr_Type
);
743 -- Once a socket is created, assign a local address to it. Raise
744 -- Socket_Error on error.
746 procedure Close_Socket
(Socket
: Socket_Type
);
747 -- Close a socket and more specifically a non-connected socket
749 procedure Connect_Socket
750 (Socket
: Socket_Type
;
751 Server
: in out Sock_Addr_Type
);
752 -- Make a connection to another socket which has the address of
753 -- Server. Raises Socket_Error on error.
755 procedure Control_Socket
756 (Socket
: Socket_Type
;
757 Request
: in out Request_Type
);
758 -- Obtain or set parameter values that control the socket. This control
759 -- differs from the socket options in that they are not specific to
760 -- sockets but are available for any device.
762 function Get_Peer_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
;
763 -- Return the peer or remote socket address of a socket. Raise
764 -- Socket_Error on error.
766 function Get_Socket_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
;
767 -- Return the local or current socket address of a socket. Return
768 -- No_Sock_Addr on error (for instance, socket closed or not locally
771 function Get_Socket_Option
772 (Socket
: Socket_Type
;
773 Level
: Level_Type
:= Socket_Level
;
774 Name
: Option_Name
) return Option_Type
;
775 -- Get the options associated with a socket. Raises Socket_Error
778 procedure Listen_Socket
779 (Socket
: Socket_Type
;
780 Length
: Positive := 15);
781 -- To accept connections, a socket is first created with Create_Socket,
782 -- a willingness to accept incoming connections and a queue Length for
783 -- incoming connections are specified. Raise Socket_Error on error.
785 procedure Receive_Socket
786 (Socket
: Socket_Type
;
787 Item
: out Ada
.Streams
.Stream_Element_Array
;
788 Last
: out Ada
.Streams
.Stream_Element_Offset
;
789 Flags
: Request_Flag_Type
:= No_Request_Flag
);
790 -- Receive message from Socket. Last is the index value such that Item
791 -- (Last) is the last character assigned. Note that Last is set to
792 -- Item'First - 1 when the socket has been closed by peer. This is not an
793 -- error and no exception is raised. Flags allows to control the
794 -- reception. Raise Socket_Error on error.
796 procedure Receive_Socket
797 (Socket
: Socket_Type
;
798 Item
: out Ada
.Streams
.Stream_Element_Array
;
799 Last
: out Ada
.Streams
.Stream_Element_Offset
;
800 From
: out Sock_Addr_Type
;
801 Flags
: Request_Flag_Type
:= No_Request_Flag
);
802 -- Receive message from Socket. If Socket is not connection-oriented, the
803 -- source address From of the message is filled in. Last is the index
804 -- value such that Item (Last) is the last character assigned. Flags
805 -- allows to control the reception. Raises Socket_Error on error.
807 procedure Receive_Vector
808 (Socket
: Socket_Type
;
809 Vector
: Vector_Type
;
810 Count
: out Ada
.Streams
.Stream_Element_Count
);
811 -- Receive data from a socket and scatter it into the set of vector
812 -- elements Vector. Count is set to the count of received stream elements.
814 function Resolve_Exception
815 (Occurrence
: Ada
.Exceptions
.Exception_Occurrence
) return Error_Type
;
816 -- When Socket_Error or Host_Error are raised, the exception message
817 -- contains the error code between brackets and a string describing the
818 -- error code. Resolve_Error extracts the error code from an exception
819 -- message and translate it into an enumeration value.
821 procedure Send_Socket
822 (Socket
: Socket_Type
;
823 Item
: Ada
.Streams
.Stream_Element_Array
;
824 Last
: out Ada
.Streams
.Stream_Element_Offset
;
825 Flags
: Request_Flag_Type
:= No_Request_Flag
);
826 -- Transmit a message to another socket. Note that Last is set to
827 -- Item'First-1 when socket has been closed by peer. This is not
828 -- considered an error and no exception is raised. Flags allows to control
829 -- the transmission. Raises Socket_Error on any other error condition.
831 procedure Send_Socket
832 (Socket
: Socket_Type
;
833 Item
: Ada
.Streams
.Stream_Element_Array
;
834 Last
: out Ada
.Streams
.Stream_Element_Offset
;
836 Flags
: Request_Flag_Type
:= No_Request_Flag
);
837 -- Transmit a message to another socket. The address is given by To. Flags
838 -- allows to control the transmission. Raises Socket_Error on error.
840 procedure Send_Vector
841 (Socket
: Socket_Type
;
842 Vector
: Vector_Type
;
843 Count
: out Ada
.Streams
.Stream_Element_Count
);
844 -- Transmit data gathered from the set of vector elements Vector to a
845 -- socket. Count is set to the count of transmitted stream elements.
847 procedure Set_Socket_Option
848 (Socket
: Socket_Type
;
849 Level
: Level_Type
:= Socket_Level
;
850 Option
: Option_Type
);
851 -- Manipulate socket options. Raises Socket_Error on error
853 procedure Shutdown_Socket
854 (Socket
: Socket_Type
;
855 How
: Shutmode_Type
:= Shut_Read_Write
);
856 -- Shutdown a connected socket. If How is Shut_Read, further receives will
857 -- be disallowed. If How is Shut_Write, further sends will be disallowed.
858 -- If how is Shut_Read_Write, further sends and receives will be
861 type Stream_Access
is access all Ada
.Streams
.Root_Stream_Type
'Class;
862 -- Same interface as Ada.Streams.Stream_IO
865 (Socket
: Socket_Type
) return Stream_Access
;
866 -- Create a stream associated with a stream-based socket that is
867 -- already connected.
870 (Socket
: Socket_Type
;
871 Send_To
: Sock_Addr_Type
) return Stream_Access
;
872 -- Create a stream associated with a datagram-based socket that is already
873 -- bound. Send_To is the socket address to which messages are being sent.
876 (Stream
: Stream_Access
) return Sock_Addr_Type
;
877 -- Return the socket address from which the last message was received
879 procedure Free
is new Ada
.Unchecked_Deallocation
880 (Ada
.Streams
.Root_Stream_Type
'Class, Stream_Access
);
881 -- Destroy a stream created by one of the Stream functions above,
882 -- releasing the corresponding resources. The user is responsible for
883 -- calling this subprogram when the stream is not needed anymore.
885 type Socket_Set_Type
is limited private;
886 -- This type allows to manipulate sets of sockets. It allows to wait for
887 -- events on multiple endpoints at one time. This is an access type on a
888 -- system dependent structure. To avoid memory leaks it is highly
889 -- recommended to clean the access value with procedure Empty.
891 procedure Clear
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
);
892 -- Remove Socket from Item
894 procedure Copy
(Source
: Socket_Set_Type
; Target
: in out Socket_Set_Type
);
895 -- Copy Source into Target as Socket_Set_Type is limited private
897 procedure Empty
(Item
: in out Socket_Set_Type
);
898 -- Remove all Sockets from Item and deallocate internal data
900 procedure Get
(Item
: in out Socket_Set_Type
; Socket
: out Socket_Type
);
901 -- Extract a Socket from socket set Item. Socket is set to
902 -- No_Socket when the set is empty.
905 (Item
: Socket_Set_Type
) return Boolean;
906 -- Return True iff Item is empty
909 (Item
: Socket_Set_Type
;
910 Socket
: Socket_Type
) return Boolean;
911 -- Return True iff Socket is present in Item
913 procedure Set
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
);
914 -- Insert Socket into Item
916 -- The select(2) system call waits for events to occur on any of a set of
917 -- file descriptors. Usually, three independent sets of descriptors are
918 -- watched (read, write and exception). A timeout gives an upper bound
919 -- on the amount of time elapsed before select returns. This function
920 -- blocks until an event occurs. On some platforms, the select(2) system
921 -- can block the full process (not just the calling thread).
923 -- Check_Selector provides the very same behaviour. The only difference is
924 -- that it does not watch for exception events. Note that on some
925 -- platforms it is kept process blocking on purpose. The timeout parameter
926 -- allows the user to have the behaviour he wants. Abort_Selector allows
927 -- to abort safely a Check_Selector that is blocked forever. A special
928 -- file descriptor is opened by Create_Selector and included in each call
929 -- to Check_Selector. Abort_Selector causes an event to occur on this
930 -- descriptor in order to unblock Check_Selector. The user must call
931 -- Close_Selector to discard this special file. A reason to abort a select
932 -- operation is typically to add a socket in one of the socket sets when
933 -- the timeout is set to forever.
935 type Selector_Type
is limited private;
936 type Selector_Access
is access all Selector_Type
;
938 -- Selector_Duration is a subtype of Standard.Duration because the full
939 -- range of Standard.Duration cannot be represented in the equivalent C
940 -- structure. Moreover, negative values are not allowed to avoid system
941 -- incompatibilities.
943 Immediate
: constant := 0.0;
944 Forever
: constant := Duration (Integer'Last) * 1.0;
946 subtype Selector_Duration
is Duration range Immediate
.. Forever
;
948 procedure Create_Selector
(Selector
: out Selector_Type
);
949 -- Create a new selector
951 procedure Close_Selector
(Selector
: in out Selector_Type
);
952 -- Close Selector and all internal descriptors associated
954 type Selector_Status
is (Completed
, Expired
, Aborted
);
956 procedure Check_Selector
957 (Selector
: in out Selector_Type
;
958 R_Socket_Set
: in out Socket_Set_Type
;
959 W_Socket_Set
: in out Socket_Set_Type
;
960 Status
: out Selector_Status
;
961 Timeout
: Selector_Duration
:= Forever
);
962 -- Return when one Socket in R_Socket_Set has some data to be read or if
963 -- one Socket in W_Socket_Set is ready to transmit some data. In these
964 -- cases Status is set to Completed and sockets that are ready are set in
965 -- R_Socket_Set or W_Socket_Set. Status is set to Expired if no socket was
966 -- ready after a Timeout expiration. Status is set to Aborted if an abort
967 -- signal has been received while checking socket status. As this
968 -- procedure returns when Timeout occurs, it is a design choice to keep
969 -- this procedure process blocking. Note that a Timeout of 0.0 returns
970 -- immediately. Also note that two different Socket_Set_Type objects must
971 -- be passed as R_Socket_Set and W_Socket_Set (even if they denote the
972 -- same set of Sockets), or some event will be lost.
973 -- Socket_Error is raised when the select(2) system call returns an
974 -- error condition, or when a read error occurs on the signalling socket
975 -- used for the implementation of Abort_Selector.
977 procedure Check_Selector
978 (Selector
: in out Selector_Type
;
979 R_Socket_Set
: in out Socket_Set_Type
;
980 W_Socket_Set
: in out Socket_Set_Type
;
981 E_Socket_Set
: in out Socket_Set_Type
;
982 Status
: out Selector_Status
;
983 Timeout
: Selector_Duration
:= Forever
);
984 -- This refined version of Check_Selector allows to watch for exception
985 -- events (that is notifications of out-of-band transmission and
986 -- reception). As above, all of R_Socket_Set, W_Socket_Set and
987 -- E_Socket_Set must be different objects.
989 procedure Abort_Selector
(Selector
: Selector_Type
);
990 -- Send an abort signal to the selector
994 type Socket_Type
is new Integer;
995 No_Socket
: constant Socket_Type
:= -1;
997 type Selector_Type
is limited record
998 R_Sig_Socket
: Socket_Type
;
999 W_Sig_Socket
: Socket_Type
;
1002 pragma Volatile
(Selector_Type
);
1004 -- The two signalling sockets are used to abort a select operation
1006 subtype Socket_Set_Access
is System
.Address
;
1007 No_Socket_Set
: constant Socket_Set_Access
:= System
.Null_Address
;
1009 type Socket_Set_Type
is record
1010 Last
: Socket_Type
:= No_Socket
;
1011 Set
: Socket_Set_Access
:= No_Socket_Set
;
1014 subtype Inet_Addr_Comp_Type
is Natural range 0 .. 255;
1015 -- Octet for Internet address
1017 type Inet_Addr_VN_Type
is array (Natural range <>) of Inet_Addr_Comp_Type
;
1019 subtype Inet_Addr_V4_Type
is Inet_Addr_VN_Type
(1 .. 4);
1020 subtype Inet_Addr_V6_Type
is Inet_Addr_VN_Type
(1 .. 16);
1022 type Inet_Addr_Type
(Family
: Family_Type
:= Family_Inet
) is record
1025 Sin_V4
: Inet_Addr_V4_Type
:= (others => 0);
1027 when Family_Inet6
=>
1028 Sin_V6
: Inet_Addr_V6_Type
:= (others => 0);
1032 Any_Port
: constant Port_Type
:= 0;
1033 No_Port
: constant Port_Type
:= 0;
1035 Any_Inet_Addr
: constant Inet_Addr_Type
:=
1036 (Family_Inet
, (others => 0));
1037 No_Inet_Addr
: constant Inet_Addr_Type
:=
1038 (Family_Inet
, (others => 0));
1039 Broadcast_Inet_Addr
: constant Inet_Addr_Type
:=
1040 (Family_Inet
, (others => 255));
1042 No_Sock_Addr
: constant Sock_Addr_Type
:= (Family_Inet
, No_Inet_Addr
, 0);
1044 Max_Name_Length
: constant := 64;
1045 -- The constant MAXHOSTNAMELEN is usually set to 64
1047 subtype Name_Index
is Natural range 1 .. Max_Name_Length
;
1050 (Length
: Name_Index
:= Max_Name_Length
)
1052 Name
: String (1 .. Length
);
1054 -- We need fixed strings to avoid access types in host entry type
1056 type Name_Array
is array (Natural range <>) of Name_Type
;
1057 type Inet_Addr_Array
is array (Natural range <>) of Inet_Addr_Type
;
1059 type Host_Entry_Type
(Aliases_Length
, Addresses_Length
: Natural) is record
1060 Official
: Name_Type
;
1061 Aliases
: Name_Array
(1 .. Aliases_Length
);
1062 Addresses
: Inet_Addr_Array
(1 .. Addresses_Length
);
1065 type Service_Entry_Type
(Aliases_Length
: Natural) is record
1066 Official
: Name_Type
;
1067 Aliases
: Name_Array
(1 .. Aliases_Length
);
1069 Protocol
: Name_Type
;
1072 type Request_Flag_Type
is mod 2 ** 8;
1073 No_Request_Flag
: constant Request_Flag_Type
:= 0;
1074 Process_Out_Of_Band_Data
: constant Request_Flag_Type
:= 1;
1075 Peek_At_Incoming_Data
: constant Request_Flag_Type
:= 2;
1076 Wait_For_A_Full_Reception
: constant Request_Flag_Type
:= 4;
1077 Send_End_Of_Record
: constant Request_Flag_Type
:= 8;