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;
39 with Interfaces.C; use Interfaces.C;
41 with System.Address_To_Access_Conversions;
43 package body GNAT.Sockets.Thin is
45 Non_Blocking_Sockets : aliased Fd_Set;
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 SOSC.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 : access C.int) 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");
97 function Syscall_Sendto
102 To : Sockaddr_In_Access;
103 Tolen : C.int) return C.int;
104 pragma Import (C, Syscall_Sendto, "sendto");
106 function Syscall_Socket
107 (Domain, Typ, Protocol : C.int) return C.int;
108 pragma Import (C, Syscall_Socket, "socket");
110 function Non_Blocking_Socket (S : C.int) return Boolean;
111 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
119 Addr : System.Address;
120 Addrlen : not null access C.int) return C.int
123 Val : aliased C.int := 1;
126 pragma Warnings (Off, Discard);
130 R := Syscall_Accept (S, Addr, Addrlen);
131 exit when SOSC.Thread_Blocking_IO
133 or else Non_Blocking_Socket (S)
134 or else Errno /= SOSC.EWOULDBLOCK;
138 if not SOSC.Thread_Blocking_IO
139 and then R /= Failure
141 -- A socket inherits the properties of its server, especially
142 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
143 -- tracks sockets set in non-blocking mode by user.
145 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
146 Discard := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
158 Name : System.Address;
159 Namelen : C.int) return C.int
164 Res := Syscall_Connect (S, Name, Namelen);
166 if SOSC.Thread_Blocking_IO
167 or else Res /= Failure
168 or else Non_Blocking_Socket (S)
169 or else Errno /= SOSC.EINPROGRESS
175 WSet : aliased Fd_Set;
176 Now : aliased Timeval;
179 Reset_Socket_Set (WSet'Access);
181 Insert_Socket_In_Set (WSet'Access, S);
188 Now'Unchecked_Access);
192 if Res = Failure then
200 Res := Syscall_Connect (S, Name, Namelen);
202 if Res = Failure and then Errno = SOSC.EISCONN then
203 return Thin_Common.Success;
217 Arg : access C.int) return C.int
220 if not SOSC.Thread_Blocking_IO
221 and then Req = SOSC.FIONBIO
224 Set_Non_Blocking_Socket (S, True);
228 return Syscall_Ioctl (S, Req, Arg);
237 Msg : System.Address;
239 Flags : C.int) return C.int
245 Res := Syscall_Recv (S, Msg, Len, Flags);
246 exit when SOSC.Thread_Blocking_IO
247 or else Res /= Failure
248 or else Non_Blocking_Socket (S)
249 or else Errno /= SOSC.EWOULDBLOCK;
262 Msg : System.Address;
265 From : Sockaddr_In_Access;
266 Fromlen : not null access C.int) return C.int
272 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
273 exit when SOSC.Thread_Blocking_IO
274 or else Res /= Failure
275 or else Non_Blocking_Socket (S)
276 or else Errno /= SOSC.EWOULDBLOCK;
289 Msg : System.Address;
292 To : Sockaddr_In_Access;
293 Tolen : C.int) return C.int
299 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
300 exit when SOSC.Thread_Blocking_IO
301 or else Res /= Failure
302 or else Non_Blocking_Socket (S)
303 or else Errno /= SOSC.EWOULDBLOCK;
317 Protocol : C.int) return C.int
320 Val : aliased C.int := 1;
323 pragma Unreferenced (Discard);
326 R := Syscall_Socket (Domain, Typ, Protocol);
328 if not SOSC.Thread_Blocking_IO
329 and then R /= Failure
331 -- Do not use C_Ioctl as this subprogram tracks sockets set
332 -- in non-blocking mode by user.
334 Discard := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
335 Set_Non_Blocking_Socket (R, False);
345 procedure Finalize is
350 -------------------------
351 -- Host_Error_Messages --
352 -------------------------
354 package body Host_Error_Messages is separate;
360 -- VMS does not support inet_pton(3), so emulate it here in terms of
365 Cp : C.Strings.chars_ptr;
366 Inp : System.Address) return C.int
372 package Conv is new System.Address_To_Access_Conversions (C.int);
373 function C_Inet_Addr (Cp : C.Strings.chars_ptr) return C.int;
374 pragma Import (C, C_Inet_Addr, "DECC$INET_ADDR");
376 if Af /= SOSC.AF_INET then
377 Set_Socket_Errno (SOSC.EAFNOSUPPORT);
381 if Cp = Null_Ptr or else Inp = Null_Address then
385 -- Special case for the all-ones broadcast address: this address has the
386 -- same in_addr_t value as Failure, and thus cannot be properly returned
389 if String'(Value (Cp)) = "255.255.255.255" then
390 Conv.To_Pointer (Inp).all := -1;
394 Res := C_Inet_Addr (Cp);
396 -- String is not a valid dotted quad
404 Conv.To_Pointer (Inp).all := Res;
412 procedure Initialize is
414 Reset_Socket_Set (Non_Blocking_Sockets'Access);
417 -------------------------
418 -- Non_Blocking_Socket --
419 -------------------------
421 function Non_Blocking_Socket (S : C.int) return Boolean is
425 R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0);
428 end Non_Blocking_Socket;
430 -----------------------------
431 -- Set_Non_Blocking_Socket --
432 -----------------------------
434 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
439 Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S);
441 Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S);
445 end Set_Non_Blocking_Socket;
451 package body Signalling_Fds is separate;
453 --------------------------
454 -- Socket_Error_Message --
455 --------------------------
457 function Socket_Error_Message
458 (Errno : Integer) return C.Strings.chars_ptr
460 use type Interfaces.C.Strings.chars_ptr;
462 C_Msg : C.Strings.chars_ptr;
465 C_Msg := C_Strerror (C.int (Errno));
467 if C_Msg = C.Strings.Null_Ptr then
468 return Unknown_System_Error;
472 end Socket_Error_Message;
480 Iov : System.Address;
481 Iovcnt : C.int) return C.int
486 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
487 for Iovec'Address use Iov;
488 pragma Import (Ada, Iovec);
491 for J in Iovec'Range loop
494 Iovec (J).Base.all'Address,
495 Interfaces.C.int (Iovec (J).Length),
501 Count := Count + Res;
513 Iov : System.Address;
514 Iovcnt : C.int) return C.int
519 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
520 for Iovec'Address use Iov;
521 pragma Import (Ada, Iovec);
524 for J in Iovec'Range loop
527 Iovec (J).Base.all'Address,
528 Interfaces.C.int (Iovec (J).Length),
529 SOSC.MSG_Forced_Flags,
536 Count := Count + Res;
542 end GNAT.Sockets.Thin;