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-2008, 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 -- Temporary version for Alpha/VMS
36 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
37 with GNAT
.Sockets
.Constants
;
40 with Interfaces
.C
; use Interfaces
.C
;
42 package body GNAT
.Sockets
.Thin
is
44 Non_Blocking_Sockets
: constant Fd_Set_Access
:=
45 New_Socket_Set
(No_Fd_Set_Access
);
46 -- When this package is initialized with Process_Blocking_IO set
47 -- to True, sockets are set in non-blocking mode to avoid blocking
48 -- the whole process when a thread wants to perform a blocking IO
49 -- operation. But the user can also set a socket in non-blocking
50 -- mode by purpose. In order to make a difference between these
51 -- two situations, we track the origin of non-blocking mode in
52 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
53 -- been set in non-blocking mode by the user.
55 Quantum
: constant Duration := 0.2;
56 -- When Constants.Thread_Blocking_IO is False, we set sockets in
57 -- non-blocking mode and we spend a period of time Quantum between
58 -- two attempts on a blocking operation.
60 Unknown_System_Error
: constant C
.Strings
.chars_ptr
:=
61 C
.Strings
.New_String
("Unknown system error");
63 function Syscall_Accept
65 Addr
: System
.Address
;
66 Addrlen
: not null access C
.int
) return C
.int
;
67 pragma Import
(C
, Syscall_Accept
, "accept");
69 function Syscall_Connect
71 Name
: System
.Address
;
72 Namelen
: C
.int
) return C
.int
;
73 pragma Import
(C
, Syscall_Connect
, "connect");
75 function Syscall_Ioctl
78 Arg
: Int_Access
) return C
.int
;
79 pragma Import
(C
, Syscall_Ioctl
, "ioctl");
85 Flags
: C
.int
) return C
.int
;
86 pragma Import
(C
, Syscall_Recv
, "recv");
88 function Syscall_Recvfrom
93 From
: Sockaddr_In_Access
;
94 Fromlen
: not null access C
.int
) return C
.int
;
95 pragma Import
(C
, Syscall_Recvfrom
, "recvfrom");
101 Flags
: C
.int
) return C
.int
;
102 pragma Import
(C
, Syscall_Send
, "send");
104 function Syscall_Sendto
106 Msg
: System
.Address
;
109 To
: Sockaddr_In_Access
;
110 Tolen
: C
.int
) return C
.int
;
111 pragma Import
(C
, Syscall_Sendto
, "sendto");
113 function Syscall_Socket
114 (Domain
, Typ
, Protocol
: C
.int
) return C
.int
;
115 pragma Import
(C
, Syscall_Socket
, "socket");
117 function Non_Blocking_Socket
(S
: C
.int
) return Boolean;
118 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean);
126 Addr
: System
.Address
;
127 Addrlen
: not null access C
.int
) return C
.int
130 Val
: aliased C
.int
:= 1;
133 pragma Warnings
(Off
, Discard
);
137 R
:= Syscall_Accept
(S
, Addr
, Addrlen
);
138 exit when Constants
.Thread_Blocking_IO
140 or else Non_Blocking_Socket
(S
)
141 or else Errno
/= Constants
.EWOULDBLOCK
;
145 if not Constants
.Thread_Blocking_IO
146 and then R
/= Failure
148 -- A socket inherits the properties of its server, especially
149 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
150 -- tracks sockets set in non-blocking mode by user.
152 Set_Non_Blocking_Socket
(R
, Non_Blocking_Socket
(S
));
153 Discard
:= Syscall_Ioctl
(R
, Constants
.FIONBIO
, Val
'Unchecked_Access);
165 Name
: System
.Address
;
166 Namelen
: C
.int
) return C
.int
171 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
173 if Constants
.Thread_Blocking_IO
174 or else Res
/= Failure
175 or else Non_Blocking_Socket
(S
)
176 or else Errno
/= Constants
.EINPROGRESS
182 WSet
: Fd_Set_Access
;
183 Now
: aliased Timeval
;
186 WSet
:= New_Socket_Set
(No_Fd_Set_Access
);
188 Insert_Socket_In_Set
(WSet
, S
);
195 Now
'Unchecked_Access);
199 if Res
= Failure
then
200 Free_Socket_Set
(WSet
);
207 Free_Socket_Set
(WSet
);
210 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
212 if Res
= Failure
and then Errno
= Constants
.EISCONN
then
213 return Thin_Common
.Success
;
227 Arg
: Int_Access
) return C
.int
230 if not Constants
.Thread_Blocking_IO
231 and then Req
= Constants
.FIONBIO
234 Set_Non_Blocking_Socket
(S
, True);
238 return Syscall_Ioctl
(S
, Req
, Arg
);
247 Msg
: System
.Address
;
249 Flags
: C
.int
) return C
.int
255 Res
:= Syscall_Recv
(S
, Msg
, Len
, Flags
);
256 exit when Constants
.Thread_Blocking_IO
257 or else Res
/= Failure
258 or else Non_Blocking_Socket
(S
)
259 or else Errno
/= Constants
.EWOULDBLOCK
;
272 Msg
: System
.Address
;
275 From
: Sockaddr_In_Access
;
276 Fromlen
: not null access C
.int
) return C
.int
282 Res
:= Syscall_Recvfrom
(S
, Msg
, Len
, Flags
, From
, Fromlen
);
283 exit when Constants
.Thread_Blocking_IO
284 or else Res
/= Failure
285 or else Non_Blocking_Socket
(S
)
286 or else Errno
/= Constants
.EWOULDBLOCK
;
299 Msg
: System
.Address
;
301 Flags
: C
.int
) return C
.int
307 Res
:= Syscall_Send
(S
, Msg
, Len
, Flags
);
308 exit when Constants
.Thread_Blocking_IO
309 or else Res
/= Failure
310 or else Non_Blocking_Socket
(S
)
311 or else Errno
/= Constants
.EWOULDBLOCK
;
324 Msg
: System
.Address
;
327 To
: Sockaddr_In_Access
;
328 Tolen
: C
.int
) return C
.int
334 Res
:= Syscall_Sendto
(S
, Msg
, Len
, Flags
, To
, Tolen
);
335 exit when Constants
.Thread_Blocking_IO
336 or else Res
/= Failure
337 or else Non_Blocking_Socket
(S
)
338 or else Errno
/= Constants
.EWOULDBLOCK
;
352 Protocol
: C
.int
) return C
.int
355 Val
: aliased C
.int
:= 1;
358 pragma Unreferenced
(Discard
);
361 R
:= Syscall_Socket
(Domain
, Typ
, Protocol
);
363 if not Constants
.Thread_Blocking_IO
364 and then R
/= Failure
366 -- Do not use C_Ioctl as this subprogram tracks sockets set
367 -- in non-blocking mode by user.
369 Discard
:= Syscall_Ioctl
(R
, Constants
.FIONBIO
, Val
'Unchecked_Access);
370 Set_Non_Blocking_Socket
(R
, False);
380 procedure Finalize
is
385 -------------------------
386 -- Host_Error_Messages --
387 -------------------------
389 package body Host_Error_Messages
is separate;
395 procedure Initialize
is
400 -------------------------
401 -- Non_Blocking_Socket --
402 -------------------------
404 function Non_Blocking_Socket
(S
: C
.int
) return Boolean is
408 R
:= (Is_Socket_In_Set
(Non_Blocking_Sockets
, S
) /= 0);
411 end Non_Blocking_Socket
;
413 -----------------------------
414 -- Set_Non_Blocking_Socket --
415 -----------------------------
417 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean) is
422 Insert_Socket_In_Set
(Non_Blocking_Sockets
, S
);
424 Remove_Socket_From_Set
(Non_Blocking_Sockets
, S
);
428 end Set_Non_Blocking_Socket
;
434 package body Signalling_Fds
is separate;
436 --------------------------
437 -- Socket_Error_Message --
438 --------------------------
440 function Socket_Error_Message
441 (Errno
: Integer) return C
.Strings
.chars_ptr
443 use type Interfaces
.C
.Strings
.chars_ptr
;
445 C_Msg
: C
.Strings
.chars_ptr
;
448 C_Msg
:= C_Strerror
(C
.int
(Errno
));
450 if C_Msg
= C
.Strings
.Null_Ptr
then
451 return Unknown_System_Error
;
455 end Socket_Error_Message
;
463 Iov
: System
.Address
;
464 Iovcnt
: C
.int
) return C
.int
469 Iovec
: array (0 .. Iovcnt
- 1) of Vector_Element
;
470 for Iovec
'Address use Iov
;
471 pragma Import
(Ada
, Iovec
);
474 for J
in Iovec
'Range loop
477 Iovec
(J
).Base
.all'Address,
478 Interfaces
.C
.int
(Iovec
(J
).Length
),
484 Count
:= Count
+ Res
;
496 Iov
: System
.Address
;
497 Iovcnt
: C
.int
) return C
.int
502 Iovec
: array (0 .. Iovcnt
- 1) of Vector_Element
;
503 for Iovec
'Address use Iov
;
504 pragma Import
(Ada
, Iovec
);
507 for J
in Iovec
'Range loop
510 Iovec
(J
).Base
.all'Address,
511 Interfaces
.C
.int
(Iovec
(J
).Length
),
512 Constants
.MSG_Forced_Flags
);
517 Count
:= Count
+ Res
;
523 end GNAT
.Sockets
.Thin
;