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