1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S --
9 -- Copyright (C) 2001-2010, 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 Interfaces.C.Strings;
39 with GNAT.Sockets.Thin_Common; use GNAT.Sockets.Thin_Common;
40 with GNAT.Sockets.Thin; use GNAT.Sockets.Thin;
42 with GNAT.Sockets.Linker_Options;
43 pragma Warnings (Off, GNAT.Sockets.Linker_Options);
44 -- Need to include pragma Linker_Options which is platform dependent
46 with System; use System;
47 with System.Communication; use System.Communication;
48 with System.CRTL; use System.CRTL;
49 with System.Task_Lock;
51 package body GNAT.Sockets is
53 package C renames Interfaces.C;
57 ENOERROR : constant := 0;
59 Netdb_Buffer_Size : constant := SOSC.Need_Netdb_Buffer * 1024;
60 Need_Netdb_Lock : constant Boolean := SOSC.Need_Netdb_Lock /= 0;
61 -- The network database functions gethostbyname, gethostbyaddr,
62 -- getservbyname and getservbyport can either be guaranteed task safe by
63 -- the operating system, or else return data through a user-provided buffer
64 -- to ensure concurrent uses do not interfere.
66 -- Correspondence tables
68 Levels : constant array (Level_Type) of C.int :=
69 (Socket_Level => SOSC.SOL_SOCKET,
70 IP_Protocol_For_IP_Level => SOSC.IPPROTO_IP,
71 IP_Protocol_For_UDP_Level => SOSC.IPPROTO_UDP,
72 IP_Protocol_For_TCP_Level => SOSC.IPPROTO_TCP);
74 Modes : constant array (Mode_Type) of C.int :=
75 (Socket_Stream => SOSC.SOCK_STREAM,
76 Socket_Datagram => SOSC.SOCK_DGRAM);
78 Shutmodes : constant array (Shutmode_Type) of C.int :=
79 (Shut_Read => SOSC.SHUT_RD,
80 Shut_Write => SOSC.SHUT_WR,
81 Shut_Read_Write => SOSC.SHUT_RDWR);
83 Requests : constant array (Request_Name) of C.int :=
84 (Non_Blocking_IO => SOSC.FIONBIO,
85 N_Bytes_To_Read => SOSC.FIONREAD);
87 Options : constant array (Option_Name) of C.int :=
88 (Keep_Alive => SOSC.SO_KEEPALIVE,
89 Reuse_Address => SOSC.SO_REUSEADDR,
90 Broadcast => SOSC.SO_BROADCAST,
91 Send_Buffer => SOSC.SO_SNDBUF,
92 Receive_Buffer => SOSC.SO_RCVBUF,
93 Linger => SOSC.SO_LINGER,
94 Error => SOSC.SO_ERROR,
95 No_Delay => SOSC.TCP_NODELAY,
96 Add_Membership => SOSC.IP_ADD_MEMBERSHIP,
97 Drop_Membership => SOSC.IP_DROP_MEMBERSHIP,
98 Multicast_If => SOSC.IP_MULTICAST_IF,
99 Multicast_TTL => SOSC.IP_MULTICAST_TTL,
100 Multicast_Loop => SOSC.IP_MULTICAST_LOOP,
101 Receive_Packet_Info => SOSC.IP_PKTINFO,
102 Send_Timeout => SOSC.SO_SNDTIMEO,
103 Receive_Timeout => SOSC.SO_RCVTIMEO);
104 -- ??? Note: for OpenSolaris, Receive_Packet_Info should be IP_RECVPKTINFO,
105 -- but for Linux compatibility this constant is the same as IP_PKTINFO.
107 Flags : constant array (0 .. 3) of C.int :=
108 (0 => SOSC.MSG_OOB, -- Process_Out_Of_Band_Data
109 1 => SOSC.MSG_PEEK, -- Peek_At_Incoming_Data
110 2 => SOSC.MSG_WAITALL, -- Wait_For_A_Full_Reception
111 3 => SOSC.MSG_EOR); -- Send_End_Of_Record
113 Socket_Error_Id : constant Exception_Id := Socket_Error'Identity;
114 Host_Error_Id : constant Exception_Id := Host_Error'Identity;
116 Hex_To_Char : constant String (1 .. 16) := "0123456789ABCDEF";
117 -- Use to print in hexadecimal format
119 -----------------------
120 -- Local subprograms --
121 -----------------------
123 function Resolve_Error
124 (Error_Value : Integer;
125 From_Errno : Boolean := True) return Error_Type;
126 -- Associate an enumeration value (error_type) to en error value (errno).
127 -- From_Errno prevents from mixing h_errno with errno.
129 function To_Name (N : String) return Name_Type;
130 function To_String (HN : Name_Type) return String;
131 -- Conversion functions
133 function To_Int (F : Request_Flag_Type) return C.int;
134 -- Return the int value corresponding to the specified flags combination
136 function Set_Forced_Flags (F : C.int) return C.int;
137 -- Return F with the bits from SOSC.MSG_Forced_Flags forced set
139 function Short_To_Network
140 (S : C.unsigned_short) return C.unsigned_short;
141 pragma Inline (Short_To_Network);
142 -- Convert a port number into a network port number
144 function Network_To_Short
145 (S : C.unsigned_short) return C.unsigned_short
146 renames Short_To_Network;
147 -- Symmetric operation
150 (Val : Inet_Addr_VN_Type;
151 Hex : Boolean := False) return String;
152 -- Output an array of inet address components in hex or decimal mode
154 function Is_IP_Address (Name : String) return Boolean;
155 -- Return true when Name is an IP address in standard dot notation
157 procedure Netdb_Lock;
158 pragma Inline (Netdb_Lock);
159 procedure Netdb_Unlock;
160 pragma Inline (Netdb_Unlock);
161 -- Lock/unlock operation used to protect netdb access for platforms that
162 -- require such protection.
164 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr;
165 procedure To_Inet_Addr
167 Result : out Inet_Addr_Type);
168 -- Conversion functions
170 function To_Host_Entry (E : Hostent_Access) return Host_Entry_Type;
171 -- Conversion function
173 function To_Service_Entry (E : Servent_Access) return Service_Entry_Type;
174 -- Conversion function
176 function Value (S : System.Address) return String;
177 -- Same as Interfaces.C.Strings.Value but taking a System.Address (on VMS,
178 -- chars_ptr is a 32-bit pointer, and here we need a 64-bit version).
180 function To_Timeval (Val : Timeval_Duration) return Timeval;
181 -- Separate Val in seconds and microseconds
183 function To_Duration (Val : Timeval) return Timeval_Duration;
184 -- Reconstruct a Duration value from a Timeval record (seconds and
187 procedure Raise_Socket_Error (Error : Integer);
188 -- Raise Socket_Error with an exception message describing the error code
191 procedure Raise_Host_Error (H_Error : Integer);
192 -- Raise Host_Error exception with message describing error code (note
193 -- hstrerror seems to be obsolete) from h_errno.
195 procedure Narrow (Item : in out Socket_Set_Type);
196 -- Update Last as it may be greater than the real last socket
198 -- Types needed for Datagram_Socket_Stream_Type
200 type Datagram_Socket_Stream_Type is new Root_Stream_Type with record
201 Socket : Socket_Type;
203 From : Sock_Addr_Type;
206 type Datagram_Socket_Stream_Access is
207 access all Datagram_Socket_Stream_Type;
210 (Stream : in out Datagram_Socket_Stream_Type;
211 Item : out Ada.Streams.Stream_Element_Array;
212 Last : out Ada.Streams.Stream_Element_Offset);
215 (Stream : in out Datagram_Socket_Stream_Type;
216 Item : Ada.Streams.Stream_Element_Array);
218 -- Types needed for Stream_Socket_Stream_Type
220 type Stream_Socket_Stream_Type is new Root_Stream_Type with record
221 Socket : Socket_Type;
224 type Stream_Socket_Stream_Access is
225 access all Stream_Socket_Stream_Type;
228 (Stream : in out Stream_Socket_Stream_Type;
229 Item : out Ada.Streams.Stream_Element_Array;
230 Last : out Ada.Streams.Stream_Element_Offset);
233 (Stream : in out Stream_Socket_Stream_Type;
234 Item : Ada.Streams.Stream_Element_Array);
236 procedure Stream_Write
237 (Socket : Socket_Type;
238 Item : Ada.Streams.Stream_Element_Array;
239 To : access Sock_Addr_Type);
240 -- Common implementation for the Write operation of Datagram_Socket_Stream_
241 -- Type and Stream_Socket_Stream_Type.
243 procedure Wait_On_Socket
244 (Socket : Socket_Type;
246 Timeout : Selector_Duration;
247 Selector : access Selector_Type := null;
248 Status : out Selector_Status);
249 -- Common code for variants of socket operations supporting a timeout:
250 -- block in Check_Selector on Socket for at most the indicated timeout.
251 -- If For_Read is True, Socket is added to the read set for this call, else
252 -- it is added to the write set. If no selector is provided, a local one is
253 -- created for this call and destroyed prior to returning.
255 type Sockets_Library_Controller is new Ada.Finalization.Limited_Controlled
257 -- This type is used to generate automatic calls to Initialize and Finalize
258 -- during the elaboration and finalization of this package. A single object
259 -- of this type must exist at library level.
261 function Err_Code_Image (E : Integer) return String;
262 -- Return the value of E surrounded with brackets
264 procedure Initialize (X : in out Sockets_Library_Controller);
265 procedure Finalize (X : in out Sockets_Library_Controller);
267 procedure Normalize_Empty_Socket_Set (S : in out Socket_Set_Type);
268 -- If S is the empty set (detected by Last = No_Socket), make sure its
269 -- fd_set component is actually cleared. Note that the case where it is
270 -- not can occur for an uninitialized Socket_Set_Type object.
272 function Is_Open (S : Selector_Type) return Boolean;
273 -- Return True for an "open" Selector_Type object, i.e. one for which
274 -- Create_Selector has been called and Close_Selector has not been called,
275 -- or the null selector.
281 function "+" (L, R : Request_Flag_Type) return Request_Flag_Type is
290 procedure Abort_Selector (Selector : Selector_Type) is
294 if not Is_Open (Selector) then
295 raise Program_Error with "closed selector";
297 elsif Selector.Is_Null then
298 raise Program_Error with "null selector";
302 -- Send one byte to unblock select system call
304 Res := Signalling_Fds.Write (C.int (Selector.W_Sig_Socket));
306 if Res = Failure then
307 Raise_Socket_Error (Socket_Errno);
315 procedure Accept_Socket
316 (Server : Socket_Type;
317 Socket : out Socket_Type;
318 Address : out Sock_Addr_Type)
321 Sin : aliased Sockaddr_In;
322 Len : aliased C.int := Sin'Size / 8;
325 Res := C_Accept (C.int (Server), Sin'Address, Len'Access);
327 if Res = Failure then
328 Raise_Socket_Error (Socket_Errno);
331 Socket := Socket_Type (Res);
333 To_Inet_Addr (Sin.Sin_Addr, Address.Addr);
334 Address.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
341 procedure Accept_Socket
342 (Server : Socket_Type;
343 Socket : out Socket_Type;
344 Address : out Sock_Addr_Type;
345 Timeout : Selector_Duration;
346 Selector : access Selector_Type := null;
347 Status : out Selector_Status)
350 if Selector /= null and then not Is_Open (Selector.all) then
351 raise Program_Error with "closed selector";
354 -- Wait for socket to become available for reading
360 Selector => Selector,
363 -- Accept connection if available
365 if Status = Completed then
366 Accept_Socket (Server, Socket, Address);
377 (E : Host_Entry_Type;
378 N : Positive := 1) return Inet_Addr_Type
381 return E.Addresses (N);
384 ----------------------
385 -- Addresses_Length --
386 ----------------------
388 function Addresses_Length (E : Host_Entry_Type) return Natural is
390 return E.Addresses_Length;
391 end Addresses_Length;
398 (E : Host_Entry_Type;
399 N : Positive := 1) return String
402 return To_String (E.Aliases (N));
410 (S : Service_Entry_Type;
411 N : Positive := 1) return String
414 return To_String (S.Aliases (N));
421 function Aliases_Length (E : Host_Entry_Type) return Natural is
423 return E.Aliases_Length;
430 function Aliases_Length (S : Service_Entry_Type) return Natural is
432 return S.Aliases_Length;
439 procedure Bind_Socket
440 (Socket : Socket_Type;
441 Address : Sock_Addr_Type)
444 Sin : aliased Sockaddr_In;
445 Len : constant C.int := Sin'Size / 8;
446 -- This assumes that Address.Family = Family_Inet???
449 if Address.Family = Family_Inet6 then
450 raise Socket_Error with "IPv6 not supported";
453 Set_Family (Sin.Sin_Family, Address.Family);
454 Set_Address (Sin'Unchecked_Access, To_In_Addr (Address.Addr));
456 (Sin'Unchecked_Access,
457 Short_To_Network (C.unsigned_short (Address.Port)));
459 Res := C_Bind (C.int (Socket), Sin'Address, Len);
461 if Res = Failure then
462 Raise_Socket_Error (Socket_Errno);
470 procedure Check_Selector
471 (Selector : Selector_Type;
472 R_Socket_Set : in out Socket_Set_Type;
473 W_Socket_Set : in out Socket_Set_Type;
474 Status : out Selector_Status;
475 Timeout : Selector_Duration := Forever)
477 E_Socket_Set : Socket_Set_Type;
480 (Selector, R_Socket_Set, W_Socket_Set, E_Socket_Set, Status, Timeout);
487 procedure Check_Selector
488 (Selector : Selector_Type;
489 R_Socket_Set : in out Socket_Set_Type;
490 W_Socket_Set : in out Socket_Set_Type;
491 E_Socket_Set : in out Socket_Set_Type;
492 Status : out Selector_Status;
493 Timeout : Selector_Duration := Forever)
497 RSig : Socket_Type := No_Socket;
498 TVal : aliased Timeval;
499 TPtr : Timeval_Access;
502 if not Is_Open (Selector) then
503 raise Program_Error with "closed selector";
508 -- No timeout or Forever is indicated by a null timeval pointer
510 if Timeout = Forever then
513 TVal := To_Timeval (Timeout);
514 TPtr := TVal'Unchecked_Access;
517 -- Add read signalling socket, if present
519 if not Selector.Is_Null then
520 RSig := Selector.R_Sig_Socket;
521 Set (R_Socket_Set, RSig);
524 Last := C.int'Max (C.int'Max (C.int (R_Socket_Set.Last),
525 C.int (W_Socket_Set.Last)),
526 C.int (E_Socket_Set.Last));
528 -- Zero out fd_set for empty Socket_Set_Type objects
530 Normalize_Empty_Socket_Set (R_Socket_Set);
531 Normalize_Empty_Socket_Set (W_Socket_Set);
532 Normalize_Empty_Socket_Set (E_Socket_Set);
537 R_Socket_Set.Set'Access,
538 W_Socket_Set.Set'Access,
539 E_Socket_Set.Set'Access,
542 if Res = Failure then
543 Raise_Socket_Error (Socket_Errno);
546 -- If Select was resumed because of read signalling socket, read this
547 -- data and remove socket from set.
549 if RSig /= No_Socket and then Is_Set (R_Socket_Set, RSig) then
550 Clear (R_Socket_Set, RSig);
552 Res := Signalling_Fds.Read (C.int (RSig));
554 if Res = Failure then
555 Raise_Socket_Error (Socket_Errno);
564 -- Update socket sets in regard to their new contents
566 Narrow (R_Socket_Set);
567 Narrow (W_Socket_Set);
568 Narrow (E_Socket_Set);
576 (Item : in out Socket_Set_Type;
577 Socket : Socket_Type)
579 Last : aliased C.int := C.int (Item.Last);
581 if Item.Last /= No_Socket then
582 Remove_Socket_From_Set (Item.Set'Access, C.int (Socket));
583 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
584 Item.Last := Socket_Type (Last);
592 procedure Close_Selector (Selector : in out Selector_Type) is
594 -- Nothing to do if selector already in closed state
596 if Selector.Is_Null or else not Is_Open (Selector) then
600 -- Close the signalling file descriptors used internally for the
601 -- implementation of Abort_Selector.
603 Signalling_Fds.Close (C.int (Selector.R_Sig_Socket));
604 Signalling_Fds.Close (C.int (Selector.W_Sig_Socket));
606 -- Reset R_Sig_Socket and W_Sig_Socket to No_Socket to ensure that any
607 -- (erroneous) subsequent attempt to use this selector properly fails.
609 Selector.R_Sig_Socket := No_Socket;
610 Selector.W_Sig_Socket := No_Socket;
617 procedure Close_Socket (Socket : Socket_Type) is
621 Res := C_Close (C.int (Socket));
623 if Res = Failure then
624 Raise_Socket_Error (Socket_Errno);
632 procedure Connect_Socket
633 (Socket : Socket_Type;
634 Server : Sock_Addr_Type)
637 Sin : aliased Sockaddr_In;
638 Len : constant C.int := Sin'Size / 8;
641 if Server.Family = Family_Inet6 then
642 raise Socket_Error with "IPv6 not supported";
645 Set_Family (Sin.Sin_Family, Server.Family);
646 Set_Address (Sin'Unchecked_Access, To_In_Addr (Server.Addr));
648 (Sin'Unchecked_Access,
649 Short_To_Network (C.unsigned_short (Server.Port)));
651 Res := C_Connect (C.int (Socket), Sin'Address, Len);
653 if Res = Failure then
654 Raise_Socket_Error (Socket_Errno);
662 procedure Connect_Socket
663 (Socket : Socket_Type;
664 Server : Sock_Addr_Type;
665 Timeout : Selector_Duration;
666 Selector : access Selector_Type := null;
667 Status : out Selector_Status)
670 -- Used to set Socket to non-blocking I/O
673 if Selector /= null and then not Is_Open (Selector.all) then
674 raise Program_Error with "closed selector";
677 -- Set the socket to non-blocking I/O
679 Req := (Name => Non_Blocking_IO, Enabled => True);
680 Control_Socket (Socket, Request => Req);
682 -- Start operation (non-blocking), will raise Socket_Error with
686 Connect_Socket (Socket, Server);
688 when E : Socket_Error =>
689 if Resolve_Exception (E) = Operation_Now_In_Progress then
696 -- Wait for socket to become available for writing
702 Selector => Selector,
705 -- Reset the socket to blocking I/O
707 Req := (Name => Non_Blocking_IO, Enabled => False);
708 Control_Socket (Socket, Request => Req);
715 procedure Control_Socket
716 (Socket : Socket_Type;
717 Request : in out Request_Type)
724 when Non_Blocking_IO =>
725 Arg := C.int (Boolean'Pos (Request.Enabled));
727 when N_Bytes_To_Read =>
732 (C.int (Socket), Requests (Request.Name), Arg'Unchecked_Access);
734 if Res = Failure then
735 Raise_Socket_Error (Socket_Errno);
739 when Non_Blocking_IO =>
742 when N_Bytes_To_Read =>
743 Request.Size := Natural (Arg);
752 (Source : Socket_Set_Type;
753 Target : out Socket_Set_Type)
759 ---------------------
760 -- Create_Selector --
761 ---------------------
763 procedure Create_Selector (Selector : out Selector_Type) is
764 Two_Fds : aliased Fd_Pair;
768 if Is_Open (Selector) then
769 -- Raise exception to prevent socket descriptor leak
771 raise Program_Error with "selector already open";
774 -- We open two signalling file descriptors. One of them is used to send
775 -- data to the other, which is included in a C_Select socket set. The
776 -- communication is used to force a call to C_Select to complete, and
777 -- the waiting task to resume its execution.
779 Res := Signalling_Fds.Create (Two_Fds'Access);
781 if Res = Failure then
782 Raise_Socket_Error (Socket_Errno);
785 Selector.R_Sig_Socket := Socket_Type (Two_Fds (Read_End));
786 Selector.W_Sig_Socket := Socket_Type (Two_Fds (Write_End));
793 procedure Create_Socket
794 (Socket : out Socket_Type;
795 Family : Family_Type := Family_Inet;
796 Mode : Mode_Type := Socket_Stream)
801 Res := C_Socket (Families (Family), Modes (Mode), 0);
803 if Res = Failure then
804 Raise_Socket_Error (Socket_Errno);
807 Socket := Socket_Type (Res);
814 procedure Empty (Item : out Socket_Set_Type) is
816 Reset_Socket_Set (Item.Set'Access);
817 Item.Last := No_Socket;
824 function Err_Code_Image (E : Integer) return String is
825 Msg : String := E'Img & "] ";
827 Msg (Msg'First) := '[';
835 procedure Finalize (X : in out Sockets_Library_Controller) is
836 pragma Unreferenced (X);
839 -- Finalization operation for the GNAT.Sockets package
848 procedure Finalize is
850 -- This is a dummy placeholder for an obsolete API.
851 -- The real finalization actions are in Initialize primitive operation
852 -- of Sockets_Library_Controller.
862 (Item : in out Socket_Set_Type;
863 Socket : out Socket_Type)
866 L : aliased C.int := C.int (Item.Last);
869 if Item.Last /= No_Socket then
871 (Item.Set'Access, Last => L'Access, Socket => S'Access);
872 Item.Last := Socket_Type (L);
873 Socket := Socket_Type (S);
884 (Stream : not null Stream_Access) return Sock_Addr_Type
887 if Stream.all in Datagram_Socket_Stream_Type then
888 return Datagram_Socket_Stream_Type (Stream.all).From;
890 return Get_Peer_Name (Stream_Socket_Stream_Type (Stream.all).Socket);
894 -------------------------
895 -- Get_Host_By_Address --
896 -------------------------
898 function Get_Host_By_Address
899 (Address : Inet_Addr_Type;
900 Family : Family_Type := Family_Inet) return Host_Entry_Type
902 pragma Unreferenced (Family);
904 HA : aliased In_Addr := To_In_Addr (Address);
905 Buflen : constant C.int := Netdb_Buffer_Size;
906 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
907 Res : aliased Hostent;
913 if C_Gethostbyaddr (HA'Address, HA'Size / 8, SOSC.AF_INET,
914 Res'Access, Buf'Address, Buflen, Err'Access) /= 0
917 Raise_Host_Error (Integer (Err));
920 return H : constant Host_Entry_Type :=
921 To_Host_Entry (Res'Unchecked_Access)
925 end Get_Host_By_Address;
927 ----------------------
928 -- Get_Host_By_Name --
929 ----------------------
931 function Get_Host_By_Name (Name : String) return Host_Entry_Type is
933 -- Detect IP address name and redirect to Inet_Addr
935 if Is_IP_Address (Name) then
936 return Get_Host_By_Address (Inet_Addr (Name));
940 HN : constant C.char_array := C.To_C (Name);
941 Buflen : constant C.int := Netdb_Buffer_Size;
942 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
943 Res : aliased Hostent;
950 (HN, Res'Access, Buf'Address, Buflen, Err'Access) /= 0
953 Raise_Host_Error (Integer (Err));
956 return H : constant Host_Entry_Type :=
957 To_Host_Entry (Res'Unchecked_Access)
962 end Get_Host_By_Name;
968 function Get_Peer_Name (Socket : Socket_Type) return Sock_Addr_Type is
969 Sin : aliased Sockaddr_In;
970 Len : aliased C.int := Sin'Size / 8;
971 Res : Sock_Addr_Type (Family_Inet);
974 if C_Getpeername (C.int (Socket), Sin'Address, Len'Access) = Failure then
975 Raise_Socket_Error (Socket_Errno);
978 To_Inet_Addr (Sin.Sin_Addr, Res.Addr);
979 Res.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
984 -------------------------
985 -- Get_Service_By_Name --
986 -------------------------
988 function Get_Service_By_Name
990 Protocol : String) return Service_Entry_Type
992 SN : constant C.char_array := C.To_C (Name);
993 SP : constant C.char_array := C.To_C (Protocol);
994 Buflen : constant C.int := Netdb_Buffer_Size;
995 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
996 Res : aliased Servent;
1001 if C_Getservbyname (SN, SP, Res'Access, Buf'Address, Buflen) /= 0 then
1003 raise Service_Error with "Service not found";
1006 -- Translate from the C format to the API format
1008 return S : constant Service_Entry_Type :=
1009 To_Service_Entry (Res'Unchecked_Access)
1013 end Get_Service_By_Name;
1015 -------------------------
1016 -- Get_Service_By_Port --
1017 -------------------------
1019 function Get_Service_By_Port
1021 Protocol : String) return Service_Entry_Type
1023 SP : constant C.char_array := C.To_C (Protocol);
1024 Buflen : constant C.int := Netdb_Buffer_Size;
1025 Buf : aliased C.char_array (1 .. Netdb_Buffer_Size);
1026 Res : aliased Servent;
1032 (C.int (Short_To_Network (C.unsigned_short (Port))), SP,
1033 Res'Access, Buf'Address, Buflen) /= 0
1036 raise Service_Error with "Service not found";
1039 -- Translate from the C format to the API format
1041 return S : constant Service_Entry_Type :=
1042 To_Service_Entry (Res'Unchecked_Access)
1046 end Get_Service_By_Port;
1048 ---------------------
1049 -- Get_Socket_Name --
1050 ---------------------
1052 function Get_Socket_Name
1053 (Socket : Socket_Type) return Sock_Addr_Type
1055 Sin : aliased Sockaddr_In;
1056 Len : aliased C.int := Sin'Size / 8;
1058 Addr : Sock_Addr_Type := No_Sock_Addr;
1061 Res := C_Getsockname (C.int (Socket), Sin'Address, Len'Access);
1063 if Res /= Failure then
1064 To_Inet_Addr (Sin.Sin_Addr, Addr.Addr);
1065 Addr.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1069 end Get_Socket_Name;
1071 -----------------------
1072 -- Get_Socket_Option --
1073 -----------------------
1075 function Get_Socket_Option
1076 (Socket : Socket_Type;
1077 Level : Level_Type := Socket_Level;
1078 Name : Option_Name) return Option_Type
1080 use type C.unsigned_char;
1082 V8 : aliased Two_Ints;
1084 V1 : aliased C.unsigned_char;
1085 VT : aliased Timeval;
1086 Len : aliased C.int;
1087 Add : System.Address;
1089 Opt : Option_Type (Name);
1093 when Multicast_Loop |
1095 Receive_Packet_Info =>
1130 if Res = Failure then
1131 Raise_Socket_Error (Socket_Errno);
1139 Opt.Enabled := (V4 /= 0);
1142 Opt.Enabled := (V8 (V8'First) /= 0);
1143 Opt.Seconds := Natural (V8 (V8'Last));
1147 Opt.Size := Natural (V4);
1150 Opt.Error := Resolve_Error (Integer (V4));
1152 when Add_Membership |
1154 To_Inet_Addr (To_In_Addr (V8 (V8'First)), Opt.Multicast_Address);
1155 To_Inet_Addr (To_In_Addr (V8 (V8'Last)), Opt.Local_Interface);
1157 when Multicast_If =>
1158 To_Inet_Addr (To_In_Addr (V4), Opt.Outgoing_If);
1160 when Multicast_TTL =>
1161 Opt.Time_To_Live := Integer (V1);
1163 when Multicast_Loop |
1164 Receive_Packet_Info =>
1165 Opt.Enabled := (V1 /= 0);
1169 Opt.Timeout := To_Duration (VT);
1173 end Get_Socket_Option;
1179 function Host_Name return String is
1180 Name : aliased C.char_array (1 .. 64);
1184 Res := C_Gethostname (Name'Address, Name'Length);
1186 if Res = Failure then
1187 Raise_Socket_Error (Socket_Errno);
1190 return C.To_Ada (Name);
1198 (Val : Inet_Addr_VN_Type;
1199 Hex : Boolean := False) return String
1201 -- The largest Inet_Addr_Comp_Type image occurs with IPv4. It
1202 -- has at most a length of 3 plus one '.' character.
1204 Buffer : String (1 .. 4 * Val'Length);
1205 Length : Natural := 1;
1206 Separator : Character;
1208 procedure Img10 (V : Inet_Addr_Comp_Type);
1209 -- Append to Buffer image of V in decimal format
1211 procedure Img16 (V : Inet_Addr_Comp_Type);
1212 -- Append to Buffer image of V in hexadecimal format
1218 procedure Img10 (V : Inet_Addr_Comp_Type) is
1219 Img : constant String := V'Img;
1220 Len : constant Natural := Img'Length - 1;
1222 Buffer (Length .. Length + Len - 1) := Img (2 .. Img'Last);
1223 Length := Length + Len;
1230 procedure Img16 (V : Inet_Addr_Comp_Type) is
1232 Buffer (Length) := Hex_To_Char (Natural (V / 16) + 1);
1233 Buffer (Length + 1) := Hex_To_Char (Natural (V mod 16) + 1);
1234 Length := Length + 2;
1237 -- Start of processing for Image
1240 Separator := (if Hex then ':' else '.');
1242 for J in Val'Range loop
1249 if J /= Val'Last then
1250 Buffer (Length) := Separator;
1251 Length := Length + 1;
1255 return Buffer (1 .. Length - 1);
1262 function Image (Value : Inet_Addr_Type) return String is
1264 if Value.Family = Family_Inet then
1265 return Image (Inet_Addr_VN_Type (Value.Sin_V4), Hex => False);
1267 return Image (Inet_Addr_VN_Type (Value.Sin_V6), Hex => True);
1275 function Image (Value : Sock_Addr_Type) return String is
1276 Port : constant String := Value.Port'Img;
1278 return Image (Value.Addr) & ':' & Port (2 .. Port'Last);
1285 function Image (Socket : Socket_Type) return String is
1294 function Image (Item : Socket_Set_Type) return String is
1295 Socket_Set : Socket_Set_Type := Item;
1299 Last_Img : constant String := Socket_Set.Last'Img;
1301 (1 .. (Integer (Socket_Set.Last) + 1) * Last_Img'Length);
1302 Index : Positive := 1;
1303 Socket : Socket_Type;
1306 while not Is_Empty (Socket_Set) loop
1307 Get (Socket_Set, Socket);
1310 Socket_Img : constant String := Socket'Img;
1312 Buffer (Index .. Index + Socket_Img'Length - 1) := Socket_Img;
1313 Index := Index + Socket_Img'Length;
1317 return "[" & Last_Img & "]" & Buffer (1 .. Index - 1);
1325 function Inet_Addr (Image : String) return Inet_Addr_Type is
1327 use Interfaces.C.Strings;
1329 Img : aliased char_array := To_C (Image);
1330 Addr : aliased C.int;
1332 Result : Inet_Addr_Type;
1335 -- Special case for an empty Image as on some platforms (e.g. Windows)
1336 -- calling Inet_Addr("") will not return an error.
1339 Raise_Socket_Error (SOSC.EINVAL);
1342 Res := Inet_Pton (SOSC.AF_INET, Img'Address, Addr'Address);
1345 Raise_Socket_Error (Socket_Errno);
1348 Raise_Socket_Error (SOSC.EINVAL);
1351 To_Inet_Addr (To_In_Addr (Addr), Result);
1359 procedure Initialize (X : in out Sockets_Library_Controller) is
1360 pragma Unreferenced (X);
1370 procedure Initialize (Process_Blocking_IO : Boolean) is
1371 Expected : constant Boolean := not SOSC.Thread_Blocking_IO;
1374 if Process_Blocking_IO /= Expected then
1375 raise Socket_Error with
1376 "incorrect Process_Blocking_IO setting, expected " & Expected'Img;
1379 -- This is a dummy placeholder for an obsolete API
1381 -- Real initialization actions are in Initialize primitive operation
1382 -- of Sockets_Library_Controller.
1391 procedure Initialize is
1393 -- This is a dummy placeholder for an obsolete API
1395 -- Real initialization actions are in Initialize primitive operation
1396 -- of Sockets_Library_Controller.
1405 function Is_Empty (Item : Socket_Set_Type) return Boolean is
1407 return Item.Last = No_Socket;
1414 function Is_IP_Address (Name : String) return Boolean is
1416 for J in Name'Range loop
1418 and then Name (J) not in '0' .. '9'
1431 function Is_Open (S : Selector_Type) return Boolean is
1437 -- Either both controlling socket descriptors are valid (case of an
1438 -- open selector) or neither (case of a closed selector).
1440 pragma Assert ((S.R_Sig_Socket /= No_Socket)
1442 (S.W_Sig_Socket /= No_Socket));
1444 return S.R_Sig_Socket /= No_Socket;
1453 (Item : Socket_Set_Type;
1454 Socket : Socket_Type) return Boolean
1457 return Item.Last /= No_Socket
1458 and then Socket <= Item.Last
1459 and then Is_Socket_In_Set (Item.Set'Access, C.int (Socket)) /= 0;
1466 procedure Listen_Socket
1467 (Socket : Socket_Type;
1468 Length : Natural := 15)
1470 Res : constant C.int := C_Listen (C.int (Socket), C.int (Length));
1472 if Res = Failure then
1473 Raise_Socket_Error (Socket_Errno);
1481 procedure Narrow (Item : in out Socket_Set_Type) is
1482 Last : aliased C.int := C.int (Item.Last);
1484 if Item.Last /= No_Socket then
1485 Last_Socket_In_Set (Item.Set'Access, Last'Unchecked_Access);
1486 Item.Last := Socket_Type (Last);
1494 procedure Netdb_Lock is
1496 if Need_Netdb_Lock then
1497 System.Task_Lock.Lock;
1505 procedure Netdb_Unlock is
1507 if Need_Netdb_Lock then
1508 System.Task_Lock.Unlock;
1512 --------------------------------
1513 -- Normalize_Empty_Socket_Set --
1514 --------------------------------
1516 procedure Normalize_Empty_Socket_Set (S : in out Socket_Set_Type) is
1518 if S.Last = No_Socket then
1519 Reset_Socket_Set (S.Set'Access);
1521 end Normalize_Empty_Socket_Set;
1527 function Official_Name (E : Host_Entry_Type) return String is
1529 return To_String (E.Official);
1536 function Official_Name (S : Service_Entry_Type) return String is
1538 return To_String (S.Official);
1541 --------------------
1542 -- Wait_On_Socket --
1543 --------------------
1545 procedure Wait_On_Socket
1546 (Socket : Socket_Type;
1548 Timeout : Selector_Duration;
1549 Selector : access Selector_Type := null;
1550 Status : out Selector_Status)
1552 type Local_Selector_Access is access Selector_Type;
1553 for Local_Selector_Access'Storage_Size use Selector_Type'Size;
1555 S : Selector_Access;
1556 -- Selector to use for waiting
1558 R_Fd_Set : Socket_Set_Type;
1559 W_Fd_Set : Socket_Set_Type;
1562 -- Create selector if not provided by the user
1564 if Selector = null then
1566 Local_S : constant Local_Selector_Access := new Selector_Type;
1568 S := Local_S.all'Unchecked_Access;
1569 Create_Selector (S.all);
1573 S := Selector.all'Access;
1577 Set (R_Fd_Set, Socket);
1579 Set (W_Fd_Set, Socket);
1582 Check_Selector (S.all, R_Fd_Set, W_Fd_Set, Status, Timeout);
1584 if Selector = null then
1585 Close_Selector (S.all);
1593 function Port_Number (S : Service_Entry_Type) return Port_Type is
1602 function Protocol_Name (S : Service_Entry_Type) return String is
1604 return To_String (S.Protocol);
1607 ----------------------
1608 -- Raise_Host_Error --
1609 ----------------------
1611 procedure Raise_Host_Error (H_Error : Integer) is
1613 raise Host_Error with
1614 Err_Code_Image (H_Error)
1615 & C.Strings.Value (Host_Error_Messages.Host_Error_Message (H_Error));
1616 end Raise_Host_Error;
1618 ------------------------
1619 -- Raise_Socket_Error --
1620 ------------------------
1622 procedure Raise_Socket_Error (Error : Integer) is
1623 use type C.Strings.chars_ptr;
1625 raise Socket_Error with
1626 Err_Code_Image (Error)
1627 & C.Strings.Value (Socket_Error_Message (Error));
1628 end Raise_Socket_Error;
1635 (Stream : in out Datagram_Socket_Stream_Type;
1636 Item : out Ada.Streams.Stream_Element_Array;
1637 Last : out Ada.Streams.Stream_Element_Offset)
1639 First : Ada.Streams.Stream_Element_Offset := Item'First;
1640 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1641 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1647 Item (First .. Max),
1653 -- Exit when all or zero data received. Zero means that the socket
1656 exit when Index < First or else Index = Max;
1667 (Stream : in out Stream_Socket_Stream_Type;
1668 Item : out Ada.Streams.Stream_Element_Array;
1669 Last : out Ada.Streams.Stream_Element_Offset)
1671 pragma Warnings (Off, Stream);
1673 First : Ada.Streams.Stream_Element_Offset := Item'First;
1674 Index : Ada.Streams.Stream_Element_Offset := First - 1;
1675 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
1679 Receive_Socket (Stream.Socket, Item (First .. Max), Index);
1682 -- Exit when all or zero data received. Zero means that the socket
1685 exit when Index < First or else Index = Max;
1691 --------------------
1692 -- Receive_Socket --
1693 --------------------
1695 procedure Receive_Socket
1696 (Socket : Socket_Type;
1697 Item : out Ada.Streams.Stream_Element_Array;
1698 Last : out Ada.Streams.Stream_Element_Offset;
1699 Flags : Request_Flag_Type := No_Request_Flag)
1705 C_Recv (C.int (Socket), Item'Address, Item'Length, To_Int (Flags));
1707 if Res = Failure then
1708 Raise_Socket_Error (Socket_Errno);
1711 Last := Last_Index (First => Item'First, Count => size_t (Res));
1714 --------------------
1715 -- Receive_Socket --
1716 --------------------
1718 procedure Receive_Socket
1719 (Socket : Socket_Type;
1720 Item : out Ada.Streams.Stream_Element_Array;
1721 Last : out Ada.Streams.Stream_Element_Offset;
1722 From : out Sock_Addr_Type;
1723 Flags : Request_Flag_Type := No_Request_Flag)
1726 Sin : aliased Sockaddr_In;
1727 Len : aliased C.int := Sin'Size / 8;
1739 if Res = Failure then
1740 Raise_Socket_Error (Socket_Errno);
1743 Last := Last_Index (First => Item'First, Count => size_t (Res));
1745 To_Inet_Addr (Sin.Sin_Addr, From.Addr);
1746 From.Port := Port_Type (Network_To_Short (Sin.Sin_Port));
1749 --------------------
1750 -- Receive_Vector --
1751 --------------------
1753 procedure Receive_Vector
1754 (Socket : Socket_Type;
1755 Vector : Vector_Type;
1756 Count : out Ada.Streams.Stream_Element_Count;
1757 Flags : Request_Flag_Type := No_Request_Flag)
1762 (Msg_Name => System.Null_Address,
1764 Msg_Iov => Vector'Address,
1766 -- recvmsg(2) returns EMSGSIZE on Linux (and probably on other
1767 -- platforms) when the supplied vector is longer than IOV_MAX,
1768 -- so use minimum of the two lengths.
1770 Msg_Iovlen => SOSC.Msg_Iovlen_T'Min
1771 (Vector'Length, SOSC.IOV_MAX),
1773 Msg_Control => System.Null_Address,
1774 Msg_Controllen => 0,
1784 if Res = ssize_t (Failure) then
1785 Raise_Socket_Error (Socket_Errno);
1788 Count := Ada.Streams.Stream_Element_Count (Res);
1795 function Resolve_Error
1796 (Error_Value : Integer;
1797 From_Errno : Boolean := True) return Error_Type
1799 use GNAT.Sockets.SOSC;
1802 if not From_Errno then
1804 when SOSC.HOST_NOT_FOUND => return Unknown_Host;
1805 when SOSC.TRY_AGAIN => return Host_Name_Lookup_Failure;
1806 when SOSC.NO_RECOVERY => return Non_Recoverable_Error;
1807 when SOSC.NO_DATA => return Unknown_Server_Error;
1808 when others => return Cannot_Resolve_Error;
1812 -- Special case: EAGAIN may be the same value as EWOULDBLOCK, so we
1813 -- can't include it in the case statement below.
1815 pragma Warnings (Off);
1816 -- Condition "EAGAIN /= EWOULDBLOCK" is known at compile time
1818 if EAGAIN /= EWOULDBLOCK and then Error_Value = EAGAIN then
1819 return Resource_Temporarily_Unavailable;
1822 -- This is not a case statement because if a particular error
1823 -- number constant is not defined, s-oscons-tmplt.c defines
1824 -- it to -1. If multiple constants are not defined, they
1825 -- would each be -1 and result in a "duplicate value in case" error.
1827 -- But we have to leave warnings off because the compiler is also
1828 -- smart enough to note that when two errnos have the same value,
1829 -- the second if condition is useless.
1830 if Error_Value = ENOERROR then
1832 elsif Error_Value = EACCES then
1833 return Permission_Denied;
1834 elsif Error_Value = EADDRINUSE then
1835 return Address_Already_In_Use;
1836 elsif Error_Value = EADDRNOTAVAIL then
1837 return Cannot_Assign_Requested_Address;
1838 elsif Error_Value = EAFNOSUPPORT then
1839 return Address_Family_Not_Supported_By_Protocol;
1840 elsif Error_Value = EALREADY then
1841 return Operation_Already_In_Progress;
1842 elsif Error_Value = EBADF then
1843 return Bad_File_Descriptor;
1844 elsif Error_Value = ECONNABORTED then
1845 return Software_Caused_Connection_Abort;
1846 elsif Error_Value = ECONNREFUSED then
1847 return Connection_Refused;
1848 elsif Error_Value = ECONNRESET then
1849 return Connection_Reset_By_Peer;
1850 elsif Error_Value = EDESTADDRREQ then
1851 return Destination_Address_Required;
1852 elsif Error_Value = EFAULT then
1854 elsif Error_Value = EHOSTDOWN then
1855 return Host_Is_Down;
1856 elsif Error_Value = EHOSTUNREACH then
1857 return No_Route_To_Host;
1858 elsif Error_Value = EINPROGRESS then
1859 return Operation_Now_In_Progress;
1860 elsif Error_Value = EINTR then
1861 return Interrupted_System_Call;
1862 elsif Error_Value = EINVAL then
1863 return Invalid_Argument;
1864 elsif Error_Value = EIO then
1865 return Input_Output_Error;
1866 elsif Error_Value = EISCONN then
1867 return Transport_Endpoint_Already_Connected;
1868 elsif Error_Value = ELOOP then
1869 return Too_Many_Symbolic_Links;
1870 elsif Error_Value = EMFILE then
1871 return Too_Many_Open_Files;
1872 elsif Error_Value = EMSGSIZE then
1873 return Message_Too_Long;
1874 elsif Error_Value = ENAMETOOLONG then
1875 return File_Name_Too_Long;
1876 elsif Error_Value = ENETDOWN then
1877 return Network_Is_Down;
1878 elsif Error_Value = ENETRESET then
1879 return Network_Dropped_Connection_Because_Of_Reset;
1880 elsif Error_Value = ENETUNREACH then
1881 return Network_Is_Unreachable;
1882 elsif Error_Value = ENOBUFS then
1883 return No_Buffer_Space_Available;
1884 elsif Error_Value = ENOPROTOOPT then
1885 return Protocol_Not_Available;
1886 elsif Error_Value = ENOTCONN then
1887 return Transport_Endpoint_Not_Connected;
1888 elsif Error_Value = ENOTSOCK then
1889 return Socket_Operation_On_Non_Socket;
1890 elsif Error_Value = EOPNOTSUPP then
1891 return Operation_Not_Supported;
1892 elsif Error_Value = EPFNOSUPPORT then
1893 return Protocol_Family_Not_Supported;
1894 elsif Error_Value = EPIPE then
1896 elsif Error_Value = EPROTONOSUPPORT then
1897 return Protocol_Not_Supported;
1898 elsif Error_Value = EPROTOTYPE then
1899 return Protocol_Wrong_Type_For_Socket;
1900 elsif Error_Value = ESHUTDOWN then
1901 return Cannot_Send_After_Transport_Endpoint_Shutdown;
1902 elsif Error_Value = ESOCKTNOSUPPORT then
1903 return Socket_Type_Not_Supported;
1904 elsif Error_Value = ETIMEDOUT then
1905 return Connection_Timed_Out;
1906 elsif Error_Value = ETOOMANYREFS then
1907 return Too_Many_References;
1908 elsif Error_Value = EWOULDBLOCK then
1909 return Resource_Temporarily_Unavailable;
1911 return Cannot_Resolve_Error;
1913 pragma Warnings (On);
1917 -----------------------
1918 -- Resolve_Exception --
1919 -----------------------
1921 function Resolve_Exception
1922 (Occurrence : Exception_Occurrence) return Error_Type
1924 Id : constant Exception_Id := Exception_Identity (Occurrence);
1925 Msg : constant String := Exception_Message (Occurrence);
1932 while First <= Msg'Last
1933 and then Msg (First) not in '0' .. '9'
1938 if First > Msg'Last then
1939 return Cannot_Resolve_Error;
1943 while Last < Msg'Last
1944 and then Msg (Last + 1) in '0' .. '9'
1949 Val := Integer'Value (Msg (First .. Last));
1951 if Id = Socket_Error_Id then
1952 return Resolve_Error (Val);
1954 elsif Id = Host_Error_Id then
1955 return Resolve_Error (Val, False);
1958 return Cannot_Resolve_Error;
1960 end Resolve_Exception;
1966 procedure Send_Socket
1967 (Socket : Socket_Type;
1968 Item : Ada.Streams.Stream_Element_Array;
1969 Last : out Ada.Streams.Stream_Element_Offset;
1970 Flags : Request_Flag_Type := No_Request_Flag)
1973 Send_Socket (Socket, Item, Last, To => null, Flags => Flags);
1980 procedure Send_Socket
1981 (Socket : Socket_Type;
1982 Item : Ada.Streams.Stream_Element_Array;
1983 Last : out Ada.Streams.Stream_Element_Offset;
1984 To : Sock_Addr_Type;
1985 Flags : Request_Flag_Type := No_Request_Flag)
1989 (Socket, Item, Last, To => To'Unrestricted_Access, Flags => Flags);
1996 procedure Send_Socket
1997 (Socket : Socket_Type;
1998 Item : Ada.Streams.Stream_Element_Array;
1999 Last : out Ada.Streams.Stream_Element_Offset;
2000 To : access Sock_Addr_Type;
2001 Flags : Request_Flag_Type := No_Request_Flag)
2005 Sin : aliased Sockaddr_In;
2006 C_To : System.Address;
2011 Set_Family (Sin.Sin_Family, To.Family);
2012 Set_Address (Sin'Unchecked_Access, To_In_Addr (To.Addr));
2014 (Sin'Unchecked_Access,
2015 Short_To_Network (C.unsigned_short (To.Port)));
2016 C_To := Sin'Address;
2017 Len := Sin'Size / 8;
2020 C_To := System.Null_Address;
2028 Set_Forced_Flags (To_Int (Flags)),
2032 if Res = Failure then
2033 Raise_Socket_Error (Socket_Errno);
2036 Last := Last_Index (First => Item'First, Count => size_t (Res));
2043 procedure Send_Vector
2044 (Socket : Socket_Type;
2045 Vector : Vector_Type;
2046 Count : out Ada.Streams.Stream_Element_Count;
2047 Flags : Request_Flag_Type := No_Request_Flag)
2053 Iov_Count : SOSC.Msg_Iovlen_T;
2054 This_Iov_Count : SOSC.Msg_Iovlen_T;
2060 while Iov_Count < Vector'Length loop
2062 pragma Warnings (Off);
2063 -- Following test may be compile time known on some targets
2066 (if Vector'Length - Iov_Count > SOSC.IOV_MAX
2068 else Vector'Length - Iov_Count);
2070 pragma Warnings (On);
2073 (Msg_Name => System.Null_Address,
2076 (Vector'First + Integer (Iov_Count))'Address,
2077 Msg_Iovlen => This_Iov_Count,
2078 Msg_Control => System.Null_Address,
2079 Msg_Controllen => 0,
2086 Set_Forced_Flags (To_Int (Flags)));
2088 if Res = ssize_t (Failure) then
2089 Raise_Socket_Error (Socket_Errno);
2092 Count := Count + Ada.Streams.Stream_Element_Count (Res);
2093 Iov_Count := Iov_Count + This_Iov_Count;
2101 procedure Set (Item : in out Socket_Set_Type; Socket : Socket_Type) is
2103 if Item.Last = No_Socket then
2105 -- Uninitialized socket set, make sure it is properly zeroed out
2107 Reset_Socket_Set (Item.Set'Access);
2108 Item.Last := Socket;
2110 elsif Item.Last < Socket then
2111 Item.Last := Socket;
2114 Insert_Socket_In_Set (Item.Set'Access, C.int (Socket));
2117 ----------------------
2118 -- Set_Forced_Flags --
2119 ----------------------
2121 function Set_Forced_Flags (F : C.int) return C.int is
2122 use type C.unsigned;
2123 function To_unsigned is
2124 new Ada.Unchecked_Conversion (C.int, C.unsigned);
2126 new Ada.Unchecked_Conversion (C.unsigned, C.int);
2128 return To_int (To_unsigned (F) or SOSC.MSG_Forced_Flags);
2129 end Set_Forced_Flags;
2131 -----------------------
2132 -- Set_Socket_Option --
2133 -----------------------
2135 procedure Set_Socket_Option
2136 (Socket : Socket_Type;
2137 Level : Level_Type := Socket_Level;
2138 Option : Option_Type)
2140 V8 : aliased Two_Ints;
2142 V1 : aliased C.unsigned_char;
2143 VT : aliased Timeval;
2145 Add : System.Address := Null_Address;
2154 V4 := C.int (Boolean'Pos (Option.Enabled));
2159 V8 (V8'First) := C.int (Boolean'Pos (Option.Enabled));
2160 V8 (V8'Last) := C.int (Option.Seconds);
2166 V4 := C.int (Option.Size);
2171 V4 := C.int (Boolean'Pos (True));
2175 when Add_Membership |
2177 V8 (V8'First) := To_Int (To_In_Addr (Option.Multicast_Address));
2178 V8 (V8'Last) := To_Int (To_In_Addr (Option.Local_Interface));
2182 when Multicast_If =>
2183 V4 := To_Int (To_In_Addr (Option.Outgoing_If));
2187 when Multicast_TTL =>
2188 V1 := C.unsigned_char (Option.Time_To_Live);
2192 when Multicast_Loop |
2193 Receive_Packet_Info =>
2194 V1 := C.unsigned_char (Boolean'Pos (Option.Enabled));
2200 VT := To_Timeval (Option.Timeout);
2209 Options (Option.Name),
2212 if Res = Failure then
2213 Raise_Socket_Error (Socket_Errno);
2215 end Set_Socket_Option;
2217 ----------------------
2218 -- Short_To_Network --
2219 ----------------------
2221 function Short_To_Network (S : C.unsigned_short) return C.unsigned_short is
2222 use type C.unsigned_short;
2225 -- Big-endian case. No conversion needed. On these platforms,
2226 -- htons() defaults to a null procedure.
2228 pragma Warnings (Off);
2229 -- Since the test can generate "always True/False" warning
2231 if Default_Bit_Order = High_Order_First then
2234 pragma Warnings (On);
2236 -- Little-endian case. We must swap the high and low bytes of this
2237 -- short to make the port number network compliant.
2240 return (S / 256) + (S mod 256) * 256;
2242 end Short_To_Network;
2244 ---------------------
2245 -- Shutdown_Socket --
2246 ---------------------
2248 procedure Shutdown_Socket
2249 (Socket : Socket_Type;
2250 How : Shutmode_Type := Shut_Read_Write)
2255 Res := C_Shutdown (C.int (Socket), Shutmodes (How));
2257 if Res = Failure then
2258 Raise_Socket_Error (Socket_Errno);
2260 end Shutdown_Socket;
2267 (Socket : Socket_Type;
2268 Send_To : Sock_Addr_Type) return Stream_Access
2270 S : Datagram_Socket_Stream_Access;
2273 S := new Datagram_Socket_Stream_Type;
2276 S.From := Get_Socket_Name (Socket);
2277 return Stream_Access (S);
2284 function Stream (Socket : Socket_Type) return Stream_Access is
2285 S : Stream_Socket_Stream_Access;
2287 S := new Stream_Socket_Stream_Type;
2289 return Stream_Access (S);
2296 procedure Stream_Write
2297 (Socket : Socket_Type;
2298 Item : Ada.Streams.Stream_Element_Array;
2299 To : access Sock_Addr_Type)
2301 First : Ada.Streams.Stream_Element_Offset;
2302 Index : Ada.Streams.Stream_Element_Offset;
2303 Max : constant Ada.Streams.Stream_Element_Offset := Item'Last;
2306 First := Item'First;
2308 while First <= Max loop
2309 Send_Socket (Socket, Item (First .. Max), Index, To);
2311 -- Exit when all or zero data sent. Zero means that the socket has
2312 -- been closed by peer.
2314 exit when Index < First or else Index = Max;
2319 -- For an empty array, we have First > Max, and hence Index >= Max (no
2320 -- error, the loop above is never executed). After a successful send,
2321 -- Index = Max. The only remaining case, Index < Max, is therefore
2322 -- always an actual send failure.
2325 Raise_Socket_Error (Socket_Errno);
2333 function To_C (Socket : Socket_Type) return Integer is
2335 return Integer (Socket);
2342 function To_Duration (Val : Timeval) return Timeval_Duration is
2344 return Natural (Val.Tv_Sec) * 1.0 + Natural (Val.Tv_Usec) * 1.0E-6;
2351 function To_Host_Entry (E : Hostent_Access) return Host_Entry_Type is
2355 Aliases_Count, Addresses_Count : Natural;
2357 -- H_Length is not used because it is currently only set to 4
2358 -- H_Addrtype is always AF_INET
2362 while Hostent_H_Alias (E, C.int (Aliases_Count)) /= Null_Address loop
2363 Aliases_Count := Aliases_Count + 1;
2366 Addresses_Count := 0;
2367 while Hostent_H_Addr (E, C.int (Addresses_Count)) /= Null_Address loop
2368 Addresses_Count := Addresses_Count + 1;
2371 return Result : Host_Entry_Type
2372 (Aliases_Length => Aliases_Count,
2373 Addresses_Length => Addresses_Count)
2375 Result.Official := To_Name (Value (Hostent_H_Name (E)));
2377 for J in Result.Aliases'Range loop
2378 Result.Aliases (J) :=
2379 To_Name (Value (Hostent_H_Alias
2380 (E, C.int (J - Result.Aliases'First))));
2383 for J in Result.Addresses'Range loop
2386 for Addr'Address use
2387 Hostent_H_Addr (E, C.int (J - Result.Addresses'First));
2388 pragma Import (Ada, Addr);
2390 To_Inet_Addr (Addr, Result.Addresses (J));
2400 function To_In_Addr (Addr : Inet_Addr_Type) return In_Addr is
2402 if Addr.Family = Family_Inet then
2403 return (S_B1 => C.unsigned_char (Addr.Sin_V4 (1)),
2404 S_B2 => C.unsigned_char (Addr.Sin_V4 (2)),
2405 S_B3 => C.unsigned_char (Addr.Sin_V4 (3)),
2406 S_B4 => C.unsigned_char (Addr.Sin_V4 (4)));
2409 raise Socket_Error with "IPv6 not supported";
2416 procedure To_Inet_Addr
2418 Result : out Inet_Addr_Type) is
2420 Result.Sin_V4 (1) := Inet_Addr_Comp_Type (Addr.S_B1);
2421 Result.Sin_V4 (2) := Inet_Addr_Comp_Type (Addr.S_B2);
2422 Result.Sin_V4 (3) := Inet_Addr_Comp_Type (Addr.S_B3);
2423 Result.Sin_V4 (4) := Inet_Addr_Comp_Type (Addr.S_B4);
2430 function To_Int (F : Request_Flag_Type) return C.int
2432 Current : Request_Flag_Type := F;
2433 Result : C.int := 0;
2436 for J in Flags'Range loop
2437 exit when Current = 0;
2439 if Current mod 2 /= 0 then
2440 if Flags (J) = -1 then
2441 Raise_Socket_Error (SOSC.EOPNOTSUPP);
2444 Result := Result + Flags (J);
2447 Current := Current / 2;
2457 function To_Name (N : String) return Name_Type is
2459 return Name_Type'(N'Length, N);
2462 ----------------------
2463 -- To_Service_Entry --
2464 ----------------------
2466 function To_Service_Entry (E : Servent_Access) return Service_Entry_Type is
2470 Aliases_Count : Natural;
2474 while Servent_S_Alias (E, C.int (Aliases_Count)) /= Null_Address loop
2475 Aliases_Count := Aliases_Count + 1;
2478 return Result : Service_Entry_Type (Aliases_Length => Aliases_Count) do
2479 Result.Official := To_Name (Value (Servent_S_Name (E)));
2481 for J in Result.Aliases'Range loop
2482 Result.Aliases (J) :=
2483 To_Name (Value (Servent_S_Alias
2484 (E, C.int (J - Result.Aliases'First))));
2487 Result.Protocol := To_Name (Value (Servent_S_Proto (E)));
2489 Port_Type (Network_To_Short (Servent_S_Port (E)));
2491 end To_Service_Entry;
2497 function To_String (HN : Name_Type) return String is
2499 return HN.Name (1 .. HN.Length);
2506 function To_Timeval (Val : Timeval_Duration) return Timeval is
2511 -- If zero, set result as zero (otherwise it gets rounded down to -1)
2517 -- Normal case where we do round down
2520 S := time_t (Val - 0.5);
2521 uS := suseconds_t (1_000_000 * (Val - Selector_Duration (S)));
2531 function Value (S : System.Address) return String is
2532 Str : String (1 .. Positive'Last);
2533 for Str'Address use S;
2534 pragma Import (Ada, Str);
2536 Terminator : Positive := Str'First;
2539 while Str (Terminator) /= ASCII.NUL loop
2540 Terminator := Terminator + 1;
2543 return Str (1 .. Terminator - 1);
2551 (Stream : in out Datagram_Socket_Stream_Type;
2552 Item : Ada.Streams.Stream_Element_Array)
2555 Stream_Write (Stream.Socket, Item, To => Stream.To'Unrestricted_Access);
2563 (Stream : in out Stream_Socket_Stream_Type;
2564 Item : Ada.Streams.Stream_Element_Array)
2567 Stream_Write (Stream.Socket, Item, To => null);
2570 Sockets_Library_Controller_Object : Sockets_Library_Controller;
2571 pragma Unreferenced (Sockets_Library_Controller_Object);
2572 -- The elaboration and finalization of this object perform the required
2573 -- initialization and cleanup actions for the sockets library.