Debian Codegraph @

gcc/ada/libgnat/g-sothco.ads

raw
1 ------------------------------------------------------------------------------
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-2025, 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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is the target-independent part of the thin sockets mapping.
33 -- This package should not be directly with'ed by an applications program.
34
35 with Ada.Unchecked_Conversion;
36 with Interfaces.C.Strings;
37 with System.Parameters;
38
39 package GNAT.Sockets.Thin_Common is
40
41 package C renames Interfaces.C;
42 package CS renames C.Strings;
43
44 Success : constant C.int := 0;
45 Failure : constant C.int := -1;
46
47 type time_t is
48 range -2 ** (System.Parameters.time_t_bits - 1)
49 .. 2 ** (System.Parameters.time_t_bits - 1) - 1;
50 for time_t'Size use System.Parameters.time_t_bits;
51 pragma Convention (C, time_t);
52
53 type suseconds_t is
54 range -2 ** (8 * SOSC.SIZEOF_tv_usec - 1)
55 .. 2 ** (8 * SOSC.SIZEOF_tv_usec - 1) - 1;
56 for suseconds_t'Size use 8 * SOSC.SIZEOF_tv_usec;
57 pragma Convention (C, suseconds_t);
58
59 type Timeval is record
60 Tv_Sec : time_t;
61 Tv_Usec : suseconds_t;
62 end record;
63 pragma Convention (C, Timeval);
64
65 type Timeval_Access is access all Timeval;
66 pragma Convention (C, Timeval_Access);
67
68 type socklen_t is mod 2 ** (8 * SOSC.SIZEOF_socklen_t);
69 for socklen_t'Size use (8 * SOSC.SIZEOF_socklen_t);
70
71 Immediat : constant Timeval := (0, 0);
72
73 -------------------------------------------
74 -- Mapping tables to low level constants --
75 -------------------------------------------
76
77 Families : constant array (Family_Type) of C.int :=
78 [Family_Unspec => SOSC.AF_UNSPEC,
79 Family_Unix => SOSC.AF_UNIX,
80 Family_Inet => SOSC.AF_INET,
81 Family_Inet6 => SOSC.AF_INET6];
82
83 Lengths : constant array (Family_Type) of C.unsigned_char :=
84 [Family_Unspec => 0,
85 Family_Unix => SOSC.SIZEOF_sockaddr_un,
86 Family_Inet => SOSC.SIZEOF_sockaddr_in,
87 Family_Inet6 => SOSC.SIZEOF_sockaddr_in6];
88
89 ----------------------------
90 -- Generic socket address --
91 ----------------------------
92
93 -- Common header
94
95 -- All socket address types (struct sockaddr, struct sockaddr_storage,
96 -- and protocol specific address types) start with the same 2-byte header,
97 -- which is either a length and a family (one byte each) or just a two-byte
98 -- family. The following unchecked union describes the two possible layouts
99 -- and is meant to be constrained with SOSC.Have_Sockaddr_Len.
100
101 type Sockaddr_Length_And_Family
102 (Has_Sockaddr_Len : Boolean := False)
103 is record
104 case Has_Sockaddr_Len is
105 when True =>
106 Length : C.unsigned_char;
107 Char_Family : C.unsigned_char;
108
109 when False =>
110 Short_Family : C.unsigned_short;
111 end case;
112 end record with Unchecked_Union, Convention => C;
113
114 procedure Set_Family
115 (Length_And_Family : out Sockaddr_Length_And_Family;
116 Family : Family_Type);
117 -- Set the family component to the appropriate value for Family, and also
118 -- set Length accordingly if applicable on this platform.
119
120 ----------------------------
121 -- AF_INET socket address --
122 ----------------------------
123
124 type In_Addr is record
125 S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
126 end record
127 with Convention => C, Alignment => C.int'Alignment, Universal_Aliasing;
128 -- IPv4 address, represented as a network-order C.int. Note that the
129 -- underlying operating system may assume that values of this type have
130 -- C.int's alignment, so we need to provide a suitable alignment clause.
131 -- We also need to inhibit strict type-based aliasing optimizations in
132 -- order to implement the following unchecked conversions efficiently.
133
134 function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
135 function To_Int is new Ada.Unchecked_Conversion (In_Addr, C.int);
136
137 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr;
138 procedure To_Inet_Addr
139 (Addr : In_Addr;
140 Result : out Inet_Addr_Type);
141 -- Conversion functions
142
143 type In6_Addr is array (1 .. 16) of C.unsigned_char with Convention => C;
144
145 Unix_Name_Length : constant := 108;
146 -- Maximum length for local unix socket name
147
148 function To_In6_Addr (Addr : Inet_Addr_Type) return In6_Addr;
149 procedure To_Inet_Addr
150 (Addr : In6_Addr;
151 Result : out Inet_Addr_Type);
152 -- Conversion functions
153
154 type Sockaddr (Family : Family_Type := Family_Inet) is record
155 case Family is
156 when Family_Inet =>
157 Sin_Family : Sockaddr_Length_And_Family;
158 -- Address family (and address length on some platforms)
159
160 Sin_Port : C.unsigned_short;
161 -- Port in network byte order
162
163 Sin_Addr : In_Addr := (others => 0);
164 -- IPv4 address
165
166 Sin_Zero : C.char_array (1 .. 8) := [others => C.nul];
167 -- Padding
168 --
169 -- Note that some platforms require that all unused (reserved) bytes
170 -- in addresses be initialized to 0 (e.g. VxWorks).
171
172 when Family_Inet6 =>
173 Sin6_Family : Sockaddr_Length_And_Family;
174 -- Address family (and address length on some platforms)
175
176 Sin6_Port : C.unsigned_short;
177 -- Port in network byte order
178
179 Sin6_FlowInfo : Interfaces.Unsigned_32 := 0;
180 Sin6_Addr : In6_Addr := [others => 0];
181 Sin6_Scope_Id : Interfaces.Unsigned_32 := 0;
182
183 when Family_Unix =>
184 Sun_Family : Sockaddr_Length_And_Family;
185 -- Address family (and address length on some platforms)
186
187 Sun_Path : C.char_array (1 .. Unix_Name_Length);
188
189 when Family_Unspec =>
190 null;
191 end case;
192 end record with Convention => C, Unchecked_Union;
193 -- Internet socket address
194
195 type Sockaddr_Access is access all Sockaddr;
196 pragma Convention (C, Sockaddr_Access);
197 -- Access to internet socket address
198
199 procedure Set_Address
200 (Sin : Sockaddr_Access;
201 Address : Sock_Addr_Type;
202 Length : out C.int);
203 -- Initialise all necessary fields in Sin from Address.
204 -- Set appropriate Family, Port, and either Sin.Sin_Addr or Sin.Sin6_Addr
205 -- depend on family.
206 -- Set the Length out parameter to the valuable Sockaddr data length.
207
208 function Get_Address (Sin : Sockaddr; Length : C.int) return Sock_Addr_Type;
209 -- Get Sock_Addr_Type from Sockaddr and its valuable data Length
210
211 ------------------
212 -- Host entries --
213 ------------------
214
215 type Hostent is new
216 System.Storage_Elements.Storage_Array (1 .. SOSC.SIZEOF_struct_hostent);
217 for Hostent'Alignment use 8;
218 -- Host entry. This is an opaque type used only via the following
219 -- accessor functions, because 'struct hostent' has different layouts on
220 -- different platforms.
221
222 type Hostent_Access is access all Hostent;
223 pragma Convention (C, Hostent_Access);
224 -- Access to host entry
225
226 function Hostent_H_Name
227 (E : Hostent_Access) return System.Address;
228
229 function Hostent_H_Alias
230 (E : Hostent_Access; I : C.int) return System.Address;
231
232 function Hostent_H_Addrtype
233 (E : Hostent_Access) return C.int;
234
235 function Hostent_H_Length
236 (E : Hostent_Access) return C.int;
237
238 function Hostent_H_Addr
239 (E : Hostent_Access; Index : C.int) return System.Address;
240
241 ---------------------
242 -- Service entries --
243 ---------------------
244
245 type Servent is new
246 System.Storage_Elements.Storage_Array (1 .. SOSC.SIZEOF_struct_servent);
247 for Servent'Alignment use 8;
248 -- Service entry. This is an opaque type used only via the following
249 -- accessor functions, because 'struct servent' has different layouts on
250 -- different platforms.
251
252 type Servent_Access is access all Servent;
253 pragma Convention (C, Servent_Access);
254 -- Access to service entry
255
256 function Servent_S_Name
257 (E : Servent_Access) return System.Address;
258
259 function Servent_S_Alias
260 (E : Servent_Access; Index : C.int) return System.Address;
261
262 function Servent_S_Port
263 (E : Servent_Access) return C.unsigned_short;
264
265 function Servent_S_Proto
266 (E : Servent_Access) return System.Address;
267
268 ------------------
269 -- NetDB access --
270 ------------------
271
272 -- There are three possible situations for the following NetDB access
273 -- functions:
274 -- - inherently thread safe (case of data returned in a thread specific
275 -- buffer);
276 -- - thread safe using user-provided buffer;
277 -- - thread unsafe.
278 --
279 -- In the first and third cases, the Buf and Buflen are ignored. In the
280 -- second case, the caller must provide a buffer large enough to
281 -- accommodate the returned data. In the third case, the caller must ensure
282 -- that these functions are called within a critical section.
283
284 function C_Gethostbyname
285 (Name : C.char_array;
286 Ret : not null access Hostent;
287 Buf : System.Address;
288 Buflen : C.size_t;
289 H_Errnop : not null access C.int) return C.int;
290
291 function C_Gethostbyaddr
292 (Addr : System.Address;
293 Addr_Len : C.int;
294 Addr_Type : C.int;
295 Ret : not null access Hostent;
296 Buf : System.Address;
297 Buflen : C.size_t;
298 H_Errnop : not null access C.int) return C.int;
299
300 function C_Getservbyname
301 (Name : C.char_array;
302 Proto : C.char_array;
303 Ret : not null access Servent;
304 Buf : System.Address;
305 Buflen : C.size_t) return C.int;
306
307 function C_Getservbyport
308 (Port : C.int;
309 Proto : C.char_array;
310 Ret : not null access Servent;
311 Buf : System.Address;
312 Buflen : C.size_t) return C.int;
313
314 Address_Size : constant := Standard'Address_Size;
315
316 type Addrinfo;
317 type Addrinfo_Access is access all Addrinfo;
318
319 type Addrinfo is record
320 ai_flags : C.int;
321 ai_family : C.int;
322 ai_socktype : C.int;
323 ai_protocol : C.int;
324 ai_addrlen : socklen_t;
325 ai_addr : Sockaddr_Access;
326 ai_canonname : CS.char_array_access;
327 ai_next : Addrinfo_Access;
328 end record with Convention => C;
329 for Addrinfo use record
330 ai_flags at SOSC.AI_FLAGS_OFFSET range 0 .. C.int'Size - 1;
331 ai_family at SOSC.AI_FAMILY_OFFSET range 0 .. C.int'Size - 1;
332 ai_socktype at SOSC.AI_SOCKTYPE_OFFSET range 0 .. C.int'Size - 1;
333 ai_protocol at SOSC.AI_PROTOCOL_OFFSET range 0 .. C.int'Size - 1;
334 ai_addrlen at SOSC.AI_ADDRLEN_OFFSET range 0 .. socklen_t'Size - 1;
335 ai_canonname at SOSC.AI_CANONNAME_OFFSET range 0 .. Address_Size - 1;
336 ai_addr at SOSC.AI_ADDR_OFFSET range 0 .. Address_Size - 1;
337 ai_next at SOSC.AI_NEXT_OFFSET range 0 .. Address_Size - 1;
338 end record;
339
340 function C_Getaddrinfo
341 (Node : CS.char_array_access;
342 Service : CS.char_array_access;
343 Hints : access constant Addrinfo;
344 Res : not null access Addrinfo_Access) return C.int;
345
346 procedure C_Freeaddrinfo (res : Addrinfo_Access);
347
348 function C_Getnameinfo
349 (sa : Sockaddr_Access;
350 salen : socklen_t;
351 host : CS.char_array_access;
352 hostlen : C.size_t;
353 serv : CS.char_array_access;
354 servlen : C.size_t;
355 flags : C.int) return C.int;
356
357 function C_GAI_Strerror (ecode : C.int) return CS.chars_ptr;
358
359 ------------------------------------
360 -- Scatter/gather vector handling --
361 ------------------------------------
362
363 type Msghdr is record
364 Msg_Name : System.Address;
365 Msg_Namelen : C.unsigned;
366 Msg_Iov : System.Address;
367 Msg_Iovlen : SOSC.Msg_Iovlen_T;
368 Msg_Control : System.Address;
369 Msg_Controllen : C.size_t;
370 Msg_Flags : C.int;
371 end record;
372 pragma Convention (C, Msghdr);
373
374 ----------------------------
375 -- Socket sets management --
376 ----------------------------
377
378 procedure Get_Socket_From_Set
379 (Set : access Fd_Set;
380 Last : access C.int;
381 Socket : access C.int);
382 -- Get last socket in Socket and remove it from the socket set. The
383 -- parameter Last is a maximum value of the largest socket. This hint is
384 -- used to avoid scanning very large socket sets. After a call to
385 -- Get_Socket_From_Set, Last is set back to the real largest socket in the
386 -- socket set.
387
388 procedure Insert_Socket_In_Set
389 (Set : access Fd_Set;
390 Socket : C.int);
391 -- Insert socket in the socket set
392
393 function Is_Socket_In_Set
394 (Set : access constant Fd_Set;
395 Socket : C.int) return C.int;
396 -- Check whether Socket is in the socket set, return a non-zero
397 -- value if it is, zero if it is not.
398
399 procedure Last_Socket_In_Set
400 (Set : access Fd_Set;
401 Last : access C.int);
402 -- Find the largest socket in the socket set. This is needed for select().
403 -- When Last_Socket_In_Set is called, parameter Last is a maximum value of
404 -- the largest socket. This hint is used to avoid scanning very large
405 -- socket sets. After the call, Last is set back to the real largest socket
406 -- in the socket set.
407
408 procedure Remove_Socket_From_Set (Set : access Fd_Set; Socket : C.int);
409 -- Remove socket from the socket set
410
411 procedure Reset_Socket_Set (Set : access Fd_Set);
412 -- Make Set empty
413
414 ------------------------------------------
415 -- Pairs of signalling file descriptors --
416 ------------------------------------------
417
418 type Two_Ints is array (0 .. 1) of C.int;
419 pragma Convention (C, Two_Ints);
420 -- Container for two int values
421
422 subtype Fd_Pair is Two_Ints;
423 -- Two_Ints as used for Create_Signalling_Fds: a pair of connected file
424 -- descriptors, one of which (the "read end" of the connection) being used
425 -- for reading, the other one (the "write end") being used for writing.
426
427 Read_End : constant := 0;
428 Write_End : constant := 1;
429 -- Indexes into an Fd_Pair value providing access to each of the connected
430 -- file descriptors.
431
432 function Inet_Pton
433 (Af : C.int;
434 Cp : System.Address;
435 Inp : System.Address) return C.int;
436
437 function Inet_Ntop
438 (Af : C.int;
439 Src : System.Address;
440 Dst : CS.char_array_access;
441 Size : socklen_t) return CS.char_array_access;
442
443 function C_Ioctl
444 (Fd : C.int;
445 Req : SOSC.IOCTL_Req_T;
446 Arg : access C.int) return C.int;
447
448 function Short_To_Network
449 (S : C.unsigned_short) return C.unsigned_short;
450 pragma Inline (Short_To_Network);
451 -- Convert a port number into a network port number
452
453 function Network_To_Short
454 (S : C.unsigned_short) return C.unsigned_short
455 renames Short_To_Network;
456 -- Symmetric operation
457
458 Minus_500ms_Windows_Timeout : constant Boolean;
459 -- Microsoft Windows desktop older then 8.0 and Microsoft Windows Server
460 -- older than 2019 need timeout correction for 500 milliseconds. This
461 -- constant is True for such versions.
462
463 private
464
465 function Get_Minus_500ms_Timeout return C.int
466 with Import, Convention => C, External_Name => "__gnat_minus_500ms";
467
468 Minus_500ms_Windows_Timeout : constant Boolean :=
469 Get_Minus_500ms_Timeout /= 0;
470
471 pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
472 pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
473 pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
474 pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
475 pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
476 pragma Import (C, Reset_Socket_Set, "__gnat_reset_socket_set");
477 pragma Import (C, C_Ioctl, "__gnat_socket_ioctl");
478 pragma Import (C, Inet_Pton, SOSC.Inet_Pton_Linkname);
479 pragma Import (C, Inet_Ntop, SOSC.Inet_Ntop_Linkname);
480
481 pragma Import (C, C_Gethostbyname, "__gnat_gethostbyname");
482 pragma Import (C, C_Gethostbyaddr, "__gnat_gethostbyaddr");
483 pragma Import (C, C_Getservbyname, "__gnat_getservbyname");
484 pragma Import (C, C_Getservbyport, "__gnat_getservbyport");
485
486 pragma Import (C, C_Getaddrinfo, "__gnat_getaddrinfo");
487 pragma Import (C, C_Freeaddrinfo, "__gnat_freeaddrinfo");
488 pragma Import (C, C_Getnameinfo, "__gnat_getnameinfo");
489 pragma Import (C, C_GAI_Strerror, "__gnat_gai_strerror");
490
491 pragma Import (C, Servent_S_Name, "__gnat_servent_s_name");
492 pragma Import (C, Servent_S_Alias, "__gnat_servent_s_alias");
493 pragma Import (C, Servent_S_Port, "__gnat_servent_s_port");
494 pragma Import (C, Servent_S_Proto, "__gnat_servent_s_proto");
495
496 pragma Import (C, Hostent_H_Name, "__gnat_hostent_h_name");
497 pragma Import (C, Hostent_H_Alias, "__gnat_hostent_h_alias");
498 pragma Import (C, Hostent_H_Addrtype, "__gnat_hostent_h_addrtype");
499 pragma Import (C, Hostent_H_Length, "__gnat_hostent_h_length");
500 pragma Import (C, Hostent_H_Addr, "__gnat_hostent_h_addr");
501
502 end GNAT.Sockets.Thin_Common;