1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N . T A S K _ S A F E _ N E T D B --
9 -- Copyright (C) 2007-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 version is used on VMS and LynxOS
38 with Interfaces.C; use Interfaces.C;
40 package body GNAT.Sockets.Thin.Task_Safe_NetDB is
42 -- The Safe_GetXXXbyYYY routines wrap the Nonreentrant_ versions using the
43 -- task lock, and copy the relevant data structures (under the lock) into
44 -- the result. The Nonreentrant_ versions are expected to be in the parent
45 -- package GNAT.Sockets.Thin (on platforms that use this version of
48 procedure Copy_Host_Entry
49 (Source_Hostent : Hostent;
50 Target_Hostent : out Hostent;
51 Target_Buffer : System.Address;
52 Target_Buffer_Length : C.int;
54 -- Copy all the information from Source_Hostent into Target_Hostent,
55 -- using Target_Buffer to store associated data.
56 -- 0 is returned on success, -1 on failure (in case the provided buffer
57 -- is too small for the associated data).
59 procedure Copy_Service_Entry
60 (Source_Servent : Servent_Access;
61 Target_Servent : Servent_Access;
62 Target_Buffer : System.Address;
63 Target_Buffer_Length : C.int;
65 -- Copy all the information from Source_Servent into Target_Servent,
66 -- using Target_Buffer to store associated data.
67 -- 0 is returned on success, -1 on failure (in case the provided buffer
68 -- is too small for the associated data).
72 Storage : in out char_array;
73 Storage_Index : in out size_t;
74 Stored_Name : out C.Strings.chars_ptr);
75 -- Store the given Name at the first available location in Storage
76 -- (indicated by Storage_Index, which is updated afterwards), and return
77 -- the address of that location in Stored_Name.
78 -- (Supporting routine for the two below).
84 procedure Copy_Host_Entry
85 (Source_Hostent : Hostent;
86 Target_Hostent : out Hostent;
87 Target_Buffer : System.Address;
88 Target_Buffer_Length : C.int;
91 use type C.Strings.chars_ptr;
93 Names_Length : size_t;
95 Source_Aliases : Chars_Ptr_Array
96 renames Chars_Ptr_Pointers.Value
97 (Source_Hostent.H_Aliases, Terminator => C.Strings.Null_Ptr);
98 -- Null-terminated list of aliases (last element of this array is
101 Source_Addresses : In_Addr_Access_Array
102 renames In_Addr_Access_Pointers.Value
103 (Source_Hostent.H_Addr_List, Terminator => null);
107 Names_Length := C.Strings.Strlen (Source_Hostent.H_Name) + 1;
109 for J in Source_Aliases'Range loop
110 if Source_Aliases (J) /= C.Strings.Null_Ptr then
112 Names_Length + C.Strings.Strlen (Source_Aliases (J)) + 1;
117 type In_Addr_Array is array (Source_Addresses'Range)
120 type Netdb_Host_Data is record
121 Aliases_List : aliased Chars_Ptr_Array (Source_Aliases'Range);
122 Names : aliased char_array (1 .. Names_Length);
124 Addresses_List : aliased In_Addr_Access_Array
125 (In_Addr_Array'Range);
126 Addresses : In_Addr_Array;
127 -- ??? This assumes support only for Inet family
131 Netdb_Data : Netdb_Host_Data;
132 pragma Import (Ada, Netdb_Data);
133 for Netdb_Data'Address use Target_Buffer;
135 Names_Index : size_t := Netdb_Data.Names'First;
136 -- Index of first available location in Netdb_Data.Names
139 if Netdb_Data'Size / 8 > Target_Buffer_Length then
146 (C.Strings.Value (Source_Hostent.H_Name),
147 Netdb_Data.Names, Names_Index,
148 Target_Hostent.H_Name);
150 -- Copy aliases (null-terminated string pointer array)
152 Target_Hostent.H_Aliases :=
153 Netdb_Data.Aliases_List
154 (Netdb_Data.Aliases_List'First)'Unchecked_Access;
155 for J in Netdb_Data.Aliases_List'Range loop
156 if J = Netdb_Data.Aliases_List'Last then
157 Netdb_Data.Aliases_List (J) := C.Strings.Null_Ptr;
160 (C.Strings.Value (Source_Aliases (J)),
161 Netdb_Data.Names, Names_Index,
162 Netdb_Data.Aliases_List (J));
166 -- Copy address type and length
168 Target_Hostent.H_Addrtype := Source_Hostent.H_Addrtype;
169 Target_Hostent.H_Length := Source_Hostent.H_Length;
173 Target_Hostent.H_Addr_List :=
174 Netdb_Data.Addresses_List
175 (Netdb_Data.Addresses_List'First)'Unchecked_Access;
177 for J in Netdb_Data.Addresses'Range loop
178 if J = Netdb_Data.Addresses'Last then
179 Netdb_Data.Addresses_List (J) := null;
181 Netdb_Data.Addresses_List (J) :=
182 Netdb_Data.Addresses (J)'Unchecked_Access;
184 Netdb_Data.Addresses (J) := Source_Addresses (J).all;
192 ------------------------
193 -- Copy_Service_Entry --
194 ------------------------
196 procedure Copy_Service_Entry
197 (Source_Servent : Servent_Access;
198 Target_Servent : Servent_Access;
199 Target_Buffer : System.Address;
200 Target_Buffer_Length : C.int;
203 use type C.Strings.chars_ptr;
205 Names_Length : size_t;
207 Source_Aliases : Chars_Ptr_Array
208 renames Chars_Ptr_Pointers.Value
209 (Servent_S_Aliases (Source_Servent),
210 Terminator => C.Strings.Null_Ptr);
211 -- Null-terminated list of aliases (last element of this array is
216 Names_Length := C.Strings.Strlen (Servent_S_Name (Source_Servent)) + 1 +
217 C.Strings.Strlen (Servent_S_Proto (Source_Servent)) + 1;
219 for J in Source_Aliases'Range loop
220 if Source_Aliases (J) /= C.Strings.Null_Ptr then
222 Names_Length + C.Strings.Strlen (Source_Aliases (J)) + 1;
227 type Netdb_Service_Data is record
228 Aliases_List : aliased Chars_Ptr_Array (Source_Aliases'Range);
229 Names : aliased char_array (1 .. Names_Length);
232 Netdb_Data : Netdb_Service_Data;
233 pragma Import (Ada, Netdb_Data);
234 for Netdb_Data'Address use Target_Buffer;
236 Names_Index : size_t := Netdb_Data.Names'First;
237 -- Index of first available location in Netdb_Data.Names
239 Stored_Name : C.Strings.chars_ptr;
242 if Netdb_Data'Size / 8 > Target_Buffer_Length then
249 (C.Strings.Value (Servent_S_Name (Source_Servent)),
250 Netdb_Data.Names, Names_Index,
252 Servent_Set_S_Name (Target_Servent, Stored_Name);
254 -- Copy aliases (null-terminated string pointer array)
256 Servent_Set_S_Aliases
258 Netdb_Data.Aliases_List
259 (Netdb_Data.Aliases_List'First)'Unchecked_Access);
263 Servent_Set_S_Port (Target_Servent, Servent_S_Port (Source_Servent));
265 -- Copy protocol name
268 (C.Strings.Value (Servent_S_Proto (Source_Servent)),
269 Netdb_Data.Names, Names_Index,
271 Servent_Set_S_Proto (Target_Servent, Stored_Name);
273 for J in Netdb_Data.Aliases_List'Range loop
274 if J = Netdb_Data.Aliases_List'Last then
275 Netdb_Data.Aliases_List (J) := C.Strings.Null_Ptr;
278 (C.Strings.Value (Source_Aliases (J)),
279 Netdb_Data.Names, Names_Index,
280 Netdb_Data.Aliases_List (J));
286 end Copy_Service_Entry;
288 ------------------------
289 -- Safe_Gethostbyaddr --
290 ------------------------
292 function Safe_Gethostbyaddr
293 (Addr : System.Address;
296 Ret : not null access Hostent;
297 Buf : System.Address;
299 H_Errnop : not null access C.int) return C.int
306 HE := Nonreentrant_Gethostbyaddr (Addr, Addr_Len, Addr_Type);
309 H_Errnop.all := C.int (Host_Errno);
313 -- Now copy the data to the user-provided buffer
316 (Source_Hostent => HE.all,
317 Target_Hostent => Ret.all,
318 Target_Buffer => Buf,
319 Target_Buffer_Length => Buflen,
323 GNAT.Task_Lock.Unlock;
325 end Safe_Gethostbyaddr;
327 ------------------------
328 -- Safe_Gethostbyname --
329 ------------------------
331 function Safe_Gethostbyname
332 (Name : C.char_array;
333 Ret : not null access Hostent;
334 Buf : System.Address;
336 H_Errnop : not null access C.int) return C.int
343 HE := Nonreentrant_Gethostbyname (Name);
346 H_Errnop.all := C.int (Host_Errno);
350 -- Now copy the data to the user-provided buffer
353 (Source_Hostent => HE.all,
354 Target_Hostent => Ret.all,
355 Target_Buffer => Buf,
356 Target_Buffer_Length => Buflen,
360 GNAT.Task_Lock.Unlock;
362 end Safe_Gethostbyname;
364 ------------------------
365 -- Safe_Getservbyname --
366 ------------------------
368 function Safe_Getservbyname
369 (Name : C.char_array;
370 Proto : C.char_array;
371 Ret : not null access Servent;
372 Buf : System.Address;
373 Buflen : C.int) return C.int
380 SE := Nonreentrant_Getservbyname (Name, Proto);
386 -- Now copy the data to the user-provided buffer. We convert Ret to
387 -- type Servent_Access using the .all'Unchecked_Access trick to avoid
388 -- an accessibility check. Ret could be pointing to a nested variable,
389 -- and we don't want to raise an exception in that case.
392 (Source_Servent => SE,
393 Target_Servent => Ret.all'Unchecked_Access,
394 Target_Buffer => Buf,
395 Target_Buffer_Length => Buflen,
399 GNAT.Task_Lock.Unlock;
401 end Safe_Getservbyname;
403 ------------------------
404 -- Safe_Getservbyport --
405 ------------------------
407 function Safe_Getservbyport
409 Proto : C.char_array;
410 Ret : not null access Servent;
411 Buf : System.Address;
412 Buflen : C.int) return C.int
420 SE := Nonreentrant_Getservbyport (Port, Proto);
426 -- Now copy the data to the user-provided buffer. See Safe_Getservbyname
427 -- for comment regarding .all'Unchecked_Access.
430 (Source_Servent => SE,
431 Target_Servent => Ret.all'Unchecked_Access,
432 Target_Buffer => Buf,
433 Target_Buffer_Length => Buflen,
437 GNAT.Task_Lock.Unlock;
439 end Safe_Getservbyport;
447 Storage : in out char_array;
448 Storage_Index : in out size_t;
449 Stored_Name : out C.Strings.chars_ptr)
451 First : constant C.size_t := Storage_Index;
452 Last : constant C.size_t := Storage_Index + Name'Length - 1;
454 Storage (First .. Last) := Name;
455 Stored_Name := C.Strings.To_Chars_Ptr
456 (Storage (First .. Last)'Unrestricted_Access);
457 Storage_Index := Last + 1;
460 end GNAT.Sockets.Thin.Task_Safe_NetDB;