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-2010, AdaCore --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This package provides a target dependent thin interface to the sockets
35 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
36 -- should not be directly with'ed by an applications program.
38 -- This version is for NT
40 with Ada
.Streams
; use Ada
.Streams
;
41 with Ada
.Unchecked_Conversion
;
42 with Interfaces
.C
.Strings
; use Interfaces
.C
.Strings
;
43 with System
; use System
;
44 with System
.Storage_Elements
; use System
.Storage_Elements
;
46 package body GNAT
.Sockets
.Thin
is
51 WSAData_Dummy
: array (1 .. 512) of C
.int
;
53 WS_Version
: constant := 16#
0202#
;
56 Initialized
: Boolean := False;
58 function Standard_Connect
60 Name
: System
.Address
;
61 Namelen
: C
.int
) return C
.int
;
62 pragma Import
(Stdcall
, Standard_Connect
, "connect");
64 function Standard_Select
66 Readfds
: access Fd_Set
;
67 Writefds
: access Fd_Set
;
68 Exceptfds
: access Fd_Set
;
69 Timeout
: Timeval_Access
) return C
.int
;
70 pragma Import
(Stdcall
, Standard_Select
, "select");
111 N_WSAVERNOTSUPPORTED
,
120 Error_Messages
: constant array (Error_Type
) of chars_ptr
:=
122 New_String
("Interrupted system call"),
124 New_String
("Bad file number"),
126 New_String
("Permission denied"),
128 New_String
("Bad address"),
130 New_String
("Invalid argument"),
132 New_String
("Too many open files"),
134 New_String
("Operation would block"),
136 New_String
("Operation now in progress. This error is "
137 & "returned if any Windows Sockets API "
138 & "function is called while a blocking "
139 & "function is in progress"),
141 New_String
("Operation already in progress"),
143 New_String
("Socket operation on nonsocket"),
145 New_String
("Destination address required"),
147 New_String
("Message too long"),
149 New_String
("Protocol wrong type for socket"),
151 New_String
("Protocol not available"),
153 New_String
("Protocol not supported"),
155 New_String
("Socket type not supported"),
157 New_String
("Operation not supported on socket"),
159 New_String
("Protocol family not supported"),
161 New_String
("Address family not supported by protocol family"),
163 New_String
("Address already in use"),
165 New_String
("Cannot assign requested address"),
167 New_String
("Network is down. This error may be "
168 & "reported at any time if the Windows "
169 & "Sockets implementation detects an "
170 & "underlying failure"),
172 New_String
("Network is unreachable"),
174 New_String
("Network dropped connection on reset"),
176 New_String
("Software caused connection abort"),
178 New_String
("Connection reset by peer"),
180 New_String
("No buffer space available"),
182 New_String
("Socket is already connected"),
184 New_String
("Socket is not connected"),
186 New_String
("Cannot send after socket shutdown"),
188 New_String
("Too many references: cannot splice"),
190 New_String
("Connection timed out"),
192 New_String
("Connection refused"),
194 New_String
("Too many levels of symbolic links"),
196 New_String
("File name too long"),
198 New_String
("Host is down"),
200 New_String
("No route to host"),
202 New_String
("Returned by WSAStartup(), indicating that "
203 & "the network subsystem is unusable"),
204 N_WSAVERNOTSUPPORTED
=>
205 New_String
("Returned by WSAStartup(), indicating that "
206 & "the Windows Sockets DLL cannot support "
207 & "this application"),
208 N_WSANOTINITIALISED
=>
209 New_String
("Winsock not initialized. This message is "
210 & "returned by any function except WSAStartup(), "
211 & "indicating that a successful WSAStartup() has "
212 & "not yet been performed"),
214 New_String
("Disconnected"),
216 New_String
("Host not found. This message indicates "
217 & "that the key (name, address, and so on) was not found"),
219 New_String
("Nonauthoritative host not found. This error may "
220 & "suggest that the name service itself is not "
223 New_String
("Nonrecoverable error. This error may suggest that the "
224 & "name service itself is not functioning"),
226 New_String
("Valid name, no data record of requested type. "
227 & "This error indicates that the key (name, address, "
228 & "and so on) was not found."),
230 New_String
("Unknown system error"));
238 Name
: System
.Address
;
239 Namelen
: C
.int
) return C
.int
244 Res
:= Standard_Connect
(S
, Name
, Namelen
);
247 if Socket_Errno
= SOSC
.EWOULDBLOCK
then
248 Set_Socket_Errno
(SOSC
.EINPROGRESS
);
259 function Socket_Ioctl
262 Arg
: access C
.int
) return C
.int
265 return C_Ioctl
(S
, Req
, Arg
);
274 Msg
: System
.Address
;
275 Flags
: C
.int
) return System
.CRTL
.ssize_t
279 Fill
: constant Boolean :=
280 (C
.unsigned
(Flags
) and SOSC
.MSG_WAITALL
) /= 0;
281 -- Is the MSG_WAITALL flag set? If so we need to fully fill all vectors
287 for MH
'Address use Msg
;
289 Iovec
: array (0 .. MH
.Msg_Iovlen
- 1) of Vector_Element
;
290 for Iovec
'Address use MH
.Msg_Iov
;
291 pragma Import
(Ada
, Iovec
);
294 Current_Iovec
: Vector_Element
;
296 function To_Access
is new Ada
.Unchecked_Conversion
297 (System
.Address
, Stream_Element_Reference
);
298 pragma Warnings
(Off
, Stream_Element_Reference
);
300 Req
: Request_Type
(Name
=> N_Bytes_To_Read
);
303 -- Windows does not provide an implementation of recvmsg(). The spec for
304 -- WSARecvMsg() is incompatible with the data types we define, and is
305 -- available starting with Windows Vista and Server 2008 only. So,
306 -- we use C_Recv instead.
308 -- Check how much data are available
310 Control_Socket
(Socket_Type
(S
), Req
);
315 Current_Iovec
:= (Base
=> null, Length
=> 0);
318 if Current_Iovec
.Length
= 0 then
319 Iov_Index
:= Iov_Index
+ 1;
320 exit when Iov_Index
> Integer (Iovec
'Last);
321 Current_Iovec
:= Iovec
(SOSC
.Msg_Iovlen_T
(Iov_Index
));
327 Current_Iovec
.Base
.all'Address,
328 C
.int
(Current_Iovec
.Length
),
332 return System
.CRTL
.ssize_t
(Res
);
334 elsif Res
= 0 and then not Fill
then
338 pragma Assert
(Stream_Element_Count
(Res
) <= Current_Iovec
.Length
);
340 Count
:= Count
+ Res
;
341 Current_Iovec
.Length
:=
342 Current_Iovec
.Length
- Stream_Element_Count
(Res
);
343 Current_Iovec
.Base
:=
344 To_Access
(Current_Iovec
.Base
.all'Address
345 + Storage_Offset
(Res
));
347 -- If all the data that was initially available read, do not
348 -- attempt to receive more, since this might block, or merge data
349 -- from successive datagrams for a datagram-oriented socket. We
350 -- still try to receive more if we need to fill all vectors
351 -- (MSG_WAITALL flag is set).
353 exit when Natural (Count
) >= Req
.Size
356 -- Either we are not in fill mode
360 -- Or else last vector filled
362 or else (Interfaces
.C
.size_t
(Iov_Index
) = Iovec
'Last
363 and then Current_Iovec
.Length
= 0));
367 return System
.CRTL
.ssize_t
(Count
);
376 Readfds
: access Fd_Set
;
377 Writefds
: access Fd_Set
;
378 Exceptfds
: access Fd_Set
;
379 Timeout
: Timeval_Access
) return C
.int
381 pragma Warnings
(Off
, Exceptfds
);
383 Original_WFS
: aliased constant Fd_Set
:= Writefds
.all;
387 Last
: aliased C
.int
;
390 -- Asynchronous connection failures are notified in the exception fd
391 -- set instead of the write fd set. To ensure POSIX compatibility, copy
392 -- write fd set into exception fd set. Once select() returns, check any
393 -- socket present in the exception fd set and peek at incoming
394 -- out-of-band data. If the test is not successful, and the socket is
395 -- present in the initial write fd set, then move the socket from the
396 -- exception fd set to the write fd set.
398 if Writefds
/= No_Fd_Set_Access
then
400 -- Add any socket present in write fd set into exception fd set
403 WFS
: aliased Fd_Set
:= Writefds
.all;
408 (WFS
'Access, S
'Unchecked_Access, Last
'Unchecked_Access);
410 Insert_Socket_In_Set
(Exceptfds
, S
);
415 Res
:= Standard_Select
(Nfds
, Readfds
, Writefds
, Exceptfds
, Timeout
);
417 if Exceptfds
/= No_Fd_Set_Access
then
419 EFSC
: aliased Fd_Set
:= Exceptfds
.all;
420 Flag
: constant C
.int
:= SOSC
.MSG_PEEK
+ SOSC
.MSG_OOB
;
423 Fromlen
: aliased C
.int
;
429 (EFSC
'Access, S
'Unchecked_Access, Last
'Unchecked_Access);
431 -- No more sockets in EFSC
435 -- Check out-of-band data
439 (S
, Buffer
'Address, 1, Flag
,
440 From
=> System
.Null_Address
,
441 Fromlen
=> Fromlen
'Unchecked_Access);
442 -- Is Fromlen necessary if From is Null_Address???
444 -- If the signal is not an out-of-band data, then it
445 -- is a connection failure notification.
448 Remove_Socket_From_Set
(Exceptfds
, S
);
450 -- If S is present in the initial write fd set, move it from
451 -- exception fd set back to write fd set. Otherwise, ignore
452 -- this event since the user is not watching for it.
454 if Writefds
/= No_Fd_Set_Access
455 and then (Is_Socket_In_Set
(Original_WFS
'Access, S
) /= 0)
457 Insert_Socket_In_Set
(Writefds
, S
);
472 Msg
: System
.Address
;
473 Flags
: C
.int
) return System
.CRTL
.ssize_t
481 for MH
'Address use Msg
;
483 Iovec
: array (0 .. MH
.Msg_Iovlen
- 1) of Vector_Element
;
484 for Iovec
'Address use MH
.Msg_Iov
;
485 pragma Import
(Ada
, Iovec
);
488 -- Windows does not provide an implementation of sendmsg(). The spec for
489 -- WSASendMsg() is incompatible with the data types we define, and is
490 -- available starting with Windows Vista and Server 2008 only. So
491 -- use C_Sendto instead.
493 for J
in Iovec
'Range loop
497 Iovec
(J
).Base
.all'Address,
498 C
.int
(Iovec
(J
).Length
),
501 Tolen
=> C
.int
(MH
.Msg_Namelen
));
504 return System
.CRTL
.ssize_t
(Res
);
506 Count
:= Count
+ Res
;
509 -- Exit now if the buffer is not fully transmitted
511 exit when Stream_Element_Count
(Res
) < Iovec
(J
).Length
;
514 return System
.CRTL
.ssize_t
(Count
);
521 procedure Finalize
is
525 Initialized
:= False;
529 -------------------------
530 -- Host_Error_Messages --
531 -------------------------
533 package body Host_Error_Messages
is
535 -- On Windows, socket and host errors share the same code space, and
536 -- error messages are provided by Socket_Error_Message, so the default
537 -- separate body for Host_Error_Messages is not used in this case.
539 function Host_Error_Message
540 (H_Errno
: Integer) return C
.Strings
.chars_ptr
541 renames Socket_Error_Message
;
543 end Host_Error_Messages
;
549 procedure Initialize
is
550 Return_Value
: Interfaces
.C
.int
;
552 if not Initialized
then
553 Return_Value
:= WSAStartup
(WS_Version
, WSAData_Dummy
'Address);
554 pragma Assert
(Return_Value
= 0);
563 package body Signalling_Fds
is separate;
565 --------------------------
566 -- Socket_Error_Message --
567 --------------------------
569 function Socket_Error_Message
570 (Errno
: Integer) return C
.Strings
.chars_ptr
572 use GNAT
.Sockets
.SOSC
;
576 when EINTR
=> return Error_Messages
(N_EINTR
);
577 when EBADF
=> return Error_Messages
(N_EBADF
);
578 when EACCES
=> return Error_Messages
(N_EACCES
);
579 when EFAULT
=> return Error_Messages
(N_EFAULT
);
580 when EINVAL
=> return Error_Messages
(N_EINVAL
);
581 when EMFILE
=> return Error_Messages
(N_EMFILE
);
582 when EWOULDBLOCK
=> return Error_Messages
(N_EWOULDBLOCK
);
583 when EINPROGRESS
=> return Error_Messages
(N_EINPROGRESS
);
584 when EALREADY
=> return Error_Messages
(N_EALREADY
);
585 when ENOTSOCK
=> return Error_Messages
(N_ENOTSOCK
);
586 when EDESTADDRREQ
=> return Error_Messages
(N_EDESTADDRREQ
);
587 when EMSGSIZE
=> return Error_Messages
(N_EMSGSIZE
);
588 when EPROTOTYPE
=> return Error_Messages
(N_EPROTOTYPE
);
589 when ENOPROTOOPT
=> return Error_Messages
(N_ENOPROTOOPT
);
590 when EPROTONOSUPPORT
=> return Error_Messages
(N_EPROTONOSUPPORT
);
591 when ESOCKTNOSUPPORT
=> return Error_Messages
(N_ESOCKTNOSUPPORT
);
592 when EOPNOTSUPP
=> return Error_Messages
(N_EOPNOTSUPP
);
593 when EPFNOSUPPORT
=> return Error_Messages
(N_EPFNOSUPPORT
);
594 when EAFNOSUPPORT
=> return Error_Messages
(N_EAFNOSUPPORT
);
595 when EADDRINUSE
=> return Error_Messages
(N_EADDRINUSE
);
596 when EADDRNOTAVAIL
=> return Error_Messages
(N_EADDRNOTAVAIL
);
597 when ENETDOWN
=> return Error_Messages
(N_ENETDOWN
);
598 when ENETUNREACH
=> return Error_Messages
(N_ENETUNREACH
);
599 when ENETRESET
=> return Error_Messages
(N_ENETRESET
);
600 when ECONNABORTED
=> return Error_Messages
(N_ECONNABORTED
);
601 when ECONNRESET
=> return Error_Messages
(N_ECONNRESET
);
602 when ENOBUFS
=> return Error_Messages
(N_ENOBUFS
);
603 when EISCONN
=> return Error_Messages
(N_EISCONN
);
604 when ENOTCONN
=> return Error_Messages
(N_ENOTCONN
);
605 when ESHUTDOWN
=> return Error_Messages
(N_ESHUTDOWN
);
606 when ETOOMANYREFS
=> return Error_Messages
(N_ETOOMANYREFS
);
607 when ETIMEDOUT
=> return Error_Messages
(N_ETIMEDOUT
);
608 when ECONNREFUSED
=> return Error_Messages
(N_ECONNREFUSED
);
609 when ELOOP
=> return Error_Messages
(N_ELOOP
);
610 when ENAMETOOLONG
=> return Error_Messages
(N_ENAMETOOLONG
);
611 when EHOSTDOWN
=> return Error_Messages
(N_EHOSTDOWN
);
612 when EHOSTUNREACH
=> return Error_Messages
(N_EHOSTUNREACH
);
614 -- Windows-specific error codes
616 when WSASYSNOTREADY
=> return Error_Messages
(N_WSASYSNOTREADY
);
617 when WSAVERNOTSUPPORTED
=>
618 return Error_Messages
(N_WSAVERNOTSUPPORTED
);
619 when WSANOTINITIALISED
=>
620 return Error_Messages
(N_WSANOTINITIALISED
);
621 when WSAEDISCON
=> return Error_Messages
(N_WSAEDISCON
);
625 when HOST_NOT_FOUND
=> return Error_Messages
(N_HOST_NOT_FOUND
);
626 when TRY_AGAIN
=> return Error_Messages
(N_TRY_AGAIN
);
627 when NO_RECOVERY
=> return Error_Messages
(N_NO_RECOVERY
);
628 when NO_DATA
=> return Error_Messages
(N_NO_DATA
);
630 when others => return Error_Messages
(N_OTHERS
);
632 end Socket_Error_Message
;
634 end GNAT
.Sockets
.Thin
;