OSDN Git Service

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