1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
9 -- Copyright (C) 2001-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 default version
40 with Interfaces
.C
.Pointers
;
41 with Interfaces
.C
.Strings
;
42 with GNAT
.Sockets
.Constants
;
47 package GNAT
.Sockets
.Thin
is
49 -- This package is intended for hosts implementing BSD sockets with a
50 -- standard interface. It will be used as a default for all the platforms
51 -- that do not have a specific version of this file.
53 package C
renames Interfaces
.C
;
56 -- This is so we can declare the Failure constant below
58 Success
: constant C
.int
:= 0;
59 Failure
: constant C
.int
:= -1;
61 function Socket_Errno
return Integer renames GNAT
.OS_Lib
.Errno
;
62 -- Returns last socket error number
64 function Socket_Error_Message
(Errno
: Integer) return C
.Strings
.chars_ptr
;
65 -- Returns the error message string for the error number Errno. If Errno is
66 -- not known it returns "Unknown system error".
68 function Host_Errno
return Integer;
69 pragma Import
(C
, Host_Errno
, "__gnat_get_h_errno");
70 -- Returns last host error number
72 subtype Fd_Set_Access
is System
.Address
;
73 No_Fd_Set
: constant Fd_Set_Access
:= System
.Null_Address
;
76 range -2 ** (8 * Constants
.SIZEOF_tv_sec
- 1)
77 .. 2 ** (8 * Constants
.SIZEOF_tv_sec
- 1) - 1;
78 for time_t
'Size use 8 * Constants
.SIZEOF_tv_sec
;
79 pragma Convention
(C
, time_t
);
82 range -2 ** (8 * Constants
.SIZEOF_tv_usec
- 1)
83 .. 2 ** (8 * Constants
.SIZEOF_tv_usec
- 1) - 1;
84 for suseconds_t
'Size use 8 * Constants
.SIZEOF_tv_usec
;
85 pragma Convention
(C
, suseconds_t
);
87 type Timeval
is record
89 Tv_Usec
: suseconds_t
;
91 pragma Convention
(C
, Timeval
);
93 type Timeval_Access
is access all Timeval
;
94 pragma Convention
(C
, Timeval_Access
);
96 Immediat
: constant Timeval
:= (0, 0);
98 type Int_Access
is access all C
.int
;
99 pragma Convention
(C
, Int_Access
);
100 -- Access to C integers
102 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
103 aliased C
.Strings
.chars_ptr
;
105 package Chars_Ptr_Pointers
is
106 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
108 -- Arrays of C (char *)
110 type In_Addr
is record
111 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
113 pragma Convention
(C
, In_Addr
);
116 type In_Addr_Access
is access all In_Addr
;
117 pragma Convention
(C
, In_Addr_Access
);
118 -- Access to internet address
120 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
121 -- Any internet address (all the interfaces)
123 type In_Addr_Access_Array
is array (C
.size_t
range <>)
124 of aliased In_Addr_Access
;
125 pragma Convention
(C
, In_Addr_Access_Array
);
127 package In_Addr_Access_Pointers
is
128 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
129 -- Array of internet addresses
131 type Sockaddr
is record
132 Sa_Family
: C
.unsigned_short
;
133 Sa_Data
: C
.char_array
(1 .. 14);
135 pragma Convention
(C
, Sockaddr
);
138 type Sockaddr_Access
is access all Sockaddr
;
139 pragma Convention
(C
, Sockaddr_Access
);
140 -- Access to socket address
142 type Sockaddr_In
is record
143 Sin_Family
: C
.unsigned_short
:= Constants
.AF_INET
;
144 Sin_Port
: C
.unsigned_short
:= 0;
145 Sin_Addr
: In_Addr
:= Inaddr_Any
;
146 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
148 pragma Convention
(C
, Sockaddr_In
);
149 -- Internet socket address
151 type Sockaddr_In_Access
is access all Sockaddr_In
;
152 pragma Convention
(C
, Sockaddr_In_Access
);
153 -- Access to internet socket address
156 (Sin
: Sockaddr_In_Access
;
158 pragma Inline
(Set_Length
);
159 -- Set Sin.Sin_Length to Len.
160 -- On this platform, nothing is done as there is no such field.
163 (Sin
: Sockaddr_In_Access
;
165 pragma Inline
(Set_Family
);
166 -- Set Sin.Sin_Family to Family
169 (Sin
: Sockaddr_In_Access
;
170 Port
: C
.unsigned_short
);
171 pragma Inline
(Set_Port
);
172 -- Set Sin.Sin_Port to Port
174 procedure Set_Address
175 (Sin
: Sockaddr_In_Access
;
177 pragma Inline
(Set_Address
);
178 -- Set Sin.Sin_Addr to Address
180 type Hostent
is record
181 H_Name
: C
.Strings
.chars_ptr
;
182 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
185 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
187 pragma Convention
(C
, Hostent
);
190 type Hostent_Access
is access all Hostent
;
191 pragma Convention
(C
, Hostent_Access
);
192 -- Access to host entry
194 type Servent
is record
195 S_Name
: C
.Strings
.chars_ptr
;
196 S_Aliases
: Chars_Ptr_Pointers
.Pointer
;
198 S_Proto
: C
.Strings
.chars_ptr
;
200 pragma Convention
(C
, Servent
);
203 type Servent_Access
is access all Servent
;
204 pragma Convention
(C
, Servent_Access
);
205 -- Access to service entry
207 type Two_Int
is array (0 .. 1) of C
.int
;
208 pragma Convention
(C
, Two_Int
);
213 Addr
: System
.Address
;
214 Addrlen
: access C
.int
) return C
.int
;
218 Name
: System
.Address
;
219 Namelen
: C
.int
) return C
.int
;
222 (Fd
: C
.int
) return C
.int
;
226 Name
: System
.Address
;
227 Namelen
: C
.int
) return C
.int
;
229 function C_Gethostbyaddr
230 (Addr
: System
.Address
;
232 Typ
: C
.int
) return Hostent_Access
;
234 function C_Gethostbyname
235 (Name
: C
.char_array
) return Hostent_Access
;
237 function C_Gethostname
238 (Name
: System
.Address
;
239 Namelen
: C
.int
) return C
.int
;
241 function C_Getpeername
243 Name
: System
.Address
;
244 Namelen
: access C
.int
) return C
.int
;
246 function C_Getservbyname
247 (Name
: C
.char_array
;
248 Proto
: C
.char_array
) return Servent_Access
;
250 function C_Getservbyport
252 Proto
: C
.char_array
) return Servent_Access
;
254 function C_Getsockname
256 Name
: System
.Address
;
257 Namelen
: access C
.int
) return C
.int
;
259 function C_Getsockopt
263 Optval
: System
.Address
;
264 Optlen
: access C
.int
) return C
.int
;
267 (Cp
: C
.Strings
.chars_ptr
) return C
.int
;
272 Arg
: Int_Access
) return C
.int
;
276 Backlog
: C
.int
) return C
.int
;
280 Iov
: System
.Address
;
281 Iovcnt
: C
.int
) return C
.int
;
285 Msg
: System
.Address
;
287 Flags
: C
.int
) return C
.int
;
291 Msg
: System
.Address
;
294 From
: Sockaddr_In_Access
;
295 Fromlen
: access C
.int
) return C
.int
;
299 Readfds
: Fd_Set_Access
;
300 Writefds
: Fd_Set_Access
;
301 Exceptfds
: Fd_Set_Access
;
302 Timeout
: Timeval_Access
) return C
.int
;
306 Msg
: System
.Address
;
308 Flags
: C
.int
) return C
.int
;
312 Msg
: System
.Address
;
315 To
: Sockaddr_In_Access
;
316 Tolen
: C
.int
) return C
.int
;
318 function C_Setsockopt
322 Optval
: System
.Address
;
323 Optlen
: C
.int
) return C
.int
;
327 How
: C
.int
) return C
.int
;
332 Protocol
: C
.int
) return C
.int
;
335 (Errnum
: C
.int
) return C
.Strings
.chars_ptr
;
338 (Command
: System
.Address
) return C
.int
;
342 Iov
: System
.Address
;
343 Iovcnt
: C
.int
) return C
.int
;
345 procedure Free_Socket_Set
346 (Set
: Fd_Set_Access
);
347 -- Free system-dependent socket set
349 procedure Get_Socket_From_Set
350 (Set
: Fd_Set_Access
;
353 -- Get last socket in Socket and remove it from the socket
354 -- set. The parameter Last is a maximum value of the largest
355 -- socket. This hint is used to avoid scanning very large socket
356 -- sets. After a call to Get_Socket_From_Set, Last is set back to
357 -- the real largest socket in the socket set.
359 procedure Insert_Socket_In_Set
360 (Set
: Fd_Set_Access
;
362 -- Insert socket in the socket set
364 function Is_Socket_In_Set
365 (Set
: Fd_Set_Access
;
366 Socket
: C
.int
) return C
.int
;
367 -- Check whether Socket is in the socket set, return a non-zero
368 -- value if it is, zero if it is not.
370 procedure Last_Socket_In_Set
371 (Set
: Fd_Set_Access
;
373 -- Find the largest socket in the socket set. This is needed for select().
374 -- When Last_Socket_In_Set is called, parameter Last is a maximum value of
375 -- the largest socket. This hint is used to avoid scanning very large
376 -- socket sets. After the call, Last is set back to the real largest socket
377 -- in the socket set.
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 and
382 -- initialize by copying Set if it is non-null, by making it empty
385 procedure Remove_Socket_From_Set
386 (Set
: Fd_Set_Access
;
388 -- Remove socket from the socket set
391 procedure Initialize
(Process_Blocking_IO
: Boolean);
394 pragma Import
(C
, C_Bind
, "bind");
395 pragma Import
(C
, C_Close
, "close");
396 pragma Import
(C
, C_Gethostbyaddr
, "gethostbyaddr");
397 pragma Import
(C
, C_Gethostbyname
, "gethostbyname");
398 pragma Import
(C
, C_Gethostname
, "gethostname");
399 pragma Import
(C
, C_Getpeername
, "getpeername");
400 pragma Import
(C
, C_Getservbyname
, "getservbyname");
401 pragma Import
(C
, C_Getservbyport
, "getservbyport");
402 pragma Import
(C
, C_Getsockname
, "getsockname");
403 pragma Import
(C
, C_Getsockopt
, "getsockopt");
404 pragma Import
(C
, C_Inet_Addr
, "inet_addr");
405 pragma Import
(C
, C_Listen
, "listen");
406 pragma Import
(C
, C_Readv
, "readv");
407 pragma Import
(C
, C_Select
, "select");
408 pragma Import
(C
, C_Setsockopt
, "setsockopt");
409 pragma Import
(C
, C_Shutdown
, "shutdown");
410 pragma Import
(C
, C_Strerror
, "strerror");
411 pragma Import
(C
, C_System
, "system");
412 pragma Import
(C
, C_Writev
, "writev");
414 pragma Import
(C
, Free_Socket_Set
, "__gnat_free_socket_set");
415 pragma Import
(C
, Get_Socket_From_Set
, "__gnat_get_socket_from_set");
416 pragma Import
(C
, Is_Socket_In_Set
, "__gnat_is_socket_in_set");
417 pragma Import
(C
, Last_Socket_In_Set
, "__gnat_last_socket_in_set");
418 pragma Import
(C
, New_Socket_Set
, "__gnat_new_socket_set");
419 pragma Import
(C
, Insert_Socket_In_Set
, "__gnat_insert_socket_in_set");
420 pragma Import
(C
, Remove_Socket_From_Set
, "__gnat_remove_socket_from_set");
421 end GNAT
.Sockets
.Thin
;