OSDN Git Service

2009-04-07 Robert Dewar <dewar@adacore.com>
[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, 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 with Interfaces.C.Strings;
42
43 package GNAT.Sockets.Thin_Common is
44
45    package C renames Interfaces.C;
46
47    use type C.int;
48    --  This is so we can declare the Failure constant below
49
50    Success : constant C.int :=  0;
51    Failure : constant C.int := -1;
52
53    type time_t is
54      range -2 ** (8 * SOSC.SIZEOF_tv_sec - 1)
55          .. 2 ** (8 * SOSC.SIZEOF_tv_sec - 1) - 1;
56    for time_t'Size use 8 * SOSC.SIZEOF_tv_sec;
57    pragma Convention (C, time_t);
58
59    type suseconds_t is
60      range -2 ** (8 * SOSC.SIZEOF_tv_usec - 1)
61          .. 2 ** (8 * SOSC.SIZEOF_tv_usec - 1) - 1;
62    for suseconds_t'Size use 8 * SOSC.SIZEOF_tv_usec;
63    pragma Convention (C, suseconds_t);
64
65    type Timeval is record
66       Tv_Sec  : time_t;
67       Tv_Usec : suseconds_t;
68    end record;
69    pragma Convention (C, Timeval);
70
71    type Timeval_Access is access all Timeval;
72    pragma Convention (C, Timeval_Access);
73
74    Immediat : constant Timeval := (0, 0);
75
76    -------------------------------------------
77    -- Mapping tables to low level constants --
78    -------------------------------------------
79
80    Families : constant array (Family_Type) of C.int :=
81                 (Family_Inet  => SOSC.AF_INET,
82                  Family_Inet6 => SOSC.AF_INET6);
83
84    Lengths  : constant array (Family_Type) of C.unsigned_char :=
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;
112    pragma Unchecked_Union (Sockaddr_Length_And_Family);
113    pragma Convention (C, Sockaddr_Length_And_Family);
114
115    procedure Set_Family
116      (Length_And_Family : out Sockaddr_Length_And_Family;
117       Family            : Family_Type);
118    --  Set the family component to the appropriate value for Family, and also
119    --  set Length accordingly if applicable on this platform.
120
121    type Sockaddr is record
122       Sa_Family : Sockaddr_Length_And_Family;
123       --  Address family (and address length on some platforms)
124
125       Sa_Data : C.char_array (1 .. 14) := (others => C.nul);
126       --  Family-specific data
127       --  Note that some platforms require that all unused (reserved) bytes
128       --  in addresses be initialized to 0 (e.g. VxWorks).
129    end record;
130    pragma Convention (C, Sockaddr);
131    --  Generic socket address
132
133    type Sockaddr_Access is access all Sockaddr;
134    pragma Convention (C, Sockaddr_Access);
135    --  Access to socket address
136
137    ----------------------------
138    -- AF_INET socket address --
139    ----------------------------
140
141    type In_Addr is record
142       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
143    end record;
144    for In_Addr'Alignment use C.int'Alignment;
145    pragma Convention (C, In_Addr);
146    --  IPv4 address, represented as a network-order C.int. Note that the
147    --  underlying operating system may assume that values of this type have
148    --  C.int alignment, so we need to provide a suitable alignment clause here.
149
150    function To_In_Addr is new Ada.Unchecked_Conversion (C.int, In_Addr);
151    function To_Int     is new Ada.Unchecked_Conversion (In_Addr, C.int);
152
153    type In_Addr_Access is access all In_Addr;
154    pragma Convention (C, In_Addr_Access);
155    --  Access to internet address
156
157    Inaddr_Any : aliased constant In_Addr := (others => 0);
158    --  Any internet address (all the interfaces)
159
160    type In_Addr_Access_Array is array (C.size_t range <>)
161      of aliased In_Addr_Access;
162    pragma Convention (C, In_Addr_Access_Array);
163
164    package In_Addr_Access_Pointers is new C.Pointers
165      (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
166    --  Array of internet addresses
167
168    type Sockaddr_In is record
169       Sin_Family : Sockaddr_Length_And_Family;
170       --  Address family (and address length on some platforms)
171
172       Sin_Port : C.unsigned_short;
173       --  Port in network byte order
174
175       Sin_Addr : In_Addr;
176       --  IPv4 address
177
178       Sin_Zero : C.char_array (1 .. 8) := (others => C.nul);
179       --  Padding
180       --
181       --  Note that some platforms require that all unused (reserved) bytes
182       --  in addresses be initialized to 0 (e.g. VxWorks).
183    end record;
184    pragma Convention (C, Sockaddr_In);
185    --  Internet socket address
186
187    type Sockaddr_In_Access is access all Sockaddr_In;
188    pragma Convention (C, Sockaddr_In_Access);
189    --  Access to internet socket address
190
191    procedure Set_Port
192      (Sin  : Sockaddr_In_Access;
193       Port : C.unsigned_short);
194    pragma Inline (Set_Port);
195    --  Set Sin.Sin_Port to Port
196
197    procedure Set_Address
198      (Sin     : Sockaddr_In_Access;
199       Address : In_Addr);
200    pragma Inline (Set_Address);
201    --  Set Sin.Sin_Addr to Address
202
203    ---------------------
204    -- Service entries --
205    ---------------------
206
207    type Chars_Ptr_Array is array (C.size_t range <>) of
208      aliased C.Strings.chars_ptr;
209
210    package Chars_Ptr_Pointers is
211       new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
212                       C.Strings.Null_Ptr);
213    --  Arrays of C (char *)
214
215    type Servent is record
216       S_Name    : C.Strings.chars_ptr;
217       S_Aliases : Chars_Ptr_Pointers.Pointer;
218       S_Port    : C.int;
219       S_Proto   : C.Strings.chars_ptr;
220    end record;
221    pragma Convention (C, Servent);
222    --  Service entry
223
224    type Servent_Access is access all Servent;
225    pragma Convention (C, Servent_Access);
226    --  Access to service entry
227
228    ------------------
229    -- Host entries --
230    ------------------
231
232    type Hostent is record
233       H_Name      : C.Strings.chars_ptr;
234       H_Aliases   : Chars_Ptr_Pointers.Pointer;
235       H_Addrtype  : SOSC.H_Addrtype_T;
236       H_Length    : SOSC.H_Length_T;
237       H_Addr_List : In_Addr_Access_Pointers.Pointer;
238    end record;
239    pragma Convention (C, Hostent);
240    --  Host entry
241
242    type Hostent_Access is access all Hostent;
243    pragma Convention (C, Hostent_Access);
244    --  Access to host entry
245
246    ----------------------------
247    -- Socket sets management --
248    ----------------------------
249
250    type Int_Access is access all C.int;
251    pragma Convention (C, Int_Access);
252    --  Access to C integers
253
254    procedure Get_Socket_From_Set
255      (Set    : access Fd_Set;
256       Socket : Int_Access;
257       Last   : Int_Access);
258    --  Get last socket in Socket and remove it from the socket set. The
259    --  parameter Last is a maximum value of the largest socket. This hint is
260    --  used to avoid scanning very large socket sets. After a call to
261    --  Get_Socket_From_Set, Last is set back to the real largest socket in the
262    --  socket set.
263
264    procedure Insert_Socket_In_Set
265      (Set    : access Fd_Set;
266       Socket : C.int);
267    --  Insert socket in the socket set
268
269    function  Is_Socket_In_Set
270      (Set    : access constant Fd_Set;
271       Socket : C.int) return C.int;
272    --  Check whether Socket is in the socket set, return a non-zero
273    --  value if it is, zero if it is not.
274
275    procedure Last_Socket_In_Set
276      (Set  : access Fd_Set;
277       Last : Int_Access);
278    --  Find the largest socket in the socket set. This is needed for select().
279    --  When Last_Socket_In_Set is called, parameter Last is a maximum value of
280    --  the largest socket. This hint is used to avoid scanning very large
281    --  socket sets. After the call, Last is set back to the real largest socket
282    --  in the socket set.
283
284    procedure Remove_Socket_From_Set (Set : access Fd_Set; Socket : C.int);
285    --  Remove socket from the socket set
286
287    procedure Reset_Socket_Set (Set : access Fd_Set);
288    --  Make Set empty
289
290    ------------------------------------------
291    -- Pairs of signalling file descriptors --
292    ------------------------------------------
293
294    type Two_Ints is array (0 .. 1) of C.int;
295    pragma Convention (C, Two_Ints);
296    --  Container for two int values
297
298    subtype Fd_Pair is Two_Ints;
299    --  Two_Ints as used for Create_Signalling_Fds: a pair of connected file
300    --  descriptors, one of which (the "read end" of the connection) being used
301    --  for reading, the other one (the "write end") being used for writing.
302
303    Read_End  : constant := 0;
304    Write_End : constant := 1;
305    --  Indices into an Fd_Pair value providing access to each of the connected
306    --  file descriptors.
307
308 private
309    pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
310    pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
311    pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
312    pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
313    pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
314    pragma Import (C, Reset_Socket_Set, "__gnat_reset_socket_set");
315 end GNAT.Sockets.Thin_Common;