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