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-2011, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package provides a target dependent thin interface to the sockets
33 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
34 -- should not be directly with'ed by an applications program.
36 -- This version is for NT
38 with Ada
.Streams
; use Ada
.Streams
;
39 with Ada
.Unchecked_Conversion
;
40 with Interfaces
.C
.Strings
; use Interfaces
.C
.Strings
;
41 with System
; use System
;
42 with System
.Storage_Elements
; use System
.Storage_Elements
;
44 package body GNAT
.Sockets
.Thin
is
49 WSAData_Dummy
: array (1 .. 512) of C
.int
;
51 WS_Version
: constant := 16#
0202#
;
54 Initialized
: Boolean := False;
56 function Standard_Connect
58 Name
: System
.Address
;
59 Namelen
: C
.int
) return C
.int
;
60 pragma Import
(Stdcall
, Standard_Connect
, "connect");
62 function Standard_Select
64 Readfds
: access Fd_Set
;
65 Writefds
: access Fd_Set
;
66 Exceptfds
: access Fd_Set
;
67 Timeout
: Timeval_Access
) return C
.int
;
68 pragma Import
(Stdcall
, Standard_Select
, "select");
109 N_WSAVERNOTSUPPORTED
,
118 Error_Messages
: constant array (Error_Type
) of chars_ptr
:=
120 New_String
("Interrupted system call"),
122 New_String
("Bad file number"),
124 New_String
("Permission denied"),
126 New_String
("Bad address"),
128 New_String
("Invalid argument"),
130 New_String
("Too many open files"),
132 New_String
("Operation would block"),
134 New_String
("Operation now in progress. This error is "
135 & "returned if any Windows Sockets API "
136 & "function is called while a blocking "
137 & "function is in progress"),
139 New_String
("Operation already in progress"),
141 New_String
("Socket operation on nonsocket"),
143 New_String
("Destination address required"),
145 New_String
("Message too long"),
147 New_String
("Protocol wrong type for socket"),
149 New_String
("Protocol not available"),
151 New_String
("Protocol not supported"),
153 New_String
("Socket type not supported"),
155 New_String
("Operation not supported on socket"),
157 New_String
("Protocol family not supported"),
159 New_String
("Address family not supported by protocol family"),
161 New_String
("Address already in use"),
163 New_String
("Cannot assign requested address"),
165 New_String
("Network is down. This error may be "
166 & "reported at any time if the Windows "
167 & "Sockets implementation detects an "
168 & "underlying failure"),
170 New_String
("Network is unreachable"),
172 New_String
("Network dropped connection on reset"),
174 New_String
("Software caused connection abort"),
176 New_String
("Connection reset by peer"),
178 New_String
("No buffer space available"),
180 New_String
("Socket is already connected"),
182 New_String
("Socket is not connected"),
184 New_String
("Cannot send after socket shutdown"),
186 New_String
("Too many references: cannot splice"),
188 New_String
("Connection timed out"),
190 New_String
("Connection refused"),
192 New_String
("Too many levels of symbolic links"),
194 New_String
("File name too long"),
196 New_String
("Host is down"),
198 New_String
("No route to host"),
200 New_String
("Returned by WSAStartup(), indicating that "
201 & "the network subsystem is unusable"),
202 N_WSAVERNOTSUPPORTED
=>
203 New_String
("Returned by WSAStartup(), indicating that "
204 & "the Windows Sockets DLL cannot support "
205 & "this application"),
206 N_WSANOTINITIALISED
=>
207 New_String
("Winsock not initialized. This message is "
208 & "returned by any function except WSAStartup(), "
209 & "indicating that a successful WSAStartup() has "
210 & "not yet been performed"),
212 New_String
("Disconnected"),
214 New_String
("Host not found. This message indicates "
215 & "that the key (name, address, and so on) was not found"),
217 New_String
("Nonauthoritative host not found. This error may "
218 & "suggest that the name service itself is not "
221 New_String
("Nonrecoverable error. This error may suggest that the "
222 & "name service itself is not functioning"),
224 New_String
("Valid name, no data record of requested type. "
225 & "This error indicates that the key (name, address, "
226 & "and so on) was not found."),
228 New_String
("Unknown system error"));
236 Name
: System
.Address
;
237 Namelen
: C
.int
) return C
.int
242 Res
:= Standard_Connect
(S
, Name
, Namelen
);
245 if Socket_Errno
= SOSC
.EWOULDBLOCK
then
246 Set_Socket_Errno
(SOSC
.EINPROGRESS
);
257 function Socket_Ioctl
260 Arg
: access C
.int
) return C
.int
263 return C_Ioctl
(S
, Req
, Arg
);
272 Msg
: System
.Address
;
273 Flags
: C
.int
) return System
.CRTL
.ssize_t
277 Fill
: constant Boolean :=
278 SOSC
.MSG_WAITALL
/= -1
279 and then (C
.unsigned
(Flags
) and SOSC
.MSG_WAITALL
) /= 0;
280 -- Is the MSG_WAITALL flag set? If so we need to fully fill all vectors
286 for MH
'Address use Msg
;
288 Iovec
: array (0 .. MH
.Msg_Iovlen
- 1) of Vector_Element
;
289 for Iovec
'Address use MH
.Msg_Iov
;
290 pragma Import
(Ada
, Iovec
);
293 Current_Iovec
: Vector_Element
;
295 function To_Access
is new Ada
.Unchecked_Conversion
296 (System
.Address
, Stream_Element_Reference
);
297 pragma Warnings
(Off
, Stream_Element_Reference
);
299 Req
: Request_Type
(Name
=> N_Bytes_To_Read
);
302 -- Windows does not provide an implementation of recvmsg(). The spec for
303 -- WSARecvMsg() is incompatible with the data types we define, and is
304 -- available starting with Windows Vista and Server 2008 only. So,
305 -- we use C_Recv instead.
307 -- Check how much data are available
309 Control_Socket
(Socket_Type
(S
), Req
);
314 Current_Iovec
:= (Base
=> null, Length
=> 0);
317 if Current_Iovec
.Length
= 0 then
318 Iov_Index
:= Iov_Index
+ 1;
319 exit when Iov_Index
> Integer (Iovec
'Last);
320 Current_Iovec
:= Iovec
(SOSC
.Msg_Iovlen_T
(Iov_Index
));
326 Current_Iovec
.Base
.all'Address,
327 C
.int
(Current_Iovec
.Length
),
331 return System
.CRTL
.ssize_t
(Res
);
333 elsif Res
= 0 and then not Fill
then
337 pragma Assert
(Stream_Element_Count
(Res
) <= Current_Iovec
.Length
);
339 Count
:= Count
+ Res
;
340 Current_Iovec
.Length
:=
341 Current_Iovec
.Length
- Stream_Element_Count
(Res
);
342 Current_Iovec
.Base
:=
343 To_Access
(Current_Iovec
.Base
.all'Address
344 + Storage_Offset
(Res
));
346 -- If all the data that was initially available read, do not
347 -- attempt to receive more, since this might block, or merge data
348 -- from successive datagrams for a datagram-oriented socket. We
349 -- still try to receive more if we need to fill all vectors
350 -- (MSG_WAITALL flag is set).
352 exit when Natural (Count
) >= Req
.Size
355 -- Either we are not in fill mode
359 -- Or else last vector filled
361 or else (Interfaces
.C
.size_t
(Iov_Index
) = Iovec
'Last
362 and then Current_Iovec
.Length
= 0));
366 return System
.CRTL
.ssize_t
(Count
);
375 Readfds
: access Fd_Set
;
376 Writefds
: access Fd_Set
;
377 Exceptfds
: access Fd_Set
;
378 Timeout
: Timeval_Access
) return C
.int
380 pragma Warnings
(Off
, Exceptfds
);
382 Original_WFS
: aliased constant Fd_Set
:= Writefds
.all;
386 Last
: aliased C
.int
;
389 -- Asynchronous connection failures are notified in the exception fd
390 -- set instead of the write fd set. To ensure POSIX compatibility, copy
391 -- write fd set into exception fd set. Once select() returns, check any
392 -- socket present in the exception fd set and peek at incoming
393 -- out-of-band data. If the test is not successful, and the socket is
394 -- present in the initial write fd set, then move the socket from the
395 -- exception fd set to the write fd set.
397 if Writefds
/= No_Fd_Set_Access
then
399 -- Add any socket present in write fd set into exception fd set
402 WFS
: aliased Fd_Set
:= Writefds
.all;
407 (WFS
'Access, S
'Unchecked_Access, Last
'Unchecked_Access);
409 Insert_Socket_In_Set
(Exceptfds
, S
);
414 Res
:= Standard_Select
(Nfds
, Readfds
, Writefds
, Exceptfds
, Timeout
);
416 if Exceptfds
/= No_Fd_Set_Access
then
418 EFSC
: aliased Fd_Set
:= Exceptfds
.all;
419 Flag
: constant C
.int
:= SOSC
.MSG_PEEK
+ SOSC
.MSG_OOB
;
422 Fromlen
: aliased C
.int
;
428 (EFSC
'Access, S
'Unchecked_Access, Last
'Unchecked_Access);
430 -- No more sockets in EFSC
434 -- Check out-of-band data
438 (S
, Buffer
'Address, 1, Flag
,
439 From
=> System
.Null_Address
,
440 Fromlen
=> Fromlen
'Unchecked_Access);
441 -- Is Fromlen necessary if From is Null_Address???
443 -- If the signal is not an out-of-band data, then it
444 -- is a connection failure notification.
447 Remove_Socket_From_Set
(Exceptfds
, S
);
449 -- If S is present in the initial write fd set, move it from
450 -- exception fd set back to write fd set. Otherwise, ignore
451 -- this event since the user is not watching for it.
453 if Writefds
/= No_Fd_Set_Access
454 and then (Is_Socket_In_Set
(Original_WFS
'Access, S
) /= 0)
456 Insert_Socket_In_Set
(Writefds
, S
);
471 Msg
: System
.Address
;
472 Flags
: C
.int
) return System
.CRTL
.ssize_t
480 for MH
'Address use Msg
;
482 Iovec
: array (0 .. MH
.Msg_Iovlen
- 1) of Vector_Element
;
483 for Iovec
'Address use MH
.Msg_Iov
;
484 pragma Import
(Ada
, Iovec
);
487 -- Windows does not provide an implementation of sendmsg(). The spec for
488 -- WSASendMsg() is incompatible with the data types we define, and is
489 -- available starting with Windows Vista and Server 2008 only. So
490 -- use C_Sendto instead.
492 for J
in Iovec
'Range loop
496 Iovec
(J
).Base
.all'Address,
497 C
.int
(Iovec
(J
).Length
),
500 Tolen
=> C
.int
(MH
.Msg_Namelen
));
503 return System
.CRTL
.ssize_t
(Res
);
505 Count
:= Count
+ Res
;
508 -- Exit now if the buffer is not fully transmitted
510 exit when Stream_Element_Count
(Res
) < Iovec
(J
).Length
;
513 return System
.CRTL
.ssize_t
(Count
);
520 procedure Finalize
is
524 Initialized
:= False;
528 -------------------------
529 -- Host_Error_Messages --
530 -------------------------
532 package body Host_Error_Messages
is
534 -- On Windows, socket and host errors share the same code space, and
535 -- error messages are provided by Socket_Error_Message, so the default
536 -- separate body for Host_Error_Messages is not used in this case.
538 function Host_Error_Message
539 (H_Errno
: Integer) return C
.Strings
.chars_ptr
540 renames Socket_Error_Message
;
542 end Host_Error_Messages
;
548 procedure Initialize
is
549 Return_Value
: Interfaces
.C
.int
;
551 if not Initialized
then
552 Return_Value
:= WSAStartup
(WS_Version
, WSAData_Dummy
'Address);
553 pragma Assert
(Return_Value
= 0);
562 package body Signalling_Fds
is separate;
564 --------------------------
565 -- Socket_Error_Message --
566 --------------------------
568 function Socket_Error_Message
569 (Errno
: Integer) return C
.Strings
.chars_ptr
571 use GNAT
.Sockets
.SOSC
;
575 when EINTR
=> return Error_Messages
(N_EINTR
);
576 when EBADF
=> return Error_Messages
(N_EBADF
);
577 when EACCES
=> return Error_Messages
(N_EACCES
);
578 when EFAULT
=> return Error_Messages
(N_EFAULT
);
579 when EINVAL
=> return Error_Messages
(N_EINVAL
);
580 when EMFILE
=> return Error_Messages
(N_EMFILE
);
581 when EWOULDBLOCK
=> return Error_Messages
(N_EWOULDBLOCK
);
582 when EINPROGRESS
=> return Error_Messages
(N_EINPROGRESS
);
583 when EALREADY
=> return Error_Messages
(N_EALREADY
);
584 when ENOTSOCK
=> return Error_Messages
(N_ENOTSOCK
);
585 when EDESTADDRREQ
=> return Error_Messages
(N_EDESTADDRREQ
);
586 when EMSGSIZE
=> return Error_Messages
(N_EMSGSIZE
);
587 when EPROTOTYPE
=> return Error_Messages
(N_EPROTOTYPE
);
588 when ENOPROTOOPT
=> return Error_Messages
(N_ENOPROTOOPT
);
589 when EPROTONOSUPPORT
=> return Error_Messages
(N_EPROTONOSUPPORT
);
590 when ESOCKTNOSUPPORT
=> return Error_Messages
(N_ESOCKTNOSUPPORT
);
591 when EOPNOTSUPP
=> return Error_Messages
(N_EOPNOTSUPP
);
592 when EPFNOSUPPORT
=> return Error_Messages
(N_EPFNOSUPPORT
);
593 when EAFNOSUPPORT
=> return Error_Messages
(N_EAFNOSUPPORT
);
594 when EADDRINUSE
=> return Error_Messages
(N_EADDRINUSE
);
595 when EADDRNOTAVAIL
=> return Error_Messages
(N_EADDRNOTAVAIL
);
596 when ENETDOWN
=> return Error_Messages
(N_ENETDOWN
);
597 when ENETUNREACH
=> return Error_Messages
(N_ENETUNREACH
);
598 when ENETRESET
=> return Error_Messages
(N_ENETRESET
);
599 when ECONNABORTED
=> return Error_Messages
(N_ECONNABORTED
);
600 when ECONNRESET
=> return Error_Messages
(N_ECONNRESET
);
601 when ENOBUFS
=> return Error_Messages
(N_ENOBUFS
);
602 when EISCONN
=> return Error_Messages
(N_EISCONN
);
603 when ENOTCONN
=> return Error_Messages
(N_ENOTCONN
);
604 when ESHUTDOWN
=> return Error_Messages
(N_ESHUTDOWN
);
605 when ETOOMANYREFS
=> return Error_Messages
(N_ETOOMANYREFS
);
606 when ETIMEDOUT
=> return Error_Messages
(N_ETIMEDOUT
);
607 when ECONNREFUSED
=> return Error_Messages
(N_ECONNREFUSED
);
608 when ELOOP
=> return Error_Messages
(N_ELOOP
);
609 when ENAMETOOLONG
=> return Error_Messages
(N_ENAMETOOLONG
);
610 when EHOSTDOWN
=> return Error_Messages
(N_EHOSTDOWN
);
611 when EHOSTUNREACH
=> return Error_Messages
(N_EHOSTUNREACH
);
613 -- Windows-specific error codes
615 when WSASYSNOTREADY
=> return Error_Messages
(N_WSASYSNOTREADY
);
616 when WSAVERNOTSUPPORTED
=>
617 return Error_Messages
(N_WSAVERNOTSUPPORTED
);
618 when WSANOTINITIALISED
=>
619 return Error_Messages
(N_WSANOTINITIALISED
);
620 when WSAEDISCON
=> return Error_Messages
(N_WSAEDISCON
);
624 when HOST_NOT_FOUND
=> return Error_Messages
(N_HOST_NOT_FOUND
);
625 when TRY_AGAIN
=> return Error_Messages
(N_TRY_AGAIN
);
626 when NO_RECOVERY
=> return Error_Messages
(N_NO_RECOVERY
);
627 when NO_DATA
=> return Error_Messages
(N_NO_DATA
);
629 when others => return Error_Messages
(N_OTHERS
);
631 end Socket_Error_Message
;
633 end GNAT
.Sockets
.Thin
;