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-2005, 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 package body GNAT.Sockets.Thin is
43 Non_Blocking_Sockets : constant Fd_Set_Access :=
44 New_Socket_Set (No_Socket_Set);
45 -- When this package is initialized with Process_Blocking_IO set
46 -- to True, sockets are set in non-blocking mode to avoid blocking
47 -- the whole process when a thread wants to perform a blocking IO
48 -- operation. But the user can also set a socket in non-blocking
49 -- mode by purpose. In order to make a difference between these
50 -- two situations, we track the origin of non-blocking mode in
51 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
52 -- been set in non-blocking mode by the user.
54 Quantum : constant Duration := 0.2;
55 -- When Thread_Blocking_IO is False, we set sockets in
56 -- non-blocking mode and we spend a period of time Quantum between
57 -- two attempts on a blocking operation.
59 Thread_Blocking_IO : Boolean := True;
61 Unknown_System_Error : constant C.Strings.chars_ptr :=
62 C.Strings.New_String ("Unknown system error");
64 function Syscall_Accept
66 Addr : System.Address;
67 Addrlen : access C.int) return C.int;
68 pragma Import (C, Syscall_Accept, "accept");
70 function Syscall_Connect
72 Name : System.Address;
73 Namelen : C.int) return C.int;
74 pragma Import (C, Syscall_Connect, "connect");
76 function Syscall_Ioctl
79 Arg : Int_Access) return C.int;
80 pragma Import (C, Syscall_Ioctl, "ioctl");
86 Flags : C.int) return C.int;
87 pragma Import (C, Syscall_Recv, "recv");
89 function Syscall_Recvfrom
94 From : Sockaddr_In_Access;
95 Fromlen : access C.int) return C.int;
96 pragma Import (C, Syscall_Recvfrom, "recvfrom");
100 Msg : System.Address;
102 Flags : C.int) return C.int;
103 pragma Import (C, Syscall_Send, "send");
105 function Syscall_Sendto
107 Msg : System.Address;
110 To : Sockaddr_In_Access;
111 Tolen : C.int) return C.int;
112 pragma Import (C, Syscall_Sendto, "sendto");
114 function Syscall_Socket
115 (Domain, Typ, Protocol : C.int) return C.int;
116 pragma Import (C, Syscall_Socket, "socket");
118 function Non_Blocking_Socket (S : C.int) return Boolean;
119 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
127 Addr : System.Address;
128 Addrlen : access C.int) return C.int
131 Val : aliased C.int := 1;
134 pragma Warnings (Off, Discard);
138 R := Syscall_Accept (S, Addr, Addrlen);
139 exit when Thread_Blocking_IO
141 or else Non_Blocking_Socket (S)
142 or else Errno /= Constants.EWOULDBLOCK;
146 if not Thread_Blocking_IO
147 and then R /= Failure
149 -- A socket inherits the properties ot its server especially
150 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
151 -- tracks sockets set in non-blocking mode by user.
153 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
154 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
166 Name : System.Address;
167 Namelen : C.int) return C.int
172 Res := Syscall_Connect (S, Name, Namelen);
174 if Thread_Blocking_IO
175 or else Res /= Failure
176 or else Non_Blocking_Socket (S)
177 or else Errno /= Constants.EINPROGRESS
183 WSet : Fd_Set_Access;
184 Now : aliased Timeval;
187 WSet := New_Socket_Set (No_Socket_Set);
189 Insert_Socket_In_Set (WSet, S);
196 Now'Unchecked_Access);
200 if Res = Failure then
201 Free_Socket_Set (WSet);
208 Free_Socket_Set (WSet);
211 Res := Syscall_Connect (S, Name, Namelen);
214 and then Errno = Constants.EISCONN
229 Arg : Int_Access) return C.int
232 if not Thread_Blocking_IO
233 and then Req = Constants.FIONBIO
236 Set_Non_Blocking_Socket (S, True);
240 return Syscall_Ioctl (S, Req, Arg);
249 Msg : System.Address;
251 Flags : C.int) return C.int
257 Res := Syscall_Recv (S, Msg, Len, Flags);
258 exit when Thread_Blocking_IO
259 or else Res /= Failure
260 or else Non_Blocking_Socket (S)
261 or else Errno /= Constants.EWOULDBLOCK;
274 Msg : System.Address;
277 From : Sockaddr_In_Access;
278 Fromlen : access C.int) return C.int
284 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
285 exit when Thread_Blocking_IO
286 or else Res /= Failure
287 or else Non_Blocking_Socket (S)
288 or else Errno /= Constants.EWOULDBLOCK;
301 Msg : System.Address;
303 Flags : C.int) return C.int
309 Res := Syscall_Send (S, Msg, Len, Flags);
310 exit when Thread_Blocking_IO
311 or else Res /= Failure
312 or else Non_Blocking_Socket (S)
313 or else Errno /= Constants.EWOULDBLOCK;
326 Msg : System.Address;
329 To : Sockaddr_In_Access;
330 Tolen : C.int) return C.int
336 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
337 exit when Thread_Blocking_IO
338 or else Res /= Failure
339 or else Non_Blocking_Socket (S)
340 or else Errno /= Constants.EWOULDBLOCK;
354 Protocol : C.int) return C.int
357 Val : aliased C.int := 1;
360 pragma Unreferenced (Discard);
363 R := Syscall_Socket (Domain, Typ, Protocol);
365 if not Thread_Blocking_IO
366 and then R /= Failure
368 -- Do not use C_Ioctl as this subprogram tracks sockets set
369 -- in non-blocking mode by user.
371 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
372 Set_Non_Blocking_Socket (R, False);
382 procedure Finalize is
391 procedure Initialize (Process_Blocking_IO : Boolean) is
393 Thread_Blocking_IO := not Process_Blocking_IO;
396 -------------------------
397 -- Non_Blocking_Socket --
398 -------------------------
400 function Non_Blocking_Socket (S : C.int) return Boolean is
404 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
407 end Non_Blocking_Socket;
413 procedure Set_Address (Sin : Sockaddr_In_Access; Address : In_Addr) is
415 Sin.Sin_Addr := Address;
422 procedure Set_Family (Sin : Sockaddr_In_Access; Family : C.int) is
424 Sin.Sin_Family := C.unsigned_short (Family);
431 procedure Set_Length (Sin : Sockaddr_In_Access; Len : C.int) is
432 pragma Unreferenced (Sin);
433 pragma Unreferenced (Len);
438 -----------------------------
439 -- Set_Non_Blocking_Socket --
440 -----------------------------
442 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
447 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
449 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
453 end Set_Non_Blocking_Socket;
459 procedure Set_Port (Sin : Sockaddr_In_Access; Port : C.unsigned_short) is
461 Sin.Sin_Port := Port;
464 --------------------------
465 -- Socket_Error_Message --
466 --------------------------
468 function Socket_Error_Message
469 (Errno : Integer) return C.Strings.chars_ptr
471 use type Interfaces.C.Strings.chars_ptr;
473 C_Msg : C.Strings.chars_ptr;
476 C_Msg := C_Strerror (C.int (Errno));
478 if C_Msg = C.Strings.Null_Ptr then
479 return Unknown_System_Error;
483 end Socket_Error_Message;
491 Iov : System.Address;
492 Iovcnt : C.int) return C.int
497 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
498 for Iovec'Address use Iov;
499 pragma Import (Ada, Iovec);
502 for J in Iovec'Range loop
505 Iovec (J).Base.all'Address,
506 Interfaces.C.int (Iovec (J).Length),
512 Count := Count + Res;
524 Iov : System.Address;
525 Iovcnt : C.int) return C.int
530 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
531 for Iovec'Address use Iov;
532 pragma Import (Ada, Iovec);
535 for J in Iovec'Range loop
538 Iovec (J).Base.all'Address,
539 Interfaces.C.int (Iovec (J).Length),
540 Constants.MSG_Forced_Flags);
545 Count := Count + Res;
551 end GNAT.Sockets.Thin;