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 (on VMS,
176 -- chars_ptr is a 32-bit pointer, and here we need a 64-bit version).
178 function To_Timeval
(Val
: Timeval_Duration
) return Timeval
;
179 -- Separate Val in seconds and microseconds
181 function To_Duration
(Val
: Timeval
) return Timeval_Duration
;
182 -- Reconstruct a Duration value from a Timeval record (seconds and
185 procedure Raise_Socket_Error
(Error
: Integer);
186 -- Raise Socket_Error with an exception message describing the error code
189 procedure Raise_Host_Error
(H_Error
: Integer);
190 -- Raise Host_Error exception with message describing error code (note
191 -- hstrerror seems to be obsolete) from h_errno.
193 procedure Narrow
(Item
: in out Socket_Set_Type
);
194 -- Update Last as it may be greater than the real last socket
196 procedure Check_For_Fd_Set
(Fd
: Socket_Type
);
197 pragma Inline
(Check_For_Fd_Set
);
198 -- Raise Constraint_Error if Fd is less than 0 or greater than or equal to
199 -- FD_SETSIZE, on platforms where fd_set is a bitmap.
201 function Connect_Socket
202 (Socket
: Socket_Type
;
203 Server
: Sock_Addr_Type
) return C
.int
;
204 pragma Inline
(Connect_Socket
);
205 -- Underlying implementation for the Connect_Socket procedures
207 -- Types needed for Datagram_Socket_Stream_Type
209 type Datagram_Socket_Stream_Type
is new Root_Stream_Type
with record
210 Socket
: Socket_Type
;
212 From
: Sock_Addr_Type
;
215 type Datagram_Socket_Stream_Access
is
216 access all Datagram_Socket_Stream_Type
;
219 (Stream
: in out Datagram_Socket_Stream_Type
;
220 Item
: out Ada
.Streams
.Stream_Element_Array
;
221 Last
: out Ada
.Streams
.Stream_Element_Offset
);
224 (Stream
: in out Datagram_Socket_Stream_Type
;
225 Item
: Ada
.Streams
.Stream_Element_Array
);
227 -- Types needed for Stream_Socket_Stream_Type
229 type Stream_Socket_Stream_Type
is new Root_Stream_Type
with record
230 Socket
: Socket_Type
;
233 type Stream_Socket_Stream_Access
is
234 access all Stream_Socket_Stream_Type
;
237 (Stream
: in out Stream_Socket_Stream_Type
;
238 Item
: out Ada
.Streams
.Stream_Element_Array
;
239 Last
: out Ada
.Streams
.Stream_Element_Offset
);
242 (Stream
: in out Stream_Socket_Stream_Type
;
243 Item
: Ada
.Streams
.Stream_Element_Array
);
245 procedure Wait_On_Socket
246 (Socket
: Socket_Type
;
248 Timeout
: Selector_Duration
;
249 Selector
: access Selector_Type
:= null;
250 Status
: out Selector_Status
);
251 -- Common code for variants of socket operations supporting a timeout:
252 -- block in Check_Selector on Socket for at most the indicated timeout.
253 -- If For_Read is True, Socket is added to the read set for this call, else
254 -- it is added to the write set. If no selector is provided, a local one is
255 -- created for this call and destroyed prior to returning.
257 type Sockets_Library_Controller
is new Ada
.Finalization
.Limited_Controlled
259 -- This type is used to generate automatic calls to Initialize and Finalize
260 -- during the elaboration and finalization of this package. A single object
261 -- of this type must exist at library level.
263 function Err_Code_Image
(E
: Integer) return String;
264 -- Return the value of E surrounded with brackets
266 procedure Initialize
(X
: in out Sockets_Library_Controller
);
267 procedure Finalize
(X
: in out Sockets_Library_Controller
);
269 procedure Normalize_Empty_Socket_Set
(S
: in out Socket_Set_Type
);
270 -- If S is the empty set (detected by Last = No_Socket), make sure its
271 -- fd_set component is actually cleared. Note that the case where it is
272 -- not can occur for an uninitialized Socket_Set_Type object.
274 function Is_Open
(S
: Selector_Type
) return Boolean;
275 -- Return True for an "open" Selector_Type object, i.e. one for which
276 -- Create_Selector has been called and Close_Selector has not been called,
277 -- or the null selector.
283 function "+" (L
, R
: Request_Flag_Type
) return Request_Flag_Type
is
292 procedure Abort_Selector
(Selector
: Selector_Type
) is
296 if not Is_Open
(Selector
) then
297 raise Program_Error
with "closed selector";
299 elsif Selector
.Is_Null
then
300 raise Program_Error
with "null selector";
304 -- Send one byte to unblock select system call
306 Res
:= Signalling_Fds
.Write
(C
.int
(Selector
.W_Sig_Socket
));
308 if Res
= Failure
then
309 Raise_Socket_Error
(Socket_Errno
);
317 procedure Accept_Socket
318 (Server
: Socket_Type
;
319 Socket
: out Socket_Type
;
320 Address
: out Sock_Addr_Type
)
323 Sin
: aliased Sockaddr_In
;
324 Len
: aliased C
.int
:= Sin
'Size / 8;
327 Res
:= C_Accept
(C
.int
(Server
), Sin
'Address, Len
'Access);
329 if Res
= Failure
then
330 Raise_Socket_Error
(Socket_Errno
);
333 Socket
:= Socket_Type
(Res
);
335 To_Inet_Addr
(Sin
.Sin_Addr
, Address
.Addr
);
336 Address
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
343 procedure Accept_Socket
344 (Server
: Socket_Type
;
345 Socket
: out Socket_Type
;
346 Address
: out Sock_Addr_Type
;
347 Timeout
: Selector_Duration
;
348 Selector
: access Selector_Type
:= null;
349 Status
: out Selector_Status
)
352 if Selector
/= null and then not Is_Open
(Selector
.all) then
353 raise Program_Error
with "closed selector";
356 -- Wait for socket to become available for reading
362 Selector
=> Selector
,
365 -- Accept connection if available
367 if Status
= Completed
then
368 Accept_Socket
(Server
, Socket
, Address
);
379 (E
: Host_Entry_Type
;
380 N
: Positive := 1) return Inet_Addr_Type
383 return E
.Addresses
(N
);
386 ----------------------
387 -- Addresses_Length --
388 ----------------------
390 function Addresses_Length
(E
: Host_Entry_Type
) return Natural is
392 return E
.Addresses_Length
;
393 end Addresses_Length
;
400 (E
: Host_Entry_Type
;
401 N
: Positive := 1) return String
404 return To_String
(E
.Aliases
(N
));
412 (S
: Service_Entry_Type
;
413 N
: Positive := 1) return String
416 return To_String
(S
.Aliases
(N
));
423 function Aliases_Length
(E
: Host_Entry_Type
) return Natural is
425 return E
.Aliases_Length
;
432 function Aliases_Length
(S
: Service_Entry_Type
) return Natural is
434 return S
.Aliases_Length
;
441 procedure Bind_Socket
442 (Socket
: Socket_Type
;
443 Address
: Sock_Addr_Type
)
446 Sin
: aliased Sockaddr_In
;
447 Len
: constant C
.int
:= Sin
'Size / 8;
448 -- This assumes that Address.Family = Family_Inet???
451 if Address
.Family
= Family_Inet6
then
452 raise Socket_Error
with "IPv6 not supported";
455 Set_Family
(Sin
.Sin_Family
, Address
.Family
);
456 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(Address
.Addr
));
458 (Sin
'Unchecked_Access,
459 Short_To_Network
(C
.unsigned_short
(Address
.Port
)));
461 Res
:= C_Bind
(C
.int
(Socket
), Sin
'Address, Len
);
463 if Res
= Failure
then
464 Raise_Socket_Error
(Socket_Errno
);
468 ----------------------
469 -- Check_For_Fd_Set --
470 ----------------------
472 procedure Check_For_Fd_Set
(Fd
: Socket_Type
) is
476 -- On Windows, fd_set is a FD_SETSIZE array of socket ids:
477 -- no check required. Warnings suppressed because condition
478 -- is known at compile time.
480 if Target_OS
= Windows
then
484 -- On other platforms, fd_set is an FD_SETSIZE bitmap: check
485 -- that Fd is within range (otherwise behaviour is undefined).
487 elsif Fd
< 0 or else Fd
>= SOSC
.FD_SETSIZE
then
488 raise Constraint_Error
489 with "invalid value for socket set: " & Image
(Fd
);
491 end Check_For_Fd_Set
;
497 procedure Check_Selector
498 (Selector
: Selector_Type
;
499 R_Socket_Set
: in out Socket_Set_Type
;
500 W_Socket_Set
: in out Socket_Set_Type
;
501 Status
: out Selector_Status
;
502 Timeout
: Selector_Duration
:= Forever
)
504 E_Socket_Set
: Socket_Set_Type
;
507 (Selector
, R_Socket_Set
, W_Socket_Set
, E_Socket_Set
, Status
, Timeout
);
510 procedure Check_Selector
511 (Selector
: Selector_Type
;
512 R_Socket_Set
: in out Socket_Set_Type
;
513 W_Socket_Set
: in out Socket_Set_Type
;
514 E_Socket_Set
: in out Socket_Set_Type
;
515 Status
: out Selector_Status
;
516 Timeout
: Selector_Duration
:= Forever
)
520 RSig
: Socket_Type
:= No_Socket
;
521 TVal
: aliased Timeval
;
522 TPtr
: Timeval_Access
;
525 if not Is_Open
(Selector
) then
526 raise Program_Error
with "closed selector";
531 -- No timeout or Forever is indicated by a null timeval pointer
533 if Timeout
= Forever
then
536 TVal
:= To_Timeval
(Timeout
);
537 TPtr
:= TVal
'Unchecked_Access;
540 -- Add read signalling socket, if present
542 if not Selector
.Is_Null
then
543 RSig
:= Selector
.R_Sig_Socket
;
544 Set
(R_Socket_Set
, RSig
);
547 Last
:= C
.int
'Max (C
.int
'Max (C
.int
(R_Socket_Set
.Last
),
548 C
.int
(W_Socket_Set
.Last
)),
549 C
.int
(E_Socket_Set
.Last
));
551 -- Zero out fd_set for empty Socket_Set_Type objects
553 Normalize_Empty_Socket_Set
(R_Socket_Set
);
554 Normalize_Empty_Socket_Set
(W_Socket_Set
);
555 Normalize_Empty_Socket_Set
(E_Socket_Set
);
560 R_Socket_Set
.Set
'Access,
561 W_Socket_Set
.Set
'Access,
562 E_Socket_Set
.Set
'Access,
565 if Res
= Failure
then
566 Raise_Socket_Error
(Socket_Errno
);
569 -- If Select was resumed because of read signalling socket, read this
570 -- data and remove socket from set.
572 if RSig
/= No_Socket
and then Is_Set
(R_Socket_Set
, RSig
) then
573 Clear
(R_Socket_Set
, RSig
);
575 Res
:= Signalling_Fds
.Read
(C
.int
(RSig
));
577 if Res
= Failure
then
578 Raise_Socket_Error
(Socket_Errno
);
587 -- Update socket sets in regard to their new contents
589 Narrow
(R_Socket_Set
);
590 Narrow
(W_Socket_Set
);
591 Narrow
(E_Socket_Set
);
599 (Item
: in out Socket_Set_Type
;
600 Socket
: Socket_Type
)
602 Last
: aliased C
.int
:= C
.int
(Item
.Last
);
605 Check_For_Fd_Set
(Socket
);
607 if Item
.Last
/= No_Socket
then
608 Remove_Socket_From_Set
(Item
.Set
'Access, C
.int
(Socket
));
609 Last_Socket_In_Set
(Item
.Set
'Access, Last
'Unchecked_Access);
610 Item
.Last
:= Socket_Type
(Last
);
618 procedure Close_Selector
(Selector
: in out Selector_Type
) is
620 -- Nothing to do if selector already in closed state
622 if Selector
.Is_Null
or else not Is_Open
(Selector
) then
626 -- Close the signalling file descriptors used internally for the
627 -- implementation of Abort_Selector.
629 Signalling_Fds
.Close
(C
.int
(Selector
.R_Sig_Socket
));
630 Signalling_Fds
.Close
(C
.int
(Selector
.W_Sig_Socket
));
632 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
633 -- (erroneous) subsequent attempt to use this selector properly fails.
635 Selector
.R_Sig_Socket
:= No_Socket
;
636 Selector
.W_Sig_Socket
:= No_Socket
;
643 procedure Close_Socket
(Socket
: Socket_Type
) is
647 Res
:= C_Close
(C
.int
(Socket
));
649 if Res
= Failure
then
650 Raise_Socket_Error
(Socket_Errno
);
658 function Connect_Socket
659 (Socket
: Socket_Type
;
660 Server
: Sock_Addr_Type
) return C
.int
662 Sin
: aliased Sockaddr_In
;
663 Len
: constant C
.int
:= Sin
'Size / 8;
666 if Server
.Family
= Family_Inet6
then
667 raise Socket_Error
with "IPv6 not supported";
670 Set_Family
(Sin
.Sin_Family
, Server
.Family
);
671 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(Server
.Addr
));
673 (Sin
'Unchecked_Access,
674 Short_To_Network
(C
.unsigned_short
(Server
.Port
)));
676 return C_Connect
(C
.int
(Socket
), Sin
'Address, Len
);
679 procedure Connect_Socket
680 (Socket
: Socket_Type
;
681 Server
: Sock_Addr_Type
)
684 if Connect_Socket
(Socket
, Server
) = Failure
then
685 Raise_Socket_Error
(Socket_Errno
);
689 procedure Connect_Socket
690 (Socket
: Socket_Type
;
691 Server
: Sock_Addr_Type
;
692 Timeout
: Selector_Duration
;
693 Selector
: access Selector_Type
:= null;
694 Status
: out Selector_Status
)
697 -- Used to set Socket to non-blocking I/O
699 Conn_Err
: aliased Integer;
700 -- Error status of the socket after completion of select(2)
703 Conn_Err_Size
: aliased C
.int
:= Conn_Err
'Size / 8;
704 -- For getsockopt(2) call
707 if Selector
/= null and then not Is_Open
(Selector
.all) then
708 raise Program_Error
with "closed selector";
711 -- Set the socket to non-blocking I/O
713 Req
:= (Name
=> Non_Blocking_IO
, Enabled
=> True);
714 Control_Socket
(Socket
, Request
=> Req
);
716 -- Start operation (non-blocking), will return Failure with errno set
719 Res
:= Connect_Socket
(Socket
, Server
);
720 if Res
= Failure
then
721 Conn_Err
:= Socket_Errno
;
722 if Conn_Err
/= SOSC
.EINPROGRESS
then
723 Raise_Socket_Error
(Conn_Err
);
727 -- Wait for socket to become available for writing (unless the Timeout
728 -- is zero, in which case we consider that it has already expired, and
729 -- we do not need to wait at all).
731 if Timeout
= 0.0 then
739 Selector
=> Selector
,
743 -- Check error condition (the asynchronous connect may have terminated
744 -- with an error, e.g. ECONNREFUSED) if select(2) completed.
746 if Status
= Completed
then
748 (C
.int
(Socket
), SOSC
.SOL_SOCKET
, SOSC
.SO_ERROR
,
749 Conn_Err
'Address, Conn_Err_Size
'Access);
752 Conn_Err
:= Socket_Errno
;
759 -- Reset the socket to blocking I/O
761 Req
:= (Name
=> Non_Blocking_IO
, Enabled
=> False);
762 Control_Socket
(Socket
, Request
=> Req
);
764 -- Report error condition if any
766 if Conn_Err
/= 0 then
767 Raise_Socket_Error
(Conn_Err
);
775 procedure Control_Socket
776 (Socket
: Socket_Type
;
777 Request
: in out Request_Type
)
784 when Non_Blocking_IO
=>
785 Arg
:= C
.int
(Boolean'Pos (Request
.Enabled
));
787 when N_Bytes_To_Read
=>
792 (C
.int
(Socket
), Requests
(Request
.Name
), Arg
'Unchecked_Access);
794 if Res
= Failure
then
795 Raise_Socket_Error
(Socket_Errno
);
799 when Non_Blocking_IO
=>
802 when N_Bytes_To_Read
=>
803 Request
.Size
:= Natural (Arg
);
812 (Source
: Socket_Set_Type
;
813 Target
: out Socket_Set_Type
)
819 ---------------------
820 -- Create_Selector --
821 ---------------------
823 procedure Create_Selector
(Selector
: out Selector_Type
) is
824 Two_Fds
: aliased Fd_Pair
;
828 if Is_Open
(Selector
) then
829 -- Raise exception to prevent socket descriptor leak
831 raise Program_Error
with "selector already open";
834 -- We open two signalling file descriptors. One of them is used to send
835 -- data to the other, which is included in a C_Select socket set. The
836 -- communication is used to force a call to C_Select to complete, and
837 -- the waiting task to resume its execution.
839 Res
:= Signalling_Fds
.Create
(Two_Fds
'Access);
841 if Res
= Failure
then
842 Raise_Socket_Error
(Socket_Errno
);
845 Selector
.R_Sig_Socket
:= Socket_Type
(Two_Fds
(Read_End
));
846 Selector
.W_Sig_Socket
:= Socket_Type
(Two_Fds
(Write_End
));
853 procedure Create_Socket
854 (Socket
: out Socket_Type
;
855 Family
: Family_Type
:= Family_Inet
;
856 Mode
: Mode_Type
:= Socket_Stream
)
861 Res
:= C_Socket
(Families
(Family
), Modes
(Mode
), 0);
863 if Res
= Failure
then
864 Raise_Socket_Error
(Socket_Errno
);
867 Socket
:= Socket_Type
(Res
);
874 procedure Empty
(Item
: out Socket_Set_Type
) is
876 Reset_Socket_Set
(Item
.Set
'Access);
877 Item
.Last
:= No_Socket
;
884 function Err_Code_Image
(E
: Integer) return String is
885 Msg
: String := E
'Img & "] ";
887 Msg
(Msg
'First) := '[';
895 procedure Finalize
(X
: in out Sockets_Library_Controller
) is
896 pragma Unreferenced
(X
);
899 -- Finalization operation for the GNAT.Sockets package
908 procedure Finalize
is
910 -- This is a dummy placeholder for an obsolete API.
911 -- The real finalization actions are in Initialize primitive operation
912 -- of Sockets_Library_Controller.
922 (Item
: in out Socket_Set_Type
;
923 Socket
: out Socket_Type
)
926 L
: aliased C
.int
:= C
.int
(Item
.Last
);
929 if Item
.Last
/= No_Socket
then
931 (Item
.Set
'Access, Last
=> L
'Access, Socket
=> S
'Access);
932 Item
.Last
:= Socket_Type
(L
);
933 Socket
:= Socket_Type
(S
);
944 (Stream
: not null Stream_Access
) return Sock_Addr_Type
947 if Stream
.all in Datagram_Socket_Stream_Type
then
948 return Datagram_Socket_Stream_Type
(Stream
.all).From
;
950 return Get_Peer_Name
(Stream_Socket_Stream_Type
(Stream
.all).Socket
);
954 -------------------------
955 -- Get_Host_By_Address --
956 -------------------------
958 function Get_Host_By_Address
959 (Address
: Inet_Addr_Type
;
960 Family
: Family_Type
:= Family_Inet
) return Host_Entry_Type
962 pragma Unreferenced
(Family
);
964 HA
: aliased In_Addr
:= To_In_Addr
(Address
);
965 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
966 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
967 Res
: aliased Hostent
;
973 if C_Gethostbyaddr
(HA
'Address, HA
'Size / 8, SOSC
.AF_INET
,
974 Res
'Access, Buf
'Address, Buflen
, Err
'Access) /= 0
977 Raise_Host_Error
(Integer (Err
));
980 return H
: constant Host_Entry_Type
:=
981 To_Host_Entry
(Res
'Unchecked_Access)
985 end Get_Host_By_Address
;
987 ----------------------
988 -- Get_Host_By_Name --
989 ----------------------
991 function Get_Host_By_Name
(Name
: String) return Host_Entry_Type
is
993 -- Detect IP address name and redirect to Inet_Addr
995 if Is_IP_Address
(Name
) then
996 return Get_Host_By_Address
(Inet_Addr
(Name
));
1000 HN
: constant C
.char_array
:= C
.To_C
(Name
);
1001 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1002 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1003 Res
: aliased Hostent
;
1004 Err
: aliased C
.int
;
1010 (HN
, Res
'Access, Buf
'Address, Buflen
, Err
'Access) /= 0
1013 Raise_Host_Error
(Integer (Err
));
1016 return H
: constant Host_Entry_Type
:=
1017 To_Host_Entry
(Res
'Unchecked_Access)
1022 end Get_Host_By_Name
;
1028 function Get_Peer_Name
(Socket
: Socket_Type
) return Sock_Addr_Type
is
1029 Sin
: aliased Sockaddr_In
;
1030 Len
: aliased C
.int
:= Sin
'Size / 8;
1031 Res
: Sock_Addr_Type
(Family_Inet
);
1034 if C_Getpeername
(C
.int
(Socket
), Sin
'Address, Len
'Access) = Failure
then
1035 Raise_Socket_Error
(Socket_Errno
);
1038 To_Inet_Addr
(Sin
.Sin_Addr
, Res
.Addr
);
1039 Res
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1044 -------------------------
1045 -- Get_Service_By_Name --
1046 -------------------------
1048 function Get_Service_By_Name
1050 Protocol
: String) return Service_Entry_Type
1052 SN
: constant C
.char_array
:= C
.To_C
(Name
);
1053 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1054 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1055 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1056 Res
: aliased Servent
;
1061 if C_Getservbyname
(SN
, SP
, Res
'Access, Buf
'Address, Buflen
) /= 0 then
1063 raise Service_Error
with "Service not found";
1066 -- Translate from the C format to the API format
1068 return S
: constant Service_Entry_Type
:=
1069 To_Service_Entry
(Res
'Unchecked_Access)
1073 end Get_Service_By_Name
;
1075 -------------------------
1076 -- Get_Service_By_Port --
1077 -------------------------
1079 function Get_Service_By_Port
1081 Protocol
: String) return Service_Entry_Type
1083 SP
: constant C
.char_array
:= C
.To_C
(Protocol
);
1084 Buflen
: constant C
.int
:= Netdb_Buffer_Size
;
1085 Buf
: aliased C
.char_array
(1 .. Netdb_Buffer_Size
);
1086 Res
: aliased Servent
;
1092 (C
.int
(Short_To_Network
(C
.unsigned_short
(Port
))), SP
,
1093 Res
'Access, Buf
'Address, Buflen
) /= 0
1096 raise Service_Error
with "Service not found";
1099 -- Translate from the C format to the API format
1101 return S
: constant Service_Entry_Type
:=
1102 To_Service_Entry
(Res
'Unchecked_Access)
1106 end Get_Service_By_Port
;
1108 ---------------------
1109 -- Get_Socket_Name --
1110 ---------------------
1112 function Get_Socket_Name
1113 (Socket
: Socket_Type
) return Sock_Addr_Type
1115 Sin
: aliased Sockaddr_In
;
1116 Len
: aliased C
.int
:= Sin
'Size / 8;
1118 Addr
: Sock_Addr_Type
:= No_Sock_Addr
;
1121 Res
:= C_Getsockname
(C
.int
(Socket
), Sin
'Address, Len
'Access);
1123 if Res
/= Failure
then
1124 To_Inet_Addr
(Sin
.Sin_Addr
, Addr
.Addr
);
1125 Addr
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1129 end Get_Socket_Name
;
1131 -----------------------
1132 -- Get_Socket_Option --
1133 -----------------------
1135 function Get_Socket_Option
1136 (Socket
: Socket_Type
;
1137 Level
: Level_Type
:= Socket_Level
;
1138 Name
: Option_Name
) return Option_Type
1141 use type C
.unsigned_char
;
1143 V8
: aliased Two_Ints
;
1145 V1
: aliased C
.unsigned_char
;
1146 VT
: aliased Timeval
;
1147 Len
: aliased C
.int
;
1148 Add
: System
.Address
;
1150 Opt
: Option_Type
(Name
);
1154 when Multicast_Loop |
1156 Receive_Packet_Info
=>
1174 -- The standard argument for SO_RCVTIMEO and SO_SNDTIMEO is a
1175 -- struct timeval, but on Windows it is a milliseconds count in
1178 if Target_OS
= Windows
then
1202 if Res
= Failure
then
1203 Raise_Socket_Error
(Socket_Errno
);
1211 Opt
.Enabled
:= (V4
/= 0);
1214 Opt
.Enabled
:= (V8
(V8
'First) /= 0);
1215 Opt
.Seconds
:= Natural (V8
(V8
'Last));
1219 Opt
.Size
:= Natural (V4
);
1222 Opt
.Error
:= Resolve_Error
(Integer (V4
));
1224 when Add_Membership |
1226 To_Inet_Addr
(To_In_Addr
(V8
(V8
'First)), Opt
.Multicast_Address
);
1227 To_Inet_Addr
(To_In_Addr
(V8
(V8
'Last)), Opt
.Local_Interface
);
1229 when Multicast_If
=>
1230 To_Inet_Addr
(To_In_Addr
(V4
), Opt
.Outgoing_If
);
1232 when Multicast_TTL
=>
1233 Opt
.Time_To_Live
:= Integer (V1
);
1235 when Multicast_Loop |
1236 Receive_Packet_Info
=>
1237 Opt
.Enabled
:= (V1
/= 0);
1242 if Target_OS
= Windows
then
1244 -- Timeout is in milliseconds, actual value is 500 ms +
1245 -- returned value (unless it is 0).
1250 Opt
.Timeout
:= Natural (V4
) * 0.001 + 0.500;
1254 Opt
.Timeout
:= To_Duration
(VT
);
1259 end Get_Socket_Option
;
1265 function Host_Name
return String is
1266 Name
: aliased C
.char_array
(1 .. 64);
1270 Res
:= C_Gethostname
(Name
'Address, Name
'Length);
1272 if Res
= Failure
then
1273 Raise_Socket_Error
(Socket_Errno
);
1276 return C
.To_Ada
(Name
);
1284 (Val
: Inet_Addr_VN_Type
;
1285 Hex
: Boolean := False) return String
1287 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1288 -- has at most a length of 3 plus one '.' character.
1290 Buffer
: String (1 .. 4 * Val
'Length);
1291 Length
: Natural := 1;
1292 Separator
: Character;
1294 procedure Img10
(V
: Inet_Addr_Comp_Type
);
1295 -- Append to Buffer image of V in decimal format
1297 procedure Img16
(V
: Inet_Addr_Comp_Type
);
1298 -- Append to Buffer image of V in hexadecimal format
1304 procedure Img10
(V
: Inet_Addr_Comp_Type
) is
1305 Img
: constant String := V
'Img;
1306 Len
: constant Natural := Img
'Length - 1;
1308 Buffer
(Length
.. Length
+ Len
- 1) := Img
(2 .. Img
'Last);
1309 Length
:= Length
+ Len
;
1316 procedure Img16
(V
: Inet_Addr_Comp_Type
) is
1318 Buffer
(Length
) := Hex_To_Char
(Natural (V
/ 16) + 1);
1319 Buffer
(Length
+ 1) := Hex_To_Char
(Natural (V
mod 16) + 1);
1320 Length
:= Length
+ 2;
1323 -- Start of processing for Image
1326 Separator
:= (if Hex
then ':' else '.');
1328 for J
in Val
'Range loop
1335 if J
/= Val
'Last then
1336 Buffer
(Length
) := Separator
;
1337 Length
:= Length
+ 1;
1341 return Buffer
(1 .. Length
- 1);
1348 function Image
(Value
: Inet_Addr_Type
) return String is
1350 if Value
.Family
= Family_Inet
then
1351 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V4
), Hex
=> False);
1353 return Image
(Inet_Addr_VN_Type
(Value
.Sin_V6
), Hex
=> True);
1361 function Image
(Value
: Sock_Addr_Type
) return String is
1362 Port
: constant String := Value
.Port
'Img;
1364 return Image
(Value
.Addr
) & ':' & Port
(2 .. Port
'Last);
1371 function Image
(Socket
: Socket_Type
) return String is
1380 function Image
(Item
: Socket_Set_Type
) return String is
1381 Socket_Set
: Socket_Set_Type
:= Item
;
1385 Last_Img
: constant String := Socket_Set
.Last
'Img;
1387 (1 .. (Integer (Socket_Set
.Last
) + 1) * Last_Img
'Length);
1388 Index
: Positive := 1;
1389 Socket
: Socket_Type
;
1392 while not Is_Empty
(Socket_Set
) loop
1393 Get
(Socket_Set
, Socket
);
1396 Socket_Img
: constant String := Socket
'Img;
1398 Buffer
(Index
.. Index
+ Socket_Img
'Length - 1) := Socket_Img
;
1399 Index
:= Index
+ Socket_Img
'Length;
1403 return "[" & Last_Img
& "]" & Buffer
(1 .. Index
- 1);
1411 function Inet_Addr
(Image
: String) return Inet_Addr_Type
is
1414 Img
: aliased char_array
:= To_C
(Image
);
1415 Addr
: aliased C
.int
;
1417 Result
: Inet_Addr_Type
;
1420 -- Special case for an empty Image as on some platforms (e.g. Windows)
1421 -- calling Inet_Addr("") will not return an error.
1424 Raise_Socket_Error
(SOSC
.EINVAL
);
1427 Res
:= Inet_Pton
(SOSC
.AF_INET
, Img
'Address, Addr
'Address);
1430 Raise_Socket_Error
(Socket_Errno
);
1433 Raise_Socket_Error
(SOSC
.EINVAL
);
1436 To_Inet_Addr
(To_In_Addr
(Addr
), Result
);
1444 procedure Initialize
(X
: in out Sockets_Library_Controller
) is
1445 pragma Unreferenced
(X
);
1455 procedure Initialize
(Process_Blocking_IO
: Boolean) is
1456 Expected
: constant Boolean := not SOSC
.Thread_Blocking_IO
;
1459 if Process_Blocking_IO
/= Expected
then
1460 raise Socket_Error
with
1461 "incorrect Process_Blocking_IO setting, expected " & Expected
'Img;
1464 -- This is a dummy placeholder for an obsolete API
1466 -- Real initialization actions are in Initialize primitive operation
1467 -- of Sockets_Library_Controller.
1476 procedure Initialize
is
1478 -- This is a dummy placeholder for an obsolete API
1480 -- Real initialization actions are in Initialize primitive operation
1481 -- of Sockets_Library_Controller.
1490 function Is_Empty
(Item
: Socket_Set_Type
) return Boolean is
1492 return Item
.Last
= No_Socket
;
1499 function Is_IP_Address
(Name
: String) return Boolean is
1501 for J
in Name
'Range loop
1503 and then Name
(J
) not in '0' .. '9'
1516 function Is_Open
(S
: Selector_Type
) return Boolean is
1522 -- Either both controlling socket descriptors are valid (case of an
1523 -- open selector) or neither (case of a closed selector).
1525 pragma Assert
((S
.R_Sig_Socket
/= No_Socket
)
1527 (S
.W_Sig_Socket
/= No_Socket
));
1529 return S
.R_Sig_Socket
/= No_Socket
;
1538 (Item
: Socket_Set_Type
;
1539 Socket
: Socket_Type
) return Boolean
1542 Check_For_Fd_Set
(Socket
);
1544 return Item
.Last
/= No_Socket
1545 and then Socket
<= Item
.Last
1546 and then Is_Socket_In_Set
(Item
.Set
'Access, C
.int
(Socket
)) /= 0;
1553 procedure Listen_Socket
1554 (Socket
: Socket_Type
;
1555 Length
: Natural := 15)
1557 Res
: constant C
.int
:= C_Listen
(C
.int
(Socket
), C
.int
(Length
));
1559 if Res
= Failure
then
1560 Raise_Socket_Error
(Socket_Errno
);
1568 procedure Narrow
(Item
: in out Socket_Set_Type
) is
1569 Last
: aliased C
.int
:= C
.int
(Item
.Last
);
1571 if Item
.Last
/= No_Socket
then
1572 Last_Socket_In_Set
(Item
.Set
'Access, Last
'Unchecked_Access);
1573 Item
.Last
:= Socket_Type
(Last
);
1581 procedure Netdb_Lock
is
1583 if Need_Netdb_Lock
then
1584 System
.Task_Lock
.Lock
;
1592 procedure Netdb_Unlock
is
1594 if Need_Netdb_Lock
then
1595 System
.Task_Lock
.Unlock
;
1599 --------------------------------
1600 -- Normalize_Empty_Socket_Set --
1601 --------------------------------
1603 procedure Normalize_Empty_Socket_Set
(S
: in out Socket_Set_Type
) is
1605 if S
.Last
= No_Socket
then
1606 Reset_Socket_Set
(S
.Set
'Access);
1608 end Normalize_Empty_Socket_Set
;
1614 function Official_Name
(E
: Host_Entry_Type
) return String is
1616 return To_String
(E
.Official
);
1623 function Official_Name
(S
: Service_Entry_Type
) return String is
1625 return To_String
(S
.Official
);
1628 --------------------
1629 -- Wait_On_Socket --
1630 --------------------
1632 procedure Wait_On_Socket
1633 (Socket
: Socket_Type
;
1635 Timeout
: Selector_Duration
;
1636 Selector
: access Selector_Type
:= null;
1637 Status
: out Selector_Status
)
1639 type Local_Selector_Access
is access Selector_Type
;
1640 for Local_Selector_Access
'Storage_Size use Selector_Type
'Size;
1642 S
: Selector_Access
;
1643 -- Selector to use for waiting
1645 R_Fd_Set
: Socket_Set_Type
;
1646 W_Fd_Set
: Socket_Set_Type
;
1649 -- Create selector if not provided by the user
1651 if Selector
= null then
1653 Local_S
: constant Local_Selector_Access
:= new Selector_Type
;
1655 S
:= Local_S
.all'Unchecked_Access;
1656 Create_Selector
(S
.all);
1660 S
:= Selector
.all'Access;
1664 Set
(R_Fd_Set
, Socket
);
1666 Set
(W_Fd_Set
, Socket
);
1669 Check_Selector
(S
.all, R_Fd_Set
, W_Fd_Set
, Status
, Timeout
);
1671 if Selector
= null then
1672 Close_Selector
(S
.all);
1680 function Port_Number
(S
: Service_Entry_Type
) return Port_Type
is
1689 function Protocol_Name
(S
: Service_Entry_Type
) return String is
1691 return To_String
(S
.Protocol
);
1694 ----------------------
1695 -- Raise_Host_Error --
1696 ----------------------
1698 procedure Raise_Host_Error
(H_Error
: Integer) is
1700 raise Host_Error
with
1701 Err_Code_Image
(H_Error
)
1702 & Host_Error_Messages
.Host_Error_Message
(H_Error
);
1703 end Raise_Host_Error
;
1705 ------------------------
1706 -- Raise_Socket_Error --
1707 ------------------------
1709 procedure Raise_Socket_Error
(Error
: Integer) is
1711 raise Socket_Error
with
1712 Err_Code_Image
(Error
) & Socket_Error_Message
(Error
);
1713 end Raise_Socket_Error
;
1720 (Stream
: in out Datagram_Socket_Stream_Type
;
1721 Item
: out Ada
.Streams
.Stream_Element_Array
;
1722 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1737 (Stream
: in out Stream_Socket_Stream_Type
;
1738 Item
: out Ada
.Streams
.Stream_Element_Array
;
1739 Last
: out Ada
.Streams
.Stream_Element_Offset
)
1741 First
: Ada
.Streams
.Stream_Element_Offset
:= Item
'First;
1742 Index
: Ada
.Streams
.Stream_Element_Offset
:= First
- 1;
1743 Max
: constant Ada
.Streams
.Stream_Element_Offset
:= Item
'Last;
1747 Receive_Socket
(Stream
.Socket
, Item
(First
.. Max
), Index
);
1750 -- Exit when all or zero data received. Zero means that the socket
1753 exit when Index
< First
or else Index
= Max
;
1759 --------------------
1760 -- Receive_Socket --
1761 --------------------
1763 procedure Receive_Socket
1764 (Socket
: Socket_Type
;
1765 Item
: out Ada
.Streams
.Stream_Element_Array
;
1766 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1767 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1773 C_Recv
(C
.int
(Socket
), Item
'Address, Item
'Length, To_Int
(Flags
));
1775 if Res
= Failure
then
1776 Raise_Socket_Error
(Socket_Errno
);
1779 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
1782 --------------------
1783 -- Receive_Socket --
1784 --------------------
1786 procedure Receive_Socket
1787 (Socket
: Socket_Type
;
1788 Item
: out Ada
.Streams
.Stream_Element_Array
;
1789 Last
: out Ada
.Streams
.Stream_Element_Offset
;
1790 From
: out Sock_Addr_Type
;
1791 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1794 Sin
: aliased Sockaddr_In
;
1795 Len
: aliased C
.int
:= Sin
'Size / 8;
1807 if Res
= Failure
then
1808 Raise_Socket_Error
(Socket_Errno
);
1811 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
1813 To_Inet_Addr
(Sin
.Sin_Addr
, From
.Addr
);
1814 From
.Port
:= Port_Type
(Network_To_Short
(Sin
.Sin_Port
));
1817 --------------------
1818 -- Receive_Vector --
1819 --------------------
1821 procedure Receive_Vector
1822 (Socket
: Socket_Type
;
1823 Vector
: Vector_Type
;
1824 Count
: out Ada
.Streams
.Stream_Element_Count
;
1825 Flags
: Request_Flag_Type
:= No_Request_Flag
)
1830 (Msg_Name
=> System
.Null_Address
,
1832 Msg_Iov
=> Vector
'Address,
1834 -- recvmsg(2) returns EMSGSIZE on Linux (and probably on other
1835 -- platforms) when the supplied vector is longer than IOV_MAX,
1836 -- so use minimum of the two lengths.
1838 Msg_Iovlen
=> SOSC
.Msg_Iovlen_T
'Min
1839 (Vector
'Length, SOSC
.IOV_MAX
),
1841 Msg_Control
=> System
.Null_Address
,
1842 Msg_Controllen
=> 0,
1852 if Res
= ssize_t
(Failure
) then
1853 Raise_Socket_Error
(Socket_Errno
);
1856 Count
:= Ada
.Streams
.Stream_Element_Count
(Res
);
1863 function Resolve_Error
1864 (Error_Value
: Integer;
1865 From_Errno
: Boolean := True) return Error_Type
1867 use GNAT
.Sockets
.SOSC
;
1870 if not From_Errno
then
1872 when SOSC
.HOST_NOT_FOUND
=> return Unknown_Host
;
1873 when SOSC
.TRY_AGAIN
=> return Host_Name_Lookup_Failure
;
1874 when SOSC
.NO_RECOVERY
=> return Non_Recoverable_Error
;
1875 when SOSC
.NO_DATA
=> return Unknown_Server_Error
;
1876 when others => return Cannot_Resolve_Error
;
1880 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1881 -- can't include it in the case statement below.
1883 pragma Warnings
(Off
);
1884 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1886 if EAGAIN
/= EWOULDBLOCK
and then Error_Value
= EAGAIN
then
1887 return Resource_Temporarily_Unavailable
;
1890 -- This is not a case statement because if a particular error
1891 -- number constant is not defined, s-oscons-tmplt.c defines
1892 -- it to -1. If multiple constants are not defined, they
1893 -- would each be -1 and result in a "duplicate value in case" error.
1895 -- But we have to leave warnings off because the compiler is also
1896 -- smart enough to note that when two errnos have the same value,
1897 -- the second if condition is useless.
1898 if Error_Value
= ENOERROR
then
1900 elsif Error_Value
= EACCES
then
1901 return Permission_Denied
;
1902 elsif Error_Value
= EADDRINUSE
then
1903 return Address_Already_In_Use
;
1904 elsif Error_Value
= EADDRNOTAVAIL
then
1905 return Cannot_Assign_Requested_Address
;
1906 elsif Error_Value
= EAFNOSUPPORT
then
1907 return Address_Family_Not_Supported_By_Protocol
;
1908 elsif Error_Value
= EALREADY
then
1909 return Operation_Already_In_Progress
;
1910 elsif Error_Value
= EBADF
then
1911 return Bad_File_Descriptor
;
1912 elsif Error_Value
= ECONNABORTED
then
1913 return Software_Caused_Connection_Abort
;
1914 elsif Error_Value
= ECONNREFUSED
then
1915 return Connection_Refused
;
1916 elsif Error_Value
= ECONNRESET
then
1917 return Connection_Reset_By_Peer
;
1918 elsif Error_Value
= EDESTADDRREQ
then
1919 return Destination_Address_Required
;
1920 elsif Error_Value
= EFAULT
then
1922 elsif Error_Value
= EHOSTDOWN
then
1923 return Host_Is_Down
;
1924 elsif Error_Value
= EHOSTUNREACH
then
1925 return No_Route_To_Host
;
1926 elsif Error_Value
= EINPROGRESS
then
1927 return Operation_Now_In_Progress
;
1928 elsif Error_Value
= EINTR
then
1929 return Interrupted_System_Call
;
1930 elsif Error_Value
= EINVAL
then
1931 return Invalid_Argument
;
1932 elsif Error_Value
= EIO
then
1933 return Input_Output_Error
;
1934 elsif Error_Value
= EISCONN
then
1935 return Transport_Endpoint_Already_Connected
;
1936 elsif Error_Value
= ELOOP
then
1937 return Too_Many_Symbolic_Links
;
1938 elsif Error_Value
= EMFILE
then
1939 return Too_Many_Open_Files
;
1940 elsif Error_Value
= EMSGSIZE
then
1941 return Message_Too_Long
;
1942 elsif Error_Value
= ENAMETOOLONG
then
1943 return File_Name_Too_Long
;
1944 elsif Error_Value
= ENETDOWN
then
1945 return Network_Is_Down
;
1946 elsif Error_Value
= ENETRESET
then
1947 return Network_Dropped_Connection_Because_Of_Reset
;
1948 elsif Error_Value
= ENETUNREACH
then
1949 return Network_Is_Unreachable
;
1950 elsif Error_Value
= ENOBUFS
then
1951 return No_Buffer_Space_Available
;
1952 elsif Error_Value
= ENOPROTOOPT
then
1953 return Protocol_Not_Available
;
1954 elsif Error_Value
= ENOTCONN
then
1955 return Transport_Endpoint_Not_Connected
;
1956 elsif Error_Value
= ENOTSOCK
then
1957 return Socket_Operation_On_Non_Socket
;
1958 elsif Error_Value
= EOPNOTSUPP
then
1959 return Operation_Not_Supported
;
1960 elsif Error_Value
= EPFNOSUPPORT
then
1961 return Protocol_Family_Not_Supported
;
1962 elsif Error_Value
= EPIPE
then
1964 elsif Error_Value
= EPROTONOSUPPORT
then
1965 return Protocol_Not_Supported
;
1966 elsif Error_Value
= EPROTOTYPE
then
1967 return Protocol_Wrong_Type_For_Socket
;
1968 elsif Error_Value
= ESHUTDOWN
then
1969 return Cannot_Send_After_Transport_Endpoint_Shutdown
;
1970 elsif Error_Value
= ESOCKTNOSUPPORT
then
1971 return Socket_Type_Not_Supported
;
1972 elsif Error_Value
= ETIMEDOUT
then
1973 return Connection_Timed_Out
;
1974 elsif Error_Value
= ETOOMANYREFS
then
1975 return Too_Many_References
;
1976 elsif Error_Value
= EWOULDBLOCK
then
1977 return Resource_Temporarily_Unavailable
;
1979 return Cannot_Resolve_Error
;
1981 pragma Warnings
(On
);
1985 -----------------------
1986 -- Resolve_Exception --
1987 -----------------------
1989 function Resolve_Exception
1990 (Occurrence
: Exception_Occurrence
) return Error_Type
1992 Id
: constant Exception_Id
:= Exception_Identity
(Occurrence
);
1993 Msg
: constant String := Exception_Message
(Occurrence
);
2000 while First
<= Msg
'Last
2001 and then Msg
(First
) not in '0' .. '9'
2006 if First
> Msg
'Last then
2007 return Cannot_Resolve_Error
;
2011 while Last
< Msg
'Last
2012 and then Msg
(Last
+ 1) in '0' .. '9'
2017 Val
:= Integer'Value (Msg
(First
.. Last
));
2019 if Id
= Socket_Error_Id
then
2020 return Resolve_Error
(Val
);
2022 elsif Id
= Host_Error_Id
then
2023 return Resolve_Error
(Val
, False);
2026 return Cannot_Resolve_Error
;
2028 end Resolve_Exception
;
2034 procedure Send_Socket
2035 (Socket
: Socket_Type
;
2036 Item
: Ada
.Streams
.Stream_Element_Array
;
2037 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2038 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2041 Send_Socket
(Socket
, Item
, Last
, To
=> null, Flags
=> Flags
);
2048 procedure Send_Socket
2049 (Socket
: Socket_Type
;
2050 Item
: Ada
.Streams
.Stream_Element_Array
;
2051 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2052 To
: Sock_Addr_Type
;
2053 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2057 (Socket
, Item
, Last
, To
=> To
'Unrestricted_Access, Flags
=> Flags
);
2064 procedure Send_Socket
2065 (Socket
: Socket_Type
;
2066 Item
: Ada
.Streams
.Stream_Element_Array
;
2067 Last
: out Ada
.Streams
.Stream_Element_Offset
;
2068 To
: access Sock_Addr_Type
;
2069 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2073 Sin
: aliased Sockaddr_In
;
2074 C_To
: System
.Address
;
2079 Set_Family
(Sin
.Sin_Family
, To
.Family
);
2080 Set_Address
(Sin
'Unchecked_Access, To_In_Addr
(To
.Addr
));
2082 (Sin
'Unchecked_Access,
2083 Short_To_Network
(C
.unsigned_short
(To
.Port
)));
2084 C_To
:= Sin
'Address;
2085 Len
:= Sin
'Size / 8;
2088 C_To
:= System
.Null_Address
;
2096 Set_Forced_Flags
(To_Int
(Flags
)),
2100 if Res
= Failure
then
2101 Raise_Socket_Error
(Socket_Errno
);
2104 Last
:= Last_Index
(First
=> Item
'First, Count
=> size_t
(Res
));
2111 procedure Send_Vector
2112 (Socket
: Socket_Type
;
2113 Vector
: Vector_Type
;
2114 Count
: out Ada
.Streams
.Stream_Element_Count
;
2115 Flags
: Request_Flag_Type
:= No_Request_Flag
)
2121 Iov_Count
: SOSC
.Msg_Iovlen_T
;
2122 This_Iov_Count
: SOSC
.Msg_Iovlen_T
;
2128 while Iov_Count
< Vector
'Length loop
2130 pragma Warnings
(Off
);
2131 -- Following test may be compile time known on some targets
2134 (if Vector
'Length - Iov_Count
> SOSC
.IOV_MAX
2136 else Vector
'Length - Iov_Count
);
2138 pragma Warnings
(On
);
2141 (Msg_Name
=> System
.Null_Address
,
2144 (Vector
'First + Integer (Iov_Count
))'Address,
2145 Msg_Iovlen
=> This_Iov_Count
,
2146 Msg_Control
=> System
.Null_Address
,
2147 Msg_Controllen
=> 0,
2154 Set_Forced_Flags
(To_Int
(Flags
)));
2156 if Res
= ssize_t
(Failure
) then
2157 Raise_Socket_Error
(Socket_Errno
);
2160 Count
:= Count
+ Ada
.Streams
.Stream_Element_Count
(Res
);
2161 Iov_Count
:= Iov_Count
+ This_Iov_Count
;
2169 procedure Set
(Item
: in out Socket_Set_Type
; Socket
: Socket_Type
) is
2171 Check_For_Fd_Set
(Socket
);
2173 if Item
.Last
= No_Socket
then
2175 -- Uninitialized socket set, make sure it is properly zeroed out
2177 Reset_Socket_Set
(Item
.Set
'Access);
2178 Item
.Last
:= Socket
;
2180 elsif Item
.Last
< Socket
then
2181 Item
.Last
:= Socket
;
2184 Insert_Socket_In_Set
(Item
.Set
'Access, C
.int
(Socket
));
2187 -----------------------
2188 -- Set_Close_On_Exec --
2189 -----------------------
2191 procedure Set_Close_On_Exec
2192 (Socket
: Socket_Type
;
2193 Close_On_Exec
: Boolean;
2194 Status
: out Boolean)
2196 function C_Set_Close_On_Exec
2197 (Socket
: Socket_Type
; Close_On_Exec
: C
.int
) return C
.int
;
2198 pragma Import
(C
, C_Set_Close_On_Exec
, "__gnat_set_close_on_exec");
2200 Status
:= C_Set_Close_On_Exec
(Socket
, Boolean'Pos (Close_On_Exec
)) = 0;
2201 end Set_Close_On_Exec
;
2203 ----------------------
2204 -- Set_Forced_Flags --
2205 ----------------------
2207 function Set_Forced_Flags
(F
: C
.int
) return C
.int
is
2208 use type C
.unsigned
;
2209 function To_unsigned
is
2210 new Ada
.Unchecked_Conversion
(C
.int
, C
.unsigned
);
2212 new Ada
.Unchecked_Conversion
(C
.unsigned
, C
.int
);
2214 return To_int
(To_unsigned
(F
) or SOSC
.MSG_Forced_Flags
);
2215 end Set_Forced_Flags
;
2217 -----------------------
2218 -- Set_Socket_Option --
2219 -----------------------
2221 procedure Set_Socket_Option
2222 (Socket
: Socket_Type
;
2223 Level
: Level_Type
:= Socket_Level
;
2224 Option
: Option_Type
)
2228 V8
: aliased Two_Ints
;
2230 V1
: aliased C
.unsigned_char
;
2231 VT
: aliased Timeval
;
2233 Add
: System
.Address
:= Null_Address
;
2242 V4
:= C
.int
(Boolean'Pos (Option
.Enabled
));
2247 V8
(V8
'First) := C
.int
(Boolean'Pos (Option
.Enabled
));
2248 V8
(V8
'Last) := C
.int
(Option
.Seconds
);
2254 V4
:= C
.int
(Option
.Size
);
2259 V4
:= C
.int
(Boolean'Pos (True));
2263 when Add_Membership |
2265 V8
(V8
'First) := To_Int
(To_In_Addr
(Option
.Multicast_Address
));
2266 V8
(V8
'Last) := To_Int
(To_In_Addr
(Option
.Local_Interface
));
2270 when Multicast_If
=>
2271 V4
:= To_Int
(To_In_Addr
(Option
.Outgoing_If
));
2275 when Multicast_TTL
=>
2276 V1
:= C
.unsigned_char
(Option
.Time_To_Live
);
2280 when Multicast_Loop |
2281 Receive_Packet_Info
=>
2282 V1
:= C
.unsigned_char
(Boolean'Pos (Option
.Enabled
));
2289 if Target_OS
= Windows
then
2291 -- On Windows, the timeout is a DWORD in milliseconds, and
2292 -- the actual timeout is 500 ms + the given value (unless it
2295 V4
:= C
.int
(Option
.Timeout
/ 0.001);
2308 VT
:= To_Timeval
(Option
.Timeout
);
2318 Options
(Option
.Name
),
2321 if Res
= Failure
then
2322 Raise_Socket_Error
(Socket_Errno
);
2324 end Set_Socket_Option
;
2326 ----------------------
2327 -- Short_To_Network --
2328 ----------------------
2330 function Short_To_Network
(S
: C
.unsigned_short
) return C
.unsigned_short
is
2331 use type C
.unsigned_short
;
2334 -- Big-endian case. No conversion needed. On these platforms, htons()
2335 -- defaults to a null procedure.
2337 if Default_Bit_Order
= High_Order_First
then
2340 -- Little-endian case. We must swap the high and low bytes of this
2341 -- short to make the port number network compliant.
2344 return (S
/ 256) + (S
mod 256) * 256;
2346 end Short_To_Network
;
2348 ---------------------
2349 -- Shutdown_Socket --
2350 ---------------------
2352 procedure Shutdown_Socket
2353 (Socket
: Socket_Type
;
2354 How
: Shutmode_Type
:= Shut_Read_Write
)
2359 Res
:= C_Shutdown
(C
.int
(Socket
), Shutmodes
(How
));
2361 if Res
= Failure
then
2362 Raise_Socket_Error
(Socket_Errno
);
2364 end Shutdown_Socket
;
2371 (Socket
: Socket_Type
;
2372 Send_To
: Sock_Addr_Type
) return Stream_Access
2374 S
: Datagram_Socket_Stream_Access
;
2377 S
:= new Datagram_Socket_Stream_Type
;
2380 S
.From
:= Get_Socket_Name
(Socket
);
2381 return Stream_Access
(S
);
2388 function Stream
(Socket
: Socket_Type
) return Stream_Access
is
2389 S
: Stream_Socket_Stream_Access
;
2391 S
:= new Stream_Socket_Stream_Type
;
2393 return Stream_Access
(S
);
2400 function To_C
(Socket
: Socket_Type
) return Integer is
2402 return Integer (Socket
);
2409 function To_Duration
(Val
: Timeval
) return Timeval_Duration
is
2411 return Natural (Val
.Tv_Sec
) * 1.0 + Natural (Val
.Tv_Usec
) * 1.0E-6;
2418 function To_Host_Entry
(E
: Hostent_Access
) return Host_Entry_Type
is
2421 Aliases_Count
, Addresses_Count
: Natural;
2423 -- H_Length is not used because it is currently only ever set to 4, as
2424 -- H_Addrtype is always AF_INET.
2428 while Hostent_H_Alias
(E
, C
.int
(Aliases_Count
)) /= Null_Address
loop
2429 Aliases_Count
:= Aliases_Count
+ 1;
2432 Addresses_Count
:= 0;
2433 while Hostent_H_Addr
(E
, C
.int
(Addresses_Count
)) /= Null_Address
loop
2434 Addresses_Count
:= Addresses_Count
+ 1;
2437 return Result
: Host_Entry_Type
2438 (Aliases_Length
=> Aliases_Count
,
2439 Addresses_Length
=> Addresses_Count
)
2441 Result
.Official
:= To_Name
(Value
(Hostent_H_Name
(E
)));
2443 for J
in Result
.Aliases
'Range loop
2444 Result
.Aliases
(J
) :=
2445 To_Name
(Value
(Hostent_H_Alias
2446 (E
, C
.int
(J
- Result
.Aliases
'First))));
2449 for J
in Result
.Addresses
'Range loop
2453 -- Hostent_H_Addr (E, <index>) may return an address that is
2454 -- not correctly aligned for In_Addr, so we need to use
2455 -- an intermediate copy operation on a type with an alignemnt
2456 -- of 1 to recover the value.
2458 subtype Addr_Buf_T
is C
.char_array
(1 .. Addr
'Size / 8);
2459 Unaligned_Addr
: Addr_Buf_T
;
2460 for Unaligned_Addr
'Address
2461 use Hostent_H_Addr
(E
, C
.int
(J
- Result
.Addresses
'First));
2462 pragma Import
(Ada
, Unaligned_Addr
);
2464 Aligned_Addr
: Addr_Buf_T
;
2465 for Aligned_Addr
'Address use Addr
'Address;
2466 pragma Import
(Ada
, Aligned_Addr
);
2469 Aligned_Addr
:= Unaligned_Addr
;
2470 To_Inet_Addr
(Addr
, Result
.Addresses
(J
));
2480 function To_In_Addr
(Addr
: Inet_Addr_Type
) return In_Addr
is
2482 if Addr
.Family
= Family_Inet
then
2483 return (S_B1
=> C
.unsigned_char
(Addr
.Sin_V4
(1)),
2484 S_B2
=> C
.unsigned_char
(Addr
.Sin_V4
(2)),
2485 S_B3
=> C
.unsigned_char
(Addr
.Sin_V4
(3)),
2486 S_B4
=> C
.unsigned_char
(Addr
.Sin_V4
(4)));
2489 raise Socket_Error
with "IPv6 not supported";
2496 procedure To_Inet_Addr
2498 Result
: out Inet_Addr_Type
) is
2500 Result
.Sin_V4
(1) := Inet_Addr_Comp_Type
(Addr
.S_B1
);
2501 Result
.Sin_V4
(2) := Inet_Addr_Comp_Type
(Addr
.S_B2
);
2502 Result
.Sin_V4
(3) := Inet_Addr_Comp_Type
(Addr
.S_B3
);
2503 Result
.Sin_V4
(4) := Inet_Addr_Comp_Type
(Addr
.S_B4
);
2510 function To_Int
(F
: Request_Flag_Type
) return C
.int
2512 Current
: Request_Flag_Type
:= F
;
2513 Result
: C
.int
:= 0;
2516 for J
in Flags
'Range loop
2517 exit when Current
= 0;
2519 if Current
mod 2 /= 0 then
2520 if Flags
(J
) = -1 then
2521 Raise_Socket_Error
(SOSC
.EOPNOTSUPP
);
2524 Result
:= Result
+ Flags
(J
);
2527 Current
:= Current
/ 2;
2537 function To_Name
(N
: String) return Name_Type
is
2539 return Name_Type
'(N'Length, N);
2542 ----------------------
2543 -- To_Service_Entry --
2544 ----------------------
2546 function To_Service_Entry (E : Servent_Access) return Service_Entry_Type is
2549 Aliases_Count : Natural;
2553 while Servent_S_Alias (E, C.int (Aliases_Count)) /= Null_Address loop
2554 Aliases_Count := Aliases_Count + 1;
2557 return Result : Service_Entry_Type (Aliases_Length => Aliases_Count) do
2558 Result.Official := To_Name (Value (Servent_S_Name (E)));
2560 for J in Result.Aliases'Range loop
2561 Result.Aliases (J) :=
2562 To_Name (Value (Servent_S_Alias
2563 (E, C.int (J - Result.Aliases'First))));
2566 Result.Protocol := To_Name (Value (Servent_S_Proto (E)));
2568 Port_Type (Network_To_Short (Servent_S_Port (E)));
2570 end To_Service_Entry;
2576 function To_String (HN : Name_Type) return String is
2578 return HN.Name (1 .. HN.Length);
2585 function To_Timeval (Val : Timeval_Duration) return Timeval is
2590 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2596 -- Normal case where we do round down
2599 S := time_t (Val - 0.5);
2600 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2610 function Value (S : System.Address) return String is
2611 Str : String (1 .. Positive'Last);
2612 for Str'Address use S;
2613 pragma Import (Ada, Str);
2615 Terminator : Positive := Str'First;
2618 while Str (Terminator) /= ASCII.NUL loop
2619 Terminator := Terminator + 1;
2622 return Str (1 .. Terminator - 1);
2630 (Stream : in out Datagram_Socket_Stream_Type;
2631 Item : Ada.Streams.Stream_Element_Array)
2633 Last : Stream_Element_Offset;
2642 -- It is an error if not all of the data has been sent
2644 if Last /= Item'Last then
2645 Raise_Socket_Error (Socket_Errno);
2654 (Stream : in out Stream_Socket_Stream_Type;
2655 Item : Ada.Streams.Stream_Element_Array)
2657 First : Ada.Streams.Stream_Element_Offset;
2658 Index : Ada.Streams.Stream_Element_Offset;
2659 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2662 First := Item'First;
2664 while First <= Max loop
2665 Send_Socket (Stream.Socket, Item (First .. Max), Index, null);
2667 -- Exit when all or zero data sent. Zero means that the socket has
2668 -- been closed by peer.
2670 exit when Index < First or else Index = Max;
2675 -- For an empty array, we have First > Max, and hence Index >= Max (no
2676 -- error, the loop above is never executed). After a successful send,
2677 -- Index = Max. The only remaining case, Index < Max, is therefore
2678 -- always an actual send failure.
2681 Raise_Socket_Error (Socket_Errno);
2685 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2686 pragma Unreferenced (Sockets_Library_Controller_Object);
2687 -- The elaboration and finalization of this object perform the required
2688 -- initialization and cleanup actions for the sockets library.