1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
9 -- Copyright (C) 2002-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 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 version is for VxWorks
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 : aliased Fd_Set;
48 -- When this package is initialized with Process_Blocking_IO set
49 -- to True, sockets are set in non-blocking mode to avoid blocking
50 -- the whole process when a thread wants to perform a blocking IO
51 -- operation. But the user can also set a socket in non-blocking
52 -- mode by purpose. In order to make a difference between these
53 -- two situations, we track the origin of non-blocking mode in
54 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
55 -- 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 in
59 -- non-blocking mode and we spend a period of time Quantum between
60 -- two attempts on a blocking operation.
62 Unknown_System_Error : constant C.Strings.chars_ptr :=
63 C.Strings.New_String ("Unknown system error");
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 -- All these require comments ???
71 function Syscall_Accept
73 Addr : System.Address;
74 Addrlen : not null access C.int) return C.int;
75 pragma Import (C, Syscall_Accept, "accept");
77 function Syscall_Connect
79 Name : System.Address;
80 Namelen : C.int) return C.int;
81 pragma Import (C, Syscall_Connect, "connect");
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
123 Protocol : C.int) return C.int;
124 pragma Import (C, Syscall_Socket, "socket");
126 function Non_Blocking_Socket (S : C.int) return Boolean;
127 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
135 Addr : System.Address;
136 Addrlen : not null access C.int) return C.int
139 Val : aliased C.int := 1;
142 pragma Unreferenced (Res);
146 R := Syscall_Accept (S, Addr, Addrlen);
147 exit when SOSC.Thread_Blocking_IO
149 or else Non_Blocking_Socket (S)
150 or else Errno /= SOSC.EWOULDBLOCK;
154 if not SOSC.Thread_Blocking_IO
155 and then R /= Failure
157 -- A socket inherits the properties of its server especially
158 -- the FIONBIO flag. Do not use Socket_Ioctl as this subprogram
159 -- tracks sockets set in non-blocking mode by user.
161 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
162 Res := C_Ioctl (R, SOSC.FIONBIO, Val'Access);
163 -- Is it OK to ignore result ???
175 Name : System.Address;
176 Namelen : C.int) return C.int
181 Res := Syscall_Connect (S, Name, Namelen);
183 if SOSC.Thread_Blocking_IO
184 or else Res /= Failure
185 or else Non_Blocking_Socket (S)
186 or else Errno /= SOSC.EINPROGRESS
192 WSet : aliased Fd_Set;
193 Now : aliased Timeval;
195 Reset_Socket_Set (WSet'Access);
197 Insert_Socket_In_Set (WSet'Access, S);
204 Now'Unchecked_Access);
208 if Res = Failure then
216 Res := Syscall_Connect (S, Name, Namelen);
219 and then Errno = SOSC.EISCONN
221 return Thin_Common.Success;
231 function Socket_Ioctl
234 Arg : access C.int) return C.int
237 if not SOSC.Thread_Blocking_IO and then Req = SOSC.FIONBIO then
239 Set_Non_Blocking_Socket (S, True);
243 return C_Ioctl (S, Req, Arg);
252 Msg : System.Address;
254 Flags : C.int) return C.int
260 Res := Syscall_Recv (S, Msg, Len, Flags);
261 exit when SOSC.Thread_Blocking_IO
262 or else Res /= Failure
263 or else Non_Blocking_Socket (S)
264 or else Errno /= SOSC.EWOULDBLOCK;
277 Msg : System.Address;
280 From : System.Address;
281 Fromlen : not null access C.int) return C.int
287 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
288 exit when SOSC.Thread_Blocking_IO
289 or else Res /= Failure
290 or else Non_Blocking_Socket (S)
291 or else Errno /= SOSC.EWOULDBLOCK;
304 Msg : System.Address;
305 Flags : C.int) return ssize_t
311 Res := Syscall_Recvmsg (S, Msg, Flags);
312 exit when SOSC.Thread_Blocking_IO
313 or else Res /= Failure
314 or else Non_Blocking_Socket (S)
315 or else Errno /= SOSC.EWOULDBLOCK;
319 return ssize_t (Res);
328 Msg : System.Address;
329 Flags : C.int) return ssize_t
335 Res := Syscall_Sendmsg (S, Msg, Flags);
336 exit when SOSC.Thread_Blocking_IO
337 or else Res /= Failure
338 or else Non_Blocking_Socket (S)
339 or else Errno /= SOSC.EWOULDBLOCK;
343 return ssize_t (Res);
352 Msg : System.Address;
356 Tolen : C.int) return C.int
362 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
363 exit when SOSC.Thread_Blocking_IO
364 or else Res /= Failure
365 or else Non_Blocking_Socket (S)
366 or else Errno /= SOSC.EWOULDBLOCK;
380 Protocol : C.int) return C.int
383 Val : aliased C.int := 1;
386 pragma Unreferenced (Res);
389 R := Syscall_Socket (Domain, Typ, Protocol);
391 if not SOSC.Thread_Blocking_IO
392 and then R /= Failure
394 -- Do not use Socket_Ioctl as this subprogram tracks sockets set
395 -- in non-blocking mode by user.
397 Res := C_Ioctl (R, SOSC.FIONBIO, Val'Access);
398 -- Is it OK to ignore result ???
399 Set_Non_Blocking_Socket (R, False);
409 procedure Finalize is
414 -------------------------
415 -- Host_Error_Messages --
416 -------------------------
418 package body Host_Error_Messages is separate;
424 procedure Initialize is
426 Reset_Socket_Set (Non_Blocking_Sockets'Access);
429 -------------------------
430 -- Non_Blocking_Socket --
431 -------------------------
433 function Non_Blocking_Socket (S : C.int) return Boolean is
437 R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0);
440 end Non_Blocking_Socket;
442 -----------------------------
443 -- Set_Non_Blocking_Socket --
444 -----------------------------
446 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
450 Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S);
452 Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S);
456 end Set_Non_Blocking_Socket;
462 package body Signalling_Fds is separate;
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;
484 end Socket_Error_Message;
486 end GNAT.Sockets.Thin;