OSDN Git Service

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