1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
10 -- Copyright (C) 2001 Ada Core Technologies, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 ------------------------------------------------------------------------------
34 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
36 with Interfaces
.C
; use Interfaces
.C
;
38 package body GNAT
.Sockets
.Thin
is
40 -- When this package is initialized with Process_Blocking_IO set
41 -- to True, sockets are set in non-blocking mode to avoid blocking
42 -- the whole process when a thread wants to perform a blocking IO
43 -- operation. But the user can set a socket in non-blocking mode
44 -- by purpose. We track the socket in such a mode by redefining
45 -- C_Ioctl. In blocking IO operations, we exit normally when the
46 -- non-blocking flag is set by user, we poll and try later when
47 -- this flag is set automatically by this package.
49 type Socket_Info
is record
50 Non_Blocking
: Boolean := False;
53 Table
: array (C
.int
range 0 .. 31) of Socket_Info
;
54 -- Get info on blocking flag. This array is limited to 32 sockets
55 -- because the select operation allows socket set of less then 32
58 Quantum
: constant Duration := 0.2;
61 Thread_Blocking_IO
: Boolean := True;
63 function Syscall_Accept
65 Addr
: System
.Address
;
66 Addrlen
: access C
.int
)
68 pragma Import
(C
, Syscall_Accept
, "accept");
70 function Syscall_Connect
72 Name
: System
.Address
;
75 pragma Import
(C
, Syscall_Connect
, "connect");
77 function Syscall_Ioctl
82 pragma Import
(C
, Syscall_Ioctl
, "ioctl");
90 pragma Import
(C
, Syscall_Recv
, "recv");
92 function Syscall_Recvfrom
97 From
: Sockaddr_In_Access
;
98 Fromlen
: access C
.int
)
100 pragma Import
(C
, Syscall_Recvfrom
, "recvfrom");
102 function Syscall_Send
104 Msg
: System
.Address
;
108 pragma Import
(C
, Syscall_Send
, "send");
110 function Syscall_Sendto
112 Msg
: System
.Address
;
115 To
: Sockaddr_In_Access
;
118 pragma Import
(C
, Syscall_Sendto
, "sendto");
120 function Syscall_Socket
121 (Domain
, Typ
, Protocol
: C
.int
)
123 pragma Import
(C
, Syscall_Socket
, "socket");
125 procedure Set_Non_Blocking
(S
: C
.int
);
133 Addr
: System
.Address
;
134 Addrlen
: access C
.int
)
141 Res
:= Syscall_Accept
(S
, Addr
, Addrlen
);
142 exit when Thread_Blocking_IO
143 or else Res
/= Failure
144 or else Table
(S
).Non_Blocking
145 or else Errno
/= Constants
.EWOULDBLOCK
;
149 if not Thread_Blocking_IO
150 and then Res
/= Failure
152 -- A socket inherits the properties ot its server especially
155 Table
(Res
).Non_Blocking
:= Table
(S
).Non_Blocking
;
156 Set_Non_Blocking
(Res
);
168 Name
: System
.Address
;
175 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
177 if Thread_Blocking_IO
178 or else Res
/= Failure
179 or else Table
(S
).Non_Blocking
180 or else Errno
/= Constants
.EINPROGRESS
186 Set
: aliased Fd_Set
;
187 Now
: aliased Timeval
;
191 Set
:= 2 ** Natural (S
);
195 null, Set
'Unchecked_Access,
196 null, Now
'Unchecked_Access);
200 if Res
= Failure
then
208 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
211 and then Errno
= Constants
.EISCONN
230 if not Thread_Blocking_IO
231 and then Req
= Constants
.FIONBIO
233 Table
(S
).Non_Blocking
:= (Arg
.all /= 0);
236 return Syscall_Ioctl
(S
, Req
, Arg
);
245 Msg
: System
.Address
;
254 Res
:= Syscall_Recv
(S
, Msg
, Len
, Flags
);
255 exit when Thread_Blocking_IO
256 or else Res
/= Failure
257 or else Table
(S
).Non_Blocking
258 or else Errno
/= Constants
.EWOULDBLOCK
;
271 Msg
: System
.Address
;
274 From
: Sockaddr_In_Access
;
275 Fromlen
: access C
.int
)
282 Res
:= Syscall_Recvfrom
(S
, Msg
, Len
, Flags
, From
, Fromlen
);
283 exit when Thread_Blocking_IO
284 or else Res
/= Failure
285 or else Table
(S
).Non_Blocking
286 or else Errno
/= Constants
.EWOULDBLOCK
;
299 Msg
: System
.Address
;
308 Res
:= Syscall_Send
(S
, Msg
, Len
, Flags
);
309 exit when Thread_Blocking_IO
310 or else Res
/= Failure
311 or else Table
(S
).Non_Blocking
312 or else Errno
/= Constants
.EWOULDBLOCK
;
325 Msg
: System
.Address
;
328 To
: Sockaddr_In_Access
;
336 Res
:= Syscall_Sendto
(S
, Msg
, Len
, Flags
, To
, Tolen
);
337 exit when Thread_Blocking_IO
338 or else Res
/= Failure
339 or else Table
(S
).Non_Blocking
340 or else Errno
/= Constants
.EWOULDBLOCK
;
360 Res
:= Syscall_Socket
(Domain
, Typ
, Protocol
);
362 if not Thread_Blocking_IO
363 and then Res
/= Failure
365 Set_Non_Blocking
(Res
);
376 (Item
: in out Fd_Set
;
379 Mask
: constant Fd_Set
:= 2 ** Natural (Socket
);
382 if (Item
and Mask
) /= 0 then
383 Item
:= Item
xor Mask
;
391 procedure Empty
(Item
: in out Fd_Set
) is
400 procedure Finalize
is
409 procedure Initialize
(Process_Blocking_IO
: Boolean) is
411 Thread_Blocking_IO
:= not Process_Blocking_IO
;
418 function Is_Empty
(Item
: Fd_Set
) return Boolean is
427 function Is_Set
(Item
: Fd_Set
; Socket
: C
.int
) return Boolean is
429 return (Item
and 2 ** Natural (Socket
)) /= 0;
436 function Max
(Item
: Fd_Set
) return C
.int
453 procedure Set
(Item
: in out Fd_Set
; Socket
: in C
.int
) is
455 Item
:= Item
or 2 ** Natural (Socket
);
458 ----------------------
459 -- Set_Non_Blocking --
460 ----------------------
462 procedure Set_Non_Blocking
(S
: C
.int
) is
464 Val
: aliased C
.int
:= 1;
468 -- Do not use C_Fcntl because this subprogram tracks the
469 -- sockets set by user in non-blocking mode.
471 Res
:= Syscall_Ioctl
(S
, Constants
.FIONBIO
, Val
'Unchecked_Access);
472 end Set_Non_Blocking
;
474 --------------------------
475 -- Socket_Error_Message --
476 --------------------------
478 function Socket_Error_Message
(Errno
: Integer) return String is
479 use type Interfaces
.C
.Strings
.chars_ptr
;
481 C_Msg
: C
.Strings
.chars_ptr
;
484 C_Msg
:= C_Strerror
(C
.int
(Errno
));
486 if C_Msg
= C
.Strings
.Null_Ptr
then
487 return "Unknown system error";
490 return C
.Strings
.Value
(C_Msg
);
492 end Socket_Error_Message
;
494 end GNAT
.Sockets
.Thin
;