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-2003 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 -- This package provides a target dependent thin interface to the sockets
35 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
36 -- should not be directly with'ed by an applications program.
38 -- This is the default version
40 with GNAT.OS_Lib; use GNAT.OS_Lib;
43 with Interfaces.C; use Interfaces.C;
45 package body GNAT.Sockets.Thin is
47 Non_Blocking_Sockets : constant Fd_Set_Access
48 := New_Socket_Set (No_Socket_Set);
49 -- When this package is initialized with Process_Blocking_IO set
50 -- to True, sockets are set in non-blocking mode to avoid blocking
51 -- the whole process when a thread wants to perform a blocking IO
52 -- operation. But the user can also set a socket in non-blocking
53 -- mode by purpose. In order to make a difference between these
54 -- two situations, we track the origin of non-blocking mode in
55 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
56 -- been set in non-blocking mode by the user.
58 Quantum : constant Duration := 0.2;
59 -- When Thread_Blocking_IO is False, we set sockets in
60 -- non-blocking mode and we spend a period of time Quantum between
61 -- two attempts on a blocking operation.
63 Thread_Blocking_IO : Boolean := True;
65 function Syscall_Accept
67 Addr : System.Address;
68 Addrlen : access C.int)
70 pragma Import (C, Syscall_Accept, "accept");
72 function Syscall_Connect
74 Name : System.Address;
77 pragma Import (C, Syscall_Connect, "connect");
79 function Syscall_Ioctl
84 pragma Import (C, Syscall_Ioctl, "ioctl");
92 pragma Import (C, Syscall_Recv, "recv");
94 function Syscall_Recvfrom
99 From : Sockaddr_In_Access;
100 Fromlen : access C.int)
102 pragma Import (C, Syscall_Recvfrom, "recvfrom");
104 function Syscall_Send
106 Msg : System.Address;
110 pragma Import (C, Syscall_Send, "send");
112 function Syscall_Sendto
114 Msg : System.Address;
117 To : Sockaddr_In_Access;
120 pragma Import (C, Syscall_Sendto, "sendto");
122 function Syscall_Socket
123 (Domain, Typ, Protocol : C.int)
125 pragma Import (C, Syscall_Socket, "socket");
127 function Non_Blocking_Socket (S : C.int) return Boolean;
128 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
136 Addr : System.Address;
137 Addrlen : access C.int)
141 Val : aliased C.int := 1;
144 pragma Warnings (Off, Discard);
148 R := Syscall_Accept (S, Addr, Addrlen);
149 exit when Thread_Blocking_IO
151 or else Non_Blocking_Socket (S)
152 or else Errno /= Constants.EWOULDBLOCK;
156 if not Thread_Blocking_IO
157 and then R /= Failure
159 -- A socket inherits the properties ot its server especially
160 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
161 -- tracks sockets set in non-blocking mode by user.
163 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
164 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
176 Name : System.Address;
183 Res := Syscall_Connect (S, Name, Namelen);
185 if Thread_Blocking_IO
186 or else Res /= Failure
187 or else Non_Blocking_Socket (S)
188 or else Errno /= Constants.EINPROGRESS
194 WSet : Fd_Set_Access;
195 Now : aliased Timeval;
198 WSet := New_Socket_Set (No_Socket_Set);
200 Insert_Socket_In_Set (WSet, S);
207 Now'Unchecked_Access);
211 if Res = Failure then
212 Free_Socket_Set (WSet);
219 Free_Socket_Set (WSet);
222 Res := Syscall_Connect (S, Name, Namelen);
225 and then Errno = Constants.EISCONN
244 if not Thread_Blocking_IO
245 and then Req = Constants.FIONBIO
248 Set_Non_Blocking_Socket (S, True);
252 return Syscall_Ioctl (S, Req, Arg);
261 Msg : System.Address;
270 Res := Syscall_Recv (S, Msg, Len, Flags);
271 exit when Thread_Blocking_IO
272 or else Res /= Failure
273 or else Non_Blocking_Socket (S)
274 or else Errno /= Constants.EWOULDBLOCK;
287 Msg : System.Address;
290 From : Sockaddr_In_Access;
291 Fromlen : access C.int)
298 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
299 exit when Thread_Blocking_IO
300 or else Res /= Failure
301 or else Non_Blocking_Socket (S)
302 or else Errno /= Constants.EWOULDBLOCK;
315 Msg : System.Address;
324 Res := Syscall_Send (S, Msg, Len, Flags);
325 exit when Thread_Blocking_IO
326 or else Res /= Failure
327 or else Non_Blocking_Socket (S)
328 or else Errno /= Constants.EWOULDBLOCK;
341 Msg : System.Address;
344 To : Sockaddr_In_Access;
352 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
353 exit when Thread_Blocking_IO
354 or else Res /= Failure
355 or else Non_Blocking_Socket (S)
356 or else Errno /= Constants.EWOULDBLOCK;
374 Val : aliased C.int := 1;
377 pragma Unreferenced (Discard);
380 R := Syscall_Socket (Domain, Typ, Protocol);
382 if not Thread_Blocking_IO
383 and then R /= Failure
385 -- Do not use C_Ioctl as this subprogram tracks sockets set
386 -- in non-blocking mode by user.
388 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
389 Set_Non_Blocking_Socket (R, False);
399 procedure Finalize is
408 procedure Initialize (Process_Blocking_IO : Boolean) is
410 Thread_Blocking_IO := not Process_Blocking_IO;
413 -------------------------
414 -- Non_Blocking_Socket --
415 -------------------------
417 function Non_Blocking_Socket (S : C.int) return Boolean is
422 R := Is_Socket_In_Set (Non_Blocking_Sockets, S);
425 end Non_Blocking_Socket;
431 procedure Set_Address
432 (Sin : Sockaddr_In_Access;
436 Sin.Sin_Addr := Address;
444 (Sin : Sockaddr_In_Access;
448 Sin.Sin_Family := C.unsigned_short (Family);
456 (Sin : Sockaddr_In_Access;
459 pragma Unreferenced (Sin);
460 pragma Unreferenced (Len);
466 -----------------------------
467 -- Set_Non_Blocking_Socket --
468 -----------------------------
470 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
475 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
477 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
481 end Set_Non_Blocking_Socket;
488 (Sin : Sockaddr_In_Access;
489 Port : C.unsigned_short)
492 Sin.Sin_Port := Port;
495 --------------------------
496 -- Socket_Error_Message --
497 --------------------------
499 function Socket_Error_Message (Errno : Integer) return String is
500 use type Interfaces.C.Strings.chars_ptr;
502 C_Msg : C.Strings.chars_ptr;
505 C_Msg := C_Strerror (C.int (Errno));
507 if C_Msg = C.Strings.Null_Ptr then
508 return "Unknown system error";
511 return C.Strings.Value (C_Msg);
513 end Socket_Error_Message;
515 end GNAT.Sockets.Thin;