1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N _ C O M M O N --
9 -- Copyright (C) 2008-2009, AdaCore --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
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.
37 with Ada
.Unchecked_Conversion
;
40 with Interfaces
.C
.Pointers
;
41 with Interfaces
.C
.Strings
;
43 package GNAT
.Sockets
.Thin_Common
is
45 package C
renames Interfaces
.C
;
48 -- This is so we can declare the Failure constant below
50 Success
: constant C
.int
:= 0;
51 Failure
: constant C
.int
:= -1;
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
);
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
);
65 type Timeval
is record
67 Tv_Usec
: suseconds_t
;
69 pragma Convention
(C
, Timeval
);
71 type Timeval_Access
is access all Timeval
;
72 pragma Convention
(C
, Timeval_Access
);
74 Immediat
: constant Timeval
:= (0, 0);
76 -------------------------------------------
77 -- Mapping tables to low level constants --
78 -------------------------------------------
80 Families
: constant array (Family_Type
) of C
.int
:=
81 (Family_Inet
=> SOSC
.AF_INET
,
82 Family_Inet6
=> SOSC
.AF_INET6
);
84 Lengths
: constant array (Family_Type
) of C
.unsigned_char
:=
85 (Family_Inet
=> SOSC
.SIZEOF_sockaddr_in
,
86 Family_Inet6
=> SOSC
.SIZEOF_sockaddr_in6
);
88 ----------------------------
89 -- Generic socket address --
90 ----------------------------
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.
100 type Sockaddr_Length_And_Family
101 (Has_Sockaddr_Len
: Boolean := False)
103 case Has_Sockaddr_Len
is
105 Length
: C
.unsigned_char
;
106 Char_Family
: C
.unsigned_char
;
109 Short_Family
: C
.unsigned_short
;
112 pragma Unchecked_Union
(Sockaddr_Length_And_Family
);
113 pragma Convention
(C
, Sockaddr_Length_And_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.
121 type Sockaddr
is record
122 Sa_Family
: Sockaddr_Length_And_Family
;
123 -- Address family (and address length on some platforms)
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).
130 pragma Convention
(C
, Sockaddr
);
131 -- Generic socket address
133 type Sockaddr_Access
is access all Sockaddr
;
134 pragma Convention
(C
, Sockaddr_Access
);
135 -- Access to socket address
137 ----------------------------
138 -- AF_INET socket address --
139 ----------------------------
141 type In_Addr
is record
142 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
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.
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
);
153 type In_Addr_Access
is access all In_Addr
;
154 pragma Convention
(C
, In_Addr_Access
);
155 -- Access to internet address
157 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
158 -- Any internet address (all the interfaces)
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
);
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
168 type Sockaddr_In
is record
169 Sin_Family
: Sockaddr_Length_And_Family
;
170 -- Address family (and address length on some platforms)
172 Sin_Port
: C
.unsigned_short
;
173 -- Port in network byte order
178 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.nul
);
181 -- Note that some platforms require that all unused (reserved) bytes
182 -- in addresses be initialized to 0 (e.g. VxWorks).
184 pragma Convention
(C
, Sockaddr_In
);
185 -- Internet socket address
187 type Sockaddr_In_Access
is access all Sockaddr_In
;
188 pragma Convention
(C
, Sockaddr_In_Access
);
189 -- Access to internet socket address
192 (Sin
: Sockaddr_In_Access
;
193 Port
: C
.unsigned_short
);
194 pragma Inline
(Set_Port
);
195 -- Set Sin.Sin_Port to Port
197 procedure Set_Address
198 (Sin
: Sockaddr_In_Access
;
200 pragma Inline
(Set_Address
);
201 -- Set Sin.Sin_Addr to Address
203 ---------------------
204 -- Service entries --
205 ---------------------
207 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
208 aliased C
.Strings
.chars_ptr
;
210 package Chars_Ptr_Pointers
is
211 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
213 -- Arrays of C (char *)
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.
222 type Servent_Access
is access all Servent
;
223 pragma Convention
(C
, Servent_Access
);
224 -- Access to service entry
226 function Servent_S_Name
227 (E
: Servent_Access
) return C
.Strings
.chars_ptr
;
229 function Servent_S_Aliases
230 (E
: Servent_Access
) return Chars_Ptr_Pointers
.Pointer
;
232 function Servent_S_Port
233 (E
: Servent_Access
) return C
.int
;
235 function Servent_S_Proto
236 (E
: Servent_Access
) return C
.Strings
.chars_ptr
;
238 procedure Servent_Set_S_Name
240 S_Name
: C
.Strings
.chars_ptr
);
242 procedure Servent_Set_S_Aliases
244 S_Aliases
: Chars_Ptr_Pointers
.Pointer
);
246 procedure Servent_Set_S_Port
250 procedure Servent_Set_S_Proto
252 S_Proto
: C
.Strings
.chars_ptr
);
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
;
265 pragma Convention
(C
, Hostent
);
268 type Hostent_Access
is access all Hostent
;
269 pragma Convention
(C
, Hostent_Access
);
270 -- Access to host entry
272 ------------------------------------
273 -- Scatter/gather vector handling --
274 ------------------------------------
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
;
285 pragma Convention
(C
, Msghdr
);
287 ----------------------------
288 -- Socket sets management --
289 ----------------------------
291 procedure Get_Socket_From_Set
292 (Set
: access Fd_Set
;
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
301 procedure Insert_Socket_In_Set
302 (Set
: access Fd_Set
;
304 -- Insert socket in the socket set
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.
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.
321 procedure Remove_Socket_From_Set
(Set
: access Fd_Set
; Socket
: C
.int
);
322 -- Remove socket from the socket set
324 procedure Reset_Socket_Set
(Set
: access Fd_Set
);
327 ------------------------------------------
328 -- Pairs of signalling file descriptors --
329 ------------------------------------------
331 type Two_Ints
is array (0 .. 1) of C
.int
;
332 pragma Convention
(C
, Two_Ints
);
333 -- Container for two int values
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.
340 Read_End
: constant := 0;
341 Write_End
: constant := 1;
342 -- Indices into an Fd_Pair value providing access to each of the connected
347 Cp
: C
.Strings
.chars_ptr
;
348 Inp
: System
.Address
) return C
.int
;
353 Arg
: access C
.int
) return C
.int
;
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
);
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
;