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-2007, 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;
44 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, 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 package Host_Error_Messages is
76 function Host_Error_Message
77 (H_Errno : Integer) return C.Strings.chars_ptr;
78 -- Returns the error message string for the host error number H_Errno.
79 -- If H_Errno is not known, returns "Unknown system error".
81 end Host_Error_Messages;
83 subtype Fd_Set_Access is System.Address;
84 No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
87 range -2 ** (8 * Constants.SIZEOF_tv_sec - 1)
88 .. 2 ** (8 * Constants.SIZEOF_tv_sec - 1) - 1;
89 for time_t'Size use 8 * Constants.SIZEOF_tv_sec;
90 pragma Convention (C, time_t);
93 range -2 ** (8 * Constants.SIZEOF_tv_usec - 1)
94 .. 2 ** (8 * Constants.SIZEOF_tv_usec - 1) - 1;
95 for suseconds_t'Size use 8 * Constants.SIZEOF_tv_usec;
96 pragma Convention (C, suseconds_t);
98 type Timeval is record
100 Tv_Usec : suseconds_t;
102 pragma Convention (C, Timeval);
104 type Timeval_Access is access all Timeval;
105 pragma Convention (C, Timeval_Access);
107 Immediat : constant Timeval := (0, 0);
109 type Int_Access is access all C.int;
110 pragma Convention (C, Int_Access);
111 -- Access to C integers
113 type Chars_Ptr_Array is array (C.size_t range <>) of
114 aliased C.Strings.chars_ptr;
116 package Chars_Ptr_Pointers is
117 new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
119 -- Arrays of C (char *)
121 type In_Addr is record
122 S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
124 for In_Addr'Alignment use C.int'Alignment;
125 pragma Convention (C, In_Addr);
126 -- IPv4 address, represented as a network-order C.int. Note that the
127 -- underlying operating system may assume that values of this type have
128 -- C.int alignment, so we need to provide a suitable alignment clause here.
130 type In_Addr_Access is access all In_Addr;
131 pragma Convention (C, In_Addr_Access);
132 -- Access to internet address
134 Inaddr_Any : aliased constant In_Addr := (others => 0);
135 -- Any internet address (all the interfaces)
137 type In_Addr_Access_Array is array (C.size_t range <>)
138 of aliased In_Addr_Access;
139 pragma Convention (C, In_Addr_Access_Array);
141 package In_Addr_Access_Pointers is
142 new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
143 -- Array of internet addresses
145 type Sockaddr is record
146 Sa_Family : C.unsigned_short;
147 Sa_Data : C.char_array (1 .. 14);
149 pragma Convention (C, Sockaddr);
152 type Sockaddr_Access is access all Sockaddr;
153 pragma Convention (C, Sockaddr_Access);
154 -- Access to socket address
156 type Sockaddr_In is record
157 Sin_Family : C.unsigned_short := Constants.AF_INET;
158 Sin_Port : C.unsigned_short := 0;
159 Sin_Addr : In_Addr := Inaddr_Any;
160 Sin_Zero : C.char_array (1 .. 8) := (others => C.char'Val (0));
162 pragma Convention (C, Sockaddr_In);
163 -- Internet socket address
165 type Sockaddr_In_Access is access all Sockaddr_In;
166 pragma Convention (C, Sockaddr_In_Access);
167 -- Access to internet socket address
170 (Sin : Sockaddr_In_Access;
172 pragma Inline (Set_Length);
173 -- Set Sin.Sin_Length to Len.
174 -- On this platform, nothing is done as there is no such field.
177 (Sin : Sockaddr_In_Access;
179 pragma Inline (Set_Family);
180 -- Set Sin.Sin_Family to Family
183 (Sin : Sockaddr_In_Access;
184 Port : C.unsigned_short);
185 pragma Inline (Set_Port);
186 -- Set Sin.Sin_Port to Port
188 procedure Set_Address
189 (Sin : Sockaddr_In_Access;
191 pragma Inline (Set_Address);
192 -- Set Sin.Sin_Addr to Address
194 type Hostent is record
195 H_Name : C.Strings.chars_ptr;
196 H_Aliases : Chars_Ptr_Pointers.Pointer;
199 H_Addr_List : In_Addr_Access_Pointers.Pointer;
201 pragma Convention (C, Hostent);
204 type Hostent_Access is access all Hostent;
205 pragma Convention (C, Hostent_Access);
206 -- Access to host entry
208 type Servent is record
209 S_Name : C.Strings.chars_ptr;
210 S_Aliases : Chars_Ptr_Pointers.Pointer;
212 S_Proto : C.Strings.chars_ptr;
214 pragma Convention (C, Servent);
217 type Servent_Access is access all Servent;
218 pragma Convention (C, Servent_Access);
219 -- Access to service entry
221 type Two_Ints is array (0 .. 1) of C.int;
222 pragma Convention (C, Two_Ints);
223 -- Container for two int values
225 subtype Fd_Pair is Two_Ints;
226 -- Two_Ints as used for Create_Signalling_Fds: a pair of connected file
227 -- descriptors, one of which (the "read end" of the connection) being used
228 -- for reading, the other one (the "write end") being used for writing.
230 Read_End : constant := 0;
231 Write_End : constant := 1;
232 -- Indices into an Fd_Pair value providing access to each of the connected
235 --------------------------------
236 -- Standard library functions --
237 --------------------------------
241 Addr : System.Address;
242 Addrlen : not null access C.int) return C.int;
246 Name : System.Address;
247 Namelen : C.int) return C.int;
250 (Fd : C.int) return C.int;
254 Name : System.Address;
255 Namelen : C.int) return C.int;
257 function C_Gethostname
258 (Name : System.Address;
259 Namelen : C.int) return C.int;
261 function C_Getpeername
263 Name : System.Address;
264 Namelen : not null access C.int) return C.int;
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 -------------------------------------------------------
358 -- Signalling file descriptors for selector abortion --
359 -------------------------------------------------------
361 package Signalling_Fds is
363 function Create (Fds : not null access Fd_Pair) return C.int;
364 pragma Convention (C, Create);
365 -- Create a pair of connected descriptors suitable for use with C_Select
366 -- (used for signalling in Selector objects).
368 function Read (Rsig : C.int) return C.int;
369 pragma Convention (C, Read);
370 -- Read one byte of data from rsig, the read end of a pair of signalling
371 -- fds created by Create_Signalling_Fds.
373 function Write (Wsig : C.int) return C.int;
374 pragma Convention (C, Write);
375 -- Write one byte of data to wsig, the write end of a pair of signalling
376 -- fds created by Create_Signalling_Fds.
378 procedure Close (Sig : C.int);
379 pragma Convention (C, Close);
380 -- Close one end of a pair of signalling fds (ignoring any error)
384 ----------------------------
385 -- Socket sets management --
386 ----------------------------
388 procedure Free_Socket_Set
389 (Set : Fd_Set_Access);
390 -- Free system-dependent socket set
392 procedure Get_Socket_From_Set
393 (Set : Fd_Set_Access;
396 -- Get last socket in Socket and remove it from the socket set. The
397 -- parameter Last is a maximum value of the largest socket. This hint is
398 -- used to avoid scanning very large socket sets. After a call to
399 -- Get_Socket_From_Set, Last is set back to the real largest socket in the
402 procedure Insert_Socket_In_Set
403 (Set : Fd_Set_Access;
405 -- Insert socket in the socket set
407 function Is_Socket_In_Set
408 (Set : Fd_Set_Access;
409 Socket : C.int) return C.int;
410 -- Check whether Socket is in the socket set, return a non-zero
411 -- value if it is, zero if it is not.
413 procedure Last_Socket_In_Set
414 (Set : Fd_Set_Access;
416 -- Find the largest socket in the socket set. This is needed for select().
417 -- When Last_Socket_In_Set is called, parameter Last is a maximum value of
418 -- the largest socket. This hint is used to avoid scanning very large
419 -- socket sets. After the call, Last is set back to the real largest socket
420 -- in the socket set.
422 function New_Socket_Set
423 (Set : Fd_Set_Access) return Fd_Set_Access;
424 -- Allocate a new socket set which is a system-dependent structure and
425 -- initialize by copying Set if it is non-null, by making it empty
428 procedure Remove_Socket_From_Set
429 (Set : Fd_Set_Access;
431 -- Remove socket from the socket set
433 -------------------------------------------
434 -- Nonreentrant network databases access --
435 -------------------------------------------
437 function Nonreentrant_Gethostbyname
438 (Name : C.char_array) return Hostent_Access;
440 function Nonreentrant_Gethostbyaddr
441 (Addr : System.Address;
443 Addr_Type : C.int) return Hostent_Access;
445 function Nonreentrant_Getservbyname
446 (Name : C.char_array;
447 Proto : C.char_array) return Servent_Access;
449 function Nonreentrant_Getservbyport
451 Proto : C.char_array) return Servent_Access;
453 procedure Initialize;
458 pragma Import (C, C_Bind, "DECC$BIND");
459 pragma Import (C, C_Close, "DECC$CLOSE");
460 pragma Import (C, C_Gethostname, "DECC$GETHOSTNAME");
461 pragma Import (C, C_Getpeername, "DECC$GETPEERNAME");
462 pragma Import (C, C_Getsockname, "DECC$GETSOCKNAME");
463 pragma Import (C, C_Getsockopt, "DECC$GETSOCKOPT");
464 pragma Import (C, C_Inet_Addr, "DECC$INET_ADDR");
465 pragma Import (C, C_Listen, "DECC$LISTEN");
466 pragma Import (C, C_Select, "DECC$SELECT");
467 pragma Import (C, C_Setsockopt, "DECC$SETSOCKOPT");
468 pragma Import (C, C_Shutdown, "DECC$SHUTDOWN");
469 pragma Import (C, C_Strerror, "DECC$STRERROR");
470 pragma Import (C, C_System, "DECC$SYSTEM");
472 pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
473 pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
474 pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
475 pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
476 pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
477 pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
478 pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
480 pragma Import (C, Nonreentrant_Gethostbyname, "DECC$GETHOSTBYNAME");
481 pragma Import (C, Nonreentrant_Gethostbyaddr, "DECC$GETHOSTBYADDR");
482 pragma Import (C, Nonreentrant_Getservbyname, "DECC$GETSERVBYNAME");
483 pragma Import (C, Nonreentrant_Getservbyport, "DECC$GETSERVBYPORT");
485 end GNAT.Sockets.Thin;