1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
10 -- Copyright (C) 2001 Ada Core Technologies, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 ------------------------------------------------------------------------------
34 with Interfaces
.C
.Pointers
;
36 with Interfaces
.C
.Strings
;
37 with GNAT
.Sockets
.Constants
;
42 package GNAT
.Sockets
.Thin
is
44 -- ??? more comments needed ???
46 -- This package is intended for hosts implementing BSD sockets with a
47 -- standard interface. It will be used as a default for all the platforms
48 -- that do not have a specific version of this file.
50 package C
renames Interfaces
.C
;
53 -- This is so we can declare the Failure constant below
55 Success
: constant C
.int
:= 0;
56 Failure
: constant C
.int
:= -1;
58 function Socket_Errno
return Integer renames GNAT
.OS_Lib
.Errno
;
59 -- Returns last socket error number.
61 function Socket_Error_Message
(Errno
: Integer) return String;
62 -- Returns the error message string for the error number Errno. If
63 -- Errno is not known it returns "Unknown system error".
65 type Fd_Set
is mod 2 ** 32;
66 pragma Convention
(C
, Fd_Set
);
68 Null_Fd_Set
: constant Fd_Set
:= 0;
70 type Fd_Set_Access
is access all Fd_Set
;
71 pragma Convention
(C
, Fd_Set_Access
);
73 type Timeval_Unit
is new C
.int
;
74 pragma Convention
(C
, Timeval_Unit
);
76 type Timeval
is record
77 Tv_Sec
: Timeval_Unit
;
78 Tv_Usec
: Timeval_Unit
;
80 pragma Convention
(C
, Timeval
);
82 type Timeval_Access
is access all Timeval
;
83 pragma Convention
(C
, Timeval_Access
);
85 Immediat
: constant Timeval
:= (0, 0);
87 type Int_Access
is access all C
.int
;
88 pragma Convention
(C
, Int_Access
);
89 -- Access to C integers
91 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
92 aliased C
.Strings
.chars_ptr
;
94 package Chars_Ptr_Pointers
is
95 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
97 -- Arrays of C (char *)
99 type In_Addr
is record
100 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
102 pragma Convention
(C
, In_Addr
);
105 type In_Addr_Access
is access all In_Addr
;
106 pragma Convention
(C
, In_Addr_Access
);
107 -- Access to internet address
109 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
110 -- Any internet address (all the interfaces)
112 type In_Addr_Access_Array
is array (C
.size_t
range <>)
113 of aliased In_Addr_Access
;
114 pragma Convention
(C
, In_Addr_Access_Array
);
116 package In_Addr_Access_Pointers
is
117 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
118 -- Array of internet addresses
120 type Sockaddr
is record
121 Sa_Family
: C
.unsigned_short
;
122 Sa_Data
: C
.char_array
(1 .. 14);
124 pragma Convention
(C
, Sockaddr
);
127 type Sockaddr_Access
is access all Sockaddr
;
128 pragma Convention
(C
, Sockaddr_Access
);
129 -- Access to socket address
131 type Sockaddr_In
is record
132 Sin_Family
: C
.unsigned_short
:= 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
144 type Hostent
is record
145 H_Name
: C
.Strings
.chars_ptr
;
146 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
149 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
151 pragma Convention
(C
, Hostent
);
154 type Hostent_Access
is access all Hostent
;
155 pragma Convention
(C
, Hostent_Access
);
156 -- Access to host entry
158 type Two_Int
is array (0 .. 1) of C
.int
;
159 pragma Convention
(C
, Two_Int
);
164 Addr
: System
.Address
;
165 Addrlen
: access C
.int
)
170 Name
: System
.Address
;
180 Name
: System
.Address
;
184 function C_Gethostbyaddr
185 (Addr
: System
.Address
;
188 return Hostent_Access
;
190 function C_Gethostbyname
191 (Name
: C
.char_array
)
192 return Hostent_Access
;
194 function C_Gethostname
195 (Name
: System
.Address
;
199 function C_Getpeername
201 Name
: System
.Address
;
202 Namelen
: access C
.int
)
205 function C_Getsockname
207 Name
: System
.Address
;
208 Namelen
: access C
.int
)
211 function C_Getsockopt
215 Optval
: System
.Address
;
216 Optlen
: access C
.int
)
220 (Cp
: C
.Strings
.chars_ptr
)
229 function C_Listen
(S
, Backlog
: C
.int
) return C
.int
;
233 Buf
: System
.Address
;
239 Msg
: System
.Address
;
246 Msg
: System
.Address
;
249 From
: Sockaddr_In_Access
;
250 Fromlen
: access C
.int
)
255 Readfds
: Fd_Set_Access
;
256 Writefds
: Fd_Set_Access
;
257 Exceptfds
: Fd_Set_Access
;
258 Timeout
: Timeval_Access
)
263 Msg
: System
.Address
;
270 Msg
: System
.Address
;
273 To
: Sockaddr_In_Access
;
277 function C_Setsockopt
281 Optval
: System
.Address
;
298 return C
.Strings
.chars_ptr
;
301 (Command
: System
.Address
)
306 Buf
: System
.Address
;
310 -- Return highest numbered socket (what does this refer to???)
312 procedure Clear
(Item
: in out Fd_Set
; Socket
: in C
.int
);
313 procedure Empty
(Item
: in out Fd_Set
);
314 function Is_Empty
(Item
: Fd_Set
) return Boolean;
315 function Is_Set
(Item
: Fd_Set
; Socket
: C
.int
) return Boolean;
316 function Max
(Item
: Fd_Set
) return C
.int
;
317 procedure Set
(Item
: in out Fd_Set
; Socket
: in C
.int
);
320 procedure Initialize
(Process_Blocking_IO
: Boolean);
324 pragma Import
(C
, C_Bind
, "bind");
325 pragma Import
(C
, C_Close
, "close");
326 pragma Import
(C
, C_Gethostbyaddr
, "gethostbyaddr");
327 pragma Import
(C
, C_Gethostbyname
, "gethostbyname");
328 pragma Import
(C
, C_Gethostname
, "gethostname");
329 pragma Import
(C
, C_Getpeername
, "getpeername");
330 pragma Import
(C
, C_Getsockname
, "getsockname");
331 pragma Import
(C
, C_Getsockopt
, "getsockopt");
332 pragma Import
(C
, C_Inet_Addr
, "inet_addr");
333 pragma Import
(C
, C_Listen
, "listen");
334 pragma Import
(C
, C_Read
, "read");
335 pragma Import
(C
, C_Select
, "select");
336 pragma Import
(C
, C_Setsockopt
, "setsockopt");
337 pragma Import
(C
, C_Shutdown
, "shutdown");
338 pragma Import
(C
, C_Strerror
, "strerror");
339 pragma Import
(C
, C_System
, "system");
340 pragma Import
(C
, C_Write
, "write");
342 end GNAT
.Sockets
.Thin
;