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 -- This version is for NT.
36 with Interfaces
.C
.Pointers
;
37 with Interfaces
.C
.Strings
;
39 with GNAT
.Sockets
.Constants
;
43 package GNAT
.Sockets
.Thin
is
45 -- ??? far more comments required ???
47 package C
renames Interfaces
.C
;
50 -- So that we can declare the Failure constant below.
52 Success
: constant C
.int
:= 0;
53 Failure
: constant C
.int
:= -1;
55 function Socket_Errno
return Integer;
56 -- Returns last socket error number.
58 function Socket_Error_Message
(Errno
: Integer) return String;
59 -- Returns the error message string for the error number Errno. If
60 -- Errno is not known it returns "Unknown system error".
62 type Socket_Fd_Array
is array (C
.unsigned
range 1 .. 64) of C
.int
;
63 pragma Convention
(C
, Socket_Fd_Array
);
66 fd_count
: C
.unsigned
;
67 fd_array
: Socket_Fd_Array
;
69 pragma Convention
(C
, Fd_Set
);
71 Null_Fd_Set
: constant Fd_Set
:= (0, (others => 0));
73 type Fd_Set_Access
is access all Fd_Set
;
74 pragma Convention
(C
, Fd_Set_Access
);
76 type Timeval_Unit
is new C
.long
;
77 pragma Convention
(C
, Timeval_Unit
);
79 type Timeval
is record
80 Tv_Sec
: Timeval_Unit
;
81 Tv_Usec
: Timeval_Unit
;
83 pragma Convention
(C
, Timeval
);
85 type Timeval_Access
is access all Timeval
;
86 pragma Convention
(C
, Timeval_Access
);
88 Immediat
: constant Timeval
:= (0, 0);
90 type Int_Access
is access all C
.int
;
91 pragma Convention
(C
, Int_Access
);
92 -- Access to C integers
94 type Chars_Ptr_Array
is array (C
.size_t
range <>) of
95 aliased C
.Strings
.chars_ptr
;
97 package Chars_Ptr_Pointers
is
98 new C
.Pointers
(C
.size_t
, C
.Strings
.chars_ptr
, Chars_Ptr_Array
,
100 -- Arrays of C (char *)
102 type In_Addr
is record
103 S_B1
, S_B2
, S_B3
, S_B4
: C
.unsigned_char
;
105 pragma Convention
(C
, In_Addr
);
108 type In_Addr_Access
is access all In_Addr
;
109 pragma Convention
(C
, In_Addr_Access
);
110 -- Access to internet address
112 Inaddr_Any
: aliased constant In_Addr
:= (others => 0);
113 -- Any internet address (all the interfaces)
115 type In_Addr_Access_Array
is array (C
.size_t
range <>)
116 of aliased In_Addr_Access
;
117 pragma Convention
(C
, In_Addr_Access_Array
);
118 package In_Addr_Access_Pointers
is
119 new C
.Pointers
(C
.size_t
, In_Addr_Access
, In_Addr_Access_Array
, null);
120 -- Array of internet addresses
122 type Sockaddr
is record
123 Sa_Family
: C
.unsigned_short
;
124 Sa_Data
: C
.char_array
(1 .. 14);
126 pragma Convention
(C
, Sockaddr
);
129 type Sockaddr_Access
is access all Sockaddr
;
130 pragma Convention
(C
, Sockaddr_Access
);
131 -- Access to socket address
133 type Sockaddr_In
is record
134 Sin_Family
: C
.unsigned_short
:= Constants
.AF_INET
;
135 Sin_Port
: C
.unsigned_short
:= 0;
136 Sin_Addr
: In_Addr
:= Inaddr_Any
;
137 Sin_Zero
: C
.char_array
(1 .. 8) := (others => C
.char
'Val (0));
139 pragma Convention
(C
, Sockaddr_In
);
140 -- Internet socket address
142 type Sockaddr_In_Access
is access all Sockaddr_In
;
143 pragma Convention
(C
, Sockaddr_In_Access
);
144 -- Access to internet socket address
146 type Hostent
is record
147 H_Name
: C
.Strings
.chars_ptr
;
148 H_Aliases
: Chars_Ptr_Pointers
.Pointer
;
149 H_Addrtype
: C
.short
;
151 H_Addr_List
: In_Addr_Access_Pointers
.Pointer
;
153 pragma Convention
(C
, Hostent
);
156 type Hostent_Access
is access all Hostent
;
157 pragma Convention
(C
, Hostent_Access
);
158 -- Access to host entry
160 type Two_Int
is array (0 .. 1) of C
.int
;
161 pragma Convention
(C
, Two_Int
);
166 Addr
: System
.Address
;
167 Addrlen
: access C
.int
)
172 Name
: System
.Address
;
182 Name
: System
.Address
;
186 function C_Gethostbyaddr
187 (Addr
: System
.Address
;
190 return Hostent_Access
;
192 function C_Gethostbyname
193 (Name
: C
.char_array
)
194 return Hostent_Access
;
196 function C_Gethostname
197 (Name
: System
.Address
;
201 function C_Getpeername
203 Name
: System
.Address
;
204 Namelen
: access C
.int
)
207 function C_Getsockname
209 Name
: System
.Address
;
210 Namelen
: access C
.int
)
213 function C_Getsockopt
217 Optval
: System
.Address
;
218 Optlen
: access C
.int
)
222 (Cp
: C
.Strings
.chars_ptr
)
237 Buf
: System
.Address
;
243 Buf
: System
.Address
;
250 Buf
: System
.Address
;
253 From
: Sockaddr_In_Access
;
254 Fromlen
: access C
.int
)
259 Readfds
: Fd_Set_Access
;
260 Writefds
: Fd_Set_Access
;
261 Exceptfds
: Fd_Set_Access
;
262 Timeout
: Timeval_Access
)
267 Buf
: System
.Address
;
274 Msg
: System
.Address
;
277 To
: Sockaddr_In_Access
;
281 function C_Setsockopt
285 Optval
: System
.Address
;
302 return C
.Strings
.chars_ptr
;
305 (Command
: System
.Address
)
310 Buf
: System
.Address
;
315 (WS_Version
: Interfaces
.C
.int
;
316 WSADataAddress
: System
.Address
)
317 return Interfaces
.C
.int
;
319 procedure WSACleanup
;
321 procedure Clear
(Item
: in out Fd_Set
; Socket
: in C
.int
);
322 procedure Empty
(Item
: in out Fd_Set
);
323 function Is_Empty
(Item
: Fd_Set
) return Boolean;
324 function Is_Set
(Item
: Fd_Set
; Socket
: C
.int
) return Boolean;
325 function Max
(Item
: Fd_Set
) return C
.int
;
326 procedure Set
(Item
: in out Fd_Set
; Socket
: in C
.int
);
329 procedure Initialize
(Process_Blocking_IO
: Boolean := False);
333 pragma Import
(Stdcall
, C_Accept
, "accept");
334 pragma Import
(Stdcall
, C_Bind
, "bind");
335 pragma Import
(Stdcall
, C_Close
, "closesocket");
336 pragma Import
(Stdcall
, C_Connect
, "connect");
337 pragma Import
(Stdcall
, C_Gethostbyaddr
, "gethostbyaddr");
338 pragma Import
(Stdcall
, C_Gethostbyname
, "gethostbyname");
339 pragma Import
(Stdcall
, C_Gethostname
, "gethostname");
340 pragma Import
(Stdcall
, C_Getpeername
, "getpeername");
341 pragma Import
(Stdcall
, C_Getsockname
, "getsockname");
342 pragma Import
(Stdcall
, C_Getsockopt
, "getsockopt");
343 pragma Import
(Stdcall
, C_Inet_Addr
, "inet_addr");
344 pragma Import
(Stdcall
, C_Ioctl
, "ioctlsocket");
345 pragma Import
(Stdcall
, C_Listen
, "listen");
346 pragma Import
(C
, C_Read
, "_read");
347 pragma Import
(Stdcall
, C_Recv
, "recv");
348 pragma Import
(Stdcall
, C_Recvfrom
, "recvfrom");
349 pragma Import
(Stdcall
, C_Select
, "select");
350 pragma Import
(Stdcall
, C_Send
, "send");
351 pragma Import
(Stdcall
, C_Sendto
, "sendto");
352 pragma Import
(Stdcall
, C_Setsockopt
, "setsockopt");
353 pragma Import
(Stdcall
, C_Shutdown
, "shutdown");
354 pragma Import
(Stdcall
, C_Socket
, "socket");
355 pragma Import
(C
, C_Strerror
, "strerror");
356 pragma Import
(C
, C_System
, "_system");
357 pragma Import
(C
, C_Write
, "_write");
358 pragma Import
(Stdcall
, Socket_Errno
, "WSAGetLastError");
359 pragma Import
(Stdcall
, WSAStartup
, "WSAStartup");
360 pragma Import
(Stdcall
, WSACleanup
, "WSACleanup");
362 end GNAT
.Sockets
.Thin
;