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, 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 *)
215 type Servent is record
216 S_Name : C.Strings.chars_ptr;
217 S_Aliases : Chars_Ptr_Pointers.Pointer;
219 S_Proto : C.Strings.chars_ptr;
221 pragma Convention (C, Servent);
224 type Servent_Access is access all Servent;
225 pragma Convention (C, Servent_Access);
226 -- Access to service entry
232 type Hostent is record
233 H_Name : C.Strings.chars_ptr;
234 H_Aliases : Chars_Ptr_Pointers.Pointer;
235 H_Addrtype : SOSC.H_Addrtype_T;
236 H_Length : SOSC.H_Length_T;
237 H_Addr_List : In_Addr_Access_Pointers.Pointer;
239 pragma Convention (C, Hostent);
242 type Hostent_Access is access all Hostent;
243 pragma Convention (C, Hostent_Access);
244 -- Access to host entry
246 ----------------------------
247 -- Socket sets management --
248 ----------------------------
250 type Int_Access is access all C.int;
251 pragma Convention (C, Int_Access);
252 -- Access to C integers
254 procedure Get_Socket_From_Set
255 (Set : access Fd_Set;
258 -- Get last socket in Socket and remove it from the socket set. The
259 -- parameter Last is a maximum value of the largest socket. This hint is
260 -- used to avoid scanning very large socket sets. After a call to
261 -- Get_Socket_From_Set, Last is set back to the real largest socket in the
264 procedure Insert_Socket_In_Set
265 (Set : access Fd_Set;
267 -- Insert socket in the socket set
269 function Is_Socket_In_Set
270 (Set : access constant Fd_Set;
271 Socket : C.int) return C.int;
272 -- Check whether Socket is in the socket set, return a non-zero
273 -- value if it is, zero if it is not.
275 procedure Last_Socket_In_Set
276 (Set : access Fd_Set;
278 -- Find the largest socket in the socket set. This is needed for select().
279 -- When Last_Socket_In_Set is called, parameter Last is a maximum value of
280 -- the largest socket. This hint is used to avoid scanning very large
281 -- socket sets. After the call, Last is set back to the real largest socket
282 -- in the socket set.
284 procedure Remove_Socket_From_Set (Set : access Fd_Set; Socket : C.int);
285 -- Remove socket from the socket set
287 procedure Reset_Socket_Set (Set : access Fd_Set);
290 ------------------------------------------
291 -- Pairs of signalling file descriptors --
292 ------------------------------------------
294 type Two_Ints is array (0 .. 1) of C.int;
295 pragma Convention (C, Two_Ints);
296 -- Container for two int values
298 subtype Fd_Pair is Two_Ints;
299 -- Two_Ints as used for Create_Signalling_Fds: a pair of connected file
300 -- descriptors, one of which (the "read end" of the connection) being used
301 -- for reading, the other one (the "write end") being used for writing.
303 Read_End : constant := 0;
304 Write_End : constant := 1;
305 -- Indices into an Fd_Pair value providing access to each of the connected
309 pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
310 pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
311 pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
312 pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
313 pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
314 pragma Import (C, Reset_Socket_Set, "__gnat_reset_socket_set");
315 end GNAT.Sockets.Thin_Common;