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