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-2012, 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 Unknown_System_Error
: constant C
.Strings
.chars_ptr
:=
61 C
.Strings
.New_String
("Unknown system error");
63 -- Comments required for following functions ???
65 function Syscall_Accept
67 Addr
: System
.Address
;
68 Addrlen
: not null access C
.int
) return C
.int
;
69 pragma Import
(C
, Syscall_Accept
, "accept");
71 function Syscall_Connect
73 Name
: System
.Address
;
74 Namelen
: C
.int
) return C
.int
;
75 pragma Import
(C
, Syscall_Connect
, "connect");
81 Flags
: C
.int
) return C
.int
;
82 pragma Import
(C
, Syscall_Recv
, "recv");
84 function Syscall_Recvfrom
89 From
: System
.Address
;
90 Fromlen
: not null access C
.int
) return C
.int
;
91 pragma Import
(C
, Syscall_Recvfrom
, "recvfrom");
93 function Syscall_Recvmsg
96 Flags
: C
.int
) return System
.CRTL
.ssize_t
;
97 pragma Import
(C
, Syscall_Recvmsg
, "recvmsg");
99 function Syscall_Sendmsg
101 Msg
: System
.Address
;
102 Flags
: C
.int
) return System
.CRTL
.ssize_t
;
103 pragma Import
(C
, Syscall_Sendmsg
, "sendmsg");
105 function Syscall_Sendto
107 Msg
: System
.Address
;
111 Tolen
: C
.int
) return C
.int
;
112 pragma Import
(C
, Syscall_Sendto
, "sendto");
114 function Syscall_Socket
117 Protocol
: C
.int
) return C
.int
;
118 pragma Import
(C
, Syscall_Socket
, "socket");
120 procedure Disable_SIGPIPE
(S
: C
.int
);
121 pragma Import
(C
, Disable_SIGPIPE
, "__gnat_disable_sigpipe");
123 procedure Disable_All_SIGPIPEs
;
124 pragma Import
(C
, Disable_All_SIGPIPEs
, "__gnat_disable_all_sigpipes");
125 -- Sets the process to ignore all SIGPIPE signals on platforms that
126 -- don't support Disable_SIGPIPE for particular streams.
128 function Non_Blocking_Socket
(S
: C
.int
) return Boolean;
129 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean);
137 Addr
: System
.Address
;
138 Addrlen
: not null access C
.int
) return C
.int
141 Val
: aliased C
.int
:= 1;
144 pragma Warnings
(Off
, Discard
);
148 R
:= Syscall_Accept
(S
, Addr
, Addrlen
);
149 exit when SOSC
.Thread_Blocking_IO
151 or else Non_Blocking_Socket
(S
)
152 or else Errno
/= SOSC
.EWOULDBLOCK
;
156 if not SOSC
.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 Socket_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
:= C_Ioctl
(R
, SOSC
.FIONBIO
, Val
'Access);
177 Name
: System
.Address
;
178 Namelen
: C
.int
) return C
.int
183 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
185 if SOSC
.Thread_Blocking_IO
186 or else Res
/= Failure
187 or else Non_Blocking_Socket
(S
)
188 or else Errno
/= SOSC
.EINPROGRESS
194 WSet
: aliased Fd_Set
;
195 Now
: aliased Timeval
;
198 Reset_Socket_Set
(WSet
'Access);
200 Insert_Socket_In_Set
(WSet
'Access, S
);
207 Now
'Unchecked_Access);
211 if Res
= Failure
then
219 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
222 and then Errno
= SOSC
.EISCONN
224 return Thin_Common
.Success
;
234 function Socket_Ioctl
236 Req
: SOSC
.IOCTL_Req_T
;
237 Arg
: access C
.int
) return C
.int
240 if not SOSC
.Thread_Blocking_IO
and then Req
= SOSC
.FIONBIO
then
242 Set_Non_Blocking_Socket
(S
, True);
246 return C_Ioctl
(S
, Req
, Arg
);
255 Msg
: System
.Address
;
257 Flags
: C
.int
) return C
.int
263 Res
:= Syscall_Recv
(S
, Msg
, Len
, Flags
);
264 exit when SOSC
.Thread_Blocking_IO
265 or else Res
/= Failure
266 or else Non_Blocking_Socket
(S
)
267 or else Errno
/= SOSC
.EWOULDBLOCK
;
280 Msg
: System
.Address
;
283 From
: System
.Address
;
284 Fromlen
: not null access C
.int
) return C
.int
290 Res
:= Syscall_Recvfrom
(S
, Msg
, Len
, Flags
, From
, Fromlen
);
291 exit when SOSC
.Thread_Blocking_IO
292 or else Res
/= Failure
293 or else Non_Blocking_Socket
(S
)
294 or else Errno
/= SOSC
.EWOULDBLOCK
;
307 Msg
: System
.Address
;
308 Flags
: C
.int
) return System
.CRTL
.ssize_t
310 Res
: System
.CRTL
.ssize_t
;
314 Res
:= Syscall_Recvmsg
(S
, Msg
, Flags
);
315 exit when SOSC
.Thread_Blocking_IO
316 or else Res
/= System
.CRTL
.ssize_t
(Failure
)
317 or else Non_Blocking_Socket
(S
)
318 or else Errno
/= SOSC
.EWOULDBLOCK
;
331 Msg
: System
.Address
;
332 Flags
: C
.int
) return System
.CRTL
.ssize_t
334 Res
: System
.CRTL
.ssize_t
;
338 Res
:= Syscall_Sendmsg
(S
, Msg
, Flags
);
339 exit when SOSC
.Thread_Blocking_IO
340 or else Res
/= System
.CRTL
.ssize_t
(Failure
)
341 or else Non_Blocking_Socket
(S
)
342 or else Errno
/= SOSC
.EWOULDBLOCK
;
355 Msg
: System
.Address
;
359 Tolen
: C
.int
) return C
.int
365 Res
:= Syscall_Sendto
(S
, Msg
, Len
, Flags
, To
, Tolen
);
366 exit when SOSC
.Thread_Blocking_IO
367 or else Res
/= Failure
368 or else Non_Blocking_Socket
(S
)
369 or else Errno
/= SOSC
.EWOULDBLOCK
;
383 Protocol
: C
.int
) return C
.int
386 Val
: aliased C
.int
:= 1;
389 pragma Unreferenced
(Discard
);
392 R
:= Syscall_Socket
(Domain
, Typ
, Protocol
);
394 if not SOSC
.Thread_Blocking_IO
395 and then R
/= Failure
397 -- Do not use Socket_Ioctl as this subprogram tracks sockets set
398 -- in non-blocking mode by user.
400 Discard
:= C_Ioctl
(R
, SOSC
.FIONBIO
, Val
'Access);
401 Set_Non_Blocking_Socket
(R
, False);
411 procedure Finalize
is
416 -------------------------
417 -- Host_Error_Messages --
418 -------------------------
420 package body Host_Error_Messages
is separate;
426 procedure Initialize
is
428 Disable_All_SIGPIPEs
;
429 Reset_Socket_Set
(Non_Blocking_Sockets
'Access);
432 -------------------------
433 -- Non_Blocking_Socket --
434 -------------------------
436 function Non_Blocking_Socket
(S
: C
.int
) return Boolean is
440 R
:= (Is_Socket_In_Set
(Non_Blocking_Sockets
'Access, S
) /= 0);
443 end Non_Blocking_Socket
;
445 -----------------------------
446 -- Set_Non_Blocking_Socket --
447 -----------------------------
449 procedure Set_Non_Blocking_Socket
(S
: C
.int
; V
: Boolean) is
454 Insert_Socket_In_Set
(Non_Blocking_Sockets
'Access, S
);
456 Remove_Socket_From_Set
(Non_Blocking_Sockets
'Access, S
);
460 end Set_Non_Blocking_Socket
;
466 package body Signalling_Fds
is
468 -- In this default implementation, we use a C version of these
469 -- subprograms provided by socket.c.
471 function C_Create
(Fds
: not null access Fd_Pair
) return C
.int
;
472 function C_Read
(Rsig
: C
.int
) return C
.int
;
473 function C_Write
(Wsig
: C
.int
) return C
.int
;
474 procedure C_Close
(Sig
: C
.int
);
476 pragma Import
(C
, C_Create
, "__gnat_create_signalling_fds");
477 pragma Import
(C
, C_Read
, "__gnat_read_signalling_fd");
478 pragma Import
(C
, C_Write
, "__gnat_write_signalling_fd");
479 pragma Import
(C
, C_Close
, "__gnat_close_signalling_fd");
482 (Fds
: not null access Fd_Pair
) return C
.int
renames C_Create
;
483 function Read
(Rsig
: C
.int
) return C
.int
renames C_Read
;
484 function Write
(Wsig
: C
.int
) return C
.int
renames C_Write
;
485 procedure Close
(Sig
: C
.int
) renames C_Close
;
489 --------------------------
490 -- Socket_Error_Message --
491 --------------------------
493 function Socket_Error_Message
494 (Errno
: Integer) return C
.Strings
.chars_ptr
497 end GNAT
.Sockets
.Thin
;