OSDN Git Service

PR 33870
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socket.ads
index f78241c..cbbb133 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---              Copyright (C) 2001-2003 Ada Core Technologies, Inc.         --
+--                     Copyright (C) 2001-2007, AdaCore                     --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -16,8 +16,8 @@
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, USA.                                              --
 --                                                                          --
 -- As a special exception,  if other files  instantiate  generics from this --
 -- unit, or you link  this unit with other files  to produce an executable, --
 --                                                                          --
 ------------------------------------------------------------------------------
 
---  This package provides an interface to the sockets communication
---  facility provided on many operating systems. This is implemented
---  on the following platforms:
+--  This package provides an interface to the sockets communication facility
+--  provided on many operating systems. This is implemented on the following
+--  platforms:
 
---     All native ports, except Interix, with restrictions as follows
+--     All native ports, with restrictions as follows
 
---       Multicast is available only on systems which provide support
---       for this feature, so it is not available if Multicast is not
---       supported, or not installed. In particular Multicast is not
---       available with the Windows version.
+--       Multicast is available only on systems which provide support for this
+--       feature, so it is not available if Multicast is not supported, or not
+--       installed. In particular Multicast is not available with the Windows
+--       version.
 
---       The VMS implementation has implemented using the DECC RTL Socket
---       API, and is thus subject to limitations in the implementation of
---       this API.
+--       The VMS implementation was implemented using the DECC RTL Socket API,
+--       and is thus subject to limitations in the implementation of this API.
 
---       This package is not supported on the Interix port of GNAT.
+--     VxWorks cross ports fully implement this package
 
---     VxWorks cross ports fully implement this package.
-
---     This package is not yet implemented on LynxOS.
+--     This package is not yet implemented on LynxOS or other cross ports
 
 with Ada.Exceptions;
 with Ada.Streams;
+with Ada.Unchecked_Deallocation;
 
 with System;
 
@@ -63,24 +61,22 @@ package GNAT.Sockets is
    --  between applications. This package provides an Ada-like interface
    --  similar to that proposed as part of the BSD socket layer.
 
-   --  GNAT.Sockets has been designed with several ideas in mind.
+   --  GNAT.Sockets has been designed with several ideas in mind
 
-   --  This is a system independent interface. Therefore, we try as
-   --  much as possible to mask system incompatibilities. Some
-   --  functionalities are not available because there are not fully
-   --  supported on some systems.
+   --  This is a system independent interface. Therefore, we try as much as
+   --  possible to mask system incompatibilities. Some functionalities are not
+   --  available because there are not fully supported on some systems.
 
-   --  This is a thick binding. For instance, a major effort has been
-   --  done to avoid using memory addresses or untyped ints. We
-   --  preferred to define streams and enumeration types. Errors are
-   --  not returned as returned values but as exceptions.
+   --  This is a thick binding. For instance, a major effort has been done to
+   --  avoid using memory addresses or untyped ints. We preferred to define
+   --  streams and enumeration types. Errors are not returned as returned
+   --  values but as exceptions.
 
    --  This package provides a POSIX-compliant interface (between two
-   --  different implementations of the same routine, we adopt the one
-   --  closest to the POSIX specification). For instance, using
-   --  select(), the notification of an asynchronous connect failure
-   --  is delivered in the write socket set (POSIX) instead of the
-   --  exception socket set (NT).
+   --  different implementations of the same routine, we adopt the one closest
+   --  to the POSIX specification). For instance, using select(), the
+   --  notification of an asynchronous connect failure is delivered in the
+   --  write socket set (POSIX) instead of the exception socket set (NT).
 
    --  Here is a typical example of what you can do:
 
@@ -119,11 +115,10 @@ package GNAT.Sockets is
    --        Address.Port := 5876;
 
    --        --  The first step is to create a socket. Once created, this
-   --        --  socket must be associated to with an address. Usually only
-   --        --  a server (Pong here) needs to bind an address explicitly.
-   --        --  Most of the time clients can skip this step because the
-   --        --  socket routines will bind an arbitrary address to an unbound
-   --        --  socket.
+   --        --  socket must be associated to with an address. Usually only a
+   --        --  server (Pong here) needs to bind an address explicitly. Most
+   --        --  of the time clients can skip this step because the socket
+   --        --  routines will bind an arbitrary address to an unbound socket.
 
    --        Create_Socket (Server);
 
@@ -185,13 +180,6 @@ package GNAT.Sockets is
    --           Socket_Level,
    --           (Reuse_Address, True));
 
-   --        --  Join a multicast group
-
-   --        Set_Socket_Option
-   --          (Socket,
-   --           IP_Protocol_For_IP_Level,
-   --           (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
-
    --        --  Controls the live time of the datagram to avoid it being
    --        --  looped forever due to routing errors. Routers decrement
    --        --  the TTL of every datagram as it traverses from one network
@@ -218,6 +206,16 @@ package GNAT.Sockets is
 
    --        Bind_Socket (Socket, Address);
 
+   --        --  Join a multicast group
+
+   --        --  Portability note: On Windows, this option may be set only
+   --        --  on a bound socket.
+
+   --        Set_Socket_Option
+   --          (Socket,
+   --           IP_Protocol_For_IP_Level,
+   --           (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
+
    --        --  If this socket is intended to send messages, provide the
    --        --  receiver socket address.
 
@@ -288,7 +286,7 @@ package GNAT.Sockets is
 
    --        Channel := Stream (Socket);
 
-   --        --  Send message to server Pong.
+   --        --  Send message to server Pong
 
    --        String'Output (Channel, "Hello world");
 
@@ -313,11 +311,6 @@ package GNAT.Sockets is
    --        Set_Socket_Option
    --          (Socket,
    --           IP_Protocol_For_IP_Level,
-   --           (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
-
-   --        Set_Socket_Option
-   --          (Socket,
-   --           IP_Protocol_For_IP_Level,
    --           (Multicast_TTL, 1));
 
    --        Set_Socket_Option
@@ -330,6 +323,11 @@ package GNAT.Sockets is
 
    --        Bind_Socket (Socket, Address);
 
+   --        Set_Socket_Option
+   --          (Socket,
+   --           IP_Protocol_For_IP_Level,
+   --           (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
+
    --        Address.Addr := Inet_Addr (Group);
    --        Address.Port := 55505;
 
@@ -359,11 +357,7 @@ package GNAT.Sockets is
    --     end Ping;
 
    --  begin
-   --     --  Indicate whether the thread library provides process
-   --     --  blocking IO. Basically, if you are not using FSU threads
-   --     --  the default is ok.
-
-   --     Initialize (Process_Blocking_IO => False);
+   --     Initialize;
    --     Ping.Start;
    --     Pong.Start;
    --     Ping.Stop;
@@ -371,62 +365,61 @@ package GNAT.Sockets is
    --     Finalize;
    --  end PingPong;
 
-   procedure Initialize (Process_Blocking_IO : Boolean := False);
+   procedure Initialize;
    --  Initialize must be called before using any other socket routines.
-   --  The Process_Blocking_IO parameter indicates whether the thread
-   --  library provides process-blocking or thread-blocking input/output
-   --  operations. In the former case (typically with FSU threads)
-   --  GNAT.Sockets should be initialized with a value of True to
-   --  provide task-blocking IO through an emulation mechanism.
-   --  Only the first call to Initialize is taken into account (further
-   --  calls will be ignored). Note that with the default value
-   --  of Process_Blocking_IO, this operation is a no-op on UNIX
-   --  platforms, but applications should make sure to call it
-   --  if portability is expected: some platforms (such as Windows)
-   --  require initialization before any other socket operations.
+   --  Note that this operation is a no-op on UNIX platforms, but applications
+   --  should make sure to call it if portability is expected: some platforms
+   --  (such as Windows) require initialization before any socket operation.
+
+   procedure Initialize (Process_Blocking_IO : Boolean);
+   pragma Obsolescent
+     (Entity => Initialize,
+      "passing a parameter to Initialize is not supported anymore");
+   --  Previous versions of GNAT.Sockets used to require the user to indicate
+   --  whether socket I/O was process- or thread-blocking on the platform.
+   --  This property is now determined automatically when the run-time library
+   --  is built. The old version of Initialize, taking a parameter, is kept
+   --  for compatibility reasons, but this interface is obsolete (and if the
+   --  value given is wrong, an exception will be raised at run time).
 
    procedure Finalize;
    --  After Finalize is called it is not possible to use any routines
    --  exported in by this package. This procedure is idempotent.
 
    type Socket_Type is private;
-   --  Sockets are used to implement a reliable bi-directional
-   --  point-to-point, stream-based connections between
-   --  hosts. No_Socket provides a special value to denote
-   --  uninitialized sockets.
+   --  Sockets are used to implement a reliable bi-directional point-to-point,
+   --  stream-based connections between hosts. No_Socket provides a special
+   --  value to denote uninitialized sockets.
 
    No_Socket : constant Socket_Type;
 
    Socket_Error : exception;
-   --  There is only one exception in this package to deal with an
-   --  error during a socket routine. Once raised, its message
-   --  contains a string describing the error code.
+   --  There is only one exception in this package to deal with an error during
+   --  a socket routine. Once raised, its message contains a string describing
+   --  the error code.
 
    function Image (Socket : Socket_Type) return String;
    --  Return a printable string for Socket
 
    function To_C (Socket : Socket_Type) return Integer;
-   --  Return a file descriptor to be used by external subprograms
-   --  especially the C functions that are not yet interfaced in this
-   --  package.
+   --  Return a file descriptor to be used by external subprograms. This is
+   --  useful for C functions that are not yet interfaced in this package.
 
    type Family_Type is (Family_Inet, Family_Inet6);
-   --  Address family (or protocol family) identifies the
-   --  communication domain and groups protocols with similar address
-   --  formats. IPv6 will soon be supported.
+   --  Address family (or protocol family) identifies the communication domain
+   --  and groups protocols with similar address formats. IPv6 will soon be
+   --  supported.
 
    type Mode_Type is (Socket_Stream, Socket_Datagram);
-   --  Stream sockets provide connection-oriented byte
-   --  streams. Datagram sockets support unreliable connectionless
-   --  message based communication.
+   --  Stream sockets provide connection-oriented byte streams. Datagram
+   --  sockets support unreliable connectionless message based communication.
 
    type Shutmode_Type is (Shut_Read, Shut_Write, Shut_Read_Write);
-   --  When a process closes a socket, the policy is to retain any
-   --  data queued until either a delivery or a timeout expiration (in
-   --  this case, the data are discarded). A finer control is
-   --  available through shutdown. With Shut_Read, no more data can be
-   --  received from the socket. With_Write, no more data can be
-   --  transmitted. Neither transmission nor reception can be
+   --  When a process closes a socket, the policy is to retain any data queued
+   --  until either a delivery or a timeout expiration (in this case, the data
+   --  are discarded). A finer control is available through shutdown. With
+   --  Shut_Read, no more data can be received from the socket. With_Write, no
+   --  more data can be transmitted. Neither transmission nor reception can be
    --  performed with Shut_Read_Write.
 
    type Port_Type is new Natural;
@@ -438,41 +431,41 @@ package GNAT.Sockets is
    No_Port  : constant Port_Type;
 
    type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
-   --  An Internet address depends on an address family (IPv4 contains
-   --  4 octets and Ipv6 contains 16 octets). Any_Inet_Address is a
-   --  special value treated like a wildcard enabling all addresses.
-   --  No_Inet_Addr provides a special value to denote uninitialized
-   --  inet addresses.
+   --  An Internet address depends on an address family (IPv4 contains 4
+   --  octets and Ipv6 contains 16 octets). Any_Inet_Addr is a special value
+   --  treated like a wildcard enabling all addresses. No_Inet_Addr provides a
+   --  special value to denote uninitialized inet addresses.
 
-   Any_Inet_Addr : constant Inet_Addr_Type;
-   No_Inet_Addr  : constant Inet_Addr_Type;
+   Any_Inet_Addr       : constant Inet_Addr_Type;
+   No_Inet_Addr        : constant Inet_Addr_Type;
+   Broadcast_Inet_Addr : constant Inet_Addr_Type;
 
    type Sock_Addr_Type (Family : Family_Type := Family_Inet) is record
       Addr : Inet_Addr_Type (Family);
       Port : Port_Type;
    end record;
-   --  Socket addresses fully define a socket connection with a
-   --  protocol family, an Internet address and a port. No_Sock_Addr
-   --  provides a special value for uninitialized socket addresses.
+   --  Socket addresses fully define a socket connection with protocol family,
+   --  an Internet address and a port. No_Sock_Addr provides a special value
+   --  for uninitialized socket addresses.
 
    No_Sock_Addr : constant Sock_Addr_Type;
 
    function Image (Value : Inet_Addr_Type) return String;
-   --  Return an image of an Internet address. IPv4 notation consists
-   --  in 4 octets in decimal format separated by dots. IPv6 notation
-   --  consists in 16 octets in hexadecimal format separated by
-   --  colons (and possibly dots).
+   --  Return an image of an Internet address. IPv4 notation consists in 4
+   --  octets in decimal format separated by dots. IPv6 notation consists in
+   --  16 octets in hexadecimal format separated by colons (and possibly
+   --  dots).
 
    function Image (Value : Sock_Addr_Type) return String;
-   --  Return inet address image and port image separated by a colon.
+   --  Return inet address image and port image separated by a colon
 
    function Inet_Addr (Image : String) return Inet_Addr_Type;
    --  Convert address image from numbers-and-dots notation into an
    --  inet address.
 
-   --  Host entries provide complete information on a given host:
-   --  the official name, an array of alternative names or aliases and
-   --  array of network addresses.
+   --  Host entries provide complete information on a given host: the official
+   --  name, an array of alternative names or aliases and array of network
+   --  addresses.
 
    type Host_Entry_Type
      (Aliases_Length, Addresses_Length : Natural) is private;
@@ -487,42 +480,40 @@ package GNAT.Sockets is
    --  Return number of addresses in host entry
 
    function Aliases
-     (E    : Host_Entry_Type;
-      N    : Positive := 1)
-      return String;
-   --  Return N'th aliases in host entry. The first index is 1.
+     (E : Host_Entry_Type;
+      N : Positive := 1) return String;
+   --  Return N'th aliases in host entry. The first index is 1
 
    function Addresses
-     (E    : Host_Entry_Type;
-      N    : Positive := 1)
-      return Inet_Addr_Type;
-   --  Return N'th addresses in host entry. The first index is 1.
+     (E : Host_Entry_Type;
+      N : Positive := 1) return Inet_Addr_Type;
+   --  Return N'th addresses in host entry. The first index is 1
 
    Host_Error : exception;
-   --  Exception raised by the two following procedures. Once raised,
-   --  its message contains a string describing the error code. This
-   --  exception is raised when an host entry can not be retrieved.
+   --  Exception raised by the two following procedures. Once raised, its
+   --  message contains a string describing the error code. This exception is
+   --  raised when an host entry cannot be retrieved.
 
    function Get_Host_By_Address
      (Address : Inet_Addr_Type;
-      Family  : Family_Type := Family_Inet)
-      return    Host_Entry_Type;
-   --  Return host entry structure for the given inet address
+      Family  : Family_Type := Family_Inet) return Host_Entry_Type;
+   --  Return host entry structure for the given Inet address. Note that no
+   --  result will be returned if there is no mapping of this IP address to a
+   --  host name in the system tables (host database, DNS or otherwise).
 
    function Get_Host_By_Name
-     (Name : String)
-      return Host_Entry_Type;
-   --  Return host entry structure for the given host name. Here name
-   --  is either a host name, or an IP address.
+     (Name : String) return Host_Entry_Type;
+   --  Return host entry structure for the given host name. Here name is
+   --  either a host name, or an IP address. If Name is an IP address, this is
+   --  equivalent to Get_Host_By_Address (Inet_Addr (Name)).
 
    function Host_Name return String;
    --  Return the name of the current host
 
-   --  Service entries provide complete information on a given
-   --  service: the official name, an array of alternative names or
-   --  aliases and the port number.
-
    type Service_Entry_Type (Aliases_Length : Natural) is private;
+   --  Service entries provide complete information on a given service: the
+   --  official name, an array of alternative names or aliases and the port
+   --  number.
 
    function Official_Name (S : Service_Entry_Type) return String;
    --  Return official name in service entry
@@ -537,31 +528,29 @@ package GNAT.Sockets is
    --  Return number of aliases in service entry
 
    function Aliases
-     (S    : Service_Entry_Type;
-      N    : Positive := 1)
-      return String;
-   --  Return N'th aliases in service entry. The first index is 1.
+     (S : Service_Entry_Type;
+      N : Positive := 1) return String;
+   --  Return N'th aliases in service entry (the first index is 1)
 
    function Get_Service_By_Name
      (Name     : String;
-      Protocol : String)
-      return     Service_Entry_Type;
+      Protocol : String) return Service_Entry_Type;
    --  Return service entry structure for the given service name
 
    function Get_Service_By_Port
      (Port     : Port_Type;
-      Protocol : String)
-      return     Service_Entry_Type;
+      Protocol : String) return Service_Entry_Type;
    --  Return service entry structure for the given service port number
 
    Service_Error : exception;
+   --  Comment required ???
 
    --  Errors are described by an enumeration type. There is only one
-   --  exception Socket_Error in this package to deal with an error
-   --  during a socket routine. Once raised, its message contains the
-   --  error code between brackets and a string describing the error code.
+   --  exception Socket_Error in this package to deal with an error during a
+   --  socket routine. Once raised, its message contains the error code
+   --  between brackets and a string describing the error code.
 
-   --  The name of the enumeration constant documents the error condition.
+   --  The name of the enumeration constant documents the error condition
 
    type Error_Type is
      (Success,
@@ -609,10 +598,19 @@ package GNAT.Sockets is
       Unknown_Server_Error,
       Cannot_Resolve_Error);
 
-   --  Get_Socket_Options and Set_Socket_Options manipulate options
-   --  associated with a socket. Options may exist at multiple
-   --  protocol levels in the communication stack. Socket_Level is the
-   --  uppermost socket level.
+   --  Timeval_Duration is a subtype of Standard.Duration because the full
+   --  range of Standard.Duration cannot be represented in the equivalent C
+   --  structure. Moreover, negative values are not allowed to avoid system
+   --  incompatibilities.
+
+   Immediate : constant := 0.0;
+   Forever   : constant := Duration (Integer'Last) * 1.0;
+
+   subtype Timeval_Duration is Duration range Immediate .. Forever;
+
+   --  Get_Socket_Options and Set_Socket_Options manipulate options associated
+   --  with a socket. Options may exist at multiple protocol levels in the
+   --  communication stack. Socket_Level is the uppermost socket level.
 
    type Level_Type is (
      Socket_Level,
@@ -620,23 +618,26 @@ package GNAT.Sockets is
      IP_Protocol_For_UDP_Level,
      IP_Protocol_For_TCP_Level);
 
-   --  There are several options available to manipulate sockets. Each
-   --  option has a name and several values available. Most of the
-   --  time, the value is a boolean to enable or disable this option.
+   --  There are several options available to manipulate sockets. Each option
+   --  has a name and several values available. Most of the time, the value is
+   --  a boolean to enable or disable this option.
 
    type Option_Name is (
-     Keep_Alive,      -- Enable sending of keep-alive messages
-     Reuse_Address,   -- Allow bind to reuse local address
-     Broadcast,       -- Enable datagram sockets to recv/send broadcast packets
-     Send_Buffer,     -- Set/get the maximum socket send buffer in bytes
-     Receive_Buffer,  -- Set/get the maximum socket recv buffer in bytes
-     Linger,          -- Shutdown wait for msg to be sent or timeout occur
-     Error,           -- Get and clear the pending socket error
-     No_Delay,        -- Do not delay send to coalesce packets (TCP_NODELAY)
-     Add_Membership,  -- Join a multicast group
-     Drop_Membership, -- Leave a multicast group
-     Multicast_TTL,   -- Indicate the time-to-live of sent multicast packets
-     Multicast_Loop); -- Sent multicast packets are looped to the local socket
+     Keep_Alive,       -- Enable sending of keep-alive messages
+     Reuse_Address,    -- Allow bind to reuse local address
+     Broadcast,        -- Enable datagram sockets to recv/send broadcasts
+     Send_Buffer,      -- Set/get the maximum socket send buffer in bytes
+     Receive_Buffer,   -- Set/get the maximum socket recv buffer in bytes
+     Linger,           -- Shutdown wait for msg to be sent or timeout occur
+     Error,            -- Get and clear the pending socket error
+     No_Delay,         -- Do not delay send to coalesce packets (TCP_NODELAY)
+     Add_Membership,   -- Join a multicast group
+     Drop_Membership,  -- Leave a multicast group
+     Multicast_If,     -- Set default outgoing interface for multicast packets
+     Multicast_TTL,    -- Indicate the time-to-live of sent multicast packets
+     Multicast_Loop,   -- Sent multicast packets are looped to local socket
+     Send_Timeout,     -- Set timeout value for output
+     Receive_Timeout); -- Set timeout value for input
 
    type Option_Type (Name : Option_Name := Keep_Alive) is record
       case Name is
@@ -664,19 +665,26 @@ package GNAT.Sockets is
 
          when Add_Membership  |
               Drop_Membership =>
-            Multiaddr : Inet_Addr_Type;
-            Interface : Inet_Addr_Type;
+            Multicast_Address : Inet_Addr_Type;
+            Local_Interface   : Inet_Addr_Type;
+
+         when Multicast_If    =>
+            Outgoing_If : Inet_Addr_Type;
 
          when Multicast_TTL   =>
             Time_To_Live : Natural;
 
+         when Send_Timeout |
+              Receive_Timeout =>
+            Timeout : Timeval_Duration;
+
       end case;
    end record;
 
-   --  There are several controls available to manipulate
-   --  sockets. Each option has a name and several values available.
-   --  These controls differ from the socket options in that they are
-   --  not specific to sockets but are available for any device.
+   --  There are several controls available to manipulate sockets. Each option
+   --  has a name and several values available. These controls differ from the
+   --  socket options in that they are not specific to sockets but are
+   --  available for any device.
 
    type Request_Name is (
       Non_Blocking_IO,  --  Cause a caller not to wait on blocking operations.
@@ -693,35 +701,36 @@ package GNAT.Sockets is
       end case;
    end record;
 
-   --  A request flag allows to specify the type of message
-   --  transmissions or receptions. A request flag can be a
-   --  combination of zero or more predefined request flags.
+   --  A request flag allows to specify the type of message transmissions or
+   --  receptions. A request flag can be combination of zero or more
+   --  predefined request flags.
 
    type Request_Flag_Type is private;
 
    No_Request_Flag : constant Request_Flag_Type;
-   --  This flag corresponds to the normal execution of an operation.
+   --  This flag corresponds to the normal execution of an operation
 
    Process_Out_Of_Band_Data : constant Request_Flag_Type;
-   --  This flag requests that the receive or send function operates
-   --  on out-of-band data when the socket supports this notion (e.g.
+   --  This flag requests that the receive or send function operates on
+   --  out-of-band data when the socket supports this notion (e.g.
    --  Socket_Stream).
 
    Peek_At_Incoming_Data : constant Request_Flag_Type;
    --  This flag causes the receive operation to return data from the
-   --  beginning of the receive queue without removing that data from
-   --  the queue. A subsequent receive call will return the same data.
+   --  beginning of the receive queue without removing that data from the
+   --  queue. A subsequent receive call will return the same data.
 
    Wait_For_A_Full_Reception : constant Request_Flag_Type;
-   --  This flag requests that the operation block until the full
-   --  request is satisfied. However, the call may still return less
-   --  data than requested if a signal is caught, an error or
-   --  disconnect occurs, or the next data to be received is of a dif-
-   --  ferent type than that returned.
+   --  This flag requests that the operation block until the full request is
+   --  satisfied. However, the call may still return less data than requested
+   --  if a signal is caught, an error or disconnect occurs, or the next data
+   --  to be received is of a different type than that returned. Note that
+   --  this flag depends on support in the underlying sockets implementation,
+   --  and is not supported under Windows.
 
    Send_End_Of_Record : constant Request_Flag_Type;
-   --  This flag indicates that the entire message has been sent and
-   --  so this terminates the record.
+   --  This flag indicates that the entire message has been sent and so this
+   --  terminates the record.
 
    function "+" (L, R : Request_Flag_Type) return Request_Flag_Type;
    --  Combine flag L with flag R
@@ -739,17 +748,17 @@ package GNAT.Sockets is
      (Socket : out Socket_Type;
       Family : Family_Type := Family_Inet;
       Mode   : Mode_Type   := Socket_Stream);
-   --  Create an endpoint for communication. Raises Socket_Error on error.
+   --  Create an endpoint for communication. Raises Socket_Error on error
 
    procedure Accept_Socket
      (Server  : Socket_Type;
       Socket  : out Socket_Type;
       Address : out Sock_Addr_Type);
-   --  Extract the first connection request on the queue of pending
-   --  connections, creates a new connected socket with mostly the
-   --  same properties as Server, and allocates a new socket. The
-   --  returned Address is filled in with the address of the
-   --  connection. Raises Socket_Error on error.
+   --  Extracts the first connection request on the queue of pending
+   --  connections, creates a new connected socket with mostly the same
+   --  properties as Server, and allocates a new socket. The returned Address
+   --  is filled in with the address of the connection. Raises Socket_Error on
+   --  error.
 
    procedure Bind_Socket
      (Socket  : Socket_Type;
@@ -758,7 +767,7 @@ package GNAT.Sockets is
    --  Socket_Error on error.
 
    procedure Close_Socket (Socket : Socket_Type);
-   --  Close a socket and more specifically a non-connected socket.
+   --  Close a socket and more specifically a non-connected socket
 
    procedure Connect_Socket
      (Socket : Socket_Type;
@@ -769,9 +778,9 @@ package GNAT.Sockets is
    procedure Control_Socket
      (Socket  : Socket_Type;
       Request : in out Request_Type);
-   --  Obtain or set parameter values that control the socket. This
-   --  control differs from the socket options in that they are not
-   --  specific to sockets but are available for any device.
+   --  Obtain or set parameter values that control the socket. This control
+   --  differs from the socket options in that they are not specific to
+   --  sockets but are available for any device.
 
    function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
    --  Return the peer or remote socket address of a socket. Raise
@@ -779,35 +788,33 @@ package GNAT.Sockets is
 
    function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
    --  Return the local or current socket address of a socket. Return
-   --  No_Sock_Addr on error (for instance, socket closed or not
-   --  locally bound).
+   --  No_Sock_Addr on error (for instance, socket closed or not locally
+   --  bound).
 
    function Get_Socket_Option
      (Socket : Socket_Type;
       Level  : Level_Type := Socket_Level;
-      Name   : Option_Name)
-      return   Option_Type;
+      Name   : Option_Name) return Option_Type;
    --  Get the options associated with a socket. Raises Socket_Error
    --  on error.
 
    procedure Listen_Socket
      (Socket : Socket_Type;
       Length : Positive := 15);
-   --  To accept connections, a socket is first created with
-   --  Create_Socket, a willingness to accept incoming connections and
-   --  a queue Length for incoming connections are specified. Raise
-   --  Socket_Error on error.
+   --  To accept connections, a socket is first created with Create_Socket,
+   --  a willingness to accept incoming connections and a queue Length for
+   --  incoming connections are specified. Raise Socket_Error on error.
 
    procedure Receive_Socket
      (Socket : Socket_Type;
       Item   : out Ada.Streams.Stream_Element_Array;
       Last   : out Ada.Streams.Stream_Element_Offset;
       Flags  : Request_Flag_Type := No_Request_Flag);
-   --  Receive message from Socket. Last is the index value such that
-   --  Item (Last) is the last character assigned. Note that Last is
-   --  set to Item'First - 1 when the socket has been closed by
-   --  peer. This is not an error and no exception is raised. Flags
-   --  allows to control the reception. Raise Socket_Error on error.
+   --  Receive message from Socket. Last is the index value such that Item
+   --  (Last) is the last character assigned. Note that Last is set to
+   --  Item'First - 1 when the socket has been closed by peer. This is not an
+   --  error and no exception is raised. Flags allows to control the
+   --  reception. Raise Socket_Error on error.
 
    procedure Receive_Socket
      (Socket : Socket_Type;
@@ -815,11 +822,10 @@ package GNAT.Sockets is
       Last   : out Ada.Streams.Stream_Element_Offset;
       From   : out Sock_Addr_Type;
       Flags  : Request_Flag_Type := No_Request_Flag);
-   --  Receive message from Socket. If Socket is not
-   --  connection-oriented, the source address From of the message is
-   --  filled in. Last is the index value such that Item (Last) is the
-   --  last character assigned. Flags allows to control the
-   --  reception. Raises Socket_Error on error.
+   --  Receive message from Socket. If Socket is not connection-oriented, the
+   --  source address From of the message is filled in. Last is the index
+   --  value such that Item (Last) is the last character assigned. Flags
+   --  allows to control the reception. Raises Socket_Error on error.
 
    procedure Receive_Vector
      (Socket : Socket_Type;
@@ -829,13 +835,11 @@ package GNAT.Sockets is
    --  elements Vector. Count is set to the count of received stream elements.
 
    function Resolve_Exception
-     (Occurrence : Ada.Exceptions.Exception_Occurrence)
-      return       Error_Type;
-   --  When Socket_Error or Host_Error are raised, the exception
-   --  message contains the error code between brackets and a string
-   --  describing the error code. Resolve_Error extracts the error
-   --  code from an exception message and translate it into an
-   --  enumeration value.
+     (Occurrence : Ada.Exceptions.Exception_Occurrence) return Error_Type;
+   --  When Socket_Error or Host_Error are raised, the exception message
+   --  contains the error code between brackets and a string describing the
+   --  error code. Resolve_Error extracts the error code from an exception
+   --  message and translate it into an enumeration value.
 
    procedure Send_Socket
      (Socket : Socket_Type;
@@ -844,9 +848,8 @@ package GNAT.Sockets is
       Flags  : Request_Flag_Type := No_Request_Flag);
    --  Transmit a message to another socket. Note that Last is set to
    --  Item'First-1 when socket has been closed by peer. This is not
-   --  considered an error and no exception is raised. Flags allows to
-   --  control the transmission. Raises Socket_Error on any other
-   --  error condition.
+   --  considered an error and no exception is raised. Flags allows to control
+   --  the transmission. Raises Socket_Error on any other error condition.
 
    procedure Send_Socket
      (Socket : Socket_Type;
@@ -854,9 +857,8 @@ package GNAT.Sockets is
       Last   : out Ada.Streams.Stream_Element_Offset;
       To     : Sock_Addr_Type;
       Flags  : Request_Flag_Type := No_Request_Flag);
-   --  Transmit a message to another socket. The address is given by
-   --  To. Flags allows to control the transmission. Raises
-   --  Socket_Error on error.
+   --  Transmit a message to another socket. The address is given by To. Flags
+   --  allows to control the transmission. Raises Socket_Error on error.
 
    procedure Send_Vector
      (Socket : Socket_Type;
@@ -869,50 +871,43 @@ package GNAT.Sockets is
      (Socket : Socket_Type;
       Level  : Level_Type := Socket_Level;
       Option : Option_Type);
-   --  Manipulate socket options. Raises Socket_Error on error.
+   --  Manipulate socket options. Raises Socket_Error on error
 
    procedure Shutdown_Socket
      (Socket : Socket_Type;
       How    : Shutmode_Type := Shut_Read_Write);
-   --  Shutdown a connected socket. If How is Shut_Read, further
-   --  receives will be disallowed. If How is Shut_Write, further
-   --  sends will be disallowed. If how is Shut_Read_Write, further
-   --  sends and receives will be disallowed.
+   --  Shutdown a connected socket. If How is Shut_Read, further receives will
+   --  be disallowed. If How is Shut_Write, further sends will be disallowed.
+   --  If how is Shut_Read_Write, further sends and receives will be
+   --  disallowed.
 
    type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
    --  Same interface as Ada.Streams.Stream_IO
 
-   function Stream
-     (Socket : Socket_Type)
-      return   Stream_Access;
+   function Stream (Socket : Socket_Type) return Stream_Access;
    --  Create a stream associated with a stream-based socket that is
    --  already connected.
 
    function Stream
      (Socket  : Socket_Type;
-      Send_To : Sock_Addr_Type)
-      return    Stream_Access;
-   --  Create a stream associated with a datagram-based socket that is
-   --  already bound. Send_To is the socket address to which messages are
-   --  being sent.
-
-   function Get_Address
-     (Stream : Stream_Access)
-      return   Sock_Addr_Type;
-   --  Return the socket address from which the last message was
-   --  received.
-
-   procedure Free (Stream : in out Stream_Access);
-   --  Destroy a stream created by one of the Stream functions above, and
-   --  release associated resources. The user is responsible for calling
-   --  this subprogram when the stream is not needed anymore.
+      Send_To : Sock_Addr_Type) return Stream_Access;
+   --  Create a stream associated with a datagram-based socket that is already
+   --  bound. Send_To is the socket address to which messages are being sent.
+
+   function Get_Address (Stream : Stream_Access) return Sock_Addr_Type;
+   --  Return the socket address from which the last message was received
+
+   procedure Free is new Ada.Unchecked_Deallocation
+     (Ada.Streams.Root_Stream_Type'Class, Stream_Access);
+   --  Destroy a stream created by one of the Stream functions above,
+   --  releasing the corresponding resources. The user is responsible for
+   --  calling this subprogram when the stream is not needed anymore.
 
    type Socket_Set_Type is limited private;
-   --  This type allows to manipulate sets of sockets. It allows to
-   --  wait for events on multiple endpoints at one time. This is an
-   --  access type on a system dependent structure. To avoid memory
-   --  leaks it is highly recommended to clean the access value with
-   --  procedure Empty.
+   --  This type allows to manipulate sets of sockets. It allows to wait for
+   --  events on multiple endpoints at one time. This is an access type on a
+   --  system dependent structure. To avoid memory leaks it is highly
+   --  recommended to clean the access value with procedure Empty.
 
    procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
    --  Remove Socket from Item
@@ -927,58 +922,49 @@ package GNAT.Sockets is
    --  Extract a Socket from socket set Item. Socket is set to
    --  No_Socket when the set is empty.
 
-   function Is_Empty
-     (Item  : Socket_Set_Type)
-      return  Boolean;
-   --  Return True if Item is empty
+   function Is_Empty (Item : Socket_Set_Type) return Boolean;
+   --  Return True iff Item is empty
 
    function Is_Set
      (Item   : Socket_Set_Type;
-      Socket : Socket_Type)
-      return   Boolean;
-   --  Return True if Socket is present in Item
+      Socket : Socket_Type) return Boolean;
+   --  Return True iff Socket is present in Item
 
-   procedure Set   (Item : in out Socket_Set_Type; Socket : Socket_Type);
+   procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type);
    --  Insert Socket into Item
 
-   --  C select() waits for a number of file descriptors to change
-   --  status. Usually, three independent sets of descriptors are
-   --  watched (read, write and exception). A timeout gives an upper
-   --  bound on the amount of time elapsed before select returns.
-   --  This function blocks until an event occurs. On some platforms,
-   --  C select can block the full process.
+   --  The select(2) system call waits for events to occur on any of a set of
+   --  file descriptors. Usually, three independent sets of descriptors are
+   --  watched (read, write  and exception). A timeout gives an upper bound
+   --  on the amount of time elapsed before select returns. This function
+   --  blocks until an event occurs. On some platforms, the select(2) system
+   --  can block the full process (not just the calling thread).
    --
-   --  Check_Selector provides the very same behaviour. The only
-   --  difference is that it does not watch for exception events. Note
-   --  that on some platforms it is kept process blocking in purpose.
-   --  The timeout parameter allows the user to have the behaviour he
-   --  wants. Abort_Selector allows to abort safely a Check_Selector
-   --  that is blocked forever. A special file descriptor is opened by
-   --  Create_Selector and included in each call to
-   --  Check_Selector. Abort_Selector causes an event to occur on this
-   --  descriptor in order to unblock Check_Selector. The user must
-   --  call Close_Selector to discard this special file. A reason to
-   --  abort a select operation is typically to add a socket in one of
-   --  the socket sets when the timeout is set to forever.
+   --  Check_Selector provides the very same behaviour. The only difference is
+   --  that it does not watch for exception events. Note that on some
+   --  platforms it is kept process blocking on purpose. The timeout parameter
+   --  allows the user to have the behaviour he wants. Abort_Selector allows
+   --  to abort safely a Check_Selector that is blocked forever. A special
+   --  file descriptor is opened by Create_Selector and included in each call
+   --  to Check_Selector. Abort_Selector causes an event to occur on this
+   --  descriptor in order to unblock Check_Selector. The user must call
+   --  Close_Selector to discard this special file. A reason to abort a select
+   --  operation is typically to add a socket in one of the socket sets when
+   --  the timeout is set to forever.
 
    type Selector_Type is limited private;
    type Selector_Access is access all Selector_Type;
 
-   --  Selector_Duration is a subtype of Standard.Duration because the
-   --  full range of Standard.Duration cannot be represented in the
-   --  equivalent C structure. Moreover, negative values are not
-   --  allowed to avoid system incompatibilities.
-
-   Immediate : constant := 0.0;
-   Forever   : constant := Duration (Integer'Last) * 1.0;
-
-   subtype Selector_Duration is Duration range Immediate .. Forever;
+   subtype Selector_Duration is Timeval_Duration;
 
    procedure Create_Selector (Selector : out Selector_Type);
    --  Create a new selector
 
    procedure Close_Selector (Selector : in out Selector_Type);
-   --  Close Selector and all internal descriptors associated
+   --  Close Selector and all internal descriptors associated; deallocate any
+   --  associated resources. This subprogram may be called only when there is
+   --  no other task still using Selector (i.e. still executing Check_Selector
+   --  or Abort_Selector on this Selector).
 
    type Selector_Status is (Completed, Expired, Aborted);
 
@@ -988,18 +974,18 @@ package GNAT.Sockets is
       W_Socket_Set : in out Socket_Set_Type;
       Status       : out Selector_Status;
       Timeout      : Selector_Duration := Forever);
-   --  Return when one Socket in R_Socket_Set has some data to be read
-   --  or if one Socket in W_Socket_Set is ready to receive some
-   --  data. In these cases Status is set to Completed and sockets
-   --  that are ready are set in R_Socket_Set or W_Socket_Set. Status
-   --  is set to Expired if no socket was ready after a Timeout
-   --  expiration. Status is set to Aborted if an abort signal has been
-   --  received while checking socket status. As this procedure
-   --  returns when Timeout occurs, it is a design choice to keep this
-   --  procedure process blocking. Note that a Timeout of 0.0 returns
-   --  immediately. Also note that two different objects must be passed
-   --  as R_Socket_Set and W_Socket_Set (even if they contain the same
-   --  set of Sockets), or some event will be lost.
+   --  Return when one Socket in R_Socket_Set has some data to be read or if
+   --  one Socket in W_Socket_Set is ready to transmit some data. In these
+   --  cases Status is set to Completed and sockets that are ready are set in
+   --  R_Socket_Set or W_Socket_Set. Status is set to Expired if no socket was
+   --  ready after a Timeout expiration. Status is set to Aborted if an abort
+   --  signal has been received while checking socket status.
+   --  Note that two different Socket_Set_Type objects must be passed as
+   --  R_Socket_Set and W_Socket_Set (even if they denote the same set of
+   --  Sockets), or some event may be lost.
+   --  Socket_Error is raised when the select(2) system call returns an
+   --  error condition, or when a read error occurs on the signalling socket
+   --  used for the implementation of Abort_Selector.
 
    procedure Check_Selector
      (Selector     : in out Selector_Type;
@@ -1008,13 +994,13 @@ package GNAT.Sockets is
       E_Socket_Set : in out Socket_Set_Type;
       Status       : out Selector_Status;
       Timeout      : Selector_Duration := Forever);
-   --  This refined version of Check_Selector allows to watch for
-   --  exception events (that is notifications of out-of-band
-   --  transmission and reception). As above, all of R_Socket_Set,
-   --  W_Socket_Set and E_Socket_Set must be different objects.
+   --  This refined version of Check_Selector allows to watch for exception
+   --  events (that is notifications of out-of-band transmission and
+   --  reception). As above, all of R_Socket_Set, W_Socket_Set and
+   --  E_Socket_Set must be different objects.
 
    procedure Abort_Selector (Selector : Selector_Type);
-   --  Send an abort signal to the selector.
+   --  Send an abort signal to the selector
 
 private
 
@@ -1022,14 +1008,13 @@ private
    No_Socket : constant Socket_Type := -1;
 
    type Selector_Type is limited record
-      R_Sig_Socket : Socket_Type;
-      W_Sig_Socket : Socket_Type;
+      R_Sig_Socket : Socket_Type := No_Socket;
+      W_Sig_Socket : Socket_Type := No_Socket;
    end record;
 
    pragma Volatile (Selector_Type);
 
-   --  The two signalling sockets are used to abort a select
-   --  operation.
+   --  The two signalling sockets are used to abort a select operation
 
    subtype Socket_Set_Access is System.Address;
    No_Socket_Set : constant Socket_Set_Access := System.Null_Address;
@@ -1060,10 +1045,14 @@ private
    Any_Port : constant Port_Type := 0;
    No_Port  : constant Port_Type := 0;
 
-   Any_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
-   No_Inet_Addr  : constant Inet_Addr_Type := (Family_Inet, (others => 0));
+   Any_Inet_Addr       : constant Inet_Addr_Type :=
+                           (Family_Inet, (others => 0));
+   No_Inet_Addr        : constant Inet_Addr_Type :=
+                           (Family_Inet, (others => 0));
+   Broadcast_Inet_Addr : constant Inet_Addr_Type :=
+                           (Family_Inet, (others => 255));
 
-   No_Sock_Addr  : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
+   No_Sock_Addr : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
 
    Max_Name_Length : constant := 64;
    --  The constant MAXHOSTNAMELEN is usually set to 64