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