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