annotate gcc/ada/libgnat/g-socthi__mingw.adb @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . S O C K E T S . T H I N --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2001-2017, AdaCore --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This package provides a target dependent thin interface to the sockets
kono
parents:
diff changeset
33 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
kono
parents:
diff changeset
34 -- should not be directly with'ed by an applications program.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 -- This version is for NT
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 with Ada.Unchecked_Conversion;
kono
parents:
diff changeset
39 with Interfaces.C.Strings; use Interfaces.C.Strings;
kono
parents:
diff changeset
40 with System; use System;
kono
parents:
diff changeset
41 with System.Storage_Elements; use System.Storage_Elements;
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 package body GNAT.Sockets.Thin is
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 use type C.unsigned;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 WSAData_Dummy : array (1 .. 512) of C.int;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 WS_Version : constant := 16#0202#;
kono
parents:
diff changeset
50 -- Winsock 2.2
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 Initialized : Boolean := False;
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 function Standard_Connect
kono
parents:
diff changeset
55 (S : C.int;
kono
parents:
diff changeset
56 Name : System.Address;
kono
parents:
diff changeset
57 Namelen : C.int) return C.int;
kono
parents:
diff changeset
58 pragma Import (Stdcall, Standard_Connect, "connect");
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 function Standard_Select
kono
parents:
diff changeset
61 (Nfds : C.int;
kono
parents:
diff changeset
62 Readfds : access Fd_Set;
kono
parents:
diff changeset
63 Writefds : access Fd_Set;
kono
parents:
diff changeset
64 Exceptfds : access Fd_Set;
kono
parents:
diff changeset
65 Timeout : Timeval_Access) return C.int;
kono
parents:
diff changeset
66 pragma Import (Stdcall, Standard_Select, "select");
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 type Error_Type is
kono
parents:
diff changeset
69 (N_EINTR,
kono
parents:
diff changeset
70 N_EBADF,
kono
parents:
diff changeset
71 N_EACCES,
kono
parents:
diff changeset
72 N_EFAULT,
kono
parents:
diff changeset
73 N_EINVAL,
kono
parents:
diff changeset
74 N_EMFILE,
kono
parents:
diff changeset
75 N_EWOULDBLOCK,
kono
parents:
diff changeset
76 N_EINPROGRESS,
kono
parents:
diff changeset
77 N_EALREADY,
kono
parents:
diff changeset
78 N_ENOTSOCK,
kono
parents:
diff changeset
79 N_EDESTADDRREQ,
kono
parents:
diff changeset
80 N_EMSGSIZE,
kono
parents:
diff changeset
81 N_EPROTOTYPE,
kono
parents:
diff changeset
82 N_ENOPROTOOPT,
kono
parents:
diff changeset
83 N_EPROTONOSUPPORT,
kono
parents:
diff changeset
84 N_ESOCKTNOSUPPORT,
kono
parents:
diff changeset
85 N_EOPNOTSUPP,
kono
parents:
diff changeset
86 N_EPFNOSUPPORT,
kono
parents:
diff changeset
87 N_EAFNOSUPPORT,
kono
parents:
diff changeset
88 N_EADDRINUSE,
kono
parents:
diff changeset
89 N_EADDRNOTAVAIL,
kono
parents:
diff changeset
90 N_ENETDOWN,
kono
parents:
diff changeset
91 N_ENETUNREACH,
kono
parents:
diff changeset
92 N_ENETRESET,
kono
parents:
diff changeset
93 N_ECONNABORTED,
kono
parents:
diff changeset
94 N_ECONNRESET,
kono
parents:
diff changeset
95 N_ENOBUFS,
kono
parents:
diff changeset
96 N_EISCONN,
kono
parents:
diff changeset
97 N_ENOTCONN,
kono
parents:
diff changeset
98 N_ESHUTDOWN,
kono
parents:
diff changeset
99 N_ETOOMANYREFS,
kono
parents:
diff changeset
100 N_ETIMEDOUT,
kono
parents:
diff changeset
101 N_ECONNREFUSED,
kono
parents:
diff changeset
102 N_ELOOP,
kono
parents:
diff changeset
103 N_ENAMETOOLONG,
kono
parents:
diff changeset
104 N_EHOSTDOWN,
kono
parents:
diff changeset
105 N_EHOSTUNREACH,
kono
parents:
diff changeset
106 N_WSASYSNOTREADY,
kono
parents:
diff changeset
107 N_WSAVERNOTSUPPORTED,
kono
parents:
diff changeset
108 N_WSANOTINITIALISED,
kono
parents:
diff changeset
109 N_WSAEDISCON,
kono
parents:
diff changeset
110 N_HOST_NOT_FOUND,
kono
parents:
diff changeset
111 N_TRY_AGAIN,
kono
parents:
diff changeset
112 N_NO_RECOVERY,
kono
parents:
diff changeset
113 N_NO_DATA,
kono
parents:
diff changeset
114 N_OTHERS);
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 Error_Messages : constant array (Error_Type) of chars_ptr :=
kono
parents:
diff changeset
117 (N_EINTR =>
kono
parents:
diff changeset
118 New_String ("Interrupted system call"),
kono
parents:
diff changeset
119 N_EBADF =>
kono
parents:
diff changeset
120 New_String ("Bad file number"),
kono
parents:
diff changeset
121 N_EACCES =>
kono
parents:
diff changeset
122 New_String ("Permission denied"),
kono
parents:
diff changeset
123 N_EFAULT =>
kono
parents:
diff changeset
124 New_String ("Bad address"),
kono
parents:
diff changeset
125 N_EINVAL =>
kono
parents:
diff changeset
126 New_String ("Invalid argument"),
kono
parents:
diff changeset
127 N_EMFILE =>
kono
parents:
diff changeset
128 New_String ("Too many open files"),
kono
parents:
diff changeset
129 N_EWOULDBLOCK =>
kono
parents:
diff changeset
130 New_String ("Operation would block"),
kono
parents:
diff changeset
131 N_EINPROGRESS =>
kono
parents:
diff changeset
132 New_String ("Operation now in progress. This error is "
kono
parents:
diff changeset
133 & "returned if any Windows Sockets API "
kono
parents:
diff changeset
134 & "function is called while a blocking "
kono
parents:
diff changeset
135 & "function is in progress"),
kono
parents:
diff changeset
136 N_EALREADY =>
kono
parents:
diff changeset
137 New_String ("Operation already in progress"),
kono
parents:
diff changeset
138 N_ENOTSOCK =>
kono
parents:
diff changeset
139 New_String ("Socket operation on nonsocket"),
kono
parents:
diff changeset
140 N_EDESTADDRREQ =>
kono
parents:
diff changeset
141 New_String ("Destination address required"),
kono
parents:
diff changeset
142 N_EMSGSIZE =>
kono
parents:
diff changeset
143 New_String ("Message too long"),
kono
parents:
diff changeset
144 N_EPROTOTYPE =>
kono
parents:
diff changeset
145 New_String ("Protocol wrong type for socket"),
kono
parents:
diff changeset
146 N_ENOPROTOOPT =>
kono
parents:
diff changeset
147 New_String ("Protocol not available"),
kono
parents:
diff changeset
148 N_EPROTONOSUPPORT =>
kono
parents:
diff changeset
149 New_String ("Protocol not supported"),
kono
parents:
diff changeset
150 N_ESOCKTNOSUPPORT =>
kono
parents:
diff changeset
151 New_String ("Socket type not supported"),
kono
parents:
diff changeset
152 N_EOPNOTSUPP =>
kono
parents:
diff changeset
153 New_String ("Operation not supported on socket"),
kono
parents:
diff changeset
154 N_EPFNOSUPPORT =>
kono
parents:
diff changeset
155 New_String ("Protocol family not supported"),
kono
parents:
diff changeset
156 N_EAFNOSUPPORT =>
kono
parents:
diff changeset
157 New_String ("Address family not supported by protocol family"),
kono
parents:
diff changeset
158 N_EADDRINUSE =>
kono
parents:
diff changeset
159 New_String ("Address already in use"),
kono
parents:
diff changeset
160 N_EADDRNOTAVAIL =>
kono
parents:
diff changeset
161 New_String ("Cannot assign requested address"),
kono
parents:
diff changeset
162 N_ENETDOWN =>
kono
parents:
diff changeset
163 New_String ("Network is down. This error may be "
kono
parents:
diff changeset
164 & "reported at any time if the Windows "
kono
parents:
diff changeset
165 & "Sockets implementation detects an "
kono
parents:
diff changeset
166 & "underlying failure"),
kono
parents:
diff changeset
167 N_ENETUNREACH =>
kono
parents:
diff changeset
168 New_String ("Network is unreachable"),
kono
parents:
diff changeset
169 N_ENETRESET =>
kono
parents:
diff changeset
170 New_String ("Network dropped connection on reset"),
kono
parents:
diff changeset
171 N_ECONNABORTED =>
kono
parents:
diff changeset
172 New_String ("Software caused connection abort"),
kono
parents:
diff changeset
173 N_ECONNRESET =>
kono
parents:
diff changeset
174 New_String ("Connection reset by peer"),
kono
parents:
diff changeset
175 N_ENOBUFS =>
kono
parents:
diff changeset
176 New_String ("No buffer space available"),
kono
parents:
diff changeset
177 N_EISCONN =>
kono
parents:
diff changeset
178 New_String ("Socket is already connected"),
kono
parents:
diff changeset
179 N_ENOTCONN =>
kono
parents:
diff changeset
180 New_String ("Socket is not connected"),
kono
parents:
diff changeset
181 N_ESHUTDOWN =>
kono
parents:
diff changeset
182 New_String ("Cannot send after socket shutdown"),
kono
parents:
diff changeset
183 N_ETOOMANYREFS =>
kono
parents:
diff changeset
184 New_String ("Too many references: cannot splice"),
kono
parents:
diff changeset
185 N_ETIMEDOUT =>
kono
parents:
diff changeset
186 New_String ("Connection timed out"),
kono
parents:
diff changeset
187 N_ECONNREFUSED =>
kono
parents:
diff changeset
188 New_String ("Connection refused"),
kono
parents:
diff changeset
189 N_ELOOP =>
kono
parents:
diff changeset
190 New_String ("Too many levels of symbolic links"),
kono
parents:
diff changeset
191 N_ENAMETOOLONG =>
kono
parents:
diff changeset
192 New_String ("File name too long"),
kono
parents:
diff changeset
193 N_EHOSTDOWN =>
kono
parents:
diff changeset
194 New_String ("Host is down"),
kono
parents:
diff changeset
195 N_EHOSTUNREACH =>
kono
parents:
diff changeset
196 New_String ("No route to host"),
kono
parents:
diff changeset
197 N_WSASYSNOTREADY =>
kono
parents:
diff changeset
198 New_String ("Returned by WSAStartup(), indicating that "
kono
parents:
diff changeset
199 & "the network subsystem is unusable"),
kono
parents:
diff changeset
200 N_WSAVERNOTSUPPORTED =>
kono
parents:
diff changeset
201 New_String ("Returned by WSAStartup(), indicating that "
kono
parents:
diff changeset
202 & "the Windows Sockets DLL cannot support "
kono
parents:
diff changeset
203 & "this application"),
kono
parents:
diff changeset
204 N_WSANOTINITIALISED =>
kono
parents:
diff changeset
205 New_String ("Winsock not initialized. This message is "
kono
parents:
diff changeset
206 & "returned by any function except WSAStartup(), "
kono
parents:
diff changeset
207 & "indicating that a successful WSAStartup() has "
kono
parents:
diff changeset
208 & "not yet been performed"),
kono
parents:
diff changeset
209 N_WSAEDISCON =>
kono
parents:
diff changeset
210 New_String ("Disconnected"),
kono
parents:
diff changeset
211 N_HOST_NOT_FOUND =>
kono
parents:
diff changeset
212 New_String ("Host not found. This message indicates "
kono
parents:
diff changeset
213 & "that the key (name, address, and so on) was not found"),
kono
parents:
diff changeset
214 N_TRY_AGAIN =>
kono
parents:
diff changeset
215 New_String ("Nonauthoritative host not found. This error may "
kono
parents:
diff changeset
216 & "suggest that the name service itself is not "
kono
parents:
diff changeset
217 & "functioning"),
kono
parents:
diff changeset
218 N_NO_RECOVERY =>
kono
parents:
diff changeset
219 New_String ("Nonrecoverable error. This error may suggest that the "
kono
parents:
diff changeset
220 & "name service itself is not functioning"),
kono
parents:
diff changeset
221 N_NO_DATA =>
kono
parents:
diff changeset
222 New_String ("Valid name, no data record of requested type. "
kono
parents:
diff changeset
223 & "This error indicates that the key (name, address, "
kono
parents:
diff changeset
224 & "and so on) was not found."),
kono
parents:
diff changeset
225 N_OTHERS =>
kono
parents:
diff changeset
226 New_String ("Unknown system error"));
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 ---------------
kono
parents:
diff changeset
229 -- C_Connect --
kono
parents:
diff changeset
230 ---------------
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 function C_Connect
kono
parents:
diff changeset
233 (S : C.int;
kono
parents:
diff changeset
234 Name : System.Address;
kono
parents:
diff changeset
235 Namelen : C.int) return C.int
kono
parents:
diff changeset
236 is
kono
parents:
diff changeset
237 Res : C.int;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 begin
kono
parents:
diff changeset
240 Res := Standard_Connect (S, Name, Namelen);
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 if Res = -1 then
kono
parents:
diff changeset
243 if Socket_Errno = SOSC.EWOULDBLOCK then
kono
parents:
diff changeset
244 Set_Socket_Errno (SOSC.EINPROGRESS);
kono
parents:
diff changeset
245 end if;
kono
parents:
diff changeset
246 end if;
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 return Res;
kono
parents:
diff changeset
249 end C_Connect;
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 ------------------
kono
parents:
diff changeset
252 -- Socket_Ioctl --
kono
parents:
diff changeset
253 ------------------
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 function Socket_Ioctl
kono
parents:
diff changeset
256 (S : C.int;
kono
parents:
diff changeset
257 Req : SOSC.IOCTL_Req_T;
kono
parents:
diff changeset
258 Arg : access C.int) return C.int
kono
parents:
diff changeset
259 is
kono
parents:
diff changeset
260 begin
kono
parents:
diff changeset
261 return C_Ioctl (S, Req, Arg);
kono
parents:
diff changeset
262 end Socket_Ioctl;
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 ---------------
kono
parents:
diff changeset
265 -- C_Recvmsg --
kono
parents:
diff changeset
266 ---------------
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 function C_Recvmsg
kono
parents:
diff changeset
269 (S : C.int;
kono
parents:
diff changeset
270 Msg : System.Address;
kono
parents:
diff changeset
271 Flags : C.int) return System.CRTL.ssize_t
kono
parents:
diff changeset
272 is
kono
parents:
diff changeset
273 use type C.size_t;
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 Fill : constant Boolean :=
kono
parents:
diff changeset
276 SOSC.MSG_WAITALL /= -1
kono
parents:
diff changeset
277 and then (C.unsigned (Flags) and SOSC.MSG_WAITALL) /= 0;
kono
parents:
diff changeset
278 -- Is the MSG_WAITALL flag set? If so we need to fully fill all vectors
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 Res : C.int;
kono
parents:
diff changeset
281 Count : C.int := 0;
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 MH : Msghdr;
kono
parents:
diff changeset
284 for MH'Address use Msg;
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 Iovec : array (0 .. MH.Msg_Iovlen - 1) of Vector_Element;
kono
parents:
diff changeset
287 for Iovec'Address use MH.Msg_Iov;
kono
parents:
diff changeset
288 pragma Import (Ada, Iovec);
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 Iov_Index : Integer;
kono
parents:
diff changeset
291 Current_Iovec : Vector_Element;
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 function To_Access is new Ada.Unchecked_Conversion
kono
parents:
diff changeset
294 (System.Address, Stream_Element_Reference);
kono
parents:
diff changeset
295 pragma Warnings (Off, Stream_Element_Reference);
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 Req : Request_Type (Name => N_Bytes_To_Read);
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 begin
kono
parents:
diff changeset
300 -- Windows does not provide an implementation of recvmsg(). The spec for
kono
parents:
diff changeset
301 -- WSARecvMsg() is incompatible with the data types we define, and is
kono
parents:
diff changeset
302 -- available starting with Windows Vista and Server 2008 only. So,
kono
parents:
diff changeset
303 -- we use C_Recv instead.
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 -- Check how much data are available
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 Control_Socket (Socket_Type (S), Req);
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 -- Fill the vectors
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 Iov_Index := -1;
kono
parents:
diff changeset
312 Current_Iovec := (Base => null, Length => 0);
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 loop
kono
parents:
diff changeset
315 if Current_Iovec.Length = 0 then
kono
parents:
diff changeset
316 Iov_Index := Iov_Index + 1;
kono
parents:
diff changeset
317 exit when Iov_Index > Integer (Iovec'Last);
kono
parents:
diff changeset
318 Current_Iovec := Iovec (SOSC.Msg_Iovlen_T (Iov_Index));
kono
parents:
diff changeset
319 end if;
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 Res :=
kono
parents:
diff changeset
322 C_Recv
kono
parents:
diff changeset
323 (S,
kono
parents:
diff changeset
324 Current_Iovec.Base.all'Address,
kono
parents:
diff changeset
325 C.int (Current_Iovec.Length),
kono
parents:
diff changeset
326 Flags);
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 if Res < 0 then
kono
parents:
diff changeset
329 return System.CRTL.ssize_t (Res);
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 elsif Res = 0 and then not Fill then
kono
parents:
diff changeset
332 exit;
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 else
kono
parents:
diff changeset
335 pragma Assert (Interfaces.C.size_t (Res) <= Current_Iovec.Length);
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 Count := Count + Res;
kono
parents:
diff changeset
338 Current_Iovec.Length :=
kono
parents:
diff changeset
339 Current_Iovec.Length - Interfaces.C.size_t (Res);
kono
parents:
diff changeset
340 Current_Iovec.Base :=
kono
parents:
diff changeset
341 To_Access (Current_Iovec.Base.all'Address
kono
parents:
diff changeset
342 + Storage_Offset (Res));
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 -- If all the data that was initially available read, do not
kono
parents:
diff changeset
345 -- attempt to receive more, since this might block, or merge data
kono
parents:
diff changeset
346 -- from successive datagrams for a datagram-oriented socket. We
kono
parents:
diff changeset
347 -- still try to receive more if we need to fill all vectors
kono
parents:
diff changeset
348 -- (MSG_WAITALL flag is set).
kono
parents:
diff changeset
349
kono
parents:
diff changeset
350 exit when Natural (Count) >= Req.Size
kono
parents:
diff changeset
351 and then
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 -- Either we are not in fill mode
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 (not Fill
kono
parents:
diff changeset
356
kono
parents:
diff changeset
357 -- Or else last vector filled
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 or else (Interfaces.C.size_t (Iov_Index) = Iovec'Last
kono
parents:
diff changeset
360 and then Current_Iovec.Length = 0));
kono
parents:
diff changeset
361 end if;
kono
parents:
diff changeset
362 end loop;
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 return System.CRTL.ssize_t (Count);
kono
parents:
diff changeset
365 end C_Recvmsg;
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 --------------
kono
parents:
diff changeset
368 -- C_Select --
kono
parents:
diff changeset
369 --------------
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 function C_Select
kono
parents:
diff changeset
372 (Nfds : C.int;
kono
parents:
diff changeset
373 Readfds : access Fd_Set;
kono
parents:
diff changeset
374 Writefds : access Fd_Set;
kono
parents:
diff changeset
375 Exceptfds : access Fd_Set;
kono
parents:
diff changeset
376 Timeout : Timeval_Access) return C.int
kono
parents:
diff changeset
377 is
kono
parents:
diff changeset
378 pragma Warnings (Off, Exceptfds);
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 Original_WFS : aliased constant Fd_Set := Writefds.all;
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 Res : C.int;
kono
parents:
diff changeset
383 S : aliased C.int;
kono
parents:
diff changeset
384 Last : aliased C.int;
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 begin
kono
parents:
diff changeset
387 -- Asynchronous connection failures are notified in the exception fd
kono
parents:
diff changeset
388 -- set instead of the write fd set. To ensure POSIX compatibility, copy
kono
parents:
diff changeset
389 -- write fd set into exception fd set. Once select() returns, check any
kono
parents:
diff changeset
390 -- socket present in the exception fd set and peek at incoming
kono
parents:
diff changeset
391 -- out-of-band data. If the test is not successful, and the socket is
kono
parents:
diff changeset
392 -- present in the initial write fd set, then move the socket from the
kono
parents:
diff changeset
393 -- exception fd set to the write fd set.
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 if Writefds /= No_Fd_Set_Access then
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 -- Add any socket present in write fd set into exception fd set
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 declare
kono
parents:
diff changeset
400 WFS : aliased Fd_Set := Writefds.all;
kono
parents:
diff changeset
401 begin
kono
parents:
diff changeset
402 Last := Nfds - 1;
kono
parents:
diff changeset
403 loop
kono
parents:
diff changeset
404 Get_Socket_From_Set
kono
parents:
diff changeset
405 (WFS'Access, S'Unchecked_Access, Last'Unchecked_Access);
kono
parents:
diff changeset
406 exit when S = -1;
kono
parents:
diff changeset
407 Insert_Socket_In_Set (Exceptfds, S);
kono
parents:
diff changeset
408 end loop;
kono
parents:
diff changeset
409 end;
kono
parents:
diff changeset
410 end if;
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 Res := Standard_Select (Nfds, Readfds, Writefds, Exceptfds, Timeout);
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 if Exceptfds /= No_Fd_Set_Access then
kono
parents:
diff changeset
415 declare
kono
parents:
diff changeset
416 EFSC : aliased Fd_Set := Exceptfds.all;
kono
parents:
diff changeset
417 Flag : constant C.int := SOSC.MSG_PEEK + SOSC.MSG_OOB;
kono
parents:
diff changeset
418 Buffer : Character;
kono
parents:
diff changeset
419 Length : C.int;
kono
parents:
diff changeset
420 Fromlen : aliased C.int;
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422 begin
kono
parents:
diff changeset
423 Last := Nfds - 1;
kono
parents:
diff changeset
424 loop
kono
parents:
diff changeset
425 Get_Socket_From_Set
kono
parents:
diff changeset
426 (EFSC'Access, S'Unchecked_Access, Last'Unchecked_Access);
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 -- No more sockets in EFSC
kono
parents:
diff changeset
429
kono
parents:
diff changeset
430 exit when S = -1;
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 -- Check out-of-band data
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 Length :=
kono
parents:
diff changeset
435 C_Recvfrom
kono
parents:
diff changeset
436 (S, Buffer'Address, 1, Flag,
kono
parents:
diff changeset
437 From => System.Null_Address,
kono
parents:
diff changeset
438 Fromlen => Fromlen'Unchecked_Access);
kono
parents:
diff changeset
439 -- Is Fromlen necessary if From is Null_Address???
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 -- If the signal is not an out-of-band data, then it
kono
parents:
diff changeset
442 -- is a connection failure notification.
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 if Length = -1 then
kono
parents:
diff changeset
445 Remove_Socket_From_Set (Exceptfds, S);
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 -- If S is present in the initial write fd set, move it from
kono
parents:
diff changeset
448 -- exception fd set back to write fd set. Otherwise, ignore
kono
parents:
diff changeset
449 -- this event since the user is not watching for it.
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 if Writefds /= No_Fd_Set_Access
kono
parents:
diff changeset
452 and then (Is_Socket_In_Set (Original_WFS'Access, S) /= 0)
kono
parents:
diff changeset
453 then
kono
parents:
diff changeset
454 Insert_Socket_In_Set (Writefds, S);
kono
parents:
diff changeset
455 end if;
kono
parents:
diff changeset
456 end if;
kono
parents:
diff changeset
457 end loop;
kono
parents:
diff changeset
458 end;
kono
parents:
diff changeset
459 end if;
kono
parents:
diff changeset
460 return Res;
kono
parents:
diff changeset
461 end C_Select;
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 ---------------
kono
parents:
diff changeset
464 -- C_Sendmsg --
kono
parents:
diff changeset
465 ---------------
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 function C_Sendmsg
kono
parents:
diff changeset
468 (S : C.int;
kono
parents:
diff changeset
469 Msg : System.Address;
kono
parents:
diff changeset
470 Flags : C.int) return System.CRTL.ssize_t
kono
parents:
diff changeset
471 is
kono
parents:
diff changeset
472 use type C.size_t;
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 Res : C.int;
kono
parents:
diff changeset
475 Count : C.int := 0;
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 MH : Msghdr;
kono
parents:
diff changeset
478 for MH'Address use Msg;
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 Iovec : array (0 .. MH.Msg_Iovlen - 1) of Vector_Element;
kono
parents:
diff changeset
481 for Iovec'Address use MH.Msg_Iov;
kono
parents:
diff changeset
482 pragma Import (Ada, Iovec);
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 begin
kono
parents:
diff changeset
485 -- Windows does not provide an implementation of sendmsg(). The spec for
kono
parents:
diff changeset
486 -- WSASendMsg() is incompatible with the data types we define, and is
kono
parents:
diff changeset
487 -- available starting with Windows Vista and Server 2008 only. So
kono
parents:
diff changeset
488 -- use C_Sendto instead.
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 for J in Iovec'Range loop
kono
parents:
diff changeset
491 Res :=
kono
parents:
diff changeset
492 C_Sendto
kono
parents:
diff changeset
493 (S,
kono
parents:
diff changeset
494 Iovec (J).Base.all'Address,
kono
parents:
diff changeset
495 C.int (Iovec (J).Length),
kono
parents:
diff changeset
496 Flags => Flags,
kono
parents:
diff changeset
497 To => MH.Msg_Name,
kono
parents:
diff changeset
498 Tolen => C.int (MH.Msg_Namelen));
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 if Res < 0 then
kono
parents:
diff changeset
501 return System.CRTL.ssize_t (Res);
kono
parents:
diff changeset
502 else
kono
parents:
diff changeset
503 Count := Count + Res;
kono
parents:
diff changeset
504 end if;
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 -- Exit now if the buffer is not fully transmitted
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 exit when Interfaces.C.size_t (Res) < Iovec (J).Length;
kono
parents:
diff changeset
509 end loop;
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 return System.CRTL.ssize_t (Count);
kono
parents:
diff changeset
512 end C_Sendmsg;
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 --------------
kono
parents:
diff changeset
515 -- Finalize --
kono
parents:
diff changeset
516 --------------
kono
parents:
diff changeset
517
kono
parents:
diff changeset
518 procedure Finalize is
kono
parents:
diff changeset
519 begin
kono
parents:
diff changeset
520 if Initialized then
kono
parents:
diff changeset
521 WSACleanup;
kono
parents:
diff changeset
522 Initialized := False;
kono
parents:
diff changeset
523 end if;
kono
parents:
diff changeset
524 end Finalize;
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 -------------------------
kono
parents:
diff changeset
527 -- Host_Error_Messages --
kono
parents:
diff changeset
528 -------------------------
kono
parents:
diff changeset
529
kono
parents:
diff changeset
530 package body Host_Error_Messages is
kono
parents:
diff changeset
531
kono
parents:
diff changeset
532 -- On Windows, socket and host errors share the same code space, and
kono
parents:
diff changeset
533 -- error messages are provided by Socket_Error_Message, so the default
kono
parents:
diff changeset
534 -- separate body for Host_Error_Messages is not used in this case.
kono
parents:
diff changeset
535
kono
parents:
diff changeset
536 function Host_Error_Message (H_Errno : Integer) return String
kono
parents:
diff changeset
537 renames Socket_Error_Message;
kono
parents:
diff changeset
538
kono
parents:
diff changeset
539 end Host_Error_Messages;
kono
parents:
diff changeset
540
kono
parents:
diff changeset
541 ----------------
kono
parents:
diff changeset
542 -- Initialize --
kono
parents:
diff changeset
543 ----------------
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 procedure Initialize is
kono
parents:
diff changeset
546 Return_Value : Interfaces.C.int;
kono
parents:
diff changeset
547 begin
kono
parents:
diff changeset
548 if not Initialized then
kono
parents:
diff changeset
549 Return_Value := WSAStartup (WS_Version, WSAData_Dummy'Address);
kono
parents:
diff changeset
550 pragma Assert (Return_Value = 0);
kono
parents:
diff changeset
551 Initialized := True;
kono
parents:
diff changeset
552 end if;
kono
parents:
diff changeset
553 end Initialize;
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 --------------------
kono
parents:
diff changeset
556 -- Signalling_Fds --
kono
parents:
diff changeset
557 --------------------
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 package body Signalling_Fds is separate;
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 --------------------------
kono
parents:
diff changeset
562 -- Socket_Error_Message --
kono
parents:
diff changeset
563 --------------------------
kono
parents:
diff changeset
564
kono
parents:
diff changeset
565 function Socket_Error_Message (Errno : Integer) return String is
kono
parents:
diff changeset
566 use GNAT.Sockets.SOSC;
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 Errm : C.Strings.chars_ptr;
kono
parents:
diff changeset
569
kono
parents:
diff changeset
570 begin
kono
parents:
diff changeset
571 case Errno is
kono
parents:
diff changeset
572 when EINTR => Errm := Error_Messages (N_EINTR);
kono
parents:
diff changeset
573 when EBADF => Errm := Error_Messages (N_EBADF);
kono
parents:
diff changeset
574 when EACCES => Errm := Error_Messages (N_EACCES);
kono
parents:
diff changeset
575 when EFAULT => Errm := Error_Messages (N_EFAULT);
kono
parents:
diff changeset
576 when EINVAL => Errm := Error_Messages (N_EINVAL);
kono
parents:
diff changeset
577 when EMFILE => Errm := Error_Messages (N_EMFILE);
kono
parents:
diff changeset
578 when EWOULDBLOCK => Errm := Error_Messages (N_EWOULDBLOCK);
kono
parents:
diff changeset
579 when EINPROGRESS => Errm := Error_Messages (N_EINPROGRESS);
kono
parents:
diff changeset
580 when EALREADY => Errm := Error_Messages (N_EALREADY);
kono
parents:
diff changeset
581 when ENOTSOCK => Errm := Error_Messages (N_ENOTSOCK);
kono
parents:
diff changeset
582 when EDESTADDRREQ => Errm := Error_Messages (N_EDESTADDRREQ);
kono
parents:
diff changeset
583 when EMSGSIZE => Errm := Error_Messages (N_EMSGSIZE);
kono
parents:
diff changeset
584 when EPROTOTYPE => Errm := Error_Messages (N_EPROTOTYPE);
kono
parents:
diff changeset
585 when ENOPROTOOPT => Errm := Error_Messages (N_ENOPROTOOPT);
kono
parents:
diff changeset
586 when EPROTONOSUPPORT => Errm := Error_Messages (N_EPROTONOSUPPORT);
kono
parents:
diff changeset
587 when ESOCKTNOSUPPORT => Errm := Error_Messages (N_ESOCKTNOSUPPORT);
kono
parents:
diff changeset
588 when EOPNOTSUPP => Errm := Error_Messages (N_EOPNOTSUPP);
kono
parents:
diff changeset
589 when EPFNOSUPPORT => Errm := Error_Messages (N_EPFNOSUPPORT);
kono
parents:
diff changeset
590 when EAFNOSUPPORT => Errm := Error_Messages (N_EAFNOSUPPORT);
kono
parents:
diff changeset
591 when EADDRINUSE => Errm := Error_Messages (N_EADDRINUSE);
kono
parents:
diff changeset
592 when EADDRNOTAVAIL => Errm := Error_Messages (N_EADDRNOTAVAIL);
kono
parents:
diff changeset
593 when ENETDOWN => Errm := Error_Messages (N_ENETDOWN);
kono
parents:
diff changeset
594 when ENETUNREACH => Errm := Error_Messages (N_ENETUNREACH);
kono
parents:
diff changeset
595 when ENETRESET => Errm := Error_Messages (N_ENETRESET);
kono
parents:
diff changeset
596 when ECONNABORTED => Errm := Error_Messages (N_ECONNABORTED);
kono
parents:
diff changeset
597 when ECONNRESET => Errm := Error_Messages (N_ECONNRESET);
kono
parents:
diff changeset
598 when ENOBUFS => Errm := Error_Messages (N_ENOBUFS);
kono
parents:
diff changeset
599 when EISCONN => Errm := Error_Messages (N_EISCONN);
kono
parents:
diff changeset
600 when ENOTCONN => Errm := Error_Messages (N_ENOTCONN);
kono
parents:
diff changeset
601 when ESHUTDOWN => Errm := Error_Messages (N_ESHUTDOWN);
kono
parents:
diff changeset
602 when ETOOMANYREFS => Errm := Error_Messages (N_ETOOMANYREFS);
kono
parents:
diff changeset
603 when ETIMEDOUT => Errm := Error_Messages (N_ETIMEDOUT);
kono
parents:
diff changeset
604 when ECONNREFUSED => Errm := Error_Messages (N_ECONNREFUSED);
kono
parents:
diff changeset
605 when ELOOP => Errm := Error_Messages (N_ELOOP);
kono
parents:
diff changeset
606 when ENAMETOOLONG => Errm := Error_Messages (N_ENAMETOOLONG);
kono
parents:
diff changeset
607 when EHOSTDOWN => Errm := Error_Messages (N_EHOSTDOWN);
kono
parents:
diff changeset
608 when EHOSTUNREACH => Errm := Error_Messages (N_EHOSTUNREACH);
kono
parents:
diff changeset
609
kono
parents:
diff changeset
610 -- Windows-specific error codes
kono
parents:
diff changeset
611
kono
parents:
diff changeset
612 when WSASYSNOTREADY => Errm := Error_Messages (N_WSASYSNOTREADY);
kono
parents:
diff changeset
613 when WSAVERNOTSUPPORTED =>
kono
parents:
diff changeset
614 Errm := Error_Messages (N_WSAVERNOTSUPPORTED);
kono
parents:
diff changeset
615 when WSANOTINITIALISED =>
kono
parents:
diff changeset
616 Errm := Error_Messages (N_WSANOTINITIALISED);
kono
parents:
diff changeset
617 when WSAEDISCON => Errm := Error_Messages (N_WSAEDISCON);
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 -- h_errno values
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 when HOST_NOT_FOUND => Errm := Error_Messages (N_HOST_NOT_FOUND);
kono
parents:
diff changeset
622 when TRY_AGAIN => Errm := Error_Messages (N_TRY_AGAIN);
kono
parents:
diff changeset
623 when NO_RECOVERY => Errm := Error_Messages (N_NO_RECOVERY);
kono
parents:
diff changeset
624 when NO_DATA => Errm := Error_Messages (N_NO_DATA);
kono
parents:
diff changeset
625 when others => Errm := Error_Messages (N_OTHERS);
kono
parents:
diff changeset
626 end case;
kono
parents:
diff changeset
627
kono
parents:
diff changeset
628 return Value (Errm);
kono
parents:
diff changeset
629 end Socket_Error_Message;
kono
parents:
diff changeset
630
kono
parents:
diff changeset
631 end GNAT.Sockets.Thin;