1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
10 -- Copyright (C) 2001-2002 Ada Core Technologies, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 ------------------------------------------------------------------------------
34 -- This package provides an interface to the sockets communication facility
35 -- provided on many operating systems. Currently this is implemented on all
36 -- native GNAT ports except for VMS. It is not yet implemented on the Lynx
39 -- Another restriction is that there is no multicast support under Windows
40 -- or under any system on which the multicast support is not available or
46 package GNAT
.Sockets
is
48 -- Sockets are designed to provide a consistent communication facility
49 -- between applications. This package provides an Ada-like intrerface
50 -- similar to that proposed as part of the BSD socket layer. This is a
51 -- system independent thick binding.
53 -- Here is a typical example of what you can do:
55 -- with GNAT.Sockets; use GNAT.Sockets;
58 -- with Ada.Exceptions; use Ada.Exceptions;
60 -- procedure PingPong is
62 -- Group : constant String := "239.255.128.128";
63 -- -- Multicast groupe: administratively scoped IP address
71 -- Address : Sock_Addr_Type;
72 -- Server : Socket_Type;
73 -- Socket : Socket_Type;
74 -- Channel : Stream_Access;
79 -- -- Get an Internet address of a host (here the local host name).
80 -- -- Note that a host can have several addresses. Here we get
81 -- -- the first one which is supposed to be the official one.
83 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
85 -- -- Get a socket address that is an Internet address and a port
87 -- Address.Port := 5432;
89 -- -- The first step is to create a socket. Once created, this
90 -- -- socket must be associated to with an address. Usually only
91 -- -- a server (Pong here) needs to bind an address explicitly.
92 -- -- Most of the time clients can skip this step because the
93 -- -- socket routines will bind an arbitrary address to an unbound
96 -- Create_Socket (Server);
98 -- -- Allow reuse of local addresses.
103 -- (Reuse_Address, True));
105 -- Bind_Socket (Server, Address);
107 -- -- A server marks a socket as willing to receive connect events.
109 -- Listen_Socket (Server);
111 -- -- Once a server calls Listen_Socket, incoming connects events
112 -- -- can be accepted. The returned Socket is a new socket that
113 -- -- represents the server side of the connection. Server remains
114 -- -- available to receive further connections.
116 -- Accept_Socket (Server, Socket, Address);
118 -- -- Return a stream associated to the connected socket.
120 -- Channel := Stream (Socket);
122 -- -- Force Pong to block
126 -- -- Receive and print message from client Ping.
129 -- Message : String := String'Input (Channel);
132 -- Ada.Text_IO.Put_Line (Message);
134 -- -- Send same message to server Pong.
136 -- String'Output (Channel, Message);
139 -- Close_Socket (Server);
140 -- Close_Socket (Socket);
142 -- -- Part of the multicast example
144 -- -- Create a datagram socket to send connectionless, unreliable
145 -- -- messages of a fixed maximum length.
147 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
149 -- -- Allow reuse of local addresses.
154 -- (Reuse_Address, True));
156 -- -- Join a multicast group.
160 -- IP_Protocol_For_IP_Level,
161 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
163 -- -- Controls the live time of the datagram to avoid it being
164 -- -- looped forever due to routing errors. Routers decrement
165 -- -- the TTL of every datagram as it traverses from one network
166 -- -- to another and when its value reaches 0 the packet is
167 -- -- dropped. Default is 1.
171 -- IP_Protocol_For_IP_Level,
172 -- (Multicast_TTL, 1));
174 -- -- Want the data you send to be looped back to your host.
178 -- IP_Protocol_For_IP_Level,
179 -- (Multicast_Loop, True));
181 -- -- If this socket is intended to receive messages, bind it to a
182 -- -- given socket address.
184 -- Address.Addr := Any_Inet_Addr;
185 -- Address.Port := 55505;
187 -- Bind_Socket (Socket, Address);
189 -- -- If this socket is intended to send messages, provide the
190 -- -- receiver socket address.
192 -- Address.Addr := Inet_Addr (Group);
193 -- Address.Port := 55506;
195 -- Channel := Stream (Socket, Address);
197 -- -- Receive and print message from client Ping.
200 -- Message : String := String'Input (Channel);
204 -- -- Get the address of the sender.
206 -- Address := Get_Address (Channel);
207 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
209 -- -- Send same message to server Pong.
211 -- String'Output (Channel, Message);
214 -- Close_Socket (Socket);
218 -- exception when E : others =>
219 -- Ada.Text_IO.Put_Line
220 -- (Exception_Name (E) & ": " & Exception_Message (E));
229 -- Address : Sock_Addr_Type;
230 -- Socket : Socket_Type;
231 -- Channel : Stream_Access;
236 -- -- See comments in Ping section for the first steps.
238 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
239 -- Address.Port := 5432;
240 -- Create_Socket (Socket);
245 -- (Reuse_Address, True));
247 -- -- Force Pong to block
251 -- -- If the client's socket is not bound, Connect_Socket will
252 -- -- bind to an unused address. The client uses Connect_Socket to
253 -- -- create a logical connection between the client's socket and
254 -- -- a server's socket returned by Accept_Socket.
256 -- Connect_Socket (Socket, Address);
258 -- Channel := Stream (Socket);
260 -- -- Send message to server Pong.
262 -- String'Output (Channel, "Hello world");
264 -- -- Force Ping to block
268 -- -- Receive and print message from server Pong.
270 -- Ada.Text_IO.Put_Line (String'Input (Channel));
271 -- Close_Socket (Socket);
273 -- -- Part of multicast example. Code similar to Pong's one.
275 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
280 -- (Reuse_Address, True));
284 -- IP_Protocol_For_IP_Level,
285 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
289 -- IP_Protocol_For_IP_Level,
290 -- (Multicast_TTL, 1));
294 -- IP_Protocol_For_IP_Level,
295 -- (Multicast_Loop, True));
297 -- Address.Addr := Any_Inet_Addr;
298 -- Address.Port := 55506;
300 -- Bind_Socket (Socket, Address);
302 -- Address.Addr := Inet_Addr (Group);
303 -- Address.Port := 55505;
305 -- Channel := Stream (Socket, Address);
307 -- -- Send message to server Pong.
309 -- String'Output (Channel, "Hello world");
311 -- -- Receive and print message from server Pong.
314 -- Message : String := String'Input (Channel);
317 -- Address := Get_Address (Channel);
318 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
321 -- Close_Socket (Socket);
325 -- exception when E : others =>
326 -- Ada.Text_IO.Put_Line
327 -- (Exception_Name (E) & ": " & Exception_Message (E));
331 -- -- Indicate whether the thread library provides process
332 -- -- blocking IO. Basically, if you are not using FSU threads
333 -- -- the default is ok.
335 -- Initialize (Process_Blocking_IO => False);
343 procedure Initialize
(Process_Blocking_IO
: Boolean := False);
344 -- Initialize must be called before using any socket routines. If
345 -- the thread library provides process blocking IO - basically
346 -- with FSU threads - GNAT.Sockets should be initialized with a
347 -- value of True to simulate thread blocking IO. Further calls to
348 -- Initialize will be ignored.
351 -- After Finalize is called it is not possible to use any routines
352 -- exported in by this package. This procedure is idempotent.
354 type Socket_Type
is private;
355 -- Sockets are used to implement a reliable bi-directional
356 -- point-to-point, stream-based connections between
357 -- hosts. No_Socket provides a special value to denote
358 -- uninitialized sockets.
360 No_Socket
: constant Socket_Type
;
362 Socket_Error
: exception;
363 -- There is only one exception in this package to deal with an
364 -- error during a socket routine. Once raised, its message
365 -- contains a string describing the error code.
367 function Image
(Socket
: Socket_Type
) return String;
368 -- Return a printable string for Socket
370 function To_C
(Socket
: Socket_Type
) return Integer;
371 -- Return a file descriptor to be used by external subprograms
372 -- especially the C functions that are not yet interfaced in this
375 type Family_Type
is (Family_Inet
, Family_Inet6
);
376 -- Address family (or protocol family) identifies the
377 -- communication domain and groups protocols with similar address
378 -- formats. IPv6 will soon be supported.
380 type Mode_Type
is (Socket_Stream
, Socket_Datagram
);
381 -- Stream sockets provide connection-oriented byte
382 -- streams. Datagram sockets support unreliable connectionless
383 -- message based communication.
385 type Shutmode_Type
is (Shut_Read
, Shut_Write
, Shut_Read_Write
);
386 -- When a process closes a socket, the policy is to retain any
387 -- data queued until either a delivery or a timeout expiration (in
388 -- this case, the data are discarded). A finer control is
389 -- available through shutdown. With Shut_Read, no more data can be
390 -- received from the socket. With_Write, no more data can be
391 -- transmitted. Neither transmission nor reception can be
392 -- performed with Shut_Read_Write.
394 type Port_Type
is new Natural;
395 -- Classical port definition. No_Port provides a special value to
396 -- denote uninitialized port. Any_Port provides a special value
397 -- enabling all ports.
399 Any_Port
: constant Port_Type
;
400 No_Port
: constant Port_Type
;
402 type Inet_Addr_Type
(Family
: Family_Type
:= Family_Inet
) is private;
403 -- An Internet address depends on an address family (IPv4 contains
404 -- 4 octets and Ipv6 contains 16 octets). Any_Inet_Address is a
405 -- special value treated like a wildcard enabling all addresses.
406 -- No_Inet_Addr provides a special value to denote uninitialized
409 Any_Inet_Addr
: constant Inet_Addr_Type
;
410 No_Inet_Addr
: constant Inet_Addr_Type
;
412 type Sock_Addr_Type
(Family
: Family_Type
:= Family_Inet
) is record
413 Addr
: Inet_Addr_Type
(Family
);
416 -- Socket addresses fully define a socket connection with a
417 -- protocol family, an Internet address and a port. No_Sock_Addr
418 -- provides a special value for uninitialized socket addresses.
420 No_Sock_Addr
: constant Sock_Addr_Type
;
422 function Image
(Value
: Inet_Addr_Type
) return String;
423 -- Return an image of an Internet address. IPv4 notation consists
424 -- in 4 octets in decimal format separated by dots. IPv6 notation
425 -- consists in 16 octets in hexadecimal format separated by
426 -- colons (and possibly dots).
428 function Image
(Value
: Sock_Addr_Type
) return String;
429 -- Return inet address image and port image separated by a colon.
431 function Inet_Addr
(Image
: String) return Inet_Addr_Type
;
432 -- Convert address image from numbers-and-dots notation into an
435 -- Host entries provide a complete information on a given host:
436 -- the official name, an array of alternative names or aliases and
437 -- array of network addresses.
440 (Aliases_Length
, Addresses_Length
: Natural) is private;
442 function Official_Name
(E
: Host_Entry_Type
) return String;
443 -- Return official name in host entry
445 function Aliases_Length
(E
: Host_Entry_Type
) return Natural;
446 -- Return number of aliases in host entry
448 function Addresses_Length
(E
: Host_Entry_Type
) return Natural;
449 -- Return number of addresses in host entry
452 (E
: Host_Entry_Type
;
455 -- Return N'th aliases in host entry. The first index is 1.
458 (E
: Host_Entry_Type
;
460 return Inet_Addr_Type
;
461 -- Return N'th addresses in host entry. The first index is 1.
463 Host_Error
: exception;
464 -- Exception raised by the two following procedures. Once raised,
465 -- its message contains a string describing the error code. This
466 -- exception is raised when an host entry can not be retrieved.
468 function Get_Host_By_Address
469 (Address
: Inet_Addr_Type
;
470 Family
: Family_Type
:= Family_Inet
)
471 return Host_Entry_Type
;
472 -- Return host entry structure for the given inet address
474 function Get_Host_By_Name
476 return Host_Entry_Type
;
477 -- Return host entry structure for the given host name
479 function Host_Name
return String;
480 -- Return the name of the current host
482 -- Errors are described by an enumeration type. There is only one
483 -- exception Socket_Error in this package to deal with an error
484 -- during a socket routine. Once raised, its message contains the
485 -- error code between brackets and a string describing the error code.
487 -- The name of the enumeration constant documents the error condition.
491 Address_Already_In_Use
,
492 Cannot_Assign_Requested_Address
,
493 Address_Family_Not_Supported_By_Protocol
,
494 Operation_Already_In_Progress
,
498 Operation_Now_In_Progress
,
499 Interrupted_System_Call
,
502 Transport_Endpoint_Already_Connected
,
504 Network_Is_Unreachable
,
505 No_Buffer_Space_Available
,
506 Protocol_Not_Available
,
507 Transport_Endpoint_Not_Connected
,
508 Operation_Not_Supported
,
509 Protocol_Not_Supported
,
510 Socket_Type_Not_Supported
,
511 Connection_Timed_Out
,
512 Resource_Temporarily_Unavailable
,
514 Host_Name_Lookup_Failure
,
515 No_Address_Associated_With_Name
,
516 Unknown_Server_Error
,
517 Cannot_Resolve_Error
);
519 -- Get_Socket_Options and Set_Socket_Options manipulate options
520 -- associated with a socket. Options may exist at multiple
521 -- protocol levels in the communication stack. Socket_Level is the
522 -- uppermost socket level.
526 IP_Protocol_For_IP_Level
,
527 IP_Protocol_For_UDP_Level
,
528 IP_Protocol_For_TCP_Level
);
530 -- There are several options available to manipulate sockets. Each
531 -- option has a name and several values available. Most of the
532 -- time, the value is a boolean to enable or disable this option.
534 type Option_Name
is (
535 Keep_Alive
, -- Enable sending of keep-alive messages
536 Reuse_Address
, -- Allow bind to reuse local address
537 Broadcast
, -- Enable datagram sockets to recv/send broadcast packets
538 Send_Buffer
, -- Set/get the maximum socket send buffer in bytes
539 Receive_Buffer
, -- Set/get the maximum socket recv buffer in bytes
540 Linger
, -- Shutdown wait for msg to be sent or timeout occur
541 Error
, -- Get and clear the pending socket error
542 No_Delay
, -- Do not delay send to coalesce packets (TCP_NODELAY)
543 Add_Membership
, -- Join a multicast group
544 Drop_Membership
, -- Leave a multicast group
545 Multicast_TTL
, -- Indicates the time-to-live of sent multicast packets
546 Multicast_Loop
); -- Sent multicast packets are looped to the local socket
548 type Option_Type
(Name
: Option_Name
:= Keep_Alive
) is record
572 when Add_Membership |
574 Multiaddr
: Inet_Addr_Type
;
575 Interface
: Inet_Addr_Type
;
577 when Multicast_TTL
=>
578 Time_To_Live
: Natural;
583 -- There are several controls available to manipulate
584 -- sockets. Each option has a name and several values available.
585 -- These controls differ from the socket options in that they are
586 -- not specific to sockets but are available for any device.
588 type Request_Name
is (
589 Non_Blocking_IO
, -- Cause a caller not to wait on blocking operations.
590 N_Bytes_To_Read
); -- Return the number of bytes available to read
592 type Request_Type
(Name
: Request_Name
:= Non_Blocking_IO
) is record
594 when Non_Blocking_IO
=>
597 when N_Bytes_To_Read
=>
603 procedure Create_Socket
604 (Socket
: out Socket_Type
;
605 Family
: Family_Type
:= Family_Inet
;
606 Mode
: Mode_Type
:= Socket_Stream
);
607 -- Create an endpoint for communication. Raise Socket_Error on error.
609 procedure Accept_Socket
610 (Server
: Socket_Type
;
611 Socket
: out Socket_Type
;
612 Address
: out Sock_Addr_Type
);
613 -- Extract the first connection request on the queue of pending
614 -- connections, creates a new connected socket with mostly the
615 -- same properties as Server, and allocates a new socket. The
616 -- returned Address is filled in with the address of the
617 -- connection. Raise Socket_Error on error.
619 procedure Bind_Socket
620 (Socket
: Socket_Type
;
621 Address
: Sock_Addr_Type
);
622 -- Once a socket is created, assign a local address to it. Raise
623 -- Socket_Error on error.
625 procedure Close_Socket
(Socket
: Socket_Type
);
626 -- Close a socket and more specifically a non-connected socket.
628 procedure Connect_Socket
629 (Socket
: Socket_Type
;
630 Server
: in out Sock_Addr_Type
);
631 -- Make a connection to another socket which has the address of
632 -- Server. Raise Socket_Error on error.
634 procedure Control_Socket
635 (Socket
: Socket_Type
;
636 Request
: in out Request_Type
);
637 -- Obtain or set parameter values that control the socket. This
638 -- control differs from the socket options in that they are not
639 -- specific to sockets but are avaiable for any device.
641 function Get_Peer_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
;
642 -- Return the peer or remote socket address of a socket. Raise
643 -- Socket_Error on error.
645 function Get_Socket_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
;
646 -- Return the local or current socket address of a socket. Raise
647 -- Socket_Error on error.
649 function Get_Socket_Option
650 (Socket
: Socket_Type
;
651 Level
: Level_Type
:= Socket_Level
;
654 -- Get the options associated with a socket. Raise Socket_Error on
657 procedure Listen_Socket
658 (Socket
: Socket_Type
;
659 Length
: Positive := 15);
660 -- To accept connections, a socket is first created with
661 -- Create_Socket, a willingness to accept incoming connections and
662 -- a queue Length for incoming connections are specified. Raise
663 -- Socket_Error on error.
665 procedure Receive_Socket
666 (Socket
: Socket_Type
;
667 Item
: out Ada
.Streams
.Stream_Element_Array
;
668 Last
: out Ada
.Streams
.Stream_Element_Offset
);
669 -- Receive message from Socket. Last is the index value such that
670 -- Item (Last) is the last character assigned. Note that Last is
671 -- set to Item'First - 1 when the socket has been closed by
672 -- peer. This is not an error and no exception is raised. Raise
673 -- Socket_Error on error.
675 procedure Receive_Socket
676 (Socket
: Socket_Type
;
677 Item
: out Ada
.Streams
.Stream_Element_Array
;
678 Last
: out Ada
.Streams
.Stream_Element_Offset
;
679 From
: out Sock_Addr_Type
);
680 -- Receive message from Socket. If Socket is not
681 -- connection-oriented, the source address From of the message is
682 -- filled in. Last is the index value such that Item (Last) is the
683 -- last character assigned. Raise Socket_Error on error.
685 function Resolve_Exception
686 (Occurrence
: Ada
.Exceptions
.Exception_Occurrence
)
688 -- When Socket_Error or Host_Error are raised, the exception
689 -- message contains the error code between brackets and a string
690 -- describing the error code. Resolve_Error extracts the error
691 -- code from an exception message and translate it into an
692 -- enumeration value.
694 procedure Send_Socket
695 (Socket
: Socket_Type
;
696 Item
: Ada
.Streams
.Stream_Element_Array
;
697 Last
: out Ada
.Streams
.Stream_Element_Offset
);
698 -- Transmit a message to another socket. Note that Last is set to
699 -- Item'First when socket has been closed by peer. This is not an
700 -- error and no exception is raised. Raise Socket_Error on error;
702 procedure Send_Socket
703 (Socket
: Socket_Type
;
704 Item
: Ada
.Streams
.Stream_Element_Array
;
705 Last
: out Ada
.Streams
.Stream_Element_Offset
;
706 To
: Sock_Addr_Type
);
707 -- Transmit a message to another socket. The address is given by
708 -- To. Raise Socket_Error on error;
710 procedure Set_Socket_Option
711 (Socket
: Socket_Type
;
712 Level
: Level_Type
:= Socket_Level
;
713 Option
: Option_Type
);
714 -- Manipulate socket options. Raise Socket_Error on error.
716 procedure Shutdown_Socket
717 (Socket
: Socket_Type
;
718 How
: Shutmode_Type
:= Shut_Read_Write
);
719 -- Shutdown a connected socket. If How is Shut_Read, further
720 -- receives will be disallowed. If How is Shut_Write, further
721 -- sends will be disallowed. If how is Shut_Read_Write, further
722 -- sends and receives will be disallowed.
724 type Stream_Access
is access all Ada
.Streams
.Root_Stream_Type
'Class;
725 -- Same interface as Ada.Streams.Stream_IO
728 (Socket
: Socket_Type
)
729 return Stream_Access
;
730 -- Associate a stream with a stream-based socket that is already
734 (Socket
: Socket_Type
;
735 Send_To
: Sock_Addr_Type
)
736 return Stream_Access
;
737 -- Associate a stream with a datagram-based socket that is already
738 -- bound. Send_To is the socket address to which messages are
742 (Stream
: Stream_Access
)
743 return Sock_Addr_Type
;
744 -- Return the socket address from which the last message was
747 type Socket_Set_Type
is private;
748 -- This type allows to manipulate sets of sockets. It allows to
749 -- wait for events on multiple endpoints at one time. This is an
750 -- access type on a system dependent structure. To avoid memory
751 -- leaks it is highly recommended to clean the access value with
754 procedure Clear
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
);
755 -- Remove Socket from Item
757 procedure Set
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
);
758 -- Insert Socket into Item
760 procedure Empty
(Item
: in out Socket_Set_Type
);
761 -- Remove all Sockets from Item and deallocate internal data
764 (Item
: Socket_Set_Type
)
766 -- Return True if Item is empty
769 (Item
: Socket_Set_Type
;
770 Socket
: Socket_Type
)
772 -- Return True if Socket is present in Item
774 -- C select() waits for a number of file descriptors to change
775 -- status. Usually, three independent sets of descriptors are
776 -- watched (read, write and exception). A timeout gives an upper
777 -- bound on the amount of time elapsed before select returns.
778 -- This function blocks until an event occurs. On some platforms,
779 -- C select can block the full process.
781 -- Check_Selector provides the very same behaviour. The only
782 -- difference is that it does not watch for exception events. Note
783 -- that on some platforms it is kept process blocking in purpose.
784 -- The timeout parameter allows the user to have the behaviour he
785 -- wants. Abort_Selector allows to abort safely a Check_Selector
786 -- that is blocked forever. A special file descriptor is opened by
787 -- Create_Selector and included in each call to
788 -- Check_Selector. Abort_Selector causes an event to occur on this
789 -- descriptor in order to unblock Check_Selector. The user must
790 -- call Close_Selector to discard this special file. A reason to
791 -- abort a select operation is typically to add a socket in one of
792 -- the socket sets when the timeout is set to forever.
794 Forever
: constant Duration;
796 type Selector_Type
is limited private;
797 type Selector_Access
is access all Selector_Type
;
799 procedure Create_Selector
(Selector
: out Selector_Type
);
800 -- Create a new selector
802 procedure Close_Selector
(Selector
: in out Selector_Type
);
803 -- Close Selector and all internal descriptors associated
805 type Selector_Status
is (Completed
, Expired
, Aborted
);
807 procedure Check_Selector
808 (Selector
: in out Selector_Type
;
809 R_Socket_Set
: in out Socket_Set_Type
;
810 W_Socket_Set
: in out Socket_Set_Type
;
811 Status
: out Selector_Status
;
812 Timeout
: Duration := Forever
);
813 -- Return when one Socket in R_Socket_Set has some data to be read
814 -- or if one Socket in W_Socket_Set is ready to receive some
815 -- data. In these cases Status is set to Completed and sockets
816 -- that are ready are set in R_Socket_Set or W_Socket_Set. Status
817 -- is set to Expired if no socket was ready after a Timeout
818 -- expiration. Status is set to Aborted if an abort signal has been
819 -- received while checking socket status. As this procedure
820 -- returns when Timeout occurs, it is a design choice to keep this
821 -- procedure process blocking. Note that a Timeout of 0.0 returns
824 procedure Abort_Selector
(Selector
: Selector_Type
);
825 -- Send an abort signal to the selector.
829 type Socket_Type
is new Integer;
830 No_Socket
: constant Socket_Type
:= -1;
832 Forever
: constant Duration := Duration'Last;
834 type Selector_Type
is limited record
835 R_Sig_Socket
: Socket_Type
;
836 W_Sig_Socket
: Socket_Type
;
837 In_Progress
: Boolean := False;
839 -- The two signalling sockets are used to abort a select
842 type Socket_Set_Record
;
843 type Socket_Set_Type
is access all Socket_Set_Record
;
845 subtype Inet_Addr_Comp_Type
is Natural range 0 .. 255;
846 -- Octet for Internet address
848 type Inet_Addr_VN_Type
is array (Natural range <>) of Inet_Addr_Comp_Type
;
850 subtype Inet_Addr_V4_Type
is Inet_Addr_VN_Type
(1 .. 4);
851 subtype Inet_Addr_V6_Type
is Inet_Addr_VN_Type
(1 .. 16);
853 type Inet_Addr_Type
(Family
: Family_Type
:= Family_Inet
) is record
856 Sin_V4
: Inet_Addr_V4_Type
:= (others => 0);
859 Sin_V6
: Inet_Addr_V6_Type
:= (others => 0);
863 Any_Port
: constant Port_Type
:= 0;
864 No_Port
: constant Port_Type
:= 0;
866 Any_Inet_Addr
: constant Inet_Addr_Type
:= (Family_Inet
, (others => 0));
867 No_Inet_Addr
: constant Inet_Addr_Type
:= (Family_Inet
, (others => 0));
869 No_Sock_Addr
: constant Sock_Addr_Type
:= (Family_Inet
, No_Inet_Addr
, 0);
871 Max_Host_Name_Length
: constant := 64;
872 -- The constant MAXHOSTNAMELEN is usually set to 64
874 subtype Host_Name_Index
is Natural range 1 .. Max_Host_Name_Length
;
877 (Length
: Host_Name_Index
:= Max_Host_Name_Length
)
879 Name
: String (1 .. Length
);
881 -- We need fixed strings to avoid access types in host entry type
883 type Host_Name_Array
is array (Natural range <>) of Host_Name_Type
;
884 type Inet_Addr_Array
is array (Natural range <>) of Inet_Addr_Type
;
886 type Host_Entry_Type
(Aliases_Length
, Addresses_Length
: Natural) is record
887 Official
: Host_Name_Type
;
888 Aliases
: Host_Name_Array
(1 .. Aliases_Length
);
889 Addresses
: Inet_Addr_Array
(1 .. Addresses_Length
);