OSDN Git Service

2008-04-08 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socthi.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                    G N A T . S O C K E T S . T H I N                     --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                     Copyright (C) 2001-2007, 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 package provides a target dependent thin interface to the sockets
35 --  layer for use by the GNAT.Sockets package (g-socket.ads). This package
36 --  should not be directly with'ed by an applications program.
37
38 --  This is the default version
39
40 with Interfaces.C.Pointers;
41 with Interfaces.C.Strings;
42
43 with GNAT.OS_Lib;
44 with GNAT.Sockets.Constants;
45
46 with System;
47
48 package GNAT.Sockets.Thin is
49
50    --  This package is intended for hosts implementing BSD sockets with a
51    --  standard interface. It will be used as a default for all the platforms
52    --  that do not have a specific version of this file.
53
54    package C renames Interfaces.C;
55
56    use type C.int;
57    --  This is so we can declare the Failure constant below
58
59    Success : constant C.int :=  0;
60    Failure : constant C.int := -1;
61
62    function Socket_Errno return Integer renames GNAT.OS_Lib.Errno;
63    --  Returns last socket error number
64
65    function Socket_Error_Message (Errno : Integer) return C.Strings.chars_ptr;
66    --  Returns the error message string for the error number Errno. If Errno is
67    --  not known, returns "Unknown system error".
68
69    function Host_Errno return Integer;
70    pragma Import (C, Host_Errno, "__gnat_get_h_errno");
71    --  Returns last host error number
72
73    package Host_Error_Messages is
74
75       function Host_Error_Message
76         (H_Errno : Integer) return C.Strings.chars_ptr;
77       --  Returns the error message string for the host error number H_Errno.
78       --  If H_Errno is not known, returns "Unknown system error".
79
80    end Host_Error_Messages;
81
82    subtype Fd_Set_Access is System.Address;
83    No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
84
85    type time_t is
86      range -2 ** (8 * Constants.SIZEOF_tv_sec - 1)
87          .. 2 ** (8 * Constants.SIZEOF_tv_sec - 1) - 1;
88    for time_t'Size use 8 * Constants.SIZEOF_tv_sec;
89    pragma Convention (C, time_t);
90
91    type suseconds_t is
92      range -2 ** (8 * Constants.SIZEOF_tv_usec - 1)
93          .. 2 ** (8 * Constants.SIZEOF_tv_usec - 1) - 1;
94    for suseconds_t'Size use 8 * Constants.SIZEOF_tv_usec;
95    pragma Convention (C, suseconds_t);
96
97    type Timeval is record
98       Tv_Sec  : time_t;
99       Tv_Usec : suseconds_t;
100    end record;
101    pragma Convention (C, Timeval);
102
103    type Timeval_Access is access all Timeval;
104    pragma Convention (C, Timeval_Access);
105
106    Immediat : constant Timeval := (0, 0);
107
108    type Int_Access is access all C.int;
109    pragma Convention (C, Int_Access);
110    --  Access to C integers
111
112    type Chars_Ptr_Array is array (C.size_t range <>) of
113      aliased C.Strings.chars_ptr;
114
115    package Chars_Ptr_Pointers is
116       new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
117                       C.Strings.Null_Ptr);
118    --  Arrays of C (char *)
119
120    type In_Addr is record
121       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
122    end record;
123    for In_Addr'Alignment use C.int'Alignment;
124    pragma Convention (C, In_Addr);
125    --  IPv4 address, represented as a network-order C.int. Note that the
126    --  underlying operating system may assume that values of this type have
127    --  C.int alignment, so we need to provide a suitable alignment clause here.
128
129    type In_Addr_Access is access all In_Addr;
130    pragma Convention (C, In_Addr_Access);
131    --  Access to internet address
132
133    Inaddr_Any : aliased constant In_Addr := (others => 0);
134    --  Any internet address (all the interfaces)
135
136    type In_Addr_Access_Array is array (C.size_t range <>)
137      of aliased In_Addr_Access;
138    pragma Convention (C, In_Addr_Access_Array);
139
140    package In_Addr_Access_Pointers is
141      new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
142    --  Array of internet addresses
143
144    type Sockaddr is record
145       Sa_Family : C.unsigned_short;
146       Sa_Data   : C.char_array (1 .. 14);
147    end record;
148    pragma Convention (C, Sockaddr);
149    --  Socket address
150
151    type Sockaddr_Access is access all Sockaddr;
152    pragma Convention (C, Sockaddr_Access);
153    --  Access to socket address
154
155    type Sockaddr_In is record
156       Sin_Family : C.unsigned_short      := Constants.AF_INET;
157       Sin_Port   : C.unsigned_short      := 0;
158       Sin_Addr   : In_Addr               := Inaddr_Any;
159       Sin_Zero   : C.char_array (1 .. 8) := (others => C.char'Val (0));
160    end record;
161    pragma Convention (C, Sockaddr_In);
162    --  Internet socket address
163
164    type Sockaddr_In_Access is access all Sockaddr_In;
165    pragma Convention (C, Sockaddr_In_Access);
166    --  Access to internet socket address
167
168    procedure Set_Length
169      (Sin : Sockaddr_In_Access;
170       Len : C.int);
171    pragma Inline (Set_Length);
172    --  Set Sin.Sin_Length to Len.
173    --  On this platform, nothing is done as there is no such field.
174
175    procedure Set_Family
176      (Sin    : Sockaddr_In_Access;
177       Family : C.int);
178    pragma Inline (Set_Family);
179    --  Set Sin.Sin_Family to Family
180
181    procedure Set_Port
182      (Sin  : Sockaddr_In_Access;
183       Port : C.unsigned_short);
184    pragma Inline (Set_Port);
185    --  Set Sin.Sin_Port to Port
186
187    procedure Set_Address
188      (Sin     : Sockaddr_In_Access;
189       Address : In_Addr);
190    pragma Inline (Set_Address);
191    --  Set Sin.Sin_Addr to Address
192
193    type Hostent is record
194       H_Name      : C.Strings.chars_ptr;
195       H_Aliases   : Chars_Ptr_Pointers.Pointer;
196       H_Addrtype  : C.int;
197       H_Length    : C.int;
198       H_Addr_List : In_Addr_Access_Pointers.Pointer;
199    end record;
200    pragma Convention (C, Hostent);
201    --  Host entry
202
203    type Hostent_Access is access all Hostent;
204    pragma Convention (C, Hostent_Access);
205    --  Access to host entry
206
207    type Servent is record
208       S_Name    : C.Strings.chars_ptr;
209       S_Aliases : Chars_Ptr_Pointers.Pointer;
210       S_Port    : C.int;
211       S_Proto   : C.Strings.chars_ptr;
212    end record;
213    pragma Convention (C, Servent);
214    --  Service entry
215
216    type Servent_Access is access all Servent;
217    pragma Convention (C, Servent_Access);
218    --  Access to service entry
219
220    type Two_Ints is array (0 .. 1) of C.int;
221    pragma Convention (C, Two_Ints);
222    --  Container for two int values
223
224    subtype Fd_Pair is Two_Ints;
225    --  Two_Ints as used for Create_Signalling_Fds: a pair of connected file
226    --  descriptors, one of which (the "read end" of the connection) being used
227    --  for reading, the other one (the "write end") being used for writing.
228
229    Read_End  : constant := 0;
230    Write_End : constant := 1;
231    --  Indices into an Fd_Pair value providing access to each of the connected
232    --  file descriptors.
233
234    --------------------------------
235    -- Standard library functions --
236    --------------------------------
237
238    function C_Accept
239      (S       : C.int;
240       Addr    : System.Address;
241       Addrlen : not null access C.int) return C.int;
242
243    function C_Bind
244      (S       : C.int;
245       Name    : System.Address;
246       Namelen : C.int) return C.int;
247
248    function C_Close
249      (Fd : C.int) return C.int;
250
251    function C_Connect
252      (S       : C.int;
253       Name    : System.Address;
254       Namelen : C.int) return C.int;
255
256    function C_Gethostname
257      (Name    : System.Address;
258       Namelen : C.int) return C.int;
259
260    function C_Getpeername
261      (S       : C.int;
262       Name    : System.Address;
263       Namelen : not null access C.int) return C.int;
264
265    function C_Getsockname
266      (S       : C.int;
267       Name    : System.Address;
268       Namelen : not null access C.int) return C.int;
269
270    function C_Getsockopt
271      (S       : C.int;
272       Level   : C.int;
273       Optname : C.int;
274       Optval  : System.Address;
275       Optlen  : not null access C.int) return C.int;
276
277    function C_Inet_Addr
278      (Cp : C.Strings.chars_ptr) return C.int;
279
280    function C_Ioctl
281      (S    : C.int;
282       Req  : C.int;
283       Arg  : Int_Access) return C.int;
284
285    function C_Listen
286      (S       : C.int;
287       Backlog : C.int) return C.int;
288
289    function C_Readv
290      (Fd     : C.int;
291       Iov    : System.Address;
292       Iovcnt : C.int) return C.int;
293
294    function C_Recv
295      (S     : C.int;
296       Msg   : System.Address;
297       Len   : C.int;
298       Flags : C.int) return C.int;
299
300    function C_Recvfrom
301      (S       : C.int;
302       Msg     : System.Address;
303       Len     : C.int;
304       Flags   : C.int;
305       From    : Sockaddr_In_Access;
306       Fromlen : not null access C.int) return C.int;
307
308    function C_Select
309      (Nfds      : C.int;
310       Readfds   : Fd_Set_Access;
311       Writefds  : Fd_Set_Access;
312       Exceptfds : Fd_Set_Access;
313       Timeout   : Timeval_Access) return C.int;
314
315    function C_Send
316      (S     : C.int;
317       Msg   : System.Address;
318       Len   : C.int;
319       Flags : C.int) return C.int;
320
321    function C_Sendto
322      (S     : C.int;
323       Msg   : System.Address;
324       Len   : C.int;
325       Flags : C.int;
326       To    : Sockaddr_In_Access;
327       Tolen : C.int) return C.int;
328
329    function C_Setsockopt
330      (S       : C.int;
331       Level   : C.int;
332       Optname : C.int;
333       Optval  : System.Address;
334       Optlen  : C.int) return C.int;
335
336    function C_Shutdown
337      (S   : C.int;
338       How : C.int) return C.int;
339
340    function C_Socket
341      (Domain   : C.int;
342       Typ      : C.int;
343       Protocol : C.int) return C.int;
344
345    function C_Strerror
346      (Errnum : C.int) return C.Strings.chars_ptr;
347
348    function C_System
349      (Command : System.Address) return C.int;
350
351    function C_Writev
352      (Fd     : C.int;
353       Iov    : System.Address;
354       Iovcnt : C.int) return C.int;
355
356    -------------------------------------------------------
357    -- Signalling file descriptors for selector abortion --
358    -------------------------------------------------------
359
360    package Signalling_Fds is
361
362       function Create (Fds : not null access Fd_Pair) return C.int;
363       pragma Convention (C, Create);
364       --  Create a pair of connected descriptors suitable for use with C_Select
365       --  (used for signalling in Selector objects).
366
367       function Read (Rsig : C.int) return C.int;
368       pragma Convention (C, Read);
369       --  Read one byte of data from rsig, the read end of a pair of signalling
370       --  fds created by Create_Signalling_Fds.
371
372       function Write (Wsig : C.int) return C.int;
373       pragma Convention (C, Write);
374       --  Write one byte of data to wsig, the write end of a pair of signalling
375       --  fds created by Create_Signalling_Fds.
376
377       procedure Close (Sig : C.int);
378       pragma Convention (C, Close);
379       --  Close one end of a pair of signalling fds (ignoring any error)
380
381    end Signalling_Fds;
382
383    ----------------------------
384    -- Socket sets management --
385    ----------------------------
386
387    procedure Free_Socket_Set
388      (Set : Fd_Set_Access);
389    --  Free system-dependent socket set
390
391    procedure Get_Socket_From_Set
392      (Set    : Fd_Set_Access;
393       Socket : Int_Access;
394       Last   : Int_Access);
395    --  Get last socket in Socket and remove it from the socket set. The
396    --  parameter Last is a maximum value of the largest socket. This hint is
397    --  used to avoid scanning very large socket sets. After a call to
398    --  Get_Socket_From_Set, Last is set back to the real largest socket in the
399    --  socket set.
400
401    procedure Insert_Socket_In_Set
402      (Set    : Fd_Set_Access;
403       Socket : C.int);
404    --  Insert socket in the socket set
405
406    function  Is_Socket_In_Set
407      (Set    : Fd_Set_Access;
408       Socket : C.int) return C.int;
409    --  Check whether Socket is in the socket set, return a non-zero
410    --  value if it is, zero if it is not.
411
412    procedure Last_Socket_In_Set
413      (Set    : Fd_Set_Access;
414       Last   : Int_Access);
415    --  Find the largest socket in the socket set. This is needed for select().
416    --  When Last_Socket_In_Set is called, parameter Last is a maximum value of
417    --  the largest socket. This hint is used to avoid scanning very large
418    --  socket sets. After the call, Last is set back to the real largest socket
419    --  in the socket set.
420
421    function  New_Socket_Set
422      (Set : Fd_Set_Access) return Fd_Set_Access;
423    --  Allocate a new socket set which is a system-dependent structure and
424    --  initialize by copying Set if it is non-null, by making it empty
425    --  otherwise.
426
427    procedure Remove_Socket_From_Set
428      (Set    : Fd_Set_Access;
429       Socket : C.int);
430    --  Remove socket from the socket set
431
432    -------------------------------------------
433    -- Nonreentrant network databases access --
434    -------------------------------------------
435
436    --  The following are used only on systems that have nonreentrant
437    --  getXXXbyYYY functions, and do NOT have corresponding getXXXbyYYY_
438    --  functions. Currently, LynxOS is the only such system.
439
440    function Nonreentrant_Gethostbyname
441      (Name : C.char_array) return Hostent_Access;
442
443    function Nonreentrant_Gethostbyaddr
444      (Addr      : System.Address;
445       Addr_Len  : C.int;
446       Addr_Type : C.int) return Hostent_Access;
447
448    function Nonreentrant_Getservbyname
449      (Name  : C.char_array;
450       Proto : C.char_array) return Servent_Access;
451
452    function Nonreentrant_Getservbyport
453      (Port  : C.int;
454       Proto : C.char_array) return Servent_Access;
455
456    procedure Initialize;
457    procedure Finalize;
458
459 private
460    pragma Import (C, C_Bind, "bind");
461    pragma Import (C, C_Close, "close");
462    pragma Import (C, C_Gethostname, "gethostname");
463    pragma Import (C, C_Getpeername, "getpeername");
464    pragma Import (C, C_Getsockname, "getsockname");
465    pragma Import (C, C_Getsockopt, "getsockopt");
466    pragma Import (C, C_Inet_Addr, "inet_addr");
467    pragma Import (C, C_Listen, "listen");
468    pragma Import (C, C_Readv, "readv");
469    pragma Import (C, C_Select, "select");
470    pragma Import (C, C_Setsockopt, "setsockopt");
471    pragma Import (C, C_Shutdown, "shutdown");
472    pragma Import (C, C_Strerror, "strerror");
473    pragma Import (C, C_System, "system");
474    pragma Import (C, C_Writev, "writev");
475
476    pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
477    pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
478    pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
479    pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
480    pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
481    pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
482    pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
483
484    pragma Import (C, Nonreentrant_Gethostbyname, "gethostbyname");
485    pragma Import (C, Nonreentrant_Gethostbyaddr, "gethostbyaddr");
486    pragma Import (C, Nonreentrant_Getservbyname, "getservbyname");
487    pragma Import (C, Nonreentrant_Getservbyport, "getservbyport");
488
489 end GNAT.Sockets.Thin;