OSDN Git Service

* 41intnam.ads, 42intnam.ads, 4aintnam.ads, 4cintnam.ads,
[pf3gnuchains/gcc-fork.git] / gcc / ada / 3wsocthi.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 --                            $Revision: 1.16 $
10 --                                                                          --
11 --              Copyright (C) 2001 Ada Core Technologies, Inc.              --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This version is for NT.
36
37 with Interfaces.C.Pointers;
38 with Interfaces.C.Strings;
39
40 with GNAT.Sockets.Constants;
41
42 with System;
43
44 package GNAT.Sockets.Thin is
45
46    --  ??? far more comments required ???
47
48    package C renames Interfaces.C;
49
50    use type C.int;
51    --  So that we can declare the Failure constant below.
52
53    Success : constant C.int :=  0;
54    Failure : constant C.int := -1;
55
56    function Socket_Errno return Integer;
57    --  Returns last socket error number.
58
59    function Socket_Error_Message (Errno : Integer) return String;
60    --  Returns the error message string for the error number Errno. If
61    --  Errno is not known it returns "Unknown system error".
62
63    type Socket_Fd_Array is array (C.unsigned range 1 .. 64) of C.int;
64    pragma Convention (C, Socket_Fd_Array);
65
66    type Fd_Set is record
67       fd_count : C.unsigned;
68       fd_array : Socket_Fd_Array;
69    end record;
70    pragma Convention (C, Fd_Set);
71
72    Null_Fd_Set : constant Fd_Set := (0, (others => 0));
73
74    type Fd_Set_Access is access all Fd_Set;
75    pragma Convention (C, Fd_Set_Access);
76
77    type Timeval_Unit is new C.long;
78    pragma Convention (C, Timeval_Unit);
79
80    type Timeval is record
81       Tv_Sec  : Timeval_Unit;
82       Tv_Usec : Timeval_Unit;
83    end record;
84    pragma Convention (C, Timeval);
85
86    type Timeval_Access is access all Timeval;
87    pragma Convention (C, Timeval_Access);
88
89    Immediat : constant Timeval := (0, 0);
90
91    type Int_Access is access all C.int;
92    pragma Convention (C, Int_Access);
93    --  Access to C integers
94
95    type Chars_Ptr_Array is array (C.size_t range <>) of
96      aliased C.Strings.chars_ptr;
97
98    package Chars_Ptr_Pointers is
99       new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
100                     C.Strings.Null_Ptr);
101    --  Arrays of C (char *)
102
103    type In_Addr is record
104       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
105    end record;
106    pragma Convention (C, In_Addr);
107    --  Internet address
108
109    type In_Addr_Access is access all In_Addr;
110    pragma Convention (C, In_Addr_Access);
111    --  Access to internet address
112
113    Inaddr_Any : aliased constant In_Addr := (others => 0);
114    --  Any internet address (all the interfaces)
115
116    type In_Addr_Access_Array is array (C.size_t range <>)
117      of aliased In_Addr_Access;
118    pragma Convention (C, In_Addr_Access_Array);
119    package In_Addr_Access_Pointers is
120      new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
121    --  Array of internet addresses
122
123    type Sockaddr is record
124       Sa_Family : C.unsigned_short;
125       Sa_Data   : C.char_array (1 .. 14);
126    end record;
127    pragma Convention (C, Sockaddr);
128    --  Socket address
129
130    type Sockaddr_Access is access all Sockaddr;
131    pragma Convention (C, Sockaddr_Access);
132    --  Access to socket address
133
134    type Sockaddr_In is record
135       Sin_Family : C.unsigned_short      := Constants.AF_INET;
136       Sin_Port   : C.unsigned_short      := 0;
137       Sin_Addr   : In_Addr               := Inaddr_Any;
138       Sin_Zero   : C.char_array (1 .. 8) := (others => C.char'Val (0));
139    end record;
140    pragma Convention (C, Sockaddr_In);
141    --  Internet socket address
142
143    type Sockaddr_In_Access is access all Sockaddr_In;
144    pragma Convention (C, Sockaddr_In_Access);
145    --  Access to internet socket address
146
147    type Hostent is record
148       H_Name      : C.Strings.chars_ptr;
149       H_Aliases   : Chars_Ptr_Pointers.Pointer;
150       H_Addrtype  : C.short;
151       H_Length    : C.short;
152       H_Addr_List : In_Addr_Access_Pointers.Pointer;
153    end record;
154    pragma Convention (C, Hostent);
155    --  Host entry
156
157    type Hostent_Access is access all Hostent;
158    pragma Convention (C, Hostent_Access);
159    --  Access to host entry
160
161    type Two_Int is array (0 .. 1) of C.int;
162    pragma Convention (C, Two_Int);
163    --  Used with pipe()
164
165    function C_Accept
166      (S       : C.int;
167       Addr    : System.Address;
168       Addrlen : access C.int)
169       return    C.int;
170
171    function C_Bind
172      (S       : C.int;
173       Name    : System.Address;
174       Namelen : C.int)
175       return    C.int;
176
177    function C_Close
178      (Fd  : C.int)
179      return C.int;
180
181    function C_Connect
182      (S       : C.int;
183       Name    : System.Address;
184       Namelen : C.int)
185       return    C.int;
186
187    function C_Gethostbyaddr
188      (Addr     : System.Address;
189       Length   : C.int;
190       Typ      : C.int)
191       return     Hostent_Access;
192
193    function C_Gethostbyname
194      (Name : C.char_array)
195       return Hostent_Access;
196
197    function C_Gethostname
198      (Name    : System.Address;
199       Namelen : C.int)
200       return    C.int;
201
202    function C_Getpeername
203      (S       : C.int;
204       Name    : System.Address;
205       Namelen : access C.int)
206       return    C.int;
207
208    function C_Getsockname
209      (S       : C.int;
210       Name    : System.Address;
211       Namelen : access C.int)
212       return    C.int;
213
214    function C_Getsockopt
215      (S       : C.int;
216       Level   : C.int;
217       Optname : C.int;
218       Optval  : System.Address;
219       Optlen  : access C.int)
220       return    C.int;
221
222    function C_Inet_Addr
223      (Cp   : C.Strings.chars_ptr)
224       return C.int;
225
226    function C_Ioctl
227      (S    : C.int;
228       Req  : C.int;
229       Arg  : Int_Access)
230       return C.int;
231
232    function C_Listen
233      (S, Backlog : C.int)
234       return       C.int;
235
236    function C_Read
237      (Fildes : C.int;
238       Buf    : System.Address;
239       Nbyte  : C.int)
240       return   C.int;
241
242    function C_Recv
243      (S     : C.int;
244       Buf   : System.Address;
245       Len   : C.int;
246       Flags : C.int)
247       return  C.int;
248
249    function C_Recvfrom
250      (S       : C.int;
251       Buf     : System.Address;
252       Len     : C.int;
253       Flags   : C.int;
254       From    : Sockaddr_In_Access;
255       Fromlen : access C.int)
256       return    C.int;
257
258    function C_Select
259      (Nfds      : C.int;
260       Readfds   : Fd_Set_Access;
261       Writefds  : Fd_Set_Access;
262       Exceptfds : Fd_Set_Access;
263       Timeout   : Timeval_Access)
264       return      C.int;
265
266    function C_Send
267      (S     : C.int;
268       Buf   : System.Address;
269       Len   : C.int;
270       Flags : C.int)
271       return  C.int;
272
273    function C_Sendto
274      (S     : C.int;
275       Msg   : System.Address;
276       Len   : C.int;
277       Flags : C.int;
278       To    : Sockaddr_In_Access;
279       Tolen : C.int)
280       return  C.int;
281
282    function C_Setsockopt
283      (S       : C.int;
284       Level   : C.int;
285       Optname : C.int;
286       Optval  : System.Address;
287       Optlen  : C.int)
288       return    C.int;
289
290    function C_Shutdown
291      (S    : C.int;
292       How  : C.int)
293       return C.int;
294
295    function C_Socket
296      (Domain   : C.int;
297       Typ      : C.int;
298       Protocol : C.int)
299       return     C.int;
300
301    function C_Strerror
302      (Errnum : C.int)
303       return   C.Strings.chars_ptr;
304
305    function C_System
306      (Command : System.Address)
307       return    C.int;
308
309    function C_Write
310      (Fildes : C.int;
311       Buf    : System.Address;
312       Nbyte  : C.int)
313       return   C.int;
314
315    function WSAStartup
316      (WS_Version     : Interfaces.C.int;
317       WSADataAddress : System.Address)
318       return           Interfaces.C.int;
319
320    procedure WSACleanup;
321
322    procedure Clear    (Item : in out Fd_Set; Socket : in C.int);
323    procedure Empty    (Item : in out Fd_Set);
324    function  Is_Empty (Item : Fd_Set) return Boolean;
325    function  Is_Set   (Item : Fd_Set; Socket : C.int) return Boolean;
326    function  Max      (Item : Fd_Set) return C.int;
327    procedure Set      (Item : in out Fd_Set; Socket : in C.int);
328
329    procedure Finalize;
330    procedure Initialize (Process_Blocking_IO : Boolean := False);
331
332 private
333
334    pragma Import (Stdcall, C_Accept, "accept");
335    pragma Import (Stdcall, C_Bind, "bind");
336    pragma Import (Stdcall, C_Close, "closesocket");
337    pragma Import (Stdcall, C_Connect, "connect");
338    pragma Import (Stdcall, C_Gethostbyaddr, "gethostbyaddr");
339    pragma Import (Stdcall, C_Gethostbyname, "gethostbyname");
340    pragma Import (Stdcall, C_Gethostname, "gethostname");
341    pragma Import (Stdcall, C_Getpeername, "getpeername");
342    pragma Import (Stdcall, C_Getsockname, "getsockname");
343    pragma Import (Stdcall, C_Getsockopt, "getsockopt");
344    pragma Import (Stdcall, C_Inet_Addr, "inet_addr");
345    pragma Import (Stdcall, C_Ioctl, "ioctlsocket");
346    pragma Import (Stdcall, C_Listen, "listen");
347    pragma Import (C, C_Read, "_read");
348    pragma Import (Stdcall, C_Recv, "recv");
349    pragma Import (Stdcall, C_Recvfrom, "recvfrom");
350    pragma Import (Stdcall, C_Select, "select");
351    pragma Import (Stdcall, C_Send, "send");
352    pragma Import (Stdcall, C_Sendto, "sendto");
353    pragma Import (Stdcall, C_Setsockopt, "setsockopt");
354    pragma Import (Stdcall, C_Shutdown, "shutdown");
355    pragma Import (Stdcall, C_Socket, "socket");
356    pragma Import (C, C_Strerror, "strerror");
357    pragma Import (C, C_System, "_system");
358    pragma Import (C, C_Write, "_write");
359    pragma Import (Stdcall, Socket_Errno, "WSAGetLastError");
360    pragma Import (Stdcall, WSAStartup, "WSAStartup");
361    pragma Import (Stdcall, WSACleanup, "WSACleanup");
362
363 end GNAT.Sockets.Thin;