OSDN Git Service

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