1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
9 -- Copyright (C) 2002-2006, AdaCore --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
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.
38 -- This is the Alpha/VMS version
40 with Interfaces.C.Pointers;
41 with Interfaces.C.Strings;
43 with GNAT.Sockets.Constants;
48 package GNAT.Sockets.Thin is
50 -- ??? more comments needed ???
52 package C renames Interfaces.C;
55 -- This is so we can declare the Failure constant below
57 Success : constant C.int := 0;
58 Failure : constant C.int := -1;
60 function Socket_Errno return Integer renames GNAT.OS_Lib.Errno;
61 -- Returns last socket error number
63 procedure Set_Socket_Errno (Errno : Integer) renames GNAT.OS_Lib.Set_Errno;
64 -- Set last socket error number
66 function Socket_Error_Message (Errno : Integer) return C.Strings.chars_ptr;
67 -- Returns the error message string for the error number Errno. If Errno is
68 -- not known it returns "Unknown system error".
70 function Host_Errno return Integer;
71 pragma Import (C, Host_Errno, "__gnat_get_h_errno");
72 -- Returns last host error number
74 subtype Fd_Set_Access is System.Address;
75 No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
78 range -2 ** (8 * Constants.SIZEOF_tv_sec - 1)
79 .. 2 ** (8 * Constants.SIZEOF_tv_sec - 1) - 1;
80 for time_t'Size use 8 * Constants.SIZEOF_tv_sec;
81 pragma Convention (C, time_t);
84 range -2 ** (8 * Constants.SIZEOF_tv_usec - 1)
85 .. 2 ** (8 * Constants.SIZEOF_tv_usec - 1) - 1;
86 for suseconds_t'Size use 8 * Constants.SIZEOF_tv_usec;
87 pragma Convention (C, suseconds_t);
89 type Timeval is record
91 Tv_Usec : suseconds_t;
93 pragma Convention (C, Timeval);
95 type Timeval_Access is access all Timeval;
96 pragma Convention (C, Timeval_Access);
98 Immediat : constant Timeval := (0, 0);
100 type Int_Access is access all C.int;
101 pragma Convention (C, Int_Access);
102 -- Access to C integers
104 type Chars_Ptr_Array is array (C.size_t range <>) of
105 aliased C.Strings.chars_ptr;
107 package Chars_Ptr_Pointers is
108 new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
110 -- Arrays of C (char *)
112 type In_Addr is record
113 S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
115 pragma Convention (C, In_Addr);
118 type In_Addr_Access is access all In_Addr;
119 pragma Convention (C, In_Addr_Access);
120 -- Access to internet address
122 Inaddr_Any : aliased constant In_Addr := (others => 0);
123 -- Any internet address (all the interfaces)
125 type In_Addr_Access_Array is array (C.size_t range <>)
126 of aliased In_Addr_Access;
127 pragma Convention (C, In_Addr_Access_Array);
129 package In_Addr_Access_Pointers is
130 new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
131 -- Array of internet addresses
133 type Sockaddr is record
134 Sa_Family : C.unsigned_short;
135 Sa_Data : C.char_array (1 .. 14);
137 pragma Convention (C, Sockaddr);
140 type Sockaddr_Access is access all Sockaddr;
141 pragma Convention (C, Sockaddr_Access);
142 -- Access to socket address
144 type Sockaddr_In is record
145 Sin_Family : C.unsigned_short := Constants.AF_INET;
146 Sin_Port : C.unsigned_short := 0;
147 Sin_Addr : In_Addr := Inaddr_Any;
148 Sin_Zero : C.char_array (1 .. 8) := (others => C.char'Val (0));
150 pragma Convention (C, Sockaddr_In);
151 -- Internet socket address
153 type Sockaddr_In_Access is access all Sockaddr_In;
154 pragma Convention (C, Sockaddr_In_Access);
155 -- Access to internet socket address
158 (Sin : Sockaddr_In_Access;
160 pragma Inline (Set_Length);
161 -- Set Sin.Sin_Length to Len.
162 -- On this platform, nothing is done as there is no such field.
165 (Sin : Sockaddr_In_Access;
167 pragma Inline (Set_Family);
168 -- Set Sin.Sin_Family to Family
171 (Sin : Sockaddr_In_Access;
172 Port : C.unsigned_short);
173 pragma Inline (Set_Port);
174 -- Set Sin.Sin_Port to Port
176 procedure Set_Address
177 (Sin : Sockaddr_In_Access;
179 pragma Inline (Set_Address);
180 -- Set Sin.Sin_Addr to Address
182 type Hostent is record
183 H_Name : C.Strings.chars_ptr;
184 H_Aliases : Chars_Ptr_Pointers.Pointer;
187 H_Addr_List : In_Addr_Access_Pointers.Pointer;
189 pragma Convention (C, Hostent);
192 type Hostent_Access is access all Hostent;
193 pragma Convention (C, Hostent_Access);
194 -- Access to host entry
196 type Servent is record
197 S_Name : C.Strings.chars_ptr;
198 S_Aliases : Chars_Ptr_Pointers.Pointer;
200 S_Proto : C.Strings.chars_ptr;
202 pragma Convention (C, Servent);
205 type Servent_Access is access all Servent;
206 pragma Convention (C, Servent_Access);
207 -- Access to service entry
209 type Two_Ints is array (0 .. 1) of C.int;
210 pragma Convention (C, Two_Ints);
211 -- Container for two int values
213 subtype Fd_Pair is Two_Ints;
214 -- Two_Ints as used for Create_Signalling_Fds: a pair of connected file
215 -- descriptors, one of which (the "read end" of the connection) being used
216 -- for reading, the other one (the "write end") being used for writing.
218 Read_End : constant := 0;
219 Write_End : constant := 1;
220 -- Indices into an Fd_Pair value providing access to each of the connected
225 Addr : System.Address;
226 Addrlen : not null access C.int) return C.int;
230 Name : System.Address;
231 Namelen : C.int) return C.int;
234 (Fd : C.int) return C.int;
238 Name : System.Address;
239 Namelen : C.int) return C.int;
241 function C_Gethostbyaddr
242 (Addr : System.Address;
244 Typ : C.int) return Hostent_Access;
246 function C_Gethostbyname
247 (Name : C.char_array) return Hostent_Access;
249 function C_Gethostname
250 (Name : System.Address;
251 Namelen : C.int) return C.int;
253 function C_Getpeername
255 Name : System.Address;
256 Namelen : not null access C.int) return C.int;
258 function C_Getservbyname
259 (Name : C.char_array;
260 Proto : C.char_array) return Servent_Access;
262 function C_Getservbyport
264 Proto : C.char_array) return Servent_Access;
266 function C_Getsockname
268 Name : System.Address;
269 Namelen : not null access C.int) return C.int;
271 function C_Getsockopt
275 Optval : System.Address;
276 Optlen : not null access C.int) return C.int;
279 (Cp : C.Strings.chars_ptr) return C.int;
284 Arg : Int_Access) return C.int;
288 Backlog : C.int) return C.int;
292 Iov : System.Address;
293 Iovcnt : C.int) return C.int;
297 Msg : System.Address;
299 Flags : C.int) return C.int;
303 Msg : System.Address;
306 From : Sockaddr_In_Access;
307 Fromlen : not null access C.int) return C.int;
311 Readfds : Fd_Set_Access;
312 Writefds : Fd_Set_Access;
313 Exceptfds : Fd_Set_Access;
314 Timeout : Timeval_Access) return C.int;
318 Msg : System.Address;
320 Flags : C.int) return C.int;
324 Msg : System.Address;
327 To : Sockaddr_In_Access;
328 Tolen : C.int) return C.int;
330 function C_Setsockopt
334 Optval : System.Address;
335 Optlen : C.int) return C.int;
339 How : C.int) return C.int;
344 Protocol : C.int) return C.int;
347 (Errnum : C.int) return C.Strings.chars_ptr;
350 (Command : System.Address) return C.int;
354 Iov : System.Address;
355 Iovcnt : C.int) return C.int;
357 package Signalling_Fds is
359 function Create (Fds : not null access Fd_Pair) return C.int;
360 pragma Convention (C, Create);
361 -- Create a pair of connected descriptors suitable for use with C_Select
362 -- (used for signalling in Selector objects).
364 function Read (Rsig : C.int) return C.int;
365 pragma Convention (C, Read);
366 -- Read one byte of data from rsig, the read end of a pair of signalling
367 -- fds created by Create_Signalling_Fds.
369 function Write (Wsig : C.int) return C.int;
370 pragma Convention (C, Write);
371 -- Write one byte of data to wsig, the write end of a pair of signalling
372 -- fds created by Create_Signalling_Fds.
376 procedure Free_Socket_Set
377 (Set : Fd_Set_Access);
378 -- Free system-dependent socket set
380 procedure Get_Socket_From_Set
381 (Set : Fd_Set_Access;
384 -- Get last socket in Socket and remove it from the socket
385 -- set. The parameter Last is a maximum value of the largest
386 -- socket. This hint is used to avoid scanning very large socket
387 -- sets. After a call to Get_Socket_From_Set, Last is set back to
388 -- the real largest socket in the socket set.
390 procedure Insert_Socket_In_Set
391 (Set : Fd_Set_Access;
393 -- Insert socket in the socket set
395 function Is_Socket_In_Set
396 (Set : Fd_Set_Access;
397 Socket : C.int) return C.int;
398 -- Check whether Socket is in the socket set, return a non-zero
399 -- value if it is, zero if it is not.
401 procedure Last_Socket_In_Set
402 (Set : Fd_Set_Access;
404 -- Find the largest socket in the socket set. This is needed for select().
405 -- When Last_Socket_In_Set is called, parameter Last is a maximum value of
406 -- the largest socket. This hint is used to avoid scanning very large
407 -- socket sets. After the call, Last is set back to the real largest socket
408 -- in the socket set.
410 function New_Socket_Set
411 (Set : Fd_Set_Access) return Fd_Set_Access;
412 -- Allocate a new socket set which is a system-dependent structure and
413 -- initialize by copying Set if it is non-null, by making it empty
416 procedure Remove_Socket_From_Set
417 (Set : Fd_Set_Access;
419 -- Remove socket from the socket set
422 procedure Initialize (Process_Blocking_IO : Boolean);
426 pragma Import (C, C_Bind, "DECC$BIND");
427 pragma Import (C, C_Close, "DECC$CLOSE");
428 pragma Import (C, C_Gethostbyaddr, "DECC$GETHOSTBYADDR");
429 pragma Import (C, C_Gethostbyname, "DECC$GETHOSTBYNAME");
430 pragma Import (C, C_Gethostname, "DECC$GETHOSTNAME");
431 pragma Import (C, C_Getpeername, "DECC$GETPEERNAME");
432 pragma Import (C, C_Getservbyname, "DECC$GETSERVBYNAME");
433 pragma Import (C, C_Getservbyport, "DECC$GETSERVBYPORT");
434 pragma Import (C, C_Getsockname, "DECC$GETSOCKNAME");
435 pragma Import (C, C_Getsockopt, "DECC$GETSOCKOPT");
436 pragma Import (C, C_Inet_Addr, "DECC$INET_ADDR");
437 pragma Import (C, C_Listen, "DECC$LISTEN");
438 pragma Import (C, C_Select, "DECC$SELECT");
439 pragma Import (C, C_Setsockopt, "DECC$SETSOCKOPT");
440 pragma Import (C, C_Shutdown, "DECC$SHUTDOWN");
441 pragma Import (C, C_Strerror, "DECC$STRERROR");
442 pragma Import (C, C_System, "DECC$SYSTEM");
444 pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
445 pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
446 pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
447 pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
448 pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
449 pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
450 pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
452 end GNAT.Sockets.Thin;