1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT COMPILER COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . S O C K E T S . T H I N _ C O M M O N --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 2008-2009, AdaCore --
|
10 |
|
|
-- --
|
11 |
|
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
12 |
|
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
13 |
|
|
-- ware Foundation; either version 2, or (at your option) any later ver- --
|
14 |
|
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
15 |
|
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
16 |
|
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
|
17 |
|
|
-- for more details. You should have received a copy of the GNU General --
|
18 |
|
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
19 |
|
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, USA. --
|
21 |
|
|
-- --
|
22 |
|
|
-- As a special exception, if other files instantiate generics from this --
|
23 |
|
|
-- unit, or you link this unit with other files to produce an executable, --
|
24 |
|
|
-- this unit does not by itself cause the resulting executable to be --
|
25 |
|
|
-- covered by the GNU General Public License. This exception does not --
|
26 |
|
|
-- however invalidate any other reasons why the executable file might be --
|
27 |
|
|
-- covered by the GNU Public License. --
|
28 |
|
|
-- --
|
29 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- This is the target-independent part of the thin sockets mapping.
|
35 |
|
|
-- This package should not be directly with'ed by an applications program.
|
36 |
|
|
|
37 |
|
|
with Ada.Unchecked_Conversion;
|
38 |
|
|
|
39 |
|
|
with Interfaces.C;
|
40 |
|
|
with Interfaces.C.Pointers;
|
41 |
|
|
with Interfaces.C.Strings;
|
42 |
|
|
|
43 |
|
|
package GNAT.Sockets.Thin_Common is
|
44 |
|
|
|
45 |
|
|
package C renames Interfaces.C;
|
46 |
|
|
|
47 |
|
|
use type C.int;
|
48 |
|
|
-- This is so we can declare the Failure constant below
|
49 |
|
|
|
50 |
|
|
Success : constant C.int := 0;
|
51 |
|
|
Failure : constant C.int := -1;
|
52 |
|
|
|
53 |
|
|
type time_t is
|
54 |
|
|
range -2 ** (8 * SOSC.SIZEOF_tv_sec - 1)
|
55 |
|
|
.. 2 ** (8 * SOSC.SIZEOF_tv_sec - 1) - 1;
|
56 |
|
|
for time_t'Size use 8 * SOSC.SIZEOF_tv_sec;
|
57 |
|
|
pragma Convention (C, time_t);
|
58 |
|
|
|
59 |
|
|
type suseconds_t is
|
60 |
|
|
range -2 ** (8 * SOSC.SIZEOF_tv_usec - 1)
|
61 |
|
|
.. 2 ** (8 * SOSC.SIZEOF_tv_usec - 1) - 1;
|
62 |
|
|
for suseconds_t'Size use 8 * SOSC.SIZEOF_tv_usec;
|
63 |
|
|
pragma Convention (C, suseconds_t);
|
64 |
|
|
|
65 |
|
|
type Timeval is record
|
66 |
|
|
Tv_Sec : time_t;
|
67 |
|
|
Tv_Usec : suseconds_t;
|
68 |
|
|
end record;
|
69 |
|
|
pragma Convention (C, Timeval);
|
70 |
|
|
|
71 |
|
|
type Timeval_Access is access all Timeval;
|
72 |
|
|
pragma Convention (C, Timeval_Access);
|
73 |
|
|
|
74 |
|
|
Immediat : constant Timeval := (0, 0);
|
75 |
|
|
|
76 |
|
|
-------------------------------------------
|
77 |
|
|
-- Mapping tables to low level constants --
|
78 |
|
|
-------------------------------------------
|
79 |
|
|
|
80 |
|
|
Families : constant array (Family_Type) of C.int :=
|
81 |
|
|
(Family_Inet => SOSC.AF_INET,
|
82 |
|
|
Family_Inet6 => SOSC.AF_INET6);
|
83 |
|
|
|
84 |
|
|
Lengths : constant array (Family_Type) of C.unsigned_char :=
|
85 |
|
|
(Family_Inet => SOSC.SIZEOF_sockaddr_in,
|
86 |
|
|
Family_Inet6 => SOSC.SIZEOF_sockaddr_in6);
|
87 |
|
|
|
88 |
|
|
----------------------------
|
89 |
|
|
-- Generic socket address --
|
90 |
|
|
----------------------------
|
91 |
|
|
|
92 |
|
|
-- Common header
|
93 |
|
|
|
94 |
|
|
-- All socket address types (struct sockaddr, struct sockaddr_storage,
|
95 |
|
|
-- and protocol specific address types) start with the same 2-byte header,
|
96 |
|
|
-- which is either a length and a family (one byte each) or just a two-byte
|
97 |
|
|
-- family. The following unchecked union describes the two possible layouts
|
98 |
|
|
-- and is meant to be constrained with SOSC.Have_Sockaddr_Len.
|
99 |
|
|
|
100 |
|
|
type Sockaddr_Length_And_Family
|
101 |
|
|
(Has_Sockaddr_Len : Boolean := False)
|
102 |
|
|
is record
|
103 |
|
|
case Has_Sockaddr_Len is
|
104 |
|
|
when True =>
|
105 |
|
|
Length : C.unsigned_char;
|
106 |
|
|
Char_Family : C.unsigned_char;
|
107 |
|
|
|
108 |
|
|
when False =>
|
109 |
|
|
Short_Family : C.unsigned_short;
|
110 |
|
|
end case;
|
111 |
|
|
end record;
|
112 |
|
|
pragma Unchecked_Union (Sockaddr_Length_And_Family);
|
113 |
|
|
pragma Convention (C, Sockaddr_Length_And_Family);
|
114 |
|
|
|
115 |
|
|
procedure Set_Family
|
116 |
|
|
(Length_And_Family : out Sockaddr_Length_And_Family;
|
117 |
|
|
Family : Family_Type);
|
118 |
|
|
-- Set the family component to the appropriate value for Family, and also
|
119 |
|
|
-- set Length accordingly if applicable on this platform.
|
120 |
|
|
|
121 |
|
|
type Sockaddr is record
|
122 |
|
|
Sa_Family : Sockaddr_Length_And_Family;
|
123 |
|
|
-- Address family (and address length on some platforms)
|
124 |
|
|
|
125 |
|
|
Sa_Data : C.char_array (1 .. 14) := (others => C.nul);
|
126 |
|
|
-- Family-specific data
|
127 |
|
|
-- Note that some platforms require that all unused (reserved) bytes
|
128 |
|
|
-- in addresses be initialized to 0 (e.g. VxWorks).
|
129 |
|
|
end record;
|
130 |
|
|
pragma Convention (C, Sockaddr);
|
131 |
|
|
-- Generic socket address
|
132 |
|
|
|
133 |
|
|
type Sockaddr_Access is access all Sockaddr;
|
134 |
|
|
pragma Convention (C, Sockaddr_Access);
|
135 |
|
|
-- Access to socket address
|
136 |
|
|
|
137 |
|
|
----------------------------
|
138 |
|
|
-- AF_INET socket address --
|
139 |
|
|
----------------------------
|
140 |
|
|
|
141 |
|
|
type In_Addr is record
|
142 |
|
|
S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
|
143 |
|
|
end record;
|
144 |
|
|
for In_Addr'Alignment use C.int'Alignment;
|
145 |
|
|
pragma Convention (C, In_Addr);
|
146 |
|
|
-- IPv4 address, represented as a network-order C.int. Note that the
|
147 |
|
|
-- underlying operating system may assume that values of this type have
|
148 |
|
|
-- C.int alignment, so we need to provide a suitable alignment clause here.
|
149 |
|
|
|
150 |
|
|
function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
|
151 |
|
|
function To_Int is new Ada.Unchecked_Conversion (In_Addr, C.int);
|
152 |
|
|
|
153 |
|
|
type In_Addr_Access is access all In_Addr;
|
154 |
|
|
pragma Convention (C, In_Addr_Access);
|
155 |
|
|
-- Access to internet address
|
156 |
|
|
|
157 |
|
|
Inaddr_Any : aliased constant In_Addr := (others => 0);
|
158 |
|
|
-- Any internet address (all the interfaces)
|
159 |
|
|
|
160 |
|
|
type In_Addr_Access_Array is array (C.size_t range <>)
|
161 |
|
|
of aliased In_Addr_Access;
|
162 |
|
|
pragma Convention (C, In_Addr_Access_Array);
|
163 |
|
|
|
164 |
|
|
package In_Addr_Access_Pointers is new C.Pointers
|
165 |
|
|
(C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
|
166 |
|
|
-- Array of internet addresses
|
167 |
|
|
|
168 |
|
|
type Sockaddr_In is record
|
169 |
|
|
Sin_Family : Sockaddr_Length_And_Family;
|
170 |
|
|
-- Address family (and address length on some platforms)
|
171 |
|
|
|
172 |
|
|
Sin_Port : C.unsigned_short;
|
173 |
|
|
-- Port in network byte order
|
174 |
|
|
|
175 |
|
|
Sin_Addr : In_Addr;
|
176 |
|
|
-- IPv4 address
|
177 |
|
|
|
178 |
|
|
Sin_Zero : C.char_array (1 .. 8) := (others => C.nul);
|
179 |
|
|
-- Padding
|
180 |
|
|
--
|
181 |
|
|
-- Note that some platforms require that all unused (reserved) bytes
|
182 |
|
|
-- in addresses be initialized to 0 (e.g. VxWorks).
|
183 |
|
|
end record;
|
184 |
|
|
pragma Convention (C, Sockaddr_In);
|
185 |
|
|
-- Internet socket address
|
186 |
|
|
|
187 |
|
|
type Sockaddr_In_Access is access all Sockaddr_In;
|
188 |
|
|
pragma Convention (C, Sockaddr_In_Access);
|
189 |
|
|
-- Access to internet socket address
|
190 |
|
|
|
191 |
|
|
procedure Set_Port
|
192 |
|
|
(Sin : Sockaddr_In_Access;
|
193 |
|
|
Port : C.unsigned_short);
|
194 |
|
|
pragma Inline (Set_Port);
|
195 |
|
|
-- Set Sin.Sin_Port to Port
|
196 |
|
|
|
197 |
|
|
procedure Set_Address
|
198 |
|
|
(Sin : Sockaddr_In_Access;
|
199 |
|
|
Address : In_Addr);
|
200 |
|
|
pragma Inline (Set_Address);
|
201 |
|
|
-- Set Sin.Sin_Addr to Address
|
202 |
|
|
|
203 |
|
|
---------------------
|
204 |
|
|
-- Service entries --
|
205 |
|
|
---------------------
|
206 |
|
|
|
207 |
|
|
type Chars_Ptr_Array is array (C.size_t range <>) of
|
208 |
|
|
aliased C.Strings.chars_ptr;
|
209 |
|
|
|
210 |
|
|
package Chars_Ptr_Pointers is
|
211 |
|
|
new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
|
212 |
|
|
C.Strings.Null_Ptr);
|
213 |
|
|
-- Arrays of C (char *)
|
214 |
|
|
|
215 |
|
|
type Servent is new
|
216 |
|
|
System.Storage_Elements.Storage_Array (1 .. SOSC.SIZEOF_struct_servent);
|
217 |
|
|
for Servent'Alignment use 8;
|
218 |
|
|
-- Service entry. This is an opaque type used only via the following
|
219 |
|
|
-- accessor functions, because 'struct servent' has different layouts on
|
220 |
|
|
-- different platforms.
|
221 |
|
|
|
222 |
|
|
type Servent_Access is access all Servent;
|
223 |
|
|
pragma Convention (C, Servent_Access);
|
224 |
|
|
-- Access to service entry
|
225 |
|
|
|
226 |
|
|
function Servent_S_Name
|
227 |
|
|
(E : Servent_Access) return C.Strings.chars_ptr;
|
228 |
|
|
|
229 |
|
|
function Servent_S_Aliases
|
230 |
|
|
(E : Servent_Access) return Chars_Ptr_Pointers.Pointer;
|
231 |
|
|
|
232 |
|
|
function Servent_S_Port
|
233 |
|
|
(E : Servent_Access) return C.int;
|
234 |
|
|
|
235 |
|
|
function Servent_S_Proto
|
236 |
|
|
(E : Servent_Access) return C.Strings.chars_ptr;
|
237 |
|
|
|
238 |
|
|
procedure Servent_Set_S_Name
|
239 |
|
|
(E : Servent_Access;
|
240 |
|
|
S_Name : C.Strings.chars_ptr);
|
241 |
|
|
|
242 |
|
|
procedure Servent_Set_S_Aliases
|
243 |
|
|
(E : Servent_Access;
|
244 |
|
|
S_Aliases : Chars_Ptr_Pointers.Pointer);
|
245 |
|
|
|
246 |
|
|
procedure Servent_Set_S_Port
|
247 |
|
|
(E : Servent_Access;
|
248 |
|
|
S_Port : C.int);
|
249 |
|
|
|
250 |
|
|
procedure Servent_Set_S_Proto
|
251 |
|
|
(E : Servent_Access;
|
252 |
|
|
S_Proto : C.Strings.chars_ptr);
|
253 |
|
|
|
254 |
|
|
------------------
|
255 |
|
|
-- Host entries --
|
256 |
|
|
------------------
|
257 |
|
|
|
258 |
|
|
type Hostent is record
|
259 |
|
|
H_Name : C.Strings.chars_ptr;
|
260 |
|
|
H_Aliases : Chars_Ptr_Pointers.Pointer;
|
261 |
|
|
H_Addrtype : SOSC.H_Addrtype_T;
|
262 |
|
|
H_Length : SOSC.H_Length_T;
|
263 |
|
|
H_Addr_List : In_Addr_Access_Pointers.Pointer;
|
264 |
|
|
end record;
|
265 |
|
|
pragma Convention (C, Hostent);
|
266 |
|
|
-- Host entry
|
267 |
|
|
|
268 |
|
|
type Hostent_Access is access all Hostent;
|
269 |
|
|
pragma Convention (C, Hostent_Access);
|
270 |
|
|
-- Access to host entry
|
271 |
|
|
|
272 |
|
|
------------------------------------
|
273 |
|
|
-- Scatter/gather vector handling --
|
274 |
|
|
------------------------------------
|
275 |
|
|
|
276 |
|
|
type Msghdr is record
|
277 |
|
|
Msg_Name : System.Address;
|
278 |
|
|
Msg_Namelen : C.unsigned;
|
279 |
|
|
Msg_Iov : System.Address;
|
280 |
|
|
Msg_Iovlen : SOSC.Msg_Iovlen_T;
|
281 |
|
|
Msg_Control : System.Address;
|
282 |
|
|
Msg_Controllen : C.size_t;
|
283 |
|
|
Msg_Flags : C.int;
|
284 |
|
|
end record;
|
285 |
|
|
pragma Convention (C, Msghdr);
|
286 |
|
|
|
287 |
|
|
----------------------------
|
288 |
|
|
-- Socket sets management --
|
289 |
|
|
----------------------------
|
290 |
|
|
|
291 |
|
|
procedure Get_Socket_From_Set
|
292 |
|
|
(Set : access Fd_Set;
|
293 |
|
|
Last : access C.int;
|
294 |
|
|
Socket : access C.int);
|
295 |
|
|
-- Get last socket in Socket and remove it from the socket set. The
|
296 |
|
|
-- parameter Last is a maximum value of the largest socket. This hint is
|
297 |
|
|
-- used to avoid scanning very large socket sets. After a call to
|
298 |
|
|
-- Get_Socket_From_Set, Last is set back to the real largest socket in the
|
299 |
|
|
-- socket set.
|
300 |
|
|
|
301 |
|
|
procedure Insert_Socket_In_Set
|
302 |
|
|
(Set : access Fd_Set;
|
303 |
|
|
Socket : C.int);
|
304 |
|
|
-- Insert socket in the socket set
|
305 |
|
|
|
306 |
|
|
function Is_Socket_In_Set
|
307 |
|
|
(Set : access constant Fd_Set;
|
308 |
|
|
Socket : C.int) return C.int;
|
309 |
|
|
-- Check whether Socket is in the socket set, return a non-zero
|
310 |
|
|
-- value if it is, zero if it is not.
|
311 |
|
|
|
312 |
|
|
procedure Last_Socket_In_Set
|
313 |
|
|
(Set : access Fd_Set;
|
314 |
|
|
Last : access C.int);
|
315 |
|
|
-- Find the largest socket in the socket set. This is needed for select().
|
316 |
|
|
-- When Last_Socket_In_Set is called, parameter Last is a maximum value of
|
317 |
|
|
-- the largest socket. This hint is used to avoid scanning very large
|
318 |
|
|
-- socket sets. After the call, Last is set back to the real largest socket
|
319 |
|
|
-- in the socket set.
|
320 |
|
|
|
321 |
|
|
procedure Remove_Socket_From_Set (Set : access Fd_Set; Socket : C.int);
|
322 |
|
|
-- Remove socket from the socket set
|
323 |
|
|
|
324 |
|
|
procedure Reset_Socket_Set (Set : access Fd_Set);
|
325 |
|
|
-- Make Set empty
|
326 |
|
|
|
327 |
|
|
------------------------------------------
|
328 |
|
|
-- Pairs of signalling file descriptors --
|
329 |
|
|
------------------------------------------
|
330 |
|
|
|
331 |
|
|
type Two_Ints is array (0 .. 1) of C.int;
|
332 |
|
|
pragma Convention (C, Two_Ints);
|
333 |
|
|
-- Container for two int values
|
334 |
|
|
|
335 |
|
|
subtype Fd_Pair is Two_Ints;
|
336 |
|
|
-- Two_Ints as used for Create_Signalling_Fds: a pair of connected file
|
337 |
|
|
-- descriptors, one of which (the "read end" of the connection) being used
|
338 |
|
|
-- for reading, the other one (the "write end") being used for writing.
|
339 |
|
|
|
340 |
|
|
Read_End : constant := 0;
|
341 |
|
|
Write_End : constant := 1;
|
342 |
|
|
-- Indices into an Fd_Pair value providing access to each of the connected
|
343 |
|
|
-- file descriptors.
|
344 |
|
|
|
345 |
|
|
function Inet_Pton
|
346 |
|
|
(Af : C.int;
|
347 |
|
|
Cp : C.Strings.chars_ptr;
|
348 |
|
|
Inp : System.Address) return C.int;
|
349 |
|
|
|
350 |
|
|
function C_Ioctl
|
351 |
|
|
(Fd : C.int;
|
352 |
|
|
Req : C.int;
|
353 |
|
|
Arg : access C.int) return C.int;
|
354 |
|
|
|
355 |
|
|
private
|
356 |
|
|
pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
|
357 |
|
|
pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
|
358 |
|
|
pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
|
359 |
|
|
pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
|
360 |
|
|
pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
|
361 |
|
|
pragma Import (C, Reset_Socket_Set, "__gnat_reset_socket_set");
|
362 |
|
|
pragma Import (C, C_Ioctl, "__gnat_socket_ioctl");
|
363 |
|
|
pragma Import (C, Inet_Pton, SOSC.Inet_Pton_Linkname);
|
364 |
|
|
|
365 |
|
|
pragma Import (C, Servent_S_Name, "__gnat_servent_s_name");
|
366 |
|
|
pragma Import (C, Servent_S_Aliases, "__gnat_servent_s_aliases");
|
367 |
|
|
pragma Import (C, Servent_S_Port, "__gnat_servent_s_port");
|
368 |
|
|
pragma Import (C, Servent_S_Proto, "__gnat_servent_s_proto");
|
369 |
|
|
pragma Import (C, Servent_Set_S_Name, "__gnat_servent_set_s_name");
|
370 |
|
|
pragma Import (C, Servent_Set_S_Aliases, "__gnat_servent_set_s_aliases");
|
371 |
|
|
pragma Import (C, Servent_Set_S_Port, "__gnat_servent_set_s_port");
|
372 |
|
|
pragma Import (C, Servent_Set_S_Proto, "__gnat_servent_set_s_proto");
|
373 |
|
|
end GNAT.Sockets.Thin_Common;
|