annotate gcc/ada/libgnat/g-socket.ads @ 131:84e7813d76e9

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