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-2004 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 version for VxWorks
40 with Interfaces
.C
.Pointers
;
42 with Ada
.Unchecked_Conversion
;
43 with Interfaces
.C
.Strings
;
44 with GNAT
.Sockets
.Constants
;
49 package GNAT
.Sockets
.Thin
is
51 package C
renames Interfaces
.C
;
54 -- This is so we can declare the Failure constant below
56 Success
: constant C
.int
:= 0;
57 Failure
: constant C
.int
:= -1;
59 function Socket_Errno
return Integer renames GNAT
.OS_Lib
.Errno
;
60 -- Returns last socket error number.
62 function Socket_Error_Message
(Errno
: Integer) return C
.Strings
.chars_ptr
;
63 -- Returns the error message string for the error number Errno. If
64 -- Errno is not known it returns "Unknown system error".
66 subtype Fd_Set_Access
is System
.Address
;
67 No_Fd_Set
: constant Fd_Set_Access
:= System
.Null_Address
;
69 type Timeval_Unit
is new C
.int
;
70 pragma Convention
(C
, Timeval_Unit
);
72 type Timeval
is record
73 Tv_Sec
: Timeval_Unit
;
74 Tv_Usec
: Timeval_Unit
;
76 pragma Convention
(C
, Timeval
);
78 type Timeval_Access
is access all Timeval
;
79 pragma Convention
(C
, Timeval_Access
);
81 Immediat
: constant Timeval
:= (0, 0);
83 type Int_Access
is access all C
.int
;
84 pragma Convention
(C
, Int_Access
);
85 -- Access to C integers
87 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
88 aliased C
.Strings
.chars_ptr
;
90 package Chars_Ptr_Pointers
is
91 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
93 -- Arrays of C (char *)
95 type In_Addr
is record
96 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
98 pragma Convention
(C
, In_Addr
);
101 function To_In_Addr
is new Ada
.Unchecked_Conversion
(C
.int
, 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_Length
: C
.unsigned_char
;
120 Sa_Family
: C
.unsigned_char
;
121 Sa_Data
: C
.char_array
(1 .. 14);
123 pragma Convention
(C
, Sockaddr
);
126 type Sockaddr_Access
is access all Sockaddr
;
127 pragma Convention
(C
, Sockaddr_Access
);
128 -- Access to socket address
130 type Sockaddr_In
is record
131 Sin_Length
: C
.unsigned_char
:= 0;
132 Sin_Family
: C
.unsigned_char
:= Constants
.AF_INET
;
133 Sin_Port
: C
.unsigned_short
:= 0;
134 Sin_Addr
: In_Addr
:= Inaddr_Any
;
135 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
137 pragma Convention
(C
, Sockaddr_In
);
138 -- Internet socket address
140 type Sockaddr_In_Access
is access all Sockaddr_In
;
141 pragma Convention
(C
, Sockaddr_In_Access
);
142 -- Access to internet socket address
145 (Sin
: Sockaddr_In_Access
;
147 pragma Inline
(Set_Length
);
148 -- Set Sin.Sin_Length to Len.
151 (Sin
: Sockaddr_In_Access
;
153 pragma Inline
(Set_Family
);
154 -- Set Sin.Sin_Family to Family.
157 (Sin
: Sockaddr_In_Access
;
158 Port
: C
.unsigned_short
);
159 pragma Inline
(Set_Port
);
160 -- Set Sin.Sin_Port to Port.
162 procedure Set_Address
163 (Sin
: Sockaddr_In_Access
;
165 pragma Inline
(Set_Address
);
166 -- Set Sin.Sin_Addr to Address.
168 type Hostent
is record
169 H_Name
: C
.Strings
.chars_ptr
;
170 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
173 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
175 pragma Convention
(C
, Hostent
);
178 type Hostent_Access
is access all Hostent
;
179 pragma Convention
(C
, Hostent_Access
);
180 -- Access to host entry
182 type Servent
is record
183 S_Name
: C
.Strings
.chars_ptr
;
184 S_Aliases
: Chars_Ptr_Pointers
.Pointer
;
186 S_Proto
: C
.Strings
.chars_ptr
;
188 pragma Convention
(C
, Servent
);
191 type Servent_Access
is access all Servent
;
192 pragma Convention
(C
, Servent_Access
);
193 -- Access to service entry
195 type Two_Int
is array (0 .. 1) of C
.int
;
196 pragma Convention
(C
, Two_Int
);
201 Addr
: System
.Address
;
202 Addrlen
: access C
.int
)
207 Name
: System
.Address
;
217 Name
: System
.Address
;
221 function C_Gethostbyaddr
222 (Addr
: System
.Address
;
225 return Hostent_Access
;
227 function C_Gethostbyname
228 (Name
: C
.char_array
)
229 return Hostent_Access
;
231 function C_Gethostname
232 (Name
: System
.Address
;
236 function C_Getpeername
238 Name
: System
.Address
;
239 Namelen
: access C
.int
)
242 function C_Getservbyname
243 (Name
: C
.char_array
;
244 Proto
: C
.char_array
)
245 return Servent_Access
;
247 function C_Getservbyport
249 Proto
: C
.char_array
)
250 return Servent_Access
;
252 function C_Getsockname
254 Name
: System
.Address
;
255 Namelen
: access C
.int
)
258 function C_Getsockopt
262 Optval
: System
.Address
;
263 Optlen
: access C
.int
)
267 (Cp
: C
.Strings
.chars_ptr
)
276 function C_Listen
(S
, Backlog
: C
.int
) return C
.int
;
280 Buf
: System
.Address
;
286 Iov
: System
.Address
;
292 Msg
: System
.Address
;
299 Msg
: System
.Address
;
302 From
: Sockaddr_In_Access
;
303 Fromlen
: access C
.int
)
308 Readfds
: Fd_Set_Access
;
309 Writefds
: Fd_Set_Access
;
310 Exceptfds
: Fd_Set_Access
;
311 Timeout
: Timeval_Access
)
316 Msg
: System
.Address
;
323 Msg
: System
.Address
;
326 To
: Sockaddr_In_Access
;
330 function C_Setsockopt
334 Optval
: System
.Address
;
351 return C
.Strings
.chars_ptr
;
354 (Command
: System
.Address
)
359 Buf
: System
.Address
;
365 Iov
: System
.Address
;
369 procedure Free_Socket_Set
370 (Set
: Fd_Set_Access
);
371 -- Free system-dependent socket set
373 procedure Get_Socket_From_Set
374 (Set
: Fd_Set_Access
;
377 -- Get last socket in Socket and remove it from the socket
378 -- set. The parameter Last is a maximum value of the largest
379 -- socket. This hint is used to avoid scanning very large socket
380 -- sets. After a call to Get_Socket_From_Set, Last is set back to
381 -- the real largest socket in the socket set.
383 procedure Insert_Socket_In_Set
384 (Set
: Fd_Set_Access
;
386 -- Insert socket in the socket set
388 function Is_Socket_In_Set
389 (Set
: Fd_Set_Access
;
392 -- Check whether Socket is in the socket set
394 procedure Last_Socket_In_Set
395 (Set
: Fd_Set_Access
;
397 -- Find the largest socket in the socket set. This is needed for
398 -- select(). When Last_Socket_In_Set is called, parameter Last is
399 -- a maximum value of the largest socket. This hint is used to
400 -- avoid scanning very large socket sets. After the call, Last is
401 -- set back to the real largest socket in the socket set.
403 function New_Socket_Set
404 (Set
: Fd_Set_Access
)
405 return Fd_Set_Access
;
406 -- Allocate a new socket set which is a system-dependent structure
407 -- and initialize by copying Set if it is non-null, by making it
410 procedure Remove_Socket_From_Set
411 (Set
: Fd_Set_Access
;
413 -- Remove socket from the socket set
416 procedure Initialize
(Process_Blocking_IO
: Boolean);
420 pragma Import
(C
, C_Bind
, "bind");
421 pragma Import
(C
, C_Close
, "close");
422 pragma Import
(C
, C_Gethostname
, "gethostname");
423 pragma Import
(C
, C_Getpeername
, "getpeername");
424 pragma Import
(C
, C_Getsockname
, "getsockname");
425 pragma Import
(C
, C_Getsockopt
, "getsockopt");
426 pragma Import
(C
, C_Inet_Addr
, "inet_addr");
427 pragma Import
(C
, C_Listen
, "listen");
428 pragma Import
(C
, C_Read
, "read");
429 pragma Import
(C
, C_Readv
, "readv");
430 pragma Import
(C
, C_Select
, "select");
431 pragma Import
(C
, C_Setsockopt
, "setsockopt");
432 pragma Import
(C
, C_Shutdown
, "shutdown");
433 pragma Import
(C
, C_Strerror
, "strerror");
434 pragma Import
(C
, C_System
, "system");
435 pragma Import
(C
, C_Write
, "write");
436 pragma Import
(C
, C_Writev
, "writev");
438 pragma Import
(C
, Free_Socket_Set
, "__gnat_free_socket_set");
439 pragma Import
(C
, Get_Socket_From_Set
, "__gnat_get_socket_from_set");
440 pragma Import
(C
, Is_Socket_In_Set
, "__gnat_is_socket_in_set");
441 pragma Import
(C
, Last_Socket_In_Set
, "__gnat_last_socket_in_set");
442 pragma Import
(C
, New_Socket_Set
, "__gnat_new_socket_set");
443 pragma Import
(C
, Insert_Socket_In_Set
, "__gnat_insert_socket_in_set");
444 pragma Import
(C
, Remove_Socket_From_Set
, "__gnat_remove_socket_from_set");
446 end GNAT
.Sockets
.Thin
;