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-2014, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package provides a target dependent thin interface to the sockets
33 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
34 -- should not be directly with'ed by an applications program.
36 -- This is the default version
38 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
41 with Interfaces
.C
; use Interfaces
.C
;
43 package body GNAT
.Sockets
.Thin
is
45 Non_Blocking_Sockets
: aliased Fd_Set
;
46 -- When this package is initialized with Process_Blocking_IO set
47 -- to True, sockets are set in non-blocking mode to avoid blocking
48 -- the whole process when a thread wants to perform a blocking IO
49 -- operation. But the user can also set a socket in non-blocking
50 -- mode by purpose. In order to make a difference between these
51 -- two situations, we track the origin of non-blocking mode in
52 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
53 -- been set in non-blocking mode by the user.
55 Quantum
: constant Duration := 0.2;
56 -- When SOSC.Thread_Blocking_IO is False, we set sockets in
57 -- non-blocking mode and we spend a period of time Quantum between
58 -- two attempts on a blocking operation.
60 -- Comments required for following functions ???
62 function Syscall_Accept
64 Addr
: System
.Address
;
65 Addrlen
: not null access C
.int
) return C
.int
;
66 pragma Import
(C
, Syscall_Accept
, "accept");
68 function Syscall_Connect
70 Name
: System
.Address
;
71 Namelen
: C
.int
) return C
.int
;
72 pragma Import
(C
, Syscall_Connect
, "connect");
78 Flags
: C
.int
) return C
.int
;
79 pragma Import
(C
, Syscall_Recv
, "recv");
81 function Syscall_Recvfrom
86 From
: System
.Address
;
87 Fromlen
: not null access C
.int
) return C
.int
;
88 pragma Import
(C
, Syscall_Recvfrom
, "recvfrom");
90 function Syscall_Recvmsg
93 Flags
: C
.int
) return System
.CRTL
.ssize_t
;
94 pragma Import
(C
, Syscall_Recvmsg
, "recvmsg");
96 function Syscall_Sendmsg
99 Flags
: C
.int
) return System
.CRTL
.ssize_t
;
100 pragma Import
(C
, Syscall_Sendmsg
, "sendmsg");
102 function Syscall_Sendto
104 Msg
: System
.Address
;
108 Tolen
: C
.int
) return C
.int
;
109 pragma Import
(C
, Syscall_Sendto
, "sendto");
111 function Syscall_Socket
114 Protocol
: C
.int
) return C
.int
;
115 pragma Import
(C
, Syscall_Socket
, "socket");
117 procedure Disable_SIGPIPE
(S
: C
.int
);
118 pragma Import
(C
, Disable_SIGPIPE
, "__gnat_disable_sigpipe");
120 procedure Disable_All_SIGPIPEs
;
121 pragma Import
(C
, Disable_All_SIGPIPEs
, "__gnat_disable_all_sigpipes");
122 -- Sets the process to ignore all SIGPIPE signals on platforms that
123 -- don't support Disable_SIGPIPE for particular streams.
125 function Non_Blocking_Socket
(S
: C
.int
) return Boolean;
126 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean);
134 Addr
: System
.Address
;
135 Addrlen
: not null access C
.int
) return C
.int
138 Val
: aliased C
.int
:= 1;
141 pragma Warnings
(Off
, Discard
);
145 R
:= Syscall_Accept
(S
, Addr
, Addrlen
);
146 exit when SOSC
.Thread_Blocking_IO
148 or else Non_Blocking_Socket
(S
)
149 or else Errno
/= SOSC
.EWOULDBLOCK
;
153 if not SOSC
.Thread_Blocking_IO
154 and then R
/= Failure
156 -- A socket inherits the properties ot its server especially
157 -- the FIONBIO flag. Do not use Socket_Ioctl as this subprogram
158 -- tracks sockets set in non-blocking mode by user.
160 Set_Non_Blocking_Socket
(R
, Non_Blocking_Socket
(S
));
161 Discard
:= C_Ioctl
(R
, SOSC
.FIONBIO
, Val
'Access);
174 Name
: System
.Address
;
175 Namelen
: C
.int
) return C
.int
180 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
182 if SOSC
.Thread_Blocking_IO
183 or else Res
/= Failure
184 or else Non_Blocking_Socket
(S
)
185 or else Errno
/= SOSC
.EINPROGRESS
191 WSet
: aliased Fd_Set
;
192 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
233 Req
: SOSC
.IOCTL_Req_T
;
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 System
.CRTL
.ssize_t
307 Res
: System
.CRTL
.ssize_t
;
311 Res
:= Syscall_Recvmsg
(S
, Msg
, Flags
);
312 exit when SOSC
.Thread_Blocking_IO
313 or else Res
/= System
.CRTL
.ssize_t
(Failure
)
314 or else Non_Blocking_Socket
(S
)
315 or else Errno
/= SOSC
.EWOULDBLOCK
;
328 Msg
: System
.Address
;
329 Flags
: C
.int
) return System
.CRTL
.ssize_t
331 Res
: System
.CRTL
.ssize_t
;
335 Res
:= Syscall_Sendmsg
(S
, Msg
, Flags
);
336 exit when SOSC
.Thread_Blocking_IO
337 or else Res
/= System
.CRTL
.ssize_t
(Failure
)
338 or else Non_Blocking_Socket
(S
)
339 or else Errno
/= SOSC
.EWOULDBLOCK
;
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;
388 R
:= Syscall_Socket
(Domain
, Typ
, Protocol
);
390 if not SOSC
.Thread_Blocking_IO
391 and then R
/= Failure
393 -- Do not use Socket_Ioctl as this subprogram tracks sockets set
394 -- in non-blocking mode by user.
396 Discard
:= C_Ioctl
(R
, SOSC
.FIONBIO
, Val
'Access);
397 Set_Non_Blocking_Socket
(R
, False);
407 procedure Finalize
is
412 -------------------------
413 -- Host_Error_Messages --
414 -------------------------
416 package body Host_Error_Messages
is separate;
422 procedure Initialize
is
424 Disable_All_SIGPIPEs
;
425 Reset_Socket_Set
(Non_Blocking_Sockets
'Access);
428 -------------------------
429 -- Non_Blocking_Socket --
430 -------------------------
432 function Non_Blocking_Socket
(S
: C
.int
) return Boolean is
436 R
:= (Is_Socket_In_Set
(Non_Blocking_Sockets
'Access, S
) /= 0);
439 end Non_Blocking_Socket
;
441 -----------------------------
442 -- Set_Non_Blocking_Socket --
443 -----------------------------
445 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
464 -- In this default implementation, we use a C version of these
465 -- subprograms provided by socket.c.
467 function C_Create
(Fds
: not null access Fd_Pair
) return C
.int
;
468 function C_Read
(Rsig
: C
.int
) return C
.int
;
469 function C_Write
(Wsig
: C
.int
) return C
.int
;
470 procedure C_Close
(Sig
: C
.int
);
472 pragma Import
(C
, C_Create
, "__gnat_create_signalling_fds");
473 pragma Import
(C
, C_Read
, "__gnat_read_signalling_fd");
474 pragma Import
(C
, C_Write
, "__gnat_write_signalling_fd");
475 pragma Import
(C
, C_Close
, "__gnat_close_signalling_fd");
478 (Fds
: not null access Fd_Pair
) return C
.int
renames C_Create
;
479 function Read
(Rsig
: C
.int
) return C
.int
renames C_Read
;
480 function Write
(Wsig
: C
.int
) return C
.int
renames C_Write
;
481 procedure Close
(Sig
: C
.int
) renames C_Close
;
485 --------------------------
486 -- Socket_Error_Message --
487 --------------------------
489 function Socket_Error_Message
(Errno
: Integer) return String is separate;
491 end GNAT
.Sockets
.Thin
;