OSDN Git Service

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