OSDN Git Service

* 1aexcept.adb, 1aexcept.ads, 1ic.ads, 1ssecsta.adb,
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socthi.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) 2001 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
30 --                                                                          --
31 ------------------------------------------------------------------------------
32
33 with Interfaces.C.Pointers;
34
35 with Interfaces.C.Strings;
36 with GNAT.Sockets.Constants;
37 with GNAT.OS_Lib;
38
39 with System;
40
41 package GNAT.Sockets.Thin is
42
43    --  ??? more comments needed ???
44
45    --  This package is intended for hosts implementing BSD sockets with a
46    --  standard interface. It will be used as a default for all the platforms
47    --  that do not have a specific version of this file.
48
49    package C renames Interfaces.C;
50
51    use type C.int;
52    --  This is so we can declare the Failure constant below
53
54    Success : constant C.int :=  0;
55    Failure : constant C.int := -1;
56
57    function Socket_Errno return Integer renames GNAT.OS_Lib.Errno;
58    --  Returns last socket error number.
59
60    function Socket_Error_Message (Errno : Integer) return String;
61    --  Returns the error message string for the error number Errno. If
62    --  Errno is not known it returns "Unknown system error".
63
64    type Fd_Set is mod 2 ** 32;
65    pragma Convention (C, Fd_Set);
66
67    Null_Fd_Set : constant Fd_Set := 0;
68
69    type Fd_Set_Access is access all Fd_Set;
70    pragma Convention (C, Fd_Set_Access);
71
72    type Timeval_Unit is new C.int;
73    pragma Convention (C, Timeval_Unit);
74
75    type Timeval is record
76       Tv_Sec  : Timeval_Unit;
77       Tv_Usec : Timeval_Unit;
78    end record;
79    pragma Convention (C, Timeval);
80
81    type Timeval_Access is access all Timeval;
82    pragma Convention (C, Timeval_Access);
83
84    Immediat : constant Timeval := (0, 0);
85
86    type Int_Access is access all C.int;
87    pragma Convention (C, Int_Access);
88    --  Access to C integers
89
90    type Chars_Ptr_Array is array (C.size_t range <>) of
91      aliased C.Strings.chars_ptr;
92
93    package Chars_Ptr_Pointers is
94       new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
95                       C.Strings.Null_Ptr);
96    --  Arrays of C (char *)
97
98    type In_Addr is record
99       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
100    end record;
101    pragma Convention (C, In_Addr);
102    --  Internet address
103
104    type In_Addr_Access is access all In_Addr;
105    pragma Convention (C, In_Addr_Access);
106    --  Access to internet address
107
108    Inaddr_Any : aliased constant In_Addr := (others => 0);
109    --  Any internet address (all the interfaces)
110
111    type In_Addr_Access_Array is array (C.size_t range <>)
112      of aliased In_Addr_Access;
113    pragma Convention (C, In_Addr_Access_Array);
114
115    package In_Addr_Access_Pointers is
116      new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
117    --  Array of internet addresses
118
119    type Sockaddr is record
120       Sa_Family : C.unsigned_short;
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_Family : C.unsigned_short      := Constants.AF_INET;
132       Sin_Port   : C.unsigned_short      := 0;
133       Sin_Addr   : In_Addr               := Inaddr_Any;
134       Sin_Zero   : C.char_array (1 .. 8) := (others => C.char'Val (0));
135    end record;
136    pragma Convention (C, Sockaddr_In);
137    --  Internet socket address
138
139    type Sockaddr_In_Access is access all Sockaddr_In;
140    pragma Convention (C, Sockaddr_In_Access);
141    --  Access to internet socket address
142
143    type Hostent is record
144       H_Name      : C.Strings.chars_ptr;
145       H_Aliases   : Chars_Ptr_Pointers.Pointer;
146       H_Addrtype  : C.int;
147       H_Length    : C.int;
148       H_Addr_List : In_Addr_Access_Pointers.Pointer;
149    end record;
150    pragma Convention (C, Hostent);
151    --  Host entry
152
153    type Hostent_Access is access all Hostent;
154    pragma Convention (C, Hostent_Access);
155    --  Access to host entry
156
157    type Two_Int is array (0 .. 1) of C.int;
158    pragma Convention (C, Two_Int);
159    --  Used with pipe()
160
161    function C_Accept
162      (S       : C.int;
163       Addr    : System.Address;
164       Addrlen : access C.int)
165       return    C.int;
166
167    function C_Bind
168      (S       : C.int;
169       Name    : System.Address;
170       Namelen : C.int)
171       return    C.int;
172
173    function C_Close
174      (Fd   : C.int)
175       return C.int;
176
177    function C_Connect
178      (S       : C.int;
179       Name    : System.Address;
180       Namelen : C.int)
181       return    C.int;
182
183    function C_Gethostbyaddr
184      (Addr : System.Address;
185       Len  : C.int;
186       Typ  : C.int)
187       return Hostent_Access;
188
189    function C_Gethostbyname
190      (Name : C.char_array)
191       return Hostent_Access;
192
193    function C_Gethostname
194      (Name    : System.Address;
195       Namelen : C.int)
196       return    C.int;
197
198    function C_Getpeername
199      (S       : C.int;
200       Name    : System.Address;
201       Namelen : access C.int)
202       return    C.int;
203
204    function C_Getsockname
205      (S       : C.int;
206       Name    : System.Address;
207       Namelen : access C.int)
208       return    C.int;
209
210    function C_Getsockopt
211      (S       : C.int;
212       Level   : C.int;
213       Optname : C.int;
214       Optval  : System.Address;
215       Optlen  : access C.int)
216       return    C.int;
217
218    function C_Inet_Addr
219      (Cp   : C.Strings.chars_ptr)
220       return C.int;
221
222    function C_Ioctl
223      (S    : C.int;
224       Req  : C.int;
225       Arg  : Int_Access)
226       return C.int;
227
228    function C_Listen (S, Backlog : C.int) return C.int;
229
230    function C_Read
231      (Fd    : C.int;
232       Buf   : System.Address;
233       Count : C.int)
234       return  C.int;
235
236    function C_Recv
237      (S     : C.int;
238       Msg   : System.Address;
239       Len   : C.int;
240       Flags : C.int)
241       return  C.int;
242
243    function C_Recvfrom
244      (S       : C.int;
245       Msg     : System.Address;
246       Len     : C.int;
247       Flags   : C.int;
248       From    : Sockaddr_In_Access;
249       Fromlen : access C.int)
250       return    C.int;
251
252    function C_Select
253      (Nfds      : C.int;
254       Readfds   : Fd_Set_Access;
255       Writefds  : Fd_Set_Access;
256       Exceptfds : Fd_Set_Access;
257       Timeout   : Timeval_Access)
258       return      C.int;
259
260    function C_Send
261      (S     : C.int;
262       Msg   : System.Address;
263       Len   : C.int;
264       Flags : C.int)
265       return  C.int;
266
267    function C_Sendto
268      (S     : C.int;
269       Msg   : System.Address;
270       Len   : C.int;
271       Flags : C.int;
272       To    : Sockaddr_In_Access;
273       Tolen : C.int)
274       return  C.int;
275
276    function C_Setsockopt
277      (S       : C.int;
278       Level   : C.int;
279       Optname : C.int;
280       Optval  : System.Address;
281       Optlen  : C.int)
282       return    C.int;
283
284    function C_Shutdown
285      (S    : C.int;
286       How  : C.int)
287       return C.int;
288
289    function C_Socket
290      (Domain   : C.int;
291       Typ      : C.int;
292       Protocol : C.int)
293       return     C.int;
294
295    function C_Strerror
296      (Errnum : C.int)
297       return   C.Strings.chars_ptr;
298
299    function C_System
300      (Command : System.Address)
301       return    C.int;
302
303    function C_Write
304      (Fd    : C.int;
305       Buf   : System.Address;
306       Count : C.int)
307       return  C.int;
308
309    --  Return highest numbered socket (what does this refer to???)
310
311    procedure Clear    (Item : in out Fd_Set; Socket : in C.int);
312    procedure Empty    (Item : in out Fd_Set);
313    function  Is_Empty (Item : Fd_Set) return Boolean;
314    function  Is_Set   (Item : Fd_Set; Socket : C.int) return Boolean;
315    function  Max      (Item : Fd_Set) return C.int;
316    procedure Set      (Item : in out Fd_Set; Socket : in C.int);
317
318    procedure Finalize;
319    procedure Initialize (Process_Blocking_IO : Boolean);
320
321 private
322
323    pragma Import (C, C_Bind, "bind");
324    pragma Import (C, C_Close, "close");
325    pragma Import (C, C_Gethostbyaddr, "gethostbyaddr");
326    pragma Import (C, C_Gethostbyname, "gethostbyname");
327    pragma Import (C, C_Gethostname, "gethostname");
328    pragma Import (C, C_Getpeername, "getpeername");
329    pragma Import (C, C_Getsockname, "getsockname");
330    pragma Import (C, C_Getsockopt, "getsockopt");
331    pragma Import (C, C_Inet_Addr, "inet_addr");
332    pragma Import (C, C_Listen, "listen");
333    pragma Import (C, C_Read, "read");
334    pragma Import (C, C_Select, "select");
335    pragma Import (C, C_Setsockopt, "setsockopt");
336    pragma Import (C, C_Shutdown, "shutdown");
337    pragma Import (C, C_Strerror, "strerror");
338    pragma Import (C, C_System, "system");
339    pragma Import (C, C_Write, "write");
340
341 end GNAT.Sockets.Thin;