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-2004 Ada Core Technologies, Inc.           --
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,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, 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
40with Interfaces.C.Pointers;
41
42with Interfaces.C.Strings;
43with GNAT.Sockets.Constants;
44with GNAT.OS_Lib;
45
46with System;
47
48package 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   function Socket_Error_Message (Errno : Integer) return C.Strings.chars_ptr;
64   --  Returns the error message string for the error number Errno. If
65   --  Errno is not known it returns "Unknown system error".
66
67   subtype Fd_Set_Access is System.Address;
68   No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
69
70   type Timeval_Unit is new C.int;
71   pragma Convention (C, Timeval_Unit);
72
73   type Timeval is record
74      Tv_Sec  : Timeval_Unit;
75      Tv_Usec : Timeval_Unit;
76   end record;
77   pragma Convention (C, Timeval);
78
79   type Timeval_Access is access all Timeval;
80   pragma Convention (C, Timeval_Access);
81
82   Immediat : constant Timeval := (0, 0);
83
84   type Int_Access is access all C.int;
85   pragma Convention (C, Int_Access);
86   --  Access to C integers
87
88   type Chars_Ptr_Array is array (C.size_t range <>) of
89     aliased C.Strings.chars_ptr;
90
91   package Chars_Ptr_Pointers is
92      new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
93                      C.Strings.Null_Ptr);
94   --  Arrays of C (char *)
95
96   type In_Addr is record
97      S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
98   end record;
99   pragma Convention (C, In_Addr);
100   --  Internet address
101
102   type In_Addr_Access is access all In_Addr;
103   pragma Convention (C, In_Addr_Access);
104   --  Access to internet address
105
106   Inaddr_Any : aliased constant In_Addr := (others => 0);
107   --  Any internet address (all the interfaces)
108
109   type In_Addr_Access_Array is array (C.size_t range <>)
110     of aliased In_Addr_Access;
111   pragma Convention (C, In_Addr_Access_Array);
112
113   package In_Addr_Access_Pointers is
114     new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
115   --  Array of internet addresses
116
117   type Sockaddr is record
118      Sa_Family : C.unsigned_short;
119      Sa_Data   : C.char_array (1 .. 14);
120   end record;
121   pragma Convention (C, Sockaddr);
122   --  Socket address
123
124   type Sockaddr_Access is access all Sockaddr;
125   pragma Convention (C, Sockaddr_Access);
126   --  Access to socket address
127
128   type Sockaddr_In is record
129      Sin_Family : C.unsigned_short      := Constants.AF_INET;
130      Sin_Port   : C.unsigned_short      := 0;
131      Sin_Addr   : In_Addr               := Inaddr_Any;
132      Sin_Zero   : C.char_array (1 .. 8) := (others => C.char'Val (0));
133   end record;
134   pragma Convention (C, Sockaddr_In);
135   --  Internet socket address
136
137   type Sockaddr_In_Access is access all Sockaddr_In;
138   pragma Convention (C, Sockaddr_In_Access);
139   --  Access to internet socket address
140
141   procedure Set_Length
142     (Sin : Sockaddr_In_Access;
143      Len : C.int);
144   pragma Inline (Set_Length);
145   --  Set Sin.Sin_Length to Len.
146   --  On this platform, nothing is done as there is no such field.
147
148   procedure Set_Family
149     (Sin    : Sockaddr_In_Access;
150      Family : C.int);
151   pragma Inline (Set_Family);
152   --  Set Sin.Sin_Family to Family
153
154   procedure Set_Port
155     (Sin     : Sockaddr_In_Access;
156      Port    : C.unsigned_short);
157   pragma Inline (Set_Port);
158   --  Set Sin.Sin_Port to Port
159
160   procedure Set_Address
161     (Sin     : Sockaddr_In_Access;
162      Address : In_Addr);
163   pragma Inline (Set_Address);
164   --  Set Sin.Sin_Addr to Address
165
166   type Hostent is record
167      H_Name      : C.Strings.chars_ptr;
168      H_Aliases   : Chars_Ptr_Pointers.Pointer;
169      H_Addrtype  : C.int;
170      H_Length    : C.int;
171      H_Addr_List : In_Addr_Access_Pointers.Pointer;
172   end record;
173   pragma Convention (C, Hostent);
174   --  Host entry
175
176   type Hostent_Access is access all Hostent;
177   pragma Convention (C, Hostent_Access);
178   --  Access to host entry
179
180   type Servent is record
181      S_Name    : C.Strings.chars_ptr;
182      S_Aliases : Chars_Ptr_Pointers.Pointer;
183      S_Port    : C.int;
184      S_Proto   : C.Strings.chars_ptr;
185   end record;
186   pragma Convention (C, Servent);
187   --  Service entry
188
189   type Servent_Access is access all Servent;
190   pragma Convention (C, Servent_Access);
191   --  Access to service entry
192
193   type Two_Int is array (0 .. 1) of C.int;
194   pragma Convention (C, Two_Int);
195   --  Used with pipe()
196
197   function C_Accept
198     (S       : C.int;
199      Addr    : System.Address;
200      Addrlen : access C.int)
201      return    C.int;
202
203   function C_Bind
204     (S       : C.int;
205      Name    : System.Address;
206      Namelen : C.int)
207      return    C.int;
208
209   function C_Close
210     (Fd   : C.int)
211      return C.int;
212
213   function C_Connect
214     (S       : C.int;
215      Name    : System.Address;
216      Namelen : C.int)
217      return    C.int;
218
219   function C_Gethostbyaddr
220     (Addr : System.Address;
221      Len  : C.int;
222      Typ  : C.int)
223      return Hostent_Access;
224
225   function C_Gethostbyname
226     (Name : C.char_array)
227      return Hostent_Access;
228
229   function C_Gethostname
230     (Name    : System.Address;
231      Namelen : C.int)
232      return    C.int;
233
234   function C_Getpeername
235     (S       : C.int;
236      Name    : System.Address;
237      Namelen : access C.int)
238      return    C.int;
239
240   function C_Getservbyname
241     (Name  : C.char_array;
242      Proto : C.char_array)
243      return Servent_Access;
244
245   function C_Getservbyport
246     (Port  : C.int;
247      Proto : C.char_array)
248      return Servent_Access;
249
250   function C_Getsockname
251     (S       : C.int;
252      Name    : System.Address;
253      Namelen : access C.int)
254      return    C.int;
255
256   function C_Getsockopt
257     (S       : C.int;
258      Level   : C.int;
259      Optname : C.int;
260      Optval  : System.Address;
261      Optlen  : access C.int)
262      return    C.int;
263
264   function C_Inet_Addr
265     (Cp   : C.Strings.chars_ptr)
266      return C.int;
267
268   function C_Ioctl
269     (S    : C.int;
270      Req  : C.int;
271      Arg  : Int_Access)
272      return C.int;
273
274   function C_Listen (S, Backlog : C.int) return C.int;
275
276   function C_Read
277     (Fd    : C.int;
278      Buf   : System.Address;
279      Count : C.int)
280      return  C.int;
281
282   function C_Readv
283     (Fd     : C.int;
284      Iov    : System.Address;
285      Iovcnt : C.int)
286      return   C.int;
287
288   function C_Recv
289     (S     : C.int;
290      Msg   : System.Address;
291      Len   : C.int;
292      Flags : C.int)
293      return  C.int;
294
295   function C_Recvfrom
296     (S       : C.int;
297      Msg     : System.Address;
298      Len     : C.int;
299      Flags   : C.int;
300      From    : Sockaddr_In_Access;
301      Fromlen : access C.int)
302      return    C.int;
303
304   function C_Select
305     (Nfds      : C.int;
306      Readfds   : Fd_Set_Access;
307      Writefds  : Fd_Set_Access;
308      Exceptfds : Fd_Set_Access;
309      Timeout   : Timeval_Access)
310      return      C.int;
311
312   function C_Send
313     (S     : C.int;
314      Msg   : System.Address;
315      Len   : C.int;
316      Flags : C.int)
317      return  C.int;
318
319   function C_Sendto
320     (S     : C.int;
321      Msg   : System.Address;
322      Len   : C.int;
323      Flags : C.int;
324      To    : Sockaddr_In_Access;
325      Tolen : C.int)
326      return  C.int;
327
328   function C_Setsockopt
329     (S       : C.int;
330      Level   : C.int;
331      Optname : C.int;
332      Optval  : System.Address;
333      Optlen  : C.int)
334      return    C.int;
335
336   function C_Shutdown
337     (S    : C.int;
338      How  : C.int)
339      return C.int;
340
341   function C_Socket
342     (Domain   : C.int;
343      Typ      : C.int;
344      Protocol : C.int)
345      return     C.int;
346
347   function C_Strerror
348     (Errnum : C.int)
349      return   C.Strings.chars_ptr;
350
351   function C_System
352     (Command : System.Address)
353      return    C.int;
354
355   function C_Write
356     (Fd    : C.int;
357      Buf   : System.Address;
358      Count : C.int)
359      return  C.int;
360
361   function C_Writev
362     (Fd     : C.int;
363      Iov    : System.Address;
364      Iovcnt : C.int)
365      return   C.int;
366
367   procedure Free_Socket_Set
368     (Set    : Fd_Set_Access);
369   --  Free system-dependent socket set.
370
371   procedure Get_Socket_From_Set
372     (Set    : Fd_Set_Access;
373      Socket : Int_Access;
374      Last   : Int_Access);
375   --  Get last socket in Socket and remove it from the socket
376   --  set. The parameter Last is a maximum value of the largest
377   --  socket. This hint is used to avoid scanning very large socket
378   --  sets. After a call to Get_Socket_From_Set, Last is set back to
379   --  the real largest socket in the socket set.
380
381   procedure Insert_Socket_In_Set
382     (Set    : Fd_Set_Access;
383      Socket : C.int);
384   --  Insert socket in the socket set.
385
386   function  Is_Socket_In_Set
387     (Set    : Fd_Set_Access;
388      Socket : C.int)
389     return Boolean;
390   --  Check whether Socket is in the socket set.
391
392   procedure Last_Socket_In_Set
393     (Set    : Fd_Set_Access;
394      Last   : Int_Access);
395   --  Find the largest socket in the socket set. This is needed for
396   --  select(). When Last_Socket_In_Set is called, parameter Last is
397   --  a maximum value of the largest socket. This hint is used to
398   --  avoid scanning very large socket sets. After the call, Last is
399   --  set back to the real largest socket in the socket set.
400
401   function  New_Socket_Set
402     (Set    : Fd_Set_Access)
403     return   Fd_Set_Access;
404   --  Allocate a new socket set which is a system-dependent structure
405   --  and initialize by copying Set if it is non-null, by making it
406   --  empty otherwise.
407
408   procedure Remove_Socket_From_Set
409     (Set    : Fd_Set_Access;
410      Socket : C.int);
411   --  Remove socket from the socket set.
412
413   procedure Finalize;
414   procedure Initialize (Process_Blocking_IO : Boolean);
415
416private
417
418   pragma Import (C, C_Bind,          "DECC$BIND");
419   pragma Import (C, C_Close,         "DECC$CLOSE");
420   pragma Import (C, C_Gethostbyaddr, "DECC$GETHOSTBYADDR");
421   pragma Import (C, C_Gethostbyname, "DECC$GETHOSTBYNAME");
422   pragma Import (C, C_Gethostname,   "DECC$GETHOSTNAME");
423   pragma Import (C, C_Getpeername,   "DECC$GETPEERNAME");
424   pragma Import (C, C_Getservbyname, "DECC$GETSERVBYNAME");
425   pragma Import (C, C_Getservbyport, "DECC$GETSERVBYPORT");
426   pragma Import (C, C_Getsockname,   "DECC$GETSOCKNAME");
427   pragma Import (C, C_Getsockopt,    "DECC$GETSOCKOPT");
428   pragma Import (C, C_Inet_Addr,     "DECC$INET_ADDR");
429   pragma Import (C, C_Listen,        "DECC$LISTEN");
430   pragma Import (C, C_Read,          "DECC$READ");
431   pragma Import (C, C_Select,        "DECC$SELECT");
432   pragma Import (C, C_Setsockopt,    "DECC$SETSOCKOPT");
433   pragma Import (C, C_Shutdown,      "DECC$SHUTDOWN");
434   pragma Import (C, C_Strerror,      "DECC$STRERROR");
435   pragma Import (C, C_System,        "DECC$SYSTEM");
436   pragma Import (C, C_Write,         "DECC$WRITE");
437
438   pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
439   pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
440   pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
441   pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
442   pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
443   pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
444   pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
445end GNAT.Sockets.Thin;
446