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_Send
113 Msg
: System
.Address
;
115 Flags
: C
.int
) return C
.int
;
116 pragma Import
(C
, Syscall_Send
, "send");
118 function Syscall_Sendto
120 Msg
: System
.Address
;
124 Tolen
: C
.int
) return C
.int
;
125 pragma Import
(C
, Syscall_Sendto
, "sendto");
127 function Syscall_Socket
130 Protocol
: C
.int
) return C
.int
;
131 pragma Import
(C
, Syscall_Socket
, "socket");
133 function Non_Blocking_Socket
(S
: C
.int
) return Boolean;
134 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean);
142 Addr
: System
.Address
;
143 Addrlen
: not null access C
.int
) return C
.int
146 Val
: aliased C
.int
:= 1;
149 pragma Unreferenced
(Res
);
153 R
:= Syscall_Accept
(S
, Addr
, Addrlen
);
154 exit when SOSC
.Thread_Blocking_IO
156 or else Non_Blocking_Socket
(S
)
157 or else Errno
/= SOSC
.EWOULDBLOCK
;
161 if not SOSC
.Thread_Blocking_IO
162 and then R
/= Failure
164 -- A socket inherits the properties of its server especially
165 -- the FIONBIO flag. Do not use Socket_Ioctl as this subprogram
166 -- tracks sockets set in non-blocking mode by user.
168 Set_Non_Blocking_Socket
(R
, Non_Blocking_Socket
(S
));
169 Res
:= C_Ioctl
(R
, SOSC
.FIONBIO
, Val
'Access);
170 -- Is it OK to ignore result ???
182 Name
: System
.Address
;
183 Namelen
: C
.int
) return C
.int
188 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
190 if SOSC
.Thread_Blocking_IO
191 or else Res
/= Failure
192 or else Non_Blocking_Socket
(S
)
193 or else Errno
/= SOSC
.EINPROGRESS
199 WSet
: aliased Fd_Set
;
200 Now
: aliased Timeval
;
202 Reset_Socket_Set
(WSet
'Access);
204 Insert_Socket_In_Set
(WSet
'Access, S
);
211 Now
'Unchecked_Access);
215 if Res
= Failure
then
223 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
226 and then Errno
= SOSC
.EISCONN
228 return Thin_Common
.Success
;
238 function Socket_Ioctl
241 Arg
: access C
.int
) return C
.int
244 if not SOSC
.Thread_Blocking_IO
and then Req
= SOSC
.FIONBIO
then
246 Set_Non_Blocking_Socket
(S
, True);
250 return C_Ioctl
(S
, Req
, Arg
);
259 Msg
: System
.Address
;
261 Flags
: C
.int
) return C
.int
267 Res
:= Syscall_Recv
(S
, Msg
, Len
, Flags
);
268 exit when SOSC
.Thread_Blocking_IO
269 or else Res
/= Failure
270 or else Non_Blocking_Socket
(S
)
271 or else Errno
/= SOSC
.EWOULDBLOCK
;
284 Msg
: System
.Address
;
287 From
: System
.Address
;
288 Fromlen
: not null access C
.int
) return C
.int
294 Res
:= Syscall_Recvfrom
(S
, Msg
, Len
, Flags
, From
, Fromlen
);
295 exit when SOSC
.Thread_Blocking_IO
296 or else Res
/= Failure
297 or else Non_Blocking_Socket
(S
)
298 or else Errno
/= SOSC
.EWOULDBLOCK
;
311 Msg
: System
.Address
;
312 Flags
: C
.int
) return ssize_t
318 Res
:= Syscall_Recvmsg
(S
, Msg
, Flags
);
319 exit when SOSC
.Thread_Blocking_IO
320 or else Res
/= Failure
321 or else Non_Blocking_Socket
(S
)
322 or else Errno
/= SOSC
.EWOULDBLOCK
;
326 return ssize_t
(Res
);
335 Msg
: System
.Address
;
336 Flags
: C
.int
) return ssize_t
342 Res
:= Syscall_Sendmsg
(S
, Msg
, Flags
);
343 exit when SOSC
.Thread_Blocking_IO
344 or else Res
/= Failure
345 or else Non_Blocking_Socket
(S
)
346 or else Errno
/= SOSC
.EWOULDBLOCK
;
350 return ssize_t
(Res
);
359 Msg
: System
.Address
;
363 Tolen
: C
.int
) return C
.int
371 if To
= Null_Address
then
373 -- In violation of the standard sockets API, VxWorks does not
374 -- support sendto(2) calls on connected sockets with a null
375 -- destination address, so use send(2) instead in that case.
377 Res
:= Syscall_Send
(S
, Msg
, Len
, Flags
);
379 -- Normal case where destination address is non-null
382 Res
:= Syscall_Sendto
(S
, Msg
, Len
, Flags
, To
, Tolen
);
385 exit when SOSC
.Thread_Blocking_IO
386 or else Res
/= Failure
387 or else Non_Blocking_Socket
(S
)
388 or else Errno
/= SOSC
.EWOULDBLOCK
;
402 Protocol
: C
.int
) return C
.int
405 Val
: aliased C
.int
:= 1;
408 pragma Unreferenced
(Res
);
411 R
:= Syscall_Socket
(Domain
, Typ
, Protocol
);
413 if not SOSC
.Thread_Blocking_IO
414 and then R
/= Failure
416 -- Do not use Socket_Ioctl as this subprogram tracks sockets set
417 -- in non-blocking mode by user.
419 Res
:= C_Ioctl
(R
, SOSC
.FIONBIO
, Val
'Access);
420 -- Is it OK to ignore result ???
421 Set_Non_Blocking_Socket
(R
, False);
431 procedure Finalize
is
436 -------------------------
437 -- Host_Error_Messages --
438 -------------------------
440 package body Host_Error_Messages
is separate;
446 procedure Initialize
is
448 Reset_Socket_Set
(Non_Blocking_Sockets
'Access);
451 -------------------------
452 -- Non_Blocking_Socket --
453 -------------------------
455 function Non_Blocking_Socket
(S
: C
.int
) return Boolean is
459 R
:= (Is_Socket_In_Set
(Non_Blocking_Sockets
'Access, S
) /= 0);
462 end Non_Blocking_Socket
;
464 -----------------------------
465 -- Set_Non_Blocking_Socket --
466 -----------------------------
468 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean) is
472 Insert_Socket_In_Set
(Non_Blocking_Sockets
'Access, S
);
474 Remove_Socket_From_Set
(Non_Blocking_Sockets
'Access, S
);
478 end Set_Non_Blocking_Socket
;
484 package body Signalling_Fds
is separate;
486 --------------------------
487 -- Socket_Error_Message --
488 --------------------------
490 function Socket_Error_Message
491 (Errno
: Integer) return C
.Strings
.chars_ptr
494 end GNAT
.Sockets
.Thin
;