Properly regenerate gcc/configure.
[official-gcc.git] / gcc / ada / g-socket.ads
blob517dd4f510ad86aa6c2de081874fd1b1cbea55fb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . S O C K E T S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2001-2014, AdaCore --
10 -- --
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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This package provides an interface to the sockets communication facility
33 -- provided on many operating systems. This is implemented on the following
34 -- platforms:
36 -- All native ports, with restrictions as follows
38 -- Multicast is available only on systems which provide support for this
39 -- feature, so it is not available if Multicast is not supported, or not
40 -- installed.
42 -- VxWorks cross ports fully implement this package
44 -- This package is not yet implemented on LynxOS or other cross ports
46 with Ada.Exceptions;
47 with Ada.Streams;
48 with Ada.Unchecked_Deallocation;
50 with Interfaces.C;
52 with System.OS_Constants;
53 with System.Storage_Elements;
55 package GNAT.Sockets is
57 -- Sockets are designed to provide a consistent communication facility
58 -- between applications. This package provides an Ada binding to the
59 -- de-facto standard BSD sockets API. The documentation below covers
60 -- only the specific binding provided by this package. It assumes that
61 -- the reader is already familiar with general network programming and
62 -- sockets usage. A useful reference on this matter is W. Richard Stevens'
63 -- "UNIX Network Programming: The Sockets Networking API"
64 -- (ISBN: 0131411551).
66 -- GNAT.Sockets has been designed with several ideas in mind
68 -- This is a system independent interface. Therefore, we try as much as
69 -- possible to mask system incompatibilities. Some functionalities are not
70 -- available because there are not fully supported on some systems.
72 -- This is a thick binding. For instance, a major effort has been done to
73 -- avoid using memory addresses or untyped ints. We preferred to define
74 -- streams and enumeration types. Errors are not returned as returned
75 -- values but as exceptions.
77 -- This package provides a POSIX-compliant interface (between two
78 -- different implementations of the same routine, we adopt the one closest
79 -- to the POSIX specification). For instance, using select(), the
80 -- notification of an asynchronous connect failure is delivered in the
81 -- write socket set (POSIX) instead of the exception socket set (NT).
83 -- The example below demonstrates various features of GNAT.Sockets:
85 -- with GNAT.Sockets; use GNAT.Sockets;
87 -- with Ada.Text_IO;
88 -- with Ada.Exceptions; use Ada.Exceptions;
90 -- procedure PingPong is
92 -- Group : constant String := "239.255.128.128";
93 -- -- Multicast group: administratively scoped IP address
95 -- task Pong is
96 -- entry Start;
97 -- entry Stop;
98 -- end Pong;
100 -- task body Pong is
101 -- Address : Sock_Addr_Type;
102 -- Server : Socket_Type;
103 -- Socket : Socket_Type;
104 -- Channel : Stream_Access;
106 -- begin
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
127 -- Set_Socket_Option
128 -- (Server,
129 -- Socket_Level,
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 Start;
145 -- Accept_Socket (Server, Socket, Address);
147 -- -- Return a stream associated to the connected socket
149 -- Channel := Stream (Socket);
151 -- -- Force Pong to block
153 -- delay 0.2;
155 -- -- Receive and print message from client Ping
157 -- declare
158 -- Message : String := String'Input (Channel);
160 -- begin
161 -- Ada.Text_IO.Put_Line (Message);
163 -- -- Send same message back to client Ping
165 -- String'Output (Channel, Message);
166 -- end;
168 -- Close_Socket (Server);
169 -- Close_Socket (Socket);
171 -- -- Part of the multicast example
173 -- -- Create a datagram socket to send connectionless, unreliable
174 -- -- messages of a fixed maximum length.
176 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
178 -- -- Allow reuse of local addresses
180 -- Set_Socket_Option
181 -- (Socket,
182 -- Socket_Level,
183 -- (Reuse_Address, True));
185 -- -- Controls the live time of the datagram to avoid it being
186 -- -- looped forever due to routing errors. Routers decrement
187 -- -- the TTL of every datagram as it traverses from one network
188 -- -- to another and when its value reaches 0 the packet is
189 -- -- dropped. Default is 1.
191 -- Set_Socket_Option
192 -- (Socket,
193 -- IP_Protocol_For_IP_Level,
194 -- (Multicast_TTL, 1));
196 -- -- Want the data you send to be looped back to your host
198 -- Set_Socket_Option
199 -- (Socket,
200 -- IP_Protocol_For_IP_Level,
201 -- (Multicast_Loop, True));
203 -- -- If this socket is intended to receive messages, bind it
204 -- -- to a given socket address.
206 -- Address.Addr := Any_Inet_Addr;
207 -- Address.Port := 55505;
209 -- Bind_Socket (Socket, Address);
211 -- -- Join a multicast group
213 -- -- Portability note: On Windows, this option may be set only
214 -- -- on a bound socket.
216 -- Set_Socket_Option
217 -- (Socket,
218 -- IP_Protocol_For_IP_Level,
219 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
221 -- -- If this socket is intended to send messages, provide the
222 -- -- receiver socket address.
224 -- Address.Addr := Inet_Addr (Group);
225 -- Address.Port := 55506;
227 -- Channel := Stream (Socket, Address);
229 -- -- Receive and print message from client Ping
231 -- declare
232 -- Message : String := String'Input (Channel);
234 -- begin
235 -- -- Get the address of the sender
237 -- Address := Get_Address (Channel);
238 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
240 -- -- Send same message back to client Ping
242 -- String'Output (Channel, Message);
243 -- end;
245 -- Close_Socket (Socket);
247 -- accept Stop;
249 -- exception when E : others =>
250 -- Ada.Text_IO.Put_Line
251 -- (Exception_Name (E) & ": " & Exception_Message (E));
252 -- end Pong;
254 -- task Ping is
255 -- entry Start;
256 -- entry Stop;
257 -- end Ping;
259 -- task body Ping is
260 -- Address : Sock_Addr_Type;
261 -- Socket : Socket_Type;
262 -- Channel : Stream_Access;
264 -- begin
265 -- accept Start;
267 -- -- See comments in Ping section for the first steps
269 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
270 -- Address.Port := 5876;
271 -- Create_Socket (Socket);
273 -- Set_Socket_Option
274 -- (Socket,
275 -- Socket_Level,
276 -- (Reuse_Address, True));
278 -- -- Force Ping to block
280 -- delay 0.2;
282 -- -- If the client's socket is not bound, Connect_Socket will
283 -- -- bind to an unused address. The client uses Connect_Socket to
284 -- -- create a logical connection between the client's socket and
285 -- -- a server's socket returned by Accept_Socket.
287 -- Connect_Socket (Socket, Address);
289 -- Channel := Stream (Socket);
291 -- -- Send message to server Pong
293 -- String'Output (Channel, "Hello world");
295 -- -- Force Ping to block
297 -- delay 0.2;
299 -- -- Receive and print message from server Pong
301 -- Ada.Text_IO.Put_Line (String'Input (Channel));
302 -- Close_Socket (Socket);
304 -- -- Part of multicast example. Code similar to Pong's one
306 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
308 -- Set_Socket_Option
309 -- (Socket,
310 -- Socket_Level,
311 -- (Reuse_Address, True));
313 -- Set_Socket_Option
314 -- (Socket,
315 -- IP_Protocol_For_IP_Level,
316 -- (Multicast_TTL, 1));
318 -- Set_Socket_Option
319 -- (Socket,
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 -- Set_Socket_Option
329 -- (Socket,
330 -- IP_Protocol_For_IP_Level,
331 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
333 -- Address.Addr := Inet_Addr (Group);
334 -- Address.Port := 55505;
336 -- Channel := Stream (Socket, Address);
338 -- -- Send message to server Pong
340 -- String'Output (Channel, "Hello world");
342 -- -- Receive and print message from server Pong
344 -- declare
345 -- Message : String := String'Input (Channel);
347 -- begin
348 -- Address := Get_Address (Channel);
349 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
350 -- end;
352 -- Close_Socket (Socket);
354 -- accept Stop;
356 -- exception when E : others =>
357 -- Ada.Text_IO.Put_Line
358 -- (Exception_Name (E) & ": " & Exception_Message (E));
359 -- end Ping;
361 -- begin
362 -- Initialize;
363 -- Ping.Start;
364 -- Pong.Start;
365 -- Ping.Stop;
366 -- Pong.Stop;
367 -- Finalize;
368 -- end PingPong;
370 package SOSC renames System.OS_Constants;
371 -- Renaming used to provide short-hand notations throughout the sockets
372 -- binding. Note that System.OS_Constants is an internal unit, and the
373 -- entities declared therein are not meant for direct access by users,
374 -- including through this renaming.
376 procedure Initialize;
377 pragma Obsolescent
378 (Entity => Initialize,
379 Message => "explicit initialization is no longer required");
380 -- Initialize must be called before using any other socket routines.
381 -- Note that this operation is a no-op on UNIX platforms, but applications
382 -- should make sure to call it if portability is expected: some platforms
383 -- (such as Windows) require initialization before any socket operation.
384 -- This is now a no-op (initialization and finalization are done
385 -- automatically).
387 procedure Initialize (Process_Blocking_IO : Boolean);
388 pragma Obsolescent
389 (Entity => Initialize,
390 Message => "passing a parameter to Initialize is no longer supported");
391 -- Previous versions of GNAT.Sockets used to require the user to indicate
392 -- whether socket I/O was process- or thread-blocking on the platform.
393 -- This property is now determined automatically when the run-time library
394 -- is built. The old version of Initialize, taking a parameter, is kept
395 -- for compatibility reasons, but this interface is obsolete (and if the
396 -- value given is wrong, an exception will be raised at run time).
397 -- This is now a no-op (initialization and finalization are done
398 -- automatically).
400 procedure Finalize;
401 pragma Obsolescent
402 (Entity => Finalize,
403 Message => "explicit finalization is no longer required");
404 -- After Finalize is called it is not possible to use any routines
405 -- exported in by this package. This procedure is idempotent.
406 -- This is now a no-op (initialization and finalization are done
407 -- automatically).
409 type Socket_Type is private;
410 -- Sockets are used to implement a reliable bi-directional point-to-point,
411 -- stream-based connections between hosts. No_Socket provides a special
412 -- value to denote uninitialized sockets.
414 No_Socket : constant Socket_Type;
416 type Selector_Type is limited private;
417 type Selector_Access is access all Selector_Type;
418 -- Selector objects are used to wait for i/o events to occur on sockets
420 Null_Selector : constant Selector_Type;
421 -- The Null_Selector can be used in place of a normal selector without
422 -- having to call Create_Selector if the use of Abort_Selector is not
423 -- required.
425 -- Timeval_Duration is a subtype of Standard.Duration because the full
426 -- range of Standard.Duration cannot be represented in the equivalent C
427 -- structure (struct timeval). Moreover, negative values are not allowed
428 -- to avoid system incompatibilities.
430 Immediate : constant Duration := 0.0;
432 Forever : constant Duration :=
433 Duration'Min (Duration'Last, 1.0 * SOSC.MAX_tv_sec);
434 -- Largest possible Duration that is also a valid value for struct timeval
436 subtype Timeval_Duration is Duration range Immediate .. Forever;
438 subtype Selector_Duration is Timeval_Duration;
439 -- Timeout value for selector operations
441 type Selector_Status is (Completed, Expired, Aborted);
442 -- Completion status of a selector operation, indicated as follows:
443 -- Complete: one of the expected events occurred
444 -- Expired: no event occurred before the expiration of the timeout
445 -- Aborted: an external action cancelled the wait operation before
446 -- any event occurred.
448 Socket_Error : exception;
449 -- There is only one exception in this package to deal with an error during
450 -- a socket routine. Once raised, its message contains a string describing
451 -- the error code.
453 function Image (Socket : Socket_Type) return String;
454 -- Return a printable string for Socket
456 function To_C (Socket : Socket_Type) return Integer;
457 -- Return a file descriptor to be used by external subprograms. This is
458 -- useful for C functions that are not yet interfaced in this package.
460 type Family_Type is (Family_Inet, Family_Inet6);
461 -- Address family (or protocol family) identifies the communication domain
462 -- and groups protocols with similar address formats.
464 type Mode_Type is (Socket_Stream, Socket_Datagram);
465 -- Stream sockets provide connection-oriented byte streams. Datagram
466 -- sockets support unreliable connectionless message based communication.
468 type Shutmode_Type is (Shut_Read, Shut_Write, Shut_Read_Write);
469 -- When a process closes a socket, the policy is to retain any data queued
470 -- until either a delivery or a timeout expiration (in this case, the data
471 -- are discarded). A finer control is available through shutdown. With
472 -- Shut_Read, no more data can be received from the socket. With_Write, no
473 -- more data can be transmitted. Neither transmission nor reception can be
474 -- performed with Shut_Read_Write.
476 type Port_Type is range 0 .. 16#ffff#;
477 -- TCP/UDP port number
479 Any_Port : constant Port_Type;
480 -- All ports
482 No_Port : constant Port_Type;
483 -- Uninitialized port number
485 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
486 -- An Internet address depends on an address family (IPv4 contains 4 octets
487 -- and IPv6 contains 16 octets). Any_Inet_Addr is a special value treated
488 -- like a wildcard enabling all addresses. No_Inet_Addr provides a special
489 -- value to denote uninitialized inet addresses.
491 Any_Inet_Addr : constant Inet_Addr_Type;
492 No_Inet_Addr : constant Inet_Addr_Type;
493 Broadcast_Inet_Addr : constant Inet_Addr_Type;
494 Loopback_Inet_Addr : constant Inet_Addr_Type;
496 -- Useful constants for IPv4 multicast addresses
498 Unspecified_Group_Inet_Addr : constant Inet_Addr_Type;
499 All_Hosts_Group_Inet_Addr : constant Inet_Addr_Type;
500 All_Routers_Group_Inet_Addr : constant Inet_Addr_Type;
502 type Sock_Addr_Type (Family : Family_Type := Family_Inet) is record
503 Addr : Inet_Addr_Type (Family);
504 Port : Port_Type;
505 end record;
506 -- Socket addresses fully define a socket connection with protocol family,
507 -- an Internet address and a port. No_Sock_Addr provides a special value
508 -- for uninitialized socket addresses.
510 No_Sock_Addr : constant Sock_Addr_Type;
512 function Image (Value : Inet_Addr_Type) return String;
513 -- Return an image of an Internet address. IPv4 notation consists in 4
514 -- octets in decimal format separated by dots. IPv6 notation consists in
515 -- 16 octets in hexadecimal format separated by colons (and possibly
516 -- dots).
518 function Image (Value : Sock_Addr_Type) return String;
519 -- Return inet address image and port image separated by a colon
521 function Inet_Addr (Image : String) return Inet_Addr_Type;
522 -- Convert address image from numbers-and-dots notation into an
523 -- inet address.
525 -- Host entries provide complete information on a given host: the official
526 -- name, an array of alternative names or aliases and array of network
527 -- addresses.
529 type Host_Entry_Type
530 (Aliases_Length, Addresses_Length : Natural) is private;
532 function Official_Name (E : Host_Entry_Type) return String;
533 -- Return official name in host entry
535 function Aliases_Length (E : Host_Entry_Type) return Natural;
536 -- Return number of aliases in host entry
538 function Addresses_Length (E : Host_Entry_Type) return Natural;
539 -- Return number of addresses in host entry
541 function Aliases
542 (E : Host_Entry_Type;
543 N : Positive := 1) return String;
544 -- Return N'th aliases in host entry. The first index is 1
546 function Addresses
547 (E : Host_Entry_Type;
548 N : Positive := 1) return Inet_Addr_Type;
549 -- Return N'th addresses in host entry. The first index is 1
551 Host_Error : exception;
552 -- Exception raised by the two following procedures. Once raised, its
553 -- message contains a string describing the error code. This exception is
554 -- raised when an host entry cannot be retrieved.
556 function Get_Host_By_Address
557 (Address : Inet_Addr_Type;
558 Family : Family_Type := Family_Inet) return Host_Entry_Type;
559 -- Return host entry structure for the given Inet address. Note that no
560 -- result will be returned if there is no mapping of this IP address to a
561 -- host name in the system tables (host database, DNS or otherwise).
563 function Get_Host_By_Name
564 (Name : String) return Host_Entry_Type;
565 -- Return host entry structure for the given host name. Here name is
566 -- either a host name, or an IP address. If Name is an IP address, this
567 -- is equivalent to Get_Host_By_Address (Inet_Addr (Name)).
569 function Host_Name return String;
570 -- Return the name of the current host
572 type Service_Entry_Type (Aliases_Length : Natural) is private;
573 -- Service entries provide complete information on a given service: the
574 -- official name, an array of alternative names or aliases and the port
575 -- number.
577 function Official_Name (S : Service_Entry_Type) return String;
578 -- Return official name in service entry
580 function Port_Number (S : Service_Entry_Type) return Port_Type;
581 -- Return port number in service entry
583 function Protocol_Name (S : Service_Entry_Type) return String;
584 -- Return Protocol in service entry (usually UDP or TCP)
586 function Aliases_Length (S : Service_Entry_Type) return Natural;
587 -- Return number of aliases in service entry
589 function Aliases
590 (S : Service_Entry_Type;
591 N : Positive := 1) return String;
592 -- Return N'th aliases in service entry (the first index is 1)
594 function Get_Service_By_Name
595 (Name : String;
596 Protocol : String) return Service_Entry_Type;
597 -- Return service entry structure for the given service name
599 function Get_Service_By_Port
600 (Port : Port_Type;
601 Protocol : String) return Service_Entry_Type;
602 -- Return service entry structure for the given service port number
604 Service_Error : exception;
605 -- Comment required ???
607 -- Errors are described by an enumeration type. There is only one exception
608 -- Socket_Error in this package to deal with an error during a socket
609 -- routine. Once raised, its message contains the error code between
610 -- brackets and a string describing the error code.
612 -- The name of the enumeration constant documents the error condition
613 -- Note that on some platforms, a single error value is used for both
614 -- EWOULDBLOCK and EAGAIN. Both errors are therefore always reported as
615 -- Resource_Temporarily_Unavailable.
617 type Error_Type is
618 (Success,
619 Permission_Denied,
620 Address_Already_In_Use,
621 Cannot_Assign_Requested_Address,
622 Address_Family_Not_Supported_By_Protocol,
623 Operation_Already_In_Progress,
624 Bad_File_Descriptor,
625 Software_Caused_Connection_Abort,
626 Connection_Refused,
627 Connection_Reset_By_Peer,
628 Destination_Address_Required,
629 Bad_Address,
630 Host_Is_Down,
631 No_Route_To_Host,
632 Operation_Now_In_Progress,
633 Interrupted_System_Call,
634 Invalid_Argument,
635 Input_Output_Error,
636 Transport_Endpoint_Already_Connected,
637 Too_Many_Symbolic_Links,
638 Too_Many_Open_Files,
639 Message_Too_Long,
640 File_Name_Too_Long,
641 Network_Is_Down,
642 Network_Dropped_Connection_Because_Of_Reset,
643 Network_Is_Unreachable,
644 No_Buffer_Space_Available,
645 Protocol_Not_Available,
646 Transport_Endpoint_Not_Connected,
647 Socket_Operation_On_Non_Socket,
648 Operation_Not_Supported,
649 Protocol_Family_Not_Supported,
650 Protocol_Not_Supported,
651 Protocol_Wrong_Type_For_Socket,
652 Cannot_Send_After_Transport_Endpoint_Shutdown,
653 Socket_Type_Not_Supported,
654 Connection_Timed_Out,
655 Too_Many_References,
656 Resource_Temporarily_Unavailable,
657 Broken_Pipe,
658 Unknown_Host,
659 Host_Name_Lookup_Failure,
660 Non_Recoverable_Error,
661 Unknown_Server_Error,
662 Cannot_Resolve_Error);
664 -- Get_Socket_Options and Set_Socket_Options manipulate options associated
665 -- with a socket. Options may exist at multiple protocol levels in the
666 -- communication stack. Socket_Level is the uppermost socket level.
668 type Level_Type is
669 (Socket_Level,
670 IP_Protocol_For_IP_Level,
671 IP_Protocol_For_UDP_Level,
672 IP_Protocol_For_TCP_Level);
674 -- There are several options available to manipulate sockets. Each option
675 -- has a name and several values available. Most of the time, the value is
676 -- a boolean to enable or disable this option.
678 type Option_Name is
679 (Keep_Alive, -- Enable sending of keep-alive messages
680 Reuse_Address, -- Allow bind to reuse local address
681 Broadcast, -- Enable datagram sockets to recv/send broadcasts
682 Send_Buffer, -- Set/get the maximum socket send buffer in bytes
683 Receive_Buffer, -- Set/get the maximum socket recv buffer in bytes
684 Linger, -- Shutdown wait for msg to be sent or timeout occur
685 Error, -- Get and clear the pending socket error
686 No_Delay, -- Do not delay send to coalesce data (TCP_NODELAY)
687 Add_Membership, -- Join a multicast group
688 Drop_Membership, -- Leave a multicast group
689 Multicast_If, -- Set default out interface for multicast packets
690 Multicast_TTL, -- Set the time-to-live of sent multicast packets
691 Multicast_Loop, -- Sent multicast packets are looped to local socket
692 Receive_Packet_Info, -- Receive low level packet info as ancillary data
693 Send_Timeout, -- Set timeout value for output
694 Receive_Timeout); -- Set timeout value for input
696 type Option_Type (Name : Option_Name := Keep_Alive) is record
697 case Name is
698 when Keep_Alive |
699 Reuse_Address |
700 Broadcast |
701 Linger |
702 No_Delay |
703 Receive_Packet_Info |
704 Multicast_Loop =>
705 Enabled : Boolean;
707 case Name is
708 when Linger =>
709 Seconds : Natural;
710 when others =>
711 null;
712 end case;
714 when Send_Buffer |
715 Receive_Buffer =>
716 Size : Natural;
718 when Error =>
719 Error : Error_Type;
721 when Add_Membership |
722 Drop_Membership =>
723 Multicast_Address : Inet_Addr_Type;
724 Local_Interface : Inet_Addr_Type;
726 when Multicast_If =>
727 Outgoing_If : Inet_Addr_Type;
729 when Multicast_TTL =>
730 Time_To_Live : Natural;
732 when Send_Timeout |
733 Receive_Timeout =>
734 Timeout : Timeval_Duration;
736 end case;
737 end record;
739 -- There are several controls available to manipulate sockets. Each option
740 -- has a name and several values available. These controls differ from the
741 -- socket options in that they are not specific to sockets but are
742 -- available for any device.
744 type Request_Name is
745 (Non_Blocking_IO, -- Cause a caller not to wait on blocking operations
746 N_Bytes_To_Read); -- Return the number of bytes available to read
748 type Request_Type (Name : Request_Name := Non_Blocking_IO) is record
749 case Name is
750 when Non_Blocking_IO =>
751 Enabled : Boolean;
753 when N_Bytes_To_Read =>
754 Size : Natural;
756 end case;
757 end record;
759 -- A request flag allows specification of the type of message transmissions
760 -- or receptions. A request flag can be combination of zero or more
761 -- predefined request flags.
763 type Request_Flag_Type is private;
765 No_Request_Flag : constant Request_Flag_Type;
766 -- This flag corresponds to the normal execution of an operation
768 Process_Out_Of_Band_Data : constant Request_Flag_Type;
769 -- This flag requests that the receive or send function operates on
770 -- out-of-band data when the socket supports this notion (e.g.
771 -- Socket_Stream).
773 Peek_At_Incoming_Data : constant Request_Flag_Type;
774 -- This flag causes the receive operation to return data from the beginning
775 -- of the receive queue without removing that data from the queue. A
776 -- subsequent receive call will return the same data.
778 Wait_For_A_Full_Reception : constant Request_Flag_Type;
779 -- This flag requests that the operation block until the full request is
780 -- satisfied. However, the call may still return less data than requested
781 -- if a signal is caught, an error or disconnect occurs, or the next data
782 -- to be received is of a different type than that returned. Note that
783 -- this flag depends on support in the underlying sockets implementation,
784 -- and is not supported under Windows.
786 Send_End_Of_Record : constant Request_Flag_Type;
787 -- This flag indicates that the entire message has been sent and so this
788 -- terminates the record.
790 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type;
791 -- Combine flag L with flag R
793 type Stream_Element_Reference is access all Ada.Streams.Stream_Element;
795 type Vector_Element is record
796 Base : Stream_Element_Reference;
797 Length : Interfaces.C.size_t;
798 end record;
800 type Vector_Type is array (Integer range <>) of Vector_Element;
802 procedure Create_Socket
803 (Socket : out Socket_Type;
804 Family : Family_Type := Family_Inet;
805 Mode : Mode_Type := Socket_Stream);
806 -- Create an endpoint for communication. Raises Socket_Error on error
808 procedure Accept_Socket
809 (Server : Socket_Type;
810 Socket : out Socket_Type;
811 Address : out Sock_Addr_Type);
812 -- Extracts the first connection request on the queue of pending
813 -- connections, creates a new connected socket with mostly the same
814 -- properties as Server, and allocates a new socket. The returned Address
815 -- is filled in with the address of the connection. Raises Socket_Error on
816 -- error. Note: if Server is a non-blocking socket, whether or not this
817 -- aspect is inherited by Socket is platform-dependent.
819 procedure Accept_Socket
820 (Server : Socket_Type;
821 Socket : out Socket_Type;
822 Address : out Sock_Addr_Type;
823 Timeout : Selector_Duration;
824 Selector : access Selector_Type := null;
825 Status : out Selector_Status);
826 -- Accept a new connection on Server using Accept_Socket, waiting no longer
827 -- than the given timeout duration. Status is set to indicate whether the
828 -- operation completed successfully, timed out, or was aborted. If Selector
829 -- is not null, the designated selector is used to wait for the socket to
830 -- become available, else a private selector object is created by this
831 -- procedure and destroyed before it returns.
833 procedure Bind_Socket
834 (Socket : Socket_Type;
835 Address : Sock_Addr_Type);
836 -- Once a socket is created, assign a local address to it. Raise
837 -- Socket_Error on error.
839 procedure Close_Socket (Socket : Socket_Type);
840 -- Close a socket and more specifically a non-connected socket
842 procedure Connect_Socket
843 (Socket : Socket_Type;
844 Server : Sock_Addr_Type);
845 -- Make a connection to another socket which has the address of Server.
846 -- Raises Socket_Error on error.
848 procedure Connect_Socket
849 (Socket : Socket_Type;
850 Server : Sock_Addr_Type;
851 Timeout : Selector_Duration;
852 Selector : access Selector_Type := null;
853 Status : out Selector_Status);
854 -- Connect Socket to the given Server address using Connect_Socket, waiting
855 -- no longer than the given timeout duration. Status is set to indicate
856 -- whether the operation completed successfully, timed out, or was aborted.
857 -- If Selector is not null, the designated selector is used to wait for the
858 -- socket to become available, else a private selector object is created
859 -- by this procedure and destroyed before it returns. If Timeout is 0.0,
860 -- no attempt is made to detect whether the connection has succeeded; it
861 -- is up to the user to determine this using Check_Selector later on.
863 procedure Control_Socket
864 (Socket : Socket_Type;
865 Request : in out Request_Type);
866 -- Obtain or set parameter values that control the socket. This control
867 -- differs from the socket options in that they are not specific to sockets
868 -- but are available for any device.
870 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
871 -- Return the peer or remote socket address of a socket. Raise
872 -- Socket_Error on error.
874 function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
875 -- Return the local or current socket address of a socket. Return
876 -- No_Sock_Addr on error (e.g. socket closed or not locally bound).
878 function Get_Socket_Option
879 (Socket : Socket_Type;
880 Level : Level_Type := Socket_Level;
881 Name : Option_Name) return Option_Type;
882 -- Get the options associated with a socket. Raises Socket_Error on error
884 procedure Listen_Socket
885 (Socket : Socket_Type;
886 Length : Natural := 15);
887 -- To accept connections, a socket is first created with Create_Socket,
888 -- a willingness to accept incoming connections and a queue Length for
889 -- incoming connections are specified. Raise Socket_Error on error.
890 -- The queue length of 15 is an example value that should be appropriate
891 -- in usual cases. It can be adjusted according to each application's
892 -- particular requirements.
894 procedure Receive_Socket
895 (Socket : Socket_Type;
896 Item : out Ada.Streams.Stream_Element_Array;
897 Last : out Ada.Streams.Stream_Element_Offset;
898 Flags : Request_Flag_Type := No_Request_Flag);
899 -- Receive message from Socket. Last is the index value such that Item
900 -- (Last) is the last character assigned. Note that Last is set to
901 -- Item'First - 1 when the socket has been closed by peer. This is not
902 -- an error, and no exception is raised in this case unless Item'First
903 -- is Stream_Element_Offset'First, in which case Constraint_Error is
904 -- raised. Flags allows control of the reception. Raise Socket_Error on
905 -- error.
907 procedure Receive_Socket
908 (Socket : Socket_Type;
909 Item : out Ada.Streams.Stream_Element_Array;
910 Last : out Ada.Streams.Stream_Element_Offset;
911 From : out Sock_Addr_Type;
912 Flags : Request_Flag_Type := No_Request_Flag);
913 -- Receive message from Socket. If Socket is not connection-oriented, the
914 -- source address From of the message is filled in. Last is the index
915 -- value such that Item (Last) is the last character assigned. Flags
916 -- allows control of the reception. Raises Socket_Error on error.
918 procedure Receive_Vector
919 (Socket : Socket_Type;
920 Vector : Vector_Type;
921 Count : out Ada.Streams.Stream_Element_Count;
922 Flags : Request_Flag_Type := No_Request_Flag);
923 -- Receive data from a socket and scatter it into the set of vector
924 -- elements Vector. Count is set to the count of received stream elements.
925 -- Flags allow control over reception.
927 function Resolve_Exception
928 (Occurrence : Ada.Exceptions.Exception_Occurrence) return Error_Type;
929 -- When Socket_Error or Host_Error are raised, the exception message
930 -- contains the error code between brackets and a string describing the
931 -- error code. Resolve_Error extracts the error code from an exception
932 -- message and translate it into an enumeration value.
934 procedure Send_Socket
935 (Socket : Socket_Type;
936 Item : Ada.Streams.Stream_Element_Array;
937 Last : out Ada.Streams.Stream_Element_Offset;
938 To : access Sock_Addr_Type;
939 Flags : Request_Flag_Type := No_Request_Flag);
940 pragma Inline (Send_Socket);
941 -- Transmit a message over a socket. For a datagram socket, the address
942 -- is given by To.all. For a stream socket, To must be null. Last
943 -- is the index value such that Item (Last) is the last character
944 -- sent. Note that Last is set to Item'First - 1 if the socket has been
945 -- closed by the peer (unless Item'First is Stream_Element_Offset'First,
946 -- in which case Constraint_Error is raised instead). This is not an error,
947 -- and Socket_Error is not raised in that case. Flags allows control of the
948 -- transmission. Raises exception Socket_Error on error. Note: this
949 -- subprogram is inlined because it is also used to implement the two
950 -- variants below.
952 procedure Send_Socket
953 (Socket : Socket_Type;
954 Item : Ada.Streams.Stream_Element_Array;
955 Last : out Ada.Streams.Stream_Element_Offset;
956 Flags : Request_Flag_Type := No_Request_Flag);
957 -- Transmit a message over a socket. Upon return, Last is set to the index
958 -- within Item of the last element transmitted. Flags allows control of
959 -- the transmission. Raises Socket_Error on any detected error condition.
961 procedure Send_Socket
962 (Socket : Socket_Type;
963 Item : Ada.Streams.Stream_Element_Array;
964 Last : out Ada.Streams.Stream_Element_Offset;
965 To : Sock_Addr_Type;
966 Flags : Request_Flag_Type := No_Request_Flag);
967 -- Transmit a message over a datagram socket. The destination address is
968 -- To. Flags allows control of the transmission. Raises Socket_Error on
969 -- error.
971 procedure Send_Vector
972 (Socket : Socket_Type;
973 Vector : Vector_Type;
974 Count : out Ada.Streams.Stream_Element_Count;
975 Flags : Request_Flag_Type := No_Request_Flag);
976 -- Transmit data gathered from the set of vector elements Vector to a
977 -- socket. Count is set to the count of transmitted stream elements. Flags
978 -- allow control over transmission.
980 procedure Set_Close_On_Exec
981 (Socket : Socket_Type;
982 Close_On_Exec : Boolean;
983 Status : out Boolean);
984 -- When Close_On_Exec is True, mark Socket to be closed automatically when
985 -- a new program is executed by the calling process (i.e. prevent Socket
986 -- from being inherited by child processes). When Close_On_Exec is False,
987 -- mark Socket to not be closed on exec (i.e. allow it to be inherited).
988 -- Status is False if the operation could not be performed, or is not
989 -- supported on the target platform.
991 procedure Set_Socket_Option
992 (Socket : Socket_Type;
993 Level : Level_Type := Socket_Level;
994 Option : Option_Type);
995 -- Manipulate socket options. Raises Socket_Error on error
997 procedure Shutdown_Socket
998 (Socket : Socket_Type;
999 How : Shutmode_Type := Shut_Read_Write);
1000 -- Shutdown a connected socket. If How is Shut_Read further receives will
1001 -- be disallowed. If How is Shut_Write further sends will be disallowed.
1002 -- If How is Shut_Read_Write further sends and receives will be disallowed.
1004 type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
1005 -- Same interface as Ada.Streams.Stream_IO
1007 function Stream (Socket : Socket_Type) return Stream_Access;
1008 -- Create a stream associated with an already connected stream-based socket
1010 function Stream
1011 (Socket : Socket_Type;
1012 Send_To : Sock_Addr_Type) return Stream_Access;
1013 -- Create a stream associated with an already bound datagram-based socket.
1014 -- Send_To is the destination address to which messages are being sent.
1016 function Get_Address
1017 (Stream : not null Stream_Access) return Sock_Addr_Type;
1018 -- Return the socket address from which the last message was received
1020 procedure Free is new Ada.Unchecked_Deallocation
1021 (Ada.Streams.Root_Stream_Type'Class, Stream_Access);
1022 -- Destroy a stream created by one of the Stream functions above, releasing
1023 -- the corresponding resources. The user is responsible for calling this
1024 -- subprogram when the stream is not needed anymore.
1026 type Socket_Set_Type is limited private;
1027 -- This type allows manipulation of sets of sockets. It allows waiting
1028 -- for events on multiple endpoints at one time. This type has default
1029 -- initialization, and the default value is the empty set.
1031 -- Note: This type used to contain a pointer to dynamically allocated
1032 -- storage, but this is not the case anymore, and no special precautions
1033 -- are required to avoid memory leaks.
1035 procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
1036 -- Remove Socket from Item
1038 procedure Copy (Source : Socket_Set_Type; Target : out Socket_Set_Type);
1039 -- Copy Source into Target as Socket_Set_Type is limited private
1041 procedure Empty (Item : out Socket_Set_Type);
1042 -- Remove all Sockets from Item
1044 procedure Get (Item : in out Socket_Set_Type; Socket : out Socket_Type);
1045 -- Extract a Socket from socket set Item. Socket is set to
1046 -- No_Socket when the set is empty.
1048 function Is_Empty (Item : Socket_Set_Type) return Boolean;
1049 -- Return True iff Item is empty
1051 function Is_Set
1052 (Item : Socket_Set_Type;
1053 Socket : Socket_Type) return Boolean;
1054 -- Return True iff Socket is present in Item
1056 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type);
1057 -- Insert Socket into Item
1059 function Image (Item : Socket_Set_Type) return String;
1060 -- Return a printable image of Item, for debugging purposes
1062 -- The select(2) system call waits for events to occur on any of a set of
1063 -- file descriptors. Usually, three independent sets of descriptors are
1064 -- watched (read, write and exception). A timeout gives an upper bound
1065 -- on the amount of time elapsed before select returns. This function
1066 -- blocks until an event occurs. On some platforms, the select(2) system
1067 -- can block the full process (not just the calling thread).
1069 -- Check_Selector provides the very same behaviour. The only difference is
1070 -- that it does not watch for exception events. Note that on some platforms
1071 -- it is kept process blocking on purpose. The timeout parameter allows the
1072 -- user to have the behaviour he wants. Abort_Selector allows the safe
1073 -- abort of a blocked Check_Selector call. A special socket is opened by
1074 -- Create_Selector and included in each call to Check_Selector.
1076 -- Abort_Selector causes an event to occur on this descriptor in order to
1077 -- unblock Check_Selector. Note that each call to Abort_Selector will cause
1078 -- exactly one call to Check_Selector to return with Aborted status. The
1079 -- special socket created by Create_Selector is closed when Close_Selector
1080 -- is called.
1082 -- A typical case where it is useful to abort a Check_Selector operation is
1083 -- the situation where a change to the monitored sockets set must be made.
1085 procedure Create_Selector (Selector : out Selector_Type);
1086 -- Initialize (open) a new selector
1088 procedure Close_Selector (Selector : in out Selector_Type);
1089 -- Close Selector and all internal descriptors associated; deallocate any
1090 -- associated resources. This subprogram may be called only when there is
1091 -- no other task still using Selector (i.e. still executing Check_Selector
1092 -- or Abort_Selector on this Selector). Has no effect if Selector is
1093 -- already closed.
1095 procedure Check_Selector
1096 (Selector : Selector_Type;
1097 R_Socket_Set : in out Socket_Set_Type;
1098 W_Socket_Set : in out Socket_Set_Type;
1099 Status : out Selector_Status;
1100 Timeout : Selector_Duration := Forever);
1101 -- Return when one Socket in R_Socket_Set has some data to be read or if
1102 -- one Socket in W_Socket_Set is ready to transmit some data. In these
1103 -- cases Status is set to Completed and sockets that are ready are set in
1104 -- R_Socket_Set or W_Socket_Set. Status is set to Expired if no socket was
1105 -- ready after a Timeout expiration. Status is set to Aborted if an abort
1106 -- signal has been received while checking socket status.
1108 -- Note that two different Socket_Set_Type objects must be passed as
1109 -- R_Socket_Set and W_Socket_Set (even if they denote the same set of
1110 -- Sockets), or some event may be lost.
1112 -- Socket_Error is raised when the select(2) system call returns an error
1113 -- condition, or when a read error occurs on the signalling socket used for
1114 -- the implementation of Abort_Selector.
1116 procedure Check_Selector
1117 (Selector : Selector_Type;
1118 R_Socket_Set : in out Socket_Set_Type;
1119 W_Socket_Set : in out Socket_Set_Type;
1120 E_Socket_Set : in out Socket_Set_Type;
1121 Status : out Selector_Status;
1122 Timeout : Selector_Duration := Forever);
1123 -- This refined version of Check_Selector allows watching for exception
1124 -- events (i.e. notifications of out-of-band transmission and reception).
1125 -- As above, all of R_Socket_Set, W_Socket_Set and E_Socket_Set must be
1126 -- different objects.
1128 procedure Abort_Selector (Selector : Selector_Type);
1129 -- Send an abort signal to the selector. The Selector may not be the
1130 -- Null_Selector.
1132 type Fd_Set is private;
1133 -- ??? This type must not be used directly, it needs to be visible because
1134 -- it is used in the visible part of GNAT.Sockets.Thin_Common. This is
1135 -- really an inversion of abstraction. The private part of GNAT.Sockets
1136 -- needs to have visibility on this type, but since Thin_Common is a child
1137 -- of Sockets, the type can't be declared there. The correct fix would
1138 -- be to move the thin sockets binding outside of GNAT.Sockets altogether,
1139 -- e.g. by renaming it to GNAT.Sockets_Thin.
1141 private
1143 type Socket_Type is new Integer;
1144 No_Socket : constant Socket_Type := -1;
1146 -- A selector is either a null selector, which is always "open" and can
1147 -- never be aborted, or a regular selector, which is created "closed",
1148 -- becomes "open" when Create_Selector is called, and "closed" again when
1149 -- Close_Selector is called.
1151 type Selector_Type (Is_Null : Boolean := False) is limited record
1152 case Is_Null is
1153 when True =>
1154 null;
1156 when False =>
1157 R_Sig_Socket : Socket_Type := No_Socket;
1158 W_Sig_Socket : Socket_Type := No_Socket;
1159 -- Signalling sockets used to abort a select operation
1160 end case;
1161 end record;
1163 pragma Volatile (Selector_Type);
1165 Null_Selector : constant Selector_Type := (Is_Null => True);
1167 type Fd_Set is
1168 new System.Storage_Elements.Storage_Array (1 .. SOSC.SIZEOF_fd_set);
1169 for Fd_Set'Alignment use Interfaces.C.long'Alignment;
1170 -- Set conservative alignment so that our Fd_Sets are always adequately
1171 -- aligned for the underlying data type (which is implementation defined
1172 -- and may be an array of C long integers).
1174 type Fd_Set_Access is access all Fd_Set;
1175 pragma Convention (C, Fd_Set_Access);
1176 No_Fd_Set_Access : constant Fd_Set_Access := null;
1178 type Socket_Set_Type is record
1179 Last : Socket_Type := No_Socket;
1180 -- Highest socket in set. Last = No_Socket denotes an empty set (which
1181 -- is the default initial value).
1183 Set : aliased Fd_Set;
1184 -- Underlying socket set. Note that the contents of this component is
1185 -- undefined if Last = No_Socket.
1186 end record;
1188 subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
1189 -- Octet for Internet address
1191 type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;
1193 subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 .. 4);
1194 subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);
1196 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
1197 case Family is
1198 when Family_Inet =>
1199 Sin_V4 : Inet_Addr_V4_Type := (others => 0);
1201 when Family_Inet6 =>
1202 Sin_V6 : Inet_Addr_V6_Type := (others => 0);
1203 end case;
1204 end record;
1206 Any_Port : constant Port_Type := 0;
1207 No_Port : constant Port_Type := 0;
1209 Any_Inet_Addr : constant Inet_Addr_Type :=
1210 (Family_Inet, (others => 0));
1211 No_Inet_Addr : constant Inet_Addr_Type :=
1212 (Family_Inet, (others => 0));
1213 Broadcast_Inet_Addr : constant Inet_Addr_Type :=
1214 (Family_Inet, (others => 255));
1215 Loopback_Inet_Addr : constant Inet_Addr_Type :=
1216 (Family_Inet, (127, 0, 0, 1));
1218 Unspecified_Group_Inet_Addr : constant Inet_Addr_Type :=
1219 (Family_Inet, (224, 0, 0, 0));
1220 All_Hosts_Group_Inet_Addr : constant Inet_Addr_Type :=
1221 (Family_Inet, (224, 0, 0, 1));
1222 All_Routers_Group_Inet_Addr : constant Inet_Addr_Type :=
1223 (Family_Inet, (224, 0, 0, 2));
1225 No_Sock_Addr : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
1227 Max_Name_Length : constant := 64;
1228 -- The constant MAXHOSTNAMELEN is usually set to 64
1230 subtype Name_Index is Natural range 1 .. Max_Name_Length;
1232 type Name_Type (Length : Name_Index := Max_Name_Length) is record
1233 Name : String (1 .. Length);
1234 end record;
1235 -- We need fixed strings to avoid access types in host entry type
1237 type Name_Array is array (Natural range <>) of Name_Type;
1238 type Inet_Addr_Array is array (Natural range <>) of Inet_Addr_Type;
1240 type Host_Entry_Type (Aliases_Length, Addresses_Length : Natural) is record
1241 Official : Name_Type;
1242 Aliases : Name_Array (1 .. Aliases_Length);
1243 Addresses : Inet_Addr_Array (1 .. Addresses_Length);
1244 end record;
1246 type Service_Entry_Type (Aliases_Length : Natural) is record
1247 Official : Name_Type;
1248 Aliases : Name_Array (1 .. Aliases_Length);
1249 Port : Port_Type;
1250 Protocol : Name_Type;
1251 end record;
1253 type Request_Flag_Type is mod 2 ** 8;
1254 No_Request_Flag : constant Request_Flag_Type := 0;
1255 Process_Out_Of_Band_Data : constant Request_Flag_Type := 1;
1256 Peek_At_Incoming_Data : constant Request_Flag_Type := 2;
1257 Wait_For_A_Full_Reception : constant Request_Flag_Type := 4;
1258 Send_End_Of_Record : constant Request_Flag_Type := 8;
1260 end GNAT.Sockets;