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 Ada Core Technologies, Inc. --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
35 with Interfaces
.C
; use Interfaces
.C
;
37 package body GNAT
.Sockets
.Thin
is
39 -- When this package is initialized with Process_Blocking_IO set
40 -- to True, sockets are set in non-blocking mode to avoid blocking
41 -- the whole process when a thread wants to perform a blocking IO
42 -- operation. But the user can set a socket in non-blocking mode
43 -- by purpose. We track the socket in such a mode by redefining
44 -- C_Ioctl. In blocking IO operations, we exit normally when the
45 -- non-blocking flag is set by user, we poll and try later when
46 -- this flag is set automatically by this package.
48 type Socket_Info
is record
49 Non_Blocking
: Boolean := False;
52 Table
: array (C
.int
range 0 .. 31) of Socket_Info
;
53 -- Get info on blocking flag. This array is limited to 32 sockets
54 -- because the select operation allows socket set of less then 32
57 Quantum
: constant Duration := 0.2;
60 Thread_Blocking_IO
: Boolean := True;
62 function Syscall_Accept
64 Addr
: System
.Address
;
65 Addrlen
: access C
.int
)
67 pragma Import
(C
, Syscall_Accept
, "accept");
69 function Syscall_Connect
71 Name
: System
.Address
;
74 pragma Import
(C
, Syscall_Connect
, "connect");
76 function Syscall_Ioctl
81 pragma Import
(C
, Syscall_Ioctl
, "ioctl");
89 pragma Import
(C
, Syscall_Recv
, "recv");
91 function Syscall_Recvfrom
96 From
: Sockaddr_In_Access
;
97 Fromlen
: access C
.int
)
99 pragma Import
(C
, Syscall_Recvfrom
, "recvfrom");
101 function Syscall_Send
103 Msg
: System
.Address
;
107 pragma Import
(C
, Syscall_Send
, "send");
109 function Syscall_Sendto
111 Msg
: System
.Address
;
114 To
: Sockaddr_In_Access
;
117 pragma Import
(C
, Syscall_Sendto
, "sendto");
119 function Syscall_Socket
120 (Domain
, Typ
, Protocol
: C
.int
)
122 pragma Import
(C
, Syscall_Socket
, "socket");
124 procedure Set_Non_Blocking
(S
: C
.int
);
132 Addr
: System
.Address
;
133 Addrlen
: access C
.int
)
140 Res
:= Syscall_Accept
(S
, Addr
, Addrlen
);
141 exit when Thread_Blocking_IO
142 or else Res
/= Failure
143 or else Table
(S
).Non_Blocking
144 or else Errno
/= Constants
.EWOULDBLOCK
;
148 if not Thread_Blocking_IO
149 and then Res
/= Failure
151 -- A socket inherits the properties ot its server especially
154 Table
(Res
).Non_Blocking
:= Table
(S
).Non_Blocking
;
155 Set_Non_Blocking
(Res
);
167 Name
: System
.Address
;
174 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
176 if Thread_Blocking_IO
177 or else Res
/= Failure
178 or else Table
(S
).Non_Blocking
179 or else Errno
/= Constants
.EINPROGRESS
185 Set
: aliased Fd_Set
;
186 Now
: aliased Timeval
;
190 Set
:= 2 ** Natural (S
);
194 null, Set
'Unchecked_Access,
195 null, Now
'Unchecked_Access);
199 if Res
= Failure
then
207 Res
:= Syscall_Connect
(S
, Name
, Namelen
);
210 and then Errno
= Constants
.EISCONN
229 if not Thread_Blocking_IO
230 and then Req
= Constants
.FIONBIO
232 Table
(S
).Non_Blocking
:= (Arg
.all /= 0);
235 return Syscall_Ioctl
(S
, Req
, Arg
);
244 Msg
: System
.Address
;
253 Res
:= Syscall_Recv
(S
, Msg
, Len
, Flags
);
254 exit when Thread_Blocking_IO
255 or else Res
/= Failure
256 or else Table
(S
).Non_Blocking
257 or else Errno
/= Constants
.EWOULDBLOCK
;
270 Msg
: System
.Address
;
273 From
: Sockaddr_In_Access
;
274 Fromlen
: access C
.int
)
281 Res
:= Syscall_Recvfrom
(S
, Msg
, Len
, Flags
, From
, Fromlen
);
282 exit when Thread_Blocking_IO
283 or else Res
/= Failure
284 or else Table
(S
).Non_Blocking
285 or else Errno
/= Constants
.EWOULDBLOCK
;
298 Msg
: System
.Address
;
307 Res
:= Syscall_Send
(S
, Msg
, Len
, Flags
);
308 exit when Thread_Blocking_IO
309 or else Res
/= Failure
310 or else Table
(S
).Non_Blocking
311 or else Errno
/= Constants
.EWOULDBLOCK
;
324 Msg
: System
.Address
;
327 To
: Sockaddr_In_Access
;
335 Res
:= Syscall_Sendto
(S
, Msg
, Len
, Flags
, To
, Tolen
);
336 exit when Thread_Blocking_IO
337 or else Res
/= Failure
338 or else Table
(S
).Non_Blocking
339 or else Errno
/= Constants
.EWOULDBLOCK
;
359 Res
:= Syscall_Socket
(Domain
, Typ
, Protocol
);
361 if not Thread_Blocking_IO
362 and then Res
/= Failure
364 Set_Non_Blocking
(Res
);
375 (Item
: in out Fd_Set
;
378 Mask
: constant Fd_Set
:= 2 ** Natural (Socket
);
381 if (Item
and Mask
) /= 0 then
382 Item
:= Item
xor Mask
;
390 procedure Empty
(Item
: in out Fd_Set
) is
399 procedure Finalize
is
408 procedure Initialize
(Process_Blocking_IO
: Boolean) is
410 Thread_Blocking_IO
:= not Process_Blocking_IO
;
417 function Is_Empty
(Item
: Fd_Set
) return Boolean is
426 function Is_Set
(Item
: Fd_Set
; Socket
: C
.int
) return Boolean is
428 return (Item
and 2 ** Natural (Socket
)) /= 0;
435 function Max
(Item
: Fd_Set
) return C
.int
452 procedure Set
(Item
: in out Fd_Set
; Socket
: in C
.int
) is
454 Item
:= Item
or 2 ** Natural (Socket
);
457 ----------------------
458 -- Set_Non_Blocking --
459 ----------------------
461 procedure Set_Non_Blocking
(S
: C
.int
) is
463 Val
: aliased C
.int
:= 1;
467 -- Do not use C_Fcntl because this subprogram tracks the
468 -- sockets set by user in non-blocking mode.
470 Res
:= Syscall_Ioctl
(S
, Constants
.FIONBIO
, Val
'Unchecked_Access);
471 end Set_Non_Blocking
;
473 --------------------------
474 -- Socket_Error_Message --
475 --------------------------
477 function Socket_Error_Message
(Errno
: Integer) return String is
478 use type Interfaces
.C
.Strings
.chars_ptr
;
480 C_Msg
: C
.Strings
.chars_ptr
;
483 C_Msg
:= C_Strerror
(C
.int
(Errno
));
485 if C_Msg
= C
.Strings
.Null_Ptr
then
486 return "Unknown system error";
489 return C
.Strings
.Value
(C_Msg
);
491 end Socket_Error_Message
;
493 end GNAT
.Sockets
.Thin
;