OSDN Git Service

2007-04-06 Thomas Quinot <quinot@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-2006, 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.Sockets.Constants;
44 with GNAT.OS_Lib;
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 it 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    subtype Fd_Set_Access is System.Address;
75    No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
76
77    type time_t is
78      range -2 ** (8 * Constants.SIZEOF_tv_sec - 1)
79          .. 2 ** (8 * Constants.SIZEOF_tv_sec - 1) - 1;
80    for time_t'Size use 8 * Constants.SIZEOF_tv_sec;
81    pragma Convention (C, time_t);
82
83    type suseconds_t is
84      range -2 ** (8 * Constants.SIZEOF_tv_usec - 1)
85          .. 2 ** (8 * Constants.SIZEOF_tv_usec - 1) - 1;
86    for suseconds_t'Size use 8 * Constants.SIZEOF_tv_usec;
87    pragma Convention (C, suseconds_t);
88
89    type Timeval is record
90       Tv_Sec  : time_t;
91       Tv_Usec : suseconds_t;
92    end record;
93    pragma Convention (C, Timeval);
94
95    type Timeval_Access is access all Timeval;
96    pragma Convention (C, Timeval_Access);
97
98    Immediat : constant Timeval := (0, 0);
99
100    type Int_Access is access all C.int;
101    pragma Convention (C, Int_Access);
102    --  Access to C integers
103
104    type Chars_Ptr_Array is array (C.size_t range <>) of
105      aliased C.Strings.chars_ptr;
106
107    package Chars_Ptr_Pointers is
108       new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
109                       C.Strings.Null_Ptr);
110    --  Arrays of C (char *)
111
112    type In_Addr is record
113       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
114    end record;
115    pragma Convention (C, In_Addr);
116    --  Internet address
117
118    type In_Addr_Access is access all In_Addr;
119    pragma Convention (C, In_Addr_Access);
120    --  Access to internet address
121
122    Inaddr_Any : aliased constant In_Addr := (others => 0);
123    --  Any internet address (all the interfaces)
124
125    type In_Addr_Access_Array is array (C.size_t range <>)
126      of aliased In_Addr_Access;
127    pragma Convention (C, In_Addr_Access_Array);
128
129    package In_Addr_Access_Pointers is
130      new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
131    --  Array of internet addresses
132
133    type Sockaddr is record
134       Sa_Family : C.unsigned_short;
135       Sa_Data   : C.char_array (1 .. 14);
136    end record;
137    pragma Convention (C, Sockaddr);
138    --  Socket address
139
140    type Sockaddr_Access is access all Sockaddr;
141    pragma Convention (C, Sockaddr_Access);
142    --  Access to socket address
143
144    type Sockaddr_In is record
145       Sin_Family : C.unsigned_short      := Constants.AF_INET;
146       Sin_Port   : C.unsigned_short      := 0;
147       Sin_Addr   : In_Addr               := Inaddr_Any;
148       Sin_Zero   : C.char_array (1 .. 8) := (others => C.char'Val (0));
149    end record;
150    pragma Convention (C, Sockaddr_In);
151    --  Internet socket address
152
153    type Sockaddr_In_Access is access all Sockaddr_In;
154    pragma Convention (C, Sockaddr_In_Access);
155    --  Access to internet socket address
156
157    procedure Set_Length
158      (Sin : Sockaddr_In_Access;
159       Len : C.int);
160    pragma Inline (Set_Length);
161    --  Set Sin.Sin_Length to Len.
162    --  On this platform, nothing is done as there is no such field.
163
164    procedure Set_Family
165      (Sin    : Sockaddr_In_Access;
166       Family : C.int);
167    pragma Inline (Set_Family);
168    --  Set Sin.Sin_Family to Family
169
170    procedure Set_Port
171      (Sin  : Sockaddr_In_Access;
172       Port : C.unsigned_short);
173    pragma Inline (Set_Port);
174    --  Set Sin.Sin_Port to Port
175
176    procedure Set_Address
177      (Sin     : Sockaddr_In_Access;
178       Address : In_Addr);
179    pragma Inline (Set_Address);
180    --  Set Sin.Sin_Addr to Address
181
182    type Hostent is record
183       H_Name      : C.Strings.chars_ptr;
184       H_Aliases   : Chars_Ptr_Pointers.Pointer;
185       H_Addrtype  : C.int;
186       H_Length    : C.int;
187       H_Addr_List : In_Addr_Access_Pointers.Pointer;
188    end record;
189    pragma Convention (C, Hostent);
190    --  Host entry
191
192    type Hostent_Access is access all Hostent;
193    pragma Convention (C, Hostent_Access);
194    --  Access to host entry
195
196    type Servent is record
197       S_Name    : C.Strings.chars_ptr;
198       S_Aliases : Chars_Ptr_Pointers.Pointer;
199       S_Port    : C.int;
200       S_Proto   : C.Strings.chars_ptr;
201    end record;
202    pragma Convention (C, Servent);
203    --  Service entry
204
205    type Servent_Access is access all Servent;
206    pragma Convention (C, Servent_Access);
207    --  Access to service entry
208
209    type Two_Ints is array (0 .. 1) of C.int;
210    pragma Convention (C, Two_Ints);
211    --  Container for two int values
212
213    subtype Fd_Pair is Two_Ints;
214    --  Two_Ints as used for Create_Signalling_Fds: a pair of connected file
215    --  descriptors, one of which (the "read end" of the connection) being used
216    --  for reading, the other one (the "write end") being used for writing.
217
218    Read_End  : constant := 0;
219    Write_End : constant := 1;
220    --  Indices into an Fd_Pair value providing access to each of the connected
221    --  file descriptors.
222
223    function C_Accept
224      (S       : C.int;
225       Addr    : System.Address;
226       Addrlen : not null access C.int) return C.int;
227
228    function C_Bind
229      (S       : C.int;
230       Name    : System.Address;
231       Namelen : C.int) return C.int;
232
233    function C_Close
234      (Fd : C.int) return C.int;
235
236    function C_Connect
237      (S       : C.int;
238       Name    : System.Address;
239       Namelen : C.int) return C.int;
240
241    function C_Gethostbyaddr
242      (Addr : System.Address;
243       Len  : C.int;
244       Typ  : C.int) return Hostent_Access;
245
246    function C_Gethostbyname
247      (Name : C.char_array) return Hostent_Access;
248
249    function C_Gethostname
250      (Name    : System.Address;
251       Namelen : C.int) return C.int;
252
253    function C_Getpeername
254      (S       : C.int;
255       Name    : System.Address;
256       Namelen : not null access C.int) return C.int;
257
258    function C_Getservbyname
259      (Name  : C.char_array;
260       Proto : C.char_array) return Servent_Access;
261
262    function C_Getservbyport
263      (Port  : C.int;
264       Proto : C.char_array) return Servent_Access;
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    package Signalling_Fds is
358
359       function Create (Fds : not null access Fd_Pair) return C.int;
360       pragma Convention (C, Create);
361       --  Create a pair of connected descriptors suitable for use with C_Select
362       --  (used for signalling in Selector objects).
363
364       function Read (Rsig : C.int) return C.int;
365       pragma Convention (C, Read);
366       --  Read one byte of data from rsig, the read end of a pair of signalling
367       --  fds created by Create_Signalling_Fds.
368
369       function Write (Wsig : C.int) return C.int;
370       pragma Convention (C, Write);
371       --  Write one byte of data to wsig, the write end of a pair of signalling
372       --  fds created by Create_Signalling_Fds.
373
374    end Signalling_Fds;
375
376    procedure Free_Socket_Set
377      (Set : Fd_Set_Access);
378    --  Free system-dependent socket set
379
380    procedure Get_Socket_From_Set
381      (Set    : Fd_Set_Access;
382       Socket : Int_Access;
383       Last   : Int_Access);
384    --  Get last socket in Socket and remove it from the socket
385    --  set. The parameter Last is a maximum value of the largest
386    --  socket. This hint is used to avoid scanning very large socket
387    --  sets. After a call to Get_Socket_From_Set, Last is set back to
388    --  the real largest socket in the socket set.
389
390    procedure Insert_Socket_In_Set
391      (Set    : Fd_Set_Access;
392       Socket : C.int);
393    --  Insert socket in the socket set
394
395    function  Is_Socket_In_Set
396      (Set    : Fd_Set_Access;
397       Socket : C.int) return C.int;
398    --  Check whether Socket is in the socket set, return a non-zero
399    --  value if it is, zero if it is not.
400
401    procedure Last_Socket_In_Set
402      (Set    : Fd_Set_Access;
403       Last   : Int_Access);
404    --  Find the largest socket in the socket set. This is needed for select().
405    --  When Last_Socket_In_Set is called, parameter Last is a maximum value of
406    --  the largest socket. This hint is used to avoid scanning very large
407    --  socket sets. After the call, Last is set back to the real largest socket
408    --  in the socket set.
409
410    function  New_Socket_Set
411      (Set : Fd_Set_Access) return Fd_Set_Access;
412    --  Allocate a new socket set which is a system-dependent structure and
413    --  initialize by copying Set if it is non-null, by making it empty
414    --  otherwise.
415
416    procedure Remove_Socket_From_Set
417      (Set    : Fd_Set_Access;
418       Socket : C.int);
419    --  Remove socket from the socket set
420
421    procedure Finalize;
422    procedure Initialize (Process_Blocking_IO : Boolean);
423
424 private
425
426    pragma Import (C, C_Bind,          "DECC$BIND");
427    pragma Import (C, C_Close,         "DECC$CLOSE");
428    pragma Import (C, C_Gethostbyaddr, "DECC$GETHOSTBYADDR");
429    pragma Import (C, C_Gethostbyname, "DECC$GETHOSTBYNAME");
430    pragma Import (C, C_Gethostname,   "DECC$GETHOSTNAME");
431    pragma Import (C, C_Getpeername,   "DECC$GETPEERNAME");
432    pragma Import (C, C_Getservbyname, "DECC$GETSERVBYNAME");
433    pragma Import (C, C_Getservbyport, "DECC$GETSERVBYPORT");
434    pragma Import (C, C_Getsockname,   "DECC$GETSOCKNAME");
435    pragma Import (C, C_Getsockopt,    "DECC$GETSOCKOPT");
436    pragma Import (C, C_Inet_Addr,     "DECC$INET_ADDR");
437    pragma Import (C, C_Listen,        "DECC$LISTEN");
438    pragma Import (C, C_Select,        "DECC$SELECT");
439    pragma Import (C, C_Setsockopt,    "DECC$SETSOCKOPT");
440    pragma Import (C, C_Shutdown,      "DECC$SHUTDOWN");
441    pragma Import (C, C_Strerror,      "DECC$STRERROR");
442    pragma Import (C, C_System,        "DECC$SYSTEM");
443
444    pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
445    pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
446    pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
447    pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
448    pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
449    pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
450    pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
451
452 end GNAT.Sockets.Thin;