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-2003 Ada Core Technologies, Inc. --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 String;
65 -- Returns the error message string for the error number Errno. If
66 -- Errno is not known it returns "Unknown system error".
68 subtype Fd_Set_Access
is System
.Address
;
69 No_Fd_Set
: constant Fd_Set_Access
:= System
.Null_Address
;
71 type Timeval_Unit
is new C
.int
;
72 pragma Convention
(C
, Timeval_Unit
);
74 type Timeval
is record
75 Tv_Sec
: Timeval_Unit
;
76 Tv_Usec
: Timeval_Unit
;
78 pragma Convention
(C
, Timeval
);
80 type Timeval_Access
is access all Timeval
;
81 pragma Convention
(C
, Timeval_Access
);
83 Immediat
: constant Timeval
:= (0, 0);
85 type Int_Access
is access all C
.int
;
86 pragma Convention
(C
, Int_Access
);
87 -- Access to C integers
89 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
90 aliased C
.Strings
.chars_ptr
;
92 package Chars_Ptr_Pointers
is
93 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
95 -- Arrays of C (char *)
97 type In_Addr
is record
98 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
100 pragma Convention
(C
, In_Addr
);
103 type In_Addr_Access
is access all In_Addr
;
104 pragma Convention
(C
, In_Addr_Access
);
105 -- Access to internet address
107 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
108 -- Any internet address (all the interfaces)
110 type In_Addr_Access_Array
is array (C
.size_t
range <>)
111 of aliased In_Addr_Access
;
112 pragma Convention
(C
, In_Addr_Access_Array
);
114 package In_Addr_Access_Pointers
is
115 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
116 -- Array of internet addresses
118 type Sockaddr
is record
119 Sa_Family
: C
.unsigned_short
;
120 Sa_Data
: C
.char_array
(1 .. 14);
122 pragma Convention
(C
, Sockaddr
);
125 type Sockaddr_Access
is access all Sockaddr
;
126 pragma Convention
(C
, Sockaddr_Access
);
127 -- Access to socket address
129 type Sockaddr_In
is record
130 Sin_Family
: C
.unsigned_short
:= Constants
.AF_INET
;
131 Sin_Port
: C
.unsigned_short
:= 0;
132 Sin_Addr
: In_Addr
:= Inaddr_Any
;
133 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
135 pragma Convention
(C
, Sockaddr_In
);
136 -- Internet socket address
138 type Sockaddr_In_Access
is access all Sockaddr_In
;
139 pragma Convention
(C
, Sockaddr_In_Access
);
140 -- Access to internet socket address
143 (Sin
: Sockaddr_In_Access
;
145 pragma Inline
(Set_Length
);
146 -- Set Sin.Sin_Length to Len.
147 -- On this platform, nothing is done as there is no such field.
150 (Sin
: Sockaddr_In_Access
;
152 pragma Inline
(Set_Family
);
153 -- Set Sin.Sin_Family to Family
156 (Sin
: Sockaddr_In_Access
;
157 Port
: C
.unsigned_short
);
158 pragma Inline
(Set_Port
);
159 -- Set Sin.Sin_Port to Port
161 procedure Set_Address
162 (Sin
: Sockaddr_In_Access
;
164 pragma Inline
(Set_Address
);
165 -- Set Sin.Sin_Addr to Address
167 type Hostent
is record
168 H_Name
: C
.Strings
.chars_ptr
;
169 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
172 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
174 pragma Convention
(C
, Hostent
);
177 type Hostent_Access
is access all Hostent
;
178 pragma Convention
(C
, Hostent_Access
);
179 -- Access to host entry
181 type Servent
is record
182 S_Name
: C
.Strings
.chars_ptr
;
183 S_Aliases
: Chars_Ptr_Pointers
.Pointer
;
185 S_Proto
: C
.Strings
.chars_ptr
;
187 pragma Convention
(C
, Servent
);
190 type Servent_Access
is access all Servent
;
191 pragma Convention
(C
, Servent_Access
);
192 -- Access to service entry
194 type Two_Int
is array (0 .. 1) of C
.int
;
195 pragma Convention
(C
, Two_Int
);
200 Addr
: System
.Address
;
201 Addrlen
: access C
.int
)
206 Name
: System
.Address
;
216 Name
: System
.Address
;
220 function C_Gethostbyaddr
221 (Addr
: System
.Address
;
224 return Hostent_Access
;
226 function C_Gethostbyname
227 (Name
: C
.char_array
)
228 return Hostent_Access
;
230 function C_Gethostname
231 (Name
: System
.Address
;
235 function C_Getpeername
237 Name
: System
.Address
;
238 Namelen
: access C
.int
)
241 function C_Getservbyname
242 (Name
: C
.char_array
;
243 Proto
: C
.char_array
)
244 return Servent_Access
;
246 function C_Getservbyport
248 Proto
: C
.char_array
)
249 return Servent_Access
;
251 function C_Getsockname
253 Name
: System
.Address
;
254 Namelen
: access C
.int
)
257 function C_Getsockopt
261 Optval
: System
.Address
;
262 Optlen
: access C
.int
)
266 (Cp
: C
.Strings
.chars_ptr
)
275 function C_Listen
(S
, Backlog
: C
.int
) return C
.int
;
279 Buf
: System
.Address
;
285 Iov
: System
.Address
;
291 Msg
: System
.Address
;
298 Msg
: System
.Address
;
301 From
: Sockaddr_In_Access
;
302 Fromlen
: access C
.int
)
307 Readfds
: Fd_Set_Access
;
308 Writefds
: Fd_Set_Access
;
309 Exceptfds
: Fd_Set_Access
;
310 Timeout
: Timeval_Access
)
315 Msg
: System
.Address
;
322 Msg
: System
.Address
;
325 To
: Sockaddr_In_Access
;
329 function C_Setsockopt
333 Optval
: System
.Address
;
350 return C
.Strings
.chars_ptr
;
353 (Command
: System
.Address
)
358 Buf
: System
.Address
;
364 Iov
: System
.Address
;
368 procedure Free_Socket_Set
369 (Set
: Fd_Set_Access
);
370 -- Free system-dependent socket set
372 procedure Get_Socket_From_Set
373 (Set
: Fd_Set_Access
;
376 -- Get last socket in Socket and remove it from the socket
377 -- set. The parameter Last is a maximum value of the largest
378 -- socket. This hint is used to avoid scanning very large socket
379 -- sets. After a call to Get_Socket_From_Set, Last is set back to
380 -- the real largest socket in the socket set.
382 procedure Insert_Socket_In_Set
383 (Set
: Fd_Set_Access
;
385 -- Insert socket in the socket set
387 function Is_Socket_In_Set
388 (Set
: Fd_Set_Access
;
391 -- Check whether Socket is in the socket set
393 procedure Last_Socket_In_Set
394 (Set
: Fd_Set_Access
;
396 -- Find the largest socket in the socket set. This is needed for
397 -- select(). When Last_Socket_In_Set is called, parameter Last is
398 -- a maximum value of the largest socket. This hint is used to
399 -- avoid scanning very large socket sets. After the call, Last is
400 -- set back to the real largest socket in the socket set.
402 function New_Socket_Set
403 (Set
: Fd_Set_Access
)
404 return Fd_Set_Access
;
405 -- Allocate a new socket set which is a system-dependent structure
406 -- and initialize by copying Set if it is non-null, by making it
409 procedure Remove_Socket_From_Set
410 (Set
: Fd_Set_Access
;
412 -- Remove socket from the socket set
415 procedure Initialize
(Process_Blocking_IO
: Boolean);
418 pragma Import
(C
, C_Bind
, "bind");
419 pragma Import
(C
, C_Close
, "close");
420 pragma Import
(C
, C_Gethostbyaddr
, "gethostbyaddr");
421 pragma Import
(C
, C_Gethostbyname
, "gethostbyname");
422 pragma Import
(C
, C_Gethostname
, "gethostname");
423 pragma Import
(C
, C_Getpeername
, "getpeername");
424 pragma Import
(C
, C_Getservbyname
, "getservbyname");
425 pragma Import
(C
, C_Getservbyport
, "getservbyport");
426 pragma Import
(C
, C_Getsockname
, "getsockname");
427 pragma Import
(C
, C_Getsockopt
, "getsockopt");
428 pragma Import
(C
, C_Inet_Addr
, "inet_addr");
429 pragma Import
(C
, C_Listen
, "listen");
430 pragma Import
(C
, C_Read
, "read");
431 pragma Import
(C
, C_Readv
, "readv");
432 pragma Import
(C
, C_Select
, "select");
433 pragma Import
(C
, C_Setsockopt
, "setsockopt");
434 pragma Import
(C
, C_Shutdown
, "shutdown");
435 pragma Import
(C
, C_Strerror
, "strerror");
436 pragma Import
(C
, C_System
, "system");
437 pragma Import
(C
, C_Write
, "write");
438 pragma Import
(C
, C_Writev
, "writev");
440 pragma Import
(C
, Free_Socket_Set
, "__gnat_free_socket_set");
441 pragma Import
(C
, Get_Socket_From_Set
, "__gnat_get_socket_from_set");
442 pragma Import
(C
, Is_Socket_In_Set
, "__gnat_is_socket_in_set");
443 pragma Import
(C
, Last_Socket_In_Set
, "__gnat_last_socket_in_set");
444 pragma Import
(C
, New_Socket_Set
, "__gnat_new_socket_set");
445 pragma Import
(C
, Insert_Socket_In_Set
, "__gnat_insert_socket_in_set");
446 pragma Import
(C
, Remove_Socket_From_Set
, "__gnat_remove_socket_from_set");
447 end GNAT
.Sockets
.Thin
;