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 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 -- This version is for NT.
35 with Interfaces
.C
.Pointers
;
36 with Interfaces
.C
.Strings
;
38 with GNAT
.Sockets
.Constants
;
42 package GNAT
.Sockets
.Thin
is
44 -- ??? far more comments required ???
46 package C
renames Interfaces
.C
;
49 -- So that we can declare the Failure constant below.
51 Success
: constant C
.int
:= 0;
52 Failure
: constant C
.int
:= -1;
54 function Socket_Errno
return Integer;
55 -- Returns last socket error number.
57 function Socket_Error_Message
(Errno
: Integer) return String;
58 -- Returns the error message string for the error number Errno. If
59 -- Errno is not known it returns "Unknown system error".
61 type Socket_Fd_Array
is array (C
.unsigned
range 1 .. 64) of C
.int
;
62 pragma Convention
(C
, Socket_Fd_Array
);
65 fd_count
: C
.unsigned
;
66 fd_array
: Socket_Fd_Array
;
68 pragma Convention
(C
, Fd_Set
);
70 Null_Fd_Set
: constant Fd_Set
:= (0, (others => 0));
72 type Fd_Set_Access
is access all Fd_Set
;
73 pragma Convention
(C
, Fd_Set_Access
);
75 type Timeval_Unit
is new C
.long
;
76 pragma Convention
(C
, Timeval_Unit
);
78 type Timeval
is record
79 Tv_Sec
: Timeval_Unit
;
80 Tv_Usec
: Timeval_Unit
;
82 pragma Convention
(C
, Timeval
);
84 type Timeval_Access
is access all Timeval
;
85 pragma Convention
(C
, Timeval_Access
);
87 Immediat
: constant Timeval
:= (0, 0);
89 type Int_Access
is access all C
.int
;
90 pragma Convention
(C
, Int_Access
);
91 -- Access to C integers
93 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
94 aliased C
.Strings
.chars_ptr
;
96 package Chars_Ptr_Pointers
is
97 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
99 -- Arrays of C (char *)
101 type In_Addr
is record
102 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
104 pragma Convention
(C
, In_Addr
);
107 type In_Addr_Access
is access all In_Addr
;
108 pragma Convention
(C
, In_Addr_Access
);
109 -- Access to internet address
111 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
112 -- Any internet address (all the interfaces)
114 type In_Addr_Access_Array
is array (C
.size_t
range <>)
115 of aliased In_Addr_Access
;
116 pragma Convention
(C
, In_Addr_Access_Array
);
117 package In_Addr_Access_Pointers
is
118 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
119 -- Array of internet addresses
121 type Sockaddr
is record
122 Sa_Family
: C
.unsigned_short
;
123 Sa_Data
: C
.char_array
(1 .. 14);
125 pragma Convention
(C
, Sockaddr
);
128 type Sockaddr_Access
is access all Sockaddr
;
129 pragma Convention
(C
, Sockaddr_Access
);
130 -- Access to socket address
132 type Sockaddr_In
is record
133 Sin_Family
: C
.unsigned_short
:= Constants
.AF_INET
;
134 Sin_Port
: C
.unsigned_short
:= 0;
135 Sin_Addr
: In_Addr
:= Inaddr_Any
;
136 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
138 pragma Convention
(C
, Sockaddr_In
);
139 -- Internet socket address
141 type Sockaddr_In_Access
is access all Sockaddr_In
;
142 pragma Convention
(C
, Sockaddr_In_Access
);
143 -- Access to internet socket address
145 type Hostent
is record
146 H_Name
: C
.Strings
.chars_ptr
;
147 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
148 H_Addrtype
: C
.short
;
150 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
152 pragma Convention
(C
, Hostent
);
155 type Hostent_Access
is access all Hostent
;
156 pragma Convention
(C
, Hostent_Access
);
157 -- Access to host entry
159 type Two_Int
is array (0 .. 1) of C
.int
;
160 pragma Convention
(C
, Two_Int
);
165 Addr
: System
.Address
;
166 Addrlen
: access C
.int
)
171 Name
: System
.Address
;
181 Name
: System
.Address
;
185 function C_Gethostbyaddr
186 (Addr
: System
.Address
;
189 return Hostent_Access
;
191 function C_Gethostbyname
192 (Name
: C
.char_array
)
193 return Hostent_Access
;
195 function C_Gethostname
196 (Name
: System
.Address
;
200 function C_Getpeername
202 Name
: System
.Address
;
203 Namelen
: access C
.int
)
206 function C_Getsockname
208 Name
: System
.Address
;
209 Namelen
: access C
.int
)
212 function C_Getsockopt
216 Optval
: System
.Address
;
217 Optlen
: access C
.int
)
221 (Cp
: C
.Strings
.chars_ptr
)
236 Buf
: System
.Address
;
242 Buf
: System
.Address
;
249 Buf
: System
.Address
;
252 From
: Sockaddr_In_Access
;
253 Fromlen
: access C
.int
)
258 Readfds
: Fd_Set_Access
;
259 Writefds
: Fd_Set_Access
;
260 Exceptfds
: Fd_Set_Access
;
261 Timeout
: Timeval_Access
)
266 Buf
: System
.Address
;
273 Msg
: System
.Address
;
276 To
: Sockaddr_In_Access
;
280 function C_Setsockopt
284 Optval
: System
.Address
;
301 return C
.Strings
.chars_ptr
;
304 (Command
: System
.Address
)
309 Buf
: System
.Address
;
314 (WS_Version
: Interfaces
.C
.int
;
315 WSADataAddress
: System
.Address
)
316 return Interfaces
.C
.int
;
318 procedure WSACleanup
;
320 procedure Clear
(Item
: in out Fd_Set
; Socket
: in C
.int
);
321 procedure Empty
(Item
: in out Fd_Set
);
322 function Is_Empty
(Item
: Fd_Set
) return Boolean;
323 function Is_Set
(Item
: Fd_Set
; Socket
: C
.int
) return Boolean;
324 function Max
(Item
: Fd_Set
) return C
.int
;
325 procedure Set
(Item
: in out Fd_Set
; Socket
: in C
.int
);
328 procedure Initialize
(Process_Blocking_IO
: Boolean := False);
332 pragma Import
(Stdcall
, C_Accept
, "accept");
333 pragma Import
(Stdcall
, C_Bind
, "bind");
334 pragma Import
(Stdcall
, C_Close
, "closesocket");
335 pragma Import
(Stdcall
, C_Connect
, "connect");
336 pragma Import
(Stdcall
, C_Gethostbyaddr
, "gethostbyaddr");
337 pragma Import
(Stdcall
, C_Gethostbyname
, "gethostbyname");
338 pragma Import
(Stdcall
, C_Gethostname
, "gethostname");
339 pragma Import
(Stdcall
, C_Getpeername
, "getpeername");
340 pragma Import
(Stdcall
, C_Getsockname
, "getsockname");
341 pragma Import
(Stdcall
, C_Getsockopt
, "getsockopt");
342 pragma Import
(Stdcall
, C_Inet_Addr
, "inet_addr");
343 pragma Import
(Stdcall
, C_Ioctl
, "ioctlsocket");
344 pragma Import
(Stdcall
, C_Listen
, "listen");
345 pragma Import
(C
, C_Read
, "_read");
346 pragma Import
(Stdcall
, C_Recv
, "recv");
347 pragma Import
(Stdcall
, C_Recvfrom
, "recvfrom");
348 pragma Import
(Stdcall
, C_Select
, "select");
349 pragma Import
(Stdcall
, C_Send
, "send");
350 pragma Import
(Stdcall
, C_Sendto
, "sendto");
351 pragma Import
(Stdcall
, C_Setsockopt
, "setsockopt");
352 pragma Import
(Stdcall
, C_Shutdown
, "shutdown");
353 pragma Import
(Stdcall
, C_Socket
, "socket");
354 pragma Import
(C
, C_Strerror
, "strerror");
355 pragma Import
(C
, C_System
, "_system");
356 pragma Import
(C
, C_Write
, "_write");
357 pragma Import
(Stdcall
, Socket_Errno
, "WSAGetLastError");
358 pragma Import
(Stdcall
, WSAStartup
, "WSAStartup");
359 pragma Import
(Stdcall
, WSACleanup
, "WSACleanup");
361 end GNAT
.Sockets
.Thin
;