g-socket.ads (Get_Host_By_Address, [...]): Clarify documentation of the behaviour...
[gcc.git] / gcc / ada / g-socket.ads
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 ------------------------------------------------------------------------------
33
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:
37
38 -- All native ports, except Interix, with restrictions as follows
39
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.
44
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.
48
49 -- This package is not supported on the Interix port of GNAT.
50
51 -- VxWorks cross ports fully implement this package.
52
53 -- This package is not yet implemented on LynxOS.
54
55 with Ada.Exceptions;
56 with Ada.Streams;
57 with Ada.Unchecked_Deallocation;
58
59 with System;
60
61 package GNAT.Sockets is
62
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.
66
67 -- GNAT.Sockets has been designed with several ideas in mind.
68
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.
73
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.
78
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).
85
86 -- Here is a typical example of what you can do:
87
88 -- with GNAT.Sockets; use GNAT.Sockets;
89
90 -- with Ada.Text_IO;
91 -- with Ada.Exceptions; use Ada.Exceptions;
92
93 -- procedure PingPong is
94
95 -- Group : constant String := "239.255.128.128";
96 -- -- Multicast group: administratively scoped IP address
97
98 -- task Pong is
99 -- entry Start;
100 -- entry Stop;
101 -- end Pong;
102
103 -- task body Pong is
104 -- Address : Sock_Addr_Type;
105 -- Server : Socket_Type;
106 -- Socket : Socket_Type;
107 -- Channel : Stream_Access;
108
109 -- begin
110 -- accept Start;
111 --
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.
115
116 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
117
118 -- -- Get a socket address that is an Internet address and a port
119
120 -- Address.Port := 5876;
121
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.
128
129 -- Create_Socket (Server);
130
131 -- -- Allow reuse of local addresses
132
133 -- Set_Socket_Option
134 -- (Server,
135 -- Socket_Level,
136 -- (Reuse_Address, True));
137
138 -- Bind_Socket (Server, Address);
139
140 -- -- A server marks a socket as willing to receive connect events
141
142 -- Listen_Socket (Server);
143
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.
148
149 -- Accept_Socket (Server, Socket, Address);
150
151 -- -- Return a stream associated to the connected socket
152
153 -- Channel := Stream (Socket);
154
155 -- -- Force Pong to block
156
157 -- delay 0.2;
158
159 -- -- Receive and print message from client Ping
160
161 -- declare
162 -- Message : String := String'Input (Channel);
163
164 -- begin
165 -- Ada.Text_IO.Put_Line (Message);
166
167 -- -- Send same message back to client Ping
168
169 -- String'Output (Channel, Message);
170 -- end;
171
172 -- Close_Socket (Server);
173 -- Close_Socket (Socket);
174
175 -- -- Part of the multicast example
176
177 -- -- Create a datagram socket to send connectionless, unreliable
178 -- -- messages of a fixed maximum length.
179
180 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
181
182 -- -- Allow reuse of local addresses
183
184 -- Set_Socket_Option
185 -- (Socket,
186 -- Socket_Level,
187 -- (Reuse_Address, True));
188
189 -- -- Join a multicast group
190
191 -- Set_Socket_Option
192 -- (Socket,
193 -- IP_Protocol_For_IP_Level,
194 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
195
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.
201
202 -- Set_Socket_Option
203 -- (Socket,
204 -- IP_Protocol_For_IP_Level,
205 -- (Multicast_TTL, 1));
206
207 -- -- Want the data you send to be looped back to your host
208
209 -- Set_Socket_Option
210 -- (Socket,
211 -- IP_Protocol_For_IP_Level,
212 -- (Multicast_Loop, True));
213
214 -- -- If this socket is intended to receive messages, bind it
215 -- -- to a given socket address.
216
217 -- Address.Addr := Any_Inet_Addr;
218 -- Address.Port := 55505;
219
220 -- Bind_Socket (Socket, Address);
221
222 -- -- If this socket is intended to send messages, provide the
223 -- -- receiver socket address.
224
225 -- Address.Addr := Inet_Addr (Group);
226 -- Address.Port := 55506;
227
228 -- Channel := Stream (Socket, Address);
229
230 -- -- Receive and print message from client Ping
231
232 -- declare
233 -- Message : String := String'Input (Channel);
234
235 -- begin
236 -- -- Get the address of the sender
237
238 -- Address := Get_Address (Channel);
239 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
240
241 -- -- Send same message back to client Ping
242
243 -- String'Output (Channel, Message);
244 -- end;
245
246 -- Close_Socket (Socket);
247
248 -- accept Stop;
249
250 -- exception when E : others =>
251 -- Ada.Text_IO.Put_Line
252 -- (Exception_Name (E) & ": " & Exception_Message (E));
253 -- end Pong;
254
255 -- task Ping is
256 -- entry Start;
257 -- entry Stop;
258 -- end Ping;
259
260 -- task body Ping is
261 -- Address : Sock_Addr_Type;
262 -- Socket : Socket_Type;
263 -- Channel : Stream_Access;
264
265 -- begin
266 -- accept Start;
267
268 -- -- See comments in Ping section for the first steps
269
270 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
271 -- Address.Port := 5876;
272 -- Create_Socket (Socket);
273
274 -- Set_Socket_Option
275 -- (Socket,
276 -- Socket_Level,
277 -- (Reuse_Address, True));
278
279 -- -- Force Pong to block
280
281 -- delay 0.2;
282
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.
287
288 -- Connect_Socket (Socket, Address);
289
290 -- Channel := Stream (Socket);
291
292 -- -- Send message to server Pong.
293
294 -- String'Output (Channel, "Hello world");
295
296 -- -- Force Ping to block
297
298 -- delay 0.2;
299
300 -- -- Receive and print message from server Pong
301
302 -- Ada.Text_IO.Put_Line (String'Input (Channel));
303 -- Close_Socket (Socket);
304
305 -- -- Part of multicast example. Code similar to Pong's one
306
307 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
308
309 -- Set_Socket_Option
310 -- (Socket,
311 -- Socket_Level,
312 -- (Reuse_Address, True));
313
314 -- Set_Socket_Option
315 -- (Socket,
316 -- IP_Protocol_For_IP_Level,
317 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
318
319 -- Set_Socket_Option
320 -- (Socket,
321 -- IP_Protocol_For_IP_Level,
322 -- (Multicast_TTL, 1));
323
324 -- Set_Socket_Option
325 -- (Socket,
326 -- IP_Protocol_For_IP_Level,
327 -- (Multicast_Loop, True));
328
329 -- Address.Addr := Any_Inet_Addr;
330 -- Address.Port := 55506;
331
332 -- Bind_Socket (Socket, Address);
333
334 -- Address.Addr := Inet_Addr (Group);
335 -- Address.Port := 55505;
336
337 -- Channel := Stream (Socket, Address);
338
339 -- -- Send message to server Pong
340
341 -- String'Output (Channel, "Hello world");
342
343 -- -- Receive and print message from server Pong
344
345 -- declare
346 -- Message : String := String'Input (Channel);
347
348 -- begin
349 -- Address := Get_Address (Channel);
350 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
351 -- end;
352
353 -- Close_Socket (Socket);
354
355 -- accept Stop;
356
357 -- exception when E : others =>
358 -- Ada.Text_IO.Put_Line
359 -- (Exception_Name (E) & ": " & Exception_Message (E));
360 -- end Ping;
361
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.
366
367 -- Initialize (Process_Blocking_IO => False);
368 -- Ping.Start;
369 -- Pong.Start;
370 -- Ping.Stop;
371 -- Pong.Stop;
372 -- Finalize;
373 -- end PingPong;
374
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.
388
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.
392
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.
398
399 No_Socket : constant Socket_Type;
400
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.
405
406 function Image (Socket : Socket_Type) return String;
407 -- Return a printable string for Socket
408
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.
412
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.
417
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.
421
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.
429
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.
434
435 Any_Port : constant Port_Type;
436 No_Port : constant Port_Type;
437
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.
444
445 Any_Inet_Addr : constant Inet_Addr_Type;
446 No_Inet_Addr : constant Inet_Addr_Type;
447
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.
455
456 No_Sock_Addr : constant Sock_Addr_Type;
457
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).
463
464 function Image (Value : Sock_Addr_Type) return String;
465 -- Return inet address image and port image separated by a colon.
466
467 function Inet_Addr (Image : String) return Inet_Addr_Type;
468 -- Convert address image from numbers-and-dots notation into an
469 -- inet address.
470
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.
474
475 type Host_Entry_Type
476 (Aliases_Length, Addresses_Length : Natural) is private;
477
478 function Official_Name (E : Host_Entry_Type) return String;
479 -- Return official name in host entry
480
481 function Aliases_Length (E : Host_Entry_Type) return Natural;
482 -- Return number of aliases in host entry
483
484 function Addresses_Length (E : Host_Entry_Type) return Natural;
485 -- Return number of addresses in host entry
486
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.
491
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.
496
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.
501
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.
506 -- Note that no result will be returned if there is no mapping of this
507 -- IP address to a host name in the system tables (host database,
508 -- DNS or otherwise).
509
510 function Get_Host_By_Name
511 (Name : String) return Host_Entry_Type;
512 -- Return host entry structure for the given host name. Here name
513 -- is either a host name, or an IP address. If Name is an IP address,
514 -- this is equivalent to Get_Host_By_Address (Inet_Addr (Name)).
515
516 function Host_Name return String;
517 -- Return the name of the current host
518
519 type Service_Entry_Type (Aliases_Length : Natural) is private;
520 -- Service entries provide complete information on a given
521 -- service: the official name, an array of alternative names or
522 -- aliases and the port number.
523
524 function Official_Name (S : Service_Entry_Type) return String;
525 -- Return official name in service entry
526
527 function Port_Number (S : Service_Entry_Type) return Port_Type;
528 -- Return port number in service entry
529
530 function Protocol_Name (S : Service_Entry_Type) return String;
531 -- Return Protocol in service entry (usually UDP or TCP)
532
533 function Aliases_Length (S : Service_Entry_Type) return Natural;
534 -- Return number of aliases in service entry
535
536 function Aliases
537 (S : Service_Entry_Type;
538 N : Positive := 1) return String;
539 -- Return N'th aliases in service entry. The first index is 1.
540
541 function Get_Service_By_Name
542 (Name : String;
543 Protocol : String) return Service_Entry_Type;
544 -- Return service entry structure for the given service name
545
546 function Get_Service_By_Port
547 (Port : Port_Type;
548 Protocol : String) return Service_Entry_Type;
549 -- Return service entry structure for the given service port number
550
551 Service_Error : exception;
552 -- Comment required ???
553
554 -- Errors are described by an enumeration type. There is only one
555 -- exception Socket_Error in this package to deal with an error
556 -- during a socket routine. Once raised, its message contains the
557 -- error code between brackets and a string describing the error code.
558
559 -- The name of the enumeration constant documents the error condition
560
561 type Error_Type is
562 (Success,
563 Permission_Denied,
564 Address_Already_In_Use,
565 Cannot_Assign_Requested_Address,
566 Address_Family_Not_Supported_By_Protocol,
567 Operation_Already_In_Progress,
568 Bad_File_Descriptor,
569 Software_Caused_Connection_Abort,
570 Connection_Refused,
571 Connection_Reset_By_Peer,
572 Destination_Address_Required,
573 Bad_Address,
574 Host_Is_Down,
575 No_Route_To_Host,
576 Operation_Now_In_Progress,
577 Interrupted_System_Call,
578 Invalid_Argument,
579 Input_Output_Error,
580 Transport_Endpoint_Already_Connected,
581 Too_Many_Symbolic_Links,
582 Too_Many_Open_Files,
583 Message_Too_Long,
584 File_Name_Too_Long,
585 Network_Is_Down,
586 Network_Dropped_Connection_Because_Of_Reset,
587 Network_Is_Unreachable,
588 No_Buffer_Space_Available,
589 Protocol_Not_Available,
590 Transport_Endpoint_Not_Connected,
591 Socket_Operation_On_Non_Socket,
592 Operation_Not_Supported,
593 Protocol_Family_Not_Supported,
594 Protocol_Not_Supported,
595 Protocol_Wrong_Type_For_Socket,
596 Cannot_Send_After_Transport_Endpoint_Shutdown,
597 Socket_Type_Not_Supported,
598 Connection_Timed_Out,
599 Too_Many_References,
600 Resource_Temporarily_Unavailable,
601 Unknown_Host,
602 Host_Name_Lookup_Failure,
603 Non_Recoverable_Error,
604 Unknown_Server_Error,
605 Cannot_Resolve_Error);
606
607 -- Get_Socket_Options and Set_Socket_Options manipulate options
608 -- associated with a socket. Options may exist at multiple
609 -- protocol levels in the communication stack. Socket_Level is the
610 -- uppermost socket level.
611
612 type Level_Type is (
613 Socket_Level,
614 IP_Protocol_For_IP_Level,
615 IP_Protocol_For_UDP_Level,
616 IP_Protocol_For_TCP_Level);
617
618 -- There are several options available to manipulate sockets. Each
619 -- option has a name and several values available. Most of the
620 -- time, the value is a boolean to enable or disable this option.
621
622 type Option_Name is (
623 Keep_Alive, -- Enable sending of keep-alive messages
624 Reuse_Address, -- Allow bind to reuse local address
625 Broadcast, -- Enable datagram sockets to recv/send broadcast packets
626 Send_Buffer, -- Set/get the maximum socket send buffer in bytes
627 Receive_Buffer, -- Set/get the maximum socket recv buffer in bytes
628 Linger, -- Shutdown wait for msg to be sent or timeout occur
629 Error, -- Get and clear the pending socket error
630 No_Delay, -- Do not delay send to coalesce packets (TCP_NODELAY)
631 Add_Membership, -- Join a multicast group
632 Drop_Membership, -- Leave a multicast group
633 Multicast_TTL, -- Indicate the time-to-live of sent multicast packets
634 Multicast_Loop); -- Sent multicast packets are looped to the local socket
635
636 type Option_Type (Name : Option_Name := Keep_Alive) is record
637 case Name is
638 when Keep_Alive |
639 Reuse_Address |
640 Broadcast |
641 Linger |
642 No_Delay |
643 Multicast_Loop =>
644 Enabled : Boolean;
645
646 case Name is
647 when Linger =>
648 Seconds : Natural;
649 when others =>
650 null;
651 end case;
652
653 when Send_Buffer |
654 Receive_Buffer =>
655 Size : Natural;
656
657 when Error =>
658 Error : Error_Type;
659
660 when Add_Membership |
661 Drop_Membership =>
662 Multicast_Address : Inet_Addr_Type;
663 Local_Interface : Inet_Addr_Type;
664
665 when Multicast_TTL =>
666 Time_To_Live : Natural;
667
668 end case;
669 end record;
670
671 -- There are several controls available to manipulate
672 -- sockets. Each option has a name and several values available.
673 -- These controls differ from the socket options in that they are
674 -- not specific to sockets but are available for any device.
675
676 type Request_Name is (
677 Non_Blocking_IO, -- Cause a caller not to wait on blocking operations.
678 N_Bytes_To_Read); -- Return the number of bytes available to read
679
680 type Request_Type (Name : Request_Name := Non_Blocking_IO) is record
681 case Name is
682 when Non_Blocking_IO =>
683 Enabled : Boolean;
684
685 when N_Bytes_To_Read =>
686 Size : Natural;
687
688 end case;
689 end record;
690
691 -- A request flag allows to specify the type of message
692 -- transmissions or receptions. A request flag can be a
693 -- combination of zero or more predefined request flags.
694
695 type Request_Flag_Type is private;
696
697 No_Request_Flag : constant Request_Flag_Type;
698 -- This flag corresponds to the normal execution of an operation.
699
700 Process_Out_Of_Band_Data : constant Request_Flag_Type;
701 -- This flag requests that the receive or send function operates
702 -- on out-of-band data when the socket supports this notion (e.g.
703 -- Socket_Stream).
704
705 Peek_At_Incoming_Data : constant Request_Flag_Type;
706 -- This flag causes the receive operation to return data from the
707 -- beginning of the receive queue without removing that data from
708 -- the queue. A subsequent receive call will return the same data.
709
710 Wait_For_A_Full_Reception : constant Request_Flag_Type;
711 -- This flag requests that the operation block until the full
712 -- request is satisfied. However, the call may still return less
713 -- data than requested if a signal is caught, an error or
714 -- disconnect occurs, or the next data to be received is of a dif-
715 -- ferent type than that returned.
716
717 Send_End_Of_Record : constant Request_Flag_Type;
718 -- This flag indicates that the entire message has been sent and
719 -- so this terminates the record.
720
721 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type;
722 -- Combine flag L with flag R
723
724 type Stream_Element_Reference is access all Ada.Streams.Stream_Element;
725
726 type Vector_Element is record
727 Base : Stream_Element_Reference;
728 Length : Ada.Streams.Stream_Element_Count;
729 end record;
730
731 type Vector_Type is array (Integer range <>) of Vector_Element;
732
733 procedure Create_Socket
734 (Socket : out Socket_Type;
735 Family : Family_Type := Family_Inet;
736 Mode : Mode_Type := Socket_Stream);
737 -- Create an endpoint for communication. Raises Socket_Error on error.
738
739 procedure Accept_Socket
740 (Server : Socket_Type;
741 Socket : out Socket_Type;
742 Address : out Sock_Addr_Type);
743 -- Extract the first connection request on the queue of pending
744 -- connections, creates a new connected socket with mostly the
745 -- same properties as Server, and allocates a new socket. The
746 -- returned Address is filled in with the address of the
747 -- connection. Raises Socket_Error on error.
748
749 procedure Bind_Socket
750 (Socket : Socket_Type;
751 Address : Sock_Addr_Type);
752 -- Once a socket is created, assign a local address to it. Raise
753 -- Socket_Error on error.
754
755 procedure Close_Socket (Socket : Socket_Type);
756 -- Close a socket and more specifically a non-connected socket.
757
758 procedure Connect_Socket
759 (Socket : Socket_Type;
760 Server : in out Sock_Addr_Type);
761 -- Make a connection to another socket which has the address of
762 -- Server. Raises Socket_Error on error.
763
764 procedure Control_Socket
765 (Socket : Socket_Type;
766 Request : in out Request_Type);
767 -- Obtain or set parameter values that control the socket. This
768 -- control differs from the socket options in that they are not
769 -- specific to sockets but are available for any device.
770
771 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
772 -- Return the peer or remote socket address of a socket. Raise
773 -- Socket_Error on error.
774
775 function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
776 -- Return the local or current socket address of a socket. Return
777 -- No_Sock_Addr on error (for instance, socket closed or not
778 -- locally bound).
779
780 function Get_Socket_Option
781 (Socket : Socket_Type;
782 Level : Level_Type := Socket_Level;
783 Name : Option_Name) return Option_Type;
784 -- Get the options associated with a socket. Raises Socket_Error
785 -- on error.
786
787 procedure Listen_Socket
788 (Socket : Socket_Type;
789 Length : Positive := 15);
790 -- To accept connections, a socket is first created with
791 -- Create_Socket, a willingness to accept incoming connections and
792 -- a queue Length for incoming connections are specified. Raise
793 -- Socket_Error on error.
794
795 procedure Receive_Socket
796 (Socket : Socket_Type;
797 Item : out Ada.Streams.Stream_Element_Array;
798 Last : out Ada.Streams.Stream_Element_Offset;
799 Flags : Request_Flag_Type := No_Request_Flag);
800 -- Receive message from Socket. Last is the index value such that
801 -- Item (Last) is the last character assigned. Note that Last is
802 -- set to Item'First - 1 when the socket has been closed by
803 -- peer. This is not an error and no exception is raised. Flags
804 -- allows to control the reception. Raise Socket_Error on error.
805
806 procedure Receive_Socket
807 (Socket : Socket_Type;
808 Item : out Ada.Streams.Stream_Element_Array;
809 Last : out Ada.Streams.Stream_Element_Offset;
810 From : out Sock_Addr_Type;
811 Flags : Request_Flag_Type := No_Request_Flag);
812 -- Receive message from Socket. If Socket is not
813 -- connection-oriented, the source address From of the message is
814 -- filled in. Last is the index value such that Item (Last) is the
815 -- last character assigned. Flags allows to control the
816 -- reception. Raises Socket_Error on error.
817
818 procedure Receive_Vector
819 (Socket : Socket_Type;
820 Vector : Vector_Type;
821 Count : out Ada.Streams.Stream_Element_Count);
822 -- Receive data from a socket and scatter it into the set of vector
823 -- elements Vector. Count is set to the count of received stream elements.
824
825 function Resolve_Exception
826 (Occurrence : Ada.Exceptions.Exception_Occurrence) return Error_Type;
827 -- When Socket_Error or Host_Error are raised, the exception
828 -- message contains the error code between brackets and a string
829 -- describing the error code. Resolve_Error extracts the error
830 -- code from an exception message and translate it into an
831 -- enumeration value.
832
833 procedure Send_Socket
834 (Socket : Socket_Type;
835 Item : Ada.Streams.Stream_Element_Array;
836 Last : out Ada.Streams.Stream_Element_Offset;
837 Flags : Request_Flag_Type := No_Request_Flag);
838 -- Transmit a message to another socket. Note that Last is set to
839 -- Item'First-1 when socket has been closed by peer. This is not
840 -- considered an error and no exception is raised. Flags allows to
841 -- control the transmission. Raises Socket_Error on any other
842 -- error condition.
843
844 procedure Send_Socket
845 (Socket : Socket_Type;
846 Item : Ada.Streams.Stream_Element_Array;
847 Last : out Ada.Streams.Stream_Element_Offset;
848 To : Sock_Addr_Type;
849 Flags : Request_Flag_Type := No_Request_Flag);
850 -- Transmit a message to another socket. The address is given by
851 -- To. Flags allows to control the transmission. Raises
852 -- Socket_Error on error.
853
854 procedure Send_Vector
855 (Socket : Socket_Type;
856 Vector : Vector_Type;
857 Count : out Ada.Streams.Stream_Element_Count);
858 -- Transmit data gathered from the set of vector elements Vector to a
859 -- socket. Count is set to the count of transmitted stream elements.
860
861 procedure Set_Socket_Option
862 (Socket : Socket_Type;
863 Level : Level_Type := Socket_Level;
864 Option : Option_Type);
865 -- Manipulate socket options. Raises Socket_Error on error.
866
867 procedure Shutdown_Socket
868 (Socket : Socket_Type;
869 How : Shutmode_Type := Shut_Read_Write);
870 -- Shutdown a connected socket. If How is Shut_Read, further
871 -- receives will be disallowed. If How is Shut_Write, further
872 -- sends will be disallowed. If how is Shut_Read_Write, further
873 -- sends and receives will be disallowed.
874
875 type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
876 -- Same interface as Ada.Streams.Stream_IO
877
878 function Stream
879 (Socket : Socket_Type) return Stream_Access;
880 -- Create a stream associated with a stream-based socket that is
881 -- already connected.
882
883 function Stream
884 (Socket : Socket_Type;
885 Send_To : Sock_Addr_Type) return Stream_Access;
886 -- Create a stream associated with a datagram-based socket that is
887 -- already bound. Send_To is the socket address to which messages are
888 -- being sent.
889
890 function Get_Address
891 (Stream : Stream_Access) return Sock_Addr_Type;
892 -- Return the socket address from which the last message was received.
893
894 procedure Free is new Ada.Unchecked_Deallocation
895 (Ada.Streams.Root_Stream_Type'Class, Stream_Access);
896 -- Destroy a stream created by one of the Stream functions above,
897 -- releasing the corresponding resources. The user is responsible
898 -- for calling this subprogram when the stream is not needed anymore.
899
900 type Socket_Set_Type is limited private;
901 -- This type allows to manipulate sets of sockets. It allows to
902 -- wait for events on multiple endpoints at one time. This is an
903 -- access type on a system dependent structure. To avoid memory
904 -- leaks it is highly recommended to clean the access value with
905 -- procedure Empty.
906
907 procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
908 -- Remove Socket from Item
909
910 procedure Copy (Source : Socket_Set_Type; Target : in out Socket_Set_Type);
911 -- Copy Source into Target as Socket_Set_Type is limited private
912
913 procedure Empty (Item : in out Socket_Set_Type);
914 -- Remove all Sockets from Item and deallocate internal data
915
916 procedure Get (Item : in out Socket_Set_Type; Socket : out Socket_Type);
917 -- Extract a Socket from socket set Item. Socket is set to
918 -- No_Socket when the set is empty.
919
920 function Is_Empty
921 (Item : Socket_Set_Type) return Boolean;
922 -- Return True iff Item is empty
923
924 function Is_Set
925 (Item : Socket_Set_Type;
926 Socket : Socket_Type) return Boolean;
927 -- Return True iff Socket is present in Item
928
929 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type);
930 -- Insert Socket into Item
931
932 -- C select() waits for a number of file descriptors to change
933 -- status. Usually, three independent sets of descriptors are
934 -- watched (read, write and exception). A timeout gives an upper
935 -- bound on the amount of time elapsed before select returns.
936 -- This function blocks until an event occurs. On some platforms,
937 -- C select can block the full process.
938 --
939 -- Check_Selector provides the very same behaviour. The only
940 -- difference is that it does not watch for exception events. Note
941 -- that on some platforms it is kept process blocking in purpose.
942 -- The timeout parameter allows the user to have the behaviour he
943 -- wants. Abort_Selector allows to abort safely a Check_Selector
944 -- that is blocked forever. A special file descriptor is opened by
945 -- Create_Selector and included in each call to
946 -- Check_Selector. Abort_Selector causes an event to occur on this
947 -- descriptor in order to unblock Check_Selector. The user must
948 -- call Close_Selector to discard this special file. A reason to
949 -- abort a select operation is typically to add a socket in one of
950 -- the socket sets when the timeout is set to forever.
951
952 type Selector_Type is limited private;
953 type Selector_Access is access all Selector_Type;
954
955 -- Selector_Duration is a subtype of Standard.Duration because the
956 -- full range of Standard.Duration cannot be represented in the
957 -- equivalent C structure. Moreover, negative values are not
958 -- allowed to avoid system incompatibilities.
959
960 Immediate : constant := 0.0;
961 Forever : constant := Duration (Integer'Last) * 1.0;
962
963 subtype Selector_Duration is Duration range Immediate .. Forever;
964
965 procedure Create_Selector (Selector : out Selector_Type);
966 -- Create a new selector
967
968 procedure Close_Selector (Selector : in out Selector_Type);
969 -- Close Selector and all internal descriptors associated
970
971 type Selector_Status is (Completed, Expired, Aborted);
972
973 procedure Check_Selector
974 (Selector : in out Selector_Type;
975 R_Socket_Set : in out Socket_Set_Type;
976 W_Socket_Set : in out Socket_Set_Type;
977 Status : out Selector_Status;
978 Timeout : Selector_Duration := Forever);
979 -- Return when one Socket in R_Socket_Set has some data to be read
980 -- or if one Socket in W_Socket_Set is ready to receive some
981 -- data. In these cases Status is set to Completed and sockets
982 -- that are ready are set in R_Socket_Set or W_Socket_Set. Status
983 -- is set to Expired if no socket was ready after a Timeout
984 -- expiration. Status is set to Aborted if an abort signal has been
985 -- received while checking socket status. As this procedure
986 -- returns when Timeout occurs, it is a design choice to keep this
987 -- procedure process blocking. Note that a Timeout of 0.0 returns
988 -- immediately. Also note that two different objects must be passed
989 -- as R_Socket_Set and W_Socket_Set (even if they contain the same
990 -- set of Sockets), or some event will be lost.
991
992 procedure Check_Selector
993 (Selector : in out Selector_Type;
994 R_Socket_Set : in out Socket_Set_Type;
995 W_Socket_Set : in out Socket_Set_Type;
996 E_Socket_Set : in out Socket_Set_Type;
997 Status : out Selector_Status;
998 Timeout : Selector_Duration := Forever);
999 -- This refined version of Check_Selector allows to watch for
1000 -- exception events (that is notifications of out-of-band
1001 -- transmission and reception). As above, all of R_Socket_Set,
1002 -- W_Socket_Set and E_Socket_Set must be different objects.
1003
1004 procedure Abort_Selector (Selector : Selector_Type);
1005 -- Send an abort signal to the selector.
1006
1007 private
1008
1009 type Socket_Type is new Integer;
1010 No_Socket : constant Socket_Type := -1;
1011
1012 type Selector_Type is limited record
1013 R_Sig_Socket : Socket_Type;
1014 W_Sig_Socket : Socket_Type;
1015 end record;
1016
1017 pragma Volatile (Selector_Type);
1018
1019 -- The two signalling sockets are used to abort a select
1020 -- operation.
1021
1022 subtype Socket_Set_Access is System.Address;
1023 No_Socket_Set : constant Socket_Set_Access := System.Null_Address;
1024
1025 type Socket_Set_Type is record
1026 Last : Socket_Type := No_Socket;
1027 Set : Socket_Set_Access := No_Socket_Set;
1028 end record;
1029
1030 subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
1031 -- Octet for Internet address
1032
1033 type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;
1034
1035 subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 .. 4);
1036 subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);
1037
1038 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
1039 case Family is
1040 when Family_Inet =>
1041 Sin_V4 : Inet_Addr_V4_Type := (others => 0);
1042
1043 when Family_Inet6 =>
1044 Sin_V6 : Inet_Addr_V6_Type := (others => 0);
1045 end case;
1046 end record;
1047
1048 Any_Port : constant Port_Type := 0;
1049 No_Port : constant Port_Type := 0;
1050
1051 Any_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
1052 No_Inet_Addr : constant Inet_Addr_Type := (Family_Inet, (others => 0));
1053
1054 No_Sock_Addr : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
1055
1056 Max_Name_Length : constant := 64;
1057 -- The constant MAXHOSTNAMELEN is usually set to 64
1058
1059 subtype Name_Index is Natural range 1 .. Max_Name_Length;
1060
1061 type Name_Type
1062 (Length : Name_Index := Max_Name_Length)
1063 is record
1064 Name : String (1 .. Length);
1065 end record;
1066 -- We need fixed strings to avoid access types in host entry type
1067
1068 type Name_Array is array (Natural range <>) of Name_Type;
1069 type Inet_Addr_Array is array (Natural range <>) of Inet_Addr_Type;
1070
1071 type Host_Entry_Type (Aliases_Length, Addresses_Length : Natural) is record
1072 Official : Name_Type;
1073 Aliases : Name_Array (1 .. Aliases_Length);
1074 Addresses : Inet_Addr_Array (1 .. Addresses_Length);
1075 end record;
1076
1077 type Service_Entry_Type (Aliases_Length : Natural) is record
1078 Official : Name_Type;
1079 Aliases : Name_Array (1 .. Aliases_Length);
1080 Port : Port_Type;
1081 Protocol : Name_Type;
1082 end record;
1083
1084 type Request_Flag_Type is mod 2 ** 8;
1085 No_Request_Flag : constant Request_Flag_Type := 0;
1086 Process_Out_Of_Band_Data : constant Request_Flag_Type := 1;
1087 Peek_At_Incoming_Data : constant Request_Flag_Type := 2;
1088 Wait_For_A_Full_Reception : constant Request_Flag_Type := 4;
1089 Send_End_Of_Record : constant Request_Flag_Type := 8;
1090
1091 end GNAT.Sockets;