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-2009, 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 is the version for OpenVMS
36 with GNAT.OS_Lib; use GNAT.OS_Lib;
39 with Interfaces.C; use Interfaces.C;
41 package body GNAT.Sockets.Thin is
43 Non_Blocking_Sockets : aliased Fd_Set;
44 -- When this package is initialized with Process_Blocking_IO set to True,
45 -- sockets are set in non-blocking mode to avoid blocking the whole process
46 -- when a thread wants to perform a blocking IO operation. But the user can
47 -- also set a socket in non-blocking mode by purpose. In order to make a
48 -- difference between these two situations, we track the origin of
49 -- non-blocking mode in Non_Blocking_Sockets. If S is in
50 -- Non_Blocking_Sockets, it has been set in non-blocking mode by the user.
52 Quantum : constant Duration := 0.2;
53 -- When SOSC.Thread_Blocking_IO is False, we set sockets to non-blocking
54 -- mode and we spend a period of time Quantum between two attempts on a
55 -- blocking operation.
57 Unknown_System_Error : constant C.Strings.chars_ptr :=
58 C.Strings.New_String ("Unknown system error");
60 function Syscall_Accept
62 Addr : System.Address;
63 Addrlen : not null access C.int) return C.int;
64 pragma Import (C, Syscall_Accept, "accept");
66 function Syscall_Connect
68 Name : System.Address;
69 Namelen : C.int) return C.int;
70 pragma Import (C, Syscall_Connect, "connect");
72 function Syscall_Ioctl
75 Arg : access C.int) return C.int;
76 pragma Import (C, Syscall_Ioctl, "ioctl");
82 Flags : C.int) return C.int;
83 pragma Import (C, Syscall_Recv, "recv");
85 function Syscall_Recvfrom
90 From : Sockaddr_In_Access;
91 Fromlen : not null access C.int) return C.int;
92 pragma Import (C, Syscall_Recvfrom, "recvfrom");
94 function Syscall_Recvmsg
97 Flags : C.int) return C.int;
98 pragma Import (C, Syscall_Recvmsg, "recvmsg");
100 function Syscall_Sendmsg
102 Msg : System.Address;
103 Flags : C.int) return C.int;
104 pragma Import (C, Syscall_Sendmsg, "sendmsg");
106 function Syscall_Sendto
108 Msg : System.Address;
111 To : Sockaddr_In_Access;
112 Tolen : C.int) return C.int;
113 pragma Import (C, Syscall_Sendto, "sendto");
115 function Syscall_Socket
116 (Domain, Typ, Protocol : C.int) return C.int;
117 pragma Import (C, Syscall_Socket, "socket");
119 function Non_Blocking_Socket (S : C.int) return Boolean;
120 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
128 Addr : System.Address;
129 Addrlen : not null access C.int) return C.int
132 Val : aliased C.int := 1;
135 pragma Warnings (Off, Discard);
139 R := Syscall_Accept (S, Addr, Addrlen);
140 exit when SOSC.Thread_Blocking_IO
142 or else Non_Blocking_Socket (S)
143 or else Errno /= SOSC.EWOULDBLOCK;
147 if not SOSC.Thread_Blocking_IO
148 and then R /= Failure
150 -- A socket inherits the properties of its server, especially
151 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
152 -- tracks sockets set in non-blocking mode by user.
154 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
155 Discard := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
167 Name : System.Address;
168 Namelen : C.int) return C.int
173 Res := Syscall_Connect (S, Name, Namelen);
175 if SOSC.Thread_Blocking_IO
176 or else Res /= Failure
177 or else Non_Blocking_Socket (S)
178 or else Errno /= SOSC.EINPROGRESS
184 WSet : aliased Fd_Set;
185 Now : aliased Timeval;
188 Reset_Socket_Set (WSet'Access);
190 Insert_Socket_In_Set (WSet'Access, S);
197 Now'Unchecked_Access);
201 if Res = Failure then
209 Res := Syscall_Connect (S, Name, Namelen);
211 if Res = Failure and then Errno = SOSC.EISCONN then
212 return Thin_Common.Success;
226 Arg : access C.int) return C.int
229 if not SOSC.Thread_Blocking_IO
230 and then Req = SOSC.FIONBIO
233 Set_Non_Blocking_Socket (S, True);
237 return Syscall_Ioctl (S, Req, Arg);
246 Msg : System.Address;
248 Flags : C.int) return C.int
254 Res := Syscall_Recv (S, Msg, Len, Flags);
255 exit when SOSC.Thread_Blocking_IO
256 or else Res /= Failure
257 or else Non_Blocking_Socket (S)
258 or else Errno /= SOSC.EWOULDBLOCK;
271 Msg : System.Address;
274 From : Sockaddr_In_Access;
275 Fromlen : not null access C.int) return C.int
281 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
282 exit when SOSC.Thread_Blocking_IO
283 or else Res /= Failure
284 or else Non_Blocking_Socket (S)
285 or else Errno /= SOSC.EWOULDBLOCK;
298 Msg : System.Address;
299 Flags : C.int) return ssize_t
305 Res := Syscall_Recvmsg (S, Msg, Flags);
306 exit when SOSC.Thread_Blocking_IO
307 or else Res /= Failure
308 or else Non_Blocking_Socket (S)
309 or else Errno /= SOSC.EWOULDBLOCK;
313 return ssize_t (Res);
322 Msg : System.Address;
323 Flags : C.int) return ssize_t
329 Res := Syscall_Sendmsg (S, Msg, Flags);
330 exit when SOSC.Thread_Blocking_IO
331 or else Res /= Failure
332 or else Non_Blocking_Socket (S)
333 or else Errno /= SOSC.EWOULDBLOCK;
337 return ssize_t (Res);
346 Msg : System.Address;
349 To : Sockaddr_In_Access;
350 Tolen : C.int) return C.int
356 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
357 exit when SOSC.Thread_Blocking_IO
358 or else Res /= Failure
359 or else Non_Blocking_Socket (S)
360 or else Errno /= SOSC.EWOULDBLOCK;
374 Protocol : C.int) return C.int
377 Val : aliased C.int := 1;
380 pragma Unreferenced (Discard);
383 R := Syscall_Socket (Domain, Typ, Protocol);
385 if not SOSC.Thread_Blocking_IO
386 and then R /= Failure
388 -- Do not use C_Ioctl as this subprogram tracks sockets set
389 -- in non-blocking mode by user.
391 Discard := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
392 Set_Non_Blocking_Socket (R, False);
402 procedure Finalize is
407 -------------------------
408 -- Host_Error_Messages --
409 -------------------------
411 package body Host_Error_Messages is separate;
417 procedure Initialize is
419 Reset_Socket_Set (Non_Blocking_Sockets'Access);
422 -------------------------
423 -- Non_Blocking_Socket --
424 -------------------------
426 function Non_Blocking_Socket (S : C.int) return Boolean is
430 R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0);
433 end Non_Blocking_Socket;
435 -----------------------------
436 -- Set_Non_Blocking_Socket --
437 -----------------------------
439 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
444 Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S);
446 Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S);
450 end Set_Non_Blocking_Socket;
456 package body Signalling_Fds is separate;
458 --------------------------
459 -- Socket_Error_Message --
460 --------------------------
462 function Socket_Error_Message
463 (Errno : Integer) return C.Strings.chars_ptr
465 use type Interfaces.C.Strings.chars_ptr;
467 C_Msg : C.Strings.chars_ptr;
470 C_Msg := C_Strerror (C.int (Errno));
472 if C_Msg = C.Strings.Null_Ptr then
473 return Unknown_System_Error;
477 end Socket_Error_Message;
479 end GNAT.Sockets.Thin;