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-2004 Ada Core Technologies, Inc. --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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;
104 pragma Import (C, Syscall_Send, "send");
106 function Syscall_Sendto
108 Msg : System.Address;
111 To : Sockaddr_In_Access;
114 pragma Import (C, Syscall_Sendto, "sendto");
116 function Syscall_Socket
117 (Domain, Typ, Protocol : C.int) return C.int;
118 pragma Import (C, Syscall_Socket, "socket");
120 function Non_Blocking_Socket (S : C.int) return Boolean;
121 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
129 Addr : System.Address;
130 Addrlen : access C.int) return C.int
133 Val : aliased C.int := 1;
136 pragma Warnings (Off, Discard);
140 R := Syscall_Accept (S, Addr, Addrlen);
141 exit when Thread_Blocking_IO
143 or else Non_Blocking_Socket (S)
144 or else Errno /= Constants.EWOULDBLOCK;
148 if not Thread_Blocking_IO
149 and then R /= Failure
151 -- A socket inherits the properties ot its server especially
152 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
153 -- tracks sockets set in non-blocking mode by user.
155 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
156 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
168 Name : System.Address;
169 Namelen : C.int) return C.int
174 Res := Syscall_Connect (S, Name, Namelen);
176 if Thread_Blocking_IO
177 or else Res /= Failure
178 or else Non_Blocking_Socket (S)
179 or else Errno /= Constants.EINPROGRESS
185 WSet : Fd_Set_Access;
186 Now : aliased Timeval;
189 WSet := New_Socket_Set (No_Socket_Set);
191 Insert_Socket_In_Set (WSet, S);
198 Now'Unchecked_Access);
202 if Res = Failure then
203 Free_Socket_Set (WSet);
210 Free_Socket_Set (WSet);
213 Res := Syscall_Connect (S, Name, Namelen);
216 and then Errno = Constants.EISCONN
231 Arg : Int_Access) return C.int
234 if not Thread_Blocking_IO
235 and then Req = Constants.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 Thread_Blocking_IO
261 or else Res /= Failure
262 or else Non_Blocking_Socket (S)
263 or else Errno /= Constants.EWOULDBLOCK;
276 Msg : System.Address;
279 From : Sockaddr_In_Access;
280 Fromlen : access C.int) return C.int
286 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
287 exit when Thread_Blocking_IO
288 or else Res /= Failure
289 or else Non_Blocking_Socket (S)
290 or else Errno /= Constants.EWOULDBLOCK;
303 Msg : System.Address;
305 Flags : C.int) return C.int
311 Res := Syscall_Send (S, Msg, Len, Flags);
312 exit when Thread_Blocking_IO
313 or else Res /= Failure
314 or else Non_Blocking_Socket (S)
315 or else Errno /= Constants.EWOULDBLOCK;
328 Msg : System.Address;
331 To : Sockaddr_In_Access;
332 Tolen : C.int) return C.int
338 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
339 exit when Thread_Blocking_IO
340 or else Res /= Failure
341 or else Non_Blocking_Socket (S)
342 or else Errno /= Constants.EWOULDBLOCK;
356 Protocol : C.int) return C.int
359 Val : aliased C.int := 1;
362 pragma Unreferenced (Discard);
365 R := Syscall_Socket (Domain, Typ, Protocol);
367 if not Thread_Blocking_IO
368 and then R /= Failure
370 -- Do not use C_Ioctl as this subprogram tracks sockets set
371 -- in non-blocking mode by user.
373 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
374 Set_Non_Blocking_Socket (R, False);
384 procedure Finalize is
393 procedure Initialize (Process_Blocking_IO : Boolean) is
395 Thread_Blocking_IO := not Process_Blocking_IO;
398 -------------------------
399 -- Non_Blocking_Socket --
400 -------------------------
402 function Non_Blocking_Socket (S : C.int) return Boolean is
406 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
409 end Non_Blocking_Socket;
415 procedure Set_Address (Sin : Sockaddr_In_Access; Address : In_Addr) is
417 Sin.Sin_Addr := Address;
424 procedure Set_Family (Sin : Sockaddr_In_Access; Family : C.int) is
426 Sin.Sin_Family := C.unsigned_short (Family);
433 procedure Set_Length (Sin : Sockaddr_In_Access; Len : C.int) is
434 pragma Unreferenced (Sin);
435 pragma Unreferenced (Len);
440 -----------------------------
441 -- Set_Non_Blocking_Socket --
442 -----------------------------
444 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
449 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
451 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
455 end Set_Non_Blocking_Socket;
461 procedure Set_Port (Sin : Sockaddr_In_Access; Port : C.unsigned_short) is
463 Sin.Sin_Port := Port;
466 --------------------------
467 -- Socket_Error_Message --
468 --------------------------
470 function Socket_Error_Message
471 (Errno : Integer) return C.Strings.chars_ptr
473 use type Interfaces.C.Strings.chars_ptr;
475 C_Msg : C.Strings.chars_ptr;
478 C_Msg := C_Strerror (C.int (Errno));
480 if C_Msg = C.Strings.Null_Ptr then
481 return Unknown_System_Error;
485 end Socket_Error_Message;
493 Iov : System.Address;
494 Iovcnt : C.int) return C.int
499 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
500 for Iovec'Address use Iov;
501 pragma Import (Ada, Iovec);
504 for J in Iovec'Range loop
507 Iovec (J).Base.all'Address,
508 Interfaces.C.int (Iovec (J).Length));
513 Count := Count + Res;
525 Iov : System.Address;
526 Iovcnt : C.int) return C.int
531 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
532 for Iovec'Address use Iov;
533 pragma Import (Ada, Iovec);
536 for J in Iovec'Range loop
539 Iovec (J).Base.all'Address,
540 Interfaces.C.int (Iovec (J).Length));
545 Count := Count + Res;
551 end GNAT.Sockets.Thin;