1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
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 with Ada
.Streams
; use Ada
.Streams
;
33 with Ada
.Exceptions
; use Ada
.Exceptions
;
34 with Ada
.Finalization
;
35 with Ada
.Unchecked_Conversion
;
37 with GNAT
.Sockets
.Thin_Common
; use GNAT
.Sockets
.Thin_Common
;
38 with GNAT
.Sockets
.Thin
; use GNAT
.Sockets
.Thin
;
40 with GNAT
.Sockets
.Linker_Options
;
41 pragma Warnings
(Off
, GNAT
.Sockets
.Linker_Options
);
42 -- Need to include pragma Linker_Options which is platform dependent
44 with System
; use System
;
45 with System
.Communication
; use System
.Communication
;
46 with System
.CRTL
; use System
.CRTL
;
47 with System
.Task_Lock
;
49 package body GNAT
.Sockets
is
51 package C
renames Interfaces
.C
;
55 ENOERROR
: constant := 0;
57 Netdb_Buffer_Size
: constant := SOSC
.Need_Netdb_Buffer
* 1024;
58 Need_Netdb_Lock
: constant Boolean := SOSC
.Need_Netdb_Lock
/= 0;
59 -- The network database functions gethostbyname, gethostbyaddr,
60 -- getservbyname and getservbyport can either be guaranteed task safe by
61 -- the operating system, or else return data through a user-provided buffer
62 -- to ensure concurrent uses do not interfere.
64 -- Correspondence tables
66 Levels
: constant array (Level_Type
) of C
.int
:=
67 (Socket_Level
=> SOSC
.SOL_SOCKET
,
68 IP_Protocol_For_IP_Level
=> SOSC
.IPPROTO_IP
,
69 IP_Protocol_For_UDP_Level
=> SOSC
.IPPROTO_UDP
,
70 IP_Protocol_For_TCP_Level
=> SOSC
.IPPROTO_TCP
);
72 Modes
: constant array (Mode_Type
) of C
.int
:=
73 (Socket_Stream
=> SOSC
.SOCK_STREAM
,
74 Socket_Datagram
=> SOSC
.SOCK_DGRAM
);
76 Shutmodes
: constant array (Shutmode_Type
) of C
.int
:=
77 (Shut_Read
=> SOSC
.SHUT_RD
,
78 Shut_Write
=> SOSC
.SHUT_WR
,
79 Shut_Read_Write
=> SOSC
.SHUT_RDWR
);
81 Requests
: constant array (Request_Name
) of SOSC
.IOCTL_Req_T
:=
82 (Non_Blocking_IO
=> SOSC
.FIONBIO
,
83 N_Bytes_To_Read
=> SOSC
.FIONREAD
);
85 Options
: constant array (Option_Name
) of C
.int
:=
86 (Keep_Alive
=> SOSC
.SO_KEEPALIVE
,
87 Reuse_Address
=> SOSC
.SO_REUSEADDR
,
88 Broadcast
=> SOSC
.SO_BROADCAST
,
89 Send_Buffer
=> SOSC
.SO_SNDBUF
,
90 Receive_Buffer
=> SOSC
.SO_RCVBUF
,
91 Linger
=> SOSC
.SO_LINGER
,
92 Error
=> SOSC
.SO_ERROR
,
93 No_Delay
=> SOSC
.TCP_NODELAY
,
94 Add_Membership
=> SOSC
.IP_ADD_MEMBERSHIP
,
95 Drop_Membership
=> SOSC
.IP_DROP_MEMBERSHIP
,
96 Multicast_If
=> SOSC
.IP_MULTICAST_IF
,
97 Multicast_TTL
=> SOSC
.IP_MULTICAST_TTL
,
98 Multicast_Loop
=> SOSC
.IP_MULTICAST_LOOP
,
99 Receive_Packet_Info
=> SOSC
.IP_PKTINFO
,
100 Send_Timeout
=> SOSC
.SO_SNDTIMEO
,
101 Receive_Timeout
=> SOSC
.SO_RCVTIMEO
);
102 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
103 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
105 Flags
: constant array (0 .. 3) of C
.int
:=
106 (0 => SOSC
.MSG_OOB
, -- Process_Out_Of_Band_Data
107 1 => SOSC
.MSG_PEEK
, -- Peek_At_Incoming_Data
108 2 => SOSC
.MSG_WAITALL
, -- Wait_For_A_Full_Reception
109 3 => SOSC
.MSG_EOR
); -- Send_End_Of_Record
111 Socket_Error_Id
: constant Exception_Id
:= Socket_Error
'Identity;
112 Host_Error_Id
: constant Exception_Id
:= Host_Error
'Identity;
114 Hex_To_Char
: constant String (1 .. 16) := "0123456789ABCDEF";
115 -- Use to print in hexadecimal format
117 -----------------------
118 -- Local subprograms --
119 -----------------------
121 function Resolve_Error
122 (Error_Value
: Integer;
123 From_Errno
: Boolean := True) return Error_Type
;
124 -- Associate an enumeration value (error_type) to an error value (errno).
125 -- From_Errno prevents from mixing h_errno with errno.
127 function To_Name
(N
: String) return Name_Type
;
128 function To_String
(HN
: Name_Type
) return String;
129 -- Conversion functions
131 function To_Int
(F
: Request_Flag_Type
) return C
.int
;
132 -- Return the int value corresponding to the specified flags combination
134 function Set_Forced_Flags
(F
: C
.int
) return C
.int
;
135 -- Return F with the bits from SOSC.MSG_Forced_Flags forced set
137 function Short_To_Network
138 (S
: C
.unsigned_short
) return C
.unsigned_short
;
139 pragma Inline
(Short_To_Network
);
140 -- Convert a port number into a network port number
142 function Network_To_Short
143 (S
: C
.unsigned_short
) return C
.unsigned_short
144 renames Short_To_Network
;
145 -- Symmetric operation
148 (Val
: Inet_Addr_VN_Type
;
149 Hex
: Boolean := False) return String;
150 -- Output an array of inet address components in hex or decimal mode
152 function Is_IP_Address
(Name
: String) return Boolean;
153 -- Return true when Name is an IP address in standard dot notation
155 procedure Netdb_Lock
;
156 pragma Inline
(Netdb_Lock
);
157 procedure Netdb_Unlock
;
158 pragma Inline
(Netdb_Unlock
);
159 -- Lock/unlock operation used to protect netdb access for platforms that
160 -- require such protection.
162 function To_In_Addr
(Addr
: Inet_Addr_Type
) return In_Addr
;
163 procedure To_Inet_Addr
165 Result
: out Inet_Addr_Type
);
166 -- Conversion functions
168 function To_Host_Entry
(E
: Hostent_Access
) return Host_Entry_Type
;
169 -- Conversion function
171 function To_Service_Entry
(E
: Servent_Access
) return Service_Entry_Type
;
172 -- Conversion function
174 function Value
(S
: System
.Address
) return String;
175 -- Same as Interfaces.C.Strings.Value but taking a System.Address
177 function To_Timeval
(Val
: Timeval_Duration
) return Timeval
;
178 -- Separate Val in seconds and microseconds
180 function To_Duration
(Val
: Timeval
) return Timeval_Duration
;
181 -- Reconstruct a Duration value from a Timeval record (seconds and
184 procedure Raise_Socket_Error
(Error
: Integer);
185 -- Raise Socket_Error with an exception message describing the error code
188 procedure Raise_Host_Error
(H_Error
: Integer);
189 -- Raise Host_Error exception with message describing error code (note
190 -- hstrerror seems to be obsolete) from h_errno.
192 procedure Narrow
(Item
: in out Socket_Set_Type
);
193 -- Update Last as it may be greater than the real last socket
195 procedure Check_For_Fd_Set
(Fd
: Socket_Type
);
196 pragma Inline
(Check_For_Fd_Set
);
197 -- Raise Constraint_Error if Fd is less than 0 or greater than or equal to
198 -- FD_SETSIZE, on platforms where fd_set is a bitmap.
200 function Connect_Socket
201 (Socket
: Socket_Type
;
202 Server
: Sock_Addr_Type
) return C
.int
;
203 pragma Inline
(Connect_Socket
);
204 -- Underlying implementation for the Connect_Socket procedures
206 -- Types needed for Datagram_Socket_Stream_Type
208 type Datagram_Socket_Stream_Type
is new Root_Stream_Type
with record
209 Socket
: Socket_Type
;
211 From
: Sock_Addr_Type
;
214 type Datagram_Socket_Stream_Access
is
215 access all Datagram_Socket_Stream_Type
;
218 (Stream
: in out Datagram_Socket_Stream_Type
;
219 Item
: out Ada
.Streams
.Stream_Element_Array
;
220 Last
: out Ada
.Streams
.Stream_Element_Offset
);
223 (Stream
: in out Datagram_Socket_Stream_Type
;
224 Item
: Ada
.Streams
.Stream_Element_Array
);
226 -- Types needed for Stream_Socket_Stream_Type
228 type Stream_Socket_Stream_Type
is new Root_Stream_Type
with record
229 Socket
: Socket_Type
;
232 type Stream_Socket_Stream_Access
is
233 access all Stream_Socket_Stream_Type
;
236 (Stream
: in out Stream_Socket_Stream_Type
;
237 Item
: out Ada
.Streams
.Stream_Element_Array
;
238 Last
: out Ada
.Streams
.Stream_Element_Offset
);
241 (Stream
: in out Stream_Socket_Stream_Type
;
242 Item
: Ada
.Streams
.Stream_Element_Array
);
244 procedure Wait_On_Socket
245 (Socket
: Socket_Type
;
247 Timeout
: Selector_Duration
;
248 Selector
: access Selector_Type
:= null;
249 Status
: out Selector_Status
);
250 -- Common code for variants of socket operations supporting a timeout:
251 -- block in Check_Selector on Socket for at most the indicated timeout.
252 -- If For_Read is True, Socket is added to the read set for this call, else
253 -- it is added to the write set. If no selector is provided, a local one is
254 -- created for this call and destroyed prior to returning.
256 type Sockets_Library_Controller
is new Ada
.Finalization
.Limited_Controlled
258 -- This type is used to generate automatic calls to Initialize and Finalize
259 -- during the elaboration and finalization of this package. A single object
260 -- of this type must exist at library level.
262 function Err_Code_Image
(E
: Integer) return String;
263 -- Return the value of E surrounded with brackets
265 procedure Initialize
(X
: in out Sockets_Library_Controller
);
266 procedure Finalize
(X
: in out Sockets_Library_Controller
);
268 procedure Normalize_Empty_Socket_Set
(S
: in out Socket_Set_Type
);
269 -- If S is the empty set (detected by Last = No_Socket), make sure its
270 -- fd_set component is actually cleared. Note that the case where it is
271 -- not can occur for an uninitialized Socket_Set_Type object.
273 function Is_Open
(S
: Selector_Type
) return Boolean;
274 -- Return True for an "open" Selector_Type object, i.e. one for which
275 -- Create_Selector has been called and Close_Selector has not been called,
276 -- or the null selector.
282 function "+" (L
, R
: Request_Flag_Type
) return Request_Flag_Type
is
291 procedure Abort_Selector
(Selector
: Selector_Type
) is
295 if not Is_Open
(Selector
) then
296 raise Program_Error
with "closed selector";
298 elsif Selector
.Is_Null
then
299 raise Program_Error
with "null selector";
303 -- Send one byte to unblock select system call
305 Res
:= Signalling_Fds
.Write
(C
.int
(Selector
.W_Sig_Socket
));
307 if Res
= Failure
then
308 Raise_Socket_Error
(Socket_Errno
);
316 procedure Accept_Socket
317 (Server
: Socket_Type
;
318 Socket
: out Socket_Type
;
319 Address
: out Sock_Addr_Type
)
322 Sin
: aliased Sockaddr_In
;
323 Len
: aliased C
.int
:= Sin
'Size / 8;
326 Res
:= C_Accept
(C
.int
(Server
), Sin
'Address, Len
'Access);
328 if Res
= Failure
then
329 Raise_Socket_Error
(Socket_Errno
);
332 Socket
:= Socket_Type
(Res
);
334 To_Inet_Addr
(Sin
.Sin_Addr
, Address
.Addr
);
335 Address
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
342 procedure Accept_Socket
343 (Server
: Socket_Type
;
344 Socket
: out Socket_Type
;
345 Address
: out Sock_Addr_Type
;
346 Timeout
: Selector_Duration
;
347 Selector
: access Selector_Type
:= null;
348 Status
: out Selector_Status
)
351 if Selector
/= null and then not Is_Open
(Selector
.all) then
352 raise Program_Error
with "closed selector";
355 -- Wait for socket to become available for reading
361 Selector
=> Selector
,
364 -- Accept connection if available
366 if Status
= Completed
then
367 Accept_Socket
(Server
, Socket
, Address
);
378 (E
: Host_Entry_Type
;
379 N
: Positive := 1) return Inet_Addr_Type
382 return E
.Addresses
(N
);
385 ----------------------
386 -- Addresses_Length --
387 ----------------------
389 function Addresses_Length
(E
: Host_Entry_Type
) return Natural is
391 return E
.Addresses_Length
;
392 end Addresses_Length
;
399 (E
: Host_Entry_Type
;
400 N
: Positive := 1) return String
403 return To_String
(E
.Aliases
(N
));
411 (S
: Service_Entry_Type
;
412 N
: Positive := 1) return String
415 return To_String
(S
.Aliases
(N
));
422 function Aliases_Length
(E
: Host_Entry_Type
) return Natural is
424 return E
.Aliases_Length
;
431 function Aliases_Length
(S
: Service_Entry_Type
) return Natural is
433 return S
.Aliases_Length
;
440 procedure Bind_Socket
441 (Socket
: Socket_Type
;
442 Address
: Sock_Addr_Type
)
445 Sin
: aliased Sockaddr_In
;
446 Len
: constant C
.int
:= Sin
'Size / 8;
447 -- This assumes that Address.Family = Family_Inet???
450 if Address
.Family
= Family_Inet6
then
451 raise Socket_Error
with "IPv6 not supported";
454 Set_Family
(Sin
.Sin_Family
, Address
.Family
);
455 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(Address
.Addr
));
457 (Sin
'Unchecked_Access,
458 Short_To_Network
(C
.unsigned_short
(Address
.Port
)));
460 Res
:= C_Bind
(C
.int
(Socket
), Sin
'Address, Len
);
462 if Res
= Failure
then
463 Raise_Socket_Error
(Socket_Errno
);
467 ----------------------
468 -- Check_For_Fd_Set --
469 ----------------------
471 procedure Check_For_Fd_Set
(Fd
: Socket_Type
) is
475 -- On Windows, fd_set is a FD_SETSIZE array of socket ids:
476 -- no check required. Warnings suppressed because condition
477 -- is known at compile time.
479 if Target_OS
= Windows
then
483 -- On other platforms, fd_set is an FD_SETSIZE bitmap: check
484 -- that Fd is within range (otherwise behaviour is undefined).
486 elsif Fd
< 0 or else Fd
>= SOSC
.FD_SETSIZE
then
487 raise Constraint_Error
488 with "invalid value for socket set: " & Image
(Fd
);
490 end Check_For_Fd_Set
;
496 procedure Check_Selector
497 (Selector
: Selector_Type
;
498 R_Socket_Set
: in out Socket_Set_Type
;
499 W_Socket_Set
: in out Socket_Set_Type
;
500 Status
: out Selector_Status
;
501 Timeout
: Selector_Duration
:= Forever
)
503 E_Socket_Set
: Socket_Set_Type
;
506 (Selector
, R_Socket_Set
, W_Socket_Set
, E_Socket_Set
, Status
, Timeout
);
509 procedure Check_Selector
510 (Selector
: Selector_Type
;
511 R_Socket_Set
: in out Socket_Set_Type
;
512 W_Socket_Set
: in out Socket_Set_Type
;
513 E_Socket_Set
: in out Socket_Set_Type
;
514 Status
: out Selector_Status
;
515 Timeout
: Selector_Duration
:= Forever
)
519 RSig
: Socket_Type
:= No_Socket
;
520 TVal
: aliased Timeval
;
521 TPtr
: Timeval_Access
;
524 if not Is_Open
(Selector
) then
525 raise Program_Error
with "closed selector";
530 -- No timeout or Forever is indicated by a null timeval pointer
532 if Timeout
= Forever
then
535 TVal
:= To_Timeval
(Timeout
);
536 TPtr
:= TVal
'Unchecked_Access;
539 -- Add read signalling socket, if present
541 if not Selector
.Is_Null
then
542 RSig
:= Selector
.R_Sig_Socket
;
543 Set
(R_Socket_Set
, RSig
);
546 Last
:= C
.int
'Max (C
.int
'Max (C
.int
(R_Socket_Set
.Last
),
547 C
.int
(W_Socket_Set
.Last
)),
548 C
.int
(E_Socket_Set
.Last
));
550 -- Zero out fd_set for empty Socket_Set_Type objects
552 Normalize_Empty_Socket_Set
(R_Socket_Set
);
553 Normalize_Empty_Socket_Set
(W_Socket_Set
);
554 Normalize_Empty_Socket_Set
(E_Socket_Set
);
559 R_Socket_Set
.Set
'Access,
560 W_Socket_Set
.Set
'Access,
561 E_Socket_Set
.Set
'Access,
564 if Res
= Failure
then
565 Raise_Socket_Error
(Socket_Errno
);
568 -- If Select was resumed because of read signalling socket, read this
569 -- data and remove socket from set.
571 if RSig
/= No_Socket
and then Is_Set
(R_Socket_Set
, RSig
) then
572 Clear
(R_Socket_Set
, RSig
);
574 Res
:= Signalling_Fds
.Read
(C
.int
(RSig
));
576 if Res
= Failure
then
577 Raise_Socket_Error
(Socket_Errno
);
586 -- Update socket sets in regard to their new contents
588 Narrow
(R_Socket_Set
);
589 Narrow
(W_Socket_Set
);
590 Narrow
(E_Socket_Set
);
598 (Item
: in out Socket_Set_Type
;
599 Socket
: Socket_Type
)
601 Last
: aliased C
.int
:= C
.int
(Item
.Last
);
604 Check_For_Fd_Set
(Socket
);
606 if Item
.Last
/= No_Socket
then
607 Remove_Socket_From_Set
(Item
.Set
'Access, C
.int
(Socket
));
608 Last_Socket_In_Set
(Item
.Set
'Access, Last
'Unchecked_Access);
609 Item
.Last
:= Socket_Type
(Last
);
617 procedure Close_Selector
(Selector
: in out Selector_Type
) is
619 -- Nothing to do if selector already in closed state
621 if Selector
.Is_Null
or else not Is_Open
(Selector
) then
625 -- Close the signalling file descriptors used internally for the
626 -- implementation of Abort_Selector.
628 Signalling_Fds
.Close
(C
.int
(Selector
.R_Sig_Socket
));
629 Signalling_Fds
.Close
(C
.int
(Selector
.W_Sig_Socket
));
631 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
632 -- (erroneous) subsequent attempt to use this selector properly fails.
634 Selector
.R_Sig_Socket
:= No_Socket
;
635 Selector
.W_Sig_Socket
:= No_Socket
;
642 procedure Close_Socket
(Socket
: Socket_Type
) is
646 Res
:= C_Close
(C
.int
(Socket
));
648 if Res
= Failure
then
649 Raise_Socket_Error
(Socket_Errno
);
657 function Connect_Socket
658 (Socket
: Socket_Type
;
659 Server
: Sock_Addr_Type
) return C
.int
661 Sin
: aliased Sockaddr_In
;
662 Len
: constant C
.int
:= Sin
'Size / 8;
665 if Server
.Family
= Family_Inet6
then
666 raise Socket_Error
with "IPv6 not supported";
669 Set_Family
(Sin
.Sin_Family
, Server
.Family
);
670 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(Server
.Addr
));
672 (Sin
'Unchecked_Access,
673 Short_To_Network
(C
.unsigned_short
(Server
.Port
)));
675 return C_Connect
(C
.int
(Socket
), Sin
'Address, Len
);
678 procedure Connect_Socket
679 (Socket
: Socket_Type
;
680 Server
: Sock_Addr_Type
)
683 if Connect_Socket
(Socket
, Server
) = Failure
then
684 Raise_Socket_Error
(Socket_Errno
);
688 procedure Connect_Socket
689 (Socket
: Socket_Type
;
690 Server
: Sock_Addr_Type
;
691 Timeout
: Selector_Duration
;
692 Selector
: access Selector_Type
:= null;
693 Status
: out Selector_Status
)
696 -- Used to set Socket to non-blocking I/O
698 Conn_Err
: aliased Integer;
699 -- Error status of the socket after completion of select(2)
702 Conn_Err_Size
: aliased C
.int
:= Conn_Err
'Size / 8;
703 -- For getsockopt(2) call
706 if Selector
/= null and then not Is_Open
(Selector
.all) then
707 raise Program_Error
with "closed selector";
710 -- Set the socket to non-blocking I/O
712 Req
:= (Name
=> Non_Blocking_IO
, Enabled
=> True);
713 Control_Socket
(Socket
, Request
=> Req
);
715 -- Start operation (non-blocking), will return Failure with errno set
718 Res
:= Connect_Socket
(Socket
, Server
);
719 if Res
= Failure
then
720 Conn_Err
:= Socket_Errno
;
721 if Conn_Err
/= SOSC
.EINPROGRESS
then
722 Raise_Socket_Error
(Conn_Err
);
726 -- Wait for socket to become available for writing (unless the Timeout
727 -- is zero, in which case we consider that it has already expired, and
728 -- we do not need to wait at all).
730 if Timeout
= 0.0 then
738 Selector
=> Selector
,
742 -- Check error condition (the asynchronous connect may have terminated
743 -- with an error, e.g. ECONNREFUSED) if select(2) completed.
745 if Status
= Completed
then
747 (C
.int
(Socket
), SOSC
.SOL_SOCKET
, SOSC
.SO_ERROR
,
748 Conn_Err
'Address, Conn_Err_Size
'Access);
751 Conn_Err
:= Socket_Errno
;
758 -- Reset the socket to blocking I/O
760 Req
:= (Name
=> Non_Blocking_IO
, Enabled
=> False);
761 Control_Socket
(Socket
, Request
=> Req
);
763 -- Report error condition if any
765 if Conn_Err
/= 0 then
766 Raise_Socket_Error
(Conn_Err
);
774 procedure Control_Socket
775 (Socket
: Socket_Type
;
776 Request
: in out Request_Type
)
783 when Non_Blocking_IO
=>
784 Arg
:= C
.int
(Boolean'Pos (Request
.Enabled
));
786 when N_Bytes_To_Read
=>
791 (C
.int
(Socket
), Requests
(Request
.Name
), Arg
'Unchecked_Access);
793 if Res
= Failure
then
794 Raise_Socket_Error
(Socket_Errno
);
798 when Non_Blocking_IO
=>
801 when N_Bytes_To_Read
=>
802 Request
.Size
:= Natural (Arg
);
811 (Source
: Socket_Set_Type
;
812 Target
: out Socket_Set_Type
)
818 ---------------------
819 -- Create_Selector --
820 ---------------------
822 procedure Create_Selector
(Selector
: out Selector_Type
) is
823 Two_Fds
: aliased Fd_Pair
;
827 if Is_Open
(Selector
) then
828 -- Raise exception to prevent socket descriptor leak
830 raise Program_Error
with "selector already open";
833 -- We open two signalling file descriptors. One of them is used to send
834 -- data to the other, which is included in a C_Select socket set. The
835 -- communication is used to force a call to C_Select to complete, and
836 -- the waiting task to resume its execution.
838 Res
:= Signalling_Fds
.Create
(Two_Fds
'Access);
840 if Res
= Failure
then
841 Raise_Socket_Error
(Socket_Errno
);
844 Selector
.R_Sig_Socket
:= Socket_Type
(Two_Fds
(Read_End
));
845 Selector
.W_Sig_Socket
:= Socket_Type
(Two_Fds
(Write_End
));
852 procedure Create_Socket
853 (Socket
: out Socket_Type
;
854 Family
: Family_Type
:= Family_Inet
;
855 Mode
: Mode_Type
:= Socket_Stream
)
860 Res
:= C_Socket
(Families
(Family
), Modes
(Mode
), 0);
862 if Res
= Failure
then
863 Raise_Socket_Error
(Socket_Errno
);
866 Socket
:= Socket_Type
(Res
);
873 procedure Empty
(Item
: out Socket_Set_Type
) is
875 Reset_Socket_Set
(Item
.Set
'Access);
876 Item
.Last
:= No_Socket
;
883 function Err_Code_Image
(E
: Integer) return String is
884 Msg
: String := E
'Img & "] ";
886 Msg
(Msg
'First) := '[';
894 procedure Finalize
(X
: in out Sockets_Library_Controller
) is
895 pragma Unreferenced
(X
);
898 -- Finalization operation for the GNAT.Sockets package
907 procedure Finalize
is
909 -- This is a dummy placeholder for an obsolete API.
910 -- The real finalization actions are in Initialize primitive operation
911 -- of Sockets_Library_Controller.
921 (Item
: in out Socket_Set_Type
;
922 Socket
: out Socket_Type
)
925 L
: aliased C
.int
:= C
.int
(Item
.Last
);
928 if Item
.Last
/= No_Socket
then
930 (Item
.Set
'Access, Last
=> L
'Access, Socket
=> S
'Access);
931 Item
.Last
:= Socket_Type
(L
);
932 Socket
:= Socket_Type
(S
);
943 (Stream
: not null Stream_Access
) return Sock_Addr_Type
946 if Stream
.all in Datagram_Socket_Stream_Type
then
947 return Datagram_Socket_Stream_Type
(Stream
.all).From
;
949 return Get_Peer_Name
(Stream_Socket_Stream_Type
(Stream
.all).Socket
);
953 -------------------------
954 -- Get_Host_By_Address --
955 -------------------------
957 function Get_Host_By_Address
958 (Address
: Inet_Addr_Type
;
959 Family
: Family_Type
:= Family_Inet
) return Host_Entry_Type
961 pragma Unreferenced
(Family
);
963 HA
: aliased In_Addr
:= To_In_Addr
(Address
);
964 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
965 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
966 Res
: aliased Hostent
;
972 if C_Gethostbyaddr
(HA
'Address, HA
'Size / 8, SOSC
.AF_INET
,
973 Res
'Access, Buf
'Address, Buflen
, Err
'Access) /= 0
976 Raise_Host_Error
(Integer (Err
));
979 return H
: constant Host_Entry_Type
:=
980 To_Host_Entry
(Res
'Unchecked_Access)
984 end Get_Host_By_Address
;
986 ----------------------
987 -- Get_Host_By_Name --
988 ----------------------
990 function Get_Host_By_Name
(Name
: String) return Host_Entry_Type
is
992 -- Detect IP address name and redirect to Inet_Addr
994 if Is_IP_Address
(Name
) then
995 return Get_Host_By_Address
(Inet_Addr
(Name
));
999 HN
: constant C
.char_array
:= C
.To_C
(Name
);
1000 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1001 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1002 Res
: aliased Hostent
;
1003 Err
: aliased C
.int
;
1009 (HN
, Res
'Access, Buf
'Address, Buflen
, Err
'Access) /= 0
1012 Raise_Host_Error
(Integer (Err
));
1015 return H
: constant Host_Entry_Type
:=
1016 To_Host_Entry
(Res
'Unchecked_Access)
1021 end Get_Host_By_Name
;
1027 function Get_Peer_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
is
1028 Sin
: aliased Sockaddr_In
;
1029 Len
: aliased C
.int
:= Sin
'Size / 8;
1030 Res
: Sock_Addr_Type
(Family_Inet
);
1033 if C_Getpeername
(C
.int
(Socket
), Sin
'Address, Len
'Access) = Failure
then
1034 Raise_Socket_Error
(Socket_Errno
);
1037 To_Inet_Addr
(Sin
.Sin_Addr
, Res
.Addr
);
1038 Res
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1043 -------------------------
1044 -- Get_Service_By_Name --
1045 -------------------------
1047 function Get_Service_By_Name
1049 Protocol
: String) return Service_Entry_Type
1051 SN
: constant C
.char_array
:= C
.To_C
(Name
);
1052 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1053 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1054 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1055 Res
: aliased Servent
;
1060 if C_Getservbyname
(SN
, SP
, Res
'Access, Buf
'Address, Buflen
) /= 0 then
1062 raise Service_Error
with "Service not found";
1065 -- Translate from the C format to the API format
1067 return S
: constant Service_Entry_Type
:=
1068 To_Service_Entry
(Res
'Unchecked_Access)
1072 end Get_Service_By_Name
;
1074 -------------------------
1075 -- Get_Service_By_Port --
1076 -------------------------
1078 function Get_Service_By_Port
1080 Protocol
: String) return Service_Entry_Type
1082 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1083 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1084 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1085 Res
: aliased Servent
;
1091 (C
.int
(Short_To_Network
(C
.unsigned_short
(Port
))), SP
,
1092 Res
'Access, Buf
'Address, Buflen
) /= 0
1095 raise Service_Error
with "Service not found";
1098 -- Translate from the C format to the API format
1100 return S
: constant Service_Entry_Type
:=
1101 To_Service_Entry
(Res
'Unchecked_Access)
1105 end Get_Service_By_Port
;
1107 ---------------------
1108 -- Get_Socket_Name --
1109 ---------------------
1111 function Get_Socket_Name
1112 (Socket
: Socket_Type
) return Sock_Addr_Type
1114 Sin
: aliased Sockaddr_In
;
1115 Len
: aliased C
.int
:= Sin
'Size / 8;
1117 Addr
: Sock_Addr_Type
:= No_Sock_Addr
;
1120 Res
:= C_Getsockname
(C
.int
(Socket
), Sin
'Address, Len
'Access);
1122 if Res
/= Failure
then
1123 To_Inet_Addr
(Sin
.Sin_Addr
, Addr
.Addr
);
1124 Addr
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1128 end Get_Socket_Name
;
1130 -----------------------
1131 -- Get_Socket_Option --
1132 -----------------------
1134 function Get_Socket_Option
1135 (Socket
: Socket_Type
;
1136 Level
: Level_Type
:= Socket_Level
;
1137 Name
: Option_Name
) return Option_Type
1140 use type C
.unsigned_char
;
1142 V8
: aliased Two_Ints
;
1144 V1
: aliased C
.unsigned_char
;
1145 VT
: aliased Timeval
;
1146 Len
: aliased C
.int
;
1147 Add
: System
.Address
;
1149 Opt
: Option_Type
(Name
);
1153 when Multicast_Loop |
1155 Receive_Packet_Info
=>
1173 -- The standard argument for SO_RCVTIMEO and SO_SNDTIMEO is a
1174 -- struct timeval, but on Windows it is a milliseconds count in
1177 if Target_OS
= Windows
then
1201 if Res
= Failure
then
1202 Raise_Socket_Error
(Socket_Errno
);
1210 Opt
.Enabled
:= (V4
/= 0);
1213 Opt
.Enabled
:= (V8
(V8
'First) /= 0);
1214 Opt
.Seconds
:= Natural (V8
(V8
'Last));
1218 Opt
.Size
:= Natural (V4
);
1221 Opt
.Error
:= Resolve_Error
(Integer (V4
));
1223 when Add_Membership |
1225 To_Inet_Addr
(To_In_Addr
(V8
(V8
'First)), Opt
.Multicast_Address
);
1226 To_Inet_Addr
(To_In_Addr
(V8
(V8
'Last)), Opt
.Local_Interface
);
1228 when Multicast_If
=>
1229 To_Inet_Addr
(To_In_Addr
(V4
), Opt
.Outgoing_If
);
1231 when Multicast_TTL
=>
1232 Opt
.Time_To_Live
:= Integer (V1
);
1234 when Multicast_Loop |
1235 Receive_Packet_Info
=>
1236 Opt
.Enabled
:= (V1
/= 0);
1241 if Target_OS
= Windows
then
1243 -- Timeout is in milliseconds, actual value is 500 ms +
1244 -- returned value (unless it is 0).
1249 Opt
.Timeout
:= Natural (V4
) * 0.001 + 0.500;
1253 Opt
.Timeout
:= To_Duration
(VT
);
1258 end Get_Socket_Option
;
1264 function Host_Name
return String is
1265 Name
: aliased C
.char_array
(1 .. 64);
1269 Res
:= C_Gethostname
(Name
'Address, Name
'Length);
1271 if Res
= Failure
then
1272 Raise_Socket_Error
(Socket_Errno
);
1275 return C
.To_Ada
(Name
);
1283 (Val
: Inet_Addr_VN_Type
;
1284 Hex
: Boolean := False) return String
1286 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1287 -- has at most a length of 3 plus one '.' character.
1289 Buffer
: String (1 .. 4 * Val
'Length);
1290 Length
: Natural := 1;
1291 Separator
: Character;
1293 procedure Img10
(V
: Inet_Addr_Comp_Type
);
1294 -- Append to Buffer image of V in decimal format
1296 procedure Img16
(V
: Inet_Addr_Comp_Type
);
1297 -- Append to Buffer image of V in hexadecimal format
1303 procedure Img10
(V
: Inet_Addr_Comp_Type
) is
1304 Img
: constant String := V
'Img;
1305 Len
: constant Natural := Img
'Length - 1;
1307 Buffer
(Length
.. Length
+ Len
- 1) := Img
(2 .. Img
'Last);
1308 Length
:= Length
+ Len
;
1315 procedure Img16
(V
: Inet_Addr_Comp_Type
) is
1317 Buffer
(Length
) := Hex_To_Char
(Natural (V
/ 16) + 1);
1318 Buffer
(Length
+ 1) := Hex_To_Char
(Natural (V
mod 16) + 1);
1319 Length
:= Length
+ 2;
1322 -- Start of processing for Image
1325 Separator
:= (if Hex
then ':' else '.');
1327 for J
in Val
'Range loop
1334 if J
/= Val
'Last then
1335 Buffer
(Length
) := Separator
;
1336 Length
:= Length
+ 1;
1340 return Buffer
(1 .. Length
- 1);
1347 function Image
(Value
: Inet_Addr_Type
) return String is
1349 if Value
.Family
= Family_Inet
then
1350 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V4
), Hex
=> False);
1352 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V6
), Hex
=> True);
1360 function Image
(Value
: Sock_Addr_Type
) return String is
1361 Port
: constant String := Value
.Port
'Img;
1363 return Image
(Value
.Addr
) & ':' & Port
(2 .. Port
'Last);
1370 function Image
(Socket
: Socket_Type
) return String is
1379 function Image
(Item
: Socket_Set_Type
) return String is
1380 Socket_Set
: Socket_Set_Type
:= Item
;
1384 Last_Img
: constant String := Socket_Set
.Last
'Img;
1386 (1 .. (Integer (Socket_Set
.Last
) + 1) * Last_Img
'Length);
1387 Index
: Positive := 1;
1388 Socket
: Socket_Type
;
1391 while not Is_Empty
(Socket_Set
) loop
1392 Get
(Socket_Set
, Socket
);
1395 Socket_Img
: constant String := Socket
'Img;
1397 Buffer
(Index
.. Index
+ Socket_Img
'Length - 1) := Socket_Img
;
1398 Index
:= Index
+ Socket_Img
'Length;
1402 return "[" & Last_Img
& "]" & Buffer
(1 .. Index
- 1);
1410 function Inet_Addr
(Image
: String) return Inet_Addr_Type
is
1413 Img
: aliased char_array
:= To_C
(Image
);
1414 Addr
: aliased C
.int
;
1416 Result
: Inet_Addr_Type
;
1419 -- Special case for an empty Image as on some platforms (e.g. Windows)
1420 -- calling Inet_Addr("") will not return an error.
1423 Raise_Socket_Error
(SOSC
.EINVAL
);
1426 Res
:= Inet_Pton
(SOSC
.AF_INET
, Img
'Address, Addr
'Address);
1429 Raise_Socket_Error
(Socket_Errno
);
1432 Raise_Socket_Error
(SOSC
.EINVAL
);
1435 To_Inet_Addr
(To_In_Addr
(Addr
), Result
);
1443 procedure Initialize
(X
: in out Sockets_Library_Controller
) is
1444 pragma Unreferenced
(X
);
1454 procedure Initialize
(Process_Blocking_IO
: Boolean) is
1455 Expected
: constant Boolean := not SOSC
.Thread_Blocking_IO
;
1458 if Process_Blocking_IO
/= Expected
then
1459 raise Socket_Error
with
1460 "incorrect Process_Blocking_IO setting, expected " & Expected
'Img;
1463 -- This is a dummy placeholder for an obsolete API
1465 -- Real initialization actions are in Initialize primitive operation
1466 -- of Sockets_Library_Controller.
1475 procedure Initialize
is
1477 -- This is a dummy placeholder for an obsolete API
1479 -- Real initialization actions are in Initialize primitive operation
1480 -- of Sockets_Library_Controller.
1489 function Is_Empty
(Item
: Socket_Set_Type
) return Boolean is
1491 return Item
.Last
= No_Socket
;
1498 function Is_IP_Address
(Name
: String) return Boolean is
1500 for J
in Name
'Range loop
1502 and then Name
(J
) not in '0' .. '9'
1515 function Is_Open
(S
: Selector_Type
) return Boolean is
1521 -- Either both controlling socket descriptors are valid (case of an
1522 -- open selector) or neither (case of a closed selector).
1524 pragma Assert
((S
.R_Sig_Socket
/= No_Socket
)
1526 (S
.W_Sig_Socket
/= No_Socket
));
1528 return S
.R_Sig_Socket
/= No_Socket
;
1537 (Item
: Socket_Set_Type
;
1538 Socket
: Socket_Type
) return Boolean
1541 Check_For_Fd_Set
(Socket
);
1543 return Item
.Last
/= No_Socket
1544 and then Socket
<= Item
.Last
1545 and then Is_Socket_In_Set
(Item
.Set
'Access, C
.int
(Socket
)) /= 0;
1552 procedure Listen_Socket
1553 (Socket
: Socket_Type
;
1554 Length
: Natural := 15)
1556 Res
: constant C
.int
:= C_Listen
(C
.int
(Socket
), C
.int
(Length
));
1558 if Res
= Failure
then
1559 Raise_Socket_Error
(Socket_Errno
);
1567 procedure Narrow
(Item
: in out Socket_Set_Type
) is
1568 Last
: aliased C
.int
:= C
.int
(Item
.Last
);
1570 if Item
.Last
/= No_Socket
then
1571 Last_Socket_In_Set
(Item
.Set
'Access, Last
'Unchecked_Access);
1572 Item
.Last
:= Socket_Type
(Last
);
1580 procedure Netdb_Lock
is
1582 if Need_Netdb_Lock
then
1583 System
.Task_Lock
.Lock
;
1591 procedure Netdb_Unlock
is
1593 if Need_Netdb_Lock
then
1594 System
.Task_Lock
.Unlock
;
1598 --------------------------------
1599 -- Normalize_Empty_Socket_Set --
1600 --------------------------------
1602 procedure Normalize_Empty_Socket_Set
(S
: in out Socket_Set_Type
) is
1604 if S
.Last
= No_Socket
then
1605 Reset_Socket_Set
(S
.Set
'Access);
1607 end Normalize_Empty_Socket_Set
;
1613 function Official_Name
(E
: Host_Entry_Type
) return String is
1615 return To_String
(E
.Official
);
1622 function Official_Name
(S
: Service_Entry_Type
) return String is
1624 return To_String
(S
.Official
);
1627 --------------------
1628 -- Wait_On_Socket --
1629 --------------------
1631 procedure Wait_On_Socket
1632 (Socket
: Socket_Type
;
1634 Timeout
: Selector_Duration
;
1635 Selector
: access Selector_Type
:= null;
1636 Status
: out Selector_Status
)
1638 type Local_Selector_Access
is access Selector_Type
;
1639 for Local_Selector_Access
'Storage_Size use Selector_Type
'Size;
1641 S
: Selector_Access
;
1642 -- Selector to use for waiting
1644 R_Fd_Set
: Socket_Set_Type
;
1645 W_Fd_Set
: Socket_Set_Type
;
1648 -- Create selector if not provided by the user
1650 if Selector
= null then
1652 Local_S
: constant Local_Selector_Access
:= new Selector_Type
;
1654 S
:= Local_S
.all'Unchecked_Access;
1655 Create_Selector
(S
.all);
1659 S
:= Selector
.all'Access;
1663 Set
(R_Fd_Set
, Socket
);
1665 Set
(W_Fd_Set
, Socket
);
1668 Check_Selector
(S
.all, R_Fd_Set
, W_Fd_Set
, Status
, Timeout
);
1670 if Selector
= null then
1671 Close_Selector
(S
.all);
1679 function Port_Number
(S
: Service_Entry_Type
) return Port_Type
is
1688 function Protocol_Name
(S
: Service_Entry_Type
) return String is
1690 return To_String
(S
.Protocol
);
1693 ----------------------
1694 -- Raise_Host_Error --
1695 ----------------------
1697 procedure Raise_Host_Error
(H_Error
: Integer) is
1699 raise Host_Error
with
1700 Err_Code_Image
(H_Error
)
1701 & Host_Error_Messages
.Host_Error_Message
(H_Error
);
1702 end Raise_Host_Error
;
1704 ------------------------
1705 -- Raise_Socket_Error --
1706 ------------------------
1708 procedure Raise_Socket_Error
(Error
: Integer) is
1710 raise Socket_Error
with
1711 Err_Code_Image
(Error
) & Socket_Error_Message
(Error
);
1712 end Raise_Socket_Error
;
1719 (Stream
: in out Datagram_Socket_Stream_Type
;
1720 Item
: out Ada
.Streams
.Stream_Element_Array
;
1721 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1736 (Stream
: in out Stream_Socket_Stream_Type
;
1737 Item
: out Ada
.Streams
.Stream_Element_Array
;
1738 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1740 First
: Ada
.Streams
.Stream_Element_Offset
:= Item
'First;
1741 Index
: Ada
.Streams
.Stream_Element_Offset
:= First
- 1;
1742 Max
: constant Ada
.Streams
.Stream_Element_Offset
:= Item
'Last;
1746 Receive_Socket
(Stream
.Socket
, Item
(First
.. Max
), Index
);
1749 -- Exit when all or zero data received. Zero means that the socket
1752 exit when Index
< First
or else Index
= Max
;
1758 --------------------
1759 -- Receive_Socket --
1760 --------------------
1762 procedure Receive_Socket
1763 (Socket
: Socket_Type
;
1764 Item
: out Ada
.Streams
.Stream_Element_Array
;
1765 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1766 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1772 C_Recv
(C
.int
(Socket
), Item
'Address, Item
'Length, To_Int
(Flags
));
1774 if Res
= Failure
then
1775 Raise_Socket_Error
(Socket_Errno
);
1778 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
1781 --------------------
1782 -- Receive_Socket --
1783 --------------------
1785 procedure Receive_Socket
1786 (Socket
: Socket_Type
;
1787 Item
: out Ada
.Streams
.Stream_Element_Array
;
1788 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1789 From
: out Sock_Addr_Type
;
1790 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1793 Sin
: aliased Sockaddr_In
;
1794 Len
: aliased C
.int
:= Sin
'Size / 8;
1806 if Res
= Failure
then
1807 Raise_Socket_Error
(Socket_Errno
);
1810 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
1812 To_Inet_Addr
(Sin
.Sin_Addr
, From
.Addr
);
1813 From
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1816 --------------------
1817 -- Receive_Vector --
1818 --------------------
1820 procedure Receive_Vector
1821 (Socket
: Socket_Type
;
1822 Vector
: Vector_Type
;
1823 Count
: out Ada
.Streams
.Stream_Element_Count
;
1824 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1829 (Msg_Name
=> System
.Null_Address
,
1831 Msg_Iov
=> Vector
'Address,
1833 -- recvmsg(2) returns EMSGSIZE on Linux (and probably on other
1834 -- platforms) when the supplied vector is longer than IOV_MAX,
1835 -- so use minimum of the two lengths.
1837 Msg_Iovlen
=> SOSC
.Msg_Iovlen_T
'Min
1838 (Vector
'Length, SOSC
.IOV_MAX
),
1840 Msg_Control
=> System
.Null_Address
,
1841 Msg_Controllen
=> 0,
1851 if Res
= ssize_t
(Failure
) then
1852 Raise_Socket_Error
(Socket_Errno
);
1855 Count
:= Ada
.Streams
.Stream_Element_Count
(Res
);
1862 function Resolve_Error
1863 (Error_Value
: Integer;
1864 From_Errno
: Boolean := True) return Error_Type
1866 use GNAT
.Sockets
.SOSC
;
1869 if not From_Errno
then
1871 when SOSC
.HOST_NOT_FOUND
=> return Unknown_Host
;
1872 when SOSC
.TRY_AGAIN
=> return Host_Name_Lookup_Failure
;
1873 when SOSC
.NO_RECOVERY
=> return Non_Recoverable_Error
;
1874 when SOSC
.NO_DATA
=> return Unknown_Server_Error
;
1875 when others => return Cannot_Resolve_Error
;
1879 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1880 -- can't include it in the case statement below.
1882 pragma Warnings
(Off
);
1883 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1885 if EAGAIN
/= EWOULDBLOCK
and then Error_Value
= EAGAIN
then
1886 return Resource_Temporarily_Unavailable
;
1889 -- This is not a case statement because if a particular error
1890 -- number constant is not defined, s-oscons-tmplt.c defines
1891 -- it to -1. If multiple constants are not defined, they
1892 -- would each be -1 and result in a "duplicate value in case" error.
1894 -- But we have to leave warnings off because the compiler is also
1895 -- smart enough to note that when two errnos have the same value,
1896 -- the second if condition is useless.
1897 if Error_Value
= ENOERROR
then
1899 elsif Error_Value
= EACCES
then
1900 return Permission_Denied
;
1901 elsif Error_Value
= EADDRINUSE
then
1902 return Address_Already_In_Use
;
1903 elsif Error_Value
= EADDRNOTAVAIL
then
1904 return Cannot_Assign_Requested_Address
;
1905 elsif Error_Value
= EAFNOSUPPORT
then
1906 return Address_Family_Not_Supported_By_Protocol
;
1907 elsif Error_Value
= EALREADY
then
1908 return Operation_Already_In_Progress
;
1909 elsif Error_Value
= EBADF
then
1910 return Bad_File_Descriptor
;
1911 elsif Error_Value
= ECONNABORTED
then
1912 return Software_Caused_Connection_Abort
;
1913 elsif Error_Value
= ECONNREFUSED
then
1914 return Connection_Refused
;
1915 elsif Error_Value
= ECONNRESET
then
1916 return Connection_Reset_By_Peer
;
1917 elsif Error_Value
= EDESTADDRREQ
then
1918 return Destination_Address_Required
;
1919 elsif Error_Value
= EFAULT
then
1921 elsif Error_Value
= EHOSTDOWN
then
1922 return Host_Is_Down
;
1923 elsif Error_Value
= EHOSTUNREACH
then
1924 return No_Route_To_Host
;
1925 elsif Error_Value
= EINPROGRESS
then
1926 return Operation_Now_In_Progress
;
1927 elsif Error_Value
= EINTR
then
1928 return Interrupted_System_Call
;
1929 elsif Error_Value
= EINVAL
then
1930 return Invalid_Argument
;
1931 elsif Error_Value
= EIO
then
1932 return Input_Output_Error
;
1933 elsif Error_Value
= EISCONN
then
1934 return Transport_Endpoint_Already_Connected
;
1935 elsif Error_Value
= ELOOP
then
1936 return Too_Many_Symbolic_Links
;
1937 elsif Error_Value
= EMFILE
then
1938 return Too_Many_Open_Files
;
1939 elsif Error_Value
= EMSGSIZE
then
1940 return Message_Too_Long
;
1941 elsif Error_Value
= ENAMETOOLONG
then
1942 return File_Name_Too_Long
;
1943 elsif Error_Value
= ENETDOWN
then
1944 return Network_Is_Down
;
1945 elsif Error_Value
= ENETRESET
then
1946 return Network_Dropped_Connection_Because_Of_Reset
;
1947 elsif Error_Value
= ENETUNREACH
then
1948 return Network_Is_Unreachable
;
1949 elsif Error_Value
= ENOBUFS
then
1950 return No_Buffer_Space_Available
;
1951 elsif Error_Value
= ENOPROTOOPT
then
1952 return Protocol_Not_Available
;
1953 elsif Error_Value
= ENOTCONN
then
1954 return Transport_Endpoint_Not_Connected
;
1955 elsif Error_Value
= ENOTSOCK
then
1956 return Socket_Operation_On_Non_Socket
;
1957 elsif Error_Value
= EOPNOTSUPP
then
1958 return Operation_Not_Supported
;
1959 elsif Error_Value
= EPFNOSUPPORT
then
1960 return Protocol_Family_Not_Supported
;
1961 elsif Error_Value
= EPIPE
then
1963 elsif Error_Value
= EPROTONOSUPPORT
then
1964 return Protocol_Not_Supported
;
1965 elsif Error_Value
= EPROTOTYPE
then
1966 return Protocol_Wrong_Type_For_Socket
;
1967 elsif Error_Value
= ESHUTDOWN
then
1968 return Cannot_Send_After_Transport_Endpoint_Shutdown
;
1969 elsif Error_Value
= ESOCKTNOSUPPORT
then
1970 return Socket_Type_Not_Supported
;
1971 elsif Error_Value
= ETIMEDOUT
then
1972 return Connection_Timed_Out
;
1973 elsif Error_Value
= ETOOMANYREFS
then
1974 return Too_Many_References
;
1975 elsif Error_Value
= EWOULDBLOCK
then
1976 return Resource_Temporarily_Unavailable
;
1978 return Cannot_Resolve_Error
;
1980 pragma Warnings
(On
);
1984 -----------------------
1985 -- Resolve_Exception --
1986 -----------------------
1988 function Resolve_Exception
1989 (Occurrence
: Exception_Occurrence
) return Error_Type
1991 Id
: constant Exception_Id
:= Exception_Identity
(Occurrence
);
1992 Msg
: constant String := Exception_Message
(Occurrence
);
1999 while First
<= Msg
'Last
2000 and then Msg
(First
) not in '0' .. '9'
2005 if First
> Msg
'Last then
2006 return Cannot_Resolve_Error
;
2010 while Last
< Msg
'Last
2011 and then Msg
(Last
+ 1) in '0' .. '9'
2016 Val
:= Integer'Value (Msg
(First
.. Last
));
2018 if Id
= Socket_Error_Id
then
2019 return Resolve_Error
(Val
);
2021 elsif Id
= Host_Error_Id
then
2022 return Resolve_Error
(Val
, False);
2025 return Cannot_Resolve_Error
;
2027 end Resolve_Exception
;
2033 procedure Send_Socket
2034 (Socket
: Socket_Type
;
2035 Item
: Ada
.Streams
.Stream_Element_Array
;
2036 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2037 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2040 Send_Socket
(Socket
, Item
, Last
, To
=> null, Flags
=> Flags
);
2047 procedure Send_Socket
2048 (Socket
: Socket_Type
;
2049 Item
: Ada
.Streams
.Stream_Element_Array
;
2050 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2051 To
: Sock_Addr_Type
;
2052 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2056 (Socket
, Item
, Last
, To
=> To
'Unrestricted_Access, Flags
=> Flags
);
2063 procedure Send_Socket
2064 (Socket
: Socket_Type
;
2065 Item
: Ada
.Streams
.Stream_Element_Array
;
2066 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2067 To
: access Sock_Addr_Type
;
2068 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2072 Sin
: aliased Sockaddr_In
;
2073 C_To
: System
.Address
;
2078 Set_Family
(Sin
.Sin_Family
, To
.Family
);
2079 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(To
.Addr
));
2081 (Sin
'Unchecked_Access,
2082 Short_To_Network
(C
.unsigned_short
(To
.Port
)));
2083 C_To
:= Sin
'Address;
2084 Len
:= Sin
'Size / 8;
2087 C_To
:= System
.Null_Address
;
2095 Set_Forced_Flags
(To_Int
(Flags
)),
2099 if Res
= Failure
then
2100 Raise_Socket_Error
(Socket_Errno
);
2103 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
2110 procedure Send_Vector
2111 (Socket
: Socket_Type
;
2112 Vector
: Vector_Type
;
2113 Count
: out Ada
.Streams
.Stream_Element_Count
;
2114 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2120 Iov_Count
: SOSC
.Msg_Iovlen_T
;
2121 This_Iov_Count
: SOSC
.Msg_Iovlen_T
;
2127 while Iov_Count
< Vector
'Length loop
2129 pragma Warnings
(Off
);
2130 -- Following test may be compile time known on some targets
2133 (if Vector
'Length - Iov_Count
> SOSC
.IOV_MAX
2135 else Vector
'Length - Iov_Count
);
2137 pragma Warnings
(On
);
2140 (Msg_Name
=> System
.Null_Address
,
2143 (Vector
'First + Integer (Iov_Count
))'Address,
2144 Msg_Iovlen
=> This_Iov_Count
,
2145 Msg_Control
=> System
.Null_Address
,
2146 Msg_Controllen
=> 0,
2153 Set_Forced_Flags
(To_Int
(Flags
)));
2155 if Res
= ssize_t
(Failure
) then
2156 Raise_Socket_Error
(Socket_Errno
);
2159 Count
:= Count
+ Ada
.Streams
.Stream_Element_Count
(Res
);
2160 Iov_Count
:= Iov_Count
+ This_Iov_Count
;
2168 procedure Set
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
) is
2170 Check_For_Fd_Set
(Socket
);
2172 if Item
.Last
= No_Socket
then
2174 -- Uninitialized socket set, make sure it is properly zeroed out
2176 Reset_Socket_Set
(Item
.Set
'Access);
2177 Item
.Last
:= Socket
;
2179 elsif Item
.Last
< Socket
then
2180 Item
.Last
:= Socket
;
2183 Insert_Socket_In_Set
(Item
.Set
'Access, C
.int
(Socket
));
2186 -----------------------
2187 -- Set_Close_On_Exec --
2188 -----------------------
2190 procedure Set_Close_On_Exec
2191 (Socket
: Socket_Type
;
2192 Close_On_Exec
: Boolean;
2193 Status
: out Boolean)
2195 function C_Set_Close_On_Exec
2196 (Socket
: Socket_Type
; Close_On_Exec
: C
.int
) return C
.int
;
2197 pragma Import
(C
, C_Set_Close_On_Exec
, "__gnat_set_close_on_exec");
2199 Status
:= C_Set_Close_On_Exec
(Socket
, Boolean'Pos (Close_On_Exec
)) = 0;
2200 end Set_Close_On_Exec
;
2202 ----------------------
2203 -- Set_Forced_Flags --
2204 ----------------------
2206 function Set_Forced_Flags
(F
: C
.int
) return C
.int
is
2207 use type C
.unsigned
;
2208 function To_unsigned
is
2209 new Ada
.Unchecked_Conversion
(C
.int
, C
.unsigned
);
2211 new Ada
.Unchecked_Conversion
(C
.unsigned
, C
.int
);
2213 return To_int
(To_unsigned
(F
) or SOSC
.MSG_Forced_Flags
);
2214 end Set_Forced_Flags
;
2216 -----------------------
2217 -- Set_Socket_Option --
2218 -----------------------
2220 procedure Set_Socket_Option
2221 (Socket
: Socket_Type
;
2222 Level
: Level_Type
:= Socket_Level
;
2223 Option
: Option_Type
)
2227 V8
: aliased Two_Ints
;
2229 V1
: aliased C
.unsigned_char
;
2230 VT
: aliased Timeval
;
2232 Add
: System
.Address
:= Null_Address
;
2241 V4
:= C
.int
(Boolean'Pos (Option
.Enabled
));
2246 V8
(V8
'First) := C
.int
(Boolean'Pos (Option
.Enabled
));
2247 V8
(V8
'Last) := C
.int
(Option
.Seconds
);
2253 V4
:= C
.int
(Option
.Size
);
2258 V4
:= C
.int
(Boolean'Pos (True));
2262 when Add_Membership |
2264 V8
(V8
'First) := To_Int
(To_In_Addr
(Option
.Multicast_Address
));
2265 V8
(V8
'Last) := To_Int
(To_In_Addr
(Option
.Local_Interface
));
2269 when Multicast_If
=>
2270 V4
:= To_Int
(To_In_Addr
(Option
.Outgoing_If
));
2274 when Multicast_TTL
=>
2275 V1
:= C
.unsigned_char
(Option
.Time_To_Live
);
2279 when Multicast_Loop |
2280 Receive_Packet_Info
=>
2281 V1
:= C
.unsigned_char
(Boolean'Pos (Option
.Enabled
));
2288 if Target_OS
= Windows
then
2290 -- On Windows, the timeout is a DWORD in milliseconds, and
2291 -- the actual timeout is 500 ms + the given value (unless it
2294 V4
:= C
.int
(Option
.Timeout
/ 0.001);
2307 VT
:= To_Timeval
(Option
.Timeout
);
2317 Options
(Option
.Name
),
2320 if Res
= Failure
then
2321 Raise_Socket_Error
(Socket_Errno
);
2323 end Set_Socket_Option
;
2325 ----------------------
2326 -- Short_To_Network --
2327 ----------------------
2329 function Short_To_Network
(S
: C
.unsigned_short
) return C
.unsigned_short
is
2330 use type C
.unsigned_short
;
2333 -- Big-endian case. No conversion needed. On these platforms, htons()
2334 -- defaults to a null procedure.
2336 if Default_Bit_Order
= High_Order_First
then
2339 -- Little-endian case. We must swap the high and low bytes of this
2340 -- short to make the port number network compliant.
2343 return (S
/ 256) + (S
mod 256) * 256;
2345 end Short_To_Network
;
2347 ---------------------
2348 -- Shutdown_Socket --
2349 ---------------------
2351 procedure Shutdown_Socket
2352 (Socket
: Socket_Type
;
2353 How
: Shutmode_Type
:= Shut_Read_Write
)
2358 Res
:= C_Shutdown
(C
.int
(Socket
), Shutmodes
(How
));
2360 if Res
= Failure
then
2361 Raise_Socket_Error
(Socket_Errno
);
2363 end Shutdown_Socket
;
2370 (Socket
: Socket_Type
;
2371 Send_To
: Sock_Addr_Type
) return Stream_Access
2373 S
: Datagram_Socket_Stream_Access
;
2376 S
:= new Datagram_Socket_Stream_Type
;
2379 S
.From
:= Get_Socket_Name
(Socket
);
2380 return Stream_Access
(S
);
2387 function Stream
(Socket
: Socket_Type
) return Stream_Access
is
2388 S
: Stream_Socket_Stream_Access
;
2390 S
:= new Stream_Socket_Stream_Type
;
2392 return Stream_Access
(S
);
2399 function To_C
(Socket
: Socket_Type
) return Integer is
2401 return Integer (Socket
);
2408 function To_Duration
(Val
: Timeval
) return Timeval_Duration
is
2410 return Natural (Val
.Tv_Sec
) * 1.0 + Natural (Val
.Tv_Usec
) * 1.0E-6;
2417 function To_Host_Entry
(E
: Hostent_Access
) return Host_Entry_Type
is
2420 Aliases_Count
, Addresses_Count
: Natural;
2422 -- H_Length is not used because it is currently only ever set to 4, as
2423 -- H_Addrtype is always AF_INET.
2427 while Hostent_H_Alias
(E
, C
.int
(Aliases_Count
)) /= Null_Address
loop
2428 Aliases_Count
:= Aliases_Count
+ 1;
2431 Addresses_Count
:= 0;
2432 while Hostent_H_Addr
(E
, C
.int
(Addresses_Count
)) /= Null_Address
loop
2433 Addresses_Count
:= Addresses_Count
+ 1;
2436 return Result
: Host_Entry_Type
2437 (Aliases_Length
=> Aliases_Count
,
2438 Addresses_Length
=> Addresses_Count
)
2440 Result
.Official
:= To_Name
(Value
(Hostent_H_Name
(E
)));
2442 for J
in Result
.Aliases
'Range loop
2443 Result
.Aliases
(J
) :=
2444 To_Name
(Value
(Hostent_H_Alias
2445 (E
, C
.int
(J
- Result
.Aliases
'First))));
2448 for J
in Result
.Addresses
'Range loop
2452 -- Hostent_H_Addr (E, <index>) may return an address that is
2453 -- not correctly aligned for In_Addr, so we need to use
2454 -- an intermediate copy operation on a type with an alignemnt
2455 -- of 1 to recover the value.
2457 subtype Addr_Buf_T
is C
.char_array
(1 .. Addr
'Size / 8);
2458 Unaligned_Addr
: Addr_Buf_T
;
2459 for Unaligned_Addr
'Address
2460 use Hostent_H_Addr
(E
, C
.int
(J
- Result
.Addresses
'First));
2461 pragma Import
(Ada
, Unaligned_Addr
);
2463 Aligned_Addr
: Addr_Buf_T
;
2464 for Aligned_Addr
'Address use Addr
'Address;
2465 pragma Import
(Ada
, Aligned_Addr
);
2468 Aligned_Addr
:= Unaligned_Addr
;
2469 To_Inet_Addr
(Addr
, Result
.Addresses
(J
));
2479 function To_In_Addr
(Addr
: Inet_Addr_Type
) return In_Addr
is
2481 if Addr
.Family
= Family_Inet
then
2482 return (S_B1
=> C
.unsigned_char
(Addr
.Sin_V4
(1)),
2483 S_B2
=> C
.unsigned_char
(Addr
.Sin_V4
(2)),
2484 S_B3
=> C
.unsigned_char
(Addr
.Sin_V4
(3)),
2485 S_B4
=> C
.unsigned_char
(Addr
.Sin_V4
(4)));
2488 raise Socket_Error
with "IPv6 not supported";
2495 procedure To_Inet_Addr
2497 Result
: out Inet_Addr_Type
) is
2499 Result
.Sin_V4
(1) := Inet_Addr_Comp_Type
(Addr
.S_B1
);
2500 Result
.Sin_V4
(2) := Inet_Addr_Comp_Type
(Addr
.S_B2
);
2501 Result
.Sin_V4
(3) := Inet_Addr_Comp_Type
(Addr
.S_B3
);
2502 Result
.Sin_V4
(4) := Inet_Addr_Comp_Type
(Addr
.S_B4
);
2509 function To_Int
(F
: Request_Flag_Type
) return C
.int
2511 Current
: Request_Flag_Type
:= F
;
2512 Result
: C
.int
:= 0;
2515 for J
in Flags
'Range loop
2516 exit when Current
= 0;
2518 if Current
mod 2 /= 0 then
2519 if Flags
(J
) = -1 then
2520 Raise_Socket_Error
(SOSC
.EOPNOTSUPP
);
2523 Result
:= Result
+ Flags
(J
);
2526 Current
:= Current
/ 2;
2536 function To_Name
(N
: String) return Name_Type
is
2538 return Name_Type
'(N'Length, N);
2541 ----------------------
2542 -- To_Service_Entry --
2543 ----------------------
2545 function To_Service_Entry (E : Servent_Access) return Service_Entry_Type is
2548 Aliases_Count : Natural;
2552 while Servent_S_Alias (E, C.int (Aliases_Count)) /= Null_Address loop
2553 Aliases_Count := Aliases_Count + 1;
2556 return Result : Service_Entry_Type (Aliases_Length => Aliases_Count) do
2557 Result.Official := To_Name (Value (Servent_S_Name (E)));
2559 for J in Result.Aliases'Range loop
2560 Result.Aliases (J) :=
2561 To_Name (Value (Servent_S_Alias
2562 (E, C.int (J - Result.Aliases'First))));
2565 Result.Protocol := To_Name (Value (Servent_S_Proto (E)));
2567 Port_Type (Network_To_Short (Servent_S_Port (E)));
2569 end To_Service_Entry;
2575 function To_String (HN : Name_Type) return String is
2577 return HN.Name (1 .. HN.Length);
2584 function To_Timeval (Val : Timeval_Duration) return Timeval is
2589 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2595 -- Normal case where we do round down
2598 S := time_t (Val - 0.5);
2599 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2609 function Value (S : System.Address) return String is
2610 Str : String (1 .. Positive'Last);
2611 for Str'Address use S;
2612 pragma Import (Ada, Str);
2614 Terminator : Positive := Str'First;
2617 while Str (Terminator) /= ASCII.NUL loop
2618 Terminator := Terminator + 1;
2621 return Str (1 .. Terminator - 1);
2629 (Stream : in out Datagram_Socket_Stream_Type;
2630 Item : Ada.Streams.Stream_Element_Array)
2632 Last : Stream_Element_Offset;
2641 -- It is an error if not all of the data has been sent
2643 if Last /= Item'Last then
2644 Raise_Socket_Error (Socket_Errno);
2653 (Stream : in out Stream_Socket_Stream_Type;
2654 Item : Ada.Streams.Stream_Element_Array)
2656 First : Ada.Streams.Stream_Element_Offset;
2657 Index : Ada.Streams.Stream_Element_Offset;
2658 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2661 First := Item'First;
2663 while First <= Max loop
2664 Send_Socket (Stream.Socket, Item (First .. Max), Index, null);
2666 -- Exit when all or zero data sent. Zero means that the socket has
2667 -- been closed by peer.
2669 exit when Index < First or else Index = Max;
2674 -- For an empty array, we have First > Max, and hence Index >= Max (no
2675 -- error, the loop above is never executed). After a successful send,
2676 -- Index = Max. The only remaining case, Index < Max, is therefore
2677 -- always an actual send failure.
2680 Raise_Socket_Error (Socket_Errno);
2684 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2685 pragma Unreferenced (Sockets_Library_Controller_Object);
2686 -- The elaboration and finalization of this object perform the required
2687 -- initialization and cleanup actions for the sockets library.