OSDN Git Service

Fix typos in gcc/ada.
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-sothco.ads
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-2010, 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 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.                                              --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
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.
36
37 with Ada.Unchecked_Conversion;
38
39 with Interfaces.C;
40 with Interfaces.C.Pointers;
41
42 package GNAT.Sockets.Thin_Common is
43
44    package C renames Interfaces.C;
45
46    use type C.int;
47    --  This is so we can declare the Failure constant below
48
49    Success : constant C.int :=  0;
50    Failure : constant C.int := -1;
51
52    type time_t is
53      range -2 ** (8 * SOSC.SIZEOF_tv_sec - 1)
54          .. 2 ** (8 * SOSC.SIZEOF_tv_sec - 1) - 1;
55    for time_t'Size use 8 * SOSC.SIZEOF_tv_sec;
56    pragma Convention (C, time_t);
57
58    type suseconds_t is
59      range -2 ** (8 * SOSC.SIZEOF_tv_usec - 1)
60          .. 2 ** (8 * SOSC.SIZEOF_tv_usec - 1) - 1;
61    for suseconds_t'Size use 8 * SOSC.SIZEOF_tv_usec;
62    pragma Convention (C, suseconds_t);
63
64    type Timeval is record
65       Tv_Sec  : time_t;
66       Tv_Usec : suseconds_t;
67    end record;
68    pragma Convention (C, Timeval);
69
70    type Timeval_Access is access all Timeval;
71    pragma Convention (C, Timeval_Access);
72
73    Immediat : constant Timeval := (0, 0);
74
75    -------------------------------------------
76    -- Mapping tables to low level constants --
77    -------------------------------------------
78
79    Families : constant array (Family_Type) of C.int :=
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_Inet  => SOSC.SIZEOF_sockaddr_in,
85                  Family_Inet6 => SOSC.SIZEOF_sockaddr_in6);
86
87    ----------------------------
88    -- Generic socket address --
89    ----------------------------
90
91    --  Common header
92
93    --  All socket address types (struct sockaddr, struct sockaddr_storage,
94    --  and protocol specific address types) start with the same 2-byte header,
95    --  which is either a length and a family (one byte each) or just a two-byte
96    --  family. The following unchecked union describes the two possible layouts
97    --  and is meant to be constrained with SOSC.Have_Sockaddr_Len.
98
99    type Sockaddr_Length_And_Family
100      (Has_Sockaddr_Len : Boolean := False)
101    is record
102       case Has_Sockaddr_Len is
103          when True =>
104             Length      : C.unsigned_char;
105             Char_Family : C.unsigned_char;
106
107          when False =>
108             Short_Family : C.unsigned_short;
109       end case;
110    end record;
111    pragma Unchecked_Union (Sockaddr_Length_And_Family);
112    pragma Convention (C, Sockaddr_Length_And_Family);
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    type Sockaddr is record
121       Sa_Family : Sockaddr_Length_And_Family;
122       --  Address family (and address length on some platforms)
123
124       Sa_Data : C.char_array (1 .. 14) := (others => C.nul);
125       --  Family-specific data
126       --  Note that some platforms require that all unused (reserved) bytes
127       --  in addresses be initialized to 0 (e.g. VxWorks).
128    end record;
129    pragma Convention (C, Sockaddr);
130    --  Generic socket address
131
132    type Sockaddr_Access is access all Sockaddr;
133    pragma Convention (C, Sockaddr_Access);
134    --  Access to socket address
135
136    ----------------------------
137    -- AF_INET socket address --
138    ----------------------------
139
140    type In_Addr is record
141       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
142    end record;
143    for In_Addr'Alignment use C.int'Alignment;
144    pragma Convention (C, In_Addr);
145    --  IPv4 address, represented as a network-order C.int. Note that the
146    --  underlying operating system may assume that values of this type have
147    --  C.int alignment, so we need to provide a suitable alignment clause here.
148
149    function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
150    function To_Int     is new Ada.Unchecked_Conversion (In_Addr, C.int);
151
152    type In_Addr_Access is access all In_Addr;
153    pragma Convention (C, In_Addr_Access);
154    --  Access to internet address
155
156    Inaddr_Any : aliased constant In_Addr := (others => 0);
157    --  Any internet address (all the interfaces)
158
159    type In_Addr_Access_Array is array (C.size_t range <>)
160      of aliased In_Addr_Access;
161    pragma Convention (C, In_Addr_Access_Array);
162
163    package In_Addr_Access_Pointers is new C.Pointers
164      (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
165    --  Array of internet addresses
166
167    type Sockaddr_In is record
168       Sin_Family : Sockaddr_Length_And_Family;
169       --  Address family (and address length on some platforms)
170
171       Sin_Port : C.unsigned_short;
172       --  Port in network byte order
173
174       Sin_Addr : In_Addr;
175       --  IPv4 address
176
177       Sin_Zero : C.char_array (1 .. 8) := (others => C.nul);
178       --  Padding
179       --
180       --  Note that some platforms require that all unused (reserved) bytes
181       --  in addresses be initialized to 0 (e.g. VxWorks).
182    end record;
183    pragma Convention (C, Sockaddr_In);
184    --  Internet socket address
185
186    type Sockaddr_In_Access is access all Sockaddr_In;
187    pragma Convention (C, Sockaddr_In_Access);
188    --  Access to internet socket address
189
190    procedure Set_Port
191      (Sin  : Sockaddr_In_Access;
192       Port : C.unsigned_short);
193    pragma Inline (Set_Port);
194    --  Set Sin.Sin_Port to Port
195
196    procedure Set_Address
197      (Sin     : Sockaddr_In_Access;
198       Address : In_Addr);
199    pragma Inline (Set_Address);
200    --  Set Sin.Sin_Addr to Address
201
202    ------------------
203    -- Host entries --
204    ------------------
205
206    type Hostent is new
207      System.Storage_Elements.Storage_Array (1 .. SOSC.SIZEOF_struct_hostent);
208    for Hostent'Alignment use 8;
209    --  Host entry. This is an opaque type used only via the following
210    --  accessor functions, because 'struct hostent' has different layouts on
211    --  different platforms.
212
213    type Hostent_Access is access all Hostent;
214    pragma Convention (C, Hostent_Access);
215    --  Access to host entry
216
217    --  Note: the hostent and servent accessors that return char*
218    --  values are compiled with GCC, and on VMS they always return
219    --  64-bit pointers, so we can't use C.Strings.chars_ptr, which
220    --  on VMS is 32 bits.
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    ------------------------------------
311    -- Scatter/gather vector handling --
312    ------------------------------------
313
314    type Msghdr is record
315       Msg_Name       : System.Address;
316       Msg_Namelen    : C.unsigned;
317       Msg_Iov        : System.Address;
318       Msg_Iovlen     : SOSC.Msg_Iovlen_T;
319       Msg_Control    : System.Address;
320       Msg_Controllen : C.size_t;
321       Msg_Flags      : C.int;
322    end record;
323    pragma Convention (C, Msghdr);
324
325    ----------------------------
326    -- Socket sets management --
327    ----------------------------
328
329    procedure Get_Socket_From_Set
330      (Set    : access Fd_Set;
331       Last   : access C.int;
332       Socket : access C.int);
333    --  Get last socket in Socket and remove it from the socket set. The
334    --  parameter Last is a maximum value of the largest socket. This hint is
335    --  used to avoid scanning very large socket sets. After a call to
336    --  Get_Socket_From_Set, Last is set back to the real largest socket in the
337    --  socket set.
338
339    procedure Insert_Socket_In_Set
340      (Set    : access Fd_Set;
341       Socket : C.int);
342    --  Insert socket in the socket set
343
344    function  Is_Socket_In_Set
345      (Set    : access constant Fd_Set;
346       Socket : C.int) return C.int;
347    --  Check whether Socket is in the socket set, return a non-zero
348    --  value if it is, zero if it is not.
349
350    procedure Last_Socket_In_Set
351      (Set  : access Fd_Set;
352       Last : access C.int);
353    --  Find the largest socket in the socket set. This is needed for select().
354    --  When Last_Socket_In_Set is called, parameter Last is a maximum value of
355    --  the largest socket. This hint is used to avoid scanning very large
356    --  socket sets. After the call, Last is set back to the real largest socket
357    --  in the socket set.
358
359    procedure Remove_Socket_From_Set (Set : access Fd_Set; Socket : C.int);
360    --  Remove socket from the socket set
361
362    procedure Reset_Socket_Set (Set : access Fd_Set);
363    --  Make Set empty
364
365    ------------------------------------------
366    -- Pairs of signalling file descriptors --
367    ------------------------------------------
368
369    type Two_Ints is array (0 .. 1) of C.int;
370    pragma Convention (C, Two_Ints);
371    --  Container for two int values
372
373    subtype Fd_Pair is Two_Ints;
374    --  Two_Ints as used for Create_Signalling_Fds: a pair of connected file
375    --  descriptors, one of which (the "read end" of the connection) being used
376    --  for reading, the other one (the "write end") being used for writing.
377
378    Read_End  : constant := 0;
379    Write_End : constant := 1;
380    --  Indexes into an Fd_Pair value providing access to each of the connected
381    --  file descriptors.
382
383    function Inet_Pton
384      (Af  : C.int;
385       Cp  : System.Address;
386       Inp : System.Address) return C.int;
387
388    function C_Ioctl
389      (Fd  : C.int;
390       Req : C.int;
391       Arg : access C.int) return C.int;
392
393 private
394    pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
395    pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
396    pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
397    pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
398    pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
399    pragma Import (C, Reset_Socket_Set, "__gnat_reset_socket_set");
400    pragma Import (C, C_Ioctl, "__gnat_socket_ioctl");
401    pragma Import (C, Inet_Pton, SOSC.Inet_Pton_Linkname);
402
403    pragma Import (C, C_Gethostbyname, "__gnat_gethostbyname");
404    pragma Import (C, C_Gethostbyaddr, "__gnat_gethostbyaddr");
405    pragma Import (C, C_Getservbyname, "__gnat_getservbyname");
406    pragma Import (C, C_Getservbyport, "__gnat_getservbyport");
407
408    pragma Import (C, Servent_S_Name,  "__gnat_servent_s_name");
409    pragma Import (C, Servent_S_Alias, "__gnat_servent_s_alias");
410    pragma Import (C, Servent_S_Port,  "__gnat_servent_s_port");
411    pragma Import (C, Servent_S_Proto, "__gnat_servent_s_proto");
412
413    pragma Import (C, Hostent_H_Name,     "__gnat_hostent_h_name");
414    pragma Import (C, Hostent_H_Alias,    "__gnat_hostent_h_alias");
415    pragma Import (C, Hostent_H_Addrtype, "__gnat_hostent_h_addrtype");
416    pragma Import (C, Hostent_H_Length,   "__gnat_hostent_h_length");
417    pragma Import (C, Hostent_H_Addr,     "__gnat_hostent_h_addr");
418
419 end GNAT.Sockets.Thin_Common;