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-2005, 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;
42 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 function Socket_Error_Message (Errno : Integer) return C.Strings.chars_ptr;
64 -- Returns the error message string for the error number Errno. If
65 -- Errno is not known it returns "Unknown system error".
67 function Host_Errno return Integer;
68 pragma Import (C, Host_Errno, "__gnat_get_h_errno");
69 -- Returns last host error number
71 subtype Fd_Set_Access is System.Address;
72 No_Fd_Set : constant Fd_Set_Access := System.Null_Address;
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);
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);
86 type Timeval is record
88 Tv_Usec : suseconds_t;
90 pragma Convention (C, Timeval);
92 type Timeval_Access is access all Timeval;
93 pragma Convention (C, Timeval_Access);
95 Immediat : constant Timeval := (0, 0);
97 type Int_Access is access all C.int;
98 pragma Convention (C, Int_Access);
99 -- Access to C integers
101 type Chars_Ptr_Array is array (C.size_t range <>) of
102 aliased C.Strings.chars_ptr;
104 package Chars_Ptr_Pointers is
105 new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
107 -- Arrays of C (char *)
109 type In_Addr is record
110 S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
112 pragma Convention (C, In_Addr);
115 type In_Addr_Access is access all In_Addr;
116 pragma Convention (C, In_Addr_Access);
117 -- Access to internet address
119 Inaddr_Any : aliased constant In_Addr := (others => 0);
120 -- Any internet address (all the interfaces)
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);
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
130 type Sockaddr is record
131 Sa_Family : C.unsigned_short;
132 Sa_Data : C.char_array (1 .. 14);
134 pragma Convention (C, Sockaddr);
137 type Sockaddr_Access is access all Sockaddr;
138 pragma Convention (C, Sockaddr_Access);
139 -- Access to socket address
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));
147 pragma Convention (C, Sockaddr_In);
148 -- Internet socket address
150 type Sockaddr_In_Access is access all Sockaddr_In;
151 pragma Convention (C, Sockaddr_In_Access);
152 -- Access to internet socket address
155 (Sin : Sockaddr_In_Access;
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.
162 (Sin : Sockaddr_In_Access;
164 pragma Inline (Set_Family);
165 -- Set Sin.Sin_Family to Family
168 (Sin : Sockaddr_In_Access;
169 Port : C.unsigned_short);
170 pragma Inline (Set_Port);
171 -- Set Sin.Sin_Port to Port
173 procedure Set_Address
174 (Sin : Sockaddr_In_Access;
176 pragma Inline (Set_Address);
177 -- Set Sin.Sin_Addr to Address
179 type Hostent is record
180 H_Name : C.Strings.chars_ptr;
181 H_Aliases : Chars_Ptr_Pointers.Pointer;
184 H_Addr_List : In_Addr_Access_Pointers.Pointer;
186 pragma Convention (C, Hostent);
189 type Hostent_Access is access all Hostent;
190 pragma Convention (C, Hostent_Access);
191 -- Access to host entry
193 type Servent is record
194 S_Name : C.Strings.chars_ptr;
195 S_Aliases : Chars_Ptr_Pointers.Pointer;
197 S_Proto : C.Strings.chars_ptr;
199 pragma Convention (C, Servent);
202 type Servent_Access is access all Servent;
203 pragma Convention (C, Servent_Access);
204 -- Access to service entry
206 type Two_Int is array (0 .. 1) of C.int;
207 pragma Convention (C, Two_Int);
212 Addr : System.Address;
213 Addrlen : access C.int) return C.int;
217 Name : System.Address;
218 Namelen : C.int) return C.int;
221 (Fd : C.int) return C.int;
225 Name : System.Address;
226 Namelen : C.int) return C.int;
228 function C_Gethostbyaddr
229 (Addr : System.Address;
231 Typ : C.int) return Hostent_Access;
233 function C_Gethostbyname
234 (Name : C.char_array) return Hostent_Access;
236 function C_Gethostname
237 (Name : System.Address;
238 Namelen : C.int) return C.int;
240 function C_Getpeername
242 Name : System.Address;
243 Namelen : access C.int) return C.int;
245 function C_Getservbyname
246 (Name : C.char_array;
247 Proto : C.char_array) return Servent_Access;
249 function C_Getservbyport
251 Proto : C.char_array) return Servent_Access;
253 function C_Getsockname
255 Name : System.Address;
256 Namelen : access C.int) return C.int;
258 function C_Getsockopt
262 Optval : System.Address;
263 Optlen : access C.int) return C.int;
266 (Cp : C.Strings.chars_ptr) return C.int;
271 Arg : Int_Access) return C.int;
273 function C_Listen (S, Backlog : C.int) return C.int;
277 Iov : System.Address;
278 Iovcnt : C.int) return C.int;
282 Msg : System.Address;
284 Flags : C.int) return C.int;
288 Msg : System.Address;
291 From : Sockaddr_In_Access;
292 Fromlen : access C.int) return C.int;
296 Readfds : Fd_Set_Access;
297 Writefds : Fd_Set_Access;
298 Exceptfds : Fd_Set_Access;
299 Timeout : Timeval_Access) return C.int;
303 Msg : System.Address;
305 Flags : C.int) return C.int;
309 Msg : System.Address;
312 To : Sockaddr_In_Access;
313 Tolen : C.int) return C.int;
315 function C_Setsockopt
319 Optval : System.Address;
320 Optlen : C.int) return C.int;
324 How : C.int) return C.int;
329 Protocol : C.int) return C.int;
332 (Errnum : C.int) return C.Strings.chars_ptr;
335 (Command : System.Address) return C.int;
339 Iov : System.Address;
340 Iovcnt : C.int) return C.int;
342 procedure Free_Socket_Set
343 (Set : Fd_Set_Access);
344 -- Free system-dependent socket set
346 procedure Get_Socket_From_Set
347 (Set : Fd_Set_Access;
350 -- Get last socket in Socket and remove it from the socket
351 -- set. The parameter Last is a maximum value of the largest
352 -- socket. This hint is used to avoid scanning very large socket
353 -- sets. After a call to Get_Socket_From_Set, Last is set back to
354 -- the real largest socket in the socket set.
356 procedure Insert_Socket_In_Set
357 (Set : Fd_Set_Access;
359 -- Insert socket in the socket set
361 function Is_Socket_In_Set
362 (Set : Fd_Set_Access;
363 Socket : C.int) return C.int;
364 -- Check whether Socket is in the socket set, return a non-zero
365 -- value if it is, zero if it is not.
367 procedure Last_Socket_In_Set
368 (Set : Fd_Set_Access;
370 -- Find the largest socket in the socket set. This is needed for
371 -- select(). When Last_Socket_In_Set is called, parameter Last is
372 -- a maximum value of the largest socket. This hint is used to
373 -- avoid scanning very large socket sets. After the call, Last is
374 -- set back to the real largest socket in the socket set.
376 function New_Socket_Set
377 (Set : Fd_Set_Access) return Fd_Set_Access;
378 -- Allocate a new socket set which is a system-dependent structure
379 -- and initialize by copying Set if it is non-null, by making it
382 procedure Remove_Socket_From_Set
383 (Set : Fd_Set_Access;
385 -- Remove socket from the socket set
388 procedure Initialize (Process_Blocking_IO : Boolean);
392 pragma Import (C, C_Bind, "DECC$BIND");
393 pragma Import (C, C_Close, "DECC$CLOSE");
394 pragma Import (C, C_Gethostbyaddr, "DECC$GETHOSTBYADDR");
395 pragma Import (C, C_Gethostbyname, "DECC$GETHOSTBYNAME");
396 pragma Import (C, C_Gethostname, "DECC$GETHOSTNAME");
397 pragma Import (C, C_Getpeername, "DECC$GETPEERNAME");
398 pragma Import (C, C_Getservbyname, "DECC$GETSERVBYNAME");
399 pragma Import (C, C_Getservbyport, "DECC$GETSERVBYPORT");
400 pragma Import (C, C_Getsockname, "DECC$GETSOCKNAME");
401 pragma Import (C, C_Getsockopt, "DECC$GETSOCKOPT");
402 pragma Import (C, C_Inet_Addr, "DECC$INET_ADDR");
403 pragma Import (C, C_Listen, "DECC$LISTEN");
404 pragma Import (C, C_Select, "DECC$SELECT");
405 pragma Import (C, C_Setsockopt, "DECC$SETSOCKOPT");
406 pragma Import (C, C_Shutdown, "DECC$SHUTDOWN");
407 pragma Import (C, C_Strerror, "DECC$STRERROR");
408 pragma Import (C, C_System, "DECC$SYSTEM");
410 pragma Import (C, Free_Socket_Set, "__gnat_free_socket_set");
411 pragma Import (C, Get_Socket_From_Set, "__gnat_get_socket_from_set");
412 pragma Import (C, Is_Socket_In_Set, "__gnat_is_socket_in_set");
413 pragma Import (C, Last_Socket_In_Set, "__gnat_last_socket_in_set");
414 pragma Import (C, New_Socket_Set, "__gnat_new_socket_set");
415 pragma Import (C, Insert_Socket_In_Set, "__gnat_insert_socket_in_set");
416 pragma Import (C, Remove_Socket_From_Set, "__gnat_remove_socket_from_set");
417 end GNAT.Sockets.Thin;