OSDN Git Service

2007-04-20 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socthi-mingw.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-2006, AdaCore                     --
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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 version is for NT
39
40 with Interfaces.C.Pointers;
41 with Interfaces.C.Strings;
42
43 with GNAT.Sockets.Constants;
44
45 with System;
46
47 package GNAT.Sockets.Thin is
48
49    package C renames Interfaces.C;
50
51    use type C.int;
52    --  So that 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;
58    --  Returns last socket error number
59
60    procedure Set_Socket_Errno (Errno : Integer);
61    --  Set last socket error number
62
63    function Socket_Error_Message (Errno : Integer) return C.Strings.chars_ptr;
64    --  Returns the error message string for the error number Errno. If Errno is
65    --  not known it returns "Unknown system error".
66
67    function Host_Errno return Integer;
68    pragma Import (C, Host_Errno, "__gnat_get_h_errno");
69    --  Returns last host error number
70
71    subtype Fd_Set_Access is System.Address;
72    No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
73
74    type time_t is
75      range -2 ** (8 * Constants.SIZEOF_tv_sec - 1)
76          .. 2 ** (8 * Constants.SIZEOF_tv_sec - 1) - 1;
77    for time_t'Size use 8 * Constants.SIZEOF_tv_sec;
78    pragma Convention (C, time_t);
79
80    type suseconds_t is
81      range -2 ** (8 * Constants.SIZEOF_tv_usec - 1)
82          .. 2 ** (8 * Constants.SIZEOF_tv_usec - 1) - 1;
83    for suseconds_t'Size use 8 * Constants.SIZEOF_tv_usec;
84    pragma Convention (C, suseconds_t);
85
86    type Timeval is record
87       Tv_Sec  : time_t;
88       Tv_Usec : suseconds_t;
89    end record;
90    pragma Convention (C, Timeval);
91
92    type Timeval_Access is access all Timeval;
93    pragma Convention (C, Timeval_Access);
94
95    Immediat : constant Timeval := (0, 0);
96
97    type Int_Access is access all C.int;
98    pragma Convention (C, Int_Access);
99    --  Access to C integers
100
101    type Chars_Ptr_Array is array (C.size_t range <>) of
102      aliased C.Strings.chars_ptr;
103
104    package Chars_Ptr_Pointers is
105       new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
106                       C.Strings.Null_Ptr);
107    --  Arrays of C (char *)
108
109    type In_Addr is record
110       S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
111    end record;
112    pragma Convention (C, In_Addr);
113    --  Internet address
114
115    type In_Addr_Access is access all In_Addr;
116    pragma Convention (C, In_Addr_Access);
117    --  Access to internet address
118
119    Inaddr_Any : aliased constant In_Addr := (others => 0);
120    --  Any internet address (all the interfaces)
121
122    type In_Addr_Access_Array is array (C.size_t range <>)
123      of aliased In_Addr_Access;
124    pragma Convention (C, In_Addr_Access_Array);
125
126    package In_Addr_Access_Pointers is
127      new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
128    --  Array of internet addresses
129
130    type Sockaddr is record
131       Sa_Family : C.unsigned_short;
132       Sa_Data   : C.char_array (1 .. 14);
133    end record;
134    pragma Convention (C, Sockaddr);
135    --  Socket address
136
137    type Sockaddr_Access is access all Sockaddr;
138    pragma Convention (C, Sockaddr_Access);
139    --  Access to socket address
140
141    type Sockaddr_In is record
142       Sin_Family : C.unsigned_short      := Constants.AF_INET;
143       Sin_Port   : C.unsigned_short      := 0;
144       Sin_Addr   : In_Addr               := Inaddr_Any;
145       Sin_Zero   : C.char_array (1 .. 8) := (others => C.char'Val (0));
146    end record;
147    pragma Convention (C, Sockaddr_In);
148    --  Internet socket address
149
150    type Sockaddr_In_Access is access all Sockaddr_In;
151    pragma Convention (C, Sockaddr_In_Access);
152    --  Access to internet socket address
153
154    procedure Set_Length
155      (Sin : Sockaddr_In_Access;
156       Len : C.int);
157    pragma Inline (Set_Length);
158    --  Set Sin.Sin_Length to Len.
159    --  On this platform, nothing is done as there is no such field.
160
161    procedure Set_Family
162      (Sin    : Sockaddr_In_Access;
163       Family : C.int);
164    pragma Inline (Set_Family);
165    --  Set Sin.Sin_Family to Family
166
167    procedure Set_Port
168      (Sin  : Sockaddr_In_Access;
169       Port : C.unsigned_short);
170    pragma Inline (Set_Port);
171    --  Set Sin.Sin_Port to Port
172
173    procedure Set_Address
174      (Sin     : Sockaddr_In_Access;
175       Address : In_Addr);
176    pragma Inline (Set_Address);
177    --  Set Sin.Sin_Addr to Address
178
179    type Hostent is record
180       H_Name      : C.Strings.chars_ptr;
181       H_Aliases   : Chars_Ptr_Pointers.Pointer;
182       H_Addrtype  : C.short;
183       H_Length    : C.short;
184       H_Addr_List : In_Addr_Access_Pointers.Pointer;
185    end record;
186    pragma Convention (C, Hostent);
187    --  Host entry
188
189    type Hostent_Access is access all Hostent;
190    pragma Convention (C, Hostent_Access);
191    --  Access to host entry
192
193    type Servent is record
194       S_Name    : C.Strings.chars_ptr;
195       S_Aliases : Chars_Ptr_Pointers.Pointer;
196       S_Port    : C.int;
197       S_Proto   : C.Strings.chars_ptr;
198    end record;
199    pragma Convention (C, Servent);
200    --  Service entry
201
202    type Servent_Access is access all Servent;
203    pragma Convention (C, Servent_Access);
204    --  Access to service entry
205
206    type Two_Ints is array (0 .. 1) of C.int;
207    pragma Convention (C, Two_Ints);
208    --  Container for two int values
209
210    subtype Fd_Pair is Two_Ints;
211    --  Two_Ints as used for Create_Signalling_Fds: a pair of connected file
212    --  descriptors, one of which (the "read end" of the connection) being used
213    --  for reading, the other one (the "write end") being used for writing.
214
215    Read_End  : constant := 0;
216    Write_End : constant := 1;
217    --  Indices into an Fd_Pair value providing access to each of the connected
218    --  file descriptors.
219
220    function C_Accept
221      (S       : C.int;
222       Addr    : System.Address;
223       Addrlen : not null access C.int) return C.int;
224
225    function C_Bind
226      (S       : C.int;
227       Name    : System.Address;
228       Namelen : C.int) return C.int;
229
230    function C_Close
231      (Fd : C.int) return C.int;
232
233    function C_Connect
234      (S       : C.int;
235       Name    : System.Address;
236       Namelen : C.int) return C.int;
237
238    function C_Gethostbyaddr
239      (Addr : System.Address;
240       Len  : C.int;
241       Typ  : C.int) return Hostent_Access;
242
243    function C_Gethostbyname
244      (Name : C.char_array) return Hostent_Access;
245
246    function C_Gethostname
247      (Name    : System.Address;
248       Namelen : C.int) return C.int;
249
250    function C_Getpeername
251      (S       : C.int;
252       Name    : System.Address;
253       Namelen : not null access C.int) return C.int;
254
255    function C_Getservbyname
256      (Name  : C.char_array;
257       Proto : C.char_array) return Servent_Access;
258
259    function C_Getservbyport
260      (Port  : C.int;
261       Proto : C.char_array) return Servent_Access;
262
263    function C_Getsockname
264      (S       : C.int;
265       Name    : System.Address;
266       Namelen : not null access C.int) return C.int;
267
268    function C_Getsockopt
269      (S       : C.int;
270       Level   : C.int;
271       Optname : C.int;
272       Optval  : System.Address;
273       Optlen  : not null access C.int) return C.int;
274
275    function C_Inet_Addr
276      (Cp : C.Strings.chars_ptr) return C.int;
277
278    function C_Ioctl
279      (S    : C.int;
280       Req  : C.int;
281       Arg  : Int_Access) return C.int;
282
283    function C_Listen
284      (S       : C.int;
285       Backlog : C.int) return C.int;
286
287    function C_Readv
288      (Fd     : C.int;
289       Iov    : System.Address;
290       Iovcnt : C.int) return C.int;
291
292    function C_Recv
293      (S     : C.int;
294       Msg   : System.Address;
295       Len   : C.int;
296       Flags : C.int) return C.int;
297
298    function C_Recvfrom
299      (S       : C.int;
300       Msg     : System.Address;
301       Len     : C.int;
302       Flags   : C.int;
303       From    : Sockaddr_In_Access;
304       Fromlen : not null access C.int) 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) return C.int;
312
313    function C_Send
314      (S     : C.int;
315       Msg   : System.Address;
316       Len   : C.int;
317       Flags : C.int) return C.int;
318
319    function C_Sendto
320      (S     : C.int;
321       Msg   : System.Address;
322       Len   : C.int;
323       Flags : C.int;
324       To    : Sockaddr_In_Access;
325       Tolen : C.int) return C.int;
326
327    function C_Setsockopt
328      (S       : C.int;
329       Level   : C.int;
330       Optname : C.int;
331       Optval  : System.Address;
332       Optlen  : C.int) return C.int;
333
334    function C_Shutdown
335      (S   : C.int;
336       How : C.int) return C.int;
337
338    function C_Socket
339      (Domain   : C.int;
340       Typ      : C.int;
341       Protocol : C.int) return C.int;
342
343    function C_Strerror
344      (Errnum : C.int) return C.Strings.chars_ptr;
345
346    function C_System
347      (Command : System.Address) return C.int;
348
349    function C_Writev
350      (Fd     : C.int;
351       Iov    : System.Address;
352       Iovcnt : C.int) return C.int;
353
354    function WSAStartup
355      (WS_Version     : Interfaces.C.int;
356       WSADataAddress : System.Address) return Interfaces.C.int;
357
358    package Signalling_Fds is
359
360       function Create (Fds : not null access Fd_Pair) return C.int;
361       pragma Convention (C, Create);
362       --  Create a pair of connected descriptors suitable for use with C_Select
363       --  (used for signalling in Selector objects).
364
365       function Read (Rsig : C.int) return C.int;
366       pragma Convention (C, Read);
367       --  Read one byte of data from rsig, the read end of a pair of signalling
368       --  fds created by Create_Signalling_Fds.
369
370       function Write (Wsig : C.int) return C.int;
371       pragma Convention (C, Write);
372       --  Write one byte of data to wsig, the write end of a pair of signalling
373       --  fds created by Create_Signalling_Fds.
374
375    end Signalling_Fds;
376
377    procedure Free_Socket_Set
378      (Set : Fd_Set_Access);
379    --  Free system-dependent socket set
380
381    procedure Get_Socket_From_Set
382      (Set    : Fd_Set_Access;
383       Socket : Int_Access;
384       Last   : Int_Access);
385    --  Get last socket in Socket and remove it from the socket
386    --  set. The parameter Last is a maximum value of the largest
387    --  socket. This hint is used to avoid scanning very large socket
388    --  sets. After a call to Get_Socket_From_Set, Last is set back to
389    --  the real largest socket in the socket set.
390
391    procedure Insert_Socket_In_Set
392      (Set    : Fd_Set_Access;
393       Socket : C.int);
394    --  Insert socket in the socket set
395
396    function  Is_Socket_In_Set
397      (Set    : Fd_Set_Access;
398       Socket : C.int) return C.int;
399    --  Check whether Socket is in the socket set, return a non-zero
400    --  value if it is, zero if it is not.
401
402    procedure Last_Socket_In_Set
403      (Set    : Fd_Set_Access;
404       Last   : Int_Access);
405    --  Find the largest socket in the socket set. This is needed for select().
406    --  When Last_Socket_In_Set is called, parameter Last is a maximum value of
407    --  the largest socket. This hint is used to avoid scanning very large
408    --  socket sets. After the call, Last is set back to the real largest socket
409    --  in the socket set.
410
411    function  New_Socket_Set
412      (Set : Fd_Set_Access) return Fd_Set_Access;
413    --  Allocate a new socket set which is a system-dependent structure and
414    --  initialize by copying Set if it is non-null, by making it empty
415    --  otherwise.
416
417    procedure Remove_Socket_From_Set
418      (Set    : Fd_Set_Access;
419       Socket : C.int);
420    --  Remove socket from the socket set
421
422    procedure WSACleanup;
423
424    procedure Finalize;
425    procedure Initialize (Process_Blocking_IO : Boolean);
426
427 private
428    pragma Import (Stdcall, C_Accept, "accept");
429    pragma Import (Stdcall, C_Bind, "bind");
430    pragma Import (Stdcall, C_Close, "closesocket");
431    pragma Import (Stdcall, C_Gethostbyaddr, "gethostbyaddr");
432    pragma Import (Stdcall, C_Gethostbyname, "gethostbyname");
433    pragma Import (Stdcall, C_Gethostname, "gethostname");
434    pragma Import (Stdcall, C_Getpeername, "getpeername");
435    pragma Import (Stdcall, C_Getservbyname, "getservbyname");
436    pragma Import (Stdcall, C_Getservbyport, "getservbyport");
437    pragma Import (Stdcall, C_Getsockname, "getsockname");
438    pragma Import (Stdcall, C_Getsockopt, "getsockopt");
439    pragma Import (Stdcall, C_Ioctl, "ioctlsocket");
440    pragma Import (Stdcall, C_Listen, "listen");
441    pragma Import (Stdcall, C_Recv, "recv");
442    pragma Import (Stdcall, C_Recvfrom, "recvfrom");
443    pragma Import (Stdcall, C_Send, "send");
444    pragma Import (Stdcall, C_Sendto, "sendto");
445    pragma Import (Stdcall, C_Setsockopt, "setsockopt");
446    pragma Import (Stdcall, C_Shutdown, "shutdown");
447    pragma Import (Stdcall, C_Socket, "socket");
448    pragma Import (C, C_Strerror, "strerror");
449    pragma Import (C, C_System, "_system");
450    pragma Import (Stdcall, Socket_Errno, "WSAGetLastError");
451    pragma Import (Stdcall, Set_Socket_Errno, "WSASetLastError");
452    pragma Import (Stdcall, WSAStartup, "WSAStartup");
453    pragma Import (Stdcall, WSACleanup, "WSACleanup");
454
455    pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
456    pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
457    pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
458    pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
459    pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
460    pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
461    pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
462
463 end GNAT.Sockets.Thin;