OSDN Git Service

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