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