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, 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 ------------------------------------------------------------------------------
36 with Interfaces.C; use Interfaces.C;
38 package body GNAT.Sockets.Thin.Task_Safe_NetDB is
40 procedure Copy_Host_Entry
41 (Source_Hostent : Hostent;
42 Target_Hostent : out Hostent;
43 Target_Buffer : System.Address;
44 Target_Buffer_Length : C.int;
46 -- Copy all the information from Source_Hostent into Target_Hostent,
47 -- using Target_Buffer to store associated data.
48 -- 0 is returned on success, -1 on failure (in case the provided buffer
49 -- is too small for the associated data).
51 procedure Copy_Service_Entry
52 (Source_Servent : Servent;
53 Target_Servent : out Servent;
54 Target_Buffer : System.Address;
55 Target_Buffer_Length : C.int;
57 -- Copy all the information from Source_Servent into Target_Servent,
58 -- using Target_Buffer to store associated data.
59 -- 0 is returned on success, -1 on failure (in case the provided buffer
60 -- is too small for the associated data).
64 Storage : in out char_array;
65 Storage_Index : in out size_t;
66 Stored_Name : out C.Strings.chars_ptr);
67 -- Store the given Name at the first available location in Storage
68 -- (indicated by Storage_Index, which is updated afterwards), and return
69 -- the address of that location in Stored_Name.
70 -- (Supporting routine for the two below).
76 procedure Copy_Host_Entry
77 (Source_Hostent : Hostent;
78 Target_Hostent : out Hostent;
79 Target_Buffer : System.Address;
80 Target_Buffer_Length : C.int;
83 use type C.Strings.chars_ptr;
85 Names_Length : size_t;
87 Source_Aliases : Chars_Ptr_Array
88 renames Chars_Ptr_Pointers.Value
89 (Source_Hostent.H_Aliases, Terminator => C.Strings.Null_Ptr);
90 -- Null-terminated list of aliases (last element of this array is
93 Source_Addresses : In_Addr_Access_Array
94 renames In_Addr_Access_Pointers.Value
95 (Source_Hostent.H_Addr_List, Terminator => null);
99 Names_Length := C.Strings.Strlen (Source_Hostent.H_Name) + 1;
101 for J in Source_Aliases'Range loop
102 if Source_Aliases (J) /= C.Strings.Null_Ptr then
104 Names_Length + C.Strings.Strlen (Source_Aliases (J)) + 1;
109 type In_Addr_Array is array (Source_Addresses'Range)
112 type Netdb_Host_Data is record
113 Aliases_List : aliased Chars_Ptr_Array (Source_Aliases'Range);
114 Names : aliased char_array (1 .. Names_Length);
116 Addresses_List : aliased In_Addr_Access_Array
117 (In_Addr_Array'Range);
118 Addresses : In_Addr_Array;
119 -- ??? This assumes support only for Inet family
123 Netdb_Data : Netdb_Host_Data;
124 pragma Import (Ada, Netdb_Data);
125 for Netdb_Data'Address use Target_Buffer;
127 Names_Index : size_t := Netdb_Data.Names'First;
128 -- Index of first available location in Netdb_Data.Names
131 if Netdb_Data'Size / 8 > Target_Buffer_Length then
138 (C.Strings.Value (Source_Hostent.H_Name),
139 Netdb_Data.Names, Names_Index,
140 Target_Hostent.H_Name);
142 -- Copy aliases (null-terminated string pointer array)
144 Target_Hostent.H_Aliases :=
145 Netdb_Data.Aliases_List
146 (Netdb_Data.Aliases_List'First)'Unchecked_Access;
147 for J in Netdb_Data.Aliases_List'Range loop
148 if J = Netdb_Data.Aliases_List'Last then
149 Netdb_Data.Aliases_List (J) := C.Strings.Null_Ptr;
152 (C.Strings.Value (Source_Aliases (J)),
153 Netdb_Data.Names, Names_Index,
154 Netdb_Data.Aliases_List (J));
158 -- Copy address type and length
160 Target_Hostent.H_Addrtype := Source_Hostent.H_Addrtype;
161 Target_Hostent.H_Length := Source_Hostent.H_Length;
165 Target_Hostent.H_Addr_List :=
166 Netdb_Data.Addresses_List
167 (Netdb_Data.Addresses_List'First)'Unchecked_Access;
169 for J in Netdb_Data.Addresses'Range loop
170 if J = Netdb_Data.Addresses'Last then
171 Netdb_Data.Addresses_List (J) := null;
173 Netdb_Data.Addresses_List (J) :=
174 Netdb_Data.Addresses (J)'Unchecked_Access;
176 Netdb_Data.Addresses (J) := Source_Addresses (J).all;
184 ------------------------
185 -- Copy_Service_Entry --
186 ------------------------
188 procedure Copy_Service_Entry
189 (Source_Servent : Servent;
190 Target_Servent : out Servent;
191 Target_Buffer : System.Address;
192 Target_Buffer_Length : C.int;
195 use type C.Strings.chars_ptr;
197 Names_Length : size_t;
199 Source_Aliases : Chars_Ptr_Array
200 renames Chars_Ptr_Pointers.Value
201 (Source_Servent.S_Aliases, Terminator => C.Strings.Null_Ptr);
202 -- Null-terminated list of aliases (last element of this array is
207 Names_Length := C.Strings.Strlen (Source_Servent.S_Name) + 1
208 + C.Strings.Strlen (Source_Servent.S_Proto) + 1;
210 for J in Source_Aliases'Range loop
211 if Source_Aliases (J) /= C.Strings.Null_Ptr then
213 Names_Length + C.Strings.Strlen (Source_Aliases (J)) + 1;
218 type Netdb_Service_Data is record
219 Aliases_List : aliased Chars_Ptr_Array (Source_Aliases'Range);
220 Names : aliased char_array (1 .. Names_Length);
223 Netdb_Data : Netdb_Service_Data;
224 pragma Import (Ada, Netdb_Data);
225 for Netdb_Data'Address use Target_Buffer;
227 Names_Index : size_t := Netdb_Data.Names'First;
228 -- Index of first available location in Netdb_Data.Names
231 if Netdb_Data'Size / 8 > Target_Buffer_Length then
238 (C.Strings.Value (Source_Servent.S_Name),
239 Netdb_Data.Names, Names_Index,
240 Target_Servent.S_Name);
242 -- Copy aliases (null-terminated string pointer array)
244 Target_Servent.S_Aliases :=
245 Netdb_Data.Aliases_List
246 (Netdb_Data.Aliases_List'First)'Unchecked_Access;
250 Target_Servent.S_Port := Source_Servent.S_Port;
252 -- Copy protocol name
255 (C.Strings.Value (Source_Servent.S_Proto),
256 Netdb_Data.Names, Names_Index,
257 Target_Servent.S_Proto);
259 for J in Netdb_Data.Aliases_List'Range loop
260 if J = Netdb_Data.Aliases_List'Last then
261 Netdb_Data.Aliases_List (J) := C.Strings.Null_Ptr;
264 (C.Strings.Value (Source_Aliases (J)),
265 Netdb_Data.Names, Names_Index,
266 Netdb_Data.Aliases_List (J));
272 end Copy_Service_Entry;
274 ------------------------
275 -- Safe_Gethostbyaddr --
276 ------------------------
278 function Safe_Gethostbyaddr
279 (Addr : System.Address;
282 Ret : not null access Hostent;
283 Buf : System.Address;
285 H_Errnop : not null access C.int) return C.int
292 HE := Nonreentrant_Gethostbyaddr (Addr, Addr_Len, Addr_Type);
295 H_Errnop.all := C.int (Host_Errno);
299 -- Now copy the data to the user-provided buffer
302 (Source_Hostent => HE.all,
303 Target_Hostent => Ret.all,
304 Target_Buffer => Buf,
305 Target_Buffer_Length => Buflen,
309 GNAT.Task_Lock.Unlock;
311 end Safe_Gethostbyaddr;
313 ------------------------
314 -- Safe_Gethostbyname --
315 ------------------------
317 function Safe_Gethostbyname
318 (Name : C.char_array;
319 Ret : not null access Hostent;
320 Buf : System.Address;
322 H_Errnop : not null access C.int) return C.int
329 HE := Nonreentrant_Gethostbyname (Name);
332 H_Errnop.all := C.int (Host_Errno);
336 -- Now copy the data to the user-provided buffer
339 (Source_Hostent => HE.all,
340 Target_Hostent => Ret.all,
341 Target_Buffer => Buf,
342 Target_Buffer_Length => Buflen,
346 GNAT.Task_Lock.Unlock;
348 end Safe_Gethostbyname;
350 ------------------------
351 -- Safe_Getservbyname --
352 ------------------------
354 function Safe_Getservbyname
355 (Name : C.char_array;
356 Proto : C.char_array;
357 Ret : not null access Servent;
358 Buf : System.Address;
359 Buflen : C.int) return C.int
366 SE := Nonreentrant_Getservbyname (Name, Proto);
372 -- Now copy the data to the user-provided buffer
375 (Source_Servent => SE.all,
376 Target_Servent => Ret.all,
377 Target_Buffer => Buf,
378 Target_Buffer_Length => Buflen,
382 GNAT.Task_Lock.Unlock;
384 end Safe_Getservbyname;
386 ------------------------
387 -- Safe_Getservbyport --
388 ------------------------
390 function Safe_Getservbyport
392 Proto : C.char_array;
393 Ret : not null access Servent;
394 Buf : System.Address;
395 Buflen : C.int) return C.int
403 SE := Nonreentrant_Getservbyport (Port, Proto);
409 -- Now copy the data to the user-provided buffer
412 (Source_Servent => SE.all,
413 Target_Servent => Ret.all,
414 Target_Buffer => Buf,
415 Target_Buffer_Length => Buflen,
419 GNAT.Task_Lock.Unlock;
421 end Safe_Getservbyport;
429 Storage : in out char_array;
430 Storage_Index : in out size_t;
431 Stored_Name : out C.Strings.chars_ptr)
433 First : constant C.size_t := Storage_Index;
434 Last : constant C.size_t := Storage_Index + Name'Length - 1;
436 Storage (First .. Last) := Name;
437 Stored_Name := C.Strings.To_Chars_Ptr
438 (Storage (First .. Last)'Unrestricted_Access);
439 Storage_Index := Last + 1;
442 end GNAT.Sockets.Thin.Task_Safe_NetDB;