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