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-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 -- 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;
41 with GNAT.Sockets.Constants;
44 with Interfaces.C; use Interfaces.C;
46 package body GNAT.Sockets.Thin is
48 Non_Blocking_Sockets : constant Fd_Set_Access :=
49 New_Socket_Set (No_Fd_Set_Access);
50 -- When this package is initialized with Process_Blocking_IO set
51 -- to True, sockets are set in non-blocking mode to avoid blocking
52 -- the whole process when a thread wants to perform a blocking IO
53 -- operation. But the user can also set a socket in non-blocking
54 -- mode by purpose. In order to make a difference between these
55 -- two situations, we track the origin of non-blocking mode in
56 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
57 -- been set in non-blocking mode by the user.
59 Quantum : constant Duration := 0.2;
60 -- When Constants.Thread_Blocking_IO is False, we set sockets in
61 -- non-blocking mode and we spend a period of time Quantum between
62 -- two attempts on a blocking operation.
64 Unknown_System_Error : constant C.Strings.chars_ptr :=
65 C.Strings.New_String ("Unknown system error");
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 -- All these require comments ???
73 function Syscall_Accept
75 Addr : System.Address;
76 Addrlen : not null access C.int) return C.int;
77 pragma Import (C, Syscall_Accept, "accept");
79 function Syscall_Connect
81 Name : System.Address;
82 Namelen : C.int) return C.int;
83 pragma Import (C, Syscall_Connect, "connect");
85 function Syscall_Ioctl
88 Arg : Int_Access) return C.int;
89 pragma Import (C, Syscall_Ioctl, "ioctl");
95 Flags : C.int) return C.int;
96 pragma Import (C, Syscall_Recv, "recv");
98 function Syscall_Recvfrom
100 Msg : System.Address;
103 From : Sockaddr_In_Access;
104 Fromlen : not null access C.int) return C.int;
105 pragma Import (C, Syscall_Recvfrom, "recvfrom");
107 function Syscall_Send
109 Msg : System.Address;
111 Flags : C.int) return C.int;
112 pragma Import (C, Syscall_Send, "send");
114 function Syscall_Sendto
116 Msg : System.Address;
119 To : Sockaddr_In_Access;
120 Tolen : C.int) return C.int;
121 pragma Import (C, Syscall_Sendto, "sendto");
123 function Syscall_Socket
126 Protocol : C.int) return C.int;
127 pragma Import (C, Syscall_Socket, "socket");
129 function Non_Blocking_Socket (S : C.int) return Boolean;
130 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
138 Addr : System.Address;
139 Addrlen : not null access C.int) return C.int
142 Val : aliased C.int := 1;
145 pragma Unreferenced (Res);
149 R := Syscall_Accept (S, Addr, Addrlen);
150 exit when Constants.Thread_Blocking_IO
152 or else Non_Blocking_Socket (S)
153 or else Errno /= Constants.EWOULDBLOCK;
157 if not Constants.Thread_Blocking_IO
158 and then R /= Failure
160 -- A socket inherits the properties of its server especially
161 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
162 -- tracks sockets set in non-blocking mode by user.
164 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
165 Res := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
166 -- Is it OK to ignore result ???
178 Name : System.Address;
179 Namelen : C.int) return C.int
184 Res := Syscall_Connect (S, Name, Namelen);
186 if Constants.Thread_Blocking_IO
187 or else Res /= Failure
188 or else Non_Blocking_Socket (S)
189 or else Errno /= Constants.EINPROGRESS
195 WSet : Fd_Set_Access;
196 Now : aliased Timeval;
199 WSet := New_Socket_Set (No_Fd_Set_Access);
202 Insert_Socket_In_Set (WSet, S);
209 Now'Unchecked_Access);
213 if Res = Failure then
214 Free_Socket_Set (WSet);
221 Free_Socket_Set (WSet);
224 Res := Syscall_Connect (S, Name, Namelen);
227 and then Errno = Constants.EISCONN
229 return Thin_Common.Success;
242 Arg : Int_Access) return C.int
245 if not Constants.Thread_Blocking_IO
246 and then Req = Constants.FIONBIO
249 Set_Non_Blocking_Socket (S, True);
253 return Syscall_Ioctl (S, Req, Arg);
262 Msg : System.Address;
264 Flags : C.int) return C.int
270 Res := Syscall_Recv (S, Msg, Len, Flags);
271 exit when Constants.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 : not null access C.int) return C.int
297 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
298 exit when Constants.Thread_Blocking_IO
299 or else Res /= Failure
300 or else Non_Blocking_Socket (S)
301 or else Errno /= Constants.EWOULDBLOCK;
314 Msg : System.Address;
316 Flags : C.int) return C.int
322 Res := Syscall_Send (S, Msg, Len, Flags);
323 exit when Constants.Thread_Blocking_IO
324 or else Res /= Failure
325 or else Non_Blocking_Socket (S)
326 or else Errno /= Constants.EWOULDBLOCK;
339 Msg : System.Address;
342 To : Sockaddr_In_Access;
343 Tolen : C.int) return C.int
349 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
350 exit when Constants.Thread_Blocking_IO
351 or else Res /= Failure
352 or else Non_Blocking_Socket (S)
353 or else Errno /= Constants.EWOULDBLOCK;
367 Protocol : C.int) return C.int
370 Val : aliased C.int := 1;
373 pragma Unreferenced (Res);
376 R := Syscall_Socket (Domain, Typ, Protocol);
378 if not Constants.Thread_Blocking_IO
379 and then R /= Failure
381 -- Do not use C_Ioctl as this subprogram tracks sockets set
382 -- in non-blocking mode by user.
384 Res := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
385 -- Is it OK to ignore result ???
386 Set_Non_Blocking_Socket (R, False);
396 procedure Finalize is
401 -------------------------
402 -- Host_Error_Messages --
403 -------------------------
405 package body Host_Error_Messages is separate;
411 procedure Initialize is
416 -------------------------
417 -- Non_Blocking_Socket --
418 -------------------------
420 function Non_Blocking_Socket (S : C.int) return Boolean is
424 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
427 end Non_Blocking_Socket;
429 -----------------------------
430 -- Set_Non_Blocking_Socket --
431 -----------------------------
433 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
437 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
439 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
443 end Set_Non_Blocking_Socket;
449 package body Signalling_Fds is separate;
451 --------------------------
452 -- Socket_Error_Message --
453 --------------------------
455 function Socket_Error_Message
456 (Errno : Integer) return C.Strings.chars_ptr
458 use type Interfaces.C.Strings.chars_ptr;
460 C_Msg : C.Strings.chars_ptr;
463 C_Msg := C_Strerror (C.int (Errno));
465 if C_Msg = C.Strings.Null_Ptr then
466 return Unknown_System_Error;
471 end Socket_Error_Message;
473 end GNAT.Sockets.Thin;