1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2017, 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
;
53 ENOERROR
: constant := 0;
55 Netdb_Buffer_Size
: constant := SOSC
.Need_Netdb_Buffer
* 1024;
56 Need_Netdb_Lock
: constant Boolean := SOSC
.Need_Netdb_Lock
/= 0;
57 -- The network database functions gethostbyname, gethostbyaddr,
58 -- getservbyname and getservbyport can either be guaranteed task safe by
59 -- the operating system, or else return data through a user-provided buffer
60 -- to ensure concurrent uses do not interfere.
62 -- Correspondence tables
64 Levels
: constant array (Level_Type
) of C
.int
:=
65 (Socket_Level
=> SOSC
.SOL_SOCKET
,
66 IP_Protocol_For_IP_Level
=> SOSC
.IPPROTO_IP
,
67 IP_Protocol_For_UDP_Level
=> SOSC
.IPPROTO_UDP
,
68 IP_Protocol_For_TCP_Level
=> SOSC
.IPPROTO_TCP
);
70 Modes
: constant array (Mode_Type
) of C
.int
:=
71 (Socket_Stream
=> SOSC
.SOCK_STREAM
,
72 Socket_Datagram
=> SOSC
.SOCK_DGRAM
);
74 Shutmodes
: constant array (Shutmode_Type
) of C
.int
:=
75 (Shut_Read
=> SOSC
.SHUT_RD
,
76 Shut_Write
=> SOSC
.SHUT_WR
,
77 Shut_Read_Write
=> SOSC
.SHUT_RDWR
);
79 Requests
: constant array (Request_Name
) of SOSC
.IOCTL_Req_T
:=
80 (Non_Blocking_IO
=> SOSC
.FIONBIO
,
81 N_Bytes_To_Read
=> SOSC
.FIONREAD
);
83 Options
: constant array (Specific_Option_Name
) of C
.int
:=
84 (Keep_Alive
=> SOSC
.SO_KEEPALIVE
,
85 Reuse_Address
=> SOSC
.SO_REUSEADDR
,
86 Broadcast
=> SOSC
.SO_BROADCAST
,
87 Send_Buffer
=> SOSC
.SO_SNDBUF
,
88 Receive_Buffer
=> SOSC
.SO_RCVBUF
,
89 Linger
=> SOSC
.SO_LINGER
,
90 Error
=> SOSC
.SO_ERROR
,
91 No_Delay
=> SOSC
.TCP_NODELAY
,
92 Add_Membership
=> SOSC
.IP_ADD_MEMBERSHIP
,
93 Drop_Membership
=> SOSC
.IP_DROP_MEMBERSHIP
,
94 Multicast_If
=> SOSC
.IP_MULTICAST_IF
,
95 Multicast_TTL
=> SOSC
.IP_MULTICAST_TTL
,
96 Multicast_Loop
=> SOSC
.IP_MULTICAST_LOOP
,
97 Receive_Packet_Info
=> SOSC
.IP_PKTINFO
,
98 Send_Timeout
=> SOSC
.SO_SNDTIMEO
,
99 Receive_Timeout
=> SOSC
.SO_RCVTIMEO
,
100 Busy_Polling
=> SOSC
.SO_BUSY_POLL
);
101 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
102 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
104 Flags
: constant array (0 .. 3) of C
.int
:=
105 (0 => SOSC
.MSG_OOB
, -- Process_Out_Of_Band_Data
106 1 => SOSC
.MSG_PEEK
, -- Peek_At_Incoming_Data
107 2 => SOSC
.MSG_WAITALL
, -- Wait_For_A_Full_Reception
108 3 => SOSC
.MSG_EOR
); -- Send_End_Of_Record
110 Socket_Error_Id
: constant Exception_Id
:= Socket_Error
'Identity;
111 Host_Error_Id
: constant Exception_Id
:= Host_Error
'Identity;
113 Hex_To_Char
: constant String (1 .. 16) := "0123456789ABCDEF";
114 -- Use to print in hexadecimal format
116 -----------------------
117 -- Local subprograms --
118 -----------------------
120 function Resolve_Error
121 (Error_Value
: Integer;
122 From_Errno
: Boolean := True) return Error_Type
;
123 -- Associate an enumeration value (error_type) to an error value (errno).
124 -- From_Errno prevents from mixing h_errno with errno.
126 function To_Name
(N
: String) return Name_Type
;
127 function To_String
(HN
: Name_Type
) return String;
128 -- Conversion functions
130 function To_Int
(F
: Request_Flag_Type
) return C
.int
;
131 -- Return the int value corresponding to the specified flags combination
133 function Set_Forced_Flags
(F
: C
.int
) return C
.int
;
134 -- Return F with the bits from SOSC.MSG_Forced_Flags forced set
136 function Short_To_Network
137 (S
: C
.unsigned_short
) return C
.unsigned_short
;
138 pragma Inline
(Short_To_Network
);
139 -- Convert a port number into a network port number
141 function Network_To_Short
142 (S
: C
.unsigned_short
) return C
.unsigned_short
143 renames Short_To_Network
;
144 -- Symmetric operation
147 (Val
: Inet_Addr_VN_Type
;
148 Hex
: Boolean := False) return String;
149 -- Output an array of inet address components in hex or decimal mode
151 function Is_IP_Address
(Name
: String) return Boolean;
152 -- Return true when Name is an IPv4 address in dotted quad notation
154 procedure Netdb_Lock
;
155 pragma Inline
(Netdb_Lock
);
156 procedure Netdb_Unlock
;
157 pragma Inline
(Netdb_Unlock
);
158 -- Lock/unlock operation used to protect netdb access for platforms that
159 -- require such protection.
161 function To_In_Addr
(Addr
: Inet_Addr_Type
) return In_Addr
;
162 procedure To_Inet_Addr
164 Result
: out Inet_Addr_Type
);
165 -- Conversion functions
167 function To_Host_Entry
(E
: Hostent_Access
) return Host_Entry_Type
;
168 -- Conversion function
170 function To_Service_Entry
(E
: Servent_Access
) return Service_Entry_Type
;
171 -- Conversion function
173 function Value
(S
: System
.Address
) return String;
174 -- Same as Interfaces.C.Strings.Value but taking a System.Address
176 function To_Timeval
(Val
: Timeval_Duration
) return Timeval
;
177 -- Separate Val in seconds and microseconds
179 function To_Duration
(Val
: Timeval
) return Timeval_Duration
;
180 -- Reconstruct a Duration value from a Timeval record (seconds and
183 procedure Raise_Socket_Error
(Error
: Integer);
184 -- Raise Socket_Error with an exception message describing the error code
187 procedure Raise_Host_Error
(H_Error
: Integer; Name
: String);
188 -- Raise Host_Error exception with message describing error code (note
189 -- hstrerror seems to be obsolete) from h_errno. Name is the name
190 -- or address that was being looked up.
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 behavior 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
), Image
(Address
));
980 return H
: constant Host_Entry_Type
:=
981 To_Host_Entry
(Res
'Unchecked_Access)
990 end Get_Host_By_Address
;
992 ----------------------
993 -- Get_Host_By_Name --
994 ----------------------
996 function Get_Host_By_Name
(Name
: String) return Host_Entry_Type
is
998 -- If the given name actually is the string representation of
999 -- an IP address, use Get_Host_By_Address instead.
1001 if Is_IP_Address
(Name
) then
1002 return Get_Host_By_Address
(Inet_Addr
(Name
));
1006 HN
: constant C
.char_array
:= C
.To_C
(Name
);
1007 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1008 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1009 Res
: aliased Hostent
;
1010 Err
: aliased C
.int
;
1016 (HN
, Res
'Access, Buf
'Address, Buflen
, Err
'Access) /= 0
1019 Raise_Host_Error
(Integer (Err
), Name
);
1022 return H
: constant Host_Entry_Type
:=
1023 To_Host_Entry
(Res
'Unchecked_Access)
1028 end Get_Host_By_Name
;
1034 function Get_Peer_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
is
1035 Sin
: aliased Sockaddr_In
;
1036 Len
: aliased C
.int
:= Sin
'Size / 8;
1037 Res
: Sock_Addr_Type
(Family_Inet
);
1040 if C_Getpeername
(C
.int
(Socket
), Sin
'Address, Len
'Access) = Failure
then
1041 Raise_Socket_Error
(Socket_Errno
);
1044 To_Inet_Addr
(Sin
.Sin_Addr
, Res
.Addr
);
1045 Res
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1050 -------------------------
1051 -- Get_Service_By_Name --
1052 -------------------------
1054 function Get_Service_By_Name
1056 Protocol
: String) return Service_Entry_Type
1058 SN
: constant C
.char_array
:= C
.To_C
(Name
);
1059 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1060 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1061 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1062 Res
: aliased Servent
;
1067 if C_Getservbyname
(SN
, SP
, Res
'Access, Buf
'Address, Buflen
) /= 0 then
1069 raise Service_Error
with "Service not found";
1072 -- Translate from the C format to the API format
1074 return S
: constant Service_Entry_Type
:=
1075 To_Service_Entry
(Res
'Unchecked_Access)
1079 end Get_Service_By_Name
;
1081 -------------------------
1082 -- Get_Service_By_Port --
1083 -------------------------
1085 function Get_Service_By_Port
1087 Protocol
: String) return Service_Entry_Type
1089 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1090 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1091 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1092 Res
: aliased Servent
;
1098 (C
.int
(Short_To_Network
(C
.unsigned_short
(Port
))), SP
,
1099 Res
'Access, Buf
'Address, Buflen
) /= 0
1102 raise Service_Error
with "Service not found";
1105 -- Translate from the C format to the API format
1107 return S
: constant Service_Entry_Type
:=
1108 To_Service_Entry
(Res
'Unchecked_Access)
1112 end Get_Service_By_Port
;
1114 ---------------------
1115 -- Get_Socket_Name --
1116 ---------------------
1118 function Get_Socket_Name
1119 (Socket
: Socket_Type
) return Sock_Addr_Type
1121 Sin
: aliased Sockaddr_In
;
1122 Len
: aliased C
.int
:= Sin
'Size / 8;
1124 Addr
: Sock_Addr_Type
:= No_Sock_Addr
;
1127 Res
:= C_Getsockname
(C
.int
(Socket
), Sin
'Address, Len
'Access);
1129 if Res
/= Failure
then
1130 To_Inet_Addr
(Sin
.Sin_Addr
, Addr
.Addr
);
1131 Addr
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1135 end Get_Socket_Name
;
1137 -----------------------
1138 -- Get_Socket_Option --
1139 -----------------------
1141 function Get_Socket_Option
1142 (Socket
: Socket_Type
;
1143 Level
: Level_Type
:= Socket_Level
;
1145 Optname
: Interfaces
.C
.int
:= -1) return Option_Type
1148 use type C
.unsigned_char
;
1150 V8
: aliased Two_Ints
;
1152 V1
: aliased C
.unsigned_char
;
1153 VT
: aliased Timeval
;
1154 Len
: aliased C
.int
;
1155 Add
: System
.Address
;
1157 Opt
: Option_Type
(Name
);
1158 Onm
: Interfaces
.C
.int
;
1161 if Name
in Specific_Option_Name
then
1162 Onm
:= Options
(Name
);
1164 elsif Optname
= -1 then
1165 raise Socket_Error
with "optname must be specified";
1174 | Receive_Packet_Info
1193 when Receive_Timeout
1196 -- The standard argument for SO_RCVTIMEO and SO_SNDTIMEO is a
1197 -- struct timeval, but on Windows it is a milliseconds count in
1200 if Target_OS
= Windows
then
1224 if Res
= Failure
then
1225 Raise_Socket_Error
(Socket_Errno
);
1229 when Generic_Option
=>
1238 Opt
.Enabled
:= (V4
/= 0);
1240 when Busy_Polling
=>
1241 Opt
.Microseconds
:= Natural (V4
);
1244 Opt
.Enabled
:= (V8
(V8
'First) /= 0);
1245 Opt
.Seconds
:= Natural (V8
(V8
'Last));
1250 Opt
.Size
:= Natural (V4
);
1253 Opt
.Error
:= Resolve_Error
(Integer (V4
));
1258 To_Inet_Addr
(To_In_Addr
(V8
(V8
'First)), Opt
.Multicast_Address
);
1259 To_Inet_Addr
(To_In_Addr
(V8
(V8
'Last)), Opt
.Local_Interface
);
1261 when Multicast_If
=>
1262 To_Inet_Addr
(To_In_Addr
(V4
), Opt
.Outgoing_If
);
1264 when Multicast_TTL
=>
1265 Opt
.Time_To_Live
:= Integer (V1
);
1268 | Receive_Packet_Info
1270 Opt
.Enabled
:= (V1
/= 0);
1272 when Receive_Timeout
1275 if Target_OS
= Windows
then
1277 -- Timeout is in milliseconds, actual value is 500 ms +
1278 -- returned value (unless it is 0).
1283 Opt
.Timeout
:= Natural (V4
) * 0.001 + 0.500;
1287 Opt
.Timeout
:= To_Duration
(VT
);
1292 end Get_Socket_Option
;
1298 function Host_Name
return String is
1299 Name
: aliased C
.char_array
(1 .. 64);
1303 Res
:= C_Gethostname
(Name
'Address, Name
'Length);
1305 if Res
= Failure
then
1306 Raise_Socket_Error
(Socket_Errno
);
1309 return C
.To_Ada
(Name
);
1317 (Val
: Inet_Addr_VN_Type
;
1318 Hex
: Boolean := False) return String
1320 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1321 -- has at most a length of 3 plus one '.' character.
1323 Buffer
: String (1 .. 4 * Val
'Length);
1324 Length
: Natural := 1;
1325 Separator
: Character;
1327 procedure Img10
(V
: Inet_Addr_Comp_Type
);
1328 -- Append to Buffer image of V in decimal format
1330 procedure Img16
(V
: Inet_Addr_Comp_Type
);
1331 -- Append to Buffer image of V in hexadecimal format
1337 procedure Img10
(V
: Inet_Addr_Comp_Type
) is
1338 Img
: constant String := V
'Img;
1339 Len
: constant Natural := Img
'Length - 1;
1341 Buffer
(Length
.. Length
+ Len
- 1) := Img
(2 .. Img
'Last);
1342 Length
:= Length
+ Len
;
1349 procedure Img16
(V
: Inet_Addr_Comp_Type
) is
1351 Buffer
(Length
) := Hex_To_Char
(Natural (V
/ 16) + 1);
1352 Buffer
(Length
+ 1) := Hex_To_Char
(Natural (V
mod 16) + 1);
1353 Length
:= Length
+ 2;
1356 -- Start of processing for Image
1359 Separator
:= (if Hex
then ':' else '.');
1361 for J
in Val
'Range loop
1368 if J
/= Val
'Last then
1369 Buffer
(Length
) := Separator
;
1370 Length
:= Length
+ 1;
1374 return Buffer
(1 .. Length
- 1);
1381 function Image
(Value
: Inet_Addr_Type
) return String is
1383 if Value
.Family
= Family_Inet
then
1384 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V4
), Hex
=> False);
1386 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V6
), Hex
=> True);
1394 function Image
(Value
: Sock_Addr_Type
) return String is
1395 Port
: constant String := Value
.Port
'Img;
1397 return Image
(Value
.Addr
) & ':' & Port
(2 .. Port
'Last);
1404 function Image
(Socket
: Socket_Type
) return String is
1413 function Image
(Item
: Socket_Set_Type
) return String is
1414 Socket_Set
: Socket_Set_Type
:= Item
;
1418 Last_Img
: constant String := Socket_Set
.Last
'Img;
1420 (1 .. (Integer (Socket_Set
.Last
) + 1) * Last_Img
'Length);
1421 Index
: Positive := 1;
1422 Socket
: Socket_Type
;
1425 while not Is_Empty
(Socket_Set
) loop
1426 Get
(Socket_Set
, Socket
);
1429 Socket_Img
: constant String := Socket
'Img;
1431 Buffer
(Index
.. Index
+ Socket_Img
'Length - 1) := Socket_Img
;
1432 Index
:= Index
+ Socket_Img
'Length;
1436 return "[" & Last_Img
& "]" & Buffer
(1 .. Index
- 1);
1444 function Inet_Addr
(Image
: String) return Inet_Addr_Type
is
1447 Img
: aliased char_array
:= To_C
(Image
);
1448 Addr
: aliased C
.int
;
1450 Result
: Inet_Addr_Type
;
1453 -- Special case for an empty Image as on some platforms (e.g. Windows)
1454 -- calling Inet_Addr("") will not return an error.
1457 Raise_Socket_Error
(SOSC
.EINVAL
);
1460 Res
:= Inet_Pton
(SOSC
.AF_INET
, Img
'Address, Addr
'Address);
1463 Raise_Socket_Error
(Socket_Errno
);
1466 Raise_Socket_Error
(SOSC
.EINVAL
);
1469 To_Inet_Addr
(To_In_Addr
(Addr
), Result
);
1477 procedure Initialize
(X
: in out Sockets_Library_Controller
) is
1478 pragma Unreferenced
(X
);
1488 procedure Initialize
(Process_Blocking_IO
: Boolean) is
1489 Expected
: constant Boolean := not SOSC
.Thread_Blocking_IO
;
1492 if Process_Blocking_IO
/= Expected
then
1493 raise Socket_Error
with
1494 "incorrect Process_Blocking_IO setting, expected " & Expected
'Img;
1497 -- This is a dummy placeholder for an obsolete API
1499 -- Real initialization actions are in Initialize primitive operation
1500 -- of Sockets_Library_Controller.
1509 procedure Initialize
is
1511 -- This is a dummy placeholder for an obsolete API
1513 -- Real initialization actions are in Initialize primitive operation
1514 -- of Sockets_Library_Controller.
1523 function Is_Empty
(Item
: Socket_Set_Type
) return Boolean is
1525 return Item
.Last
= No_Socket
;
1532 function Is_IP_Address
(Name
: String) return Boolean is
1533 Dots
: Natural := 0;
1536 -- Perform a cursory check for a dotted quad: we must have 1 to 3 dots,
1537 -- and there must be at least one digit around each.
1539 for J
in Name
'Range loop
1540 if Name
(J
) = '.' then
1542 -- Check that the dot is not in first or last position, and that
1543 -- it is followed by a digit. Note that we already know that it is
1544 -- preceded by a digit, or we would have returned earlier on.
1546 if J
in Name
'First + 1 .. Name
'Last - 1
1547 and then Name
(J
+ 1) in '0' .. '9'
1551 -- Definitely not a proper dotted quad
1557 elsif Name
(J
) not in '0' .. '9' then
1562 return Dots
in 1 .. 3;
1569 function Is_Open
(S
: Selector_Type
) return Boolean is
1575 -- Either both controlling socket descriptors are valid (case of an
1576 -- open selector) or neither (case of a closed selector).
1578 pragma Assert
((S
.R_Sig_Socket
/= No_Socket
)
1580 (S
.W_Sig_Socket
/= No_Socket
));
1582 return S
.R_Sig_Socket
/= No_Socket
;
1591 (Item
: Socket_Set_Type
;
1592 Socket
: Socket_Type
) return Boolean
1595 Check_For_Fd_Set
(Socket
);
1597 return Item
.Last
/= No_Socket
1598 and then Socket
<= Item
.Last
1599 and then Is_Socket_In_Set
(Item
.Set
'Access, C
.int
(Socket
)) /= 0;
1606 procedure Listen_Socket
1607 (Socket
: Socket_Type
;
1608 Length
: Natural := 15)
1610 Res
: constant C
.int
:= C_Listen
(C
.int
(Socket
), C
.int
(Length
));
1612 if Res
= Failure
then
1613 Raise_Socket_Error
(Socket_Errno
);
1621 procedure Narrow
(Item
: in out Socket_Set_Type
) is
1622 Last
: aliased C
.int
:= C
.int
(Item
.Last
);
1624 if Item
.Last
/= No_Socket
then
1625 Last_Socket_In_Set
(Item
.Set
'Access, Last
'Unchecked_Access);
1626 Item
.Last
:= Socket_Type
(Last
);
1634 procedure Netdb_Lock
is
1636 if Need_Netdb_Lock
then
1637 System
.Task_Lock
.Lock
;
1645 procedure Netdb_Unlock
is
1647 if Need_Netdb_Lock
then
1648 System
.Task_Lock
.Unlock
;
1652 --------------------------------
1653 -- Normalize_Empty_Socket_Set --
1654 --------------------------------
1656 procedure Normalize_Empty_Socket_Set
(S
: in out Socket_Set_Type
) is
1658 if S
.Last
= No_Socket
then
1659 Reset_Socket_Set
(S
.Set
'Access);
1661 end Normalize_Empty_Socket_Set
;
1667 function Official_Name
(E
: Host_Entry_Type
) return String is
1669 return To_String
(E
.Official
);
1676 function Official_Name
(S
: Service_Entry_Type
) return String is
1678 return To_String
(S
.Official
);
1681 --------------------
1682 -- Wait_On_Socket --
1683 --------------------
1685 procedure Wait_On_Socket
1686 (Socket
: Socket_Type
;
1688 Timeout
: Selector_Duration
;
1689 Selector
: access Selector_Type
:= null;
1690 Status
: out Selector_Status
)
1692 type Local_Selector_Access
is access Selector_Type
;
1693 for Local_Selector_Access
'Storage_Size use Selector_Type
'Size;
1695 S
: Selector_Access
;
1696 -- Selector to use for waiting
1698 R_Fd_Set
: Socket_Set_Type
;
1699 W_Fd_Set
: Socket_Set_Type
;
1702 -- Create selector if not provided by the user
1704 if Selector
= null then
1706 Local_S
: constant Local_Selector_Access
:= new Selector_Type
;
1708 S
:= Local_S
.all'Unchecked_Access;
1709 Create_Selector
(S
.all);
1713 S
:= Selector
.all'Access;
1717 Set
(R_Fd_Set
, Socket
);
1719 Set
(W_Fd_Set
, Socket
);
1722 Check_Selector
(S
.all, R_Fd_Set
, W_Fd_Set
, Status
, Timeout
);
1724 if Selector
= null then
1725 Close_Selector
(S
.all);
1733 function Port_Number
(S
: Service_Entry_Type
) return Port_Type
is
1742 function Protocol_Name
(S
: Service_Entry_Type
) return String is
1744 return To_String
(S
.Protocol
);
1747 ----------------------
1748 -- Raise_Host_Error --
1749 ----------------------
1751 procedure Raise_Host_Error
(H_Error
: Integer; Name
: String) is
1752 function Dedot
(Value
: String) return String is
1753 (if Value
/= "" and then Value
(Value
'Last) = '.' then
1754 Value
(Value
'First .. Value
'Last - 1)
1757 -- Removes dot at the end of error message
1760 raise Host_Error
with
1761 Err_Code_Image
(H_Error
)
1762 & Dedot
(Host_Error_Messages
.Host_Error_Message
(H_Error
))
1764 end Raise_Host_Error
;
1766 ------------------------
1767 -- Raise_Socket_Error --
1768 ------------------------
1770 procedure Raise_Socket_Error
(Error
: Integer) is
1772 raise Socket_Error
with
1773 Err_Code_Image
(Error
) & Socket_Error_Message
(Error
);
1774 end Raise_Socket_Error
;
1781 (Stream
: in out Datagram_Socket_Stream_Type
;
1782 Item
: out Ada
.Streams
.Stream_Element_Array
;
1783 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1798 (Stream
: in out Stream_Socket_Stream_Type
;
1799 Item
: out Ada
.Streams
.Stream_Element_Array
;
1800 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1802 First
: Ada
.Streams
.Stream_Element_Offset
:= Item
'First;
1803 Index
: Ada
.Streams
.Stream_Element_Offset
:= First
- 1;
1804 Max
: constant Ada
.Streams
.Stream_Element_Offset
:= Item
'Last;
1808 Receive_Socket
(Stream
.Socket
, Item
(First
.. Max
), Index
);
1811 -- Exit when all or zero data received. Zero means that the socket
1814 exit when Index
< First
or else Index
= Max
;
1820 --------------------
1821 -- Receive_Socket --
1822 --------------------
1824 procedure Receive_Socket
1825 (Socket
: Socket_Type
;
1826 Item
: out Ada
.Streams
.Stream_Element_Array
;
1827 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1828 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1834 C_Recv
(C
.int
(Socket
), Item
'Address, Item
'Length, To_Int
(Flags
));
1836 if Res
= Failure
then
1837 Raise_Socket_Error
(Socket_Errno
);
1840 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
1843 --------------------
1844 -- Receive_Socket --
1845 --------------------
1847 procedure Receive_Socket
1848 (Socket
: Socket_Type
;
1849 Item
: out Ada
.Streams
.Stream_Element_Array
;
1850 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1851 From
: out Sock_Addr_Type
;
1852 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1855 Sin
: aliased Sockaddr_In
;
1856 Len
: aliased C
.int
:= Sin
'Size / 8;
1868 if Res
= Failure
then
1869 Raise_Socket_Error
(Socket_Errno
);
1872 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
1874 To_Inet_Addr
(Sin
.Sin_Addr
, From
.Addr
);
1875 From
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1878 --------------------
1879 -- Receive_Vector --
1880 --------------------
1882 procedure Receive_Vector
1883 (Socket
: Socket_Type
;
1884 Vector
: Vector_Type
;
1885 Count
: out Ada
.Streams
.Stream_Element_Count
;
1886 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1891 (Msg_Name
=> System
.Null_Address
,
1893 Msg_Iov
=> Vector
'Address,
1895 -- recvmsg(2) returns EMSGSIZE on Linux (and probably on other
1896 -- platforms) when the supplied vector is longer than IOV_MAX,
1897 -- so use minimum of the two lengths.
1899 Msg_Iovlen
=> SOSC
.Msg_Iovlen_T
'Min
1900 (Vector
'Length, SOSC
.IOV_MAX
),
1902 Msg_Control
=> System
.Null_Address
,
1903 Msg_Controllen
=> 0,
1913 if Res
= ssize_t
(Failure
) then
1914 Raise_Socket_Error
(Socket_Errno
);
1917 Count
:= Ada
.Streams
.Stream_Element_Count
(Res
);
1924 function Resolve_Error
1925 (Error_Value
: Integer;
1926 From_Errno
: Boolean := True) return Error_Type
1928 use GNAT
.Sockets
.SOSC
;
1931 if not From_Errno
then
1933 when SOSC
.HOST_NOT_FOUND
=> return Unknown_Host
;
1934 when SOSC
.TRY_AGAIN
=> return Host_Name_Lookup_Failure
;
1935 when SOSC
.NO_RECOVERY
=> return Non_Recoverable_Error
;
1936 when SOSC
.NO_DATA
=> return Unknown_Server_Error
;
1937 when others => return Cannot_Resolve_Error
;
1941 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1942 -- can't include it in the case statement below.
1944 pragma Warnings
(Off
);
1945 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1947 if EAGAIN
/= EWOULDBLOCK
and then Error_Value
= EAGAIN
then
1948 return Resource_Temporarily_Unavailable
;
1951 -- This is not a case statement because if a particular error
1952 -- number constant is not defined, s-oscons-tmplt.c defines
1953 -- it to -1. If multiple constants are not defined, they
1954 -- would each be -1 and result in a "duplicate value in case" error.
1956 -- But we have to leave warnings off because the compiler is also
1957 -- smart enough to note that when two errnos have the same value,
1958 -- the second if condition is useless.
1959 if Error_Value
= ENOERROR
then
1961 elsif Error_Value
= EACCES
then
1962 return Permission_Denied
;
1963 elsif Error_Value
= EADDRINUSE
then
1964 return Address_Already_In_Use
;
1965 elsif Error_Value
= EADDRNOTAVAIL
then
1966 return Cannot_Assign_Requested_Address
;
1967 elsif Error_Value
= EAFNOSUPPORT
then
1968 return Address_Family_Not_Supported_By_Protocol
;
1969 elsif Error_Value
= EALREADY
then
1970 return Operation_Already_In_Progress
;
1971 elsif Error_Value
= EBADF
then
1972 return Bad_File_Descriptor
;
1973 elsif Error_Value
= ECONNABORTED
then
1974 return Software_Caused_Connection_Abort
;
1975 elsif Error_Value
= ECONNREFUSED
then
1976 return Connection_Refused
;
1977 elsif Error_Value
= ECONNRESET
then
1978 return Connection_Reset_By_Peer
;
1979 elsif Error_Value
= EDESTADDRREQ
then
1980 return Destination_Address_Required
;
1981 elsif Error_Value
= EFAULT
then
1983 elsif Error_Value
= EHOSTDOWN
then
1984 return Host_Is_Down
;
1985 elsif Error_Value
= EHOSTUNREACH
then
1986 return No_Route_To_Host
;
1987 elsif Error_Value
= EINPROGRESS
then
1988 return Operation_Now_In_Progress
;
1989 elsif Error_Value
= EINTR
then
1990 return Interrupted_System_Call
;
1991 elsif Error_Value
= EINVAL
then
1992 return Invalid_Argument
;
1993 elsif Error_Value
= EIO
then
1994 return Input_Output_Error
;
1995 elsif Error_Value
= EISCONN
then
1996 return Transport_Endpoint_Already_Connected
;
1997 elsif Error_Value
= ELOOP
then
1998 return Too_Many_Symbolic_Links
;
1999 elsif Error_Value
= EMFILE
then
2000 return Too_Many_Open_Files
;
2001 elsif Error_Value
= EMSGSIZE
then
2002 return Message_Too_Long
;
2003 elsif Error_Value
= ENAMETOOLONG
then
2004 return File_Name_Too_Long
;
2005 elsif Error_Value
= ENETDOWN
then
2006 return Network_Is_Down
;
2007 elsif Error_Value
= ENETRESET
then
2008 return Network_Dropped_Connection_Because_Of_Reset
;
2009 elsif Error_Value
= ENETUNREACH
then
2010 return Network_Is_Unreachable
;
2011 elsif Error_Value
= ENOBUFS
then
2012 return No_Buffer_Space_Available
;
2013 elsif Error_Value
= ENOPROTOOPT
then
2014 return Protocol_Not_Available
;
2015 elsif Error_Value
= ENOTCONN
then
2016 return Transport_Endpoint_Not_Connected
;
2017 elsif Error_Value
= ENOTSOCK
then
2018 return Socket_Operation_On_Non_Socket
;
2019 elsif Error_Value
= EOPNOTSUPP
then
2020 return Operation_Not_Supported
;
2021 elsif Error_Value
= EPFNOSUPPORT
then
2022 return Protocol_Family_Not_Supported
;
2023 elsif Error_Value
= EPIPE
then
2025 elsif Error_Value
= EPROTONOSUPPORT
then
2026 return Protocol_Not_Supported
;
2027 elsif Error_Value
= EPROTOTYPE
then
2028 return Protocol_Wrong_Type_For_Socket
;
2029 elsif Error_Value
= ESHUTDOWN
then
2030 return Cannot_Send_After_Transport_Endpoint_Shutdown
;
2031 elsif Error_Value
= ESOCKTNOSUPPORT
then
2032 return Socket_Type_Not_Supported
;
2033 elsif Error_Value
= ETIMEDOUT
then
2034 return Connection_Timed_Out
;
2035 elsif Error_Value
= ETOOMANYREFS
then
2036 return Too_Many_References
;
2037 elsif Error_Value
= EWOULDBLOCK
then
2038 return Resource_Temporarily_Unavailable
;
2040 return Cannot_Resolve_Error
;
2042 pragma Warnings
(On
);
2046 -----------------------
2047 -- Resolve_Exception --
2048 -----------------------
2050 function Resolve_Exception
2051 (Occurrence
: Exception_Occurrence
) return Error_Type
2053 Id
: constant Exception_Id
:= Exception_Identity
(Occurrence
);
2054 Msg
: constant String := Exception_Message
(Occurrence
);
2061 while First
<= Msg
'Last
2062 and then Msg
(First
) not in '0' .. '9'
2067 if First
> Msg
'Last then
2068 return Cannot_Resolve_Error
;
2072 while Last
< Msg
'Last
2073 and then Msg
(Last
+ 1) in '0' .. '9'
2078 Val
:= Integer'Value (Msg
(First
.. Last
));
2080 if Id
= Socket_Error_Id
then
2081 return Resolve_Error
(Val
);
2083 elsif Id
= Host_Error_Id
then
2084 return Resolve_Error
(Val
, False);
2087 return Cannot_Resolve_Error
;
2089 end Resolve_Exception
;
2095 procedure Send_Socket
2096 (Socket
: Socket_Type
;
2097 Item
: Ada
.Streams
.Stream_Element_Array
;
2098 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2099 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2102 Send_Socket
(Socket
, Item
, Last
, To
=> null, Flags
=> Flags
);
2109 procedure Send_Socket
2110 (Socket
: Socket_Type
;
2111 Item
: Ada
.Streams
.Stream_Element_Array
;
2112 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2113 To
: Sock_Addr_Type
;
2114 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2118 (Socket
, Item
, Last
, To
=> To
'Unrestricted_Access, Flags
=> Flags
);
2125 procedure Send_Socket
2126 (Socket
: Socket_Type
;
2127 Item
: Ada
.Streams
.Stream_Element_Array
;
2128 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2129 To
: access Sock_Addr_Type
;
2130 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2134 Sin
: aliased Sockaddr_In
;
2135 C_To
: System
.Address
;
2140 Set_Family
(Sin
.Sin_Family
, To
.Family
);
2141 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(To
.Addr
));
2143 (Sin
'Unchecked_Access,
2144 Short_To_Network
(C
.unsigned_short
(To
.Port
)));
2145 C_To
:= Sin
'Address;
2146 Len
:= Sin
'Size / 8;
2149 C_To
:= System
.Null_Address
;
2157 Set_Forced_Flags
(To_Int
(Flags
)),
2161 if Res
= Failure
then
2162 Raise_Socket_Error
(Socket_Errno
);
2165 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
2172 procedure Send_Vector
2173 (Socket
: Socket_Type
;
2174 Vector
: Vector_Type
;
2175 Count
: out Ada
.Streams
.Stream_Element_Count
;
2176 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2182 Iov_Count
: SOSC
.Msg_Iovlen_T
;
2183 This_Iov_Count
: SOSC
.Msg_Iovlen_T
;
2189 while Iov_Count
< Vector
'Length loop
2191 pragma Warnings
(Off
);
2192 -- Following test may be compile time known on some targets
2195 (if Vector
'Length - Iov_Count
> SOSC
.IOV_MAX
2197 else Vector
'Length - Iov_Count
);
2199 pragma Warnings
(On
);
2202 (Msg_Name
=> System
.Null_Address
,
2205 (Vector
'First + Integer (Iov_Count
))'Address,
2206 Msg_Iovlen
=> This_Iov_Count
,
2207 Msg_Control
=> System
.Null_Address
,
2208 Msg_Controllen
=> 0,
2215 Set_Forced_Flags
(To_Int
(Flags
)));
2217 if Res
= ssize_t
(Failure
) then
2218 Raise_Socket_Error
(Socket_Errno
);
2221 Count
:= Count
+ Ada
.Streams
.Stream_Element_Count
(Res
);
2222 Iov_Count
:= Iov_Count
+ This_Iov_Count
;
2230 procedure Set
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
) is
2232 Check_For_Fd_Set
(Socket
);
2234 if Item
.Last
= No_Socket
then
2236 -- Uninitialized socket set, make sure it is properly zeroed out
2238 Reset_Socket_Set
(Item
.Set
'Access);
2239 Item
.Last
:= Socket
;
2241 elsif Item
.Last
< Socket
then
2242 Item
.Last
:= Socket
;
2245 Insert_Socket_In_Set
(Item
.Set
'Access, C
.int
(Socket
));
2248 -----------------------
2249 -- Set_Close_On_Exec --
2250 -----------------------
2252 procedure Set_Close_On_Exec
2253 (Socket
: Socket_Type
;
2254 Close_On_Exec
: Boolean;
2255 Status
: out Boolean)
2257 function C_Set_Close_On_Exec
2258 (Socket
: Socket_Type
; Close_On_Exec
: C
.int
) return C
.int
;
2259 pragma Import
(C
, C_Set_Close_On_Exec
, "__gnat_set_close_on_exec");
2261 Status
:= C_Set_Close_On_Exec
(Socket
, Boolean'Pos (Close_On_Exec
)) = 0;
2262 end Set_Close_On_Exec
;
2264 ----------------------
2265 -- Set_Forced_Flags --
2266 ----------------------
2268 function Set_Forced_Flags
(F
: C
.int
) return C
.int
is
2269 use type C
.unsigned
;
2270 function To_unsigned
is
2271 new Ada
.Unchecked_Conversion
(C
.int
, C
.unsigned
);
2273 new Ada
.Unchecked_Conversion
(C
.unsigned
, C
.int
);
2275 return To_int
(To_unsigned
(F
) or SOSC
.MSG_Forced_Flags
);
2276 end Set_Forced_Flags
;
2278 -----------------------
2279 -- Set_Socket_Option --
2280 -----------------------
2282 procedure Set_Socket_Option
2283 (Socket
: Socket_Type
;
2284 Level
: Level_Type
:= Socket_Level
;
2285 Option
: Option_Type
)
2289 V8
: aliased Two_Ints
;
2291 V1
: aliased C
.unsigned_char
;
2292 VT
: aliased Timeval
;
2294 Add
: System
.Address
:= Null_Address
;
2300 when Generic_Option
=>
2301 V4
:= Option
.Optval
;
2310 V4
:= C
.int
(Boolean'Pos (Option
.Enabled
));
2314 when Busy_Polling
=>
2315 V4
:= C
.int
(Option
.Microseconds
);
2320 V8
(V8
'First) := C
.int
(Boolean'Pos (Option
.Enabled
));
2321 V8
(V8
'Last) := C
.int
(Option
.Seconds
);
2328 V4
:= C
.int
(Option
.Size
);
2333 V4
:= C
.int
(Boolean'Pos (True));
2340 V8
(V8
'First) := To_Int
(To_In_Addr
(Option
.Multicast_Address
));
2341 V8
(V8
'Last) := To_Int
(To_In_Addr
(Option
.Local_Interface
));
2345 when Multicast_If
=>
2346 V4
:= To_Int
(To_In_Addr
(Option
.Outgoing_If
));
2350 when Multicast_TTL
=>
2351 V1
:= C
.unsigned_char
(Option
.Time_To_Live
);
2356 | Receive_Packet_Info
2358 V1
:= C
.unsigned_char
(Boolean'Pos (Option
.Enabled
));
2362 when Receive_Timeout
2365 if Target_OS
= Windows
then
2367 -- On Windows, the timeout is a DWORD in milliseconds, and
2368 -- the actual timeout is 500 ms + the given value (unless it
2371 V4
:= C
.int
(Option
.Timeout
/ 0.001);
2384 VT
:= To_Timeval
(Option
.Timeout
);
2390 if Option
.Name
in Specific_Option_Name
then
2391 Onm
:= Options
(Option
.Name
);
2393 elsif Option
.Optname
= -1 then
2394 raise Socket_Error
with "optname must be specified";
2397 Onm
:= Option
.Optname
;
2406 if Res
= Failure
then
2407 Raise_Socket_Error
(Socket_Errno
);
2409 end Set_Socket_Option
;
2411 ----------------------
2412 -- Short_To_Network --
2413 ----------------------
2415 function Short_To_Network
(S
: C
.unsigned_short
) return C
.unsigned_short
is
2416 use type C
.unsigned_short
;
2419 -- Big-endian case. No conversion needed. On these platforms, htons()
2420 -- defaults to a null procedure.
2422 if Default_Bit_Order
= High_Order_First
then
2425 -- Little-endian case. We must swap the high and low bytes of this
2426 -- short to make the port number network compliant.
2429 return (S
/ 256) + (S
mod 256) * 256;
2431 end Short_To_Network
;
2433 ---------------------
2434 -- Shutdown_Socket --
2435 ---------------------
2437 procedure Shutdown_Socket
2438 (Socket
: Socket_Type
;
2439 How
: Shutmode_Type
:= Shut_Read_Write
)
2444 Res
:= C_Shutdown
(C
.int
(Socket
), Shutmodes
(How
));
2446 if Res
= Failure
then
2447 Raise_Socket_Error
(Socket_Errno
);
2449 end Shutdown_Socket
;
2456 (Socket
: Socket_Type
;
2457 Send_To
: Sock_Addr_Type
) return Stream_Access
2459 S
: Datagram_Socket_Stream_Access
;
2462 S
:= new Datagram_Socket_Stream_Type
;
2465 S
.From
:= Get_Socket_Name
(Socket
);
2466 return Stream_Access
(S
);
2473 function Stream
(Socket
: Socket_Type
) return Stream_Access
is
2474 S
: Stream_Socket_Stream_Access
;
2476 S
:= new Stream_Socket_Stream_Type
;
2478 return Stream_Access
(S
);
2485 function To_Ada
(Fd
: Integer) return Socket_Type
is
2487 return Socket_Type
(Fd
);
2494 function To_C
(Socket
: Socket_Type
) return Integer is
2496 return Integer (Socket
);
2503 function To_Duration
(Val
: Timeval
) return Timeval_Duration
is
2505 return Natural (Val
.Tv_Sec
) * 1.0 + Natural (Val
.Tv_Usec
) * 1.0E-6;
2512 function To_Host_Entry
(E
: Hostent_Access
) return Host_Entry_Type
is
2515 Aliases_Count
, Addresses_Count
: Natural;
2517 -- H_Length is not used because it is currently only ever set to 4, as
2518 -- we only handle the case of H_Addrtype being AF_INET.
2521 if Hostent_H_Addrtype
(E
) /= SOSC
.AF_INET
then
2522 Raise_Socket_Error
(SOSC
.EPFNOSUPPORT
);
2526 while Hostent_H_Alias
(E
, C
.int
(Aliases_Count
)) /= Null_Address
loop
2527 Aliases_Count
:= Aliases_Count
+ 1;
2530 Addresses_Count
:= 0;
2531 while Hostent_H_Addr
(E
, C
.int
(Addresses_Count
)) /= Null_Address
loop
2532 Addresses_Count
:= Addresses_Count
+ 1;
2535 return Result
: Host_Entry_Type
2536 (Aliases_Length
=> Aliases_Count
,
2537 Addresses_Length
=> Addresses_Count
)
2539 Result
.Official
:= To_Name
(Value
(Hostent_H_Name
(E
)));
2541 for J
in Result
.Aliases
'Range loop
2542 Result
.Aliases
(J
) :=
2543 To_Name
(Value
(Hostent_H_Alias
2544 (E
, C
.int
(J
- Result
.Aliases
'First))));
2547 for J
in Result
.Addresses
'Range loop
2551 -- Hostent_H_Addr (E, <index>) may return an address that is
2552 -- not correctly aligned for In_Addr, so we need to use
2553 -- an intermediate copy operation on a type with an alignment
2554 -- of 1 to recover the value.
2556 subtype Addr_Buf_T
is C
.char_array
(1 .. Addr
'Size / 8);
2557 Unaligned_Addr
: Addr_Buf_T
;
2558 for Unaligned_Addr
'Address
2559 use Hostent_H_Addr
(E
, C
.int
(J
- Result
.Addresses
'First));
2560 pragma Import
(Ada
, Unaligned_Addr
);
2562 Aligned_Addr
: Addr_Buf_T
;
2563 for Aligned_Addr
'Address use Addr
'Address;
2564 pragma Import
(Ada
, Aligned_Addr
);
2567 Aligned_Addr
:= Unaligned_Addr
;
2568 To_Inet_Addr
(Addr
, Result
.Addresses
(J
));
2578 function To_In_Addr
(Addr
: Inet_Addr_Type
) return In_Addr
is
2580 if Addr
.Family
= Family_Inet
then
2581 return (S_B1
=> C
.unsigned_char
(Addr
.Sin_V4
(1)),
2582 S_B2
=> C
.unsigned_char
(Addr
.Sin_V4
(2)),
2583 S_B3
=> C
.unsigned_char
(Addr
.Sin_V4
(3)),
2584 S_B4
=> C
.unsigned_char
(Addr
.Sin_V4
(4)));
2587 raise Socket_Error
with "IPv6 not supported";
2594 procedure To_Inet_Addr
2596 Result
: out Inet_Addr_Type
) is
2598 Result
.Sin_V4
(1) := Inet_Addr_Comp_Type
(Addr
.S_B1
);
2599 Result
.Sin_V4
(2) := Inet_Addr_Comp_Type
(Addr
.S_B2
);
2600 Result
.Sin_V4
(3) := Inet_Addr_Comp_Type
(Addr
.S_B3
);
2601 Result
.Sin_V4
(4) := Inet_Addr_Comp_Type
(Addr
.S_B4
);
2608 function To_Int
(F
: Request_Flag_Type
) return C
.int
2610 Current
: Request_Flag_Type
:= F
;
2611 Result
: C
.int
:= 0;
2614 for J
in Flags
'Range loop
2615 exit when Current
= 0;
2617 if Current
mod 2 /= 0 then
2618 if Flags
(J
) = -1 then
2619 Raise_Socket_Error
(SOSC
.EOPNOTSUPP
);
2622 Result
:= Result
+ Flags
(J
);
2625 Current
:= Current
/ 2;
2635 function To_Name
(N
: String) return Name_Type
is
2637 return Name_Type
'(N'Length, N);
2640 ----------------------
2641 -- To_Service_Entry --
2642 ----------------------
2644 function To_Service_Entry (E : Servent_Access) return Service_Entry_Type is
2645 Aliases_Count : Natural;
2649 while Servent_S_Alias (E, C.int (Aliases_Count)) /= Null_Address loop
2650 Aliases_Count := Aliases_Count + 1;
2653 return Result : Service_Entry_Type (Aliases_Length => Aliases_Count) do
2654 Result.Official := To_Name (Value (Servent_S_Name (E)));
2656 for J in Result.Aliases'Range loop
2657 Result.Aliases (J) :=
2658 To_Name (Value (Servent_S_Alias
2659 (E, C.int (J - Result.Aliases'First))));
2662 Result.Protocol := To_Name (Value (Servent_S_Proto (E)));
2664 Port_Type (Network_To_Short (Servent_S_Port (E)));
2666 end To_Service_Entry;
2672 function To_String (HN : Name_Type) return String is
2674 return HN.Name (1 .. HN.Length);
2681 function To_Timeval (Val : Timeval_Duration) return Timeval is
2686 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2692 -- Normal case where we do round down
2695 S := time_t (Val - 0.5);
2696 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2706 function Value (S : System.Address) return String is
2707 Str : String (1 .. Positive'Last);
2708 for Str'Address use S;
2709 pragma Import (Ada, Str);
2711 Terminator : Positive := Str'First;
2714 while Str (Terminator) /= ASCII.NUL loop
2715 Terminator := Terminator + 1;
2718 return Str (1 .. Terminator - 1);
2726 (Stream : in out Datagram_Socket_Stream_Type;
2727 Item : Ada.Streams.Stream_Element_Array)
2729 Last : Stream_Element_Offset;
2738 -- It is an error if not all of the data has been sent
2740 if Last /= Item'Last then
2741 Raise_Socket_Error (Socket_Errno);
2750 (Stream : in out Stream_Socket_Stream_Type;
2751 Item : Ada.Streams.Stream_Element_Array)
2753 First : Ada.Streams.Stream_Element_Offset;
2754 Index : Ada.Streams.Stream_Element_Offset;
2755 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2758 First := Item'First;
2760 while First <= Max loop
2761 Send_Socket (Stream.Socket, Item (First .. Max), Index, null);
2763 -- Exit when all or zero data sent. Zero means that the socket has
2764 -- been closed by peer.
2766 exit when Index < First or else Index = Max;
2771 -- For an empty array, we have First > Max, and hence Index >= Max (no
2772 -- error, the loop above is never executed). After a successful send,
2773 -- Index = Max. The only remaining case, Index < Max, is therefore
2774 -- always an actual send failure.
2777 Raise_Socket_Error (Socket_Errno);
2781 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2782 pragma Unreferenced (Sockets_Library_Controller_Object);
2783 -- The elaboration and finalization of this object perform the required
2784 -- initialization and cleanup actions for the sockets library.