g-soccon.ads: New file.
[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-2008, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 facility
35 -- provided on many operating systems. This is implemented on the following
36 -- platforms:
37
38 -- All native ports, with restrictions as follows
39
40 -- Multicast is available only on systems which provide support for this
41 -- feature, so it is not available if Multicast is not supported, or not
42 -- installed.
43
44 -- The VMS implementation was implemented using the DECC RTL Socket API,
45 -- and is thus subject to limitations in the implementation of this API.
46
47 -- VxWorks cross ports fully implement this package
48
49 -- This package is not yet implemented on LynxOS or other cross ports
50
51 with Ada.Exceptions;
52 with Ada.Streams;
53 with Ada.Unchecked_Deallocation;
54
55 with System.OS_Constants;
56
57 package GNAT.Sockets is
58
59 -- Sockets are designed to provide a consistent communication facility
60 -- between applications. This package provides an Ada binding to the
61 -- the de-facto standard BSD sockets API. The documentation below covers
62 -- only the specific binding provided by this package. It assumes that
63 -- the reader is already familiar with general network programming and
64 -- sockets usage. A useful reference on this matter is W. Richard Stevens'
65 -- "UNIX Network Programming: The Sockets Networking API"
66 -- (ISBN: 0131411551).
67
68 -- GNAT.Sockets has been designed with several ideas in mind
69
70 -- This is a system independent interface. Therefore, we try as much as
71 -- possible to mask system incompatibilities. Some functionalities are not
72 -- available because there are not fully supported on some systems.
73
74 -- This is a thick binding. For instance, a major effort has been done to
75 -- avoid using memory addresses or untyped ints. We preferred to define
76 -- streams and enumeration types. Errors are not returned as returned
77 -- 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 closest
81 -- to the POSIX specification). For instance, using select(), the
82 -- notification of an asynchronous connect failure is delivered in the
83 -- write socket set (POSIX) instead of the exception socket set (NT).
84
85 -- The example below demonstrates various features of GNAT.Sockets:
86
87 -- with GNAT.Sockets; use GNAT.Sockets;
88
89 -- with Ada.Text_IO;
90 -- with Ada.Exceptions; use Ada.Exceptions;
91
92 -- procedure PingPong is
93
94 -- Group : constant String := "239.255.128.128";
95 -- -- Multicast group: administratively scoped IP address
96
97 -- task Pong is
98 -- entry Start;
99 -- entry Stop;
100 -- end Pong;
101
102 -- task body Pong is
103 -- Address : Sock_Addr_Type;
104 -- Server : Socket_Type;
105 -- Socket : Socket_Type;
106 -- Channel : Stream_Access;
107
108 -- begin
109 -- accept Start;
110 --
111 -- -- Get an Internet address of a host (here the local host name).
112 -- -- Note that a host can have several addresses. Here we get
113 -- -- the first one which is supposed to be the official one.
114
115 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
116
117 -- -- Get a socket address that is an Internet address and a port
118
119 -- Address.Port := 5876;
120
121 -- -- The first step is to create a socket. Once created, this
122 -- -- socket must be associated to with an address. Usually only a
123 -- -- server (Pong here) needs to bind an address explicitly. Most
124 -- -- of the time clients can skip this step because the socket
125 -- -- routines will bind an arbitrary address to an unbound socket.
126
127 -- Create_Socket (Server);
128
129 -- -- Allow reuse of local addresses
130
131 -- Set_Socket_Option
132 -- (Server,
133 -- Socket_Level,
134 -- (Reuse_Address, True));
135
136 -- Bind_Socket (Server, Address);
137
138 -- -- A server marks a socket as willing to receive connect events
139
140 -- Listen_Socket (Server);
141
142 -- -- Once a server calls Listen_Socket, incoming connects events
143 -- -- can be accepted. The returned Socket is a new socket that
144 -- -- represents the server side of the connection. Server remains
145 -- -- available to receive further connections.
146
147 -- Accept_Socket (Server, Socket, Address);
148
149 -- -- Return a stream associated to the connected socket
150
151 -- Channel := Stream (Socket);
152
153 -- -- Force Pong to block
154
155 -- delay 0.2;
156
157 -- -- Receive and print message from client Ping
158
159 -- declare
160 -- Message : String := String'Input (Channel);
161
162 -- begin
163 -- Ada.Text_IO.Put_Line (Message);
164
165 -- -- Send same message back to client Ping
166
167 -- String'Output (Channel, Message);
168 -- end;
169
170 -- Close_Socket (Server);
171 -- Close_Socket (Socket);
172
173 -- -- Part of the multicast example
174
175 -- -- Create a datagram socket to send connectionless, unreliable
176 -- -- messages of a fixed maximum length.
177
178 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
179
180 -- -- Allow reuse of local addresses
181
182 -- Set_Socket_Option
183 -- (Socket,
184 -- Socket_Level,
185 -- (Reuse_Address, True));
186
187 -- -- Controls the live time of the datagram to avoid it being
188 -- -- looped forever due to routing errors. Routers decrement
189 -- -- the TTL of every datagram as it traverses from one network
190 -- -- to another and when its value reaches 0 the packet is
191 -- -- dropped. Default is 1.
192
193 -- Set_Socket_Option
194 -- (Socket,
195 -- IP_Protocol_For_IP_Level,
196 -- (Multicast_TTL, 1));
197
198 -- -- Want the data you send to be looped back to your host
199
200 -- Set_Socket_Option
201 -- (Socket,
202 -- IP_Protocol_For_IP_Level,
203 -- (Multicast_Loop, True));
204
205 -- -- If this socket is intended to receive messages, bind it
206 -- -- to a given socket address.
207
208 -- Address.Addr := Any_Inet_Addr;
209 -- Address.Port := 55505;
210
211 -- Bind_Socket (Socket, Address);
212
213 -- -- Join a multicast group
214
215 -- -- Portability note: On Windows, this option may be set only
216 -- -- on a bound socket.
217
218 -- Set_Socket_Option
219 -- (Socket,
220 -- IP_Protocol_For_IP_Level,
221 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
222
223 -- -- If this socket is intended to send messages, provide the
224 -- -- receiver socket address.
225
226 -- Address.Addr := Inet_Addr (Group);
227 -- Address.Port := 55506;
228
229 -- Channel := Stream (Socket, Address);
230
231 -- -- Receive and print message from client Ping
232
233 -- declare
234 -- Message : String := String'Input (Channel);
235
236 -- begin
237 -- -- Get the address of the sender
238
239 -- Address := Get_Address (Channel);
240 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
241
242 -- -- Send same message back to client Ping
243
244 -- String'Output (Channel, Message);
245 -- end;
246
247 -- Close_Socket (Socket);
248
249 -- accept Stop;
250
251 -- exception when E : others =>
252 -- Ada.Text_IO.Put_Line
253 -- (Exception_Name (E) & ": " & Exception_Message (E));
254 -- end Pong;
255
256 -- task Ping is
257 -- entry Start;
258 -- entry Stop;
259 -- end Ping;
260
261 -- task body Ping is
262 -- Address : Sock_Addr_Type;
263 -- Socket : Socket_Type;
264 -- Channel : Stream_Access;
265
266 -- begin
267 -- accept Start;
268
269 -- -- See comments in Ping section for the first steps
270
271 -- Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
272 -- Address.Port := 5876;
273 -- Create_Socket (Socket);
274
275 -- Set_Socket_Option
276 -- (Socket,
277 -- Socket_Level,
278 -- (Reuse_Address, True));
279
280 -- -- Force Pong to block
281
282 -- delay 0.2;
283
284 -- -- If the client's socket is not bound, Connect_Socket will
285 -- -- bind to an unused address. The client uses Connect_Socket to
286 -- -- create a logical connection between the client's socket and
287 -- -- a server's socket returned by Accept_Socket.
288
289 -- Connect_Socket (Socket, Address);
290
291 -- Channel := Stream (Socket);
292
293 -- -- Send message to server Pong
294
295 -- String'Output (Channel, "Hello world");
296
297 -- -- Force Ping to block
298
299 -- delay 0.2;
300
301 -- -- Receive and print message from server Pong
302
303 -- Ada.Text_IO.Put_Line (String'Input (Channel));
304 -- Close_Socket (Socket);
305
306 -- -- Part of multicast example. Code similar to Pong's one
307
308 -- Create_Socket (Socket, Family_Inet, Socket_Datagram);
309
310 -- Set_Socket_Option
311 -- (Socket,
312 -- Socket_Level,
313 -- (Reuse_Address, True));
314
315 -- Set_Socket_Option
316 -- (Socket,
317 -- IP_Protocol_For_IP_Level,
318 -- (Multicast_TTL, 1));
319
320 -- Set_Socket_Option
321 -- (Socket,
322 -- IP_Protocol_For_IP_Level,
323 -- (Multicast_Loop, True));
324
325 -- Address.Addr := Any_Inet_Addr;
326 -- Address.Port := 55506;
327
328 -- Bind_Socket (Socket, Address);
329
330 -- Set_Socket_Option
331 -- (Socket,
332 -- IP_Protocol_For_IP_Level,
333 -- (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
334
335 -- Address.Addr := Inet_Addr (Group);
336 -- Address.Port := 55505;
337
338 -- Channel := Stream (Socket, Address);
339
340 -- -- Send message to server Pong
341
342 -- String'Output (Channel, "Hello world");
343
344 -- -- Receive and print message from server Pong
345
346 -- declare
347 -- Message : String := String'Input (Channel);
348
349 -- begin
350 -- Address := Get_Address (Channel);
351 -- Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
352 -- end;
353
354 -- Close_Socket (Socket);
355
356 -- accept Stop;
357
358 -- exception when E : others =>
359 -- Ada.Text_IO.Put_Line
360 -- (Exception_Name (E) & ": " & Exception_Message (E));
361 -- end Ping;
362
363 -- begin
364 -- Initialize;
365 -- Ping.Start;
366 -- Pong.Start;
367 -- Ping.Stop;
368 -- Pong.Stop;
369 -- Finalize;
370 -- end PingPong;
371
372 package SOSC renames System.OS_Constants;
373 -- Renaming used to provide short-hand notations thoughout the sockets
374 -- binding. Note that System.OS_Constants is an internal unit, and the
375 -- entities declared therein are not meant for direct access by users,
376 -- including through this renaming.
377
378 procedure Initialize;
379 -- Initialize must be called before using any other socket routines.
380 -- Note that this operation is a no-op on UNIX platforms, but applications
381 -- should make sure to call it if portability is expected: some platforms
382 -- (such as Windows) require initialization before any socket operation.
383
384 procedure Initialize (Process_Blocking_IO : Boolean);
385 pragma Obsolescent
386 (Entity => Initialize,
387 "passing a parameter to Initialize is not supported anymore");
388 -- Previous versions of GNAT.Sockets used to require the user to indicate
389 -- whether socket I/O was process- or thread-blocking on the platform.
390 -- This property is now determined automatically when the run-time library
391 -- is built. The old version of Initialize, taking a parameter, is kept
392 -- for compatibility reasons, but this interface is obsolete (and if the
393 -- value given is wrong, an exception will be raised at run time).
394
395 procedure Finalize;
396 -- After Finalize is called it is not possible to use any routines
397 -- exported in by this package. This procedure is idempotent.
398
399 type Socket_Type is private;
400 -- Sockets are used to implement a reliable bi-directional point-to-point,
401 -- stream-based connections between hosts. No_Socket provides a special
402 -- value to denote uninitialized sockets.
403
404 No_Socket : constant Socket_Type;
405
406 type Selector_Type is limited private;
407 type Selector_Access is access all Selector_Type;
408 -- Selector objects are used to wait for i/o events to occur on sockets
409
410 -- Timeval_Duration is a subtype of Standard.Duration because the full
411 -- range of Standard.Duration cannot be represented in the equivalent C
412 -- structure. Moreover, negative values are not allowed to avoid system
413 -- incompatibilities.
414
415 Immediate : constant Duration := 0.0;
416
417 Timeval_Forever : constant := 2.0 ** (SOSC.SIZEOF_tv_sec * 8 - 1) - 1.0;
418 Forever : constant Duration :=
419 Duration'Min (Duration'Last, Timeval_Forever);
420
421 subtype Timeval_Duration is Duration range Immediate .. Forever;
422
423 subtype Selector_Duration is Timeval_Duration;
424 -- Timeout value for selector operations
425
426 type Selector_Status is (Completed, Expired, Aborted);
427 -- Completion status of a selector operation, indicated as follows:
428 -- Complete: one of the expected events occurred
429 -- Expired: no event occurred before the expiration of the timeout
430 -- Aborted: an external action cancelled the wait operation before
431 -- any event occurred.
432
433 Socket_Error : exception;
434 -- There is only one exception in this package to deal with an error during
435 -- a socket routine. Once raised, its message contains a string describing
436 -- the error code.
437
438 function Image (Socket : Socket_Type) return String;
439 -- Return a printable string for Socket
440
441 function To_C (Socket : Socket_Type) return Integer;
442 -- Return a file descriptor to be used by external subprograms. This is
443 -- useful for C functions that are not yet interfaced in this package.
444
445 type Family_Type is (Family_Inet, Family_Inet6);
446 -- Address family (or protocol family) identifies the communication domain
447 -- and groups protocols with similar address formats. IPv6 will soon be
448 -- supported.
449
450 type Mode_Type is (Socket_Stream, Socket_Datagram);
451 -- Stream sockets provide connection-oriented byte streams. Datagram
452 -- sockets support unreliable connectionless message based communication.
453
454 type Shutmode_Type is (Shut_Read, Shut_Write, Shut_Read_Write);
455 -- When a process closes a socket, the policy is to retain any data queued
456 -- until either a delivery or a timeout expiration (in this case, the data
457 -- are discarded). A finer control is available through shutdown. With
458 -- Shut_Read, no more data can be received from the socket. With_Write, no
459 -- more data can be transmitted. Neither transmission nor reception can be
460 -- performed with Shut_Read_Write.
461
462 type Port_Type is new Natural;
463 -- Classical port definition. No_Port provides a special value to
464 -- denote uninitialized port. Any_Port provides a special value
465 -- enabling all ports.
466
467 Any_Port : constant Port_Type;
468 No_Port : constant Port_Type;
469
470 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
471 -- An Internet address depends on an address family (IPv4 contains 4 octets
472 -- and IPv6 contains 16 octets). Any_Inet_Addr is a special value treated
473 -- like a wildcard enabling all addresses. No_Inet_Addr provides a special
474 -- value to denote uninitialized inet addresses.
475
476 Any_Inet_Addr : constant Inet_Addr_Type;
477 No_Inet_Addr : constant Inet_Addr_Type;
478 Broadcast_Inet_Addr : constant Inet_Addr_Type;
479
480 type Sock_Addr_Type (Family : Family_Type := Family_Inet) is record
481 Addr : Inet_Addr_Type (Family);
482 Port : Port_Type;
483 end record;
484 -- Socket addresses fully define a socket connection with protocol family,
485 -- an Internet address and a port. No_Sock_Addr provides a special value
486 -- for uninitialized socket addresses.
487
488 No_Sock_Addr : constant Sock_Addr_Type;
489
490 function Image (Value : Inet_Addr_Type) return String;
491 -- Return an image of an Internet address. IPv4 notation consists in 4
492 -- octets in decimal format separated by dots. IPv6 notation consists in
493 -- 16 octets in hexadecimal format separated by colons (and possibly
494 -- dots).
495
496 function Image (Value : Sock_Addr_Type) return String;
497 -- Return inet address image and port image separated by a colon
498
499 function Inet_Addr (Image : String) return Inet_Addr_Type;
500 -- Convert address image from numbers-and-dots notation into an
501 -- inet address.
502
503 -- Host entries provide complete information on a given host: the official
504 -- name, an array of alternative names or aliases and array of network
505 -- addresses.
506
507 type Host_Entry_Type
508 (Aliases_Length, Addresses_Length : Natural) is private;
509
510 function Official_Name (E : Host_Entry_Type) return String;
511 -- Return official name in host entry
512
513 function Aliases_Length (E : Host_Entry_Type) return Natural;
514 -- Return number of aliases in host entry
515
516 function Addresses_Length (E : Host_Entry_Type) return Natural;
517 -- Return number of addresses in host entry
518
519 function Aliases
520 (E : Host_Entry_Type;
521 N : Positive := 1) return String;
522 -- Return N'th aliases in host entry. The first index is 1
523
524 function Addresses
525 (E : Host_Entry_Type;
526 N : Positive := 1) return Inet_Addr_Type;
527 -- Return N'th addresses in host entry. The first index is 1
528
529 Host_Error : exception;
530 -- Exception raised by the two following procedures. Once raised, its
531 -- message contains a string describing the error code. This exception is
532 -- raised when an host entry cannot be retrieved.
533
534 function Get_Host_By_Address
535 (Address : Inet_Addr_Type;
536 Family : Family_Type := Family_Inet) return Host_Entry_Type;
537 -- Return host entry structure for the given Inet address. Note that no
538 -- result will be returned if there is no mapping of this IP address to a
539 -- host name in the system tables (host database, DNS or otherwise).
540
541 function Get_Host_By_Name
542 (Name : String) return Host_Entry_Type;
543 -- Return host entry structure for the given host name. Here name is
544 -- either a host name, or an IP address. If Name is an IP address, this
545 -- is equivalent to Get_Host_By_Address (Inet_Addr (Name)).
546
547 function Host_Name return String;
548 -- Return the name of the current host
549
550 type Service_Entry_Type (Aliases_Length : Natural) is private;
551 -- Service entries provide complete information on a given service: the
552 -- official name, an array of alternative names or aliases and the port
553 -- number.
554
555 function Official_Name (S : Service_Entry_Type) return String;
556 -- Return official name in service entry
557
558 function Port_Number (S : Service_Entry_Type) return Port_Type;
559 -- Return port number in service entry
560
561 function Protocol_Name (S : Service_Entry_Type) return String;
562 -- Return Protocol in service entry (usually UDP or TCP)
563
564 function Aliases_Length (S : Service_Entry_Type) return Natural;
565 -- Return number of aliases in service entry
566
567 function Aliases
568 (S : Service_Entry_Type;
569 N : Positive := 1) return String;
570 -- Return N'th aliases in service entry (the first index is 1)
571
572 function Get_Service_By_Name
573 (Name : String;
574 Protocol : String) return Service_Entry_Type;
575 -- Return service entry structure for the given service name
576
577 function Get_Service_By_Port
578 (Port : Port_Type;
579 Protocol : String) return Service_Entry_Type;
580 -- Return service entry structure for the given service port number
581
582 Service_Error : exception;
583 -- Comment required ???
584
585 -- Errors are described by an enumeration type. There is only one exception
586 -- Socket_Error in this package to deal with an error during a socket
587 -- routine. Once raised, its message contains the error code between
588 -- brackets and a string describing the error code.
589
590 -- The name of the enumeration constant documents the error condition
591
592 type Error_Type is
593 (Success,
594 Permission_Denied,
595 Address_Already_In_Use,
596 Cannot_Assign_Requested_Address,
597 Address_Family_Not_Supported_By_Protocol,
598 Operation_Already_In_Progress,
599 Bad_File_Descriptor,
600 Software_Caused_Connection_Abort,
601 Connection_Refused,
602 Connection_Reset_By_Peer,
603 Destination_Address_Required,
604 Bad_Address,
605 Host_Is_Down,
606 No_Route_To_Host,
607 Operation_Now_In_Progress,
608 Interrupted_System_Call,
609 Invalid_Argument,
610 Input_Output_Error,
611 Transport_Endpoint_Already_Connected,
612 Too_Many_Symbolic_Links,
613 Too_Many_Open_Files,
614 Message_Too_Long,
615 File_Name_Too_Long,
616 Network_Is_Down,
617 Network_Dropped_Connection_Because_Of_Reset,
618 Network_Is_Unreachable,
619 No_Buffer_Space_Available,
620 Protocol_Not_Available,
621 Transport_Endpoint_Not_Connected,
622 Socket_Operation_On_Non_Socket,
623 Operation_Not_Supported,
624 Protocol_Family_Not_Supported,
625 Protocol_Not_Supported,
626 Protocol_Wrong_Type_For_Socket,
627 Cannot_Send_After_Transport_Endpoint_Shutdown,
628 Socket_Type_Not_Supported,
629 Connection_Timed_Out,
630 Too_Many_References,
631 Resource_Temporarily_Unavailable,
632 Unknown_Host,
633 Host_Name_Lookup_Failure,
634 Non_Recoverable_Error,
635 Unknown_Server_Error,
636 Cannot_Resolve_Error);
637
638 -- Get_Socket_Options and Set_Socket_Options manipulate options associated
639 -- with a socket. Options may exist at multiple protocol levels in the
640 -- communication stack. Socket_Level is the uppermost socket level.
641
642 type Level_Type is (
643 Socket_Level,
644 IP_Protocol_For_IP_Level,
645 IP_Protocol_For_UDP_Level,
646 IP_Protocol_For_TCP_Level);
647
648 -- There are several options available to manipulate sockets. Each option
649 -- has a name and several values available. Most of the time, the value is
650 -- a boolean to enable or disable this option.
651
652 type Option_Name is (
653 Keep_Alive, -- Enable sending of keep-alive messages
654 Reuse_Address, -- Allow bind to reuse local address
655 Broadcast, -- Enable datagram sockets to recv/send broadcasts
656 Send_Buffer, -- Set/get the maximum socket send buffer in bytes
657 Receive_Buffer, -- Set/get the maximum socket recv buffer in bytes
658 Linger, -- Shutdown wait for msg to be sent or timeout occur
659 Error, -- Get and clear the pending socket error
660 No_Delay, -- Do not delay send to coalesce data (TCP_NODELAY)
661 Add_Membership, -- Join a multicast group
662 Drop_Membership, -- Leave a multicast group
663 Multicast_If, -- Set default out interface for multicast packets
664 Multicast_TTL, -- Set the time-to-live of sent multicast packets
665 Multicast_Loop, -- Sent multicast packets are looped to local socket
666 Receive_Packet_Info, -- Receive low level packet info as ancillary data
667 Send_Timeout, -- Set timeout value for output
668 Receive_Timeout); -- Set timeout value for input
669
670 type Option_Type (Name : Option_Name := Keep_Alive) is record
671 case Name is
672 when Keep_Alive |
673 Reuse_Address |
674 Broadcast |
675 Linger |
676 No_Delay |
677 Receive_Packet_Info |
678 Multicast_Loop =>
679 Enabled : Boolean;
680
681 case Name is
682 when Linger =>
683 Seconds : Natural;
684 when others =>
685 null;
686 end case;
687
688 when Send_Buffer |
689 Receive_Buffer =>
690 Size : Natural;
691
692 when Error =>
693 Error : Error_Type;
694
695 when Add_Membership |
696 Drop_Membership =>
697 Multicast_Address : Inet_Addr_Type;
698 Local_Interface : Inet_Addr_Type;
699
700 when Multicast_If =>
701 Outgoing_If : Inet_Addr_Type;
702
703 when Multicast_TTL =>
704 Time_To_Live : Natural;
705
706 when Send_Timeout |
707 Receive_Timeout =>
708 Timeout : Timeval_Duration;
709
710 end case;
711 end record;
712
713 -- There are several controls available to manipulate sockets. Each option
714 -- has a name and several values available. These controls differ from the
715 -- socket options in that they are not specific to sockets but are
716 -- available for any device.
717
718 type Request_Name is (
719 Non_Blocking_IO, -- Cause a caller not to wait on blocking operations.
720 N_Bytes_To_Read); -- Return the number of bytes available to read
721
722 type Request_Type (Name : Request_Name := Non_Blocking_IO) is record
723 case Name is
724 when Non_Blocking_IO =>
725 Enabled : Boolean;
726
727 when N_Bytes_To_Read =>
728 Size : Natural;
729
730 end case;
731 end record;
732
733 -- A request flag allows to specify the type of message transmissions or
734 -- receptions. A request flag can be combination of zero or more
735 -- predefined request flags.
736
737 type Request_Flag_Type is private;
738
739 No_Request_Flag : constant Request_Flag_Type;
740 -- This flag corresponds to the normal execution of an operation
741
742 Process_Out_Of_Band_Data : constant Request_Flag_Type;
743 -- This flag requests that the receive or send function operates on
744 -- out-of-band data when the socket supports this notion (e.g.
745 -- Socket_Stream).
746
747 Peek_At_Incoming_Data : constant Request_Flag_Type;
748 -- This flag causes the receive operation to return data from the beginning
749 -- of the receive queue without removing that data from the queue. A
750 -- subsequent receive call will return the same data.
751
752 Wait_For_A_Full_Reception : constant Request_Flag_Type;
753 -- This flag requests that the operation block until the full request is
754 -- satisfied. However, the call may still return less data than requested
755 -- if a signal is caught, an error or disconnect occurs, or the next data
756 -- to be received is of a different type than that returned. Note that
757 -- this flag depends on support in the underlying sockets implementation,
758 -- and is not supported under Windows.
759
760 Send_End_Of_Record : constant Request_Flag_Type;
761 -- This flag indicates that the entire message has been sent and so this
762 -- terminates the record.
763
764 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type;
765 -- Combine flag L with flag R
766
767 type Stream_Element_Reference is access all Ada.Streams.Stream_Element;
768
769 type Vector_Element is record
770 Base : Stream_Element_Reference;
771 Length : Ada.Streams.Stream_Element_Count;
772 end record;
773
774 type Vector_Type is array (Integer range <>) of Vector_Element;
775
776 procedure Create_Socket
777 (Socket : out Socket_Type;
778 Family : Family_Type := Family_Inet;
779 Mode : Mode_Type := Socket_Stream);
780 -- Create an endpoint for communication. Raises Socket_Error on error
781
782 procedure Accept_Socket
783 (Server : Socket_Type;
784 Socket : out Socket_Type;
785 Address : out Sock_Addr_Type);
786 -- Extracts the first connection request on the queue of pending
787 -- connections, creates a new connected socket with mostly the same
788 -- properties as Server, and allocates a new socket. The returned Address
789 -- is filled in with the address of the connection. Raises Socket_Error on
790 -- error.
791
792 procedure Accept_Socket
793 (Server : Socket_Type;
794 Socket : out Socket_Type;
795 Address : out Sock_Addr_Type;
796 Timeout : Selector_Duration;
797 Selector : access Selector_Type := null;
798 Status : out Selector_Status);
799 -- Accept a new connection on Server using Accept_Socket, waiting no longer
800 -- than the given timeout duration. Status is set to indicate whether the
801 -- operation completed successfully, timed out, or was aborted. If Selector
802 -- is not null, the designated selector is used to wait for the socket to
803 -- become available, else a private selector object is created by this
804 -- procedure and destroyed before it returns.
805
806 procedure Bind_Socket
807 (Socket : Socket_Type;
808 Address : Sock_Addr_Type);
809 -- Once a socket is created, assign a local address to it. Raise
810 -- Socket_Error on error.
811
812 procedure Close_Socket (Socket : Socket_Type);
813 -- Close a socket and more specifically a non-connected socket
814
815 procedure Connect_Socket
816 (Socket : Socket_Type;
817 Server : Sock_Addr_Type);
818 -- Make a connection to another socket which has the address of Server.
819 -- Raises Socket_Error on error.
820
821 procedure Connect_Socket
822 (Socket : Socket_Type;
823 Server : Sock_Addr_Type;
824 Timeout : Selector_Duration;
825 Selector : access Selector_Type := null;
826 Status : out Selector_Status);
827 -- Connect Socket to the given Server address using Connect_Socket, waiting
828 -- no longer than the given timeout duration. Status is set to indicate
829 -- whether the operation completed successfully, timed out, or was aborted.
830 -- If Selector is not null, the designated selector is used to wait for the
831 -- socket to become available, else a private selector object is created
832 -- by this procedure and destroyed before it returns.
833
834 procedure Control_Socket
835 (Socket : Socket_Type;
836 Request : in out Request_Type);
837 -- Obtain or set parameter values that control the socket. This control
838 -- differs from the socket options in that they are not specific to sockets
839 -- but are available for any device.
840
841 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type;
842 -- Return the peer or remote socket address of a socket. Raise
843 -- Socket_Error on error.
844
845 function Get_Socket_Name (Socket : Socket_Type) return Sock_Addr_Type;
846 -- Return the local or current socket address of a socket. Return
847 -- No_Sock_Addr on error (e.g. socket closed or not locally bound).
848
849 function Get_Socket_Option
850 (Socket : Socket_Type;
851 Level : Level_Type := Socket_Level;
852 Name : Option_Name) return Option_Type;
853 -- Get the options associated with a socket. Raises Socket_Error on error
854
855 procedure Listen_Socket
856 (Socket : Socket_Type;
857 Length : Natural := 15);
858 -- To accept connections, a socket is first created with Create_Socket,
859 -- a willingness to accept incoming connections and a queue Length for
860 -- incoming connections are specified. Raise Socket_Error on error.
861 -- The queue length of 15 is an example value that should be appropriate
862 -- in usual cases. It can be adjusted according to each application's
863 -- particular requirements.
864
865 procedure Receive_Socket
866 (Socket : Socket_Type;
867 Item : out Ada.Streams.Stream_Element_Array;
868 Last : out Ada.Streams.Stream_Element_Offset;
869 Flags : Request_Flag_Type := No_Request_Flag);
870 -- Receive message from Socket. Last is the index value such that Item
871 -- (Last) is the last character assigned. Note that Last is set to
872 -- Item'First - 1 when the socket has been closed by peer. This is not an
873 -- error and no exception is raised. Flags allows to control the
874 -- reception. Raise Socket_Error on error.
875
876 procedure Receive_Socket
877 (Socket : Socket_Type;
878 Item : out Ada.Streams.Stream_Element_Array;
879 Last : out Ada.Streams.Stream_Element_Offset;
880 From : out Sock_Addr_Type;
881 Flags : Request_Flag_Type := No_Request_Flag);
882 -- Receive message from Socket. If Socket is not connection-oriented, the
883 -- source address From of the message is filled in. Last is the index
884 -- value such that Item (Last) is the last character assigned. Flags
885 -- allows to control the reception. Raises Socket_Error on error.
886
887 procedure Receive_Vector
888 (Socket : Socket_Type;
889 Vector : Vector_Type;
890 Count : out Ada.Streams.Stream_Element_Count);
891 -- Receive data from a socket and scatter it into the set of vector
892 -- elements Vector. Count is set to the count of received stream elements.
893
894 function Resolve_Exception
895 (Occurrence : Ada.Exceptions.Exception_Occurrence) return Error_Type;
896 -- When Socket_Error or Host_Error are raised, the exception message
897 -- contains the error code between brackets and a string describing the
898 -- error code. Resolve_Error extracts the error code from an exception
899 -- message and translate it into an enumeration value.
900
901 procedure Send_Socket
902 (Socket : Socket_Type;
903 Item : Ada.Streams.Stream_Element_Array;
904 Last : out Ada.Streams.Stream_Element_Offset;
905 Flags : Request_Flag_Type := No_Request_Flag);
906 -- Transmit a message to another socket. Note that Last is set to
907 -- Item'First-1 when socket has been closed by peer. This is not
908 -- considered an error and no exception is raised. Flags allows to control
909 -- the transmission. Raises Socket_Error on any other error condition.
910
911 procedure Send_Socket
912 (Socket : Socket_Type;
913 Item : Ada.Streams.Stream_Element_Array;
914 Last : out Ada.Streams.Stream_Element_Offset;
915 To : Sock_Addr_Type;
916 Flags : Request_Flag_Type := No_Request_Flag);
917 -- Transmit a message to another socket. The address is given by To. Flags
918 -- allows to control the transmission. Raises Socket_Error on error.
919
920 procedure Send_Vector
921 (Socket : Socket_Type;
922 Vector : Vector_Type;
923 Count : out Ada.Streams.Stream_Element_Count);
924 -- Transmit data gathered from the set of vector elements Vector to a
925 -- socket. Count is set to the count of transmitted stream elements.
926
927 procedure Set_Socket_Option
928 (Socket : Socket_Type;
929 Level : Level_Type := Socket_Level;
930 Option : Option_Type);
931 -- Manipulate socket options. Raises Socket_Error on error
932
933 procedure Shutdown_Socket
934 (Socket : Socket_Type;
935 How : Shutmode_Type := Shut_Read_Write);
936 -- Shutdown a connected socket. If How is Shut_Read, further receives will
937 -- be disallowed. If How is Shut_Write, further sends will be disallowed.
938 -- If how is Shut_Read_Write, further sends and receives will be
939 -- disallowed.
940
941 type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
942 -- Same interface as Ada.Streams.Stream_IO
943
944 function Stream (Socket : Socket_Type) return Stream_Access;
945 -- Create a stream associated with a stream-based socket that is
946 -- already connected.
947
948 function Stream
949 (Socket : Socket_Type;
950 Send_To : Sock_Addr_Type) return Stream_Access;
951 -- Create a stream associated with a datagram-based socket that is already
952 -- bound. Send_To is the socket address to which messages are being sent.
953
954 function Get_Address
955 (Stream : not null Stream_Access) return Sock_Addr_Type;
956 -- Return the socket address from which the last message was received
957
958 procedure Free is new Ada.Unchecked_Deallocation
959 (Ada.Streams.Root_Stream_Type'Class, Stream_Access);
960 -- Destroy a stream created by one of the Stream functions above,
961 -- releasing the corresponding resources. The user is responsible for
962 -- calling this subprogram when the stream is not needed anymore.
963
964 type Socket_Set_Type is limited private;
965 -- This type allows to manipulate sets of sockets. It allows to wait for
966 -- events on multiple endpoints at one time. This is an access type on a
967 -- system dependent structure. To avoid memory leaks it is highly
968 -- recommended to clean the access value with procedure Empty.
969
970 procedure Clear (Item : in out Socket_Set_Type; Socket : Socket_Type);
971 -- Remove Socket from Item
972
973 procedure Copy (Source : Socket_Set_Type; Target : in out Socket_Set_Type);
974 -- Copy Source into Target as Socket_Set_Type is limited private
975
976 procedure Empty (Item : in out Socket_Set_Type);
977 -- Remove all Sockets from Item and deallocate internal data
978
979 procedure Get (Item : in out Socket_Set_Type; Socket : out Socket_Type);
980 -- Extract a Socket from socket set Item. Socket is set to
981 -- No_Socket when the set is empty.
982
983 function Is_Empty (Item : Socket_Set_Type) return Boolean;
984 -- Return True iff Item is empty
985
986 function Is_Set
987 (Item : Socket_Set_Type;
988 Socket : Socket_Type) return Boolean;
989 -- Return True iff Socket is present in Item
990
991 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type);
992 -- Insert Socket into Item
993
994 -- The select(2) system call waits for events to occur on any of a set of
995 -- file descriptors. Usually, three independent sets of descriptors are
996 -- watched (read, write and exception). A timeout gives an upper bound
997 -- on the amount of time elapsed before select returns. This function
998 -- blocks until an event occurs. On some platforms, the select(2) system
999 -- can block the full process (not just the calling thread).
1000 --
1001 -- Check_Selector provides the very same behaviour. The only difference is
1002 -- that it does not watch for exception events. Note that on some
1003 -- platforms it is kept process blocking on purpose. The timeout parameter
1004 -- allows the user to have the behaviour he wants. Abort_Selector allows
1005 -- to abort safely a Check_Selector that is blocked forever. A special
1006 -- file descriptor is opened by Create_Selector and included in each call
1007 -- to Check_Selector. Abort_Selector causes an event to occur on this
1008 -- descriptor in order to unblock Check_Selector. The user must call
1009 -- Close_Selector to discard this special file. A reason to abort a select
1010 -- operation is typically to add a socket in one of the socket sets when
1011 -- the timeout is set to forever.
1012
1013 procedure Create_Selector (Selector : out Selector_Type);
1014 -- Create a new selector
1015
1016 procedure Close_Selector (Selector : in out Selector_Type);
1017 -- Close Selector and all internal descriptors associated; deallocate any
1018 -- associated resources. This subprogram may be called only when there is
1019 -- no other task still using Selector (i.e. still executing Check_Selector
1020 -- or Abort_Selector on this Selector).
1021
1022 procedure Check_Selector
1023 (Selector : in out Selector_Type;
1024 R_Socket_Set : in out Socket_Set_Type;
1025 W_Socket_Set : in out Socket_Set_Type;
1026 Status : out Selector_Status;
1027 Timeout : Selector_Duration := Forever);
1028 -- Return when one Socket in R_Socket_Set has some data to be read or if
1029 -- one Socket in W_Socket_Set is ready to transmit some data. In these
1030 -- cases Status is set to Completed and sockets that are ready are set in
1031 -- R_Socket_Set or W_Socket_Set. Status is set to Expired if no socket was
1032 -- ready after a Timeout expiration. Status is set to Aborted if an abort
1033 -- signal has been received while checking socket status.
1034 -- Note that two different Socket_Set_Type objects must be passed as
1035 -- R_Socket_Set and W_Socket_Set (even if they denote the same set of
1036 -- Sockets), or some event may be lost.
1037 -- Socket_Error is raised when the select(2) system call returns an
1038 -- error condition, or when a read error occurs on the signalling socket
1039 -- used for the implementation of Abort_Selector.
1040
1041 procedure Check_Selector
1042 (Selector : in out Selector_Type;
1043 R_Socket_Set : in out Socket_Set_Type;
1044 W_Socket_Set : in out Socket_Set_Type;
1045 E_Socket_Set : in out Socket_Set_Type;
1046 Status : out Selector_Status;
1047 Timeout : Selector_Duration := Forever);
1048 -- This refined version of Check_Selector allows to watch for exception
1049 -- events (that is notifications of out-of-band transmission and
1050 -- reception). As above, all of R_Socket_Set, W_Socket_Set and
1051 -- E_Socket_Set must be different objects.
1052
1053 procedure Abort_Selector (Selector : Selector_Type);
1054 -- Send an abort signal to the selector
1055
1056 type Fd_Set_Access is private;
1057 No_Fd_Set_Access : constant Fd_Set_Access;
1058 -- ??? This type must not be used directly, it needs to be visible because
1059 -- it is used in the visible part of GNAT.Sockets.Thin_Common. This is
1060 -- really an inversion of abstraction. The private part of GNAT.Sockets
1061 -- needs to have visibility on this type, but since Thin_Common is a child
1062 -- of Sockets, the type can't be declared there. The correct fix would
1063 -- be to move the thin sockets binding outside of GNAT.Sockets altogether,
1064 -- e.g. by renaming it to GNAT.Sockets_Thin.
1065
1066 private
1067
1068 type Socket_Type is new Integer;
1069 No_Socket : constant Socket_Type := -1;
1070
1071 type Selector_Type is limited record
1072 R_Sig_Socket : Socket_Type := No_Socket;
1073 W_Sig_Socket : Socket_Type := No_Socket;
1074 -- Signalling sockets used to abort a select operation
1075 end record;
1076
1077 pragma Volatile (Selector_Type);
1078
1079 type Fd_Set is null record;
1080 type Fd_Set_Access is access all Fd_Set;
1081 pragma Convention (C, Fd_Set_Access);
1082 No_Fd_Set_Access : constant Fd_Set_Access := null;
1083
1084 type Socket_Set_Type is record
1085 Last : Socket_Type := No_Socket;
1086 Set : Fd_Set_Access;
1087 end record;
1088
1089 subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
1090 -- Octet for Internet address
1091
1092 type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;
1093
1094 subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 .. 4);
1095 subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);
1096
1097 type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
1098 case Family is
1099 when Family_Inet =>
1100 Sin_V4 : Inet_Addr_V4_Type := (others => 0);
1101
1102 when Family_Inet6 =>
1103 Sin_V6 : Inet_Addr_V6_Type := (others => 0);
1104 end case;
1105 end record;
1106
1107 Any_Port : constant Port_Type := 0;
1108 No_Port : constant Port_Type := 0;
1109
1110 Any_Inet_Addr : constant Inet_Addr_Type :=
1111 (Family_Inet, (others => 0));
1112 No_Inet_Addr : constant Inet_Addr_Type :=
1113 (Family_Inet, (others => 0));
1114 Broadcast_Inet_Addr : constant Inet_Addr_Type :=
1115 (Family_Inet, (others => 255));
1116
1117 No_Sock_Addr : constant Sock_Addr_Type := (Family_Inet, No_Inet_Addr, 0);
1118
1119 Max_Name_Length : constant := 64;
1120 -- The constant MAXHOSTNAMELEN is usually set to 64
1121
1122 subtype Name_Index is Natural range 1 .. Max_Name_Length;
1123
1124 type Name_Type
1125 (Length : Name_Index := Max_Name_Length)
1126 is record
1127 Name : String (1 .. Length);
1128 end record;
1129 -- We need fixed strings to avoid access types in host entry type
1130
1131 type Name_Array is array (Natural range <>) of Name_Type;
1132 type Inet_Addr_Array is array (Natural range <>) of Inet_Addr_Type;
1133
1134 type Host_Entry_Type (Aliases_Length, Addresses_Length : Natural) is record
1135 Official : Name_Type;
1136 Aliases : Name_Array (1 .. Aliases_Length);
1137 Addresses : Inet_Addr_Array (1 .. Addresses_Length);
1138 end record;
1139
1140 type Service_Entry_Type (Aliases_Length : Natural) is record
1141 Official : Name_Type;
1142 Aliases : Name_Array (1 .. Aliases_Length);
1143 Port : Port_Type;
1144 Protocol : Name_Type;
1145 end record;
1146
1147 type Request_Flag_Type is mod 2 ** 8;
1148 No_Request_Flag : constant Request_Flag_Type := 0;
1149 Process_Out_Of_Band_Data : constant Request_Flag_Type := 1;
1150 Peek_At_Incoming_Data : constant Request_Flag_Type := 2;
1151 Wait_For_A_Full_Reception : constant Request_Flag_Type := 4;
1152 Send_End_Of_Record : constant Request_Flag_Type := 8;
1153
1154 end GNAT.Sockets;