1 |
12 |
jlechner |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT COMPILER COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . S O C K E T S . T H I N --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 2001-2005, 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 a target dependent thin interface to the sockets
|
35 |
|
|
-- layer for use by the GNAT.Sockets package (g-socket.ads). This package
|
36 |
|
|
-- should not be directly with'ed by an applications program.
|
37 |
|
|
|
38 |
|
|
-- This version is for NT
|
39 |
|
|
|
40 |
|
|
with Interfaces.C.Pointers;
|
41 |
|
|
with Interfaces.C.Strings;
|
42 |
|
|
|
43 |
|
|
with GNAT.Sockets.Constants;
|
44 |
|
|
|
45 |
|
|
with System;
|
46 |
|
|
|
47 |
|
|
package GNAT.Sockets.Thin is
|
48 |
|
|
|
49 |
|
|
package C renames Interfaces.C;
|
50 |
|
|
|
51 |
|
|
use type C.int;
|
52 |
|
|
-- So that we can declare the Failure constant below
|
53 |
|
|
|
54 |
|
|
Success : constant C.int := 0;
|
55 |
|
|
Failure : constant C.int := -1;
|
56 |
|
|
|
57 |
|
|
function Socket_Errno return Integer;
|
58 |
|
|
-- Returns last socket error number
|
59 |
|
|
|
60 |
|
|
procedure Set_Socket_Errno (Errno : Integer);
|
61 |
|
|
-- Set last socket error number
|
62 |
|
|
|
63 |
|
|
function Socket_Error_Message
|
64 |
|
|
(Errno : Integer) return C.Strings.chars_ptr;
|
65 |
|
|
-- Returns the error message string for the error number Errno. If
|
66 |
|
|
-- Errno is not known it returns "Unknown system error".
|
67 |
|
|
|
68 |
|
|
function Host_Errno return Integer;
|
69 |
|
|
pragma Import (C, Host_Errno, "__gnat_get_h_errno");
|
70 |
|
|
-- Returns last host error number
|
71 |
|
|
|
72 |
|
|
subtype Fd_Set_Access is System.Address;
|
73 |
|
|
No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
|
74 |
|
|
|
75 |
|
|
type time_t is
|
76 |
|
|
range -(2 ** (8 * Constants.SIZEOF_tv_sec - 1))
|
77 |
|
|
.. 2 ** (8 * Constants.SIZEOF_tv_sec - 1) - 1;
|
78 |
|
|
for time_t'Size use 8 * Constants.SIZEOF_tv_sec;
|
79 |
|
|
pragma Convention (C, time_t);
|
80 |
|
|
|
81 |
|
|
type suseconds_t is
|
82 |
|
|
range -(2 ** (8 * Constants.SIZEOF_tv_usec - 1))
|
83 |
|
|
.. 2 ** (8 * Constants.SIZEOF_tv_usec - 1) - 1;
|
84 |
|
|
for suseconds_t'Size use 8 * Constants.SIZEOF_tv_usec;
|
85 |
|
|
pragma Convention (C, suseconds_t);
|
86 |
|
|
|
87 |
|
|
type Timeval is record
|
88 |
|
|
Tv_Sec : time_t;
|
89 |
|
|
Tv_Usec : suseconds_t;
|
90 |
|
|
end record;
|
91 |
|
|
pragma Convention (C, Timeval);
|
92 |
|
|
|
93 |
|
|
type Timeval_Access is access all Timeval;
|
94 |
|
|
pragma Convention (C, Timeval_Access);
|
95 |
|
|
|
96 |
|
|
Immediat : constant Timeval := (0, 0);
|
97 |
|
|
|
98 |
|
|
type Int_Access is access all C.int;
|
99 |
|
|
pragma Convention (C, Int_Access);
|
100 |
|
|
-- Access to C integers
|
101 |
|
|
|
102 |
|
|
type Chars_Ptr_Array is array (C.size_t range <>) of
|
103 |
|
|
aliased C.Strings.chars_ptr;
|
104 |
|
|
|
105 |
|
|
package Chars_Ptr_Pointers is
|
106 |
|
|
new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
|
107 |
|
|
C.Strings.Null_Ptr);
|
108 |
|
|
-- Arrays of C (char *)
|
109 |
|
|
|
110 |
|
|
type In_Addr is record
|
111 |
|
|
S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
|
112 |
|
|
end record;
|
113 |
|
|
pragma Convention (C, In_Addr);
|
114 |
|
|
-- Internet address
|
115 |
|
|
|
116 |
|
|
type In_Addr_Access is access all In_Addr;
|
117 |
|
|
pragma Convention (C, In_Addr_Access);
|
118 |
|
|
-- Access to internet address
|
119 |
|
|
|
120 |
|
|
Inaddr_Any : aliased constant In_Addr := (others => 0);
|
121 |
|
|
-- Any internet address (all the interfaces)
|
122 |
|
|
|
123 |
|
|
type In_Addr_Access_Array is array (C.size_t range <>)
|
124 |
|
|
of aliased In_Addr_Access;
|
125 |
|
|
pragma Convention (C, In_Addr_Access_Array);
|
126 |
|
|
package In_Addr_Access_Pointers is
|
127 |
|
|
new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
|
128 |
|
|
-- Array of internet addresses
|
129 |
|
|
|
130 |
|
|
type Sockaddr is record
|
131 |
|
|
Sa_Family : C.unsigned_short;
|
132 |
|
|
Sa_Data : C.char_array (1 .. 14);
|
133 |
|
|
end record;
|
134 |
|
|
pragma Convention (C, Sockaddr);
|
135 |
|
|
-- Socket address
|
136 |
|
|
|
137 |
|
|
type Sockaddr_Access is access all Sockaddr;
|
138 |
|
|
pragma Convention (C, Sockaddr_Access);
|
139 |
|
|
-- Access to socket address
|
140 |
|
|
|
141 |
|
|
type Sockaddr_In is record
|
142 |
|
|
Sin_Family : C.unsigned_short := Constants.AF_INET;
|
143 |
|
|
Sin_Port : C.unsigned_short := 0;
|
144 |
|
|
Sin_Addr : In_Addr := Inaddr_Any;
|
145 |
|
|
Sin_Zero : C.char_array (1 .. 8) := (others => C.char'Val (0));
|
146 |
|
|
end record;
|
147 |
|
|
pragma Convention (C, Sockaddr_In);
|
148 |
|
|
-- Internet socket address
|
149 |
|
|
|
150 |
|
|
type Sockaddr_In_Access is access all Sockaddr_In;
|
151 |
|
|
pragma Convention (C, Sockaddr_In_Access);
|
152 |
|
|
-- Access to internet socket address
|
153 |
|
|
|
154 |
|
|
procedure Set_Length
|
155 |
|
|
(Sin : Sockaddr_In_Access;
|
156 |
|
|
Len : C.int);
|
157 |
|
|
pragma Inline (Set_Length);
|
158 |
|
|
-- Set Sin.Sin_Length to Len.
|
159 |
|
|
-- On this platform, nothing is done as there is no such field.
|
160 |
|
|
|
161 |
|
|
procedure Set_Family
|
162 |
|
|
(Sin : Sockaddr_In_Access;
|
163 |
|
|
Family : C.int);
|
164 |
|
|
pragma Inline (Set_Family);
|
165 |
|
|
-- Set Sin.Sin_Family to Family
|
166 |
|
|
|
167 |
|
|
procedure Set_Port
|
168 |
|
|
(Sin : Sockaddr_In_Access;
|
169 |
|
|
Port : C.unsigned_short);
|
170 |
|
|
pragma Inline (Set_Port);
|
171 |
|
|
-- Set Sin.Sin_Port to Port
|
172 |
|
|
|
173 |
|
|
procedure Set_Address
|
174 |
|
|
(Sin : Sockaddr_In_Access;
|
175 |
|
|
Address : In_Addr);
|
176 |
|
|
pragma Inline (Set_Address);
|
177 |
|
|
-- Set Sin.Sin_Addr to Address
|
178 |
|
|
|
179 |
|
|
type Hostent is record
|
180 |
|
|
H_Name : C.Strings.chars_ptr;
|
181 |
|
|
H_Aliases : Chars_Ptr_Pointers.Pointer;
|
182 |
|
|
H_Addrtype : C.short;
|
183 |
|
|
H_Length : C.short;
|
184 |
|
|
H_Addr_List : In_Addr_Access_Pointers.Pointer;
|
185 |
|
|
end record;
|
186 |
|
|
pragma Convention (C, Hostent);
|
187 |
|
|
-- Host entry
|
188 |
|
|
|
189 |
|
|
type Hostent_Access is access all Hostent;
|
190 |
|
|
pragma Convention (C, Hostent_Access);
|
191 |
|
|
-- Access to host entry
|
192 |
|
|
|
193 |
|
|
type Servent is record
|
194 |
|
|
S_Name : C.Strings.chars_ptr;
|
195 |
|
|
S_Aliases : Chars_Ptr_Pointers.Pointer;
|
196 |
|
|
S_Port : C.int;
|
197 |
|
|
S_Proto : C.Strings.chars_ptr;
|
198 |
|
|
end record;
|
199 |
|
|
pragma Convention (C, Servent);
|
200 |
|
|
-- Service entry
|
201 |
|
|
|
202 |
|
|
type Servent_Access is access all Servent;
|
203 |
|
|
pragma Convention (C, Servent_Access);
|
204 |
|
|
-- Access to service entry
|
205 |
|
|
|
206 |
|
|
type Two_Int is array (0 .. 1) of C.int;
|
207 |
|
|
pragma Convention (C, Two_Int);
|
208 |
|
|
-- Used with pipe()
|
209 |
|
|
|
210 |
|
|
function C_Accept
|
211 |
|
|
(S : C.int;
|
212 |
|
|
Addr : System.Address;
|
213 |
|
|
Addrlen : access C.int) return C.int;
|
214 |
|
|
|
215 |
|
|
function C_Bind
|
216 |
|
|
(S : C.int;
|
217 |
|
|
Name : System.Address;
|
218 |
|
|
Namelen : C.int) return C.int;
|
219 |
|
|
|
220 |
|
|
function C_Close
|
221 |
|
|
(Fd : C.int) return C.int;
|
222 |
|
|
|
223 |
|
|
function C_Connect
|
224 |
|
|
(S : C.int;
|
225 |
|
|
Name : System.Address;
|
226 |
|
|
Namelen : C.int) return C.int;
|
227 |
|
|
|
228 |
|
|
function C_Gethostbyaddr
|
229 |
|
|
(Addr : System.Address;
|
230 |
|
|
Length : C.int;
|
231 |
|
|
Typ : C.int) return Hostent_Access;
|
232 |
|
|
|
233 |
|
|
function C_Gethostbyname
|
234 |
|
|
(Name : C.char_array) return Hostent_Access;
|
235 |
|
|
|
236 |
|
|
function C_Gethostname
|
237 |
|
|
(Name : System.Address;
|
238 |
|
|
Namelen : C.int) return C.int;
|
239 |
|
|
|
240 |
|
|
function C_Getpeername
|
241 |
|
|
(S : C.int;
|
242 |
|
|
Name : System.Address;
|
243 |
|
|
Namelen : access C.int) return C.int;
|
244 |
|
|
|
245 |
|
|
function C_Getservbyname
|
246 |
|
|
(Name : C.char_array;
|
247 |
|
|
Proto : C.char_array) return Servent_Access;
|
248 |
|
|
|
249 |
|
|
function C_Getservbyport
|
250 |
|
|
(Port : C.int;
|
251 |
|
|
Proto : C.char_array) return Servent_Access;
|
252 |
|
|
|
253 |
|
|
function C_Getsockname
|
254 |
|
|
(S : C.int;
|
255 |
|
|
Name : System.Address;
|
256 |
|
|
Namelen : access C.int) return C.int;
|
257 |
|
|
|
258 |
|
|
function C_Getsockopt
|
259 |
|
|
(S : C.int;
|
260 |
|
|
Level : C.int;
|
261 |
|
|
Optname : C.int;
|
262 |
|
|
Optval : System.Address;
|
263 |
|
|
Optlen : access C.int) return C.int;
|
264 |
|
|
|
265 |
|
|
function C_Inet_Addr
|
266 |
|
|
(Cp : C.Strings.chars_ptr) return C.int;
|
267 |
|
|
|
268 |
|
|
function C_Ioctl
|
269 |
|
|
(S : C.int;
|
270 |
|
|
Req : C.int;
|
271 |
|
|
Arg : Int_Access) return C.int;
|
272 |
|
|
|
273 |
|
|
function C_Listen
|
274 |
|
|
(S : C.int;
|
275 |
|
|
Backlog : C.int) return C.int;
|
276 |
|
|
|
277 |
|
|
function C_Readv
|
278 |
|
|
(Socket : C.int;
|
279 |
|
|
Iov : System.Address;
|
280 |
|
|
Iovcnt : C.int) return C.int;
|
281 |
|
|
|
282 |
|
|
function C_Recv
|
283 |
|
|
(S : C.int;
|
284 |
|
|
Buf : System.Address;
|
285 |
|
|
Len : C.int;
|
286 |
|
|
Flags : C.int) return C.int;
|
287 |
|
|
|
288 |
|
|
function C_Recvfrom
|
289 |
|
|
(S : C.int;
|
290 |
|
|
Buf : System.Address;
|
291 |
|
|
Len : C.int;
|
292 |
|
|
Flags : C.int;
|
293 |
|
|
From : Sockaddr_In_Access;
|
294 |
|
|
Fromlen : access C.int) return C.int;
|
295 |
|
|
|
296 |
|
|
function C_Select
|
297 |
|
|
(Nfds : C.int;
|
298 |
|
|
Readfds : Fd_Set_Access;
|
299 |
|
|
Writefds : Fd_Set_Access;
|
300 |
|
|
Exceptfds : Fd_Set_Access;
|
301 |
|
|
Timeout : Timeval_Access) return C.int;
|
302 |
|
|
|
303 |
|
|
function C_Send
|
304 |
|
|
(S : C.int;
|
305 |
|
|
Buf : System.Address;
|
306 |
|
|
Len : C.int;
|
307 |
|
|
Flags : C.int) return C.int;
|
308 |
|
|
|
309 |
|
|
function C_Sendto
|
310 |
|
|
(S : C.int;
|
311 |
|
|
Msg : System.Address;
|
312 |
|
|
Len : C.int;
|
313 |
|
|
Flags : C.int;
|
314 |
|
|
To : Sockaddr_In_Access;
|
315 |
|
|
Tolen : C.int) return C.int;
|
316 |
|
|
|
317 |
|
|
function C_Setsockopt
|
318 |
|
|
(S : C.int;
|
319 |
|
|
Level : C.int;
|
320 |
|
|
Optname : C.int;
|
321 |
|
|
Optval : System.Address;
|
322 |
|
|
Optlen : C.int) return C.int;
|
323 |
|
|
|
324 |
|
|
function C_Shutdown
|
325 |
|
|
(S : C.int;
|
326 |
|
|
How : C.int) return C.int;
|
327 |
|
|
|
328 |
|
|
function C_Socket
|
329 |
|
|
(Domain : C.int;
|
330 |
|
|
Typ : C.int;
|
331 |
|
|
Protocol : C.int) return C.int;
|
332 |
|
|
|
333 |
|
|
function C_Strerror
|
334 |
|
|
(Errnum : C.int) return C.Strings.chars_ptr;
|
335 |
|
|
|
336 |
|
|
function C_System
|
337 |
|
|
(Command : System.Address) return C.int;
|
338 |
|
|
|
339 |
|
|
function C_Writev
|
340 |
|
|
(Socket : C.int;
|
341 |
|
|
Iov : System.Address;
|
342 |
|
|
Iovcnt : C.int) return C.int;
|
343 |
|
|
|
344 |
|
|
function WSAStartup
|
345 |
|
|
(WS_Version : Interfaces.C.int;
|
346 |
|
|
WSADataAddress : System.Address) return Interfaces.C.int;
|
347 |
|
|
|
348 |
|
|
procedure Free_Socket_Set
|
349 |
|
|
(Set : Fd_Set_Access);
|
350 |
|
|
-- Free system-dependent socket set
|
351 |
|
|
|
352 |
|
|
procedure Get_Socket_From_Set
|
353 |
|
|
(Set : Fd_Set_Access;
|
354 |
|
|
Socket : Int_Access;
|
355 |
|
|
Last : Int_Access);
|
356 |
|
|
-- Get last socket in Socket and remove it from the socket
|
357 |
|
|
-- set. The parameter Last is a maximum value of the largest
|
358 |
|
|
-- socket. This hint is used to avoid scanning very large socket
|
359 |
|
|
-- sets. After a call to Get_Socket_From_Set, Last is set back to
|
360 |
|
|
-- the real largest socket in the socket set.
|
361 |
|
|
|
362 |
|
|
procedure Insert_Socket_In_Set
|
363 |
|
|
(Set : Fd_Set_Access;
|
364 |
|
|
Socket : C.int);
|
365 |
|
|
-- Insert socket in the socket set
|
366 |
|
|
|
367 |
|
|
function Is_Socket_In_Set
|
368 |
|
|
(Set : Fd_Set_Access;
|
369 |
|
|
Socket : C.int) return C.int;
|
370 |
|
|
-- Check whether Socket is in the socket set, return a non-zero
|
371 |
|
|
-- value if it is, zero if it is not.
|
372 |
|
|
|
373 |
|
|
procedure Last_Socket_In_Set
|
374 |
|
|
(Set : Fd_Set_Access;
|
375 |
|
|
Last : Int_Access);
|
376 |
|
|
-- Find the largest socket in the socket set. This is needed for
|
377 |
|
|
-- select(). When Last_Socket_In_Set is called, parameter Last is
|
378 |
|
|
-- a maximum value of the largest socket. This hint is used to
|
379 |
|
|
-- avoid scanning very large socket sets. After the call, Last is
|
380 |
|
|
-- set back to the real largest socket in the socket set.
|
381 |
|
|
|
382 |
|
|
function New_Socket_Set
|
383 |
|
|
(Set : Fd_Set_Access) return Fd_Set_Access;
|
384 |
|
|
-- Allocate a new socket set which is a system-dependent structure
|
385 |
|
|
-- and initialize by copying Set if it is non-null, by making it
|
386 |
|
|
-- empty otherwise.
|
387 |
|
|
|
388 |
|
|
procedure Remove_Socket_From_Set
|
389 |
|
|
(Set : Fd_Set_Access;
|
390 |
|
|
Socket : C.int);
|
391 |
|
|
-- Remove socket from the socket set
|
392 |
|
|
|
393 |
|
|
procedure WSACleanup;
|
394 |
|
|
|
395 |
|
|
procedure Finalize;
|
396 |
|
|
procedure Initialize (Process_Blocking_IO : Boolean := False);
|
397 |
|
|
|
398 |
|
|
private
|
399 |
|
|
pragma Import (Stdcall, C_Accept, "accept");
|
400 |
|
|
pragma Import (Stdcall, C_Bind, "bind");
|
401 |
|
|
pragma Import (Stdcall, C_Close, "closesocket");
|
402 |
|
|
pragma Import (Stdcall, C_Gethostbyaddr, "gethostbyaddr");
|
403 |
|
|
pragma Import (Stdcall, C_Gethostbyname, "gethostbyname");
|
404 |
|
|
pragma Import (Stdcall, C_Gethostname, "gethostname");
|
405 |
|
|
pragma Import (Stdcall, C_Getpeername, "getpeername");
|
406 |
|
|
pragma Import (Stdcall, C_Getservbyname, "getservbyname");
|
407 |
|
|
pragma Import (Stdcall, C_Getservbyport, "getservbyport");
|
408 |
|
|
pragma Import (Stdcall, C_Getsockname, "getsockname");
|
409 |
|
|
pragma Import (Stdcall, C_Getsockopt, "getsockopt");
|
410 |
|
|
pragma Import (Stdcall, C_Ioctl, "ioctlsocket");
|
411 |
|
|
pragma Import (Stdcall, C_Listen, "listen");
|
412 |
|
|
pragma Import (Stdcall, C_Recv, "recv");
|
413 |
|
|
pragma Import (Stdcall, C_Recvfrom, "recvfrom");
|
414 |
|
|
pragma Import (Stdcall, C_Send, "send");
|
415 |
|
|
pragma Import (Stdcall, C_Sendto, "sendto");
|
416 |
|
|
pragma Import (Stdcall, C_Setsockopt, "setsockopt");
|
417 |
|
|
pragma Import (Stdcall, C_Shutdown, "shutdown");
|
418 |
|
|
pragma Import (Stdcall, C_Socket, "socket");
|
419 |
|
|
pragma Import (C, C_Strerror, "strerror");
|
420 |
|
|
pragma Import (C, C_System, "_system");
|
421 |
|
|
pragma Import (Stdcall, Socket_Errno, "WSAGetLastError");
|
422 |
|
|
pragma Import (Stdcall, Set_Socket_Errno, "WSASetLastError");
|
423 |
|
|
pragma Import (Stdcall, WSAStartup, "WSAStartup");
|
424 |
|
|
pragma Import (Stdcall, WSACleanup, "WSACleanup");
|
425 |
|
|
|
426 |
|
|
pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
|
427 |
|
|
pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
|
428 |
|
|
pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
|
429 |
|
|
pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
|
430 |
|
|
pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
|
431 |
|
|
pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
|
432 |
|
|
pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
|
433 |
|
|
end GNAT.Sockets.Thin;
|