OSDN Git Service

2005-06-15 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socthi-vxworks.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-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
40 with Interfaces.C.Pointers;
41
42 with Ada.Unchecked_Conversion;
43 with Interfaces.C.Strings;
44 with GNAT.Sockets.Constants;
45 with GNAT.OS_Lib;
46
47 with System;
48
49 package 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_Readv
279      (Fd     : C.int;
280       Iov    : System.Address;
281       Iovcnt : C.int)
282       return   C.int;
283
284    function C_Recv
285      (S     : C.int;
286       Msg   : System.Address;
287       Len   : C.int;
288       Flags : C.int)
289       return  C.int;
290
291    function C_Recvfrom
292      (S       : C.int;
293       Msg     : System.Address;
294       Len     : C.int;
295       Flags   : C.int;
296       From    : Sockaddr_In_Access;
297       Fromlen : access C.int)
298       return    C.int;
299
300    function C_Select
301      (Nfds      : C.int;
302       Readfds   : Fd_Set_Access;
303       Writefds  : Fd_Set_Access;
304       Exceptfds : Fd_Set_Access;
305       Timeout   : Timeval_Access)
306       return      C.int;
307
308    function C_Send
309      (S     : C.int;
310       Msg   : System.Address;
311       Len   : C.int;
312       Flags : C.int)
313       return  C.int;
314
315    function C_Sendto
316      (S     : C.int;
317       Msg   : System.Address;
318       Len   : C.int;
319       Flags : C.int;
320       To    : Sockaddr_In_Access;
321       Tolen : C.int)
322       return  C.int;
323
324    function C_Setsockopt
325      (S       : C.int;
326       Level   : C.int;
327       Optname : C.int;
328       Optval  : System.Address;
329       Optlen  : C.int)
330       return    C.int;
331
332    function C_Shutdown
333      (S    : C.int;
334       How  : C.int)
335       return C.int;
336
337    function C_Socket
338      (Domain   : C.int;
339       Typ      : C.int;
340       Protocol : C.int)
341       return     C.int;
342
343    function C_Strerror
344      (Errnum : C.int)
345       return   C.Strings.chars_ptr;
346
347    function C_System
348      (Command : System.Address)
349       return    C.int;
350
351    function C_Writev
352      (Fd     : C.int;
353       Iov    : System.Address;
354       Iovcnt : C.int)
355       return   C.int;
356
357    procedure Free_Socket_Set
358      (Set    : Fd_Set_Access);
359    --  Free system-dependent socket set
360
361    procedure Get_Socket_From_Set
362      (Set    : Fd_Set_Access;
363       Socket : Int_Access;
364       Last   : Int_Access);
365    --  Get last socket in Socket and remove it from the socket
366    --  set. The parameter Last is a maximum value of the largest
367    --  socket. This hint is used to avoid scanning very large socket
368    --  sets. After a call to Get_Socket_From_Set, Last is set back to
369    --  the real largest socket in the socket set.
370
371    procedure Insert_Socket_In_Set
372      (Set    : Fd_Set_Access;
373       Socket : C.int);
374    --  Insert socket in the socket set
375
376    function  Is_Socket_In_Set
377      (Set    : Fd_Set_Access;
378       Socket : C.int) return C.int;
379    --  Check whether Socket is in the socket set, return a non-zero
380    --  value if it is, zero if it is not.
381
382    procedure Last_Socket_In_Set
383      (Set    : Fd_Set_Access;
384       Last   : Int_Access);
385    --  Find the largest socket in the socket set. This is needed for
386    --  select(). When Last_Socket_In_Set is called, parameter Last is
387    --  a maximum value of the largest socket. This hint is used to
388    --  avoid scanning very large socket sets. After the call, Last is
389    --  set back to the real largest socket in the socket set.
390
391    function  New_Socket_Set
392      (Set    : Fd_Set_Access)
393      return   Fd_Set_Access;
394    --  Allocate a new socket set which is a system-dependent structure
395    --  and initialize by copying Set if it is non-null, by making it
396    --  empty otherwise.
397
398    procedure Remove_Socket_From_Set
399      (Set    : Fd_Set_Access;
400       Socket : C.int);
401    --  Remove socket from the socket set
402
403    procedure Finalize;
404    procedure Initialize (Process_Blocking_IO : Boolean);
405
406 private
407
408    pragma Import (C, C_Bind, "bind");
409    pragma Import (C, C_Close, "close");
410    pragma Import (C, C_Gethostname, "gethostname");
411    pragma Import (C, C_Getpeername, "getpeername");
412    pragma Import (C, C_Getsockname, "getsockname");
413    pragma Import (C, C_Getsockopt, "getsockopt");
414    pragma Import (C, C_Inet_Addr, "inet_addr");
415    pragma Import (C, C_Listen, "listen");
416    pragma Import (C, C_Readv, "readv");
417    pragma Import (C, C_Select, "select");
418    pragma Import (C, C_Setsockopt, "setsockopt");
419    pragma Import (C, C_Shutdown, "shutdown");
420    pragma Import (C, C_Strerror, "strerror");
421    pragma Import (C, C_System, "system");
422    pragma Import (C, C_Writev, "writev");
423
424    pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
425    pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
426    pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
427    pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
428    pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
429    pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
430    pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
431
432 end GNAT.Sockets.Thin;