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 version for VxWorks
40 with Interfaces
.C
.Pointers
;
41 with Interfaces
.C
.Strings
;
43 with Ada
.Unchecked_Conversion
;
46 with GNAT
.Sockets
.Constants
;
50 package GNAT
.Sockets
.Thin
is
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 function To_In_Addr
is new Ada
.Unchecked_Conversion
(C
.int
, In_Addr
);
132 type In_Addr_Access
is access all In_Addr
;
133 pragma Convention
(C
, In_Addr_Access
);
134 -- Access to internet address
136 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
137 -- Any internet address (all the interfaces)
139 type In_Addr_Access_Array
is array (C
.size_t
range <>)
140 of aliased In_Addr_Access
;
141 pragma Convention
(C
, In_Addr_Access_Array
);
143 package In_Addr_Access_Pointers
is
144 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
145 -- Array of internet addresses
147 type Sockaddr
is record
148 Sa_Length
: C
.unsigned_char
;
149 Sa_Family
: C
.unsigned_char
;
150 Sa_Data
: C
.char_array
(1 .. 14);
152 pragma Convention
(C
, Sockaddr
);
155 type Sockaddr_Access
is access all Sockaddr
;
156 pragma Convention
(C
, Sockaddr_Access
);
157 -- Access to socket address
159 type Sockaddr_In
is record
160 Sin_Length
: C
.unsigned_char
:= 0;
161 Sin_Family
: C
.unsigned_char
:= Constants
.AF_INET
;
162 Sin_Port
: C
.unsigned_short
:= 0;
163 Sin_Addr
: In_Addr
:= Inaddr_Any
;
164 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
166 pragma Convention
(C
, Sockaddr_In
);
167 -- Internet socket address
169 type Sockaddr_In_Access
is access all Sockaddr_In
;
170 pragma Convention
(C
, Sockaddr_In_Access
);
171 -- Access to internet socket address
174 (Sin
: Sockaddr_In_Access
;
176 pragma Inline
(Set_Length
);
177 -- Set Sin.Sin_Length to Len
180 (Sin
: Sockaddr_In_Access
;
182 pragma Inline
(Set_Family
);
183 -- Set Sin.Sin_Family to Family
186 (Sin
: Sockaddr_In_Access
;
187 Port
: C
.unsigned_short
);
188 pragma Inline
(Set_Port
);
189 -- Set Sin.Sin_Port to Port
191 procedure Set_Address
192 (Sin
: Sockaddr_In_Access
;
194 pragma Inline
(Set_Address
);
195 -- Set Sin.Sin_Addr to Address
197 type Hostent
is record
198 H_Name
: C
.Strings
.chars_ptr
;
199 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
202 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
204 pragma Convention
(C
, Hostent
);
207 type Hostent_Access
is access all Hostent
;
208 pragma Convention
(C
, Hostent_Access
);
209 -- Access to host entry
211 type Servent
is record
212 S_Name
: C
.Strings
.chars_ptr
;
213 S_Aliases
: Chars_Ptr_Pointers
.Pointer
;
215 S_Proto
: C
.Strings
.chars_ptr
;
217 pragma Convention
(C
, Servent
);
220 type Servent_Access
is access all Servent
;
221 pragma Convention
(C
, Servent_Access
);
222 -- Access to service entry
224 type Two_Ints
is array (0 .. 1) of C
.int
;
225 pragma Convention
(C
, Two_Ints
);
226 -- Container for two int values
228 subtype Fd_Pair
is Two_Ints
;
229 -- Two_Ints as used for Create_Signalling_Fds: a pair of connected file
230 -- descriptors, one of which (the "read end" of the connection) being used
231 -- for reading, the other one (the "write end") being used for writing.
233 Read_End
: constant := 0;
234 Write_End
: constant := 1;
235 -- Indices into an Fd_Pair value providing access to each of the connected
238 --------------------------------
239 -- Standard library functions --
240 --------------------------------
244 Addr
: System
.Address
;
245 Addrlen
: not null access C
.int
) return C
.int
;
249 Name
: System
.Address
;
250 Namelen
: C
.int
) return C
.int
;
253 (Fd
: C
.int
) return C
.int
;
257 Name
: System
.Address
;
258 Namelen
: C
.int
) return C
.int
;
260 function C_Gethostname
261 (Name
: System
.Address
;
262 Namelen
: C
.int
) return C
.int
;
264 function C_Getpeername
266 Name
: System
.Address
;
267 Namelen
: not null access C
.int
) return C
.int
;
269 function C_Getsockname
271 Name
: System
.Address
;
272 Namelen
: not null access C
.int
) return C
.int
;
274 function C_Getsockopt
278 Optval
: System
.Address
;
279 Optlen
: not null access C
.int
) return C
.int
;
282 (Cp
: C
.Strings
.chars_ptr
) return C
.int
;
287 Arg
: Int_Access
) return C
.int
;
291 Backlog
: C
.int
) return C
.int
;
295 Iov
: System
.Address
;
296 Iovcnt
: C
.int
) return C
.int
;
300 Msg
: System
.Address
;
302 Flags
: C
.int
) return C
.int
;
306 Msg
: System
.Address
;
309 From
: Sockaddr_In_Access
;
310 Fromlen
: not null access C
.int
) return C
.int
;
314 Readfds
: Fd_Set_Access
;
315 Writefds
: Fd_Set_Access
;
316 Exceptfds
: Fd_Set_Access
;
317 Timeout
: Timeval_Access
) return C
.int
;
321 Msg
: System
.Address
;
323 Flags
: C
.int
) return C
.int
;
327 Msg
: System
.Address
;
330 To
: Sockaddr_In_Access
;
331 Tolen
: C
.int
) return C
.int
;
333 function C_Setsockopt
337 Optval
: System
.Address
;
338 Optlen
: C
.int
) return C
.int
;
342 How
: C
.int
) return C
.int
;
347 Protocol
: C
.int
) return C
.int
;
350 (Errnum
: C
.int
) return C
.Strings
.chars_ptr
;
353 (Command
: System
.Address
) return C
.int
;
357 Iov
: System
.Address
;
358 Iovcnt
: C
.int
) return C
.int
;
360 -------------------------------------------------------
361 -- Signalling file descriptors for selector abortion --
362 -------------------------------------------------------
364 package Signalling_Fds
is
366 function Create
(Fds
: not null access Fd_Pair
) return C
.int
;
367 pragma Convention
(C
, Create
);
368 -- Create a pair of connected descriptors suitable for use with C_Select
369 -- (used for signalling in Selector objects).
371 function Read
(Rsig
: C
.int
) return C
.int
;
372 pragma Convention
(C
, Read
);
373 -- Read one byte of data from rsig, the read end of a pair of signalling
374 -- fds created by Create_Signalling_Fds.
376 function Write
(Wsig
: C
.int
) return C
.int
;
377 pragma Convention
(C
, Write
);
378 -- Write one byte of data to wsig, the write end of a pair of signalling
379 -- fds created by Create_Signalling_Fds.
381 procedure Close
(Sig
: C
.int
);
382 pragma Convention
(C
, Close
);
383 -- Close one end of a pair of signalling fds (ignoring any error)
387 ----------------------------
388 -- Socket sets management --
389 ----------------------------
391 procedure Free_Socket_Set
392 (Set
: Fd_Set_Access
);
393 -- Free system-dependent socket set
395 procedure Get_Socket_From_Set
396 (Set
: Fd_Set_Access
;
399 -- Get last socket in Socket and remove it from the socket set. The
400 -- parameter Last is a maximum value of the largest socket. This hint is
401 -- used to avoid scanning very large socket sets. After a call to
402 -- Get_Socket_From_Set, Last is set back to the real largest socket in the
405 procedure Insert_Socket_In_Set
406 (Set
: Fd_Set_Access
;
408 -- Insert socket in the socket set
410 function Is_Socket_In_Set
411 (Set
: Fd_Set_Access
;
412 Socket
: C
.int
) return C
.int
;
413 -- Check whether Socket is in the socket set, return a non-zero
414 -- value if it is, zero if it is not.
416 procedure Last_Socket_In_Set
417 (Set
: Fd_Set_Access
;
419 -- Find the largest socket in the socket set. This is needed for select().
420 -- When Last_Socket_In_Set is called, parameter Last is a maximum value of
421 -- the largest socket. This hint is used to avoid scanning very large
422 -- socket sets. After the call, Last is set back to the real largest socket
423 -- in the socket set.
425 function New_Socket_Set
426 (Set
: Fd_Set_Access
) return Fd_Set_Access
;
427 -- Allocate a new socket set which is a system-dependent structure and
428 -- initialize by copying Set if it is non-null, by making it empty
431 procedure Remove_Socket_From_Set
432 (Set
: Fd_Set_Access
;
434 -- Remove socket from the socket set
436 procedure Initialize
;
440 pragma Import
(C
, C_Bind
, "bind");
441 pragma Import
(C
, C_Close
, "close");
442 pragma Import
(C
, C_Gethostname
, "gethostname");
443 pragma Import
(C
, C_Getpeername
, "getpeername");
444 pragma Import
(C
, C_Getsockname
, "getsockname");
445 pragma Import
(C
, C_Getsockopt
, "getsockopt");
446 pragma Import
(C
, C_Inet_Addr
, "inet_addr");
447 pragma Import
(C
, C_Listen
, "listen");
448 pragma Import
(C
, C_Readv
, "readv");
449 pragma Import
(C
, C_Select
, "select");
450 pragma Import
(C
, C_Setsockopt
, "setsockopt");
451 pragma Import
(C
, C_Shutdown
, "shutdown");
452 pragma Import
(C
, C_Strerror
, "strerror");
453 pragma Import
(C
, C_System
, "system");
454 pragma Import
(C
, C_Writev
, "writev");
456 pragma Import
(C
, Free_Socket_Set
, "__gnat_free_socket_set");
457 pragma Import
(C
, Get_Socket_From_Set
, "__gnat_get_socket_from_set");
458 pragma Import
(C
, Is_Socket_In_Set
, "__gnat_is_socket_in_set");
459 pragma Import
(C
, Last_Socket_In_Set
, "__gnat_last_socket_in_set");
460 pragma Import
(C
, New_Socket_Set
, "__gnat_new_socket_set");
461 pragma Import
(C
, Insert_Socket_In_Set
, "__gnat_insert_socket_in_set");
462 pragma Import
(C
, Remove_Socket_From_Set
, "__gnat_remove_socket_from_set");
464 end GNAT
.Sockets
.Thin
;