Remove some compile time warnings about duplicate definitions.
[official-gcc.git] / gcc / ada / g-socket.ads
blob4837ecef966058ef99974fb376b4eea191c34c96
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 -- $Revision: 1.1 $
10 -- --
11 -- Copyright (C) 2001 Ada Core Technologies, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
30 -- --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 -- --
33 ------------------------------------------------------------------------------
35 -- This package provides an interface to the sockets communication facility
36 -- provided on many operating systems. Currently this is implemented on all
37 -- native GNAT ports except for VMS. It is not yet implemented for any of
38 -- the cross-ports (e.g. it is not available for VxWorks or LynxOS).
39 -- Another restriction is that there is no multicast support under Windows
40 -- or under any system on which the multicast support is not available or
41 -- installed.
43 with Ada.Exceptions;
44 with Ada.Streams;
46 package GNAT.Sockets is
48 -- Sockets are designed to provide a consistent communication
49 -- facility between applications. This package provides an
50 -- Ada-like interface similar to the one proposed as part of the
51 -- BSD socket layer. This is a system independent thick binding.
52 -- Here is a typical example of what you can do.
54 -- with GNAT.Sockets; use GNAT.Sockets;
56 -- with Ada.Text_IO;
57 -- with Ada.Exceptions; use Ada.Exceptions;
59 -- procedure PingPong is
61 -- Group : constant String := "239.255.128.128";
62 -- -- Multicast groupe: administratively scoped IP address
64 -- task Pong is
65 -- entry Start;
66 -- entry Stop;
67 -- end Pong;
69 -- task body Pong is
70 -- Address : Sock_Addr_Type;
71 -- Server : Socket_Type;
72 -- Socket : Socket_Type;
73 -- Channel : Stream_Access;
75 -- begin
76 -- accept Start;
78 -- -- Get an Internet address of a host (here "localhost").
79 -- -- Note that a host can have several addresses. Here we get
80 -- -- the first one which is supposed to be the official one.
82 -- Address.Addr := Addresses (Get_Host_By_Name ("localhost"), 1);
84 -- -- Get a socket address that is an Internet address and a port
86 -- Address.Port := 5432;
88 -- -- The first step is to create a socket. Once created, this
89 -- -- socket must be associated to with an address. Usually only a
90 -- -- server (Pong here) needs to bind an address explicitly.
91 -- -- Most of the time clients can skip this step because the
92 -- -- socket routines will bind an arbitrary address to an unbound
93 -- -- socket.
95 -- Create_Socket (Server);
97 -- -- Allow reuse of local addresses.
99 -- Set_Socket_Option
100 -- (Server,
101 -- Socket_Level,
102 -- (Reuse_Address, True));
104 -- Bind_Socket (Server, Address);
106 -- -- A server marks a socket as willing to receive connect events.
108 -- Listen_Socket (Server);
110 -- -- Once a server calls Listen_Socket, incoming connects events
111 -- -- can be accepted. The returned Socket is a new socket that
112 -- -- represents the server side of the connection. Server remains
113 -- -- available to receive further connections.
115 -- Accept_Socket (Server, Socket, Address);
117 -- -- Return a stream associated to the connected socket.
119 -- Channel := Stream (Socket);
121 -- -- Force Pong to block
123 -- delay 0.2;
125 -- -- Receive and print message from client Ping.
127 -- declare
128 -- Message : String := String'Input (Channel);
130 -- begin
131 -- Ada.Text_IO.Put_Line (Message);
133 -- -- Send same message to server Pong.
135 -- String'Output (Channel, Message);
136 -- end;
138 -- Close_Socket (Server);
139 -- Close_Socket (Socket);
141 -- -- Part of the multicast example
143 -- -- Create a datagram socket to send connectionless, unreliable
144 -- -- messages of a fixed maximum length.
146 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
148 -- -- Allow reuse of local addresses.
150 -- Set_Socket_Option
151 -- (Socket,
152 -- Socket_Level,
153 -- (Reuse_Address, True));
155 -- -- Join a multicast group.
157 -- Set_Socket_Option
158 -- (Socket,
159 -- IP_Protocol_For_IP_Level,
160 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
162 -- -- Controls the live time of the datagram to avoid it being
163 -- -- looped forever due to routing errors. Routers decrement
164 -- -- the TTL of every datagram as it traverses from one network
165 -- -- to another and when its value reaches 0 the packet is
166 -- -- dropped. Default is 1.
168 -- Set_Socket_Option
169 -- (Socket,
170 -- IP_Protocol_For_IP_Level,
171 -- (Multicast_TTL, 1));
173 -- -- Want the data you send to be looped back to your host.
175 -- Set_Socket_Option
176 -- (Socket,
177 -- IP_Protocol_For_IP_Level,
178 -- (Multicast_Loop, True));
180 -- -- If this socket is intended to receive messages, bind it to a
181 -- -- given socket address.
183 -- Address.Addr := Any_Inet_Addr;
184 -- Address.Port := 55505;
186 -- Bind_Socket (Socket, Address);
188 -- -- If this socket is intended to send messages, provide the
189 -- -- receiver socket address.
191 -- Address.Addr := Inet_Addr (Group);
192 -- Address.Port := 55506;
194 -- Channel := Stream (Socket, Address);
196 -- -- Receive and print message from client Ping.
198 -- declare
199 -- Message : String := String'Input (Channel);
201 -- begin
203 -- -- Get the address of the sender.
205 -- Address := Get_Address (Channel);
206 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
208 -- -- Send same message to server Pong.
210 -- String'Output (Channel, Message);
211 -- end;
213 -- Close_Socket (Socket);
215 -- accept Stop;
217 -- exception when E : others =>
218 -- Ada.Text_IO.Put_Line
219 -- (Exception_Name (E) & ": " & Exception_Message (E));
220 -- end Pong;
222 -- task Ping is
223 -- entry Start;
224 -- entry Stop;
225 -- end Ping;
227 -- task body Ping is
228 -- Address : Sock_Addr_Type;
229 -- Socket : Socket_Type;
230 -- Channel : Stream_Access;
232 -- begin
233 -- accept Start;
235 -- -- See comments in Ping section for the first steps.
237 -- Address.Addr := Addresses (Get_Host_By_Name ("localhost"), 1);
238 -- Address.Port := 5432;
239 -- Create_Socket (Socket);
241 -- Set_Socket_Option
242 -- (Socket,
243 -- Socket_Level,
244 -- (Reuse_Address, True));
246 -- -- Force Pong to block
248 -- delay 0.2;
250 -- -- If the client's socket is not bound, Connect_Socket will
251 -- -- bind to an unused address. The client uses Connect_Socket to
252 -- -- create a logical connection between the client's socket and
253 -- -- a server's socket returned by Accept_Socket.
255 -- Connect_Socket (Socket, Address);
257 -- Channel := Stream (Socket);
259 -- -- Send message to server Pong.
261 -- String'Output (Channel, "Hello world");
263 -- -- Force Ping to block
265 -- delay 0.2;
267 -- -- Receive and print message from server Pong.
269 -- Ada.Text_IO.Put_Line (String'Input (Channel));
270 -- Close_Socket (Socket);
272 -- -- Part of multicast example. Code similar to Pong's one.
274 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
276 -- Set_Socket_Option
277 -- (Socket,
278 -- Socket_Level,
279 -- (Reuse_Address, True));
281 -- Set_Socket_Option
282 -- (Socket,
283 -- IP_Protocol_For_IP_Level,
284 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
286 -- Set_Socket_Option
287 -- (Socket,
288 -- IP_Protocol_For_IP_Level,
289 -- (Multicast_TTL, 1));
291 -- Set_Socket_Option
292 -- (Socket,
293 -- IP_Protocol_For_IP_Level,
294 -- (Multicast_Loop, True));
296 -- Address.Addr := Any_Inet_Addr;
297 -- Address.Port := 55506;
299 -- Bind_Socket (Socket, Address);
301 -- Address.Addr := Inet_Addr (Group);
302 -- Address.Port := 55505;
304 -- Channel := Stream (Socket, Address);
306 -- -- Send message to server Pong.
308 -- String'Output (Channel, "Hello world");
310 -- -- Receive and print message from server Pong.
312 -- declare
313 -- Message : String := String'Input (Channel);
315 -- begin
316 -- Address := Get_Address (Channel);
317 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
318 -- end;
320 -- Close_Socket (Socket);
322 -- accept Stop;
324 -- exception when E : others =>
325 -- Ada.Text_IO.Put_Line
326 -- (Exception_Name (E) & ": " & Exception_Message (E));
327 -- end Ping;
329 -- begin
330 -- -- Indicate whether the thread library provides process
331 -- -- blocking IO. Basically, if you are not using FSU threads
332 -- -- the default is ok.
334 -- Initialize (Process_Blocking_IO => False);
335 -- Ping.Start;
336 -- Pong.Start;
337 -- Ping.Stop;
338 -- Pong.Stop;
339 -- Finalize;
340 -- end PingPong;
342 procedure Initialize (Process_Blocking_IO : Boolean := False);
343 -- Initialize must be called before using any socket routines. If
344 -- the thread library provides process blocking IO - basically
345 -- with FSU threads - GNAT.Sockets should be initialized with a
346 -- value of True to simulate thread blocking IO. Further calls to
347 -- Initialize will be ignored.
349 procedure Finalize;
350 -- After Finalize is called it is not possible to use any routines
351 -- exported in by this package. This procedure is idempotent.
353 type Socket_Type is private;
354 -- Sockets are used to implement a reliable bi-directional
355 -- point-to-point, stream-based connections between
356 -- hosts. No_Socket provides a special value to denote
357 -- uninitialized sockets.
359 No_Socket : constant Socket_Type;
361 Socket_Error : exception;
362 -- There is only one exception in this package to deal with an
363 -- error during a socket routine. Once raised, its message
364 -- contains a string describing the error code.
366 function Image (Socket : Socket_Type) return String;
367 -- Return a printable string for Socket
369 function To_C (Socket : Socket_Type) return Integer;
370 -- Return a file descriptor to be used by external subprograms
371 -- especially the C functions that are not yet interfaced in this
372 -- package.
374 type Family_Type is (Family_Inet, Family_Inet6);
375 -- Address family (or protocol family) identifies the
376 -- communication domain and groups protocols with similar address
377 -- formats. IPv6 will soon be supported.
379 type Mode_Type is (Socket_Stream, Socket_Datagram);
380 -- Stream sockets provide connection-oriented byte
381 -- streams. Datagram sockets support unreliable connectionless
382 -- message based communication.
384 type Shutmode_Type is (Shut_Read, Shut_Write, Shut_Read_Write);
385 -- When a process closes a socket, the policy is to retain any
386 -- data queued until either a delivery or a timeout expiration (in
387 -- this case, the data are discarded). A finer control is
388 -- available through shutdown. With Shut_Read, no more data can be
389 -- received from the socket. With_Write, no more data can be
390 -- transmitted. Neither transmission nor reception can be
391 -- performed with Shut_Read_Write.
393 type Port_Type is new Natural;
394 -- Classical port definition. No_Port provides a special value to
395 -- denote uninitialized port. Any_Port provides a special value
396 -- enabling all ports.
398 Any_Port : constant Port_Type;
399 No_Port : constant Port_Type;
401 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
402 -- An Internet address depends on an address family (IPv4 contains
403 -- 4 octets and Ipv6 contains 16 octets). Any_Inet_Address is a
404 -- special value treated like a wildcard enabling all addresses.
405 -- No_Inet_Addr provides a special value to denote uninitialized
406 -- inet addresses.
408 Any_Inet_Addr : constant Inet_Addr_Type;
409 No_Inet_Addr : constant Inet_Addr_Type;
411 type Sock_Addr_Type (Family : Family_Type := Family_Inet) is record
412 Addr : Inet_Addr_Type (Family);
413 Port : Port_Type;
414 end record;
415 -- Socket addresses fully define a socket connection with a
416 -- protocol family, an Internet address and a port. No_Sock_Addr
417 -- provides a special value for uninitialized socket addresses.
419 No_Sock_Addr : constant Sock_Addr_Type;
421 function Image (Value : Inet_Addr_Type) return String;
422 -- Return an image of an Internet address. IPv4 notation consists
423 -- in 4 octets in decimal format separated by dots. IPv6 notation
424 -- consists in 16 octets in hexadecimal format separated by
425 -- colons (and possibly dots).
427 function Image (Value : Sock_Addr_Type) return String;
428 -- Return inet address image and port image separated by a colon.
430 function Inet_Addr (Image : String) return Inet_Addr_Type;
431 -- Convert address image from numbers-and-dots notation into an
432 -- inet address.
434 -- Host entries provide a complete information on a given host:
435 -- the official name, an array of alternative names or aliases and
436 -- array of network addresses.
438 type Host_Entry_Type
439 (Aliases_Length, Addresses_Length : Natural) is private;
441 function Official_Name (E : Host_Entry_Type) return String;
442 -- Return official name in host entry
444 function Aliases_Length (E : Host_Entry_Type) return Natural;
445 -- Return number of aliases in host entry
447 function Addresses_Length (E : Host_Entry_Type) return Natural;
448 -- Return number of addresses in host entry
450 function Aliases
451 (E : Host_Entry_Type;
452 N : Positive := 1)
453 return String;
454 -- Return N'th aliases in host entry. The first index is 1.
456 function Addresses
457 (E : Host_Entry_Type;
458 N : Positive := 1)
459 return Inet_Addr_Type;
460 -- Return N'th addresses in host entry. The first index is 1.
462 Host_Error : exception;
463 -- Exception raised by the two following procedures. Once raised,
464 -- its message contains a string describing the error code. This
465 -- exception is raised when an host entry can not be retrieved.
467 function Get_Host_By_Address
468 (Address : Inet_Addr_Type;
469 Family : Family_Type := Family_Inet)
470 return Host_Entry_Type;
471 -- Return host entry structure for the given inet address
473 function Get_Host_By_Name
474 (Name : String)
475 return Host_Entry_Type;
476 -- Return host entry structure for the given host name
478 function Host_Name return String;
479 -- Return the name of the current host
481 -- Errors are described by an enumeration type. There is only one
482 -- exception Socket_Error in this package to deal with an error
483 -- during a socket routine. Once raised, its message contains the
484 -- error code between brackets and a string describing the error
485 -- code.
487 type Error_Type is
488 (Permission_Denied,
489 Address_Already_In_Use,
490 Cannot_Assign_Requested_Address,
491 Address_Family_Not_Supported_By_Protocol,
492 Operation_Already_In_Progress,
493 Bad_File_Descriptor,
494 Connection_Refused,
495 Bad_Address,
496 Operation_Now_In_Progress,
497 Interrupted_System_Call,
498 Invalid_Argument,
499 Input_Output_Error,
500 Transport_Endpoint_Already_Connected,
501 Message_Too_Long,
502 Network_Is_Unreachable,
503 No_Buffer_Space_Available,
504 Protocol_Not_Available,
505 Transport_Endpoint_Not_Connected,
506 Operation_Not_Supported,
507 Protocol_Not_Supported,
508 Socket_Type_Not_Supported,
509 Connection_Timed_Out,
510 Resource_Temporarily_Unavailable,
511 Unknown_Host,
512 Host_Name_Lookup_Failure,
513 No_Address_Associated_With_Name,
514 Unknown_Server_Error,
515 Cannot_Resolve_Error);
517 -- Get_Socket_Options and Set_Socket_Options manipulate options
518 -- associated with a socket. Options may exist at multiple
519 -- protocol levels in the communication stack. Socket_Level is the
520 -- uppermost socket level.
522 type Level_Type is (
523 Socket_Level,
524 IP_Protocol_For_IP_Level,
525 IP_Protocol_For_UDP_Level,
526 IP_Protocol_For_TCP_Level);
528 -- There are several options available to manipulate sockets. Each
529 -- option has a name and several values available. Most of the
530 -- time, the value is a boolean to enable or disable this option.
532 type Option_Name is (
533 Keep_Alive, -- Enable sending of keep-alive messages
534 Reuse_Address, -- Allow bind to reuse local address
535 Broadcast, -- Enable datagram sockets to recv/send broadcast packets
536 Send_Buffer, -- Set/get the maximum socket send buffer in bytes
537 Receive_Buffer, -- Set/get the maximum socket recv buffer in bytes
538 Linger, -- Shutdown wait for msg to be sent or timeout occur
539 Error, -- Get and clear the pending socket error
540 No_Delay, -- Do not delay send to coalesce packets (TCP_NODELAY)
541 Add_Membership, -- Join a multicast group
542 Drop_Membership, -- Leave a multicast group
543 Multicast_TTL, -- Indicates the time-to-live of sent multicast packets
544 Multicast_Loop); -- Sent multicast packets are looped to the local socket
546 type Option_Type (Name : Option_Name := Keep_Alive) is record
547 case Name is
548 when Keep_Alive |
549 Reuse_Address |
550 Broadcast |
551 Linger |
552 No_Delay |
553 Multicast_Loop =>
554 Enabled : Boolean;
556 case Name is
557 when Linger =>
558 Seconds : Natural;
559 when others =>
560 null;
561 end case;
563 when Send_Buffer |
564 Receive_Buffer =>
565 Size : Natural;
567 when Error =>
568 Error : Error_Type;
570 when Add_Membership |
571 Drop_Membership =>
572 Multiaddr : Inet_Addr_Type;
573 Interface : Inet_Addr_Type;
575 when Multicast_TTL =>
576 Time_To_Live : Natural;
578 end case;
579 end record;
581 -- There are several controls available to manipulate
582 -- sockets. Each option has a name and several values available.
583 -- These controls differ from the socket options in that they are
584 -- not specific to sockets but are available for any device.
586 type Request_Name is (
587 Non_Blocking_IO, -- Cause a caller not to wait on blocking operations.
588 N_Bytes_To_Read); -- Return the number of bytes available to read
590 type Request_Type (Name : Request_Name := Non_Blocking_IO) is record
591 case Name is
592 when Non_Blocking_IO =>
593 Enabled : Boolean;
595 when N_Bytes_To_Read =>
596 Size : Natural;
598 end case;
599 end record;
601 procedure Create_Socket
602 (Socket : out Socket_Type;
603 Family : Family_Type := Family_Inet;
604 Mode : Mode_Type := Socket_Stream);
605 -- Create an endpoint for communication. Raise Socket_Error on error.
607 procedure Accept_Socket
608 (Server : Socket_Type;
609 Socket : out Socket_Type;
610 Address : out Sock_Addr_Type);
611 -- Extract the first connection request on the queue of pending
612 -- connections, creates a new connected socket with mostly the
613 -- same properties as Server, and allocates a new socket. The
614 -- returned Address is filled in with the address of the
615 -- connection. Raise Socket_Error on error.
617 procedure Bind_Socket
618 (Socket : Socket_Type;
619 Address : Sock_Addr_Type);
620 -- Once a socket is created, assign a local address to it. Raise
621 -- Socket_Error on error.
623 procedure Close_Socket (Socket : Socket_Type);
624 -- Close a socket and more specifically a non-connected socket.
625 -- Fail silently.
627 procedure Connect_Socket
628 (Socket : Socket_Type;
629 Server : in out Sock_Addr_Type);
630 -- Make a connection to another socket which has the address of
631 -- Server. Raise Socket_Error on error.
633 procedure Control_Socket
634 (Socket : Socket_Type;
635 Request : in out Request_Type);
636 -- Obtain or set parameter values that control the socket. This
637 -- control differs from the socket options in that they are not
638 -- specific to sockets but are avaiable for any device.
640 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
641 -- Return the peer or remote socket address of a socket. Raise
642 -- Socket_Error on error.
644 function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
645 -- Return the local or current socket address of a socket. Raise
646 -- Socket_Error on error.
648 function Get_Socket_Option
649 (Socket : Socket_Type;
650 Level : Level_Type := Socket_Level;
651 Name : Option_Name)
652 return Option_Type;
653 -- Get the options associated with a socket. Raise Socket_Error on
654 -- error.
656 procedure Listen_Socket
657 (Socket : Socket_Type;
658 Length : Positive := 15);
659 -- To accept connections, a socket is first created with
660 -- Create_Socket, a willingness to accept incoming connections and
661 -- a queue Length for incoming connections are specified. Raise
662 -- Socket_Error on error.
664 procedure Receive_Socket
665 (Socket : Socket_Type;
666 Item : out Ada.Streams.Stream_Element_Array;
667 Last : out Ada.Streams.Stream_Element_Offset);
668 -- Receive message from Socket. Last is the index value such that
669 -- Item (Last) is the last character assigned. Note that Last is
670 -- set to Item'First - 1 when the socket has been closed by
671 -- peer. This is not an error and no exception is raised. Raise
672 -- Socket_Error on error.
674 procedure Receive_Socket
675 (Socket : Socket_Type;
676 Item : out Ada.Streams.Stream_Element_Array;
677 Last : out Ada.Streams.Stream_Element_Offset;
678 From : out Sock_Addr_Type);
679 -- Receive message from Socket. If Socket is not
680 -- connection-oriented, the source address From of the message is
681 -- filled in. Last is the index value such that Item (Last) is the
682 -- last character assigned. Raise Socket_Error on error.
684 function Resolve_Exception
685 (Occurrence : Ada.Exceptions.Exception_Occurrence)
686 return Error_Type;
687 -- When Socket_Error or Host_Error are raised, the exception
688 -- message contains the error code between brackets and a string
689 -- describing the error code. Resolve_Error extracts the error
690 -- code from an exception message and translate it into an
691 -- enumeration value.
693 procedure Send_Socket
694 (Socket : Socket_Type;
695 Item : Ada.Streams.Stream_Element_Array;
696 Last : out Ada.Streams.Stream_Element_Offset);
697 -- Transmit a message to another socket. Note that Last is set to
698 -- Item'First when socket has been closed by peer. This is not an
699 -- error and no exception is raised. Raise Socket_Error on error;
701 procedure Send_Socket
702 (Socket : Socket_Type;
703 Item : Ada.Streams.Stream_Element_Array;
704 Last : out Ada.Streams.Stream_Element_Offset;
705 To : Sock_Addr_Type);
706 -- Transmit a message to another socket. The address is given by
707 -- To. Raise Socket_Error on error;
709 procedure Set_Socket_Option
710 (Socket : Socket_Type;
711 Level : Level_Type := Socket_Level;
712 Option : Option_Type);
713 -- Manipulate socket options. Raise Socket_Error on error.
715 procedure Shutdown_Socket
716 (Socket : Socket_Type;
717 How : Shutmode_Type := Shut_Read_Write);
718 -- Shutdown a connected socket. If How is Shut_Read, further
719 -- receives will be disallowed. If How is Shut_Write, further
720 -- sends will be disallowed. If how is Shut_Read_Write, further
721 -- sends and receives will be disallowed. Fail silently.
723 type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
724 -- Same interface as Ada.Streams.Stream_IO
726 function Stream
727 (Socket : Socket_Type)
728 return Stream_Access;
729 -- Associate a stream with a stream-based socket that is already
730 -- connected.
732 function Stream
733 (Socket : Socket_Type;
734 Send_To : Sock_Addr_Type)
735 return Stream_Access;
736 -- Associate a stream with a datagram-based socket that is already
737 -- bound. Send_To is the socket address to which messages are
738 -- being sent.
740 function Get_Address
741 (Stream : Stream_Access)
742 return Sock_Addr_Type;
743 -- Return the socket address from which the last message was
744 -- received.
746 type Socket_Set_Type is private;
747 -- This type allows to manipulate sets of sockets. It allows to
748 -- wait for events on multiple endpoints at one time. This is an
749 -- access type on a system dependent structure. To avoid memory
750 -- leaks it is highly recommended to clean the access value with
751 -- procedure Empty.
753 procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
754 -- Remove Socket from Item
756 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type);
757 -- Insert Socket into Item
759 procedure Empty (Item : in out Socket_Set_Type);
760 -- Remove all Sockets from Item and deallocate internal data
762 function Is_Empty
763 (Item : Socket_Set_Type)
764 return Boolean;
765 -- Return True if Item is empty
767 function Is_Set
768 (Item : Socket_Set_Type;
769 Socket : Socket_Type)
770 return Boolean;
771 -- Return True if Socket is present in Item
773 -- C select() waits for a number of file descriptors to change
774 -- status. Usually, three independent sets of descriptors are
775 -- watched (read, write and exception). A timeout gives an upper
776 -- bound on the amount of time elapsed before select returns.
777 -- This function blocks until an event occurs. On some platforms,
778 -- C select can block the full process.
780 -- Check_Selector provides the very same behaviour. The only
781 -- difference is that it does not watch for exception events. Note
782 -- that on some platforms it is kept process blocking in purpose.
783 -- The timeout parameter allows the user to have the behaviour he
784 -- wants. Abort_Selector allows to abort safely a Check_Selector
785 -- that is blocked forever. A special file descriptor is opened by
786 -- Create_Selector and included in each call to
787 -- Check_Selector. Abort_Selector causes an event to occur on this
788 -- descriptor in order to unblock Check_Selector. The user must
789 -- call Close_Selector to discard this special file. A reason to
790 -- abort a select operation is typically to add a socket in one of
791 -- the socket sets when the timeout is set to forever.
793 Forever : constant Duration;
795 type Selector_Type is limited private;
796 type Selector_Access is access all Selector_Type;
798 procedure Create_Selector (Selector : out Selector_Type);
799 -- Create a new selector
801 procedure Close_Selector (Selector : in out Selector_Type);
802 -- Close Selector and all internal descriptors associated
804 type Selector_Status is (Completed, Expired, Aborted);
806 procedure Check_Selector
807 (Selector : in out Selector_Type;
808 R_Socket_Set : in out Socket_Set_Type;
809 W_Socket_Set : in out Socket_Set_Type;
810 Status : out Selector_Status;
811 Timeout : Duration := Forever);
812 -- Return when one Socket in R_Socket_Set has some data to be read
813 -- or if one Socket in W_Socket_Set is ready to receive some
814 -- data. In these cases Status is set to Completed and sockets
815 -- that are ready are set in R_Socket_Set or W_Socket_Set. Status
816 -- is set to Expired if no socket was ready after a Timeout
817 -- expiration. Status is set to Aborted if an abort signal as been
818 -- received while checking socket status. As this procedure
819 -- returns when Timeout occurs, it is a design choice to keep this
820 -- procedure process blocking. Note that a Timeout of 0.0 returns
821 -- immediatly.
823 procedure Abort_Selector (Selector : Selector_Type);
824 -- Send an abort signal to the selector.
826 private
828 type Socket_Type is new Integer;
829 No_Socket : constant Socket_Type := -1;
831 Forever : constant Duration := Duration'Last;
833 type Selector_Type is limited record
834 R_Sig_Socket : Socket_Type;
835 W_Sig_Socket : Socket_Type;
836 In_Progress : Boolean := False;
837 end record;
838 -- The two signalling sockets are used to abort a select
839 -- operation.
841 type Socket_Set_Record;
842 type Socket_Set_Type is access all Socket_Set_Record;
844 subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
845 -- Octet for Internet address
847 type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;
849 subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 .. 4);
850 subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);
852 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
853 case Family is
854 when Family_Inet =>
855 Sin_V4 : Inet_Addr_V4_Type := (others => 0);
857 when Family_Inet6 =>
858 Sin_V6 : Inet_Addr_V6_Type := (others => 0);
859 end case;
860 end record;
862 Any_Port : constant Port_Type := 0;
863 No_Port : constant Port_Type := 0;
865 Any_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
866 No_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
868 No_Sock_Addr : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
870 Max_Host_Name_Length : constant := 64;
871 -- The constant MAXHOSTNAMELEN is usually set to 64
873 subtype Host_Name_Index is Natural range 1 .. Max_Host_Name_Length;
875 type Host_Name_Type
876 (Length : Host_Name_Index := Max_Host_Name_Length)
877 is record
878 Name : String (1 .. Length);
879 end record;
880 -- We need fixed strings to avoid access types in host entry type
882 type Host_Name_Array is array (Natural range <>) of Host_Name_Type;
883 type Inet_Addr_Array is array (Natural range <>) of Inet_Addr_Type;
885 type Host_Entry_Type (Aliases_Length, Addresses_Length : Natural) is record
886 Official : Host_Name_Type;
887 Aliases : Host_Name_Array (1 .. Aliases_Length);
888 Addresses : Inet_Addr_Array (1 .. Addresses_Length);
889 end record;
891 end GNAT.Sockets;