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