1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
11 -- Copyright (C) 2001 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- This version is for NT.
37 with Interfaces
.C
.Pointers
;
38 with Interfaces
.C
.Strings
;
40 with GNAT
.Sockets
.Constants
;
44 package GNAT
.Sockets
.Thin
is
46 -- ??? far more comments required ???
48 package C
renames Interfaces
.C
;
51 -- So that we can declare the Failure constant below.
53 Success
: constant C
.int
:= 0;
54 Failure
: constant C
.int
:= -1;
56 function Socket_Errno
return Integer;
57 -- Returns last socket error number.
59 function Socket_Error_Message
(Errno
: Integer) return String;
60 -- Returns the error message string for the error number Errno. If
61 -- Errno is not known it returns "Unknown system error".
63 type Socket_Fd_Array
is array (C
.unsigned
range 1 .. 64) of C
.int
;
64 pragma Convention
(C
, Socket_Fd_Array
);
67 fd_count
: C
.unsigned
;
68 fd_array
: Socket_Fd_Array
;
70 pragma Convention
(C
, Fd_Set
);
72 Null_Fd_Set
: constant Fd_Set
:= (0, (others => 0));
74 type Fd_Set_Access
is access all Fd_Set
;
75 pragma Convention
(C
, Fd_Set_Access
);
77 type Timeval_Unit
is new C
.long
;
78 pragma Convention
(C
, Timeval_Unit
);
80 type Timeval
is record
81 Tv_Sec
: Timeval_Unit
;
82 Tv_Usec
: Timeval_Unit
;
84 pragma Convention
(C
, Timeval
);
86 type Timeval_Access
is access all Timeval
;
87 pragma Convention
(C
, Timeval_Access
);
89 Immediat
: constant Timeval
:= (0, 0);
91 type Int_Access
is access all C
.int
;
92 pragma Convention
(C
, Int_Access
);
93 -- Access to C integers
95 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
96 aliased C
.Strings
.chars_ptr
;
98 package Chars_Ptr_Pointers
is
99 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
101 -- Arrays of C (char *)
103 type In_Addr
is record
104 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
106 pragma Convention
(C
, In_Addr
);
109 type In_Addr_Access
is access all In_Addr
;
110 pragma Convention
(C
, In_Addr_Access
);
111 -- Access to internet address
113 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
114 -- Any internet address (all the interfaces)
116 type In_Addr_Access_Array
is array (C
.size_t
range <>)
117 of aliased In_Addr_Access
;
118 pragma Convention
(C
, In_Addr_Access_Array
);
119 package In_Addr_Access_Pointers
is
120 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
121 -- Array of internet addresses
123 type Sockaddr
is record
124 Sa_Family
: C
.unsigned_short
;
125 Sa_Data
: C
.char_array
(1 .. 14);
127 pragma Convention
(C
, Sockaddr
);
130 type Sockaddr_Access
is access all Sockaddr
;
131 pragma Convention
(C
, Sockaddr_Access
);
132 -- Access to socket address
134 type Sockaddr_In
is record
135 Sin_Family
: C
.unsigned_short
:= Constants
.AF_INET
;
136 Sin_Port
: C
.unsigned_short
:= 0;
137 Sin_Addr
: In_Addr
:= Inaddr_Any
;
138 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
140 pragma Convention
(C
, Sockaddr_In
);
141 -- Internet socket address
143 type Sockaddr_In_Access
is access all Sockaddr_In
;
144 pragma Convention
(C
, Sockaddr_In_Access
);
145 -- Access to internet socket address
147 type Hostent
is record
148 H_Name
: C
.Strings
.chars_ptr
;
149 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
150 H_Addrtype
: C
.short
;
152 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
154 pragma Convention
(C
, Hostent
);
157 type Hostent_Access
is access all Hostent
;
158 pragma Convention
(C
, Hostent_Access
);
159 -- Access to host entry
161 type Two_Int
is array (0 .. 1) of C
.int
;
162 pragma Convention
(C
, Two_Int
);
167 Addr
: System
.Address
;
168 Addrlen
: access C
.int
)
173 Name
: System
.Address
;
183 Name
: System
.Address
;
187 function C_Gethostbyaddr
188 (Addr
: System
.Address
;
191 return Hostent_Access
;
193 function C_Gethostbyname
194 (Name
: C
.char_array
)
195 return Hostent_Access
;
197 function C_Gethostname
198 (Name
: System
.Address
;
202 function C_Getpeername
204 Name
: System
.Address
;
205 Namelen
: access C
.int
)
208 function C_Getsockname
210 Name
: System
.Address
;
211 Namelen
: access C
.int
)
214 function C_Getsockopt
218 Optval
: System
.Address
;
219 Optlen
: access C
.int
)
223 (Cp
: C
.Strings
.chars_ptr
)
238 Buf
: System
.Address
;
244 Buf
: System
.Address
;
251 Buf
: System
.Address
;
254 From
: Sockaddr_In_Access
;
255 Fromlen
: access C
.int
)
260 Readfds
: Fd_Set_Access
;
261 Writefds
: Fd_Set_Access
;
262 Exceptfds
: Fd_Set_Access
;
263 Timeout
: Timeval_Access
)
268 Buf
: System
.Address
;
275 Msg
: System
.Address
;
278 To
: Sockaddr_In_Access
;
282 function C_Setsockopt
286 Optval
: System
.Address
;
303 return C
.Strings
.chars_ptr
;
306 (Command
: System
.Address
)
311 Buf
: System
.Address
;
316 (WS_Version
: Interfaces
.C
.int
;
317 WSADataAddress
: System
.Address
)
318 return Interfaces
.C
.int
;
320 procedure WSACleanup
;
322 procedure Clear
(Item
: in out Fd_Set
; Socket
: in C
.int
);
323 procedure Empty
(Item
: in out Fd_Set
);
324 function Is_Empty
(Item
: Fd_Set
) return Boolean;
325 function Is_Set
(Item
: Fd_Set
; Socket
: C
.int
) return Boolean;
326 function Max
(Item
: Fd_Set
) return C
.int
;
327 procedure Set
(Item
: in out Fd_Set
; Socket
: in C
.int
);
330 procedure Initialize
(Process_Blocking_IO
: Boolean := False);
334 pragma Import
(Stdcall
, C_Accept
, "accept");
335 pragma Import
(Stdcall
, C_Bind
, "bind");
336 pragma Import
(Stdcall
, C_Close
, "closesocket");
337 pragma Import
(Stdcall
, C_Connect
, "connect");
338 pragma Import
(Stdcall
, C_Gethostbyaddr
, "gethostbyaddr");
339 pragma Import
(Stdcall
, C_Gethostbyname
, "gethostbyname");
340 pragma Import
(Stdcall
, C_Gethostname
, "gethostname");
341 pragma Import
(Stdcall
, C_Getpeername
, "getpeername");
342 pragma Import
(Stdcall
, C_Getsockname
, "getsockname");
343 pragma Import
(Stdcall
, C_Getsockopt
, "getsockopt");
344 pragma Import
(Stdcall
, C_Inet_Addr
, "inet_addr");
345 pragma Import
(Stdcall
, C_Ioctl
, "ioctlsocket");
346 pragma Import
(Stdcall
, C_Listen
, "listen");
347 pragma Import
(C
, C_Read
, "_read");
348 pragma Import
(Stdcall
, C_Recv
, "recv");
349 pragma Import
(Stdcall
, C_Recvfrom
, "recvfrom");
350 pragma Import
(Stdcall
, C_Select
, "select");
351 pragma Import
(Stdcall
, C_Send
, "send");
352 pragma Import
(Stdcall
, C_Sendto
, "sendto");
353 pragma Import
(Stdcall
, C_Setsockopt
, "setsockopt");
354 pragma Import
(Stdcall
, C_Shutdown
, "shutdown");
355 pragma Import
(Stdcall
, C_Socket
, "socket");
356 pragma Import
(C
, C_Strerror
, "strerror");
357 pragma Import
(C
, C_System
, "_system");
358 pragma Import
(C
, C_Write
, "_write");
359 pragma Import
(Stdcall
, Socket_Errno
, "WSAGetLastError");
360 pragma Import
(Stdcall
, WSAStartup
, "WSAStartup");
361 pragma Import
(Stdcall
, WSACleanup
, "WSACleanup");
363 end GNAT
.Sockets
.Thin
;