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 type VMS_Msghdr is new Msghdr;
44 pragma Pack (VMS_Msghdr);
45 -- On VMS (unlike other platforms), struct msghdr is packed, so a specific
46 -- derived type is required.
48 Non_Blocking_Sockets : aliased Fd_Set;
49 -- When this package is initialized with Process_Blocking_IO set to True,
50 -- sockets are set in non-blocking mode to avoid blocking the whole process
51 -- when a thread wants to perform a blocking IO operation. But the user can
52 -- also set a socket in non-blocking mode by purpose. In order to make a
53 -- difference between these two situations, we track the origin of
54 -- non-blocking mode in Non_Blocking_Sockets. If S is in
55 -- Non_Blocking_Sockets, it has been set in non-blocking mode by the user.
57 Quantum : constant Duration := 0.2;
58 -- When SOSC.Thread_Blocking_IO is False, we set sockets to non-blocking
59 -- mode and we spend a period of time Quantum between two attempts on a
60 -- blocking operation.
62 Unknown_System_Error : constant C.Strings.chars_ptr :=
63 C.Strings.New_String ("Unknown system error");
65 function Syscall_Accept
67 Addr : System.Address;
68 Addrlen : not null access C.int) return C.int;
69 pragma Import (C, Syscall_Accept, "accept");
71 function Syscall_Connect
73 Name : System.Address;
74 Namelen : C.int) return C.int;
75 pragma Import (C, Syscall_Connect, "connect");
77 function Syscall_Ioctl
80 Arg : access C.int) return C.int;
81 pragma Import (C, Syscall_Ioctl, "ioctl");
87 Flags : C.int) return C.int;
88 pragma Import (C, Syscall_Recv, "recv");
90 function Syscall_Recvfrom
95 From : System.Address;
96 Fromlen : not null access C.int) return C.int;
97 pragma Import (C, Syscall_Recvfrom, "recvfrom");
99 function Syscall_Recvmsg
101 Msg : System.Address;
102 Flags : C.int) return C.int;
103 pragma Import (C, Syscall_Recvmsg, "recvmsg");
105 function Syscall_Sendmsg
107 Msg : System.Address;
108 Flags : C.int) return C.int;
109 pragma Import (C, Syscall_Sendmsg, "sendmsg");
111 function Syscall_Sendto
113 Msg : System.Address;
117 Tolen : C.int) return C.int;
118 pragma Import (C, Syscall_Sendto, "sendto");
120 function Syscall_Socket
121 (Domain, Typ, Protocol : C.int) return C.int;
122 pragma Import (C, Syscall_Socket, "socket");
124 function Non_Blocking_Socket (S : C.int) return Boolean;
125 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
133 Addr : System.Address;
134 Addrlen : not null access C.int) return C.int
137 Val : aliased C.int := 1;
140 pragma Warnings (Off, Discard);
144 R := Syscall_Accept (S, Addr, Addrlen);
145 exit when SOSC.Thread_Blocking_IO
147 or else Non_Blocking_Socket (S)
148 or else Errno /= SOSC.EWOULDBLOCK;
152 if not SOSC.Thread_Blocking_IO
153 and then R /= Failure
155 -- A socket inherits the properties of its server, especially
156 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
157 -- tracks sockets set in non-blocking mode by user.
159 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
160 Discard := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
172 Name : System.Address;
173 Namelen : C.int) return C.int
178 Res := Syscall_Connect (S, Name, Namelen);
180 if SOSC.Thread_Blocking_IO
181 or else Res /= Failure
182 or else Non_Blocking_Socket (S)
183 or else Errno /= SOSC.EINPROGRESS
189 WSet : aliased Fd_Set;
190 Now : aliased Timeval;
193 Reset_Socket_Set (WSet'Access);
195 Insert_Socket_In_Set (WSet'Access, S);
202 Now'Unchecked_Access);
206 if Res = Failure then
214 Res := Syscall_Connect (S, Name, Namelen);
216 if Res = Failure and then Errno = SOSC.EISCONN then
217 return Thin_Common.Success;
231 Arg : access C.int) return C.int
234 if not SOSC.Thread_Blocking_IO
235 and then Req = SOSC.FIONBIO
238 Set_Non_Blocking_Socket (S, True);
242 return Syscall_Ioctl (S, Req, Arg);
251 Msg : System.Address;
253 Flags : C.int) return C.int
259 Res := Syscall_Recv (S, Msg, Len, Flags);
260 exit when SOSC.Thread_Blocking_IO
261 or else Res /= Failure
262 or else Non_Blocking_Socket (S)
263 or else Errno /= SOSC.EWOULDBLOCK;
276 Msg : System.Address;
279 From : System.Address;
280 Fromlen : not null access C.int) return C.int
286 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
287 exit when SOSC.Thread_Blocking_IO
288 or else Res /= Failure
289 or else Non_Blocking_Socket (S)
290 or else Errno /= SOSC.EWOULDBLOCK;
303 Msg : System.Address;
304 Flags : C.int) return ssize_t
309 for GNAT_Msg'Address use Msg;
310 pragma Import (Ada, GNAT_Msg);
312 VMS_Msg : aliased VMS_Msghdr := VMS_Msghdr (GNAT_Msg);
315 Res := Syscall_Recvmsg (S, VMS_Msg'Address, Flags);
316 exit when SOSC.Thread_Blocking_IO
317 or else Res /= Failure
318 or else Non_Blocking_Socket (S)
319 or else Errno /= SOSC.EWOULDBLOCK;
322 GNAT_Msg := Msghdr (VMS_Msg);
324 return ssize_t (Res);
333 Msg : System.Address;
334 Flags : C.int) return ssize_t
339 for GNAT_Msg'Address use Msg;
340 pragma Import (Ada, GNAT_Msg);
342 VMS_Msg : aliased VMS_Msghdr := VMS_Msghdr (GNAT_Msg);
346 Res := Syscall_Sendmsg (S, VMS_Msg'Address, Flags);
347 exit when SOSC.Thread_Blocking_IO
348 or else Res /= Failure
349 or else Non_Blocking_Socket (S)
350 or else Errno /= SOSC.EWOULDBLOCK;
353 GNAT_Msg := Msghdr (VMS_Msg);
355 return ssize_t (Res);
364 Msg : System.Address;
368 Tolen : C.int) return C.int
374 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
375 exit when SOSC.Thread_Blocking_IO
376 or else Res /= Failure
377 or else Non_Blocking_Socket (S)
378 or else Errno /= SOSC.EWOULDBLOCK;
392 Protocol : C.int) return C.int
395 Val : aliased C.int := 1;
398 pragma Unreferenced (Discard);
401 R := Syscall_Socket (Domain, Typ, Protocol);
403 if not SOSC.Thread_Blocking_IO
404 and then R /= Failure
406 -- Do not use C_Ioctl as this subprogram tracks sockets set
407 -- in non-blocking mode by user.
409 Discard := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
410 Set_Non_Blocking_Socket (R, False);
420 procedure Finalize is
425 -------------------------
426 -- Host_Error_Messages --
427 -------------------------
429 package body Host_Error_Messages is separate;
435 procedure Initialize is
437 Reset_Socket_Set (Non_Blocking_Sockets'Access);
440 -------------------------
441 -- Non_Blocking_Socket --
442 -------------------------
444 function Non_Blocking_Socket (S : C.int) return Boolean is
448 R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0);
451 end Non_Blocking_Socket;
453 -----------------------------
454 -- Set_Non_Blocking_Socket --
455 -----------------------------
457 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
462 Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S);
464 Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S);
468 end Set_Non_Blocking_Socket;
474 package body Signalling_Fds is separate;
476 --------------------------
477 -- Socket_Error_Message --
478 --------------------------
480 function Socket_Error_Message
481 (Errno : Integer) return C.Strings.chars_ptr
483 use type Interfaces.C.Strings.chars_ptr;
485 C_Msg : C.Strings.chars_ptr;
488 C_Msg := C_Strerror (C.int (Errno));
490 if C_Msg = C.Strings.Null_Ptr then
491 return Unknown_System_Error;
495 end Socket_Error_Message;
497 end GNAT.Sockets.Thin;